forked from Ar-zz-duboy/Arduboy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio.cpp
More file actions
332 lines (295 loc) · 10.2 KB
/
Copy pathaudio.cpp
File metadata and controls
332 lines (295 loc) · 10.2 KB
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
#include "Arduboy.h"
#include "audio.h"
const byte PROGMEM tune_pin_to_timer_PGM[] = { 3, 1 };
volatile byte *_tunes_timer1_pin_port;
volatile byte _tunes_timer1_pin_mask;
volatile int32_t timer1_toggle_count;
volatile byte *_tunes_timer3_pin_port;
volatile byte _tunes_timer3_pin_mask;
byte _tune_pins[AVAILABLE_TIMERS];
byte _tune_num_chans = 0;
volatile boolean tune_playing; // is the score still playing?
volatile unsigned wait_timer_frequency2; /* its current frequency */
volatile unsigned wait_timer_old_frequency2; /* its previous frequency */
volatile boolean wait_timer_playing = false; /* is it currently playing a note? */
volatile boolean doing_delay = false; /* are we using it for a tune_delay()? */
volatile boolean tonePlaying = false;
volatile unsigned long wait_toggle_count; /* countdown score waits */
volatile unsigned long delay_toggle_count; /* countdown tune_ delay() delays */
// pointers to your musical score and your position in said score
volatile const byte *score_start = 0;
volatile const byte *score_cursor = 0;
// Table of midi note frequencies * 2
// They are times 2 for greater accuracy, yet still fits in a word.
// Generated from Excel by =ROUND(2*440/32*(2^((x-9)/12)),0) for 0<x<128
// The lowest notes might not work, depending on the Arduino clock frequency
// Ref: http://www.phy.mtu.edu/~suits/notefreqs.html
const unsigned int PROGMEM _midi_note_frequencies[128] = {
16,17,18,19,21,22,23,24,26,28,29,31,33,35,37,39,41,44,46,49,52,55,58,62,65,
69,73,78,82,87,92,98,104,110,117,123,131,139,147,156,165,175,185,196,208,220,
233,247,262,277,294,311,330,349,370,392,415,440,466,494,523,554,587,622,659,
698,740,784,831,880,932,988,1047,1109,1175,1245,1319,1397,1480,1568,1661,1760,
1865,1976,2093,2217,2349,2489,2637,2794,2960,3136,3322,3520,3729,3951,4186,
4435,4699,4978,5274,5588,5920,6272,6645,7040,7459,7902,8372,8870,9397,9956,
10548,11175,11840,12544,13290,14080,14917,15804,16744,17740,18795,19912,21096,
22351,23680,25088 };
/* AUDIO */
void ArduboyAudio::on() {
power_timer1_enable();
power_timer3_enable();
audio_enabled = true;
}
bool ArduboyAudio::enabled() {
return audio_enabled;
}
void ArduboyAudio::off() {
audio_enabled = false;
power_timer1_disable();
power_timer3_disable();
}
void ArduboyAudio::save_on_off() {
EEPROM.write(EEPROM_AUDIO_ON_OFF, audio_enabled);
}
void ArduboyAudio::setup() {
tune_playing = false;
if (EEPROM.read(EEPROM_AUDIO_ON_OFF))
on();
}
void ArduboyAudio::tone(uint8_t channel, unsigned int frequency, unsigned long duration)
{
// if (audio_enabled)
// ::tone(channel, frequency, duration);
}
/* TUNES */
void ArduboyTunes::initChannel(byte pin) {
byte timer_num;
// we are all out of timers
if (_tune_num_chans == AVAILABLE_TIMERS)
return;
timer_num = pgm_read_byte(tune_pin_to_timer_PGM + _tune_num_chans);
_tune_pins[_tune_num_chans] = pin;
_tune_num_chans++;
pinMode(pin, OUTPUT);
switch (timer_num) {
case 1: // 16 bit timer
TCCR1A = 0;
TCCR1B = 0;
bitWrite(TCCR1B, WGM12, 1);
bitWrite(TCCR1B, CS10, 1);
_tunes_timer1_pin_port = portOutputRegister(digitalPinToPort(pin));
_tunes_timer1_pin_mask = digitalPinToBitMask(pin);
break;
case 3: // 16 bit timer
TCCR3A = 0;
TCCR3B = 0;
bitWrite(TCCR3B, WGM32, 1);
bitWrite(TCCR3B, CS30, 1);
_tunes_timer3_pin_port = portOutputRegister(digitalPinToPort(pin));
_tunes_timer3_pin_mask = digitalPinToBitMask(pin);
playNote(0, 60); /* start and stop channel 0 (timer 3) on middle C so wait/delay works */
stopNote(0);
break;
}
}
void ArduboyTunes::playNote(byte chan, byte note) {
byte timer_num;
byte prescalar_bits;
unsigned int frequency2; /* frequency times 2 */
unsigned long ocr;
// we can't plan on a channel that does not exist
if (chan >= _tune_num_chans)
return;
// we only have frequencies for 128 notes
if (note > 127)
note = 127;
timer_num = pgm_read_byte(tune_pin_to_timer_PGM + chan);
frequency2 = pgm_read_word(_midi_note_frequencies + note);
//****** 16-bit timer *********
// two choices for the 16 bit timers: ck/1 or ck/64
ocr = F_CPU / frequency2 - 1;
prescalar_bits = 0b001;
if (ocr > 0xffff) {
ocr = F_CPU / frequency2 / 64 - 1;
prescalar_bits = 0b011;
}
// Set the OCR for the given timer, then turn on the interrupts
switch (timer_num) {
case 1:
TCCR1B = (TCCR1B & 0b11111000) | prescalar_bits;
OCR1A = ocr;
bitWrite(TIMSK1, OCIE1A, 1);
break;
case 3:
TCCR3B = (TCCR3B & 0b11111000) | prescalar_bits;
OCR3A = ocr;
wait_timer_frequency2 = frequency2; // for "tune_delay" function
wait_timer_playing = true;
bitWrite(TIMSK3, OCIE3A, 1);
break;
}
}
void ArduboyTunes::stopNote(byte chan) {
byte timer_num;
timer_num = pgm_read_byte(tune_pin_to_timer_PGM + chan);
switch (timer_num) {
case 1:
TIMSK1 &= ~(1 << OCIE1A); // disable the interrupt
*_tunes_timer1_pin_port &= ~(_tunes_timer1_pin_mask); // keep pin low after stop
break;
case 3:
wait_timer_playing = false;
*_tunes_timer3_pin_port &= ~(_tunes_timer3_pin_mask); // keep pin low after stop
break;
}
}
void ArduboyTunes::playScore(const byte *score) {
score_start = score;
score_cursor = score_start;
step(); /* execute initial commands */
tune_playing = true; /* release the interrupt routine */
}
void ArduboyTunes::stopScore (void) {
for (uint8_t i = 0; i < _tune_num_chans; i++)
stopNote(i);
tune_playing = false;
}
bool ArduboyTunes::playing()
{
return tune_playing;
}
/* Do score commands until a "wait" is found, or the score is stopped.
This is called initially from tune_playcore, but then is called
from the interrupt routine when waits expire.
*/
/* if CMD < 0x80, then the other 7 bits and the next byte are a 15-bit big-endian number of msec to wait */
void ArduboyTunes::step() {
byte command, opcode, chan;
unsigned duration;
while (1) {
command = pgm_read_byte(score_cursor++);
opcode = command & 0xf0;
chan = command & 0x0f;
if (opcode == TUNE_OP_STOPNOTE) { /* stop note */
stopNote(chan);
}
else if (opcode == TUNE_OP_PLAYNOTE) { /* play note */
playNote(chan, pgm_read_byte(score_cursor++));
}
else if (opcode == TUNE_OP_RESTART) { /* restart score */
score_cursor = score_start;
}
else if (opcode == TUNE_OP_STOP) { /* stop score */
tune_playing = false;
break;
}
else if (opcode < 0x80) { /* wait count in msec. */
duration = ((unsigned)command << 8) | (pgm_read_byte(score_cursor++));
wait_toggle_count = ((unsigned long) wait_timer_frequency2 * duration + 500) / 1000;
if (wait_toggle_count == 0) wait_toggle_count = 1;
break;
}
}
}
void ArduboyTunes::delay (unsigned duration) {
boolean notdone;
noInterrupts();
delay_toggle_count = ((unsigned long) wait_timer_frequency2 * duration + 500) / 1000;
doing_delay = true;
interrupts();
do { // wait until the interrupt routines decrements the toggle count to zero
noInterrupts();
notdone = delay_toggle_count != 0; /* interrupt-safe test */
interrupts();
}
while (notdone);
doing_delay = false;
}
void ArduboyTunes::closeChannels(void) {
byte timer_num;
for (uint8_t chan=0; chan < _tune_num_chans; chan++) {
timer_num = pgm_read_byte(tune_pin_to_timer_PGM + chan);
switch (timer_num) {
case 1:
TIMSK1 &= ~(1 << OCIE1A);
break;
case 3:
TIMSK3 &= ~(1 << OCIE3A);
break;
}
digitalWrite(_tune_pins[chan], 0);
}
_tune_num_chans = 0;
tune_playing = false;
}
void ArduboyTunes::soundOutput()
{
if (wait_timer_playing) { // toggle the pin if we're sounding a note
*_tunes_timer3_pin_port ^= _tunes_timer3_pin_mask;
}
if (tune_playing && wait_toggle_count && --wait_toggle_count == 0) {
// end of a score wait, so execute more score commands
wait_timer_old_frequency2 = wait_timer_frequency2; // save this timer's frequency
ArduboyTunes::step(); // execute commands
// If this timer's frequency has changed and we're using it for a tune_delay(),
// recompute the number of toggles to wait for
if (doing_delay && wait_timer_old_frequency2 != wait_timer_frequency2) {
if (delay_toggle_count >= 0x20000UL && wait_timer_frequency2 >= 0x4000U) {
// Need to avoid 32-bit overflow...
delay_toggle_count = ( (delay_toggle_count+4>>3) * (wait_timer_frequency2+2>>2) / wait_timer_old_frequency2 )<<5;
}
else {
delay_toggle_count = delay_toggle_count * wait_timer_frequency2 / wait_timer_old_frequency2;
}
}
}
if (doing_delay && delay_toggle_count) --delay_toggle_count; // countdown for tune_delay()
}
void ArduboyTunes::tone(unsigned int frequency, unsigned long duration) {
tonePlaying = true;
uint8_t prescalarbits = 0b001;
int32_t toggle_count = 0;
uint32_t ocr = 0;
// two choices for the 16 bit timers: ck/1 or ck/64
ocr = F_CPU / frequency / 2 - 1;
prescalarbits = 0b001;
if (ocr > 0xffff) {
ocr = F_CPU / frequency / 2 / 64 - 1;
prescalarbits = 0b011;
}
TCCR1B = (TCCR1B & 0b11111000) | prescalarbits;
// Calculate the toggle count
if (duration > 0) {
toggle_count = 2 * frequency * duration / 1000;
}
else {
toggle_count = -1;
}
// Set the OCR for the given timer,
// set the toggle count,
// then turn on the interrupts
OCR1A = ocr;
timer1_toggle_count = toggle_count;
bitWrite(TIMSK1, OCIE1A, 1);
}
ISR(TIMER1_COMPA_vect) { // TIMER 1
if (tonePlaying) {
if (timer1_toggle_count != 0) {
// toggle the pin
*_tunes_timer1_pin_port ^= _tunes_timer1_pin_mask;
if (timer1_toggle_count > 0) timer1_toggle_count--;
}
else {
tonePlaying = false;
TIMSK1 &= ~(1 << OCIE1A); // disable the interrupt
*_tunes_timer1_pin_port &= ~(_tunes_timer1_pin_mask); // keep pin low after stop
}
}
else {
*_tunes_timer1_pin_port ^= _tunes_timer1_pin_mask; // toggle the pin
}
}
ISR(TIMER3_COMPA_vect) { // TIMER 3
// Timer 3 is the one assigned first, so we keep it running always
// and use it to time score waits, whether or not it is playing a note.
ArduboyTunes::soundOutput();
}