aboutsummaryrefslogtreecommitdiff
path: root/quantum/audio/audio_pwm.c
diff options
context:
space:
mode:
Diffstat (limited to 'quantum/audio/audio_pwm.c')
-rw-r--r--quantum/audio/audio_pwm.c637
1 files changed, 287 insertions, 350 deletions
diff --git a/quantum/audio/audio_pwm.c b/quantum/audio/audio_pwm.c
index ed6713609..545aef6dd 100644
--- a/quantum/audio/audio_pwm.c
+++ b/quantum/audio/audio_pwm.c
@@ -29,7 +29,6 @@
29 29
30#define CPU_PRESCALER 8 30#define CPU_PRESCALER 8
31 31
32
33// Timer Abstractions 32// Timer Abstractions
34 33
35// TIMSK3 - Timer/Counter #3 Interrupt Mask Register 34// TIMSK3 - Timer/Counter #3 Interrupt Mask Register
@@ -37,70 +36,67 @@
37#define ENABLE_AUDIO_COUNTER_3_ISR TIMSK3 |= _BV(OCIE3A) 36#define ENABLE_AUDIO_COUNTER_3_ISR TIMSK3 |= _BV(OCIE3A)
38#define DISABLE_AUDIO_COUNTER_3_ISR TIMSK3 &= ~_BV(OCIE3A) 37#define DISABLE_AUDIO_COUNTER_3_ISR TIMSK3 &= ~_BV(OCIE3A)
39 38
40
41// TCCR3A: Timer/Counter #3 Control Register 39// TCCR3A: Timer/Counter #3 Control Register
42// Compare Output Mode (COM3An) = 0b00 = Normal port operation, OC3A disconnected from PC6 40// Compare Output Mode (COM3An) = 0b00 = Normal port operation, OC3A disconnected from PC6
43#define ENABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A |= _BV(COM3A1); 41#define ENABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A |= _BV(COM3A1);
44#define DISABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A &= ~(_BV(COM3A1) | _BV(COM3A0)); 42#define DISABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A &= ~(_BV(COM3A1) | _BV(COM3A0));
45 43
46
47#define NOTE_PERIOD ICR3 44#define NOTE_PERIOD ICR3
48#define NOTE_DUTY_CYCLE OCR3A 45#define NOTE_DUTY_CYCLE OCR3A
49 46
50
51#ifdef PWM_AUDIO 47#ifdef PWM_AUDIO
52 #include "wave.h" 48# include "wave.h"
53 #define SAMPLE_DIVIDER 39 49# define SAMPLE_DIVIDER 39
54 #define SAMPLE_RATE (2000000.0/SAMPLE_DIVIDER/2048) 50# define SAMPLE_RATE (2000000.0 / SAMPLE_DIVIDER / 2048)
55 // Resistor value of 1/ (2 * PI * 10nF * (2000000 hertz / SAMPLE_DIVIDER / 10)) for 10nF cap 51// Resistor value of 1/ (2 * PI * 10nF * (2000000 hertz / SAMPLE_DIVIDER / 10)) for 10nF cap
56 52
57 float places[8] = {0, 0, 0, 0, 0, 0, 0, 0}; 53float places[8] = {0, 0, 0, 0, 0, 0, 0, 0};
58 uint16_t place_int = 0; 54uint16_t place_int = 0;
59 bool repeat = true; 55bool repeat = true;
60#endif 56#endif
61 57
62void delay_us(int count) { 58void delay_us(int count) {
63 while(count--) { 59 while (count--) {
64 _delay_us(1); 60 _delay_us(1);
65 } 61 }
66} 62}
67 63
68int voices = 0; 64int voices = 0;
69int voice_place = 0; 65int voice_place = 0;
70float frequency = 0; 66float frequency = 0;
71int volume = 0; 67int volume = 0;
72long position = 0; 68long position = 0;
73 69
74float frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0}; 70float frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
75int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0}; 71int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
76bool sliding = false; 72bool sliding = false;
77 73
78float place = 0; 74float place = 0;
79 75
80uint8_t * sample; 76uint8_t* sample;
81uint16_t sample_length = 0; 77uint16_t sample_length = 0;
82// float freq = 0; 78// float freq = 0;
83 79
84bool playing_notes = false; 80bool playing_notes = false;
85bool playing_note = false; 81bool playing_note = false;
86float note_frequency = 0; 82float note_frequency = 0;
87float note_length = 0; 83float note_length = 0;
88uint8_t note_tempo = TEMPO_DEFAULT; 84uint8_t note_tempo = TEMPO_DEFAULT;
89float note_timbre = TIMBRE_DEFAULT; 85float note_timbre = TIMBRE_DEFAULT;
90uint16_t note_position = 0; 86uint16_t note_position = 0;
91float (* notes_pointer)[][2]; 87float (*notes_pointer)[][2];
92uint16_t notes_count; 88uint16_t notes_count;
93bool notes_repeat; 89bool notes_repeat;
94float notes_rest; 90float notes_rest;
95bool note_resting = false; 91bool note_resting = false;
96 92
97uint16_t current_note = 0; 93uint16_t current_note = 0;
98uint8_t rest_counter = 0; 94uint8_t rest_counter = 0;
99 95
100#ifdef VIBRATO_ENABLE 96#ifdef VIBRATO_ENABLE
101float vibrato_counter = 0; 97float vibrato_counter = 0;
102float vibrato_strength = .5; 98float vibrato_strength = .5;
103float vibrato_rate = 0.125; 99float vibrato_rate = 0.125;
104#endif 100#endif
105 101
106float polyphony_rate = 0; 102float polyphony_rate = 0;
@@ -112,50 +108,49 @@ audio_config_t audio_config;
112uint16_t envelope_index = 0; 108uint16_t envelope_index = 0;
113 109
114void audio_init() { 110void audio_init() {
115
116 // Check EEPROM 111 // Check EEPROM
117 if (!eeconfig_is_enabled()) 112 if (!eeconfig_is_enabled()) {
118 {
119 eeconfig_init(); 113 eeconfig_init();
120 } 114 }
121 audio_config.raw = eeconfig_read_audio(); 115 audio_config.raw = eeconfig_read_audio();
122 116
123 #ifdef PWM_AUDIO 117#ifdef PWM_AUDIO
124 118
125 PLLFRQ = _BV(PDIV2); 119 PLLFRQ = _BV(PDIV2);
126 PLLCSR = _BV(PLLE); 120 PLLCSR = _BV(PLLE);
127 while(!(PLLCSR & _BV(PLOCK))); 121 while (!(PLLCSR & _BV(PLOCK)))
128 PLLFRQ |= _BV(PLLTM0); /* PCK 48MHz */ 122 ;
123 PLLFRQ |= _BV(PLLTM0); /* PCK 48MHz */
129 124
130 /* Init a fast PWM on Timer4 */ 125 /* Init a fast PWM on Timer4 */
131 TCCR4A = _BV(COM4A0) | _BV(PWM4A); /* Clear OC4A on Compare Match */ 126 TCCR4A = _BV(COM4A0) | _BV(PWM4A); /* Clear OC4A on Compare Match */
132 TCCR4B = _BV(CS40); /* No prescaling => f = PCK/256 = 187500Hz */ 127 TCCR4B = _BV(CS40); /* No prescaling => f = PCK/256 = 187500Hz */
133 OCR4A = 0; 128 OCR4A = 0;
134 129
135 /* Enable the OC4A output */ 130 /* Enable the OC4A output */
136 DDRC |= _BV(PORTC6); 131 DDRC |= _BV(PORTC6);
137 132
138 DISABLE_AUDIO_COUNTER_3_ISR; // Turn off 3A interputs 133 DISABLE_AUDIO_COUNTER_3_ISR; // Turn off 3A interputs
139 134
140 TCCR3A = 0x0; // Options not needed 135 TCCR3A = 0x0; // Options not needed
141 TCCR3B = _BV(CS31) | _BV(CS30) | _BV(WGM32); // 64th prescaling and CTC 136 TCCR3B = _BV(CS31) | _BV(CS30) | _BV(WGM32); // 64th prescaling and CTC
142 OCR3A = SAMPLE_DIVIDER - 1; // Correct count/compare, related to sample playback 137 OCR3A = SAMPLE_DIVIDER - 1; // Correct count/compare, related to sample playback
143 138
144 #else 139#else
145 140
146 // Set port PC6 (OC3A and /OC4A) as output 141 // Set port PC6 (OC3A and /OC4A) as output
147 DDRC |= _BV(PORTC6); 142 DDRC |= _BV(PORTC6);
148 143
149 DISABLE_AUDIO_COUNTER_3_ISR; 144 DISABLE_AUDIO_COUNTER_3_ISR;
150 145
151 // TCCR3A / TCCR3B: Timer/Counter #3 Control Registers 146 // TCCR3A / TCCR3B: Timer/Counter #3 Control Registers
152 // Compare Output Mode (COM3An) = 0b00 = Normal port operation, OC3A disconnected from PC6 147 // Compare Output Mode (COM3An) = 0b00 = Normal port operation, OC3A disconnected from PC6
153 // Waveform Generation Mode (WGM3n) = 0b1110 = Fast PWM Mode 14 (Period = ICR3, Duty Cycle = OCR3A) 148 // Waveform Generation Mode (WGM3n) = 0b1110 = Fast PWM Mode 14 (Period = ICR3, Duty Cycle = OCR3A)
154 // Clock Select (CS3n) = 0b010 = Clock / 8 149 // Clock Select (CS3n) = 0b010 = Clock / 8
155 TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30); 150 TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
156 TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30); 151 TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
157 152
158 #endif 153#endif
159 154
160 audio_initialized = true; 155 audio_initialized = true;
161} 156}
@@ -165,62 +160,59 @@ void stop_all_notes() {
165 audio_init(); 160 audio_init();
166 } 161 }
167 voices = 0; 162 voices = 0;
168 #ifdef PWM_AUDIO 163#ifdef PWM_AUDIO
169 DISABLE_AUDIO_COUNTER_3_ISR; 164 DISABLE_AUDIO_COUNTER_3_ISR;
170 #else 165#else
171 DISABLE_AUDIO_COUNTER_3_ISR; 166 DISABLE_AUDIO_COUNTER_3_ISR;
172 DISABLE_AUDIO_COUNTER_3_OUTPUT; 167 DISABLE_AUDIO_COUNTER_3_OUTPUT;
173 #endif 168#endif
174 169
175 playing_notes = false; 170 playing_notes = false;
176 playing_note = false; 171 playing_note = false;
177 frequency = 0; 172 frequency = 0;
178 volume = 0; 173 volume = 0;
179 174
180 for (uint8_t i = 0; i < 8; i++) 175 for (uint8_t i = 0; i < 8; i++) {
181 {
182 frequencies[i] = 0; 176 frequencies[i] = 0;
183 volumes[i] = 0; 177 volumes[i] = 0;
184 } 178 }
185} 179}
186 180
187void stop_note(float freq) 181void stop_note(float freq) {
188{
189 if (playing_note) { 182 if (playing_note) {
190 if (!audio_initialized) { 183 if (!audio_initialized) {
191 audio_init(); 184 audio_init();
192 } 185 }
193 #ifdef PWM_AUDIO 186#ifdef PWM_AUDIO
194 freq = freq / SAMPLE_RATE; 187 freq = freq / SAMPLE_RATE;
195 #endif 188#endif
196 for (int i = 7; i >= 0; i--) { 189 for (int i = 7; i >= 0; i--) {
197 if (frequencies[i] == freq) { 190 if (frequencies[i] == freq) {
198 frequencies[i] = 0; 191 frequencies[i] = 0;
199 volumes[i] = 0; 192 volumes[i] = 0;
200 for (int j = i; (j < 7); j++) { 193 for (int j = i; (j < 7); j++) {
201 frequencies[j] = frequencies[j+1]; 194 frequencies[j] = frequencies[j + 1];
202 frequencies[j+1] = 0; 195 frequencies[j + 1] = 0;
203 volumes[j] = volumes[j+1]; 196 volumes[j] = volumes[j + 1];
204 volumes[j+1] = 0; 197 volumes[j + 1] = 0;
205 } 198 }
206 break; 199 break;
207 } 200 }
208 } 201 }
209 voices--; 202 voices--;
210 if (voices < 0) 203 if (voices < 0) voices = 0;
211 voices = 0;
212 if (voice_place >= voices) { 204 if (voice_place >= voices) {
213 voice_place = 0; 205 voice_place = 0;
214 } 206 }
215 if (voices == 0) { 207 if (voices == 0) {
216 #ifdef PWM_AUDIO 208#ifdef PWM_AUDIO
217 DISABLE_AUDIO_COUNTER_3_ISR; 209 DISABLE_AUDIO_COUNTER_3_ISR;
218 #else 210#else
219 DISABLE_AUDIO_COUNTER_3_ISR; 211 DISABLE_AUDIO_COUNTER_3_ISR;
220 DISABLE_AUDIO_COUNTER_3_OUTPUT; 212 DISABLE_AUDIO_COUNTER_3_OUTPUT;
221 #endif 213#endif
222 frequency = 0; 214 frequency = 0;
223 volume = 0; 215 volume = 0;
224 playing_note = false; 216 playing_note = false;
225 } 217 }
226 } 218 }
@@ -228,126 +220,120 @@ void stop_note(float freq)
228 220
229#ifdef VIBRATO_ENABLE 221#ifdef VIBRATO_ENABLE
230 222
231float mod(float a, int b) 223float mod(float a, int b) {
232{
233 float r = fmod(a, b); 224 float r = fmod(a, b);
234 return r < 0 ? r + b : r; 225 return r < 0 ? r + b : r;
235} 226}
236 227
237float vibrato(float average_freq) { 228float vibrato(float average_freq) {
238 #ifdef VIBRATO_STRENGTH_ENABLE 229# ifdef VIBRATO_STRENGTH_ENABLE
239 float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength); 230 float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
240 #else 231# else
241 float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter]; 232 float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
242 #endif 233# endif
243 vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH); 234 vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0 / average_freq)), VIBRATO_LUT_LENGTH);
244 return vibrated_freq; 235 return vibrated_freq;
245} 236}
246 237
247#endif 238#endif
248 239
249ISR(TIMER3_COMPA_vect) 240ISR(TIMER3_COMPA_vect) {
250{
251 if (playing_note) { 241 if (playing_note) {
252 #ifdef PWM_AUDIO 242#ifdef PWM_AUDIO
253 if (voices == 1) { 243 if (voices == 1) {
244 // SINE
245 OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 2;
246
247 // SQUARE
248 // if (((int)place) >= 1024){
249 // OCR4A = 0xFF >> 2;
250 // } else {
251 // OCR4A = 0x00;
252 // }
253
254 // SAWTOOTH
255 // OCR4A = (int)place / 4;
256
257 // TRIANGLE
258 // if (((int)place) >= 1024) {
259 // OCR4A = (int)place / 2;
260 // } else {
261 // OCR4A = 2048 - (int)place / 2;
262 // }
263
264 place += frequency;
265
266 if (place >= SINE_LENGTH) place -= SINE_LENGTH;
267
268 } else {
269 int sum = 0;
270 for (int i = 0; i < voices; i++) {
254 // SINE 271 // SINE
255 OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 2; 272 sum += pgm_read_byte(&sinewave[(uint16_t)places[i]]) >> 2;
256 273
257 // SQUARE 274 // SQUARE
258 // if (((int)place) >= 1024){ 275 // if (((int)places[i]) >= 1024){
259 // OCR4A = 0xFF >> 2; 276 // sum += 0xFF >> 2;
260 // } else { 277 // } else {
261 // OCR4A = 0x00; 278 // sum += 0x00;
262 // } 279 // }
263 280
264 // SAWTOOTH 281 places[i] += frequencies[i];
265 // OCR4A = (int)place / 4;
266
267 // TRIANGLE
268 // if (((int)place) >= 1024) {
269 // OCR4A = (int)place / 2;
270 // } else {
271 // OCR4A = 2048 - (int)place / 2;
272 // }
273 282
274 place += frequency; 283 if (places[i] >= SINE_LENGTH) places[i] -= SINE_LENGTH;
275
276 if (place >= SINE_LENGTH)
277 place -= SINE_LENGTH;
278
279 } else {
280 int sum = 0;
281 for (int i = 0; i < voices; i++) {
282 // SINE
283 sum += pgm_read_byte(&sinewave[(uint16_t)places[i]]) >> 2;
284
285 // SQUARE
286 // if (((int)places[i]) >= 1024){
287 // sum += 0xFF >> 2;
288 // } else {
289 // sum += 0x00;
290 // }
291
292 places[i] += frequencies[i];
293
294 if (places[i] >= SINE_LENGTH)
295 places[i] -= SINE_LENGTH;
296 }
297 OCR4A = sum;
298 } 284 }
299 #else 285 OCR4A = sum;
300 if (voices > 0) { 286 }
301 float freq; 287#else
302 if (polyphony_rate > 0) { 288 if (voices > 0) {
303 if (voices > 1) { 289 float freq;
304 voice_place %= voices; 290 if (polyphony_rate > 0) {
305 if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) { 291 if (voices > 1) {
306 voice_place = (voice_place + 1) % voices; 292 voice_place %= voices;
307 place = 0.0; 293 if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
308 } 294 voice_place = (voice_place + 1) % voices;
309 } 295 place = 0.0;
310 #ifdef VIBRATO_ENABLE
311 if (vibrato_strength > 0) {
312 freq = vibrato(frequencies[voice_place]);
313 } else {
314 #else
315 {
316 #endif
317 freq = frequencies[voice_place];
318 } 296 }
297 }
298# ifdef VIBRATO_ENABLE
299 if (vibrato_strength > 0) {
300 freq = vibrato(frequencies[voice_place]);
319 } else { 301 } else {
320 if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) { 302# else
321 frequency = frequency * pow(2, 440/frequency/12/2); 303 {
322 } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) { 304# endif
323 frequency = frequency * pow(2, -440/frequency/12/2); 305 freq = frequencies[voice_place];
324 } else { 306 }
325 frequency = frequencies[voices - 1]; 307 } else {
326 } 308 if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440 / frequencies[voices - 1] / 12 / 2)) {
327 309 frequency = frequency * pow(2, 440 / frequency / 12 / 2);
328 310 } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440 / frequencies[voices - 1] / 12 / 2)) {
329 #ifdef VIBRATO_ENABLE 311 frequency = frequency * pow(2, -440 / frequency / 12 / 2);
330 if (vibrato_strength > 0) { 312 } else {
331 freq = vibrato(frequency); 313 frequency = frequencies[voices - 1];
332 } else {
333 #else
334 {
335 #endif
336 freq = frequency;
337 }
338 } 314 }
339 315
340 if (envelope_index < 65535) { 316# ifdef VIBRATO_ENABLE
341 envelope_index++; 317 if (vibrato_strength > 0) {
318 freq = vibrato(frequency);
319 } else {
320# else
321 {
322# endif
323 freq = frequency;
342 } 324 }
343 freq = voice_envelope(freq); 325 }
344 326
345 if (freq < 30.517578125) 327 if (envelope_index < 65535) {
346 freq = 30.52; 328 envelope_index++;
347 NOTE_PERIOD = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period
348 NOTE_DUTY_CYCLE = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
349 } 329 }
350 #endif 330 freq = voice_envelope(freq);
331
332 if (freq < 30.517578125) freq = 30.52;
333 NOTE_PERIOD = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period
334 NOTE_DUTY_CYCLE = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
335 }
336#endif
351 } 337 }
352 338
353 // SAMPLE 339 // SAMPLE
@@ -361,41 +347,38 @@ ISR(TIMER3_COMPA_vect)
361 // else 347 // else
362 // DISABLE_AUDIO_COUNTER_3_ISR; 348 // DISABLE_AUDIO_COUNTER_3_ISR;
363 349
364
365 if (playing_notes) { 350 if (playing_notes) {
366 #ifdef PWM_AUDIO 351#ifdef PWM_AUDIO
367 OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 0; 352 OCR4A = pgm_read_byte(&sinewave[(uint16_t)place]) >> 0;
368
369 place += note_frequency;
370 if (place >= SINE_LENGTH)
371 place -= SINE_LENGTH;
372 #else
373 if (note_frequency > 0) {
374 float freq;
375
376 #ifdef VIBRATO_ENABLE
377 if (vibrato_strength > 0) {
378 freq = vibrato(note_frequency);
379 } else {
380 #else
381 {
382 #endif
383 freq = note_frequency;
384 }
385 353
386 if (envelope_index < 65535) { 354 place += note_frequency;
387 envelope_index++; 355 if (place >= SINE_LENGTH) place -= SINE_LENGTH;
388 } 356#else
389 freq = voice_envelope(freq); 357 if (note_frequency > 0) {
358 float freq;
390 359
391 NOTE_PERIOD = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period 360# ifdef VIBRATO_ENABLE
392 NOTE_DUTY_CYCLE = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period 361 if (vibrato_strength > 0) {
362 freq = vibrato(note_frequency);
393 } else { 363 } else {
394 NOTE_PERIOD = 0; 364# else
395 NOTE_DUTY_CYCLE = 0; 365 {
366# endif
367 freq = note_frequency;
396 } 368 }
397 #endif
398 369
370 if (envelope_index < 65535) {
371 envelope_index++;
372 }
373 freq = voice_envelope(freq);
374
375 NOTE_PERIOD = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period
376 NOTE_DUTY_CYCLE = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period
377 } else {
378 NOTE_PERIOD = 0;
379 NOTE_DUTY_CYCLE = 0;
380 }
381#endif
399 382
400 note_position++; 383 note_position++;
401 bool end_of_note = false; 384 bool end_of_note = false;
@@ -409,126 +392,116 @@ ISR(TIMER3_COMPA_vect)
409 if (notes_repeat) { 392 if (notes_repeat) {
410 current_note = 0; 393 current_note = 0;
411 } else { 394 } else {
412 #ifdef PWM_AUDIO 395#ifdef PWM_AUDIO
413 DISABLE_AUDIO_COUNTER_3_ISR; 396 DISABLE_AUDIO_COUNTER_3_ISR;
414 #else 397#else
415 DISABLE_AUDIO_COUNTER_3_ISR; 398 DISABLE_AUDIO_COUNTER_3_ISR;
416 DISABLE_AUDIO_COUNTER_3_OUTPUT; 399 DISABLE_AUDIO_COUNTER_3_OUTPUT;
417 #endif 400#endif
418 playing_notes = false; 401 playing_notes = false;
419 return; 402 return;
420 } 403 }
421 } 404 }
422 if (!note_resting && (notes_rest > 0)) { 405 if (!note_resting && (notes_rest > 0)) {
423 note_resting = true; 406 note_resting = true;
424 note_frequency = 0; 407 note_frequency = 0;
425 note_length = notes_rest; 408 note_length = notes_rest;
426 current_note--; 409 current_note--;
427 } else { 410 } else {
428 note_resting = false; 411 note_resting = false;
429 #ifdef PWM_AUDIO 412#ifdef PWM_AUDIO
430 note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE; 413 note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
431 note_length = (*notes_pointer)[current_note][1] * (((float)note_tempo) / 100); 414 note_length = (*notes_pointer)[current_note][1] * (((float)note_tempo) / 100);
432 #else 415#else
433 envelope_index = 0; 416 envelope_index = 0;
434 note_frequency = (*notes_pointer)[current_note][0]; 417 note_frequency = (*notes_pointer)[current_note][0];
435 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100); 418 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
436 #endif 419#endif
437 } 420 }
438 note_position = 0; 421 note_position = 0;
439 } 422 }
440
441 } 423 }
442 424
443 if (!audio_config.enable) { 425 if (!audio_config.enable) {
444 playing_notes = false; 426 playing_notes = false;
445 playing_note = false; 427 playing_note = false;
446 } 428 }
447} 429}
448 430
449void play_note(float freq, int vol) { 431void play_note(float freq, int vol) {
450
451 if (!audio_initialized) { 432 if (!audio_initialized) {
452 audio_init(); 433 audio_init();
453 } 434 }
454 435
455 if (audio_config.enable && voices < 8) { 436 if (audio_config.enable && voices < 8) {
456 DISABLE_AUDIO_COUNTER_3_ISR; 437 DISABLE_AUDIO_COUNTER_3_ISR;
457
458 // Cancel notes if notes are playing
459 if (playing_notes)
460 stop_all_notes();
461 438
462 playing_note = true; 439 // Cancel notes if notes are playing
440 if (playing_notes) stop_all_notes();
463 441
464 envelope_index = 0; 442 playing_note = true;
465 443
466 #ifdef PWM_AUDIO 444 envelope_index = 0;
467 freq = freq / SAMPLE_RATE;
468 #endif
469 if (freq > 0) {
470 frequencies[voices] = freq;
471 volumes[voices] = vol;
472 voices++;
473 }
474 445
475 #ifdef PWM_AUDIO 446#ifdef PWM_AUDIO
476 ENABLE_AUDIO_COUNTER_3_ISR; 447 freq = freq / SAMPLE_RATE;
477 #else 448#endif
478 ENABLE_AUDIO_COUNTER_3_ISR; 449 if (freq > 0) {
479 ENABLE_AUDIO_COUNTER_3_OUTPUT; 450 frequencies[voices] = freq;
480 #endif 451 volumes[voices] = vol;
481 } 452 voices++;
453 }
482 454
455#ifdef PWM_AUDIO
456 ENABLE_AUDIO_COUNTER_3_ISR;
457#else
458 ENABLE_AUDIO_COUNTER_3_ISR;
459 ENABLE_AUDIO_COUNTER_3_OUTPUT;
460#endif
461 }
483} 462}
484 463
485void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat, float n_rest) 464void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat, float n_rest) {
486{
487
488 if (!audio_initialized) { 465 if (!audio_initialized) {
489 audio_init(); 466 audio_init();
490 } 467 }
491 468
492 if (audio_config.enable) { 469 if (audio_config.enable) {
493 470 DISABLE_AUDIO_COUNTER_3_ISR;
494 DISABLE_AUDIO_COUNTER_3_ISR;
495
496 // Cancel note if a note is playing
497 if (playing_note)
498 stop_all_notes();
499
500 playing_notes = true;
501 471
502 notes_pointer = np; 472 // Cancel note if a note is playing
503 notes_count = n_count; 473 if (playing_note) stop_all_notes();
504 notes_repeat = n_repeat;
505 notes_rest = n_rest;
506 474
507 place = 0; 475 playing_notes = true;
508 current_note = 0;
509 476
510 #ifdef PWM_AUDIO 477 notes_pointer = np;
511 note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE; 478 notes_count = n_count;
512 note_length = (*notes_pointer)[current_note][1] * (((float)note_tempo) / 100); 479 notes_repeat = n_repeat;
513 #else 480 notes_rest = n_rest;
514 note_frequency = (*notes_pointer)[current_note][0];
515 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
516 #endif
517 note_position = 0;
518 481
482 place = 0;
483 current_note = 0;
519 484
520 #ifdef PWM_AUDIO 485#ifdef PWM_AUDIO
521 ENABLE_AUDIO_COUNTER_3_ISR; 486 note_frequency = (*notes_pointer)[current_note][0] / SAMPLE_RATE;
522 #else 487 note_length = (*notes_pointer)[current_note][1] * (((float)note_tempo) / 100);
523 ENABLE_AUDIO_COUNTER_3_ISR; 488#else
524 ENABLE_AUDIO_COUNTER_3_OUTPUT; 489 note_frequency = (*notes_pointer)[current_note][0];
525 #endif 490 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
526 } 491#endif
492 note_position = 0;
527 493
494#ifdef PWM_AUDIO
495 ENABLE_AUDIO_COUNTER_3_ISR;
496#else
497 ENABLE_AUDIO_COUNTER_3_ISR;
498 ENABLE_AUDIO_COUNTER_3_OUTPUT;
499#endif
500 }
528} 501}
529 502
530#ifdef PWM_AUDIO 503#ifdef PWM_AUDIO
531void play_sample(uint8_t * s, uint16_t l, bool r) { 504void play_sample(uint8_t* s, uint16_t l, bool r) {
532 if (!audio_initialized) { 505 if (!audio_initialized) {
533 audio_init(); 506 audio_init();
534 } 507 }
@@ -536,17 +509,16 @@ void play_sample(uint8_t * s, uint16_t l, bool r) {
536 if (audio_config.enable) { 509 if (audio_config.enable) {
537 DISABLE_AUDIO_COUNTER_3_ISR; 510 DISABLE_AUDIO_COUNTER_3_ISR;
538 stop_all_notes(); 511 stop_all_notes();
539 place_int = 0; 512 place_int = 0;
540 sample = s; 513 sample = s;
541 sample_length = l; 514 sample_length = l;
542 repeat = r; 515 repeat = r;
543 516
544 ENABLE_AUDIO_COUNTER_3_ISR; 517 ENABLE_AUDIO_COUNTER_3_ISR;
545 } 518 }
546} 519}
547#endif 520#endif
548 521
549
550void audio_toggle(void) { 522void audio_toggle(void) {
551 audio_config.enable ^= 1; 523 audio_config.enable ^= 1;
552 eeconfig_update_audio(audio_config.raw); 524 eeconfig_update_audio(audio_config.raw);
@@ -566,73 +538,45 @@ void audio_off(void) {
566 538
567// Vibrato rate functions 539// Vibrato rate functions
568 540
569void set_vibrato_rate(float rate) { 541void set_vibrato_rate(float rate) { vibrato_rate = rate; }
570 vibrato_rate = rate;
571}
572 542
573void increase_vibrato_rate(float change) { 543void increase_vibrato_rate(float change) { vibrato_rate *= change; }
574 vibrato_rate *= change;
575}
576 544
577void decrease_vibrato_rate(float change) { 545void decrease_vibrato_rate(float change) { vibrato_rate /= change; }
578 vibrato_rate /= change;
579}
580 546
581#ifdef VIBRATO_STRENGTH_ENABLE 547# ifdef VIBRATO_STRENGTH_ENABLE
582 548
583void set_vibrato_strength(float strength) { 549void set_vibrato_strength(float strength) { vibrato_strength = strength; }
584 vibrato_strength = strength;
585}
586 550
587void increase_vibrato_strength(float change) { 551void increase_vibrato_strength(float change) { vibrato_strength *= change; }
588 vibrato_strength *= change;
589}
590 552
591void decrease_vibrato_strength(float change) { 553void decrease_vibrato_strength(float change) { vibrato_strength /= change; }
592 vibrato_strength /= change;
593}
594 554
595#endif /* VIBRATO_STRENGTH_ENABLE */ 555# endif /* VIBRATO_STRENGTH_ENABLE */
596 556
597#endif /* VIBRATO_ENABLE */ 557#endif /* VIBRATO_ENABLE */
598 558
599// Polyphony functions 559// Polyphony functions
600 560
601void set_polyphony_rate(float rate) { 561void set_polyphony_rate(float rate) { polyphony_rate = rate; }
602 polyphony_rate = rate;
603}
604 562
605void enable_polyphony() { 563void enable_polyphony() { polyphony_rate = 5; }
606 polyphony_rate = 5;
607}
608 564
609void disable_polyphony() { 565void disable_polyphony() { polyphony_rate = 0; }
610 polyphony_rate = 0;
611}
612 566
613void increase_polyphony_rate(float change) { 567void increase_polyphony_rate(float change) { polyphony_rate *= change; }
614 polyphony_rate *= change;
615}
616 568
617void decrease_polyphony_rate(float change) { 569void decrease_polyphony_rate(float change) { polyphony_rate /= change; }
618 polyphony_rate /= change;
619}
620 570
621// Timbre function 571// Timbre function
622 572
623void set_timbre(float timbre) { 573void set_timbre(float timbre) { note_timbre = timbre; }
624 note_timbre = timbre;
625}
626 574
627// Tempo functions 575// Tempo functions
628 576
629void set_tempo(uint8_t tempo) { 577void set_tempo(uint8_t tempo) { note_tempo = tempo; }
630 note_tempo = tempo;
631}
632 578
633void decrease_tempo(uint8_t tempo_change) { 579void decrease_tempo(uint8_t tempo_change) { note_tempo += tempo_change; }
634 note_tempo += tempo_change;
635}
636 580
637void increase_tempo(uint8_t tempo_change) { 581void increase_tempo(uint8_t tempo_change) {
638 if (note_tempo - tempo_change < 10) { 582 if (note_tempo - tempo_change < 10) {
@@ -642,17 +586,10 @@ void increase_tempo(uint8_t tempo_change) {
642 } 586 }
643} 587}
644 588
645
646//------------------------------------------------------------------------------ 589//------------------------------------------------------------------------------
647// Override these functions in your keymap file to play different tunes on 590// Override these functions in your keymap file to play different tunes on
648// startup and bootloader jump 591// startup and bootloader jump
649__attribute__ ((weak)) 592__attribute__((weak)) void play_startup_tone() {}
650void play_startup_tone()
651{
652}
653 593
654__attribute__ ((weak)) 594__attribute__((weak)) void play_goodbye_tone() {}
655void play_goodbye_tone()
656{
657}
658//------------------------------------------------------------------------------ 595//------------------------------------------------------------------------------