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;
44 #define MUSIC_ON_SONG SONG(MUSIC_ON_SOUND)
46 #ifndef MUSIC_OFF_SONG
47 #define MUSIC_OFF_SONG SONG(MUSIC_OFF_SOUND)
49 float music_on_song[][2] = MUSIC_ON_SONG;
50 float music_off_song[][2] = MUSIC_OFF_SONG;
53 static void music_noteon(uint8_t note) {
55 process_audio_noteon(note);
57 #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
58 process_midi_basic_noteon(note);
62 static void music_noteoff(uint8_t note) {
64 process_audio_noteoff(note);
66 #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
67 process_midi_basic_noteoff(note);
71 void music_all_notes_off(void) {
73 process_audio_all_notes_off();
75 #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
76 process_midi_all_notes_off();
80 bool process_music(uint16_t keycode, keyrecord_t *record) {
82 if (keycode == MU_ON && record->event.pressed) {
87 if (keycode == MU_OFF && record->event.pressed) {
92 if (keycode == MU_TOG && record->event.pressed) {
93 if (music_activated) {
101 if (music_activated) {
103 if (keycode == KC_LCTL && record->event.pressed) { // Start recording
104 music_all_notes_off();
105 music_sequence_recording = true;
106 music_sequence_recorded = false;
107 music_sequence_playing = false;
108 music_sequence_count = 0;
112 if (keycode == KC_LALT && record->event.pressed) { // Stop recording/playing
113 music_all_notes_off();
114 if (music_sequence_recording) { // was recording
115 music_sequence_recorded = true;
117 music_sequence_recording = false;
118 music_sequence_playing = false;
122 if (keycode == KC_LGUI && record->event.pressed && music_sequence_recorded) { // Start playing
123 music_all_notes_off();
124 music_sequence_recording = false;
125 music_sequence_playing = true;
126 music_sequence_position = 0;
127 music_sequence_timer = 0;
131 if (keycode == KC_UP) {
132 if (record->event.pressed)
133 music_sequence_interval-=10;
137 if (keycode == KC_DOWN) {
138 if (record->event.pressed)
139 music_sequence_interval+=10;
143 #define MUSIC_MODE_GUITAR
145 #ifdef MUSIC_MODE_CHROMATIC
146 uint8_t note = (music_starting_note + record->event.key.col + music_offset - 3)+12*(MATRIX_ROWS - record->event.key.row);
147 #elif defined(MUSIC_MODE_GUITAR)
148 uint8_t note = (music_starting_note + record->event.key.col + music_offset + 32)+5*(MATRIX_ROWS - record->event.key.row);
149 #elif defined(MUSIC_MODE_VIOLIN)
150 uint8_t note = (music_starting_note + record->event.key.col + music_offset + 32)+7*(MATRIX_ROWS - record->event.key.row);
152 uint8_t note = (music_starting_note + SCALE[record->event.key.col + music_offset] - 3)+12*(MATRIX_ROWS - record->event.key.row);
155 if (record->event.pressed) {
157 if (music_sequence_recording) {
158 music_sequence[music_sequence_count] = note;
159 music_sequence_count++;
165 if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through
172 bool is_music_on(void) {
173 return (music_activated != 0);
176 void music_toggle(void) {
177 if (!music_activated) {
184 void music_on(void) {
187 PLAY_SONG(music_on_song);
192 void music_off(void) {
193 music_all_notes_off();
196 PLAY_SONG(music_off_song);
200 void matrix_scan_music(void) {
201 if (music_sequence_playing) {
202 if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) {
203 music_sequence_timer = timer_read();
204 uint8_t prev_note = music_sequence[(music_sequence_position - 1 < 0)?(music_sequence_position - 1 + music_sequence_count):(music_sequence_position - 1)];
205 uint8_t next_note = music_sequence[music_sequence_position];
206 music_noteoff(prev_note);
207 music_noteon(next_note);
208 music_sequence_position = (music_sequence_position + 1) % music_sequence_count;
213 __attribute__ ((weak))
214 void music_on_user() {}
216 __attribute__ ((weak))
217 void music_scale_user() {}
219 #endif // defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))