-
Notifications
You must be signed in to change notification settings - Fork 0
/
midi.c
430 lines (350 loc) · 15.2 KB
/
midi.c
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
* Copyright (c) 2018, The libmidi authors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "midi.h"
/*
* Masks / patterns that define leading high bits; these are used to
* determine what type of status message has been received.
*/
#define SYS_REALTIME_MASK 0xf8
#define SYS_COMMON_MASK 0xf0
#define CHAN_STATUS_MASK 0x80
#define CHAN_TYPE_MASK 0xf0 // Mask for extracting channel msg type
#define CHAN_MASK 0x0f // Mask for extracting channel number
/**
* System real-time status message types.
*/
#define SYS_REALTIME_TIMING_CLOCK 0xf8 // Timing clock sent 24 times/quarter.
#define SYS_REALTIME_RESERVED_F9 0xf9 // Undefined (Reserved.)
#define SYS_REALTIME_SEQ_START 0xfa // Start the current sequence.
#define SYS_REALTIME_SEQ_CONTINUE 0xfb // Continue the current sequence.
#define SYS_REALTIME_SEQ_STOP 0xfc // Stop the current sequence.
#define SYS_REALTIME_RESERVED_FD 0xfd // Undefined (Reserved.)
#define SYS_REALTIME_ACTIVE_SENSE 0xfe // Active sensing message (each 300ms.)
#define SYS_REALTIME_RESET 0xff // Reset all receivers to power-up.
/**
* Define channel voice message types. Before the type can be compared with a
* MIDI channel status byte, the CHAN_TYPE_MASK must be used to mask off the
* channel bits.
*/
#define CHAN_NOTE_OFF 0x80
#define CHAN_NOTE_ON 0x90
#define CHAN_POLY_AFTER_TOUCH 0xa0
#define CHAN_CONTROL_CHANGE 0xb0 // Could be a CHANNEL MODE message
#define CHAN_PROGRAM_CHANGE 0xc0
#define CHAN_AFTER_TOUCH 0xd0
#define CHAN_PITCH_BEND 0xe0
/**
* Enumeration that represents the states that the MIDI protocol
* state machine can be in.
*/
enum PROTOCOL_STATE {
// The machine is waiting for a status byte.
STATE_WAITING_FOR_STATUS = 0,
STATE_ERROR = 1,
// Note Off: waiting for key and velocity data bytes, respectively.
STATE_WAITING_CHAN_NOTE_OFF_KEY,
STATE_WAITING_CHAN_NOTE_OFF_VELOCITY,
// Note On: waiting for key and velocity data bytes, respectively.
STATE_WAITING_CHAN_NOTE_ON_KEY,
STATE_WAITING_CHAN_NOTE_ON_VELOCITY,
// Poly After-touch: waiting for key and pressure value, respectively.
STATE_WAITING_CHAN_POLY_AFTERTOUCH_KEY,
STATE_WAITING_CHAN_POLY_AFTERTOUCH_PRESSURE,
// Channel control change: waiting for control and value data bytes.
// Note: depending on these values, messages of this type may be
// considered to be "channel mode" messages.
STATE_WAITING_CHAN_CONTROL_CHANGE_CONTROL,
STATE_WAITING_CHAN_CONTROL_CHANGE_VALUE,
// Program change: waiting for the program data byte.
STATE_WAITING_CHAN_PROGRAM_CHANGE_PROGRAM,
// Channel After-touch - waiting for the pressure data byte.
STATE_WAITING_CHAN_AFTERTOUCH_PRESSURE,
// Pitch Bend: waiting for two bytes representing the bend amount.
// The least-significant 7 bits are sent first. The most significant
// bits are set second.
STATE_WAITING_CHAN_PITCH_BEND_LSBITS,
STATE_WAITING_CHAN_PITCH_BEND_MSBITS
};
/**
* Globals that hold current MIDI state. For now, these are globals. If
* there is a desire to allow the library to manage more than one MIDI
* interface, they could be placed into a structure; other routines that
* access the globals would have to be updated accordingly.
*
* A future library enhancement may remove the single interface
* restriction.
*/
// State of the "MIDI protocol state machine"
static char g_state = STATE_WAITING_FOR_STATUS;
// Last bytes received, saved for debugging.
static char g_debug_last_status_byte = 0;
static char g_debug_last_data_byte = 0;
// The following three variables are updated during message parsing.
static char g_current_channel = 0;
static char g_data_byte_one = 0;
static char g_data_byte_two = 0;
// Counter that records number of complete MIDI messages received.
static unsigned long g_message_counter = 0;
// Callback table.
static midi_event_callback_t g_callbacks[EVT_MAX] = {0};
// The null event callback is used by default for all events.
static void null_event_cb(char channel, char a, char b) {
// Just implement the event counter.
++g_message_counter;
}
// Wrapper that invokes callback functions.
static inline void invoke_callback(int evt) {
// Reject invalid events.
if ((evt < 0) || (evt >= EVT_MAX)) {
return;
}
// Increment the global event counter.
++g_message_counter;
// Invoke the callback.
(g_callbacks[evt])(g_current_channel, g_data_byte_one, g_data_byte_two);
// Clear data state
g_data_byte_one = 0;
g_data_byte_two = 0;
}
/****************************************************************************
* Internal APIs *
****************************************************************************/
/**
* Process a "system real-time" message, which has no data bytes.
*
* Real-time status bytes may arrive at any time. In fact, according to the
* MIDI specification, real-time status bytes can be interleaved with bytes
* that comprise other, lower priority messages.
*
* For this reason, the rx_status_sys_realtime_byte() handler does NOT update
* much state; messages of this type should conceptually just be handled
* and then processing of the MIDI byte stream should continue as though the
* real-time byte was never received.
*/
static status_t rx_status_sys_realtime_byte(char byte) {
switch (byte) {
case SYS_REALTIME_TIMING_CLOCK:
invoke_callback(EVT_SYS_REALTIME_TIMING_CLOCK);
break;
case SYS_REALTIME_RESERVED_F9:
invoke_callback(EVT_SYS_REALTIME_RESERVED_F9);
break;
case SYS_REALTIME_SEQ_START:
invoke_callback(EVT_SYS_REALTIME_SEQ_START);
break;
case SYS_REALTIME_SEQ_CONTINUE:
invoke_callback(EVT_SYS_REALTIME_SEQ_CONTINUE);
break;
case SYS_REALTIME_SEQ_STOP:
invoke_callback(EVT_SYS_REALTIME_SEQ_STOP);
break;
case SYS_REALTIME_RESERVED_FD:
invoke_callback(EVT_SYS_REALTIME_RESERVED_FD);
break;
case SYS_REALTIME_ACTIVE_SENSE:
invoke_callback(EVT_SYS_REALTIME_ACTIVE_SENSE);
break;
case SYS_REALTIME_RESET:
invoke_callback(EVT_SYS_REALTIME_RESET);
break;
}
return 1;
}
// Process a "system common" status byte (0 or more data bytes follow.)
static status_t rx_status_sys_common_byte(char byte) {
// TODO(tdial): Implement
return 0;
}
// Process a "channel" status byte. (1 or 2 data bytes follow.)
static status_t rx_status_channel_byte(char byte) {
// Mask of the channel bits, leaving only the message type.
const char type = (byte & CHAN_TYPE_MASK);
// Update the state machine with the MIDI channel of the message that
// we are now processing. This is held in a global.
g_current_channel = (byte & CHAN_MASK);
switch (type) {
case CHAN_NOTE_OFF:
g_state = STATE_WAITING_CHAN_NOTE_OFF_KEY;
break;
case CHAN_NOTE_ON:
g_state = STATE_WAITING_CHAN_NOTE_ON_KEY;
break;
case CHAN_POLY_AFTER_TOUCH:
g_state = STATE_WAITING_CHAN_POLY_AFTERTOUCH_KEY;
break;
case CHAN_CONTROL_CHANGE:
g_state = STATE_WAITING_CHAN_CONTROL_CHANGE_CONTROL;
break;
case CHAN_PROGRAM_CHANGE:
g_state = STATE_WAITING_CHAN_PROGRAM_CHANGE_PROGRAM;
break;
case CHAN_AFTER_TOUCH:
g_state = STATE_WAITING_CHAN_AFTERTOUCH_PRESSURE;
break;
case CHAN_PITCH_BEND:
g_state = STATE_WAITING_CHAN_PITCH_BEND_LSBITS;
break;
default:
g_state = STATE_ERROR;
return E_MIDI_BAD_CHANNEL_STATE;
}
return 0;
}
// Process a trailing data byte.
static status_t rx_data_byte(char byte) {
switch (g_state) {
// Process first byte of a "note off" message.
case STATE_WAITING_CHAN_NOTE_OFF_KEY:
g_data_byte_one = byte;
g_state = STATE_WAITING_CHAN_NOTE_OFF_VELOCITY;
break;
// Process second byte of a "note off" message, and invoke callback.
// We reset the state in case there is a "running state" note off.
case STATE_WAITING_CHAN_NOTE_OFF_VELOCITY:
g_data_byte_two = byte;
invoke_callback(EVT_CHAN_NOTE_OFF);
g_state = STATE_WAITING_CHAN_NOTE_OFF_KEY;
return 1;
// Process first byte of a "note on" message.
case STATE_WAITING_CHAN_NOTE_ON_KEY:
g_data_byte_one = byte;
g_state = STATE_WAITING_CHAN_NOTE_ON_VELOCITY;
break;
// Process second byte of a "note on" message, and invoke callback.
case STATE_WAITING_CHAN_NOTE_ON_VELOCITY:
g_data_byte_two = byte;
invoke_callback(EVT_CHAN_NOTE_ON);
g_state = STATE_WAITING_CHAN_NOTE_ON_KEY;
return 1;
// Process first byte of a poly after-touch message.
case STATE_WAITING_CHAN_POLY_AFTERTOUCH_KEY:
g_data_byte_one = byte;
g_state = STATE_WAITING_CHAN_POLY_AFTERTOUCH_PRESSURE;
break;
// Process second byte of a poly after-touch message, invoke callback.
case STATE_WAITING_CHAN_POLY_AFTERTOUCH_PRESSURE:
g_data_byte_two = byte;
invoke_callback(EVT_CHAN_POLY_AFTERTOUCH);
g_state = STATE_WAITING_CHAN_POLY_AFTERTOUCH_KEY;
return 1;
// Process first byte of a channel control change message.
case STATE_WAITING_CHAN_CONTROL_CHANGE_CONTROL:
g_data_byte_one = byte;
g_state = STATE_WAITING_CHAN_CONTROL_CHANGE_VALUE;
break;
// Process second byte of a channel control change, invoke callback.
case STATE_WAITING_CHAN_CONTROL_CHANGE_VALUE:
g_data_byte_two = byte;
invoke_callback(EVT_CHAN_CONTROL_CHANGE);
g_state = STATE_WAITING_CHAN_CONTROL_CHANGE_CONTROL;
return 1;
// Process program change, invoke callback.
case STATE_WAITING_CHAN_PROGRAM_CHANGE_PROGRAM:
g_data_byte_one = byte;
g_data_byte_two = 0;
invoke_callback(EVT_CHAN_PROGRAM_CHANGE);
// Leave state intact in case there is another via running status.
return 1;
// Process channel after-touch message, invoke callback.
case STATE_WAITING_CHAN_AFTERTOUCH_PRESSURE:
g_data_byte_one = byte;
g_data_byte_two = 0;
invoke_callback(EVT_CHAN_AFTERTOUCH);
// Leave state intact in case there is another via running status.
return 1;
// Process first byte of pitch bend.
case STATE_WAITING_CHAN_PITCH_BEND_LSBITS:
g_data_byte_one = byte;
g_state = STATE_WAITING_CHAN_PITCH_BEND_MSBITS;
break;
// Process second byte of pitch bend.
case STATE_WAITING_CHAN_PITCH_BEND_MSBITS:
g_data_byte_two = byte;
invoke_callback(EVT_CHAN_PITCH_BEND);
g_state = STATE_WAITING_CHAN_PITCH_BEND_LSBITS;
return 1;
// Handle bad state.
default:
g_data_byte_one = 0;
g_data_byte_two = 0;
// TODO(tdial): Do we have to touch the state?
break;
}
// No messages processed; return 0.
return 0;
}
/****************************************************************************
* Public APIs *
****************************************************************************/
status_t midi_init() {
// Initialize the callback table; all events to the null callback.
for (int i = 0; i < EVT_MAX; ++i) {
g_callbacks[i] = null_event_cb;
}
return 0;
}
status_t midi_register_event_handler(event_type evt, midi_event_callback_t cb) {
if (cb) {
g_callbacks[evt] = cb;
} else {
g_callbacks[evt] = null_event_cb;
}
return 0;
}
status_t midi_receive_byte(char byte) {
/*
* The statements below, which are performed in deliberate order, determine
* which type of byte has arrived on the input. First, we test the lead
* bits to see if they match the expected mask for a system real-time
* status byte. Next, we check for a system common status byte, and then
* for a channel status byte, which could either be a voice or mode type.
*
* If it is determined that the byte is not any type of status byte, then
* by process of elimination, it must be a data byte.
*/
if ((byte & SYS_REALTIME_MASK) == SYS_REALTIME_MASK) {
// The byte is a system real-time status byte.
g_debug_last_status_byte = byte;
return rx_status_sys_realtime_byte(byte);
} else if ((byte & SYS_COMMON_MASK) == SYS_COMMON_MASK) {
// The byte is a system common status byte.
g_debug_last_status_byte = byte;
return rx_status_sys_common_byte(byte);
} else if (byte & CHAN_STATUS_MASK) {
// The byte is a channel voice or channel mode status byte.
g_debug_last_status_byte = byte;
return rx_status_channel_byte(byte);
} else {
// The byte is a regular data byte.
g_debug_last_data_byte = byte;
return rx_data_byte(byte);
}
}