1 #include "process_midi.h"
3 #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
7 static uint8_t tone_status[MIDI_TONE_COUNT];
9 static uint8_t midi_modulation;
10 static int8_t midi_modulation_step;
11 static uint16_t midi_modulation_timer;
13 inline uint8_t compute_velocity(uint8_t setting)
15 return (setting + 1) * (128 / (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN + 1));
20 midi_config.octave = MI_OCT_2 - MIDI_OCTAVE_MIN;
21 midi_config.transpose = 0;
22 midi_config.velocity = (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN);
23 midi_config.channel = 0;
24 midi_config.modulation_interval = 8;
26 for (uint8_t i = 0; i < MIDI_TONE_COUNT; i++)
28 tone_status[i] = MIDI_INVALID_NOTE;
32 midi_modulation_step = 0;
33 midi_modulation_timer = 0;
38 if (timer_elapsed(midi_modulation_timer) < midi_config.modulation_interval)
40 midi_modulation_timer = timer_read();
42 if (midi_modulation_step != 0)
44 dprintf("midi modulation %d\n", midi_modulation);
45 midi_send_cc(&midi_device, midi_config.channel, 0x1, midi_modulation);
47 if (midi_modulation_step < 0 && midi_modulation < -midi_modulation_step) {
49 midi_modulation_step = 0;
53 midi_modulation += midi_modulation_step;
55 if (midi_modulation > 127)
56 midi_modulation = 127;
60 uint8_t midi_compute_note(uint16_t keycode)
62 return 12 * midi_config.octave + (keycode - MIDI_TONE_MIN) + midi_config.transpose;
65 bool process_midi(uint16_t keycode, keyrecord_t *record)
68 case MIDI_TONE_MIN ... MIDI_TONE_MAX:
70 uint8_t channel = midi_config.channel;
71 uint8_t tone = keycode - MIDI_TONE_MIN;
72 uint8_t velocity = compute_velocity(midi_config.velocity);
73 if (record->event.pressed) {
74 uint8_t note = midi_compute_note(keycode);
75 midi_send_noteon(&midi_device, channel, note, velocity);
76 dprintf("midi noteon channel:%d note:%d velocity:%d\n", channel, note, velocity);
77 tone_status[tone] = note;
80 uint8_t note = tone_status[tone];
81 if (note != MIDI_INVALID_NOTE)
83 midi_send_noteoff(&midi_device, channel, note, velocity);
84 dprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, velocity);
86 tone_status[tone] = MIDI_INVALID_NOTE;
90 case MIDI_OCTAVE_MIN ... MIDI_OCTAVE_MAX:
91 if (record->event.pressed) {
92 midi_config.octave = keycode - MIDI_OCTAVE_MIN;
93 dprintf("midi octave %d\n", midi_config.octave);
97 if (record->event.pressed && midi_config.octave > 0) {
99 dprintf("midi octave %d\n", midi_config.octave);
103 if (record->event.pressed && midi_config.octave < (MIDI_OCTAVE_MAX - MIDI_OCTAVE_MIN)) {
104 midi_config.octave++;
105 dprintf("midi octave %d\n", midi_config.octave);
108 case MIDI_TRANSPOSE_MIN ... MIDI_TRANSPOSE_MAX:
109 if (record->event.pressed) {
110 midi_config.transpose = keycode - MI_TRNS_0;
111 dprintf("midi transpose %d\n", midi_config.transpose);
115 if (record->event.pressed && midi_config.transpose > (MIDI_TRANSPOSE_MIN - MI_TRNS_0)) {
116 midi_config.transpose--;
117 dprintf("midi transpose %d\n", midi_config.transpose);
121 if (record->event.pressed && midi_config.transpose < (MIDI_TRANSPOSE_MAX - MI_TRNS_0)) {
122 const bool positive = midi_config.transpose > 0;
123 midi_config.transpose++;
124 if (positive && midi_config.transpose < 0)
125 midi_config.transpose--;
126 dprintf("midi transpose %d\n", midi_config.transpose);
129 case MIDI_VELOCITY_MIN ... MIDI_VELOCITY_MAX:
130 if (record->event.pressed) {
131 midi_config.velocity = keycode - MIDI_VELOCITY_MIN;
132 dprintf("midi velocity %d\n", midi_config.velocity);
136 if (record->event.pressed && midi_config.velocity > 0) {
137 midi_config.velocity--;
138 dprintf("midi velocity %d\n", midi_config.velocity);
142 if (record->event.pressed) {
143 midi_config.velocity++;
144 dprintf("midi velocity %d\n", midi_config.velocity);
147 case MIDI_CHANNEL_MIN ... MIDI_CHANNEL_MAX:
148 if (record->event.pressed) {
149 midi_config.channel = keycode - MIDI_CHANNEL_MIN;
150 dprintf("midi channel %d\n", midi_config.channel);
154 if (record->event.pressed) {
155 midi_config.channel--;
156 dprintf("midi channel %d\n", midi_config.channel);
160 if (record->event.pressed) {
161 midi_config.channel++;
162 dprintf("midi channel %d\n", midi_config.channel);
166 if (record->event.pressed) {
167 midi_send_cc(&midi_device, midi_config.channel, 0x7B, 0);
168 dprintf("midi off\n");
172 midi_send_cc(&midi_device, midi_config.channel, 0x40, record->event.pressed ? 127 : 0);
173 dprintf("midi sustain %d\n", record->event.pressed);
176 midi_send_cc(&midi_device, midi_config.channel, 0x41, record->event.pressed ? 127 : 0);
177 dprintf("midi portamento %d\n", record->event.pressed);
180 midi_send_cc(&midi_device, midi_config.channel, 0x42, record->event.pressed ? 127 : 0);
181 dprintf("midi sostenuto %d\n", record->event.pressed);
184 midi_send_cc(&midi_device, midi_config.channel, 0x43, record->event.pressed ? 127 : 0);
185 dprintf("midi soft %d\n", record->event.pressed);
188 midi_send_cc(&midi_device, midi_config.channel, 0x43, record->event.pressed ? 127 : 0);
189 dprintf("midi legato %d\n", record->event.pressed);
192 midi_modulation_step = record->event.pressed ? 1 : -1;
195 if (record->event.pressed) {
196 midi_config.modulation_interval++;
198 if (midi_config.modulation_interval == 0)
199 midi_config.modulation_interval--;
200 dprintf("midi modulation interval %d\n", midi_config.modulation_interval);
204 if (record->event.pressed && midi_config.modulation_interval > 0) {
205 midi_config.modulation_interval--;
206 dprintf("midi modulation interval %d\n", midi_config.modulation_interval);
214 #endif // MIDI_ADVANCED