1 #include "process_midi.h"
8 void process_midi_basic_noteon(uint8_t note)
10 midi_send_noteon(&midi_device, 0, note, 128);
13 void process_midi_basic_noteoff(uint8_t note)
15 midi_send_noteoff(&midi_device, 0, note, 0);
18 void process_midi_all_notes_off(void)
20 midi_send_cc(&midi_device, 0, 0x7B, 0);
29 static uint8_t tone_status[MIDI_TONE_COUNT];
31 static uint8_t midi_modulation;
32 static int8_t midi_modulation_step;
33 static uint16_t midi_modulation_timer;
35 inline uint8_t compute_velocity(uint8_t setting)
37 return (setting + 1) * (128 / (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN + 1));
42 midi_config.octave = MI_OCT_2 - MIDI_OCTAVE_MIN;
43 midi_config.transpose = 0;
44 midi_config.velocity = (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN);
45 midi_config.channel = 0;
46 midi_config.modulation_interval = 8;
48 for (uint8_t i = 0; i < MIDI_TONE_COUNT; i++)
50 tone_status[i] = MIDI_INVALID_NOTE;
54 midi_modulation_step = 0;
55 midi_modulation_timer = 0;
60 if (timer_elapsed(midi_modulation_timer) < midi_config.modulation_interval)
62 midi_modulation_timer = timer_read();
64 if (midi_modulation_step != 0)
66 dprintf("midi modulation %d\n", midi_modulation);
67 midi_send_cc(&midi_device, midi_config.channel, 0x1, midi_modulation);
69 if (midi_modulation_step < 0 && midi_modulation < -midi_modulation_step) {
71 midi_modulation_step = 0;
75 midi_modulation += midi_modulation_step;
77 if (midi_modulation > 127)
78 midi_modulation = 127;
82 uint8_t midi_compute_note(uint16_t keycode)
84 return 12 * midi_config.octave + (keycode - MIDI_TONE_MIN) + midi_config.transpose;
87 bool process_midi(uint16_t keycode, keyrecord_t *record)
90 case MIDI_TONE_MIN ... MIDI_TONE_MAX:
92 uint8_t channel = midi_config.channel;
93 uint8_t tone = keycode - MIDI_TONE_MIN;
94 uint8_t velocity = compute_velocity(midi_config.velocity);
95 if (record->event.pressed) {
96 uint8_t note = midi_compute_note(keycode);
97 midi_send_noteon(&midi_device, channel, note, velocity);
98 dprintf("midi noteon channel:%d note:%d velocity:%d\n", channel, note, velocity);
99 tone_status[tone] = note;
102 uint8_t note = tone_status[tone];
103 if (note != MIDI_INVALID_NOTE)
105 midi_send_noteoff(&midi_device, channel, note, velocity);
106 dprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, velocity);
108 tone_status[tone] = MIDI_INVALID_NOTE;
112 case MIDI_OCTAVE_MIN ... MIDI_OCTAVE_MAX:
113 if (record->event.pressed) {
114 midi_config.octave = keycode - MIDI_OCTAVE_MIN;
115 dprintf("midi octave %d\n", midi_config.octave);
119 if (record->event.pressed && midi_config.octave > 0) {
120 midi_config.octave--;
121 dprintf("midi octave %d\n", midi_config.octave);
125 if (record->event.pressed && midi_config.octave < (MIDI_OCTAVE_MAX - MIDI_OCTAVE_MIN)) {
126 midi_config.octave++;
127 dprintf("midi octave %d\n", midi_config.octave);
130 case MIDI_TRANSPOSE_MIN ... MIDI_TRANSPOSE_MAX:
131 if (record->event.pressed) {
132 midi_config.transpose = keycode - MI_TRNS_0;
133 dprintf("midi transpose %d\n", midi_config.transpose);
137 if (record->event.pressed && midi_config.transpose > (MIDI_TRANSPOSE_MIN - MI_TRNS_0)) {
138 midi_config.transpose--;
139 dprintf("midi transpose %d\n", midi_config.transpose);
143 if (record->event.pressed && midi_config.transpose < (MIDI_TRANSPOSE_MAX - MI_TRNS_0)) {
144 const bool positive = midi_config.transpose > 0;
145 midi_config.transpose++;
146 if (positive && midi_config.transpose < 0)
147 midi_config.transpose--;
148 dprintf("midi transpose %d\n", midi_config.transpose);
151 case MIDI_VELOCITY_MIN ... MIDI_VELOCITY_MAX:
152 if (record->event.pressed) {
153 midi_config.velocity = keycode - MIDI_VELOCITY_MIN;
154 dprintf("midi velocity %d\n", midi_config.velocity);
158 if (record->event.pressed && midi_config.velocity > 0) {
159 midi_config.velocity--;
160 dprintf("midi velocity %d\n", midi_config.velocity);
164 if (record->event.pressed) {
165 midi_config.velocity++;
166 dprintf("midi velocity %d\n", midi_config.velocity);
169 case MIDI_CHANNEL_MIN ... MIDI_CHANNEL_MAX:
170 if (record->event.pressed) {
171 midi_config.channel = keycode - MIDI_CHANNEL_MIN;
172 dprintf("midi channel %d\n", midi_config.channel);
176 if (record->event.pressed) {
177 midi_config.channel--;
178 dprintf("midi channel %d\n", midi_config.channel);
182 if (record->event.pressed) {
183 midi_config.channel++;
184 dprintf("midi channel %d\n", midi_config.channel);
188 if (record->event.pressed) {
189 midi_send_cc(&midi_device, midi_config.channel, 0x7B, 0);
190 dprintf("midi all notes off\n");
194 midi_send_cc(&midi_device, midi_config.channel, 0x40, record->event.pressed ? 127 : 0);
195 dprintf("midi sustain %d\n", record->event.pressed);
198 midi_send_cc(&midi_device, midi_config.channel, 0x41, record->event.pressed ? 127 : 0);
199 dprintf("midi portamento %d\n", record->event.pressed);
202 midi_send_cc(&midi_device, midi_config.channel, 0x42, record->event.pressed ? 127 : 0);
203 dprintf("midi sostenuto %d\n", record->event.pressed);
206 midi_send_cc(&midi_device, midi_config.channel, 0x43, record->event.pressed ? 127 : 0);
207 dprintf("midi soft %d\n", record->event.pressed);
210 midi_send_cc(&midi_device, midi_config.channel, 0x43, record->event.pressed ? 127 : 0);
211 dprintf("midi legato %d\n", record->event.pressed);
214 midi_modulation_step = record->event.pressed ? 1 : -1;
217 if (record->event.pressed) {
218 midi_config.modulation_interval++;
220 if (midi_config.modulation_interval == 0)
221 midi_config.modulation_interval--;
222 dprintf("midi modulation interval %d\n", midi_config.modulation_interval);
226 if (record->event.pressed && midi_config.modulation_interval > 0) {
227 midi_config.modulation_interval--;
228 dprintf("midi modulation interval %d\n", midi_config.modulation_interval);
236 #endif // MIDI_ADVANCED
238 #endif // MIDI_ENABLE