1 /* Copyright 2016 Jack Humbert
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.
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.
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/>.
16 #include "process_music.h"
19 #include "process_audio.h"
21 #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
22 #include "process_midi.h"
25 #if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
27 bool music_activated = false;
28 bool midi_activated = false;
29 uint8_t music_starting_note = 0x0C;
31 uint8_t music_mode = MUSIC_MODE_MAJOR;
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;
41 static uint16_t music_sequence_timer = 0;
42 static uint16_t music_sequence_interval = 100;
46 #define MUSIC_ON_SONG SONG(MUSIC_ON_SOUND)
48 #ifndef MUSIC_OFF_SONG
49 #define MUSIC_OFF_SONG SONG(MUSIC_OFF_SOUND)
52 #define MIDI_ON_SONG SONG(MUSIC_ON_SOUND)
55 #define MIDI_OFF_SONG SONG(MUSIC_OFF_SOUND)
57 #ifndef CHROMATIC_SONG
58 #define CHROMATIC_SONG SONG(CHROMATIC_SOUND)
61 #define GUITAR_SONG SONG(GUITAR_SOUND)
64 #define VIOLIN_SONG SONG(VIOLIN_SOUND)
67 #define MAJOR_SONG SONG(MAJOR_SOUND)
69 float music_mode_songs[NUMBER_OF_MODES][5][2] = {
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;
81 static void music_noteon(uint8_t note) {
84 process_audio_noteon(note);
86 #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
88 process_midi_basic_noteon(note);
92 static void music_noteoff(uint8_t note) {
95 process_audio_noteoff(note);
97 #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
99 process_midi_basic_noteoff(note);
103 void music_all_notes_off(void) {
106 process_audio_all_notes_off();
108 #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
110 process_midi_all_notes_off();
114 bool process_music(uint16_t keycode, keyrecord_t *record) {
116 if (keycode == MU_ON && record->event.pressed) {
121 if (keycode == MU_OFF && record->event.pressed) {
126 if (keycode == MU_TOG && record->event.pressed) {
127 if (music_activated) {
135 if (keycode == MI_ON && record->event.pressed) {
140 if (keycode == MI_OFF && record->event.pressed) {
145 if (keycode == MI_TOG && record->event.pressed) {
146 if (midi_activated) {
154 if (keycode == MU_MOD && record->event.pressed) {
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;
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;
175 music_sequence_recording = false;
176 music_sequence_playing = false;
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;
189 if (keycode == KC_UP) {
190 music_sequence_interval-=10;
194 if (keycode == KC_DOWN) {
195 music_sequence_interval+=10;
202 if (music_mode == MUSIC_MODE_CHROMATIC) {
203 note = music_starting_note + music_offset + 36 + music_map[record->event.key.row][record->event.key.col];
205 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;
209 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);
211 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);
213 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);
215 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);
218 note = music_starting_note;
221 if (record->event.pressed) {
223 if (music_sequence_recording) {
224 music_sequence[music_sequence_count] = note;
225 music_sequence_count++;
231 if (music_mask(keycode))
238 bool music_mask(uint16_t keycode) {
242 return music_mask_kb(keycode);
246 __attribute__((weak))
247 bool music_mask_kb(uint16_t keycode) {
248 return music_mask_user(keycode);
251 __attribute__((weak))
252 bool music_mask_user(uint16_t keycode) {
253 return keycode < 0xFF;
256 bool is_music_on(void) {
257 return (music_activated != 0);
260 void music_toggle(void) {
261 if (!music_activated) {
268 void music_on(void) {
271 PLAY_SONG(music_on_song);
276 void music_off(void) {
277 music_all_notes_off();
280 PLAY_SONG(music_off_song);
284 bool is_midi_on(void) {
285 return (midi_activated != 0);
288 void midi_toggle(void) {
289 if (!midi_activated) {
299 PLAY_SONG(midi_on_song);
304 void midi_off(void) {
305 #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
306 process_midi_all_notes_off();
310 PLAY_SONG(midi_off_song);
314 void music_mode_cycle(void) {
315 music_all_notes_off();
316 music_mode = (music_mode + 1) % NUMBER_OF_MODES;
318 PLAY_SONG(music_mode_songs[music_mode]);
322 void matrix_scan_music(void) {
323 if (music_sequence_playing) {
324 if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) {
325 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)];
327 uint8_t next_note = music_sequence[music_sequence_position];
328 music_noteoff(prev_note);
329 music_noteon(next_note);
330 music_sequence_position = (music_sequence_position + 1) % music_sequence_count;
335 __attribute__ ((weak))
336 void music_on_user() {}
338 __attribute__ ((weak))
339 void midi_on_user() {}
341 __attribute__ ((weak))
342 void music_scale_user() {}
344 #endif // defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))