]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/process_keycode/process_music.c
escape song playing in music
[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 #ifdef AUDIO_ENABLE
43   #ifndef MUSIC_ON_SONG
44     #define MUSIC_ON_SONG SONG(MUSIC_ON_SOUND)
45   #endif
46   #ifndef MUSIC_OFF_SONG
47     #define MUSIC_OFF_SONG SONG(MUSIC_OFF_SOUND)
48   #endif
49   float music_on_song[][2] = MUSIC_ON_SONG;
50   float music_off_song[][2] = MUSIC_OFF_SONG;
51 #endif
52
53 static void music_noteon(uint8_t note) {
54     #ifdef AUDIO_ENABLE
55     process_audio_noteon(note);
56     #endif
57     #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
58     process_midi_basic_noteon(note);
59     #endif
60 }
61
62 static void music_noteoff(uint8_t note) {
63     #ifdef AUDIO_ENABLE
64     process_audio_noteoff(note);
65     #endif
66     #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
67     process_midi_basic_noteoff(note);
68     #endif
69 }
70
71 void music_all_notes_off(void) {
72     #ifdef AUDIO_ENABLE
73     process_audio_all_notes_off();
74     #endif
75     #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
76     process_midi_all_notes_off();
77     #endif
78 }
79
80 bool process_music(uint16_t keycode, keyrecord_t *record) {
81
82     if (keycode == MU_ON && record->event.pressed) {
83         music_on();
84         return false;
85     }
86
87     if (keycode == MU_OFF && record->event.pressed) {
88         music_off();
89         return false;
90     }
91
92     if (keycode == MU_TOG && record->event.pressed) {
93         if (music_activated) {
94             music_off();
95         } else {
96             music_on();
97         }
98         return false;
99     }
100
101     if (music_activated) {
102
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;
109         return false;
110       }
111
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;
116         }
117         music_sequence_recording = false;
118         music_sequence_playing = false;
119         return false;
120       }
121
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;
128         return false;
129       }
130
131       if (keycode == KC_UP) {
132         if (record->event.pressed)
133             music_sequence_interval-=10;
134         return false;
135       }
136
137       if (keycode == KC_DOWN) {
138         if (record->event.pressed)
139             music_sequence_interval+=10;
140         return false;
141       }
142
143       #define MUSIC_MODE_GUITAR
144
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);
151       #else
152       uint8_t note = (music_starting_note + SCALE[record->event.key.col + music_offset] - 3)+12*(MATRIX_ROWS - record->event.key.row);
153       #endif
154
155       if (record->event.pressed) {
156         music_noteon(note);
157         if (music_sequence_recording) {
158           music_sequence[music_sequence_count] = note;
159           music_sequence_count++;
160         }
161       } else {
162         music_noteoff(note);
163       }
164
165       if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through
166         return false;
167     }
168
169     return true;
170 }
171
172 bool is_music_on(void) {
173     return (music_activated != 0);
174 }
175
176 void music_toggle(void) {
177     if (!music_activated) {
178         music_on();
179     } else {
180         music_off();
181     }
182 }
183
184 void music_on(void) {
185     music_activated = 1;
186     #ifdef AUDIO_ENABLE
187       PLAY_SONG(music_on_song);
188     #endif
189     music_on_user();
190 }
191
192 void music_off(void) {
193     music_all_notes_off();
194     music_activated = 0;
195     #ifdef AUDIO_ENABLE
196       PLAY_SONG(music_off_song);
197     #endif
198 }
199
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;
209     }
210   }
211 }
212
213 __attribute__ ((weak))
214 void music_on_user() {}
215
216 __attribute__ ((weak))
217 void music_scale_user() {}
218
219 #endif // defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))