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
524
|
/******************************************************************************
* The MIT License
*
* Copyright (c) 2010 Perry Hung.
* Copyright (c) 2013 Magnus Lundin.
* Copyright (c) 2013 Donald Delmar Davis, Suspect Devices.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This is a munging of Tymm's arduino Midi Library into the Libusb class.
* I am not sure this is the right way to go especially since its licensed differently.
*
* Midi.cpp: Code for MIDI processing library, Arduino version
*
* (c) 2003-2008 Tymm Twillman <tymm@booyaka.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/>.
*
*
*****************************************************************************/
/**
* @brief USB MIDI device with a class compatible with maplemidi
*/
#include <wirish/usb_midi.h>
#include <string.h>
#include <stdint.h>
#include <libmaple/nvic.h>
#include <libmaple/usb_midi_device.h>
#include <libmaple/usb.h>
#include <wirish/wirish.h>
/*
* USBMidi interface
*/
#define USB_TIMEOUT 50
USBMidi::USBMidi(void) {
#if !BOARD_HAVE_MIDIUSB
ASSERT(0);
#endif
}
// Constructor -- set up defaults for variables, get ready for use (but don't
// take over serial port yet)
void USBMidi::begin(unsigned int channel) {
#if BOARD_HAVE_MIDIUSB
usb_midi_enable(BOARD_USB_DISC_DEV, BOARD_USB_DISC_BIT);
/* 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;
#endif
}
void USBMidi::end(void) {
#if BOARD_HAVE_MIDIUSB
usb_midi_disable(BOARD_USB_DISC_DEV, BOARD_USB_DISC_BIT);
#endif
}
void USBMidi::writePacket(uint32 p) {
this->writePackets(&p, 1);
}
void USBMidi::writePackets(const void *buf, uint32 len) {
if (!this->isConnected() || !buf) {
return;
}
uint32 txed = 0;
uint32 old_txed = 0;
uint32 start = millis();
uint32 sent = 0;
while (txed < len && (millis() - start < USB_TIMEOUT)) {
sent = usb_midi_tx((const uint32*)buf + txed, len - txed);
txed += sent;
if (old_txed != txed) {
start = millis();
}
old_txed = txed;
}
if (sent == USB_MIDI_TX_EPSIZE) {
while (usb_midi_is_transmitting() != 0) {
}
/* flush out to avoid having the pc wait for more data */
usb_midi_tx(NULL, 0);
}
}
uint32 USBMidi::available(void) {
return usb_midi_data_available();
}
uint32 USBMidi::readPackets(void *buf, uint32 len) {
if (!buf) {
return 0;
}
uint32 rxed = 0;
while (rxed < len) {
rxed += usb_midi_rx((uint32*)buf + rxed, len - rxed);
}
return rxed;
}
/* Blocks forever until 1 byte is received */
uint32 USBMidi::readPacket(void) {
uint32 p;
this->readPackets(&p, 1);
return p;
}
uint8 USBMidi::pending(void) {
return usb_midi_get_pending();
}
uint8 USBMidi::isConnected(void) {
return usb_is_connected(USBLIB) && usb_is_configured(USBLIB);
}
#if BOARD_HAVE_MIDIUSB
USBMidi MidiUSB;
#endif
// 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 are defined in MidiSpec.h
union EVENT_t {
uint32 i;
uint8 b[4];
MIDI_EVENT_PACKET_t p;
};
// 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 USBMidi::dispatchPacket(uint32 p)
{
union EVENT_t e;
e.i=p;
// !!!!!!!!!!!!!!!! Add a sysex handler FIX THIS VERY VERY SHORTLY !!!!!!!!!!!!!!
if (recvMode_ & MODE_PROPRIETARY
&& CIN_IS_SYSEX(e.p.cin))
{
/* If sysex handling compiled in, just pass all data received
* to the sysex handler
*/
#ifdef CONFIG_MIDI_PROPRIETARY
// handleSysex(p);
#endif
return;
}
switch (e.p.cin) {
case CIN_3BYTE_SYS_COMMON:
if (e.p.midi0 == MIDIv1_SONG_POSITION_PTR) {
handleSongPosition(((uint16)e.p.midi2)<<7|((uint16)e.p.midi1));
}
break;
case CIN_2BYTE_SYS_COMMON:
switch (e.p.midi0) {
case MIDIv1_SONG_SELECT:
handleSongSelect(e.p.midi1);
break;
case MIDIv1_MTC_QUARTER_FRAME:
// reference library doesnt handle quarter frame.
break;
}
break;
case CIN_NOTE_OFF:
handleNoteOff(MIDIv1_VOICE_CHANNEL(e.p.midi0), e.p.midi1, e.p.midi2);
break;
case CIN_NOTE_ON:
handleNoteOn(MIDIv1_VOICE_CHANNEL(e.p.midi0), e.p.midi1, e.p.midi2);
break;
case CIN_AFTER_TOUCH:
handleVelocityChange(MIDIv1_VOICE_CHANNEL(e.p.midi0), e.p.midi1, e.p.midi2);
break;
case CIN_CONTROL_CHANGE:
handleControlChange(MIDIv1_VOICE_CHANNEL(e.p.midi0), e.p.midi1, e.p.midi2);
break;
case CIN_PROGRAM_CHANGE:
handleProgramChange(MIDIv1_VOICE_CHANNEL(e.p.midi0), e.p.midi1);
break;
case CIN_CHANNEL_PRESSURE:
handleAfterTouch(MIDIv1_VOICE_CHANNEL(e.p.midi0), e.p.midi1);
break;
case CIN_PITCH_WHEEL:
handlePitchChange(((uint16)e.p.midi2)<<7|((uint16)e.p.midi1));
break;
case CIN_1BYTE:
switch (e.p.midi0) {
case MIDIv1_CLOCK:
handleSync();
break;
case MIDIv1_TICK:
break;
case MIDIv1_START:
handleStart();
break;
case MIDIv1_CONTINUE:
handleContinue();
break;
case MIDIv1_STOP:
handleStop();
break;
case MIDIv1_ACTIVE_SENSE:
handleActiveSense();
break;
case MIDIv1_RESET:
handleReset();
break;
case MIDIv1_TUNE_REQUEST:
handleTuneRequest();
break;
default:
break;
}
break;
}
}
// Try to read data at serial port & pass anything read to processing function
void USBMidi::poll(void)
{ while(available()) {
dispatchPacket(readPacket());
}
}
static union EVENT_t outPacket; // since we only use one at a time no point in reallocating it
// Send Midi NOTE OFF message to a given channel, with note 0-127 and velocity 0-127
void USBMidi::sendNoteOff(unsigned int channel, unsigned int note, unsigned int velocity)
{
outPacket.p.cable=DEFAULT_MIDI_CABLE;
outPacket.p.cin=CIN_NOTE_OFF;
outPacket.p.midi0=MIDIv1_NOTE_OFF|(channel & 0x0f);
outPacket.p.midi1=note;
outPacket.p.midi2=velocity;
writePacket(outPacket.i);
}
// Send Midi NOTE ON message to a given channel, with note 0-127 and velocity 0-127
void USBMidi::sendNoteOn(unsigned int channel, unsigned int note, unsigned int velocity)
{
outPacket.p.cable=DEFAULT_MIDI_CABLE;
outPacket.p.cin=CIN_NOTE_ON;
outPacket.p.midi0=MIDIv1_NOTE_ON|(channel & 0x0f);
outPacket.p.midi1=note;
outPacket.p.midi2=velocity;
writePacket(outPacket.i);
}
// Send a Midi VELOCITY CHANGE message to a given channel, with given note 0-127,
// and new velocity 0-127
// Note velocity change == polyphonic aftertouch.
// Note aftertouch == channel pressure.
void USBMidi::sendVelocityChange(unsigned int channel, unsigned int note, unsigned int velocity)
{
outPacket.p.cable=DEFAULT_MIDI_CABLE;
outPacket.p.cin=CIN_AFTER_TOUCH;
outPacket.p.midi0=MIDIv1_AFTER_TOUCH |(channel & 0x0f);
outPacket.p.midi1=note;
outPacket.p.midi2=velocity;
writePacket(outPacket.i);
}
// Send a Midi CC message to a given channel, as a given controller 0-127, with given
// value 0-127
void USBMidi::sendControlChange(unsigned int channel, unsigned int controller, unsigned int value)
{
outPacket.p.cable=DEFAULT_MIDI_CABLE;
outPacket.p.cin=CIN_CONTROL_CHANGE;
outPacket.p.midi0=MIDIv1_CONTROL_CHANGE |(channel & 0x0f);
outPacket.p.midi1=controller;
outPacket.p.midi2=value;
writePacket(outPacket.i);
}
// Send a Midi PROGRAM CHANGE message to given channel, with program ID 0-127
void USBMidi::sendProgramChange(unsigned int channel, unsigned int program)
{
outPacket.p.cable=DEFAULT_MIDI_CABLE;
outPacket.p.cin=CIN_PROGRAM_CHANGE;
outPacket.p.midi0=MIDIv1_PROGRAM_CHANGE |(channel & 0x0f);
outPacket.p.midi1=program;
writePacket(outPacket.i);
}
// Send a Midi AFTER TOUCH message to given channel, with velocity 0-127
void USBMidi::sendAfterTouch(unsigned int channel, unsigned int velocity)
{
outPacket.p.cable=DEFAULT_MIDI_CABLE;
outPacket.p.cin=CIN_CHANNEL_PRESSURE;
outPacket.p.midi0=MIDIv1_CHANNEL_PRESSURE |(channel & 0x0f);
outPacket.p.midi1=velocity;
writePacket(outPacket.i);
}
// Send a Midi PITCH CHANGE message, with a 14-bit pitch (always for all channels)
void USBMidi::sendPitchChange(unsigned int pitch)
{
outPacket.p.cable=DEFAULT_MIDI_CABLE;
outPacket.p.cin=CIN_PITCH_WHEEL;
outPacket.p.midi0=MIDIv1_PITCH_WHEEL;
outPacket.p.midi1= (uint8) pitch & 0x07F;
outPacket.p.midi2= (uint8) (pitch<<7) & 0x7f;
writePacket(outPacket.i);
}
// Send a Midi SONG POSITION message, with a 14-bit position (always for all channels)
void USBMidi::sendSongPosition(unsigned int position)
{
outPacket.p.cable=DEFAULT_MIDI_CABLE;
outPacket.p.cin=CIN_3BYTE_SYS_COMMON;
outPacket.p.midi0=MIDIv1_SONG_POSITION_PTR;
outPacket.p.midi1= (uint8) position & 0x07F;
outPacket.p.midi2= (uint8) (position<<7) & 0x7f;
writePacket(outPacket.i);
}
// Send a Midi SONG SELECT message, with a song ID of 0-127 (always for all channels)
void USBMidi::sendSongSelect(unsigned int song)
{
outPacket.p.cable=DEFAULT_MIDI_CABLE;
outPacket.p.cin=CIN_2BYTE_SYS_COMMON;
outPacket.p.midi0=MIDIv1_SONG_SELECT;
outPacket.p.midi1= (uint8) song & 0x07F;
writePacket(outPacket.i);
}
// Send a Midi TUNE REQUEST message (TUNE REQUEST is always for all channels)
void USBMidi::sendTuneRequest(void)
{
outPacket.p.cable=DEFAULT_MIDI_CABLE;
outPacket.p.cin=CIN_1BYTE;
outPacket.p.midi0=MIDIv1_TUNE_REQUEST;
writePacket(outPacket.i);
}
// Send a Midi SYNC message (SYNC is always for all channels)
void USBMidi::sendSync(void)
{
outPacket.p.cable=DEFAULT_MIDI_CABLE;
outPacket.p.cin=CIN_1BYTE;
outPacket.p.midi0=MIDIv1_CLOCK;
writePacket(outPacket.i);
}
// Send a Midi START message (START is always for all channels)
void USBMidi::sendStart(void)
{
outPacket.p.cable=DEFAULT_MIDI_CABLE;
outPacket.p.cin=CIN_1BYTE;
outPacket.p.midi0=MIDIv1_START ;
writePacket(outPacket.i);
}
// Send a Midi CONTINUE message (CONTINUE is always for all channels)
void USBMidi::sendContinue(void)
{
outPacket.p.cable=DEFAULT_MIDI_CABLE;
outPacket.p.cin=CIN_1BYTE;
outPacket.p.midi0=MIDIv1_CONTINUE ;
writePacket(outPacket.i);
}
// Send a Midi STOP message (STOP is always for all channels)
void USBMidi::sendStop(void)
{
outPacket.p.cable=DEFAULT_MIDI_CABLE;
outPacket.p.cin=CIN_1BYTE;
outPacket.p.midi0=MIDIv1_STOP ;
writePacket(outPacket.i);
}
// Send a Midi ACTIVE SENSE message (ACTIVE SENSE is always for all channels)
void USBMidi::sendActiveSense(void)
{
outPacket.p.cable=DEFAULT_MIDI_CABLE;
outPacket.p.cin=CIN_1BYTE;
outPacket.p.midi0=MIDIv1_ACTIVE_SENSE ;
writePacket(outPacket.i);
}
// Send a Midi RESET message (RESET is always for all channels)
void USBMidi::sendReset(void)
{
outPacket.p.cable=DEFAULT_MIDI_CABLE;
outPacket.p.cin=CIN_1BYTE;
outPacket.p.midi0=MIDIv1_RESET ;
writePacket(outPacket.i);
}
// Set (package-specific) parameters for the Midi instance
void USBMidi::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 USBMidi::getParam(unsigned int param)
{
if (param == PARAM_SEND_FULL_COMMANDS) {
return sendFullCommands_;
} else if (param == PARAM_CHANNEL_IN) {
return channelIn_;
}
return 0;
}
// Placeholders. You should subclass the Midi base class and define these to have your
// version called.
void USBMidi::handleNoteOff(unsigned int channel, unsigned int note, unsigned int velocity) {}
void USBMidi::handleNoteOn(unsigned int channel, unsigned int note, unsigned int velocity) {}
void USBMidi::handleVelocityChange(unsigned int channel, unsigned int note, unsigned int velocity) {}
void USBMidi::handleControlChange(unsigned int channel, unsigned int controller, unsigned int value) {}
void USBMidi::handleProgramChange(unsigned int channel, unsigned int program) {}
void USBMidi::handleAfterTouch(unsigned int channel, unsigned int velocity) {}
void USBMidi::handlePitchChange(unsigned int pitch) {}
void USBMidi::handleSongPosition(unsigned int position) {}
void USBMidi::handleSongSelect(unsigned int song) {}
void USBMidi::handleTuneRequest(void) {}
void USBMidi::handleSync(void) {}
void USBMidi::handleStart(void) {}
void USBMidi::handleContinue(void) {}
void USBMidi::handleStop(void) {}
void USBMidi::handleActiveSense(void) {}
void USBMidi::handleReset(void) {}
|