1 #include "process_midi.h"
4 bool midi_activated = false;
5 uint8_t midi_starting_note = 0x0C;
18 midi_config_t midi_config;
20 #define MIDI_INVALID_NOTE 0xFF
22 #define MIDI_USE_NOTE_ON_ARRAY
24 #ifdef MIDI_USE_NOTE_ON_ARRAY
26 #define MIDI_MAX_NOTES_ON 10
31 } midi_notes_on_array_entry_t;
35 midi_notes_on_array_entry_t values[MIDI_MAX_NOTES_ON];
36 } midi_notes_on_array_t;
38 static midi_notes_on_array_t notes_on;
42 #define MIDI_TONE_COUNT (MIDI_TONE_MAX - MIDI_TONE_MIN + 1)
43 static uint8_t tone_status[MIDI_TONE_COUNT];
49 inline uint8_t compute_velocity(uint8_t setting)
51 return (setting + 1) * (128 / (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN + 1));
56 midi_config.octave = MI_OCT_0 - MIDI_OCTAVE_MIN;
57 midi_config.velocity = (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN);
58 midi_config.channel = 0;
59 #ifdef MIDI_USE_NOTE_ON_ARRAY
62 for (uint8_t i = 0; i < MIDI_TONE_COUNT; i++)
64 tone_status[i] = MIDI_INVALID_NOTE;
69 bool process_midi(uint16_t keycode, keyrecord_t *record)
72 case MIDI_TONE_MIN ... MIDI_TONE_MAX:
74 uint8_t channel = midi_config.channel;
75 uint8_t tone = keycode - MIDI_TONE_MIN;
76 uint8_t velocity = compute_velocity(midi_config.velocity);
77 #ifdef MIDI_USE_NOTE_ON_ARRAY
78 if (record->event.pressed && notes_on.length < MIDI_MAX_NOTES_ON) {
80 if (record->event.pressed) {
82 uint8_t note = 12 * midi_config.octave + tone;
83 midi_send_noteon(&midi_device, channel, note, velocity);
84 dprintf("midi noteon channel:%d note:%d velocity:%d\n", channel, note, velocity);
86 #ifdef MIDI_USE_NOTE_ON_ARRAY
88 notes_on.values[notes_on.length].note = note;
89 notes_on.values[notes_on.length].tone = tone;
94 tone_status[tone] = note;
100 #ifdef MIDI_USE_NOTE_ON_ARRAY
102 for (uint8_t i = 0; i < notes_on.length; i++) {
103 uint8_t note = notes_on.values[i].note;
104 if (tone == notes_on.values[i].tone) {
105 midi_send_noteoff(&midi_device, channel, note, velocity);
106 dprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, velocity);
108 for (uint8_t j=i; j < notes_on.length - 1; j++)
110 notes_on.values[j] = notes_on.values[j + 1];
120 uint8_t note = tone_status[tone];
121 if (note != MIDI_INVALID_NOTE)
123 midi_send_noteoff(&midi_device, channel, note, velocity);
124 dprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, velocity);
126 tone_status[tone] = MIDI_INVALID_NOTE;
132 case MIDI_OCTAVE_MIN ... MIDI_OCTAVE_MAX:
133 if (record->event.pressed)
134 midi_config.octave = keycode - MIDI_OCTAVE_MIN;
137 if (record->event.pressed && midi_config.octave > 0)
138 midi_config.octave--;
141 if (record->event.pressed && midi_config.octave < (MIDI_OCTAVE_MAX - MIDI_OCTAVE_MIN))
142 midi_config.octave++;
144 case MIDI_VELOCITY_MIN ... MIDI_VELOCITY_MAX:
145 if (record->event.pressed)
146 midi_config.velocity = keycode - MIDI_VELOCITY_MIN;
149 if (record->event.pressed && midi_config.velocity > 0)
150 midi_config.velocity--;
153 if (record->event.pressed)
154 midi_config.velocity++;
156 case MIDI_CHANNEL_MIN ... MIDI_CHANNEL_MAX:
157 if (record->event.pressed)
158 midi_config.channel = keycode - MIDI_CHANNEL_MIN;
161 if (record->event.pressed)
162 midi_config.channel--;
165 if (record->event.pressed)
166 midi_config.channel++;
174 if (keycode == MI_ON && record->event.pressed) {
175 midi_activated = true;
182 if (keycode == MI_OFF && record->event.pressed) {
183 midi_activated = false;
184 midi_send_cc(&midi_device, 0, 0x7B, 0);
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);
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);
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);
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);
214 // uint8_t note = (midi_starting_note + SCALE[record->event.key.col + midi_offset])+12*(MATRIX_ROWS - record->event.key.row);
216 // uint8_t note = (midi_starting_note + record->event.key.col + midi_offset)+12*(MATRIX_ROWS - record->event.key.row);
218 uint8_t note = (midi_starting_note + record->event.key.col + midi_offset)+5*(MATRIX_ROWS - record->event.key.row);
220 // uint8_t note = (midi_starting_note + record->event.key.col + midi_offset)+7*(MATRIX_ROWS - record->event.key.row);
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);
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);
230 if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through