1 #define MIDI_TONE_KEYCODE_OCTAVES 2
3 #include "process_midi.h"
6 static uint8_t tone_status[MIDI_TONE_COUNT];
8 static uint8_t midi_modulation;
9 static int8_t midi_modulation_step;
10 static uint16_t midi_modulation_timer;
12 inline uint8_t compute_velocity(uint8_t setting)
14 return (setting + 1) * (128 / (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN + 1));
19 midi_config.octave = MI_OCT_2 - MIDI_OCTAVE_MIN;
20 midi_config.transpose = 0;
21 midi_config.velocity = (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN);
22 midi_config.channel = 0;
23 midi_config.modulation_interval = 8;
25 for (uint8_t i = 0; i < MIDI_TONE_COUNT; i++)
27 tone_status[i] = MIDI_INVALID_NOTE;
31 midi_modulation_step = 0;
32 midi_modulation_timer = 0;
37 if (timer_elapsed(midi_modulation_timer) < midi_config.modulation_interval)
39 midi_modulation_timer = timer_read();
41 if (midi_modulation_step != 0)
43 dprintf("midi modulation %d\n", midi_modulation);
44 midi_send_cc(&midi_device, midi_config.channel, 0x1, midi_modulation);
46 if (midi_modulation_step < 0 && midi_modulation < -midi_modulation_step) {
48 midi_modulation_step = 0;
52 midi_modulation += midi_modulation_step;
54 if (midi_modulation > 127)
55 midi_modulation = 127;
59 uint8_t midi_compute_note(uint16_t keycode)
61 return 12 * midi_config.octave + (keycode - MIDI_TONE_MIN) + midi_config.transpose;
64 bool process_midi(uint16_t keycode, keyrecord_t *record)
67 case MIDI_TONE_MIN ... MIDI_TONE_MAX:
69 uint8_t channel = midi_config.channel;
70 uint8_t tone = keycode - MIDI_TONE_MIN;
71 uint8_t velocity = compute_velocity(midi_config.velocity);
72 if (record->event.pressed) {
73 uint8_t note = midi_compute_note(keycode);
74 midi_send_noteon(&midi_device, channel, note, velocity);
75 dprintf("midi noteon channel:%d note:%d velocity:%d\n", channel, note, velocity);
76 tone_status[tone] = note;
79 uint8_t note = tone_status[tone];
80 if (note != MIDI_INVALID_NOTE)
82 midi_send_noteoff(&midi_device, channel, note, velocity);
83 dprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, velocity);
85 tone_status[tone] = MIDI_INVALID_NOTE;
89 case MIDI_OCTAVE_MIN ... MIDI_OCTAVE_MAX:
90 if (record->event.pressed) {
91 midi_config.octave = keycode - MIDI_OCTAVE_MIN;
92 dprintf("midi octave %d\n", midi_config.octave);
96 if (record->event.pressed && midi_config.octave > 0) {
98 dprintf("midi octave %d\n", midi_config.octave);
102 if (record->event.pressed && midi_config.octave < (MIDI_OCTAVE_MAX - MIDI_OCTAVE_MIN)) {
103 midi_config.octave++;
104 dprintf("midi octave %d\n", midi_config.octave);
107 case MIDI_TRANSPOSE_MIN ... MIDI_TRANSPOSE_MAX:
108 if (record->event.pressed) {
109 midi_config.transpose = keycode - MI_TRNS_0;
110 dprintf("midi transpose %d\n", midi_config.transpose);
114 if (record->event.pressed && midi_config.transpose > (MIDI_TRANSPOSE_MIN - MI_TRNS_0)) {
115 midi_config.transpose--;
116 dprintf("midi transpose %d\n", midi_config.transpose);
120 if (record->event.pressed && midi_config.transpose < (MIDI_TRANSPOSE_MAX - MI_TRNS_0)) {
121 const bool positive = midi_config.transpose > 0;
122 midi_config.transpose++;
123 if (positive && midi_config.transpose < 0)
124 midi_config.transpose--;
125 dprintf("midi transpose %d\n", midi_config.transpose);
128 case MIDI_VELOCITY_MIN ... MIDI_VELOCITY_MAX:
129 if (record->event.pressed) {
130 midi_config.velocity = keycode - MIDI_VELOCITY_MIN;
131 dprintf("midi velocity %d\n", midi_config.velocity);
135 if (record->event.pressed && midi_config.velocity > 0) {
136 midi_config.velocity--;
137 dprintf("midi velocity %d\n", midi_config.velocity);
141 if (record->event.pressed) {
142 midi_config.velocity++;
143 dprintf("midi velocity %d\n", midi_config.velocity);
146 case MIDI_CHANNEL_MIN ... MIDI_CHANNEL_MAX:
147 if (record->event.pressed) {
148 midi_config.channel = keycode - MIDI_CHANNEL_MIN;
149 dprintf("midi channel %d\n", midi_config.channel);
153 if (record->event.pressed) {
154 midi_config.channel--;
155 dprintf("midi channel %d\n", midi_config.channel);
159 if (record->event.pressed) {
160 midi_config.channel++;
161 dprintf("midi channel %d\n", midi_config.channel);
165 if (record->event.pressed) {
166 midi_send_cc(&midi_device, midi_config.channel, 0x7B, 0);
167 dprintf("midi off\n");
171 midi_send_cc(&midi_device, midi_config.channel, 0x40, record->event.pressed ? 127 : 0);
172 dprintf("midi sustain %d\n", record->event.pressed);
175 midi_send_cc(&midi_device, midi_config.channel, 0x41, record->event.pressed ? 127 : 0);
176 dprintf("midi portamento %d\n", record->event.pressed);
179 midi_send_cc(&midi_device, midi_config.channel, 0x42, record->event.pressed ? 127 : 0);
180 dprintf("midi sostenuto %d\n", record->event.pressed);
183 midi_send_cc(&midi_device, midi_config.channel, 0x43, record->event.pressed ? 127 : 0);
184 dprintf("midi soft %d\n", record->event.pressed);
187 midi_send_cc(&midi_device, midi_config.channel, 0x43, record->event.pressed ? 127 : 0);
188 dprintf("midi legato %d\n", record->event.pressed);
191 midi_modulation_step = record->event.pressed ? 1 : -1;
194 if (record->event.pressed) {
195 midi_config.modulation_interval++;
197 if (midi_config.modulation_interval == 0)
198 midi_config.modulation_interval--;
199 dprintf("midi modulation interval %d\n", midi_config.modulation_interval);
203 if (record->event.pressed && midi_config.modulation_interval > 0) {
204 midi_config.modulation_interval--;
205 dprintf("midi modulation interval %d\n", midi_config.modulation_interval);