Path: blob/master/src/java.desktop/share/classes/javax/sound/midi/ShortMessage.java
41159 views
/*1* Copyright (c) 1998, 2018, 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* A {@code ShortMessage} contains a MIDI message that has at most two data29* bytes following its status byte. The types of MIDI message that satisfy this30* criterion are channel voice, channel mode, system common, and system31* real-time--in other words, everything except system exclusive and32* meta-events. The {@code ShortMessage} class provides methods for getting and33* setting the contents of the MIDI message.34* <p>35* A number of {@code ShortMessage} methods have integer parameters by which you36* specify a MIDI status or data byte. If you know the numeric value, you can37* express it directly. For system common and system real-time messages, you can38* often use the corresponding fields of {@code ShortMessage}, such as39* {@link #SYSTEM_RESET SYSTEM_RESET}. For channel messages, the upper four bits40* of the status byte are specified by a command value and the lower four bits41* are specified by a MIDI channel number. To convert incoming MIDI data bytes42* that are in the form of Java's signed bytes, you can use the43* <a href="MidiMessage.html#integersVsBytes">conversion code</a> given in the44* {@link MidiMessage} class description.45*46* @author David Rivas47* @author Kara Kytle48* @author Florian Bomers49* @see SysexMessage50* @see MetaMessage51*/52public class ShortMessage extends MidiMessage {5354// Status byte defines5556// System common messages5758/**59* Status byte for MIDI Time Code Quarter Frame message (0xF1, or 241).60*61* @see MidiMessage#getStatus62*/63public static final int MIDI_TIME_CODE = 0xF1; // 2416465/**66* Status byte for Song Position Pointer message (0xF2, or 242).67*68* @see MidiMessage#getStatus69*/70public static final int SONG_POSITION_POINTER = 0xF2; // 2427172/**73* Status byte for MIDI Song Select message (0xF3, or 243).74*75* @see MidiMessage#getStatus76*/77public static final int SONG_SELECT = 0xF3; // 2437879/**80* Status byte for Tune Request message (0xF6, or 246).81*82* @see MidiMessage#getStatus83*/84public static final int TUNE_REQUEST = 0xF6; // 2468586/**87* Status byte for End of System Exclusive message (0xF7, or 247).88*89* @see MidiMessage#getStatus90*/91public static final int END_OF_EXCLUSIVE = 0xF7; // 2479293// System real-time messages9495/**96* Status byte for Timing Clock message (0xF8, or 248).97*98* @see MidiMessage#getStatus99*/100public static final int TIMING_CLOCK = 0xF8; // 248101102/**103* Status byte for Start message (0xFA, or 250).104*105* @see MidiMessage#getStatus106*/107public static final int START = 0xFA; // 250108109/**110* Status byte for Continue message (0xFB, or 251).111*112* @see MidiMessage#getStatus113*/114public static final int CONTINUE = 0xFB; // 251115116/**117* Status byte for Stop message (0xFC, or 252).118*119* @see MidiMessage#getStatus120*/121public static final int STOP = 0xFC; //252122123/**124* Status byte for Active Sensing message (0xFE, or 254).125*126* @see MidiMessage#getStatus127*/128public static final int ACTIVE_SENSING = 0xFE; // 254129130/**131* Status byte for System Reset message (0xFF, or 255).132*133* @see MidiMessage#getStatus134*/135public static final int SYSTEM_RESET = 0xFF; // 255136137// Channel voice message upper nibble defines138139/**140* Command value for Note Off message (0x80, or 128).141*/142public static final int NOTE_OFF = 0x80; // 128143144/**145* Command value for Note On message (0x90, or 144).146*/147public static final int NOTE_ON = 0x90; // 144148149/**150* Command value for Polyphonic Key Pressure (Aftertouch) message (0xA0, or151* 160).152*/153public static final int POLY_PRESSURE = 0xA0; // 160154155/**156* Command value for Control Change message (0xB0, or 176).157*/158public static final int CONTROL_CHANGE = 0xB0; // 176159160/**161* Command value for Program Change message (0xC0, or 192).162*/163public static final int PROGRAM_CHANGE = 0xC0; // 192164165/**166* Command value for Channel Pressure (Aftertouch) message (0xD0, or 208).167*/168public static final int CHANNEL_PRESSURE = 0xD0; // 208169170/**171* Command value for Pitch Bend message (0xE0, or 224).172*/173public static final int PITCH_BEND = 0xE0; // 224174175/**176* Constructs a new {@code ShortMessage}. The contents of the new message177* are guaranteed to specify a valid MIDI message. Subsequently, you may set178* the contents of the message using one of the {@code setMessage} methods.179*180* @see #setMessage181*/182public ShortMessage() {183this(new byte[3]);184// Default message data: NOTE_ON on Channel 0 with max volume185data[0] = (byte) (NOTE_ON & 0xFF);186data[1] = (byte) 64;187data[2] = (byte) 127;188length = 3;189}190191/**192* Constructs a new {@code ShortMessage} which represents a MIDI message193* that takes no data bytes. The contents of the message can be changed by194* using one of the {@code setMessage} methods.195*196* @param status the MIDI status byte197* @throws InvalidMidiDataException if {@code status} does not specify a198* valid MIDI status byte for a message that requires no data bytes199* @see #setMessage(int)200* @see #setMessage(int, int, int)201* @see #setMessage(int, int, int, int)202* @see #getStatus()203* @since 1.7204*/205public ShortMessage(int status) throws InvalidMidiDataException {206super(null);207setMessage(status); // can throw InvalidMidiDataException208}209210/**211* Constructs a new {@code ShortMessage} which represents a MIDI message212* that takes up to two data bytes. If the message only takes one data byte,213* the second data byte is ignored. If the message does not take any data214* bytes, both data bytes are ignored. The contents of the message can be215* changed by using one of the {@code setMessage} methods.216*217* @param status the MIDI status byte218* @param data1 the first data byte219* @param data2 the second data byte220* @throws InvalidMidiDataException if the status byte or all data bytes221* belonging to the message do not specify a valid MIDI message222* @see #setMessage(int)223* @see #setMessage(int, int, int)224* @see #setMessage(int, int, int, int)225* @see #getStatus()226* @see #getData1()227* @see #getData2()228* @since 1.7229*/230public ShortMessage(int status, int data1, int data2)231throws InvalidMidiDataException {232super(null);233setMessage(status, data1, data2); // can throw InvalidMidiDataException234}235236/**237* Constructs a new {@code ShortMessage} which represents a channel MIDI238* message that takes up to two data bytes. If the message only takes one239* data byte, the second data byte is ignored. If the message does not take240* any data bytes, both data bytes are ignored. The contents of the message241* can be changed by using one of the {@code setMessage} methods.242*243* @param command the MIDI command represented by this message244* @param channel the channel associated with the message245* @param data1 the first data byte246* @param data2 the second data byte247* @throws InvalidMidiDataException if the command value, channel value or248* all data bytes belonging to the message do not specify a valid249* MIDI message250* @see #setMessage(int)251* @see #setMessage(int, int, int)252* @see #setMessage(int, int, int, int)253* @see #getCommand()254* @see #getChannel()255* @see #getData1()256* @see #getData2()257* @since 1.7258*/259public ShortMessage(int command, int channel, int data1, int data2)260throws InvalidMidiDataException {261super(null);262setMessage(command, channel, data1, data2);263}264265/**266* Constructs a new {@code ShortMessage}.267*268* @param data an array of bytes containing the complete message. The269* message data may be changed using the {@code setMessage} method.270* @see #setMessage271*/272// $$fb this should throw an Exception in case of an illegal message!273protected ShortMessage(byte[] data) {274// $$fb this may set an invalid message.275// Can't correct without compromising compatibility276super(data);277}278279/**280* Sets the parameters for a MIDI message that takes no data bytes.281*282* @param status the MIDI status byte283* @throws InvalidMidiDataException if {@code status} does not specify a284* valid MIDI status byte for a message that requires no data bytes285* @see #setMessage(int, int, int)286* @see #setMessage(int, int, int, int)287*/288public void setMessage(int status) throws InvalidMidiDataException {289// check for valid values290int dataLength = getDataLength(status); // can throw InvalidMidiDataException291if (dataLength != 0) {292throw new InvalidMidiDataException("Status byte; " + status + " requires " + dataLength + " data bytes");293}294setMessage(status, 0, 0);295}296297/**298* Sets the parameters for a MIDI message that takes one or two data bytes.299* If the message takes only one data byte, the second data byte is ignored;300* if the message does not take any data bytes, both data bytes are ignored.301*302* @param status the MIDI status byte303* @param data1 the first data byte304* @param data2 the second data byte305* @throws InvalidMidiDataException if the status byte, or all data bytes306* belonging to the message, do not specify a valid MIDI message307* @see #setMessage(int, int, int, int)308* @see #setMessage(int)309*/310public void setMessage(int status, int data1, int data2) throws InvalidMidiDataException {311// check for valid values312int dataLength = getDataLength(status); // can throw InvalidMidiDataException313if (dataLength > 0) {314if (data1 < 0 || data1 > 127) {315throw new InvalidMidiDataException("data1 out of range: " + data1);316}317if (dataLength > 1) {318if (data2 < 0 || data2 > 127) {319throw new InvalidMidiDataException("data2 out of range: " + data2);320}321}322}323324325// set the length326length = dataLength + 1;327// re-allocate array if ShortMessage(byte[]) constructor gave array with fewer elements328if (data == null || data.length < length) {329data = new byte[3];330}331332// set the data333data[0] = (byte) (status & 0xFF);334if (length > 1) {335data[1] = (byte) (data1 & 0xFF);336if (length > 2) {337data[2] = (byte) (data2 & 0xFF);338}339}340}341342/**343* Sets the short message parameters for a channel message which takes up to344* two data bytes. If the message only takes one data byte, the second data345* byte is ignored; if the message does not take any data bytes, both data346* bytes are ignored.347*348* @param command the MIDI command represented by this message349* @param channel the channel associated with the message350* @param data1 the first data byte351* @param data2 the second data byte352* @throws InvalidMidiDataException if the status byte or all data bytes353* belonging to the message, do not specify a valid MIDI message354* @see #setMessage(int, int, int)355* @see #setMessage(int)356* @see #getCommand357* @see #getChannel358* @see #getData1359* @see #getData2360*/361public void setMessage(int command, int channel, int data1, int data2) throws InvalidMidiDataException {362// check for valid values363if (command >= 0xF0 || command < 0x80) {364throw new InvalidMidiDataException("command out of range: 0x" + Integer.toHexString(command));365}366if ((channel & 0xFFFFFFF0) != 0) { // <=> (channel<0 || channel>15)367throw new InvalidMidiDataException("channel out of range: " + channel);368}369setMessage((command & 0xF0) | (channel & 0x0F), data1, data2);370}371372/**373* Obtains the MIDI channel associated with this event. This method assumes374* that the event is a MIDI channel message; if not, the return value will375* not be meaningful.376*377* @return MIDI channel associated with the message378* @see #setMessage(int, int, int, int)379*/380public int getChannel() {381// this returns 0 if an invalid message is set382return (getStatus() & 0x0F);383}384385/**386* Obtains the MIDI command associated with this event. This method assumes387* that the event is a MIDI channel message; if not, the return value will388* not be meaningful.389*390* @return the MIDI command associated with this event391* @see #setMessage(int, int, int, int)392*/393public int getCommand() {394// this returns 0 if an invalid message is set395return (getStatus() & 0xF0);396}397398/**399* Obtains the first data byte in the message.400*401* @return the value of the {@code data1} field402* @see #setMessage(int, int, int)403*/404public int getData1() {405if (length > 1) {406return (data[1] & 0xFF);407}408return 0;409}410411/**412* Obtains the second data byte in the message.413*414* @return the value of the {@code data2} field415* @see #setMessage(int, int, int)416*/417public int getData2() {418if (length > 2) {419return (data[2] & 0xFF);420}421return 0;422}423424/**425* Creates a new object of the same class and with the same contents as this426* object.427*428* @return a clone of this instance429*/430@Override431public Object clone() {432byte[] newData = new byte[length];433System.arraycopy(data, 0, newData, 0, newData.length);434return new ShortMessage(newData);435}436437/**438* Retrieves the number of data bytes associated with a particular status439* byte value.440*441* @param status status byte value, which must represent a short MIDI442* message443* @return data length in bytes (0, 1, or 2)444* @throws InvalidMidiDataException if the {@code status} argument does not445* represent the status byte for any short message446*/447protected final int getDataLength(int status) throws InvalidMidiDataException {448// system common and system real-time messages449switch(status) {450case 0xF6: // Tune Request451case 0xF7: // EOX452// System real-time messages453case 0xF8: // Timing Clock454case 0xF9: // Undefined455case 0xFA: // Start456case 0xFB: // Continue457case 0xFC: // Stop458case 0xFD: // Undefined459case 0xFE: // Active Sensing460case 0xFF: // System Reset461return 0;462case 0xF1: // MTC Quarter Frame463case 0xF3: // Song Select464return 1;465case 0xF2: // Song Position Pointer466return 2;467default:468}469470// channel voice and mode messages471switch(status & 0xF0) {472case 0x80:473case 0x90:474case 0xA0:475case 0xB0:476case 0xE0:477return 2;478case 0xC0:479case 0xD0:480return 1;481default:482throw new InvalidMidiDataException("Invalid status byte: " + status);483}484}485}486487488