-
Notifications
You must be signed in to change notification settings - Fork 1
/
Midi.cpp
523 lines (416 loc) · 15.1 KB
/
Midi.cpp
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/* Midi.cpp: Code for MIDI processing library, Arduino version
*
* (c) 2003-2011 Tymm Twillman <tymmothy@gmail.com>
*
* This file is part of Tymm's Arduino Midi Library.
*
* Tymm's Arduino Midi Library is free software: you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 2.1
* of the License, or (at your option) any later version.
*
* Tymm's Arduino Midi Library 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Tymm's Arduino Midi Library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include "HardwareSerial.h"
#include "Midi.h"
// This is used for tracking when we're processing a proprietary stream of data
// The assigned value is arbitrary; just for internal use.
static const int MODE_PROPRIETARY = 0xff;
// These are midi status message types as sent on the wire
static const int STATUS_EVENT_NOTE_OFF = 0x80;
static const int STATUS_EVENT_NOTE_ON = 0x90;
static const int STATUS_EVENT_VELOCITY_CHANGE = 0xA0;
static const int STATUS_EVENT_CONTROL_CHANGE = 0xB0;
static const int STATUS_EVENT_PROGRAM_CHANGE = 0xC0;
static const int STATUS_AFTER_TOUCH = 0xD0;
static const int STATUS_PITCH_CHANGE = 0xE0;
static const int STATUS_START_PROPRIETARY = 0xF0;
static const int STATUS_SONG_POSITION = 0xF2;
static const int STATUS_SONG_SELECT = 0xF3;
static const int STATUS_TUNE_REQUEST = 0xF6;
static const int STATUS_END_PROPRIETARY = 0xF7;
static const int STATUS_SYNC = 0xF8;
static const int STATUS_START = 0xFA;
static const int STATUS_CONTINUE = 0xFB;
static const int STATUS_STOP = 0xFC;
static const int STATUS_ACTIVE_SENSE = 0xFE;
static const int STATUS_RESET = 0xFF;
/******************************************************************************
*
* These are the hardware interface bits. To use with different kinds of
* hardware, or for Arduino core changes, these should be the bits that
* need to be tweaked.
*
*****************************************************************************/
// Constructor -- set up defaults for variables, get ready for use (but don't
// take over serial port yet)
Midi::Midi(HardwareSerial &serial) : serial_(serial)
{
init();
}
// Open the serial port and begin processing.
void Midi::begin(unsigned int channel, unsigned long baud)
{
channelIn_ = channel;
serial_.begin(baud);
}
// Try to read data at serial port & pass anything read to processing function
void Midi::poll(void)
{
int c;
// Just keep sucking data from serial port until it runs out, processing
// MIDI messages as we go
while((c = serial_.read()) != -1) {
recvByte(c);
}
}
// Function to actually send a byte of data over MIDI (separated out to
// make easy interfacing with different hardware easy)
void Midi::sendByte(unsigned char value)
{
serial_.write(value);
}
/******************************************************************************
*
* These are the general MIDI message handling functions; they handle
* parsing and constructing MIDI messages. You probably won't need to
* make any changes to these.
*
*****************************************************************************/
// Handle decoding incoming MIDI traffic a byte at a time -- remembers
// what it needs to from one call to the next.
//
// This is a private function & not meant to be called from outside this class.
// It's used whenever data is available from the serial port.
//
void Midi::recvByte(int value)
{
int tmp;
int channel;
int bigval; /* temp 14-bit value for pitch, song pos */
if (recvMode_ & MODE_PROPRIETARY
&& value != STATUS_END_PROPRIETARY)
{
/* If proprietary handling compiled in, just pass all data received
* after a START_PROPRIETARY event to proprietary_decode
* until get an END_PROPRIETARY event
*/
#ifdef CONFIG_MIDI_PROPRIETARY
proprietaryDecode(value);
#endif
return;
}
if (value & 0x80) {
/* All < 0xf0 events get at least 1 arg byte so
* it's ok to mask off the low 4 bits to figure
* out how to handle the event for < 0xf0 events.
*/
tmp = value;
if (tmp < 0xf0)
tmp &= 0xf0;
switch (tmp) {
/* These status events take 2 bytes as arguments */
case STATUS_EVENT_NOTE_OFF:
case STATUS_EVENT_NOTE_ON:
case STATUS_EVENT_VELOCITY_CHANGE:
case STATUS_EVENT_CONTROL_CHANGE:
case STATUS_PITCH_CHANGE:
case STATUS_SONG_POSITION:
recvBytesNeeded_ = 2;
recvByteCount_ = 0;
recvEvent_ = value;
break;
/* 1 byte arguments */
case STATUS_EVENT_PROGRAM_CHANGE:
case STATUS_AFTER_TOUCH:
case STATUS_SONG_SELECT:
recvBytesNeeded_ = 1;
recvByteCount_ = 0;
recvEvent_ = value;
return;
/* No arguments ( > 0xf0 events) */
case STATUS_START_PROPRIETARY:
recvMode_ |= MODE_PROPRIETARY;
#ifdef CONFIG_MIDI_PROPRIETARY
proprietaryDecodeStart();
#endif
break;
case STATUS_END_PROPRIETARY:
recvMode_ &= ~MODE_PROPRIETARY;
#ifdef CONFIG_MIDI_PROPRIETARY
proprietaryDecodeEnd();
#endif
break;
case STATUS_TUNE_REQUEST:
handleTuneRequest();
break;
case STATUS_SYNC:
handleSync();
break;
case STATUS_START:
handleStart();
break;
case STATUS_CONTINUE:
handleContinue();
break;
case STATUS_STOP:
handleStop();
break;
case STATUS_ACTIVE_SENSE:
handleActiveSense();
break;
case STATUS_RESET:
handleReset();
break;
}
return;
}
if (++recvByteCount_ == recvBytesNeeded_) {
/* Copy out the channel (if applicable; in some cases this will be meaningless,
* but in those cases the value will be ignored)
*/
channel = (recvEvent_ & 0x0f) + 1;
tmp = recvEvent_;
if (tmp < 0xf0) {
tmp &= 0xf0;
}
/* See if this event matches our MIDI channel
* (or we're accepting for all channels)
*/
if (!channelIn_
|| (channel == channelIn_)
|| (tmp >= 0xf0))
{
switch (tmp) {
case STATUS_EVENT_NOTE_ON:
/* If velocity is 0, it's actually a note off & should fall thru
* to the note off case
*/
if (value) {
handleNoteOn(channel, recvArg0_, value);
break;
}
case STATUS_EVENT_NOTE_OFF:
handleNoteOff(channel, recvArg0_, value);
break;
case STATUS_EVENT_VELOCITY_CHANGE:
handleVelocityChange(channel, recvArg0_, value);
break;
case STATUS_EVENT_CONTROL_CHANGE:
handleControlChange(channel, recvArg0_, value);
break;
case STATUS_EVENT_PROGRAM_CHANGE:
handleProgramChange(channel, value);
break;
case STATUS_AFTER_TOUCH:
handleAfterTouch(channel, value);
break;
case STATUS_PITCH_CHANGE:
bigval = (value << 7) | recvArg0_;
handlePitchChange(bigval);
break;
case STATUS_SONG_POSITION:
bigval = (value << 7) | recvArg0_;
handleSongPosition(bigval);
break;
case STATUS_SONG_SELECT:
handleSongSelect(value);
break;
}
}
/* Just reset the byte count; keep the same event -- might get more messages
trailing from current event.
*/
recvByteCount_ = 0;
}
recvArg0_ = value;
}
// Send Midi NOTE OFF message to a given channel, with note 0-127 and velocity 0-127
void Midi::sendNoteOff(unsigned int channel, unsigned int note, unsigned int velocity)
{
int status = STATUS_EVENT_NOTE_OFF | ((channel - 1) & 0x0f);
if (sendFullCommands_ || (lastStatusSent_ != status)) {
sendByte(status);
}
sendByte(note & 0x7f);
sendByte(velocity & 0x7f);
}
// Send Midi NOTE ON message to a given channel, with note 0-127 and velocity 0-127
void Midi::sendNoteOn(unsigned int channel, unsigned int note, unsigned int velocity)
{
int status = STATUS_EVENT_NOTE_ON | ((channel - 1) & 0x0f);
if (sendFullCommands_ || (lastStatusSent_ != status)) {
sendByte(status);
}
sendByte(note & 0x7f);
sendByte(velocity & 0x7f);
}
// Send a Midi VELOCITY CHANGE message to a given channel, with given note 0-127,
// and new velocity 0-127
void Midi::sendVelocityChange(unsigned int channel, unsigned int note, unsigned int velocity)
{
int status = STATUS_EVENT_VELOCITY_CHANGE | ((channel - 1) & 0x0f);
if (sendFullCommands_ || (lastStatusSent_ != status)) {
sendByte(status);
}
sendByte(note & 0x7f);
sendByte(velocity & 0x7f);
}
// Send a Midi CC message to a given channel, as a given controller 0-127, with given
// value 0-127
void Midi::sendControlChange(unsigned int channel, unsigned int controller, unsigned int value)
{
int status = STATUS_EVENT_CONTROL_CHANGE | ((channel - 1) & 0x0f);
if (sendFullCommands_ || (lastStatusSent_ != status)) {
sendByte(status);
}
sendByte(controller & 0x7f);
sendByte(value & 0x7f);
}
// Send a Midi PROGRAM CHANGE message to given channel, with program ID 0-127
void Midi::sendProgramChange(unsigned int channel, unsigned int program)
{
int status = STATUS_EVENT_PROGRAM_CHANGE | ((channel - 1) & 0x0f);
if (sendFullCommands_ || (lastStatusSent_ != status)) {
sendByte(status);
}
sendByte(program & 0x7f);
}
// Send a Midi AFTER TOUCH message to given channel, with velocity 0-127
void Midi::sendAfterTouch(unsigned int channel, unsigned int velocity)
{
int status = STATUS_AFTER_TOUCH | ((channel - 1) & 0x0f);
if (sendFullCommands_ || (lastStatusSent_ != status)) {
sendByte(status);
}
sendByte(velocity & 0x7f);
}
// Send a Midi PITCH CHANGE message, with a 14-bit pitch (always for all channels)
void Midi::sendPitchChange(unsigned int pitch)
{
sendByte(STATUS_PITCH_CHANGE);
sendByte(pitch & 0x7f);
sendByte((pitch >> 7) & 0x7f);
}
// Send a Midi SONG POSITION message, with a 14-bit position (always for all channels)
void Midi::sendSongPosition(unsigned int position)
{
sendByte(STATUS_SONG_POSITION);
sendByte(position & 0x7f);
sendByte((position >> 7) & 0x7f);
}
// Send a Midi SONG SELECT message, with a song ID of 0-127 (always for all channels)
void Midi::sendSongSelect(unsigned int song)
{
sendByte(STATUS_SONG_SELECT);
sendByte(song & 0x7f);
}
// Send a Midi TUNE REQUEST message (TUNE REQUEST is always for all channels)
void Midi::sendTuneRequest(void)
{
sendByte(STATUS_TUNE_REQUEST);
}
// Send a Midi SYNC message (SYNC is always for all channels)
void Midi::sendSync(void)
{
sendByte(STATUS_SYNC);
}
// Send a Midi START message (START is always for all channels)
void Midi::sendStart(void)
{
sendByte(STATUS_START);
}
// Send a Midi CONTINUE message (CONTINUE is always for all channels)
void Midi::sendContinue(void)
{
sendByte(STATUS_CONTINUE);
}
// Send a Midi STOP message (STOP is always for all channels)
void Midi::sendStop(void)
{
sendByte(STATUS_STOP);
}
// Send a Midi ACTIVE SENSE message (ACTIVE SENSE is always for all channels)
void Midi::sendActiveSense(void)
{
sendByte(STATUS_ACTIVE_SENSE);
}
// Send a Midi RESET message (RESET is always for all channels)
void Midi::sendReset(void)
{
sendByte(STATUS_RESET);
}
void Midi::init()
{
/* Not in proprietary stream */
recvMode_ = 0;
/* No bytes recevied */
recvByteCount_ = 0;
/* Not processing an event */
recvEvent_ = 0;
/* No arguments to the event we haven't received */
recvArg0_ = 0;
/* Not waiting for bytes to complete a message */
recvBytesNeeded_ = 0;
// There was no last event.
lastStatusSent_ = false;
// Don't send the extra bytes; just send deltas
sendFullCommands_ = false;
/* Listening to all channels */
channelIn_ = 0;
}
// Set (package-specific) parameters for the Midi instance
void Midi::setParam(unsigned int param, unsigned int val)
{
if (param == PARAM_SEND_FULL_COMMANDS) {
if (val) {
sendFullCommands_ = true;
} else {
sendFullCommands_ = false;
}
} else if (param == PARAM_CHANNEL_IN) {
channelIn_ = val;
}
}
// Get (package-specific) parameters for the Midi instance
unsigned int Midi::getParam(unsigned int param)
{
if (param == PARAM_SEND_FULL_COMMANDS) {
return sendFullCommands_;
} else if (param == PARAM_CHANNEL_IN) {
return channelIn_;
}
return 0;
}
/******************************************************************************
*
* These are placeholder functions for the different possible MIDI event
* handlers.
*
* You should subclass the Midi base class and define these to have your
* version called, for any events that you want to receive.
*
*****************************************************************************/
void Midi::handleNoteOff(unsigned int channel, unsigned int note, unsigned int velocity) {}
void Midi::handleNoteOn(unsigned int channel, unsigned int note, unsigned int velocity) {}
void Midi::handleVelocityChange(unsigned int channel, unsigned int note, unsigned int velocity) {}
void Midi::handleControlChange(unsigned int channel, unsigned int controller, unsigned int value) {}
void Midi::handleProgramChange(unsigned int channel, unsigned int program) {}
void Midi::handleAfterTouch(unsigned int channel, unsigned int velocity) {}
void Midi::handlePitchChange(unsigned int pitch) {}
void Midi::handleSongPosition(unsigned int position) {}
void Midi::handleSongSelect(unsigned int song) {}
void Midi::handleTuneRequest(void) {}
void Midi::handleSync(void) {}
void Midi::handleStart(void) {}
void Midi::handleContinue(void) {}
void Midi::handleStop(void) {}
void Midi::handleActiveSense(void) {}
void Midi::handleReset(void) {}