You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.5 KiB

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