1. /*
  2. * @(#)Line.java 1.25 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.sound.sampled;
  8. /**
  9. * The <code>Line</code> interface represents a mono or multi-channel
  10. * audio feed. A line is an element of the digital audio
  11. * "pipeline," such as a mixer, an input or output port,
  12. * or a data path into or out of a mixer.
  13. * <p>
  14. * A line can have controls, such as gain, pan, and reverb.
  15. * The controls themselves are instances of classes that extend the
  16. * base <code>{@link Control}</code> class.
  17. * The <code>Line</code> interface provides two accessor methods for
  18. * obtaining the line's controls: <code>{@link #getControls getControls}</code> returns the
  19. * entire set, and <code>{@link #getControl getControl}</code> returns a single control of
  20. * specified type.
  21. * <p>
  22. * Lines exist in various states at different times. When a line opens, it reserves system
  23. * resources for itself, and when it closes, these resources are freed for
  24. * other objects or applications. The <code>{@link #isOpen()}</code> method lets
  25. * you discover whether a line is open or closed.
  26. * An open line need not be processing data, however. Such processing is
  27. * typically initiated by subinterface methods such as
  28. * <code>{@link SourceDataLine#write SourceDataLine.write}</code> and
  29. * <code>{@link TargetDataLine#read TargetDataLine.read}</code>.
  30. *<p>
  31. * You can register an object to receive notifications whenever the line's
  32. * state changes. The object must implement the <code>{@link LineListener}</code>
  33. * interface, which consists of the single method
  34. * <code>{@link LineListener#update update}</code>.
  35. * This method will be invoked when a line opens and closes (and, if it's a
  36. * {@link DataLine}, when it starts and stops).
  37. *<p>
  38. * An object can be registered to listen to multiple lines. The event it
  39. * receives in its <code>update</code> method will specify which line created
  40. * the event, what type of event it was
  41. * (<code>OPEN</code>, <code>CLOSE</code>, <code>START</code>, or <code>STOP</code>),
  42. * and how many sample frames the line had processed at the time the event occurred.
  43. * <p>
  44. * Certain line operations, such as open and close, can generate security
  45. * exceptions if invoked by unprivileged code when the line is a shared audio
  46. * resource.
  47. *
  48. * @author Kara Kytle
  49. * @version 1.25, 03/01/23
  50. *
  51. * @see LineEvent
  52. * @since 1.3
  53. */
  54. public interface Line {
  55. /**
  56. * Obtains the <code>Line.Info</code> object describing this
  57. * line.
  58. * @return description of the line
  59. */
  60. public Line.Info getLineInfo();
  61. /**
  62. * Opens the line, indicating that it should acquire any required
  63. * system resources and become operational.
  64. * If this operation
  65. * succeeds, the line is marked as open, and an <code>OPEN</code> event is dispatched
  66. * to the line's listeners.
  67. * <p>
  68. * Note that some lines, once closed, cannot be reopened. Attempts
  69. * to reopen such a line will always result in an <code>LineUnavailableException</code>.
  70. * <p>
  71. * Some types of lines have configurable properties that may affect
  72. * resource allocation. For example, a <code>DataLine</code> must
  73. * be opened with a particular format and buffer size. Such lines
  74. * should provide a mechanism for configuring these properties, such
  75. * as an additional <code>open</code> method or methods which allow
  76. * an application to specify the desired settings.
  77. * <p>
  78. * This method takes no arguments, and opens the line with the current
  79. * settings. For <code>{@link SourceDataLine}</code> and
  80. * <code>{@link TargetDataLine}</code> objects, this means that the line is
  81. * opened with default settings. For a <code>{@link Clip}</code>, however,
  82. * the buffer size is determined when data is loaded. Since this method does not
  83. * allow the application to specify any data to load, it allocates
  84. * resources for a clip with zero frames of data, and there is no
  85. * means for subsequently loading data into that clip. Therefore,
  86. * you should instead use one of the <code>open</code> methods provided in
  87. * the <code>Clip</code> interface to load data into the <code>Clip</code>.
  88. *
  89. * @throws LineUnavailableException if the line cannot be
  90. * opened due to resource restrictions.
  91. * @throws SecurityException if the line cannot be
  92. * opened due to security restrictions.
  93. *
  94. * @see #close
  95. * @see #isOpen
  96. * @see LineEvent
  97. * @see DataLine
  98. * @see Clip#open(AudioFormat, byte[], int, int)
  99. * @see Clip#open(AudioInputStream)
  100. */
  101. public void open() throws LineUnavailableException;
  102. /**
  103. * Closes the line, indicating that any system resources
  104. * in use by the line can be released. If this operation
  105. * succeeds, the line is marked closed and a <code>CLOSE</code> event is dispatched
  106. * to the line's listeners.
  107. * @throws SecurityException if the line cannot be
  108. * closed due to security restrictions.
  109. *
  110. * @see #open
  111. * @see #isOpen
  112. * @see LineEvent
  113. */
  114. public void close();
  115. /**
  116. * Indicates whether the line is open, meaning that it has reserved
  117. * system resources and is operational, although it might not currently be
  118. * playing or capturing sound.
  119. * @return <code>true</code> if the line is open, otherwise <code>false</code>
  120. *
  121. * @see #open()
  122. * @see #close()
  123. */
  124. public boolean isOpen();
  125. /**
  126. * Obtains the set of controls associated with this line.
  127. * Some controls may only be available when the line is open.
  128. * If there are no controls, this method returns an array of length 0.
  129. * @return the array of controls
  130. * @see #getControl
  131. */
  132. public Control[] getControls();
  133. /**
  134. * Indicates whether the line supports a control of the specified type.
  135. * Some controls may only be available when the line is open.
  136. * @param control the type of the control for which support is queried
  137. * @return <code>true</code> if at least one control of the specified type is
  138. * supported, otherwise <code>false</code>.
  139. */
  140. public boolean isControlSupported(Control.Type control);
  141. /**
  142. * Obtains a control of the specified type,
  143. * if there is any.
  144. * Some controls may only be available when the line is open.
  145. * @param control the type of the requested control
  146. * @return a control of the specified type
  147. * @throws IllegalArgumentException if a control of the specified type
  148. * is not supported
  149. * @see #getControls
  150. * @see #isControlSupported(Control.Type control)
  151. */
  152. public Control getControl(Control.Type control);
  153. /**
  154. * Adds a listener to this line. Whenever the line's status changes, the
  155. * listener's <code>update()</code> method is called with a <code>LineEvent</code> object
  156. * that describes the change.
  157. * @param listener the object to add as a listener to this line
  158. * @see #removeLineListener
  159. * @see LineListener#update
  160. * @see LineEvent
  161. */
  162. public void addLineListener(LineListener listener);
  163. /**
  164. * Removes the specified listener from this line's list of listeners.
  165. * @param listener listener to remove
  166. * @see #addLineListener
  167. */
  168. public void removeLineListener(LineListener listener);
  169. /**
  170. * A <code>Line.Info</code> object contains information about a line.
  171. * The only information provided by <code>Line.Info</code> itself
  172. * is the Java class of the line.
  173. * A subclass of <code>Line.Info</code> adds other kinds of information
  174. * about the line. This additional information depends on which <code>Line</code>
  175. * subinterface is implemented by the kind of line that the <code>Line.Info</code>
  176. * subclass describes.
  177. * <p>
  178. * A <code>Line.Info</code> can be retrieved using various methods of
  179. * the <code>Line</code>, <code>Mixer</code>, and <code>AudioSystem</code>
  180. * interfaces. Other such methods let you pass a <code>Line.Info</code> as
  181. * an argument, to learn whether lines matching the specified configuration
  182. * are available and to obtain them.
  183. *
  184. * @author Kara Kytle
  185. * @version 1.25, 03/01/23
  186. *
  187. * @see Line#getLineInfo
  188. * @see Mixer#getSourceLineInfo
  189. * @see Mixer#getTargetLineInfo
  190. * @see Mixer#getLine <code>Mixer.getLine(Line.Info)</code>
  191. * @see Mixer#getSourceLineInfo(Line.Info) <code>Mixer.getSourceLineInfo(Line.Info)</code>
  192. * @see Mixer#getSourceLineInfo(Line.Info) <code>Mixer.getTargetLineInfo(Line.Info)</code>
  193. * @see Mixer#isLineSupported <code>Mixer.isLineSupported(Line.Info)</code>
  194. * @see AudioSystem#getLine <code>AudioSystem.getLine(Line.Info)</code>
  195. * @see AudioSystem#getSourceLineInfo <code>AudioSystem.getSourceLineInfo(Line.Info)</code>
  196. * @see AudioSystem#getTargetLineInfo <code>AudioSystem.getTargetLineInfo(Line.Info)</code>
  197. * @see AudioSystem#isLineSupported <code>AudioSystem.isLineSupported(Line.Info)</code>
  198. * @since 1.3
  199. */
  200. public static class Info {
  201. /**
  202. * The class of the line described by the info object.
  203. */
  204. private final Class lineClass;
  205. /**
  206. * Constructs an info object that describes a line of the specified class.
  207. * This constructor is typically used by an application to
  208. * describe a desired line.
  209. * @param lineClass the class of the line that the new Line.Info object describes
  210. */
  211. public Info(Class lineClass) {
  212. if (lineClass == null) {
  213. this.lineClass = Line.class;
  214. } else {
  215. this.lineClass = lineClass;
  216. }
  217. }
  218. /**
  219. * Obtains the class of the line that this Line.Info object describes.
  220. * @return the described line's class
  221. */
  222. public Class getLineClass() {
  223. return lineClass;
  224. }
  225. /**
  226. * Indicates whether the specified info object matches this one.
  227. * To match, the specified object must be identical to or
  228. * a special case of this one. The specified info object
  229. * must be either an instance of the same class as this one,
  230. * or an instance of a sub-type of this one. In addition, the
  231. * attributes of the specified object must be compatible with the
  232. * capabilities of this one. Specifically, the routing configuration
  233. * for the specified info object must be compatible with that of this
  234. * one.
  235. * Subclasses may add other criteria to determine whether the two objects
  236. * match.
  237. *
  238. * @param info the info object which is being compared to this one
  239. * @return <code>true</code> if the specified object matches this one,
  240. * <code>false</code> otherwise
  241. */
  242. public boolean matches(Info info) {
  243. // $$kk: 08.30.99: is this backwards?
  244. // dataLine.matches(targetDataLine) == true: targetDataLine is always dataLine
  245. // targetDataLine.matches(dataLine) == false
  246. // so if i want to make sure i get a targetDataLine, i need:
  247. // targetDataLine.matches(prospective_match) == true
  248. // => prospective_match may be other things as well, but it is at least a targetDataLine
  249. // targetDataLine defines the requirements which prospective_match must meet.
  250. // "if this Class object represents a declared class, this method returns
  251. // true if the specified Object argument is an instance of the represented
  252. // class (or of any of its subclasses)"
  253. // GainControlClass.isInstance(MyGainObj) => true
  254. // GainControlClass.isInstance(MySpecialGainInterfaceObj) => true
  255. // this_class.isInstance(that_object) => that object can by cast to this class
  256. // => that_object's class may be a subtype of this_class
  257. // => that may be more specific (subtype) of this
  258. // "If this Class object represents an interface, this method returns true
  259. // if the class or any superclass of the specified Object argument implements
  260. // this interface"
  261. // GainControlClass.isInstance(MyGainObj) => true
  262. // GainControlClass.isInstance(GenericControlObj) => may be false
  263. // => that may be more specific
  264. if (! (this.getClass().isInstance(info)) ) {
  265. return false;
  266. }
  267. // this.isAssignableFrom(that) => this is same or super to that
  268. // => this is at least as general as that
  269. // => that may be subtype of this
  270. if (! (getLineClass().isAssignableFrom(info.getLineClass())) ) {
  271. return false;
  272. }
  273. return true;
  274. }
  275. /**
  276. * Obtains a textual description of the line info.
  277. * @return a string description
  278. */
  279. public String toString() {
  280. String fullPackagePath = "javax.sound.sampled.";
  281. String initialString = new String(getLineClass().toString());
  282. String finalString;
  283. int index = initialString.indexOf(fullPackagePath);
  284. if (index != -1) {
  285. finalString = initialString.substring(0, index) + initialString.substring( (index + fullPackagePath.length()), initialString.length() );
  286. } else {
  287. finalString = initialString;
  288. }
  289. return finalString;
  290. }
  291. } // class Info
  292. } // interface Line