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;
45 # ifndef MUSIC_ON_SONG
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)
54 # ifndef MIDI_OFF_SONG
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] = {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;
76 static void music_noteon(uint8_t note) {
78 if (music_activated) process_audio_noteon(note);
80 # if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
81 if (midi_activated) process_midi_basic_noteon(note);
85 static void music_noteoff(uint8_t note) {
87 if (music_activated) process_audio_noteoff(note);
89 # if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
90 if (midi_activated) process_midi_basic_noteoff(note);
94 void music_all_notes_off(void) {
96 if (music_activated) process_audio_all_notes_off();
98 # if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
99 if (midi_activated) process_midi_all_notes_off();
103 bool process_music(uint16_t keycode, keyrecord_t *record) {
104 if (keycode == MU_ON && record->event.pressed) {
109 if (keycode == MU_OFF && record->event.pressed) {
114 if (keycode == MU_TOG && record->event.pressed) {
115 if (music_activated) {
123 if (keycode == MI_ON && record->event.pressed) {
128 if (keycode == MI_OFF && record->event.pressed) {
133 if (keycode == MI_TOG && record->event.pressed) {
134 if (midi_activated) {
142 if (keycode == MU_MOD && record->event.pressed) {
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;
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;
163 music_sequence_recording = false;
164 music_sequence_playing = false;
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;
177 if (keycode == KC_UP) {
178 music_sequence_interval -= 10;
182 if (keycode == KC_DOWN) {
183 music_sequence_interval += 10;
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];
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;
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);
206 note = music_starting_note;
209 if (record->event.pressed) {
211 if (music_sequence_recording) {
212 music_sequence[music_sequence_count] = note;
213 music_sequence_count++;
219 if (music_mask(keycode)) return false;
225 bool music_mask(uint16_t keycode) {
229 return music_mask_kb(keycode);
233 __attribute__((weak)) bool music_mask_kb(uint16_t keycode) { return music_mask_user(keycode); }
235 __attribute__((weak)) bool music_mask_user(uint16_t keycode) { return keycode < 0xFF; }
237 bool is_music_on(void) { return (music_activated != 0); }
239 void music_toggle(void) {
240 if (!music_activated) {
247 void music_on(void) {
250 PLAY_SONG(music_on_song);
255 void music_off(void) {
256 music_all_notes_off();
259 PLAY_SONG(music_off_song);
263 bool is_midi_on(void) { return (midi_activated != 0); }
265 void midi_toggle(void) {
266 if (!midi_activated) {
276 PLAY_SONG(midi_on_song);
281 void midi_off(void) {
282 # if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
283 process_midi_all_notes_off();
287 PLAY_SONG(midi_off_song);
291 void music_mode_cycle(void) {
292 music_all_notes_off();
293 music_mode = (music_mode + 1) % NUMBER_OF_MODES;
295 PLAY_SONG(music_mode_songs[music_mode]);
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;
312 __attribute__((weak)) void music_on_user() {}
314 __attribute__((weak)) void midi_on_user() {}
316 __attribute__((weak)) void music_scale_user() {}
318 #endif // defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))