]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/audio/audio_arm.c
6760015ef462700d20249c79cc009346265b3e2b
[qmk_firmware.git] / quantum / audio / audio_arm.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 "audio.h"
18 #include "ch.h"
19 #include "hal.h"
20
21 #include <string.h>
22 #include "print.h"
23 #include "keymap.h"
24
25 #include "eeconfig.h"
26
27 // -----------------------------------------------------------------------------
28
29 int voices = 0;
30 int voice_place = 0;
31 float frequency = 0;
32 float frequency_alt = 0;
33 int volume = 0;
34 long position = 0;
35
36 float frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
37 int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
38 bool sliding = false;
39
40 float place = 0;
41
42 uint8_t * sample;
43 uint16_t sample_length = 0;
44
45 bool     playing_notes = false;
46 bool     playing_note = false;
47 float    note_frequency = 0;
48 float    note_length = 0;
49 uint8_t  note_tempo = TEMPO_DEFAULT;
50 float    note_timbre = TIMBRE_DEFAULT;
51 uint16_t note_position = 0;
52 float (* notes_pointer)[][2];
53 uint16_t notes_count;
54 bool     notes_repeat;
55 bool     note_resting = false;
56
57 uint16_t current_note = 0;
58 uint8_t rest_counter = 0;
59
60 #ifdef VIBRATO_ENABLE
61 float vibrato_counter = 0;
62 float vibrato_strength = .5;
63 float vibrato_rate = 0.125;
64 #endif
65
66 float polyphony_rate = 0;
67
68 static bool audio_initialized = false;
69
70 audio_config_t audio_config;
71
72 uint16_t envelope_index = 0;
73 bool glissando = true;
74
75 #ifndef STARTUP_SONG
76     #define STARTUP_SONG SONG(STARTUP_SOUND)
77 #endif
78 float startup_song[][2] = STARTUP_SONG;
79
80 static void gpt_cb8(GPTDriver *gptp);
81
82 #define DAC_BUFFER_SIZE 720
83 #ifndef DAC_SAMPLE_MAX
84 #define DAC_SAMPLE_MAX  65535U
85 #endif
86
87 #define START_CHANNEL_1() gptStart(&GPTD6, &gpt6cfg1); \
88     gptStartContinuous(&GPTD6, 2U)
89 #define START_CHANNEL_2() gptStart(&GPTD7, &gpt7cfg1); \
90     gptStartContinuous(&GPTD7, 2U)
91 #define STOP_CHANNEL_1() gptStopTimer(&GPTD6)
92 #define STOP_CHANNEL_2() gptStopTimer(&GPTD7)
93 #define RESTART_CHANNEL_1() STOP_CHANNEL_1(); \
94     START_CHANNEL_1()
95 #define RESTART_CHANNEL_2() STOP_CHANNEL_2(); \
96     START_CHANNEL_2()
97 #define UPDATE_CHANNEL_1_FREQ(freq) gpt6cfg1.frequency = freq * DAC_BUFFER_SIZE; \
98     RESTART_CHANNEL_1()
99 #define UPDATE_CHANNEL_2_FREQ(freq) gpt7cfg1.frequency = freq * DAC_BUFFER_SIZE; \
100     RESTART_CHANNEL_2()
101 #define GET_CHANNEL_1_FREQ gpt6cfg1.frequency
102 #define GET_CHANNEL_2_FREQ gpt7cfg1.frequency
103
104
105 /*
106  * GPT6 configuration.
107  */
108 // static const GPTConfig gpt6cfg1 = {
109 //   .frequency    = 1000000U,
110 //   .callback     = NULL,
111 //   .cr2          = TIM_CR2_MMS_1,    /* MMS = 010 = TRGO on Update Event.    */
112 //   .dier         = 0U
113 // };
114
115 GPTConfig gpt6cfg1 = {
116   .frequency    = 440U*DAC_BUFFER_SIZE,
117   .callback     = NULL,
118   .cr2          = TIM_CR2_MMS_1,    /* MMS = 010 = TRGO on Update Event.    */
119   .dier         = 0U
120 };
121
122 GPTConfig gpt7cfg1 = {
123   .frequency    = 440U*DAC_BUFFER_SIZE,
124   .callback     = NULL,
125   .cr2          = TIM_CR2_MMS_1,    /* MMS = 010 = TRGO on Update Event.    */
126   .dier         = 0U
127 };
128
129 GPTConfig gpt8cfg1 = {
130   .frequency    = 10,
131   .callback     = gpt_cb8,
132   .cr2          = TIM_CR2_MMS_1,    /* MMS = 010 = TRGO on Update Event.    */
133   .dier         = 0U
134 };
135
136
137 /*
138  * DAC test buffer (sine wave).
139  */
140 // static const dacsample_t dac_buffer[DAC_BUFFER_SIZE] = {
141 //   2047, 2082, 2118, 2154, 2189, 2225, 2260, 2296, 2331, 2367, 2402, 2437,
142 //   2472, 2507, 2542, 2576, 2611, 2645, 2679, 2713, 2747, 2780, 2813, 2846,
143 //   2879, 2912, 2944, 2976, 3008, 3039, 3070, 3101, 3131, 3161, 3191, 3221,
144 //   3250, 3278, 3307, 3335, 3362, 3389, 3416, 3443, 3468, 3494, 3519, 3544,
145 //   3568, 3591, 3615, 3637, 3660, 3681, 3703, 3723, 3744, 3763, 3782, 3801,
146 //   3819, 3837, 3854, 3870, 3886, 3902, 3917, 3931, 3944, 3958, 3970, 3982,
147 //   3993, 4004, 4014, 4024, 4033, 4041, 4049, 4056, 4062, 4068, 4074, 4078,
148 //   4082, 4086, 4089, 4091, 4092, 4093, 4094, 4093, 4092, 4091, 4089, 4086,
149 //   4082, 4078, 4074, 4068, 4062, 4056, 4049, 4041, 4033, 4024, 4014, 4004,
150 //   3993, 3982, 3970, 3958, 3944, 3931, 3917, 3902, 3886, 3870, 3854, 3837,
151 //   3819, 3801, 3782, 3763, 3744, 3723, 3703, 3681, 3660, 3637, 3615, 3591,
152 //   3568, 3544, 3519, 3494, 3468, 3443, 3416, 3389, 3362, 3335, 3307, 3278,
153 //   3250, 3221, 3191, 3161, 3131, 3101, 3070, 3039, 3008, 2976, 2944, 2912,
154 //   2879, 2846, 2813, 2780, 2747, 2713, 2679, 2645, 2611, 2576, 2542, 2507,
155 //   2472, 2437, 2402, 2367, 2331, 2296, 2260, 2225, 2189, 2154, 2118, 2082,
156 //   2047, 2012, 1976, 1940, 1905, 1869, 1834, 1798, 1763, 1727, 1692, 1657,
157 //   1622, 1587, 1552, 1518, 1483, 1449, 1415, 1381, 1347, 1314, 1281, 1248,
158 //   1215, 1182, 1150, 1118, 1086, 1055, 1024,  993,  963,  933,  903,  873,
159 //    844,  816,  787,  759,  732,  705,  678,  651,  626,  600,  575,  550,
160 //    526,  503,  479,  457,  434,  413,  391,  371,  350,  331,  312,  293,
161 //    275,  257,  240,  224,  208,  192,  177,  163,  150,  136,  124,  112,
162 //    101,   90,   80,   70,   61,   53,   45,   38,   32,   26,   20,   16,
163 //     12,    8,    5,    3,    2,    1,    0,    1,    2,    3,    5,    8,
164 //     12,   16,   20,   26,   32,   38,   45,   53,   61,   70,   80,   90,
165 //    101,  112,  124,  136,  150,  163,  177,  192,  208,  224,  240,  257,
166 //    275,  293,  312,  331,  350,  371,  391,  413,  434,  457,  479,  503,
167 //    526,  550,  575,  600,  626,  651,  678,  705,  732,  759,  787,  816,
168 //    844,  873,  903,  933,  963,  993, 1024, 1055, 1086, 1118, 1150, 1182,
169 //   1215, 1248, 1281, 1314, 1347, 1381, 1415, 1449, 1483, 1518, 1552, 1587,
170 //   1622, 1657, 1692, 1727, 1763, 1798, 1834, 1869, 1905, 1940, 1976, 2012
171 // };
172
173 // static const dacsample_t dac_buffer_2[DAC_BUFFER_SIZE] = {
174 //     12,    8,    5,    3,    2,    1,    0,    1,    2,    3,    5,    8,
175 //     12,   16,   20,   26,   32,   38,   45,   53,   61,   70,   80,   90,
176 //    101,  112,  124,  136,  150,  163,  177,  192,  208,  224,  240,  257,
177 //    275,  293,  312,  331,  350,  371,  391,  413,  434,  457,  479,  503,
178 //    526,  550,  575,  600,  626,  651,  678,  705,  732,  759,  787,  816,
179 //    844,  873,  903,  933,  963,  993, 1024, 1055, 1086, 1118, 1150, 1182,
180 //   1215, 1248, 1281, 1314, 1347, 1381, 1415, 1449, 1483, 1518, 1552, 1587,
181 //   1622, 1657, 1692, 1727, 1763, 1798, 1834, 1869, 1905, 1940, 1976, 2012,
182 //   2047, 2082, 2118, 2154, 2189, 2225, 2260, 2296, 2331, 2367, 2402, 2437,
183 //   2472, 2507, 2542, 2576, 2611, 2645, 2679, 2713, 2747, 2780, 2813, 2846,
184 //   2879, 2912, 2944, 2976, 3008, 3039, 3070, 3101, 3131, 3161, 3191, 3221,
185 //   3250, 3278, 3307, 3335, 3362, 3389, 3416, 3443, 3468, 3494, 3519, 3544,
186 //   3568, 3591, 3615, 3637, 3660, 3681, 3703, 3723, 3744, 3763, 3782, 3801,
187 //   3819, 3837, 3854, 3870, 3886, 3902, 3917, 3931, 3944, 3958, 3970, 3982,
188 //   3993, 4004, 4014, 4024, 4033, 4041, 4049, 4056, 4062, 4068, 4074, 4078,
189 //   4082, 4086, 4089, 4091, 4092, 4093, 4094, 4093, 4092, 4091, 4089, 4086,
190 //   4082, 4078, 4074, 4068, 4062, 4056, 4049, 4041, 4033, 4024, 4014, 4004,
191 //   3993, 3982, 3970, 3958, 3944, 3931, 3917, 3902, 3886, 3870, 3854, 3837,
192 //   3819, 3801, 3782, 3763, 3744, 3723, 3703, 3681, 3660, 3637, 3615, 3591,
193 //   3568, 3544, 3519, 3494, 3468, 3443, 3416, 3389, 3362, 3335, 3307, 3278,
194 //   3250, 3221, 3191, 3161, 3131, 3101, 3070, 3039, 3008, 2976, 2944, 2912,
195 //   2879, 2846, 2813, 2780, 2747, 2713, 2679, 2645, 2611, 2576, 2542, 2507,
196 //   2472, 2437, 2402, 2367, 2331, 2296, 2260, 2225, 2189, 2154, 2118, 2082,
197 //   2047, 2012, 1976, 1940, 1905, 1869, 1834, 1798, 1763, 1727, 1692, 1657,
198 //   1622, 1587, 1552, 1518, 1483, 1449, 1415, 1381, 1347, 1314, 1281, 1248,
199 //   1215, 1182, 1150, 1118, 1086, 1055, 1024,  993,  963,  933,  903,  873,
200 //    844,  816,  787,  759,  732,  705,  678,  651,  626,  600,  575,  550,
201 //    526,  503,  479,  457,  434,  413,  391,  371,  350,  331,  312,  293,
202 //    275,  257,  240,  224,  208,  192,  177,  163,  150,  136,  124,  112,
203 //    101,   90,   80,   70,   61,   53,   45,   38,   32,   26,   20,   16
204 // };
205
206 // squarewave
207 static const dacsample_t dac_buffer[DAC_BUFFER_SIZE] = {
208   // First half is max, second half is 0
209   [0                 ... DAC_BUFFER_SIZE/2-1] = DAC_SAMPLE_MAX,
210   [DAC_BUFFER_SIZE/2 ... DAC_BUFFER_SIZE  -1] = 0,
211 };
212
213 // squarewave
214 static const dacsample_t dac_buffer_2[DAC_BUFFER_SIZE] = {
215   // opposite of dac_buffer above
216   [0                 ... DAC_BUFFER_SIZE/2-1] = 0,
217   [DAC_BUFFER_SIZE/2 ... DAC_BUFFER_SIZE  -1] = DAC_SAMPLE_MAX,
218 };
219
220 /*
221  * DAC streaming callback.
222  */
223 size_t nx = 0, ny = 0, nz = 0;
224 static void end_cb1(DACDriver *dacp, dacsample_t *buffer, size_t n) {
225
226   (void)dacp;
227
228   nz++;
229   if (dac_buffer == buffer) {
230     nx += n;
231   }
232   else {
233     ny += n;
234   }
235
236   if ((nz % 1000) == 0) {
237     // palTogglePad(GPIOD, GPIOD_LED3);
238   }
239 }
240
241 /*
242  * DAC error callback.
243  */
244 static void error_cb1(DACDriver *dacp, dacerror_t err) {
245
246   (void)dacp;
247   (void)err;
248
249   chSysHalt("DAC failure");
250 }
251
252 static const DACConfig dac1cfg1 = {
253   .init         = DAC_SAMPLE_MAX,
254   .datamode     = DAC_DHRM_12BIT_RIGHT
255 };
256
257 static const DACConversionGroup dacgrpcfg1 = {
258   .num_channels = 1U,
259   .end_cb       = end_cb1,
260   .error_cb     = error_cb1,
261   .trigger      = DAC_TRG(0)
262 };
263
264 static const DACConfig dac1cfg2 = {
265   .init         = DAC_SAMPLE_MAX,
266   .datamode     = DAC_DHRM_12BIT_RIGHT
267 };
268
269 static const DACConversionGroup dacgrpcfg2 = {
270   .num_channels = 1U,
271   .end_cb       = end_cb1,
272   .error_cb     = error_cb1,
273   .trigger      = DAC_TRG(0)
274 };
275
276 void audio_init() {
277
278   if (audio_initialized) {
279     return;
280   }
281
282   // Check EEPROM
283   #if defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE)
284     if (!eeconfig_is_enabled()) {
285       eeconfig_init();
286     }
287     audio_config.raw = eeconfig_read_audio();
288 #else // ARM EEPROM
289     audio_config.enable = true;
290   #ifdef AUDIO_CLICKY_ON
291     audio_config.clicky_enable = true;
292   #endif
293 #endif // ARM EEPROM
294
295   /*
296    * Starting DAC1 driver, setting up the output pin as analog as suggested
297    * by the Reference Manual.
298    */
299   palSetPadMode(GPIOA, 4, PAL_MODE_INPUT_ANALOG);
300   palSetPadMode(GPIOA, 5, PAL_MODE_INPUT_ANALOG);
301   dacStart(&DACD1, &dac1cfg1);
302   dacStart(&DACD2, &dac1cfg2);
303
304   /*
305    * Starting GPT6/7 driver, it is used for triggering the DAC.
306    */
307   START_CHANNEL_1();
308   START_CHANNEL_2();
309
310   /*
311    * Starting a continuous conversion.
312    */
313   dacStartConversion(&DACD1, &dacgrpcfg1, (dacsample_t *)dac_buffer, DAC_BUFFER_SIZE);
314   dacStartConversion(&DACD2, &dacgrpcfg2, (dacsample_t *)dac_buffer_2, DAC_BUFFER_SIZE);
315
316   audio_initialized = true;
317
318   if (audio_config.enable) {
319     PLAY_SONG(startup_song);
320   } else {
321     stop_all_notes();
322   }
323
324 }
325
326 void stop_all_notes() {
327     dprintf("audio stop all notes");
328
329     if (!audio_initialized) {
330         audio_init();
331     }
332     voices = 0;
333
334     gptStopTimer(&GPTD6);
335     gptStopTimer(&GPTD7);
336     gptStopTimer(&GPTD8);
337
338     playing_notes = false;
339     playing_note = false;
340     frequency = 0;
341     frequency_alt = 0;
342     volume = 0;
343
344     for (uint8_t i = 0; i < 8; i++)
345     {
346         frequencies[i] = 0;
347         volumes[i] = 0;
348     }
349 }
350
351 void stop_note(float freq) {
352   dprintf("audio stop note freq=%d", (int)freq);
353
354   if (playing_note) {
355     if (!audio_initialized) {
356       audio_init();
357     }
358     for (int i = 7; i >= 0; i--) {
359       if (frequencies[i] == freq) {
360         frequencies[i] = 0;
361         volumes[i] = 0;
362         for (int j = i; (j < 7); j++) {
363           frequencies[j] = frequencies[j+1];
364           frequencies[j+1] = 0;
365           volumes[j] = volumes[j+1];
366           volumes[j+1] = 0;
367         }
368         break;
369       }
370     }
371     voices--;
372     if (voices < 0) {
373       voices = 0;
374     }
375     if (voice_place >= voices) {
376       voice_place = 0;
377     }
378     if (voices == 0) {
379       STOP_CHANNEL_1();
380       STOP_CHANNEL_2();
381       gptStopTimer(&GPTD8);
382       frequency = 0;
383       frequency_alt = 0;
384       volume = 0;
385       playing_note = false;
386     }
387   }
388 }
389
390 #ifdef VIBRATO_ENABLE
391
392 float mod(float a, int b) {
393   float r = fmod(a, b);
394   return r < 0 ? r + b : r;
395 }
396
397 float vibrato(float average_freq) {
398   #ifdef VIBRATO_STRENGTH_ENABLE
399     float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
400   #else
401     float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
402   #endif
403   vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH);
404   return vibrated_freq;
405 }
406
407 #endif
408
409 static void gpt_cb8(GPTDriver *gptp) {
410   float freq;
411
412   if (playing_note) {
413     if (voices > 0) {
414
415       float freq_alt = 0;
416       if (voices > 1) {
417         if (polyphony_rate == 0) {
418           if (glissando) {
419             if (frequency_alt != 0 && frequency_alt < frequencies[voices - 2] && frequency_alt < frequencies[voices - 2] * pow(2, -440/frequencies[voices - 2]/12/2)) {
420               frequency_alt = frequency_alt * pow(2, 440/frequency_alt/12/2);
421             } else if (frequency_alt != 0 && frequency_alt > frequencies[voices - 2] && frequency_alt > frequencies[voices - 2] * pow(2, 440/frequencies[voices - 2]/12/2)) {
422               frequency_alt = frequency_alt * pow(2, -440/frequency_alt/12/2);
423             } else {
424               frequency_alt = frequencies[voices - 2];
425             }
426           } else {
427             frequency_alt = frequencies[voices - 2];
428           }
429
430           #ifdef VIBRATO_ENABLE
431             if (vibrato_strength > 0) {
432               freq_alt = vibrato(frequency_alt);
433             } else {
434               freq_alt = frequency_alt;
435             }
436           #else
437             freq_alt = frequency_alt;
438           #endif
439         }
440
441         if (envelope_index < 65535) {
442           envelope_index++;
443         }
444
445         freq_alt = voice_envelope(freq_alt);
446
447         if (freq_alt < 30.517578125) {
448           freq_alt = 30.52;
449         }
450
451         if (GET_CHANNEL_2_FREQ != (uint16_t)freq_alt) {
452           UPDATE_CHANNEL_2_FREQ(freq_alt);
453         } else {
454           RESTART_CHANNEL_2();
455         }
456         //note_timbre;
457       }
458
459       if (polyphony_rate > 0) {
460         if (voices > 1) {
461           voice_place %= voices;
462           if (place++ > (frequencies[voice_place] / polyphony_rate)) {
463             voice_place = (voice_place + 1) % voices;
464             place = 0.0;
465           }
466         }
467
468         #ifdef VIBRATO_ENABLE
469           if (vibrato_strength > 0) {
470               freq = vibrato(frequencies[voice_place]);
471           } else {
472               freq = frequencies[voice_place];
473           }
474         #else
475           freq = frequencies[voice_place];
476         #endif
477       } else {
478         if (glissando) {
479           if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) {
480             frequency = frequency * pow(2, 440/frequency/12/2);
481           } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
482             frequency = frequency * pow(2, -440/frequency/12/2);
483           } else {
484             frequency = frequencies[voices - 1];
485           }
486         } else {
487           frequency = frequencies[voices - 1];
488         }
489
490         #ifdef VIBRATO_ENABLE
491           if (vibrato_strength > 0) {
492             freq = vibrato(frequency);
493           } else {
494             freq = frequency;
495           }
496         #else
497           freq = frequency;
498         #endif
499       }
500
501       if (envelope_index < 65535) {
502         envelope_index++;
503       }
504
505       freq = voice_envelope(freq);
506
507       if (freq < 30.517578125) {
508         freq = 30.52;
509       }
510
511
512       if (GET_CHANNEL_1_FREQ != (uint16_t)freq) {
513         UPDATE_CHANNEL_1_FREQ(freq);
514       } else {
515         RESTART_CHANNEL_1();
516       }
517       //note_timbre;
518     }
519   }
520
521   if (playing_notes) {
522     if (note_frequency > 0) {
523       #ifdef VIBRATO_ENABLE
524         if (vibrato_strength > 0) {
525           freq = vibrato(note_frequency);
526         } else {
527           freq = note_frequency;
528         }
529       #else
530         freq = note_frequency;
531       #endif
532
533       if (envelope_index < 65535) {
534         envelope_index++;
535       }
536       freq = voice_envelope(freq);
537
538
539       if (GET_CHANNEL_1_FREQ != (uint16_t)freq) {
540         UPDATE_CHANNEL_1_FREQ(freq);
541         UPDATE_CHANNEL_2_FREQ(freq);
542       }
543       //note_timbre;
544     } else {
545         // gptStopTimer(&GPTD6);
546         // gptStopTimer(&GPTD7);
547     }
548
549     note_position++;
550     bool end_of_note = false;
551     if (GET_CHANNEL_1_FREQ > 0) {
552       if (!note_resting)
553         end_of_note = (note_position >= (note_length*8 - 1));
554       else
555         end_of_note = (note_position >= (note_length*8));
556     } else {
557       end_of_note = (note_position >= (note_length*8));
558     }
559
560     if (end_of_note) {
561       current_note++;
562       if (current_note >= notes_count) {
563         if (notes_repeat) {
564           current_note = 0;
565         } else {
566           STOP_CHANNEL_1();
567           STOP_CHANNEL_2();
568           // gptStopTimer(&GPTD8);
569           playing_notes = false;
570           return;
571         }
572       }
573       if (!note_resting) {
574         note_resting = true;
575         current_note--;
576         if ((*notes_pointer)[current_note][0] == (*notes_pointer)[current_note + 1][0]) {
577           note_frequency = 0;
578           note_length = 1;
579         } else {
580           note_frequency = (*notes_pointer)[current_note][0];
581           note_length = 1;
582         }
583       } else {
584         note_resting = false;
585         envelope_index = 0;
586         note_frequency = (*notes_pointer)[current_note][0];
587         note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
588       }
589
590       note_position = 0;
591     }
592   }
593
594   if (!audio_config.enable) {
595     playing_notes = false;
596     playing_note = false;
597   }
598 }
599
600 void play_note(float freq, int vol) {
601
602   dprintf("audio play note freq=%d vol=%d", (int)freq, vol);
603
604   if (!audio_initialized) {
605       audio_init();
606   }
607
608   if (audio_config.enable && voices < 8) {
609
610      // Cancel notes if notes are playing
611     if (playing_notes) {
612       stop_all_notes();
613     }
614
615     playing_note = true;
616
617     envelope_index = 0;
618
619     if (freq > 0) {
620       frequencies[voices] = freq;
621       volumes[voices] = vol;
622       voices++;
623     }
624
625     gptStart(&GPTD8, &gpt8cfg1);
626     gptStartContinuous(&GPTD8, 2U);
627     RESTART_CHANNEL_1();
628     RESTART_CHANNEL_2();
629   }
630
631 }
632
633 void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat) {
634
635   if (!audio_initialized) {
636     audio_init();
637   }
638
639   if (audio_config.enable) {
640
641     // Cancel note if a note is playing
642     if (playing_note) {
643       stop_all_notes();
644     }
645
646     playing_notes = true;
647
648     notes_pointer = np;
649     notes_count = n_count;
650     notes_repeat = n_repeat;
651
652     place = 0;
653     current_note = 0;
654
655     note_frequency = (*notes_pointer)[current_note][0];
656     note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
657     note_position = 0;
658
659     gptStart(&GPTD8, &gpt8cfg1);
660     gptStartContinuous(&GPTD8, 2U);
661     RESTART_CHANNEL_1();
662     RESTART_CHANNEL_2();
663   }
664 }
665
666 bool is_playing_notes(void) {
667   return playing_notes;
668 }
669
670 bool is_audio_on(void) {
671   return (audio_config.enable != 0);
672 }
673
674 void audio_toggle(void) {
675   audio_config.enable ^= 1;
676   eeconfig_update_audio(audio_config.raw);
677   if (audio_config.enable) {
678     audio_on_user();
679   }
680 }
681
682 void audio_on(void) {
683   audio_config.enable = 1;
684   eeconfig_update_audio(audio_config.raw);
685   audio_on_user();
686 }
687
688 void audio_off(void) {
689   stop_all_notes();
690   audio_config.enable = 0;
691   eeconfig_update_audio(audio_config.raw);
692 }
693
694 #ifdef VIBRATO_ENABLE
695
696 // Vibrato rate functions
697
698 void set_vibrato_rate(float rate) {
699   vibrato_rate = rate;
700 }
701
702 void increase_vibrato_rate(float change) {
703   vibrato_rate *= change;
704 }
705
706 void decrease_vibrato_rate(float change) {
707   vibrato_rate /= change;
708 }
709
710 #ifdef VIBRATO_STRENGTH_ENABLE
711
712 void set_vibrato_strength(float strength) {
713   vibrato_strength = strength;
714 }
715
716 void increase_vibrato_strength(float change) {
717   vibrato_strength *= change;
718 }
719
720 void decrease_vibrato_strength(float change) {
721   vibrato_strength /= change;
722 }
723
724 #endif  /* VIBRATO_STRENGTH_ENABLE */
725
726 #endif /* VIBRATO_ENABLE */
727
728 // Polyphony functions
729
730 void set_polyphony_rate(float rate) {
731   polyphony_rate = rate;
732 }
733
734 void enable_polyphony() {
735   polyphony_rate = 5;
736 }
737
738 void disable_polyphony() {
739   polyphony_rate = 0;
740 }
741
742 void increase_polyphony_rate(float change) {
743   polyphony_rate *= change;
744 }
745
746 void decrease_polyphony_rate(float change) {
747   polyphony_rate /= change;
748 }
749
750 // Timbre function
751
752 void set_timbre(float timbre) {
753   note_timbre = timbre;
754 }
755
756 // Tempo functions
757
758 void set_tempo(uint8_t tempo) {
759   note_tempo = tempo;
760 }
761
762 void decrease_tempo(uint8_t tempo_change) {
763   note_tempo += tempo_change;
764 }
765
766 void increase_tempo(uint8_t tempo_change) {
767   if (note_tempo - tempo_change < 10) {
768     note_tempo = 10;
769   } else {
770     note_tempo -= tempo_change;
771   }
772 }