process_audio.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. float compute_freq_for_midi_note(uint8_t note) {
  11. // https://en.wikipedia.org/wiki/MIDI_tuning_standard
  12. return pow(2.0, (note - 69) / 12.0) * PITCH_STANDARD_A;
  13. }
  14. bool process_audio(uint16_t keycode, keyrecord_t *record) {
  15. if (keycode == QK_AUDIO_ON && record->event.pressed) {
  16. audio_on();
  17. return false;
  18. }
  19. if (keycode == QK_AUDIO_OFF && record->event.pressed) {
  20. audio_off();
  21. return false;
  22. }
  23. if (keycode == QK_AUDIO_TOGGLE && record->event.pressed) {
  24. if (is_audio_on()) {
  25. audio_off();
  26. } else {
  27. audio_on();
  28. }
  29. return false;
  30. }
  31. if (keycode == QK_AUDIO_VOICE_NEXT && record->event.pressed) {
  32. voice_iterate();
  33. PLAY_SONG(voice_change_song);
  34. return false;
  35. }
  36. if (keycode == QK_AUDIO_VOICE_PREVIOUS && record->event.pressed) {
  37. voice_deiterate();
  38. PLAY_SONG(voice_change_song);
  39. return false;
  40. }
  41. return true;
  42. }
  43. void process_audio_noteon(uint8_t note) {
  44. play_note(compute_freq_for_midi_note(note), 0xF);
  45. }
  46. void process_audio_noteoff(uint8_t note) {
  47. stop_note(compute_freq_for_midi_note(note));
  48. }
  49. void process_audio_all_notes_off(void) {
  50. stop_all_notes();
  51. }
  52. __attribute__((weak)) void audio_on_user() {}
  53. __attribute__((weak)) void audio_off_user() {}