1 #include "process_midi.h"
10 uint8_t modulation_interval :4;
14 midi_config_t midi_config;
16 #define MIDI_INVALID_NOTE 0xFF
18 #define MIDI_TONE_COUNT (MIDI_TONE_MAX - MIDI_TONE_MIN + 1)
19 static uint8_t tone_status[MIDI_TONE_COUNT];
21 static uint8_t midi_modulation;
22 static int8_t midi_modulation_step;
23 static uint16_t midi_modulation_timer;
25 inline uint8_t compute_velocity(uint8_t setting)
27 return (setting + 1) * (128 / (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN + 1));
32 midi_config.octave = MI_OCT_0 - MIDI_OCTAVE_MIN;
33 midi_config.velocity = (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN);
34 midi_config.channel = 0;
35 midi_config.modulation_interval = 8;
37 for (uint8_t i = 0; i < MIDI_TONE_COUNT; i++)
39 tone_status[i] = MIDI_INVALID_NOTE;
43 midi_modulation_step = 0;
44 midi_modulation_timer = 0;
49 if (timer_elapsed(midi_modulation_timer) < midi_config.modulation_interval)
51 midi_modulation_timer = timer_read();
53 if (midi_modulation_step != 0)
55 dprintf("midi modulation %d\n", midi_modulation);
56 midi_send_cc(&midi_device, midi_config.channel, 0x1, midi_modulation);
58 if (midi_modulation_step < 0 && midi_modulation < -midi_modulation_step) {
60 midi_modulation_step = 0;
64 midi_modulation += midi_modulation_step;
66 if (midi_modulation > 127)
67 midi_modulation = 127;
71 bool process_midi(uint16_t keycode, keyrecord_t *record)
74 case MIDI_TONE_MIN ... MIDI_TONE_MAX:
76 uint8_t channel = midi_config.channel;
77 uint8_t tone = keycode - MIDI_TONE_MIN;
78 uint8_t velocity = compute_velocity(midi_config.velocity);
79 if (record->event.pressed) {
80 uint8_t note = 12 * midi_config.octave + tone;
81 midi_send_noteon(&midi_device, channel, note, velocity);
82 dprintf("midi noteon channel:%d note:%d velocity:%d\n", channel, note, velocity);
83 tone_status[tone] = note;
86 uint8_t note = tone_status[tone];
87 if (note != MIDI_INVALID_NOTE)
89 midi_send_noteoff(&midi_device, channel, note, velocity);
90 dprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, velocity);
92 tone_status[tone] = MIDI_INVALID_NOTE;
96 case MIDI_OCTAVE_MIN ... MIDI_OCTAVE_MAX:
97 if (record->event.pressed) {
98 midi_config.octave = keycode - MIDI_OCTAVE_MIN;
99 dprintf("midi octave %d\n", midi_config.octave);
103 if (record->event.pressed && midi_config.octave > 0) {
104 midi_config.octave--;
105 dprintf("midi octave %d\n", midi_config.octave);
109 if (record->event.pressed && midi_config.octave < (MIDI_OCTAVE_MAX - MIDI_OCTAVE_MIN)) {
110 midi_config.octave++;
111 dprintf("midi octave %d\n", midi_config.octave);
114 case MIDI_VELOCITY_MIN ... MIDI_VELOCITY_MAX:
115 if (record->event.pressed) {
116 midi_config.velocity = keycode - MIDI_VELOCITY_MIN;
117 dprintf("midi velocity %d\n", midi_config.velocity);
121 if (record->event.pressed && midi_config.velocity > 0) {
122 midi_config.velocity--;
123 dprintf("midi velocity %d\n", midi_config.velocity);
127 if (record->event.pressed) {
128 midi_config.velocity++;
129 dprintf("midi velocity %d\n", midi_config.velocity);
132 case MIDI_CHANNEL_MIN ... MIDI_CHANNEL_MAX:
133 if (record->event.pressed) {
134 midi_config.channel = keycode - MIDI_CHANNEL_MIN;
135 dprintf("midi channel %d\n", midi_config.channel);
139 if (record->event.pressed) {
140 midi_config.channel--;
141 dprintf("midi channel %d\n", midi_config.channel);
145 if (record->event.pressed) {
146 midi_config.channel++;
147 dprintf("midi channel %d\n", midi_config.channel);
151 if (record->event.pressed) {
152 midi_send_cc(&midi_device, midi_config.channel, 0x7B, 0);
153 dprintf("midi off\n");
157 midi_send_cc(&midi_device, midi_config.channel, 0x40, record->event.pressed ? 127 : 0);
158 dprintf("midi sustain %d\n", record->event.pressed);
161 midi_send_cc(&midi_device, midi_config.channel, 0x41, record->event.pressed ? 127 : 0);
162 dprintf("midi portamento %d\n", record->event.pressed);
165 midi_send_cc(&midi_device, midi_config.channel, 0x42, record->event.pressed ? 127 : 0);
166 dprintf("midi sostenuto %d\n", record->event.pressed);
169 midi_send_cc(&midi_device, midi_config.channel, 0x43, record->event.pressed ? 127 : 0);
170 dprintf("midi soft %d\n", record->event.pressed);
173 midi_send_cc(&midi_device, midi_config.channel, 0x43, record->event.pressed ? 127 : 0);
174 dprintf("midi legato %d\n", record->event.pressed);
177 midi_modulation_step = record->event.pressed ? 1 : -1;
180 if (record->event.pressed) {
181 midi_config.modulation_interval++;
183 if (midi_config.modulation_interval == 0)
184 midi_config.modulation_interval--;
185 dprintf("midi modulation interval %d\n", midi_config.modulation_interval);
189 if (record->event.pressed && midi_config.modulation_interval > 0) {
190 midi_config.modulation_interval--;
191 dprintf("midi modulation interval %d\n", midi_config.modulation_interval);