From 462601f5e8de3476963c6fef44a88653e19fc3fd Mon Sep 17 00:00:00 2001 From: Jack Humbert Date: Tue, 19 Apr 2016 21:25:48 -0400 Subject: breaking changes - restructuring audio.c a little --- quantum/audio.c | 108 +++++++++++++++++++++++++++++++++----------------------- quantum/audio.h | 1 - 2 files changed, 63 insertions(+), 46 deletions(-) (limited to 'quantum') diff --git a/quantum/audio.c b/quantum/audio.c index 8ea1bf6ff..bbdbc824c 100644 --- a/quantum/audio.c +++ b/quantum/audio.c @@ -74,7 +74,9 @@ float vibrato_counter = 0; float vibrato_strength = .5; float vibrato_rate = 0.125; -float polyphony_rate = .5; +float polyphony_rate = 0; + +bool inited = false; audio_config_t audio_config; @@ -170,7 +172,49 @@ void increase_tempo(uint8_t tempo_change) { } } +void audio_init() { + + /* check signature */ + if (!eeconfig_is_enabled()) { + eeconfig_init(); + } + audio_config.raw = eeconfig_read_audio(); + + #ifdef PWM_AUDIO + PLLFRQ = _BV(PDIV2); + PLLCSR = _BV(PLLE); + while(!(PLLCSR & _BV(PLOCK))); + PLLFRQ |= _BV(PLLTM0); /* PCK 48MHz */ + + /* Init a fast PWM on Timer4 */ + TCCR4A = _BV(COM4A0) | _BV(PWM4A); /* Clear OC4A on Compare Match */ + TCCR4B = _BV(CS40); /* No prescaling => f = PCK/256 = 187500Hz */ + OCR4A = 0; + + /* Enable the OC4A output */ + DDRC |= _BV(PORTC6); + + TIMSK3 &= ~_BV(OCIE3A); // Turn off 3A interputs + + TCCR3A = 0x0; // Options not needed + TCCR3B = _BV(CS31) | _BV(CS30) | _BV(WGM32); // 64th prescaling and CTC + OCR3A = SAMPLE_DIVIDER - 1; // Correct count/compare, related to sample playback + #else + DDRC |= _BV(PORTC6); + + TIMSK3 &= ~_BV(OCIE3A); // Turn off 3A interputs + + TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30); + TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30); + #endif + + inited = true; +} + void stop_all_notes() { + if (!inited) { + audio_init(); + } voices = 0; #ifdef PWM_AUDIO TIMSK3 &= ~_BV(OCIE3A); @@ -191,7 +235,9 @@ void stop_all_notes() { void stop_note(double freq) { if (note) { - cli(); + if (!inited) { + audio_init(); + } #ifdef PWM_AUDIO freq = freq / SAMPLE_RATE; #endif @@ -225,47 +271,9 @@ void stop_note(double freq) { volume = 0; note = false; } - sei(); } } -void init_notes() { - - /* check signature */ - if (!eeconfig_is_enabled()) { - eeconfig_init(); - } - audio_config.raw = eeconfig_read_audio(); - - #ifdef PWM_AUDIO - PLLFRQ = _BV(PDIV2); - PLLCSR = _BV(PLLE); - while(!(PLLCSR & _BV(PLOCK))); - PLLFRQ |= _BV(PLLTM0); /* PCK 48MHz */ - - /* Init a fast PWM on Timer4 */ - TCCR4A = _BV(COM4A0) | _BV(PWM4A); /* Clear OC4A on Compare Match */ - TCCR4B = _BV(CS40); /* No prescaling => f = PCK/256 = 187500Hz */ - OCR4A = 0; - - /* Enable the OC4A output */ - DDRC |= _BV(PORTC6); - - TIMSK3 &= ~_BV(OCIE3A); // Turn off 3A interputs - - TCCR3A = 0x0; // Options not needed - TCCR3B = _BV(CS31) | _BV(CS30) | _BV(WGM32); // 64th prescaling and CTC - OCR3A = SAMPLE_DIVIDER - 1; // Correct count/compare, related to sample playback - #else - DDRC |= _BV(PORTC6); - - TIMSK3 &= ~_BV(OCIE3A); // Turn off 3A interputs - - TCCR3A = (0 << COM3A1) | (0 << COM3A0) | (1 << WGM31) | (0 << WGM30); - TCCR3B = (1 << WGM33) | (1 << WGM32) | (0 << CS32) | (1 << CS31) | (0 << CS30); - #endif -} - float mod(float a, int b) { float r = fmod(a, b); @@ -456,7 +464,10 @@ ISR(TIMER3_COMPA_vect) { void play_notes(float (*np)[][2], uint8_t n_count, bool n_repeat, float n_rest) { if (audio_config.enable) { - cli(); + TIMSK3 &= ~_BV(OCIE3A); + if (!inited) { + audio_init(); + } // Cancel note if a note is playing if (note) stop_all_notes(); @@ -485,7 +496,6 @@ if (audio_config.enable) { TIMSK3 |= _BV(OCIE3A); TCCR3A |= _BV(COM3A1); #endif - sei(); } } @@ -493,7 +503,10 @@ if (audio_config.enable) { void play_sample(uint8_t * s, uint16_t l, bool r) { if (audio_config.enable) { - + TIMSK3 &= ~_BV(OCIE3A); + if (!inited) { + audio_init(); + } stop_all_notes(); place_int = 0; sample = s; @@ -512,7 +525,10 @@ if (audio_config.enable) { void play_note(double freq, int vol) { if (audio_config.enable && voices < 8) { - cli(); + TIMSK3 &= ~_BV(OCIE3A); + if (!inited) { + audio_init(); + } // Cancel notes if notes are playing if (notes) stop_all_notes(); @@ -532,7 +548,6 @@ if (audio_config.enable && voices < 8) { TIMSK3 |= _BV(OCIE3A); TCCR3A |= _BV(COM3A1); #endif - sei(); } } @@ -545,8 +560,11 @@ void play_startup_tone() { } + + __attribute__ ((weak)) void play_goodbye_tone() { + } //------------------------------------------------------------------------------ diff --git a/quantum/audio.h b/quantum/audio.h index 85756af9d..0fe3eac9a 100644 --- a/quantum/audio.h +++ b/quantum/audio.h @@ -55,7 +55,6 @@ void play_sample(uint8_t * s, uint16_t l, bool r); void play_note(double freq, int vol); void stop_note(double freq); void stop_all_notes(void); -void init_notes(void); void play_notes(float (*np)[][2], uint8_t n_count, bool n_repeat, float n_rest); #define SCALE (int []){ 0 + (12*0), 2 + (12*0), 4 + (12*0), 5 + (12*0), 7 + (12*0), 9 + (12*0), 11 + (12*0), \ -- cgit v1.2.3