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