1. /*
  2. * @(#)Clip.java 1.38 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. import java.io.InputStream;
  9. import java.io.IOException;
  10. /**
  11. * The <code>Clip</code> interface represents a special kind of data line whose
  12. * audio data can be loaded prior to playback, instead of being streamed in
  13. * real time.
  14. * <p>
  15. * Because the data is pre-loaded and has a known length, you can set a clip
  16. * to start playing at any position in its audio data. You can also create a
  17. * loop, so that when the clip is played it will cycle repeatedly. Loops are
  18. * specified with a starting and ending sample frame, along with the number of
  19. * times that the loop should be played.
  20. * <p>
  21. * Clips may be obtained from a <code>{@link Mixer}</code> that supports lines
  22. * of this type. Data is loaded into a clip when it is opened.
  23. * <p>
  24. * Playback of an audio clip may be started and stopped using the <code>start</code>
  25. * and <code>stop</code> methods. These methods do not reset the media position;
  26. * <code>start</code> causes playback to continue from the position where playback
  27. * was last stopped. To restart playback from the beginning of the clip's audio
  28. * data, simply follow the invocation of <code>{@link DataLine#stop stop}</code>
  29. * with setFramePosition(0), which rewinds the media to the beginning
  30. * of the clip.
  31. *
  32. * @author Kara Kytle
  33. * @version 1.38, 03/12/19
  34. * @since 1.3
  35. */
  36. public interface Clip extends DataLine {
  37. /**
  38. * A value indicating that looping should continue indefinitely rather than
  39. * complete after a specific number of loops.
  40. * @see #loop
  41. */
  42. public static final int LOOP_CONTINUOUSLY = -1;
  43. /**
  44. * Opens the clip, meaning that it should acquire any required
  45. * system resources and become operational. The clip is opened
  46. * with the format and audio data indicated.
  47. * If this operation succeeds, the line is marked as open and an
  48. * <code>{@link LineEvent.Type#OPEN OPEN}</code> event is dispatched
  49. * to the line's listeners.
  50. * <p>
  51. * Invoking this method on a line which is already open is illegal
  52. * and may result in an IllegalStateException.
  53. * <p>
  54. * Note that some lines, once closed, cannot be reopened. Attempts
  55. * to reopen such a line will always result in a
  56. * <code>{@link LineUnavailableException}</code>.
  57. *
  58. * @param format the format of the supplied audio data
  59. * @param data a byte array containing audio data to load into the clip
  60. * @param offset the point at which to start copying, expressed in
  61. * <em>bytes</em> from the beginning of the array
  62. * @param bufferSize the number of <em>bytes</em>
  63. * of data to load into the clip from the array.
  64. * @throws LineUnavailableException if the line cannot be
  65. * opened due to resource restrictions
  66. * @throws IllegalArgumentException if the buffer size does not represent
  67. * an integral number of sample frames,
  68. * or if <code>format</code> is not fully specified or invalid
  69. * @throws IllegalStateException if the line is already open
  70. * @throws SecurityException if the line cannot be
  71. * opened due to security restrictions
  72. *
  73. * @see #close
  74. * @see #isOpen
  75. * @see LineListener
  76. */
  77. public void open(AudioFormat format, byte[] data, int offset, int bufferSize) throws LineUnavailableException;
  78. /**
  79. * Opens the clip with the format and audio data present in the provided audio
  80. * input stream. Opening a clip means that it should acquire any required
  81. * system resources and become operational. If this operation
  82. * input stream. If this operation
  83. * succeeds, the line is marked open and an
  84. * <code>{@link LineEvent.Type#OPEN OPEN}</code> event is dispatched
  85. * to the line's listeners.
  86. * <p>
  87. * Invoking this method on a line which is already open is illegal
  88. * and may result in an IllegalStateException.
  89. * <p>
  90. * Note that some lines, once closed, cannot be reopened. Attempts
  91. * to reopen such a line will always result in a
  92. * <code>{@link LineUnavailableException}</code>.
  93. *
  94. * @param stream an audio input stream from which audio data will be read into
  95. * the clip
  96. * @throws LineUnavailableException if the line cannot be
  97. * opened due to resource restrictions
  98. * @throws IOException if an I/O exception occurs during reading of
  99. * the stream
  100. * @throws IllegalArgumentException if the stream's audio format
  101. * is not fully specified or invalid
  102. * @throws IllegalStateException if the line is already open
  103. * @throws SecurityException if the line cannot be
  104. * opened due to security restrictions
  105. *
  106. * @see #close
  107. * @see #isOpen
  108. * @see LineListener
  109. */
  110. public void open(AudioInputStream stream) throws LineUnavailableException, IOException;
  111. /**
  112. * Obtains the media length in sample frames.
  113. * @return the media length, expressed in sample frames,
  114. * or <code>AudioSystem.NOT_SPECIFIED</code> if the line is not open.
  115. * @see AudioSystem#NOT_SPECIFIED
  116. */
  117. public int getFrameLength();
  118. /**
  119. * Obtains the media duration in microseconds
  120. * @return the media duration, expressed in microseconds,
  121. * or <code>AudioSystem.NOT_SPECIFIED</code> if the line is not open.
  122. * @see AudioSystem#NOT_SPECIFIED
  123. */
  124. public long getMicrosecondLength();
  125. /**
  126. * Sets the media position in sample frames. The position is zero-based;
  127. * the first frame is frame number zero. When the clip begins playing the
  128. * next time, it will start by playing the frame at this position.
  129. * <p>
  130. * To obtain the current position in sample frames, use the
  131. * <code>{@link DataLine#getFramePosition getFramePosition}</code>
  132. * method of <code>DataLine</code>.
  133. *
  134. * @param frames the desired new media position, expressed in sample frames
  135. */
  136. public void setFramePosition(int frames);
  137. /**
  138. * Sets the media position in microseconds. When the clip begins playing the
  139. * next time, it will start at this position.
  140. * The level of precision is not guaranteed. For example, an implementation
  141. * might calculate the microsecond position from the current frame position
  142. * and the audio sample frame rate. The precision in microseconds would
  143. * then be limited to the number of microseconds per sample frame.
  144. * <p>
  145. * To obtain the current position in microseconds, use the
  146. * <code>{@link DataLine#getMicrosecondPosition getMicrosecondPosition}</code>
  147. * method of <code>DataLine</code>.
  148. *
  149. * @param microseconds the desired new media position, expressed in microseconds
  150. */
  151. public void setMicrosecondPosition(long microseconds);
  152. /**
  153. * Sets the first and last sample frames that will be played in
  154. * the loop. The ending point must be greater than
  155. * or equal to the starting point, and both must fall within the
  156. * the size of the loaded media. A value of 0 for the starting
  157. * point means the beginning of the loaded media. Similarly, a value of -1
  158. * for the ending point indicates the last frame of the media.
  159. * @param start the loop's starting position, in sample frames (zero-based)
  160. * @param end the loop's ending position, in sample frames (zero-based), or
  161. * -1 to indicate the final frame
  162. * @throws IllegalArgumentException if the requested
  163. * loop points cannot be set, usually because one or both falls outside
  164. * the media's duration or because the ending point is
  165. * before the starting point
  166. */
  167. public void setLoopPoints(int start, int end);
  168. /**
  169. * Starts looping playback from the current position. Playback will
  170. * continue to the loop's end point, then loop back to the loop start point
  171. * <code>count</code> times, and finally continue playback to the end of
  172. * the clip.
  173. * <p>
  174. * If the current position when this method is invoked is greater than the
  175. * loop end point, playback simply continues to the
  176. * end of the clip without looping.
  177. * <p>
  178. * A <code>count</code> value of 0 indicates that any current looping should
  179. * cease and playback should continue to the end of the clip. The behavior
  180. * is undefined when this method is invoked with any other value during a
  181. * loop operation.
  182. * <p>
  183. * If playback is stopped during looping, the current loop status is
  184. * cleared; the behavior of subsequent loop and start requests is not
  185. * affected by an interrupted loop operation.
  186. *
  187. * @param count the number of times playback should loop back from the
  188. * loop's end position to the loop's start position, or
  189. * <code>{@link #LOOP_CONTINUOUSLY}</code> to indicate that looping should
  190. * continue until interrupted
  191. */
  192. public void loop(int count);
  193. }