1 7 8 package javax.sound.midi; 9 10 import java.io.ByteArrayOutputStream ; 11 import java.io.DataOutputStream ; 12 import java.io.IOException ; 13 14 15 43 44 public class MetaMessage extends MidiMessage { 45 46 47 49 55 public static final int META = 0xFF; 57 58 61 private static byte[] defaultMessage = { (byte)META, 0 }; 62 63 64 65 67 74 private int dataLength = 0; 75 76 77 83 public MetaMessage() { 84 this(defaultMessage); 86 } 87 88 89 96 protected MetaMessage(byte[] data) { 97 super(data); 98 if (data.length>=3) { 100 dataLength=data.length-3; 101 int pos=2; 102 while (pos<data.length && (data[pos] & 0x80)!=0) { 103 dataLength--; pos++; 104 } 105 } 106 } 107 108 109 127 public void setMessage(int type, byte[] data, int length) throws InvalidMidiDataException { 128 129 if (type >= 128 || type < 0) { 130 throw new InvalidMidiDataException ("Invalid meta event with type " + type); 131 } 132 if ((length > 0 && length > data.length) || length < 0) { 133 throw new InvalidMidiDataException ("length out of bounds: "+length); 134 } 135 136 this.length = 2 + getVarIntLength(length) + length; 137 this.dataLength = length; 138 this.data = new byte[this.length]; 139 this.data[0] = (byte) META; this.data[1] = (byte) type; writeVarInt(this.data, 2, length); if (length > 0) { 143 System.arraycopy(data, 0, this.data, this.length - this.dataLength, this.dataLength); 144 } 145 } 146 147 148 152 public int getType() { 153 if (length>=2) { 154 return data[1] & 0xFF; 155 } 156 return 0; 157 } 158 159 160 161 171 public byte[] getData() { 172 byte[] returnedArray = new byte[dataLength]; 173 System.arraycopy(data, (length - dataLength), returnedArray, 0, dataLength); 174 return returnedArray; 175 } 176 177 178 183 public Object clone() { 184 byte[] newData = new byte[length]; 185 System.arraycopy(data, 0, newData, 0, newData.length); 186 187 MetaMessage event = new MetaMessage (newData); 188 return event; 189 } 190 191 193 private int getVarIntLength(long value) { 194 int length = 0; 195 do { 196 value = value >> 7; 197 length++; 198 } while (value > 0); 199 return length; 200 } 201 202 private final static long mask = 0x7F; 203 204 private void writeVarInt(byte[] data, int off, long value) { 205 int shift=63; while ((shift > 0) && ((value & (mask << shift)) == 0)) shift-=7; 208 while (shift > 0) { 210 data[off++]=(byte) (((value & (mask << shift)) >> shift) | 0x80); 211 shift-=7; 212 } 213 data[off] = (byte) (value & mask); 214 } 215 216 } 217 | Popular Tags |