1. /*
  2. * @(#)TargetDataLine.java 1.20 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.sampled;
  8. /**
  9. * A target data line is a type of <code>{@link DataLine}</code> from which
  10. * audio data can be read. The most common example is a data line that gets
  11. * its data from an audio capture device. (The device is implemented as a
  12. * mixer that writes to the target data line.)
  13. * <p>
  14. * Note that the naming convention for this interface reflects the relationship
  15. * between the line and its mixer. From the perspective of an application,
  16. * a target data line may act as a source for audio data.
  17. * <p>
  18. * The target data line can be obtained from a mixer by invoking the
  19. * <code>{@link Mixer#getLine getLine}</code>
  20. * method of <code>Mixer</code> with an appropriate
  21. * <code>{@link DataLine.Info}</code> object.
  22. * <p>
  23. * The <code>TargetDataLine</code> interface provides a method for reading the
  24. * captured data from the target data line's buffer.Applications
  25. * that record audio should read data from the target data line quickly enough
  26. * to keep the buffer from overflowing, which could cause discontinuities in
  27. * the captured data that are perceived as clicks. Applications can use the
  28. * <code>{@link DataLine#available available}</code> method defined in the
  29. * <code>DataLine</code> interface to determine the amount of data currently
  30. * queued in the data line's buffer. If the buffer does overflow,
  31. * the oldest queued data is discarded and replaced by new data.
  32. *
  33. * @author Kara Kytle
  34. * @version 1.20 03/12/19
  35. * @see Mixer
  36. * @see DataLine
  37. * @see SourceDataLine
  38. * @since 1.3
  39. */
  40. public interface TargetDataLine extends DataLine {
  41. /**
  42. * Opens the line with the specified format and requested buffer size,
  43. * causing the line to acquire any required system resources and become
  44. * operational.
  45. * <p>
  46. * The buffer size is specified in bytes, but must represent an integral
  47. * number of sample frames. Invoking this method with a requested buffer
  48. * size that does not meet this requirement may result in an
  49. * IllegalArgumentException. The actual buffer size for the open line may
  50. * differ from the requested buffer size. The value actually set may be
  51. * queried by subsequently calling <code>{@link DataLine#getBufferSize}</code>
  52. * <p>
  53. * If this operation succeeds, the line is marked as open, and an
  54. * <code>{@link LineEvent.Type#OPEN OPEN}</code> event is dispatched to the
  55. * line's listeners.
  56. * <p>
  57. * Invoking this method on a line that is already open is illegal
  58. * and may result in an <code>IllegalStateException</code>.
  59. * <p>
  60. * Some lines, once closed, cannot be reopened. Attempts
  61. * to reopen such a line will always result in a
  62. * <code>LineUnavailableException</code>.
  63. *
  64. * @param format the desired audio format
  65. * @param bufferSize the desired buffer size, in bytes.
  66. * @throws LineUnavailableException if the line cannot be
  67. * opened due to resource restrictions
  68. * @throws IllegalArgumentException if the buffer size does not represent
  69. * an integral number of sample frames,
  70. * or if <code>format</code> is not fully specified or invalid
  71. * @throws IllegalStateException if the line is already open
  72. * @throws SecurityException if the line cannot be
  73. * opened due to security restrictions
  74. *
  75. * @see #open(AudioFormat)
  76. * @see Line#open
  77. * @see Line#close
  78. * @see Line#isOpen
  79. * @see LineEvent
  80. */
  81. public void open(AudioFormat format, int bufferSize) throws LineUnavailableException;
  82. /**
  83. * Opens the line with the specified format, causing the line to acquire any
  84. * required system resources and become operational.
  85. *
  86. * <p>
  87. * The implementation chooses a buffer size, which is measured in bytes but
  88. * which encompasses an integral number of sample frames. The buffer size
  89. * that the system has chosen may be queried by subsequently calling <code>{@link DataLine#getBufferSize}</code>
  90. * <p>
  91. * If this operation succeeds, the line is marked as open, and an
  92. * <code>{@link LineEvent.Type#OPEN OPEN}</code> event is dispatched to the
  93. * line's listeners.
  94. * <p>
  95. * Invoking this method on a line that is already open is illegal
  96. * and may result in an <code>IllegalStateException</code>.
  97. * <p>
  98. * Some lines, once closed, cannot be reopened. Attempts
  99. * to reopen such a line will always result in a
  100. * <code>LineUnavailableException</code>.
  101. *
  102. * @param format the desired audio format
  103. * @throws LineUnavailableException if the line cannot be
  104. * opened due to resource restrictions
  105. * @throws IllegalArgumentException if <code>format</code>
  106. * is not fully specified or invalid
  107. * @throws IllegalStateException if the line is already open
  108. * @throws SecurityException if the line cannot be
  109. * opened due to security restrictions
  110. *
  111. * @see #open(AudioFormat, int)
  112. * @see Line#open
  113. * @see Line#close
  114. * @see Line#isOpen
  115. * @see LineEvent
  116. */
  117. public void open(AudioFormat format) throws LineUnavailableException;
  118. /**
  119. * Reads audio data from the data line's input buffer. The requested
  120. * number of bytes is read into the specified array, starting at
  121. * the specified offset into the array in bytes. This method blocks until
  122. * the requested amount of data has been read. However, if the data line
  123. * is closed, stopped, drained, or flushed before the requested amount has
  124. * been read, the method no longer blocks, but returns the number of bytes
  125. * read thus far.
  126. * <p>
  127. * The number of bytes that can be read without blocking can be ascertained
  128. * using the <code>{@link DataLine#available available}</code> method of the
  129. * <code>DataLine</code> interface. (While it is guaranteed that
  130. * this number of bytes can be read without blocking, there is no guarantee
  131. * that attempts to read additional data will block.)
  132. * <p>
  133. * The number of bytes to be read must represent an integral number of
  134. * sample frames, such that:
  135. * <br>
  136. * <center><code>[ bytes read ] % [frame size in bytes ] == 0</code></center>
  137. * <br>
  138. * The return value will always meet this requirement. A request to read a
  139. * number of bytes representing a non-integral number of sample frames cannot
  140. * be fulfilled and may result in an IllegalArgumentException.
  141. *
  142. * @param b a byte array that will contain the requested input data when
  143. * this method returns
  144. * @param off the offset from the beginning of the array, in bytes
  145. * @param len the requested number of bytes to read
  146. * @return the number of bytes actually read
  147. * @throws IllegalArgumentException if the requested number of bytes does
  148. * not represent an integral number of sample frames.
  149. * or if <code>len</code> is negative.
  150. * @throws ArrayIndexOutOfBoundsException if <code>off</code> is negative,
  151. * or <code>off+len</code> is greater than the length of the array
  152. * <code>b</code>.
  153. *
  154. * @see SourceDataLine#write
  155. * @see DataLine#available
  156. */
  157. public int read(byte[] b, int off, int len);
  158. /**
  159. * Obtains the number of sample frames of audio data that can be read from
  160. * the target data line without blocking. Note that the return value
  161. * measures sample frames, not bytes.
  162. * @return the number of sample frames currently available for reading
  163. * @see SourceDataLine#availableWrite
  164. */
  165. //public int availableRead();
  166. }