process_audio.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "audio.h"
  2. #include "process_audio.h"
  3. #ifndef VOICE_CHANGE_SONG
  4. #define VOICE_CHANGE_SONG SONG(VOICE_CHANGE_SOUND)
  5. #endif
  6. float voice_change_song[][2] = VOICE_CHANGE_SONG;
  7. #ifndef PITCH_STANDARD_A
  8. #define PITCH_STANDARD_A 440.0f
  9. #endif
  10. #ifdef AUDIO_CLICKY
  11. #ifdef AUDIO_CLICKY_ON
  12. bool clicky_enable = true;
  13. #else
  14. bool clicky_enable = false;
  15. #endif
  16. #ifndef AUDIO_CLICKY_FREQ_DEFAULT
  17. #define AUDIO_CLICKY_FREQ_DEFAULT 440.0f
  18. #endif
  19. #ifndef AUDIO_CLICKY_FREQ_MIN
  20. #define AUDIO_CLICKY_FREQ_MIN 65.0f
  21. #endif
  22. #ifndef AUDIO_CLICKY_FREQ_MAX
  23. #define AUDIO_CLICKY_FREQ_MAX 1500.0f
  24. #endif
  25. #ifndef AUDIO_CLICKY_FREQ_FACTOR
  26. #define AUDIO_CLICKY_FREQ_FACTOR 1.18921f
  27. #endif
  28. #ifndef AUDIO_CLICKY_FREQ_RANDOMNESS
  29. #define AUDIO_CLICKY_FREQ_RANDOMNESS 0.05f
  30. #endif
  31. float clicky_freq = AUDIO_CLICKY_FREQ_DEFAULT;
  32. float clicky_song[][2] = {{AUDIO_CLICKY_FREQ_DEFAULT, 3}, {AUDIO_CLICKY_FREQ_DEFAULT, 1}}; // 3 and 1 --> durations
  33. #ifndef NO_MUSIC_MODE
  34. extern bool music_activated;
  35. extern bool midi_activated;
  36. #endif
  37. void clicky_play(void) {
  38. #ifndef NO_MUSIC_MODE
  39. if (music_activated || midi_activated) return;
  40. #endif
  41. clicky_song[0][0] = 2.0f * clicky_freq * (1.0f + AUDIO_CLICKY_FREQ_RANDOMNESS * ( ((float)rand()) / ((float)(RAND_MAX)) ) );
  42. clicky_song[1][0] = clicky_freq * (1.0f + AUDIO_CLICKY_FREQ_RANDOMNESS * ( ((float)rand()) / ((float)(RAND_MAX)) ) );
  43. PLAY_SONG(clicky_song);
  44. }
  45. #endif
  46. static float compute_freq_for_midi_note(uint8_t note)
  47. {
  48. // https://en.wikipedia.org/wiki/MIDI_tuning_standard
  49. return pow(2.0, (note - 69) / 12.0) * PITCH_STANDARD_A;
  50. }
  51. bool process_audio(uint16_t keycode, keyrecord_t *record) {
  52. if (keycode == AU_ON && record->event.pressed) {
  53. audio_on();
  54. return false;
  55. }
  56. if (keycode == AU_OFF && record->event.pressed) {
  57. audio_off();
  58. return false;
  59. }
  60. if (keycode == AU_TOG && record->event.pressed) {
  61. if (is_audio_on()) {
  62. audio_off();
  63. } else {
  64. audio_on();
  65. }
  66. return false;
  67. }
  68. if (keycode == MUV_IN && record->event.pressed) {
  69. voice_iterate();
  70. PLAY_SONG(voice_change_song);
  71. return false;
  72. }
  73. if (keycode == MUV_DE && record->event.pressed) {
  74. voice_deiterate();
  75. PLAY_SONG(voice_change_song);
  76. return false;
  77. }
  78. #ifdef AUDIO_CLICKY
  79. if (keycode == CLICKY_TOGGLE && record->event.pressed) { clicky_enable = !clicky_enable; }
  80. if (keycode == CLICKY_RESET && record->event.pressed) { clicky_freq = AUDIO_CLICKY_FREQ_DEFAULT; }
  81. if (keycode == CLICKY_UP && record->event.pressed) {
  82. float new_freq = clicky_freq * AUDIO_CLICKY_FREQ_FACTOR;
  83. if (new_freq < AUDIO_CLICKY_FREQ_MAX) {
  84. clicky_freq = new_freq;
  85. }
  86. }
  87. if (keycode == CLICKY_TOGGLE && record->event.pressed) {
  88. float new_freq = clicky_freq / AUDIO_CLICKY_FREQ_FACTOR;
  89. if (new_freq > AUDIO_CLICKY_FREQ_MIN) {
  90. clicky_freq = new_freq;
  91. }
  92. }
  93. if ( (clicky_enable && keycode != CLICKY_TOGGLE) || (!clicky_enable && keycode == CLICKY_TOGGLE) ) {
  94. if (record->event.pressed) {
  95. stop_all_notes();
  96. clicky_play();;
  97. }
  98. }
  99. #endif // AUDIO_CLICKY
  100. return true;
  101. }
  102. void process_audio_noteon(uint8_t note) {
  103. play_note(compute_freq_for_midi_note(note), 0xF);
  104. }
  105. void process_audio_noteoff(uint8_t note) {
  106. stop_note(compute_freq_for_midi_note(note));
  107. }
  108. void process_audio_all_notes_off(void) {
  109. stop_all_notes();
  110. }
  111. __attribute__ ((weak))
  112. void audio_on_user() {}