Path: blob/master/src/java.desktop/share/classes/javax/sound/midi/MidiMessage.java
41159 views
/*1* Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.sound.midi;2627/**28* {@code MidiMessage} is the base class for MIDI messages. They include not29* only the standard MIDI messages that a synthesizer can respond to, but also30* "meta-events" that can be used by sequencer programs. There are meta-events31* for such information as lyrics, copyrights, tempo indications, time and key32* signatures, markers, etc. For more information, see the Standard MIDI Files33* 1.0 specification, which is part of the Complete MIDI 1.0 Detailed34* Specification published by the MIDI Manufacturer's Association35* (<a href = http://www.midi.org>http://www.midi.org</a>).36* <p>37* The base {@code MidiMessage} class provides access to three types of38* information about a MIDI message:39* <ul>40* <li>The messages's status byte41* <li>The total length of the message in bytes (the status byte plus any data42* bytes)43* <li>A byte array containing the complete message44* </ul>45*46* {@code MidiMessage} includes methods to get, but not set, these values.47* Setting them is a subclass responsibility.48* <p>49* <a id="integersVsBytes"></a>The MIDI standard expresses MIDI data in bytes.50* However, because Java uses signed bytes, the Java Sound API uses51* integers instead of bytes when expressing MIDI data. For example, the52* {@link #getStatus()} method of {@code MidiMessage} returns MIDI status bytes53* as integers. If you are processing MIDI data that originated outside Java54* Sound and now is encoded as signed bytes, the bytes can be converted to55* integers using this conversion:56* <p style="text-align:center">57* {@code int i = (int)(byte & 0xFF)}58* <p>59* If you simply need to pass a known MIDI byte value as a method parameter, it60* can be expressed directly as an integer, using (for example) decimal or61* hexadecimal notation. For instance, to pass the "active sensing" status byte62* as the first argument to {@code ShortMessage}'s63* {@link ShortMessage#setMessage(int) setMessage(int)} method, you can express64* it as 254 or 0xFE.65*66* @author David Rivas67* @author Kara Kytle68* @see Track69* @see Sequence70* @see Receiver71*/72public abstract class MidiMessage implements Cloneable {7374/**75* The MIDI message data. The first byte is the status byte for the message;76* subsequent bytes up to the length of the message are data bytes for this77* message.78*79* @see #getLength80*/81protected byte[] data;8283/**84* The number of bytes in the MIDI message, including the status byte and85* any data bytes.86*87* @see #getLength88*/89protected int length = 0;9091/**92* Constructs a new {@code MidiMessage}. This protected constructor is93* called by concrete subclasses, which should ensure that the data array94* specifies a complete, valid MIDI message.95*96* @param data an array of bytes containing the complete message. The97* message data may be changed using the {@code setMessage} method.98* @see #setMessage99*/100protected MidiMessage(byte[] data) {101this.data = data;102if (data != null) {103this.length = data.length;104}105}106107/**108* Sets the data for the MIDI message. This protected method is called by109* concrete subclasses, which should ensure that the data array specifies a110* complete, valid MIDI message.111*112* @param data the data bytes in the MIDI message113* @param length the number of bytes in the data byte array114* @throws InvalidMidiDataException if the parameter values do not specify a115* valid MIDI meta message116*/117protected void setMessage(byte[] data, int length)118throws InvalidMidiDataException {119if (length < 0 || (length > 0 && length > data.length)) {120throw new IndexOutOfBoundsException(121"length out of bounds: " + length);122}123this.length = length;124125if (this.data == null || this.data.length < this.length) {126this.data = new byte[this.length];127}128System.arraycopy(data, 0, this.data, 0, length);129}130131/**132* Obtains the MIDI message data. The first byte of the returned byte array133* is the status byte of the message. Any subsequent bytes up to the length134* of the message are data bytes. The byte array may have a length which is135* greater than that of the actual message; the total length of the message136* in bytes is reported by the {@link #getLength} method.137*138* @return the byte array containing the complete {@code MidiMessage} data139*/140public byte[] getMessage() {141byte[] returnedArray = new byte[length];142System.arraycopy(data, 0, returnedArray, 0, length);143return returnedArray;144}145146/**147* Obtains the status byte for the MIDI message. The status "byte" is148* represented as an integer; see the149* <a href="#integersVsBytes">discussion</a> in the {@code MidiMessage}150* class description.151*152* @return the integer representation of this event's status byte153*/154public int getStatus() {155if (length > 0) {156return (data[0] & 0xFF);157}158return 0;159}160161/**162* Obtains the total length of the MIDI message in bytes. A MIDI message163* consists of one status byte and zero or more data bytes. The return value164* ranges from 1 for system real-time messages, to 2 or 3 for channel165* messages, to any value for meta and system exclusive messages.166*167* @return the length of the message in bytes168*/169public int getLength() {170return length;171}172173/**174* Creates a new object of the same class and with the same contents as this175* object.176*177* @return a clone of this instance178*/179@Override180public abstract Object clone();181}182183184