aboutsummaryrefslogtreecommitdiff
path: root/quantum/process_keycode
diff options
context:
space:
mode:
authorJack Humbert <jack.humb@gmail.com>2017-03-28 09:26:54 -0400
committerGitHub <noreply@github.com>2017-03-28 09:26:54 -0400
commit7e37daa2ce6edad82de3835384176b51a8081537 (patch)
tree58f349edb063667c9ae0affd99cec2bf7a7f93cb /quantum/process_keycode
parent216f669276b30393fb35a409011ccdad8b521156 (diff)
parent0734f569409974624b40735fcd498dac9adba2d2 (diff)
downloadqmk_firmware-7e37daa2ce6edad82de3835384176b51a8081537.tar.gz
qmk_firmware-7e37daa2ce6edad82de3835384176b51a8081537.zip
Merge pull request #1112 from newsboytko/newsboytko/midi-keycodes
Flesh out MIDI support
Diffstat (limited to 'quantum/process_keycode')
-rw-r--r--quantum/process_keycode/process_audio.c62
-rw-r--r--quantum/process_keycode/process_audio.h11
-rw-r--r--quantum/process_keycode/process_midi.c292
-rw-r--r--quantum/process_keycode/process_midi.h225
-rw-r--r--quantum/process_keycode/process_music.c118
-rw-r--r--quantum/process_keycode/process_music.h6
6 files changed, 399 insertions, 315 deletions
diff --git a/quantum/process_keycode/process_audio.c b/quantum/process_keycode/process_audio.c
new file mode 100644
index 000000000..0b6380ed3
--- /dev/null
+++ b/quantum/process_keycode/process_audio.c
@@ -0,0 +1,62 @@
1#include "audio.h"
2#include "process_audio.h"
3
4static float compute_freq_for_midi_note(uint8_t note)
5{
6 // https://en.wikipedia.org/wiki/MIDI_tuning_standard
7 return pow(2.0, (note - 69) / 12.0) * 440.0f;
8}
9
10bool process_audio(uint16_t keycode, keyrecord_t *record) {
11
12 if (keycode == AU_ON && record->event.pressed) {
13 audio_on();
14 return false;
15 }
16
17 if (keycode == AU_OFF && record->event.pressed) {
18 audio_off();
19 return false;
20 }
21
22 if (keycode == AU_TOG && record->event.pressed) {
23 if (is_audio_on())
24 {
25 audio_off();
26 }
27 else
28 {
29 audio_on();
30 }
31 return false;
32 }
33
34 if (keycode == MUV_IN && record->event.pressed) {
35 voice_iterate();
36 music_scale_user();
37 return false;
38 }
39
40 if (keycode == MUV_DE && record->event.pressed) {
41 voice_deiterate();
42 music_scale_user();
43 return false;
44 }
45
46 return true;
47}
48
49void process_audio_noteon(uint8_t note) {
50 play_note(compute_freq_for_midi_note(note), 0xF);
51}
52
53void process_audio_noteoff(uint8_t note) {
54 stop_note(compute_freq_for_midi_note(note));
55}
56
57void process_audio_all_notes_off(void) {
58 stop_all_notes();
59}
60
61__attribute__ ((weak))
62void audio_on_user() {} \ No newline at end of file
diff --git a/quantum/process_keycode/process_audio.h b/quantum/process_keycode/process_audio.h
new file mode 100644
index 000000000..7ac15b733
--- /dev/null
+++ b/quantum/process_keycode/process_audio.h
@@ -0,0 +1,11 @@
1#ifndef PROCESS_AUDIO_H
2#define PROCESS_AUDIO_H
3
4bool process_audio(uint16_t keycode, keyrecord_t *record);
5void process_audio_noteon(uint8_t note);
6void process_audio_noteoff(uint8_t note);
7void process_audio_all_notes_off(void);
8
9void audio_on_user(void);
10
11#endif \ No newline at end of file
diff --git a/quantum/process_keycode/process_midi.c b/quantum/process_keycode/process_midi.c
index 577dad43a..700c6ce8e 100644
--- a/quantum/process_keycode/process_midi.c
+++ b/quantum/process_keycode/process_midi.c
@@ -1,68 +1,238 @@
1#include "process_midi.h" 1#include "process_midi.h"
2 2
3bool midi_activated = false; 3#ifdef MIDI_ENABLE
4uint8_t midi_starting_note = 0x0C; 4#include "midi.h"
5int midi_offset = 7; 5
6 6#ifdef MIDI_BASIC
7bool process_midi(uint16_t keycode, keyrecord_t *record) { 7
8 if (keycode == MI_ON && record->event.pressed) { 8void process_midi_basic_noteon(uint8_t note)
9 midi_activated = true; 9{
10#ifdef AUDIO_ENABLE 10 midi_send_noteon(&midi_device, 0, note, 128);
11 music_scale_user(); 11}
12#endif 12
13 return false; 13void process_midi_basic_noteoff(uint8_t note)
14 } 14{
15 midi_send_noteoff(&midi_device, 0, note, 0);
16}
17
18void process_midi_all_notes_off(void)
19{
20 midi_send_cc(&midi_device, 0, 0x7B, 0);
21}
22
23#endif // MIDI_BASIC
24
25#ifdef MIDI_ADVANCED
26
27#include "timer.h"
28
29static uint8_t tone_status[MIDI_TONE_COUNT];
30
31static uint8_t midi_modulation;
32static int8_t midi_modulation_step;
33static uint16_t midi_modulation_timer;
34
35inline uint8_t compute_velocity(uint8_t setting)
36{
37 return (setting + 1) * (128 / (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN + 1));
38}
15 39
16 if (keycode == MI_OFF && record->event.pressed) { 40void midi_init(void)
17 midi_activated = false; 41{
18 midi_send_cc(&midi_device, 0, 0x7B, 0); 42 midi_config.octave = MI_OCT_2 - MIDI_OCTAVE_MIN;
19 return false; 43 midi_config.transpose = 0;
44 midi_config.velocity = (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN);
45 midi_config.channel = 0;
46 midi_config.modulation_interval = 8;
47
48 for (uint8_t i = 0; i < MIDI_TONE_COUNT; i++)
49 {
50 tone_status[i] = MIDI_INVALID_NOTE;
20 } 51 }
21 52
22 if (midi_activated) { 53 midi_modulation = 0;
23 if (record->event.key.col == (MATRIX_COLS - 1) && record->event.key.row == (MATRIX_ROWS - 1)) { 54 midi_modulation_step = 0;
24 if (record->event.pressed) { 55 midi_modulation_timer = 0;
25 midi_starting_note++; // Change key 56}
26 midi_send_cc(&midi_device, 0, 0x7B, 0); 57
27 } 58void midi_task(void)
28 return false; 59{
29 } 60 if (timer_elapsed(midi_modulation_timer) < midi_config.modulation_interval)
30 if (record->event.key.col == (MATRIX_COLS - 2) && record->event.key.row == (MATRIX_ROWS - 1)) { 61 return;
31 if (record->event.pressed) { 62 midi_modulation_timer = timer_read();
32 midi_starting_note--; // Change key 63
33 midi_send_cc(&midi_device, 0, 0x7B, 0); 64 if (midi_modulation_step != 0)
34 } 65 {
35 return false; 66 dprintf("midi modulation %d\n", midi_modulation);
36 } 67 midi_send_cc(&midi_device, midi_config.channel, 0x1, midi_modulation);
37 if (record->event.key.col == (MATRIX_COLS - 3) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) { 68
38 midi_offset++; // Change scale 69 if (midi_modulation_step < 0 && midi_modulation < -midi_modulation_step) {
39 midi_send_cc(&midi_device, 0, 0x7B, 0); 70 midi_modulation = 0;
40 return false; 71 midi_modulation_step = 0;
41 } 72 return;
42 if (record->event.key.col == (MATRIX_COLS - 4) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) { 73 }
43 midi_offset--; // Change scale 74
44 midi_send_cc(&midi_device, 0, 0x7B, 0); 75 midi_modulation += midi_modulation_step;
45 return false; 76
46 } 77 if (midi_modulation > 127)
47 // basic 78 midi_modulation = 127;
48 // uint8_t note = (midi_starting_note + SCALE[record->event.key.col + midi_offset])+12*(MATRIX_ROWS - record->event.key.row);
49 // advanced
50 // uint8_t note = (midi_starting_note + record->event.key.col + midi_offset)+12*(MATRIX_ROWS - record->event.key.row);
51 // guitar
52 uint8_t note = (midi_starting_note + record->event.key.col + midi_offset)+5*(MATRIX_ROWS - record->event.key.row);
53 // violin
54 // uint8_t note = (midi_starting_note + record->event.key.col + midi_offset)+7*(MATRIX_ROWS - record->event.key.row);
55
56 if (record->event.pressed) {
57 // midi_send_noteon(&midi_device, record->event.key.row, midi_starting_note + SCALE[record->event.key.col], 127);
58 midi_send_noteon(&midi_device, 0, note, 127);
59 } else {
60 // midi_send_noteoff(&midi_device, record->event.key.row, midi_starting_note + SCALE[record->event.key.col], 127);
61 midi_send_noteoff(&midi_device, 0, note, 127);
62 }
63
64 if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through
65 return false;
66 } 79 }
67 return true;
68} 80}
81
82uint8_t midi_compute_note(uint16_t keycode)
83{
84 return 12 * midi_config.octave + (keycode - MIDI_TONE_MIN) + midi_config.transpose;
85}
86
87bool process_midi(uint16_t keycode, keyrecord_t *record)
88{
89 switch (keycode) {
90 case MIDI_TONE_MIN ... MIDI_TONE_MAX:
91 {
92 uint8_t channel = midi_config.channel;
93 uint8_t tone = keycode - MIDI_TONE_MIN;
94 uint8_t velocity = compute_velocity(midi_config.velocity);
95 if (record->event.pressed) {
96 uint8_t note = midi_compute_note(keycode);
97 midi_send_noteon(&midi_device, channel, note, velocity);
98 dprintf("midi noteon channel:%d note:%d velocity:%d\n", channel, note, velocity);
99 tone_status[tone] = note;
100 }
101 else {
102 uint8_t note = tone_status[tone];
103 if (note != MIDI_INVALID_NOTE)
104 {
105 midi_send_noteoff(&midi_device, channel, note, velocity);
106 dprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, velocity);
107 }
108 tone_status[tone] = MIDI_INVALID_NOTE;
109 }
110 return false;
111 }
112 case MIDI_OCTAVE_MIN ... MIDI_OCTAVE_MAX:
113 if (record->event.pressed) {
114 midi_config.octave = keycode - MIDI_OCTAVE_MIN;
115 dprintf("midi octave %d\n", midi_config.octave);
116 }
117 return false;
118 case MI_OCTD:
119 if (record->event.pressed && midi_config.octave > 0) {
120 midi_config.octave--;
121 dprintf("midi octave %d\n", midi_config.octave);
122 }
123 return false;
124 case MI_OCTU:
125 if (record->event.pressed && midi_config.octave < (MIDI_OCTAVE_MAX - MIDI_OCTAVE_MIN)) {
126 midi_config.octave++;
127 dprintf("midi octave %d\n", midi_config.octave);
128 }
129 return false;
130 case MIDI_TRANSPOSE_MIN ... MIDI_TRANSPOSE_MAX:
131 if (record->event.pressed) {
132 midi_config.transpose = keycode - MI_TRNS_0;
133 dprintf("midi transpose %d\n", midi_config.transpose);
134 }
135 return false;
136 case MI_TRNSD:
137 if (record->event.pressed && midi_config.transpose > (MIDI_TRANSPOSE_MIN - MI_TRNS_0)) {
138 midi_config.transpose--;
139 dprintf("midi transpose %d\n", midi_config.transpose);
140 }
141 return false;
142 case MI_TRNSU:
143 if (record->event.pressed && midi_config.transpose < (MIDI_TRANSPOSE_MAX - MI_TRNS_0)) {
144 const bool positive = midi_config.transpose > 0;
145 midi_config.transpose++;
146 if (positive && midi_config.transpose < 0)
147 midi_config.transpose--;
148 dprintf("midi transpose %d\n", midi_config.transpose);
149 }
150 return false;
151 case MIDI_VELOCITY_MIN ... MIDI_VELOCITY_MAX:
152 if (record->event.pressed) {
153 midi_config.velocity = keycode - MIDI_VELOCITY_MIN;
154 dprintf("midi velocity %d\n", midi_config.velocity);
155 }
156 return false;
157 case MI_VELD:
158 if (record->event.pressed && midi_config.velocity > 0) {
159 midi_config.velocity--;
160 dprintf("midi velocity %d\n", midi_config.velocity);
161 }
162 return false;
163 case MI_VELU:
164 if (record->event.pressed) {
165 midi_config.velocity++;
166 dprintf("midi velocity %d\n", midi_config.velocity);
167 }
168 return false;
169 case MIDI_CHANNEL_MIN ... MIDI_CHANNEL_MAX:
170 if (record->event.pressed) {
171 midi_config.channel = keycode - MIDI_CHANNEL_MIN;
172 dprintf("midi channel %d\n", midi_config.channel);
173 }
174 return false;
175 case MI_CHD:
176 if (record->event.pressed) {
177 midi_config.channel--;
178 dprintf("midi channel %d\n", midi_config.channel);
179 }
180 return false;
181 case MI_CHU:
182 if (record->event.pressed) {
183 midi_config.channel++;
184 dprintf("midi channel %d\n", midi_config.channel);
185 }
186 return false;
187 case MI_ALLOFF:
188 if (record->event.pressed) {
189 midi_send_cc(&midi_device, midi_config.channel, 0x7B, 0);
190 dprintf("midi all notes off\n");
191 }
192 return false;
193 case MI_SUS:
194 midi_send_cc(&midi_device, midi_config.channel, 0x40, record->event.pressed ? 127 : 0);
195 dprintf("midi sustain %d\n", record->event.pressed);
196 return false;
197 case MI_PORT:
198 midi_send_cc(&midi_device, midi_config.channel, 0x41, record->event.pressed ? 127 : 0);
199 dprintf("midi portamento %d\n", record->event.pressed);
200 return false;
201 case MI_SOST:
202 midi_send_cc(&midi_device, midi_config.channel, 0x42, record->event.pressed ? 127 : 0);
203 dprintf("midi sostenuto %d\n", record->event.pressed);
204 return false;
205 case MI_SOFT:
206 midi_send_cc(&midi_device, midi_config.channel, 0x43, record->event.pressed ? 127 : 0);
207 dprintf("midi soft %d\n", record->event.pressed);
208 return false;
209 case MI_LEG:
210 midi_send_cc(&midi_device, midi_config.channel, 0x43, record->event.pressed ? 127 : 0);
211 dprintf("midi legato %d\n", record->event.pressed);
212 return false;
213 case MI_MOD:
214 midi_modulation_step = record->event.pressed ? 1 : -1;
215 return false;
216 case MI_MODSD:
217 if (record->event.pressed) {
218 midi_config.modulation_interval++;
219 // prevent overflow
220 if (midi_config.modulation_interval == 0)
221 midi_config.modulation_interval--;
222 dprintf("midi modulation interval %d\n", midi_config.modulation_interval);
223 }
224 return false;
225 case MI_MODSU:
226 if (record->event.pressed && midi_config.modulation_interval > 0) {
227 midi_config.modulation_interval--;
228 dprintf("midi modulation interval %d\n", midi_config.modulation_interval);
229 }
230 return false;
231 };
232
233 return true;
234}
235
236#endif // MIDI_ADVANCED
237
238#endif // MIDI_ENABLE
diff --git a/quantum/process_keycode/process_midi.h b/quantum/process_keycode/process_midi.h
index acd4fc1b1..58b7650c6 100644
--- a/quantum/process_keycode/process_midi.h
+++ b/quantum/process_keycode/process_midi.h
@@ -3,205 +3,38 @@
3 3
4#include "quantum.h" 4#include "quantum.h"
5 5
6bool process_midi(uint16_t keycode, keyrecord_t *record); 6#ifdef MIDI_ENABLE
7
8#ifdef MIDI_BASIC
9void process_midi_basic_noteon(uint8_t note);
10void process_midi_basic_noteoff(uint8_t note);
11void process_midi_all_notes_off(void);
12#endif
13
14#ifdef MIDI_ADVANCED
15typedef union {
16 uint32_t raw;
17 struct {
18 uint8_t octave :4;
19 int8_t transpose :4;
20 uint8_t velocity :4;
21 uint8_t channel :4;
22 uint8_t modulation_interval :4;
23 };
24} midi_config_t;
7 25
8#define MIDI(n) ((n) | 0x6000) 26midi_config_t midi_config;
9#define MIDI12 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000, 0x6000 27
28void midi_init(void);
29void midi_task(void);
30bool process_midi(uint16_t keycode, keyrecord_t *record);
10 31
11#define CHNL(note, channel) (note + (channel << 8)) 32#define MIDI_INVALID_NOTE 0xFF
33#define MIDI_TONE_COUNT (MIDI_TONE_MAX - MIDI_TONE_MIN + 1)
12 34
13#define SCALE (int8_t []){ 0 + (12*0), 2 + (12*0), 4 + (12*0), 5 + (12*0), 7 + (12*0), 9 + (12*0), 11 + (12*0), \ 35uint8_t midi_compute_note(uint16_t keycode);
14 0 + (12*1), 2 + (12*1), 4 + (12*1), 5 + (12*1), 7 + (12*1), 9 + (12*1), 11 + (12*1), \ 36#endif // MIDI_ADVANCED
15 0 + (12*2), 2 + (12*2), 4 + (12*2), 5 + (12*2), 7 + (12*2), 9 + (12*2), 11 + (12*2), \
16 0 + (12*3), 2 + (12*3), 4 + (12*3), 5 + (12*3), 7 + (12*3), 9 + (12*3), 11 + (12*3), \
17 0 + (12*4), 2 + (12*4), 4 + (12*4), 5 + (12*4), 7 + (12*4), 9 + (12*4), 11 + (12*4), }
18 37
19#define N_CN1 (0x600C + (12 * -1) + 0 ) 38#endif // MIDI_ENABLE
20#define N_CN1S (0x600C + (12 * -1) + 1 )
21#define N_DN1F (0x600C + (12 * -1) + 1 )
22#define N_DN1 (0x600C + (12 * -1) + 2 )
23#define N_DN1S (0x600C + (12 * -1) + 3 )
24#define N_EN1F (0x600C + (12 * -1) + 3 )
25#define N_EN1 (0x600C + (12 * -1) + 4 )
26#define N_FN1 (0x600C + (12 * -1) + 5 )
27#define N_FN1S (0x600C + (12 * -1) + 6 )
28#define N_GN1F (0x600C + (12 * -1) + 6 )
29#define N_GN1 (0x600C + (12 * -1) + 7 )
30#define N_GN1S (0x600C + (12 * -1) + 8 )
31#define N_AN1F (0x600C + (12 * -1) + 8 )
32#define N_AN1 (0x600C + (12 * -1) + 9 )
33#define N_AN1S (0x600C + (12 * -1) + 10)
34#define N_BN1F (0x600C + (12 * -1) + 10)
35#define N_BN1 (0x600C + (12 * -1) + 11)
36#define N_C0 (0x600C + (12 * 0) + 0 )
37#define N_C0S (0x600C + (12 * 0) + 1 )
38#define N_D0F (0x600C + (12 * 0) + 1 )
39#define N_D0 (0x600C + (12 * 0) + 2 )
40#define N_D0S (0x600C + (12 * 0) + 3 )
41#define N_E0F (0x600C + (12 * 0) + 3 )
42#define N_E0 (0x600C + (12 * 0) + 4 )
43#define N_F0 (0x600C + (12 * 0) + 5 )
44#define N_F0S (0x600C + (12 * 0) + 6 )
45#define N_G0F (0x600C + (12 * 0) + 6 )
46#define N_G0 (0x600C + (12 * 0) + 7 )
47#define N_G0S (0x600C + (12 * 0) + 8 )
48#define N_A0F (0x600C + (12 * 0) + 8 )
49#define N_A0 (0x600C + (12 * 0) + 9 )
50#define N_A0S (0x600C + (12 * 0) + 10)
51#define N_B0F (0x600C + (12 * 0) + 10)
52#define N_B0 (0x600C + (12 * 0) + 11)
53#define N_C1 (0x600C + (12 * 1) + 0 )
54#define N_C1S (0x600C + (12 * 1) + 1 )
55#define N_D1F (0x600C + (12 * 1) + 1 )
56#define N_D1 (0x600C + (12 * 1) + 2 )
57#define N_D1S (0x600C + (12 * 1) + 3 )
58#define N_E1F (0x600C + (12 * 1) + 3 )
59#define N_E1 (0x600C + (12 * 1) + 4 )
60#define N_F1 (0x600C + (12 * 1) + 5 )
61#define N_F1S (0x600C + (12 * 1) + 6 )
62#define N_G1F (0x600C + (12 * 1) + 6 )
63#define N_G1 (0x600C + (12 * 1) + 7 )
64#define N_G1S (0x600C + (12 * 1) + 8 )
65#define N_A1F (0x600C + (12 * 1) + 8 )
66#define N_A1 (0x600C + (12 * 1) + 9 )
67#define N_A1S (0x600C + (12 * 1) + 10)
68#define N_B1F (0x600C + (12 * 1) + 10)
69#define N_B1 (0x600C + (12 * 1) + 11)
70#define N_C2 (0x600C + (12 * 2) + 0 )
71#define N_C2S (0x600C + (12 * 2) + 1 )
72#define N_D2F (0x600C + (12 * 2) + 1 )
73#define N_D2 (0x600C + (12 * 2) + 2 )
74#define N_D2S (0x600C + (12 * 2) + 3 )
75#define N_E2F (0x600C + (12 * 2) + 3 )
76#define N_E2 (0x600C + (12 * 2) + 4 )
77#define N_F2 (0x600C + (12 * 2) + 5 )
78#define N_F2S (0x600C + (12 * 2) + 6 )
79#define N_G2F (0x600C + (12 * 2) + 6 )
80#define N_G2 (0x600C + (12 * 2) + 7 )
81#define N_G2S (0x600C + (12 * 2) + 8 )
82#define N_A2F (0x600C + (12 * 2) + 8 )
83#define N_A2 (0x600C + (12 * 2) + 9 )
84#define N_A2S (0x600C + (12 * 2) + 10)
85#define N_B2F (0x600C + (12 * 2) + 10)
86#define N_B2 (0x600C + (12 * 2) + 11)
87#define N_C3 (0x600C + (12 * 3) + 0 )
88#define N_C3S (0x600C + (12 * 3) + 1 )
89#define N_D3F (0x600C + (12 * 3) + 1 )
90#define N_D3 (0x600C + (12 * 3) + 2 )
91#define N_D3S (0x600C + (12 * 3) + 3 )
92#define N_E3F (0x600C + (12 * 3) + 3 )
93#define N_E3 (0x600C + (12 * 3) + 4 )
94#define N_F3 (0x600C + (12 * 3) + 5 )
95#define N_F3S (0x600C + (12 * 3) + 6 )
96#define N_G3F (0x600C + (12 * 3) + 6 )
97#define N_G3 (0x600C + (12 * 3) + 7 )
98#define N_G3S (0x600C + (12 * 3) + 8 )
99#define N_A3F (0x600C + (12 * 3) + 8 )
100#define N_A3 (0x600C + (12 * 3) + 9 )
101#define N_A3S (0x600C + (12 * 3) + 10)
102#define N_B3F (0x600C + (12 * 3) + 10)
103#define N_B3 (0x600C + (12 * 3) + 11)
104#define N_C4 (0x600C + (12 * 4) + 0 )
105#define N_C4S (0x600C + (12 * 4) + 1 )
106#define N_D4F (0x600C + (12 * 4) + 1 )
107#define N_D4 (0x600C + (12 * 4) + 2 )
108#define N_D4S (0x600C + (12 * 4) + 3 )
109#define N_E4F (0x600C + (12 * 4) + 3 )
110#define N_E4 (0x600C + (12 * 4) + 4 )
111#define N_F4 (0x600C + (12 * 4) + 5 )
112#define N_F4S (0x600C + (12 * 4) + 6 )
113#define N_G4F (0x600C + (12 * 4) + 6 )
114#define N_G4 (0x600C + (12 * 4) + 7 )
115#define N_G4S (0x600C + (12 * 4) + 8 )
116#define N_A4F (0x600C + (12 * 4) + 8 )
117#define N_A4 (0x600C + (12 * 4) + 9 )
118#define N_A4S (0x600C + (12 * 4) + 10)
119#define N_B4F (0x600C + (12 * 4) + 10)
120#define N_B4 (0x600C + (12 * 4) + 11)
121#define N_C5 (0x600C + (12 * 5) + 0 )
122#define N_C5S (0x600C + (12 * 5) + 1 )
123#define N_D5F (0x600C + (12 * 5) + 1 )
124#define N_D5 (0x600C + (12 * 5) + 2 )
125#define N_D5S (0x600C + (12 * 5) + 3 )
126#define N_E5F (0x600C + (12 * 5) + 3 )
127#define N_E5 (0x600C + (12 * 5) + 4 )
128#define N_F5 (0x600C + (12 * 5) + 5 )
129#define N_F5S (0x600C + (12 * 5) + 6 )
130#define N_G5F (0x600C + (12 * 5) + 6 )
131#define N_G5 (0x600C + (12 * 5) + 7 )
132#define N_G5S (0x600C + (12 * 5) + 8 )
133#define N_A5F (0x600C + (12 * 5) + 8 )
134#define N_A5 (0x600C + (12 * 5) + 9 )
135#define N_A5S (0x600C + (12 * 5) + 10)
136#define N_B5F (0x600C + (12 * 5) + 10)
137#define N_B5 (0x600C + (12 * 5) + 11)
138#define N_C6 (0x600C + (12 * 6) + 0 )
139#define N_C6S (0x600C + (12 * 6) + 1 )
140#define N_D6F (0x600C + (12 * 6) + 1 )
141#define N_D6 (0x600C + (12 * 6) + 2 )
142#define N_D6S (0x600C + (12 * 6) + 3 )
143#define N_E6F (0x600C + (12 * 6) + 3 )
144#define N_E6 (0x600C + (12 * 6) + 4 )
145#define N_F6 (0x600C + (12 * 6) + 5 )
146#define N_F6S (0x600C + (12 * 6) + 6 )
147#define N_G6F (0x600C + (12 * 6) + 6 )
148#define N_G6 (0x600C + (12 * 6) + 7 )
149#define N_G6S (0x600C + (12 * 6) + 8 )
150#define N_A6F (0x600C + (12 * 6) + 8 )
151#define N_A6 (0x600C + (12 * 6) + 9 )
152#define N_A6S (0x600C + (12 * 6) + 10)
153#define N_B6F (0x600C + (12 * 6) + 10)
154#define N_B6 (0x600C + (12 * 6) + 11)
155#define N_C7 (0x600C + (12 * 7) + 0 )
156#define N_C7S (0x600C + (12 * 7) + 1 )
157#define N_D7F (0x600C + (12 * 7) + 1 )
158#define N_D7 (0x600C + (12 * 7) + 2 )
159#define N_D7S (0x600C + (12 * 7) + 3 )
160#define N_E7F (0x600C + (12 * 7) + 3 )
161#define N_E7 (0x600C + (12 * 7) + 4 )
162#define N_F7 (0x600C + (12 * 7) + 5 )
163#define N_F7S (0x600C + (12 * 7) + 6 )
164#define N_G7F (0x600C + (12 * 7) + 6 )
165#define N_G7 (0x600C + (12 * 7) + 7 )
166#define N_G7S (0x600C + (12 * 7) + 8 )
167#define N_A7F (0x600C + (12 * 7) + 8 )
168#define N_A7 (0x600C + (12 * 7) + 9 )
169#define N_A7S (0x600C + (12 * 7) + 10)
170#define N_B7F (0x600C + (12 * 7) + 10)
171#define N_B7 (0x600C + (12 * 7) + 11)
172#define N_C8 (0x600C + (12 * 8) + 0 )
173#define N_C8S (0x600C + (12 * 8) + 1 )
174#define N_D8F (0x600C + (12 * 8) + 1 )
175#define N_D8 (0x600C + (12 * 8) + 2 )
176#define N_D8S (0x600C + (12 * 8) + 3 )
177#define N_E8F (0x600C + (12 * 8) + 3 )
178#define N_E8 (0x600C + (12 * 8) + 4 )
179#define N_F8 (0x600C + (12 * 8) + 5 )
180#define N_F8S (0x600C + (12 * 8) + 6 )
181#define N_G8F (0x600C + (12 * 8) + 6 )
182#define N_G8 (0x600C + (12 * 8) + 7 )
183#define N_G8S (0x600C + (12 * 8) + 8 )
184#define N_A8F (0x600C + (12 * 8) + 8 )
185#define N_A8 (0x600C + (12 * 8) + 9 )
186#define N_A8S (0x600C + (12 * 8) + 10)
187#define N_B8F (0x600C + (12 * 8) + 10)
188#define N_B8 (0x600C + (12 * 8) + 11)
189#define N_C8 (0x600C + (12 * 8) + 0 )
190#define N_C8S (0x600C + (12 * 8) + 1 )
191#define N_D8F (0x600C + (12 * 8) + 1 )
192#define N_D8 (0x600C + (12 * 8) + 2 )
193#define N_D8S (0x600C + (12 * 8) + 3 )
194#define N_E8F (0x600C + (12 * 8) + 3 )
195#define N_E8 (0x600C + (12 * 8) + 4 )
196#define N_F8 (0x600C + (12 * 8) + 5 )
197#define N_F8S (0x600C + (12 * 8) + 6 )
198#define N_G8F (0x600C + (12 * 8) + 6 )
199#define N_G8 (0x600C + (12 * 8) + 7 )
200#define N_G8S (0x600C + (12 * 8) + 8 )
201#define N_A8F (0x600C + (12 * 8) + 8 )
202#define N_A8 (0x600C + (12 * 8) + 9 )
203#define N_A8S (0x600C + (12 * 8) + 10)
204#define N_B8F (0x600C + (12 * 8) + 10)
205#define N_B8 (0x600C + (12 * 8) + 11)
206 39
207#endif \ No newline at end of file 40#endif \ No newline at end of file
diff --git a/quantum/process_keycode/process_music.c b/quantum/process_keycode/process_music.c
index 1e2648bff..f89a04ee3 100644
--- a/quantum/process_keycode/process_music.c
+++ b/quantum/process_keycode/process_music.c
@@ -1,5 +1,14 @@
1#include "process_music.h" 1#include "process_music.h"
2 2
3#ifdef AUDIO_ENABLE
4#include "process_audio.h"
5#endif
6#if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
7#include "process_midi.h"
8#endif
9
10#if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
11
3bool music_activated = false; 12bool music_activated = false;
4uint8_t music_starting_note = 0x0C; 13uint8_t music_starting_note = 0x0C;
5int music_offset = 7; 14int music_offset = 7;
@@ -8,36 +17,41 @@ int music_offset = 7;
8static bool music_sequence_recording = false; 17static bool music_sequence_recording = false;
9static bool music_sequence_recorded = false; 18static bool music_sequence_recorded = false;
10static bool music_sequence_playing = false; 19static bool music_sequence_playing = false;
11static float music_sequence[16] = {0}; 20static uint8_t music_sequence[16] = {0};
12static uint8_t music_sequence_count = 0; 21static uint8_t music_sequence_count = 0;
13static uint8_t music_sequence_position = 0; 22static uint8_t music_sequence_position = 0;
14 23
15static uint16_t music_sequence_timer = 0; 24static uint16_t music_sequence_timer = 0;
16static uint16_t music_sequence_interval = 100; 25static uint16_t music_sequence_interval = 100;
17 26
18bool process_music(uint16_t keycode, keyrecord_t *record) { 27static void music_noteon(uint8_t note) {
28 #ifdef AUDIO_ENABLE
29 process_audio_noteon(note);
30 #endif
31 #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
32 process_midi_basic_noteon(note);
33 #endif
34}
19 35
20 if (keycode == AU_ON && record->event.pressed) { 36static void music_noteoff(uint8_t note) {
21 audio_on(); 37 #ifdef AUDIO_ENABLE
22 return false; 38 process_audio_noteoff(note);
23 } 39 #endif
40 #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
41 process_midi_basic_noteoff(note);
42 #endif
43}
24 44
25 if (keycode == AU_OFF && record->event.pressed) { 45void music_all_notes_off(void) {
26 audio_off(); 46 #ifdef AUDIO_ENABLE
27 return false; 47 process_audio_all_notes_off();
28 } 48 #endif
49 #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
50 process_midi_all_notes_off();
51 #endif
52}
29 53
30 if (keycode == AU_TOG && record->event.pressed) { 54bool process_music(uint16_t keycode, keyrecord_t *record) {
31 if (is_audio_on())
32 {
33 audio_off();
34 }
35 else
36 {
37 audio_on();
38 }
39 return false;
40 }
41 55
42 if (keycode == MU_ON && record->event.pressed) { 56 if (keycode == MU_ON && record->event.pressed) {
43 music_on(); 57 music_on();
@@ -61,22 +75,10 @@ bool process_music(uint16_t keycode, keyrecord_t *record) {
61 return false; 75 return false;
62 } 76 }
63 77
64 if (keycode == MUV_IN && record->event.pressed) {
65 voice_iterate();
66 music_scale_user();
67 return false;
68 }
69
70 if (keycode == MUV_DE && record->event.pressed) {
71 voice_deiterate();
72 music_scale_user();
73 return false;
74 }
75
76 if (music_activated) { 78 if (music_activated) {
77 79
78 if (keycode == KC_LCTL && record->event.pressed) { // Start recording 80 if (keycode == KC_LCTL && record->event.pressed) { // Start recording
79 stop_all_notes(); 81 music_all_notes_off();
80 music_sequence_recording = true; 82 music_sequence_recording = true;
81 music_sequence_recorded = false; 83 music_sequence_recorded = false;
82 music_sequence_playing = false; 84 music_sequence_playing = false;
@@ -85,7 +87,7 @@ bool process_music(uint16_t keycode, keyrecord_t *record) {
85 } 87 }
86 88
87 if (keycode == KC_LALT && record->event.pressed) { // Stop recording/playing 89 if (keycode == KC_LALT && record->event.pressed) { // Stop recording/playing
88 stop_all_notes(); 90 music_all_notes_off();
89 if (music_sequence_recording) { // was recording 91 if (music_sequence_recording) { // was recording
90 music_sequence_recorded = true; 92 music_sequence_recorded = true;
91 } 93 }
@@ -95,7 +97,7 @@ bool process_music(uint16_t keycode, keyrecord_t *record) {
95 } 97 }
96 98
97 if (keycode == KC_LGUI && record->event.pressed && music_sequence_recorded) { // Start playing 99 if (keycode == KC_LGUI && record->event.pressed && music_sequence_recorded) { // Start playing
98 stop_all_notes(); 100 music_all_notes_off();
99 music_sequence_recording = false; 101 music_sequence_recording = false;
100 music_sequence_playing = true; 102 music_sequence_playing = true;
101 music_sequence_position = 0; 103 music_sequence_position = 0;
@@ -114,32 +116,34 @@ bool process_music(uint16_t keycode, keyrecord_t *record) {
114 music_sequence_interval+=10; 116 music_sequence_interval+=10;
115 return false; 117 return false;
116 } 118 }
119
117 #define MUSIC_MODE_GUITAR 120 #define MUSIC_MODE_GUITAR
118 121
119 #ifdef MUSIC_MODE_CHROMATIC 122 #ifdef MUSIC_MODE_CHROMATIC
120 float freq = ((float)220.0)*pow(2.0, -5.0)*pow(2.0,(music_starting_note + record->event.key.col + music_offset)/12.0+(MATRIX_ROWS - record->event.key.row)); 123 uint8_t note = (music_starting_note + record->event.key.col + music_offset - 3)+12*(MATRIX_ROWS - record->event.key.row);
121 #elif defined(MUSIC_MODE_GUITAR) 124 #elif defined(MUSIC_MODE_GUITAR)
122 float freq = ((float)220.0)*pow(2.0, -5.0)*pow(2.0,(music_starting_note + record->event.key.col + music_offset)/12.0+(float)(MATRIX_ROWS - record->event.key.row + 7)*5.0/12); 125 uint8_t note = (music_starting_note + record->event.key.col + music_offset + 32)+5*(MATRIX_ROWS - record->event.key.row);
123 #elif defined(MUSIC_MODE_VIOLIN) 126 #elif defined(MUSIC_MODE_VIOLIN)
124 float freq = ((float)220.0)*pow(2.0, -5.0)*pow(2.0,(music_starting_note + record->event.key.col + music_offset)/12.0+(float)(MATRIX_ROWS - record->event.key.row + 5)*7.0/12); 127 uint8_t note = (music_starting_note + record->event.key.col + music_offset + 32)+7*(MATRIX_ROWS - record->event.key.row);
125 #else 128 #else
126 float freq = ((float)220.0)*pow(2.0, -5.0)*pow(2.0,(music_starting_note + SCALE[record->event.key.col + music_offset])/12.0+(MATRIX_ROWS - record->event.key.row)); 129 uint8_t note = (music_starting_note + SCALE[record->event.key.col + music_offset] - 3)+12*(MATRIX_ROWS - record->event.key.row);
127 #endif 130 #endif
128 131
129 if (record->event.pressed) { 132 if (record->event.pressed) {
130 play_note(freq, 0xF); 133 music_noteon(note);
131 if (music_sequence_recording) { 134 if (music_sequence_recording) {
132 music_sequence[music_sequence_count] = freq; 135 music_sequence[music_sequence_count] = note;
133 music_sequence_count++; 136 music_sequence_count++;
134 } 137 }
135 } else { 138 } else {
136 stop_note(freq); 139 music_noteoff(note);
137 } 140 }
138 141
139 if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through 142 if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through
140 return false; 143 return false;
141 } 144 }
142 return true; 145
146 return true;
143} 147}
144 148
145bool is_music_on(void) { 149bool is_music_on(void) {
@@ -161,26 +165,26 @@ void music_on(void) {
161 165
162void music_off(void) { 166void music_off(void) {
163 music_activated = 0; 167 music_activated = 0;
164 stop_all_notes(); 168 music_all_notes_off();
165} 169}
166 170
167
168__attribute__ ((weak))
169void music_on_user() {}
170
171__attribute__ ((weak))
172void audio_on_user() {}
173
174__attribute__ ((weak))
175void music_scale_user() {}
176
177void matrix_scan_music(void) { 171void matrix_scan_music(void) {
178 if (music_sequence_playing) { 172 if (music_sequence_playing) {
179 if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) { 173 if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) {
180 music_sequence_timer = timer_read(); 174 music_sequence_timer = timer_read();
181 stop_note(music_sequence[(music_sequence_position - 1 < 0)?(music_sequence_position - 1 + music_sequence_count):(music_sequence_position - 1)]); 175 uint8_t prev_note = music_sequence[(music_sequence_position - 1 < 0)?(music_sequence_position - 1 + music_sequence_count):(music_sequence_position - 1)];
182 play_note(music_sequence[music_sequence_position], 0xF); 176 uint8_t next_note = music_sequence[music_sequence_position];
177 music_noteoff(prev_note);
178 music_noteon(next_note);
183 music_sequence_position = (music_sequence_position + 1) % music_sequence_count; 179 music_sequence_position = (music_sequence_position + 1) % music_sequence_count;
184 } 180 }
185 } 181 }
186} 182}
183
184__attribute__ ((weak))
185void music_on_user() {}
186
187__attribute__ ((weak))
188void music_scale_user() {}
189
190#endif // defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC)) \ No newline at end of file
diff --git a/quantum/process_keycode/process_music.h b/quantum/process_keycode/process_music.h
index 318b3e387..a36514a44 100644
--- a/quantum/process_keycode/process_music.h
+++ b/quantum/process_keycode/process_music.h
@@ -3,6 +3,8 @@
3 3
4#include "quantum.h" 4#include "quantum.h"
5 5
6#if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
7
6bool process_music(uint16_t keycode, keyrecord_t *record); 8bool process_music(uint16_t keycode, keyrecord_t *record);
7 9
8bool is_music_on(void); 10bool is_music_on(void);
@@ -10,9 +12,9 @@ void music_toggle(void);
10void music_on(void); 12void music_on(void);
11void music_off(void); 13void music_off(void);
12 14
13void audio_on_user(void);
14void music_on_user(void); 15void music_on_user(void);
15void music_scale_user(void); 16void music_scale_user(void);
17void music_all_notes_off(void);
16 18
17void matrix_scan_music(void); 19void matrix_scan_music(void);
18 20
@@ -24,4 +26,6 @@ void matrix_scan_music(void);
24 0 + (12*4), 2 + (12*4), 4 + (12*4), 5 + (12*4), 7 + (12*4), 9 + (12*4), 11 + (12*4), } 26 0 + (12*4), 2 + (12*4), 4 + (12*4), 5 + (12*4), 7 + (12*4), 9 + (12*4), 11 + (12*4), }
25#endif 27#endif
26 28
29#endif // defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
30
27#endif \ No newline at end of file 31#endif \ No newline at end of file