1 /* Copyright 2016 Jack Humbert
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #include "process_midi.h"
23 void process_midi_basic_noteon(uint8_t note)
25 midi_send_noteon(&midi_device, 0, note, 128);
28 void process_midi_basic_noteoff(uint8_t note)
30 midi_send_noteoff(&midi_device, 0, note, 0);
33 void process_midi_all_notes_off(void)
35 midi_send_cc(&midi_device, 0, 0x7B, 0);
44 static uint8_t tone_status[MIDI_TONE_COUNT];
46 static uint8_t midi_modulation;
47 static int8_t midi_modulation_step;
48 static uint16_t midi_modulation_timer;
50 inline uint8_t compute_velocity(uint8_t setting)
52 return (setting + 1) * (128 / (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN + 1));
57 midi_config.octave = MI_OCT_2 - MIDI_OCTAVE_MIN;
58 midi_config.transpose = 0;
59 midi_config.velocity = (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN);
60 midi_config.channel = 0;
61 midi_config.modulation_interval = 8;
63 for (uint8_t i = 0; i < MIDI_TONE_COUNT; i++)
65 tone_status[i] = MIDI_INVALID_NOTE;
69 midi_modulation_step = 0;
70 midi_modulation_timer = 0;
75 if (timer_elapsed(midi_modulation_timer) < midi_config.modulation_interval)
77 midi_modulation_timer = timer_read();
79 if (midi_modulation_step != 0)
81 dprintf("midi modulation %d\n", midi_modulation);
82 midi_send_cc(&midi_device, midi_config.channel, 0x1, midi_modulation);
84 if (midi_modulation_step < 0 && midi_modulation < -midi_modulation_step) {
86 midi_modulation_step = 0;
90 midi_modulation += midi_modulation_step;
92 if (midi_modulation > 127)
93 midi_modulation = 127;
97 uint8_t midi_compute_note(uint16_t keycode)
99 return 12 * midi_config.octave + (keycode - MIDI_TONE_MIN) + midi_config.transpose;
102 bool process_midi(uint16_t keycode, keyrecord_t *record)
105 case MIDI_TONE_MIN ... MIDI_TONE_MAX:
107 uint8_t channel = midi_config.channel;
108 uint8_t tone = keycode - MIDI_TONE_MIN;
109 uint8_t velocity = compute_velocity(midi_config.velocity);
110 if (record->event.pressed) {
111 uint8_t note = midi_compute_note(keycode);
112 midi_send_noteon(&midi_device, channel, note, velocity);
113 dprintf("midi noteon channel:%d note:%d velocity:%d\n", channel, note, velocity);
114 tone_status[tone] = note;
117 uint8_t note = tone_status[tone];
118 if (note != MIDI_INVALID_NOTE)
120 midi_send_noteoff(&midi_device, channel, note, velocity);
121 dprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, velocity);
123 tone_status[tone] = MIDI_INVALID_NOTE;
127 case MIDI_OCTAVE_MIN ... MIDI_OCTAVE_MAX:
128 if (record->event.pressed) {
129 midi_config.octave = keycode - MIDI_OCTAVE_MIN;
130 dprintf("midi octave %d\n", midi_config.octave);
134 if (record->event.pressed && midi_config.octave > 0) {
135 midi_config.octave--;
136 dprintf("midi octave %d\n", midi_config.octave);
140 if (record->event.pressed && midi_config.octave < (MIDI_OCTAVE_MAX - MIDI_OCTAVE_MIN)) {
141 midi_config.octave++;
142 dprintf("midi octave %d\n", midi_config.octave);
145 case MIDI_TRANSPOSE_MIN ... MIDI_TRANSPOSE_MAX:
146 if (record->event.pressed) {
147 midi_config.transpose = keycode - MI_TRNS_0;
148 dprintf("midi transpose %d\n", midi_config.transpose);
152 if (record->event.pressed && midi_config.transpose > (MIDI_TRANSPOSE_MIN - MI_TRNS_0)) {
153 midi_config.transpose--;
154 dprintf("midi transpose %d\n", midi_config.transpose);
158 if (record->event.pressed && midi_config.transpose < (MIDI_TRANSPOSE_MAX - MI_TRNS_0)) {
159 const bool positive = midi_config.transpose > 0;
160 midi_config.transpose++;
161 if (positive && midi_config.transpose < 0)
162 midi_config.transpose--;
163 dprintf("midi transpose %d\n", midi_config.transpose);
166 case MIDI_VELOCITY_MIN ... MIDI_VELOCITY_MAX:
167 if (record->event.pressed) {
168 midi_config.velocity = keycode - MIDI_VELOCITY_MIN;
169 dprintf("midi velocity %d\n", midi_config.velocity);
173 if (record->event.pressed && midi_config.velocity > 0) {
174 midi_config.velocity--;
175 dprintf("midi velocity %d\n", midi_config.velocity);
179 if (record->event.pressed) {
180 midi_config.velocity++;
181 dprintf("midi velocity %d\n", midi_config.velocity);
184 case MIDI_CHANNEL_MIN ... MIDI_CHANNEL_MAX:
185 if (record->event.pressed) {
186 midi_config.channel = keycode - MIDI_CHANNEL_MIN;
187 dprintf("midi channel %d\n", midi_config.channel);
191 if (record->event.pressed) {
192 midi_config.channel--;
193 dprintf("midi channel %d\n", midi_config.channel);
197 if (record->event.pressed) {
198 midi_config.channel++;
199 dprintf("midi channel %d\n", midi_config.channel);
203 if (record->event.pressed) {
204 midi_send_cc(&midi_device, midi_config.channel, 0x7B, 0);
205 dprintf("midi all notes off\n");
209 midi_send_cc(&midi_device, midi_config.channel, 0x40, record->event.pressed ? 127 : 0);
210 dprintf("midi sustain %d\n", record->event.pressed);
213 midi_send_cc(&midi_device, midi_config.channel, 0x41, record->event.pressed ? 127 : 0);
214 dprintf("midi portamento %d\n", record->event.pressed);
217 midi_send_cc(&midi_device, midi_config.channel, 0x42, record->event.pressed ? 127 : 0);
218 dprintf("midi sostenuto %d\n", record->event.pressed);
221 midi_send_cc(&midi_device, midi_config.channel, 0x43, record->event.pressed ? 127 : 0);
222 dprintf("midi soft %d\n", record->event.pressed);
225 midi_send_cc(&midi_device, midi_config.channel, 0x43, record->event.pressed ? 127 : 0);
226 dprintf("midi legato %d\n", record->event.pressed);
229 midi_modulation_step = record->event.pressed ? 1 : -1;
232 if (record->event.pressed) {
233 midi_config.modulation_interval++;
235 if (midi_config.modulation_interval == 0)
236 midi_config.modulation_interval--;
237 dprintf("midi modulation interval %d\n", midi_config.modulation_interval);
241 if (record->event.pressed && midi_config.modulation_interval > 0) {
242 midi_config.modulation_interval--;
243 dprintf("midi modulation interval %d\n", midi_config.modulation_interval);
251 #endif // MIDI_ADVANCED
253 #endif // MIDI_ENABLE