]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/process_keycode/process_music.c
Merge remote-tracking branch 'upstream/master'
[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 static void music_noteon(uint8_t note) {
43     #ifdef AUDIO_ENABLE
44     process_audio_noteon(note);
45     #endif
46     #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
47     process_midi_basic_noteon(note);
48     #endif
49 }
50
51 static void music_noteoff(uint8_t note) {
52     #ifdef AUDIO_ENABLE
53     process_audio_noteoff(note);
54     #endif
55     #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
56     process_midi_basic_noteoff(note);
57     #endif
58 }
59
60 void music_all_notes_off(void) {
61     #ifdef AUDIO_ENABLE
62     process_audio_all_notes_off();
63     #endif
64     #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
65     process_midi_all_notes_off();
66     #endif
67 }
68
69 bool process_music(uint16_t keycode, keyrecord_t *record) {
70
71     if (keycode == MU_ON && record->event.pressed) {
72         music_on();
73         return false;
74     }
75
76     if (keycode == MU_OFF && record->event.pressed) {
77         music_off();
78         return false;
79     }
80
81     if (keycode == MU_TOG && record->event.pressed) {
82         if (music_activated)
83         {
84             music_off();
85         }
86         else
87         {
88             music_on();
89         }
90         return false;
91     }
92
93     if (music_activated) {
94
95       if (keycode == KC_LCTL && record->event.pressed) { // Start recording
96         music_all_notes_off();
97         music_sequence_recording = true;
98         music_sequence_recorded = false;
99         music_sequence_playing = false;
100         music_sequence_count = 0;
101         return false;
102       }
103
104       if (keycode == KC_LALT && record->event.pressed) { // Stop recording/playing
105         music_all_notes_off();
106         if (music_sequence_recording) { // was recording
107           music_sequence_recorded = true;
108         }
109         music_sequence_recording = false;
110         music_sequence_playing = false;
111         return false;
112       }
113
114       if (keycode == KC_LGUI && record->event.pressed && music_sequence_recorded) { // Start playing
115         music_all_notes_off();
116         music_sequence_recording = false;
117         music_sequence_playing = true;
118         music_sequence_position = 0;
119         music_sequence_timer = 0;
120         return false;
121       }
122
123       if (keycode == KC_UP) {
124         if (record->event.pressed)
125             music_sequence_interval-=10;
126         return false;
127       }
128
129       if (keycode == KC_DOWN) {
130         if (record->event.pressed)
131             music_sequence_interval+=10;
132         return false;
133       }
134
135       #define MUSIC_MODE_GUITAR
136
137       #ifdef MUSIC_MODE_CHROMATIC
138       uint8_t note = (music_starting_note + record->event.key.col + music_offset - 3)+12*(MATRIX_ROWS - record->event.key.row);
139       #elif defined(MUSIC_MODE_GUITAR)
140       uint8_t note = (music_starting_note + record->event.key.col + music_offset + 32)+5*(MATRIX_ROWS - record->event.key.row);
141       #elif defined(MUSIC_MODE_VIOLIN)
142       uint8_t note = (music_starting_note + record->event.key.col + music_offset + 32)+7*(MATRIX_ROWS - record->event.key.row);
143       #else
144       uint8_t note = (music_starting_note + SCALE[record->event.key.col + music_offset] - 3)+12*(MATRIX_ROWS - record->event.key.row);
145       #endif
146
147       if (record->event.pressed) {
148         music_noteon(note);
149         if (music_sequence_recording) {
150           music_sequence[music_sequence_count] = note;
151           music_sequence_count++;
152         }
153       } else {
154         music_noteoff(note);
155       }
156
157       if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through
158         return false;
159     }
160
161     return true;
162 }
163
164 bool is_music_on(void) {
165     return (music_activated != 0);
166 }
167
168 void music_toggle(void) {
169     if (!music_activated) {
170         music_on();
171     } else {
172         music_off();
173     }
174 }
175
176 void music_on(void) {
177     music_activated = 1;
178     music_on_user();
179 }
180
181 void music_off(void) {
182     music_activated = 0;
183     music_all_notes_off();
184 }
185
186 void matrix_scan_music(void) {
187   if (music_sequence_playing) {
188     if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) {
189       music_sequence_timer = timer_read();
190       uint8_t prev_note = music_sequence[(music_sequence_position - 1 < 0)?(music_sequence_position - 1 + music_sequence_count):(music_sequence_position - 1)];
191       uint8_t next_note = music_sequence[music_sequence_position];
192       music_noteoff(prev_note);
193       music_noteon(next_note);
194       music_sequence_position = (music_sequence_position + 1) % music_sequence_count;
195     }
196   }
197 }
198
199 __attribute__ ((weak))
200 void music_on_user() {}
201
202 __attribute__ ((weak))
203 void music_scale_user() {}
204
205 #endif // defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))