author | amenkov |
Fri, 17 Apr 2009 15:02:46 +0400 | |
changeset 2651 | c6612086e8c9 |
parent 2 | 90ce3da70b43 |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* Copyright 1999-2003 Sun Microsystems, Inc. All Rights Reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package javax.sound.midi; |
|
27 |
||
28 |
import java.io.ByteArrayOutputStream; |
|
29 |
import java.io.DataOutputStream; |
|
30 |
import java.io.IOException; |
|
31 |
||
32 |
||
33 |
/** |
|
34 |
* A <code>MetaMessage</code> is a <code>{@link MidiMessage}</code> that is not meaningful to synthesizers, but |
|
35 |
* that can be stored in a MIDI file and interpreted by a sequencer program. |
|
36 |
* (See the discussion in the <code>MidiMessage</code> |
|
37 |
* class description.) The Standard MIDI Files specification defines |
|
38 |
* various types of meta-events, such as sequence number, lyric, cue point, |
|
39 |
* and set tempo. There are also meta-events |
|
40 |
* for such information as lyrics, copyrights, tempo indications, time and key |
|
41 |
* signatures, markers, etc. For more information, see the Standard MIDI Files 1.0 |
|
42 |
* specification, which is part of the Complete MIDI 1.0 Detailed Specification |
|
43 |
* published by the MIDI Manufacturer's Association |
|
44 |
* (<a href = http://www.midi.org>http://www.midi.org</a>). |
|
45 |
* |
|
46 |
* <p> |
|
47 |
* When data is being transported using MIDI wire protocol, |
|
48 |
* a <code>{@link ShortMessage}</code> with the status value <code>0xFF</code> represents |
|
49 |
* a system reset message. In MIDI files, this same status value denotes a <code>MetaMessage</code>. |
|
50 |
* The types of meta-message are distinguished from each other by the first byte |
|
51 |
* that follows the status byte <code>0xFF</code>. The subsequent bytes are data |
|
52 |
* bytes. As with system exclusive messages, there are an arbitrary number of |
|
53 |
* data bytes, depending on the type of <code>MetaMessage</code>. |
|
54 |
* |
|
55 |
* @see MetaEventListener |
|
56 |
* |
|
57 |
* @author David Rivas |
|
58 |
* @author Kara Kytle |
|
59 |
*/ |
|
60 |
||
61 |
public class MetaMessage extends MidiMessage { |
|
62 |
||
63 |
||
64 |
// Status byte defines |
|
65 |
||
66 |
/** |
|
67 |
* Status byte for <code>MetaMessage</code> (0xFF, or 255), which is used |
|
68 |
* in MIDI files. It has the same value as SYSTEM_RESET, which |
|
69 |
* is used in the real-time "MIDI wire" protocol. |
|
70 |
* @see MidiMessage#getStatus |
|
71 |
*/ |
|
72 |
public static final int META = 0xFF; // 255 |
|
73 |
||
74 |
||
75 |
// Default meta message data: just the META status byte value |
|
76 |
// $$kk: 09.09.99: need a real event here!! |
|
77 |
||
78 |
private static byte[] defaultMessage = { (byte)META, 0 }; |
|
79 |
||
80 |
||
81 |
||
82 |
// Instance variables |
|
83 |
||
84 |
/** |
|
85 |
* The length of the actual message in the data array. |
|
86 |
* This is used to determine how many bytes of the data array |
|
87 |
* is the message, and how many are the status byte, the |
|
88 |
* type byte, and the variable-length-int describing the |
|
89 |
* length of the message. |
|
90 |
*/ |
|
91 |
private int dataLength = 0; |
|
92 |
||
93 |
||
94 |
/** |
|
95 |
* Constructs a new <code>MetaMessage</code>. The contents of |
|
96 |
* the message are not set here; use |
|
97 |
* {@link #setMessage(int, byte[], int) setMessage} |
|
98 |
* to set them subsequently. |
|
99 |
*/ |
|
100 |
public MetaMessage() { |
|
101 |
//super(defaultMessage); |
|
102 |
this(defaultMessage); |
|
103 |
} |
|
104 |
||
2651
c6612086e8c9
5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents:
2
diff
changeset
|
105 |
/** |
c6612086e8c9
5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents:
2
diff
changeset
|
106 |
* Constructs a new {@code MetaMessage} and sets the message parameters. |
c6612086e8c9
5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents:
2
diff
changeset
|
107 |
* The contents of the message can be changed by using |
c6612086e8c9
5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents:
2
diff
changeset
|
108 |
* the {@code setMessage} method. |
c6612086e8c9
5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents:
2
diff
changeset
|
109 |
* |
c6612086e8c9
5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents:
2
diff
changeset
|
110 |
* @param type meta-message type (must be less than 128) |
c6612086e8c9
5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents:
2
diff
changeset
|
111 |
* @param data the data bytes in the MIDI message |
c6612086e8c9
5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents:
2
diff
changeset
|
112 |
* @param length an amount of bytes in the {@code data} byte array; |
c6612086e8c9
5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents:
2
diff
changeset
|
113 |
* it should be non-negative and less than or equal to |
c6612086e8c9
5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents:
2
diff
changeset
|
114 |
* {@code data.length} |
c6612086e8c9
5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents:
2
diff
changeset
|
115 |
* @throws InvalidMidiDataException if the parameter values do not specify |
c6612086e8c9
5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents:
2
diff
changeset
|
116 |
* a valid MIDI meta message |
c6612086e8c9
5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents:
2
diff
changeset
|
117 |
* @see #setMessage(int, byte[], int) |
c6612086e8c9
5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents:
2
diff
changeset
|
118 |
* @see #getType() |
c6612086e8c9
5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents:
2
diff
changeset
|
119 |
* @see #getData() |
c6612086e8c9
5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents:
2
diff
changeset
|
120 |
* @since 1.7 |
c6612086e8c9
5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents:
2
diff
changeset
|
121 |
*/ |
c6612086e8c9
5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents:
2
diff
changeset
|
122 |
public MetaMessage(int type, byte[] data, int length) |
c6612086e8c9
5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents:
2
diff
changeset
|
123 |
throws InvalidMidiDataException { |
c6612086e8c9
5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents:
2
diff
changeset
|
124 |
super(null); |
c6612086e8c9
5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents:
2
diff
changeset
|
125 |
setMessage(type, data, length); // can throw InvalidMidiDataException |
c6612086e8c9
5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents:
2
diff
changeset
|
126 |
} |
c6612086e8c9
5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents:
2
diff
changeset
|
127 |
|
2 | 128 |
|
129 |
/** |
|
130 |
* Constructs a new <code>MetaMessage</code>. |
|
131 |
* @param data an array of bytes containing the complete message. |
|
132 |
* The message data may be changed using the <code>setMessage</code> |
|
133 |
* method. |
|
134 |
* @see #setMessage |
|
135 |
*/ |
|
136 |
protected MetaMessage(byte[] data) { |
|
137 |
super(data); |
|
138 |
//$$fb 2001-10-06: need to calculate dataLength. Fix for bug #4511796 |
|
139 |
if (data.length>=3) { |
|
140 |
dataLength=data.length-3; |
|
141 |
int pos=2; |
|
142 |
while (pos<data.length && (data[pos] & 0x80)!=0) { |
|
143 |
dataLength--; pos++; |
|
144 |
} |
|
145 |
} |
|
146 |
} |
|
147 |
||
148 |
||
149 |
/** |
|
150 |
* Sets the message parameters for a <code>MetaMessage</code>. |
|
151 |
* Since only one status byte value, <code>0xFF</code>, is allowed for meta-messages, |
|
152 |
* it does not need to be specified here. Calls to <code>{@link MidiMessage#getStatus getStatus}</code> return |
|
153 |
* <code>0xFF</code> for all meta-messages. |
|
154 |
* <p> |
|
155 |
* The <code>type</code> argument should be a valid value for the byte that |
|
156 |
* follows the status byte in the <code>MetaMessage</code>. The <code>data</code> argument |
|
157 |
* should contain all the subsequent bytes of the <code>MetaMessage</code>. In other words, |
|
158 |
* the byte that specifies the type of <code>MetaMessage</code> is not considered a data byte. |
|
159 |
* |
|
160 |
* @param type meta-message type (must be less than 128) |
|
161 |
* @param data the data bytes in the MIDI message |
|
162 |
* @param length the number of bytes in the <code>data</code> |
|
163 |
* byte array |
|
164 |
* @throws <code>InvalidMidiDataException</code> if the |
|
165 |
* parameter values do not specify a valid MIDI meta message |
|
166 |
*/ |
|
167 |
public void setMessage(int type, byte[] data, int length) throws InvalidMidiDataException { |
|
168 |
||
169 |
if (type >= 128 || type < 0) { |
|
170 |
throw new InvalidMidiDataException("Invalid meta event with type " + type); |
|
171 |
} |
|
172 |
if ((length > 0 && length > data.length) || length < 0) { |
|
173 |
throw new InvalidMidiDataException("length out of bounds: "+length); |
|
174 |
} |
|
175 |
||
176 |
this.length = 2 + getVarIntLength(length) + length; |
|
177 |
this.dataLength = length; |
|
178 |
this.data = new byte[this.length]; |
|
179 |
this.data[0] = (byte) META; // status value for MetaMessages (meta events) |
|
180 |
this.data[1] = (byte) type; // MetaMessage type |
|
181 |
writeVarInt(this.data, 2, length); // write the length as a variable int |
|
182 |
if (length > 0) { |
|
183 |
System.arraycopy(data, 0, this.data, this.length - this.dataLength, this.dataLength); |
|
184 |
} |
|
185 |
} |
|
186 |
||
187 |
||
188 |
/** |
|
189 |
* Obtains the type of the <code>MetaMessage</code>. |
|
190 |
* @return an integer representing the <code>MetaMessage</code> type |
|
191 |
*/ |
|
192 |
public int getType() { |
|
193 |
if (length>=2) { |
|
194 |
return data[1] & 0xFF; |
|
195 |
} |
|
196 |
return 0; |
|
197 |
} |
|
198 |
||
199 |
||
200 |
||
201 |
/** |
|
202 |
* Obtains a copy of the data for the meta message. The returned |
|
203 |
* array of bytes does not include the status byte or the message |
|
204 |
* length data. The length of the data for the meta message is |
|
205 |
* the length of the array. Note that the length of the entire |
|
206 |
* message includes the status byte and the meta message type |
|
207 |
* byte, and therefore may be longer than the returned array. |
|
208 |
* @return array containing the meta message data. |
|
209 |
* @see MidiMessage#getLength |
|
210 |
*/ |
|
211 |
public byte[] getData() { |
|
212 |
byte[] returnedArray = new byte[dataLength]; |
|
213 |
System.arraycopy(data, (length - dataLength), returnedArray, 0, dataLength); |
|
214 |
return returnedArray; |
|
215 |
} |
|
216 |
||
217 |
||
218 |
/** |
|
219 |
* Creates a new object of the same class and with the same contents |
|
220 |
* as this object. |
|
221 |
* @return a clone of this instance |
|
222 |
*/ |
|
223 |
public Object clone() { |
|
224 |
byte[] newData = new byte[length]; |
|
225 |
System.arraycopy(data, 0, newData, 0, newData.length); |
|
226 |
||
227 |
MetaMessage event = new MetaMessage(newData); |
|
228 |
return event; |
|
229 |
} |
|
230 |
||
231 |
// HELPER METHODS |
|
232 |
||
233 |
private int getVarIntLength(long value) { |
|
234 |
int length = 0; |
|
235 |
do { |
|
236 |
value = value >> 7; |
|
237 |
length++; |
|
238 |
} while (value > 0); |
|
239 |
return length; |
|
240 |
} |
|
241 |
||
242 |
private final static long mask = 0x7F; |
|
243 |
||
244 |
private void writeVarInt(byte[] data, int off, long value) { |
|
245 |
int shift=63; // number of bitwise left-shifts of mask |
|
246 |
// first screen out leading zeros |
|
247 |
while ((shift > 0) && ((value & (mask << shift)) == 0)) shift-=7; |
|
248 |
// then write actual values |
|
249 |
while (shift > 0) { |
|
250 |
data[off++]=(byte) (((value & (mask << shift)) >> shift) | 0x80); |
|
251 |
shift-=7; |
|
252 |
} |
|
253 |
data[off] = (byte) (value & mask); |
|
254 |
} |
|
255 |
||
256 |
} |