24 */ |
24 */ |
25 |
25 |
26 package javax.sound.midi; |
26 package javax.sound.midi; |
27 |
27 |
28 /** |
28 /** |
29 * <code>MidiMessage</code> is the base class for MIDI messages. They include |
29 * {@code MidiMessage} is the base class for MIDI messages. They include not |
30 * not only the standard MIDI messages that a synthesizer can respond to, but also |
30 * only the standard MIDI messages that a synthesizer can respond to, but also |
31 * "meta-events" that can be used by sequencer programs. There are meta-events |
31 * "meta-events" that can be used by sequencer programs. There are meta-events |
32 * for such information as lyrics, copyrights, tempo indications, time and key |
32 * for such information as lyrics, copyrights, tempo indications, time and key |
33 * signatures, markers, etc. For more information, see the Standard MIDI Files 1.0 |
33 * signatures, markers, etc. For more information, see the Standard MIDI Files |
34 * specification, which is part of the Complete MIDI 1.0 Detailed Specification |
34 * 1.0 specification, which is part of the Complete MIDI 1.0 Detailed |
35 * published by the MIDI Manufacturer's Association |
35 * Specification published by the MIDI Manufacturer's Association |
36 * (<a href = http://www.midi.org>http://www.midi.org</a>). |
36 * (<a href = http://www.midi.org>http://www.midi.org</a>). |
37 * <p> |
37 * <p> |
38 * The base <code>MidiMessage</code> class provides access to three types of |
38 * The base {@code MidiMessage} class provides access to three types of |
39 * information about a MIDI message: |
39 * information about a MIDI message: |
40 * <ul> |
40 * <ul> |
41 * <li>The messages's status byte</li> |
41 * <li>The messages's status byte</li> |
42 * <li>The total length of the message in bytes (the status byte plus any data bytes)</li> |
42 * <li>The total length of the message in bytes (the status byte plus any data |
|
43 * bytes)</li> |
43 * <li>A byte array containing the complete message</li> |
44 * <li>A byte array containing the complete message</li> |
44 * </ul> |
45 * </ul> |
45 * |
46 * |
46 * <code>MidiMessage</code> includes methods to get, but not set, these values. |
47 * {@code MidiMessage} includes methods to get, but not set, these values. |
47 * Setting them is a subclass responsibility. |
48 * Setting them is a subclass responsibility. |
48 * <p> |
49 * <p> |
49 * <a name="integersVsBytes"></a> |
50 * <a name="integersVsBytes"></a> The MIDI standard expresses MIDI data in |
50 * The MIDI standard expresses MIDI data in bytes. However, because |
51 * bytes. However, because Java<sup>TM</sup> uses signed bytes, the Java Sound |
51 * Java<sup>TM</sup> uses signed bytes, the Java Sound API uses integers |
52 * API uses integers instead of bytes when expressing MIDI data. For example, |
52 * instead of bytes when expressing MIDI data. For example, the |
53 * the {@link #getStatus()} method of {@code MidiMessage} returns MIDI status |
53 * {@link #getStatus()} method of |
54 * bytes as integers. If you are processing MIDI data that originated outside |
54 * <code>MidiMessage</code> returns MIDI status bytes as integers. If you are |
55 * Java Sound and now is encoded as signed bytes, the bytes can can be |
55 * processing MIDI data that originated outside Java Sound and now |
56 * converted to integers using this conversion: |
56 * is encoded as signed bytes, the bytes can |
57 * |
57 * can be converted to integers using this conversion: |
|
58 * <center>{@code int i = (int)(byte & 0xFF)}</center> |
58 * <center>{@code int i = (int)(byte & 0xFF)}</center> |
59 * <p> |
59 * <p> |
60 * If you simply need to pass a known MIDI byte value as a method parameter, |
60 * If you simply need to pass a known MIDI byte value as a method parameter, it |
61 * it can be expressed directly as an integer, using (for example) decimal or |
61 * can be expressed directly as an integer, using (for example) decimal or |
62 * hexadecimal notation. For instance, to pass the "active sensing" status byte |
62 * hexadecimal notation. For instance, to pass the "active sensing" status byte |
63 * as the first argument to ShortMessage's |
63 * as the first argument to ShortMessage's |
64 * {@link ShortMessage#setMessage(int) setMessage(int)} |
64 * {@link ShortMessage#setMessage(int) setMessage(int)} method, you can express |
65 * method, you can express it as 254 or 0xFE. |
65 * it as 254 or 0xFE. |
66 * |
66 * |
|
67 * @author David Rivas |
|
68 * @author Kara Kytle |
67 * @see Track |
69 * @see Track |
68 * @see Sequence |
70 * @see Sequence |
69 * @see Receiver |
71 * @see Receiver |
70 * |
|
71 * @author David Rivas |
|
72 * @author Kara Kytle |
|
73 */ |
72 */ |
74 |
|
75 public abstract class MidiMessage implements Cloneable { |
73 public abstract class MidiMessage implements Cloneable { |
76 |
74 |
77 // Instance variables |
|
78 |
|
79 /** |
75 /** |
80 * The MIDI message data. The first byte is the status |
76 * The MIDI message data. The first byte is the status byte for the message; |
81 * byte for the message; subsequent bytes up to the length |
77 * subsequent bytes up to the length of the message are data bytes for this |
82 * of the message are data bytes for this message. |
78 * message. |
|
79 * |
83 * @see #getLength |
80 * @see #getLength |
84 */ |
81 */ |
85 protected byte[] data; |
82 protected byte[] data; |
86 |
83 |
87 |
|
88 /** |
84 /** |
89 * The number of bytes in the MIDI message, including the |
85 * The number of bytes in the MIDI message, including the status byte and |
90 * status byte and any data bytes. |
86 * any data bytes. |
|
87 * |
91 * @see #getLength |
88 * @see #getLength |
92 */ |
89 */ |
93 protected int length = 0; |
90 protected int length = 0; |
94 |
91 |
95 |
|
96 /** |
92 /** |
97 * Constructs a new <code>MidiMessage</code>. This protected |
93 * Constructs a new {@code MidiMessage}. This protected constructor is |
98 * constructor is called by concrete subclasses, which should |
94 * called by concrete subclasses, which should ensure that the data array |
99 * ensure that the data array specifies a complete, valid MIDI |
95 * specifies a complete, valid MIDI message. |
100 * message. |
|
101 * |
96 * |
102 * @param data an array of bytes containing the complete message. |
97 * @param data an array of bytes containing the complete message. The |
103 * The message data may be changed using the <code>setMessage</code> |
98 * message data may be changed using the {@code setMessage} method. |
104 * method. |
|
105 * |
|
106 * @see #setMessage |
99 * @see #setMessage |
107 */ |
100 */ |
108 protected MidiMessage(byte[] data) { |
101 protected MidiMessage(byte[] data) { |
109 this.data = data; |
102 this.data = data; |
110 if (data != null) { |
103 if (data != null) { |
111 this.length = data.length; |
104 this.length = data.length; |
112 } |
105 } |
113 } |
106 } |
114 |
107 |
115 |
|
116 /** |
108 /** |
117 * Sets the data for the MIDI message. This protected |
109 * Sets the data for the MIDI message. This protected method is called by |
118 * method is called by concrete subclasses, which should |
110 * concrete subclasses, which should ensure that the data array specifies a |
119 * ensure that the data array specifies a complete, valid MIDI |
111 * complete, valid MIDI message. |
120 * message. |
|
121 * |
112 * |
122 * @param data the data bytes in the MIDI message |
113 * @param data the data bytes in the MIDI message |
123 * @param length the number of bytes in the data byte array |
114 * @param length the number of bytes in the data byte array |
124 * @throws InvalidMidiDataException if the parameter values do not specify a valid MIDI meta message |
115 * @throws InvalidMidiDataException if the parameter values do not specify a |
|
116 * valid MIDI meta message |
125 */ |
117 */ |
126 protected void setMessage(byte[] data, int length) throws InvalidMidiDataException { |
118 protected void setMessage(byte[] data, int length) |
|
119 throws InvalidMidiDataException { |
127 if (length < 0 || (length > 0 && length > data.length)) { |
120 if (length < 0 || (length > 0 && length > data.length)) { |
128 throw new IndexOutOfBoundsException("length out of bounds: "+length); |
121 throw new IndexOutOfBoundsException( |
|
122 "length out of bounds: " + length); |
129 } |
123 } |
130 this.length = length; |
124 this.length = length; |
131 |
125 |
132 if (this.data == null || this.data.length < this.length) { |
126 if (this.data == null || this.data.length < this.length) { |
133 this.data = new byte[this.length]; |
127 this.data = new byte[this.length]; |
134 } |
128 } |
135 System.arraycopy(data, 0, this.data, 0, length); |
129 System.arraycopy(data, 0, this.data, 0, length); |
136 } |
130 } |
137 |
131 |
138 |
|
139 /** |
132 /** |
140 * Obtains the MIDI message data. The first byte of the returned byte |
133 * Obtains the MIDI message data. The first byte of the returned byte array |
141 * array is the status byte of the message. Any subsequent bytes up to |
134 * is the status byte of the message. Any subsequent bytes up to the length |
142 * the length of the message are data bytes. The byte array may have a |
135 * of the message are data bytes. The byte array may have a length which is |
143 * length which is greater than that of the actual message; the total |
136 * greater than that of the actual message; the total length of the message |
144 * length of the message in bytes is reported by the <code>{@link #getLength}</code> |
137 * in bytes is reported by the {@link #getLength} method. |
145 * method. |
|
146 * |
138 * |
147 * @return the byte array containing the complete <code>MidiMessage</code> data |
139 * @return the byte array containing the complete {@code MidiMessage} data |
148 */ |
140 */ |
149 public byte[] getMessage() { |
141 public byte[] getMessage() { |
150 byte[] returnedArray = new byte[length]; |
142 byte[] returnedArray = new byte[length]; |
151 System.arraycopy(data, 0, returnedArray, 0, length); |
143 System.arraycopy(data, 0, returnedArray, 0, length); |
152 return returnedArray; |
144 return returnedArray; |
153 } |
145 } |
154 |
146 |
155 |
|
156 /** |
147 /** |
157 * Obtains the status byte for the MIDI message. The status "byte" is |
148 * Obtains the status byte for the MIDI message. The status "byte" is |
158 * represented as an integer; see the |
149 * represented as an integer; see the |
159 * <a href="#integersVsBytes">discussion</a> in the |
150 * <a href="#integersVsBytes">discussion</a> in the {@code MidiMessage} |
160 * <code>MidiMessage</code> class description. |
151 * class description. |
161 * |
152 * |
162 * @return the integer representation of this event's status byte |
153 * @return the integer representation of this event's status byte |
163 */ |
154 */ |
164 public int getStatus() { |
155 public int getStatus() { |
165 if (length > 0) { |
156 if (length > 0) { |
166 return (data[0] & 0xFF); |
157 return (data[0] & 0xFF); |
167 } |
158 } |
168 return 0; |
159 return 0; |
169 } |
160 } |
170 |
161 |
171 |
|
172 /** |
162 /** |
173 * Obtains the total length of the MIDI message in bytes. A |
163 * Obtains the total length of the MIDI message in bytes. A MIDI message |
174 * MIDI message consists of one status byte and zero or more |
164 * consists of one status byte and zero or more data bytes. The return value |
175 * data bytes. The return value ranges from 1 for system real-time messages, |
165 * ranges from 1 for system real-time messages, to 2 or 3 for channel |
176 * to 2 or 3 for channel messages, to any value for meta and system |
166 * messages, to any value for meta and system exclusive messages. |
177 * exclusive messages. |
|
178 * |
167 * |
179 * @return the length of the message in bytes |
168 * @return the length of the message in bytes |
180 */ |
169 */ |
181 public int getLength() { |
170 public int getLength() { |
182 return length; |
171 return length; |
183 } |
172 } |
184 |
173 |
185 |
|
186 /** |
174 /** |
187 * Creates a new object of the same class and with the same contents |
175 * Creates a new object of the same class and with the same contents as this |
188 * as this object. |
176 * object. |
189 * @return a clone of this instance. |
177 * |
|
178 * @return a clone of this instance |
190 */ |
179 */ |
191 public abstract Object clone(); |
180 public abstract Object clone(); |
192 } |
181 } |