diff options
author | Gabriel Young <gabeplaysdrums@live.com> | 2017-02-18 03:40:49 -0800 |
---|---|---|
committer | Gabriel Young <gabeplaysdrums@live.com> | 2017-02-19 16:42:04 -0800 |
commit | a4163466cb09144a96e2ea7bc697af1af8a5e770 (patch) | |
tree | 8d6741528a610eeed062db3e5f3fdbd38c40f868 /quantum/process_keycode/process_midi.c | |
parent | f2b2e05f126403c8a6f0fc3d542beddac7974e9b (diff) | |
download | qmk_firmware-a4163466cb09144a96e2ea7bc697af1af8a5e770.tar.gz qmk_firmware-a4163466cb09144a96e2ea7bc697af1af8a5e770.zip |
Alternative version with a tone array
tone array:
text data bss dec hex filename
0 25698 0 25698 6462 satan_newsboytko.hex
0x6480 bytes written into 0x7000 bytes memory (89.73%).
note on array:
text data bss dec hex filename
0 25802 0 25802 64ca satan_newsboytko.hex
0x6500 bytes written into 0x7000 bytes memory (90.18%).
Diffstat (limited to 'quantum/process_keycode/process_midi.c')
-rw-r--r-- | quantum/process_keycode/process_midi.c | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/quantum/process_keycode/process_midi.c b/quantum/process_keycode/process_midi.c index acaae7c30..4fbb28816 100644 --- a/quantum/process_keycode/process_midi.c +++ b/quantum/process_keycode/process_midi.c | |||
@@ -19,6 +19,10 @@ midi_config_t midi_config; | |||
19 | 19 | ||
20 | #define MIDI_INVALID_NOTE 0xFF | 20 | #define MIDI_INVALID_NOTE 0xFF |
21 | 21 | ||
22 | #define MIDI_USE_NOTE_ON_ARRAY | ||
23 | |||
24 | #ifdef MIDI_USE_NOTE_ON_ARRAY | ||
25 | |||
22 | #define MIDI_MAX_NOTES_ON 10 | 26 | #define MIDI_MAX_NOTES_ON 10 |
23 | 27 | ||
24 | typedef struct { | 28 | typedef struct { |
@@ -33,6 +37,15 @@ typedef struct { | |||
33 | 37 | ||
34 | static midi_notes_on_array_t notes_on; | 38 | static midi_notes_on_array_t notes_on; |
35 | 39 | ||
40 | #else | ||
41 | |||
42 | #define MIDI_TONE_COUNT (MIDI_TONE_MAX - MIDI_TONE_MIN + 1) | ||
43 | static uint8_t tone_status[MIDI_TONE_COUNT]; | ||
44 | |||
45 | #endif | ||
46 | |||
47 | |||
48 | |||
36 | inline uint8_t compute_velocity(uint8_t setting) | 49 | inline uint8_t compute_velocity(uint8_t setting) |
37 | { | 50 | { |
38 | return (setting + 1) * (128 / (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN + 1)); | 51 | return (setting + 1) * (128 / (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN + 1)); |
@@ -43,7 +56,14 @@ void midi_init(void) | |||
43 | midi_config.octave = MI_OCT_0 - MIDI_OCTAVE_MIN; | 56 | midi_config.octave = MI_OCT_0 - MIDI_OCTAVE_MIN; |
44 | midi_config.velocity = (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN); | 57 | midi_config.velocity = (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN); |
45 | midi_config.channel = 0; | 58 | midi_config.channel = 0; |
59 | #ifdef MIDI_USE_NOTE_ON_ARRAY | ||
46 | notes_on.length = 0; | 60 | notes_on.length = 0; |
61 | #else | ||
62 | for (uint8_t i = 0; i < MIDI_TONE_COUNT; i++) | ||
63 | { | ||
64 | tone_status[i] = MIDI_INVALID_NOTE; | ||
65 | } | ||
66 | #endif | ||
47 | } | 67 | } |
48 | 68 | ||
49 | bool process_midi(uint16_t keycode, keyrecord_t *record) | 69 | bool process_midi(uint16_t keycode, keyrecord_t *record) |
@@ -54,15 +74,31 @@ bool process_midi(uint16_t keycode, keyrecord_t *record) | |||
54 | uint8_t channel = midi_config.channel; | 74 | uint8_t channel = midi_config.channel; |
55 | uint8_t tone = keycode - MIDI_TONE_MIN; | 75 | uint8_t tone = keycode - MIDI_TONE_MIN; |
56 | uint8_t velocity = compute_velocity(midi_config.velocity); | 76 | uint8_t velocity = compute_velocity(midi_config.velocity); |
77 | #ifdef MIDI_USE_NOTE_ON_ARRAY | ||
57 | if (record->event.pressed && notes_on.length < MIDI_MAX_NOTES_ON) { | 78 | if (record->event.pressed && notes_on.length < MIDI_MAX_NOTES_ON) { |
79 | #else | ||
80 | if (record->event.pressed) { | ||
81 | #endif | ||
58 | uint8_t note = 12 * midi_config.octave + tone; | 82 | uint8_t note = 12 * midi_config.octave + tone; |
59 | midi_send_noteon(&midi_device, channel, note, velocity); | 83 | midi_send_noteon(&midi_device, channel, note, velocity); |
60 | dprintf("midi noteon channel:%d note:%d velocity:%d\n", channel, note, velocity); | 84 | dprintf("midi noteon channel:%d note:%d velocity:%d\n", channel, note, velocity); |
85 | |||
86 | #ifdef MIDI_USE_NOTE_ON_ARRAY | ||
87 | |||
61 | notes_on.values[notes_on.length].note = note; | 88 | notes_on.values[notes_on.length].note = note; |
62 | notes_on.values[notes_on.length].tone = tone; | 89 | notes_on.values[notes_on.length].tone = tone; |
63 | notes_on.length++; | 90 | notes_on.length++; |
91 | |||
92 | #else | ||
93 | |||
94 | tone_status[tone] = note; | ||
95 | |||
96 | #endif | ||
64 | } | 97 | } |
65 | else { | 98 | else { |
99 | |||
100 | #ifdef MIDI_USE_NOTE_ON_ARRAY | ||
101 | |||
66 | for (uint8_t i = 0; i < notes_on.length; i++) { | 102 | for (uint8_t i = 0; i < notes_on.length; i++) { |
67 | uint8_t note = notes_on.values[i].note; | 103 | uint8_t note = notes_on.values[i].note; |
68 | if (tone == notes_on.values[i].tone) { | 104 | if (tone == notes_on.values[i].tone) { |
@@ -78,6 +114,18 @@ bool process_midi(uint16_t keycode, keyrecord_t *record) | |||
78 | break; | 114 | break; |
79 | } | 115 | } |
80 | } | 116 | } |
117 | |||
118 | #else | ||
119 | |||
120 | uint8_t note = tone_status[tone]; | ||
121 | if (note != MIDI_INVALID_NOTE) | ||
122 | { | ||
123 | midi_send_noteoff(&midi_device, channel, note, velocity); | ||
124 | dprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, velocity); | ||
125 | } | ||
126 | tone_status[tone] = MIDI_INVALID_NOTE; | ||
127 | |||
128 | #endif | ||
81 | } | 129 | } |
82 | return false; | 130 | return false; |
83 | } | 131 | } |
@@ -122,5 +170,66 @@ bool process_midi(uint16_t keycode, keyrecord_t *record) | |||
122 | return false; | 170 | return false; |
123 | }; | 171 | }; |
124 | 172 | ||
173 | #if 0 | ||
174 | if (keycode == MI_ON && record->event.pressed) { | ||
175 | midi_activated = true; | ||
176 | #ifdef AUDIO_ENABLE | ||
177 | music_scale_user(); | ||
178 | #endif | ||
179 | return false; | ||
180 | } | ||
181 | |||
182 | if (keycode == MI_OFF && record->event.pressed) { | ||
183 | midi_activated = false; | ||
184 | midi_send_cc(&midi_device, 0, 0x7B, 0); | ||
185 | return false; | ||
186 | } | ||
187 | |||
188 | if (midi_activated) { | ||
189 | if (record->event.key.col == (MATRIX_COLS - 1) && record->event.key.row == (MATRIX_ROWS - 1)) { | ||
190 | if (record->event.pressed) { | ||
191 | midi_starting_note++; // Change key | ||
192 | midi_send_cc(&midi_device, 0, 0x7B, 0); | ||
193 | } | ||
194 | return false; | ||
195 | } | ||
196 | if (record->event.key.col == (MATRIX_COLS - 2) && record->event.key.row == (MATRIX_ROWS - 1)) { | ||
197 | if (record->event.pressed) { | ||
198 | midi_starting_note--; // Change key | ||
199 | midi_send_cc(&midi_device, 0, 0x7B, 0); | ||
200 | } | ||
201 | return false; | ||
202 | } | ||
203 | if (record->event.key.col == (MATRIX_COLS - 3) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) { | ||
204 | midi_offset++; // Change scale | ||
205 | midi_send_cc(&midi_device, 0, 0x7B, 0); | ||
206 | return false; | ||
207 | } | ||
208 | if (record->event.key.col == (MATRIX_COLS - 4) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) { | ||
209 | midi_offset--; // Change scale | ||
210 | midi_send_cc(&midi_device, 0, 0x7B, 0); | ||
211 | return false; | ||
212 | } | ||
213 | // basic | ||
214 | // uint8_t note = (midi_starting_note + SCALE[record->event.key.col + midi_offset])+12*(MATRIX_ROWS - record->event.key.row); | ||
215 | // advanced | ||
216 | // uint8_t note = (midi_starting_note + record->event.key.col + midi_offset)+12*(MATRIX_ROWS - record->event.key.row); | ||
217 | // guitar | ||
218 | uint8_t note = (midi_starting_note + record->event.key.col + midi_offset)+5*(MATRIX_ROWS - record->event.key.row); | ||
219 | // violin | ||
220 | // uint8_t note = (midi_starting_note + record->event.key.col + midi_offset)+7*(MATRIX_ROWS - record->event.key.row); | ||
221 | |||
222 | if (record->event.pressed) { | ||
223 | // midi_send_noteon(&midi_device, record->event.key.row, midi_starting_note + SCALE[record->event.key.col], 127); | ||
224 | midi_send_noteon(&midi_device, 0, note, 127); | ||
225 | } else { | ||
226 | // midi_send_noteoff(&midi_device, record->event.key.row, midi_starting_note + SCALE[record->event.key.col], 127); | ||
227 | midi_send_noteoff(&midi_device, 0, note, 127); | ||
228 | } | ||
229 | |||
230 | if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through | ||
231 | return false; | ||
232 | } | ||
233 | #endif | ||
125 | return true; | 234 | return true; |
126 | } | 235 | } |