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