1. /*
  2. * @(#)MetaMessage.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. import java.io.ByteArrayOutputStream;
  9. import java.io.DataOutputStream;
  10. import java.io.IOException;
  11. /**
  12. * A <code>MetaMessage</code> is a <code>{@link MidiMessage}</code> that is not meaningful to synthesizers, but
  13. * that can be stored in a MIDI file and interpreted by a sequencer program.
  14. * (See the discussion in the <code>MidiMessage</code>
  15. * class description.) The Standard MIDI Files specification defines
  16. * various types of meta-events, such as sequence number, lyric, cue point,
  17. * and set tempo. There are also meta-events
  18. * for such information as lyrics, copyrights, tempo indications, time and key
  19. * signatures, markers, etc. For more information, see the Standard MIDI Files 1.0
  20. * specification, which is part of the Complete MIDI 1.0 Detailed Specification
  21. * published by the MIDI Manufacturer's Association
  22. * (<a href = http://www.midi.org>http://www.midi.org</a>).
  23. *
  24. * <p>
  25. * When data is being transported using MIDI wire protocol,
  26. * a <code>{@link ShortMessage}</code> with the status value <code>0xFF</code> represents
  27. * a system reset message. In MIDI files, this same status value denotes a <code>MetaMessage</code>.
  28. * The types of meta-message are distinguished from each other by the first byte
  29. * that follows the status byte <code>0xFF</code>. The subsequent bytes are data
  30. * bytes. As with system exclusive messages, there are an arbitrary number of
  31. * data bytes, depending on the type of <code>MetaMessage</code>.
  32. *
  33. * @see MetaEventListener
  34. *
  35. * @version 1.24, 03/12/19
  36. * @author David Rivas
  37. * @author Kara Kytle
  38. */
  39. public class MetaMessage extends MidiMessage {
  40. // Status byte defines
  41. /**
  42. * Status byte for <code>MetaMessage</code> (0xFF, or 255), which is used
  43. * in MIDI files. It has the same value as SYSTEM_RESET, which
  44. * is used in the real-time "MIDI wire" protocol.
  45. * @see MidiMessage#getStatus
  46. */
  47. public static final int META = 0xFF; // 255
  48. // Default meta message data: just the META status byte value
  49. // $$kk: 09.09.99: need a real event here!!
  50. private static byte[] defaultMessage = { (byte)META, 0 };
  51. // Instance variables
  52. /**
  53. * The length of the actual message in the data array.
  54. * This is used to determine how many bytes of the data array
  55. * is the message, and how many are the status byte, the
  56. * type byte, and the variable-length-int describing the
  57. * length of the message.
  58. */
  59. private int dataLength = 0;
  60. /**
  61. * Constructs a new <code>MetaMessage</code>. The contents of
  62. * the message are not set here; use
  63. * {@link #setMessage(int, byte[], int) setMessage}
  64. * to set them subsequently.
  65. */
  66. public MetaMessage() {
  67. //super(defaultMessage);
  68. this(defaultMessage);
  69. }
  70. /**
  71. * Constructs a new <code>MetaMessage</code>.
  72. * @param data an array of bytes containing the complete message.
  73. * The message data may be changed using the <code>setMessage</code>
  74. * method.
  75. * @see #setMessage
  76. */
  77. protected MetaMessage(byte[] data) {
  78. super(data);
  79. //$$fb 2001-10-06: need to calculate dataLength. Fix for bug #4511796
  80. if (data.length>=3) {
  81. dataLength=data.length-3;
  82. int pos=2;
  83. while (pos<data.length && (data[pos] & 0x80)!=0) {
  84. dataLength--; pos++;
  85. }
  86. }
  87. }
  88. /**
  89. * Sets the message parameters for a <code>MetaMessage</code>.
  90. * Since only one status byte value, <code>0xFF</code>, is allowed for meta-messages,
  91. * it does not need to be specified here. Calls to <code>{@link MidiMessage#getStatus getStatus}</code> return
  92. * <code>0xFF</code> for all meta-messages.
  93. * <p>
  94. * The <code>type</code> argument should be a valid value for the byte that
  95. * follows the status byte in the <code>MetaMessage</code>. The <code>data</code> argument
  96. * should contain all the subsequent bytes of the <code>MetaMessage</code>. In other words,
  97. * the byte that specifies the type of <code>MetaMessage</code> is not considered a data byte.
  98. *
  99. * @param type meta-message type (must be less than 128)
  100. * @param data the data bytes in the MIDI message
  101. * @param length the number of bytes in the <code>data</code>
  102. * byte array
  103. * @throws <code>InvalidMidiDataException</code> if the
  104. * parameter values do not specify a valid MIDI meta message
  105. */
  106. public void setMessage(int type, byte[] data, int length) throws InvalidMidiDataException {
  107. if (type >= 128 || type < 0) {
  108. throw new InvalidMidiDataException("Invalid meta event with type " + type);
  109. }
  110. if ((length > 0 && length > data.length) || length < 0) {
  111. throw new InvalidMidiDataException("length out of bounds: "+length);
  112. }
  113. this.length = 2 + getVarIntLength(length) + length;
  114. this.dataLength = length;
  115. this.data = new byte[this.length];
  116. this.data[0] = (byte) META; // status value for MetaMessages (meta events)
  117. this.data[1] = (byte) type; // MetaMessage type
  118. writeVarInt(this.data, 2, length); // write the length as a variable int
  119. if (length > 0) {
  120. System.arraycopy(data, 0, this.data, this.length - this.dataLength, this.dataLength);
  121. }
  122. }
  123. /**
  124. * Obtains the type of the <code>MetaMessage</code>.
  125. * @return an integer representing the <code>MetaMessage</code> type
  126. */
  127. public int getType() {
  128. if (length>=2) {
  129. return data[1] & 0xFF;
  130. }
  131. return 0;
  132. }
  133. /**
  134. * Obtains a copy of the data for the meta message. The returned
  135. * array of bytes does not include the status byte or the message
  136. * length data. The length of the data for the meta message is
  137. * the length of the array. Note that the length of the entire
  138. * message includes the status byte and the meta message type
  139. * byte, and therefore may be longer than the returned array.
  140. * @return array containing the meta message data.
  141. * @see MidiMessage#getLength
  142. */
  143. public byte[] getData() {
  144. byte[] returnedArray = new byte[dataLength];
  145. System.arraycopy(data, (length - dataLength), returnedArray, 0, dataLength);
  146. return returnedArray;
  147. }
  148. /**
  149. * Creates a new object of the same class and with the same contents
  150. * as this object.
  151. * @return a clone of this instance
  152. */
  153. public Object clone() {
  154. byte[] newData = new byte[length];
  155. System.arraycopy(data, 0, newData, 0, newData.length);
  156. MetaMessage event = new MetaMessage(newData);
  157. return event;
  158. }
  159. // HELPER METHODS
  160. private int getVarIntLength(long value) {
  161. int length = 0;
  162. do {
  163. value = value >> 7;
  164. length++;
  165. } while (value > 0);
  166. return length;
  167. }
  168. private final static long mask = 0x7F;
  169. private void writeVarInt(byte[] data, int off, long value) {
  170. int shift=63; // number of bitwise left-shifts of mask
  171. // first screen out leading zeros
  172. while ((shift > 0) && ((value & (mask << shift)) == 0)) shift-=7;
  173. // then write actual values
  174. while (shift > 0) {
  175. data[off++]=(byte) (((value & (mask << shift)) >> shift) | 0x80);
  176. shift-=7;
  177. }
  178. data[off] = (byte) (value & mask);
  179. }
  180. }