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/>.
21 #include <avr/pgmspace.h>
22 #include <avr/interrupt.h>
32 #define CPU_PRESCALER 8
34 // -----------------------------------------------------------------------------
36 // -----------------------------------------------------------------------------
38 // TIMSK3 - Timer/Counter #3 Interrupt Mask Register
39 // Turn on/off 3A interputs, stopping/enabling the ISR calls
41 #define ENABLE_AUDIO_COUNTER_3_ISR TIMSK3 |= _BV(OCIE3A)
42 #define DISABLE_AUDIO_COUNTER_3_ISR TIMSK3 &= ~_BV(OCIE3A)
46 #define ENABLE_AUDIO_COUNTER_1_ISR TIMSK1 |= _BV(OCIE1A)
47 #define DISABLE_AUDIO_COUNTER_1_ISR TIMSK1 &= ~_BV(OCIE1A)
50 // TCCR3A: Timer/Counter #3 Control Register
51 // Compare Output Mode (COM3An) = 0b00 = Normal port operation, OC3A disconnected from PC6
54 #define ENABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A |= _BV(COM3A1);
55 #define DISABLE_AUDIO_COUNTER_3_OUTPUT TCCR3A &= ~(_BV(COM3A1) | _BV(COM3A0));
59 #define ENABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A |= _BV(COM1A1);
60 #define DISABLE_AUDIO_COUNTER_1_OUTPUT TCCR1A &= ~(_BV(COM1A1) | _BV(COM1A0));
63 // Fast PWM Mode Controls
66 #define TIMER_3_PERIOD ICR3
67 #define TIMER_3_DUTY_CYCLE OCR3A
71 #define TIMER_1_PERIOD ICR1
72 #define TIMER_1_DUTY_CYCLE OCR1A
76 // -----------------------------------------------------------------------------
82 float frequency_alt = 0;
86 float frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0};
87 int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0};
93 uint16_t sample_length = 0;
95 bool playing_notes = false;
96 bool playing_note = false;
97 float note_frequency = 0;
98 float note_length = 0;
99 uint8_t note_tempo = TEMPO_DEFAULT;
100 float note_timbre = TIMBRE_DEFAULT;
101 uint16_t note_position = 0;
102 float (* notes_pointer)[][2];
103 uint16_t notes_count;
105 bool note_resting = false;
107 uint8_t current_note = 0;
108 uint8_t rest_counter = 0;
110 #ifdef VIBRATO_ENABLE
111 float vibrato_counter = 0;
112 float vibrato_strength = .5;
113 float vibrato_rate = 0.125;
116 float polyphony_rate = 0;
118 static bool audio_initialized = false;
120 audio_config_t audio_config;
122 uint16_t envelope_index = 0;
123 bool glissando = true;
126 #define STARTUP_SONG SONG(STARTUP_SOUND)
128 #ifndef AUDIO_ON_SONG
129 #define AUDIO_ON_SONG SONG(AUDIO_ON_SOUND)
131 #ifndef AUDIO_OFF_SONG
132 #define AUDIO_OFF_SONG SONG(AUDIO_OFF_SOUND)
134 float startup_song[][2] = STARTUP_SONG;
135 float audio_on_song[][2] = AUDIO_ON_SONG;
136 float audio_off_song[][2] = AUDIO_OFF_SONG;
142 if (!eeconfig_is_enabled())
146 audio_config.raw = eeconfig_read_audio();
148 if (!audio_initialized) {
150 // Set port PC6 (OC3A and /OC4A) as output
156 PORTC &= ~_BV(PORTC6);
163 PORTB &= ~_BV(PORTB5);
167 DISABLE_AUDIO_COUNTER_3_ISR;
171 DISABLE_AUDIO_COUNTER_1_ISR;
174 // TCCR3A / TCCR3B: Timer/Counter #3 Control Registers
175 // Compare Output Mode (COM3An) = 0b00 = Normal port operation, OC3A disconnected from PC6
176 // Waveform Generation Mode (WGM3n) = 0b1110 = Fast PWM Mode 14 (Period = ICR3, Duty Cycle = OCR3A)
177 // Clock Select (CS3n) = 0b010 = Clock / 8
180 TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
181 TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
185 TCCR1A = (0 << COM1A1) | (0 << COM1A0) | (1 << WGM11) | (0 << WGM10);
186 TCCR1B = (1 << WGM13) | (1 << WGM12) | (0 << CS12) | (1 << CS11) | (0 << CS10);
189 audio_initialized = true;
192 if (audio_config.enable) {
193 PLAY_SONG(startup_song);
198 void stop_all_notes()
200 dprintf("audio stop all notes");
202 if (!audio_initialized) {
209 DISABLE_AUDIO_COUNTER_3_ISR;
210 DISABLE_AUDIO_COUNTER_3_OUTPUT;
214 DISABLE_AUDIO_COUNTER_1_ISR;
215 DISABLE_AUDIO_COUNTER_1_OUTPUT;
218 playing_notes = false;
219 playing_note = false;
224 for (uint8_t i = 0; i < 8; i++)
231 void stop_note(float freq)
233 dprintf("audio stop note freq=%d", (int)freq);
236 if (!audio_initialized) {
239 for (int i = 7; i >= 0; i--) {
240 if (frequencies[i] == freq) {
243 for (int j = i; (j < 7); j++) {
244 frequencies[j] = frequencies[j+1];
245 frequencies[j+1] = 0;
246 volumes[j] = volumes[j+1];
255 if (voice_place >= voices) {
260 DISABLE_AUDIO_COUNTER_3_ISR;
261 DISABLE_AUDIO_COUNTER_3_OUTPUT;
264 DISABLE_AUDIO_COUNTER_1_ISR;
265 DISABLE_AUDIO_COUNTER_1_OUTPUT;
270 playing_note = false;
275 #ifdef VIBRATO_ENABLE
277 float mod(float a, int b)
279 float r = fmod(a, b);
280 return r < 0 ? r + b : r;
283 float vibrato(float average_freq) {
284 #ifdef VIBRATO_STRENGTH_ENABLE
285 float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
287 float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
289 vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH);
290 return vibrated_freq;
296 ISR(TIMER3_COMPA_vect)
306 if (polyphony_rate == 0) {
308 if (frequency_alt != 0 && frequency_alt < frequencies[voices - 2] && frequency_alt < frequencies[voices - 2] * pow(2, -440/frequencies[voices - 2]/12/2)) {
309 frequency_alt = frequency_alt * pow(2, 440/frequency_alt/12/2);
310 } else if (frequency_alt != 0 && frequency_alt > frequencies[voices - 2] && frequency_alt > frequencies[voices - 2] * pow(2, 440/frequencies[voices - 2]/12/2)) {
311 frequency_alt = frequency_alt * pow(2, -440/frequency_alt/12/2);
313 frequency_alt = frequencies[voices - 2];
316 frequency_alt = frequencies[voices - 2];
319 #ifdef VIBRATO_ENABLE
320 if (vibrato_strength > 0) {
321 freq_alt = vibrato(frequency_alt);
323 freq_alt = frequency_alt;
326 freq_alt = frequency_alt;
330 if (envelope_index < 65535) {
334 freq_alt = voice_envelope(freq_alt);
336 if (freq_alt < 30.517578125) {
340 TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (freq_alt * CPU_PRESCALER));
341 TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq_alt * CPU_PRESCALER)) * note_timbre);
345 if (polyphony_rate > 0) {
347 voice_place %= voices;
348 if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
349 voice_place = (voice_place + 1) % voices;
354 #ifdef VIBRATO_ENABLE
355 if (vibrato_strength > 0) {
356 freq = vibrato(frequencies[voice_place]);
358 freq = frequencies[voice_place];
361 freq = frequencies[voice_place];
365 if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) {
366 frequency = frequency * pow(2, 440/frequency/12/2);
367 } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
368 frequency = frequency * pow(2, -440/frequency/12/2);
370 frequency = frequencies[voices - 1];
373 frequency = frequencies[voices - 1];
376 #ifdef VIBRATO_ENABLE
377 if (vibrato_strength > 0) {
378 freq = vibrato(frequency);
387 if (envelope_index < 65535) {
391 freq = voice_envelope(freq);
393 if (freq < 30.517578125) {
397 TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
398 TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
403 if (note_frequency > 0) {
404 #ifdef VIBRATO_ENABLE
405 if (vibrato_strength > 0) {
406 freq = vibrato(note_frequency);
408 freq = note_frequency;
411 freq = note_frequency;
414 if (envelope_index < 65535) {
417 freq = voice_envelope(freq);
419 TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
420 TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
423 TIMER_3_DUTY_CYCLE = 0;
427 bool end_of_note = false;
428 if (TIMER_3_PERIOD > 0) {
430 end_of_note = (note_position >= (note_length / TIMER_3_PERIOD * 0xFFFF - 1));
432 end_of_note = (note_position >= (note_length));
434 end_of_note = (note_position >= (note_length));
439 if (current_note >= notes_count) {
443 DISABLE_AUDIO_COUNTER_3_ISR;
444 DISABLE_AUDIO_COUNTER_3_OUTPUT;
445 playing_notes = false;
452 if ((*notes_pointer)[current_note][0] == (*notes_pointer)[current_note + 1][0]) {
456 note_frequency = (*notes_pointer)[current_note][0];
460 note_resting = false;
462 note_frequency = (*notes_pointer)[current_note][0];
463 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
470 if (!audio_config.enable) {
471 playing_notes = false;
472 playing_note = false;
478 ISR(TIMER1_COMPA_vect)
480 #if defined(B5_AUDIO) && !defined(C6_AUDIO)
485 if (polyphony_rate > 0) {
487 voice_place %= voices;
488 if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
489 voice_place = (voice_place + 1) % voices;
494 #ifdef VIBRATO_ENABLE
495 if (vibrato_strength > 0) {
496 freq = vibrato(frequencies[voice_place]);
498 freq = frequencies[voice_place];
501 freq = frequencies[voice_place];
505 if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) {
506 frequency = frequency * pow(2, 440/frequency/12/2);
507 } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
508 frequency = frequency * pow(2, -440/frequency/12/2);
510 frequency = frequencies[voices - 1];
513 frequency = frequencies[voices - 1];
516 #ifdef VIBRATO_ENABLE
517 if (vibrato_strength > 0) {
518 freq = vibrato(frequency);
527 if (envelope_index < 65535) {
531 freq = voice_envelope(freq);
533 if (freq < 30.517578125) {
537 TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
538 TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
543 if (note_frequency > 0) {
544 #ifdef VIBRATO_ENABLE
545 if (vibrato_strength > 0) {
546 freq = vibrato(note_frequency);
548 freq = note_frequency;
551 freq = note_frequency;
554 if (envelope_index < 65535) {
557 freq = voice_envelope(freq);
559 TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
560 TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
563 TIMER_1_DUTY_CYCLE = 0;
567 bool end_of_note = false;
568 if (TIMER_1_PERIOD > 0) {
570 end_of_note = (note_position >= (note_length / TIMER_1_PERIOD * 0xFFFF - 1));
572 end_of_note = (note_position >= (note_length));
574 end_of_note = (note_position >= (note_length));
579 if (current_note >= notes_count) {
583 DISABLE_AUDIO_COUNTER_1_ISR;
584 DISABLE_AUDIO_COUNTER_1_OUTPUT;
585 playing_notes = false;
592 if ((*notes_pointer)[current_note][0] == (*notes_pointer)[current_note + 1][0]) {
596 note_frequency = (*notes_pointer)[current_note][0];
600 note_resting = false;
602 note_frequency = (*notes_pointer)[current_note][0];
603 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
610 if (!audio_config.enable) {
611 playing_notes = false;
612 playing_note = false;
618 void play_note(float freq, int vol) {
620 dprintf("audio play note freq=%d vol=%d", (int)freq, vol);
622 if (!audio_initialized) {
626 if (audio_config.enable && voices < 8) {
628 DISABLE_AUDIO_COUNTER_3_ISR;
631 DISABLE_AUDIO_COUNTER_1_ISR;
634 // Cancel notes if notes are playing
643 frequencies[voices] = freq;
644 volumes[voices] = vol;
649 ENABLE_AUDIO_COUNTER_3_ISR;
650 ENABLE_AUDIO_COUNTER_3_OUTPUT;
655 ENABLE_AUDIO_COUNTER_1_ISR;
656 ENABLE_AUDIO_COUNTER_1_OUTPUT;
659 ENABLE_AUDIO_COUNTER_1_ISR;
660 ENABLE_AUDIO_COUNTER_1_OUTPUT;
667 void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat)
670 if (!audio_initialized) {
674 if (audio_config.enable) {
677 DISABLE_AUDIO_COUNTER_3_ISR;
680 DISABLE_AUDIO_COUNTER_1_ISR;
683 // Cancel note if a note is playing
687 playing_notes = true;
690 notes_count = n_count;
691 notes_repeat = n_repeat;
696 note_frequency = (*notes_pointer)[current_note][0];
697 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
702 ENABLE_AUDIO_COUNTER_3_ISR;
703 ENABLE_AUDIO_COUNTER_3_OUTPUT;
707 ENABLE_AUDIO_COUNTER_1_ISR;
708 ENABLE_AUDIO_COUNTER_1_OUTPUT;
715 bool is_playing_notes(void) {
716 return playing_notes;
719 bool is_audio_on(void) {
720 return (audio_config.enable != 0);
723 void audio_toggle(void) {
724 audio_config.enable ^= 1;
725 eeconfig_update_audio(audio_config.raw);
726 if (audio_config.enable)
730 void audio_on(void) {
731 audio_config.enable = 1;
732 eeconfig_update_audio(audio_config.raw);
734 PLAY_SONG(audio_on_song);
737 void audio_off(void) {
738 PLAY_SONG(audio_off_song);
741 audio_config.enable = 0;
742 eeconfig_update_audio(audio_config.raw);
745 #ifdef VIBRATO_ENABLE
747 // Vibrato rate functions
749 void set_vibrato_rate(float rate) {
753 void increase_vibrato_rate(float change) {
754 vibrato_rate *= change;
757 void decrease_vibrato_rate(float change) {
758 vibrato_rate /= change;
761 #ifdef VIBRATO_STRENGTH_ENABLE
763 void set_vibrato_strength(float strength) {
764 vibrato_strength = strength;
767 void increase_vibrato_strength(float change) {
768 vibrato_strength *= change;
771 void decrease_vibrato_strength(float change) {
772 vibrato_strength /= change;
775 #endif /* VIBRATO_STRENGTH_ENABLE */
777 #endif /* VIBRATO_ENABLE */
779 // Polyphony functions
781 void set_polyphony_rate(float rate) {
782 polyphony_rate = rate;
785 void enable_polyphony() {
789 void disable_polyphony() {
793 void increase_polyphony_rate(float change) {
794 polyphony_rate *= change;
797 void decrease_polyphony_rate(float change) {
798 polyphony_rate /= change;
803 void set_timbre(float timbre) {
804 note_timbre = timbre;
809 void set_tempo(uint8_t tempo) {
813 void decrease_tempo(uint8_t tempo_change) {
814 note_tempo += tempo_change;
817 void increase_tempo(uint8_t tempo_change) {
818 if (note_tempo - tempo_change < 10) {
821 note_tempo -= tempo_change;