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;
30 uint8_t music_mode = MUSIC_MODE_CHROMATIC;
33 static bool music_sequence_recording = false;
34 static bool music_sequence_recorded = false;
35 static bool music_sequence_playing = false;
36 static uint8_t music_sequence[16] = {0};
37 static uint8_t music_sequence_count = 0;
38 static uint8_t music_sequence_position = 0;
40 static uint16_t music_sequence_timer = 0;
41 static uint16_t music_sequence_interval = 100;
45 #define MUSIC_ON_SONG SONG(MUSIC_ON_SOUND)
47 #ifndef MUSIC_OFF_SONG
48 #define MUSIC_OFF_SONG SONG(MUSIC_OFF_SOUND)
50 #ifndef CHROMATIC_SONG
51 #define CHROMATIC_SONG SONG(CHROMATIC_SOUND)
54 #define GUITAR_SONG SONG(GUITAR_SOUND)
57 #define VIOLIN_SONG SONG(VIOLIN_SOUND)
60 #define MAJOR_SONG SONG(MAJOR_SOUND)
62 float music_mode_songs[NUMBER_OF_MODES][5][2] = {
68 float music_on_song[][2] = MUSIC_ON_SONG;
69 float music_off_song[][2] = MUSIC_OFF_SONG;
73 #define MUSIC_MASK keycode < 0xFF
76 static void music_noteon(uint8_t note) {
78 process_audio_noteon(note);
80 #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
81 process_midi_basic_noteon(note);
85 static void music_noteoff(uint8_t note) {
87 process_audio_noteoff(note);
89 #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
90 process_midi_basic_noteoff(note);
94 void music_all_notes_off(void) {
96 process_audio_all_notes_off();
98 #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
99 process_midi_all_notes_off();
103 bool process_music(uint16_t keycode, keyrecord_t *record) {
105 if (keycode == MU_ON && record->event.pressed) {
110 if (keycode == MU_OFF && record->event.pressed) {
115 if (keycode == MU_TOG && record->event.pressed) {
116 if (music_activated) {
124 if (keycode == MU_MOD && record->event.pressed) {
129 if (music_activated) {
130 if (record->event.pressed) {
131 if (keycode == KC_LCTL) { // Start recording
132 music_all_notes_off();
133 music_sequence_recording = true;
134 music_sequence_recorded = false;
135 music_sequence_playing = false;
136 music_sequence_count = 0;
140 if (keycode == KC_LALT) { // Stop recording/playing
141 music_all_notes_off();
142 if (music_sequence_recording) { // was recording
143 music_sequence_recorded = true;
145 music_sequence_recording = false;
146 music_sequence_playing = false;
150 if (keycode == KC_LGUI && music_sequence_recorded) { // Start playing
151 music_all_notes_off();
152 music_sequence_recording = false;
153 music_sequence_playing = true;
154 music_sequence_position = 0;
155 music_sequence_timer = 0;
159 if (keycode == KC_UP) {
160 music_sequence_interval-=10;
164 if (keycode == KC_DOWN) {
165 music_sequence_interval+=10;
171 if (music_mode == MUSIC_MODE_CHROMATIC)
172 note = (music_starting_note + record->event.key.col + music_offset - 3)+12*(MATRIX_ROWS - record->event.key.row);
173 else if (music_mode == MUSIC_MODE_GUITAR)
174 note = (music_starting_note + record->event.key.col + music_offset + 32)+5*(MATRIX_ROWS - record->event.key.row);
175 else if (music_mode == MUSIC_MODE_VIOLIN)
176 note = (music_starting_note + record->event.key.col + music_offset + 32)+7*(MATRIX_ROWS - record->event.key.row);
177 else if (music_mode == MUSIC_MODE_MAJOR)
178 note = (music_starting_note + SCALE[record->event.key.col + music_offset] - 3)+12*(MATRIX_ROWS - record->event.key.row);
180 note = music_starting_note;
182 if (record->event.pressed) {
184 if (music_sequence_recording) {
185 music_sequence[music_sequence_count] = note;
186 music_sequence_count++;
199 bool is_music_on(void) {
200 return (music_activated != 0);
203 void music_toggle(void) {
204 if (!music_activated) {
211 void music_on(void) {
214 PLAY_SONG(music_on_song);
219 void music_off(void) {
220 music_all_notes_off();
223 PLAY_SONG(music_off_song);
227 void music_mode_cycle(void) {
228 music_all_notes_off();
229 music_mode = (music_mode + 1) % NUMBER_OF_MODES;
231 PLAY_SONG(music_mode_songs[music_mode]);
235 void matrix_scan_music(void) {
236 if (music_sequence_playing) {
237 if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) {
238 music_sequence_timer = timer_read();
239 uint8_t prev_note = music_sequence[(music_sequence_position - 1 < 0)?(music_sequence_position - 1 + music_sequence_count):(music_sequence_position - 1)];
240 uint8_t next_note = music_sequence[music_sequence_position];
241 music_noteoff(prev_note);
242 music_noteon(next_note);
243 music_sequence_position = (music_sequence_position + 1) % music_sequence_count;
248 __attribute__ ((weak))
249 void music_on_user() {}
251 __attribute__ ((weak))
252 void music_scale_user() {}
254 #endif // defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))