1. /*
  2. * @(#)TargetDataLine.java 1.17 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. * 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.17 03/01/23
  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. * @throws IllegalStateException if the line is already open
  71. * @throws SecurityException if the line cannot be
  72. * opened due to security restrictions
  73. *
  74. * @see #open(AudioFormat)
  75. * @see Line#open
  76. * @see Line#close
  77. * @see Line#isOpen
  78. * @see LineEvent
  79. */
  80. public void open(AudioFormat format, int bufferSize) throws LineUnavailableException;
  81. /**
  82. * Opens the line with the specified format, causing the line to acquire any
  83. * required system resources and become operational.
  84. *
  85. * <p>
  86. * The implementation chooses a buffer size, which is measured in bytes but
  87. * which encompasses an integral number of sample frames. The buffer size
  88. * that the system has chosen may be queried by subsequently calling <code>{@link DataLine#getBufferSize}</code>
  89. * <p>
  90. * If this operation succeeds, the line is marked as open, and an
  91. * <code>{@link LineEvent.Type#OPEN OPEN}</code> event is dispatched to the
  92. * line's listeners.
  93. * <p>
  94. * Invoking this method on a line that is already open is illegal
  95. * and may result in an <code>IllegalStateException</code>.
  96. * <p>
  97. * Some lines, once closed, cannot be reopened. Attempts
  98. * to reopen such a line will always result in a
  99. * <code>LineUnavailableException</code>.
  100. *
  101. * @param format the desired audio format
  102. * @throws LineUnavailableException if the line cannot be
  103. * opened due to resource restrictions
  104. * @throws IllegalStateException if the line is already open
  105. * @throws SecurityException if the line cannot be
  106. * opened due to security restrictions
  107. *
  108. * @see #open(AudioFormat, int)
  109. * @see Line#open
  110. * @see Line#close
  111. * @see Line#isOpen
  112. * @see LineEvent
  113. */
  114. public void open(AudioFormat format) throws LineUnavailableException;
  115. /**
  116. * Reads audio data from the data line's input buffer. The requested
  117. * number of bytes is read into the specified array, starting at
  118. * the specified offset into the array in bytes. This method blocks until
  119. * the requested amount of data has been read. However, if the data line
  120. * is closed, stopped, drained, or flushed before the requested amount has
  121. * been read, the method no longer blocks, but returns the number of bytes
  122. * read thus far.
  123. * <p>
  124. * The number of bytes that can be read without blocking can be ascertained
  125. * using the <code>{@link DataLine#available available}</code> method of the
  126. * <code>DataLine</code> interface. (While it is guaranteed that
  127. * this number of bytes can be read without blocking, there is no guarantee
  128. * that attempts to read additional data will block.)
  129. * <p>
  130. * The number of bytes to be read must represent an integral number of
  131. * sample frames, such that:
  132. * <br>
  133. * <center><code>[ bytes read ] % [frame size in bytes ] == 0</code></center>
  134. * <br>
  135. * The return value will always meet this requirement. A request to read a
  136. * number of bytes representing a non-integral number of sample frames cannot
  137. * be fulfilled and may result in an IllegalArgumentException.
  138. *
  139. * @param b a byte array that will contain the requested input data when
  140. * this method returns
  141. * @param off the offset from the beginning of the array, in bytes
  142. * @param len the requested number of bytes to read
  143. * @return the number of bytes actually read
  144. * @throws IllegalArgumentException if the requested number of bytes does
  145. * not represent an integral number of sample frames.
  146. *
  147. * @see SourceDataLine#write
  148. * @see DataLine#available
  149. */
  150. public int read(byte[] b, int off, int len);
  151. /**
  152. * Obtains the number of sample frames of audio data that can be read from
  153. * the target data line without blocking. Note that the return value
  154. * measures sample frames, not bytes.
  155. * @return the number of sample frames currently available for reading
  156. * @see SourceDataLine#availableWrite
  157. */
  158. //public int availableRead();
  159. }