forked from munshkr/picopico
-
Notifications
You must be signed in to change notification settings - Fork 0
/
picopico.ino
800 lines (723 loc) · 26.6 KB
/
picopico.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
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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
/*
* picopico
* Copyright (C) 2017 Damián Silvani
*
* 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/>.
*
* Original code by David Johnson-Davies
* www.technoblogy.com - 27th March 2016
*
* ATtiny85 @ 8MHz (internal oscillator; BOD disabled)
*
*/
#include "player.h"
#include "tune.h"
#include <MIDIUSB.h>
#include <pitchToNote.h>
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#define NUM_VOICES 8
#define DEFAULT_OCTAVE 4
#define DEFAULT_NLEN 4
#define DEFAULT_VOL 15
#define DEFAULT_PW 0xA0
#define FIRST_MANUAL_VOICE 4 // index
const int PIN_LED = 13;
const bool useMIDI = false;
const bool useButtonArray = true;
byte currentManualVoice = FIRST_MANUAL_VOICE;
// midi constants
const unsigned char MESSAGE_NOTE_OFF = 0b1000;
const unsigned char MESSAGE_NOTE_ON = 0b1001;
const unsigned char MASK_MESSAGE_CHANNEL = 0b00001111;
// Note buffer
volatile uint16_t lfsr = 1;
volatile char lfsrOut = 0;
volatile signed char oldTemp = 0; // FIXME change variable name
// Global tick counter
volatile uint16_t timer0_tick = 0;
volatile uint8_t ticks = 0;
volatile bool nextTick = false;
volatile bool nextBigTick = false;
Voice voices[NUM_VOICES] = {};
byte playingVoices = 0;
volatile bool playing = false;
volatile bool mustRestartSong = false;
// for miditoy integration, rows and cols
const int NUM_ROWS = 5;
const int NUM_COLS = 8;
const int ROWS_PIN[NUM_ROWS] = {A0, A1, A2, A3, A4};
const int COLS_PIN[NUM_COLS] = {0, 1, 2, 3, 4, 5, 6, 7};
const int NUM_BUTTONS = NUM_COLS * NUM_ROWS;
int buttonState[NUM_BUTTONS];
int prevButtonState[NUM_BUTTONS];
const int REMAPPED_BUTTONS[NUM_BUTTONS] = {
4,
3, 2, 1, 0, 9, 8, 7, 6, 5, 14,
13, 12, 11, 10, 19, 18, 17, 16, 15, 24,
23, 22, 21, 20, 29, 28, 27, 26, 25, 34,
33, 32, 31, 30, 36, 35, 36, 00, 00
}; // sequential order of buttons
const int BUTTON_MIDI_BASE_NOTE = 48;
char debugStringBuffer[20];
bool debugMode = true;
// sprintf + serial of 20 bytes takes ~200us
// sprintf + serial of 10 bytes takes ~144us
// sprintf + serial of 5 bytes takes ~108us
#define serialDebug(...) \
if (debugMode) { \
sprintf(debugStringBuffer, __VA_ARGS__); \
Serial.print(debugStringBuffer); \
}
ISR(TIMER0_COMPA_vect) {
// tick counter for time keeping
timer0_tick++;
if (timer0_tick > 333) { // 333 = 16.65ms, 111 = 5.55ms
timer0_tick = 0;
// if the tick counter fires before the last tick was completed,
// while still playing, toggle the led
if (nextTick && playing) {
digitalWrite(PIN_LED, abs(digitalRead(PIN_LED) - 1));
}
// ticks and big ticks (every 1 second)
nextTick = true;
ticks++;
if (ticks > 60) { // 16.65ms * 60 = 999ms
ticks = 0;
nextBigTick = true;
// digitalWrite(PIN_LED, abs(digitalRead(PIN_LED) - 1));
}
}
unsigned char temp;
signed char stemp, mask, out = 0;
Voice* v;
for (int i = 0; i < NUM_VOICES; i++) {
v = &voices[i];
switch (v->waveform) {
case PULSE:
v->acc += v->freq;
temp = (v->acc >> 8) & v->pw;
out += (temp ? v->amp : 0) >> 2;
break;
case TRI:
v->acc += v->freq / 2;
stemp = v->acc >> 8;
mask = stemp >> 7;
if (v->amp != 0) {
if (v->volume > 12) { // 13 14 15
out += (stemp ^ mask) >> 1;
} else if (v->volume > 8) { // 9 10 11 12
out += (stemp ^ mask) >> 2;
} else if (v->volume > 4) { // 5 6 7 8
out += (stemp ^ mask) >> 3;
} else if (v->volume > 0) { // 1 2 3 4
out += (stemp ^ mask) >> 4;
}
}
break;
case SAW:
v->acc += v->freq;
temp = v->acc >> 8;
out += (temp ? v->amp : 0) >> 2;
break;
case NOISE:
v->acc += v->freq * 8;
stemp = (v->acc >> 8) & 0x80;
// if temp != oldTemp, trigger the LFSR to generate a new pseudorandom value
if (stemp != oldTemp) {
lfsrOut = (lfsr & 1) ^ ((lfsr & 2) >> 1); // output is bit 0 XOR bit 1
lfsr = (lfsr >> 1) | (lfsrOut << 14); // shift and include output on bit 15
oldTemp = stemp;
}
out += (lfsrOut ? v->amp : 0) >> 2;
break;
}
}
// notes on the noise channels:
// This noise generator is somewhat based on the mechanism found in the NES APU.
// The NES has a linear-feedback shift register for generating pseudorandom numbers.
// It starts with a register set to 1, and when the period counter reaches 0, it
// clocks the shift register.
// The LFSR performs an Exclusive OR between bit 0 and bit 1, then shifts to the
// right, and sets/resets bit 15 based on the exclusive OR result.
OCR1B = out;
}
// Setup **********************************************
void setup() {
Serial.begin(115200);
cli();
// button array setup
for (int col = 0; col < NUM_COLS; col++) {
pinMode(COLS_PIN[col], OUTPUT);
}
for (int row = 0; row < NUM_ROWS; row++) {
pinMode(ROWS_PIN[row], INPUT);
}
// Enable 64 MHz PLL and use as source for Timer1/Timer4
// for ATtiny25-45-85
#if defined(PLLCSR) && defined(PCKE)
PLLCSR = 1 << PLLE; //Enable PLL
_delay_us(100); // Wait for a sync;
while (!(PLLCSR & (1 << PLOCK))) {
};
// PLL Synced
PLLCSR |= (1 << PCKE); // Enable Asynchronous PCK mode.
#endif
/*
// for ATmega16u4-32u4
#if defined(PLLCSR) && defined(PLLFRQ)
sbi(PLLCSR, PINDIV); // When using a 16MHz clock source, this bit must be set to 1 before enabling PLL
sbi(PLLCSR, PLLE); //Enable PLL
_delay_us(100); // Wait for a sync;
while (!(PLLCSR & (1 << PLOCK))) {
};
// PLL Synced
sbi(PLLFRQ, PLLTM1); // PLL Postcaler Factor for High-Speed Timer: 1.5 division
cbi(PLLFRQ, PLLTM0);
sbi(PLLFRQ, PDIV3); // PLL Output Frequency 96MHz
cbi(PLLFRQ, PDIV3);
sbi(PLLFRQ, PDIV3);
cbi(PLLFRQ, PDIV3);
#endif
*/
// for ATtiny25-45-85
#if defined(TCCR1)
// TCCR1 => CTC1 PWM1A COM1A1 COM1A0 CS13 CS12 CS11 CS10
// GTCCR => TSM PWM1B COM1B1 COM1B0 FOC1B FOC1A PSR1 PSR0
// Set up Timer/Counter1 for PWM output
TCCR1 = 1<<CS10; // 1:1 prescale
GTCCR = 1<<PWM1B | 2<<COM1B0; // PWM B, clear on match
OCR1B = 128;
pinMode(4, OUTPUT); // Enable PWM output pin
#endif
/*
// for ATmega16u4-32u4
#if defined(TCCR4A)
// TCCR4A => COM4A1 COM4A0 COM4B1 COM4B0 FOC4A FOC4B PWM4A PWM4B
// TCCR4B => PWM4X PSR4 DTPS41 DTPS40 CS43 CS42 CS41 CS40
// TCCR4C => COM4A1S COM4A0S COM4B1S COMAB0S COM4D1 COM4D0 FOC4D PWM4D
// TCCR4D => FPIE4 FPEN4 FPNC4 FPES4 FPAC4 FPF4 WGM41 WGM40
// TCCR4E => TLOCK4 ENHC4 OC4OE5 OC4OE4 OC4OE3 OC4OE2 OC4OE1 OC4OE0
sbi(TCCR4B, CS40); // set prescaler 1:1
cbi(TCCR4B, CS41);
cbi(TCCR4B, CS42);
cbi(TCCR4B, CS43);
sbi(TCCR4A, PWM4A); // enable PWM A
cbi(TCCR4D, WGM40); // Fast PWM (non PWM6)
cbi(TCCR4D, WGM41);
cbi(TCCR4A, COM4A0); // compare output mode: clear on compare match
sbi(TCCR4A, COM4A1);
OCR4A = 128;
pinMode(5, OUTPUT); // Enable PWM output pin
#endif
*/
// for ATmega328
//#if defined(TCCR1A) && !defined(TCCR4A)
//#if defined(TCCR1A)
// TCCR1A => COM1A1 COM1A0 COM1B1 COM1B0 – – WGM11 WGM10
// TCCR1B => ICNC1 ICES1 – WGM13 WGM12 CS12 CS11 CS10
// Set up Timer/Counter1 for PWM output
sbi(TCCR1B, CS10); // 1:1 prescale
cbi(TCCR1B, CS11);
cbi(TCCR1B, CS12);
sbi(TCCR1A, WGM10); // Fast PWM Mode, 8bit
cbi(TCCR1A, WGM11);
sbi(TCCR1B, WGM12);
cbi(TCCR1B, WGM13);
cbi(TCCR1A, COM1B0); // Clear OC1B on Compare Match
sbi(TCCR1A, COM1B1);
OCR1B = 128;
pinMode(10, OUTPUT); // Enable PWM output pin
//#endif
// Set up Timer/Counter0 for 20kHz interrupt to output samples.
// The resulting frequency is calculated similar to the CTC mode.
// F_CPU / (2 * PRESCALER * (1 + OCR0A))
TCCR0A = 0;
TCCR0B = 0;
sbi(TCCR0A, WGM00);
sbi(TCCR0A, WGM01);
sbi(TCCR0B, WGM02); // Fast PWM
sbi(TCCR0B, CS01); // 1/8 prescale
// OCR0A = 49; // Divide by 800 (40KHz)
OCR0A = 99; // Divide by 1600 (20KHz)
// On Timer0, enable timer compare match, disable overflow
#if defined(TIMSK)
TIMSK = 1 << OCIE0A | 0 << TOIE0;
#elif defined(TIMSK0)
TIMSK0 = 1 << OCIE0A | 0 << TOIE0;
#endif
sei();
// voice initialization
for (int i = 0; i < NUM_VOICES; i++) {
Voice* v = &voices[i];
// the manual voice is only played through midi
if (i < FIRST_MANUAL_VOICE) {
v->ptr = SongData[i];
}
v->playing = false;
v->finished = false;
v->nlen = DEFAULT_NLEN;
v->qlen = v->nlen;
v->octave = DEFAULT_OCTAVE;
v->transpose = 0;
v->volume = DEFAULT_VOL;
v->track_loop_ptr = NULL;
v->loops_idx = -1;
v->gate = false;
v->pw = DEFAULT_PW;
}
// voices
voices[0].waveform = PULSE;
voices[1].waveform = PULSE;
voices[2].waveform = TRI;
voices[3].waveform = NOISE;
// manual voices
for (int i = FIRST_MANUAL_VOICE; i < NUM_VOICES; i++) {
voices[i].waveform = PULSE;
voices[i].pw = 0x80;
}
//playing = true;
serialDebug("START\n");
}
// Main loop
bool playVoice(Voice& voice) {
if (voice.finished) return false;
if (voice.playing) {
if (voice.qlen_c == 0) {
voice.gate = false;
voice.amp = 0;
}
if (voice.nlen_c == 0) voice.playing = false;
voice.qlen_c--;
voice.nlen_c--;
}
if (voice.playing) {
playSequences(voice);
return true;
}
while (true) {
const byte cmd = fetchNextByte(voice);
if (!cmd) {
if (voice.track_loop_ptr) {
voice.ptr = voice.track_loop_ptr;
} else {
voice.gate = false;
voice.finished = true;
break;
}
} else if (cmd < 0x80) {
playNote(voice, cmd);
playSequences(voice);
break;
} else {
executeCommand(voice, cmd);
}
}
return true;
}
inline byte fetchNextByte(Voice& voice) {
// return pgm_read_byte(voice.ptr++);
return *(voice.ptr++);
}
inline void playNote(Voice& voice, byte note) {
voice.playing = true;
// If note is not a rest, set frequency based on note and current octave
if ((note & NOTE_MASK) == REST) {
voice.amp = 0;
} else {
voice.note = (note & NOTE_MASK) - 2;
voice.amp = amp[voice.volume];
voice.gate = true;
}
// Set note length counter
if (note & WITH_LEN) {
// If note command has a length counter parameter, use it (word or byte)
if (note & WORD) {
voice.nlen_c = fetchNextByte(voice) | (fetchNextByte(voice) << 8);
} else {
voice.nlen_c = fetchNextByte(voice);
}
} else {
// Otherwise, use default note length counter
voice.nlen_c = voice.nlen;
}
// Set quantization length counter
if (note & WITH_Q) {
if (note & WORD) {
voice.qlen_c = fetchNextByte(voice) | (fetchNextByte(voice) << 8);
} else {
voice.qlen_c = fetchNextByte(voice);
}
} else {
voice.qlen_c = voice.qlen;
}
// Reset sequence pointers
resetSequences(voice);
}
inline byte fetchSeqValue(const Envelope& env) {
// return pgm_read_byte(Seqs[env.id - 1] + env.i - 1);
return *(Seqs[env.id - 1] + env.i - 1);
}
byte playSequence(Voice& voice, Envelope& env) {
byte value = fetchSeqValue(env);
// [1 2 3 4 5 | 7 6 = 4 3 2 1 0] 1 2 3 4 5 7 6 [7 6 ...] 4 3 2 1 0
// [1 2 3 4 5 | 7 6] 1 2 3 4 5 [7 6 7 6 ...] 0
// [1 2 3 4 5 = 4 3 2 1 0] 1 2 3 4 [5 5 ...] 4 3 2 1 0
// [1 2 3 4 5] 1 2 3 4 [5 5 ...] 0
if (voice.gate) {
if (value == SEQ_LOOP) {
// Found a Loop Marker, advance and save it for later
env.i++;
env.loop_i = env.i;
value = fetchSeqValue(env);
} else if (value == SEQ_END || value == SEQ_REL) {
// Found a Release Marker, save it for later
if (value == SEQ_REL) {
env.rel_i = env.i + 1;
}
// Go back to Loop Marker if there is one
if (env.loop_i) {
env.i = env.loop_i;
value = fetchSeqValue(env);
}
} else {
env.i++;
}
} else {
if (env.rel_i && env.i < env.rel_i) {
env.i = env.rel_i;
value = fetchSeqValue(env);
} else if (value != SEQ_END) {
env.i++;
}
}
if (value == SEQ_LOOP || value == SEQ_REL || value == SEQ_END) return 0;
return value;
}
inline void playSequences(Voice& voice) {
// Volume Envelope
if (voice.volume_env.id) {
const byte value = playSequence(voice, voice.volume_env);
if (value) {
voice.amp = amp[MAX(0, ((value - 1) + (voice.volume - 15)))];
}
}
// Note Envelope
const byte value = voice.note + voice.transpose + (voice.note_env.id ? playSequence(voice, voice.note_env) : 0);
const byte rel_note = value % 12;
const byte rel_octave = value / 12;
if (voice.waveform == NOISE) {
voice.freq = noisePeriods[rel_note];
} else {
voice.freq = scale[rel_note] >> (8 - ((voice.octave + rel_octave) % 8));
}
// Timbre Envelope
if (voice.timbre_env.id) {
const byte value = playSequence(voice, voice.timbre_env);
if (value) {
voice.pw = value;
}
}
// Pitch Envelope
// TODO...
}
void resetSequence(Envelope& env) {
if (env.id) {
env.i = 1;
env.loop_i = env.rel_i = 0;
}
}
inline void resetSequences(Voice& voice) {
resetSequence(voice.volume_env);
resetSequence(voice.note_env);
resetSequence(voice.timbre_env);
resetSequence(voice.pitch_env);
}
inline void executeCommand(Voice& voice, const byte cmd) {
switch (cmd) {
case TRACK_LOOP:
voice.track_loop_ptr = voice.ptr;
break;
case LOOP_START: {
const byte times = fetchNextByte(voice);
const byte i = ++voice.loops_idx;
voice.loops_ptr[i] = voice.ptr;
voice.loops_c[i] = times;
break;
}
case LOOP_END: {
const byte i = voice.loops_idx;
voice.loops_c[i]--;
if (voice.loops_c[i] == 0) {
voice.loops_idx--;
} else {
voice.ptr = voice.loops_ptr[i];
}
break;
}
case NOTE_LEN:
voice.nlen = fetchNextByte(voice);
break;
case NOTE_LEN_WORD:
voice.nlen = fetchNextByte(voice) | (fetchNextByte(voice) << 8);
break;
case QUANT_LEN:
voice.qlen = fetchNextByte(voice);
break;
case QUANT_LEN_WORD:
voice.qlen = fetchNextByte(voice) | (fetchNextByte(voice) << 8);
break;
case OCTAVE:
voice.octave = fetchNextByte(voice);;
break;
case INC_OCTAVE:
if (voice.octave < 8) voice.octave++;
break;
case DEC_OCTAVE:
if (voice.octave > 0) voice.octave--;
break;
case TRANSPOSE: {
voice.transpose = (signed char) fetchNextByte(voice);
break;
}
case DETUNE: {
// TODO
break;
}
case TIMBRE:
voice.pw = fetchNextByte(voice);
break;
case VOLUME:
voice.volume = fetchNextByte(voice);
break;
case INC_VOLUME:
if (voice.volume < 16) voice.volume++;
break;
case DEC_VOLUME:
if (voice.volume > 0) voice.volume--;
break;
case PITCH_SWEEP: {
// TODO
break;
}
// Select Envelope commands
case VOLUME_ENV:
voice.volume_env.id = fetchNextByte(voice);
break;
case NOTE_ENV:
voice.note_env.id = fetchNextByte(voice);
break;
case TIMBRE_ENV:
voice.timbre_env.id = fetchNextByte(voice);
break;
case PITCH_ENV:
voice.pitch_env.id = fetchNextByte(voice);
break;
}
}
void loop() {
Voice* v;
if (playing && nextTick) {
// debug voices via serial
if (ticks == 0) {
for (int i = 0; i < NUM_VOICES; i++) {
v = &voices[i];
if (i < FIRST_MANUAL_VOICE) {
serialDebug("v: %.1i ptr: %.3u | ", i, (unsigned int)(v->ptr - SongData[i]));
}
}
serialDebug("\n");
}
// play recorded song data
bool anyVoicePlaying = false;
for (int i = 0; i < NUM_VOICES; i++) {
anyVoicePlaying |= playVoice(voices[i]);
}
// after each voice finishes playing, restore the flag
nextTick = false;
// stop playback after all voices have finished playing
if (!anyVoicePlaying) {
for (int i = 0; i < NUM_VOICES; i++) {
v = &voices[i];
// the manual voice can continue playing after the song ends
if (i < FIRST_MANUAL_VOICE) {
v->amp = 0;
}
}
playing = false;
ticks = 0;
nextBigTick = false;
mustRestartSong = true;
serialDebug("END\n");
digitalWrite(PIN_LED, LOW);
}
}
// restart song when flag is raised
if (mustRestartSong && nextBigTick) {
nextBigTick = false;
mustRestartSong = false;
for (int i = 0; i < NUM_VOICES; i++) {
v = &voices[i];
// the manual voices are only played through midi
if (i < FIRST_MANUAL_VOICE) {
v->ptr = SongData[i];
v->finished = false;
v->playing = false;
v->track_loop_ptr = NULL;
v->loops_ptr[v->loops_idx] = v->ptr;
v->loops_idx = -1;
resetSequences(voices[i]);
}
}
lfsr = 1;
lfsrOut = 0;
playing = true;
serialDebug("START\n");
}
// midi usb playback using the param FIRST_MANUAL_VOICE
if (useMIDI) {
midiEventPacket_t rx;
do {
rx = MidiUSB.read();
if (rx.header == 0) continue;
const byte message = (rx.byte1 >> 4) & MASK_MESSAGE_CHANNEL;
// const byte channel = (rx.byte1 & MASK_MESSAGE_CHANNEL) + 1;
const byte midiNote = rx.byte2;
const byte velocity = rx.byte3;
if (message == MESSAGE_NOTE_ON) {
if (midiNote == pitchC1 || midiNote == pitchD1b || midiNote == pitchD1 || midiNote == pitchE1b || midiNote == pitchE1) continue;
// find first available manual voice, by cycling through all
// manual voices, defaulting on the last used voice
byte initialManualVoice = currentManualVoice;
do {
currentManualVoice++;
if (currentManualVoice == NUM_VOICES) currentManualVoice = FIRST_MANUAL_VOICE;
if (voices[currentManualVoice].amp == 0) break;
} while (initialManualVoice != currentManualVoice);
// after manual voice is found, play note in that manual voice
v = &voices[currentManualVoice];
v->octave = ((midiNote - midiNote % 12) / 12) - 1;
v->note = ((midiNote % 12) + 2) - 2;
v->volume = velocity / 8;
if (v->volume > 15) v->volume = 15;
v->amp = amp[v->volume];
v->gate = false;
v->freq = scale[v->note] >> (8 - (v->octave % 8));
serialDebug("midi note: %d", midiNote);
// serialDebug(" - v note: %d", v->note);
// serialDebug(" - v octave: %d", v->octave);
serialDebug(" - velocity: %d", velocity);
serialDebug("\n");
}
if (message == MESSAGE_NOTE_OFF) {
const uint8_t noteOffOctave = ((midiNote - midiNote % 12) / 12) - 1;
const uint8_t noteOffNote = ((midiNote % 12) + 2) - 2;
// cycle through all voices, if a voice is playing the note, turn it off
for (int i = FIRST_MANUAL_VOICE; i < NUM_VOICES; i++) {
v = &voices[i];
if (v->note == noteOffNote && v->octave == noteOffOctave) {
v->amp = 0;
}
}
// special note messages
if (midiNote == pitchC1) {
debugMode = !debugMode;
}
if (midiNote == pitchD1b) {
for (int i = FIRST_MANUAL_VOICE; i < NUM_VOICES; i++) {
voices[i].waveform = TRI;
voices[i].pw = 0x80;
}
}
if (midiNote == pitchD1) {
for (int i = FIRST_MANUAL_VOICE; i < NUM_VOICES; i++) {
voices[i].waveform = PULSE;
}
}
if (midiNote == pitchE1b) {
playing = !playing;
for (int i = 0; i < NUM_VOICES; i++) {
v = &voices[i];
v->amp = 0;
}
serialDebug(!playing ? "PAUSE\n" : "UNPAUSE\n");
digitalWrite(PIN_LED, LOW);
}
if (midiNote == pitchE1) {
nextBigTick = true;
serialDebug("RESTART\n");
mustRestartSong = true;
}
}
} while (rx.header != 0);
}
if (useButtonArray) {
int button;
for (int col = 0; col < NUM_COLS; col++) {
// put column high, read rows, put column low,
// and compute note on and note off commands
digitalWrite(COLS_PIN[col], HIGH);
for (int row = 0; row < NUM_ROWS; row++) {
button = NUM_ROWS * col + row;
prevButtonState[button] = buttonState[button];
buttonState[button] = digitalRead(ROWS_PIN[row]);
// if the button state changed, fire an event
if (buttonState[button] != prevButtonState[button]) {
serialDebug("%d ", REMAPPED_BUTTONS[button]);
const byte midiNote = REMAPPED_BUTTONS[button] + BUTTON_MIDI_BASE_NOTE;
const byte velocity = 127;
if (buttonState[button] == 1) {
// if the button is now pressed, play a note
serialDebug("on (midi note: %d)", midiNote);
// digitalWrite(PIN_LED, HIGH);
// find first available manual voice, by cycling through all
// manual voices, defaulting on the last used voice
byte initialManualVoice = currentManualVoice;
do {
currentManualVoice++;
if (currentManualVoice == NUM_VOICES) currentManualVoice = FIRST_MANUAL_VOICE;
if (voices[currentManualVoice].amp == 0) break;
} while (initialManualVoice != currentManualVoice);
// after manual voice is found, play note in that manual voice
v = &voices[currentManualVoice];
v->octave = ((midiNote - midiNote % 12) / 12) - 1;
v->note = ((midiNote % 12) + 2) - 2;
v->volume = velocity / 8;
if (v->volume > 15) v->volume = 15;
v->amp = amp[v->volume];
v->gate = false;
v->freq = scale[v->note] >> (8 - (v->octave % 8));
} else {
// if the button is now released, stop the note
serialDebug("off (midi note: %d)", midiNote);
// digitalWrite(PIN_LED, LOW);
// cycle through all voices, if a voice is playing the note, turn it off
const uint8_t noteOffOctave = ((midiNote - midiNote % 12) / 12) - 1;
const uint8_t noteOffNote = ((midiNote % 12) + 2) - 2;
for (int i = FIRST_MANUAL_VOICE; i < NUM_VOICES; i++) {
v = &voices[i];
if (v->note == noteOffNote && v->octave == noteOffOctave) {
v->amp = 0;
}
}
}
serialDebug("\n");
}
}
digitalWrite(COLS_PIN[col], LOW);
}
}
}