From a2f31c886ff13c5e7adeccccfe672698c1c7efb9 Mon Sep 17 00:00:00 2001 From: Jack Humbert Date: Tue, 19 Apr 2016 12:58:13 -0400 Subject: [PATCH] getting ready for getters and setters --- keyboard/preonic/keymaps/default/keymap.c | 28 ++---- quantum/audio.c | 81 +++++++++-------- quantum/audio.h | 104 ++++++++++++++++++++++ 3 files changed, 156 insertions(+), 57 deletions(-) diff --git a/keyboard/preonic/keymaps/default/keymap.c b/keyboard/preonic/keymaps/default/keymap.c index 2546d8f6c..144e9c308 100644 --- a/keyboard/preonic/keymaps/default/keymap.c +++ b/keyboard/preonic/keymaps/default/keymap.c @@ -14,9 +14,8 @@ #define _DVORAK 2 #define _LOWER 3 #define _RAISE 4 +#define _MUSIC 5 #define _ADJUST 6 -#define _MUSIC 7 -#define _STOPPER 5 // Macro name shortcuts #define QWERTY M(_QWERTY) @@ -159,7 +158,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { [_ADJUST] = { {KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12}, {_______, RESET, _______, _______, _______, _______, _______, _______, _______, _______, _______, KC_DEL}, - {_______, _______, _______, AUD_ON, AUD_OFF, AG_NORM, AG_SWAP, QWERTY, COLEMAK, DVORAK, _______, _______}, + {_______, _______, _______, AUD_ON, AUD_OFF, AG_NORM, AG_SWAP, QWERTY, COLEMAK, DVORAK, _______, _______}, {_______, _______, _______, MUS_ON, MUS_OFF, _______, _______, _______, _______, _______, _______, _______}, {_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______} }, @@ -168,22 +167,11 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { * */ [_MUSIC] = { - {_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______}, - {_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______}, - {_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______}, - {_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______}, - {_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______} -}, - -/* Stopper - * - */ -[_STOPPER] = { {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, RAISE, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX} + {XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, LOWER, XXXXXXX, XXXXXXX, RAISE, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX} } }; @@ -237,7 +225,7 @@ float tone_music[][2] = { {440.0*pow(2.0,(23)/12.0), 8}, {440.0*pow(2.0,(24)/12.0), 8} }; -float ode_to_joy[][2] = SONG(ODE_TO_JOY); +float music_scale[][2] = SONG(MUSIC_SCALE_SOUND); float goodbye[][2] = SONG(GOODBYE_SOUND); #endif @@ -320,7 +308,6 @@ const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt) if (record->event.pressed) { #ifdef AUDIO_ENABLE layer_off(_MUSIC); - layer_off(_STOPPER); stop_all_notes(); #endif } @@ -329,10 +316,8 @@ const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt) if (record->event.pressed) { #ifdef AUDIO_ENABLE init_notes(); - set_tempo(150); - PLAY_NOTE_ARRAY(ode_to_joy, false, .25); + PLAY_NOTE_ARRAY(music_scale, false, 0); layer_on(_MUSIC); - layer_on(_STOPPER); #endif } break; @@ -358,6 +343,7 @@ 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 } @@ -365,5 +351,5 @@ void matrix_init_user(void) { void play_goodbye_tone() { PLAY_NOTE_ARRAY(goodbye, false, 0); - _delay_ms(2000); + _delay_ms(150); } \ No newline at end of file diff --git a/quantum/audio.c b/quantum/audio.c index d8768f160..c92cb5373 100644 --- a/quantum/audio.c +++ b/quantum/audio.c @@ -34,8 +34,6 @@ int voice_place = 0; double frequency = 0; int volume = 0; long position = 0; -int duty_place = 1; -int duty_counter = 0; double frequencies[8] = {0, 0, 0, 0, 0, 0, 0, 0}; int volumes[8] = {0, 0, 0, 0, 0, 0, 0, 0}; @@ -51,7 +49,7 @@ uint16_t place_int = 0; bool repeat = true; uint8_t * sample; uint16_t sample_length = 0; - +double freq = 0; bool notes = false; bool note = false; @@ -65,11 +63,15 @@ uint8_t notes_count; bool notes_repeat; float notes_rest; bool note_resting = false; -int note_flipper = 0; uint8_t current_note = 0; uint8_t rest_counter = 0; +uint8_t vibrato_counter = 0; +float vibrato_strength = 0; + +float polyphony_rate = 0; + audio_config_t audio_config; @@ -182,6 +184,11 @@ void init_notes() { #endif } +float mod(float a, int b) +{ + float r = fmod(a, b); + return r < 0 ? r + b : r; +} ISR(TIMER3_COMPA_vect) { if (note) { @@ -233,48 +240,41 @@ ISR(TIMER3_COMPA_vect) { OCR4A = sum; } #else - if (frequencies[voice_place] > 0) { - // if (frequencies[voice_place] > 880.0) { - // if (note_flipper == 100) { - // note_flipper = 0; - // return; - // } - // note_flipper++; - // } else { - // note_flipper = 0; - // } - // ICR3 = (int)(((double)F_CPU) / frequency); // Set max to the period - // OCR3A = (int)(((double)F_CPU) / frequency) >> 1; // Set compare to half the period - - double freq; - if (false) { - voice_place %= voices; - if (place > (frequencies[voice_place] / 50)) { - voice_place = (voice_place + 1) % voices; - place = 0.0; + if (voices > 0) { + if (false && polyphony_rate > 0) { + if (voices > 1) { + voice_place %= voices; + if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER / voices)) { + voice_place = (voice_place + 1) % voices; + place = 0.0; + } } - freq = frequencies[voice_place]; + if (vibrato_strength > 0) { + freq = frequencies[voice_place] * pow(VIBRATO_LUT[(int)vibrato_counter], vibrato_strength); + vibrato_counter = mod((vibrato_counter + 1), VIBRATO_LUT_LENGTH); + } else { + freq = frequencies[voice_place]; + } } else { if (frequency != 0) { if (frequency < frequencies[voices - 1]) { - frequency = frequency * 1.01454533494; + frequency = frequency * pow(2, 440/frequencies[voices - 1]/12/4); } else if (frequency > frequencies[voices - 1]) { - frequency = frequency * 0.98566319864; + frequency = frequency * pow(2, -440/frequencies[voices - 1]/12/4); } } else { frequency = frequencies[voices - 1]; } - freq = frequency; + + if (false && vibrato_strength > 0) { + freq = frequency * pow(VIBRATO_LUT[(int)vibrato_counter], vibrato_strength); + vibrato_counter = mod((vibrato_counter + 1 + 440/frequencies[voices - 1]), VIBRATO_LUT_LENGTH); + } else { + freq = frequency; + } } ICR3 = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period OCR3A = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period - //OCR3A = (int)(((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)) >> 1 * duty_place; // Set compare to half the period - place++; - // if (duty_counter > (frequencies[voice_place] / 500)) { - // duty_place = (duty_place % 3) + 1; - // duty_counter = 0; - // } - // duty_counter++; } #endif } @@ -300,8 +300,17 @@ ISR(TIMER3_COMPA_vect) { place -= SINE_LENGTH; #else if (note_frequency > 0) { - ICR3 = (int)(((double)F_CPU) / (note_frequency * CPU_PRESCALER)); // Set max to the period - OCR3A = (int)((((double)F_CPU) / (note_frequency * CPU_PRESCALER)) * note_timbre); // Set compare to half the period + float freq; + + if (false && vibrato_strength > 0) { + freq = note_frequency * pow(VIBRATO_LUT[(int)vibrato_counter], vibrato_strength); + vibrato_counter = mod((vibrato_counter + 1), VIBRATO_LUT_LENGTH); + } else { + freq = note_frequency; + } + + ICR3 = (int)(((double)F_CPU) / (freq * CPU_PRESCALER)); // Set max to the period + OCR3A = (int)((((double)F_CPU) / (freq * CPU_PRESCALER)) * note_timbre); // Set compare to half the period } else { ICR3 = 0; OCR3A = 0; diff --git a/quantum/audio.h b/quantum/audio.h index 44cafccd6..f705341d7 100644 --- a/quantum/audio.h +++ b/quantum/audio.h @@ -47,4 +47,108 @@ void decrease_tempo(uint8_t tempo_change); void play_goodbye_tone(void); void play_startup_tone(void); +#define VIBRATO_LUT (float []) { \ +1.00090714186239, \ +1.00181152169061, \ +1.00270955652027, \ +1.00359767896099, \ +1.00447235162891, \ +1.00533008160601, \ +1.00616743486158, \ +1.00698105056935, \ +1.00776765525194, \ +1.00852407668313, \ +1.0092472574777, \ +1.00993426829815, \ +1.01058232060837, \ +1.01118877890462, \ +1.01175117235612, \ +1.01226720578933, \ +1.01273476995269, \ +1.01315195100182, \ +1.0135170391489, \ +1.01382853642434, \ +1.01408516350345, \ +1.01428586555648, \ +1.0144298170856, \ +1.0145164257189, \ +1.01454533493752, \ +1.0145164257189, \ +1.0144298170856, \ +1.01428586555648, \ +1.01408516350345, \ +1.01382853642434, \ +1.0135170391489, \ +1.01315195100182, \ +1.01273476995269, \ +1.01226720578933, \ +1.01175117235612, \ +1.01118877890462, \ +1.01058232060837, \ +1.00993426829815, \ +1.0092472574777, \ +1.00852407668313, \ +1.00776765525194, \ +1.00698105056935, \ +1.00616743486158, \ +1.00533008160601, \ +1.00447235162891, \ +1.00359767896099, \ +1.00270955652027, \ +1.00181152169061, \ +1.00090714186239, \ +1, \ +0.999093680298157, \ +0.998191753986265, \ +0.997297765337276, \ +0.996415217934032, \ +0.995547561242821, \ +0.99469817754036, \ +0.993870369236874, \ +0.993067346634376, \ +0.992292216155724, \ +0.991547969076588, \ +0.990837470789065, \ +0.990163450622494, \ +0.989528492243954, \ +0.988935024658062, \ +0.988385313823004, \ +0.98788145489731, \ +0.987425365129624, \ +0.987018777401739, \ +0.986663234433381, \ +0.986360083655655, \ +0.986110472758728, \ +0.985915345918143, \ +0.985775440703176, \ +0.985691285669809, \ +0.985663198640188, \ +0.985691285669809, \ +0.985775440703176, \ +0.985915345918143, \ +0.986110472758728, \ +0.986360083655655, \ +0.986663234433381, \ +0.987018777401739, \ +0.987425365129624, \ +0.98788145489731, \ +0.988385313823004, \ +0.988935024658062, \ +0.989528492243954, \ +0.990163450622494, \ +0.990837470789065, \ +0.991547969076588, \ +0.992292216155724, \ +0.993067346634376, \ +0.993870369236874, \ +0.99469817754036, \ +0.99554756124282, \ +0.996415217934032, \ +0.997297765337276, \ +0.998191753986265, \ +0.999093680298157, \ +1, \ +} +#define VIBRATO_LUT_LENGTH NOTE_ARRAY_SIZE(VIBRATO_LUT) + #endif \ No newline at end of file