FileDocCategorySizeDatePackage
MetaMessage.javaAPI DocJava SE 5 API7334Fri Aug 26 14:57:48 BST 2005javax.sound.midi

MetaMessage

public class MetaMessage extends MidiMessage
A MetaMessage is a {@link MidiMessage} that is not meaningful to synthesizers, but that can be stored in a MIDI file and interpreted by a sequencer program. (See the discussion in the MidiMessage class description.) The Standard MIDI Files specification defines various types of meta-events, such as sequence number, lyric, cue point, and set tempo. There are also meta-events for such information as lyrics, copyrights, tempo indications, time and key signatures, markers, etc. For more information, see the Standard MIDI Files 1.0 specification, which is part of the Complete MIDI 1.0 Detailed Specification published by the MIDI Manufacturer's Association (http://www.midi.org).

When data is being transported using MIDI wire protocol, a {@link ShortMessage} with the status value 0xFF represents a system reset message. In MIDI files, this same status value denotes a MetaMessage. The types of meta-message are distinguished from each other by the first byte that follows the status byte 0xFF. The subsequent bytes are data bytes. As with system exclusive messages, there are an arbitrary number of data bytes, depending on the type of MetaMessage.

see
MetaEventListener
version
1.24, 03/12/19
author
David Rivas
author
Kara Kytle

Fields Summary
public static final int
META
Status byte for MetaMessage (0xFF, or 255), which is used in MIDI files. It has the same value as SYSTEM_RESET, which is used in the real-time "MIDI wire" protocol.
private static byte[]
defaultMessage
private int
dataLength
The length of the actual message in the data array. This is used to determine how many bytes of the data array is the message, and how many are the status byte, the type byte, and the variable-length-int describing the length of the message.
private static final long
mask
Constructors Summary
public MetaMessage()
Constructs a new MetaMessage. The contents of the message are not set here; use {@link #setMessage(int, byte[], int) setMessage} to set them subsequently.



                                
      
	//super(defaultMessage);
	this(defaultMessage);
    
protected MetaMessage(byte[] data)
Constructs a new MetaMessage.

param
data an array of bytes containing the complete message. The message data may be changed using the setMessage method.
see
#setMessage

	super(data);
	//$$fb 2001-10-06: need to calculate dataLength. Fix for bug #4511796
	if (data.length>=3) {
	    dataLength=data.length-3;
	    int pos=2;
	    while (pos<data.length && (data[pos] & 0x80)!=0) {
		dataLength--; pos++;
	    }
	}
    
Methods Summary
public java.lang.Objectclone()
Creates a new object of the same class and with the same contents as this object.

return
a clone of this instance

	byte[] newData = new byte[length];
	System.arraycopy(data, 0, newData, 0, newData.length);

	MetaMessage event = new MetaMessage(newData);
	return event;
    
public byte[]getData()
Obtains a copy of the data for the meta message. The returned array of bytes does not include the status byte or the message length data. The length of the data for the meta message is the length of the array. Note that the length of the entire message includes the status byte and the meta message type byte, and therefore may be longer than the returned array.

return
array containing the meta message data.
see
MidiMessage#getLength

	byte[] returnedArray = new byte[dataLength];
	System.arraycopy(data, (length - dataLength), returnedArray, 0, dataLength);
	return returnedArray;
    
public intgetType()
Obtains the type of the MetaMessage.

return
an integer representing the MetaMessage type

	if (length>=2) {
	    return data[1] & 0xFF;
	}
	return 0;
    
private intgetVarIntLength(long value)

	int length = 0;
	do {
	    value = value >> 7;
	    length++;
	} while (value > 0);
	return length;
    
public voidsetMessage(int type, byte[] data, int length)
Sets the message parameters for a MetaMessage. Since only one status byte value, 0xFF, is allowed for meta-messages, it does not need to be specified here. Calls to {@link MidiMessage#getStatus getStatus} return 0xFF for all meta-messages.

The type argument should be a valid value for the byte that follows the status byte in the MetaMessage. The data argument should contain all the subsequent bytes of the MetaMessage. In other words, the byte that specifies the type of MetaMessage is not considered a data byte.

param
type meta-message type (must be less than 128)
param
data the data bytes in the MIDI message
param
length the number of bytes in the data byte array
throws
InvalidMidiDataException if the parameter values do not specify a valid MIDI meta message


	if (type >= 128 || type < 0) {
	    throw new InvalidMidiDataException("Invalid meta event with type " + type);
	}
	if ((length > 0 && length > data.length) || length < 0) {
	    throw new InvalidMidiDataException("length out of bounds: "+length);
	}
	
	this.length = 2 + getVarIntLength(length) + length;
	this.dataLength = length;
	this.data = new byte[this.length];
	this.data[0] = (byte) META;        // status value for MetaMessages (meta events)
	this.data[1] = (byte) type;        // MetaMessage type
	writeVarInt(this.data, 2, length); // write the length as a variable int
	if (length > 0) {
	    System.arraycopy(data, 0, this.data, this.length - this.dataLength, this.dataLength);
	}
    
private voidwriteVarInt(byte[] data, int off, long value)


            
    	int shift=63; // number of bitwise left-shifts of mask
    	// first screen out leading zeros
    	while ((shift > 0) && ((value & (mask << shift)) == 0)) shift-=7;
    	// then write actual values
    	while (shift > 0) {
	    data[off++]=(byte) (((value & (mask << shift)) >> shift) | 0x80);
	    shift-=7;
    	}
    	data[off] = (byte) (value & mask);