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 uint8_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 720
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 gpt6cfg1.frequency
102 #define GET_CHANNEL_2_FREQ gpt7cfg1.frequency
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)
279 if (audio_initialized)
283 // if (!eeconfig_is_enabled())
287 // audio_config.raw = eeconfig_read_audio();
288 audio_config.enable = true;
291 * Starting DAC1 driver, setting up the output pin as analog as suggested
292 * by the Reference Manual.
294 palSetPadMode(GPIOA, 4, PAL_MODE_INPUT_ANALOG);
295 palSetPadMode(GPIOA, 5, PAL_MODE_INPUT_ANALOG);
296 dacStart(&DACD1, &dac1cfg1);
297 dacStart(&DACD2, &dac1cfg2);
300 * Starting GPT6/7 driver, it is used for triggering the DAC.
306 * Starting a continuous conversion.
308 dacStartConversion(&DACD1, &dacgrpcfg1, (dacsample_t *)dac_buffer, DAC_BUFFER_SIZE);
309 dacStartConversion(&DACD2, &dacgrpcfg2, (dacsample_t *)dac_buffer_2, DAC_BUFFER_SIZE);
311 audio_initialized = true;
313 if (audio_config.enable) {
314 PLAY_SONG(startup_song);
319 void stop_all_notes()
321 dprintf("audio stop all notes");
323 if (!audio_initialized) {
328 gptStopTimer(&GPTD6);
329 gptStopTimer(&GPTD7);
330 gptStopTimer(&GPTD8);
332 playing_notes = false;
333 playing_note = false;
338 for (uint8_t i = 0; i < 8; i++)
345 void stop_note(float freq)
347 dprintf("audio stop note freq=%d", (int)freq);
350 if (!audio_initialized) {
353 for (int i = 7; i >= 0; i--) {
354 if (frequencies[i] == freq) {
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];
369 if (voice_place >= voices) {
375 gptStopTimer(&GPTD8);
379 playing_note = false;
384 #ifdef VIBRATO_ENABLE
386 float mod(float a, int b)
388 float r = fmod(a, b);
389 return r < 0 ? r + b : r;
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);
396 float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
398 vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH);
399 return vibrated_freq;
404 static void gpt_cb8(GPTDriver *gptp) {
412 if (polyphony_rate == 0) {
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);
419 frequency_alt = frequencies[voices - 2];
422 frequency_alt = frequencies[voices - 2];
425 #ifdef VIBRATO_ENABLE
426 if (vibrato_strength > 0) {
427 freq_alt = vibrato(frequency_alt);
429 freq_alt = frequency_alt;
432 freq_alt = frequency_alt;
436 if (envelope_index < 65535) {
440 freq_alt = voice_envelope(freq_alt);
442 if (freq_alt < 30.517578125) {
446 if (GET_CHANNEL_2_FREQ != (uint16_t)freq_alt) {
447 UPDATE_CHANNEL_2_FREQ(freq_alt);
454 if (polyphony_rate > 0) {
456 voice_place %= voices;
457 if (place++ > (frequencies[voice_place] / polyphony_rate)) {
458 voice_place = (voice_place + 1) % voices;
463 #ifdef VIBRATO_ENABLE
464 if (vibrato_strength > 0) {
465 freq = vibrato(frequencies[voice_place]);
467 freq = frequencies[voice_place];
470 freq = frequencies[voice_place];
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);
479 frequency = frequencies[voices - 1];
482 frequency = frequencies[voices - 1];
485 #ifdef VIBRATO_ENABLE
486 if (vibrato_strength > 0) {
487 freq = vibrato(frequency);
496 if (envelope_index < 65535) {
500 freq = voice_envelope(freq);
502 if (freq < 30.517578125) {
507 if (GET_CHANNEL_1_FREQ != (uint16_t)freq) {
508 UPDATE_CHANNEL_1_FREQ(freq);
517 if (note_frequency > 0) {
518 #ifdef VIBRATO_ENABLE
519 if (vibrato_strength > 0) {
520 freq = vibrato(note_frequency);
522 freq = note_frequency;
525 freq = note_frequency;
528 if (envelope_index < 65535) {
531 freq = voice_envelope(freq);
534 if (GET_CHANNEL_1_FREQ != (uint16_t)freq) {
535 UPDATE_CHANNEL_1_FREQ(freq);
536 UPDATE_CHANNEL_2_FREQ(freq);
540 // gptStopTimer(&GPTD6);
541 // gptStopTimer(&GPTD7);
545 bool end_of_note = false;
546 if (GET_CHANNEL_1_FREQ > 0) {
548 end_of_note = (note_position >= (note_length*8 - 1));
550 end_of_note = (note_position >= (note_length*8));
552 end_of_note = (note_position >= (note_length*8));
557 if (current_note >= notes_count) {
563 // gptStopTimer(&GPTD8);
564 playing_notes = false;
571 if ((*notes_pointer)[current_note][0] == (*notes_pointer)[current_note + 1][0]) {
575 note_frequency = (*notes_pointer)[current_note][0];
579 note_resting = false;
581 note_frequency = (*notes_pointer)[current_note][0];
582 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
589 if (!audio_config.enable) {
590 playing_notes = false;
591 playing_note = false;
595 void play_note(float freq, int vol) {
597 dprintf("audio play note freq=%d vol=%d", (int)freq, vol);
599 if (!audio_initialized) {
603 if (audio_config.enable && voices < 8) {
606 // Cancel notes if notes are playing
615 frequencies[voices] = freq;
616 volumes[voices] = vol;
620 gptStart(&GPTD8, &gpt8cfg1);
621 gptStartContinuous(&GPTD8, 2U);
628 void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat)
631 if (!audio_initialized) {
635 if (audio_config.enable) {
637 // Cancel note if a note is playing
641 playing_notes = true;
644 notes_count = n_count;
645 notes_repeat = n_repeat;
650 note_frequency = (*notes_pointer)[current_note][0];
651 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
654 gptStart(&GPTD8, &gpt8cfg1);
655 gptStartContinuous(&GPTD8, 2U);
662 bool is_playing_notes(void) {
663 return playing_notes;
666 bool is_audio_on(void) {
667 return (audio_config.enable != 0);
670 void audio_toggle(void) {
671 audio_config.enable ^= 1;
672 eeconfig_update_audio(audio_config.raw);
673 if (audio_config.enable)
677 void audio_on(void) {
678 audio_config.enable = 1;
679 eeconfig_update_audio(audio_config.raw);
683 void audio_off(void) {
684 audio_config.enable = 0;
685 eeconfig_update_audio(audio_config.raw);
688 #ifdef VIBRATO_ENABLE
690 // Vibrato rate functions
692 void set_vibrato_rate(float rate) {
696 void increase_vibrato_rate(float change) {
697 vibrato_rate *= change;
700 void decrease_vibrato_rate(float change) {
701 vibrato_rate /= change;
704 #ifdef VIBRATO_STRENGTH_ENABLE
706 void set_vibrato_strength(float strength) {
707 vibrato_strength = strength;
710 void increase_vibrato_strength(float change) {
711 vibrato_strength *= change;
714 void decrease_vibrato_strength(float change) {
715 vibrato_strength /= change;
718 #endif /* VIBRATO_STRENGTH_ENABLE */
720 #endif /* VIBRATO_ENABLE */
722 // Polyphony functions
724 void set_polyphony_rate(float rate) {
725 polyphony_rate = rate;
728 void enable_polyphony() {
732 void disable_polyphony() {
736 void increase_polyphony_rate(float change) {
737 polyphony_rate *= change;
740 void decrease_polyphony_rate(float change) {
741 polyphony_rate /= change;
746 void set_timbre(float timbre) {
747 note_timbre = timbre;
752 void set_tempo(uint8_t tempo) {
756 void decrease_tempo(uint8_t tempo_change) {
757 note_tempo += tempo_change;
760 void increase_tempo(uint8_t tempo_change) {
761 if (note_tempo - tempo_change < 10) {
764 note_tempo -= tempo_change;