diff options
| author | Gabriel Young <gabeplaysdrums@live.com> | 2017-02-18 05:32:55 -0800 |
|---|---|---|
| committer | Gabriel Young <gabeplaysdrums@live.com> | 2017-02-19 16:43:02 -0800 |
| commit | dd8f8e6baeb1549735403edf2a2f04f07edb4bf2 (patch) | |
| tree | 459ba917de580e7bdb5b2bad9688486209b46825 /quantum/process_keycode/process_midi.c | |
| parent | 7c5e510fe2e57d1b3c0f98612f1f89d413c07525 (diff) | |
| download | qmk_firmware-dd8f8e6baeb1549735403edf2a2f04f07edb4bf2.tar.gz qmk_firmware-dd8f8e6baeb1549735403edf2a2f04f07edb4bf2.zip | |
implement modulation
Diffstat (limited to 'quantum/process_keycode/process_midi.c')
| -rw-r--r-- | quantum/process_keycode/process_midi.c | 58 |
1 files changed, 54 insertions, 4 deletions
diff --git a/quantum/process_keycode/process_midi.c b/quantum/process_keycode/process_midi.c index f7a8b6650..d09aa0b38 100644 --- a/quantum/process_keycode/process_midi.c +++ b/quantum/process_keycode/process_midi.c | |||
| @@ -1,4 +1,5 @@ | |||
| 1 | #include "process_midi.h" | 1 | #include "process_midi.h" |
| 2 | #include "timer.h" | ||
| 2 | 3 | ||
| 3 | typedef union { | 4 | typedef union { |
| 4 | uint16_t raw; | 5 | uint16_t raw; |
| @@ -6,6 +7,7 @@ typedef union { | |||
| 6 | uint8_t octave :4; | 7 | uint8_t octave :4; |
| 7 | uint8_t velocity :4; | 8 | uint8_t velocity :4; |
| 8 | uint8_t channel :4; | 9 | uint8_t channel :4; |
| 10 | uint8_t modulation_interval :4; | ||
| 9 | }; | 11 | }; |
| 10 | } midi_config_t; | 12 | } midi_config_t; |
| 11 | 13 | ||
| @@ -16,6 +18,10 @@ midi_config_t midi_config; | |||
| 16 | #define MIDI_TONE_COUNT (MIDI_TONE_MAX - MIDI_TONE_MIN + 1) | 18 | #define MIDI_TONE_COUNT (MIDI_TONE_MAX - MIDI_TONE_MIN + 1) |
| 17 | static uint8_t tone_status[MIDI_TONE_COUNT]; | 19 | static uint8_t tone_status[MIDI_TONE_COUNT]; |
| 18 | 20 | ||
| 21 | static uint8_t midi_modulation; | ||
| 22 | static int8_t midi_modulation_step; | ||
| 23 | static uint16_t midi_modulation_timer; | ||
| 24 | |||
| 19 | inline uint8_t compute_velocity(uint8_t setting) | 25 | inline uint8_t compute_velocity(uint8_t setting) |
| 20 | { | 26 | { |
| 21 | return (setting + 1) * (128 / (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN + 1)); | 27 | return (setting + 1) * (128 / (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN + 1)); |
| @@ -26,14 +32,40 @@ void midi_init(void) | |||
| 26 | midi_config.octave = MI_OCT_0 - MIDI_OCTAVE_MIN; | 32 | midi_config.octave = MI_OCT_0 - MIDI_OCTAVE_MIN; |
| 27 | midi_config.velocity = (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN); | 33 | midi_config.velocity = (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN); |
| 28 | midi_config.channel = 0; | 34 | midi_config.channel = 0; |
| 29 | #ifdef MIDI_USE_NOTE_ON_ARRAY | 35 | midi_config.modulation_interval = 8; |
| 30 | notes_on.length = 0; | 36 | |
| 31 | #else | ||
| 32 | for (uint8_t i = 0; i < MIDI_TONE_COUNT; i++) | 37 | for (uint8_t i = 0; i < MIDI_TONE_COUNT; i++) |
| 33 | { | 38 | { |
| 34 | tone_status[i] = MIDI_INVALID_NOTE; | 39 | tone_status[i] = MIDI_INVALID_NOTE; |
| 35 | } | 40 | } |
| 36 | #endif | 41 | |
| 42 | midi_modulation = 0; | ||
| 43 | midi_modulation_step = 0; | ||
| 44 | midi_modulation_timer = 0; | ||
| 45 | } | ||
| 46 | |||
| 47 | void midi_task(void) | ||
| 48 | { | ||
| 49 | if (timer_elapsed(midi_modulation_timer) < midi_config.modulation_interval) | ||
| 50 | return; | ||
| 51 | midi_modulation_timer = timer_read(); | ||
| 52 | |||
| 53 | if (midi_modulation_step != 0) | ||
| 54 | { | ||
| 55 | dprintf("midi modulation %d\n", midi_modulation); | ||
| 56 | midi_send_cc(&midi_device, midi_config.channel, 0x1, midi_modulation); | ||
| 57 | |||
| 58 | if (midi_modulation_step < 0 && midi_modulation < -midi_modulation_step) { | ||
| 59 | midi_modulation = 0; | ||
| 60 | midi_modulation_step = 0; | ||
| 61 | return; | ||
| 62 | } | ||
| 63 | |||
| 64 | midi_modulation += midi_modulation_step; | ||
| 65 | |||
| 66 | if (midi_modulation > 127) | ||
| 67 | midi_modulation = 127; | ||
| 68 | } | ||
| 37 | } | 69 | } |
| 38 | 70 | ||
| 39 | bool process_midi(uint16_t keycode, keyrecord_t *record) | 71 | bool process_midi(uint16_t keycode, keyrecord_t *record) |
| @@ -141,6 +173,24 @@ bool process_midi(uint16_t keycode, keyrecord_t *record) | |||
| 141 | midi_send_cc(&midi_device, midi_config.channel, 0x43, record->event.pressed ? 127 : 0); | 173 | midi_send_cc(&midi_device, midi_config.channel, 0x43, record->event.pressed ? 127 : 0); |
| 142 | dprintf("midi legato %d\n", record->event.pressed); | 174 | dprintf("midi legato %d\n", record->event.pressed); |
| 143 | return false; | 175 | return false; |
| 176 | case MI_MOD: | ||
| 177 | midi_modulation_step = record->event.pressed ? 1 : -1; | ||
| 178 | return false; | ||
| 179 | case MI_MODSD: | ||
| 180 | if (record->event.pressed) { | ||
| 181 | midi_config.modulation_interval++; | ||
| 182 | // prevent overflow | ||
| 183 | if (midi_config.modulation_interval == 0) | ||
| 184 | midi_config.modulation_interval--; | ||
| 185 | dprintf("midi modulation interval %d\n", midi_config.modulation_interval); | ||
| 186 | } | ||
| 187 | return false; | ||
| 188 | case MI_MODSU: | ||
| 189 | if (record->event.pressed && midi_config.modulation_interval > 0) { | ||
| 190 | midi_config.modulation_interval--; | ||
| 191 | dprintf("midi modulation interval %d\n", midi_config.modulation_interval); | ||
| 192 | } | ||
| 193 | return false; | ||
| 144 | }; | 194 | }; |
| 145 | 195 | ||
| 146 | return true; | 196 | return true; |
