]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/audio/audio_arm.c
Make ARM Audio max volume configurable (#4540)
[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 uint8_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
279     if (audio_initialized)
280         return;
281
282     // Check EEPROM
283     // if (!eeconfig_is_enabled())
284     // {
285     //     eeconfig_init();
286     // }
287     // audio_config.raw = eeconfig_read_audio();
288     audio_config.enable = true;
289
290   /*
291    * Starting DAC1 driver, setting up the output pin as analog as suggested
292    * by the Reference Manual.
293    */
294   palSetPadMode(GPIOA, 4, PAL_MODE_INPUT_ANALOG);
295   palSetPadMode(GPIOA, 5, PAL_MODE_INPUT_ANALOG);
296   dacStart(&DACD1, &dac1cfg1);
297   dacStart(&DACD2, &dac1cfg2);
298
299   /*
300    * Starting GPT6/7 driver, it is used for triggering the DAC.
301    */
302   START_CHANNEL_1();
303   START_CHANNEL_2();
304
305   /*
306    * Starting a continuous conversion.
307    */
308   dacStartConversion(&DACD1, &dacgrpcfg1, (dacsample_t *)dac_buffer, DAC_BUFFER_SIZE);
309   dacStartConversion(&DACD2, &dacgrpcfg2, (dacsample_t *)dac_buffer_2, DAC_BUFFER_SIZE);
310
311     audio_initialized = true;
312
313     if (audio_config.enable) {
314         PLAY_SONG(startup_song);
315     }
316
317 }
318
319 void stop_all_notes()
320 {
321     dprintf("audio stop all notes");
322
323     if (!audio_initialized) {
324         audio_init();
325     }
326     voices = 0;
327
328     gptStopTimer(&GPTD6);
329     gptStopTimer(&GPTD7);
330     gptStopTimer(&GPTD8);
331
332     playing_notes = false;
333     playing_note = false;
334     frequency = 0;
335     frequency_alt = 0;
336     volume = 0;
337
338     for (uint8_t i = 0; i < 8; i++)
339     {
340         frequencies[i] = 0;
341         volumes[i] = 0;
342     }
343 }
344
345 void stop_note(float freq)
346 {
347     dprintf("audio stop note freq=%d", (int)freq);
348
349     if (playing_note) {
350         if (!audio_initialized) {
351             audio_init();
352         }
353         for (int i = 7; i >= 0; i--) {
354             if (frequencies[i] == freq) {
355                 frequencies[i] = 0;
356                 volumes[i] = 0;
357                 for (int j = i; (j < 7); j++) {
358                     frequencies[j] = frequencies[j+1];
359                     frequencies[j+1] = 0;
360                     volumes[j] = volumes[j+1];
361                     volumes[j+1] = 0;
362                 }
363                 break;
364             }
365         }
366         voices--;
367         if (voices < 0)
368             voices = 0;
369         if (voice_place >= voices) {
370             voice_place = 0;
371         }
372         if (voices == 0) {
373             STOP_CHANNEL_1();
374             STOP_CHANNEL_2();
375             gptStopTimer(&GPTD8);
376             frequency = 0;
377             frequency_alt = 0;
378             volume = 0;
379             playing_note = false;
380         }
381     }
382 }
383
384 #ifdef VIBRATO_ENABLE
385
386 float mod(float a, int b)
387 {
388     float r = fmod(a, b);
389     return r < 0 ? r + b : r;
390 }
391
392 float vibrato(float average_freq) {
393     #ifdef VIBRATO_STRENGTH_ENABLE
394         float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
395     #else
396         float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
397     #endif
398     vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH);
399     return vibrated_freq;
400 }
401
402 #endif
403
404 static void gpt_cb8(GPTDriver *gptp) {
405     float freq;
406
407     if (playing_note) {
408         if (voices > 0) {
409
410             float freq_alt = 0;
411                 if (voices > 1) {
412                     if (polyphony_rate == 0) {
413                         if (glissando) {
414                             if (frequency_alt != 0 && frequency_alt < frequencies[voices - 2] && frequency_alt < frequencies[voices - 2] * pow(2, -440/frequencies[voices - 2]/12/2)) {
415                                 frequency_alt = frequency_alt * pow(2, 440/frequency_alt/12/2);
416                             } else if (frequency_alt != 0 && frequency_alt > frequencies[voices - 2] && frequency_alt > frequencies[voices - 2] * pow(2, 440/frequencies[voices - 2]/12/2)) {
417                                 frequency_alt = frequency_alt * pow(2, -440/frequency_alt/12/2);
418                             } else {
419                                 frequency_alt = frequencies[voices - 2];
420                             }
421                         } else {
422                             frequency_alt = frequencies[voices - 2];
423                         }
424
425                         #ifdef VIBRATO_ENABLE
426                             if (vibrato_strength > 0) {
427                                 freq_alt = vibrato(frequency_alt);
428                             } else {
429                                 freq_alt = frequency_alt;
430                             }
431                         #else
432                             freq_alt = frequency_alt;
433                         #endif
434                     }
435
436                     if (envelope_index < 65535) {
437                         envelope_index++;
438                     }
439
440                     freq_alt = voice_envelope(freq_alt);
441
442                     if (freq_alt < 30.517578125) {
443                         freq_alt = 30.52;
444                     }
445
446                     if (GET_CHANNEL_2_FREQ != (uint16_t)freq_alt) {
447                         UPDATE_CHANNEL_2_FREQ(freq_alt);
448                     } else {
449                         RESTART_CHANNEL_2();
450                     }
451                     //note_timbre;
452                 }
453
454             if (polyphony_rate > 0) {
455                 if (voices > 1) {
456                     voice_place %= voices;
457                     if (place++ > (frequencies[voice_place] / polyphony_rate)) {
458                         voice_place = (voice_place + 1) % voices;
459                         place = 0.0;
460                     }
461                 }
462
463                 #ifdef VIBRATO_ENABLE
464                     if (vibrato_strength > 0) {
465                         freq = vibrato(frequencies[voice_place]);
466                     } else {
467                         freq = frequencies[voice_place];
468                     }
469                 #else
470                     freq = frequencies[voice_place];
471                 #endif
472             } else {
473                 if (glissando) {
474                     if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) {
475                         frequency = frequency * pow(2, 440/frequency/12/2);
476                     } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
477                         frequency = frequency * pow(2, -440/frequency/12/2);
478                     } else {
479                         frequency = frequencies[voices - 1];
480                     }
481                 } else {
482                     frequency = frequencies[voices - 1];
483                 }
484
485                 #ifdef VIBRATO_ENABLE
486                     if (vibrato_strength > 0) {
487                         freq = vibrato(frequency);
488                     } else {
489                         freq = frequency;
490                     }
491                 #else
492                     freq = frequency;
493                 #endif
494             }
495
496             if (envelope_index < 65535) {
497                 envelope_index++;
498             }
499
500             freq = voice_envelope(freq);
501
502             if (freq < 30.517578125) {
503                 freq = 30.52;
504             }
505
506
507             if (GET_CHANNEL_1_FREQ != (uint16_t)freq) {
508                 UPDATE_CHANNEL_1_FREQ(freq);
509             } else {
510                 RESTART_CHANNEL_1();
511             }
512             //note_timbre;
513         }
514     }
515
516     if (playing_notes) {
517         if (note_frequency > 0) {
518             #ifdef VIBRATO_ENABLE
519                 if (vibrato_strength > 0) {
520                     freq = vibrato(note_frequency);
521                 } else {
522                     freq = note_frequency;
523                 }
524             #else
525                     freq = note_frequency;
526             #endif
527
528             if (envelope_index < 65535) {
529                 envelope_index++;
530             }
531             freq = voice_envelope(freq);
532
533
534             if (GET_CHANNEL_1_FREQ != (uint16_t)freq) {
535                 UPDATE_CHANNEL_1_FREQ(freq);
536                 UPDATE_CHANNEL_2_FREQ(freq);
537             }
538             //note_timbre;
539         } else {
540             // gptStopTimer(&GPTD6);
541             // gptStopTimer(&GPTD7);
542         }
543
544         note_position++;
545         bool end_of_note = false;
546         if (GET_CHANNEL_1_FREQ > 0) {
547             if (!note_resting)
548                 end_of_note = (note_position >= (note_length*8 - 1));
549             else
550                 end_of_note = (note_position >= (note_length*8));
551         } else {
552             end_of_note = (note_position >= (note_length*8));
553         }
554
555         if (end_of_note) {
556             current_note++;
557             if (current_note >= notes_count) {
558                 if (notes_repeat) {
559                     current_note = 0;
560                 } else {
561                     STOP_CHANNEL_1();
562                     STOP_CHANNEL_2();
563                     // gptStopTimer(&GPTD8);
564                     playing_notes = false;
565                     return;
566                 }
567             }
568             if (!note_resting) {
569                 note_resting = true;
570                 current_note--;
571                 if ((*notes_pointer)[current_note][0] == (*notes_pointer)[current_note + 1][0]) {
572                     note_frequency = 0;
573                     note_length = 1;
574                 } else {
575                     note_frequency = (*notes_pointer)[current_note][0];
576                     note_length = 1;
577                 }
578             } else {
579                 note_resting = false;
580                 envelope_index = 0;
581                 note_frequency = (*notes_pointer)[current_note][0];
582                 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
583             }
584
585             note_position = 0;
586         }
587     }
588
589     if (!audio_config.enable) {
590         playing_notes = false;
591         playing_note = false;
592     }
593 }
594
595 void play_note(float freq, int vol) {
596
597     dprintf("audio play note freq=%d vol=%d", (int)freq, vol);
598
599     if (!audio_initialized) {
600         audio_init();
601     }
602
603     if (audio_config.enable && voices < 8) {
604
605
606         // Cancel notes if notes are playing
607         if (playing_notes)
608             stop_all_notes();
609
610         playing_note = true;
611
612         envelope_index = 0;
613
614         if (freq > 0) {
615             frequencies[voices] = freq;
616             volumes[voices] = vol;
617             voices++;
618         }
619
620         gptStart(&GPTD8, &gpt8cfg1);
621         gptStartContinuous(&GPTD8, 2U);
622         RESTART_CHANNEL_1();
623         RESTART_CHANNEL_2();
624     }
625
626 }
627
628 void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat)
629 {
630
631     if (!audio_initialized) {
632         audio_init();
633     }
634
635     if (audio_config.enable) {
636
637         // Cancel note if a note is playing
638         if (playing_note)
639             stop_all_notes();
640
641         playing_notes = true;
642
643         notes_pointer = np;
644         notes_count = n_count;
645         notes_repeat = n_repeat;
646
647         place = 0;
648         current_note = 0;
649
650         note_frequency = (*notes_pointer)[current_note][0];
651         note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
652         note_position = 0;
653
654         gptStart(&GPTD8, &gpt8cfg1);
655         gptStartContinuous(&GPTD8, 2U);
656         RESTART_CHANNEL_1();
657         RESTART_CHANNEL_2();
658     }
659
660 }
661
662 bool is_playing_notes(void) {
663     return playing_notes;
664 }
665
666 bool is_audio_on(void) {
667     return (audio_config.enable != 0);
668 }
669
670 void audio_toggle(void) {
671     audio_config.enable ^= 1;
672     eeconfig_update_audio(audio_config.raw);
673     if (audio_config.enable)
674         audio_on_user();
675 }
676
677 void audio_on(void) {
678     audio_config.enable = 1;
679     eeconfig_update_audio(audio_config.raw);
680     audio_on_user();
681 }
682
683 void audio_off(void) {
684     audio_config.enable = 0;
685     eeconfig_update_audio(audio_config.raw);
686 }
687
688 #ifdef VIBRATO_ENABLE
689
690 // Vibrato rate functions
691
692 void set_vibrato_rate(float rate) {
693     vibrato_rate = rate;
694 }
695
696 void increase_vibrato_rate(float change) {
697     vibrato_rate *= change;
698 }
699
700 void decrease_vibrato_rate(float change) {
701     vibrato_rate /= change;
702 }
703
704 #ifdef VIBRATO_STRENGTH_ENABLE
705
706 void set_vibrato_strength(float strength) {
707     vibrato_strength = strength;
708 }
709
710 void increase_vibrato_strength(float change) {
711     vibrato_strength *= change;
712 }
713
714 void decrease_vibrato_strength(float change) {
715     vibrato_strength /= change;
716 }
717
718 #endif  /* VIBRATO_STRENGTH_ENABLE */
719
720 #endif /* VIBRATO_ENABLE */
721
722 // Polyphony functions
723
724 void set_polyphony_rate(float rate) {
725     polyphony_rate = rate;
726 }
727
728 void enable_polyphony() {
729     polyphony_rate = 5;
730 }
731
732 void disable_polyphony() {
733     polyphony_rate = 0;
734 }
735
736 void increase_polyphony_rate(float change) {
737     polyphony_rate *= change;
738 }
739
740 void decrease_polyphony_rate(float change) {
741     polyphony_rate /= change;
742 }
743
744 // Timbre function
745
746 void set_timbre(float timbre) {
747     note_timbre = timbre;
748 }
749
750 // Tempo functions
751
752 void set_tempo(uint8_t tempo) {
753     note_tempo = tempo;
754 }
755
756 void decrease_tempo(uint8_t tempo_change) {
757     note_tempo += tempo_change;
758 }
759
760 void increase_tempo(uint8_t tempo_change) {
761     if (note_tempo - tempo_change < 10) {
762         note_tempo = 10;
763     } else {
764         note_tempo -= tempo_change;
765     }
766 }