jdk/src/share/classes/javax/sound/midi/Track.java
author darcy
Thu, 05 Jun 2014 23:17:05 -0700
changeset 25131 49006bd0e69d
parent 19207 3448b0cb4077
child 26003 d630c97424bd
permissions -rw-r--r--
8044553: Fix raw and unchecked lint warnings in javax.sound.* Reviewed-by: prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 19207
diff changeset
     2
 * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.sound.midi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import com.sun.media.sound.MidiUtils;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * A MIDI track is an independent stream of MIDI events (time-stamped MIDI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * data) that can be stored along with other tracks in a standard MIDI file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * The MIDI specification allows only 16 channels of MIDI data, but tracks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * are a way to get around this limitation.  A MIDI file can contain any number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * of tracks, each containing its own stream of up to 16 channels of MIDI data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * A <code>Track</code> occupies a middle level in the hierarchy of data played
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * by a <code>{@link Sequencer}</code>: sequencers play sequences, which contain tracks,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * which contain MIDI events.  A sequencer may provide controls that mute
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * or solo individual tracks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * The timing information and resolution for a track is controlled by and stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * in the sequence containing the track. A given <code>Track</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * is considered to belong to the particular <code>{@link Sequence}</code> that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * maintains its timing. For this reason, a new (empty) track is created by calling the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <code>{@link Sequence#createTrack}</code> method, rather than by directly invoking a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <code>Track</code> constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * The <code>Track</code> class provides methods to edit the track by adding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * or removing <code>MidiEvent</code> objects from it.  These operations keep
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * the event list in the correct time order.  Methods are also
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * included to obtain the track's size, in terms of either the number of events
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * it contains or its duration in ticks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @see Sequencer#setTrackMute
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @see Sequencer#setTrackSolo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @author Kara Kytle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @author Florian Bomers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
public class Track {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // TODO: use arrays for faster access
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // the list containing the events
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 19207
diff changeset
    69
    private ArrayList<MidiEvent> eventsList = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // use a hashset to detect duplicate events in add(MidiEvent)
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 19207
diff changeset
    72
    private HashSet<MidiEvent> set = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private MidiEvent eotEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Package-private constructor.  Constructs a new, empty Track object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * which initially contains one event, the meta-event End of Track.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    Track() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // start with the end of track event
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        MetaMessage eot = new ImmutableEndOfTrack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        eotEvent = new MidiEvent(eot, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        eventsList.add(eotEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        set.add(eotEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Adds a new event to the track.  However, if the event is already
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * contained in the track, it is not added again.  The list of events
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * is kept in time order, meaning that this event inserted at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * appropriate place in the list, not necessarily at the end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param event the event to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @return <code>true</code> if the event did not already exist in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * track and was added, otherwise <code>false</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public boolean add(MidiEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (event == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        synchronized(eventsList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            if (!set.contains(event)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                int eventsCount = eventsList.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                // get the last event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                MidiEvent lastEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                if (eventsCount > 0) {
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 19207
diff changeset
   111
                    lastEvent = eventsList.get(eventsCount - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                // sanity check that we have a correct end-of-track
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                if (lastEvent != eotEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    // if there is no eot event, add our immutable instance again
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    if (lastEvent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                        // set eotEvent's tick to the last tick of the track
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                        eotEvent.setTick(lastEvent.getTick());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                        // if the events list is empty, just set the tick to 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                        eotEvent.setTick(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    // we needn't check for a duplicate of eotEvent in "eventsList",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    // since then it would appear in the set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    eventsList.add(eotEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    set.add(eotEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    eventsCount = eventsList.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                // first see if we are trying to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                // and endoftrack event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                if (MidiUtils.isMetaEndOfTrack(event.getMessage())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    // since end of track event is useful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    // for delays at the end of a track, we want to keep
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    // the tick value requested here if it is greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    // than the one on the eot we are maintaining.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    // Otherwise, we only want a single eot event, so ignore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    if (event.getTick() > eotEvent.getTick()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                        eotEvent.setTick(event.getTick());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                // prevent duplicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                set.add(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                // insert event such that events is sorted in increasing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                // tick order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                int i = eventsCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                for ( ; i > 0; i--) {
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 19207
diff changeset
   151
                    if (event.getTick() >= (eventsList.get(i-1)).getTick()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                if (i == eventsCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    // we're adding an event after the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    // tick value of our eot, so push the eot out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    // Always add at the end for better performance:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    // this saves all the checks and arraycopy when inserting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    // overwrite eot with new event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    eventsList.set(eventsCount - 1, event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    // set new time of eot, if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    if (eotEvent.getTick() < event.getTick()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                        eotEvent.setTick(event.getTick());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    // add eot again at the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    eventsList.add(eotEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    eventsList.add(i, event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Removes the specified event from the track.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @param event the event to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @return <code>true</code> if the event existed in the track and was removed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * otherwise <code>false</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public boolean remove(MidiEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        // this implementation allows removing the EOT event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        // pretty bad, but would probably be too risky to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        // change behavior now, in case someone does tricks like:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        // while (track.size() > 0) track.remove(track.get(track.size() - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        // also, would it make sense to adjust the EOT's time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        // to the last event, if the last non-EOT event is removed?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        // Or: document that the ticks() length will not be reduced
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        // by deleting events (unless the EOT event is removed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        synchronized(eventsList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            if (set.remove(event)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                int i = eventsList.indexOf(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                if (i >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    eventsList.remove(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * Obtains the event at the specified index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @param index the location of the desired event in the event vector
19207
3448b0cb4077 8022174: Fix doclint warnings in javax.sound
darcy
parents: 5506
diff changeset
   214
     * @throws ArrayIndexOutOfBoundsException  if the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * specified index is negative or not less than the current size of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * this track.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @see #size
19207
3448b0cb4077 8022174: Fix doclint warnings in javax.sound
darcy
parents: 5506
diff changeset
   218
     * @return the event at the specified index
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public MidiEvent get(int index) throws ArrayIndexOutOfBoundsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            synchronized(eventsList) {
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 19207
diff changeset
   223
                return eventsList.get(index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        } catch (IndexOutOfBoundsException ioobe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            throw new ArrayIndexOutOfBoundsException(ioobe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * Obtains the number of events in this track.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @return the size of the track's event vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        synchronized(eventsList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            return eventsList.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Obtains the length of the track, expressed in MIDI ticks.  (The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * duration of a tick in seconds is determined by the timing resolution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * of the <code>Sequence</code> containing this track, and also by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * the tempo of the music as set by the sequencer.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @return the duration, in ticks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @see Sequence#Sequence(float, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @see Sequencer#setTempoInBPM(float)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @see Sequencer#getTickPosition()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public long ticks() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        long ret = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        synchronized (eventsList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            if (eventsList.size() > 0) {
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 19207
diff changeset
   256
                ret = (eventsList.get(eventsList.size() - 1)).getTick();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    private static class ImmutableEndOfTrack extends MetaMessage {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        private ImmutableEndOfTrack() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            super(new byte[3]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            data[0] = (byte) META;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            data[1] = MidiUtils.META_END_OF_TRACK_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            data[2] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        public void setMessage(int type, byte[] data, int length) throws InvalidMidiDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            throw new InvalidMidiDataException("cannot modify end of track message");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
}