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;
103 bool note_resting = false;
105 uint8_t current_note = 0;
106 uint8_t rest_counter = 0;
108 #ifdef VIBRATO_ENABLE
109 float vibrato_counter = 0;
110 float vibrato_strength = .5;
111 float vibrato_rate = 0.125;
114 float polyphony_rate = 0;
116 static bool audio_initialized = false;
118 audio_config_t audio_config;
120 uint16_t envelope_index = 0;
121 bool glissando = true;
124 #define STARTUP_SONG SONG(STARTUP_SOUND)
126 float startup_song[][2] = STARTUP_SONG;
131 if (audio_initialized)
135 if (!eeconfig_is_enabled())
139 audio_config.raw = eeconfig_read_audio();
141 // Set port PC6 (OC3A and /OC4A) as output
147 PORTC &= ~_BV(PORTC6);
154 PORTB &= ~_BV(PORTB5);
158 DISABLE_AUDIO_COUNTER_3_ISR;
162 DISABLE_AUDIO_COUNTER_1_ISR;
165 // TCCR3A / TCCR3B: Timer/Counter #3 Control Registers
166 // Compare Output Mode (COM3An) = 0b00 = Normal port operation, OC3A disconnected from PC6
167 // Waveform Generation Mode (WGM3n) = 0b1110 = Fast PWM Mode 14 (Period = ICR3, Duty Cycle = OCR3A)
168 // Clock Select (CS3n) = 0b010 = Clock / 8
171 TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30);
172 TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30);
176 TCCR1A = (0 << COM1A1) | (0 << COM1A0) | (1 << WGM11) | (0 << WGM10);
177 TCCR1B = (1 << WGM13) | (1 << WGM12) | (0 << CS12) | (1 << CS11) | (0 << CS10);
180 audio_initialized = true;
182 if (audio_config.enable) {
183 PLAY_NOTE_ARRAY(startup_song, false, LEGATO);
188 void stop_all_notes()
190 dprintf("audio stop all notes");
192 if (!audio_initialized) {
199 DISABLE_AUDIO_COUNTER_3_ISR;
200 DISABLE_AUDIO_COUNTER_3_OUTPUT;
204 DISABLE_AUDIO_COUNTER_1_ISR;
205 DISABLE_AUDIO_COUNTER_1_OUTPUT;
208 playing_notes = false;
209 playing_note = false;
214 for (uint8_t i = 0; i < 8; i++)
221 void stop_note(float freq)
223 dprintf("audio stop note freq=%d", (int)freq);
226 if (!audio_initialized) {
229 for (int i = 7; i >= 0; i--) {
230 if (frequencies[i] == freq) {
233 for (int j = i; (j < 7); j++) {
234 frequencies[j] = frequencies[j+1];
235 frequencies[j+1] = 0;
236 volumes[j] = volumes[j+1];
245 if (voice_place >= voices) {
250 DISABLE_AUDIO_COUNTER_3_ISR;
251 DISABLE_AUDIO_COUNTER_3_OUTPUT;
254 DISABLE_AUDIO_COUNTER_1_ISR;
255 DISABLE_AUDIO_COUNTER_1_OUTPUT;
260 playing_note = false;
265 #ifdef VIBRATO_ENABLE
267 float mod(float a, int b)
269 float r = fmod(a, b);
270 return r < 0 ? r + b : r;
273 float vibrato(float average_freq) {
274 #ifdef VIBRATO_STRENGTH_ENABLE
275 float vibrated_freq = average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
277 float vibrated_freq = average_freq * vibrato_lut[(int)vibrato_counter];
279 vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH);
280 return vibrated_freq;
286 ISR(TIMER3_COMPA_vect)
296 if (polyphony_rate == 0) {
298 if (frequency_alt != 0 && frequency_alt < frequencies[voices - 2] && frequency_alt < frequencies[voices - 2] * pow(2, -440/frequencies[voices - 2]/12/2)) {
299 frequency_alt = frequency_alt * pow(2, 440/frequency_alt/12/2);
300 } else if (frequency_alt != 0 && frequency_alt > frequencies[voices - 2] && frequency_alt > frequencies[voices - 2] * pow(2, 440/frequencies[voices - 2]/12/2)) {
301 frequency_alt = frequency_alt * pow(2, -440/frequency_alt/12/2);
303 frequency_alt = frequencies[voices - 2];
306 frequency_alt = frequencies[voices - 2];
309 #ifdef VIBRATO_ENABLE
310 if (vibrato_strength > 0) {
311 freq_alt = vibrato(frequency_alt);
313 freq_alt = frequency_alt;
316 freq_alt = frequency_alt;
320 if (envelope_index < 65535) {
324 freq_alt = voice_envelope(freq_alt);
326 if (freq_alt < 30.517578125) {
330 TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (freq_alt * CPU_PRESCALER));
331 TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq_alt * CPU_PRESCALER)) * note_timbre);
335 if (polyphony_rate > 0) {
337 voice_place %= voices;
338 if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
339 voice_place = (voice_place + 1) % voices;
344 #ifdef VIBRATO_ENABLE
345 if (vibrato_strength > 0) {
346 freq = vibrato(frequencies[voice_place]);
348 freq = frequencies[voice_place];
351 freq = frequencies[voice_place];
355 if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) {
356 frequency = frequency * pow(2, 440/frequency/12/2);
357 } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
358 frequency = frequency * pow(2, -440/frequency/12/2);
360 frequency = frequencies[voices - 1];
363 frequency = frequencies[voices - 1];
366 #ifdef VIBRATO_ENABLE
367 if (vibrato_strength > 0) {
368 freq = vibrato(frequency);
377 if (envelope_index < 65535) {
381 freq = voice_envelope(freq);
383 if (freq < 30.517578125) {
387 TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
388 TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
393 if (note_frequency > 0) {
394 #ifdef VIBRATO_ENABLE
395 if (vibrato_strength > 0) {
396 freq = vibrato(note_frequency);
398 freq = note_frequency;
401 freq = note_frequency;
404 if (envelope_index < 65535) {
407 freq = voice_envelope(freq);
409 TIMER_3_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
410 TIMER_3_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
413 TIMER_3_DUTY_CYCLE = 0;
417 bool end_of_note = false;
418 if (TIMER_3_PERIOD > 0) {
419 end_of_note = (note_position >= (note_length / TIMER_3_PERIOD * 0xFFFF));
421 end_of_note = (note_position >= (note_length * 0x7FF));
426 if (current_note >= notes_count) {
430 DISABLE_AUDIO_COUNTER_3_ISR;
431 DISABLE_AUDIO_COUNTER_3_OUTPUT;
432 playing_notes = false;
436 if (!note_resting && (notes_rest > 0)) {
439 note_length = notes_rest;
442 note_resting = false;
444 note_frequency = (*notes_pointer)[current_note][0];
445 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
452 if (!audio_config.enable) {
453 playing_notes = false;
454 playing_note = false;
460 ISR(TIMER1_COMPA_vect)
462 #if defined(B5_AUDIO) && !defined(C6_AUDIO)
467 if (polyphony_rate > 0) {
469 voice_place %= voices;
470 if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) {
471 voice_place = (voice_place + 1) % voices;
476 #ifdef VIBRATO_ENABLE
477 if (vibrato_strength > 0) {
478 freq = vibrato(frequencies[voice_place]);
480 freq = frequencies[voice_place];
483 freq = frequencies[voice_place];
487 if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) {
488 frequency = frequency * pow(2, 440/frequency/12/2);
489 } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) {
490 frequency = frequency * pow(2, -440/frequency/12/2);
492 frequency = frequencies[voices - 1];
495 frequency = frequencies[voices - 1];
498 #ifdef VIBRATO_ENABLE
499 if (vibrato_strength > 0) {
500 freq = vibrato(frequency);
509 if (envelope_index < 65535) {
513 freq = voice_envelope(freq);
515 if (freq < 30.517578125) {
519 TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
520 TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
525 if (note_frequency > 0) {
526 #ifdef VIBRATO_ENABLE
527 if (vibrato_strength > 0) {
528 freq = vibrato(note_frequency);
530 freq = note_frequency;
533 freq = note_frequency;
536 if (envelope_index < 65535) {
539 freq = voice_envelope(freq);
541 TIMER_1_PERIOD = (uint16_t)(((float)F_CPU) / (freq * CPU_PRESCALER));
542 TIMER_1_DUTY_CYCLE = (uint16_t)((((float)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre);
545 TIMER_1_DUTY_CYCLE = 0;
549 bool end_of_note = false;
550 if (TIMER_1_PERIOD > 0) {
551 end_of_note = (note_position >= (note_length / TIMER_1_PERIOD * 0xFFFF));
553 end_of_note = (note_position >= (note_length * 0x7FF));
558 if (current_note >= notes_count) {
562 DISABLE_AUDIO_COUNTER_1_ISR;
563 DISABLE_AUDIO_COUNTER_1_OUTPUT;
564 playing_notes = false;
568 if (!note_resting && (notes_rest > 0)) {
571 note_length = notes_rest;
574 note_resting = false;
576 note_frequency = (*notes_pointer)[current_note][0];
577 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
584 if (!audio_config.enable) {
585 playing_notes = false;
586 playing_note = false;
592 void play_note(float freq, int vol) {
594 dprintf("audio play note freq=%d vol=%d", (int)freq, vol);
596 if (!audio_initialized) {
600 if (audio_config.enable && voices < 8) {
602 DISABLE_AUDIO_COUNTER_3_ISR;
605 DISABLE_AUDIO_COUNTER_1_ISR;
608 // Cancel notes if notes are playing
617 frequencies[voices] = freq;
618 volumes[voices] = vol;
623 ENABLE_AUDIO_COUNTER_3_ISR;
624 ENABLE_AUDIO_COUNTER_3_OUTPUT;
629 ENABLE_AUDIO_COUNTER_1_ISR;
630 ENABLE_AUDIO_COUNTER_1_OUTPUT;
633 ENABLE_AUDIO_COUNTER_1_ISR;
634 ENABLE_AUDIO_COUNTER_1_OUTPUT;
641 void play_notes(float (*np)[][2], uint16_t n_count, bool n_repeat, float n_rest)
644 if (!audio_initialized) {
648 if (audio_config.enable) {
651 DISABLE_AUDIO_COUNTER_3_ISR;
654 DISABLE_AUDIO_COUNTER_1_ISR;
657 // Cancel note if a note is playing
661 playing_notes = true;
664 notes_count = n_count;
665 notes_repeat = n_repeat;
671 note_frequency = (*notes_pointer)[current_note][0];
672 note_length = ((*notes_pointer)[current_note][1] / 4) * (((float)note_tempo) / 100);
677 ENABLE_AUDIO_COUNTER_3_ISR;
678 ENABLE_AUDIO_COUNTER_3_OUTPUT;
682 ENABLE_AUDIO_COUNTER_1_ISR;
683 ENABLE_AUDIO_COUNTER_1_OUTPUT;
690 bool is_playing_notes(void) {
691 return playing_notes;
694 bool is_audio_on(void) {
695 return (audio_config.enable != 0);
698 void audio_toggle(void) {
699 audio_config.enable ^= 1;
700 eeconfig_update_audio(audio_config.raw);
701 if (audio_config.enable)
705 void audio_on(void) {
706 audio_config.enable = 1;
707 eeconfig_update_audio(audio_config.raw);
711 void audio_off(void) {
712 audio_config.enable = 0;
713 eeconfig_update_audio(audio_config.raw);
716 #ifdef VIBRATO_ENABLE
718 // Vibrato rate functions
720 void set_vibrato_rate(float rate) {
724 void increase_vibrato_rate(float change) {
725 vibrato_rate *= change;
728 void decrease_vibrato_rate(float change) {
729 vibrato_rate /= change;
732 #ifdef VIBRATO_STRENGTH_ENABLE
734 void set_vibrato_strength(float strength) {
735 vibrato_strength = strength;
738 void increase_vibrato_strength(float change) {
739 vibrato_strength *= change;
742 void decrease_vibrato_strength(float change) {
743 vibrato_strength /= change;
746 #endif /* VIBRATO_STRENGTH_ENABLE */
748 #endif /* VIBRATO_ENABLE */
750 // Polyphony functions
752 void set_polyphony_rate(float rate) {
753 polyphony_rate = rate;
756 void enable_polyphony() {
760 void disable_polyphony() {
764 void increase_polyphony_rate(float change) {
765 polyphony_rate *= change;
768 void decrease_polyphony_rate(float change) {
769 polyphony_rate /= change;
774 void set_timbre(float timbre) {
775 note_timbre = timbre;
780 void set_tempo(uint8_t tempo) {
784 void decrease_tempo(uint8_t tempo_change) {
785 note_tempo += tempo_change;
788 void increase_tempo(uint8_t tempo_change) {
789 if (note_tempo - tempo_change < 10) {
792 note_tempo -= tempo_change;