1 7 8 package javax.sound.midi; 9 10 37 38 public class ShortMessage extends MidiMessage { 39 40 41 43 44 46 50 public static final int MIDI_TIME_CODE = 0xF1; 52 56 public static final int SONG_POSITION_POINTER = 0xF2; 58 62 public static final int SONG_SELECT = 0xF3; 64 68 public static final int TUNE_REQUEST = 0xF6; 70 74 public static final int END_OF_EXCLUSIVE = 0xF7; 76 77 79 83 public static final int TIMING_CLOCK = 0xF8; 85 89 public static final int START = 0xFA; 91 95 public static final int CONTINUE = 0xFB; 97 101 public static final int STOP = 0xFC; 103 107 public static final int ACTIVE_SENSING = 0xFE; 109 113 public static final int SYSTEM_RESET = 0xFF; 115 116 118 121 public static final int NOTE_OFF = 0x80; 123 126 public static final int NOTE_ON = 0x90; 128 131 public static final int POLY_PRESSURE = 0xA0; 133 136 public static final int CONTROL_CHANGE = 0xB0; 138 141 public static final int PROGRAM_CHANGE = 0xC0; 143 146 public static final int CHANNEL_PRESSURE = 0xD0; 148 151 public static final int PITCH_BEND = 0xE0; 153 154 156 164 public ShortMessage() { 165 this(new byte[3]); 166 data[0] = (byte) (NOTE_ON & 0xFF); 168 data[1] = (byte) 64; 169 data[2] = (byte) 127; 170 length = 3; 171 } 172 173 174 181 protected ShortMessage(byte[] data) { 183 super(data); 186 } 187 188 189 197 public void setMessage(int status) throws InvalidMidiDataException { 198 int dataLength = getDataLength(status); if (dataLength != 0) { 201 throw new InvalidMidiDataException ("Status byte; " + status + " requires " + dataLength + " data bytes"); 202 } 203 setMessage(status, 0, 0); 204 } 205 206 207 222 public void setMessage(int status, int data1, int data2) throws InvalidMidiDataException { 223 int dataLength = getDataLength(status); if (dataLength > 0) { 226 if (data1 < 0 || data1 > 127) { 227 throw new InvalidMidiDataException ("data1 out of range: " + data1); 228 } 229 if (dataLength > 1) { 230 if (data2 < 0 || data2 > 127) { 231 throw new InvalidMidiDataException ("data2 out of range: " + data2); 232 } 233 } 234 } 235 236 237 length = dataLength + 1; 239 if (data == null || data.length < length) { 241 data = new byte[3]; 242 } 243 244 data[0] = (byte) (status & 0xFF); 246 if (length > 1) { 247 data[1] = (byte) (data1 & 0xFF); 248 if (length > 2) { 249 data[2] = (byte) (data2 & 0xFF); 250 } 251 } 252 } 253 254 255 277 public void setMessage(int command, int channel, int data1, int data2) throws InvalidMidiDataException { 278 if (command >= 0xF0 || command < 0x80) { 280 throw new InvalidMidiDataException ("command out of range: 0x" + Integer.toHexString(command)); 281 } 282 if ((channel & 0xFFFFFFF0) != 0) { throw new InvalidMidiDataException ("channel out of range: " + channel); 284 } 285 setMessage((command & 0xF0) | (channel & 0x0F), data1, data2); 286 } 287 288 289 296 public int getChannel() { 297 return (getStatus() & 0x0F); 299 } 300 301 302 308 public int getCommand() { 309 return (getStatus() & 0xF0); 311 } 312 313 314 319 public int getData1() { 320 if (length > 1) { 321 return (data[1] & 0xFF); 322 } 323 return 0; 324 } 325 326 327 332 public int getData2() { 333 if (length > 2) { 334 return (data[2] & 0xFF); 335 } 336 return 0; 337 } 338 339 340 345 public Object clone() { 346 byte[] newData = new byte[length]; 347 System.arraycopy(data, 0, newData, 0, newData.length); 348 349 ShortMessage msg = new ShortMessage (newData); 350 return msg; 351 } 352 353 354 363 protected final int getDataLength(int status) throws InvalidMidiDataException { 364 switch(status) { 366 case 0xF6: case 0xF7: case 0xF8: case 0xF9: case 0xFA: case 0xFB: case 0xFC: case 0xFD: case 0xFE: case 0xFF: return 0; 378 case 0xF1: case 0xF3: return 1; 381 case 0xF2: return 2; 383 default: 384 } 385 386 switch(status & 0xF0) { 388 case 0x80: 389 case 0x90: 390 case 0xA0: 391 case 0xB0: 392 case 0xE0: 393 return 2; 394 case 0xC0: 395 case 0xD0: 396 return 1; 397 default: 398 throw new InvalidMidiDataException ("Invalid status byte: " + status); 399 } 400 } 401 } 402 | Popular Tags |