diff options
Diffstat (limited to 'quantum/process_keycode/process_music.c')
-rw-r--r-- | quantum/process_keycode/process_music.c | 354 |
1 files changed, 164 insertions, 190 deletions
diff --git a/quantum/process_keycode/process_music.c b/quantum/process_keycode/process_music.c index 697aa237f..b61a16e87 100644 --- a/quantum/process_keycode/process_music.c +++ b/quantum/process_keycode/process_music.c | |||
@@ -16,103 +16,91 @@ | |||
16 | #include "process_music.h" | 16 | #include "process_music.h" |
17 | 17 | ||
18 | #ifdef AUDIO_ENABLE | 18 | #ifdef AUDIO_ENABLE |
19 | #include "process_audio.h" | 19 | # include "process_audio.h" |
20 | #endif | 20 | #endif |
21 | #if defined(MIDI_ENABLE) && defined(MIDI_BASIC) | 21 | #if defined(MIDI_ENABLE) && defined(MIDI_BASIC) |
22 | #include "process_midi.h" | 22 | # include "process_midi.h" |
23 | #endif | 23 | #endif |
24 | 24 | ||
25 | #if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC)) | 25 | #if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC)) |
26 | 26 | ||
27 | bool music_activated = false; | 27 | bool music_activated = false; |
28 | bool midi_activated = false; | 28 | bool midi_activated = false; |
29 | uint8_t music_starting_note = 0x0C; | 29 | uint8_t music_starting_note = 0x0C; |
30 | int music_offset = 7; | 30 | int music_offset = 7; |
31 | uint8_t music_mode = MUSIC_MODE_MAJOR; | 31 | uint8_t music_mode = MUSIC_MODE_MAJOR; |
32 | 32 | ||
33 | // music sequencer | 33 | // music sequencer |
34 | static bool music_sequence_recording = false; | 34 | static bool music_sequence_recording = false; |
35 | static bool music_sequence_recorded = false; | 35 | static bool music_sequence_recorded = false; |
36 | static bool music_sequence_playing = false; | 36 | static bool music_sequence_playing = false; |
37 | static uint8_t music_sequence[16] = {0}; | 37 | static uint8_t music_sequence[16] = {0}; |
38 | static uint8_t music_sequence_count = 0; | 38 | static uint8_t music_sequence_count = 0; |
39 | static uint8_t music_sequence_position = 0; | 39 | static uint8_t music_sequence_position = 0; |
40 | 40 | ||
41 | static uint16_t music_sequence_timer = 0; | 41 | static uint16_t music_sequence_timer = 0; |
42 | static uint16_t music_sequence_interval = 100; | 42 | static uint16_t music_sequence_interval = 100; |
43 | 43 | ||
44 | #ifdef AUDIO_ENABLE | 44 | # ifdef AUDIO_ENABLE |
45 | #ifndef MUSIC_ON_SONG | 45 | # ifndef MUSIC_ON_SONG |
46 | #define MUSIC_ON_SONG SONG(MUSIC_ON_SOUND) | 46 | # define MUSIC_ON_SONG SONG(MUSIC_ON_SOUND) |
47 | #endif | 47 | # endif |
48 | #ifndef MUSIC_OFF_SONG | 48 | # ifndef MUSIC_OFF_SONG |
49 | #define MUSIC_OFF_SONG SONG(MUSIC_OFF_SOUND) | 49 | # define MUSIC_OFF_SONG SONG(MUSIC_OFF_SOUND) |
50 | #endif | 50 | # endif |
51 | #ifndef MIDI_ON_SONG | 51 | # ifndef MIDI_ON_SONG |
52 | #define MIDI_ON_SONG SONG(MUSIC_ON_SOUND) | 52 | # define MIDI_ON_SONG SONG(MUSIC_ON_SOUND) |
53 | #endif | 53 | # endif |
54 | #ifndef MIDI_OFF_SONG | 54 | # ifndef MIDI_OFF_SONG |
55 | #define MIDI_OFF_SONG SONG(MUSIC_OFF_SOUND) | 55 | # define MIDI_OFF_SONG SONG(MUSIC_OFF_SOUND) |
56 | #endif | 56 | # endif |
57 | #ifndef CHROMATIC_SONG | 57 | # ifndef CHROMATIC_SONG |
58 | #define CHROMATIC_SONG SONG(CHROMATIC_SOUND) | 58 | # define CHROMATIC_SONG SONG(CHROMATIC_SOUND) |
59 | #endif | 59 | # endif |
60 | #ifndef GUITAR_SONG | 60 | # ifndef GUITAR_SONG |
61 | #define GUITAR_SONG SONG(GUITAR_SOUND) | 61 | # define GUITAR_SONG SONG(GUITAR_SOUND) |
62 | #endif | 62 | # endif |
63 | #ifndef VIOLIN_SONG | 63 | # ifndef VIOLIN_SONG |
64 | #define VIOLIN_SONG SONG(VIOLIN_SOUND) | 64 | # define VIOLIN_SONG SONG(VIOLIN_SOUND) |
65 | #endif | 65 | # endif |
66 | #ifndef MAJOR_SONG | 66 | # ifndef MAJOR_SONG |
67 | #define MAJOR_SONG SONG(MAJOR_SOUND) | 67 | # define MAJOR_SONG SONG(MAJOR_SOUND) |
68 | #endif | 68 | # endif |
69 | float music_mode_songs[NUMBER_OF_MODES][5][2] = { | 69 | float music_mode_songs[NUMBER_OF_MODES][5][2] = {CHROMATIC_SONG, GUITAR_SONG, VIOLIN_SONG, MAJOR_SONG}; |
70 | CHROMATIC_SONG, | 70 | float music_on_song[][2] = MUSIC_ON_SONG; |
71 | GUITAR_SONG, | 71 | float music_off_song[][2] = MUSIC_OFF_SONG; |
72 | VIOLIN_SONG, | 72 | float midi_on_song[][2] = MIDI_ON_SONG; |
73 | MAJOR_SONG | 73 | float midi_off_song[][2] = MIDI_OFF_SONG; |
74 | }; | 74 | # endif |
75 | float music_on_song[][2] = MUSIC_ON_SONG; | ||
76 | float music_off_song[][2] = MUSIC_OFF_SONG; | ||
77 | float midi_on_song[][2] = MIDI_ON_SONG; | ||
78 | float midi_off_song[][2] = MIDI_OFF_SONG; | ||
79 | #endif | ||
80 | 75 | ||
81 | static void music_noteon(uint8_t note) { | 76 | static void music_noteon(uint8_t note) { |
82 | #ifdef AUDIO_ENABLE | 77 | # ifdef AUDIO_ENABLE |
83 | if (music_activated) | 78 | if (music_activated) process_audio_noteon(note); |
84 | process_audio_noteon(note); | 79 | # endif |
85 | #endif | 80 | # if defined(MIDI_ENABLE) && defined(MIDI_BASIC) |
86 | #if defined(MIDI_ENABLE) && defined(MIDI_BASIC) | 81 | if (midi_activated) process_midi_basic_noteon(note); |
87 | if (midi_activated) | 82 | # endif |
88 | process_midi_basic_noteon(note); | ||
89 | #endif | ||
90 | } | 83 | } |
91 | 84 | ||
92 | static void music_noteoff(uint8_t note) { | 85 | static void music_noteoff(uint8_t note) { |
93 | #ifdef AUDIO_ENABLE | 86 | # ifdef AUDIO_ENABLE |
94 | if (music_activated) | 87 | if (music_activated) process_audio_noteoff(note); |
95 | process_audio_noteoff(note); | 88 | # endif |
96 | #endif | 89 | # if defined(MIDI_ENABLE) && defined(MIDI_BASIC) |
97 | #if defined(MIDI_ENABLE) && defined(MIDI_BASIC) | 90 | if (midi_activated) process_midi_basic_noteoff(note); |
98 | if (midi_activated) | 91 | # endif |
99 | process_midi_basic_noteoff(note); | ||
100 | #endif | ||
101 | } | 92 | } |
102 | 93 | ||
103 | void music_all_notes_off(void) { | 94 | void music_all_notes_off(void) { |
104 | #ifdef AUDIO_ENABLE | 95 | # ifdef AUDIO_ENABLE |
105 | if (music_activated) | 96 | if (music_activated) process_audio_all_notes_off(); |
106 | process_audio_all_notes_off(); | 97 | # endif |
107 | #endif | 98 | # if defined(MIDI_ENABLE) && defined(MIDI_BASIC) |
108 | #if defined(MIDI_ENABLE) && defined(MIDI_BASIC) | 99 | if (midi_activated) process_midi_all_notes_off(); |
109 | if (midi_activated) | 100 | # endif |
110 | process_midi_all_notes_off(); | ||
111 | #endif | ||
112 | } | 101 | } |
113 | 102 | ||
114 | bool process_music(uint16_t keycode, keyrecord_t *record) { | 103 | bool process_music(uint16_t keycode, keyrecord_t *record) { |
115 | |||
116 | if (keycode == MU_ON && record->event.pressed) { | 104 | if (keycode == MU_ON && record->event.pressed) { |
117 | music_on(); | 105 | music_on(); |
118 | return false; | 106 | return false; |
@@ -152,110 +140,101 @@ bool process_music(uint16_t keycode, keyrecord_t *record) { | |||
152 | } | 140 | } |
153 | 141 | ||
154 | if (keycode == MU_MOD && record->event.pressed) { | 142 | if (keycode == MU_MOD && record->event.pressed) { |
155 | music_mode_cycle(); | 143 | music_mode_cycle(); |
156 | return false; | 144 | return false; |
157 | } | 145 | } |
158 | 146 | ||
159 | if (music_activated || midi_activated) { | 147 | if (music_activated || midi_activated) { |
160 | if (record->event.pressed) { | 148 | if (record->event.pressed) { |
161 | if (keycode == KC_LCTL) { // Start recording | 149 | if (keycode == KC_LCTL) { // Start recording |
162 | music_all_notes_off(); | 150 | music_all_notes_off(); |
163 | music_sequence_recording = true; | 151 | music_sequence_recording = true; |
164 | music_sequence_recorded = false; | 152 | music_sequence_recorded = false; |
165 | music_sequence_playing = false; | 153 | music_sequence_playing = false; |
166 | music_sequence_count = 0; | 154 | music_sequence_count = 0; |
167 | return false; | 155 | return false; |
168 | } | 156 | } |
169 | 157 | ||
170 | if (keycode == KC_LALT) { // Stop recording/playing | 158 | if (keycode == KC_LALT) { // Stop recording/playing |
171 | music_all_notes_off(); | 159 | music_all_notes_off(); |
172 | if (music_sequence_recording) { // was recording | 160 | if (music_sequence_recording) { // was recording |
173 | music_sequence_recorded = true; | 161 | music_sequence_recorded = true; |
174 | } | 162 | } |
175 | music_sequence_recording = false; | 163 | music_sequence_recording = false; |
176 | music_sequence_playing = false; | 164 | music_sequence_playing = false; |
177 | return false; | 165 | return false; |
178 | } | 166 | } |
179 | 167 | ||
180 | if (keycode == KC_LGUI && music_sequence_recorded) { // Start playing | 168 | if (keycode == KC_LGUI && music_sequence_recorded) { // Start playing |
181 | music_all_notes_off(); | 169 | music_all_notes_off(); |
182 | music_sequence_recording = false; | 170 | music_sequence_recording = false; |
183 | music_sequence_playing = true; | 171 | music_sequence_playing = true; |
184 | music_sequence_position = 0; | 172 | music_sequence_position = 0; |
185 | music_sequence_timer = 0; | 173 | music_sequence_timer = 0; |
186 | return false; | 174 | return false; |
187 | } | 175 | } |
188 | 176 | ||
189 | if (keycode == KC_UP) { | 177 | if (keycode == KC_UP) { |
190 | music_sequence_interval-=10; | 178 | music_sequence_interval -= 10; |
191 | return false; | 179 | return false; |
180 | } | ||
181 | |||
182 | if (keycode == KC_DOWN) { | ||
183 | music_sequence_interval += 10; | ||
184 | return false; | ||
185 | } | ||
192 | } | 186 | } |
193 | 187 | ||
194 | if (keycode == KC_DOWN) { | 188 | uint8_t note = 36; |
195 | music_sequence_interval+=10; | 189 | # ifdef MUSIC_MAP |
196 | return false; | ||
197 | } | ||
198 | } | ||
199 | |||
200 | uint8_t note = 36; | ||
201 | #ifdef MUSIC_MAP | ||
202 | if (music_mode == MUSIC_MODE_CHROMATIC) { | 190 | if (music_mode == MUSIC_MODE_CHROMATIC) { |
203 | note = music_starting_note + music_offset + 36 + music_map[record->event.key.row][record->event.key.col]; | 191 | note = music_starting_note + music_offset + 36 + music_map[record->event.key.row][record->event.key.col]; |
204 | } else { | 192 | } else { |
205 | uint8_t position = music_map[record->event.key.row][record->event.key.col]; | 193 | uint8_t position = music_map[record->event.key.row][record->event.key.col]; |
206 | note = music_starting_note + music_offset + 36 + SCALE[position % 12] + (position / 12)*12; | 194 | note = music_starting_note + music_offset + 36 + SCALE[position % 12] + (position / 12) * 12; |
207 | } | 195 | } |
208 | #else | 196 | # else |
209 | if (music_mode == MUSIC_MODE_CHROMATIC) | 197 | if (music_mode == MUSIC_MODE_CHROMATIC) |
210 | note = (music_starting_note + record->event.key.col + music_offset - 3)+12*(MATRIX_ROWS - record->event.key.row); | 198 | note = (music_starting_note + record->event.key.col + music_offset - 3) + 12 * (MATRIX_ROWS - record->event.key.row); |
211 | else if (music_mode == MUSIC_MODE_GUITAR) | 199 | else if (music_mode == MUSIC_MODE_GUITAR) |
212 | note = (music_starting_note + record->event.key.col + music_offset + 32)+5*(MATRIX_ROWS - record->event.key.row); | 200 | note = (music_starting_note + record->event.key.col + music_offset + 32) + 5 * (MATRIX_ROWS - record->event.key.row); |
213 | else if (music_mode == MUSIC_MODE_VIOLIN) | 201 | else if (music_mode == MUSIC_MODE_VIOLIN) |
214 | note = (music_starting_note + record->event.key.col + music_offset + 32)+7*(MATRIX_ROWS - record->event.key.row); | 202 | note = (music_starting_note + record->event.key.col + music_offset + 32) + 7 * (MATRIX_ROWS - record->event.key.row); |
215 | else if (music_mode == MUSIC_MODE_MAJOR) | 203 | else if (music_mode == MUSIC_MODE_MAJOR) |
216 | note = (music_starting_note + SCALE[record->event.key.col + music_offset] - 3)+12*(MATRIX_ROWS - record->event.key.row); | 204 | note = (music_starting_note + SCALE[record->event.key.col + music_offset] - 3) + 12 * (MATRIX_ROWS - record->event.key.row); |
217 | else | 205 | else |
218 | note = music_starting_note; | 206 | note = music_starting_note; |
219 | #endif | 207 | # endif |
220 | 208 | ||
221 | if (record->event.pressed) { | 209 | if (record->event.pressed) { |
222 | music_noteon(note); | 210 | music_noteon(note); |
223 | if (music_sequence_recording) { | 211 | if (music_sequence_recording) { |
224 | music_sequence[music_sequence_count] = note; | 212 | music_sequence[music_sequence_count] = note; |
225 | music_sequence_count++; | 213 | music_sequence_count++; |
214 | } | ||
215 | } else { | ||
216 | music_noteoff(note); | ||
226 | } | 217 | } |
227 | } else { | ||
228 | music_noteoff(note); | ||
229 | } | ||
230 | 218 | ||
231 | if (music_mask(keycode)) | 219 | if (music_mask(keycode)) return false; |
232 | return false; | ||
233 | } | 220 | } |
234 | 221 | ||
235 | return true; | 222 | return true; |
236 | } | 223 | } |
237 | 224 | ||
238 | bool music_mask(uint16_t keycode) { | 225 | bool music_mask(uint16_t keycode) { |
239 | #ifdef MUSIC_MASK | 226 | # ifdef MUSIC_MASK |
240 | return MUSIC_MASK; | 227 | return MUSIC_MASK; |
241 | #else | 228 | # else |
242 | return music_mask_kb(keycode); | 229 | return music_mask_kb(keycode); |
243 | #endif | 230 | # endif |
244 | } | 231 | } |
245 | 232 | ||
246 | __attribute__((weak)) | 233 | __attribute__((weak)) bool music_mask_kb(uint16_t keycode) { return music_mask_user(keycode); } |
247 | bool music_mask_kb(uint16_t keycode) { | ||
248 | return music_mask_user(keycode); | ||
249 | } | ||
250 | 234 | ||
251 | __attribute__((weak)) | 235 | __attribute__((weak)) bool music_mask_user(uint16_t keycode) { return keycode < 0xFF; } |
252 | bool music_mask_user(uint16_t keycode) { | ||
253 | return keycode < 0xFF; | ||
254 | } | ||
255 | 236 | ||
256 | bool is_music_on(void) { | 237 | bool is_music_on(void) { return (music_activated != 0); } |
257 | return (music_activated != 0); | ||
258 | } | ||
259 | 238 | ||
260 | void music_toggle(void) { | 239 | void music_toggle(void) { |
261 | if (!music_activated) { | 240 | if (!music_activated) { |
@@ -267,23 +246,21 @@ void music_toggle(void) { | |||
267 | 246 | ||
268 | void music_on(void) { | 247 | void music_on(void) { |
269 | music_activated = 1; | 248 | music_activated = 1; |
270 | #ifdef AUDIO_ENABLE | 249 | # ifdef AUDIO_ENABLE |
271 | PLAY_SONG(music_on_song); | 250 | PLAY_SONG(music_on_song); |
272 | #endif | 251 | # endif |
273 | music_on_user(); | 252 | music_on_user(); |
274 | } | 253 | } |
275 | 254 | ||
276 | void music_off(void) { | 255 | void music_off(void) { |
277 | music_all_notes_off(); | 256 | music_all_notes_off(); |
278 | music_activated = 0; | 257 | music_activated = 0; |
279 | #ifdef AUDIO_ENABLE | 258 | # ifdef AUDIO_ENABLE |
280 | PLAY_SONG(music_off_song); | 259 | PLAY_SONG(music_off_song); |
281 | #endif | 260 | # endif |
282 | } | 261 | } |
283 | 262 | ||
284 | bool is_midi_on(void) { | 263 | bool is_midi_on(void) { return (midi_activated != 0); } |
285 | return (midi_activated != 0); | ||
286 | } | ||
287 | 264 | ||
288 | void midi_toggle(void) { | 265 | void midi_toggle(void) { |
289 | if (!midi_activated) { | 266 | if (!midi_activated) { |
@@ -295,50 +272,47 @@ void midi_toggle(void) { | |||
295 | 272 | ||
296 | void midi_on(void) { | 273 | void midi_on(void) { |
297 | midi_activated = 1; | 274 | midi_activated = 1; |
298 | #ifdef AUDIO_ENABLE | 275 | # ifdef AUDIO_ENABLE |
299 | PLAY_SONG(midi_on_song); | 276 | PLAY_SONG(midi_on_song); |
300 | #endif | 277 | # endif |
301 | midi_on_user(); | 278 | midi_on_user(); |
302 | } | 279 | } |
303 | 280 | ||
304 | void midi_off(void) { | 281 | void midi_off(void) { |
305 | #if defined(MIDI_ENABLE) && defined(MIDI_BASIC) | 282 | # if defined(MIDI_ENABLE) && defined(MIDI_BASIC) |
306 | process_midi_all_notes_off(); | 283 | process_midi_all_notes_off(); |
307 | #endif | 284 | # endif |
308 | midi_activated = 0; | 285 | midi_activated = 0; |
309 | #ifdef AUDIO_ENABLE | 286 | # ifdef AUDIO_ENABLE |
310 | PLAY_SONG(midi_off_song); | 287 | PLAY_SONG(midi_off_song); |
311 | #endif | 288 | # endif |
312 | } | 289 | } |
313 | 290 | ||
314 | void music_mode_cycle(void) { | 291 | void music_mode_cycle(void) { |
315 | music_all_notes_off(); | 292 | music_all_notes_off(); |
316 | music_mode = (music_mode + 1) % NUMBER_OF_MODES; | 293 | music_mode = (music_mode + 1) % NUMBER_OF_MODES; |
317 | #ifdef AUDIO_ENABLE | 294 | # ifdef AUDIO_ENABLE |
318 | PLAY_SONG(music_mode_songs[music_mode]); | 295 | PLAY_SONG(music_mode_songs[music_mode]); |
319 | #endif | 296 | # endif |
320 | } | 297 | } |
321 | 298 | ||
322 | void matrix_scan_music(void) { | 299 | void matrix_scan_music(void) { |
323 | if (music_sequence_playing) { | 300 | if (music_sequence_playing) { |
324 | if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) { | 301 | if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) { |
325 | music_sequence_timer = timer_read(); | 302 | music_sequence_timer = timer_read(); |
326 | uint8_t prev_note = music_sequence[(music_sequence_position - 1 < 0)?(music_sequence_position - 1 + music_sequence_count):(music_sequence_position - 1)]; | 303 | uint8_t prev_note = music_sequence[(music_sequence_position - 1 < 0) ? (music_sequence_position - 1 + music_sequence_count) : (music_sequence_position - 1)]; |
327 | uint8_t next_note = music_sequence[music_sequence_position]; | 304 | uint8_t next_note = music_sequence[music_sequence_position]; |
328 | music_noteoff(prev_note); | 305 | music_noteoff(prev_note); |
329 | music_noteon(next_note); | 306 | music_noteon(next_note); |
330 | music_sequence_position = (music_sequence_position + 1) % music_sequence_count; | 307 | music_sequence_position = (music_sequence_position + 1) % music_sequence_count; |
308 | } | ||
331 | } | 309 | } |
332 | } | ||
333 | } | 310 | } |
334 | 311 | ||
335 | __attribute__ ((weak)) | 312 | __attribute__((weak)) void music_on_user() {} |
336 | void music_on_user() {} | ||
337 | 313 | ||
338 | __attribute__ ((weak)) | 314 | __attribute__((weak)) void midi_on_user() {} |
339 | void midi_on_user() {} | ||
340 | 315 | ||
341 | __attribute__ ((weak)) | 316 | __attribute__((weak)) void music_scale_user() {} |
342 | void music_scale_user() {} | ||
343 | 317 | ||
344 | #endif // defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC)) | 318 | #endif // defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC)) |