-
Notifications
You must be signed in to change notification settings - Fork 2
/
picopico.ino
486 lines (424 loc) · 14.1 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
/*
* 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 <avr/sleep.h>
#include "player.h"
#include "tune.h"
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#define NUM_VOICES 4
#define DEFAULT_OCTAVE 4
#define DEFAULT_NLEN 16
#define DEFAULT_VOL 15
#define DEFAULT_PW 0x80
// Note buffer
volatile uint16_t lfsr = 1;
volatile char lfsrOut = 0;
volatile signed char oldTemp = 0; // FIXME change variable name
// Global tick counter
volatile unsigned int ticks = 0;
volatile bool nextTick = false;
Voice voices[NUM_VOICES] = {};
byte playingVoices = 0;
bool playing = false, wantsToStop = false;
volatile bool justAwoke = false;
// External interrupt 0
ISR(INT0_vect) {
GIMSK = 0; // Disable interrupt because this routine may fire multiple times
// while pin is held low (button pressed)
justAwoke = true;
}
// Watchdog interrupt counts ticks (every 16ms)
ISR(WDT_vect) {
WDTCR |= 1<<WDIE;
ticks++;
nextTick = true;
}
ISR(TIMER0_COMPA_vect) {
unsigned char temp;
signed char stemp, mask, out = 0;
Voice* v;
// Voice 1 and 2: Pulses
for (int c = 0; c < 2; c++) {
v = &voices[c];
v->acc += v->freq;
temp = (v->acc >> 8) & v->pw;
out += (temp ? v->amp : 0) >> 2;
}
// Voice 3: Triangle
v = &voices[2];
v->acc += v->freq;
stemp = v->acc >> 8;
mask = stemp >> 7;
if (v->amp != 0) out += (stemp ^ mask) >> 1;
// Voice 4: Noise
//
// 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.
//
v = &voices[3];
v->acc += v->freq;
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;
OCR1B = out;
}
// Setup **********************************************
void setup() {
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
// Set all pins as input and enable internal pull-up resistor
for (int i = 0; i <= 3; i++) {
pinMode(i, INPUT_PULLUP);
}
// Enable 64 MHz PLL and use as source for Timer1
PLLCSR = 1<<PCKE | 1<<PLLE;
// 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
// Set up Timer/Counter0 for 20kHz interrupt to output samples.
TCCR0A = 3<<WGM00; // Fast PWM
TCCR0B = 1<<WGM02 | 2<<CS00; // 1/8 prescale
OCR0A = 49; // Divide by 400
GIMSK = 0; // Disable INT0
}
void goToSleep(void) {
byte adcsra, mcucr1, mcucr2, wdtcr;
WDTCR = 0; // Disable Watchdog timer
TIMSK = 0; // Disable all timers
sleep_enable();
adcsra = ADCSRA; // Save ADCSRA
ADCSRA &= ~_BV(ADEN); // Disable ADC
cli(); // Stop interrupts to ensure the BOD timed sequence executes as required
GIMSK = _BV(INT0); // Enable INT0
mcucr1 = MCUCR | _BV(BODS) | _BV(BODSE); // Turn off the brown-out detector
mcucr2 = mcucr1 & ~_BV(BODSE); // If the MCU does not have BOD disable capability,
MCUCR = mcucr1; // this code has no effect
MCUCR = mcucr2;
sei(); // Ensure interrupts enabled so we can wake up again
sleep_cpu(); // Go to sleep
// Now asleep ...
// ... awake again
sleep_disable(); // Wake up here
ADCSRA = adcsra; // Restore ADCSRA
PLLCSR = 1<<PCKE | 1<<PLLE; // Re-enable PLL (for some reason, this is needed after sleeping...)
TIMSK = 1<<OCIE0A; // Enable timer compare match, disable overflow
WDTCR = 1<<WDIE; // Enable Watchdog timer for 128Hz interrupt
}
// 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;
}
byte fetchNextByte(Voice& voice) {
return pgm_read_byte(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 != REST) {
voice.note = (note & 0xf) - 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 const byte fetchSeqValue(const Envelope& env) {
return pgm_read_byte(Seqs[env.id - 1] + env.i - 1);
}
const 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 char rel_note = value % 12;
const char 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() {
// Check if button is pressed for stoping song, taking care of debouncing
byte buttonPressed = !digitalRead(2);
if (buttonPressed) {
if (!justAwoke) {
// Song was playing, user is pressing button. Prepare for stop
wantsToStop = true;
}
} else {
if (justAwoke) {
// User left button after waking up.
justAwoke = false;
} else if (wantsToStop) {
// User was pressing button while song was playing,
// now stopped pressing. Stop playing.
playing = false;
wantsToStop = false;
}
}
// If we are not playing, go to sleep.
// After waking up, reset state.
if (!playing) {
goToSleep();
playing = true;
for (int i = 0; i < NUM_VOICES; i++) {
Voice* v = &voices[i];
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[0].waveform = PULSE;
voices[1].waveform = PULSE;
voices[2].waveform = TRI;
voices[3].waveform = NOISE;
lfsrOut = 0;
return;
}
if (nextTick) {
nextTick = false;
bool anyVoicePlaying = false;
for (int i = 0; i < NUM_VOICES; i++) {
anyVoicePlaying |= playVoice(voices[i]);
}
if (!anyVoicePlaying) playing = false;
}
}