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 uint8_t music_starting_note = 0x0C;
32 static bool music_sequence_recording = false;
33 static bool music_sequence_recorded = false;
34 static bool music_sequence_playing = false;
35 static uint8_t music_sequence[16] = {0};
36 static uint8_t music_sequence_count = 0;
37 static uint8_t music_sequence_position = 0;
39 static uint16_t music_sequence_timer = 0;
40 static uint16_t music_sequence_interval = 100;
42 static void music_noteon(uint8_t note) {
44 process_audio_noteon(note);
46 #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
47 process_midi_basic_noteon(note);
51 static void music_noteoff(uint8_t note) {
53 process_audio_noteoff(note);
55 #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
56 process_midi_basic_noteoff(note);
60 void music_all_notes_off(void) {
62 process_audio_all_notes_off();
64 #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
65 process_midi_all_notes_off();
69 bool process_music(uint16_t keycode, keyrecord_t *record) {
71 if (keycode == MU_ON && record->event.pressed) {
76 if (keycode == MU_OFF && record->event.pressed) {
81 if (keycode == MU_TOG && record->event.pressed) {
93 if (music_activated) {
95 if (keycode == KC_LCTL && record->event.pressed) { // Start recording
96 music_all_notes_off();
97 music_sequence_recording = true;
98 music_sequence_recorded = false;
99 music_sequence_playing = false;
100 music_sequence_count = 0;
104 if (keycode == KC_LALT && record->event.pressed) { // Stop recording/playing
105 music_all_notes_off();
106 if (music_sequence_recording) { // was recording
107 music_sequence_recorded = true;
109 music_sequence_recording = false;
110 music_sequence_playing = false;
114 if (keycode == KC_LGUI && record->event.pressed && music_sequence_recorded) { // Start playing
115 music_all_notes_off();
116 music_sequence_recording = false;
117 music_sequence_playing = true;
118 music_sequence_position = 0;
119 music_sequence_timer = 0;
123 if (keycode == KC_UP) {
124 if (record->event.pressed)
125 music_sequence_interval-=10;
129 if (keycode == KC_DOWN) {
130 if (record->event.pressed)
131 music_sequence_interval+=10;
135 #define MUSIC_MODE_GUITAR
137 #ifdef MUSIC_MODE_CHROMATIC
138 uint8_t note = (music_starting_note + record->event.key.col + music_offset - 3)+12*(MATRIX_ROWS - record->event.key.row);
139 #elif defined(MUSIC_MODE_GUITAR)
140 uint8_t note = (music_starting_note + record->event.key.col + music_offset + 32)+5*(MATRIX_ROWS - record->event.key.row);
141 #elif defined(MUSIC_MODE_VIOLIN)
142 uint8_t note = (music_starting_note + record->event.key.col + music_offset + 32)+7*(MATRIX_ROWS - record->event.key.row);
144 uint8_t note = (music_starting_note + SCALE[record->event.key.col + music_offset] - 3)+12*(MATRIX_ROWS - record->event.key.row);
147 if (record->event.pressed) {
149 if (music_sequence_recording) {
150 music_sequence[music_sequence_count] = note;
151 music_sequence_count++;
157 if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through
164 bool is_music_on(void) {
165 return (music_activated != 0);
168 void music_toggle(void) {
169 if (!music_activated) {
176 void music_on(void) {
181 void music_off(void) {
183 music_all_notes_off();
186 void matrix_scan_music(void) {
187 if (music_sequence_playing) {
188 if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) {
189 music_sequence_timer = timer_read();
190 uint8_t prev_note = music_sequence[(music_sequence_position - 1 < 0)?(music_sequence_position - 1 + music_sequence_count):(music_sequence_position - 1)];
191 uint8_t next_note = music_sequence[music_sequence_position];
192 music_noteoff(prev_note);
193 music_noteon(next_note);
194 music_sequence_position = (music_sequence_position + 1) % music_sequence_count;
199 __attribute__ ((weak))
200 void music_on_user() {}
202 __attribute__ ((weak))
203 void music_scale_user() {}
205 #endif // defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))