qmk

QMK Firmware
git clone git://git.z3bra.org/qmk.git
Log | Files | Refs | Submodules | LICENSE

process_clicky.c (3577B)


      1 #include "audio.h"
      2 #include "process_clicky.h"
      3 
      4 #ifdef AUDIO_CLICKY
      5 
      6 #    ifndef AUDIO_CLICKY_DELAY_DURATION
      7 #        define AUDIO_CLICKY_DELAY_DURATION 1
      8 #    endif  // !AUDIO_CLICKY_DELAY_DURATION
      9 #    ifndef AUDIO_CLICKY_FREQ_DEFAULT
     10 #        define AUDIO_CLICKY_FREQ_DEFAULT 440.0f
     11 #    endif  // !AUDIO_CLICKY_FREQ_DEFAULT
     12 #    ifndef AUDIO_CLICKY_FREQ_MIN
     13 #        define AUDIO_CLICKY_FREQ_MIN 65.0f
     14 #    endif  // !AUDIO_CLICKY_FREQ_MIN
     15 #    ifndef AUDIO_CLICKY_FREQ_MAX
     16 #        define AUDIO_CLICKY_FREQ_MAX 1500.0f
     17 #    endif  // !AUDIO_CLICKY_FREQ_MAX
     18 #    ifndef AUDIO_CLICKY_FREQ_FACTOR
     19 #        define AUDIO_CLICKY_FREQ_FACTOR 1.18921f
     20 #    endif  // !AUDIO_CLICKY_FREQ_FACTOR
     21 #    ifndef AUDIO_CLICKY_FREQ_RANDOMNESS
     22 #        define AUDIO_CLICKY_FREQ_RANDOMNESS 0.05f
     23 #    endif  // !AUDIO_CLICKY_FREQ_RANDOMNESS
     24 
     25 float clicky_freq = AUDIO_CLICKY_FREQ_DEFAULT;
     26 float clicky_rand = AUDIO_CLICKY_FREQ_RANDOMNESS;
     27 
     28 // the first "note" is an intentional delay; the 2nd and 3rd notes are the "clicky"
     29 float clicky_song[][2] = {{AUDIO_CLICKY_FREQ_MIN, AUDIO_CLICKY_DELAY_DURATION}, {AUDIO_CLICKY_FREQ_DEFAULT, 3}, {AUDIO_CLICKY_FREQ_DEFAULT, 1}};  // 3 and 1 --> durations
     30 
     31 extern audio_config_t audio_config;
     32 
     33 #    ifndef NO_MUSIC_MODE
     34 extern bool music_activated;
     35 extern bool midi_activated;
     36 #    endif  // !NO_MUSIC_MODE
     37 
     38 void clicky_play(void) {
     39 #    ifndef NO_MUSIC_MODE
     40     if (music_activated || midi_activated || !audio_config.enable) return;
     41 #    endif  // !NO_MUSIC_MODE
     42     clicky_song[1][0] = 2.0f * clicky_freq * (1.0f + clicky_rand * (((float)rand()) / ((float)(RAND_MAX))));
     43     clicky_song[2][0] = clicky_freq * (1.0f + clicky_rand * (((float)rand()) / ((float)(RAND_MAX))));
     44     PLAY_SONG(clicky_song);
     45 }
     46 
     47 void clicky_freq_up(void) {
     48     float new_freq = clicky_freq * AUDIO_CLICKY_FREQ_FACTOR;
     49     if (new_freq < AUDIO_CLICKY_FREQ_MAX) {
     50         clicky_freq = new_freq;
     51     }
     52 }
     53 
     54 void clicky_freq_down(void) {
     55     float new_freq = clicky_freq / AUDIO_CLICKY_FREQ_FACTOR;
     56     if (new_freq > AUDIO_CLICKY_FREQ_MIN) {
     57         clicky_freq = new_freq;
     58     }
     59 }
     60 
     61 void clicky_freq_reset(void) { clicky_freq = AUDIO_CLICKY_FREQ_DEFAULT; }
     62 
     63 void clicky_toggle(void) {
     64     audio_config.clicky_enable ^= 1;
     65     eeconfig_update_audio(audio_config.raw);
     66 }
     67 
     68 void clicky_on(void) {
     69     audio_config.clicky_enable = 1;
     70     eeconfig_update_audio(audio_config.raw);
     71 }
     72 
     73 void clicky_off(void) {
     74     audio_config.clicky_enable = 0;
     75     eeconfig_update_audio(audio_config.raw);
     76 }
     77 
     78 bool is_clicky_on(void) { return (audio_config.clicky_enable != 0); }
     79 
     80 bool process_clicky(uint16_t keycode, keyrecord_t *record) {
     81     if (keycode == CLICKY_TOGGLE && record->event.pressed) {
     82         clicky_toggle();
     83     }
     84 
     85     if (keycode == CLICKY_ENABLE && record->event.pressed) {
     86         clicky_on();
     87     }
     88     if (keycode == CLICKY_DISABLE && record->event.pressed) {
     89         clicky_off();
     90     }
     91 
     92     if (keycode == CLICKY_RESET && record->event.pressed) {
     93         clicky_freq_reset();
     94     }
     95 
     96     if (keycode == CLICKY_UP && record->event.pressed) {
     97         clicky_freq_up();
     98     }
     99     if (keycode == CLICKY_DOWN && record->event.pressed) {
    100         clicky_freq_down();
    101     }
    102 
    103     if (audio_config.enable && audio_config.clicky_enable) {
    104         if (record->event.pressed) {                       // Leave this separate so it's easier to add upstroke sound
    105             if (keycode != AU_OFF && keycode != AU_TOG) {  // DO NOT PLAY if audio will be disabled, and causes issuse on ARM
    106                 clicky_play();
    107             }
    108         }
    109     }
    110     return true;
    111 }
    112 
    113 #endif  // AUDIO_CLICKY