diff options
| author | Zay950 <Zay950@users.noreply.github.com> | 2017-03-29 12:00:38 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-03-29 12:00:38 -0700 |
| commit | 2366ebfbbdeb6ec29cc9a0facda44d666305dd6e (patch) | |
| tree | 883efed0b7260f3143f5a2a879bc3844a8255e0b /quantum/process_keycode/process_midi.c | |
| parent | 80c5ada3394c5ad8087df00ef878eb2cbcd87d70 (diff) | |
| parent | 942f2ccee44bdb2e251553e9730cd8d59307d8b2 (diff) | |
| download | qmk_firmware-2366ebfbbdeb6ec29cc9a0facda44d666305dd6e.tar.gz qmk_firmware-2366ebfbbdeb6ec29cc9a0facda44d666305dd6e.zip | |
Merge branch 'master' into to_push
Diffstat (limited to 'quantum/process_keycode/process_midi.c')
| -rw-r--r-- | quantum/process_keycode/process_midi.c | 307 |
1 files changed, 246 insertions, 61 deletions
diff --git a/quantum/process_keycode/process_midi.c b/quantum/process_keycode/process_midi.c index 577dad43a..9184feaae 100644 --- a/quantum/process_keycode/process_midi.c +++ b/quantum/process_keycode/process_midi.c | |||
| @@ -1,68 +1,253 @@ | |||
| 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 | */ | ||
| 1 | #include "process_midi.h" | 16 | #include "process_midi.h" |
| 2 | 17 | ||
| 3 | bool midi_activated = false; | 18 | #ifdef MIDI_ENABLE |
| 4 | uint8_t midi_starting_note = 0x0C; | 19 | #include "midi.h" |
| 5 | int midi_offset = 7; | 20 | |
| 6 | 21 | #ifdef MIDI_BASIC | |
| 7 | bool process_midi(uint16_t keycode, keyrecord_t *record) { | 22 | |
| 8 | if (keycode == MI_ON && record->event.pressed) { | 23 | void process_midi_basic_noteon(uint8_t note) |
| 9 | midi_activated = true; | 24 | { |
| 10 | #ifdef AUDIO_ENABLE | 25 | midi_send_noteon(&midi_device, 0, note, 128); |
| 11 | music_scale_user(); | 26 | } |
| 12 | #endif | 27 | |
| 13 | return false; | 28 | void process_midi_basic_noteoff(uint8_t note) |
| 14 | } | 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 | } | ||
| 15 | 54 | ||
| 16 | if (keycode == MI_OFF && record->event.pressed) { | 55 | void midi_init(void) |
| 17 | midi_activated = false; | 56 | { |
| 18 | midi_send_cc(&midi_device, 0, 0x7B, 0); | 57 | midi_config.octave = MI_OCT_2 - MIDI_OCTAVE_MIN; |
| 19 | return false; | 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; | ||
| 20 | } | 66 | } |
| 21 | 67 | ||
| 22 | if (midi_activated) { | 68 | midi_modulation = 0; |
| 23 | if (record->event.key.col == (MATRIX_COLS - 1) && record->event.key.row == (MATRIX_ROWS - 1)) { | 69 | midi_modulation_step = 0; |
| 24 | if (record->event.pressed) { | 70 | midi_modulation_timer = 0; |
| 25 | midi_starting_note++; // Change key | 71 | } |
| 26 | midi_send_cc(&midi_device, 0, 0x7B, 0); | 72 | |
| 27 | } | 73 | void midi_task(void) |
| 28 | return false; | 74 | { |
| 29 | } | 75 | if (timer_elapsed(midi_modulation_timer) < midi_config.modulation_interval) |
| 30 | if (record->event.key.col == (MATRIX_COLS - 2) && record->event.key.row == (MATRIX_ROWS - 1)) { | 76 | return; |
| 31 | if (record->event.pressed) { | 77 | midi_modulation_timer = timer_read(); |
| 32 | midi_starting_note--; // Change key | 78 | |
| 33 | midi_send_cc(&midi_device, 0, 0x7B, 0); | 79 | if (midi_modulation_step != 0) |
| 34 | } | 80 | { |
| 35 | return false; | 81 | dprintf("midi modulation %d\n", midi_modulation); |
| 36 | } | 82 | midi_send_cc(&midi_device, midi_config.channel, 0x1, midi_modulation); |
| 37 | if (record->event.key.col == (MATRIX_COLS - 3) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) { | 83 | |
| 38 | midi_offset++; // Change scale | 84 | if (midi_modulation_step < 0 && midi_modulation < -midi_modulation_step) { |
| 39 | midi_send_cc(&midi_device, 0, 0x7B, 0); | 85 | midi_modulation = 0; |
| 40 | return false; | 86 | midi_modulation_step = 0; |
| 41 | } | 87 | return; |
| 42 | if (record->event.key.col == (MATRIX_COLS - 4) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) { | 88 | } |
| 43 | midi_offset--; // Change scale | 89 | |
| 44 | midi_send_cc(&midi_device, 0, 0x7B, 0); | 90 | midi_modulation += midi_modulation_step; |
| 45 | return false; | 91 | |
| 46 | } | 92 | if (midi_modulation > 127) |
| 47 | // basic | 93 | midi_modulation = 127; |
| 48 | // uint8_t note = (midi_starting_note + SCALE[record->event.key.col + midi_offset])+12*(MATRIX_ROWS - record->event.key.row); | ||
| 49 | // advanced | ||
| 50 | // uint8_t note = (midi_starting_note + record->event.key.col + midi_offset)+12*(MATRIX_ROWS - record->event.key.row); | ||
| 51 | // guitar | ||
| 52 | uint8_t note = (midi_starting_note + record->event.key.col + midi_offset)+5*(MATRIX_ROWS - record->event.key.row); | ||
| 53 | // violin | ||
| 54 | // uint8_t note = (midi_starting_note + record->event.key.col + midi_offset)+7*(MATRIX_ROWS - record->event.key.row); | ||
| 55 | |||
| 56 | if (record->event.pressed) { | ||
| 57 | // midi_send_noteon(&midi_device, record->event.key.row, midi_starting_note + SCALE[record->event.key.col], 127); | ||
| 58 | midi_send_noteon(&midi_device, 0, note, 127); | ||
| 59 | } else { | ||
| 60 | // midi_send_noteoff(&midi_device, record->event.key.row, midi_starting_note + SCALE[record->event.key.col], 127); | ||
| 61 | midi_send_noteoff(&midi_device, 0, note, 127); | ||
| 62 | } | ||
| 63 | |||
| 64 | if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through | ||
| 65 | return false; | ||
| 66 | } | 94 | } |
| 67 | return true; | ||
| 68 | } | 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 | ||
