1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
/* ShortMessage.java -- A MIDI message no longer than 3 bytes
Copyright (C) 2005 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package javax.sound.midi;
/**
* A short MIDI message that is no longer than 3 bytes long.
*
* @author Anthony Green (green@redhat.com)
* @since 1.3
*
*/
public class ShortMessage extends MidiMessage
{
/**
* Status byte for Time Code message.
*/
public static final int MIDI_TIME_CODE = 0xF1;
/**
* Status byte for Song Position Pointer message.
*/
public static final int SONG_POSITION_POINTER = 0xF2;
/**
* Status byte for Song Select message.
*/
public static final int SONG_SELECT = 0xF3;
/**
* Status byte for Tune Request message.
*/
public static final int TUNE_REQUEST = 0xF6;
/**
* Status byte for End Of Exclusive message.
*/
public static final int END_OF_EXCLUSIVE = 0xF7;
/**
* Status byte for Timing Clock message.
*/
public static final int TIMING_CLOCK = 0xF8;
/**
* Status byte for Start message.
*/
public static final int START = 0xFA;
/**
* Status byte for Continue message.
*/
public static final int CONTINUE = 0xFB;
/**
* Status byte for Stop message.
*/
public static final int STOP = 0xFC;
/**
* Status byte for Active Sensing message.
*/
public static final int ACTIVE_SENSING = 0xFE;
/**
* Status byte for System Reset message.
*/
public static final int SYSTEM_RESET = 0xFF;
/**
* Status nibble for Note Off message.
*/
public static final int NOTE_OFF = 0x80;
/**
* Status nibble for Note On message.
*/
public static final int NOTE_ON = 0x90;
/**
* Status nibble for Poly Pressure message.
*/
public static final int POLY_PRESSURE = 0xA0;
/**
* Status nibble for Control Change message.
*/
public static final int CONTROL_CHANGE = 0xB0;
/**
* Status nibble for Program Change message.
*/
public static final int PROGRAM_CHANGE = 0xC0;
/**
* Statue nibble for Channel Pressure message.
*/
public static final int CHANNEL_PRESSURE = 0xD0;
/**
* Status nibble for Pitch Bend message.
*/
public static final int PITCH_BEND = 0xE0;
// Create and initialize a default, arbitrary message.
private static byte[] defaultMessage;
static
{
defaultMessage = new byte[1];
defaultMessage[0] = (byte) STOP;
}
/**
* Create a short MIDI message.
*
* The spec requires that this represent a valid MIDI message, but doesn't
* specify what it should be. We've chosen the STOP message for our
* implementation.
*/
public ShortMessage()
{
this(defaultMessage);
}
/**
* Create a short MIDI message.
*
* The data argument should be a valid MIDI message. Unfortunately the spec
* does not allow us to throw an InvalidMidiDataException if data is invalid.
*
* @param data the message data
*/
protected ShortMessage(byte[] data)
{
super(data);
}
/**
* Set the MIDI message.
*
* @param status the status byte for this message
* @param data1 the first data byte for this message
* @param data2 the second data byte for this message
* @throws InvalidMidiDataException if status is bad, or data is out of range
*/
public void setMessage(int status, int data1, int data2)
throws InvalidMidiDataException
{
length = getDataLength(status);
length++;
if (data == null || data.length < length)
data = new byte[length];
data[0] = (byte) status;
if (length > 1)
{
if (data1 < 0 || data1 > 127)
throw new InvalidMidiDataException("data1 (" + data1
+ ") must be between 0 and 127.");
data[1] = (byte) data1;
if (length > 2)
{
if (data2 < 0 || data2 > 127)
throw new InvalidMidiDataException("data2 (" + data2
+ ") must be between 0 and 127.");
data[2] = (byte) data2;
}
}
}
public void setMessage(int command, int channel, int data1, int data2)
throws InvalidMidiDataException
{
// TODO: This could probably stand some error checking.
// It currently assumes command and channel are valid values.
setMessage(command + channel, data1, data2);
}
/**
* Set the MIDI message to one that requires no data bytes.
*
* @param status the status byte for this message
* @throws InvalidMidiDataException if status is bad, or requires data
*/
public void setMessage(int status) throws InvalidMidiDataException
{
int length = getDataLength(status);
if (length != 0)
throw new InvalidMidiDataException("Status byte 0x"
+ Integer.toHexString(status)
+ " requires "
+ length + " bytes of data.");
setMessage(status, 0, 0);
}
/**
* Return the number of data bytes needed for a given MIDI status byte.
*
* @param status the status byte for a short MIDI message
* @return the number of data bytes needed for this status byte
* @throws InvalidMidiDataException if status is an invalid status byte
*/
protected final int getDataLength(int status) throws InvalidMidiDataException
{
int originalStatus = status;
if ((status & 0xF0) != 0xF0)
status &= 0xF0;
switch (status)
{
case NOTE_OFF:
case NOTE_ON:
case POLY_PRESSURE:
case CONTROL_CHANGE:
case PITCH_BEND:
case SONG_POSITION_POINTER:
return 2;
case PROGRAM_CHANGE:
case CHANNEL_PRESSURE:
case SONG_SELECT:
case 0xF5: // FIXME: unofficial bus select. Not in spec??
return 1;
case TUNE_REQUEST:
case END_OF_EXCLUSIVE:
case TIMING_CLOCK:
case START:
case CONTINUE:
case STOP:
case ACTIVE_SENSING:
case SYSTEM_RESET:
return 0;
default:
throw new InvalidMidiDataException("Invalid status: 0x"
+ Integer.toHexString(originalStatus));
}
}
/**
* Get the channel information from this MIDI message, assuming it is a
* MIDI channel message.
*
* @return the MIDI channel for this message
*/
public int getChannel()
{
return data[0] & 0x0F;
}
/**
* Get the command nibble from this MIDI message, assuming it is a MIDI
* channel message.
*
* @return the MIDI command for this message
*/
public int getCommand()
{
return data[0] & 0xF0;
}
/**
* Get the first data byte from this message, assuming it exists, and
* zero otherwise.
*
* @return the first data byte or zero if none exists.
*/
public int getData1()
{
if (length > 1)
return data[1];
else
return 0;
}
/**
* Get the second data byte from this message, assuming it exists, and
* zero otherwise.
*
* @return the second date byte or zero if none exists.
*/
public int getData2()
{
if (length > 2)
return data[2];
else
return 0;
}
/* Create a deep-copy clone of this object.
* @see java.lang.Object#clone()
*/
public Object clone()
{
byte message[] = new byte[length];
System.arraycopy(data, 0, message, 0, length);
return new ShortMessage(message);
}
}
|