1. /*
  2. * @(#)MidiDevice.java 1.38 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.sound.midi;
  8. import java.util.List;
  9. /**
  10. * <code>MidiDevice</code> is the base interface for all MIDI devices.
  11. * Common devices include synthesizers, sequencers, MIDI input ports, and MIDI
  12. * output ports.
  13. *
  14. * <p>A <code>MidiDevice</code> can be a transmitter or a receiver of
  15. * MIDI events, or both. Therefore, it can provide {@link Transmitter}
  16. * or {@link Receiver} instances (or both). Typically, MIDI IN ports
  17. * provide transmitters, MIDI OUT ports and synthesizers provide
  18. * receivers. A Sequencer typically provides transmitters for playback
  19. * and receivers for recording.
  20. *
  21. * <p>A <code>MidiDevice</code> can be opened and closed explicitly as
  22. * well as implicitly. Explicit opening is accomplished by calling
  23. * {@link #open}, explicit closing is done by calling {@link
  24. * #close} on the <code>MidiDevice</code> instance.
  25. * If an application opens a <code>MidiDevice</code>
  26. * explicitly, it has to close it explicitly to free system resources
  27. * and enable the application to exit cleanly. Implicit opening is
  28. * done by calling {@link javax.sound.midi.MidiSystem#getReceiver
  29. * MidiSystem.getReceiver} and {@link
  30. * javax.sound.midi.MidiSystem#getTransmitter
  31. * MidiSystem.getTransmitter}. The <code>MidiDevice</code> used by
  32. * <code>MidiSystem.getReceiver</code> and
  33. * <code>MidiSystem.getTransmitter</code> is implementation-dependant
  34. * unless the properties <code>javax.sound.midi.Receiver</code>
  35. * and <code>javax.sound.midi.Transmitter</code> are used (see the
  36. * description of properties to select default providers in
  37. * {@link javax.sound.midi.MidiSystem}). A <code>MidiDevice</code>
  38. * that was opened implicitly, is closed implicitly by closing the
  39. * <code>Receiver</code> or <code>Transmitter</code> that resulted in
  40. * opening it. If more than one implicitly opening
  41. * <code>Receiver</code> or <code>Transmitter</code> were obtained by
  42. * the application, the decive is closed after the last
  43. * <code>Receiver</code> or <code>Transmitter</code> has been
  44. * closed. On the other hand, calling <code>getReceiver</code> or
  45. * <code>getTransmitter</code> on the device instance directly does
  46. * not open the device implicitly. Closing these
  47. * <code>Transmitter</code>s and <code>Receiver</code>s does not close
  48. * the device implicitly. To use a device with <code>Receiver</code>s
  49. * or <code>Transmitter</code>s obtained this way, the device has to
  50. * be opened and closed explicitly.
  51. *
  52. * <p>If implicit and explicit opening and closing are mixed on the
  53. * same <code>MidiDevice</code> instance, the following rules apply:
  54. *
  55. * <ul>
  56. * <li>After an explicit open (either before or after implicit
  57. * opens), the device will not be closed by implicit closing. The only
  58. * way to close an explicitly opened device is an explicit close.</li>
  59. *
  60. * <li>An explicit close always closes the device, even if it also has
  61. * been opened implicitly. A subsequent implicit close has no further
  62. * effect.</li>
  63. * </ul>
  64. *
  65. * To detect if a MidiDevice represents a hardware MIDI port, the
  66. * following programming technique can be used:
  67. *
  68. * <pre>
  69. * MidiDevice device = ...;
  70. * if ( ! (device instanceof Sequencer) && ! (device instanceof Synthesizer)) {
  71. * // we're now sure that device represents a MIDI port
  72. * // ...
  73. * }
  74. * </pre>
  75. *
  76. * <p>
  77. * A <code>MidiDevice</code> includes a <code>{@link MidiDevice.Info}</code> object
  78. * to provide manufacturer information and so on.
  79. *
  80. * @see Synthesizer
  81. * @see Sequencer
  82. * @see Receiver
  83. * @see Transmitter
  84. *
  85. * @version 1.38, 03/12/19
  86. * @author Kara Kytle
  87. * @author Florian Bomers
  88. */
  89. public interface MidiDevice {
  90. /**
  91. * Obtains information about the device, including its Java class and
  92. * <code>Strings</code> containing its name, vendor, and description.
  93. *
  94. * @return device info
  95. */
  96. public Info getDeviceInfo();
  97. /**
  98. * Opens the device, indicating that it should now acquire any
  99. * system resources it requires and become operational.
  100. *
  101. * <p>An application opening a device explicitly with this call
  102. * has to close the device by calling {@link #close}. This is
  103. * necessary to release system resources and allow applications to
  104. * exit cleanly.
  105. *
  106. * <p>
  107. * Note that some devices, once closed, cannot be reopened. Attempts
  108. * to reopen such a device will always result in a MidiUnavailableException.
  109. *
  110. * @throws MidiUnavailableException thrown if the device cannot be
  111. * opened due to resource restrictions.
  112. * @throws SecurityException thrown if the device cannot be
  113. * opened due to security restrictions.
  114. *
  115. * @see #close
  116. * @see #isOpen
  117. */
  118. public void open() throws MidiUnavailableException;
  119. /**
  120. * Closes the device, indicating that the device should now release
  121. * any system resources it is using.
  122. *
  123. * <p>All <code>Receiver</code> and <code>Transmitter</code> instances
  124. * open from this device are closed. This includes instances retrieved
  125. * via <code>MidiSystem</code>.
  126. *
  127. * @see #open
  128. * @see #isOpen
  129. */
  130. public void close();
  131. /**
  132. * Reports whether the device is open.
  133. *
  134. * @return <code>true</code> if the device is open, otherwise
  135. * <code>false</code>
  136. * @see #open
  137. * @see #close
  138. */
  139. public boolean isOpen();
  140. /**
  141. * Obtains the current time-stamp of the device, in microseconds.
  142. * If a device supports time-stamps, it should start counting at
  143. * 0 when the device is opened and continue incrementing its
  144. * time-stamp in microseconds until the device is closed.
  145. * If it does not support time-stamps, it should always return
  146. * -1.
  147. * @return the current time-stamp of the device in microseconds,
  148. * or -1 if time-stamping is not supported by the device.
  149. */
  150. public long getMicrosecondPosition();
  151. /**
  152. * Obtains the maximum number of MIDI IN connections available on this
  153. * MIDI device for receiving MIDI data.
  154. * @return maximum number of MIDI IN connections,
  155. * or -1 if an unlimited number of connections is available.
  156. */
  157. public int getMaxReceivers();
  158. /**
  159. * Obtains the maximum number of MIDI OUT connections available on this
  160. * MIDI device for transmitting MIDI data.
  161. * @return maximum number of MIDI OUT connections,
  162. * or -1 if an unlimited number of connections is available.
  163. */
  164. public int getMaxTransmitters();
  165. /**
  166. * Obtains a MIDI IN receiver through which the MIDI device may receive
  167. * MIDI data. The returned receiver must be closed when the application
  168. * has finished using it.
  169. *
  170. * <p>Obtaining a <code>Receiver</code> with this method does not
  171. * open the device. To be able to use the device, it has to be
  172. * opened explicitly by calling {@link #open}. Also, closing the
  173. * <code>Receiver</code> does not close the device. It has to be
  174. * closed explicitly by calling {@link #close}.
  175. *
  176. * @return a receiver for the device.
  177. * @throws MidiUnavailableException thrown if a receiver is not available
  178. * due to resource restrictions
  179. * @see Receiver#close()
  180. */
  181. public Receiver getReceiver() throws MidiUnavailableException;
  182. /**
  183. * Returns all currently active, non-closed receivers
  184. * connected with this MidiDevice.
  185. * A receiver can be removed
  186. * from the device by closing it.
  187. * @return an unmodifiable list of the open receivers
  188. * @since 1.5
  189. */
  190. List<Receiver> getReceivers();
  191. /**
  192. * Obtains a MIDI OUT connection from which the MIDI device will transmit
  193. * MIDI data The returned transmitter must be closed when the application
  194. * has finished using it.
  195. *
  196. * <p>Obtaining a <code>Transmitter</code> with this method does not
  197. * open the device. To be able to use the device, it has to be
  198. * opened explicitly by calling {@link #open}. Also, closing the
  199. * <code>Transmitter</code> does not close the device. It has to be
  200. * closed explicitly by calling {@link #close}.
  201. *
  202. * @return a MIDI OUT transmitter for the device.
  203. * @throws MidiUnavailableException thrown if a transmitter is not available
  204. * due to resource restrictions
  205. * @see Transmitter#close()
  206. */
  207. public Transmitter getTransmitter() throws MidiUnavailableException;
  208. /**
  209. * Returns all currently active, non-closed transmitters
  210. * connected with this MidiDevice.
  211. * A transmitter can be removed
  212. * from the device by closing it.
  213. * @return an unmodifiable list of the open transmitters
  214. * @since 1.5
  215. */
  216. List<Transmitter> getTransmitters();
  217. /**
  218. * A <code>MidiDevice.Info</code> object contains assorted
  219. * data about a <code>{@link MidiDevice}</code>, including its
  220. * name, the company who created it, and descriptive text.
  221. *
  222. * @see MidiDevice#getDeviceInfo
  223. */
  224. public static class Info {
  225. /**
  226. * The device's name.
  227. */
  228. private String name;
  229. /**
  230. * The name of the company who provides the device.
  231. */
  232. private String vendor;
  233. /**
  234. * A description of the device.
  235. */
  236. private String description;
  237. /**
  238. * Device version.
  239. */
  240. private String version;
  241. /**
  242. * Constructs a device info object.
  243. *
  244. * @param name the name of the device
  245. * @param vendor the name of the company who provides the device
  246. * @param description a description of the device
  247. * @param version version information for the device
  248. */
  249. protected Info(String name, String vendor, String description, String version) {
  250. this.name = name;
  251. this.vendor = vendor;
  252. this.description = description;
  253. this.version = version;
  254. }
  255. /**
  256. * Reports whether two objects are equal.
  257. * Returns <code>true</code> if the objects are identical.
  258. * @param obj the reference object with which to compare this
  259. * object
  260. * @return <code>true</code> if this object is the same as the
  261. * <code>obj</code> argument; <code>false</code> otherwise
  262. */
  263. public final boolean equals(Object obj) {
  264. return super.equals(obj);
  265. }
  266. /**
  267. * Finalizes the hashcode method.
  268. */
  269. public final int hashCode() {
  270. return super.hashCode();
  271. }
  272. /**
  273. * Obtains the name of the device.
  274. *
  275. * @return a string containing the device's name
  276. */
  277. public final String getName() {
  278. return name;
  279. }
  280. /**
  281. * Obtains the name of the company who supplies the device.
  282. * @return device the vendor's name
  283. */
  284. public final String getVendor() {
  285. return vendor;
  286. }
  287. /**
  288. * Obtains the description of the device.
  289. * @return a description of the device
  290. */
  291. public final String getDescription() {
  292. return description;
  293. }
  294. /**
  295. * Obtains the version of the device.
  296. * @return textual version information for the device.
  297. */
  298. public final String getVersion() {
  299. return version;
  300. }
  301. /**
  302. * Provides a string representation of the device information.
  303. * @return a description of the info object
  304. */
  305. public final String toString() {
  306. return name;
  307. }
  308. } // class Info
  309. }