diff options
author | skullY <skullydazed@gmail.com> | 2019-08-30 11:19:03 -0700 |
---|---|---|
committer | skullydazed <skullydazed@users.noreply.github.com> | 2019-08-30 15:01:52 -0700 |
commit | b624f32f944acdc59dcb130674c09090c5c404cb (patch) | |
tree | bc13adbba137d122d9a2c2fb2fafcbb08ac10e25 /quantum/process_keycode/process_midi.c | |
parent | 61af76a10d00aba185b8338604171de490a13e3b (diff) | |
download | qmk_firmware-b624f32f944acdc59dcb130674c09090c5c404cb.tar.gz qmk_firmware-b624f32f944acdc59dcb130674c09090c5c404cb.zip |
clang-format changes
Diffstat (limited to 'quantum/process_keycode/process_midi.c')
-rw-r--r-- | quantum/process_keycode/process_midi.c | 115 |
1 files changed, 42 insertions, 73 deletions
diff --git a/quantum/process_keycode/process_midi.c b/quantum/process_keycode/process_midi.c index be6455ee9..b2fb902eb 100644 --- a/quantum/process_keycode/process_midi.c +++ b/quantum/process_keycode/process_midi.c | |||
@@ -16,86 +16,65 @@ | |||
16 | #include "process_midi.h" | 16 | #include "process_midi.h" |
17 | 17 | ||
18 | #ifdef MIDI_ENABLE | 18 | #ifdef MIDI_ENABLE |
19 | #include <LUFA/Drivers/USB/USB.h> | 19 | # include <LUFA/Drivers/USB/USB.h> |
20 | #include "midi.h" | 20 | # include "midi.h" |
21 | #include "qmk_midi.h" | 21 | # include "qmk_midi.h" |
22 | 22 | ||
23 | #ifdef MIDI_BASIC | 23 | # ifdef MIDI_BASIC |
24 | 24 | ||
25 | void process_midi_basic_noteon(uint8_t note) | 25 | void process_midi_basic_noteon(uint8_t note) { midi_send_noteon(&midi_device, 0, note, 127); } |
26 | { | ||
27 | midi_send_noteon(&midi_device, 0, note, 127); | ||
28 | } | ||
29 | 26 | ||
30 | void process_midi_basic_noteoff(uint8_t note) | 27 | void process_midi_basic_noteoff(uint8_t note) { midi_send_noteoff(&midi_device, 0, note, 0); } |
31 | { | ||
32 | midi_send_noteoff(&midi_device, 0, note, 0); | ||
33 | } | ||
34 | 28 | ||
35 | void process_midi_all_notes_off(void) | 29 | void process_midi_all_notes_off(void) { midi_send_cc(&midi_device, 0, 0x7B, 0); } |
36 | { | ||
37 | midi_send_cc(&midi_device, 0, 0x7B, 0); | ||
38 | } | ||
39 | 30 | ||
40 | #endif // MIDI_BASIC | 31 | # endif // MIDI_BASIC |
41 | 32 | ||
42 | #ifdef MIDI_ADVANCED | 33 | # ifdef MIDI_ADVANCED |
43 | 34 | ||
44 | #include "timer.h" | 35 | # include "timer.h" |
45 | 36 | ||
46 | static uint8_t tone_status[MIDI_TONE_COUNT]; | 37 | static uint8_t tone_status[MIDI_TONE_COUNT]; |
47 | 38 | ||
48 | static uint8_t midi_modulation; | 39 | static uint8_t midi_modulation; |
49 | static int8_t midi_modulation_step; | 40 | static int8_t midi_modulation_step; |
50 | static uint16_t midi_modulation_timer; | 41 | static uint16_t midi_modulation_timer; |
51 | midi_config_t midi_config; | 42 | midi_config_t midi_config; |
52 | 43 | ||
53 | inline uint8_t compute_velocity(uint8_t setting) | 44 | inline uint8_t compute_velocity(uint8_t setting) { return (setting + 1) * (128 / (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN + 1)); } |
54 | { | ||
55 | return (setting + 1) * (128 / (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN + 1)); | ||
56 | } | ||
57 | 45 | ||
58 | void midi_init(void) | 46 | void midi_init(void) { |
59 | { | 47 | midi_config.octave = MI_OCT_2 - MIDI_OCTAVE_MIN; |
60 | midi_config.octave = MI_OCT_2 - MIDI_OCTAVE_MIN; | 48 | midi_config.transpose = 0; |
61 | midi_config.transpose = 0; | 49 | midi_config.velocity = (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN); |
62 | midi_config.velocity = (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN); | 50 | midi_config.channel = 0; |
63 | midi_config.channel = 0; | ||
64 | midi_config.modulation_interval = 8; | 51 | midi_config.modulation_interval = 8; |
65 | 52 | ||
66 | for (uint8_t i = 0; i < MIDI_TONE_COUNT; i++) | 53 | for (uint8_t i = 0; i < MIDI_TONE_COUNT; i++) { |
67 | { | ||
68 | tone_status[i] = MIDI_INVALID_NOTE; | 54 | tone_status[i] = MIDI_INVALID_NOTE; |
69 | } | 55 | } |
70 | 56 | ||
71 | midi_modulation = 0; | 57 | midi_modulation = 0; |
72 | midi_modulation_step = 0; | 58 | midi_modulation_step = 0; |
73 | midi_modulation_timer = 0; | 59 | midi_modulation_timer = 0; |
74 | } | 60 | } |
75 | 61 | ||
76 | uint8_t midi_compute_note(uint16_t keycode) | 62 | uint8_t midi_compute_note(uint16_t keycode) { return 12 * midi_config.octave + (keycode - MIDI_TONE_MIN) + midi_config.transpose; } |
77 | { | ||
78 | return 12 * midi_config.octave + (keycode - MIDI_TONE_MIN) + midi_config.transpose; | ||
79 | } | ||
80 | 63 | ||
81 | bool process_midi(uint16_t keycode, keyrecord_t *record) | 64 | bool process_midi(uint16_t keycode, keyrecord_t *record) { |
82 | { | ||
83 | switch (keycode) { | 65 | switch (keycode) { |
84 | case MIDI_TONE_MIN ... MIDI_TONE_MAX: | 66 | case MIDI_TONE_MIN ... MIDI_TONE_MAX: { |
85 | { | 67 | uint8_t channel = midi_config.channel; |
86 | uint8_t channel = midi_config.channel; | 68 | uint8_t tone = keycode - MIDI_TONE_MIN; |
87 | uint8_t tone = keycode - MIDI_TONE_MIN; | ||
88 | uint8_t velocity = compute_velocity(midi_config.velocity); | 69 | uint8_t velocity = compute_velocity(midi_config.velocity); |
89 | if (record->event.pressed) { | 70 | if (record->event.pressed) { |
90 | uint8_t note = midi_compute_note(keycode); | 71 | uint8_t note = midi_compute_note(keycode); |
91 | midi_send_noteon(&midi_device, channel, note, velocity); | 72 | midi_send_noteon(&midi_device, channel, note, velocity); |
92 | dprintf("midi noteon channel:%d note:%d velocity:%d\n", channel, note, velocity); | 73 | dprintf("midi noteon channel:%d note:%d velocity:%d\n", channel, note, velocity); |
93 | tone_status[tone] = note; | 74 | tone_status[tone] = note; |
94 | } | 75 | } else { |
95 | else { | ||
96 | uint8_t note = tone_status[tone]; | 76 | uint8_t note = tone_status[tone]; |
97 | if (note != MIDI_INVALID_NOTE) | 77 | if (note != MIDI_INVALID_NOTE) { |
98 | { | ||
99 | midi_send_noteoff(&midi_device, channel, note, velocity); | 78 | midi_send_noteoff(&midi_device, channel, note, velocity); |
100 | dprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, velocity); | 79 | dprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, velocity); |
101 | } | 80 | } |
@@ -137,8 +116,7 @@ bool process_midi(uint16_t keycode, keyrecord_t *record) | |||
137 | if (record->event.pressed && midi_config.transpose < (MIDI_TRANSPOSE_MAX - MI_TRNS_0)) { | 116 | if (record->event.pressed && midi_config.transpose < (MIDI_TRANSPOSE_MAX - MI_TRNS_0)) { |
138 | const bool positive = midi_config.transpose > 0; | 117 | const bool positive = midi_config.transpose > 0; |
139 | midi_config.transpose++; | 118 | midi_config.transpose++; |
140 | if (positive && midi_config.transpose < 0) | 119 | if (positive && midi_config.transpose < 0) midi_config.transpose--; |
141 | midi_config.transpose--; | ||
142 | dprintf("midi transpose %d\n", midi_config.transpose); | 120 | dprintf("midi transpose %d\n", midi_config.transpose); |
143 | } | 121 | } |
144 | return false; | 122 | return false; |
@@ -211,8 +189,7 @@ bool process_midi(uint16_t keycode, keyrecord_t *record) | |||
211 | if (record->event.pressed) { | 189 | if (record->event.pressed) { |
212 | midi_config.modulation_interval++; | 190 | midi_config.modulation_interval++; |
213 | // prevent overflow | 191 | // prevent overflow |
214 | if (midi_config.modulation_interval == 0) | 192 | if (midi_config.modulation_interval == 0) midi_config.modulation_interval--; |
215 | midi_config.modulation_interval--; | ||
216 | dprintf("midi modulation interval %d\n", midi_config.modulation_interval); | 193 | dprintf("midi modulation interval %d\n", midi_config.modulation_interval); |
217 | } | 194 | } |
218 | return false; | 195 | return false; |
@@ -226,8 +203,7 @@ bool process_midi(uint16_t keycode, keyrecord_t *record) | |||
226 | if (record->event.pressed) { | 203 | if (record->event.pressed) { |
227 | midi_send_pitchbend(&midi_device, midi_config.channel, -0x2000); | 204 | midi_send_pitchbend(&midi_device, midi_config.channel, -0x2000); |
228 | dprintf("midi pitchbend channel:%d amount:%d\n", midi_config.channel, -0x2000); | 205 | dprintf("midi pitchbend channel:%d amount:%d\n", midi_config.channel, -0x2000); |
229 | } | 206 | } else { |
230 | else { | ||
231 | midi_send_pitchbend(&midi_device, midi_config.channel, 0); | 207 | midi_send_pitchbend(&midi_device, midi_config.channel, 0); |
232 | dprintf("midi pitchbend channel:%d amount:%d\n", midi_config.channel, 0); | 208 | dprintf("midi pitchbend channel:%d amount:%d\n", midi_config.channel, 0); |
233 | } | 209 | } |
@@ -236,8 +212,7 @@ bool process_midi(uint16_t keycode, keyrecord_t *record) | |||
236 | if (record->event.pressed) { | 212 | if (record->event.pressed) { |
237 | midi_send_pitchbend(&midi_device, midi_config.channel, 0x1fff); | 213 | midi_send_pitchbend(&midi_device, midi_config.channel, 0x1fff); |
238 | dprintf("midi pitchbend channel:%d amount:%d\n", midi_config.channel, 0x1fff); | 214 | dprintf("midi pitchbend channel:%d amount:%d\n", midi_config.channel, 0x1fff); |
239 | } | 215 | } else { |
240 | else { | ||
241 | midi_send_pitchbend(&midi_device, midi_config.channel, 0); | 216 | midi_send_pitchbend(&midi_device, midi_config.channel, 0); |
242 | dprintf("midi pitchbend channel:%d amount:%d\n", midi_config.channel, 0); | 217 | dprintf("midi pitchbend channel:%d amount:%d\n", midi_config.channel, 0); |
243 | } | 218 | } |
@@ -247,35 +222,29 @@ bool process_midi(uint16_t keycode, keyrecord_t *record) | |||
247 | return true; | 222 | return true; |
248 | } | 223 | } |
249 | 224 | ||
250 | #endif // MIDI_ADVANCED | 225 | # endif // MIDI_ADVANCED |
251 | 226 | ||
252 | void midi_task(void) | 227 | void midi_task(void) { |
253 | { | ||
254 | midi_device_process(&midi_device); | 228 | midi_device_process(&midi_device); |
255 | #ifdef MIDI_ADVANCED | 229 | # ifdef MIDI_ADVANCED |
256 | if (timer_elapsed(midi_modulation_timer) < midi_config.modulation_interval) | 230 | if (timer_elapsed(midi_modulation_timer) < midi_config.modulation_interval) return; |
257 | return; | ||
258 | midi_modulation_timer = timer_read(); | 231 | midi_modulation_timer = timer_read(); |
259 | 232 | ||
260 | if (midi_modulation_step != 0) | 233 | if (midi_modulation_step != 0) { |
261 | { | ||
262 | dprintf("midi modulation %d\n", midi_modulation); | 234 | dprintf("midi modulation %d\n", midi_modulation); |
263 | midi_send_cc(&midi_device, midi_config.channel, 0x1, midi_modulation); | 235 | midi_send_cc(&midi_device, midi_config.channel, 0x1, midi_modulation); |
264 | 236 | ||
265 | if (midi_modulation_step < 0 && midi_modulation < -midi_modulation_step) { | 237 | if (midi_modulation_step < 0 && midi_modulation < -midi_modulation_step) { |
266 | midi_modulation = 0; | 238 | midi_modulation = 0; |
267 | midi_modulation_step = 0; | 239 | midi_modulation_step = 0; |
268 | return; | 240 | return; |
269 | } | 241 | } |
270 | 242 | ||
271 | midi_modulation += midi_modulation_step; | 243 | midi_modulation += midi_modulation_step; |
272 | 244 | ||
273 | if (midi_modulation > 127) | 245 | if (midi_modulation > 127) midi_modulation = 127; |
274 | midi_modulation = 127; | ||
275 | } | 246 | } |
276 | #endif | 247 | # endif |
277 | } | 248 | } |
278 | 249 | ||
279 | 250 | #endif // MIDI_ENABLE | |
280 | |||
281 | #endif // MIDI_ENABLE | ||