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/>.
28 // -----------------------------------------------------------------------------
33 float frequency_alt = 0;
37 float frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
38 int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
44 uint16_t sample_length = 0;
46 bool playing_notes = false;
47 bool playing_note = false;
48 float note_frequency = 0;
49 float note_length = 0;
50 uint8_t note_tempo = TEMPO_DEFAULT;
51 float note_timbre = TIMBRE_DEFAULT;
52 uint16_t note_position = 0;
53 float (* notes_pointer)[][2];
56 bool note_resting = false;
58 uint8_t current_note = 0;
59 uint8_t rest_counter = 0;
62 float vibrato_counter = 0;
63 float vibrato_strength = .5;
64 float vibrato_rate = 0.125;
67 float polyphony_rate = 0;
69 static bool audio_initialized = false;
71 audio_config_t audio_config;
73 uint16_t envelope_index = 0;
74 bool glissando = true;
77 #define STARTUP_SONG SONG(STARTUP_SOUND)
79 float startup_song[][2] = STARTUP_SONG;
81 static void gpt_cb6(GPTDriver *gptp);
82 static void gpt_cb7(GPTDriver *gptp);
83 static void gpt_cb8(GPTDriver *gptp);
88 GPTConfig gpt6cfg1 = {
91 .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
95 GPTConfig gpt7cfg1 = {
98 .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
102 GPTConfig gpt8cfg1 = {
105 .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
109 static void gpt_cb6(GPTDriver *gptp) {
110 palTogglePad(GPIOA, 4);
114 static void gpt_cb7(GPTDriver *gptp) {
115 palTogglePad(GPIOA, 5);
121 if (audio_initialized)
125 // if (!eeconfig_is_enabled())
129 // audio_config.raw = eeconfig_read_audio();
130 audio_config.enable = true;
132 palSetPadMode(GPIOA, 4, PAL_MODE_OUTPUT_PUSHPULL);
133 palSetPadMode(GPIOA, 5, PAL_MODE_OUTPUT_PUSHPULL);
135 audio_initialized = true;
137 if (audio_config.enable) {
138 PLAY_SONG(startup_song);
143 void stop_all_notes()
145 dprintf("audio stop all notes");
147 if (!audio_initialized) {
152 gptStopTimer(&GPTD6);
153 gptStopTimer(&GPTD7);
154 gptStopTimer(&GPTD8);
156 playing_notes = false;
157 playing_note = false;
162 for (uint8_t i = 0; i < 8; i++)
169 void stop_note(float freq)
171 dprintf("audio stop note freq=%d", (int)freq);
174 if (!audio_initialized) {
177 for (int i = 7; i >= 0; i--) {
178 if (frequencies[i] == freq) {
181 for (int j = i; (j < 7); j++) {
182 frequencies[j] = frequencies[j+1];
183 frequencies[j+1] = 0;
184 volumes[j] = volumes[j+1];
193 if (voice_place >= voices) {
197 gptStopTimer(&GPTD6);
198 gptStopTimer(&GPTD7);
199 gptStopTimer(&GPTD8);
203 playing_note = false;
208 #ifdef VIBRATO_ENABLE
210 float mod(float a, int b)
212 float r = fmod(a, b);
213 return r < 0 ? r + b : r;
216 float vibrato(float average_freq) {
217 #ifdef VIBRATO_STRENGTH_ENABLE
218 float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
220 float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
222 vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH);
223 return vibrated_freq;
228 static void restart_gpt6(void) {
229 // gptStopTimer(&GPTD6);
231 gptStart(&GPTD6, &gpt6cfg1);
232 gptStartContinuous(&GPTD6, 2U);
235 static void restart_gpt7(void) {
236 // gptStopTimer(&GPTD7);
238 gptStart(&GPTD7, &gpt7cfg1);
239 gptStartContinuous(&GPTD7, 2U);
242 static void gpt_cb8(GPTDriver *gptp) {
250 if (polyphony_rate == 0) {
252 if (frequency_alt != 0 && frequency_alt < frequencies[voices - 2] && frequency_alt < frequencies[voices - 2] * pow(2, -440/frequencies[voices - 2]/12/2)) {
253 frequency_alt = frequency_alt * pow(2, 440/frequency_alt/12/2);
254 } else if (frequency_alt != 0 && frequency_alt > frequencies[voices - 2] && frequency_alt > frequencies[voices - 2] * pow(2, 440/frequencies[voices - 2]/12/2)) {
255 frequency_alt = frequency_alt * pow(2, -440/frequency_alt/12/2);
257 frequency_alt = frequencies[voices - 2];
260 frequency_alt = frequencies[voices - 2];
263 #ifdef VIBRATO_ENABLE
264 if (vibrato_strength > 0) {
265 freq_alt = vibrato(frequency_alt);
267 freq_alt = frequency_alt;
270 freq_alt = frequency_alt;
274 if (envelope_index < 65535) {
278 freq_alt = voice_envelope(freq_alt);
280 if (freq_alt < 30.517578125) {
284 if (gpt6cfg1.frequency != (uint16_t)freq_alt) {
285 gpt6cfg1.frequency = freq_alt;
290 // gptStopTimer(&GPTD6);
293 if (polyphony_rate > 0) {
295 voice_place %= voices;
296 if (place++ > (frequencies[voice_place] / polyphony_rate)) {
297 voice_place = (voice_place + 1) % voices;
302 #ifdef VIBRATO_ENABLE
303 if (vibrato_strength > 0) {
304 freq = vibrato(frequencies[voice_place]);
306 freq = frequencies[voice_place];
309 freq = frequencies[voice_place];
313 if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) {
314 frequency = frequency * pow(2, 440/frequency/12/2);
315 } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
316 frequency = frequency * pow(2, -440/frequency/12/2);
318 frequency = frequencies[voices - 1];
321 frequency = frequencies[voices - 1];
324 #ifdef VIBRATO_ENABLE
325 if (vibrato_strength > 0) {
326 freq = vibrato(frequency);
335 if (envelope_index < 65535) {
339 freq = voice_envelope(freq);
341 if (freq < 30.517578125) {
346 if (gpt7cfg1.frequency != (uint16_t)freq) {
347 gpt7cfg1.frequency = freq;
352 // gptStopTimer(&GPTD7);
357 if (note_frequency > 0) {
358 #ifdef VIBRATO_ENABLE
359 if (vibrato_strength > 0) {
360 freq = vibrato(note_frequency);
362 freq = note_frequency;
365 freq = note_frequency;
368 if (envelope_index < 65535) {
371 freq = voice_envelope(freq);
374 if (gpt6cfg1.frequency != (uint16_t)freq) {
375 gpt6cfg1.frequency = freq;
377 gpt7cfg1.frequency = freq;
382 // gptStopTimer(&GPTD6);
383 // gptStopTimer(&GPTD7);
387 bool end_of_note = false;
388 if (gpt6cfg1.frequency > 0) {
390 end_of_note = (note_position >= (note_length*16 - 1));
392 end_of_note = (note_position >= (note_length*16));
394 end_of_note = (note_position >= (note_length*16));
399 if (current_note >= notes_count) {
403 gptStopTimer(&GPTD6);
404 gptStopTimer(&GPTD7);
405 // gptStopTimer(&GPTD8);
406 playing_notes = false;
413 if ((*notes_pointer)[current_note][0] == (*notes_pointer)[current_note + 1][0]) {
417 note_frequency = (*notes_pointer)[current_note][0];
421 note_resting = false;
423 note_frequency = (*notes_pointer)[current_note][0];
424 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
431 if (!audio_config.enable) {
432 playing_notes = false;
433 playing_note = false;
437 void play_note(float freq, int vol) {
439 dprintf("audio play note freq=%d vol=%d", (int)freq, vol);
441 if (!audio_initialized) {
445 if (audio_config.enable && voices < 8) {
447 // Cancel notes if notes are playing
456 frequencies[voices] = freq;
457 volumes[voices] = vol;
461 gptStart(&GPTD8, &gpt8cfg1);
462 gptStartContinuous(&GPTD8, 2U);
468 void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat)
471 if (!audio_initialized) {
475 if (audio_config.enable) {
477 // Cancel note if a note is playing
481 playing_notes = true;
484 notes_count = n_count;
485 notes_repeat = n_repeat;
490 note_frequency = (*notes_pointer)[current_note][0];
491 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
494 gptStart(&GPTD8, &gpt8cfg1);
495 gptStartContinuous(&GPTD8, 2U);
502 bool is_playing_notes(void) {
503 return playing_notes;
506 bool is_audio_on(void) {
507 return (audio_config.enable != 0);
510 void audio_toggle(void) {
511 audio_config.enable ^= 1;
512 eeconfig_update_audio(audio_config.raw);
513 if (audio_config.enable)
517 void audio_on(void) {
518 audio_config.enable = 1;
519 eeconfig_update_audio(audio_config.raw);
523 void audio_off(void) {
524 audio_config.enable = 0;
525 eeconfig_update_audio(audio_config.raw);
528 #ifdef VIBRATO_ENABLE
530 // Vibrato rate functions
532 void set_vibrato_rate(float rate) {
536 void increase_vibrato_rate(float change) {
537 vibrato_rate *= change;
540 void decrease_vibrato_rate(float change) {
541 vibrato_rate /= change;
544 #ifdef VIBRATO_STRENGTH_ENABLE
546 void set_vibrato_strength(float strength) {
547 vibrato_strength = strength;
550 void increase_vibrato_strength(float change) {
551 vibrato_strength *= change;
554 void decrease_vibrato_strength(float change) {
555 vibrato_strength /= change;
558 #endif /* VIBRATO_STRENGTH_ENABLE */
560 #endif /* VIBRATO_ENABLE */
562 // Polyphony functions
564 void set_polyphony_rate(float rate) {
565 polyphony_rate = rate;
568 void enable_polyphony() {
572 void disable_polyphony() {
576 void increase_polyphony_rate(float change) {
577 polyphony_rate *= change;
580 void decrease_polyphony_rate(float change) {
581 polyphony_rate /= change;
586 void set_timbre(float timbre) {
587 note_timbre = timbre;
592 void set_tempo(uint8_t tempo) {
596 void decrease_tempo(uint8_t tempo_change) {
597 note_tempo += tempo_change;
600 void increase_tempo(uint8_t tempo_change) {
601 if (note_tempo - tempo_change < 10) {
604 note_tempo -= tempo_change;