1. /*
  2. * @(#)ShortMessage.java 1.24 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.sound.midi;
  8. /**
  9. * A <code>ShortMessage</code> contains a MIDI message that has at most
  10. * two data bytes following its status byte. The types of MIDI message
  11. * that satisfy this criterion are channel voice, channel mode, system common,
  12. * and system real-time--in other words, everything except system exclusive
  13. * and meta-events. The <code>ShortMessage</code> class provides methods
  14. * for getting and setting the contents of the MIDI message.
  15. * <p>
  16. * A number of <code>ShortMessage</code> methods have integer parameters by which
  17. * you specify a MIDI status or data byte. If you know the numeric value, you
  18. * can express it directly. For system common and system real-time messages,
  19. * you can often use the corresponding fields of <code>ShortMessage</code>, such as
  20. * {@link #SYSTEM_RESET SYSTEM_RESET}. For channel messages,
  21. * the upper four bits of the status byte are specified by a command value and
  22. * the lower four bits are specified by a MIDI channel number. To
  23. * convert incoming MIDI data bytes that are in the form of Java's signed bytes,
  24. * you can use the <A HREF="MidiMessage.html#integersVsBytes">conversion code</A>
  25. * given in the <code>{@link MidiMessage}</code> class description.
  26. *
  27. * @see SysexMessage
  28. * @see MetaMessage
  29. *
  30. * @version 1.24, 03/12/19
  31. * @author David Rivas
  32. * @author Kara Kytle
  33. * @author Florian Bomers
  34. */
  35. public class ShortMessage extends MidiMessage {
  36. // Status byte defines
  37. // System common messages
  38. /**
  39. * Status byte for MIDI Time Code Quarter Frame message (0xF1, or 241).
  40. * @see MidiMessage#getStatus
  41. */
  42. public static final int MIDI_TIME_CODE = 0xF1; // 241
  43. /**
  44. * Status byte for Song Position Pointer message (0xF2, or 242).
  45. * @see MidiMessage#getStatus
  46. */
  47. public static final int SONG_POSITION_POINTER = 0xF2; // 242
  48. /**
  49. * Status byte for MIDI Song Select message (0xF3, or 243).
  50. * @see MidiMessage#getStatus
  51. */
  52. public static final int SONG_SELECT = 0xF3; // 243
  53. /**
  54. * Status byte for Tune Request message (0xF6, or 246).
  55. * @see MidiMessage#getStatus
  56. */
  57. public static final int TUNE_REQUEST = 0xF6; // 246
  58. /**
  59. * Status byte for End of System Exclusive message (0xF7, or 247).
  60. * @see MidiMessage#getStatus
  61. */
  62. public static final int END_OF_EXCLUSIVE = 0xF7; // 247
  63. // System real-time messages
  64. /**
  65. * Status byte for Timing Clock messagem (0xF8, or 248).
  66. * @see MidiMessage#getStatus
  67. */
  68. public static final int TIMING_CLOCK = 0xF8; // 248
  69. /**
  70. * Status byte for Start message (0xFA, or 250).
  71. * @see MidiMessage#getStatus
  72. */
  73. public static final int START = 0xFA; // 250
  74. /**
  75. * Status byte for Continue message (0xFB, or 251).
  76. * @see MidiMessage#getStatus
  77. */
  78. public static final int CONTINUE = 0xFB; // 251
  79. /**
  80. * Status byte for Stop message (0xFC, or 252).
  81. * @see MidiMessage#getStatus
  82. */
  83. public static final int STOP = 0xFC; //252
  84. /**
  85. * Status byte for Active Sensing message (0xFE, or 254).
  86. * @see MidiMessage#getStatus
  87. */
  88. public static final int ACTIVE_SENSING = 0xFE; // 254
  89. /**
  90. * Status byte for System Reset message (0xFF, or 255).
  91. * @see MidiMessage#getStatus
  92. */
  93. public static final int SYSTEM_RESET = 0xFF; // 255
  94. // Channel voice message upper nibble defines
  95. /**
  96. * Command value for Note Off message (0x80, or 128)
  97. */
  98. public static final int NOTE_OFF = 0x80; // 128
  99. /**
  100. * Command value for Note On message (0x90, or 144)
  101. */
  102. public static final int NOTE_ON = 0x90; // 144
  103. /**
  104. * Command value for Polyphonic Key Pressure (Aftertouch) message (0xA0, or 128)
  105. */
  106. public static final int POLY_PRESSURE = 0xA0; // 160
  107. /**
  108. * Command value for Control Change message (0xB0, or 176)
  109. */
  110. public static final int CONTROL_CHANGE = 0xB0; // 176
  111. /**
  112. * Command value for Program Change message (0xC0, or 192)
  113. */
  114. public static final int PROGRAM_CHANGE = 0xC0; // 192
  115. /**
  116. * Command value for Channel Pressure (Aftertouch) message (0xD0, or 208)
  117. */
  118. public static final int CHANNEL_PRESSURE = 0xD0; // 208
  119. /**
  120. * Command value for Pitch Bend message (0xE0, or 224)
  121. */
  122. public static final int PITCH_BEND = 0xE0; // 224
  123. // Instance variables
  124. /**
  125. * Constructs a new <code>ShortMessage</code>. The
  126. * contents of the new message are guaranteed to specify
  127. * a valid MIDI message. Subsequently, you may set the
  128. * contents of the message using one of the <code>setMessage</code>
  129. * methods.
  130. * @see #setMessage
  131. */
  132. public ShortMessage() {
  133. this(new byte[3]);
  134. // Default message data: NOTE_ON on Channel 0 with max volume
  135. data[0] = (byte) (NOTE_ON & 0xFF);
  136. data[1] = (byte) 64;
  137. data[2] = (byte) 127;
  138. length = 3;
  139. }
  140. /**
  141. * Constructs a new <code>ShortMessage</code>.
  142. * @param data an array of bytes containing the complete message.
  143. * The message data may be changed using the <code>setMessage</code>
  144. * method.
  145. * @see #setMessage
  146. */
  147. // $$fb this should throw an Exception in case of an illegal message!
  148. protected ShortMessage(byte[] data) {
  149. // $$fb this may set an invalid message.
  150. // Can't correct without compromising compatibility
  151. super(data);
  152. }
  153. /**
  154. * Sets the parameters for a MIDI message that takes no data bytes.
  155. * @param status the MIDI status byte
  156. * @throws <code>InvalidMidiDataException</code> if <code>status</code> does not
  157. * specify a valid MIDI status byte for a message that requires no data bytes.
  158. * @see #setMessage(int, int, int)
  159. * @see #setMessage(int, int, int, int)
  160. */
  161. public void setMessage(int status) throws InvalidMidiDataException {
  162. // check for valid values
  163. int dataLength = getDataLength(status); // can throw InvalidMidiDataException
  164. if (dataLength != 0) {
  165. throw new InvalidMidiDataException("Status byte; " + status + " requires " + dataLength + " data bytes");
  166. }
  167. setMessage(status, 0, 0);
  168. }
  169. /**
  170. * Sets the parameters for a MIDI message that takes one or two data
  171. * bytes. If the message takes only one data byte, the second data
  172. * byte is ignored; if the message does not take any data bytes, both
  173. * data bytes are ignored.
  174. *
  175. * @param status the MIDI status byte
  176. * @param data1 the first data byte
  177. * @param data2 the second data byte
  178. * @throws <code>InvalidMidiDataException</code> if the
  179. * the status byte, or all data bytes belonging to the message, do
  180. * not specify a valid MIDI message.
  181. * @see #setMessage(int, int, int, int)
  182. * @see #setMessage(int)
  183. */
  184. public void setMessage(int status, int data1, int data2) throws InvalidMidiDataException {
  185. // check for valid values
  186. int dataLength = getDataLength(status); // can throw InvalidMidiDataException
  187. if (dataLength > 0) {
  188. if (data1 < 0 || data1 > 127) {
  189. throw new InvalidMidiDataException("data1 out of range: " + data1);
  190. }
  191. if (dataLength > 1) {
  192. if (data2 < 0 || data2 > 127) {
  193. throw new InvalidMidiDataException("data2 out of range: " + data2);
  194. }
  195. }
  196. }
  197. // set the length
  198. length = dataLength + 1;
  199. // re-allocate array if ShortMessage(byte[]) constructor gave array with fewer elements
  200. if (data == null || data.length < length) {
  201. data = new byte[3];
  202. }
  203. // set the data
  204. data[0] = (byte) (status & 0xFF);
  205. if (length > 1) {
  206. data[1] = (byte) (data1 & 0xFF);
  207. if (length > 2) {
  208. data[2] = (byte) (data2 & 0xFF);
  209. }
  210. }
  211. }
  212. /**
  213. * Sets the short message parameters for a channel message
  214. * which takes up to two data bytes. If the message only
  215. * takes one data byte, the second data byte is ignored; if
  216. * the message does not take any data bytes, both data bytes
  217. * are ignored.
  218. *
  219. * @param command the MIDI command represented by this message
  220. * @param channel the channel associated with the message
  221. * @param data1 the first data byte
  222. * @param data2 the second data byte
  223. * @throws <code>InvalidMidiDataException</code> if the
  224. * status byte or all data bytes belonging to the message, do
  225. * not specify a valid MIDI message
  226. *
  227. * @see #setMessage(int, int, int)
  228. * @see #setMessage(int)
  229. * @see #getCommand
  230. * @see #getChannel
  231. * @see #getData1
  232. * @see #getData2
  233. */
  234. public void setMessage(int command, int channel, int data1, int data2) throws InvalidMidiDataException {
  235. // check for valid values
  236. if (command >= 0xF0 || command < 0x80) {
  237. throw new InvalidMidiDataException("command out of range: 0x" + Integer.toHexString(command));
  238. }
  239. if ((channel & 0xFFFFFFF0) != 0) { // <=> (channel<0 || channel>15)
  240. throw new InvalidMidiDataException("channel out of range: " + channel);
  241. }
  242. setMessage((command & 0xF0) | (channel & 0x0F), data1, data2);
  243. }
  244. /**
  245. * Obtains the MIDI channel associated with this event. This method
  246. * assumes that the event is a MIDI channel message; if not, the return
  247. * value will not be meaningful.
  248. * @return MIDI channel associated with the message.
  249. * @see #setMessage(int, int, int, int)
  250. */
  251. public int getChannel() {
  252. // this returns 0 if an invalid message is set
  253. return (getStatus() & 0x0F);
  254. }
  255. /**
  256. * Obtains the MIDI command associated with this event. This method
  257. * assumes that the event is a MIDI channel message; if not, the return
  258. * value will not be meaningful.
  259. * @see #setMessage(int, int, int, int)
  260. */
  261. public int getCommand() {
  262. // this returns 0 if an invalid message is set
  263. return (getStatus() & 0xF0);
  264. }
  265. /**
  266. * Obtains the first data byte in the message.
  267. * @return the value of the <code>data1</code> field
  268. * @see #setMessage(int, int, int)
  269. */
  270. public int getData1() {
  271. if (length > 1) {
  272. return (data[1] & 0xFF);
  273. }
  274. return 0;
  275. }
  276. /**
  277. * Obtains the second data byte in the message.
  278. * @return the value of the <code>data2</code> field
  279. * @see #setMessage(int, int, int)
  280. */
  281. public int getData2() {
  282. if (length > 2) {
  283. return (data[2] & 0xFF);
  284. }
  285. return 0;
  286. }
  287. /**
  288. * Creates a new object of the same class and with the same contents
  289. * as this object.
  290. * @return a clone of this instance.
  291. */
  292. public Object clone() {
  293. byte[] newData = new byte[length];
  294. System.arraycopy(data, 0, newData, 0, newData.length);
  295. ShortMessage msg = new ShortMessage(newData);
  296. return msg;
  297. }
  298. /**
  299. * Retrieves the number of data bytes associated with a particular
  300. * status byte value.
  301. * @param status status byte value, which must represent a short MIDI message
  302. * @return data length in bytes (0, 1, or 2)
  303. * @throws <code>InvalidMidiDataException</code> if the
  304. * <code>status</code> argument does not represent the status byte for any
  305. * short message
  306. */
  307. protected final int getDataLength(int status) throws InvalidMidiDataException {
  308. // system common and system real-time messages
  309. switch(status) {
  310. case 0xF6: // Tune Request
  311. case 0xF7: // EOX
  312. // System real-time messages
  313. case 0xF8: // Timing Clock
  314. case 0xF9: // Undefined
  315. case 0xFA: // Start
  316. case 0xFB: // Continue
  317. case 0xFC: // Stop
  318. case 0xFD: // Undefined
  319. case 0xFE: // Active Sensing
  320. case 0xFF: // System Reset
  321. return 0;
  322. case 0xF1: // MTC Quarter Frame
  323. case 0xF3: // Song Select
  324. return 1;
  325. case 0xF2: // Song Position Pointer
  326. return 2;
  327. default:
  328. }
  329. // channel voice and mode messages
  330. switch(status & 0xF0) {
  331. case 0x80:
  332. case 0x90:
  333. case 0xA0:
  334. case 0xB0:
  335. case 0xE0:
  336. return 2;
  337. case 0xC0:
  338. case 0xD0:
  339. return 1;
  340. default:
  341. throw new InvalidMidiDataException("Invalid status byte: " + status);
  342. }
  343. }
  344. }