1 /* Sequence.java -- A sequence of MIDI events
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.sound.midi;
41 import java.util.Iterator;
42 import java.util.Vector;
45 * Objects of this type represent sequences of MIDI messages that can be
46 * played back by a Sequencer.
48 * @author Anthony Green (green@redhat.com)
55 * The timing division type for this sequence (PPQ or SMPTE*)
57 protected float divisionType;
60 * The timing resolution in ticks/beat or ticks/frame, depending on the
63 protected int resolution;
66 * The MIDI tracks used by this sequence.
68 protected Vector<Track> tracks;
71 * Tempo-based timing. Resolution is specified in ticks per beat.
73 public static final float PPQ = 0.0f;
76 * 24 frames/second timing. Resolution is specific in ticks per frame.
78 public static final float SMPTE_24 = 24.0f;
81 * 25 frames/second timing. Resolution is specific in ticks per frame.
83 public static final float SMPTE_25 = 25.0f;
86 * 30 frames/second timing. Resolution is specific in ticks per frame.
88 public static final float SMPTE_30 = 30.0f;
91 * 29.97 frames/second timing. Resolution is specific in ticks per frame.
93 public static final float SMPTE_30DROP = 29.97f;
95 // Private helper class
96 private void init(float divisionType, int resolution, int numTracks)
97 throws InvalidMidiDataException
99 if (divisionType != PPQ
100 && divisionType != SMPTE_24
101 && divisionType != SMPTE_25
102 && divisionType != SMPTE_30
103 && divisionType != SMPTE_30DROP)
104 throw new InvalidMidiDataException("Invalid division type ("
105 + divisionType + ")");
107 this.divisionType = divisionType;
108 this.resolution = resolution;
110 tracks = new Vector<Track>(numTracks);
111 while (numTracks > 0)
112 tracks.set(--numTracks, new Track());
116 * Create a MIDI sequence object with no initial tracks.
118 * @param divisionType the division type (must be one of PPQ or SMPTE_*)
119 * @param resolution the timing resolution
120 * @throws InvalidMidiDataException if the division type is invalid
122 public Sequence(float divisionType, int resolution)
123 throws InvalidMidiDataException
125 init(divisionType, resolution, 0);
129 * Create a MIDI seqence object.
131 * @param divisionType the division type (must be one of PPQ or SMPTE_*)
132 * @param resolution the timing resolution
133 * @param numTracks the number of initial tracks
134 * @throws InvalidMidiDataException if the division type is invalid
136 public Sequence(float divisionType, int resolution, int numTracks)
137 throws InvalidMidiDataException
139 init(divisionType, resolution, 0);
143 * The division type of this sequence.
145 * @return division type of this sequence
147 public float getDivisionType()
153 * The timing resolution for this sequence, relative to the division type.
155 * @return the timing resolution for this sequence
157 public int getResolution()
163 * Create a new empty MIDI track and add it to this sequence.
165 * @return the newly create MIDI track
167 public Track createTrack()
169 Track track = new Track();
175 * Remove the specified MIDI track from this sequence.
177 * @param track the track to remove
178 * @return true if track was removed and false othewise
180 public boolean deleteTrack(Track track)
182 return tracks.remove(track);
186 * Get an array of MIDI tracks used in this sequence.
188 * @return a possibly empty array of tracks
190 public Track[] getTracks()
192 return tracks.toArray(new Track[tracks.size()]);
196 * The length of this sequence in microseconds.
198 * @return the length of this sequence in microseconds
200 public long getMicrosecondLength()
202 long tickLength = getTickLength();
204 if (divisionType == PPQ)
207 // How can this possible be computed? PPQ is pulses per quarter-note,
208 // which is dependent on the tempo of the Sequencer.
210 UnsupportedOperationException("Can't compute PPQ based lengths yet");
214 // This is a fixed tick per frame computation
215 return (long) ((tickLength * 1000000) / (divisionType * resolution));
220 * The length of this sequence in MIDI ticks.
222 * @return the length of this sequence in MIDI ticks
224 public long getTickLength()
227 Iterator<Track> itr = tracks.iterator();
228 while (itr.hasNext())
230 Track track = itr.next();
231 long trackTicks = track.ticks();
232 if (trackTicks > length)
239 * Get an array of patches used in this sequence.
241 * @return an array of patches used in this sequence
243 public Patch[] getPatchList()
245 // FIXE: not quite sure how to do this yet.
246 throw new UnsupportedOperationException("Can't get patch list yet");