GNU Classpath (0.20) | |
Frames | No Frames |
1: /* ShortMessage.java -- A MIDI message no longer than 3 bytes 2: Copyright (C) 2005 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2, or (at your option) 9: any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; see the file COPYING. If not, write to the 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: 39: package javax.sound.midi; 40: 41: /** 42: * A short MIDI message that is no longer than 3 bytes long. 43: * 44: * @author Anthony Green (green@redhat.com) 45: * @since 1.3 46: * 47: */ 48: public class ShortMessage extends MidiMessage 49: { 50: /** 51: * Status byte for System Exclusive message. 52: */ 53: public static final int SYSTEM_EXCLUSIVE = 0xF0; 54: 55: /** 56: * Status byte for Time Code message. 57: */ 58: public static final int MIDI_TIME_CODE = 0xF1; 59: 60: /** 61: * Status byte for Song Position Pointer message. 62: */ 63: public static final int SONG_POSITION_POINTER = 0xF2; 64: 65: /** 66: * Status byte for Song Select message. 67: */ 68: public static final int SONG_SELECT = 0xF3; 69: 70: /** 71: * Status byte for Tune Request message. 72: */ 73: public static final int TUNE_REQUEST = 0xF6; 74: 75: /** 76: * Status byte for End Of Exclusive message. 77: */ 78: public static final int END_OF_EXCLUSIVE = 0xF7; 79: 80: /** 81: * Status byte for Timing Clock message. 82: */ 83: public static final int TIMING_CLOCK = 0xF8; 84: 85: /** 86: * Status byte for Start message. 87: */ 88: public static final int START = 0xFA; 89: 90: /** 91: * Status byte for Continue message. 92: */ 93: public static final int CONTINUE = 0xFB; 94: 95: /** 96: * Status byte for Stop message. 97: */ 98: public static final int STOP = 0xFC; 99: 100: /** 101: * Status byte for Active Sensing message. 102: */ 103: public static final int ACTIVE_SENSING = 0xFE; 104: 105: /** 106: * Status byte for System Reset message. 107: */ 108: public static final int SYSTEM_RESET = 0xFF; 109: 110: /** 111: * Status nibble for Note Off message. 112: */ 113: public static final int NOTE_OFF = 0x80; 114: 115: /** 116: * Status nibble for Note On message. 117: */ 118: public static final int NOTE_ON = 0x90; 119: 120: /** 121: * Status nibble for Poly Pressure message. 122: */ 123: public static final int POLY_PRESSURE = 0xA0; 124: 125: /** 126: * Status nibble for Control Change message. 127: */ 128: public static final int CONTROL_CHANGE = 0xB0; 129: 130: /** 131: * Status nibble for Program Change message. 132: */ 133: public static final int PROGRAM_CHANGE = 0xC0; 134: 135: /** 136: * Statue nibble for Channel Pressure message. 137: */ 138: public static final int CHANNEL_PRESSURE = 0xD0; 139: 140: /** 141: * Status nibble for Pitch Bend message. 142: */ 143: public static final int PITCH_BEND = 0xE0; 144: 145: // Create and initialize a default, arbitrary message. 146: private static byte[] defaultMessage; 147: static 148: { 149: defaultMessage = new byte[1]; 150: defaultMessage[0] = (byte) STOP; 151: } 152: 153: /** 154: * Create a short MIDI message. 155: * 156: * The spec requires that this represent a valid MIDI message, but doesn't 157: * specify what it should be. We've chosen the STOP message for our 158: * implementation. 159: */ 160: public ShortMessage() 161: { 162: this(defaultMessage); 163: } 164: 165: /** 166: * Create a short MIDI message. 167: * 168: * The data argument should be a valid MIDI message. Unfortunately the spec 169: * does not allow us to throw an InvalidMidiDataException if data is invalid. 170: * 171: * @param data the message data 172: */ 173: public ShortMessage(byte[] data) 174: { 175: super(data); 176: } 177: 178: /** 179: * Set the MIDI message. 180: * 181: * @param status the status byte for this message 182: * @param data1 the first data byte for this message 183: * @param data2 the second data byte for this message 184: * @throws InvalidMidiDataException if status is bad, or data is out of range 185: */ 186: public void setMessage(int status, int data1, int data2) 187: throws InvalidMidiDataException 188: { 189: length = getDataLength(status); 190: length++; 191: if (data == null || data.length < length) 192: data = new byte[length]; 193: data[0] = (byte) status; 194: if (length > 1) 195: { 196: if (data1 < 0 || data1 > 127) 197: throw new InvalidMidiDataException("data1 (" + data1 198: + ") must be between 0 and 127."); 199: data[1] = (byte) data1; 200: if (length > 2) 201: { 202: if (data2 < 0 || data2 > 127) 203: throw new InvalidMidiDataException("data2 (" + data2 204: + ") must be between 0 and 127."); 205: data[2] = (byte) data2; 206: } 207: } 208: } 209: 210: public void setMessage(int command, int channel, int data1, int data2) 211: throws InvalidMidiDataException 212: { 213: // TODO: This could probably stand some error checking. 214: // It currently assumes command and channel are valid values. 215: setMessage(command + channel, data1, data2); 216: } 217: 218: /** 219: * Set the MIDI message to one that requires no data bytes. 220: * 221: * @param status the status byte for this message 222: * @throws InvalidMidiDataException if status is bad, or requires data 223: */ 224: public void setMessage(int status) throws InvalidMidiDataException 225: { 226: int length = getDataLength(status); 227: if (length != 0) 228: throw new InvalidMidiDataException("Status byte 0x" 229: + Integer.toHexString(status) 230: + " requires " 231: + length + " bytes of data."); 232: setMessage(status, 0, 0); 233: } 234: 235: 236: /** 237: * Return the number of data bytes needed for a given MIDI status byte. 238: * 239: * @param status the status byte for a short MIDI message 240: * @return the number of data bytes needed for this status byte 241: * @throws InvalidMidiDataException if status is an invalid status byte 242: */ 243: protected final int getDataLength(int status) throws InvalidMidiDataException 244: { 245: int originalStatus = status; 246: 247: if ((status & 0xF0) != 0xF0) 248: status &= 0xF0; 249: 250: switch (status) 251: { 252: case NOTE_OFF: 253: case NOTE_ON: 254: case POLY_PRESSURE: 255: case CONTROL_CHANGE: 256: case PITCH_BEND: 257: case SONG_POSITION_POINTER: 258: return 2; 259: 260: case PROGRAM_CHANGE: 261: case CHANNEL_PRESSURE: 262: case SONG_SELECT: 263: case 0xF5: // FIXME: unofficial bus select. Not in spec?? 264: return 1; 265: 266: case SYSTEM_EXCLUSIVE: 267: return 0; // FIXME: is this correct? 268: 269: case TUNE_REQUEST: 270: case END_OF_EXCLUSIVE: 271: case TIMING_CLOCK: 272: case START: 273: case CONTINUE: 274: case STOP: 275: case ACTIVE_SENSING: 276: case SYSTEM_RESET: 277: return 0; 278: 279: default: 280: throw new InvalidMidiDataException("Invalid status: 0x" 281: + Integer.toHexString(originalStatus)); 282: } 283: } 284: 285: /** 286: * Get the channel information from this MIDI message, assuming it is a 287: * MIDI channel message. 288: * 289: * @return the MIDI channel for this message 290: */ 291: public int getChannel() 292: { 293: return data[0] & 0x0F; 294: } 295: 296: /** 297: * Get the command nibble from this MIDI message, assuming it is a MIDI 298: * channel message. 299: * 300: * @return the MIDI command for this message 301: */ 302: public int getCommand() 303: { 304: return data[0] & 0xF0; 305: } 306: 307: /** 308: * Get the first data byte from this message, assuming it exists, and 309: * zero otherwise. 310: * 311: * @return the first data byte or zero if none exists. 312: */ 313: public int getData1() 314: { 315: if (length > 1) 316: return data[1]; 317: else 318: return 0; 319: } 320: 321: /** 322: * Get the second data byte from this message, assuming it exists, and 323: * zero otherwise. 324: * 325: * @return the second date byte or zero if none exists. 326: */ 327: public int getData2() 328: { 329: if (length > 2) 330: return data[2]; 331: else 332: return 0; 333: } 334: 335: /* Create a deep-copy clone of this object. 336: * @see java.lang.Object#clone() 337: */ 338: public Object clone() 339: { 340: byte message[] = new byte[length]; 341: System.arraycopy(data, 0, message, 0, length); 342: return new ShortMessage(message); 343: } 344: }
GNU Classpath (0.20) |