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_MAX_NOTES_ON 10
27 } midi_notes_on_array_entry_t;
31 midi_notes_on_array_entry_t values[MIDI_MAX_NOTES_ON];
32 } midi_notes_on_array_t;
34 static midi_notes_on_array_t notes_on;
36 inline uint8_t compute_velocity(uint8_t setting)
38 return (setting + 1) * (128 / (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN + 1));
43 midi_config.octave = MI_OCT_0 - MIDI_OCTAVE_MIN;
44 midi_config.velocity = (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN);
45 midi_config.channel = 0;
49 bool process_midi(uint16_t keycode, keyrecord_t *record)
52 case MIDI_TONE_MIN ... MIDI_TONE_MAX:
54 uint8_t channel = midi_config.channel;
55 uint8_t tone = keycode - MIDI_TONE_MIN;
56 uint8_t velocity = compute_velocity(midi_config.velocity);
57 if (record->event.pressed && notes_on.length < MIDI_MAX_NOTES_ON) {
58 uint8_t note = 12 * midi_config.octave + tone;
59 midi_send_noteon(&midi_device, channel, note, velocity);
60 dprintf("midi noteon channel:%d note:%d velocity:%d\n", channel, note, velocity);
61 notes_on.values[notes_on.length].note = note;
62 notes_on.values[notes_on.length].tone = tone;
66 for (uint8_t i = 0; i < notes_on.length; i++) {
67 uint8_t note = notes_on.values[i].note;
68 if (tone == notes_on.values[i].tone) {
69 midi_send_noteoff(&midi_device, channel, note, velocity);
70 dprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, velocity);
72 for (uint8_t j=i; j < notes_on.length - 1; j++)
74 notes_on.values[j] = notes_on.values[j + 1];
84 case MIDI_OCTAVE_MIN ... MIDI_OCTAVE_MAX:
85 if (record->event.pressed)
86 midi_config.octave = keycode - MIDI_OCTAVE_MIN;
89 if (record->event.pressed && midi_config.octave > 0)
93 if (record->event.pressed && midi_config.octave < (MIDI_OCTAVE_MAX - MIDI_OCTAVE_MIN))
96 case MIDI_VELOCITY_MIN ... MIDI_VELOCITY_MAX:
97 if (record->event.pressed)
98 midi_config.velocity = keycode - MIDI_VELOCITY_MIN;
101 if (record->event.pressed && midi_config.velocity > 0)
102 midi_config.velocity--;
105 if (record->event.pressed)
106 midi_config.velocity++;
108 case MIDI_CHANNEL_MIN ... MIDI_CHANNEL_MAX:
109 if (record->event.pressed)
110 midi_config.channel = keycode - MIDI_CHANNEL_MIN;
113 if (record->event.pressed)
114 midi_config.channel--;
117 if (record->event.pressed)
118 midi_config.channel++;