Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/sound/midi/MidiMessage.java
38830 views
/*1* Copyright (c) 1998, 2013, 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</code> is the base class for MIDI messages. They include29* not 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 Files 1.033* specification, which is part of the Complete MIDI 1.0 Detailed Specification34* 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</code> class provides access to three types of38* information about a MIDI message:39* <ul>40* <li>The messages's status byte</li>41* <li>The total length of the message in bytes (the status byte plus any data bytes)</li>42* <li>A byte array containing the complete message</li>43* </ul>44*45* <code>MidiMessage</code> includes methods to get, but not set, these values.46* Setting them is a subclass responsibility.47* <p>48* <a name="integersVsBytes"></a>49* The MIDI standard expresses MIDI data in bytes. However, because50* Java<sup>TM</sup> uses signed bytes, the Java Sound API uses integers51* instead of bytes when expressing MIDI data. For example, the52* {@link #getStatus()} method of53* <code>MidiMessage</code> returns MIDI status bytes as integers. If you are54* processing MIDI data that originated outside Java Sound and now55* is encoded as signed bytes, the bytes can56* can be converted to integers using this conversion:57* <center>{@code int i = (int)(byte & 0xFF)}</center>58* <p>59* If you simply need to pass a known MIDI byte value as a method parameter,60* it 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 ShortMessage's63* {@link ShortMessage#setMessage(int) setMessage(int)}64* method, you can express it as 254 or 0xFE.65*66* @see Track67* @see Sequence68* @see Receiver69*70* @author David Rivas71* @author Kara Kytle72*/7374public abstract class MidiMessage implements Cloneable {7576// Instance variables7778/**79* The MIDI message data. The first byte is the status80* byte for the message; subsequent bytes up to the length81* of the message are data bytes for this message.82* @see #getLength83*/84protected byte[] data;858687/**88* The number of bytes in the MIDI message, including the89* status byte and any data bytes.90* @see #getLength91*/92protected int length = 0;939495/**96* Constructs a new <code>MidiMessage</code>. This protected97* constructor is called by concrete subclasses, which should98* ensure that the data array specifies a complete, valid MIDI99* message.100*101* @param data an array of bytes containing the complete message.102* The message data may be changed using the <code>setMessage</code>103* method.104*105* @see #setMessage106*/107protected MidiMessage(byte[] data) {108this.data = data;109if (data != null) {110this.length = data.length;111}112}113114115/**116* Sets the data for the MIDI message. This protected117* method is called by concrete subclasses, which should118* ensure that the data array specifies a complete, valid MIDI119* message.120*121* @param data the data bytes in the MIDI message122* @param length the number of bytes in the data byte array123* @throws InvalidMidiDataException if the parameter values do not specify a valid MIDI meta message124*/125protected void setMessage(byte[] data, int length) throws InvalidMidiDataException {126if (length < 0 || (length > 0 && length > data.length)) {127throw new IndexOutOfBoundsException("length out of bounds: "+length);128}129this.length = length;130131if (this.data == null || this.data.length < this.length) {132this.data = new byte[this.length];133}134System.arraycopy(data, 0, this.data, 0, length);135}136137138/**139* Obtains the MIDI message data. The first byte of the returned byte140* array is the status byte of the message. Any subsequent bytes up to141* the length of the message are data bytes. The byte array may have a142* length which is greater than that of the actual message; the total143* length of the message in bytes is reported by the <code>{@link #getLength}</code>144* method.145*146* @return the byte array containing the complete <code>MidiMessage</code> data147*/148public byte[] getMessage() {149byte[] returnedArray = new byte[length];150System.arraycopy(data, 0, returnedArray, 0, length);151return returnedArray;152}153154155/**156* Obtains the status byte for the MIDI message. The status "byte" is157* represented as an integer; see the158* <a href="#integersVsBytes">discussion</a> in the159* <code>MidiMessage</code> class description.160*161* @return the integer representation of this event's status byte162*/163public int getStatus() {164if (length > 0) {165return (data[0] & 0xFF);166}167return 0;168}169170171/**172* Obtains the total length of the MIDI message in bytes. A173* MIDI message consists of one status byte and zero or more174* data bytes. The return value ranges from 1 for system real-time messages,175* to 2 or 3 for channel messages, to any value for meta and system176* exclusive messages.177*178* @return the length of the message in bytes179*/180public int getLength() {181return length;182}183184185/**186* Creates a new object of the same class and with the same contents187* as this object.188* @return a clone of this instance.189*/190public abstract Object clone();191}192193194