0N/A/*
2362N/A * Copyright (c) 1999, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.sound.midi;
0N/A
0N/Aimport java.util.Vector;
0N/Aimport com.sun.media.sound.MidiUtils;
0N/A
0N/A
0N/A/**
0N/A * A <code>Sequence</code> is a data structure containing musical
0N/A * information (often an entire song or composition) that can be played
0N/A * back by a <code>{@link Sequencer}</code> object. Specifically, the
0N/A * <code>Sequence</code> contains timing
0N/A * information and one or more tracks. Each <code>{@link Track track}</code> consists of a
0N/A * series of MIDI events (such as note-ons, note-offs, program changes, and meta-events).
0N/A * The sequence's timing information specifies the type of unit that is used
0N/A * to time-stamp the events in the sequence.
0N/A * <p>
0N/A * A <code>Sequence</code> can be created from a MIDI file by reading the file
0N/A * into an input stream and invoking one of the <code>getSequence</code> methods of
0N/A * {@link MidiSystem}. A sequence can also be built from scratch by adding new
0N/A * <code>Tracks</code> to an empty <code>Sequence</code>, and adding
0N/A * <code>{@link MidiEvent}</code> objects to these <code>Tracks</code>.
0N/A *
0N/A * @see Sequencer#setSequence(java.io.InputStream stream)
0N/A * @see Sequencer#setSequence(Sequence sequence)
0N/A * @see Track#add(MidiEvent)
0N/A * @see MidiFileFormat
0N/A *
0N/A * @author Kara Kytle
0N/A */
0N/Apublic class Sequence {
0N/A
0N/A
0N/A // Timing types
0N/A
0N/A /**
0N/A * The tempo-based timing type, for which the resolution is expressed in pulses (ticks) per quarter note.
0N/A * @see #Sequence(float, int)
0N/A */
0N/A public static final float PPQ = 0.0f;
0N/A
0N/A /**
0N/A * The SMPTE-based timing type with 24 frames per second (resolution is expressed in ticks per frame).
0N/A * @see #Sequence(float, int)
0N/A */
0N/A public static final float SMPTE_24 = 24.0f;
0N/A
0N/A /**
0N/A * The SMPTE-based timing type with 25 frames per second (resolution is expressed in ticks per frame).
0N/A * @see #Sequence(float, int)
0N/A */
0N/A public static final float SMPTE_25 = 25.0f;
0N/A
0N/A /**
0N/A * The SMPTE-based timing type with 29.97 frames per second (resolution is expressed in ticks per frame).
0N/A * @see #Sequence(float, int)
0N/A */
0N/A public static final float SMPTE_30DROP = 29.97f;
0N/A
0N/A /**
0N/A * The SMPTE-based timing type with 30 frames per second (resolution is expressed in ticks per frame).
0N/A * @see #Sequence(float, int)
0N/A */
0N/A public static final float SMPTE_30 = 30.0f;
0N/A
0N/A
0N/A // Variables
0N/A
0N/A /**
0N/A * The timing division type of the sequence.
0N/A * @see #PPQ
0N/A * @see #SMPTE_24
0N/A * @see #SMPTE_25
0N/A * @see #SMPTE_30DROP
0N/A * @see #SMPTE_30
0N/A * @see #getDivisionType
0N/A */
0N/A protected float divisionType;
0N/A
0N/A /**
0N/A * The timing resolution of the sequence.
0N/A * @see #getResolution
0N/A */
0N/A protected int resolution;
0N/A
0N/A /**
0N/A * The MIDI tracks in this sequence.
0N/A * @see #getTracks
0N/A */
0N/A protected Vector<Track> tracks = new Vector<Track>();
0N/A
0N/A
0N/A /**
0N/A * Constructs a new MIDI sequence with the specified timing division
0N/A * type and timing resolution. The division type must be one of the
0N/A * recognized MIDI timing types. For tempo-based timing,
0N/A * <code>divisionType</code> is PPQ (pulses per quarter note) and
0N/A * the resolution is specified in ticks per beat. For SMTPE timing,
0N/A * <code>divisionType</code> specifies the number of frames per
0N/A * second and the resolution is specified in ticks per frame.
0N/A * The sequence will contain no initial tracks. Tracks may be
0N/A * added to or removed from the sequence using <code>{@link #createTrack}</code>
0N/A * and <code>{@link #deleteTrack}</code>.
0N/A *
0N/A * @param divisionType the timing division type (PPQ or one of the SMPTE types)
0N/A * @param resolution the timing resolution
0N/A * @throws InvalidMidiDataException if <code>divisionType</code> is not valid
0N/A *
0N/A * @see #PPQ
0N/A * @see #SMPTE_24
0N/A * @see #SMPTE_25
0N/A * @see #SMPTE_30DROP
0N/A * @see #SMPTE_30
0N/A * @see #getDivisionType
0N/A * @see #getResolution
0N/A * @see #getTracks
0N/A */
0N/A public Sequence(float divisionType, int resolution) throws InvalidMidiDataException {
0N/A
0N/A if (divisionType == PPQ)
0N/A this.divisionType = PPQ;
0N/A else if (divisionType == SMPTE_24)
0N/A this.divisionType = SMPTE_24;
0N/A else if (divisionType == SMPTE_25)
0N/A this.divisionType = SMPTE_25;
0N/A else if (divisionType == SMPTE_30DROP)
0N/A this.divisionType = SMPTE_30DROP;
0N/A else if (divisionType == SMPTE_30)
0N/A this.divisionType = SMPTE_30;
0N/A else throw new InvalidMidiDataException("Unsupported division type: " + divisionType);
0N/A
0N/A this.resolution = resolution;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Constructs a new MIDI sequence with the specified timing division
0N/A * type, timing resolution, and number of tracks. The division type must be one of the
0N/A * recognized MIDI timing types. For tempo-based timing,
0N/A * <code>divisionType</code> is PPQ (pulses per quarter note) and
0N/A * the resolution is specified in ticks per beat. For SMTPE timing,
0N/A * <code>divisionType</code> specifies the number of frames per
0N/A * second and the resolution is specified in ticks per frame.
0N/A * The sequence will be initialized with the number of tracks specified by
0N/A * <code>numTracks</code>. These tracks are initially empty (i.e.
0N/A * they contain only the meta-event End of Track).
0N/A * The tracks may be retrieved for editing using the <code>{@link #getTracks}</code>
0N/A * method. Additional tracks may be added, or existing tracks removed,
0N/A * using <code>{@link #createTrack}</code> and <code>{@link #deleteTrack}</code>.
0N/A *
0N/A * @param divisionType the timing division type (PPQ or one of the SMPTE types)
0N/A * @param resolution the timing resolution
0N/A * @param numTracks the initial number of tracks in the sequence.
0N/A * @throws InvalidMidiDataException if <code>divisionType</code> is not valid
0N/A *
0N/A * @see #PPQ
0N/A * @see #SMPTE_24
0N/A * @see #SMPTE_25
0N/A * @see #SMPTE_30DROP
0N/A * @see #SMPTE_30
0N/A * @see #getDivisionType
0N/A * @see #getResolution
0N/A */
0N/A public Sequence(float divisionType, int resolution, int numTracks) throws InvalidMidiDataException {
0N/A
0N/A if (divisionType == PPQ)
0N/A this.divisionType = PPQ;
0N/A else if (divisionType == SMPTE_24)
0N/A this.divisionType = SMPTE_24;
0N/A else if (divisionType == SMPTE_25)
0N/A this.divisionType = SMPTE_25;
0N/A else if (divisionType == SMPTE_30DROP)
0N/A this.divisionType = SMPTE_30DROP;
0N/A else if (divisionType == SMPTE_30)
0N/A this.divisionType = SMPTE_30;
0N/A else throw new InvalidMidiDataException("Unsupported division type: " + divisionType);
0N/A
0N/A this.resolution = resolution;
0N/A
0N/A for (int i = 0; i < numTracks; i++) {
0N/A tracks.addElement(new Track());
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Obtains the timing division type for this sequence.
0N/A * @return the division type (PPQ or one of the SMPTE types)
0N/A *
0N/A * @see #PPQ
0N/A * @see #SMPTE_24
0N/A * @see #SMPTE_25
0N/A * @see #SMPTE_30DROP
0N/A * @see #SMPTE_30
0N/A * @see #Sequence(float, int)
0N/A * @see MidiFileFormat#getDivisionType()
0N/A */
0N/A public float getDivisionType() {
0N/A return divisionType;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Obtains the timing resolution for this sequence.
0N/A * If the sequence's division type is PPQ, the resolution is specified in ticks per beat.
0N/A * For SMTPE timing, the resolution is specified in ticks per frame.
0N/A *
0N/A * @return the number of ticks per beat (PPQ) or per frame (SMPTE)
0N/A * @see #getDivisionType
0N/A * @see #Sequence(float, int)
0N/A * @see MidiFileFormat#getResolution()
0N/A */
0N/A public int getResolution() {
0N/A return resolution;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Creates a new, initially empty track as part of this sequence.
0N/A * The track initially contains the meta-event End of Track.
0N/A * The newly created track is returned. All tracks in the sequence
0N/A * may be retrieved using <code>{@link #getTracks}</code>. Tracks may be
0N/A * removed from the sequence using <code>{@link #deleteTrack}</code>.
0N/A * @return the newly created track
0N/A */
0N/A public Track createTrack() {
0N/A
0N/A Track track = new Track();
0N/A tracks.addElement(track);
0N/A
0N/A return track;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Removes the specified track from the sequence.
0N/A * @param track the track to remove
0N/A * @return <code>true</code> if the track existed in the track and was removed,
0N/A * otherwise <code>false</code>.
0N/A *
0N/A * @see #createTrack
0N/A * @see #getTracks
0N/A */
0N/A public boolean deleteTrack(Track track) {
0N/A
0N/A synchronized(tracks) {
0N/A
0N/A return tracks.removeElement(track);
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Obtains an array containing all the tracks in this sequence.
0N/A * If the sequence contains no tracks, an array of length 0 is returned.
0N/A * @return the array of tracks
0N/A *
0N/A * @see #createTrack
0N/A * @see #deleteTrack
0N/A */
0N/A public Track[] getTracks() {
0N/A
0N/A return (Track[]) tracks.toArray(new Track[tracks.size()]);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Obtains the duration of this sequence, expressed in microseconds.
0N/A * @return this sequence's duration in microseconds.
0N/A */
0N/A public long getMicrosecondLength() {
0N/A
0N/A return com.sun.media.sound.MidiUtils.tick2microsecond(this, getTickLength(), null);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Obtains the duration of this sequence, expressed in MIDI ticks.
0N/A *
0N/A * @return this sequence's length in ticks
0N/A *
0N/A * @see #getMicrosecondLength
0N/A */
0N/A public long getTickLength() {
0N/A
0N/A long length = 0;
0N/A
0N/A synchronized(tracks) {
0N/A
0N/A for(int i=0; i<tracks.size(); i++ ) {
0N/A long temp = ((Track)tracks.elementAt(i)).ticks();
0N/A if( temp>length ) {
0N/A length = temp;
0N/A }
0N/A }
0N/A return length;
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Obtains a list of patches referenced in this sequence.
0N/A * This patch list may be used to load the required
0N/A * <code>{@link Instrument}</code> objects
0N/A * into a <code>{@link Synthesizer}</code>.
0N/A *
0N/A * @return an array of <code>{@link Patch}</code> objects used in this sequence
0N/A *
0N/A * @see Synthesizer#loadInstruments(Soundbank, Patch[])
0N/A */
0N/A public Patch[] getPatchList() {
0N/A
0N/A // $$kk: 04.09.99: need to implement!!
0N/A return new Patch[0];
0N/A }
0N/A}