1. /*
  2. * @(#)MetaMessage.java 1.22 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. 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.22, 03/01/23
  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. /*
  114. int oldLength=0;
  115. int oldDataLength=0;
  116. byte[] oldData=null;
  117. try {
  118. //$$fb 2001-10-06: this is a very inefficient implementation !
  119. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  120. DataOutputStream dos = new DataOutputStream(bos);
  121. dos.writeByte(0xFF); // status value for MetaMessages (meta events)
  122. dos.writeByte(type); // MetaMessage type
  123. writeVarInt( length, dos ); // write the length as a
  124. // variable int
  125. dos.write(data, 0, length);
  126. //this.data = bos.toByteArray();
  127. //this.length = this.data.length;
  128. //this.dataLength = length;
  129. oldData = bos.toByteArray();
  130. oldLength = oldData.length;
  131. oldDataLength = length;
  132. } catch (IOException e) {
  133. }
  134. */
  135. this.length = 2 + getVarIntLength(length) + length;
  136. this.dataLength = length;
  137. this.data = new byte[this.length];
  138. this.data[0] = (byte) META; // status value for MetaMessages (meta events)
  139. this.data[1] = (byte) type; // MetaMessage type
  140. writeVarInt(this.data, 2, length); // write the length as a variable int
  141. if (length > 0) {
  142. System.arraycopy(data, 0, this.data, this.length - this.dataLength, this.dataLength);
  143. }
  144. /*
  145. // check equality
  146. if (this.length != oldLength) {
  147. System.out.println("length = "+length+" oldLength = "+oldLength);
  148. } else
  149. if (this.dataLength != oldDataLength) {
  150. System.out.println("dataLength = "+dataLength+" oldDataLength = "+oldDataLength);
  151. } else
  152. if (this.data.length != oldData.length) {
  153. System.out.println("this.data.length = "+this.data.length+" oldData.length = "+oldData.length);
  154. } else {
  155. boolean succ=true;
  156. for (int i=0; i<this.data.length; i++) {
  157. if (this.data[i]!=oldData[i]) {
  158. succ=false;
  159. System.out.println(" this.data["+i+"] = "+this.data[i]+" oldData["+i+"] = "+oldData[i]);
  160. }
  161. }
  162. if (succ) {
  163. System.out.println("Message with length = "+length+" is equal.");
  164. }
  165. }
  166. */
  167. }
  168. /**
  169. * Obtains the type of the <code>MetaMessage</code>.
  170. * @return an integer representing the <code>MetaMessage</code> type
  171. */
  172. public int getType() {
  173. if (length>=2) {
  174. return data[1] & 0xFF;
  175. }
  176. return 0;
  177. }
  178. /**
  179. * Obtains a copy of the data for the meta message. The returned
  180. * array of bytes does not include the status byte or the message
  181. * length data. The length of the data for the meta message is
  182. * the length of the array. Note that the length of the entire
  183. * message includes the status byte and the meta message type
  184. * byte, and therefore may be longer than the returned array.
  185. * @return array containing the meta message data.
  186. * @see MidiMessage#getLength
  187. */
  188. public byte[] getData() {
  189. byte[] returnedArray = new byte[dataLength];
  190. System.arraycopy(data, (length - dataLength), returnedArray, 0, dataLength);
  191. return returnedArray;
  192. }
  193. /**
  194. * Creates a new object of the same class and with the same contents
  195. * as this object.
  196. * @return a clone of this instance
  197. */
  198. public Object clone() {
  199. byte[] newData = new byte[length];
  200. System.arraycopy(data, 0, newData, 0, newData.length);
  201. MetaMessage event = new MetaMessage(newData);
  202. return event;
  203. }
  204. // HELPER METHODS
  205. /*
  206. private void writeVarInt(int value, DataOutputStream dos ) throws InvalidMidiDataException, IOException {
  207. int MAX_LENGTH = 6;
  208. byte bytes[] = new byte[MAX_LENGTH];
  209. int length = 0;
  210. int currentByte = 0;
  211. // first make sure our array is zeroed
  212. for(int i=0; i<MAX_LENGTH; i++) bytes[i] = 0;
  213. // we fill this array up from the end so that bytes will be
  214. // written to the stream msb first
  215. for(int i=MAX_LENGTH-1; i >= 0; i--) {
  216. bytes[i] = (byte) (value & 0x7F);
  217. value = value >>> 7;
  218. length++;
  219. if( length>1 ) {
  220. // this is not the last byte to be written to the stream,
  221. // so change the msb to 1
  222. bytes[i] |= 0x80;
  223. }
  224. if( value==0 ) {
  225. // we don't need any more bytes to store this integer
  226. break;
  227. }
  228. }
  229. // make sure we terminated properly
  230. if ( (bytes[MAX_LENGTH-1] & 0x80) != 0 ) {
  231. throw new InvalidMidiDataException("Unable to create variable-length integer");
  232. }
  233. // now write our bytes, msb first, to the stream
  234. dos.write(bytes, (MAX_LENGTH-length), length);
  235. }
  236. */
  237. private int getVarIntLength(long value) {
  238. int length = 0;
  239. do {
  240. value = value >> 7;
  241. length++;
  242. } while (value > 0);
  243. return length;
  244. }
  245. private final static long mask = 0x7F;
  246. private void writeVarInt(byte[] data, int off, long value) {
  247. int shift=63; // number of bitwise left-shifts of mask
  248. // first screen out leading zeros
  249. while ((shift > 0) && ((value & (mask << shift)) == 0)) shift-=7;
  250. // then write actual values
  251. while (shift > 0) {
  252. data[off++]=(byte) (((value & (mask << shift)) >> shift) | 0x80);
  253. shift-=7;
  254. }
  255. data[off] = (byte) (value & mask);
  256. }
  257. }