1. /*
  2. * @(#)DataLine.java 1.27 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. * <code>DataLine</code> adds media-related functionality to its
  10. * superinterface, <code>{@link Line}</code>. This functionality includes
  11. * transport-control methods that start, stop, drain, and flush
  12. * the audio data that passes through the line. A data line can also
  13. * report the current position, volume, and audio format of the media.
  14. * Data lines are used for output of audio by means of the
  15. * subinterfaces <code>{@link SourceDataLine}</code> or
  16. * <code>{@link Clip}</code>, which allow an application program to write data. Similarly,
  17. * audio input is handled by the subinterface <code>{@link TargetDataLine}</code>,
  18. * which allows data to be read.
  19. * <p>
  20. * A data line has an internal buffer in which
  21. * the incoming or outgoing audio data is queued. The
  22. * <code>{@link #drain()}</code> method blocks until this internal buffer
  23. * becomes empty, usually because all queued data has been processed. The
  24. * <code>{@link #flush()}</code> method discards any available queued data
  25. * from the internal buffer.
  26. * <p>
  27. * A data line produces <code>{@link LineEvent.Type#START START}</code> and
  28. * <code>{@link LineEvent.Type#STOP STOP}</code> events whenever
  29. * it begins or ceases active presentation or capture of data. These events
  30. * can be generated in response to specific requests, or as a result of
  31. * less direct state changes. For example, if <code>{@link #start()}</code> is called
  32. * on an inactive data line, and data is available for capture or playback, a
  33. * <code>START</code> event will be generated shortly, when data playback
  34. * or capture actually begins. Or, if the flow of data to an active data
  35. * line is constricted so that a gap occurs in the presentation of data,
  36. * a <code>STOP</code> event is generated.
  37. * <p>
  38. * Mixers often support synchronized control of multiple data lines.
  39. * Synchronization can be established through the Mixer interface's
  40. * <code>{@link Mixer#synchronize synchronize}</code> method.
  41. * See the description of the <code>{@link Mixer Mixer}</code> interface
  42. * for a more complete description.
  43. *
  44. * @author Kara Kytle
  45. * @version 1.27, 03/01/23
  46. * @see LineEvent
  47. * @since 1.3
  48. */
  49. public interface DataLine extends Line {
  50. /**
  51. * Drains queued data from the line by continuing data I/O until the
  52. * data line's internal buffer has been emptied.
  53. * This method blocks until the draining is complete. Because this is a
  54. * blocking method, it should be used with care. If <code>drain()</code>
  55. * is invoked on a stopped line that has data in its queue, the method will
  56. * block until the line is running and the data queue becomes empty. If
  57. * <code>drain()</code> is invoked by one thread, and another continues to
  58. * fill the data queue, the operation will not complete.
  59. * This method always returns when the data line is closed.
  60. *
  61. * @see #flush()
  62. */
  63. public void drain();
  64. /**
  65. * Flushes queued data from the line. The flushed data is discarded.
  66. * In some cases, not all queued data can be discarded. For example, a
  67. * mixer can flush data from the buffer for a specific input line, but any
  68. * unplayed data already in the output buffer (the result of the mix) will
  69. * still be played. You can invoke this method after pausing a line (the
  70. * normal case) if you want to skip the "stale" data when you restart
  71. * playback or capture. (It is legal to flush a line that is not stopped,
  72. * but doing so on an active line is likely to cause a discontinuity in the
  73. * data, resulting in a perceptible click.)
  74. *
  75. * @see #stop()
  76. * @see #drain()
  77. */
  78. public void flush();
  79. /**
  80. * Allows a line to engage in data I/O. If invoked on a line
  81. * that is already running, this method does nothing. Unless the data in
  82. * the buffer has been flushed, the line resumes I/O starting
  83. * with the first frame that was unprocessed at the time the line was
  84. * stopped. When audio capture or playback starts, a
  85. * <code>{@link LineEvent.Type#START START}</code> event is generated.
  86. *
  87. * @see #stop()
  88. * @see #isRunning()
  89. * @see LineEvent
  90. */
  91. public void start();
  92. /**
  93. * Stops the line. A stopped line should cease I/O activity.
  94. * If the line is open and running, however, it should retain the resources required
  95. * to resume activity. A stopped line should retain any audio data in its buffer
  96. * instead of discarding it, so that upon resumption the I/O can continue where it left off,
  97. * if possible. (This doesn't guarantee that there will never be discontinuities beyond the
  98. * current buffer, of course; if the stopped condition continues
  99. * for too long, input or output samples might be dropped.) If desired, the retained data can be
  100. * discarded by invoking the <code>flush</code> method.
  101. * When audio capture or playback stops, a <code>{@link LineEvent.Type#STOP STOP}</code> event is generated.
  102. *
  103. * @see #start()
  104. * @see #isRunning()
  105. * @see #flush()
  106. * @see LineEvent
  107. */
  108. public void stop();
  109. /**
  110. * Indicates whether the line is running. The default is <code>false</code>.
  111. * An open line begins running when the first data is presented in response to an
  112. * invocation of the <code>start</code> method, and continues
  113. * until presentation ceases in response to a call to <code>stop</code> or
  114. * because playback completes.
  115. * @return <code>true</code> if the line is running, otherwise <code>false</code>
  116. * @see #start()
  117. * @see #stop()
  118. */
  119. public boolean isRunning();
  120. /**
  121. * Indicates whether the line is engaging in active I/O (such as playback
  122. * or capture). When an inactive line becomes active, it sends a
  123. * <code>{@link LineEvent.Type#START START}</code> event to its listeners. Similarly, when
  124. * an active line becomes inactive, it sends a
  125. * <code>{@link LineEvent.Type#STOP STOP}</code> event.
  126. * @return <code>true</code> if the line is actively capturing or rendering
  127. * sound, otherwise <code>false</code>
  128. * @see #isOpen
  129. * @see #addLineListener
  130. * @see #removeLineListener
  131. * @see LineEvent
  132. * @see LineListener
  133. */
  134. public boolean isActive();
  135. /**
  136. * Obtains the current format (encoding, sample rate, number of channels,
  137. * etc.) of the data line's audio data.
  138. * @return current audio data format
  139. * @see AudioFormat
  140. */
  141. public AudioFormat getFormat();
  142. /**
  143. * Obtains the maximum number of bytes of data that will fit in the data line's
  144. * internal buffer. For a source data line, this is the size of the buffer to
  145. * which data can be written. For a target data line, it is the size of
  146. * the buffer from which data can be read. Note that
  147. * the units used are bytes, but will always correspond to an integral
  148. * number of sample frames of audio data.
  149. *
  150. * @return the size of the buffer in bytes
  151. */
  152. /*
  153. * ISSUE: frames or bytes??
  154. * $$kk: 09.22.99: Should be bytes by analogy with read and write methods,
  155. * i should think!
  156. */
  157. public int getBufferSize();
  158. /**
  159. * Obtains the number of bytes of data currently available to the
  160. * application for processing in the data line's internal buffer. For a
  161. * source data line, this is the amount of data that can be written to the
  162. * buffer without blocking. For a target data line, this is the amount of data
  163. * available to be read by the application. For a clip, this value is always
  164. * 0 because the audio data is loaded into the buffer when the clip is opened,
  165. * and persists without modification until the clip is closed.
  166. * <p>
  167. * Note that the units used are bytes, but will always
  168. * correspond to an integral number of sample frames of audio data.
  169. * <p>
  170. * An application is guaranteed that a read or
  171. * write operation of up to the number of bytes returned from
  172. * <code>available()</code> will not block; however, there is no guarantee
  173. * that attempts to read or write more data will block.
  174. *
  175. * @return the amount of data available, in bytes
  176. */
  177. public int available();
  178. /**
  179. * Obtains the current position in the audio data, in sample frames.
  180. * The frame position measures the number of sample
  181. * frames captured by, or rendered from, the line since it was opened.
  182. * @return the number of frames already processed since the line was opened
  183. */
  184. public int getFramePosition();
  185. /**
  186. * Obtains the current position in the audio data, in microseconds.
  187. * The microsecond position measures the time corresponding to the number
  188. * of sample frames captured by, or rendered from, the line since it was opened.
  189. * The level of precision is not guaranteed. For example, an implementation
  190. * might calculate the microsecond position from the current frame position
  191. * and the audio sample frame rate. The precision in microseconds would
  192. * then be limited to the number of microseconds per sample frame.
  193. *
  194. * @return the number of microseconds of data processed since the line was opened
  195. */
  196. public long getMicrosecondPosition();
  197. /**
  198. * Obtains the current volume level for the line. This level is a measure
  199. * of the signal's current amplitude, and should not be confused with the
  200. * current setting of a gain control. The range is from 0.0 (silence) to
  201. * 1.0 (maximum possible amplitude for the sound waveform). The units
  202. * measure linear amplitude, not decibels.
  203. *
  204. * @return the current amplitude of the signal in this line, or
  205. * <code>{@link AudioSystem#NOT_SPECIFIED}</code>
  206. */
  207. public float getLevel();
  208. /**
  209. * Besides the class information inherited from its superclass,
  210. * <code>DataLine.Info</code> provides additional information specific to data lines.
  211. * This information includes:
  212. * <ul>
  213. * <li> the audio formats supported by the data line
  214. * <li> the minimum and maximum sizes of its internal buffer
  215. * </ul>
  216. * Because a <code>Line.Info</code> knows the class of the line its describes, a
  217. * <code>DataLine.Info</code> object can describe <code>DataLine</code>
  218. * subinterfaces such as <code>{@link SourceDataLine}</code>,
  219. * <code>{@link TargetDataLine}</code>, and <code>{@link Clip}</code>.
  220. * You can query a mixer for lines of any of these types, passing an appropriate
  221. * instance of <code>DataLine.Info</code> as the argument to a method such as
  222. * <code>{@link Mixer#getLine Mixer.getLine(Line.Info)}</code>.
  223. *
  224. * @see Line.Info
  225. * @author Kara Kytle
  226. * @version 1.27, 03/01/23
  227. * @since 1.3
  228. */
  229. public static class Info extends Line.Info {
  230. private AudioFormat[] formats;
  231. private int minBufferSize;
  232. private int maxBufferSize;
  233. /**
  234. * Constructs a data line's info object from the specified information,
  235. * which includes a set of supported audio formats and a range for the buffer size.
  236. * This constructor is typically used by mixer implementations
  237. * when returning information about a supported line.
  238. *
  239. * @param lineClass the class of the data line described by the info object
  240. * @param formats set of formats supported
  241. * @param minBufferSize minimum buffer size supported by the data line, in bytes
  242. * @param maxBufferSize maximum buffer size supported by the data line, in bytes
  243. */
  244. public Info(Class lineClass, AudioFormat[] formats, int minBufferSize, int maxBufferSize) {
  245. super(lineClass);
  246. if (formats == null) {
  247. this.formats = new AudioFormat[0];
  248. } else {
  249. this.formats = formats;
  250. }
  251. this.minBufferSize = minBufferSize;
  252. this.maxBufferSize = maxBufferSize;
  253. }
  254. /**
  255. * Constructs a data line's info object from the specified information,
  256. * which includes a single audio format and a desired buffer size.
  257. * This constructor is typically used by an application to
  258. * describe a desired line.
  259. *
  260. * @param lineClass the class of the data line described by the info object
  261. * @param format desired format
  262. * @param bufferSize desired buffer size in bytes
  263. */
  264. public Info(Class lineClass, AudioFormat format, int bufferSize) {
  265. super(lineClass);
  266. if (format == null) {
  267. this.formats = new AudioFormat[0];
  268. } else {
  269. AudioFormat[] formats = { format };
  270. this.formats = formats;
  271. }
  272. this.minBufferSize = bufferSize;
  273. this.maxBufferSize = bufferSize;
  274. }
  275. /**
  276. * Constructs a data line's info object from the specified information,
  277. * which includes a single audio format.
  278. * This constructor is typically used by an application to
  279. * describe a desired line.
  280. *
  281. * @param lineClass the class of the data line described by the info object
  282. * @param format desired format
  283. */
  284. public Info(Class lineClass, AudioFormat format) {
  285. this(lineClass, format, AudioSystem.NOT_SPECIFIED);
  286. }
  287. /**
  288. * Obtains a set of audio formats supported by the data line.
  289. * Note that <code>isFormatSupported(AudioFormat)</code> might return
  290. * <code>true</code> for certain additional formats that are missing from
  291. * the set returned by <code>getFormats()</code>. The reverse is not
  292. * the case: <code>isFormatSupported(AudioFormat)</code> is guaranteed to return
  293. * <code>true</code> for all formats returned by <code>getFormats()</code>.
  294. * @return a set of supported audio formats.
  295. * @see #isFormatSupported(AudioFormat)
  296. */
  297. public AudioFormat[] getFormats() {
  298. AudioFormat[] returnedArray = new AudioFormat[formats.length];
  299. System.arraycopy(formats, 0, returnedArray, 0, formats.length);
  300. return returnedArray;
  301. }
  302. /**
  303. * Indicates whether this data line supports a particular audio format.
  304. * The default implementation of this method simply returns <code>true</code> if
  305. * the specified format matches any of the supported formats.
  306. *
  307. * @param format the audio format for which support is queried.
  308. * @return <code>true</code> if the format is supported, otherwise <code>false</code>
  309. * @see #getFormats
  310. * @see AudioFormat#matches
  311. */
  312. public boolean isFormatSupported(AudioFormat format) {
  313. for (int i = 0; i < formats.length; i++) {
  314. if (format.matches(formats[i])) {
  315. return true;
  316. }
  317. }
  318. return false;
  319. }
  320. /**
  321. * Obtains the minimum buffer size supported by the data line.
  322. * @return minimum buffer size in bytes, or <code>AudioSystem.NOT_SPECIFIED</code>
  323. */
  324. public int getMinBufferSize() {
  325. return minBufferSize;
  326. }
  327. /**
  328. * Obtains the maximum buffer size supported by the data line.
  329. * @return maximum buffer size in bytes, or <code>AudioSystem.NOT_SPECIFIED</code>
  330. */
  331. public int getMaxBufferSize() {
  332. return maxBufferSize;
  333. }
  334. /**
  335. * Determines whether the specified info object matches this one.
  336. * To match, the superclass match requirements must be met. In
  337. * addition, this object's minimum buffer size must be at least as
  338. * large as that of the object specified, its maximum buffer size must
  339. * be at most as large as that of the object specified, and all of its
  340. * formats must match formats supported by the object specified.
  341. * @return <code>true</code> if this object matches the one specified,
  342. * otherwise <code>false</code>.
  343. */
  344. public boolean matches(Line.Info info) {
  345. if (! (super.matches(info)) ) {
  346. return false;
  347. }
  348. Info dataLineInfo = (Info)info;
  349. if ((getMaxBufferSize() != AudioSystem.NOT_SPECIFIED) && (dataLineInfo.getMaxBufferSize() != AudioSystem.NOT_SPECIFIED)) {
  350. if (getMaxBufferSize() > dataLineInfo.getMaxBufferSize()) {
  351. return false;
  352. }
  353. }
  354. if ((getMinBufferSize() != AudioSystem.NOT_SPECIFIED) && (dataLineInfo.getMinBufferSize() != AudioSystem.NOT_SPECIFIED)) {
  355. if (getMinBufferSize() < dataLineInfo.getMinBufferSize()) {
  356. return false;
  357. }
  358. }
  359. AudioFormat[] localFormats = getFormats();
  360. if (localFormats != null) {
  361. for (int i = 0; i < localFormats.length; i++) {
  362. if (! (localFormats[i] == null) ) {
  363. if (! (dataLineInfo.isFormatSupported(localFormats[i])) ) {
  364. return false;
  365. }
  366. }
  367. }
  368. }
  369. return true;
  370. }
  371. /**
  372. * Obtains a textual description of the data line info.
  373. * @return a string description
  374. */
  375. public String toString() {
  376. StringBuffer buf = new StringBuffer();
  377. if ( (formats.length == 1) && (formats[0] != null) ) {
  378. buf.append(" supporting format " + formats[0]);
  379. } else if (getFormats().length > 1) {
  380. buf.append(" supporting " + getFormats().length + " audio formats");
  381. }
  382. if ( (minBufferSize != AudioSystem.NOT_SPECIFIED) && (maxBufferSize != AudioSystem.NOT_SPECIFIED) ) {
  383. buf.append(", and buffers of " + minBufferSize + " to " + maxBufferSize + " bytes");
  384. } else if ( (minBufferSize != AudioSystem.NOT_SPECIFIED) && (minBufferSize > 0) ) {
  385. buf.append(", and buffers of at least " + minBufferSize + " bytes");
  386. } else if (maxBufferSize != AudioSystem.NOT_SPECIFIED) {
  387. buf.append(", and buffers of up to " + minBufferSize + " bytes");
  388. }
  389. return new String(super.toString() + buf);
  390. }
  391. } // class Info
  392. } // interface DataLine