1 /* Copyright 2016 Jack Humbert
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.
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.
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/>.
27 // -----------------------------------------------------------------------------
32 float frequency_alt = 0;
36 float frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
37 int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
43 uint16_t sample_length = 0;
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];
55 bool note_resting = false;
57 uint16_t current_note = 0;
58 uint8_t rest_counter = 0;
61 float vibrato_counter = 0;
62 float vibrato_strength = .5;
63 float vibrato_rate = 0.125;
66 float polyphony_rate = 0;
68 static bool audio_initialized = false;
70 audio_config_t audio_config;
72 uint16_t envelope_index = 0;
73 bool glissando = true;
76 #define STARTUP_SONG SONG(STARTUP_SOUND)
78 float startup_song[][2] = STARTUP_SONG;
80 static void gpt_cb8(GPTDriver *gptp);
82 #define DAC_BUFFER_SIZE 100
83 #ifndef DAC_SAMPLE_MAX
84 #define DAC_SAMPLE_MAX 65535U
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(); \
95 #define RESTART_CHANNEL_2() STOP_CHANNEL_2(); \
97 #define UPDATE_CHANNEL_1_FREQ(freq) gpt6cfg1.frequency = freq * DAC_BUFFER_SIZE; \
99 #define UPDATE_CHANNEL_2_FREQ(freq) gpt7cfg1.frequency = freq * DAC_BUFFER_SIZE; \
101 #define GET_CHANNEL_1_FREQ (uint16_t)(gpt6cfg1.frequency * DAC_BUFFER_SIZE)
102 #define GET_CHANNEL_2_FREQ (uint16_t)(gpt7cfg1.frequency * DAC_BUFFER_SIZE)
106 * GPT6 configuration.
108 // static const GPTConfig gpt6cfg1 = {
109 // .frequency = 1000000U,
111 // .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
115 GPTConfig gpt6cfg1 = {
116 .frequency = 440U*DAC_BUFFER_SIZE,
118 .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
122 GPTConfig gpt7cfg1 = {
123 .frequency = 440U*DAC_BUFFER_SIZE,
125 .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
129 GPTConfig gpt8cfg1 = {
132 .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
138 * DAC test buffer (sine wave).
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
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
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,
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,
221 * DAC streaming callback.
223 size_t nx = 0, ny = 0, nz = 0;
224 static void end_cb1(DACDriver *dacp, dacsample_t *buffer, size_t n) {
229 if (dac_buffer == buffer) {
236 if ((nz % 1000) == 0) {
237 // palTogglePad(GPIOD, GPIOD_LED3);
242 * DAC error callback.
244 static void error_cb1(DACDriver *dacp, dacerror_t err) {
249 chSysHalt("DAC failure");
252 static const DACConfig dac1cfg1 = {
253 .init = DAC_SAMPLE_MAX,
254 .datamode = DAC_DHRM_12BIT_RIGHT
257 static const DACConversionGroup dacgrpcfg1 = {
260 .error_cb = error_cb1,
261 .trigger = DAC_TRG(0)
264 static const DACConfig dac1cfg2 = {
265 .init = DAC_SAMPLE_MAX,
266 .datamode = DAC_DHRM_12BIT_RIGHT
269 static const DACConversionGroup dacgrpcfg2 = {
272 .error_cb = error_cb1,
273 .trigger = DAC_TRG(0)
278 if (audio_initialized) {
283 #if defined(STM32_EEPROM_ENABLE) || defined(PROTOCOL_ARM_ATSAM) || defined(EEPROM_SIZE)
284 if (!eeconfig_is_enabled()) {
287 audio_config.raw = eeconfig_read_audio();
289 audio_config.enable = true;
290 #ifdef AUDIO_CLICKY_ON
291 audio_config.clicky_enable = true;
296 * Starting DAC1 driver, setting up the output pin as analog as suggested
297 * by the Reference Manual.
299 palSetPadMode(GPIOA, 4, PAL_MODE_INPUT_ANALOG);
300 palSetPadMode(GPIOA, 5, PAL_MODE_INPUT_ANALOG);
301 dacStart(&DACD1, &dac1cfg1);
302 dacStart(&DACD2, &dac1cfg2);
305 * Starting GPT6/7 driver, it is used for triggering the DAC.
311 * Starting a continuous conversion.
313 dacStartConversion(&DACD1, &dacgrpcfg1, (dacsample_t *)dac_buffer, DAC_BUFFER_SIZE);
314 dacStartConversion(&DACD2, &dacgrpcfg2, (dacsample_t *)dac_buffer_2, DAC_BUFFER_SIZE);
316 audio_initialized = true;
318 if (audio_config.enable) {
319 PLAY_SONG(startup_song);
326 void stop_all_notes() {
327 dprintf("audio stop all notes");
329 if (!audio_initialized) {
334 gptStopTimer(&GPTD6);
335 gptStopTimer(&GPTD7);
336 gptStopTimer(&GPTD8);
338 playing_notes = false;
339 playing_note = false;
344 for (uint8_t i = 0; i < 8; i++)
351 void stop_note(float freq) {
352 dprintf("audio stop note freq=%d", (int)freq);
355 if (!audio_initialized) {
358 for (int i = 7; i >= 0; i--) {
359 if (frequencies[i] == freq) {
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];
375 if (voice_place >= voices) {
381 gptStopTimer(&GPTD8);
385 playing_note = false;
390 #ifdef VIBRATO_ENABLE
392 float mod(float a, int b) {
393 float r = fmod(a, b);
394 return r < 0 ? r + b : r;
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);
401 float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
403 vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH);
404 return vibrated_freq;
409 static void gpt_cb8(GPTDriver *gptp) {
417 if (polyphony_rate == 0) {
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);
424 frequency_alt = frequencies[voices - 2];
427 frequency_alt = frequencies[voices - 2];
430 #ifdef VIBRATO_ENABLE
431 if (vibrato_strength > 0) {
432 freq_alt = vibrato(frequency_alt);
434 freq_alt = frequency_alt;
437 freq_alt = frequency_alt;
441 if (envelope_index < 65535) {
445 freq_alt = voice_envelope(freq_alt);
447 if (freq_alt < 30.517578125) {
451 if (GET_CHANNEL_2_FREQ != (uint16_t)freq_alt) {
452 UPDATE_CHANNEL_2_FREQ(freq_alt);
459 if (polyphony_rate > 0) {
461 voice_place %= voices;
462 if (place++ > (frequencies[voice_place] / polyphony_rate)) {
463 voice_place = (voice_place + 1) % voices;
468 #ifdef VIBRATO_ENABLE
469 if (vibrato_strength > 0) {
470 freq = vibrato(frequencies[voice_place]);
472 freq = frequencies[voice_place];
475 freq = frequencies[voice_place];
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);
484 frequency = frequencies[voices - 1];
487 frequency = frequencies[voices - 1];
490 #ifdef VIBRATO_ENABLE
491 if (vibrato_strength > 0) {
492 freq = vibrato(frequency);
501 if (envelope_index < 65535) {
505 freq = voice_envelope(freq);
507 if (freq < 30.517578125) {
512 if (GET_CHANNEL_1_FREQ != (uint16_t)freq) {
513 UPDATE_CHANNEL_1_FREQ(freq);
522 if (note_frequency > 0) {
523 #ifdef VIBRATO_ENABLE
524 if (vibrato_strength > 0) {
525 freq = vibrato(note_frequency);
527 freq = note_frequency;
530 freq = note_frequency;
533 if (envelope_index < 65535) {
536 freq = voice_envelope(freq);
539 if (GET_CHANNEL_1_FREQ != (uint16_t)freq) {
540 UPDATE_CHANNEL_1_FREQ(freq);
541 UPDATE_CHANNEL_2_FREQ(freq);
545 // gptStopTimer(&GPTD6);
546 // gptStopTimer(&GPTD7);
550 bool end_of_note = false;
551 if (GET_CHANNEL_1_FREQ > 0) {
553 end_of_note = (note_position >= (note_length*8 - 1));
555 end_of_note = (note_position >= (note_length*8));
557 end_of_note = (note_position >= (note_length*8));
562 if (current_note >= notes_count) {
568 // gptStopTimer(&GPTD8);
569 playing_notes = false;
576 if ((*notes_pointer)[current_note][0] == (*notes_pointer)[current_note + 1][0]) {
580 note_frequency = (*notes_pointer)[current_note][0];
584 note_resting = false;
586 note_frequency = (*notes_pointer)[current_note][0];
587 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
594 if (!audio_config.enable) {
595 playing_notes = false;
596 playing_note = false;
600 void play_note(float freq, int vol) {
602 dprintf("audio play note freq=%d vol=%d", (int)freq, vol);
604 if (!audio_initialized) {
608 if (audio_config.enable && voices < 8) {
610 // Cancel notes if notes are playing
620 frequencies[voices] = freq;
621 volumes[voices] = vol;
625 gptStart(&GPTD8, &gpt8cfg1);
626 gptStartContinuous(&GPTD8, 2U);
633 void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat) {
635 if (!audio_initialized) {
639 if (audio_config.enable) {
641 // Cancel note if a note is playing
646 playing_notes = true;
649 notes_count = n_count;
650 notes_repeat = n_repeat;
655 note_frequency = (*notes_pointer)[current_note][0];
656 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
659 gptStart(&GPTD8, &gpt8cfg1);
660 gptStartContinuous(&GPTD8, 2U);
666 bool is_playing_notes(void) {
667 return playing_notes;
670 bool is_audio_on(void) {
671 return (audio_config.enable != 0);
674 void audio_toggle(void) {
675 audio_config.enable ^= 1;
676 eeconfig_update_audio(audio_config.raw);
677 if (audio_config.enable) {
682 void audio_on(void) {
683 audio_config.enable = 1;
684 eeconfig_update_audio(audio_config.raw);
688 void audio_off(void) {
690 audio_config.enable = 0;
691 eeconfig_update_audio(audio_config.raw);
694 #ifdef VIBRATO_ENABLE
696 // Vibrato rate functions
698 void set_vibrato_rate(float rate) {
702 void increase_vibrato_rate(float change) {
703 vibrato_rate *= change;
706 void decrease_vibrato_rate(float change) {
707 vibrato_rate /= change;
710 #ifdef VIBRATO_STRENGTH_ENABLE
712 void set_vibrato_strength(float strength) {
713 vibrato_strength = strength;
716 void increase_vibrato_strength(float change) {
717 vibrato_strength *= change;
720 void decrease_vibrato_strength(float change) {
721 vibrato_strength /= change;
724 #endif /* VIBRATO_STRENGTH_ENABLE */
726 #endif /* VIBRATO_ENABLE */
728 // Polyphony functions
730 void set_polyphony_rate(float rate) {
731 polyphony_rate = rate;
734 void enable_polyphony() {
738 void disable_polyphony() {
742 void increase_polyphony_rate(float change) {
743 polyphony_rate *= change;
746 void decrease_polyphony_rate(float change) {
747 polyphony_rate /= change;
752 void set_timbre(float timbre) {
753 note_timbre = timbre;
758 void set_tempo(uint8_t tempo) {
762 void decrease_tempo(uint8_t tempo_change) {
763 note_tempo += tempo_change;
766 void increase_tempo(uint8_t tempo_change) {
767 if (note_tempo - tempo_change < 10) {
770 note_tempo -= tempo_change;