]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/process_keycode/process_music.c
clean-up planck and preonic keymaps, move audio stuff around
[qmk_firmware.git] / quantum / process_keycode / process_music.c
1 /* Copyright 2016 Jack Humbert
2  *
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.
7  *
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.
12  *
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/>.
15  */
16 #include "process_music.h"
17
18 #ifdef AUDIO_ENABLE
19 #include "process_audio.h"
20 #endif
21 #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
22 #include "process_midi.h"
23 #endif
24
25 #if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
26
27 bool music_activated = false;
28 uint8_t music_starting_note = 0x0C;
29 int music_offset = 7;
30
31 // music sequencer
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;
38
39 static uint16_t music_sequence_timer = 0;
40 static uint16_t music_sequence_interval = 100;
41
42 #ifndef MUSIC_ON_SONG
43   #define MUSIC_ON_SONG SONG(MUSIC_ON_SOUND)
44 #endif
45 #ifndef MUSIC_OFF_SONG
46   #define MUSIC_OFF_SONG SONG(MUSIC_OFF_SOUND)
47 #endif
48 float music_on_song[][2] = MUSIC_ON_SONG;
49 float music_off_song[][2] = MUSIC_OFF_SONG;
50
51 static void music_noteon(uint8_t note) {
52     #ifdef AUDIO_ENABLE
53     process_audio_noteon(note);
54     #endif
55     #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
56     process_midi_basic_noteon(note);
57     #endif
58 }
59
60 static void music_noteoff(uint8_t note) {
61     #ifdef AUDIO_ENABLE
62     process_audio_noteoff(note);
63     #endif
64     #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
65     process_midi_basic_noteoff(note);
66     #endif
67 }
68
69 void music_all_notes_off(void) {
70     #ifdef AUDIO_ENABLE
71     process_audio_all_notes_off();
72     #endif
73     #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
74     process_midi_all_notes_off();
75     #endif
76 }
77
78 bool process_music(uint16_t keycode, keyrecord_t *record) {
79
80     if (keycode == MU_ON && record->event.pressed) {
81         music_on();
82         return false;
83     }
84
85     if (keycode == MU_OFF && record->event.pressed) {
86         music_off();
87         return false;
88     }
89
90     if (keycode == MU_TOG && record->event.pressed) {
91         if (music_activated) {
92             music_off();
93         } else {
94             music_on();
95         }
96         return false;
97     }
98
99     if (music_activated) {
100
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;
107         return false;
108       }
109
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;
114         }
115         music_sequence_recording = false;
116         music_sequence_playing = false;
117         return false;
118       }
119
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;
126         return false;
127       }
128
129       if (keycode == KC_UP) {
130         if (record->event.pressed)
131             music_sequence_interval-=10;
132         return false;
133       }
134
135       if (keycode == KC_DOWN) {
136         if (record->event.pressed)
137             music_sequence_interval+=10;
138         return false;
139       }
140
141       #define MUSIC_MODE_GUITAR
142
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);
149       #else
150       uint8_t note = (music_starting_note + SCALE[record->event.key.col + music_offset] - 3)+12*(MATRIX_ROWS - record->event.key.row);
151       #endif
152
153       if (record->event.pressed) {
154         music_noteon(note);
155         if (music_sequence_recording) {
156           music_sequence[music_sequence_count] = note;
157           music_sequence_count++;
158         }
159       } else {
160         music_noteoff(note);
161       }
162
163       if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through
164         return false;
165     }
166
167     return true;
168 }
169
170 bool is_music_on(void) {
171     return (music_activated != 0);
172 }
173
174 void music_toggle(void) {
175     if (!music_activated) {
176         music_on();
177     } else {
178         music_off();
179     }
180 }
181
182 void music_on(void) {
183     music_activated = 1;
184     PLAY_SONG(music_on_song);
185     music_on_user();
186 }
187
188 void music_off(void) {
189     music_all_notes_off();
190     music_activated = 0;
191     PLAY_SONG(music_off_song);
192 }
193
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;
203     }
204   }
205 }
206
207 __attribute__ ((weak))
208 void music_on_user() {}
209
210 __attribute__ ((weak))
211 void music_scale_user() {}
212
213 #endif // defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))