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