DSSISynthesizer.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. /* DSSISynthesizer.java -- DSSI Synthesizer Provider
  2. Copyright (C) 2005, 2006, 2012 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.javax.sound.midi.dssi;
  32. import java.util.ArrayList;
  33. import java.util.Iterator;
  34. import java.util.List;
  35. import javax.sound.midi.Instrument;
  36. import javax.sound.midi.MidiChannel;
  37. import javax.sound.midi.MidiMessage;
  38. import javax.sound.midi.MidiUnavailableException;
  39. import javax.sound.midi.Patch;
  40. import javax.sound.midi.Receiver;
  41. import javax.sound.midi.ShortMessage;
  42. import javax.sound.midi.Soundbank;
  43. import javax.sound.midi.SoundbankResource;
  44. import javax.sound.midi.Synthesizer;
  45. import javax.sound.midi.Transmitter;
  46. import javax.sound.midi.VoiceStatus;
  47. /**
  48. * DSSI soft-synth support.
  49. *
  50. * All DSSI soft-synths are expected to be installed in /usr/lib/dssi.
  51. *
  52. * @author Anthony Green (green@redhat.com)
  53. *
  54. */
  55. public class DSSISynthesizer implements Synthesizer
  56. {
  57. /**
  58. * The DSSI Instrument class.
  59. *
  60. * @author Anthony Green (green@redhat.com)
  61. *
  62. */
  63. class DSSIInstrument extends Instrument
  64. {
  65. DSSIInstrument (Soundbank soundbank, Patch patch, String name)
  66. {
  67. super (soundbank, patch, name, null);
  68. }
  69. /* @see javax.sound.midi.SoundbankResource#getData()
  70. */
  71. public Object getData()
  72. {
  73. return null;
  74. }
  75. }
  76. /**
  77. * DSSISoundbank holds all instruments.
  78. *
  79. * @author Anthony Green (green@redhat.com)
  80. *
  81. */
  82. class DSSISoundbank implements Soundbank
  83. {
  84. private String name;
  85. private String description;
  86. private List<Instrument> instruments = new ArrayList<Instrument>();
  87. private List<SoundbankResource> resources = new ArrayList<SoundbankResource>();
  88. private String vendor;
  89. private String version;
  90. public DSSISoundbank(String name, String description, String vendor, String version)
  91. {
  92. this.name = name;
  93. this.description = description;
  94. this.vendor = vendor;
  95. this.version = version;
  96. }
  97. void add(Instrument instrument)
  98. {
  99. instruments.add(instrument);
  100. }
  101. /* @see javax.sound.midi.Soundbank#getName()
  102. */
  103. public String getName()
  104. {
  105. return name;
  106. }
  107. /* @see javax.sound.midi.Soundbank#getVersion()
  108. */
  109. public String getVersion()
  110. {
  111. return version;
  112. }
  113. /* @see javax.sound.midi.Soundbank#getVendor()
  114. */
  115. public String getVendor()
  116. {
  117. return vendor;
  118. }
  119. /* @see javax.sound.midi.Soundbank#getDescription()
  120. */
  121. public String getDescription()
  122. {
  123. return description;
  124. }
  125. /* @see javax.sound.midi.Soundbank#getResources()
  126. */
  127. public SoundbankResource[] getResources()
  128. {
  129. return resources.toArray(new SoundbankResource[resources.size()]);
  130. }
  131. /* @see javax.sound.midi.Soundbank#getInstruments()
  132. */
  133. public Instrument[] getInstruments()
  134. {
  135. return instruments.toArray(new Instrument[instruments.size()]);
  136. }
  137. /* @see javax.sound.midi.Soundbank#getInstrument(javax.sound.midi.Patch)
  138. */
  139. public Instrument getInstrument(Patch patch)
  140. {
  141. Iterator<Instrument> itr = instruments.iterator();
  142. while (itr.hasNext())
  143. {
  144. Instrument i = itr.next();
  145. if (i.getPatch().equals(patch))
  146. return i;
  147. }
  148. return null;
  149. }
  150. }
  151. /**
  152. * The Receiver class receives all MIDI messages from a connected
  153. * Transmitter.
  154. *
  155. * @author Anthony Green (green@redhat.com)
  156. *
  157. */
  158. class DSSIReceiver implements Receiver
  159. {
  160. /* (non-Javadoc)
  161. * @see javax.sound.midi.Receiver#send(javax.sound.midi.MidiMessage, long)
  162. */
  163. public void send(MidiMessage message, long timeStamp)
  164. throws IllegalStateException
  165. {
  166. if (message instanceof ShortMessage)
  167. {
  168. ShortMessage smessage = (ShortMessage) message;
  169. switch (message.getStatus())
  170. {
  171. case ShortMessage.NOTE_ON:
  172. int velocity = smessage.getData2();
  173. if (velocity > 0)
  174. channels[smessage.getChannel()].noteOn(smessage.getData1(),
  175. smessage.getData2());
  176. else
  177. channels[smessage.getChannel()].noteOff(smessage.getData1());
  178. break;
  179. case ShortMessage.CONTROL_CHANGE:
  180. channels[smessage.getChannel()].controlChange(smessage.getData1(),
  181. smessage.getData2());
  182. break;
  183. default:
  184. System.out.println ("Unhandled message: " + message.getStatus());
  185. break;
  186. }
  187. }
  188. }
  189. /* (non-Javadoc)
  190. * @see javax.sound.midi.Receiver#close()
  191. */
  192. public void close()
  193. {
  194. // TODO Auto-generated method stub
  195. }
  196. }
  197. static native void noteOn_(long handle, int channel, int noteNumber, int velocity);
  198. static native void noteOff_(long handle, int channel, int noteNumber, int velocity);
  199. static native void setPolyPressure_(long handle, int channel, int noteNumber, int pressure);
  200. static native int getPolyPressure_(long handle, int channel, int noteNumber);
  201. static native void controlChange_(long handle, int channel, int control, int value);
  202. static native void open_(long handle);
  203. static native void close_(long handle);
  204. static native String getProgramName_(long handle, int index);
  205. static native int getProgramBank_(long handle, int index);
  206. static native int getProgramProgram_(long handle, int index);
  207. static native void selectProgram_(long handle, int bank, int program);
  208. /**
  209. * @author Anthony Green (green@redhat.com)
  210. *
  211. */
  212. public class DSSIMidiChannel implements MidiChannel
  213. {
  214. int channel = 0;
  215. /**
  216. * Default contructor.
  217. */
  218. public DSSIMidiChannel(int channel)
  219. {
  220. super();
  221. this.channel = channel;
  222. }
  223. /* (non-Javadoc)
  224. * @see javax.sound.midi.MidiChannel#noteOn(int, int)
  225. */
  226. public void noteOn(int noteNumber, int velocity)
  227. {
  228. noteOn_(sohandle, channel, noteNumber, velocity);
  229. }
  230. /* (non-Javadoc)
  231. * @see javax.sound.midi.MidiChannel#noteOff(int, int)
  232. */
  233. public void noteOff(int noteNumber, int velocity)
  234. {
  235. noteOff_(sohandle, channel, noteNumber, velocity);
  236. }
  237. /* (non-Javadoc)
  238. * @see javax.sound.midi.MidiChannel#noteOff(int)
  239. */
  240. public void noteOff(int noteNumber)
  241. {
  242. noteOff_(sohandle, channel, noteNumber, -1);
  243. }
  244. /* (non-Javadoc)
  245. * @see javax.sound.midi.MidiChannel#setPolyPressure(int, int)
  246. */
  247. public void setPolyPressure(int noteNumber, int pressure)
  248. {
  249. setPolyPressure_(sohandle, channel, noteNumber, pressure);
  250. }
  251. /* (non-Javadoc)
  252. * @see javax.sound.midi.MidiChannel#getPolyPressure(int)
  253. */
  254. public int getPolyPressure(int noteNumber)
  255. {
  256. return getPolyPressure_(sohandle, channel, noteNumber);
  257. }
  258. /* (non-Javadoc)
  259. * @see javax.sound.midi.MidiChannel#setChannelPressure(int)
  260. */
  261. public void setChannelPressure(int pressure)
  262. {
  263. // TODO Auto-generated method stub
  264. }
  265. /* (non-Javadoc)
  266. * @see javax.sound.midi.MidiChannel#getChannelPressure()
  267. */
  268. public int getChannelPressure()
  269. {
  270. // TODO Auto-generated method stub
  271. return 0;
  272. }
  273. /* @see javax.sound.midi.MidiChannel#controlChange(int, int) */
  274. public void controlChange(int controller, int value)
  275. {
  276. controlChange_(sohandle, channel, controller, value);
  277. }
  278. /* (non-Javadoc)
  279. * @see javax.sound.midi.MidiChannel#getController(int)
  280. */
  281. public int getController(int controller)
  282. {
  283. // TODO Auto-generated method stub
  284. return 0;
  285. }
  286. /* (non-Javadoc)
  287. * @see javax.sound.midi.MidiChannel#programChange(int)
  288. */
  289. public void programChange(int program)
  290. {
  291. // TODO Auto-generated method stub
  292. }
  293. /* (non-Javadoc)
  294. * @see javax.sound.midi.MidiChannel#programChange(int, int)
  295. */
  296. public void programChange(int bank, int program)
  297. {
  298. // TODO Auto-generated method stub
  299. }
  300. /* (non-Javadoc)
  301. * @see javax.sound.midi.MidiChannel#getProgram()
  302. */
  303. public int getProgram()
  304. {
  305. // TODO Auto-generated method stub
  306. return 0;
  307. }
  308. /* (non-Javadoc)
  309. * @see javax.sound.midi.MidiChannel#setPitchBend(int)
  310. */
  311. public void setPitchBend(int bend)
  312. {
  313. // TODO Auto-generated method stub
  314. }
  315. /* (non-Javadoc)
  316. * @see javax.sound.midi.MidiChannel#getPitchBend()
  317. */
  318. public int getPitchBend()
  319. {
  320. // TODO Auto-generated method stub
  321. return 0;
  322. }
  323. /* (non-Javadoc)
  324. * @see javax.sound.midi.MidiChannel#resetAllControllers()
  325. */
  326. public void resetAllControllers()
  327. {
  328. // TODO Auto-generated method stub
  329. }
  330. /* (non-Javadoc)
  331. * @see javax.sound.midi.MidiChannel#allNotesOff()
  332. */
  333. public void allNotesOff()
  334. {
  335. // TODO Auto-generated method stub
  336. }
  337. /* (non-Javadoc)
  338. * @see javax.sound.midi.MidiChannel#allSoundOff()
  339. */
  340. public void allSoundOff()
  341. {
  342. // TODO Auto-generated method stub
  343. }
  344. /* (non-Javadoc)
  345. * @see javax.sound.midi.MidiChannel#localControl(boolean)
  346. */
  347. public boolean localControl(boolean on)
  348. {
  349. // TODO Auto-generated method stub
  350. return false;
  351. }
  352. /* (non-Javadoc)
  353. * @see javax.sound.midi.MidiChannel#setMono(boolean)
  354. */
  355. public void setMono(boolean on)
  356. {
  357. // TODO Auto-generated method stub
  358. }
  359. /* (non-Javadoc)
  360. * @see javax.sound.midi.MidiChannel#getMono()
  361. */
  362. public boolean getMono()
  363. {
  364. // TODO Auto-generated method stub
  365. return false;
  366. }
  367. /* (non-Javadoc)
  368. * @see javax.sound.midi.MidiChannel#setOmni(boolean)
  369. */
  370. public void setOmni(boolean on)
  371. {
  372. // TODO Auto-generated method stub
  373. }
  374. /* (non-Javadoc)
  375. * @see javax.sound.midi.MidiChannel#getOmni()
  376. */
  377. public boolean getOmni()
  378. {
  379. // TODO Auto-generated method stub
  380. return false;
  381. }
  382. /* (non-Javadoc)
  383. * @see javax.sound.midi.MidiChannel#setMute(boolean)
  384. */
  385. public void setMute(boolean mute)
  386. {
  387. // TODO Auto-generated method stub
  388. }
  389. /* (non-Javadoc)
  390. * @see javax.sound.midi.MidiChannel#getMute()
  391. */
  392. public boolean getMute()
  393. {
  394. // TODO Auto-generated method stub
  395. return false;
  396. }
  397. /* (non-Javadoc)
  398. * @see javax.sound.midi.MidiChannel#setSolo(boolean)
  399. */
  400. public void setSolo(boolean solo)
  401. {
  402. // TODO Auto-generated method stub
  403. }
  404. /* (non-Javadoc)
  405. * @see javax.sound.midi.MidiChannel#getSolo()
  406. */
  407. public boolean getSolo()
  408. {
  409. // TODO Auto-generated method stub
  410. return false;
  411. }
  412. }
  413. long sohandle;
  414. long handle;
  415. private Info info;
  416. MidiChannel channels[] = new MidiChannel[16];
  417. // The list of known soundbanks, and the default one.
  418. List<Soundbank> soundbanks = new ArrayList<Soundbank>();
  419. DSSISoundbank defaultSoundbank;
  420. /**
  421. * Create a DSSI Synthesizer.
  422. *
  423. * @param info the DSSIInfo for this soft-synth
  424. * @param soname the name of the .so file for this DSSI synth
  425. * @param index the DSSI index for this soft-synth
  426. */
  427. public DSSISynthesizer(Info info, String soname, long index)
  428. {
  429. super();
  430. this.info = info;
  431. sohandle = DSSIMidiDeviceProvider.dlopen_(soname);
  432. handle = DSSIMidiDeviceProvider.getDSSIHandle_(sohandle, index);
  433. channels[0] = new DSSIMidiChannel(0);
  434. defaultSoundbank = new DSSISoundbank("name", "description",
  435. "vendor", "version");
  436. soundbanks.add(defaultSoundbank);
  437. int i = 0;
  438. String name;
  439. do
  440. {
  441. name = getProgramName_(sohandle, i);
  442. if (name != null)
  443. {
  444. defaultSoundbank.
  445. add(new DSSIInstrument(defaultSoundbank,
  446. new Patch(getProgramBank_(sohandle, i),
  447. getProgramProgram_(sohandle, i)),
  448. name));
  449. i++;
  450. }
  451. } while (name != null);
  452. }
  453. /* (non-Javadoc)
  454. * @see javax.sound.midi.Synthesizer#getMaxPolyphony()
  455. */
  456. public int getMaxPolyphony()
  457. {
  458. // TODO Auto-generated method stub
  459. return 0;
  460. }
  461. /* (non-Javadoc)
  462. * @see javax.sound.midi.Synthesizer#getLatency()
  463. */
  464. public long getLatency()
  465. {
  466. // DSSI and LADSPA provide no way to determine the latency.
  467. // Let's just return 0 for now.
  468. return 0;
  469. }
  470. /* (non-Javadoc)
  471. * @see javax.sound.midi.Synthesizer#getChannels()
  472. */
  473. public MidiChannel[] getChannels()
  474. {
  475. return channels;
  476. }
  477. /* (non-Javadoc)
  478. * @see javax.sound.midi.Synthesizer#getVoiceStatus()
  479. */
  480. public VoiceStatus[] getVoiceStatus()
  481. {
  482. // TODO Auto-generated method stub
  483. return null;
  484. }
  485. /* (non-Javadoc)
  486. * @see javax.sound.midi.Synthesizer#isSoundbankSupported(javax.sound.midi.Soundbank)
  487. */
  488. public boolean isSoundbankSupported(Soundbank soundbank)
  489. {
  490. // TODO Auto-generated method stub
  491. return false;
  492. }
  493. /* @see javax.sound.midi.Synthesizer#loadInstrument(javax.sound.midi.Instrument)
  494. */
  495. public boolean loadInstrument(Instrument instrument)
  496. {
  497. // FIXME: perhaps this isn't quite right. It can probably
  498. // be in any soundbank.
  499. if (instrument.getSoundbank() != defaultSoundbank)
  500. throw new IllegalArgumentException ("Synthesizer doesn't support this instrument's soundbank");
  501. Patch patch = instrument.getPatch();
  502. selectProgram_(sohandle, patch.getBank(), patch.getProgram());
  503. return true;
  504. }
  505. /* (non-Javadoc)
  506. * @see javax.sound.midi.Synthesizer#unloadInstrument(javax.sound.midi.Instrument)
  507. */
  508. public void unloadInstrument(Instrument instrument)
  509. {
  510. // TODO Auto-generated method stub
  511. }
  512. /* (non-Javadoc)
  513. * @see javax.sound.midi.Synthesizer#remapInstrument(javax.sound.midi.Instrument, javax.sound.midi.Instrument)
  514. */
  515. public boolean remapInstrument(Instrument from, Instrument to)
  516. {
  517. // TODO Auto-generated method stub
  518. return false;
  519. }
  520. /* @see javax.sound.midi.Synthesizer#getDefaultSoundbank()
  521. */
  522. public Soundbank getDefaultSoundbank()
  523. {
  524. return defaultSoundbank;
  525. }
  526. /* @see javax.sound.midi.Synthesizer#getAvailableInstruments()
  527. */
  528. public Instrument[] getAvailableInstruments()
  529. {
  530. List<Instrument> instruments = new ArrayList<Instrument>();
  531. Iterator<Soundbank> itr = soundbanks.iterator();
  532. while (itr.hasNext())
  533. {
  534. Soundbank sb = itr.next();
  535. Instrument ins[] = sb.getInstruments();
  536. for (int i = 0; i < ins.length; i++)
  537. instruments.add(ins[i]);
  538. }
  539. return instruments.toArray(new Instrument[instruments.size()]);
  540. }
  541. /* (non-Javadoc)
  542. * @see javax.sound.midi.Synthesizer#getLoadedInstruments()
  543. */
  544. public Instrument[] getLoadedInstruments()
  545. {
  546. // TODO Auto-generated method stub
  547. return null;
  548. }
  549. /* (non-Javadoc)
  550. * @see javax.sound.midi.Synthesizer#loadAllInstruments(javax.sound.midi.Soundbank)
  551. */
  552. public boolean loadAllInstruments(Soundbank soundbank)
  553. {
  554. // TODO Auto-generated method stub
  555. return false;
  556. }
  557. /* (non-Javadoc)
  558. * @see javax.sound.midi.Synthesizer#unloadAllInstruments(javax.sound.midi.Soundbank)
  559. */
  560. public void unloadAllInstruments(Soundbank soundbank)
  561. {
  562. // TODO Auto-generated method stub
  563. }
  564. /* (non-Javadoc)
  565. * @see javax.sound.midi.Synthesizer#loadInstruments(javax.sound.midi.Soundbank, javax.sound.midi.Patch[])
  566. */
  567. public boolean loadInstruments(Soundbank soundbank, Patch[] patchList)
  568. {
  569. // TODO Auto-generated method stub
  570. return false;
  571. }
  572. /* (non-Javadoc)
  573. * @see javax.sound.midi.Synthesizer#unloadInstruments(javax.sound.midi.Soundbank, javax.sound.midi.Patch[])
  574. */
  575. public void unloadInstruments(Soundbank soundbank, Patch[] patchList)
  576. {
  577. // TODO Auto-generated method stub
  578. }
  579. /* @see javax.sound.midi.MidiDevice#getDeviceInfo()
  580. */
  581. public Info getDeviceInfo()
  582. {
  583. return info;
  584. }
  585. /* @see javax.sound.midi.MidiDevice#open()
  586. */
  587. public void open() throws MidiUnavailableException
  588. {
  589. open_(sohandle);
  590. }
  591. /* @see javax.sound.midi.MidiDevice#close()
  592. */
  593. public void close()
  594. {
  595. close_(sohandle);
  596. }
  597. /* (non-Javadoc)
  598. * @see javax.sound.midi.MidiDevice#isOpen()
  599. */
  600. public boolean isOpen()
  601. {
  602. // TODO Auto-generated method stub
  603. return false;
  604. }
  605. /* (non-Javadoc)
  606. * @see javax.sound.midi.MidiDevice#getMicrosecondPosition()
  607. */
  608. public long getMicrosecondPosition()
  609. {
  610. // TODO Auto-generated method stub
  611. return 0;
  612. }
  613. /* @see javax.sound.midi.MidiDevice#getMaxReceivers()
  614. */
  615. public int getMaxReceivers()
  616. {
  617. return 1;
  618. }
  619. /* @see javax.sound.midi.MidiDevice#getMaxTransmitters()
  620. */
  621. public int getMaxTransmitters()
  622. {
  623. return 0;
  624. }
  625. /* @see javax.sound.midi.MidiDevice#getReceiver()
  626. */
  627. public Receiver getReceiver() throws MidiUnavailableException
  628. {
  629. return new DSSIReceiver();
  630. }
  631. /* @see javax.sound.midi.MidiDevice#getTransmitter()
  632. */
  633. public Transmitter getTransmitter() throws MidiUnavailableException
  634. {
  635. return null;
  636. }
  637. }