diff options
author | Stephan Bösebeck <sb@caluga.de> | 2016-04-19 23:07:11 +0200 |
---|---|---|
committer | Stephan Bösebeck <sb@caluga.de> | 2016-04-19 23:07:11 +0200 |
commit | 6ff5e1059256cfd6ec1cb88c12e90f57c10f44ef (patch) | |
tree | fa23de88dc8b8d3f3fe6a2e353eaa3ae1e38a1b8 /quantum | |
parent | 04f36b36818885475c0b93046726d7e053691e7a (diff) | |
parent | a2ee27715880616a15756e1b9f42183d9fb08051 (diff) | |
download | qmk_firmware-6ff5e1059256cfd6ec1cb88c12e90f57c10f44ef.tar.gz qmk_firmware-6ff5e1059256cfd6ec1cb88c12e90f57c10f44ef.zip |
Merge branch 'master' of https://github.com/jackhumbert/qmk_firmware
Diffstat (limited to 'quantum')
-rw-r--r-- | quantum/audio.c | 245 | ||||
-rw-r--r-- | quantum/audio.h | 38 | ||||
-rw-r--r-- | quantum/keymap_common.c | 15 | ||||
-rw-r--r-- | quantum/musical_notes.h | 5 | ||||
-rw-r--r-- | quantum/song_list.h | 104 | ||||
-rw-r--r-- | quantum/vibrato_lut.h | 108 |
6 files changed, 404 insertions, 111 deletions
diff --git a/quantum/audio.c b/quantum/audio.c index e0413051a0..8ea1bf6ff0 100644 --- a/quantum/audio.c +++ b/quantum/audio.c @@ -10,10 +10,13 @@ #include "eeconfig.h" +#include "vibrato_lut.h" + #define PI 3.14159265 #define CPU_PRESCALER 8 +// Largely untested PWM audio mode (doesn't sound as good) // #define PWM_AUDIO #ifdef PWM_AUDIO @@ -34,8 +37,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 +52,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; @@ -69,6 +70,12 @@ bool note_resting = false; uint8_t current_note = 0; uint8_t rest_counter = 0; +float vibrato_counter = 0; +float vibrato_strength = .5; +float vibrato_rate = 0.125; + +float polyphony_rate = .5; + audio_config_t audio_config; @@ -87,6 +94,81 @@ void audio_off(void) { eeconfig_write_audio(audio_config.raw); } +// Vibrato rate functions + +void set_vibrato_rate(float rate) { + vibrato_rate = rate; +} + +void increase_vibrato_rate(float change) { + vibrato_rate *= change; +} + +void decrease_vibrato_rate(float change) { + vibrato_rate /= change; +} + +#ifdef VIBRATO_STRENGTH_ENABLE + +void set_vibrato_strength(float strength) { + vibrato_strength = strength; +} + +void increase_vibrato_strength(float change) { + vibrato_strength *= change; +} + +void decrease_vibrato_strength(float change) { + vibrato_strength /= change; +} + +#endif + +// Polyphony functions + +void set_polyphony_rate(float rate) { + polyphony_rate = rate; +} + +void enable_polyphony() { + polyphony_rate = 5; +} + +void disable_polyphony() { + polyphony_rate = 0; +} + +void increase_polyphony_rate(float change) { + polyphony_rate *= change; +} + +void decrease_polyphony_rate(float change) { + polyphony_rate /= change; +} + +// Timbre function + +void set_timbre(float timbre) { + note_timbre = timbre; +} + +// Tempo functions + +void set_tempo(float tempo) { + note_tempo = tempo; +} + +void decrease_tempo(uint8_t tempo_change) { + note_tempo += (float) tempo_change; +} + +void increase_tempo(uint8_t tempo_change) { + if (note_tempo - (float) tempo_change < 10) { + note_tempo = 10; + } else { + note_tempo -= (float) tempo_change; + } +} void stop_all_notes() { voices = 0; @@ -109,6 +191,7 @@ void stop_all_notes() { void stop_note(double freq) { if (note) { + cli(); #ifdef PWM_AUDIO freq = freq / SAMPLE_RATE; #endif @@ -122,11 +205,15 @@ void stop_note(double freq) { 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); @@ -137,26 +224,8 @@ void stop_note(double freq) { frequency = 0; volume = 0; note = false; - } else { - double freq = frequencies[voices - 1]; - int vol = volumes[voices - 1]; - double starting_f = frequency; - if (frequency < freq) { - sliding = true; - for (double f = starting_f; f <= freq; f += ((freq - starting_f) / 2000.0)) { - frequency = f; - } - sliding = false; - } else if (frequency > freq) { - sliding = true; - for (double f = starting_f; f >= freq; f -= ((starting_f - freq) / 2000.0)) { - frequency = f; - } - sliding = false; - } - frequency = freq; - volume = vol; } + sei(); } } @@ -197,6 +266,21 @@ void init_notes() { #endif } +float mod(float a, int b) +{ + float r = fmod(a, b); + return r < 0 ? r + b : r; +} + +float vibrato(float average_freq) { + #ifdef VIBRATO_STRENGTH_ENABLE + float vibrated_freq = average_freq * pow(VIBRATO_LUT[(int)vibrato_counter], vibrato_strength); + #else + float vibrated_freq = average_freq * VIBRATO_LUT[(int)vibrato_counter]; + #endif + vibrato_counter = mod((vibrato_counter + vibrato_rate * (1.0 + 440.0/average_freq)), VIBRATO_LUT_LENGTH); + return vibrated_freq; +} ISR(TIMER3_COMPA_vect) { if (note) { @@ -248,23 +332,37 @@ ISR(TIMER3_COMPA_vect) { OCR4A = sum; } #else - if (frequency > 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 - voice_place %= voices; - if (place > (frequencies[voice_place] / 50)) { - voice_place = (voice_place + 1) % voices; - place = 0.0; + if (voices > 0) { + if (polyphony_rate > 0) { + if (voices > 1) { + voice_place %= voices; + if (place++ > (frequencies[voice_place] / polyphony_rate / CPU_PRESCALER)) { + voice_place = (voice_place + 1) % voices; + place = 0.0; + } + } + if (vibrato_strength > 0) { + freq = vibrato(frequencies[voice_place]); + } else { + freq = frequencies[voice_place]; + } + } else { + if (frequency != 0 && frequency < frequencies[voices - 1] && frequency < frequencies[voices - 1] * pow(2, -440/frequencies[voices - 1]/12/2)) { + frequency = frequency * pow(2, 440/frequency/12/2); + } else if (frequency != 0 && frequency > frequencies[voices - 1] && frequency > frequencies[voices - 1] * pow(2, 440/frequencies[voices - 1]/12/2)) { + frequency = frequency * pow(2, -440/frequency/12/2); + } else { + frequency = frequencies[voices - 1]; + } + + if (vibrato_strength > 0) { + freq = vibrato(frequency); + } else { + freq = frequency; + } } - ICR3 = (int)(((double)F_CPU) / (frequencies[voice_place] * CPU_PRESCALER)); // Set max to the period - OCR3A = (int)((((double)F_CPU) / (frequencies[voice_place] * 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++; + 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 } #endif } @@ -290,8 +388,16 @@ 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 (vibrato_strength > 0) { + freq = vibrato(note_frequency); + } 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; @@ -350,7 +456,8 @@ 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(); + // Cancel note if a note is playing if (note) stop_all_notes(); notes = true; @@ -378,7 +485,7 @@ if (audio_config.enable) { TIMSK3 |= _BV(OCIE3A); TCCR3A |= _BV(COM3A1); #endif - + sei(); } } @@ -405,7 +512,8 @@ if (audio_config.enable) { void play_note(double freq, int vol) { if (audio_config.enable && voices < 8) { - + cli(); + // Cancel notes if notes are playing if (notes) stop_all_notes(); note = true; @@ -413,23 +521,8 @@ if (audio_config.enable && voices < 8) { freq = freq / SAMPLE_RATE; #endif if (freq > 0) { - if (frequency != 0) { - double starting_f = frequency; - if (frequency < freq) { - for (double f = starting_f; f <= freq; f += ((freq - starting_f) / 2000.0)) { - frequency = f; - } - } else if (frequency > freq) { - for (double f = starting_f; f >= freq; f -= ((starting_f - freq) / 2000.0)) { - frequency = f; - } - } - } - frequency = freq; - volume = vol; - - frequencies[voices] = frequency; - volumes[voices] = volume; + frequencies[voices] = freq; + volumes[voices] = vol; voices++; } @@ -439,35 +532,21 @@ if (audio_config.enable && voices < 8) { TIMSK3 |= _BV(OCIE3A); TCCR3A |= _BV(COM3A1); #endif - + sei(); } } -void set_timbre(float timbre) -{ - note_timbre = timbre; -} - -void set_tempo(float tempo) +//------------------------------------------------------------------------------ +// Override these functions in your keymap file to play different tunes on +// startup and bootloader jump +__attribute__ ((weak)) +void play_startup_tone() { - note_tempo = tempo; } -void decrease_tempo(uint8_t tempo_change) +__attribute__ ((weak)) +void play_goodbye_tone() { - note_tempo += (float) tempo_change; } - -void increase_tempo(uint8_t tempo_change) -{ - if (note_tempo - (float) tempo_change < 10) - { - note_tempo = 10; - } - else - { - note_tempo -= (float) tempo_change; - } -} - +//------------------------------------------------------------------------------ diff --git a/quantum/audio.h b/quantum/audio.h index 05d314c940..85756af9d4 100644 --- a/quantum/audio.h +++ b/quantum/audio.h @@ -8,6 +8,9 @@ #ifndef AUDIO_H #define AUDIO_H +// Enable vibrato strength/amplitude - slows down ISR too much +// #define VIBRATO_STRENGTH_ENABLE + typedef union { uint8_t raw; struct { @@ -20,6 +23,34 @@ void audio_toggle(void); void audio_on(void); void audio_off(void); +// Vibrato rate functions + +void set_vibrato_rate(float rate); +void increase_vibrato_rate(float change); +void decrease_vibrato_rate(float change); + +#ifdef VIBRATO_STRENGTH_ENABLE + +void set_vibrato_strength(float strength); +void increase_vibrato_strength(float change); +void decrease_vibrato_strength(float change); + +#endif + +// Polyphony functions + +void set_polyphony_rate(float rate); +void enable_polyphony(); +void disable_polyphony(); +void increase_polyphony_rate(float change); +void decrease_polyphony_rate(float change); + +void set_timbre(float timbre); +void set_tempo(float tempo); + +void increase_tempo(uint8_t tempo_change); +void decrease_tempo(uint8_t tempo_change); + void play_sample(uint8_t * s, uint16_t l, bool r); void play_note(double freq, int vol); void stop_note(double freq); @@ -27,11 +58,6 @@ 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); -void set_timbre(float timbre); -void set_tempo(float tempo); -void increase_tempo(uint8_t tempo_change); -void decrease_tempo(uint8_t tempo_change); - #define SCALE (int []){ 0 + (12*0), 2 + (12*0), 4 + (12*0), 5 + (12*0), 7 + (12*0), 9 + (12*0), 11 + (12*0), \ 0 + (12*1), 2 + (12*1), 4 + (12*1), 5 + (12*1), 7 + (12*1), 9 + (12*1), 11 + (12*1), \ 0 + (12*2), 2 + (12*2), 4 + (12*2), 5 + (12*2), 7 + (12*2), 9 + (12*2), 11 + (12*2), \ @@ -44,5 +70,7 @@ void decrease_tempo(uint8_t tempo_change); #define NOTE_ARRAY_SIZE(x) ((int)(sizeof(x) / (sizeof(x[0])))) #define PLAY_NOTE_ARRAY(note_array, note_repeat, note_rest_style) play_notes(¬e_array, NOTE_ARRAY_SIZE((note_array)), (note_repeat), (note_rest_style)); +void play_goodbye_tone(void); +void play_startup_tone(void); #endif
\ No newline at end of file diff --git a/quantum/keymap_common.c b/quantum/keymap_common.c index 2001438b90..43debf4ef6 100644 --- a/quantum/keymap_common.c +++ b/quantum/keymap_common.c @@ -34,12 +34,6 @@ extern keymap_config_t keymap_config; #include <inttypes.h> #ifdef AUDIO_ENABLE #include "audio.h" - - #ifndef TONE_GOODBYE - #define TONE_GOODBYE OLKB_GOODBYE - #endif /*! TONE_GOODBYE */ - - float tone_goodbye[][2] = SONG(TONE_GOODBYE); #endif /* AUDIO_ENABLE */ static action_t keycode_to_action(uint16_t keycode); @@ -47,7 +41,7 @@ static action_t keycode_to_action(uint16_t keycode); /* converts key to action */ action_t action_for_key(uint8_t layer, keypos_t key) { - // 16bit keycodes - important + // 16bit keycodes - important uint16_t keycode = keymap_key_to_keycode(layer, key); switch (keycode) { @@ -190,7 +184,8 @@ static action_t keycode_to_action(uint16_t keycode) case RESET: ; // RESET is 0x5000, which is why this is here clear_keyboard(); #ifdef AUDIO_ENABLE - PLAY_NOTE_ARRAY(tone_goodbye, false, 0); + stop_all_notes(); + play_goodbye_tone(); #endif _delay_ms(250); #ifdef ATREUS_ASTAR @@ -303,7 +298,7 @@ static action_t keycode_to_action(uint16_t keycode) /* translates key to keycode */ uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key) { - // Read entire word (16bits) + // Read entire word (16bits) return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]); } @@ -315,7 +310,7 @@ action_t keymap_fn_to_action(uint16_t keycode) action_t keymap_func_to_action(uint16_t keycode) { - // For FUNC without 8bit limit + // For FUNC without 8bit limit return (action_t){ .code = pgm_read_word(&fn_actions[(int)keycode]) }; } diff --git a/quantum/musical_notes.h b/quantum/musical_notes.h index ccdc34f270..b08d16a6fa 100644 --- a/quantum/musical_notes.h +++ b/quantum/musical_notes.h @@ -51,7 +51,10 @@ // Notes - # = Octave + #define NOTE_REST 0.00 + +/* These notes are currently bugged #define NOTE_C0 16.35 #define NOTE_CS0 17.32 #define NOTE_D0 18.35 @@ -75,6 +78,8 @@ #define NOTE_GS1 51.91 #define NOTE_A1 55.00 #define NOTE_AS1 58.27 +*/ + #define NOTE_B1 61.74 #define NOTE_C2 65.41 #define NOTE_CS2 69.30 diff --git a/quantum/song_list.h b/quantum/song_list.h index b626c3fa6b..e992bd18a2 100644 --- a/quantum/song_list.h +++ b/quantum/song_list.h @@ -4,20 +4,98 @@ #define SONG_LIST_H #define ODE_TO_JOY \ - Q__NOTE(_E4), Q__NOTE(_E4), Q__NOTE(_F4), Q__NOTE(_G4), \ - Q__NOTE(_G4), Q__NOTE(_F4), Q__NOTE(_E4), Q__NOTE(_D4), \ - Q__NOTE(_C4), Q__NOTE(_C4), Q__NOTE(_D4), Q__NOTE(_E4), \ - QD_NOTE(_E4), E__NOTE(_D4), H__NOTE(_D4), + Q__NOTE(_E4), Q__NOTE(_E4), Q__NOTE(_F4), Q__NOTE(_G4), \ + Q__NOTE(_G4), Q__NOTE(_F4), Q__NOTE(_E4), Q__NOTE(_D4), \ + Q__NOTE(_C4), Q__NOTE(_C4), Q__NOTE(_D4), Q__NOTE(_E4), \ + QD_NOTE(_E4), E__NOTE(_D4), H__NOTE(_D4), #define ROCK_A_BYE_BABY \ - QD_NOTE(_B4), E__NOTE(_D4), Q__NOTE(_B5), \ - H__NOTE(_A5), Q__NOTE(_G5), \ - QD_NOTE(_B4), E__NOTE(_D5), Q__NOTE(_G5), \ - H__NOTE(_FS5), - -#define OLKB_GOODBYE \ - E__NOTE(_E7), \ - E__NOTE(_A6), \ - ED_NOTE(_E6), + QD_NOTE(_B4), E__NOTE(_D4), Q__NOTE(_B5), \ + H__NOTE(_A5), Q__NOTE(_G5), \ + QD_NOTE(_B4), E__NOTE(_D5), Q__NOTE(_G5), \ + H__NOTE(_FS5), + +#define CLOSE_ENCOUNTERS_5_NOTE \ + Q__NOTE(_D5), \ + Q__NOTE(_E5), \ + Q__NOTE(_C5), \ + Q__NOTE(_C4), \ + Q__NOTE(_G4), + +#define DOE_A_DEER \ + QD_NOTE(_C4), E__NOTE(_D4), \ + QD_NOTE(_E4), E__NOTE(_C4), \ + Q__NOTE(_E4), Q__NOTE(_C4), \ + Q__NOTE(_E4), + +#define GOODBYE_SOUND \ + E__NOTE(_E7), \ + E__NOTE(_A6), \ + ED_NOTE(_E6), + +#define STARTUP_SOUND \ + ED_NOTE(_E7 ), \ + E__NOTE(_CS7), \ + E__NOTE(_E6 ), \ + E__NOTE(_A6 ), \ + M__NOTE(_CS7, 20), + +#define QWERTY_SOUND \ + E__NOTE(_GS6 ), \ + E__NOTE(_A6 ), \ + S__NOTE(_REST), \ + Q__NOTE(_E7 ), + +#define COLEMAK_SOUND \ + E__NOTE(_GS6 ), \ + E__NOTE(_A6 ), \ + S__NOTE(_REST), \ + ED_NOTE(_E7 ), \ + S__NOTE(_REST), \ + ED_NOTE(_GS7 ), + +#define DVORAK_SOUND \ + E__NOTE(_GS6 ), \ + E__NOTE(_A6 ), \ + S__NOTE(_REST), \ + E__NOTE(_E7 ), \ + S__NOTE(_REST), \ + E__NOTE(_FS7 ), \ + S__NOTE(_REST), \ + E__NOTE(_E7 ), + +#define MUSIC_SCALE_SOUND \ + E__NOTE(_A5 ), \ + E__NOTE(_B5 ), \ + E__NOTE(_CS6), \ + E__NOTE(_D6 ), \ + E__NOTE(_E6 ), \ + E__NOTE(_FS6), \ + E__NOTE(_GS6), \ + E__NOTE(_A6 ), + +#define CAPS_LOCK_ON_SOUND \ + E__NOTE(_A3), \ + E__NOTE(_B3), + +#define CAPS_LOCK_OFF_SOUND \ + E__NOTE(_B3), \ + E__NOTE(_A3), + +#define SCROLL_LOCK_ON_SOUND \ + E__NOTE(_D4), \ + E__NOTE(_E4), + +#define SCROLL_LOCK_OFF_SOUND \ + E__NOTE(_E4), \ + E__NOTE(_D4), + +#define NUM_LOCK_ON_SOUND \ + E__NOTE(_D5), \ + E__NOTE(_E5), + +#define NUM_LOCK_OFF_SOUND \ + E__NOTE(_E5), \ + E__NOTE(_D5), #endif diff --git a/quantum/vibrato_lut.h b/quantum/vibrato_lut.h new file mode 100644 index 0000000000..4c267a626a --- /dev/null +++ b/quantum/vibrato_lut.h @@ -0,0 +1,108 @@ +#include <avr/io.h> +#include <avr/interrupt.h> +#include <avr/pgmspace.h> + +#define VIBRATO_LUT_LENGTH 100 + +const float VIBRATO_LUT[VIBRATO_LUT_LENGTH] = { \ +1.00045346811453, +1.00090535101508, +1.00135386178926, +1.00179722447259, +1.00223368114872, +1.0026614990145, +1.00307897737994, +1.00348445457284, +1.00387631471807, +1.00425299436105, +1.00461298890553, +1.00495485883603, +1.00527723569589, +1.00557882779254, +1.00585842560279, +1.00611490685176, +1.00634724124066, +1.00655449479987, +1.00673583384565, +1.00689052852052, +1.00701795589922, +1.00711760264454, +1.0071890671992, +1.00723206150266, +1.0072464122237, +1.00723206150266, +1.0071890671992, +1.00711760264454, +1.00701795589922, +1.00689052852052, +1.00673583384565, +1.00655449479987, +1.00634724124066, +1.00611490685176, +1.00585842560279, +1.00557882779254, +1.00527723569589, +1.00495485883603, +1.00461298890553, +1.00425299436105, +1.00387631471807, +1.00348445457284, +1.00307897737994, +1.0026614990145, +1.00223368114872, +1.00179722447259, +1.00135386178926, +1.00090535101508, +1.00045346811453, +1, +0.999546737425598, +0.999095467903976, +0.998647968674285, +0.998205999748565, +0.99777129706302, +0.997345565759612, +0.996930473622346, +0.996527644691494, +0.996138653077835, +0.99576501699778, +0.995408193048995, +0.995069570744927, +0.994750467325326, +0.994452122858643, +0.994175695650927, +0.993922257974591, +0.99369279212925, +0.993488186845591, +0.993309234042139, +0.993156625943589, +0.993030952568311, +0.99293269959154, +0.992862246589715, +0.992819865670409, +0.992805720491269, +0.992819865670409, +0.992862246589715, +0.99293269959154, +0.993030952568311, +0.993156625943589, +0.993309234042139, +0.993488186845591, +0.99369279212925, +0.993922257974591, +0.994175695650927, +0.994452122858643, +0.994750467325326, +0.995069570744927, +0.995408193048995, +0.99576501699778, +0.996138653077835, +0.996527644691494, +0.996930473622346, +0.997345565759612, +0.99777129706302, +0.998205999748565, +0.998647968674285, +0.999095467903976, +0.999546737425598, +1 +};
\ No newline at end of file |