]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/audio/audio.c
Remove more commented out MCUs
[qmk_firmware.git] / quantum / audio / audio.c
1 /* Copyright 2016 Jack Humbert
2  *
3  * This program is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16
17 #include <stdio.h>
18 #include <string.h>
19 //#include <math.h>
20 #if defined(__AVR__)
21   #include <avr/pgmspace.h>
22   #include <avr/interrupt.h>
23   #include <avr/io.h>
24 #endif
25 #include "print.h"
26 #include "audio.h"
27 #include "keymap.h"
28 #include "wait.h"
29
30 #include "eeconfig.h"
31
32 #define CPU_PRESCALER 8
33
34 // -----------------------------------------------------------------------------
35 // Timer Abstractions
36 // -----------------------------------------------------------------------------
37
38 //Currently we support timers 1 and 3 used at the sime time, channels A-C,
39 //pins PB5, PB6, PB7, PC4, PC5, and PC6
40 #if defined(C6_AUDIO)
41     #define CPIN_AUDIO
42     #define CPIN_SET_DIRECTION DDRC |= _BV(PORTC6);
43     #define INIT_AUDIO_COUNTER_3 TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
44     #define ENABLE_AUDIO_COUNTER_3_ISR TIMSK3 |= _BV(OCIE3A)
45     #define DISABLE_AUDIO_COUNTER_3_ISR TIMSK3 &= ~_BV(OCIE3A)
46     #define ENABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A |= _BV(COM3A1);
47     #define DISABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A &= ~(_BV(COM3A1) | _BV(COM3A0));
48     #define TIMER_3_PERIOD     ICR3
49     #define TIMER_3_DUTY_CYCLE OCR3A
50     #define TIMER3_AUDIO_vect TIMER3_COMPA_vect
51 #endif
52 #if defined(C5_AUDIO)
53     #define CPIN_AUDIO
54     #define CPIN_SET_DIRECTION DDRC |= _BV(PORTC5);
55     #define INIT_AUDIO_COUNTER_3 TCCR3A = (0 << COM3B1) | (0 << COM3B0) | (1 << WGM31) | (0 << WGM30);
56     #define ENABLE_AUDIO_COUNTER_3_ISR TIMSK3 |= _BV(OCIE3B)
57     #define DISABLE_AUDIO_COUNTER_3_ISR TIMSK3 &= ~_BV(OCIE3B)
58     #define ENABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A |= _BV(COM3B1);
59     #define DISABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A &= ~(_BV(COM3B1) | _BV(COM3B0));
60     #define TIMER_3_PERIOD     ICR3
61     #define TIMER_3_DUTY_CYCLE OCR3B
62     #define TIMER3_AUDIO_vect TIMER3_COMPB_vect
63 #endif
64 #if defined(C4_AUDIO)
65     #define CPIN_AUDIO
66     #define CPIN_SET_DIRECTION DDRC |= _BV(PORTC4);
67     #define INIT_AUDIO_COUNTER_3 TCCR3A = (0 << COM3C1) | (0 << COM3C0) | (1 << WGM31) | (0 << WGM30);
68     #define ENABLE_AUDIO_COUNTER_3_ISR TIMSK3 |= _BV(OCIE3C)
69     #define DISABLE_AUDIO_COUNTER_3_ISR TIMSK3 &= ~_BV(OCIE3C)
70     #define ENABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A |= _BV(COM3C1);
71     #define DISABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A &= ~(_BV(COM3C1) | _BV(COM3C0));
72     #define TIMER_3_PERIOD     ICR3
73     #define TIMER_3_DUTY_CYCLE OCR3C
74     #define TIMER3_AUDIO_vect TIMER3_COMPC_vect
75 #endif
76
77 #if defined(B5_AUDIO)
78     #define BPIN_AUDIO
79     #define BPIN_SET_DIRECTION DDRB |= _BV(PORTB5);
80     #define INIT_AUDIO_COUNTER_1 TCCR1A = (0 << COM1A1) | (0 << COM1A0) | (1 << WGM11) | (0 << WGM10);
81     #define ENABLE_AUDIO_COUNTER_1_ISR TIMSK1 |= _BV(OCIE1A)
82     #define DISABLE_AUDIO_COUNTER_1_ISR TIMSK1 &= ~_BV(OCIE1A)
83     #define ENABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A |= _BV(COM1A1);
84     #define DISABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A &= ~(_BV(COM1A1) | _BV(COM1A0));
85     #define TIMER_1_PERIOD     ICR1
86     #define TIMER_1_DUTY_CYCLE OCR1A
87     #define TIMER1_AUDIO_vect TIMER1_COMPA_vect
88 #endif
89 #if defined(B6_AUDIO)
90     #define BPIN_AUDIO
91     #define BPIN_SET_DIRECTION DDRB |= _BV(PORTB6);
92     #define INIT_AUDIO_COUNTER_1 TCCR1A = (0 << COM1B1) | (0 << COM1B0) | (1 << WGM11) | (0 << WGM10);
93     #define ENABLE_AUDIO_COUNTER_1_ISR TIMSK1 |= _BV(OCIE1B)
94     #define DISABLE_AUDIO_COUNTER_1_ISR TIMSK1 &= ~_BV(OCIE1B)
95     #define ENABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A |= _BV(COM1B1);
96     #define DISABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A &= ~(_BV(COM1B1) | _BV(COM1B0));
97     #define TIMER_1_PERIOD     ICR1
98     #define TIMER_1_DUTY_CYCLE OCR1B
99     #define TIMER1_AUDIO_vect TIMER1_COMPB_vect
100 #endif
101 #if defined(B7_AUDIO)
102     #define BPIN_AUDIO
103     #define BPIN_SET_DIRECTION DDRB |= _BV(PORTB7);
104     #define INIT_AUDIO_COUNTER_1 TCCR1A = (0 << COM1C1) | (0 << COM1C0) | (1 << WGM11) | (0 << WGM10);
105     #define ENABLE_AUDIO_COUNTER_1_ISR TIMSK1 |= _BV(OCIE1C)
106     #define DISABLE_AUDIO_COUNTER_1_ISR TIMSK1 &= ~_BV(OCIE1C)
107     #define ENABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A |= _BV(COM1C1);
108     #define DISABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A &= ~(_BV(COM1C1) | _BV(COM1C0));
109     #define TIMER_1_PERIOD     ICR1
110     #define TIMER_1_DUTY_CYCLE OCR1C
111     #define TIMER1_AUDIO_vect TIMER1_COMPC_vect
112 #endif
113 // -----------------------------------------------------------------------------
114
115
116 int voices = 0;
117 int voice_place = 0;
118 float frequency = 0;
119 float frequency_alt = 0;
120 int volume = 0;
121 long position = 0;
122
123 float frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
124 int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
125 bool sliding = false;
126
127 float place = 0;
128
129 uint8_t * sample;
130 uint16_t sample_length = 0;
131
132 bool     playing_notes = false;
133 bool     playing_note = false;
134 float    note_frequency = 0;
135 float    note_length = 0;
136 uint8_t  note_tempo = TEMPO_DEFAULT;
137 float    note_timbre = TIMBRE_DEFAULT;
138 uint16_t note_position = 0;
139 float (* notes_pointer)[][2];
140 uint16_t notes_count;
141 bool     notes_repeat;
142 bool     note_resting = false;
143
144 uint16_t current_note = 0;
145 uint8_t rest_counter = 0;
146
147 #ifdef VIBRATO_ENABLE
148 float vibrato_counter = 0;
149 float vibrato_strength = .5;
150 float vibrato_rate = 0.125;
151 #endif
152
153 float polyphony_rate = 0;
154
155 static bool audio_initialized = false;
156
157 audio_config_t audio_config;
158
159 uint16_t envelope_index = 0;
160 bool glissando = true;
161
162 #ifndef STARTUP_SONG
163     #define STARTUP_SONG SONG(STARTUP_SOUND)
164 #endif
165 #ifndef AUDIO_ON_SONG
166     #define AUDIO_ON_SONG SONG(AUDIO_ON_SOUND)
167 #endif
168 #ifndef AUDIO_OFF_SONG
169     #define AUDIO_OFF_SONG SONG(AUDIO_OFF_SOUND)
170 #endif
171 float startup_song[][2] = STARTUP_SONG;
172 float audio_on_song[][2] = AUDIO_ON_SONG;
173 float audio_off_song[][2] = AUDIO_OFF_SONG;
174
175 void audio_init()
176 {
177
178     // Check EEPROM
179     if (!eeconfig_is_enabled())
180     {
181         eeconfig_init();
182     }
183     audio_config.raw = eeconfig_read_audio();
184
185     if (!audio_initialized) {
186
187         // Set audio ports as output
188         #ifdef CPIN_AUDIO
189           CPIN_SET_DIRECTION
190           DISABLE_AUDIO_COUNTER_3_ISR;
191         #endif
192         #ifdef BPIN_AUDIO
193           BPIN_SET_DIRECTION
194           DISABLE_AUDIO_COUNTER_1_ISR;
195         #endif
196
197         // TCCR3A / TCCR3B: Timer/Counter #3 Control Registers TCCR3A/TCCR3B, TCCR1A/TCCR1B
198         // Compare Output Mode (COM3An and COM1An) = 0b00 = Normal port operation
199         //   OC3A -- PC6
200         //   OC3B -- PC5
201         //   OC3C -- PC4
202         //   OC1A -- PB5
203         //   OC1B -- PB6
204         //   OC1C -- PB7
205
206         // Waveform Generation Mode (WGM3n) = 0b1110 = Fast PWM Mode 14. Period = ICR3, Duty Cycle OCR3A)
207         //   OCR3A - PC6
208         //   OCR3B - PC5
209         //   OCR3C - PC4
210         //   OCR1A - PB5
211         //   OCR1B - PB6
212         //   OCR1C - PB7
213
214         // Clock Select (CS3n) = 0b010 = Clock / 8
215         #ifdef CPIN_AUDIO
216             INIT_AUDIO_COUNTER_3
217             TCCR3B = (1 << WGM33)  | (1 << WGM32)  | (0 << CS32)  | (1 << CS31) | (0 << CS30);
218             TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (440 * CPU_PRESCALER));
219             TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (440 * CPU_PRESCALER)) * note_timbre);
220         #endif
221         #ifdef BPIN_AUDIO
222             INIT_AUDIO_COUNTER_1
223             TCCR1B = (1 << WGM13)  | (1 << WGM12)  | (0 << CS12)  | (1 << CS11) | (0 << CS10);
224             TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (440 * CPU_PRESCALER));
225             TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (440 * CPU_PRESCALER)) * note_timbre);
226         #endif
227
228         audio_initialized = true;
229     }
230
231     if (audio_config.enable) {
232         PLAY_SONG(startup_song);
233     }
234
235 }
236
237 void stop_all_notes()
238 {
239     dprintf("audio stop all notes");
240
241     if (!audio_initialized) {
242         audio_init();
243     }
244     voices = 0;
245
246     #ifdef CPIN_AUDIO
247         DISABLE_AUDIO_COUNTER_3_ISR;
248         DISABLE_AUDIO_COUNTER_3_OUTPUT;
249     #endif
250
251     #ifdef BPIN_AUDIO
252         DISABLE_AUDIO_COUNTER_1_ISR;
253         DISABLE_AUDIO_COUNTER_1_OUTPUT;
254     #endif
255
256     playing_notes = false;
257     playing_note = false;
258     frequency = 0;
259     frequency_alt = 0;
260     volume = 0;
261
262     for (uint8_t i = 0; i < 8; i++)
263     {
264         frequencies[i] = 0;
265         volumes[i] = 0;
266     }
267 }
268
269 void stop_note(float freq)
270 {
271     dprintf("audio stop note freq=%d", (int)freq);
272
273     if (playing_note) {
274         if (!audio_initialized) {
275             audio_init();
276         }
277         for (int i = 7; i >= 0; i--) {
278             if (frequencies[i] == freq) {
279                 frequencies[i] = 0;
280                 volumes[i] = 0;
281                 for (int j = i; (j < 7); j++) {
282                     frequencies[j] = frequencies[j+1];
283                     frequencies[j+1] = 0;
284                     volumes[j] = volumes[j+1];
285                     volumes[j+1] = 0;
286                 }
287                 break;
288             }
289         }
290         voices--;
291         if (voices < 0)
292             voices = 0;
293         if (voice_place >= voices) {
294             voice_place = 0;
295         }
296         if (voices == 0) {
297             #ifdef CPIN_AUDIO
298                 DISABLE_AUDIO_COUNTER_3_ISR;
299                 DISABLE_AUDIO_COUNTER_3_OUTPUT;
300             #endif
301             #ifdef BPIN_AUDIO
302                 DISABLE_AUDIO_COUNTER_1_ISR;
303                 DISABLE_AUDIO_COUNTER_1_OUTPUT;
304             #endif
305             frequency = 0;
306             frequency_alt = 0;
307             volume = 0;
308             playing_note = false;
309         }
310     }
311 }
312
313 #ifdef VIBRATO_ENABLE
314
315 float mod(float a, int b)
316 {
317     float r = fmod(a, b);
318     return r < 0 ? r + b : r;
319 }
320
321 float vibrato(float average_freq) {
322     #ifdef VIBRATO_STRENGTH_ENABLE
323         float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
324     #else
325         float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
326     #endif
327     vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH);
328     return vibrated_freq;
329 }
330
331 #endif
332
333 #ifdef CPIN_AUDIO
334 ISR(TIMER3_AUDIO_vect)
335 {
336     float freq;
337
338     if (playing_note) {
339         if (voices > 0) {
340
341             #ifdef BPIN_AUDIO
342             float freq_alt = 0;
343                 if (voices > 1) {
344                     if (polyphony_rate == 0) {
345                         if (glissando) {
346                             if (frequency_alt != 0 && frequency_alt < frequencies[voices - 2] && frequency_alt < frequencies[voices - 2] * pow(2, -440/frequencies[voices - 2]/12/2)) {
347                                 frequency_alt = frequency_alt * pow(2, 440/frequency_alt/12/2);
348                             } else if (frequency_alt != 0 && frequency_alt > frequencies[voices - 2] && frequency_alt > frequencies[voices - 2] * pow(2, 440/frequencies[voices - 2]/12/2)) {
349                                 frequency_alt = frequency_alt * pow(2, -440/frequency_alt/12/2);
350                             } else {
351                                 frequency_alt = frequencies[voices - 2];
352                             }
353                         } else {
354                             frequency_alt = frequencies[voices - 2];
355                         }
356
357                         #ifdef VIBRATO_ENABLE
358                             if (vibrato_strength > 0) {
359                                 freq_alt = vibrato(frequency_alt);
360                             } else {
361                                 freq_alt = frequency_alt;
362                             }
363                         #else
364                             freq_alt = frequency_alt;
365                         #endif
366                     }
367
368                     if (envelope_index < 65535) {
369                         envelope_index++;
370                     }
371
372                     freq_alt = voice_envelope(freq_alt);
373
374                     if (freq_alt < 30.517578125) {
375                         freq_alt = 30.52;
376                     }
377
378                     TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (freq_alt * CPU_PRESCALER));
379                     TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq_alt * CPU_PRESCALER)) * note_timbre);
380                 }
381             #endif
382
383             if (polyphony_rate > 0) {
384                 if (voices > 1) {
385                     voice_place %= voices;
386                     if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
387                         voice_place = (voice_place + 1) % voices;
388                         place = 0.0;
389                     }
390                 }
391
392                 #ifdef VIBRATO_ENABLE
393                     if (vibrato_strength > 0) {
394                         freq = vibrato(frequencies[voice_place]);
395                     } else {
396                         freq = frequencies[voice_place];
397                     }
398                 #else
399                     freq = frequencies[voice_place];
400                 #endif
401             } else {
402                 if (glissando) {
403                     if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) {
404                         frequency = frequency * pow(2, 440/frequency/12/2);
405                     } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
406                         frequency = frequency * pow(2, -440/frequency/12/2);
407                     } else {
408                         frequency = frequencies[voices - 1];
409                     }
410                 } else {
411                     frequency = frequencies[voices - 1];
412                 }
413
414                 #ifdef VIBRATO_ENABLE
415                     if (vibrato_strength > 0) {
416                         freq = vibrato(frequency);
417                     } else {
418                         freq = frequency;
419                     }
420                 #else
421                     freq = frequency;
422                 #endif
423             }
424
425             if (envelope_index < 65535) {
426                 envelope_index++;
427             }
428
429             freq = voice_envelope(freq);
430
431             if (freq < 30.517578125) {
432                 freq = 30.52;
433             }
434
435             TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
436             TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
437         }
438     }
439
440     if (playing_notes) {
441         if (note_frequency > 0) {
442             #ifdef VIBRATO_ENABLE
443                 if (vibrato_strength > 0) {
444                     freq = vibrato(note_frequency);
445                 } else {
446                     freq = note_frequency;
447                 }
448             #else
449                     freq = note_frequency;
450             #endif
451
452             if (envelope_index < 65535) {
453                 envelope_index++;
454             }
455             freq = voice_envelope(freq);
456
457             TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
458             TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
459         } else {
460             TIMER_3_PERIOD = 0;
461             TIMER_3_DUTY_CYCLE = 0;
462         }
463
464         note_position++;
465         bool end_of_note = false;
466         if (TIMER_3_PERIOD > 0) {
467             if (!note_resting)
468                 end_of_note = (note_position >= (note_length / TIMER_3_PERIOD * 0xFFFF - 1));
469             else
470                 end_of_note = (note_position >= (note_length));
471         } else {
472             end_of_note = (note_position >= (note_length));
473         }
474
475         if (end_of_note) {
476             current_note++;
477             if (current_note >= notes_count) {
478                 if (notes_repeat) {
479                     current_note = 0;
480                 } else {
481                     DISABLE_AUDIO_COUNTER_3_ISR;
482                     DISABLE_AUDIO_COUNTER_3_OUTPUT;
483                     playing_notes = false;
484                     return;
485                 }
486             }
487             if (!note_resting) {
488                 note_resting = true;
489                 current_note--;
490                 if ((*notes_pointer)[current_note][0] == (*notes_pointer)[current_note + 1][0]) {
491                     note_frequency = 0;
492                     note_length = 1;
493                 } else {
494                     note_frequency = (*notes_pointer)[current_note][0];
495                     note_length = 1;
496                 }
497             } else {
498                 note_resting = false;
499                 envelope_index = 0;
500                 note_frequency = (*notes_pointer)[current_note][0];
501                 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
502             }
503
504             note_position = 0;
505         }
506     }
507
508     if (!audio_config.enable) {
509         playing_notes = false;
510         playing_note = false;
511     }
512 }
513 #endif
514
515 #ifdef BPIN_AUDIO
516 ISR(TIMER1_AUDIO_vect)
517 {
518     #if defined(BPIN_AUDIO) && !defined(CPIN_AUDIO)
519     float freq = 0;
520
521     if (playing_note) {
522         if (voices > 0) {
523             if (polyphony_rate > 0) {
524                 if (voices > 1) {
525                     voice_place %= voices;
526                     if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
527                         voice_place = (voice_place + 1) % voices;
528                         place = 0.0;
529                     }
530                 }
531
532                 #ifdef VIBRATO_ENABLE
533                     if (vibrato_strength > 0) {
534                         freq = vibrato(frequencies[voice_place]);
535                     } else {
536                         freq = frequencies[voice_place];
537                     }
538                 #else
539                     freq = frequencies[voice_place];
540                 #endif
541             } else {
542                 if (glissando) {
543                     if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) {
544                         frequency = frequency * pow(2, 440/frequency/12/2);
545                     } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
546                         frequency = frequency * pow(2, -440/frequency/12/2);
547                     } else {
548                         frequency = frequencies[voices - 1];
549                     }
550                 } else {
551                     frequency = frequencies[voices - 1];
552                 }
553
554                 #ifdef VIBRATO_ENABLE
555                     if (vibrato_strength > 0) {
556                         freq = vibrato(frequency);
557                     } else {
558                         freq = frequency;
559                     }
560                 #else
561                     freq = frequency;
562                 #endif
563             }
564
565             if (envelope_index < 65535) {
566                 envelope_index++;
567             }
568
569             freq = voice_envelope(freq);
570
571             if (freq < 30.517578125) {
572                 freq = 30.52;
573             }
574
575             TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
576             TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
577         }
578     }
579
580     if (playing_notes) {
581         if (note_frequency > 0) {
582             #ifdef VIBRATO_ENABLE
583                 if (vibrato_strength > 0) {
584                     freq = vibrato(note_frequency);
585                 } else {
586                     freq = note_frequency;
587                 }
588             #else
589                     freq = note_frequency;
590             #endif
591
592             if (envelope_index < 65535) {
593                 envelope_index++;
594             }
595             freq = voice_envelope(freq);
596
597             TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
598             TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
599         } else {
600             TIMER_1_PERIOD = 0;
601             TIMER_1_DUTY_CYCLE = 0;
602         }
603
604         note_position++;
605         bool end_of_note = false;
606         if (TIMER_1_PERIOD > 0) {
607             if (!note_resting)
608                 end_of_note = (note_position >= (note_length / TIMER_1_PERIOD * 0xFFFF - 1));
609             else
610                 end_of_note = (note_position >= (note_length));
611         } else {
612             end_of_note = (note_position >= (note_length));
613         }
614
615         if (end_of_note) {
616             current_note++;
617             if (current_note >= notes_count) {
618                 if (notes_repeat) {
619                     current_note = 0;
620                 } else {
621                     DISABLE_AUDIO_COUNTER_1_ISR;
622                     DISABLE_AUDIO_COUNTER_1_OUTPUT;
623                     playing_notes = false;
624                     return;
625                 }
626             }
627             if (!note_resting) {
628                 note_resting = true;
629                 current_note--;
630                 if ((*notes_pointer)[current_note][0] == (*notes_pointer)[current_note + 1][0]) {
631                     note_frequency = 0;
632                     note_length = 1;
633                 } else {
634                     note_frequency = (*notes_pointer)[current_note][0];
635                     note_length = 1;
636                 }
637             } else {
638                 note_resting = false;
639                 envelope_index = 0;
640                 note_frequency = (*notes_pointer)[current_note][0];
641                 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
642             }
643
644             note_position = 0;
645         }
646     }
647
648     if (!audio_config.enable) {
649         playing_notes = false;
650         playing_note = false;
651     }
652 #endif
653 }
654 #endif
655
656 void play_note(float freq, int vol) {
657
658     dprintf("audio play note freq=%d vol=%d", (int)freq, vol);
659
660     if (!audio_initialized) {
661         audio_init();
662     }
663
664     if (audio_config.enable && voices < 8) {
665         #ifdef CPIN_AUDIO
666             DISABLE_AUDIO_COUNTER_3_ISR;
667         #endif
668         #ifdef BPIN_AUDIO
669             DISABLE_AUDIO_COUNTER_1_ISR;
670         #endif
671
672         // Cancel notes if notes are playing
673         if (playing_notes)
674             stop_all_notes();
675
676         playing_note = true;
677
678         envelope_index = 0;
679
680         if (freq > 0) {
681             frequencies[voices] = freq;
682             volumes[voices] = vol;
683             voices++;
684         }
685
686         #ifdef CPIN_AUDIO
687             ENABLE_AUDIO_COUNTER_3_ISR;
688             ENABLE_AUDIO_COUNTER_3_OUTPUT;
689         #endif
690         #ifdef BPIN_AUDIO
691             #ifdef CPIN_AUDIO
692             if (voices > 1) {
693                 ENABLE_AUDIO_COUNTER_1_ISR;
694                 ENABLE_AUDIO_COUNTER_1_OUTPUT;
695             }
696             #else
697             ENABLE_AUDIO_COUNTER_1_ISR;
698             ENABLE_AUDIO_COUNTER_1_OUTPUT;
699             #endif
700         #endif
701     }
702
703 }
704
705 void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat)
706 {
707
708     if (!audio_initialized) {
709         audio_init();
710     }
711
712     if (audio_config.enable) {
713
714         #ifdef CPIN_AUDIO
715             DISABLE_AUDIO_COUNTER_3_ISR;
716         #endif
717         #ifdef BPIN_AUDIO
718             DISABLE_AUDIO_COUNTER_1_ISR;
719         #endif
720
721         // Cancel note if a note is playing
722         if (playing_note)
723             stop_all_notes();
724
725         playing_notes = true;
726
727         notes_pointer = np;
728         notes_count = n_count;
729         notes_repeat = n_repeat;
730
731         place = 0;
732         current_note = 0;
733
734         note_frequency = (*notes_pointer)[current_note][0];
735         note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
736         note_position = 0;
737
738
739         #ifdef CPIN_AUDIO
740             ENABLE_AUDIO_COUNTER_3_ISR;
741             ENABLE_AUDIO_COUNTER_3_OUTPUT;
742         #endif
743         #ifdef BPIN_AUDIO
744             #ifndef CPIN_AUDIO
745             ENABLE_AUDIO_COUNTER_1_ISR;
746             ENABLE_AUDIO_COUNTER_1_OUTPUT;
747             #endif
748         #endif
749     }
750
751 }
752
753 bool is_playing_notes(void) {
754     return playing_notes;
755 }
756
757 bool is_audio_on(void) {
758     return (audio_config.enable != 0);
759 }
760
761 void audio_toggle(void) {
762     audio_config.enable ^= 1;
763     eeconfig_update_audio(audio_config.raw);
764     if (audio_config.enable)
765         audio_on_user();
766 }
767
768 void audio_on(void) {
769     audio_config.enable = 1;
770     eeconfig_update_audio(audio_config.raw);
771     audio_on_user();
772     PLAY_SONG(audio_on_song);
773 }
774
775 void audio_off(void) {
776     PLAY_SONG(audio_off_song);
777     wait_ms(100);
778     stop_all_notes();
779     audio_config.enable = 0;
780     eeconfig_update_audio(audio_config.raw);
781 }
782
783 #ifdef VIBRATO_ENABLE
784
785 // Vibrato rate functions
786
787 void set_vibrato_rate(float rate) {
788     vibrato_rate = rate;
789 }
790
791 void increase_vibrato_rate(float change) {
792     vibrato_rate *= change;
793 }
794
795 void decrease_vibrato_rate(float change) {
796     vibrato_rate /= change;
797 }
798
799 #ifdef VIBRATO_STRENGTH_ENABLE
800
801 void set_vibrato_strength(float strength) {
802     vibrato_strength = strength;
803 }
804
805 void increase_vibrato_strength(float change) {
806     vibrato_strength *= change;
807 }
808
809 void decrease_vibrato_strength(float change) {
810     vibrato_strength /= change;
811 }
812
813 #endif  /* VIBRATO_STRENGTH_ENABLE */
814
815 #endif /* VIBRATO_ENABLE */
816
817 // Polyphony functions
818
819 void set_polyphony_rate(float rate) {
820     polyphony_rate = rate;
821 }
822
823 void enable_polyphony() {
824     polyphony_rate = 5;
825 }
826
827 void disable_polyphony() {
828     polyphony_rate = 0;
829 }
830
831 void increase_polyphony_rate(float change) {
832     polyphony_rate *= change;
833 }
834
835 void decrease_polyphony_rate(float change) {
836     polyphony_rate /= change;
837 }
838
839 // Timbre function
840
841 void set_timbre(float timbre) {
842     note_timbre = timbre;
843 }
844
845 // Tempo functions
846
847 void set_tempo(uint8_t tempo) {
848     note_tempo = tempo;
849 }
850
851 void decrease_tempo(uint8_t tempo_change) {
852     note_tempo += tempo_change;
853 }
854
855 void increase_tempo(uint8_t tempo_change) {
856     if (note_tempo - tempo_change < 10) {
857         note_tempo = 10;
858     } else {
859         note_tempo -= tempo_change;
860     }
861 }