/
MIDIListener.cpp
598 lines (523 loc) · 11.4 KB
/
MIDIListener.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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/*
Copyright (C) 2008 Gabor Papp
This program 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 3 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <iostream>
#include "MIDIListener.h"
using namespace std;
/** maximum number of midi notes stored */
static const unsigned MAX_MIDI_NOTE_COUNT = 256;
/** maximum number of midi events stored.. there could be *a lot* of events,
so this size is intensionally left small. **/
static const unsigned MAX_MIDI_EVENT_COUNT = 16;
MIDINote::MIDINote(int _on_off, int _channel, int _note, int _velocity) :
on_off(_on_off),
channel(_channel),
note(_note),
velocity(_velocity)
{
}
MIDIEvent::MIDIEvent(int _channel, int _controller, int _value) :
channel(_channel),
controller(_controller),
value(_value)
{
}
void midi_callback(double deltatime, vector<unsigned char> *message,
void *user_data)
{
static_cast<MIDIListener *>(user_data)->callback(deltatime, message);
}
MIDIListener::MIDIListener(int port /*= -1*/) :
midiin(NULL),
last_event(""),
cc_encoder_mode(MIDI_CC_ABSOLUTE)
{
init_midi();
if (port >= 0)
open(port);
/* allocate array for controller values and clear it */
cntrl_values = new signed char[MAX_CNTRL];
fill(cntrl_values, cntrl_values + MAX_CNTRL, 0);
/* likewise for the per channel "program" values */
pgm_values = new unsigned char[MAX_CHAN];
fill(pgm_values, pgm_values + MAX_CHAN, 0);
pthread_mutex_init(&mutex, NULL);
set_signature(4, 4);
}
MIDIListener::~MIDIListener()
{
if (midiin)
delete midiin;
delete [] cntrl_values;
deque<MIDINote *>::iterator i = midi_notes.begin();
for (; i < midi_notes.end(); i++)
{
delete *i;
}
midi_notes.clear();
pthread_mutex_destroy(&mutex);
}
void MIDIListener::init_midi(void)
{
if (midiin == NULL)
{
try
{
midiin = new RtMidiIn("FluxusMidi Input Client");
}
catch (RtError &error)
{
error.printMessage();
midiin = NULL;
}
/* ignore MIDI sysex, timing and active sensing messages */
midiin->ignoreTypes(true, true, true);
}
deque<MIDINote *>::iterator i = midi_notes.begin();
for (; i < midi_notes.end(); i++)
{
delete *i;
}
midi_notes.clear();
}
/**
* Returns MIDI input port information.
* \retval vector<string> string identifiers for the available MIDI
* input ports
**/
vector<string> MIDIListener::info(void)
{
port_names.clear();
if (midiin == NULL)
{
init_midi();
if (midiin == NULL)
return port_names;
}
int port_count = midiin->getPortCount();
std::string port_name;
for (int i = 0; i < port_count; i++)
{
try
{
port_name = midiin->getPortName(i);
port_names.push_back(port_name);
}
catch (RtError &error)
{
error.printMessage();
break;
}
}
return port_names;
}
/**
* Opens MIDI port.
* \param port port to open
**/
void MIDIListener::open(int port)
{
static bool callback_set = false;
if (midiin == NULL)
{
init_midi();
if (midiin == NULL)
return;
}
int port_count = midiin->getPortCount();
if (port >= port_count)
{
cerr << "midi listener: invalid port\n";
return;
}
try
{
midiin->openPort(port, "FluxusMidi Input");
}
catch (RtError &error)
{
error.printMessage();
return;
}
if (callback_set)
{
midiin->cancelCallback();
}
midiin->setCallback(&midi_callback, this);
callback_set = true;
}
/**
* Closes MIDI port.
**/
void MIDIListener::close()
{
if (midiin == NULL)
{
return;
}
try
{
midiin->closePort();
}
catch (RtError &error)
{
error.printMessage();
return;
}
}
/**
* Sets controller encoder mode
* \param mode MIDI_CC_ABSOLUTE, MIDI_CC_ABLETON or MIDI_CC_DOEPFER
**/
void MIDIListener::set_cc_encoder_mode(int mode)
{
cc_encoder_mode = mode;
}
/**
* Returns current controller encoder mode
* \retval mode MIDI_CC_ABSOLUTE, MIDI_CC_ABLETON or MIDI_CC_DOEPFER
**/
int MIDIListener::get_cc_encoder_mode(void)
{
return cc_encoder_mode;
}
/**
* Returns controller values.
* \param channel MIDI channel
* \param cntrl_number controller number
* \retval int controller value
**/
int MIDIListener::get_cc(int channel, int cntrl_number)
{
if (midiin == NULL)
{
init_midi();
if (midiin == NULL)
return 0;
}
int i = (channel << 7) + cntrl_number;
pthread_mutex_lock(&mutex);
int v = cntrl_values[i];
if (cc_encoder_mode != MIDI_CC_ABSOLUTE)
{
cntrl_values[i] = 0;
}
pthread_mutex_unlock(&mutex);
return v;
}
/**
* Returns program value.
* \param channel MIDI channel
* \retval int program value
**/
int MIDIListener::get_program(int channel)
{
if (midiin == NULL)
{
init_midi();
if (midiin == NULL)
return 0;
}
pthread_mutex_lock(&mutex);
int v = pgm_values[channel];
pthread_mutex_unlock(&mutex);
return v;
}
/**
* Returns normalised controller values.
* \param channel MIDI channel
* \param cntrl_number controller number
* \retval float controller value normalised to the [0, 1] interval
**/
float MIDIListener::get_ccn(int channel, int cntrl_number)
{
if (midiin == NULL)
{
init_midi();
if (midiin == NULL)
return 0;
}
int i = (channel << 7) + cntrl_number;
pthread_mutex_lock(&mutex);
float v = (float)cntrl_values[i] / 127.0;
if (cc_encoder_mode != MIDI_CC_ABSOLUTE)
{
cntrl_values[i] = 0;
}
pthread_mutex_unlock(&mutex);
return v;
}
/**
* Returns last MIDI event as string.
* \retval string MIDI event in string format
**/
string MIDIListener::get_last_event(void)
{
static string last_event_static = "";
pthread_mutex_lock(&mutex);
last_event_static = last_event;
pthread_mutex_unlock(&mutex);
return last_event_static;
}
/**
* Returns next MIDI note event from event queue.
* \retval MIDINote* pointer to MIDI note or NULL if the event queue is empty
**/
MIDINote *MIDIListener::get_note(void)
{
static MIDINote note;
pthread_mutex_lock(&mutex);
if (midi_notes.empty())
{
pthread_mutex_unlock(&mutex);
return NULL;
}
MIDINote *n = midi_notes.front();
midi_notes.pop_front();
pthread_mutex_unlock(&mutex);
note.on_off = n->on_off;
note.channel = n->channel;
note.note = n->note;
note.velocity = n->velocity;
delete n;
return ¬e;
}
/**
* Returns next MIDI controller event from event queue.
* \retval MIDIEvent* pointer to MIDI Event or NULL if the queue is empty
**/
MIDIEvent *MIDIListener::get_cc_event(void)
{
static MIDIEvent evt;
pthread_mutex_lock(&mutex);
if (midi_events.empty())
{
pthread_mutex_unlock(&mutex);
return NULL;
}
MIDIEvent *n = midi_events.front();
midi_events.pop_front();
pthread_mutex_unlock(&mutex);
evt.channel = n->channel;
evt.value = n->value;
evt.controller = n->controller;
delete n;
return &evt;
}
int MIDIListener::get_bar(void)
{
pthread_mutex_lock(&mutex);
int ret = bar;
pthread_mutex_unlock(&mutex);
return ret;
}
int MIDIListener::get_beat(void)
{
pthread_mutex_lock(&mutex);
int ret = beat;
pthread_mutex_unlock(&mutex);
return ret;
}
int MIDIListener::get_pulse(void)
{
pthread_mutex_lock(&mutex);
int ret = pulse;
pthread_mutex_unlock(&mutex);
return ret;
}
int MIDIListener::get_beats_per_bar()
{
pthread_mutex_lock(&mutex);
int ret = beats_per_bar;
pthread_mutex_unlock(&mutex);
return ret;
}
int MIDIListener::get_clocks_per_beat()
{
pthread_mutex_lock(&mutex);
int ret = clocks_per_beat;
pthread_mutex_unlock(&mutex);
return ret;
}
void MIDIListener::set_signature(int upper, int lower)
{
pthread_mutex_lock(&mutex);
beats_per_bar = upper;
clocks_per_beat = (24*4)/lower;
pthread_mutex_unlock(&mutex);
reset_song_position();
}
/**
* Adds a new event to the event queue.
**/
void MIDIListener::add_event(int channel, int controller, int value)
{
MIDIEvent *n = new MIDIEvent(channel,controller,value);
midi_events.push_back(n);
while (midi_events.size() > MAX_MIDI_EVENT_COUNT)
{
delete midi_events.front();
midi_events.pop_front();
}
}
void MIDIListener::reset_song_position()
{
pthread_mutex_lock(&mutex);
bar = 0;
beat = 0;
pulse = 0;
pthread_mutex_unlock(&mutex);
}
/**
* Adds a new note to the event queue.
**/
void MIDIListener::add_note(int on_off, int ch, int note, int velocity)
{
MIDINote *n = new MIDINote(on_off, ch, note, velocity);
midi_notes.push_back(n);
while (midi_notes.size() > MAX_MIDI_NOTE_COUNT)
{
delete midi_notes.front();
midi_notes.pop_front();
}
}
void MIDIListener::callback(double deltatime, vector<unsigned char> *message)
{
char buf[256];
unsigned int count = message->size();
int status = (*message)[0] >> 4;
int ch = (*message)[0] & 0xf;
switch (status)
{
case MIDIListener::MIDI_PROGRAM_CHANGE:
if (count == 2)
{
int program_number = (*message)[1];
pthread_mutex_lock(&mutex);
pgm_values[ch] = program_number;
pthread_mutex_unlock(&mutex);
}
break;
case MIDIListener::MIDI_CONTROLLER:
if (count == 3)
{
int cntrl_number; /* controller number */
if (cc_encoder_mode == MIDI_CC_DOEPFER)
cntrl_number = (*message)[2];
else
cntrl_number = (*message)[1];
/* array index from channel and controller number */
int i = (ch << 7) + cntrl_number;
int value; /* controller value */
if (cc_encoder_mode == MIDI_CC_DOEPFER)
{
value = (*message)[1] == 97 ? -1 : 1;
}
else
{
value = (*message)[2];
if ((cc_encoder_mode == MIDI_CC_ABLETON) && (value > 64))
{
value = 64 - value;
}
}
pthread_mutex_lock(&mutex);
cntrl_values[i] = value;
add_event(ch, cntrl_number, value);
pthread_mutex_unlock(&mutex);
}
break;
case MIDIListener::MIDI_NOTE_OFF:
case MIDIListener::MIDI_NOTE_ON:
if (count == 3)
{
pthread_mutex_lock(&mutex);
add_note(status, ch, (*message)[1], (*message)[2]);
pthread_mutex_unlock(&mutex);
}
break;
case MIDI_SYSTEM:
if(count==1) switch(ch)
{
case MIDIListener::MIDI_START:
{
//set_started(true);
reset_song_position();
}
break;
case MIDIListener::MIDI_STOP:
{
//set_started(false);
}
break;
case MIDIListener::MIDI_CONTINUE:
{
//set_started(true);
}
break;
case MIDIListener::MIDI_CLOCK:
{
pthread_mutex_lock(&mutex);
++pulse;
if(clocks_per_beat==pulse)
{
pulse = 0;
++beat;
if(beats_per_bar==beat)
{
beat = 0;
++bar;
}
}
pthread_mutex_unlock(&mutex);
}
break;
default:
break;
}
default:
break;
}
/* debug string for last_event */
for (unsigned i = 0; i < count; i++)
{
if (i == 0)
{
switch (status)
{
case MIDIListener::MIDI_NOTE_OFF:
snprintf(buf, 256, "%d (note off) %d ", status, ch);
last_event = string(buf);
break;
case MIDIListener::MIDI_NOTE_ON:
snprintf(buf, 256, "%d (note on) %d ", status, ch);
last_event = string(buf);
break;
case MIDIListener::MIDI_CONTROLLER:
snprintf(buf, 256, "%d (cc) %d ", status, ch);
last_event = string(buf);
break;
default:
snprintf(buf, 256, "%d %d ", status, ch);
last_event = string(buf);
break;
}
}
else
{
snprintf(buf, 256, "%d ", (*message)[i]);
last_event += string(buf);
}
}
}