-
Notifications
You must be signed in to change notification settings - Fork 38
/
MidiDecode.cpp
654 lines (577 loc) · 19.7 KB
/
MidiDecode.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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
/*
MidiDecode.cpp
Copyright 2017 Will Godfrey
This file is part of yoshimi, which is free software: you can redistribute
it and/or modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
yoshimi 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 (version 2 or
later) for more details.
You should have received a copy of the GNU General Public License along with
yoshimi; if not, write to the Free Software Foundation, Inc., 51 Franklin
Street, Fifth Floor, Boston, MA 02110-1301, USA.
Modified September 2017
*/
#include <iostream>
#include <bitset>
#include <unistd.h>
#include <list>
#include <string>
#include <unistd.h>
using namespace std;
#include "Interface/MidiDecode.h"
#include "MusicIO/MidiControl.h"
#include "Interface/InterChange.h"
#include "Misc/MiscFuncs.h"
#include "Misc/SynthEngine.h"
MidiDecode::MidiDecode(SynthEngine *_synth) :
synth(_synth)
{
//init
}
MidiDecode::~MidiDecode()
{
//close
}
void MidiDecode::midiProcess(unsigned char par0, unsigned char par1, unsigned char par2, bool in_place)
{
if (synth->isMuted())
return; // nobody listening!
unsigned char channel, note, velocity;
int ctrltype, par;
channel = par0 & 0x0F;
unsigned int ev = par0 & 0xF0;
par = 0;
switch (ev)
{
case 0x80: // note-off
note = par1;
setMidiNoteOff(channel, note);
break;
case 0x90: // note-on
// cout << "note " << int(par1) << endl;
note = par1;
if (note)
{
velocity = par2;
setMidiNote(channel, note, velocity);
}
else
setMidiNoteOff(channel, note);
break;
case 0xA0: // key aftertouch
ctrltype = C_channelpressure;
/*
* temporarily pretend it's a chanel aftertouch
* need to work out how to use key numbers (par1)
* for actual key pressure sensing.
*
* ctrltype = C_keypressure;
*/
par = par2;
setMidiController(channel, ctrltype, par, in_place);
break;
case 0xB0: // controller
ctrltype = par1; // getMidiController(par1);
par = par2;
setMidiController(channel, ctrltype, par, in_place);
break;
case 0xC0: // program change
ctrltype = C_programchange;
par = par1;
setMidiProgram(channel, par, in_place);
break;
case 0xD0: // channel aftertouch
ctrltype = C_channelpressure;
par = par1;
setMidiController(channel, ctrltype, par, in_place);
break;
case 0xE0: // pitch bend
ctrltype = C_pitchwheel;
par = (par2 << 7) | par1;
setMidiController(channel, ctrltype, par, in_place);
break;
default: // wot, more?
if (par0 == 0xFF)
{
ctrltype = C_reset;
if (!in_place) // never want to get this when freewheeling!
setMidiController(channel, ctrltype, 0);
}
else
synth->getRuntime().Log("other event: " + asString((int)ev), 1);
break;
}
}
void MidiDecode::setMidiController(unsigned char ch, int ctrl, int param, bool in_place)
{
int nLow;
int nHigh;
if (synth->getRuntime().monitorCCin)
{
string ctltype;
switch (ctrl)
{
case C_NULL:
ctltype = "Ignored";
break;
case C_reset:
ctltype = "Master Reset";
break;
case C_programchange:
ctltype = "program";
break;
case C_pitchwheel:
ctltype = "Pitchwheel";
break;
case C_channelpressure:
ctltype = "Ch Press";
break;
case C_keypressure:
ctltype = "Key Press";
break;
default:
ctltype = asString(ctrl);
break;
}
synth->getRuntime().Log("Chan " + asString(((int) ch) + 1) + " CC " + ctltype + " Value " + asString(param));
}
if (ctrl == C_reset)
{
synth->interchange.flagsWrite(0xf0000000);
return;
}
if (ctrl == synth->getRuntime().midi_bank_root)
{
setMidiBankOrRootDir(param, in_place, true);
return;
}
if (ctrl == synth->getRuntime().midi_bank_C)
{
setMidiBankOrRootDir(param, in_place);
return;
}
if (ctrl == synth->getRuntime().midi_upper_voice_C)
{
// it's really an upper set program change
setMidiProgram(ch, (param & 0x1f) | 0x80, in_place);
return;
}
if (ctrl == C_nrpnL || ctrl == C_nrpnH)
{
if (ctrl == C_nrpnL)
{
if (synth->getRuntime().nrpnL != param)
{
synth->getRuntime().nrpnL = param;
unsigned char type = synth->getRuntime().nrpnH;
if (type >= 0x41 && type <= 0x43)
{ // shortform
if (param > 0x77) // disable it
{
synth->getRuntime().channelSwitchType = 0;
synth->getRuntime().channelSwitchCC = 0x80;
}
else
{
synth->getRuntime().channelSwitchType = type & 3; // row/column/loop
synth->getRuntime().channelSwitchCC = param;
}
return;
}
if (type == 0x44 && param == 0x44)
{
synth->getRuntime().runSynth = false;
return; // bye bye everyone
}
//synth->getRuntime().Log("Set nrpn LSB to " + asString(param));
}
nLow = param;
nHigh = synth->getRuntime().nrpnH;
}
else // MSB
{
if (synth->getRuntime().nrpnH != param)
{
synth->getRuntime().nrpnH = param;
//synth->getRuntime().Log("Set nrpn MSB to " + asString(param));
if (param == 0x41) // set shortform
{
synth->getRuntime().nrpnL = 0x7f;
return;
}
}
nHigh = param;
nLow = synth->getRuntime().nrpnL;
}
synth->getRuntime().dataL = 0x80; // we've changed the NRPN
synth->getRuntime().dataH = 0x80; // so these are now invalid
synth->getRuntime().nrpnActive = (nLow < 0x7f && nHigh < 0x7f);
// synth->getRuntime().Log("Status nrpn " + asString(synth->getRuntime().nrpnActive));
return;
}
if (synth->getRuntime().nrpnActive)
{
if (ctrl == C_dataI || ctrl == C_dataD)
{ // translate these to C_dataL and C_dataH
int dHigh = synth->getRuntime().dataH;
int dLow = synth->getRuntime().dataL;
bool msbPar = (param >= 0x40);
param &= 0x3f;
if (ctrl == C_dataI)
{
if (msbPar)
{
dHigh &= 0x7f; // clear disabled state
param += dHigh;
ctrl = C_dataH; // change controller type
}
else
{
dLow &= 0x7f; // clear disabled state
param += dLow;
ctrl = C_dataL; // change controller type
}
if (param > 0x7f)
param = 0x7f;
}
else
{ // data decrement
if (msbPar)
{
param = dHigh - param;
ctrl = C_dataH; // change controller type
}
else
{
param = dLow - param;
ctrl = C_dataL; // change controller type
}
if (param < 0)
param = 0;
}
}
if (ctrl == C_dataL || ctrl == C_dataH)
{
nrpnProcessData(ch, ctrl, param, in_place);
return;
}
}
unsigned char vecChan;
if (synth->getRuntime().channelSwitchType == 1)
vecChan = synth->getRuntime().channelSwitchValue;
// force vectors to obey channel switcher
else
vecChan = ch;
if (synth->getRuntime().vectordata.Enabled[vecChan] && synth->getRuntime().NumAvailableParts > NUM_MIDI_CHANNELS)
{ // vector control is direct to parts
if (nrpnRunVector(vecChan, ctrl, param))
return; // **** test this it may be wrong!
}
// pick up a drop-through if CC doesn't match the above
if (ctrl == C_resetallcontrollers && synth->getRuntime().ignoreResetCCs == true)
{
//synth->getRuntime().Log("Reset controllers ignored");
return;
}
/*
* set / run midi learn will pass 'in_place' so entire operation
* can be done in MidiLearn.cpp
* return true if blocking further calls
*
* need to work out some kind of loop-back so optional
* vector control CCs can be picked up.
*
* Some controller valuse are >= 640 so they will be ignored by
* later calls, but are passed as 128+ for this call.
* Pitch wheel is 640 and is 14 bit. It sets bit 1 of 'category'
*/
if (synth->midilearn.runMidiLearn(param, ctrl & 255, ch, in_place | ((ctrl == 640) << 1)))
return;
if (ctrl == C_breath)
{
synth->SetController(ch, C_volume, param);
ctrl = C_filtercutoff;
}
else
/*
* This is done here instead of in 'setMidi' so MidiLearn
* handles all 14 bit values the same.
*/
if (ctrl == C_pitchwheel)
param -= 8192;
// do what's left!
synth->SetController(ch, ctrl, param);
}
bool MidiDecode::nrpnRunVector(unsigned char ch, int ctrl, int param)
{
int Xopps = synth->getRuntime().vectordata.Xfeatures[ch];
int Yopps = synth->getRuntime().vectordata.Yfeatures[ch];
int p_rev = 127 - param;
int swap1;
int swap2;
unsigned char type;
if (ctrl == synth->getRuntime().vectordata.Xaxis[ch])
{
if (Xopps & 1) // fixed as volume
{
synth->SetController(ch | 0x80, C_volume,127 - (p_rev * p_rev / 127));
synth->SetController(ch | 0x90, C_volume, 127 - (param * param / 127));
}
if (Xopps & 2) // default is pan
{
type = synth->getRuntime().vectordata.Xcc2[ch];
swap1 = (Xopps & 0x10) | 0x80;
swap2 = swap1 ^ 0x10;
synth->SetController(ch | swap1, type, param);
synth->SetController(ch | swap2, type, p_rev);
}
if (Xopps & 4) // default is 'brightness'
{
type = synth->getRuntime().vectordata.Xcc4[ch];
swap1 = ((Xopps >> 1) & 0x10) | 0x80;
swap2 = swap1 ^ 0x10;
synth->SetController(ch | swap1, type, param);
synth->SetController(ch | swap2, type, p_rev);
}
if (Xopps & 8) // default is mod wheel
{
type = synth->getRuntime().vectordata.Xcc8[ch];
swap1 = ((Xopps >> 2) & 0x10) | 0x80;
swap2 = swap1 ^ 0x10;
synth->SetController(ch | swap1, type, param);
synth->SetController(ch | swap2, type, p_rev);
}
return true;
}
else if (ctrl == synth->getRuntime().vectordata.Yaxis[ch])
{ // if Y hasn't been set these commands will be ignored
if (Yopps & 1) // fixed as volume
{
synth->SetController(ch | 0xa0, C_volume,127 - (p_rev * p_rev / 127));
synth->SetController(ch | 0xb0, C_volume, 127 - (param * param / 127));
}
if (Yopps & 2) // default is pan
{
type = synth->getRuntime().vectordata.Ycc2[ch];
swap1 = (Yopps & 0x10) | 0xa0;
swap2 = swap1 ^ 0x10;
synth->SetController(ch | swap1, type, param);
synth->SetController(ch | swap2, type, p_rev);
}
if (Yopps & 4) // default is 'brightness'
{
type = synth->getRuntime().vectordata.Ycc4[ch];
swap1 = ((Yopps >> 1) & 0x10) | 0xa0;
swap2 = swap1 ^ 0x10;
synth->SetController(ch | swap1, type, param);
synth->SetController(ch | swap2, type, p_rev);
}
if (Yopps & 8) // default is mod wheel
{
type = synth->getRuntime().vectordata.Ycc8[ch];
swap1 = ((Yopps >> 2) & 0x10) | 0xa0;
swap2 = swap1 ^ 0x10;
synth->SetController(ch | swap1, type, param);
synth->SetController(ch | swap2, type, p_rev);
}
return true;
}
return false;
}
void MidiDecode::nrpnProcessData(unsigned char chan, int type, int par, bool in_place)
{
int nHigh = synth->getRuntime().nrpnH;
int nLow = synth->getRuntime().nrpnL;
/* if (nLow < nHigh && (nHigh == 4 || nHigh == 8 ))
{
if (type == C_dataL)
synth->getRuntime().dataL = par;
else
synth->getRuntime().dataH = par;
synth->SetZynControls();
return;
}*/
bool noHigh = (synth->getRuntime().dataH > 0x7f);
if (type == C_dataL)
{
synth->getRuntime().dataL = par;
// synth->getRuntime().Log("Data LSB value " + asString(par));
if (noHigh)
return;
}
if (type == C_dataH)
{
synth->getRuntime().dataH = par;
// synth->getRuntime().Log("Data MSB value " + asString(par));
if (noHigh && synth->getRuntime().dataL <= 0x7f)
par = synth->getRuntime().dataL;
else
return; // we're currently using MSB as parameter not a value
}
/*
* All the above runaround performance is to deal with a data LSB
* arriving either before or after the MSB and immediately after
* a new NRPN has been set. After this, running data values expect
* MSB sub parameter before LSB value until the next full NRPN.
*/
int dHigh = synth->getRuntime().dataH;
/*synth->getRuntime().Log(" nHigh " + asString((int)nHigh)
+ " nLow " + asString((int)nLow)
+ " dataH " + asString((int)synth->getRuntime().dataH)
+ " dataL " + asString((int)synth->getRuntime().dataL)
+ " chan " + asString((int)chan)
+ " type" + asString((int)type)
+ " par " + asString((int)par));
*/
// midi learn must come before everything else
if (synth->midilearn.runMidiLearn(dHigh << 7 | par, 0x10000 | (nHigh << 7) | nLow , chan, in_place | 2))
return;
if (nLow < nHigh && (nHigh == 4 || nHigh == 8 ))
{
if (type == C_dataL)
synth->getRuntime().dataL = par;
else
synth->getRuntime().dataH = par;
synth->SetZynControls();
return;
}
if (nHigh != 64 && nLow < 0x7f)
{
synth->getRuntime().Log("Go away NRPN 0x" + asHexString(nHigh) + " " + asHexString(nLow) +" We don't know you!");
//done this way to ensure we see both bytes even if nHigh is zero
synth->getRuntime().nrpnActive = false; // we were sent a turkey!
return;
}
if (nLow == 0) // direct part change
nrpnDirectPart(dHigh, par);
else if (nLow == 1) // it's vector control
nrpnSetVector(dHigh, chan, par);
else if (nLow == 2) // system settings
synth->SetSystemValue(dHigh, par);
}
void MidiDecode::nrpnDirectPart(int dHigh, int par)
{
switch (dHigh)
{
case 0: // set part number
if (par < synth->getRuntime().NumAvailableParts)
{
synth->getRuntime().dataL = par;
synth->getRuntime().vectordata.Part = par;
}
else // It's bad. Kill it
{
synth->getRuntime().dataL = 128;
synth->getRuntime().dataH = 128;
}
break;
case 1: // Program Change
setMidiProgram(synth->getRuntime().vectordata.Part | 0x80, par);
break;
case 2: // Set controller number
synth->getRuntime().vectordata.Controller = par;
synth->getRuntime().dataL = par;
break;
case 3: // Set controller value
synth->SetController(synth->getRuntime().vectordata.Part | 0x80, synth->getRuntime().vectordata.Controller, par);
break;
case 4: // Set part's channel number
synth->SetPartChan(synth->getRuntime().vectordata.Part, par);
break;
case 5: // Set part's audio destination
if (par > 0 and par < 4)
synth->SetPartDestination(synth->getRuntime().vectordata.Part, par);
break;
case 64:
synth->SetPartShift(synth->getRuntime().vectordata.Part, par);
break;
}
}
void MidiDecode:: nrpnSetVector(int dHigh, unsigned char chan, int par)
{
if (synth->vectorInit(dHigh, chan, par))
return;
switch (dHigh)
{
/*
* these have to go through the program change
* thread otherwise they could block following
* MIDI messages.
* TODO
* We need to change this so it goes through
* the common RBP thread.
*/
case 4:
setMidiProgram(chan | 0x80, par);
break;
case 5:
setMidiProgram(chan | 0x90, par);
break;
case 6:
setMidiProgram(chan | 0xa0, par);
break;
case 7:
setMidiProgram(chan | 0xb0, par);
break;
default:
synth->vectorSet(dHigh, chan, par);
break;
}
}
//bank change and root dir change change share the same thread
//to make changes consistent
void MidiDecode::setMidiBankOrRootDir(unsigned int bank_or_root_num, bool in_place, bool setRootDir)
{
if (setRootDir)
{
if (bank_or_root_num == synth->getBankRef().getCurrentRootID())
return; // nothing to do!
}
else
if (bank_or_root_num == synth->getBankRef().getCurrentBankID())
return; // still nothing to do!
if (in_place)
setRootDir ? synth->SetBankRoot(bank_or_root_num) : synth->SetBank(bank_or_root_num);
else
{
if (setRootDir)
synth->writeRBP(1 ,bank_or_root_num,0);
else
synth->writeRBP(2 ,bank_or_root_num,0);
}
}
void MidiDecode::setMidiProgram(unsigned char ch, int prg, bool in_place)
{
int partnum;
if (ch < NUM_MIDI_CHANNELS)
partnum = ch;
else
partnum = ch & 0x7f; // this is for direct part access instead of channel
if (partnum >= synth->getRuntime().NumAvailableParts)
return;
if (synth->getRuntime().EnableProgChange)
{
if (in_place)
{
//synth->getRuntime().Log("Freewheeling");
synth->SetProgram(ch, prg);
}
else
{
//synth->getRuntime().Log("Normal");
synth->writeRBP(3, ch ,prg);
}
}
}
void MidiDecode::setMidiNote(unsigned char channel, unsigned char note,
unsigned char velocity)
{
synth->NoteOn(channel, note, velocity);
}
void MidiDecode::setMidiNoteOff(unsigned char channel, unsigned char note)
{
synth->NoteOff(channel, note);
}