Parent directory

process_audio.c

1524 bytes
 1#include "audio.h"
 2#include "process_audio.h"
 3#include <math.h>
 4
 5#ifndef VOICE_CHANGE_SONG
 6#    define VOICE_CHANGE_SONG SONG(VOICE_CHANGE_SOUND)
 7#endif
 8float voice_change_song[][2] = VOICE_CHANGE_SONG;
 9
10#ifndef PITCH_STANDARD_A
11#    define PITCH_STANDARD_A 440.0f
12#endif
13
14float compute_freq_for_midi_note(uint8_t note) {
15    // https://en.wikipedia.org/wiki/MIDI_tuning_standard
16    return powf(2.0f, (note - 69) / 12.0f) * PITCH_STANDARD_A;
17}
18
19bool process_audio(uint16_t keycode, keyrecord_t *record) {
20    if (keycode == QK_AUDIO_ON && record->event.pressed) {
21        audio_on();
22        return false;
23    }
24
25    if (keycode == QK_AUDIO_OFF && record->event.pressed) {
26        audio_off();
27        return false;
28    }
29
30    if (keycode == QK_AUDIO_TOGGLE && record->event.pressed) {
31        if (is_audio_on()) {
32            audio_off();
33        } else {
34            audio_on();
35        }
36        return false;
37    }
38
39    if (keycode == QK_AUDIO_VOICE_NEXT && record->event.pressed) {
40        voice_iterate();
41        PLAY_SONG(voice_change_song);
42        return false;
43    }
44
45    if (keycode == QK_AUDIO_VOICE_PREVIOUS && record->event.pressed) {
46        voice_deiterate();
47        PLAY_SONG(voice_change_song);
48        return false;
49    }
50
51    return true;
52}
53
54void process_audio_noteon(uint8_t note) {
55    play_note(compute_freq_for_midi_note(note), 0xF);
56}
57
58void process_audio_noteoff(uint8_t note) {
59    stop_note(compute_freq_for_midi_note(note));
60}
61
62void process_audio_all_notes_off(void) {
63    stop_all_notes();
64}