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/>.
20 #include <avr/pgmspace.h>
21 #include <avr/interrupt.h>
29 #define CPU_PRESCALER 8
31 // -----------------------------------------------------------------------------
33 // -----------------------------------------------------------------------------
35 // TIMSK3 - Timer/Counter #3 Interrupt Mask Register
36 // Turn on/off 3A interputs, stopping/enabling the ISR calls
38 #define ENABLE_AUDIO_COUNTER_3_ISR TIMSK3 |= _BV(OCIE3A)
39 #define DISABLE_AUDIO_COUNTER_3_ISR TIMSK3 &= ~_BV(OCIE3A)
43 #define ENABLE_AUDIO_COUNTER_1_ISR TIMSK1 |= _BV(OCIE1A)
44 #define DISABLE_AUDIO_COUNTER_1_ISR TIMSK1 &= ~_BV(OCIE1A)
47 // TCCR3A: Timer/Counter #3 Control Register
48 // Compare Output Mode (COM3An) = 0b00 = Normal port operation, OC3A disconnected from PC6
51 #define ENABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A |= _BV(COM3A1);
52 #define DISABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A &= ~(_BV(COM3A1) | _BV(COM3A0));
56 #define ENABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A |= _BV(COM1A1);
57 #define DISABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A &= ~(_BV(COM1A1) | _BV(COM1A0));
60 // Fast PWM Mode Controls
63 #define TIMER_3_PERIOD ICR3
64 #define TIMER_3_DUTY_CYCLE OCR3A
68 #define TIMER_1_PERIOD ICR1
69 #define TIMER_1_DUTY_CYCLE OCR1A
73 // -----------------------------------------------------------------------------
79 float frequency_alt = 0;
83 float frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
84 int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
90 uint16_t sample_length = 0;
92 bool playing_notes = false;
93 bool playing_note = false;
94 float note_frequency = 0;
95 float note_length = 0;
96 uint8_t note_tempo = TEMPO_DEFAULT;
97 float note_timbre = TIMBRE_DEFAULT;
98 uint16_t note_position = 0;
99 float (* notes_pointer)[][2];
100 uint16_t notes_count;
102 bool note_resting = false;
104 uint8_t current_note = 0;
105 uint8_t rest_counter = 0;
107 #ifdef VIBRATO_ENABLE
108 float vibrato_counter = 0;
109 float vibrato_strength = .5;
110 float vibrato_rate = 0.125;
113 float polyphony_rate = 0;
115 static bool audio_initialized = false;
117 audio_config_t audio_config;
119 uint16_t envelope_index = 0;
120 bool glissando = true;
123 #define STARTUP_SONG SONG(STARTUP_SOUND)
125 float startup_song[][2] = STARTUP_SONG;
130 if (audio_initialized)
134 if (!eeconfig_is_enabled())
138 audio_config.raw = eeconfig_read_audio();
140 // Set port PC6 (OC3A and /OC4A) as output
146 PORTC &= ~_BV(PORTC6);
153 PORTB &= ~_BV(PORTB5);
157 DISABLE_AUDIO_COUNTER_3_ISR;
161 DISABLE_AUDIO_COUNTER_1_ISR;
164 // TCCR3A / TCCR3B: Timer/Counter #3 Control Registers
165 // Compare Output Mode (COM3An) = 0b00 = Normal port operation, OC3A disconnected from PC6
166 // Waveform Generation Mode (WGM3n) = 0b1110 = Fast PWM Mode 14 (Period = ICR3, Duty Cycle = OCR3A)
167 // Clock Select (CS3n) = 0b010 = Clock / 8
170 TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
171 TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
175 TCCR1A = (0 << COM1A1) | (0 << COM1A0) | (1 << WGM11) | (0 << WGM10);
176 TCCR1B = (1 << WGM13) | (1 << WGM12) | (0 << CS12) | (1 << CS11) | (0 << CS10);
179 audio_initialized = true;
181 if (audio_config.enable) {
182 PLAY_SONG(startup_song);
187 void stop_all_notes()
189 dprintf("audio stop all notes");
191 if (!audio_initialized) {
198 DISABLE_AUDIO_COUNTER_3_ISR;
199 DISABLE_AUDIO_COUNTER_3_OUTPUT;
203 DISABLE_AUDIO_COUNTER_1_ISR;
204 DISABLE_AUDIO_COUNTER_1_OUTPUT;
207 playing_notes = false;
208 playing_note = false;
213 for (uint8_t i = 0; i < 8; i++)
220 void stop_note(float freq)
222 dprintf("audio stop note freq=%d", (int)freq);
225 if (!audio_initialized) {
228 for (int i = 7; i >= 0; i--) {
229 if (frequencies[i] == freq) {
232 for (int j = i; (j < 7); j++) {
233 frequencies[j] = frequencies[j+1];
234 frequencies[j+1] = 0;
235 volumes[j] = volumes[j+1];
244 if (voice_place >= voices) {
249 DISABLE_AUDIO_COUNTER_3_ISR;
250 DISABLE_AUDIO_COUNTER_3_OUTPUT;
253 DISABLE_AUDIO_COUNTER_1_ISR;
254 DISABLE_AUDIO_COUNTER_1_OUTPUT;
259 playing_note = false;
264 #ifdef VIBRATO_ENABLE
266 float mod(float a, int b)
268 float r = fmod(a, b);
269 return r < 0 ? r + b : r;
272 float vibrato(float average_freq) {
273 #ifdef VIBRATO_STRENGTH_ENABLE
274 float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
276 float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
278 vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH);
279 return vibrated_freq;
285 ISR(TIMER3_COMPA_vect)
295 if (polyphony_rate == 0) {
297 if (frequency_alt != 0 && frequency_alt < frequencies[voices - 2] && frequency_alt < frequencies[voices - 2] * pow(2, -440/frequencies[voices - 2]/12/2)) {
298 frequency_alt = frequency_alt * pow(2, 440/frequency_alt/12/2);
299 } else if (frequency_alt != 0 && frequency_alt > frequencies[voices - 2] && frequency_alt > frequencies[voices - 2] * pow(2, 440/frequencies[voices - 2]/12/2)) {
300 frequency_alt = frequency_alt * pow(2, -440/frequency_alt/12/2);
302 frequency_alt = frequencies[voices - 2];
305 frequency_alt = frequencies[voices - 2];
308 #ifdef VIBRATO_ENABLE
309 if (vibrato_strength > 0) {
310 freq_alt = vibrato(frequency_alt);
312 freq_alt = frequency_alt;
315 freq_alt = frequency_alt;
319 if (envelope_index < 65535) {
323 freq_alt = voice_envelope(freq_alt);
325 if (freq_alt < 30.517578125) {
329 TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (freq_alt * CPU_PRESCALER));
330 TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq_alt * CPU_PRESCALER)) * note_timbre);
334 if (polyphony_rate > 0) {
336 voice_place %= voices;
337 if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
338 voice_place = (voice_place + 1) % voices;
343 #ifdef VIBRATO_ENABLE
344 if (vibrato_strength > 0) {
345 freq = vibrato(frequencies[voice_place]);
347 freq = frequencies[voice_place];
350 freq = frequencies[voice_place];
354 if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) {
355 frequency = frequency * pow(2, 440/frequency/12/2);
356 } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
357 frequency = frequency * pow(2, -440/frequency/12/2);
359 frequency = frequencies[voices - 1];
362 frequency = frequencies[voices - 1];
365 #ifdef VIBRATO_ENABLE
366 if (vibrato_strength > 0) {
367 freq = vibrato(frequency);
376 if (envelope_index < 65535) {
380 freq = voice_envelope(freq);
382 if (freq < 30.517578125) {
386 TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
387 TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
392 if (note_frequency > 0) {
393 #ifdef VIBRATO_ENABLE
394 if (vibrato_strength > 0) {
395 freq = vibrato(note_frequency);
397 freq = note_frequency;
400 freq = note_frequency;
403 if (envelope_index < 65535) {
406 freq = voice_envelope(freq);
408 TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
409 TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
412 TIMER_3_DUTY_CYCLE = 0;
416 bool end_of_note = false;
417 if (TIMER_3_PERIOD > 0) {
419 end_of_note = (note_position >= (note_length / TIMER_3_PERIOD * 0xFFFF - 1));
421 end_of_note = (note_position >= (note_length));
423 end_of_note = (note_position >= (note_length));
428 if (current_note >= notes_count) {
432 DISABLE_AUDIO_COUNTER_3_ISR;
433 DISABLE_AUDIO_COUNTER_3_OUTPUT;
434 playing_notes = false;
441 if ((*notes_pointer)[current_note][0] == (*notes_pointer)[current_note + 1][0]) {
445 note_frequency = (*notes_pointer)[current_note][0];
449 note_resting = false;
451 note_frequency = (*notes_pointer)[current_note][0];
452 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
459 if (!audio_config.enable) {
460 playing_notes = false;
461 playing_note = false;
467 ISR(TIMER1_COMPA_vect)
469 #if defined(B5_AUDIO) && !defined(C6_AUDIO)
474 if (polyphony_rate > 0) {
476 voice_place %= voices;
477 if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
478 voice_place = (voice_place + 1) % voices;
483 #ifdef VIBRATO_ENABLE
484 if (vibrato_strength > 0) {
485 freq = vibrato(frequencies[voice_place]);
487 freq = frequencies[voice_place];
490 freq = frequencies[voice_place];
494 if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) {
495 frequency = frequency * pow(2, 440/frequency/12/2);
496 } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
497 frequency = frequency * pow(2, -440/frequency/12/2);
499 frequency = frequencies[voices - 1];
502 frequency = frequencies[voices - 1];
505 #ifdef VIBRATO_ENABLE
506 if (vibrato_strength > 0) {
507 freq = vibrato(frequency);
516 if (envelope_index < 65535) {
520 freq = voice_envelope(freq);
522 if (freq < 30.517578125) {
526 TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
527 TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
532 if (note_frequency > 0) {
533 #ifdef VIBRATO_ENABLE
534 if (vibrato_strength > 0) {
535 freq = vibrato(note_frequency);
537 freq = note_frequency;
540 freq = note_frequency;
543 if (envelope_index < 65535) {
546 freq = voice_envelope(freq);
548 TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
549 TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
552 TIMER_1_DUTY_CYCLE = 0;
556 bool end_of_note = false;
557 if (TIMER_1_PERIOD > 0) {
559 end_of_note = (note_position >= (note_length / TIMER_1_PERIOD * 0xFFFF - 1));
561 end_of_note = (note_position >= (note_length));
563 end_of_note = (note_position >= (note_length));
568 if (current_note >= notes_count) {
572 DISABLE_AUDIO_COUNTER_1_ISR;
573 DISABLE_AUDIO_COUNTER_1_OUTPUT;
574 playing_notes = false;
581 if ((*notes_pointer)[current_note][0] == (*notes_pointer)[current_note + 1][0]) {
585 note_frequency = (*notes_pointer)[current_note][0];
589 note_resting = false;
591 note_frequency = (*notes_pointer)[current_note][0];
592 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
599 if (!audio_config.enable) {
600 playing_notes = false;
601 playing_note = false;
607 void play_note(float freq, int vol) {
609 dprintf("audio play note freq=%d vol=%d", (int)freq, vol);
611 if (!audio_initialized) {
615 if (audio_config.enable && voices < 8) {
617 DISABLE_AUDIO_COUNTER_3_ISR;
620 DISABLE_AUDIO_COUNTER_1_ISR;
623 // Cancel notes if notes are playing
632 frequencies[voices] = freq;
633 volumes[voices] = vol;
638 ENABLE_AUDIO_COUNTER_3_ISR;
639 ENABLE_AUDIO_COUNTER_3_OUTPUT;
644 ENABLE_AUDIO_COUNTER_1_ISR;
645 ENABLE_AUDIO_COUNTER_1_OUTPUT;
648 ENABLE_AUDIO_COUNTER_1_ISR;
649 ENABLE_AUDIO_COUNTER_1_OUTPUT;
656 void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat)
659 if (!audio_initialized) {
663 if (audio_config.enable) {
666 DISABLE_AUDIO_COUNTER_3_ISR;
669 DISABLE_AUDIO_COUNTER_1_ISR;
672 // Cancel note if a note is playing
676 playing_notes = true;
679 notes_count = n_count;
680 notes_repeat = n_repeat;
685 note_frequency = (*notes_pointer)[current_note][0];
686 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
691 ENABLE_AUDIO_COUNTER_3_ISR;
692 ENABLE_AUDIO_COUNTER_3_OUTPUT;
696 ENABLE_AUDIO_COUNTER_1_ISR;
697 ENABLE_AUDIO_COUNTER_1_OUTPUT;
704 bool is_playing_notes(void) {
705 return playing_notes;
708 bool is_audio_on(void) {
709 return (audio_config.enable != 0);
712 void audio_toggle(void) {
713 audio_config.enable ^= 1;
714 eeconfig_update_audio(audio_config.raw);
715 if (audio_config.enable)
719 void audio_on(void) {
720 audio_config.enable = 1;
721 eeconfig_update_audio(audio_config.raw);
725 void audio_off(void) {
726 audio_config.enable = 0;
727 eeconfig_update_audio(audio_config.raw);
730 #ifdef VIBRATO_ENABLE
732 // Vibrato rate functions
734 void set_vibrato_rate(float rate) {
738 void increase_vibrato_rate(float change) {
739 vibrato_rate *= change;
742 void decrease_vibrato_rate(float change) {
743 vibrato_rate /= change;
746 #ifdef VIBRATO_STRENGTH_ENABLE
748 void set_vibrato_strength(float strength) {
749 vibrato_strength = strength;
752 void increase_vibrato_strength(float change) {
753 vibrato_strength *= change;
756 void decrease_vibrato_strength(float change) {
757 vibrato_strength /= change;
760 #endif /* VIBRATO_STRENGTH_ENABLE */
762 #endif /* VIBRATO_ENABLE */
764 // Polyphony functions
766 void set_polyphony_rate(float rate) {
767 polyphony_rate = rate;
770 void enable_polyphony() {
774 void disable_polyphony() {
778 void increase_polyphony_rate(float change) {
779 polyphony_rate *= change;
782 void decrease_polyphony_rate(float change) {
783 polyphony_rate /= change;
788 void set_timbre(float timbre) {
789 note_timbre = timbre;
794 void set_tempo(uint8_t tempo) {
798 void decrease_tempo(uint8_t tempo_change) {
799 note_tempo += tempo_change;
802 void increase_tempo(uint8_t tempo_change) {
803 if (note_tempo - tempo_change < 10) {
806 note_tempo -= tempo_change;