diff options
Diffstat (limited to 'quantum/process_keycode/process_music.c')
-rw-r--r-- | quantum/process_keycode/process_music.c | 137 |
1 files changed, 59 insertions, 78 deletions
diff --git a/quantum/process_keycode/process_music.c b/quantum/process_keycode/process_music.c index ac906b628..a1e270df1 100644 --- a/quantum/process_keycode/process_music.c +++ b/quantum/process_keycode/process_music.c | |||
@@ -1,5 +1,14 @@ | |||
1 | #include "process_music.h" | 1 | #include "process_music.h" |
2 | 2 | ||
3 | #ifdef AUDIO_ENABLE | ||
4 | #include "process_audio.h" | ||
5 | #endif | ||
6 | #if defined(MIDI_ENABLE) && defined(MIDI_BASIC) | ||
7 | #include "process_midi.h" | ||
8 | #endif | ||
9 | |||
10 | #if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC)) | ||
11 | |||
3 | bool music_activated = false; | 12 | bool music_activated = false; |
4 | uint8_t music_starting_note = 0x0C; | 13 | uint8_t music_starting_note = 0x0C; |
5 | int music_offset = 7; | 14 | int music_offset = 7; |
@@ -8,38 +17,41 @@ int music_offset = 7; | |||
8 | static bool music_sequence_recording = false; | 17 | static bool music_sequence_recording = false; |
9 | static bool music_sequence_recorded = false; | 18 | static bool music_sequence_recorded = false; |
10 | static bool music_sequence_playing = false; | 19 | static bool music_sequence_playing = false; |
11 | static float music_sequence[16] = {0}; | 20 | static uint8_t music_sequence[16] = {0}; |
12 | static uint8_t music_sequence_count = 0; | 21 | static uint8_t music_sequence_count = 0; |
13 | static uint8_t music_sequence_position = 0; | 22 | static uint8_t music_sequence_position = 0; |
14 | 23 | ||
15 | static uint16_t music_sequence_timer = 0; | 24 | static uint16_t music_sequence_timer = 0; |
16 | static uint16_t music_sequence_interval = 100; | 25 | static uint16_t music_sequence_interval = 100; |
17 | 26 | ||
18 | bool process_music(uint16_t keycode, keyrecord_t *record) { | 27 | static void music_noteon(uint8_t note) { |
28 | #ifdef AUDIO_ENABLE | ||
29 | process_audio_noteon(note); | ||
30 | #endif | ||
31 | #if defined(MIDI_ENABLE) && defined(MIDI_BASIC) | ||
32 | process_midi_basic_noteon(note); | ||
33 | #endif | ||
34 | } | ||
19 | 35 | ||
20 | #ifdef AUDIO_ENABLE | 36 | static void music_noteoff(uint8_t note) { |
21 | if (keycode == AU_ON && record->event.pressed) { | 37 | #ifdef AUDIO_ENABLE |
22 | audio_on(); | 38 | process_audio_noteoff(note); |
23 | return false; | 39 | #endif |
24 | } | 40 | #if defined(MIDI_ENABLE) && defined(MIDI_BASIC) |
41 | process_midi_basic_noteoff(note); | ||
42 | #endif | ||
43 | } | ||
25 | 44 | ||
26 | if (keycode == AU_OFF && record->event.pressed) { | 45 | static void music_all_notes_off(void) { |
27 | audio_off(); | 46 | #ifdef AUDIO_ENABLE |
28 | return false; | 47 | process_audio_stop_all_notes(); |
29 | } | 48 | #endif |
49 | #if defined(MIDI_ENABLE) && defined(MIDI_BASIC) | ||
50 | process_midi_basic_stop_all_notes(); | ||
51 | #endif | ||
52 | } | ||
30 | 53 | ||
31 | if (keycode == AU_TOG && record->event.pressed) { | 54 | bool process_music(uint16_t keycode, keyrecord_t *record) { |
32 | if (is_audio_on()) | ||
33 | { | ||
34 | audio_off(); | ||
35 | } | ||
36 | else | ||
37 | { | ||
38 | audio_on(); | ||
39 | } | ||
40 | return false; | ||
41 | } | ||
42 | #endif // AUDIO_ENABLE | ||
43 | 55 | ||
44 | if (keycode == MU_ON && record->event.pressed) { | 56 | if (keycode == MU_ON && record->event.pressed) { |
45 | music_on(); | 57 | music_on(); |
@@ -63,26 +75,10 @@ bool process_music(uint16_t keycode, keyrecord_t *record) { | |||
63 | return false; | 75 | return false; |
64 | } | 76 | } |
65 | 77 | ||
66 | #ifdef AUDIO_ENABLE | ||
67 | if (keycode == MUV_IN && record->event.pressed) { | ||
68 | voice_iterate(); | ||
69 | music_scale_user(); | ||
70 | return false; | ||
71 | } | ||
72 | |||
73 | if (keycode == MUV_DE && record->event.pressed) { | ||
74 | voice_deiterate(); | ||
75 | music_scale_user(); | ||
76 | return false; | ||
77 | } | ||
78 | #endif // AUDIO_ENABLE | ||
79 | |||
80 | if (music_activated) { | 78 | if (music_activated) { |
81 | 79 | ||
82 | if (keycode == KC_LCTL && record->event.pressed) { // Start recording | 80 | if (keycode == KC_LCTL && record->event.pressed) { // Start recording |
83 | #ifdef AUDIO_ENABLE | 81 | music_all_notes_off(); |
84 | stop_all_notes(); | ||
85 | #endif | ||
86 | music_sequence_recording = true; | 82 | music_sequence_recording = true; |
87 | music_sequence_recorded = false; | 83 | music_sequence_recorded = false; |
88 | music_sequence_playing = false; | 84 | music_sequence_playing = false; |
@@ -91,9 +87,7 @@ bool process_music(uint16_t keycode, keyrecord_t *record) { | |||
91 | } | 87 | } |
92 | 88 | ||
93 | if (keycode == KC_LALT && record->event.pressed) { // Stop recording/playing | 89 | if (keycode == KC_LALT && record->event.pressed) { // Stop recording/playing |
94 | #ifdef AUDIO_ENABLE | 90 | music_all_notes_off(); |
95 | stop_all_notes(); | ||
96 | #endif | ||
97 | if (music_sequence_recording) { // was recording | 91 | if (music_sequence_recording) { // was recording |
98 | music_sequence_recorded = true; | 92 | music_sequence_recorded = true; |
99 | } | 93 | } |
@@ -103,9 +97,7 @@ bool process_music(uint16_t keycode, keyrecord_t *record) { | |||
103 | } | 97 | } |
104 | 98 | ||
105 | if (keycode == KC_LGUI && record->event.pressed && music_sequence_recorded) { // Start playing | 99 | if (keycode == KC_LGUI && record->event.pressed && music_sequence_recorded) { // Start playing |
106 | #ifdef AUDIO_ENABLE | 100 | music_all_notes_off(); |
107 | stop_all_notes(); | ||
108 | #endif | ||
109 | music_sequence_recording = false; | 101 | music_sequence_recording = false; |
110 | music_sequence_playing = true; | 102 | music_sequence_playing = true; |
111 | music_sequence_position = 0; | 103 | music_sequence_position = 0; |
@@ -124,32 +116,27 @@ bool process_music(uint16_t keycode, keyrecord_t *record) { | |||
124 | music_sequence_interval+=10; | 116 | music_sequence_interval+=10; |
125 | return false; | 117 | return false; |
126 | } | 118 | } |
119 | |||
127 | #define MUSIC_MODE_GUITAR | 120 | #define MUSIC_MODE_GUITAR |
128 | 121 | ||
129 | #ifdef AUDIO_ENABLE | ||
130 | #ifdef MUSIC_MODE_CHROMATIC | 122 | #ifdef MUSIC_MODE_CHROMATIC |
131 | float freq = ((float)220.0)*pow(2.0, -5.0)*pow(2.0,(music_starting_note + record->event.key.col + music_offset)/12.0+(MATRIX_ROWS - record->event.key.row)); | 123 | uint8_t note = (music_starting_note + record->event.key.col + music_offset - 3)+12*(MATRIX_ROWS - record->event.key.row); |
132 | #elif defined(MUSIC_MODE_GUITAR) | 124 | #elif defined(MUSIC_MODE_GUITAR) |
133 | float freq = ((float)220.0)*pow(2.0, -5.0)*pow(2.0,(music_starting_note + record->event.key.col + music_offset)/12.0+(float)(MATRIX_ROWS - record->event.key.row + 7)*5.0/12); | 125 | uint8_t note = (music_starting_note + record->event.key.col + music_offset + 32)+5*(MATRIX_ROWS - record->event.key.row); |
134 | #elif defined(MUSIC_MODE_VIOLIN) | 126 | #elif defined(MUSIC_MODE_VIOLIN) |
135 | float freq = ((float)220.0)*pow(2.0, -5.0)*pow(2.0,(music_starting_note + record->event.key.col + music_offset)/12.0+(float)(MATRIX_ROWS - record->event.key.row + 5)*7.0/12); | 127 | uint8_t note = (music_starting_note + record->event.key.col + music_offset + 32)+7*(MATRIX_ROWS - record->event.key.row); |
136 | #else | 128 | #else |
137 | float freq = ((float)220.0)*pow(2.0, -5.0)*pow(2.0,(music_starting_note + SCALE[record->event.key.col + music_offset])/12.0+(MATRIX_ROWS - record->event.key.row)); | 129 | uint8_t note = (music_starting_note + SCALE[record->event.key.col + music_offset] - 3)+12*(MATRIX_ROWS - record->event.key.row); |
138 | #endif | 130 | #endif |
139 | #endif // AUDIO_ENABLE | ||
140 | 131 | ||
141 | if (record->event.pressed) { | 132 | if (record->event.pressed) { |
142 | #ifdef AUDIO_ENABLE | 133 | music_noteon(note); |
143 | play_note(freq, 0xF); | ||
144 | if (music_sequence_recording) { | 134 | if (music_sequence_recording) { |
145 | music_sequence[music_sequence_count] = freq; | 135 | music_sequence[music_sequence_count] = note; |
146 | music_sequence_count++; | 136 | music_sequence_count++; |
147 | } | 137 | } |
148 | #endif | ||
149 | } else { | 138 | } else { |
150 | #ifdef AUDIO_ENABLE | 139 | music_noteoff(note); |
151 | stop_note(freq); | ||
152 | #endif | ||
153 | } | 140 | } |
154 | 141 | ||
155 | if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through | 142 | if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through |
@@ -177,32 +164,26 @@ void music_on(void) { | |||
177 | 164 | ||
178 | void music_off(void) { | 165 | void music_off(void) { |
179 | music_activated = 0; | 166 | music_activated = 0; |
180 | #ifdef AUDIO_ENABLE | 167 | music_all_notes_off(); |
181 | stop_all_notes(); | ||
182 | #endif | ||
183 | } | 168 | } |
184 | 169 | ||
185 | |||
186 | __attribute__ ((weak)) | ||
187 | void music_on_user() {} | ||
188 | |||
189 | #ifdef AUDIO_ENABLE | ||
190 | __attribute__ ((weak)) | ||
191 | void audio_on_user() {} | ||
192 | #endif | ||
193 | |||
194 | __attribute__ ((weak)) | ||
195 | void music_scale_user() {} | ||
196 | |||
197 | void matrix_scan_music(void) { | 170 | void matrix_scan_music(void) { |
198 | if (music_sequence_playing) { | 171 | if (music_sequence_playing) { |
199 | if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) { | 172 | if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) { |
200 | music_sequence_timer = timer_read(); | 173 | music_sequence_timer = timer_read(); |
201 | #ifdef AUDIO_ENABLE | 174 | uint8_t prev_note = music_sequence[(music_sequence_position - 1 < 0)?(music_sequence_position - 1 + music_sequence_count):(music_sequence_position - 1)]; |
202 | stop_note(music_sequence[(music_sequence_position - 1 < 0)?(music_sequence_position - 1 + music_sequence_count):(music_sequence_position - 1)]); | 175 | uint8_t next_note = music_sequence[music_sequence_position]; |
203 | play_note(music_sequence[music_sequence_position], 0xF); | 176 | music_noteoff(prev_note); |
204 | #endif | 177 | music_noteon(next_note); |
205 | music_sequence_position = (music_sequence_position + 1) % music_sequence_count; | 178 | music_sequence_position = (music_sequence_position + 1) % music_sequence_count; |
206 | } | 179 | } |
207 | } | 180 | } |
208 | } | 181 | } |
182 | |||
183 | __attribute__ ((weak)) | ||
184 | void music_on_user() {} | ||
185 | |||
186 | __attribute__ ((weak)) | ||
187 | void music_scale_user() {} | ||
188 | |||
189 | #endif // defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC)) \ No newline at end of file | ||