jdk/src/java.desktop/share/classes/javax/sound/midi/MetaMessage.java
author ddehaven
Tue, 19 Aug 2014 10:32:16 -0700
changeset 26037 508779ce6619
parent 26003 jdk/src/share/classes/javax/sound/midi/MetaMessage.java@d630c97424bd
parent 25859 jdk/src/share/classes/javax/sound/midi/MetaMessage.java@3317bb8137f4
child 32865 f9cb6e427f9e
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
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: 2651
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: 2651
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: 2651
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2651
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2651
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
/**
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    29
 * A {@code MetaMessage} is a {@link MidiMessage} that is not meaningful to
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    30
 * synthesizers, but that can be stored in a MIDI file and interpreted by a
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    31
 * sequencer program. (See the discussion in the {@code MidiMessage} class
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    32
 * description.) The Standard MIDI Files specification defines various types of
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    33
 * meta-events, such as sequence number, lyric, cue point, and set tempo. There
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    34
 * are also meta-events for such information as lyrics, copyrights, tempo
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    35
 * indications, time and key signatures, markers, etc. For more information, see
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    36
 * the Standard MIDI Files 1.0 specification, which is part of the Complete MIDI
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    37
 * 1.0 Detailed Specification published by the MIDI Manufacturer's Association
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * (<a href = http://www.midi.org>http://www.midi.org</a>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p>
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    40
 * When data is being transported using MIDI wire protocol, a
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    41
 * {@link ShortMessage} with the status value {@code 0xFF} represents a system
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    42
 * reset message. In MIDI files, this same status value denotes a
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    43
 * {@code MetaMessage}. The types of meta-message are distinguished from each
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    44
 * other by the first byte that follows the status byte {@code 0xFF}. The
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    45
 * subsequent bytes are data bytes. As with system exclusive messages, there are
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    46
 * an arbitrary number of data bytes, depending on the type of
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    47
 * {@code MetaMessage}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author David Rivas
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author Kara Kytle
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    51
 * @see MetaEventListener
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public class MetaMessage extends MidiMessage {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    56
     * Status byte for {@code MetaMessage} (0xFF, or 255), which is used in MIDI
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    57
     * files. It has the same value as SYSTEM_RESET, which is used in the
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    58
     * real-time "MIDI wire" protocol.
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    59
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @see MidiMessage#getStatus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public static final int META                                                = 0xFF; // 255
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    65
     * The length of the actual message in the data array. This is used to
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    66
     * determine how many bytes of the data array is the message, and how many
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    67
     * are the status byte, the type byte, and the variable-length-int
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    68
     * describing the length of the message.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private int dataLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    73
     * Constructs a new {@code MetaMessage}. The contents of the message are not
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    74
     * set here; use {@link #setMessage(int, byte[], int) setMessage} to set
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    75
     * them subsequently.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public MetaMessage() {
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    78
        // Default meta message data: just the META status byte value
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    79
        this(new byte[]{(byte) META, 0});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
2651
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
    82
    /**
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    83
     * Constructs a new {@code MetaMessage} and sets the message parameters. The
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    84
     * contents of the message can be changed by using the {@code setMessage}
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    85
     * method.
2651
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
    86
     *
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    87
     * @param  type meta-message type (must be less than 128)
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    88
     * @param  data the data bytes in the MIDI message
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    89
     * @param  length an amount of bytes in the {@code data} byte array; it
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    90
     *         should be non-negative and less than or equal to
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    91
     *         {@code data.length}
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    92
     * @throws InvalidMidiDataException if the parameter values do not specify a
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
    93
     *         valid MIDI meta message
2651
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
    94
     * @see #setMessage(int, byte[], int)
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
    95
     * @see #getType()
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
    96
     * @see #getData()
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
    97
     * @since 1.7
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
    98
     */
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
    99
    public MetaMessage(int type, byte[] data, int length)
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
   100
            throws InvalidMidiDataException {
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
   101
        super(null);
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
   102
        setMessage(type, data, length); // can throw InvalidMidiDataException
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
   103
    }
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
   104
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
   106
     * Constructs a new {@code MetaMessage}.
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
   107
     *
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
   108
     * @param  data an array of bytes containing the complete message. The
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
   109
     *         message data may be changed using the {@code setMessage} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @see #setMessage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    protected MetaMessage(byte[] data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        super(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        //$$fb 2001-10-06: need to calculate dataLength. Fix for bug #4511796
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (data.length>=3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            dataLength=data.length-3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            int pos=2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            while (pos<data.length && (data[pos] & 0x80)!=0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                dataLength--; pos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
   125
     * Sets the message parameters for a {@code MetaMessage}. Since only one
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
   126
     * status byte value, {@code 0xFF}, is allowed for meta-messages, it does
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
   127
     * not need to be specified here. Calls to
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
   128
     * {@link MidiMessage#getStatus getStatus} return {@code 0xFF} for all
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
   129
     * meta-messages.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * <p>
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
   131
     * The {@code type} argument should be a valid value for the byte that
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
   132
     * follows the status byte in the {@code MetaMessage}. The {@code data}
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
   133
     * argument should contain all the subsequent bytes of the
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
   134
     * {@code MetaMessage}. In other words, the byte that specifies the type of
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
   135
     * {@code MetaMessage} is not considered a data byte.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
   137
     * @param  type meta-message type (must be less than 128)
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
   138
     * @param  data the data bytes in the MIDI message
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
   139
     * @param  length the number of bytes in the {@code data} byte array
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
   140
     * @throws InvalidMidiDataException if the parameter values do not specify a
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
   141
     *         valid MIDI meta message
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public void setMessage(int type, byte[] data, int length) throws InvalidMidiDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (type >= 128 || type < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            throw new InvalidMidiDataException("Invalid meta event with type " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if ((length > 0 && length > data.length) || length < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            throw new InvalidMidiDataException("length out of bounds: "+length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        this.length = 2 + getVarIntLength(length) + length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        this.dataLength = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        this.data = new byte[this.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        this.data[0] = (byte) META;        // status value for MetaMessages (meta events)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        this.data[1] = (byte) type;        // MetaMessage type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        writeVarInt(this.data, 2, length); // write the length as a variable int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            System.arraycopy(data, 0, this.data, this.length - this.dataLength, this.dataLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
   164
     * Obtains the type of the {@code MetaMessage}.
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
   165
     *
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
   166
     * @return an integer representing the {@code MetaMessage} type
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public int getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (length>=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            return data[1] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
   176
     * Obtains a copy of the data for the meta message. The returned array of
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
   177
     * bytes does not include the status byte or the message length data. The
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
   178
     * length of the data for the meta message is the length of the array. Note
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
   179
     * that the length of the entire message includes the status byte and the
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
   180
     * meta message type byte, and therefore may be longer than the returned
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
   181
     * array.
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
   182
     *
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
   183
     * @return array containing the meta message data
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @see MidiMessage#getLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public byte[] getData() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        byte[] returnedArray = new byte[dataLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        System.arraycopy(data, (length - dataLength), returnedArray, 0, dataLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return returnedArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
   193
     * Creates a new object of the same class and with the same contents as this
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
   194
     * object.
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 19207
diff changeset
   195
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @return a clone of this instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        byte[] newData = new byte[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        System.arraycopy(data, 0, newData, 0, newData.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        MetaMessage event = new MetaMessage(newData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    // HELPER METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    private int getVarIntLength(long value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        int length = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            value = value >> 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            length++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        } while (value > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        return length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    private final static long mask = 0x7F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    private void writeVarInt(byte[] data, int off, long value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        int shift=63; // number of bitwise left-shifts of mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        // first screen out leading zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        while ((shift > 0) && ((value & (mask << shift)) == 0)) shift-=7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        // then write actual values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        while (shift > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            data[off++]=(byte) (((value & (mask << shift)) >> shift) | 0x80);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            shift-=7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        data[off] = (byte) (value & mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
}