]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/process_keycode/process_midi.c
Do some cleanup for the API
[qmk_firmware.git] / quantum / process_keycode / process_midi.c
1 /* Copyright 2016 Jack Humbert
2  *
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.
7  *
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.
12  *
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/>.
15  */
16 #include "process_midi.h"
17
18 #ifdef MIDI_ENABLE
19 #include "midi.h"
20
21 #ifdef MIDI_BASIC
22
23 void process_midi_basic_noteon(uint8_t note) 
24 {
25     midi_send_noteon(&midi_device, 0, note, 128);
26 }
27
28 void process_midi_basic_noteoff(uint8_t note)
29 {
30     midi_send_noteoff(&midi_device, 0, note, 0);
31 }
32
33 void process_midi_all_notes_off(void)
34 {
35     midi_send_cc(&midi_device, 0, 0x7B, 0);
36 }
37
38 #endif // MIDI_BASIC
39
40 #ifdef MIDI_ADVANCED
41
42 #include "timer.h"
43
44 static uint8_t tone_status[MIDI_TONE_COUNT];
45
46 static uint8_t midi_modulation;
47 static int8_t midi_modulation_step;
48 static uint16_t midi_modulation_timer;
49
50 inline uint8_t compute_velocity(uint8_t setting)
51 {
52     return (setting + 1) * (128 / (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN + 1));
53 }
54
55 void midi_init(void)
56 {
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;
62
63     for (uint8_t i = 0; i < MIDI_TONE_COUNT; i++)
64     {
65         tone_status[i] = MIDI_INVALID_NOTE;
66     }
67
68     midi_modulation = 0;
69     midi_modulation_step = 0;
70     midi_modulation_timer = 0;
71 }
72
73 void midi_task(void)
74 {
75     if (timer_elapsed(midi_modulation_timer) < midi_config.modulation_interval)
76         return;
77     midi_modulation_timer = timer_read();
78
79     if (midi_modulation_step != 0)
80     {
81         dprintf("midi modulation %d\n", midi_modulation);
82         midi_send_cc(&midi_device, midi_config.channel, 0x1, midi_modulation);
83
84         if (midi_modulation_step < 0 && midi_modulation < -midi_modulation_step) {
85             midi_modulation = 0;
86             midi_modulation_step = 0;
87             return;
88         }
89
90         midi_modulation += midi_modulation_step;
91
92         if (midi_modulation > 127)
93             midi_modulation = 127;
94     }
95 }
96
97 uint8_t midi_compute_note(uint16_t keycode)
98 {
99     return 12 * midi_config.octave + (keycode - MIDI_TONE_MIN) + midi_config.transpose;
100 }
101
102 bool process_midi(uint16_t keycode, keyrecord_t *record)
103 {
104     switch (keycode) {
105         case MIDI_TONE_MIN ... MIDI_TONE_MAX:
106         {
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;
115             }
116             else {
117                 uint8_t note = tone_status[tone];
118                 if (note != MIDI_INVALID_NOTE)
119                 {
120                     midi_send_noteoff(&midi_device, channel, note, velocity);
121                     dprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, velocity);
122                 }
123                 tone_status[tone] = MIDI_INVALID_NOTE;
124             }
125             return false;
126         }
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);
131             }
132             return false;
133         case MI_OCTD:
134             if (record->event.pressed && midi_config.octave > 0) {
135                 midi_config.octave--;
136                 dprintf("midi octave %d\n", midi_config.octave);
137             }
138             return false;
139         case MI_OCTU:
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);
143             }
144             return false;
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);
149             }
150             return false;
151         case MI_TRNSD:
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);
155             }
156             return false;
157         case MI_TRNSU:
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);
164             }
165             return false;
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);
170             }
171             return false;
172         case MI_VELD:
173             if (record->event.pressed && midi_config.velocity > 0) {
174                 midi_config.velocity--;
175                 dprintf("midi velocity %d\n", midi_config.velocity);
176             }
177             return false;
178         case MI_VELU:
179             if (record->event.pressed) {
180                 midi_config.velocity++;
181                 dprintf("midi velocity %d\n", midi_config.velocity);
182             }
183             return false;
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);
188             }
189             return false;
190         case MI_CHD:
191             if (record->event.pressed) {
192                 midi_config.channel--;
193                 dprintf("midi channel %d\n", midi_config.channel);
194             }
195             return false;
196         case MI_CHU:
197             if (record->event.pressed) {
198                 midi_config.channel++;
199                 dprintf("midi channel %d\n", midi_config.channel);
200             }
201             return false;
202         case MI_ALLOFF:
203             if (record->event.pressed) {
204                 midi_send_cc(&midi_device, midi_config.channel, 0x7B, 0);
205                 dprintf("midi all notes off\n");
206             }
207             return false;
208         case MI_SUS:
209             midi_send_cc(&midi_device, midi_config.channel, 0x40, record->event.pressed ? 127 : 0);
210             dprintf("midi sustain %d\n", record->event.pressed);
211             return false;
212         case MI_PORT:
213             midi_send_cc(&midi_device, midi_config.channel, 0x41, record->event.pressed ? 127 : 0);
214             dprintf("midi portamento %d\n", record->event.pressed);
215             return false;
216         case MI_SOST:
217             midi_send_cc(&midi_device, midi_config.channel, 0x42, record->event.pressed ? 127 : 0);
218             dprintf("midi sostenuto %d\n", record->event.pressed);
219             return false;
220         case MI_SOFT:
221             midi_send_cc(&midi_device, midi_config.channel, 0x43, record->event.pressed ? 127 : 0);
222             dprintf("midi soft %d\n", record->event.pressed);
223             return false;
224         case MI_LEG:
225             midi_send_cc(&midi_device, midi_config.channel, 0x43, record->event.pressed ? 127 : 0);
226             dprintf("midi legato %d\n", record->event.pressed);
227             return false;
228         case MI_MOD:
229             midi_modulation_step = record->event.pressed ? 1 : -1;
230             return false;
231         case MI_MODSD:
232             if (record->event.pressed) {
233                 midi_config.modulation_interval++;
234                 // prevent overflow
235                 if (midi_config.modulation_interval == 0)
236                     midi_config.modulation_interval--;
237                 dprintf("midi modulation interval %d\n", midi_config.modulation_interval);
238             }
239             return false;
240         case MI_MODSU:
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);
244             }
245             return false;
246     };
247
248     return true;
249 }
250
251 #endif // MIDI_ADVANCED
252
253 #endif // MIDI_ENABLE