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
29 static void right_shift_uint128_t(uint128_t* val, uint8_t shift)
31 uint64_t high_mask = ~0 >> (64 - shift);
32 uint64_t high_bits = (val->high & high_mask) << (64 - shift);
33 val->high = val->high >> shift;
34 val->low = (val->low >> shift) | high_bits;
38 static uint64_t left_shift_uint64_t(uint64_t val, uint8_t shift)
40 dprintf("left_shift_uint64_t(val, %c) ...\n", val, shift);
42 dprintf(" left_shift_uint64_t: val=?, shift=%c\n", val, shift);
46 dprintf(" left_shift_uint64_t: val=?, shift=%c\n", val, shift);
51 static void set_bit_uint128_t(uint128_t* val, uint8_t shift)
57 x = left_shift_uint64_t(x, shift);
58 dprintf("x: %d\n", x);
59 dprintf("set_bit_uint128_t (%d): 0x%016X%016X\n", shift, 0, x);
60 val->low = val->low | left_shift_uint64_t(1u, shift);
64 x = left_shift_uint64_t(x, shift - 64);
65 dprintf("set_bit_uint128_t (%d): 0x%016X%016X\n", shift, x, 0);
66 val->high = val->high | left_shift_uint64_t(1u, shift - 64);
70 static void clear_bit_uint128_t(uint128_t* val, uint8_t shift)
74 val->low = val->low & ~left_shift_uint64_t(1u, shift);
78 val->high = val->high & ~left_shift_uint64_t(1u, shift - 64);
82 static bool is_bit_set_uint128_t(const uint128_t* val, uint8_t shift)
86 return !!(val->low & (1u << shift));
90 return !!(val->high & (1u << (shift - 64)));
94 uint128_t note_status = { 0, 0 };
98 #define MIDI_MAX_NOTES_ON 10
103 } midi_notes_on_array_entry_t;
107 midi_notes_on_array_entry_t values[MIDI_MAX_NOTES_ON];
108 } midi_notes_on_array_t;
110 static midi_notes_on_array_t notes_on;
112 inline uint8_t compute_velocity(uint8_t setting)
114 return (setting + 1) * (128 / (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN + 1));
119 midi_config.octave = MI_OCT_0 - MIDI_OCTAVE_MIN;
120 midi_config.velocity = (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN);
121 midi_config.channel = 0;
125 bool process_midi(uint16_t keycode, keyrecord_t *record)
128 case MIDI_TONE_MIN ... MIDI_TONE_MAX:
130 uint8_t channel = midi_config.channel;
131 uint8_t tone = keycode - MIDI_TONE_MIN;
132 uint8_t velocity = compute_velocity(midi_config.velocity);
133 if (record->event.pressed && notes_on.length < MIDI_MAX_NOTES_ON) {
134 uint8_t note = 12 * midi_config.octave + tone;
135 midi_send_noteon(&midi_device, channel, note, velocity);
136 dprintf("midi noteon channel:%d note:%d velocity:%d\n", channel, note, velocity);
137 notes_on.values[notes_on.length].note = note;
138 notes_on.values[notes_on.length].tone = tone;
142 for (uint8_t i = 0; i < notes_on.length; i++) {
143 uint8_t note = notes_on.values[i].note;
144 if (tone == notes_on.values[i].tone) {
145 midi_send_noteoff(&midi_device, channel, note, velocity);
146 dprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, velocity);
148 for (uint8_t j=i; j < notes_on.length - 1; j++)
150 notes_on.values[j] = notes_on.values[j + 1];
160 case MIDI_OCTAVE_MIN ... MIDI_OCTAVE_MAX:
161 if (record->event.pressed)
162 midi_config.octave = keycode - MIDI_OCTAVE_MIN;
165 if (record->event.pressed && midi_config.octave > 0)
166 midi_config.octave--;
169 if (record->event.pressed && midi_config.octave < (MIDI_OCTAVE_MAX - MIDI_OCTAVE_MIN))
170 midi_config.octave++;
172 case MIDI_VELOCITY_MIN ... MIDI_VELOCITY_MAX:
173 if (record->event.pressed)
174 midi_config.velocity = keycode - MIDI_VELOCITY_MIN;
177 if (record->event.pressed && midi_config.velocity > 0)
178 midi_config.velocity--;
181 if (record->event.pressed)
182 midi_config.velocity++;
184 case MIDI_CHANNEL_MIN ... MIDI_CHANNEL_MAX:
185 if (record->event.pressed)
186 midi_config.channel = keycode - MIDI_CHANNEL_MIN;
189 if (record->event.pressed)
190 midi_config.channel--;
193 if (record->event.pressed)
194 midi_config.channel++;
202 if (keycode == MI_ON && record->event.pressed) {
203 midi_activated = true;
210 if (keycode == MI_OFF && record->event.pressed) {
211 midi_activated = false;
212 midi_send_cc(&midi_device, 0, 0x7B, 0);
216 if (midi_activated) {
217 if (record->event.key.col == (MATRIX_COLS - 1) && record->event.key.row == (MATRIX_ROWS - 1)) {
218 if (record->event.pressed) {
219 midi_starting_note++; // Change key
220 midi_send_cc(&midi_device, 0, 0x7B, 0);
224 if (record->event.key.col == (MATRIX_COLS - 2) && record->event.key.row == (MATRIX_ROWS - 1)) {
225 if (record->event.pressed) {
226 midi_starting_note--; // Change key
227 midi_send_cc(&midi_device, 0, 0x7B, 0);
231 if (record->event.key.col == (MATRIX_COLS - 3) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) {
232 midi_offset++; // Change scale
233 midi_send_cc(&midi_device, 0, 0x7B, 0);
236 if (record->event.key.col == (MATRIX_COLS - 4) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) {
237 midi_offset--; // Change scale
238 midi_send_cc(&midi_device, 0, 0x7B, 0);
242 // uint8_t note = (midi_starting_note + SCALE[record->event.key.col + midi_offset])+12*(MATRIX_ROWS - record->event.key.row);
244 // uint8_t note = (midi_starting_note + record->event.key.col + midi_offset)+12*(MATRIX_ROWS - record->event.key.row);
246 uint8_t note = (midi_starting_note + record->event.key.col + midi_offset)+5*(MATRIX_ROWS - record->event.key.row);
248 // uint8_t note = (midi_starting_note + record->event.key.col + midi_offset)+7*(MATRIX_ROWS - record->event.key.row);
250 if (record->event.pressed) {
251 // midi_send_noteon(&midi_device, record->event.key.row, midi_starting_note + SCALE[record->event.key.col], 127);
252 midi_send_noteon(&midi_device, 0, note, 127);
254 // midi_send_noteoff(&midi_device, record->event.key.row, midi_starting_note + SCALE[record->event.key.col], 127);
255 midi_send_noteoff(&midi_device, 0, note, 127);
258 if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through