]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/process_keycode/process_music.c
63841d1e8754dd746ae31eb51e7bb886d1139840
[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 uint8_t music_starting_note = 0x0C;
29 int music_offset = 7;
30 uint8_t music_mode = MUSIC_MODE_CHROMATIC;
31
32 // music sequencer
33 static bool music_sequence_recording = false;
34 static bool music_sequence_recorded = false;
35 static bool music_sequence_playing = false;
36 static uint8_t music_sequence[16] = {0};
37 static uint8_t music_sequence_count = 0;
38 static uint8_t music_sequence_position = 0;
39
40 static uint16_t music_sequence_timer = 0;
41 static uint16_t music_sequence_interval = 100;
42
43 #ifdef AUDIO_ENABLE
44   #ifndef MUSIC_ON_SONG
45     #define MUSIC_ON_SONG SONG(MUSIC_ON_SOUND)
46   #endif
47   #ifndef MUSIC_OFF_SONG
48     #define MUSIC_OFF_SONG SONG(MUSIC_OFF_SOUND)
49   #endif
50   #ifndef CHROMATIC_SONG
51     #define CHROMATIC_SONG SONG(CHROMATIC_SOUND)
52   #endif
53   #ifndef GUITAR_SONG
54     #define GUITAR_SONG SONG(GUITAR_SOUND)
55   #endif
56   #ifndef VIOLIN_SONG
57     #define VIOLIN_SONG SONG(VIOLIN_SOUND)
58   #endif
59   #ifndef MAJOR_SONG
60     #define MAJOR_SONG SONG(MAJOR_SOUND)
61   #endif
62   float music_mode_songs[NUMBER_OF_MODES][5][2] = {
63     CHROMATIC_SONG,
64     GUITAR_SONG,
65     VIOLIN_SONG,
66     MAJOR_SONG
67   };
68   float music_on_song[][2] = MUSIC_ON_SONG;
69   float music_off_song[][2] = MUSIC_OFF_SONG;
70 #endif
71
72 #ifndef MUSIC_MASK
73   #define MUSIC_MASK keycode < 0xFF
74 #endif
75
76 static void music_noteon(uint8_t note) {
77     #ifdef AUDIO_ENABLE
78     process_audio_noteon(note);
79     #endif
80     #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
81     process_midi_basic_noteon(note);
82     #endif
83 }
84
85 static void music_noteoff(uint8_t note) {
86     #ifdef AUDIO_ENABLE
87     process_audio_noteoff(note);
88     #endif
89     #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
90     process_midi_basic_noteoff(note);
91     #endif
92 }
93
94 void music_all_notes_off(void) {
95     #ifdef AUDIO_ENABLE
96     process_audio_all_notes_off();
97     #endif
98     #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
99     process_midi_all_notes_off();
100     #endif
101 }
102
103 bool process_music(uint16_t keycode, keyrecord_t *record) {
104
105     if (keycode == MU_ON && record->event.pressed) {
106         music_on();
107         return false;
108     }
109
110     if (keycode == MU_OFF && record->event.pressed) {
111         music_off();
112         return false;
113     }
114
115     if (keycode == MU_TOG && record->event.pressed) {
116         if (music_activated) {
117             music_off();
118         } else {
119             music_on();
120         }
121         return false;
122     }
123
124     if (keycode == MU_MOD && record->event.pressed) {
125       music_mode_cycle();
126       return false;
127     }
128
129     if (music_activated) {
130       if (record->event.pressed) {
131         if (keycode == KC_LCTL) { // Start recording
132           music_all_notes_off();
133           music_sequence_recording = true;
134           music_sequence_recorded = false;
135           music_sequence_playing = false;
136           music_sequence_count = 0;
137           return false;
138         }
139
140         if (keycode == KC_LALT) { // Stop recording/playing
141           music_all_notes_off();
142           if (music_sequence_recording) { // was recording
143             music_sequence_recorded = true;
144           }
145           music_sequence_recording = false;
146           music_sequence_playing = false;
147           return false;
148         }
149
150         if (keycode == KC_LGUI && music_sequence_recorded) { // Start playing
151           music_all_notes_off();
152           music_sequence_recording = false;
153           music_sequence_playing = true;
154           music_sequence_position = 0;
155           music_sequence_timer = 0;
156           return false;
157         }
158
159         if (keycode == KC_UP) {
160           music_sequence_interval-=10;
161           return false;
162         }
163
164         if (keycode == KC_DOWN) {
165           music_sequence_interval+=10;
166           return false;
167         }
168       }
169
170       uint8_t note;
171       if (music_mode == MUSIC_MODE_CHROMATIC) 
172         note = (music_starting_note + record->event.key.col + music_offset - 3)+12*(MATRIX_ROWS - record->event.key.row);
173       else if (music_mode == MUSIC_MODE_GUITAR)
174         note = (music_starting_note + record->event.key.col + music_offset + 32)+5*(MATRIX_ROWS - record->event.key.row);
175       else if (music_mode == MUSIC_MODE_VIOLIN)
176         note = (music_starting_note + record->event.key.col + music_offset + 32)+7*(MATRIX_ROWS - record->event.key.row);
177       else if (music_mode == MUSIC_MODE_MAJOR)
178         note = (music_starting_note + SCALE[record->event.key.col + music_offset] - 3)+12*(MATRIX_ROWS - record->event.key.row);
179       else
180         note = music_starting_note;
181
182       if (record->event.pressed) {
183         music_noteon(note);
184         if (music_sequence_recording) {
185           music_sequence[music_sequence_count] = note;
186           music_sequence_count++;
187         }
188       } else {
189         music_noteoff(note);
190       }
191
192       if (MUSIC_MASK)
193         return false;
194     }
195
196     return true;
197 }
198
199 bool is_music_on(void) {
200     return (music_activated != 0);
201 }
202
203 void music_toggle(void) {
204     if (!music_activated) {
205         music_on();
206     } else {
207         music_off();
208     }
209 }
210
211 void music_on(void) {
212     music_activated = 1;
213     #ifdef AUDIO_ENABLE
214       PLAY_SONG(music_on_song);
215     #endif
216     music_on_user();
217 }
218
219 void music_off(void) {
220     music_all_notes_off();
221     music_activated = 0;
222     #ifdef AUDIO_ENABLE
223       PLAY_SONG(music_off_song);
224     #endif
225 }
226
227 void music_mode_cycle(void) {
228   music_all_notes_off();
229   music_mode = (music_mode + 1) % NUMBER_OF_MODES;
230   #ifdef AUDIO_ENABLE
231     PLAY_SONG(music_mode_songs[music_mode]);
232   #endif
233 }
234
235 void matrix_scan_music(void) {
236   if (music_sequence_playing) {
237     if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) {
238       music_sequence_timer = timer_read();
239       uint8_t prev_note = music_sequence[(music_sequence_position - 1 < 0)?(music_sequence_position - 1 + music_sequence_count):(music_sequence_position - 1)];
240       uint8_t next_note = music_sequence[music_sequence_position];
241       music_noteoff(prev_note);
242       music_noteon(next_note);
243       music_sequence_position = (music_sequence_position + 1) % music_sequence_count;
244     }
245   }
246 }
247
248 __attribute__ ((weak))
249 void music_on_user() {}
250
251 __attribute__ ((weak))
252 void music_scale_user() {}
253
254 #endif // defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))