1 #include "process_midi.h"
4 static uint8_t tone_status[MIDI_TONE_COUNT];
6 static uint8_t midi_modulation;
7 static int8_t midi_modulation_step;
8 static uint16_t midi_modulation_timer;
10 inline uint8_t compute_velocity(uint8_t setting)
12 return (setting + 1) * (128 / (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN + 1));
17 midi_config.octave = MI_OCT_2 - MIDI_OCTAVE_MIN;
18 midi_config.transpose = 0;
19 midi_config.velocity = (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN);
20 midi_config.channel = 0;
21 midi_config.modulation_interval = 8;
23 for (uint8_t i = 0; i < MIDI_TONE_COUNT; i++)
25 tone_status[i] = MIDI_INVALID_NOTE;
29 midi_modulation_step = 0;
30 midi_modulation_timer = 0;
35 if (timer_elapsed(midi_modulation_timer) < midi_config.modulation_interval)
37 midi_modulation_timer = timer_read();
39 if (midi_modulation_step != 0)
41 dprintf("midi modulation %d\n", midi_modulation);
42 midi_send_cc(&midi_device, midi_config.channel, 0x1, midi_modulation);
44 if (midi_modulation_step < 0 && midi_modulation < -midi_modulation_step) {
46 midi_modulation_step = 0;
50 midi_modulation += midi_modulation_step;
52 if (midi_modulation > 127)
53 midi_modulation = 127;
57 uint8_t midi_compute_note(uint16_t keycode)
59 return 12 * midi_config.octave + (keycode - MIDI_TONE_MIN) + midi_config.transpose;
62 bool process_midi(uint16_t keycode, keyrecord_t *record)
65 case MIDI_TONE_MIN ... MIDI_TONE_MAX:
67 uint8_t channel = midi_config.channel;
68 uint8_t tone = keycode - MIDI_TONE_MIN;
69 uint8_t velocity = compute_velocity(midi_config.velocity);
70 if (record->event.pressed) {
71 uint8_t note = midi_compute_note(keycode);
72 midi_send_noteon(&midi_device, channel, note, velocity);
73 dprintf("midi noteon channel:%d note:%d velocity:%d\n", channel, note, velocity);
74 tone_status[tone] = note;
77 uint8_t note = tone_status[tone];
78 if (note != MIDI_INVALID_NOTE)
80 midi_send_noteoff(&midi_device, channel, note, velocity);
81 dprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, velocity);
83 tone_status[tone] = MIDI_INVALID_NOTE;
87 case MIDI_OCTAVE_MIN ... MIDI_OCTAVE_MAX:
88 if (record->event.pressed) {
89 midi_config.octave = keycode - MIDI_OCTAVE_MIN;
90 dprintf("midi octave %d\n", midi_config.octave);
94 if (record->event.pressed && midi_config.octave > 0) {
96 dprintf("midi octave %d\n", midi_config.octave);
100 if (record->event.pressed && midi_config.octave < (MIDI_OCTAVE_MAX - MIDI_OCTAVE_MIN)) {
101 midi_config.octave++;
102 dprintf("midi octave %d\n", midi_config.octave);
105 case MIDI_TRANSPOSE_MIN ... MIDI_TRANSPOSE_MAX:
106 if (record->event.pressed) {
107 midi_config.transpose = keycode - MI_TRNS_0;
108 dprintf("midi transpose %d\n", midi_config.transpose);
112 if (record->event.pressed && midi_config.transpose > (MIDI_TRANSPOSE_MIN - MI_TRNS_0)) {
113 midi_config.transpose--;
114 dprintf("midi transpose %d\n", midi_config.transpose);
118 if (record->event.pressed && midi_config.transpose < (MIDI_TRANSPOSE_MAX - MI_TRNS_0)) {
119 const bool positive = midi_config.transpose > 0;
120 midi_config.transpose++;
121 if (positive && midi_config.transpose < 0)
122 midi_config.transpose--;
123 dprintf("midi transpose %d\n", midi_config.transpose);
126 case MIDI_VELOCITY_MIN ... MIDI_VELOCITY_MAX:
127 if (record->event.pressed) {
128 midi_config.velocity = keycode - MIDI_VELOCITY_MIN;
129 dprintf("midi velocity %d\n", midi_config.velocity);
133 if (record->event.pressed && midi_config.velocity > 0) {
134 midi_config.velocity--;
135 dprintf("midi velocity %d\n", midi_config.velocity);
139 if (record->event.pressed) {
140 midi_config.velocity++;
141 dprintf("midi velocity %d\n", midi_config.velocity);
144 case MIDI_CHANNEL_MIN ... MIDI_CHANNEL_MAX:
145 if (record->event.pressed) {
146 midi_config.channel = keycode - MIDI_CHANNEL_MIN;
147 dprintf("midi channel %d\n", midi_config.channel);
151 if (record->event.pressed) {
152 midi_config.channel--;
153 dprintf("midi channel %d\n", midi_config.channel);
157 if (record->event.pressed) {
158 midi_config.channel++;
159 dprintf("midi channel %d\n", midi_config.channel);
163 if (record->event.pressed) {
164 midi_send_cc(&midi_device, midi_config.channel, 0x7B, 0);
165 dprintf("midi off\n");
169 midi_send_cc(&midi_device, midi_config.channel, 0x40, record->event.pressed ? 127 : 0);
170 dprintf("midi sustain %d\n", record->event.pressed);
173 midi_send_cc(&midi_device, midi_config.channel, 0x41, record->event.pressed ? 127 : 0);
174 dprintf("midi portamento %d\n", record->event.pressed);
177 midi_send_cc(&midi_device, midi_config.channel, 0x42, record->event.pressed ? 127 : 0);
178 dprintf("midi sostenuto %d\n", record->event.pressed);
181 midi_send_cc(&midi_device, midi_config.channel, 0x43, record->event.pressed ? 127 : 0);
182 dprintf("midi soft %d\n", record->event.pressed);
185 midi_send_cc(&midi_device, midi_config.channel, 0x43, record->event.pressed ? 127 : 0);
186 dprintf("midi legato %d\n", record->event.pressed);
189 midi_modulation_step = record->event.pressed ? 1 : -1;
192 if (record->event.pressed) {
193 midi_config.modulation_interval++;
195 if (midi_config.modulation_interval == 0)
196 midi_config.modulation_interval--;
197 dprintf("midi modulation interval %d\n", midi_config.modulation_interval);
201 if (record->event.pressed && midi_config.modulation_interval > 0) {
202 midi_config.modulation_interval--;
203 dprintf("midi modulation interval %d\n", midi_config.modulation_interval);