2020 February 29 Breaking Changes Update (#8064)
[jackhill/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 100
83 #ifndef DAC_SAMPLE_MAX
84 # define DAC_SAMPLE_MAX 65535U
85 #endif
86
87 #define START_CHANNEL_1() \
88 gptStart(&GPTD6, &gpt6cfg1); \
89 gptStartContinuous(&GPTD6, 2U)
90 #define START_CHANNEL_2() \
91 gptStart(&GPTD7, &gpt7cfg1); \
92 gptStartContinuous(&GPTD7, 2U)
93 #define STOP_CHANNEL_1() gptStopTimer(&GPTD6)
94 #define STOP_CHANNEL_2() gptStopTimer(&GPTD7)
95 #define RESTART_CHANNEL_1() \
96 STOP_CHANNEL_1(); \
97 START_CHANNEL_1()
98 #define RESTART_CHANNEL_2() \
99 STOP_CHANNEL_2(); \
100 START_CHANNEL_2()
101 #define UPDATE_CHANNEL_1_FREQ(freq) \
102 gpt6cfg1.frequency = freq * DAC_BUFFER_SIZE; \
103 RESTART_CHANNEL_1()
104 #define UPDATE_CHANNEL_2_FREQ(freq) \
105 gpt7cfg1.frequency = freq * DAC_BUFFER_SIZE; \
106 RESTART_CHANNEL_2()
107 #define GET_CHANNEL_1_FREQ (uint16_t)(gpt6cfg1.frequency * DAC_BUFFER_SIZE)
108 #define GET_CHANNEL_2_FREQ (uint16_t)(gpt7cfg1.frequency * DAC_BUFFER_SIZE)
109
110 /*
111 * GPT6 configuration.
112 */
113 // static const GPTConfig gpt6cfg1 = {
114 // .frequency = 1000000U,
115 // .callback = NULL,
116 // .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
117 // .dier = 0U
118 // };
119
120 GPTConfig gpt6cfg1 = {.frequency = 440U * DAC_BUFFER_SIZE,
121 .callback = NULL,
122 .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
123 .dier = 0U};
124
125 GPTConfig gpt7cfg1 = {.frequency = 440U * DAC_BUFFER_SIZE,
126 .callback = NULL,
127 .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
128 .dier = 0U};
129
130 GPTConfig gpt8cfg1 = {.frequency = 10,
131 .callback = gpt_cb8,
132 .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
133 .dier = 0U};
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 nz = 0;
222 static void end_cb1(DACDriver *dacp) {
223 (void)dacp;
224
225 nz++;
226 if ((nz % 1000) == 0) {
227 // palTogglePad(GPIOD, GPIOD_LED3);
228 }
229 }
230
231 /*
232 * DAC error callback.
233 */
234 static void error_cb1(DACDriver *dacp, dacerror_t err) {
235 (void)dacp;
236 (void)err;
237
238 chSysHalt("DAC failure");
239 }
240
241 static const DACConfig dac1cfg1 = {.init = DAC_SAMPLE_MAX, .datamode = DAC_DHRM_12BIT_RIGHT};
242
243 static const DACConversionGroup dacgrpcfg1 = {.num_channels = 1U, .end_cb = end_cb1, .error_cb = error_cb1, .trigger = DAC_TRG(0)};
244
245 static const DACConfig dac1cfg2 = {.init = DAC_SAMPLE_MAX, .datamode = DAC_DHRM_12BIT_RIGHT};
246
247 static const DACConversionGroup dacgrpcfg2 = {.num_channels = 1U, .end_cb = end_cb1, .error_cb = error_cb1, .trigger = DAC_TRG(0)};
248
249 void audio_init() {
250 if (audio_initialized) {
251 return;
252 }
253
254 // Check EEPROM
255 #ifdef EEPROM_ENABLE
256 if (!eeconfig_is_enabled()) {
257 eeconfig_init();
258 }
259 audio_config.raw = eeconfig_read_audio();
260 #else // ARM EEPROM
261 audio_config.enable = true;
262 # ifdef AUDIO_CLICKY_ON
263 audio_config.clicky_enable = true;
264 # endif
265 #endif // ARM EEPROM
266
267 /*
268 * Starting DAC1 driver, setting up the output pin as analog as suggested
269 * by the Reference Manual.
270 */
271 palSetPadMode(GPIOA, 4, PAL_MODE_INPUT_ANALOG);
272 palSetPadMode(GPIOA, 5, PAL_MODE_INPUT_ANALOG);
273 dacStart(&DACD1, &dac1cfg1);
274 dacStart(&DACD2, &dac1cfg2);
275
276 /*
277 * Starting GPT6/7 driver, it is used for triggering the DAC.
278 */
279 START_CHANNEL_1();
280 START_CHANNEL_2();
281
282 /*
283 * Starting a continuous conversion.
284 */
285 dacStartConversion(&DACD1, &dacgrpcfg1, (dacsample_t *)dac_buffer, DAC_BUFFER_SIZE);
286 dacStartConversion(&DACD2, &dacgrpcfg2, (dacsample_t *)dac_buffer_2, DAC_BUFFER_SIZE);
287
288 audio_initialized = true;
289
290 if (audio_config.enable) {
291 PLAY_SONG(startup_song);
292 } else {
293 stop_all_notes();
294 }
295 }
296
297 void stop_all_notes() {
298 dprintf("audio stop all notes");
299
300 if (!audio_initialized) {
301 audio_init();
302 }
303 voices = 0;
304
305 gptStopTimer(&GPTD6);
306 gptStopTimer(&GPTD7);
307 gptStopTimer(&GPTD8);
308
309 playing_notes = false;
310 playing_note = false;
311 frequency = 0;
312 frequency_alt = 0;
313 volume = 0;
314
315 for (uint8_t i = 0; i < 8; i++) {
316 frequencies[i] = 0;
317 volumes[i] = 0;
318 }
319 }
320
321 void stop_note(float freq) {
322 dprintf("audio stop note freq=%d", (int)freq);
323
324 if (playing_note) {
325 if (!audio_initialized) {
326 audio_init();
327 }
328 for (int i = 7; i >= 0; i--) {
329 if (frequencies[i] == freq) {
330 frequencies[i] = 0;
331 volumes[i] = 0;
332 for (int j = i; (j < 7); j++) {
333 frequencies[j] = frequencies[j + 1];
334 frequencies[j + 1] = 0;
335 volumes[j] = volumes[j + 1];
336 volumes[j + 1] = 0;
337 }
338 break;
339 }
340 }
341 voices--;
342 if (voices < 0) {
343 voices = 0;
344 }
345 if (voice_place >= voices) {
346 voice_place = 0;
347 }
348 if (voices == 0) {
349 STOP_CHANNEL_1();
350 STOP_CHANNEL_2();
351 gptStopTimer(&GPTD8);
352 frequency = 0;
353 frequency_alt = 0;
354 volume = 0;
355 playing_note = false;
356 }
357 }
358 }
359
360 #ifdef VIBRATO_ENABLE
361
362 float mod(float a, int b) {
363 float r = fmod(a, b);
364 return r < 0 ? r + b : r;
365 }
366
367 float vibrato(float average_freq) {
368 # ifdef VIBRATO_STRENGTH_ENABLE
369 float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
370 # else
371 float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
372 # endif
373 vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0 / average_freq)), VIBRATO_LUT_LENGTH);
374 return vibrated_freq;
375 }
376
377 #endif
378
379 static void gpt_cb8(GPTDriver *gptp) {
380 float freq;
381
382 if (playing_note) {
383 if (voices > 0) {
384 float freq_alt = 0;
385 if (voices > 1) {
386 if (polyphony_rate == 0) {
387 if (glissando) {
388 if (frequency_alt != 0 && frequency_alt < frequencies[voices - 2] && frequency_alt < frequencies[voices - 2] * pow(2, -440 / frequencies[voices - 2] / 12 / 2)) {
389 frequency_alt = frequency_alt * pow(2, 440 / frequency_alt / 12 / 2);
390 } else if (frequency_alt != 0 && frequency_alt > frequencies[voices - 2] && frequency_alt > frequencies[voices - 2] * pow(2, 440 / frequencies[voices - 2] / 12 / 2)) {
391 frequency_alt = frequency_alt * pow(2, -440 / frequency_alt / 12 / 2);
392 } else {
393 frequency_alt = frequencies[voices - 2];
394 }
395 } else {
396 frequency_alt = frequencies[voices - 2];
397 }
398
399 #ifdef VIBRATO_ENABLE
400 if (vibrato_strength > 0) {
401 freq_alt = vibrato(frequency_alt);
402 } else {
403 freq_alt = frequency_alt;
404 }
405 #else
406 freq_alt = frequency_alt;
407 #endif
408 }
409
410 if (envelope_index < 65535) {
411 envelope_index++;
412 }
413
414 freq_alt = voice_envelope(freq_alt);
415
416 if (freq_alt < 30.517578125) {
417 freq_alt = 30.52;
418 }
419
420 if (GET_CHANNEL_2_FREQ != (uint16_t)freq_alt) {
421 UPDATE_CHANNEL_2_FREQ(freq_alt);
422 } else {
423 RESTART_CHANNEL_2();
424 }
425 // note_timbre;
426 }
427
428 if (polyphony_rate > 0) {
429 if (voices > 1) {
430 voice_place %= voices;
431 if (place++ > (frequencies[voice_place] / polyphony_rate)) {
432 voice_place = (voice_place + 1) % voices;
433 place = 0.0;
434 }
435 }
436
437 #ifdef VIBRATO_ENABLE
438 if (vibrato_strength > 0) {
439 freq = vibrato(frequencies[voice_place]);
440 } else {
441 freq = frequencies[voice_place];
442 }
443 #else
444 freq = frequencies[voice_place];
445 #endif
446 } else {
447 if (glissando) {
448 if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440 / frequencies[voices - 1] / 12 / 2)) {
449 frequency = frequency * pow(2, 440 / frequency / 12 / 2);
450 } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440 / frequencies[voices - 1] / 12 / 2)) {
451 frequency = frequency * pow(2, -440 / frequency / 12 / 2);
452 } else {
453 frequency = frequencies[voices - 1];
454 }
455 } else {
456 frequency = frequencies[voices - 1];
457 }
458
459 #ifdef VIBRATO_ENABLE
460 if (vibrato_strength > 0) {
461 freq = vibrato(frequency);
462 } else {
463 freq = frequency;
464 }
465 #else
466 freq = frequency;
467 #endif
468 }
469
470 if (envelope_index < 65535) {
471 envelope_index++;
472 }
473
474 freq = voice_envelope(freq);
475
476 if (freq < 30.517578125) {
477 freq = 30.52;
478 }
479
480 if (GET_CHANNEL_1_FREQ != (uint16_t)freq) {
481 UPDATE_CHANNEL_1_FREQ(freq);
482 } else {
483 RESTART_CHANNEL_1();
484 }
485 // note_timbre;
486 }
487 }
488
489 if (playing_notes) {
490 if (note_frequency > 0) {
491 #ifdef VIBRATO_ENABLE
492 if (vibrato_strength > 0) {
493 freq = vibrato(note_frequency);
494 } else {
495 freq = note_frequency;
496 }
497 #else
498 freq = note_frequency;
499 #endif
500
501 if (envelope_index < 65535) {
502 envelope_index++;
503 }
504 freq = voice_envelope(freq);
505
506 if (GET_CHANNEL_1_FREQ != (uint16_t)freq) {
507 UPDATE_CHANNEL_1_FREQ(freq);
508 UPDATE_CHANNEL_2_FREQ(freq);
509 }
510 // note_timbre;
511 } else {
512 // gptStopTimer(&GPTD6);
513 // gptStopTimer(&GPTD7);
514 }
515
516 note_position++;
517 bool end_of_note = false;
518 if (GET_CHANNEL_1_FREQ > 0) {
519 if (!note_resting)
520 end_of_note = (note_position >= (note_length * 8 - 1));
521 else
522 end_of_note = (note_position >= (note_length * 8));
523 } else {
524 end_of_note = (note_position >= (note_length * 8));
525 }
526
527 if (end_of_note) {
528 current_note++;
529 if (current_note >= notes_count) {
530 if (notes_repeat) {
531 current_note = 0;
532 } else {
533 STOP_CHANNEL_1();
534 STOP_CHANNEL_2();
535 // gptStopTimer(&GPTD8);
536 playing_notes = false;
537 return;
538 }
539 }
540 if (!note_resting) {
541 note_resting = true;
542 current_note--;
543 if ((*notes_pointer)[current_note][0] == (*notes_pointer)[current_note + 1][0]) {
544 note_frequency = 0;
545 note_length = 1;
546 } else {
547 note_frequency = (*notes_pointer)[current_note][0];
548 note_length = 1;
549 }
550 } else {
551 note_resting = false;
552 envelope_index = 0;
553 note_frequency = (*notes_pointer)[current_note][0];
554 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
555 }
556
557 note_position = 0;
558 }
559 }
560
561 if (!audio_config.enable) {
562 playing_notes = false;
563 playing_note = false;
564 }
565 }
566
567 void play_note(float freq, int vol) {
568 dprintf("audio play note freq=%d vol=%d", (int)freq, vol);
569
570 if (!audio_initialized) {
571 audio_init();
572 }
573
574 if (audio_config.enable && voices < 8) {
575 // Cancel notes if notes are playing
576 if (playing_notes) {
577 stop_all_notes();
578 }
579
580 playing_note = true;
581
582 envelope_index = 0;
583
584 if (freq > 0) {
585 frequencies[voices] = freq;
586 volumes[voices] = vol;
587 voices++;
588 }
589
590 gptStart(&GPTD8, &gpt8cfg1);
591 gptStartContinuous(&GPTD8, 2U);
592 RESTART_CHANNEL_1();
593 RESTART_CHANNEL_2();
594 }
595 }
596
597 void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat) {
598 if (!audio_initialized) {
599 audio_init();
600 }
601
602 if (audio_config.enable) {
603 // Cancel note if a note is playing
604 if (playing_note) {
605 stop_all_notes();
606 }
607
608 playing_notes = true;
609
610 notes_pointer = np;
611 notes_count = n_count;
612 notes_repeat = n_repeat;
613
614 place = 0;
615 current_note = 0;
616
617 note_frequency = (*notes_pointer)[current_note][0];
618 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
619 note_position = 0;
620
621 gptStart(&GPTD8, &gpt8cfg1);
622 gptStartContinuous(&GPTD8, 2U);
623 RESTART_CHANNEL_1();
624 RESTART_CHANNEL_2();
625 }
626 }
627
628 bool is_playing_notes(void) { return playing_notes; }
629
630 bool is_audio_on(void) { return (audio_config.enable != 0); }
631
632 void audio_toggle(void) {
633 audio_config.enable ^= 1;
634 eeconfig_update_audio(audio_config.raw);
635 if (audio_config.enable) {
636 audio_on_user();
637 }
638 }
639
640 void audio_on(void) {
641 audio_config.enable = 1;
642 eeconfig_update_audio(audio_config.raw);
643 audio_on_user();
644 }
645
646 void audio_off(void) {
647 stop_all_notes();
648 audio_config.enable = 0;
649 eeconfig_update_audio(audio_config.raw);
650 }
651
652 #ifdef VIBRATO_ENABLE
653
654 // Vibrato rate functions
655
656 void set_vibrato_rate(float rate) { vibrato_rate = rate; }
657
658 void increase_vibrato_rate(float change) { vibrato_rate *= change; }
659
660 void decrease_vibrato_rate(float change) { vibrato_rate /= change; }
661
662 # ifdef VIBRATO_STRENGTH_ENABLE
663
664 void set_vibrato_strength(float strength) { vibrato_strength = strength; }
665
666 void increase_vibrato_strength(float change) { vibrato_strength *= change; }
667
668 void decrease_vibrato_strength(float change) { vibrato_strength /= change; }
669
670 # endif /* VIBRATO_STRENGTH_ENABLE */
671
672 #endif /* VIBRATO_ENABLE */
673
674 // Polyphony functions
675
676 void set_polyphony_rate(float rate) { polyphony_rate = rate; }
677
678 void enable_polyphony() { polyphony_rate = 5; }
679
680 void disable_polyphony() { polyphony_rate = 0; }
681
682 void increase_polyphony_rate(float change) { polyphony_rate *= change; }
683
684 void decrease_polyphony_rate(float change) { polyphony_rate /= change; }
685
686 // Timbre function
687
688 void set_timbre(float timbre) { note_timbre = timbre; }
689
690 // Tempo functions
691
692 void set_tempo(uint8_t tempo) { note_tempo = tempo; }
693
694 void decrease_tempo(uint8_t tempo_change) { note_tempo += tempo_change; }
695
696 void increase_tempo(uint8_t tempo_change) {
697 if (note_tempo - tempo_change < 10) {
698 note_tempo = 10;
699 } else {
700 note_tempo -= tempo_change;
701 }
702 }