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;
43 #define MUSIC_ON_SONG SONG(MUSIC_ON_SOUND)
45 #ifndef MUSIC_OFF_SONG
46 #define MUSIC_OFF_SONG SONG(MUSIC_OFF_SOUND)
48 float music_on_song[][2] = MUSIC_ON_SONG;
49 float music_off_song[][2] = MUSIC_OFF_SONG;
51 static void music_noteon(uint8_t note) {
53 process_audio_noteon(note);
55 #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
56 process_midi_basic_noteon(note);
60 static void music_noteoff(uint8_t note) {
62 process_audio_noteoff(note);
64 #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
65 process_midi_basic_noteoff(note);
69 void music_all_notes_off(void) {
71 process_audio_all_notes_off();
73 #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
74 process_midi_all_notes_off();
78 bool process_music(uint16_t keycode, keyrecord_t *record) {
80 if (keycode == MU_ON && record->event.pressed) {
85 if (keycode == MU_OFF && record->event.pressed) {
90 if (keycode == MU_TOG && record->event.pressed) {
91 if (music_activated) {
99 if (music_activated) {
101 if (keycode == KC_LCTL && record->event.pressed) { // Start recording
102 music_all_notes_off();
103 music_sequence_recording = true;
104 music_sequence_recorded = false;
105 music_sequence_playing = false;
106 music_sequence_count = 0;
110 if (keycode == KC_LALT && record->event.pressed) { // Stop recording/playing
111 music_all_notes_off();
112 if (music_sequence_recording) { // was recording
113 music_sequence_recorded = true;
115 music_sequence_recording = false;
116 music_sequence_playing = false;
120 if (keycode == KC_LGUI && record->event.pressed && music_sequence_recorded) { // Start playing
121 music_all_notes_off();
122 music_sequence_recording = false;
123 music_sequence_playing = true;
124 music_sequence_position = 0;
125 music_sequence_timer = 0;
129 if (keycode == KC_UP) {
130 if (record->event.pressed)
131 music_sequence_interval-=10;
135 if (keycode == KC_DOWN) {
136 if (record->event.pressed)
137 music_sequence_interval+=10;
141 #define MUSIC_MODE_GUITAR
143 #ifdef MUSIC_MODE_CHROMATIC
144 uint8_t note = (music_starting_note + record->event.key.col + music_offset - 3)+12*(MATRIX_ROWS - record->event.key.row);
145 #elif defined(MUSIC_MODE_GUITAR)
146 uint8_t note = (music_starting_note + record->event.key.col + music_offset + 32)+5*(MATRIX_ROWS - record->event.key.row);
147 #elif defined(MUSIC_MODE_VIOLIN)
148 uint8_t note = (music_starting_note + record->event.key.col + music_offset + 32)+7*(MATRIX_ROWS - record->event.key.row);
150 uint8_t note = (music_starting_note + SCALE[record->event.key.col + music_offset] - 3)+12*(MATRIX_ROWS - record->event.key.row);
153 if (record->event.pressed) {
155 if (music_sequence_recording) {
156 music_sequence[music_sequence_count] = note;
157 music_sequence_count++;
163 if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through
170 bool is_music_on(void) {
171 return (music_activated != 0);
174 void music_toggle(void) {
175 if (!music_activated) {
182 void music_on(void) {
184 PLAY_SONG(music_on_song);
188 void music_off(void) {
189 music_all_notes_off();
191 PLAY_SONG(music_off_song);
194 void matrix_scan_music(void) {
195 if (music_sequence_playing) {
196 if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) {
197 music_sequence_timer = timer_read();
198 uint8_t prev_note = music_sequence[(music_sequence_position - 1 < 0)?(music_sequence_position - 1 + music_sequence_count):(music_sequence_position - 1)];
199 uint8_t next_note = music_sequence[music_sequence_position];
200 music_noteoff(prev_note);
201 music_noteon(next_note);
202 music_sequence_position = (music_sequence_position + 1) % music_sequence_count;
207 __attribute__ ((weak))
208 void music_on_user() {}
210 __attribute__ ((weak))
211 void music_scale_user() {}
213 #endif // defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))