]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/process_keycode/process_music.c
742bb08b126a557f3484f0eaa6bda12e2a8d6d0c
[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] = {
70     CHROMATIC_SONG,
71     GUITAR_SONG,
72     VIOLIN_SONG,
73     MAJOR_SONG
74   };
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
81 static void music_noteon(uint8_t note) {
82     #ifdef AUDIO_ENABLE
83     if (music_activated)
84       process_audio_noteon(note);
85     #endif
86     #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
87     if (midi_activated)
88       process_midi_basic_noteon(note);
89     #endif
90 }
91
92 static void music_noteoff(uint8_t note) {
93     #ifdef AUDIO_ENABLE
94     if (music_activated)
95       process_audio_noteoff(note);
96     #endif
97     #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
98     if (midi_activated)
99       process_midi_basic_noteoff(note);
100     #endif
101 }
102
103 void music_all_notes_off(void) {
104     #ifdef AUDIO_ENABLE
105     if (music_activated)
106       process_audio_all_notes_off();
107     #endif
108     #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
109     if (midi_activated)
110       process_midi_all_notes_off();
111     #endif
112 }
113
114 bool process_music(uint16_t keycode, keyrecord_t *record) {
115
116     if (keycode == MU_ON && record->event.pressed) {
117         music_on();
118         return false;
119     }
120
121     if (keycode == MU_OFF && record->event.pressed) {
122         music_off();
123         return false;
124     }
125
126     if (keycode == MU_TOG && record->event.pressed) {
127         if (music_activated) {
128             music_off();
129         } else {
130             music_on();
131         }
132         return false;
133     }
134
135     if (keycode == MI_ON && record->event.pressed) {
136         midi_on();
137         return false;
138     }
139
140     if (keycode == MI_OFF && record->event.pressed) {
141         midi_off();
142         return false;
143     }
144
145     if (keycode == MI_TOG && record->event.pressed) {
146         if (midi_activated) {
147             midi_off();
148         } else {
149             midi_on();
150         }
151         return false;
152     }
153
154     if (keycode == MU_MOD && record->event.pressed) {
155       music_mode_cycle();
156       return false;
157     }
158
159     if (music_activated || midi_activated) {
160       if (record->event.pressed) {
161         if (keycode == KC_LCTL) { // Start recording
162           music_all_notes_off();
163           music_sequence_recording = true;
164           music_sequence_recorded = false;
165           music_sequence_playing = false;
166           music_sequence_count = 0;
167           return false;
168         }
169
170         if (keycode == KC_LALT) { // Stop recording/playing
171           music_all_notes_off();
172           if (music_sequence_recording) { // was recording
173             music_sequence_recorded = true;
174           }
175           music_sequence_recording = false;
176           music_sequence_playing = false;
177           return false;
178         }
179
180         if (keycode == KC_LGUI && music_sequence_recorded) { // Start playing
181           music_all_notes_off();
182           music_sequence_recording = false;
183           music_sequence_playing = true;
184           music_sequence_position = 0;
185           music_sequence_timer = 0;
186           return false;
187         }
188
189         if (keycode == KC_UP) {
190           music_sequence_interval-=10;
191           return false;
192         }
193
194         if (keycode == KC_DOWN) {
195           music_sequence_interval+=10;
196           return false;
197         }
198       }
199
200       uint8_t note;
201       if (music_mode == MUSIC_MODE_CHROMATIC)
202         note = (music_starting_note + record->event.key.col + music_offset - 3)+12*(MATRIX_ROWS - record->event.key.row);
203       else if (music_mode == MUSIC_MODE_GUITAR)
204         note = (music_starting_note + record->event.key.col + music_offset + 32)+5*(MATRIX_ROWS - record->event.key.row);
205       else if (music_mode == MUSIC_MODE_VIOLIN)
206         note = (music_starting_note + record->event.key.col + music_offset + 32)+7*(MATRIX_ROWS - record->event.key.row);
207       else if (music_mode == MUSIC_MODE_MAJOR)
208         note = (music_starting_note + SCALE[record->event.key.col + music_offset] - 3)+12*(MATRIX_ROWS - record->event.key.row);
209       else
210         note = music_starting_note;
211
212       if (record->event.pressed) {
213         music_noteon(note);
214         if (music_sequence_recording) {
215           music_sequence[music_sequence_count] = note;
216           music_sequence_count++;
217         }
218       } else {
219         music_noteoff(note);
220       }
221
222       if (music_mask(keycode))
223         return false;
224     }
225
226     return true;
227 }
228
229 bool music_mask(uint16_t keycode) {
230   #ifdef MUSIC_MASK
231     return MUSIC_MASK;
232   #else
233     return music_mask_kb(keycode);
234   #endif
235 }
236
237 __attribute__((weak))
238 bool music_mask_kb(uint16_t keycode) {
239   return music_mask_user(keycode);
240 }
241
242 __attribute__((weak))
243 bool music_mask_user(uint16_t keycode) {
244   return keycode < 0xFF;
245 }
246
247 bool is_music_on(void) {
248     return (music_activated != 0);
249 }
250
251 void music_toggle(void) {
252     if (!music_activated) {
253         music_on();
254     } else {
255         music_off();
256     }
257 }
258
259 void music_on(void) {
260     music_activated = 1;
261     #ifdef AUDIO_ENABLE
262       PLAY_SONG(music_on_song);
263     #endif
264     music_on_user();
265 }
266
267 void music_off(void) {
268     music_all_notes_off();
269     music_activated = 0;
270     #ifdef AUDIO_ENABLE
271       PLAY_SONG(music_off_song);
272     #endif
273 }
274
275 bool is_midi_on(void) {
276     return (midi_activated != 0);
277 }
278
279 void midi_toggle(void) {
280     if (!midi_activated) {
281         midi_on();
282     } else {
283         midi_off();
284     }
285 }
286
287 void midi_on(void) {
288     midi_activated = 1;
289     #ifdef AUDIO_ENABLE
290       PLAY_SONG(midi_on_song);
291     #endif
292     midi_on_user();
293 }
294
295 void midi_off(void) {
296     #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
297       process_midi_all_notes_off();
298     #endif
299     midi_activated = 0;
300     #ifdef AUDIO_ENABLE
301       PLAY_SONG(midi_off_song);
302     #endif
303 }
304
305 void music_mode_cycle(void) {
306   music_all_notes_off();
307   music_mode = (music_mode + 1) % NUMBER_OF_MODES;
308   #ifdef AUDIO_ENABLE
309     PLAY_SONG(music_mode_songs[music_mode]);
310   #endif
311 }
312
313 void matrix_scan_music(void) {
314   if (music_sequence_playing) {
315     if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) {
316       music_sequence_timer = timer_read();
317       uint8_t prev_note = music_sequence[(music_sequence_position - 1 < 0)?(music_sequence_position - 1 + music_sequence_count):(music_sequence_position - 1)];
318       uint8_t next_note = music_sequence[music_sequence_position];
319       music_noteoff(prev_note);
320       music_noteon(next_note);
321       music_sequence_position = (music_sequence_position + 1) % music_sequence_count;
322     }
323   }
324 }
325
326 __attribute__ ((weak))
327 void music_on_user() {}
328
329 __attribute__ ((weak))
330 void midi_on_user() {}
331
332 __attribute__ ((weak))
333 void music_scale_user() {}
334
335 #endif // defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))