1. /*
  2. * @(#)Clip.java 1.36 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. 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.36, 03/01/23
  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. * @throws IllegalStateException if the line is already open
  69. * @throws SecurityException if the line cannot be
  70. * opened due to security restrictions
  71. *
  72. * @see #close
  73. * @see #isOpen
  74. * @see LineListener
  75. */
  76. public void open(AudioFormat format, byte[] data, int offset, int bufferSize) throws LineUnavailableException;
  77. /**
  78. * Opens the clip with the format and audio data present in the provided audio
  79. * input stream. Opening a clip means that it should acquire any required
  80. * system resources and become operational. If this operation
  81. * input stream. If this operation
  82. * succeeds, the line is marked open and an
  83. * <code>{@link LineEvent.Type#OPEN OPEN}</code> event is dispatched
  84. * to the line's listeners.
  85. * <p>
  86. * Invoking this method on a line which is already open is illegal
  87. * and may result in an IllegalStateException.
  88. * <p>
  89. * Note that some lines, once closed, cannot be reopened. Attempts
  90. * to reopen such a line will always result in a
  91. * <code>{@link LineUnavailableException}</code>.
  92. *
  93. * @param stream an audio input stream from which audio data will be read into
  94. * the clip
  95. * @throws LineUnavailableException if the line cannot be
  96. * opened due to resource restrictions
  97. * @throws IOException if an I/O exception occurs during reading of
  98. * the stream
  99. * @throws IllegalStateException if the line is already open
  100. * @throws SecurityException if the line cannot be
  101. * opened due to security restrictions
  102. *
  103. * @see #close
  104. * @see #isOpen
  105. * @see LineListener
  106. */
  107. public void open(AudioInputStream stream) throws LineUnavailableException, IOException;
  108. /**
  109. * Obtains the media length in sample frames.
  110. * @return the media length, expressed in sample frames,
  111. * or <code>AudioSystem.NOT_SPECIFIED</code> if the line is not open.
  112. * @see AudioSystem#NOT_SPECIFIED
  113. */
  114. public int getFrameLength();
  115. /**
  116. * Obtains the media duration in microseconds
  117. * @return the media duration, expressed in microseconds,
  118. * or <code>AudioSystem.NOT_SPECIFIED</code> if the line is not open.
  119. * @see AudioSystem#NOT_SPECIFIED
  120. */
  121. public long getMicrosecondLength();
  122. /**
  123. * Sets the media position in sample frames. The position is zero-based;
  124. * the first frame is frame number zero. When the clip begins playing the
  125. * next time, it will start by playing the frame at this position.
  126. * <p>
  127. * To obtain the current position in sample frames, use the
  128. * <code>{@link DataLine#getFramePosition getFramePosition}</code>
  129. * method of <code>DataLine</code>.
  130. *
  131. * @param frames the desired new media position, expressed in sample frames
  132. */
  133. public void setFramePosition(int frames);
  134. /**
  135. * Sets the media position in microseconds. When the clip begins playing the
  136. * next time, it will start at this position.
  137. * The level of precision is not guaranteed. For example, an implementation
  138. * might calculate the microsecond position from the current frame position
  139. * and the audio sample frame rate. The precision in microseconds would
  140. * then be limited to the number of microseconds per sample frame.
  141. * <p>
  142. * To obtain the current position in microseconds, use the
  143. * <code>{@link DataLine#getMicrosecondPosition getMicrosecondPosition}</code>
  144. * method of <code>DataLine</code>.
  145. *
  146. * @param microseconds the desired new media position, expressed in microseconds
  147. */
  148. public void setMicrosecondPosition(long microseconds);
  149. /**
  150. * Sets the first and last sample frames that will be played in
  151. * the loop. The ending point must be greater than
  152. * or equal to the starting point, and both must fall within the
  153. * the size of the loaded media. A value of 0 for the starting
  154. * point means the beginning of the loaded media. Similarly, a value of -1
  155. * for the ending point indicates the last frame of the media.
  156. * @param start the loop's starting position, in sample frames (zero-based)
  157. * @param end the loop's ending position, in sample frames (zero-based), or
  158. * -1 to indicate the final frame
  159. * @throws IllegalArgumentException if the requested
  160. * loop points cannot be set, usually because one or both falls outside
  161. * the media's duration or because the ending point is
  162. * before the starting point
  163. */
  164. public void setLoopPoints(int start, int end);
  165. /**
  166. * Starts looping playback from the current position. Playback will
  167. * continue to the loop's end point, then loop back to the loop start point
  168. * <code>count</code> times, and finally continue playback to the end of
  169. * the clip.
  170. * <p>
  171. * If the current position when this method is invoked is greater than the
  172. * loop end point, playback simply continues to the
  173. * end of the clip without looping.
  174. * <p>
  175. * A <code>count</code> value of 0 indicates that any current looping should
  176. * cease and playback should continue to the end of the clip. The behavior
  177. * is undefined when this method is invoked with any other value during a
  178. * loop operation.
  179. * <p>
  180. * If playback is stopped during looping, the current loop status is
  181. * cleared; the behavior of subsequent loop and start requests is not
  182. * affected by an interrupted loop operation.
  183. *
  184. * @param count the number of times playback should loop back from the
  185. * loop's end position to the loop's start position, or
  186. * <code>{@link #LOOP_CONTINUOUSLY}</code> to indicate that looping should
  187. * continue until interrupted
  188. */
  189. public void loop(int count);
  190. }