1. /*
  2. * @(#)SysexMessage.java 1.25 03/01/23
  3. *
  4. * Copyright 2003 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>SysexMessage</code> object represents a MIDI system exclusive message.
  10. * <p>
  11. * When a system exclusive message is read from a MIDI file, it always has
  12. * a defined length. Data from a system exclusive message from a MIDI file
  13. * should be stored in the data array of a <code>SysexMessage</code> as
  14. * follows: the system exclusive message status byte (0xF0 or 0xF7), all
  15. * message data bytes, and finally the end-of-exclusive flag (0xF7).
  16. * The length reported by the <code>SysexMessage</code> object is therefore
  17. * the length of the system exclusive data plus two: one byte for the status
  18. * byte and one for the end-of-exclusive flag.
  19. * <p>
  20. * As dictated by the Standard MIDI Files specification, two status byte values are legal
  21. * for a <code>SysexMessage</code> read from a MIDI file:
  22. * <ul>
  23. * <li>0xF0: System Exclusive message (same as in MIDI wire protocol)</li>
  24. * <li>0xF7: Special System Exclusive message</li>
  25. * </ul>
  26. * <p>
  27. * When Java Sound is used to handle system exclusive data that is being received
  28. * using MIDI wire protocol, it should place the data in one or more
  29. * <code>SysexMessages</code>. In this case, the length of the system exclusive data
  30. * is not known in advance; the end of the system exclusive data is marked by an
  31. * end-of-exclusive flag (0xF7) in the MIDI wire byte stream.
  32. * <ul>
  33. * <li>0xF0: System Exclusive message (same as in MIDI wire protocol)</li>
  34. * <li>0xF7: End of Exclusive (EOX)</li>
  35. * </ul>
  36. * The first <code>SysexMessage</code> object containing data for a particular system
  37. * exclusive message should have the status value 0xF0. If this message contains all
  38. * the system exclusive data
  39. * for the message, it should end with the status byte 0xF7 (EOX).
  40. * Otherwise, additional system exclusive data should be sent in one or more
  41. * <code>SysexMessages</code> with a status value of 0xF7. The <code>SysexMessage</code>
  42. * containing the last of the data for the system exclusive message should end with the
  43. * value 0xF7 (EOX) to mark the end of the system exclusive message.
  44. * <p>
  45. * If system exclusive data from <code>SysexMessages</code> objects is being transmitted
  46. * using MIDI wire protocol, only the initial 0xF0 status byte, the system exclusive
  47. * data itself, and the final 0xF7 (EOX) byte should be propagated; any 0xF7 status
  48. * bytes used to indicate that a <code>SysexMessage</code> contains continuing system
  49. * exclusive data should not be propagated via MIDI wire protocol.
  50. *
  51. * @version 1.25, 03/01/23
  52. * @author David Rivas
  53. * @author Kara Kytle
  54. * @author Florian Bomers
  55. */
  56. public class SysexMessage extends MidiMessage {
  57. // Status byte defines
  58. /**
  59. * Status byte for System Exclusive message (0xF0, or 240).
  60. * @see MidiMessage#getStatus
  61. */
  62. public static final int SYSTEM_EXCLUSIVE = 0xF0; // 240
  63. /**
  64. * Status byte for Special System Exclusive message (0xF7, or 247), which is used
  65. * in MIDI files. It has the same value as END_OF_EXCLUSIVE, which
  66. * is used in the real-time "MIDI wire" protocol.
  67. * @see MidiMessage#getStatus
  68. */
  69. public static final int SPECIAL_SYSTEM_EXCLUSIVE = 0xF7; // 247
  70. // Instance variables
  71. /*
  72. * The data bytes for this system exclusive message. These are
  73. * initialized to <code>null</code> and are set explicitly
  74. * by {@link #setMessage(int, byte[], int, long) setMessage}.
  75. */
  76. //protected byte[] data = null;
  77. /**
  78. * Constructs a new <code>SysexMessage</code>. The
  79. * contents of the new message are guaranteed to specify
  80. * a valid MIDI message. Subsequently, you may set the
  81. * contents of the message using one of the <code>setMessage</code>
  82. * methods.
  83. * @see #setMessage
  84. */
  85. public SysexMessage() {
  86. this(new byte[2]);
  87. // Default sysex message data: SOX followed by EOX
  88. data[0] = (byte) (SYSTEM_EXCLUSIVE & 0xFF);
  89. data[1] = (byte) (ShortMessage.END_OF_EXCLUSIVE & 0xFF);
  90. }
  91. /**
  92. * Constructs a new <code>SysexMessage</code>.
  93. * @param data an array of bytes containing the complete message.
  94. * The message data may be changed using the <code>setMessage</code>
  95. * method.
  96. * @see #setMessage
  97. */
  98. protected SysexMessage(byte[] data) {
  99. super(data);
  100. }
  101. /**
  102. * Sets the data for the system exclusive message. The
  103. * first byte of the data array must be a valid system
  104. * exclusive status byte (0xF0 or 0xF7).
  105. * @param data the system exclusive message data
  106. * @param length the length of the valid message data in
  107. * the array, including the status byte.
  108. */
  109. public void setMessage(byte[] data, int length) throws InvalidMidiDataException {
  110. int status = (data[0] & 0xFF);
  111. if ((status != 0xF0) && (status != 0xF7)) {
  112. throw new InvalidMidiDataException("Invalid status byte for sysex message: 0x" + Integer.toHexString(status));
  113. }
  114. super.setMessage(data, length);
  115. }
  116. /**
  117. * Sets the data for the system exclusive message.
  118. * @param status the status byte for the message (0xF0 or 0xF7)
  119. * @param data the system exclusive message data
  120. * @param length the length of the valid message data in
  121. * the array
  122. */
  123. public void setMessage(int status, byte[] data, int length) throws InvalidMidiDataException {
  124. if ( (status != 0xF0) && (status != 0xF7) ) {
  125. throw new InvalidMidiDataException("Invalid status byte for sysex message: 0x" + Integer.toHexString(status));
  126. }
  127. if (length < 0 || length > data.length) {
  128. throw new IndexOutOfBoundsException("length out of bounds: "+length);
  129. }
  130. this.length = length + 1;
  131. if (this.data==null || this.data.length < this.length) {
  132. this.data = new byte[this.length];
  133. }
  134. this.data[0] = (byte) (status & 0xFF);
  135. if (length > 0) {
  136. System.arraycopy(data, 0, this.data, 1, length);
  137. }
  138. }
  139. /**
  140. * Obtains a copy of the data for the system exclusive message.
  141. * The returned array of bytes does not include the status byte.
  142. * @return array containing the system exclusive message data.
  143. */
  144. public byte[] getData() {
  145. byte[] returnedArray = new byte[length - 1];
  146. System.arraycopy(data, 1, returnedArray, 0, (length - 1));
  147. return returnedArray;
  148. }
  149. /**
  150. * Creates a new object of the same class and with the same contents
  151. * as this object.
  152. * @return a clone of this instance
  153. */
  154. public Object clone() {
  155. byte[] newData = new byte[length];
  156. System.arraycopy(data, 0, newData, 0, newData.length);
  157. SysexMessage event = new SysexMessage(newData);
  158. return event;
  159. }
  160. }