]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/process_keycode/process_music.c
fixes midi compile error
[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_CHROMATIC;
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 #ifndef MUSIC_MASK
82   #define MUSIC_MASK keycode < 0xFF
83 #endif
84
85 static void music_noteon(uint8_t note) {
86     #ifdef AUDIO_ENABLE
87     if (music_activated)
88       process_audio_noteon(note);
89     #endif
90     #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
91     if (midi_activated)
92       process_midi_basic_noteon(note);
93     #endif
94 }
95
96 static void music_noteoff(uint8_t note) {
97     #ifdef AUDIO_ENABLE
98     if (music_activated)
99       process_audio_noteoff(note);
100     #endif
101     #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
102     if (midi_activated)
103       process_midi_basic_noteoff(note);
104     #endif
105 }
106
107 void music_all_notes_off(void) {
108     #ifdef AUDIO_ENABLE
109     if (music_activated)
110       process_audio_all_notes_off();
111     #endif
112     #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
113     if (midi_activated)
114       process_midi_all_notes_off();
115     #endif
116 }
117
118 bool process_music(uint16_t keycode, keyrecord_t *record) {
119
120     if (keycode == MU_ON && record->event.pressed) {
121         music_on();
122         return false;
123     }    
124
125     if (keycode == MU_OFF && record->event.pressed) {
126         music_off();
127         return false;
128     }
129
130     if (keycode == MU_TOG && record->event.pressed) {
131         if (music_activated) {
132             music_off();
133         } else {
134             music_on();
135         }
136         return false;
137     }
138
139     if (keycode == MI_ON && record->event.pressed) {
140         midi_on();
141         return false;
142     }    
143
144     if (keycode == MI_OFF && record->event.pressed) {
145         midi_off();
146         return false;
147     }
148
149     if (keycode == MI_TOG && record->event.pressed) {
150         if (midi_activated) {
151             midi_off();
152         } else {
153             midi_on();
154         }
155         return false;
156     }
157
158     if (keycode == MU_MOD && record->event.pressed) {
159       music_mode_cycle();
160       return false;
161     }
162
163     if (music_activated || midi_activated) {
164       if (record->event.pressed) {
165         if (keycode == KC_LCTL) { // Start recording
166           music_all_notes_off();
167           music_sequence_recording = true;
168           music_sequence_recorded = false;
169           music_sequence_playing = false;
170           music_sequence_count = 0;
171           return false;
172         }
173
174         if (keycode == KC_LALT) { // Stop recording/playing
175           music_all_notes_off();
176           if (music_sequence_recording) { // was recording
177             music_sequence_recorded = true;
178           }
179           music_sequence_recording = false;
180           music_sequence_playing = false;
181           return false;
182         }
183
184         if (keycode == KC_LGUI && music_sequence_recorded) { // Start playing
185           music_all_notes_off();
186           music_sequence_recording = false;
187           music_sequence_playing = true;
188           music_sequence_position = 0;
189           music_sequence_timer = 0;
190           return false;
191         }
192
193         if (keycode == KC_UP) {
194           music_sequence_interval-=10;
195           return false;
196         }
197
198         if (keycode == KC_DOWN) {
199           music_sequence_interval+=10;
200           return false;
201         }
202       }
203
204       uint8_t note;
205       if (music_mode == MUSIC_MODE_CHROMATIC) 
206         note = (music_starting_note + record->event.key.col + music_offset - 3)+12*(MATRIX_ROWS - record->event.key.row);
207       else if (music_mode == MUSIC_MODE_GUITAR)
208         note = (music_starting_note + record->event.key.col + music_offset + 32)+5*(MATRIX_ROWS - record->event.key.row);
209       else if (music_mode == MUSIC_MODE_VIOLIN)
210         note = (music_starting_note + record->event.key.col + music_offset + 32)+7*(MATRIX_ROWS - record->event.key.row);
211       else if (music_mode == MUSIC_MODE_MAJOR)
212         note = (music_starting_note + SCALE[record->event.key.col + music_offset] - 3)+12*(MATRIX_ROWS - record->event.key.row);
213       else
214         note = music_starting_note;
215
216       if (record->event.pressed) {
217         music_noteon(note);
218         if (music_sequence_recording) {
219           music_sequence[music_sequence_count] = note;
220           music_sequence_count++;
221         }
222       } else {
223         music_noteoff(note);
224       }
225
226       if (MUSIC_MASK)
227         return false;
228     }
229
230     return true;
231 }
232
233 bool is_music_on(void) {
234     return (music_activated != 0);
235 }
236
237 void music_toggle(void) {
238     if (!music_activated) {
239         music_on();
240     } else {
241         music_off();
242     }
243 }
244
245 void music_on(void) {
246     music_activated = 1;
247     #ifdef AUDIO_ENABLE
248       PLAY_SONG(music_on_song);
249     #endif
250     music_on_user();
251 }
252
253 void music_off(void) {
254     music_all_notes_off();
255     music_activated = 0;
256     #ifdef AUDIO_ENABLE
257       PLAY_SONG(music_off_song);
258     #endif
259 }
260
261 bool is_midi_on(void) {
262     return (midi_activated != 0);
263 }
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))
313 void music_on_user() {}
314
315 __attribute__ ((weak))
316 void midi_on_user() {}
317
318 __attribute__ ((weak))
319 void music_scale_user() {}
320
321 #endif // defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))