-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathroto.ino
594 lines (509 loc) · 16.3 KB
/
roto.ino
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
/* Copyright (c) 2018 Peter Teichman */
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SerialFlash.h>
#include "amfm_audio.h"
#include "manual.h"
#include "monitor_audio.h"
#include "preamp_audio.h"
#include "tonewheel_osc_audio.h"
#include "vibrato_audio.h"
// Hammond B-3.
AudioMixer4 organOut;
TonewheelOsc tonewheels;
Monitor tonewheelsMonitor;
Vibrato vibrato;
AudioConnection patchCord0(tonewheels, 0, tonewheelsMonitor, 0);
AudioConnection patchCord1(tonewheelsMonitor, 0, vibrato, 0);
AudioConnection patchCord2(vibrato, 0, organOut, 0);
TonewheelOsc percussion;
AudioEffectEnvelope percussionEnv;
AudioConnection patchCord3(percussion, 0, percussionEnv, 0);
AudioConnection patchCord4(percussionEnv, 0, organOut, 1);
AudioAmplifier swell;
AudioConnection patchCord5(organOut, 0, swell, 0);
// This antialias filter is here to band limit the organ signal, in
// case key click transients are too high frequency, and also to give
// a slight reduction in key click.
AudioFilterBiquad antialias;
AudioConnection patchCord6(swell, 0, antialias, 0);
// Leslie 122
Preamp preamp;
AudioFilterStateVariable crossover;
AmFm leslieBassR;
AmFm leslieTrebleR;
AudioMixer4 leslieR;
AmFm leslieBassL;
AmFm leslieTrebleL;
AudioMixer4 leslieL;
AudioConnection patchCord7(antialias, 0, preamp, 0);
AudioConnection patchCord8(preamp, 0, crossover, 0);
AudioConnection patchCord9(crossover, 0, leslieBassR, 0);
AudioConnection patchCord10(crossover, 2, leslieTrebleR, 0);
AudioConnection patchCord11(leslieBassR, 0, leslieR, 0);
AudioConnection patchCord12(leslieTrebleR, 0, leslieR, 1);
AudioConnection patchCord13(crossover, 0, leslieBassL, 0);
AudioConnection patchCord14(crossover, 2, leslieTrebleL, 0);
AudioConnection patchCord15(leslieBassL, 0, leslieL, 0);
AudioConnection patchCord16(leslieTrebleL, 0, leslieL, 1);
// Teensy audio board output.
AudioOutputI2S i2s1;
AudioControlSGTL5000 audioShield;
AudioConnection patchCord17(leslieR, 0, i2s1, 0);
AudioConnection patchCord18(leslieL, 0, i2s1, 1);
#ifdef AUDIO_INTERFACE
// If the board is configured for USB audio, mirror the i2s output to USB.
AudioOutputUSB usbAudio;
AudioConnection patchCord19(leslieR, 0, usbAudio, 0);
AudioConnection patchCord20(leslieL, 0, usbAudio, 1);
#endif
// MIDI state. keys[n] will be nonzero if a key is down (value being
// the most recent velocity). control[n] is the most recent value of a
// control message.
//
// In both cases, the highest bit indicates whether the value is the
// result of a message (1 if yes, 0 if it hasn't changed since
// initialization.
uint8_t midiKeys[127] = {0};
uint8_t midiControl[127] = {0};
#define MANUAL_KEY_0 (35)
#define MANUAL_KEY_61 (MANUAL_KEY_0 + 61)
#define CC_SWELL (11)
#define CC_RESET (46)
#define CC_DRAWBAR_0 (69)
#define CC_DRAWBAR_9 (CC_DRAWBAR_0 + 9)
#define CC_ROTARY_SPEED (82)
#define CC_ROTARY_STOP (79)
#define CC_VIBRATO_MODE (84)
#define CC_PERCUSSION (87)
#define CC_PERCUSSION_FAST (88)
#define CC_PERCUSSION_SOFT (89)
#define CC_PERCUSSION_THIRD (95) // is this correct?
#define CC_VIBRATO (107)
#define CC_SPEAKER_DRIVE (111)
// numKeysDown is used to keep the percussion effect single triggered:
// only the first key down affects the percussion setting.
uint8_t numKeysDown = 0;
void handleNoteOn(byte chan, byte note, byte vel);
void handleNoteOff(byte chan, byte note, byte vel);
void handleControlChange(byte chan, byte ctrl, byte val);
// reset restores everything to just-booted state:
// 1) it thinks all keys are up
// 2) drawbar registration is set to 888800000
// 3) percussion is off, vibrato is set to C1
// 4) the Leslie is set to slow
void reset() {
// Release all keys and reset all control settings.
memset(midiKeys, 0, 127);
memset(midiControl, 0, 127);
// Set drawbars to Green Onions.
midiControl[CC_DRAWBAR_0 + 1] = 127;
midiControl[CC_DRAWBAR_0 + 2] = 127;
midiControl[CC_DRAWBAR_0 + 3] = 127;
midiControl[CC_DRAWBAR_0 + 4] = 127;
// Minimal drive by default.
midiControl[CC_SPEAKER_DRIVE] = 0;
// Reset Leslie rotation position. Our R microphone leads the L by
// 90 degrees.
leslieBassL.setPhase(0);
leslieTrebleL.setPhase(0);
leslieBassR.setPhase(0.25);
leslieTrebleR.setPhase(0.25);
updateLeslieAmplifier();
updateLeslieRotation();
updatePercussionEnvelope();
updateTonewheelVolumes();
updateVibrato();
}
enum {
NO_TONEWHEEL,
ONE_TONEWHEEL,
ALL_DRAWBARS,
PERCUSSION,
VIBRATO,
LESLIE,
LESLIE_FAST_GROWL,
FULL_POLYPHONY,
};
void preset(int conf) {
reset();
switch (conf) {
case NO_TONEWHEEL:
midiControl[CC_DRAWBAR_0 + 1] = 0;
midiControl[CC_DRAWBAR_0 + 2] = 0;
midiControl[CC_DRAWBAR_0 + 3] = 0;
midiControl[CC_DRAWBAR_0 + 4] = 0;
midiControl[CC_DRAWBAR_0 + 5] = 0;
midiControl[CC_DRAWBAR_0 + 6] = 0;
midiControl[CC_DRAWBAR_0 + 7] = 0;
midiControl[CC_DRAWBAR_0 + 8] = 0;
midiControl[CC_DRAWBAR_0 + 9] = 0;
midiControl[CC_ROTARY_SPEED] = 0;
break;
case ONE_TONEWHEEL:
midiControl[CC_DRAWBAR_0 + 1] = 0;
midiControl[CC_DRAWBAR_0 + 2] = 0;
midiControl[CC_DRAWBAR_0 + 3] = 127;
midiControl[CC_DRAWBAR_0 + 4] = 0;
midiControl[CC_DRAWBAR_0 + 5] = 0;
midiControl[CC_DRAWBAR_0 + 6] = 0;
midiControl[CC_DRAWBAR_0 + 7] = 0;
midiControl[CC_DRAWBAR_0 + 8] = 0;
midiControl[CC_DRAWBAR_0 + 9] = 0;
break;
case ALL_DRAWBARS:
midiControl[CC_DRAWBAR_0 + 1] = 127;
midiControl[CC_DRAWBAR_0 + 2] = 127;
midiControl[CC_DRAWBAR_0 + 3] = 127;
midiControl[CC_DRAWBAR_0 + 4] = 127;
midiControl[CC_DRAWBAR_0 + 5] = 127;
midiControl[CC_DRAWBAR_0 + 6] = 127;
midiControl[CC_DRAWBAR_0 + 7] = 127;
midiControl[CC_DRAWBAR_0 + 8] = 127;
midiControl[CC_DRAWBAR_0 + 9] = 127;
break;
case PERCUSSION:
midiControl[CC_PERCUSSION] = 127;
midiControl[CC_PERCUSSION_THIRD] = 127;
midiControl[CC_ROTARY_STOP] = 127;
break;
case VIBRATO:
midiControl[CC_VIBRATO] = 127;
midiControl[CC_VIBRATO_MODE] = 127;
midiControl[CC_ROTARY_STOP] = 127;
break;
case LESLIE:
midiControl[CC_ROTARY_STOP] = 0;
midiControl[CC_ROTARY_SPEED] = 0;
break;
case LESLIE_FAST_GROWL:
midiControl[CC_ROTARY_STOP] = 0;
midiControl[CC_ROTARY_SPEED] = 127;
midiControl[CC_SPEAKER_DRIVE] = 127;
break;
case FULL_POLYPHONY:
// cheating around what sounds like some overflow / sign errors
midiControl[CC_DRAWBAR_0 + 1] = 0;
midiControl[CC_DRAWBAR_0 + 2] = 0;
midiControl[CC_DRAWBAR_0 + 3] = 0;
midiControl[CC_DRAWBAR_0 + 4] = 0;
midiControl[CC_DRAWBAR_0 + 5] = 0;
midiControl[CC_DRAWBAR_0 + 6] = 127;
midiControl[CC_DRAWBAR_0 + 7] = 127;
midiControl[CC_DRAWBAR_0 + 8] = 127;
midiControl[CC_DRAWBAR_0 + 9] = 127;
midiControl[CC_ROTARY_SPEED] = 0;
fullPolyphony();
break;
}
updateLeslieAmplifier();
updateLeslieRotation();
updatePercussionEnvelope();
updateTonewheelVolumes();
updateVibrato();
}
void setup() {
Serial.begin(115200);
AudioMemory(10);
leslieBassR.init();
leslieTrebleR.init();
leslieBassL.init();
leslieTrebleL.init();
tonewheels.init();
percussion.init();
vibrato.init();
reset();
swell.gain(1.0);
organOut.gain(0, 0.50); // tonewheels + vibrato
organOut.gain(1, 0.50); // percussionEnv
organOut.gain(2, 0);
organOut.gain(3, 0);
leslieR.gain(0, 0.70); // bass
leslieR.gain(1, 0.30); // treble
leslieL.gain(0, 0.70); // bass
leslieL.gain(1, 0.30); // treble
// The antialias filter is here for two purposes:
//
// 1) To band limit the output of the organ, just in case it
// produces something above our Nyquist frequency (22050 Hz)
//
// 2) To cut the transients when turning on new tonewheels,
// reducing key click.
antialias.setLowpass(0, 2150, 0.707);
audioShield.enable();
audioShield.volume(0.5);
usbMIDI.begin();
usbMIDI.setHandleControlChange(handleControlChange);
usbMIDI.setHandleNoteOn(handleNoteOn);
usbMIDI.setHandleNoteOff(handleNoteOff);
}
int count = 0;
void loop() {
usbMIDI.read();
if ((count++ % 500000) == 0) {
status();
statusVolume();
}
}
int note2key(byte note) {
return (int)note - 35;
}
void fullPolyphony() {
for (int n = 0; n < 128; n++) {
handleNoteOn(1, n, 127);
}
}
void randomDrawbars() {
for (int i = 1; i <= 9; i++) {
midiControl[CC_DRAWBAR_0 + i] = random(0, 127);
}
updateTonewheelVolumes();
}
void handleNoteOn(byte chan, byte note, byte velocity) {
Serial.print("Note on: ");
Serial.print(note);
Serial.print("\n");
// MIDI notes always have the high bit unset, but just in case.
if (note & 0x80) {
return;
}
midiKeys[note] = velocity;
if (note <= MANUAL_KEY_0 || note > MANUAL_KEY_61) {
return;
}
updateTonewheelVolumes();
if (++numKeysDown == 1 && midiControl[CC_PERCUSSION]) {
percussionEnv.noteOn();
}
}
void handleNoteOff(byte chan, byte note, byte vel) {
Serial.print("Note off: ");
Serial.print(note);
Serial.print("\n");
if (note & 0x80) {
return;
}
midiKeys[note] = 0;
if (note <= MANUAL_KEY_0 || note > MANUAL_KEY_61) {
return;
}
if (--numKeysDown == 0 && midiControl[CC_PERCUSSION]) {
percussionEnv.noteOff();
}
updateTonewheelVolumes();
}
void updateReset() {
if (midiControl[CC_RESET]) {
midiControl[CC_RESET] = 0;
reset();
}
}
void updateVibrato() {
uint8_t mode = midiControl[CC_VIBRATO_MODE];
if (mode == 0) {
vibrato.setMode(V1);
} else if (mode <= 26) {
vibrato.setMode(C1);
} else if (mode <= 51) {
vibrato.setMode(V2);
} else if (mode <= 84) {
vibrato.setMode(C2);
} else if (mode <= 102) {
vibrato.setMode(V3);
} else if (mode <= 127) {
vibrato.setMode(C3);
}
if (!midiControl[CC_VIBRATO]) {
vibrato.setMode(Off);
}
}
void updatePercussionEnvelope() {
percussionEnv.delay(0.0);
percussionEnv.attack(0.1);
percussionEnv.sustain(0.0);
percussionEnv.release(0.0);
if (midiControl[CC_PERCUSSION_FAST]) {
percussionEnv.decay(300.0);
} else {
percussionEnv.decay(630.0);
}
if (midiControl[CC_PERCUSSION_SOFT]) {
organOut.gain(1, 0.25);
} else {
organOut.gain(1, 0.50);
}
}
uint8_t bars[10] = {0};
uint16_t volumes[92] = {0};
uint8_t percBars[10] = {0};
uint16_t percVolumes[92] = {0};
void updateTonewheelVolumes() {
for (int i = 1; i < 10; i++) {
bars[i] = manual_quantize_drawbar(midiControl[CC_DRAWBAR_0 + i]);
}
if (midiControl[CC_PERCUSSION]) {
bars[9] = 0;
if (midiControl[CC_PERCUSSION_THIRD]) {
percBars[5] = manual_quantize_drawbar(127);
} else {
percBars[4] = manual_quantize_drawbar(127);
}
}
manual_fill_volumes(&midiKeys[MANUAL_KEY_0], percBars, percVolumes);
percussion.setVolumes(percVolumes);
manual_fill_volumes(&midiKeys[MANUAL_KEY_0], bars, volumes);
tonewheels.setVolumes(volumes);
}
void updateLeslieAmplifier() {
float k = remap((float)midiControl[CC_SPEAKER_DRIVE], 0, 127, 5.0, 50.0);
preamp.setK(k);
crossover.frequency(800);
crossover.resonance(0.707);
}
void updateLeslieRotation() {
// Reset some things that should be constant.
leslieBassR.setTremoloDepth(0.3);
leslieTrebleR.setTremoloDepth(0.1);
leslieBassL.setTremoloDepth(0.3);
leslieTrebleL.setTremoloDepth(0.1);
// Vibrato in the AMFM blocks currently has some fizz artifacts.
// These Leslie speeds are from
// http://www.dairiki.org/HammondWiki/LeslieRotationSpeed
if (midiControl[CC_ROTARY_STOP]) {
// Stop
leslieBassR.setRotationRate(0);
leslieTrebleR.setRotationRate(0);
leslieBassL.setRotationRate(0);
leslieTrebleL.setRotationRate(0);
} else if (midiControl[CC_ROTARY_SPEED]) {
// Fast
leslieBassR.setRotationRate(5.7);
leslieTrebleR.setRotationRate(6.66);
leslieBassL.setRotationRate(5.7);
leslieTrebleL.setRotationRate(6.66);
} else {
// Slow
leslieBassR.setRotationRate(0.666);
leslieTrebleR.setRotationRate(0.8);
leslieBassL.setRotationRate(0.666);
leslieTrebleL.setRotationRate(0.8);
}
}
float remap(float v, float oldmin, float oldmax, float newmin, float newmax) {
return newmin + (v - oldmin) * (newmax - newmin) / (oldmax - oldmin);
}
// handleControlChange is compatible (where possible) with the Nord
// Electro 3 MIDI implementation:
// http://www.nordkeyboards.com/sites/default/files/files/downloads/manuals/nord-electro-3/Nord%20Electro%203%20English%20User%20Manual%20v3.x%20Edition%203.1.pdf
void handleControlChange(byte chan, byte ctrl, byte val) {
if (ctrl == 1) {
// Skip logging aftertouch messages, so the serial log isn't
// spammed with them.
return;
}
Serial.print("Control Change, ch=");
Serial.print(chan, DEC);
Serial.print(", control=");
Serial.print(ctrl, DEC);
Serial.print(", value=");
Serial.print(val, DEC);
Serial.println();
if (ctrl & 0x80) {
return;
}
midiControl[ctrl] = val;
if (ctrl == CC_SWELL) {
swell.gain(remap((float)val, 0, 127, 0, 2.5));
} else if (ctrl == CC_RESET) {
updateReset();
} else if (ctrl == CC_PERCUSSION) {
updatePercussionEnvelope();
updateTonewheelVolumes();
} else if (ctrl == CC_PERCUSSION_FAST) {
updatePercussionEnvelope();
} else if (ctrl == CC_PERCUSSION_SOFT) {
updatePercussionEnvelope();
} else if (ctrl > CC_DRAWBAR_0 && ctrl <= CC_DRAWBAR_9) {
updateTonewheelVolumes();
} else if (ctrl == CC_ROTARY_STOP || ctrl == CC_ROTARY_SPEED) {
updateLeslieRotation();
} else if (ctrl == CC_VIBRATO || ctrl == CC_VIBRATO_MODE) {
updateVibrato();
}
}
void showKeys() {
for (int i = 0; i < 62; i++) {
Serial.print("keys[");
Serial.print(i);
Serial.print("] = ");
Serial.print(midiKeys[MANUAL_KEY_0 + i]);
Serial.print("\n");
}
}
void showVolumes(uint16_t volumes[92]) {
for (int i = 0; i < 92; i++) {
Serial.print("volumes[");
Serial.print(i);
Serial.print("] = ");
Serial.print(volumes[i]);
Serial.print("\n");
}
}
void status() {
Serial.print("CPU: ");
Serial.print("tonewheels=");
Serial.print(tonewheels.processorUsage());
Serial.print(",");
Serial.print(tonewheels.processorUsageMax());
Serial.print(" ");
Serial.print("vibrato=");
Serial.print(vibrato.processorUsage());
Serial.print(",");
Serial.print(vibrato.processorUsageMax());
Serial.print(" ");
Serial.print("antialias=");
Serial.print(antialias.processorUsage());
Serial.print(",");
Serial.print(antialias.processorUsageMax());
Serial.print(" ");
Serial.print("all=");
Serial.print(AudioProcessorUsage());
Serial.print(",");
Serial.print(AudioProcessorUsageMax());
Serial.print(" ");
Serial.print("Memory: ");
Serial.print(AudioMemoryUsage());
Serial.print(",");
Serial.print(AudioMemoryUsageMax());
Serial.print(" ");
Serial.println();
}
void statusVolume() {
Serial.print("Volume: ");
Serial.print("tonewheels=");
Serial.print(tonewheelsMonitor.volumeUsageMin());
Serial.print(",");
Serial.print(tonewheelsMonitor.volumeUsageMax());
Serial.print(" ");
Serial.print(tonewheelsMonitor.volumeUsageMinEver());
Serial.print(",");
Serial.print(tonewheelsMonitor.volumeUsageMaxEver());
Serial.println();
tonewheelsMonitor.reset();
}
void statusPerc() {
Serial.print("percOn=");
Serial.print(midiControl[CC_PERCUSSION]);
Serial.print(" ");
Serial.print("percFast=");
Serial.print(midiControl[CC_PERCUSSION_FAST]);
Serial.print(" ");
Serial.print("percSoft=");
Serial.print(midiControl[CC_PERCUSSION_SOFT]);
Serial.print(" ");
Serial.print("percThird=");
Serial.print(midiControl[CC_PERCUSSION_THIRD]);
Serial.print(" ");
Serial.println();
}