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"
19 #include <LUFA/Drivers/USB/USB.h>
25 void process_midi_basic_noteon(uint8_t note)
27 midi_send_noteon(&midi_device, 0, note, 128);
30 void process_midi_basic_noteoff(uint8_t note)
32 midi_send_noteoff(&midi_device, 0, note, 0);
35 void process_midi_all_notes_off(void)
37 midi_send_cc(&midi_device, 0, 0x7B, 0);
46 static uint8_t tone_status[MIDI_TONE_COUNT];
48 static uint8_t midi_modulation;
49 static int8_t midi_modulation_step;
50 static uint16_t midi_modulation_timer;
51 midi_config_t midi_config;
53 inline uint8_t compute_velocity(uint8_t setting)
55 return (setting + 1) * (128 / (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN + 1));
60 midi_config.octave = MI_OCT_2 - MIDI_OCTAVE_MIN;
61 midi_config.transpose = 0;
62 midi_config.velocity = (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN);
63 midi_config.channel = 0;
64 midi_config.modulation_interval = 8;
66 for (uint8_t i = 0; i < MIDI_TONE_COUNT; i++)
68 tone_status[i] = MIDI_INVALID_NOTE;
72 midi_modulation_step = 0;
73 midi_modulation_timer = 0;
76 uint8_t midi_compute_note(uint16_t keycode)
78 return 12 * midi_config.octave + (keycode - MIDI_TONE_MIN) + midi_config.transpose;
81 bool process_midi(uint16_t keycode, keyrecord_t *record)
84 case MIDI_TONE_MIN ... MIDI_TONE_MAX:
86 uint8_t channel = midi_config.channel;
87 uint8_t tone = keycode - MIDI_TONE_MIN;
88 uint8_t velocity = compute_velocity(midi_config.velocity);
89 if (record->event.pressed) {
90 uint8_t note = midi_compute_note(keycode);
91 midi_send_noteon(&midi_device, channel, note, velocity);
92 dprintf("midi noteon channel:%d note:%d velocity:%d\n", channel, note, velocity);
93 tone_status[tone] = note;
96 uint8_t note = tone_status[tone];
97 if (note != MIDI_INVALID_NOTE)
99 midi_send_noteoff(&midi_device, channel, note, velocity);
100 dprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, velocity);
102 tone_status[tone] = MIDI_INVALID_NOTE;
106 case MIDI_OCTAVE_MIN ... MIDI_OCTAVE_MAX:
107 if (record->event.pressed) {
108 midi_config.octave = keycode - MIDI_OCTAVE_MIN;
109 dprintf("midi octave %d\n", midi_config.octave);
113 if (record->event.pressed && midi_config.octave > 0) {
114 midi_config.octave--;
115 dprintf("midi octave %d\n", midi_config.octave);
119 if (record->event.pressed && midi_config.octave < (MIDI_OCTAVE_MAX - MIDI_OCTAVE_MIN)) {
120 midi_config.octave++;
121 dprintf("midi octave %d\n", midi_config.octave);
124 case MIDI_TRANSPOSE_MIN ... MIDI_TRANSPOSE_MAX:
125 if (record->event.pressed) {
126 midi_config.transpose = keycode - MI_TRNS_0;
127 dprintf("midi transpose %d\n", midi_config.transpose);
131 if (record->event.pressed && midi_config.transpose > (MIDI_TRANSPOSE_MIN - MI_TRNS_0)) {
132 midi_config.transpose--;
133 dprintf("midi transpose %d\n", midi_config.transpose);
137 if (record->event.pressed && midi_config.transpose < (MIDI_TRANSPOSE_MAX - MI_TRNS_0)) {
138 const bool positive = midi_config.transpose > 0;
139 midi_config.transpose++;
140 if (positive && midi_config.transpose < 0)
141 midi_config.transpose--;
142 dprintf("midi transpose %d\n", midi_config.transpose);
145 case MIDI_VELOCITY_MIN ... MIDI_VELOCITY_MAX:
146 if (record->event.pressed) {
147 midi_config.velocity = keycode - MIDI_VELOCITY_MIN;
148 dprintf("midi velocity %d\n", midi_config.velocity);
152 if (record->event.pressed && midi_config.velocity > 0) {
153 midi_config.velocity--;
154 dprintf("midi velocity %d\n", midi_config.velocity);
158 if (record->event.pressed) {
159 midi_config.velocity++;
160 dprintf("midi velocity %d\n", midi_config.velocity);
163 case MIDI_CHANNEL_MIN ... MIDI_CHANNEL_MAX:
164 if (record->event.pressed) {
165 midi_config.channel = keycode - MIDI_CHANNEL_MIN;
166 dprintf("midi channel %d\n", midi_config.channel);
170 if (record->event.pressed) {
171 midi_config.channel--;
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_send_cc(&midi_device, midi_config.channel, 0x7B, 0);
184 dprintf("midi all notes off\n");
188 midi_send_cc(&midi_device, midi_config.channel, 0x40, record->event.pressed ? 127 : 0);
189 dprintf("midi sustain %d\n", record->event.pressed);
192 midi_send_cc(&midi_device, midi_config.channel, 0x41, record->event.pressed ? 127 : 0);
193 dprintf("midi portamento %d\n", record->event.pressed);
196 midi_send_cc(&midi_device, midi_config.channel, 0x42, record->event.pressed ? 127 : 0);
197 dprintf("midi sostenuto %d\n", record->event.pressed);
200 midi_send_cc(&midi_device, midi_config.channel, 0x43, record->event.pressed ? 127 : 0);
201 dprintf("midi soft %d\n", record->event.pressed);
204 midi_send_cc(&midi_device, midi_config.channel, 0x43, record->event.pressed ? 127 : 0);
205 dprintf("midi legato %d\n", record->event.pressed);
208 midi_modulation_step = record->event.pressed ? 1 : -1;
211 if (record->event.pressed) {
212 midi_config.modulation_interval++;
214 if (midi_config.modulation_interval == 0)
215 midi_config.modulation_interval--;
216 dprintf("midi modulation interval %d\n", midi_config.modulation_interval);
220 if (record->event.pressed && midi_config.modulation_interval > 0) {
221 midi_config.modulation_interval--;
222 dprintf("midi modulation interval %d\n", midi_config.modulation_interval);
230 #endif // MIDI_ADVANCED
234 midi_device_process(&midi_device);
236 if (timer_elapsed(midi_modulation_timer) < midi_config.modulation_interval)
238 midi_modulation_timer = timer_read();
240 if (midi_modulation_step != 0)
242 dprintf("midi modulation %d\n", midi_modulation);
243 midi_send_cc(&midi_device, midi_config.channel, 0x1, midi_modulation);
245 if (midi_modulation_step < 0 && midi_modulation < -midi_modulation_step) {
247 midi_modulation_step = 0;
251 midi_modulation += midi_modulation_step;
253 if (midi_modulation > 127)
254 midi_modulation = 127;
261 #endif // MIDI_ENABLE