From 462601f5e8de3476963c6fef44a88653e19fc3fd Mon Sep 17 00:00:00 2001 From: Jack Humbert Date: Tue, 19 Apr 2016 21:25:48 -0400 Subject: [PATCH] breaking changes - restructuring audio.c a little --- keyboard/planck/keymaps/default/keymap.c | 24 ++-- keyboard/preonic/keymaps/default/keymap.c | 37 +++--- quantum/audio.c | 150 ++++++++++++---------- quantum/audio.h | 1 - 4 files changed, 115 insertions(+), 97 deletions(-) diff --git a/keyboard/planck/keymaps/default/keymap.c b/keyboard/planck/keymaps/default/keymap.c index 25a0d78f3b..ccf9c7adef 100644 --- a/keyboard/planck/keymaps/default/keymap.c +++ b/keyboard/planck/keymaps/default/keymap.c @@ -145,7 +145,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { {XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, LOWER, XXXXXXX, XXXXXXX, RAISE, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX} }, -/* Plover layer (http://openstenoproject.org) +/* Plover layer (http://opensteno.org) * ,-----------------------------------------------------------------------------------. * | # | # | # | # | # | # | # | # | # | # | # | # | * |------+------+------+------+------+-------------+------+------+------+------+------| @@ -225,16 +225,10 @@ float tone_dvorak[][2] = { {440.0*pow(2.0,(31)/12.0), 8} }; -float tone_music[][2] = { - {440.0*pow(2.0,(12)/12.0), 8}, - {440.0*pow(2.0,(14)/12.0), 8}, - {440.0*pow(2.0,(16)/12.0), 8}, - {440.0*pow(2.0,(17)/12.0), 8}, - {440.0*pow(2.0,(19)/12.0), 8}, - {440.0*pow(2.0,(21)/12.0), 8}, - {440.0*pow(2.0,(23)/12.0), 8}, - {440.0*pow(2.0,(24)/12.0), 8} -}; + +float music_scale[][2] = SONG(MUSIC_SCALE_SOUND); +float goodbye[][2] = SONG(GOODBYE_SOUND); + #endif @@ -324,7 +318,7 @@ const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt) case 9: if (record->event.pressed) { #ifdef AUDIO_ENABLE - PLAY_NOTE_ARRAY(tone_music, false, 0); + PLAY_NOTE_ARRAY(music_scale, false, 0); layer_on(_MUSIC); #endif } @@ -374,3 +368,9 @@ void matrix_init_user(void) { PLAY_NOTE_ARRAY(tone_startup, false, 0); #endif } + +void play_goodbye_tone() +{ + PLAY_NOTE_ARRAY(goodbye, false, 0); + _delay_ms(150); +} diff --git a/keyboard/preonic/keymaps/default/keymap.c b/keyboard/preonic/keymaps/default/keymap.c index 144e9c3086..ecf3400f8d 100644 --- a/keyboard/preonic/keymaps/default/keymap.c +++ b/keyboard/preonic/keymaps/default/keymap.c @@ -15,7 +15,7 @@ #define _LOWER 3 #define _RAISE 4 #define _MUSIC 5 -#define _ADJUST 6 +#define _ADJUST 16 // Macro name shortcuts #define QWERTY M(_QWERTY) @@ -142,6 +142,17 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { {_______, _______, _______, _______, _______, _______, _______, _______, KC_MNXT, KC_VOLD, KC_VOLU, KC_MPLY} }, +/* Music (reserved for process_action_user) + * + */ +[_MUSIC] = { + {XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX}, + {XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX}, + {XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX}, + {XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX}, + {XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, LOWER, XXXXXXX, XXXXXXX, RAISE, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX} +}, + /* Adjust (Lower + Raise) * ,-----------------------------------------------------------------------------------. * | F1 | F2 | F3 | F4 | F5 | F6 | F7 | F8 | F9 | F10 | F11 | F12 | @@ -161,19 +172,9 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { {_______, _______, _______, AUD_ON, AUD_OFF, AG_NORM, AG_SWAP, QWERTY, COLEMAK, DVORAK, _______, _______}, {_______, _______, _______, MUS_ON, MUS_OFF, _______, _______, _______, _______, _______, _______, _______}, {_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______} -}, - -/* Music (reserved for process_action_user) - * - */ -[_MUSIC] = { - {XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX}, - {XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX}, - {XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX}, - {XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX}, - {XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, LOWER, XXXXXXX, XXXXXXX, RAISE, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX} } + }; const uint16_t PROGMEM fn_actions[] = { @@ -315,7 +316,6 @@ const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt) case 9: if (record->event.pressed) { #ifdef AUDIO_ENABLE - init_notes(); PLAY_NOTE_ARRAY(music_scale, false, 0); layer_on(_MUSIC); #endif @@ -341,11 +341,12 @@ void process_action_user(keyrecord_t *record) { } void matrix_init_user(void) { - #ifdef AUDIO_ENABLE - init_notes(); - _delay_ms(10); - PLAY_NOTE_ARRAY(start_up, false, 0); - #endif + play_startup_tone(); +} + +void play_startup_tone() +{ + PLAY_NOTE_ARRAY(start_up, false, 0); } void play_goodbye_tone() diff --git a/quantum/audio.c b/quantum/audio.c index 8ea1bf6ff0..bbdbc824c9 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,66 +172,7 @@ void increase_tempo(uint8_t tempo_change) { } } -void stop_all_notes() { - voices = 0; - #ifdef PWM_AUDIO - TIMSK3 &= ~_BV(OCIE3A); - #else - TIMSK3 &= ~_BV(OCIE3A); - TCCR3A &= ~_BV(COM3A1); - #endif - notes = false; - note = false; - frequency = 0; - volume = 0; - - for (int i = 0; i < 8; i++) { - frequencies[i] = 0; - volumes[i] = 0; - } -} - -void stop_note(double freq) { - if (note) { - cli(); - #ifdef PWM_AUDIO - freq = freq / SAMPLE_RATE; - #endif - for (int i = 7; i >= 0; i--) { - if (frequencies[i] == freq) { - frequencies[i] = 0; - volumes[i] = 0; - for (int j = i; (j < 7); j++) { - frequencies[j] = frequencies[j+1]; - frequencies[j+1] = 0; - volumes[j] = volumes[j+1]; - volumes[j+1] = 0; - } - break; - } - } - voices--; - if (voices < 0) - voices = 0; - if (voice_place >= voices) { - voice_place = 0; - } - if (voices == 0) { - #ifdef PWM_AUDIO - TIMSK3 &= ~_BV(OCIE3A); - #else - TIMSK3 &= ~_BV(OCIE3A); - TCCR3A &= ~_BV(COM3A1); - #endif - frequency = 0; - volume = 0; - note = false; - } - sei(); - } -} - -void init_notes() { +void audio_init() { /* check signature */ if (!eeconfig_is_enabled()) { @@ -264,6 +207,71 @@ void init_notes() { 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); + #else + TIMSK3 &= ~_BV(OCIE3A); + TCCR3A &= ~_BV(COM3A1); + #endif + notes = false; + note = false; + frequency = 0; + volume = 0; + + for (int i = 0; i < 8; i++) { + frequencies[i] = 0; + volumes[i] = 0; + } +} + +void stop_note(double freq) { + if (note) { + if (!inited) { + audio_init(); + } + #ifdef PWM_AUDIO + freq = freq / SAMPLE_RATE; + #endif + for (int i = 7; i >= 0; i--) { + if (frequencies[i] == freq) { + frequencies[i] = 0; + volumes[i] = 0; + for (int j = i; (j < 7); j++) { + frequencies[j] = frequencies[j+1]; + frequencies[j+1] = 0; + volumes[j] = volumes[j+1]; + volumes[j+1] = 0; + } + break; + } + } + voices--; + if (voices < 0) + voices = 0; + if (voice_place >= voices) { + voice_place = 0; + } + if (voices == 0) { + #ifdef PWM_AUDIO + TIMSK3 &= ~_BV(OCIE3A); + #else + TIMSK3 &= ~_BV(OCIE3A); + TCCR3A &= ~_BV(COM3A1); + #endif + frequency = 0; + volume = 0; + note = false; + } + } } float mod(float a, int 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 85756af9d4..0fe3eac9af 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), \