jdk/src/java.desktop/share/classes/javax/sound/midi/Track.java
author serb
Mon, 15 Aug 2016 20:08:55 +0300
changeset 40444 afabcfc2f3ef
parent 26037 508779ce6619
child 47195 b309b58eb190
permissions -rw-r--r--
8163949: Cleanup of classes which are related to JavaSound 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.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.HashSet;
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    30
2
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
/**
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    34
 * A MIDI track is an independent stream of MIDI events (time-stamped MIDI data)
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    35
 * that can be stored along with other tracks in a standard MIDI file. The MIDI
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    36
 * specification allows only 16 channels of MIDI data, but tracks are a way to
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    37
 * get around this limitation. A MIDI file can contain any number of tracks,
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    38
 * each containing its own stream of up to 16 channels of MIDI data.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p>
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    40
 * A {@code Track} occupies a middle level in the hierarchy of data played by a
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    41
 * {@link Sequencer}: sequencers play sequences, which contain tracks, which
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    42
 * contain MIDI events. A sequencer may provide controls that mute or solo
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    43
 * individual tracks.
2
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
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    46
 * in the sequence containing the track. A given {@code Track} is considered to
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    47
 * belong to the particular {@link Sequence} that maintains its timing. For this
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    48
 * reason, a new (empty) track is created by calling the
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    49
 * {@link Sequence#createTrack} method, rather than by directly invoking a
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    50
 * {@code Track} constructor.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    52
 * The {@code Track} class provides methods to edit the track by adding or
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    53
 * removing {@code MidiEvent} objects from it. These operations keep the event
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    54
 * list in the correct time order. Methods are also included to obtain the
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    55
 * track's size, in terms of either the number of events it contains or its
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    56
 * duration in ticks.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @author Kara Kytle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @author Florian Bomers
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    60
 * @see Sequencer#setTrackMute
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    61
 * @see Sequencer#setTrackSolo
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
public class Track {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // TODO: use arrays for faster access
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // the list containing the events
40444
afabcfc2f3ef 8163949: Cleanup of classes which are related to JavaSound
serb
parents: 26037
diff changeset
    68
    private final ArrayList<MidiEvent> eventsList = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // use a hashset to detect duplicate events in add(MidiEvent)
40444
afabcfc2f3ef 8163949: Cleanup of classes which are related to JavaSound
serb
parents: 26037
diff changeset
    71
    private final HashSet<MidiEvent> set = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
40444
afabcfc2f3ef 8163949: Cleanup of classes which are related to JavaSound
serb
parents: 26037
diff changeset
    73
    private final MidiEvent eotEvent;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    76
     * Package-private constructor. Constructs a new, empty Track object, which
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    77
     * initially contains one event, the meta-event End of Track.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    Track() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        // start with the end of track event
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        MetaMessage eot = new ImmutableEndOfTrack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        eotEvent = new MidiEvent(eot, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        eventsList.add(eotEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        set.add(eotEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    88
     * Adds a new event to the track. However, if the event is already contained
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    89
     * in the track, it is not added again. The list of events is kept in time
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    90
     * order, meaning that this event inserted at the appropriate place in the
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    91
     * list, not necessarily at the end.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    93
     * @param  event the event to add
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    94
     * @return {@code true} if the event did not already exist in the track and
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
    95
     *         was added, otherwise {@code false}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public boolean add(MidiEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (event == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        synchronized(eventsList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            if (!set.contains(event)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                int eventsCount = eventsList.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                // get the last event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                MidiEvent lastEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                if (eventsCount > 0) {
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 19207
diff changeset
   109
                    lastEvent = eventsList.get(eventsCount - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                // sanity check that we have a correct end-of-track
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                if (lastEvent != eotEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    // if there is no eot event, add our immutable instance again
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    if (lastEvent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                        // set eotEvent's tick to the last tick of the track
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                        eotEvent.setTick(lastEvent.getTick());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                        // if the events list is empty, just set the tick to 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                        eotEvent.setTick(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    // we needn't check for a duplicate of eotEvent in "eventsList",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    // since then it would appear in the set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    eventsList.add(eotEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    set.add(eotEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    eventsCount = eventsList.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                // first see if we are trying to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                // and endoftrack event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                if (MidiUtils.isMetaEndOfTrack(event.getMessage())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    // since end of track event is useful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    // for delays at the end of a track, we want to keep
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    // the tick value requested here if it is greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    // than the one on the eot we are maintaining.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    // Otherwise, we only want a single eot event, so ignore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    if (event.getTick() > eotEvent.getTick()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                        eotEvent.setTick(event.getTick());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                // prevent duplicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                set.add(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                // insert event such that events is sorted in increasing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                // tick order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                int i = eventsCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                for ( ; i > 0; i--) {
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 19207
diff changeset
   149
                    if (event.getTick() >= (eventsList.get(i-1)).getTick()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                if (i == eventsCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    // we're adding an event after the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    // tick value of our eot, so push the eot out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    // Always add at the end for better performance:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    // this saves all the checks and arraycopy when inserting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    // overwrite eot with new event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    eventsList.set(eventsCount - 1, event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    // set new time of eot, if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    if (eotEvent.getTick() < event.getTick()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                        eotEvent.setTick(event.getTick());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    // add eot again at the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    eventsList.add(eotEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    eventsList.add(i, event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Removes the specified event from the track.
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
   179
     *
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
   180
     * @param  event the event to remove
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
   181
     * @return {@code true} if the event existed in the track and was removed,
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
   182
     *         otherwise {@code false}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public boolean remove(MidiEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        // this implementation allows removing the EOT event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        // pretty bad, but would probably be too risky to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        // change behavior now, in case someone does tricks like:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        // while (track.size() > 0) track.remove(track.get(track.size() - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        // also, would it make sense to adjust the EOT's time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        // to the last event, if the last non-EOT event is removed?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        // Or: document that the ticks() length will not be reduced
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        // by deleting events (unless the EOT event is removed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        synchronized(eventsList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            if (set.remove(event)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                int i = eventsList.indexOf(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                if (i >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    eventsList.remove(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Obtains the event at the specified index.
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
   210
     *
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
   211
     * @param  index the location of the desired event in the event vector
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
   212
     * @return the event at the specified index
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
   213
     * @throws ArrayIndexOutOfBoundsException if the specified index is negative
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
   214
     *         or not less than the current size of this track
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @see #size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    public MidiEvent get(int index) throws ArrayIndexOutOfBoundsException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            synchronized(eventsList) {
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 19207
diff changeset
   220
                return eventsList.get(index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        } catch (IndexOutOfBoundsException ioobe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            throw new ArrayIndexOutOfBoundsException(ioobe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * Obtains the number of events in this track.
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
   229
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @return the size of the track's event vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        synchronized(eventsList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            return eventsList.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
   239
     * Obtains the length of the track, expressed in MIDI ticks. (The duration
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
   240
     * of a tick in seconds is determined by the timing resolution of the
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
   241
     * {@code Sequence} containing this track, and also by the tempo of the
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
   242
     * music as set by the sequencer.)
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 25131
diff changeset
   243
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @return the duration, in ticks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @see Sequence#Sequence(float, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @see Sequencer#setTempoInBPM(float)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @see Sequencer#getTickPosition()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public long ticks() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        long ret = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        synchronized (eventsList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            if (eventsList.size() > 0) {
25131
49006bd0e69d 8044553: Fix raw and unchecked lint warnings in javax.sound.*
darcy
parents: 19207
diff changeset
   253
                ret = (eventsList.get(eventsList.size() - 1)).getTick();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    private static class ImmutableEndOfTrack extends MetaMessage {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        private ImmutableEndOfTrack() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            super(new byte[3]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            data[0] = (byte) META;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            data[1] = MidiUtils.META_END_OF_TRACK_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            data[2] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
40444
afabcfc2f3ef 8163949: Cleanup of classes which are related to JavaSound
serb
parents: 26037
diff changeset
   267
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        public void setMessage(int type, byte[] data, int length) throws InvalidMidiDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            throw new InvalidMidiDataException("cannot modify end of track message");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
}