1. /*
  2. * @(#)Sequence.java 1.27 04/05/05
  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.midi;
  8. import java.util.Vector;
  9. import com.sun.media.sound.MidiUtils;
  10. /**
  11. * A <code>Sequence</code> is a data structure containing musical
  12. * information (often an entire song or composition) that can be played
  13. * back by a <code>{@link Sequencer}</code> object. Specifically, the
  14. * <code>Sequence</code> contains timing
  15. * information and one or more tracks. Each <code>{@link Track track}</code> consists of a
  16. * series of MIDI events (such as note-ons, note-offs, program changes, and meta-events).
  17. * The sequence's timing information specifies the type of unit that is used
  18. * to time-stamp the events in the sequence.
  19. * <p>
  20. * A <code>Sequence</code> can be created from a MIDI file by reading the file
  21. * into an input stream and invoking one of the <code>getSequence</code> methods of
  22. * {@link MidiSystem}. A sequence can also be built from scratch by adding new
  23. * <code>Tracks</code> to an empty <code>Sequence</code>, and adding
  24. * <code>{@link MidiEvent}</code> objects to these <code>Tracks</code>.
  25. *
  26. * @see Sequencer#setSequence(java.io.InputStream stream)
  27. * @see Sequencer#setSequence(Sequence sequence)
  28. * @see Track#add(MidiEvent)
  29. * @see MidiFileFormat
  30. *
  31. * @version 1.27, 04/05/05
  32. * @author Kara Kytle
  33. */
  34. public class Sequence {
  35. // Timing types
  36. /**
  37. * The tempo-based timing type, for which the resolution is expressed in pulses (ticks) per quarter note.
  38. * @see #Sequence(float, int)
  39. */
  40. public static final float PPQ = 0.0f;
  41. /**
  42. * The SMPTE-based timing type with 24 frames per second (resolution is expressed in ticks per frame).
  43. * @see #Sequence(float, int)
  44. */
  45. public static final float SMPTE_24 = 24.0f;
  46. /**
  47. * The SMPTE-based timing type with 25 frames per second (resolution is expressed in ticks per frame).
  48. * @see #Sequence(float, int)
  49. */
  50. public static final float SMPTE_25 = 25.0f;
  51. /**
  52. * The SMPTE-based timing type with 29.97 frames per second (resolution is expressed in ticks per frame).
  53. * @see #Sequence(float, int)
  54. */
  55. public static final float SMPTE_30DROP = 29.97f;
  56. /**
  57. * The SMPTE-based timing type with 30 frames per second (resolution is expressed in ticks per frame).
  58. * @see #Sequence(float, int)
  59. */
  60. public static final float SMPTE_30 = 30.0f;
  61. // Variables
  62. /**
  63. * The timing division type of the sequence.
  64. * @see #PPQ
  65. * @see #SMPTE_24
  66. * @see #SMPTE_25
  67. * @see #SMPTE_30DROP
  68. * @see #SMPTE_30
  69. * @see #getDivisionType
  70. */
  71. protected float divisionType;
  72. /**
  73. * The timing resolution of the sequence.
  74. * @see #getResolution
  75. */
  76. protected int resolution;
  77. /**
  78. * The MIDI tracks in this sequence.
  79. * @see #getTracks
  80. */
  81. protected Vector<Track> tracks = new Vector<Track>();
  82. /**
  83. * Constructs a new MIDI sequence with the specified timing division
  84. * type and timing resolution. The division type must be one of the
  85. * recognized MIDI timing types. For tempo-based timing,
  86. * <code>divisionType</code> is PPQ (pulses per quarter note) and
  87. * the resolution is specified in ticks per beat. For SMTPE timing,
  88. * <code>divisionType</code> specifies the number of frames per
  89. * second and the resolution is specified in ticks per frame.
  90. * The sequence will contain no initial tracks. Tracks may be
  91. * added to or removed from the sequence using <code>{@link #createTrack}</code>
  92. * and <code>{@link #deleteTrack}</code>.
  93. *
  94. * @param divisionType the timing division type (PPQ or one of the SMPTE types)
  95. * @param resolution the timing resolution
  96. * @throws InvalidMidiDataException if <code>divisionType</code> is not valid
  97. *
  98. * @see #PPQ
  99. * @see #SMPTE_24
  100. * @see #SMPTE_25
  101. * @see #SMPTE_30DROP
  102. * @see #SMPTE_30
  103. * @see #getDivisionType
  104. * @see #getResolution
  105. * @see #getTracks
  106. */
  107. public Sequence(float divisionType, int resolution) throws InvalidMidiDataException {
  108. if (divisionType == PPQ)
  109. this.divisionType = PPQ;
  110. else if (divisionType == SMPTE_24)
  111. this.divisionType = SMPTE_24;
  112. else if (divisionType == SMPTE_25)
  113. this.divisionType = SMPTE_25;
  114. else if (divisionType == SMPTE_30DROP)
  115. this.divisionType = SMPTE_30DROP;
  116. else if (divisionType == SMPTE_30)
  117. this.divisionType = SMPTE_30;
  118. else throw new InvalidMidiDataException("Unsupported division type: " + divisionType);
  119. this.resolution = resolution;
  120. }
  121. /**
  122. * Constructs a new MIDI sequence with the specified timing division
  123. * type, timing resolution, and number of tracks. The division type must be one of the
  124. * recognized MIDI timing types. For tempo-based timing,
  125. * <code>divisionType</code> is PPQ (pulses per quarter note) and
  126. * the resolution is specified in ticks per beat. For SMTPE timing,
  127. * <code>divisionType</code> specifies the number of frames per
  128. * second and the resolution is specified in ticks per frame.
  129. * The sequence will be initialized with the number of tracks specified by
  130. * <code>numTracks</code>. These tracks are initially empty (i.e.
  131. * they contain only the meta-event End of Track).
  132. * The tracks may be retrieved for editing using the <code>{@link #getTracks}</code>
  133. * method. Additional tracks may be added, or existing tracks removed,
  134. * using <code>{@link #createTrack}</code> and <code>{@link #deleteTrack}</code>.
  135. *
  136. * @param divisionType the timing division type (PPQ or one of the SMPTE types)
  137. * @param resolution the timing resolution
  138. * @param numTracks the initial number of tracks in the sequence.
  139. * @throws InvalidMidiDataException if <code>divisionType</code> is not valid
  140. *
  141. * @see #PPQ
  142. * @see #SMPTE_24
  143. * @see #SMPTE_25
  144. * @see #SMPTE_30DROP
  145. * @see #SMPTE_30
  146. * @see #getDivisionType
  147. * @see #getResolution
  148. */
  149. public Sequence(float divisionType, int resolution, int numTracks) throws InvalidMidiDataException {
  150. if (divisionType == PPQ)
  151. this.divisionType = PPQ;
  152. else if (divisionType == SMPTE_24)
  153. this.divisionType = SMPTE_24;
  154. else if (divisionType == SMPTE_25)
  155. this.divisionType = SMPTE_25;
  156. else if (divisionType == SMPTE_30DROP)
  157. this.divisionType = SMPTE_30DROP;
  158. else if (divisionType == SMPTE_30)
  159. this.divisionType = SMPTE_30;
  160. else throw new InvalidMidiDataException("Unsupported division type: " + divisionType);
  161. this.resolution = resolution;
  162. for (int i = 0; i < numTracks; i++) {
  163. tracks.addElement(new Track());
  164. }
  165. }
  166. /**
  167. * Obtains the timing division type for this sequence.
  168. * @return the division type (PPQ or one of the SMPTE types)
  169. *
  170. * @see #PPQ
  171. * @see #SMPTE_24
  172. * @see #SMPTE_25
  173. * @see #SMPTE_30DROP
  174. * @see #SMPTE_30
  175. * @see #Sequence(float, int)
  176. * @see MidiFileFormat#getDivisionType()
  177. */
  178. public float getDivisionType() {
  179. return divisionType;
  180. }
  181. /**
  182. * Obtains the timing resolution for this sequence.
  183. * If the sequence's division type is PPQ, the resolution is specified in ticks per beat.
  184. * For SMTPE timing, the resolution is specified in ticks per frame.
  185. *
  186. * @return the number of ticks per beat (PPQ) or per frame (SMPTE)
  187. * @see #getDivisionType
  188. * @see #Sequence(float, int)
  189. * @see MidiFileFormat#getResolution()
  190. */
  191. public int getResolution() {
  192. return resolution;
  193. }
  194. /**
  195. * Creates a new, initially empty track as part of this sequence.
  196. * The track initially contains the meta-event End of Track.
  197. * The newly created track is returned. All tracks in the sequence
  198. * may be retrieved using <code>{@link #getTracks}</code>. Tracks may be
  199. * removed from the sequence using <code>{@link #deleteTrack}</code>.
  200. * @return the newly created track
  201. */
  202. public Track createTrack() {
  203. Track track = new Track();
  204. tracks.addElement(track);
  205. return track;
  206. }
  207. /**
  208. * Removes the specified track from the sequence.
  209. * @param track the track to remove
  210. * @return <code>true</code> if the track existed in the track and was removed,
  211. * otherwise <code>false</code>.
  212. *
  213. * @see #createTrack
  214. * @see #getTracks
  215. */
  216. public boolean deleteTrack(Track track) {
  217. synchronized(tracks) {
  218. return tracks.removeElement(track);
  219. }
  220. }
  221. /**
  222. * Obtains an array containing all the tracks in this sequence.
  223. * If the sequence contains no tracks, an array of length 0 is returned.
  224. * @return the array of tracks
  225. *
  226. * @see #createTrack
  227. * @see #deleteTrack
  228. */
  229. public Track[] getTracks() {
  230. return (Track[]) tracks.toArray(new Track[tracks.size()]);
  231. }
  232. /**
  233. * Obtains the duration of this sequence, expressed in microseconds.
  234. * @return this sequence's duration in microseconds.
  235. */
  236. public long getMicrosecondLength() {
  237. return com.sun.media.sound.MidiUtils.tick2microsecond(this, getTickLength(), null);
  238. }
  239. /**
  240. * Obtains the duration of this sequence, expressed in MIDI ticks.
  241. *
  242. * @return this sequence's length in ticks
  243. *
  244. * @see #getMicrosecondLength
  245. */
  246. public long getTickLength() {
  247. long length = 0;
  248. synchronized(tracks) {
  249. for(int i=0; i<tracks.size(); i++ ) {
  250. long temp = ((Track)tracks.elementAt(i)).ticks();
  251. if( temp>length ) {
  252. length = temp;
  253. }
  254. }
  255. return length;
  256. }
  257. }
  258. /**
  259. * Obtains a list of patches referenced in this sequence.
  260. * This patch list may be used to load the required
  261. * <code>{@link Instrument}</code> objects
  262. * into a <code>{@link Synthesizer}</code>.
  263. *
  264. * @return an array of <code>{@link Patch}</code> objects used in this sequence
  265. *
  266. * @see Synthesizer#loadInstruments(Soundbank, Patch[])
  267. */
  268. public Patch[] getPatchList() {
  269. // $$kk: 04.09.99: need to implement!!
  270. return new Patch[0];
  271. }
  272. }