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_cb6(GPTDriver *gptp);
81 static void gpt_cb7(GPTDriver *gptp);
82 static void gpt_cb8(GPTDriver *gptp);
87 GPTConfig gpt6cfg1 = {
90 .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
94 GPTConfig gpt7cfg1 = {
97 .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
101 GPTConfig gpt8cfg1 = {
104 .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
108 static void gpt_cb6(GPTDriver *gptp) {
109 palTogglePad(GPIOA, 4);
113 static void gpt_cb7(GPTDriver *gptp) {
114 palTogglePad(GPIOA, 5);
120 if (audio_initialized)
124 // if (!eeconfig_is_enabled())
128 // audio_config.raw = eeconfig_read_audio();
129 audio_config.enable = true;
131 palSetPadMode(GPIOA, 4, PAL_MODE_OUTPUT_PUSHPULL);
132 palSetPadMode(GPIOA, 5, PAL_MODE_OUTPUT_PUSHPULL);
134 audio_initialized = true;
136 if (audio_config.enable) {
137 PLAY_SONG(startup_song);
142 void stop_all_notes()
144 dprintf("audio stop all notes");
146 if (!audio_initialized) {
151 gptStopTimer(&GPTD6);
152 gptStopTimer(&GPTD7);
153 gptStopTimer(&GPTD8);
155 playing_notes = false;
156 playing_note = false;
161 for (uint8_t i = 0; i < 8; i++)
168 void stop_note(float freq)
170 dprintf("audio stop note freq=%d", (int)freq);
173 if (!audio_initialized) {
176 for (int i = 7; i >= 0; i--) {
177 if (frequencies[i] == freq) {
180 for (int j = i; (j < 7); j++) {
181 frequencies[j] = frequencies[j+1];
182 frequencies[j+1] = 0;
183 volumes[j] = volumes[j+1];
192 if (voice_place >= voices) {
196 gptStopTimer(&GPTD6);
197 gptStopTimer(&GPTD7);
198 gptStopTimer(&GPTD8);
202 playing_note = false;
207 #ifdef VIBRATO_ENABLE
209 float mod(float a, int b)
211 float r = fmod(a, b);
212 return r < 0 ? r + b : r;
215 float vibrato(float average_freq) {
216 #ifdef VIBRATO_STRENGTH_ENABLE
217 float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
219 float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
221 vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH);
222 return vibrated_freq;
227 static void restart_gpt6(void) {
228 // gptStopTimer(&GPTD6);
230 gptStart(&GPTD6, &gpt6cfg1);
231 gptStartContinuous(&GPTD6, 2U);
234 static void restart_gpt7(void) {
235 // gptStopTimer(&GPTD7);
237 gptStart(&GPTD7, &gpt7cfg1);
238 gptStartContinuous(&GPTD7, 2U);
241 static void gpt_cb8(GPTDriver *gptp) {
249 if (polyphony_rate == 0) {
251 if (frequency_alt != 0 && frequency_alt < frequencies[voices - 2] && frequency_alt < frequencies[voices - 2] * pow(2, -440/frequencies[voices - 2]/12/2)) {
252 frequency_alt = frequency_alt * pow(2, 440/frequency_alt/12/2);
253 } else if (frequency_alt != 0 && frequency_alt > frequencies[voices - 2] && frequency_alt > frequencies[voices - 2] * pow(2, 440/frequencies[voices - 2]/12/2)) {
254 frequency_alt = frequency_alt * pow(2, -440/frequency_alt/12/2);
256 frequency_alt = frequencies[voices - 2];
259 frequency_alt = frequencies[voices - 2];
262 #ifdef VIBRATO_ENABLE
263 if (vibrato_strength > 0) {
264 freq_alt = vibrato(frequency_alt);
266 freq_alt = frequency_alt;
269 freq_alt = frequency_alt;
273 if (envelope_index < 65535) {
277 freq_alt = voice_envelope(freq_alt);
279 if (freq_alt < 30.517578125) {
283 if (gpt6cfg1.frequency != (uint16_t)freq_alt) {
284 gpt6cfg1.frequency = freq_alt;
289 // gptStopTimer(&GPTD6);
292 if (polyphony_rate > 0) {
294 voice_place %= voices;
295 if (place++ > (frequencies[voice_place] / polyphony_rate)) {
296 voice_place = (voice_place + 1) % voices;
301 #ifdef VIBRATO_ENABLE
302 if (vibrato_strength > 0) {
303 freq = vibrato(frequencies[voice_place]);
305 freq = frequencies[voice_place];
308 freq = frequencies[voice_place];
312 if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) {
313 frequency = frequency * pow(2, 440/frequency/12/2);
314 } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
315 frequency = frequency * pow(2, -440/frequency/12/2);
317 frequency = frequencies[voices - 1];
320 frequency = frequencies[voices - 1];
323 #ifdef VIBRATO_ENABLE
324 if (vibrato_strength > 0) {
325 freq = vibrato(frequency);
334 if (envelope_index < 65535) {
338 freq = voice_envelope(freq);
340 if (freq < 30.517578125) {
345 if (gpt7cfg1.frequency != (uint16_t)freq) {
346 gpt7cfg1.frequency = freq;
351 // gptStopTimer(&GPTD7);
356 if (note_frequency > 0) {
357 #ifdef VIBRATO_ENABLE
358 if (vibrato_strength > 0) {
359 freq = vibrato(note_frequency);
361 freq = note_frequency;
364 freq = note_frequency;
367 if (envelope_index < 65535) {
370 freq = voice_envelope(freq);
373 if (gpt6cfg1.frequency != (uint16_t)freq) {
374 gpt6cfg1.frequency = freq;
376 gpt7cfg1.frequency = freq;
381 // gptStopTimer(&GPTD6);
382 // gptStopTimer(&GPTD7);
386 bool end_of_note = false;
387 if (gpt6cfg1.frequency > 0) {
389 end_of_note = (note_position >= (note_length*16 - 1));
391 end_of_note = (note_position >= (note_length*16));
393 end_of_note = (note_position >= (note_length*16));
398 if (current_note >= notes_count) {
402 gptStopTimer(&GPTD6);
403 gptStopTimer(&GPTD7);
404 // gptStopTimer(&GPTD8);
405 playing_notes = false;
412 if ((*notes_pointer)[current_note][0] == (*notes_pointer)[current_note + 1][0]) {
416 note_frequency = (*notes_pointer)[current_note][0];
420 note_resting = false;
422 note_frequency = (*notes_pointer)[current_note][0];
423 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
430 if (!audio_config.enable) {
431 playing_notes = false;
432 playing_note = false;
436 void play_note(float freq, int vol) {
438 dprintf("audio play note freq=%d vol=%d", (int)freq, vol);
440 if (!audio_initialized) {
444 if (audio_config.enable && voices < 8) {
446 // Cancel notes if notes are playing
455 frequencies[voices] = freq;
456 volumes[voices] = vol;
460 gptStart(&GPTD8, &gpt8cfg1);
461 gptStartContinuous(&GPTD8, 2U);
467 void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat)
470 if (!audio_initialized) {
474 if (audio_config.enable) {
476 // Cancel note if a note is playing
480 playing_notes = true;
483 notes_count = n_count;
484 notes_repeat = n_repeat;
489 note_frequency = (*notes_pointer)[current_note][0];
490 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
493 gptStart(&GPTD8, &gpt8cfg1);
494 gptStartContinuous(&GPTD8, 2U);
501 bool is_playing_notes(void) {
502 return playing_notes;
505 bool is_audio_on(void) {
506 return (audio_config.enable != 0);
509 void audio_toggle(void) {
510 audio_config.enable ^= 1;
511 eeconfig_update_audio(audio_config.raw);
512 if (audio_config.enable)
516 void audio_on(void) {
517 audio_config.enable = 1;
518 eeconfig_update_audio(audio_config.raw);
522 void audio_off(void) {
523 audio_config.enable = 0;
524 eeconfig_update_audio(audio_config.raw);
527 #ifdef VIBRATO_ENABLE
529 // Vibrato rate functions
531 void set_vibrato_rate(float rate) {
535 void increase_vibrato_rate(float change) {
536 vibrato_rate *= change;
539 void decrease_vibrato_rate(float change) {
540 vibrato_rate /= change;
543 #ifdef VIBRATO_STRENGTH_ENABLE
545 void set_vibrato_strength(float strength) {
546 vibrato_strength = strength;
549 void increase_vibrato_strength(float change) {
550 vibrato_strength *= change;
553 void decrease_vibrato_strength(float change) {
554 vibrato_strength /= change;
557 #endif /* VIBRATO_STRENGTH_ENABLE */
559 #endif /* VIBRATO_ENABLE */
561 // Polyphony functions
563 void set_polyphony_rate(float rate) {
564 polyphony_rate = rate;
567 void enable_polyphony() {
571 void disable_polyphony() {
575 void increase_polyphony_rate(float change) {
576 polyphony_rate *= change;
579 void decrease_polyphony_rate(float change) {
580 polyphony_rate /= change;
585 void set_timbre(float timbre) {
586 note_timbre = timbre;
591 void set_tempo(uint8_t tempo) {
595 void decrease_tempo(uint8_t tempo_change) {
596 note_tempo += tempo_change;
599 void increase_tempo(uint8_t tempo_change) {
600 if (note_tempo - tempo_change < 10) {
603 note_tempo -= tempo_change;