]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/process_keycode/process_music.c
[Keyboard] Clueboard 60 fix col 11 12 mixup (#7685)
[qmk_firmware.git] / quantum / process_keycode / process_music.c
1 /* Copyright 2016 Jack Humbert
2  *
3  * This program is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16 #include "process_music.h"
17
18 #ifdef AUDIO_ENABLE
19 #    include "process_audio.h"
20 #endif
21 #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
22 #    include "process_midi.h"
23 #endif
24
25 #if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
26
27 bool    music_activated     = false;
28 bool    midi_activated      = false;
29 uint8_t music_starting_note = 0x0C;
30 int     music_offset        = 7;
31 uint8_t music_mode          = MUSIC_MODE_MAJOR;
32
33 // music sequencer
34 static bool    music_sequence_recording = false;
35 static bool    music_sequence_recorded  = false;
36 static bool    music_sequence_playing   = false;
37 static uint8_t music_sequence[16]       = {0};
38 static uint8_t music_sequence_count     = 0;
39 static uint8_t music_sequence_position  = 0;
40
41 static uint16_t music_sequence_timer    = 0;
42 static uint16_t music_sequence_interval = 100;
43
44 #    ifdef AUDIO_ENABLE
45 #        ifndef MUSIC_ON_SONG
46 #            define MUSIC_ON_SONG SONG(MUSIC_ON_SOUND)
47 #        endif
48 #        ifndef MUSIC_OFF_SONG
49 #            define MUSIC_OFF_SONG SONG(MUSIC_OFF_SOUND)
50 #        endif
51 #        ifndef MIDI_ON_SONG
52 #            define MIDI_ON_SONG SONG(MUSIC_ON_SOUND)
53 #        endif
54 #        ifndef MIDI_OFF_SONG
55 #            define MIDI_OFF_SONG SONG(MUSIC_OFF_SOUND)
56 #        endif
57 #        ifndef CHROMATIC_SONG
58 #            define CHROMATIC_SONG SONG(CHROMATIC_SOUND)
59 #        endif
60 #        ifndef GUITAR_SONG
61 #            define GUITAR_SONG SONG(GUITAR_SOUND)
62 #        endif
63 #        ifndef VIOLIN_SONG
64 #            define VIOLIN_SONG SONG(VIOLIN_SOUND)
65 #        endif
66 #        ifndef MAJOR_SONG
67 #            define MAJOR_SONG SONG(MAJOR_SOUND)
68 #        endif
69 float music_mode_songs[NUMBER_OF_MODES][5][2] = {CHROMATIC_SONG, GUITAR_SONG, VIOLIN_SONG, MAJOR_SONG};
70 float music_on_song[][2]                      = MUSIC_ON_SONG;
71 float music_off_song[][2]                     = MUSIC_OFF_SONG;
72 float midi_on_song[][2]                       = MIDI_ON_SONG;
73 float midi_off_song[][2]                      = MIDI_OFF_SONG;
74 #    endif
75
76 static void music_noteon(uint8_t note) {
77 #    ifdef AUDIO_ENABLE
78     if (music_activated) process_audio_noteon(note);
79 #    endif
80 #    if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
81     if (midi_activated) process_midi_basic_noteon(note);
82 #    endif
83 }
84
85 static void music_noteoff(uint8_t note) {
86 #    ifdef AUDIO_ENABLE
87     if (music_activated) process_audio_noteoff(note);
88 #    endif
89 #    if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
90     if (midi_activated) process_midi_basic_noteoff(note);
91 #    endif
92 }
93
94 void music_all_notes_off(void) {
95 #    ifdef AUDIO_ENABLE
96     if (music_activated) process_audio_all_notes_off();
97 #    endif
98 #    if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
99     if (midi_activated) process_midi_all_notes_off();
100 #    endif
101 }
102
103 bool process_music(uint16_t keycode, keyrecord_t *record) {
104     if (keycode == MU_ON && record->event.pressed) {
105         music_on();
106         return false;
107     }
108
109     if (keycode == MU_OFF && record->event.pressed) {
110         music_off();
111         return false;
112     }
113
114     if (keycode == MU_TOG && record->event.pressed) {
115         if (music_activated) {
116             music_off();
117         } else {
118             music_on();
119         }
120         return false;
121     }
122
123     if (keycode == MI_ON && record->event.pressed) {
124         midi_on();
125         return false;
126     }
127
128     if (keycode == MI_OFF && record->event.pressed) {
129         midi_off();
130         return false;
131     }
132
133     if (keycode == MI_TOG && record->event.pressed) {
134         if (midi_activated) {
135             midi_off();
136         } else {
137             midi_on();
138         }
139         return false;
140     }
141
142     if (keycode == MU_MOD && record->event.pressed) {
143         music_mode_cycle();
144         return false;
145     }
146
147     if (music_activated || midi_activated) {
148         if (record->event.pressed) {
149             if (keycode == KC_LCTL) {  // Start recording
150                 music_all_notes_off();
151                 music_sequence_recording = true;
152                 music_sequence_recorded  = false;
153                 music_sequence_playing   = false;
154                 music_sequence_count     = 0;
155                 return false;
156             }
157
158             if (keycode == KC_LALT) {  // Stop recording/playing
159                 music_all_notes_off();
160                 if (music_sequence_recording) {  // was recording
161                     music_sequence_recorded = true;
162                 }
163                 music_sequence_recording = false;
164                 music_sequence_playing   = false;
165                 return false;
166             }
167
168             if (keycode == KC_LGUI && music_sequence_recorded) {  // Start playing
169                 music_all_notes_off();
170                 music_sequence_recording = false;
171                 music_sequence_playing   = true;
172                 music_sequence_position  = 0;
173                 music_sequence_timer     = 0;
174                 return false;
175             }
176
177             if (keycode == KC_UP) {
178                 music_sequence_interval -= 10;
179                 return false;
180             }
181
182             if (keycode == KC_DOWN) {
183                 music_sequence_interval += 10;
184                 return false;
185             }
186         }
187
188         uint8_t note = 36;
189 #    ifdef MUSIC_MAP
190         if (music_mode == MUSIC_MODE_CHROMATIC) {
191             note = music_starting_note + music_offset + 36 + music_map[record->event.key.row][record->event.key.col];
192         } else {
193             uint8_t position = music_map[record->event.key.row][record->event.key.col];
194             note             = music_starting_note + music_offset + 36 + SCALE[position % 12] + (position / 12) * 12;
195         }
196 #    else
197         if (music_mode == MUSIC_MODE_CHROMATIC)
198             note = (music_starting_note + record->event.key.col + music_offset - 3) + 12 * (MATRIX_ROWS - record->event.key.row);
199         else if (music_mode == MUSIC_MODE_GUITAR)
200             note = (music_starting_note + record->event.key.col + music_offset + 32) + 5 * (MATRIX_ROWS - record->event.key.row);
201         else if (music_mode == MUSIC_MODE_VIOLIN)
202             note = (music_starting_note + record->event.key.col + music_offset + 32) + 7 * (MATRIX_ROWS - record->event.key.row);
203         else if (music_mode == MUSIC_MODE_MAJOR)
204             note = (music_starting_note + SCALE[record->event.key.col + music_offset] - 3) + 12 * (MATRIX_ROWS - record->event.key.row);
205         else
206             note = music_starting_note;
207 #    endif
208
209         if (record->event.pressed) {
210             music_noteon(note);
211             if (music_sequence_recording) {
212                 music_sequence[music_sequence_count] = note;
213                 music_sequence_count++;
214             }
215         } else {
216             music_noteoff(note);
217         }
218
219         if (music_mask(keycode)) return false;
220     }
221
222     return true;
223 }
224
225 bool music_mask(uint16_t keycode) {
226 #    ifdef MUSIC_MASK
227     return MUSIC_MASK;
228 #    else
229     return music_mask_kb(keycode);
230 #    endif
231 }
232
233 __attribute__((weak)) bool music_mask_kb(uint16_t keycode) { return music_mask_user(keycode); }
234
235 __attribute__((weak)) bool music_mask_user(uint16_t keycode) { return keycode < 0xFF; }
236
237 bool is_music_on(void) { return (music_activated != 0); }
238
239 void music_toggle(void) {
240     if (!music_activated) {
241         music_on();
242     } else {
243         music_off();
244     }
245 }
246
247 void music_on(void) {
248     music_activated = 1;
249 #    ifdef AUDIO_ENABLE
250     PLAY_SONG(music_on_song);
251 #    endif
252     music_on_user();
253 }
254
255 void music_off(void) {
256     music_all_notes_off();
257     music_activated = 0;
258 #    ifdef AUDIO_ENABLE
259     PLAY_SONG(music_off_song);
260 #    endif
261 }
262
263 bool is_midi_on(void) { return (midi_activated != 0); }
264
265 void midi_toggle(void) {
266     if (!midi_activated) {
267         midi_on();
268     } else {
269         midi_off();
270     }
271 }
272
273 void midi_on(void) {
274     midi_activated = 1;
275 #    ifdef AUDIO_ENABLE
276     PLAY_SONG(midi_on_song);
277 #    endif
278     midi_on_user();
279 }
280
281 void midi_off(void) {
282 #    if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
283     process_midi_all_notes_off();
284 #    endif
285     midi_activated = 0;
286 #    ifdef AUDIO_ENABLE
287     PLAY_SONG(midi_off_song);
288 #    endif
289 }
290
291 void music_mode_cycle(void) {
292     music_all_notes_off();
293     music_mode = (music_mode + 1) % NUMBER_OF_MODES;
294 #    ifdef AUDIO_ENABLE
295     PLAY_SONG(music_mode_songs[music_mode]);
296 #    endif
297 }
298
299 void matrix_scan_music(void) {
300     if (music_sequence_playing) {
301         if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) {
302             music_sequence_timer = timer_read();
303             uint8_t prev_note    = music_sequence[(music_sequence_position - 1 < 0) ? (music_sequence_position - 1 + music_sequence_count) : (music_sequence_position - 1)];
304             uint8_t next_note    = music_sequence[music_sequence_position];
305             music_noteoff(prev_note);
306             music_noteon(next_note);
307             music_sequence_position = (music_sequence_position + 1) % music_sequence_count;
308         }
309     }
310 }
311
312 __attribute__((weak)) void music_on_user() {}
313
314 __attribute__((weak)) void midi_on_user() {}
315
316 __attribute__((weak)) void music_scale_user() {}
317
318 #endif  // defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))