src/share/classes/javax/sound/midi/ShortMessage.java

Print this page


   1 /*
   2  * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.sound.midi;
  27 
  28 /**
  29  * A <code>ShortMessage</code> contains a MIDI message that has at most
  30  * two data bytes following its status byte.  The types of MIDI message
  31  * that satisfy this criterion are channel voice, channel mode, system common,
  32  * and system real-time--in other words, everything except system exclusive
  33  * and meta-events.  The <code>ShortMessage</code> class provides methods
  34  * for getting and setting the contents of the MIDI message.
  35  * <p>
  36  * A number of <code>ShortMessage</code> methods have integer parameters by which
  37  * you specify a MIDI status or data byte.  If you know the numeric value, you
  38  * can express it directly.  For system common and system real-time messages,
  39  * you can often use the corresponding fields of <code>ShortMessage</code>, such as
  40  * {@link #SYSTEM_RESET SYSTEM_RESET}.  For channel messages,
  41  * the upper four bits of the status byte are specified by a command value and
  42  * the lower four bits are specified by a MIDI channel number. To
  43  * convert incoming MIDI data bytes that are in the form of Java's signed bytes,
  44  * you can use the <A HREF="MidiMessage.html#integersVsBytes">conversion code</A>
  45  * given in the <code>{@link MidiMessage}</code> class description.
  46  *
  47  * @see SysexMessage
  48  * @see MetaMessage
  49  *
  50  * @author David Rivas
  51  * @author Kara Kytle
  52  * @author Florian Bomers


  53  */
  54 
  55 public class ShortMessage extends MidiMessage {
  56 
  57 
  58     // Status byte defines
  59 
  60 
  61     // System common messages
  62 
  63     /**
  64      * Status byte for MIDI Time Code Quarter Frame message (0xF1, or 241).

  65      * @see MidiMessage#getStatus
  66      */
  67     public static final int MIDI_TIME_CODE                              = 0xF1; // 241
  68 
  69     /**
  70      * Status byte for Song Position Pointer message (0xF2, or 242).

  71      * @see MidiMessage#getStatus
  72      */
  73     public static final int SONG_POSITION_POINTER               = 0xF2; // 242
  74 
  75     /**
  76      * Status byte for MIDI Song Select message (0xF3, or 243).

  77      * @see MidiMessage#getStatus
  78      */
  79     public static final int SONG_SELECT                                 = 0xF3; // 243
  80 
  81     /**
  82      * Status byte for Tune Request message (0xF6, or 246).

  83      * @see MidiMessage#getStatus
  84      */
  85     public static final int TUNE_REQUEST                                = 0xF6; // 246
  86 
  87     /**
  88      * Status byte for End of System Exclusive message (0xF7, or 247).

  89      * @see MidiMessage#getStatus
  90      */
  91     public static final int END_OF_EXCLUSIVE                    = 0xF7; // 247
  92 
  93 
  94     // System real-time messages
  95 
  96     /**
  97      * Status byte for Timing Clock message (0xF8, or 248).

  98      * @see MidiMessage#getStatus
  99      */
 100     public static final int TIMING_CLOCK                                = 0xF8; // 248
 101 
 102     /**
 103      * Status byte for Start message (0xFA, or 250).

 104      * @see MidiMessage#getStatus
 105      */
 106     public static final int START                                               = 0xFA; // 250
 107 
 108     /**
 109      * Status byte for Continue message (0xFB, or 251).

 110      * @see MidiMessage#getStatus
 111      */
 112     public static final int CONTINUE                                    = 0xFB; // 251
 113 
 114     /**
 115      * Status byte for Stop message (0xFC, or 252).

 116      * @see MidiMessage#getStatus
 117      */
 118     public static final int STOP                                                = 0xFC; //252
 119 
 120     /**
 121      * Status byte for Active Sensing message (0xFE, or 254).

 122      * @see MidiMessage#getStatus
 123      */
 124     public static final int ACTIVE_SENSING                              = 0xFE; // 254
 125 
 126     /**
 127      * Status byte for System Reset message (0xFF, or 255).

 128      * @see MidiMessage#getStatus
 129      */
 130     public static final int SYSTEM_RESET                                = 0xFF; // 255
 131 
 132 
 133     // Channel voice message upper nibble defines
 134 
 135     /**
 136      * Command value for Note Off message (0x80, or 128)
 137      */
 138     public static final int NOTE_OFF                                    = 0x80;  // 128
 139 
 140     /**
 141      * Command value for Note On message (0x90, or 144)
 142      */
 143     public static final int NOTE_ON                                             = 0x90;  // 144
 144 
 145     /**
 146      * Command value for Polyphonic Key Pressure (Aftertouch) message (0xA0, or 160)

 147      */
 148     public static final int POLY_PRESSURE                               = 0xA0;  // 160
 149 
 150     /**
 151      * Command value for Control Change message (0xB0, or 176)
 152      */
 153     public static final int CONTROL_CHANGE                              = 0xB0;  // 176
 154 
 155     /**
 156      * Command value for Program Change message (0xC0, or 192)
 157      */
 158     public static final int PROGRAM_CHANGE                              = 0xC0;  // 192
 159 
 160     /**
 161      * Command value for Channel Pressure (Aftertouch) message (0xD0, or 208)
 162      */
 163     public static final int CHANNEL_PRESSURE                    = 0xD0;  // 208
 164 
 165     /**
 166      * Command value for Pitch Bend message (0xE0, or 224)
 167      */
 168     public static final int PITCH_BEND                                  = 0xE0;  // 224
 169 
 170 
 171     // Instance variables
 172 
 173     /**
 174      * Constructs a new <code>ShortMessage</code>.  The
 175      * contents of the new message are guaranteed to specify
 176      * a valid MIDI message.  Subsequently, you may set the
 177      * contents of the message using one of the <code>setMessage</code>
 178      * methods.
 179      * @see #setMessage
 180      */
 181     public ShortMessage() {
 182         this(new byte[3]);
 183         // Default message data: NOTE_ON on Channel 0 with max volume
 184         data[0] = (byte) (NOTE_ON & 0xFF);
 185         data[1] = (byte) 64;
 186         data[2] = (byte) 127;
 187         length = 3;
 188     }
 189 
 190     /**
 191      * Constructs a new {@code ShortMessage} which represents a MIDI
 192      * message that takes no data bytes.
 193      * The contents of the message can be changed by using one of
 194      * the {@code setMessage} methods.
 195      *
 196      * @param status the MIDI status byte
 197      * @throws InvalidMidiDataException if {@code status} does not specify
 198      *     a valid MIDI status byte for a message that requires no data bytes
 199      * @see #setMessage(int)
 200      * @see #setMessage(int, int, int)
 201      * @see #setMessage(int, int, int, int)
 202      * @see #getStatus()
 203      * @since 1.7
 204      */
 205     public ShortMessage(int status) throws InvalidMidiDataException {
 206         super(null);
 207         setMessage(status); // can throw InvalidMidiDataException
 208     }
 209 
 210     /**
 211      * Constructs a new {@code ShortMessage} which represents a MIDI message
 212      * 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
 214      * any data bytes, both data bytes are ignored.
 215      * The contents of the message can be changed by using one of
 216      * the {@code setMessage} methods.
 217      *
 218      * @param status   the MIDI status byte
 219      * @param data1    the first data byte
 220      * @param data2    the second data byte
 221      * @throws InvalidMidiDataException if the status byte or all data bytes
 222      *     belonging to the message do not specify a valid MIDI message
 223      * @see #setMessage(int)
 224      * @see #setMessage(int, int, int)
 225      * @see #setMessage(int, int, int, int)
 226      * @see #getStatus()
 227      * @see #getData1()
 228      * @see #getData2()
 229      * @since 1.7
 230      */
 231     public ShortMessage(int status, int data1, int data2)
 232             throws InvalidMidiDataException {
 233         super(null);
 234         setMessage(status, data1, data2); // can throw InvalidMidiDataException
 235     }
 236 
 237     /**
 238      * Constructs a new {@code ShortMessage} which represents a channel
 239      * MIDI message that takes up to two data bytes. If the message only takes
 240      * one data byte, the second data byte is ignored. If the message does not
 241      * take any data bytes, both data bytes are ignored.
 242      * The contents of the message can be changed by using one of
 243      * the {@code setMessage} methods.
 244      *
 245      * @param command  the MIDI command represented by this message
 246      * @param channel  the channel associated with the message
 247      * @param data1    the first data byte
 248      * @param data2    the second data byte
 249      * @throws InvalidMidiDataException if the command value, channel value
 250      *     or all data bytes belonging to the message do not specify
 251      *     a valid MIDI message
 252      * @see #setMessage(int)
 253      * @see #setMessage(int, int, int)
 254      * @see #setMessage(int, int, int, int)
 255      * @see #getCommand()
 256      * @see #getChannel()
 257      * @see #getData1()
 258      * @see #getData2()
 259      * @since 1.7
 260      */
 261     public ShortMessage(int command, int channel, int data1, int data2)
 262             throws InvalidMidiDataException {
 263         super(null);
 264         setMessage(command, channel, data1, data2);
 265     }
 266 
 267 
 268     /**
 269      * Constructs a new <code>ShortMessage</code>.
 270      * @param data an array of bytes containing the complete message.
 271      * The message data may be changed using the <code>setMessage</code>
 272      * method.
 273      * @see #setMessage
 274      */
 275     // $$fb this should throw an Exception in case of an illegal message!
 276     protected ShortMessage(byte[] data) {
 277         // $$fb this may set an invalid message.
 278         // Can't correct without compromising compatibility
 279         super(data);
 280     }
 281 
 282 
 283     /**
 284      * Sets the parameters for a MIDI message that takes no data bytes.

 285      * @param status    the MIDI status byte
 286      * @throws  InvalidMidiDataException if <code>status</code> does not
 287      * specify a valid MIDI status byte for a message that requires no data bytes.
 288      * @see #setMessage(int, int, int)
 289      * @see #setMessage(int, int, int, int)
 290      */
 291     public void setMessage(int status) throws InvalidMidiDataException {
 292         // check for valid values
 293         int dataLength = getDataLength(status); // can throw InvalidMidiDataException
 294         if (dataLength != 0) {
 295             throw new InvalidMidiDataException("Status byte; " + status + " requires " + dataLength + " data bytes");
 296         }
 297         setMessage(status, 0, 0);
 298     }
 299 
 300 
 301     /**
 302      * Sets the  parameters for a MIDI message that takes one or two data
 303      * bytes.  If the message takes only one data byte, the second data
 304      * byte is ignored; if the message does not take any data bytes, both
 305      * data bytes are ignored.
 306      *
 307      * @param status    the MIDI status byte
 308      * @param data1             the first data byte
 309      * @param data2             the second data byte
 310      * @throws  InvalidMidiDataException if the
 311      * the status byte, or all data bytes belonging to the message, do
 312      * not specify a valid MIDI message.
 313      * @see #setMessage(int, int, int, int)
 314      * @see #setMessage(int)
 315      */
 316     public void setMessage(int status, int data1, int data2) throws InvalidMidiDataException {
 317         // check for valid values
 318         int dataLength = getDataLength(status); // can throw InvalidMidiDataException
 319         if (dataLength > 0) {
 320             if (data1 < 0 || data1 > 127) {
 321                 throw new InvalidMidiDataException("data1 out of range: " + data1);
 322             }
 323             if (dataLength > 1) {
 324                 if (data2 < 0 || data2 > 127) {
 325                     throw new InvalidMidiDataException("data2 out of range: " + data2);
 326                 }
 327             }
 328         }
 329 
 330 
 331         // set the length
 332         length = dataLength + 1;
 333         // re-allocate array if ShortMessage(byte[]) constructor gave array with fewer elements
 334         if (data == null || data.length < length) {
 335             data = new byte[3];
 336         }
 337 
 338         // set the data
 339         data[0] = (byte) (status & 0xFF);
 340         if (length > 1) {
 341             data[1] = (byte) (data1 & 0xFF);
 342             if (length > 2) {
 343                 data[2] = (byte) (data2 & 0xFF);
 344             }
 345         }
 346     }
 347 
 348 
 349     /**
 350      * Sets the short message parameters for a  channel message
 351      * which takes up to two data bytes.  If the message only
 352      * takes one data byte, the second data byte is ignored; if
 353      * the message does not take any data bytes, both data bytes
 354      * are ignored.
 355      *
 356      * @param command   the MIDI command represented by this message
 357      * @param channel   the channel associated with the message
 358      * @param data1             the first data byte
 359      * @param data2             the second data byte
 360      * @throws          InvalidMidiDataException if the
 361      * status byte or all data bytes belonging to the message, do
 362      * not specify a valid MIDI message
 363      *
 364      * @see #setMessage(int, int, int)
 365      * @see #setMessage(int)
 366      * @see #getCommand
 367      * @see #getChannel
 368      * @see #getData1
 369      * @see #getData2
 370      */
 371     public void setMessage(int command, int channel, int data1, int data2) throws InvalidMidiDataException {
 372         // check for valid values
 373         if (command >= 0xF0 || command < 0x80) {
 374             throw new InvalidMidiDataException("command out of range: 0x" + Integer.toHexString(command));
 375         }
 376         if ((channel & 0xFFFFFFF0) != 0) { // <=> (channel<0 || channel>15)
 377             throw new InvalidMidiDataException("channel out of range: " + channel);
 378         }
 379         setMessage((command & 0xF0) | (channel & 0x0F), data1, data2);
 380     }
 381 
 382 
 383     /**
 384      * Obtains the MIDI channel associated with this event.  This method
 385      * assumes that the event is a MIDI channel message; if not, the return
 386      * value will not be meaningful.
 387      * @return MIDI channel associated with the message.

 388      * @see #setMessage(int, int, int, int)
 389      */
 390     public int getChannel() {
 391         // this returns 0 if an invalid message is set
 392         return (getStatus() & 0x0F);
 393     }
 394 
 395 
 396     /**
 397      * Obtains the MIDI command associated with this event.  This method
 398      * assumes that the event is a MIDI channel message; if not, the return
 399      * value will not be meaningful.

 400      * @return the MIDI command associated with this event
 401      * @see #setMessage(int, int, int, int)
 402      */
 403     public int getCommand() {
 404         // this returns 0 if an invalid message is set
 405         return (getStatus() & 0xF0);
 406     }
 407 
 408 
 409     /**
 410      * Obtains the first data byte in the message.
 411      * @return the value of the <code>data1</code> field

 412      * @see #setMessage(int, int, int)
 413      */
 414     public int getData1() {
 415         if (length > 1) {
 416             return (data[1] & 0xFF);
 417         }
 418         return 0;
 419     }
 420 
 421 
 422     /**
 423      * Obtains the second data byte in the message.
 424      * @return the value of the <code>data2</code> field

 425      * @see #setMessage(int, int, int)
 426      */
 427     public int getData2() {
 428         if (length > 2) {
 429             return (data[2] & 0xFF);
 430         }
 431         return 0;
 432     }
 433 
 434 
 435     /**
 436      * Creates a new object of the same class and with the same contents
 437      * as this object.
 438      * @return a clone of this instance.

 439      */
 440     public Object clone() {
 441         byte[] newData = new byte[length];
 442         System.arraycopy(data, 0, newData, 0, newData.length);
 443 
 444         ShortMessage msg = new ShortMessage(newData);
 445         return msg;
 446     }
 447 
 448 
 449     /**
 450      * Retrieves the number of data bytes associated with a particular
 451      * status byte value.
 452      * @param status status byte value, which must represent a short MIDI message


 453      * @return data length in bytes (0, 1, or 2)
 454      * @throws InvalidMidiDataException if the
 455      * <code>status</code> argument does not represent the status byte for any
 456      * short message
 457      */
 458     protected final int getDataLength(int status) throws InvalidMidiDataException {
 459         // system common and system real-time messages
 460         switch(status) {
 461         case 0xF6:                      // Tune Request
 462         case 0xF7:                      // EOX
 463             // System real-time messages
 464         case 0xF8:                      // Timing Clock
 465         case 0xF9:                      // Undefined
 466         case 0xFA:                      // Start
 467         case 0xFB:                      // Continue
 468         case 0xFC:                      // Stop
 469         case 0xFD:                      // Undefined
 470         case 0xFE:                      // Active Sensing
 471         case 0xFF:                      // System Reset
 472             return 0;
 473         case 0xF1:                      // MTC Quarter Frame
 474         case 0xF3:                      // Song Select
 475             return 1;
 476         case 0xF2:                      // Song Position Pointer
   1 /*
   2  * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.sound.midi;
  27 
  28 /**
  29  * A {@code ShortMessage} contains a MIDI message that has at most two data
  30  * bytes following its status byte. The types of MIDI message that satisfy this
  31  * criterion are channel voice, channel mode, system common, and system
  32  * real-time--in other words, everything except system exclusive and
  33  * meta-events. The {@code ShortMessage} class provides methods for getting and
  34  * setting the contents of the MIDI message.
  35  * <p>
  36  * A number of {@code ShortMessage} methods have integer parameters by which you
  37  * specify a MIDI status or data byte. If you know the numeric value, you can
  38  * express it directly. For system common and system real-time messages, you can
  39  * often use the corresponding fields of {@code ShortMessage}, such as
  40  * {@link #SYSTEM_RESET SYSTEM_RESET}. For channel messages, the upper four bits
  41  * of the status byte are specified by a command value and the lower four bits
  42  * are specified by a MIDI channel number. To convert incoming MIDI data bytes
  43  * that are in the form of Java's signed bytes, you can use the
  44  * <a href="MidiMessage.html#integersVsBytes">conversion code</a> given in the
  45  * {@link MidiMessage} class description.



  46  *
  47  * @author David Rivas
  48  * @author Kara Kytle
  49  * @author Florian Bomers
  50  * @see SysexMessage
  51  * @see MetaMessage
  52  */

  53 public class ShortMessage extends MidiMessage {
  54 

  55     // Status byte defines
  56 

  57     // System common messages
  58 
  59     /**
  60      * Status byte for MIDI Time Code Quarter Frame message (0xF1, or 241).
  61      *
  62      * @see MidiMessage#getStatus
  63      */
  64     public static final int MIDI_TIME_CODE                              = 0xF1; // 241
  65 
  66     /**
  67      * Status byte for Song Position Pointer message (0xF2, or 242).
  68      *
  69      * @see MidiMessage#getStatus
  70      */
  71     public static final int SONG_POSITION_POINTER               = 0xF2; // 242
  72 
  73     /**
  74      * Status byte for MIDI Song Select message (0xF3, or 243).
  75      *
  76      * @see MidiMessage#getStatus
  77      */
  78     public static final int SONG_SELECT                                 = 0xF3; // 243
  79 
  80     /**
  81      * Status byte for Tune Request message (0xF6, or 246).
  82      *
  83      * @see MidiMessage#getStatus
  84      */
  85     public static final int TUNE_REQUEST                                = 0xF6; // 246
  86 
  87     /**
  88      * Status byte for End of System Exclusive message (0xF7, or 247).
  89      *
  90      * @see MidiMessage#getStatus
  91      */
  92     public static final int END_OF_EXCLUSIVE                    = 0xF7; // 247
  93 

  94     // System real-time messages
  95 
  96     /**
  97      * Status byte for Timing Clock message (0xF8, or 248).
  98      *
  99      * @see MidiMessage#getStatus
 100      */
 101     public static final int TIMING_CLOCK                                = 0xF8; // 248
 102 
 103     /**
 104      * Status byte for Start message (0xFA, or 250).
 105      *
 106      * @see MidiMessage#getStatus
 107      */
 108     public static final int START                                               = 0xFA; // 250
 109 
 110     /**
 111      * Status byte for Continue message (0xFB, or 251).
 112      *
 113      * @see MidiMessage#getStatus
 114      */
 115     public static final int CONTINUE                                    = 0xFB; // 251
 116 
 117     /**
 118      * Status byte for Stop message (0xFC, or 252).
 119      *
 120      * @see MidiMessage#getStatus
 121      */
 122     public static final int STOP                                                = 0xFC; //252
 123 
 124     /**
 125      * Status byte for Active Sensing message (0xFE, or 254).
 126      *
 127      * @see MidiMessage#getStatus
 128      */
 129     public static final int ACTIVE_SENSING                              = 0xFE; // 254
 130 
 131     /**
 132      * Status byte for System Reset message (0xFF, or 255).
 133      *
 134      * @see MidiMessage#getStatus
 135      */
 136     public static final int SYSTEM_RESET                                = 0xFF; // 255
 137 

 138     // Channel voice message upper nibble defines
 139 
 140     /**
 141      * Command value for Note Off message (0x80, or 128).
 142      */
 143     public static final int NOTE_OFF                                    = 0x80;  // 128
 144 
 145     /**
 146      * Command value for Note On message (0x90, or 144).
 147      */
 148     public static final int NOTE_ON                                             = 0x90;  // 144
 149 
 150     /**
 151      * Command value for Polyphonic Key Pressure (Aftertouch) message (0xA0, or
 152      * 160).
 153      */
 154     public static final int POLY_PRESSURE                               = 0xA0;  // 160
 155 
 156     /**
 157      * Command value for Control Change message (0xB0, or 176).
 158      */
 159     public static final int CONTROL_CHANGE                              = 0xB0;  // 176
 160 
 161     /**
 162      * Command value for Program Change message (0xC0, or 192).
 163      */
 164     public static final int PROGRAM_CHANGE                              = 0xC0;  // 192
 165 
 166     /**
 167      * Command value for Channel Pressure (Aftertouch) message (0xD0, or 208).
 168      */
 169     public static final int CHANNEL_PRESSURE                    = 0xD0;  // 208
 170 
 171     /**
 172      * Command value for Pitch Bend message (0xE0, or 224).
 173      */
 174     public static final int PITCH_BEND                                  = 0xE0;  // 224
 175 



 176     /**
 177      * Constructs a new {@code ShortMessage}. The contents of the new message
 178      * are guaranteed to specify a valid MIDI message. Subsequently, you may set
 179      * the contents of the message using one of the {@code setMessage} methods.
 180      *

 181      * @see #setMessage
 182      */
 183     public ShortMessage() {
 184         this(new byte[3]);
 185         // Default message data: NOTE_ON on Channel 0 with max volume
 186         data[0] = (byte) (NOTE_ON & 0xFF);
 187         data[1] = (byte) 64;
 188         data[2] = (byte) 127;
 189         length = 3;
 190     }
 191 
 192     /**
 193      * Constructs a new {@code ShortMessage} which represents a MIDI message
 194      * that takes no data bytes. The contents of the message can be changed by
 195      * using one of the {@code setMessage} methods.

 196      *
 197      * @param  status the MIDI status byte
 198      * @throws InvalidMidiDataException if {@code status} does not specify a
 199      *         valid MIDI status byte for a message that requires no data bytes
 200      * @see #setMessage(int)
 201      * @see #setMessage(int, int, int)
 202      * @see #setMessage(int, int, int, int)
 203      * @see #getStatus()
 204      * @since 1.7
 205      */
 206     public ShortMessage(int status) throws InvalidMidiDataException {
 207         super(null);
 208         setMessage(status); // can throw InvalidMidiDataException
 209     }
 210 
 211     /**
 212      * Constructs a new {@code ShortMessage} which represents a MIDI message
 213      * that takes up to two data bytes. If the message only takes one data byte,
 214      * the second data byte is ignored. If the message does not take any data
 215      * bytes, both data bytes are ignored. The contents of the message can be
 216      * changed by using one of the {@code setMessage} methods.

 217      *
 218      * @param  status the MIDI status byte
 219      * @param  data1 the first data byte
 220      * @param  data2 the second data byte
 221      * @throws InvalidMidiDataException if the status byte or all data bytes
 222      *         belonging to the message do not specify a valid MIDI message
 223      * @see #setMessage(int)
 224      * @see #setMessage(int, int, int)
 225      * @see #setMessage(int, int, int, int)
 226      * @see #getStatus()
 227      * @see #getData1()
 228      * @see #getData2()
 229      * @since 1.7
 230      */
 231     public ShortMessage(int status, int data1, int data2)
 232             throws InvalidMidiDataException {
 233         super(null);
 234         setMessage(status, data1, data2); // can throw InvalidMidiDataException
 235     }
 236 
 237     /**
 238      * Constructs a new {@code ShortMessage} which represents a channel MIDI
 239      * message that takes up to two data bytes. If the message only takes one
 240      * data byte, the second data byte is ignored. If the message does not take
 241      * any data bytes, both data bytes are ignored. The contents of the message
 242      * can be changed by using one of the {@code setMessage} methods.

 243      *
 244      * @param  command the MIDI command represented by this message
 245      * @param  channel the channel associated with the message
 246      * @param  data1 the first data byte
 247      * @param  data2 the second data byte
 248      * @throws InvalidMidiDataException if the command value, channel value or
 249      *         all data bytes belonging to the message do not specify a valid
 250      *         MIDI message
 251      * @see #setMessage(int)
 252      * @see #setMessage(int, int, int)
 253      * @see #setMessage(int, int, int, int)
 254      * @see #getCommand()
 255      * @see #getChannel()
 256      * @see #getData1()
 257      * @see #getData2()
 258      * @since 1.7
 259      */
 260     public ShortMessage(int command, int channel, int data1, int data2)
 261             throws InvalidMidiDataException {
 262         super(null);
 263         setMessage(command, channel, data1, data2);
 264     }
 265 

 266     /**
 267      * Constructs a new {@code ShortMessage}.
 268      *
 269      * @param  data an array of bytes containing the complete message. The
 270      *         message data may be changed using the {@code setMessage} method.
 271      * @see #setMessage
 272      */
 273     // $$fb this should throw an Exception in case of an illegal message!
 274     protected ShortMessage(byte[] data) {
 275         // $$fb this may set an invalid message.
 276         // Can't correct without compromising compatibility
 277         super(data);
 278     }
 279 

 280     /**
 281      * Sets the parameters for a MIDI message that takes no data bytes.
 282      *
 283      * @param  status the MIDI status byte
 284      * @throws InvalidMidiDataException if {@code status} does not specify a
 285      *         valid MIDI status byte for a message that requires no data bytes
 286      * @see #setMessage(int, int, int)
 287      * @see #setMessage(int, int, int, int)
 288      */
 289     public void setMessage(int status) throws InvalidMidiDataException {
 290         // check for valid values
 291         int dataLength = getDataLength(status); // can throw InvalidMidiDataException
 292         if (dataLength != 0) {
 293             throw new InvalidMidiDataException("Status byte; " + status + " requires " + dataLength + " data bytes");
 294         }
 295         setMessage(status, 0, 0);
 296     }
 297 

 298     /**
 299      * Sets the parameters for a MIDI message that takes one or two data bytes.
 300      * If the message takes only one data byte, the second data byte is ignored;
 301      * if the message does not take any data bytes, both data bytes are ignored.

 302      *
 303      * @param  status the MIDI status byte
 304      * @param  data1 the first data byte
 305      * @param  data2 the second data byte
 306      * @throws InvalidMidiDataException if the the status byte, or all data
 307      *         bytes belonging to the message, do not specify a valid MIDI
 308      *         message
 309      * @see #setMessage(int, int, int, int)
 310      * @see #setMessage(int)
 311      */
 312     public void setMessage(int status, int data1, int data2) throws InvalidMidiDataException {
 313         // check for valid values
 314         int dataLength = getDataLength(status); // can throw InvalidMidiDataException
 315         if (dataLength > 0) {
 316             if (data1 < 0 || data1 > 127) {
 317                 throw new InvalidMidiDataException("data1 out of range: " + data1);
 318             }
 319             if (dataLength > 1) {
 320                 if (data2 < 0 || data2 > 127) {
 321                     throw new InvalidMidiDataException("data2 out of range: " + data2);
 322                 }
 323             }
 324         }
 325 
 326 
 327         // set the length
 328         length = dataLength + 1;
 329         // re-allocate array if ShortMessage(byte[]) constructor gave array with fewer elements
 330         if (data == null || data.length < length) {
 331             data = new byte[3];
 332         }
 333 
 334         // set the data
 335         data[0] = (byte) (status & 0xFF);
 336         if (length > 1) {
 337             data[1] = (byte) (data1 & 0xFF);
 338             if (length > 2) {
 339                 data[2] = (byte) (data2 & 0xFF);
 340             }
 341         }
 342     }
 343 

 344     /**
 345      * Sets the short message parameters for a channel message which takes up to
 346      * two data bytes. If the message only takes one data byte, the second data
 347      * byte is ignored; if the message does not take any data bytes, both data
 348      * bytes are ignored.

 349      *
 350      * @param  command the MIDI command represented by this message
 351      * @param  channel the channel associated with the message
 352      * @param  data1 the first data byte
 353      * @param  data2 the second data byte
 354      * @throws InvalidMidiDataException if the status byte or all data bytes
 355      *         belonging to the message, do not specify a valid MIDI message


 356      * @see #setMessage(int, int, int)
 357      * @see #setMessage(int)
 358      * @see #getCommand
 359      * @see #getChannel
 360      * @see #getData1
 361      * @see #getData2
 362      */
 363     public void setMessage(int command, int channel, int data1, int data2) throws InvalidMidiDataException {
 364         // check for valid values
 365         if (command >= 0xF0 || command < 0x80) {
 366             throw new InvalidMidiDataException("command out of range: 0x" + Integer.toHexString(command));
 367         }
 368         if ((channel & 0xFFFFFFF0) != 0) { // <=> (channel<0 || channel>15)
 369             throw new InvalidMidiDataException("channel out of range: " + channel);
 370         }
 371         setMessage((command & 0xF0) | (channel & 0x0F), data1, data2);
 372     }
 373 

 374     /**
 375      * Obtains the MIDI channel associated with this event. This method assumes
 376      * that the event is a MIDI channel message; if not, the return value will
 377      * not be meaningful.
 378      *
 379      * @return MIDI channel associated with the message
 380      * @see #setMessage(int, int, int, int)
 381      */
 382     public int getChannel() {
 383         // this returns 0 if an invalid message is set
 384         return (getStatus() & 0x0F);
 385     }
 386 

 387     /**
 388      * Obtains the MIDI command associated with this event. This method assumes
 389      * that the event is a MIDI channel message; if not, the return value will
 390      * not be meaningful.
 391      *
 392      * @return the MIDI command associated with this event
 393      * @see #setMessage(int, int, int, int)
 394      */
 395     public int getCommand() {
 396         // this returns 0 if an invalid message is set
 397         return (getStatus() & 0xF0);
 398     }
 399 

 400     /**
 401      * Obtains the first data byte in the message.
 402      *
 403      * @return the value of the {@code data1} field
 404      * @see #setMessage(int, int, int)
 405      */
 406     public int getData1() {
 407         if (length > 1) {
 408             return (data[1] & 0xFF);
 409         }
 410         return 0;
 411     }
 412 

 413     /**
 414      * Obtains the second data byte in the message.
 415      *
 416      * @return the value of the {@code data2} field
 417      * @see #setMessage(int, int, int)
 418      */
 419     public int getData2() {
 420         if (length > 2) {
 421             return (data[2] & 0xFF);
 422         }
 423         return 0;
 424     }
 425 

 426     /**
 427      * Creates a new object of the same class and with the same contents as this
 428      * object.
 429      *
 430      * @return a clone of this instance
 431      */
 432     public Object clone() {
 433         byte[] newData = new byte[length];
 434         System.arraycopy(data, 0, newData, 0, newData.length);
 435 
 436         ShortMessage msg = new ShortMessage(newData);
 437         return msg;
 438     }
 439 

 440     /**
 441      * Retrieves the number of data bytes associated with a particular status
 442      * byte value.
 443      *
 444      * @param  status status byte value, which must represent a short MIDI
 445      *         message
 446      * @return data length in bytes (0, 1, or 2)
 447      * @throws InvalidMidiDataException if the {@code status} argument does not
 448      *         represent the status byte for any short message

 449      */
 450     protected final int getDataLength(int status) throws InvalidMidiDataException {
 451         // system common and system real-time messages
 452         switch(status) {
 453         case 0xF6:                      // Tune Request
 454         case 0xF7:                      // EOX
 455             // System real-time messages
 456         case 0xF8:                      // Timing Clock
 457         case 0xF9:                      // Undefined
 458         case 0xFA:                      // Start
 459         case 0xFB:                      // Continue
 460         case 0xFC:                      // Stop
 461         case 0xFD:                      // Undefined
 462         case 0xFE:                      // Active Sensing
 463         case 0xFF:                      // System Reset
 464             return 0;
 465         case 0xF1:                      // MTC Quarter Frame
 466         case 0xF3:                      // Song Select
 467             return 1;
 468         case 0xF2:                      // Song Position Pointer