jdk/src/java.desktop/share/classes/javax/sound/midi/MidiMessage.java
author serb
Mon, 15 Aug 2016 20:08:55 +0300
changeset 40444 afabcfc2f3ef
parent 28059 e576535359cc
child 45025 9ad3afa82b5e
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
/*
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
     2
 * Copyright (c) 1998, 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
/**
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    29
 * {@code MidiMessage} is the base class for MIDI messages. They include not
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    30
 * only the standard MIDI messages that a synthesizer can respond to, but also
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    31
 * "meta-events" that can be used by sequencer programs. There are meta-events
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * for such information as lyrics, copyrights, tempo indications, time and key
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    33
 * signatures, markers, etc. For more information, see the Standard MIDI Files
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    34
 * 1.0 specification, which is part of the Complete MIDI 1.0 Detailed
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    35
 * Specification published by the MIDI Manufacturer's Association
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * (<a href = http://www.midi.org>http://www.midi.org</a>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    38
 * The base {@code MidiMessage} class provides access to three types of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * information about a MIDI message:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <li>The messages's status byte</li>
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    42
 * <li>The total length of the message in bytes (the status byte plus any data
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    43
 * bytes)</li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <li>A byte array containing the complete message</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    47
 * {@code MidiMessage} includes methods to get, but not set, these values.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Setting them is a subclass responsibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p>
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    50
 * <a name="integersVsBytes"></a> The MIDI standard expresses MIDI data in
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    51
 * bytes. However, because Java<sup>TM</sup> uses signed bytes, the Java Sound
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    52
 * API uses integers instead of bytes when expressing MIDI data. For example,
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    53
 * the {@link #getStatus()} method of {@code MidiMessage} returns MIDI status
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    54
 * bytes as integers. If you are processing MIDI data that originated outside
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 26037
diff changeset
    55
 * Java Sound and now is encoded as signed bytes, the bytes can be
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    56
 * converted to integers using this conversion:
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    57
 *
19207
3448b0cb4077 8022174: Fix doclint warnings in javax.sound
darcy
parents: 5506
diff changeset
    58
 * <center>{@code int i = (int)(byte & 0xFF)}</center>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <p>
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    60
 * If you simply need to pass a known MIDI byte value as a method parameter, it
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    61
 * can be expressed directly as an integer, using (for example) decimal or
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    62
 * hexadecimal notation. For instance, to pass the "active sensing" status byte
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * as the first argument to ShortMessage's
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    64
 * {@link ShortMessage#setMessage(int) setMessage(int)} method, you can express
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    65
 * it as 254 or 0xFE.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    67
 * @author David Rivas
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    68
 * @author Kara Kytle
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * @see Track
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * @see Sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * @see Receiver
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
public abstract class MidiMessage implements Cloneable {
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: 21278
diff changeset
    76
     * The MIDI message data. The first byte is the status byte for the message;
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    77
     * subsequent bytes up to the length of the message are data bytes for this
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    78
     * message.
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    79
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @see #getLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    protected byte[] data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    85
     * The number of bytes in the MIDI message, including the status byte and
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    86
     * any data bytes.
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    87
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @see #getLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    protected int length = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    93
     * Constructs a new {@code MidiMessage}. This protected constructor is
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    94
     * called by concrete subclasses, which should ensure that the data array
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    95
     * specifies a complete, valid MIDI message.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    97
     * @param  data an array of bytes containing the complete message. The
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
    98
     *         message data may be changed using the {@code setMessage} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @see #setMessage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    protected MidiMessage(byte[] data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        this.data = data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (data != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            this.length = data.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
   109
     * Sets the data for the MIDI message. This protected method is called by
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
   110
     * concrete subclasses, which should ensure that the data array specifies a
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
   111
     * complete, valid MIDI message.
19207
3448b0cb4077 8022174: Fix doclint warnings in javax.sound
darcy
parents: 5506
diff changeset
   112
     *
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
   113
     * @param  data the data bytes in the MIDI message
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
   114
     * @param  length the number of bytes in the data byte array
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
   115
     * @throws InvalidMidiDataException if the parameter values do not specify a
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
   116
     *         valid MIDI meta message
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
   118
    protected void setMessage(byte[] data, int length)
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
   119
            throws InvalidMidiDataException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (length < 0 || (length > 0 && length > data.length)) {
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
   121
            throw new IndexOutOfBoundsException(
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
   122
                    "length out of bounds: " + length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        this.length = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (this.data == null || this.data.length < this.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            this.data = new byte[this.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        System.arraycopy(data, 0, this.data, 0, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
   133
     * Obtains the MIDI message data. The first byte of the returned byte array
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
   134
     * is the status byte of the message. Any subsequent bytes up to the length
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
   135
     * of the message are data bytes. The byte array may have a length which is
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
   136
     * greater than that of the actual message; the total length of the message
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
   137
     * in bytes is reported by the {@link #getLength} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
   139
     * @return the byte array containing the complete {@code MidiMessage} data
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public byte[] getMessage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        byte[] returnedArray = new byte[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        System.arraycopy(data, 0, returnedArray, 0, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return returnedArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
   148
     * Obtains the status byte for the MIDI message. The status "byte" is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * represented as an integer; see the
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
   150
     * <a href="#integersVsBytes">discussion</a> in the {@code MidiMessage}
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
   151
     * class description.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @return the integer representation of this event's status byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public int getStatus() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            return (data[0] & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
   163
     * Obtains the total length of the MIDI message in bytes. A MIDI message
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
   164
     * consists of one status byte and zero or more data bytes. The return value
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
   165
     * ranges from 1 for system real-time messages, to 2 or 3 for channel
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
   166
     * messages, to any value for meta and system exclusive messages.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @return the length of the message in bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public int getLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
26003
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
   175
     * 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: 21278
diff changeset
   176
     * object.
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
   177
     *
d630c97424bd 8050852: Javadoc cleanup of javax.sound.midi package
serb
parents: 21278
diff changeset
   178
     * @return a clone of this instance
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
40444
afabcfc2f3ef 8163949: Cleanup of classes which are related to JavaSound
serb
parents: 28059
diff changeset
   180
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public abstract Object clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
}