]> git.donarmstrong.com Git - qmk_firmware.git/blobdiff - quantum/process_keycode/process_midi.c
Chimera Ortho Readme (#1943)
[qmk_firmware.git] / quantum / process_keycode / process_midi.c
index d09aa0b3821aeb53abf062ab440f47716152f604..9184feaae846027500a1d302592567e00e825f96 100644 (file)
@@ -1,21 +1,46 @@
+/* Copyright 2016 Jack Humbert
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
 #include "process_midi.h"
-#include "timer.h"
 
-typedef union {
-  uint16_t raw;
-  struct {
-    uint8_t octave   :4;
-    uint8_t velocity :4;
-    uint8_t channel  :4;
-    uint8_t modulation_interval  :4;
-  };
-} midi_config_t;
+#ifdef MIDI_ENABLE
+#include "midi.h"
+
+#ifdef MIDI_BASIC
 
-midi_config_t midi_config;
+void process_midi_basic_noteon(uint8_t note) 
+{
+    midi_send_noteon(&midi_device, 0, note, 128);
+}
+
+void process_midi_basic_noteoff(uint8_t note)
+{
+    midi_send_noteoff(&midi_device, 0, note, 0);
+}
 
-#define MIDI_INVALID_NOTE 0xFF
+void process_midi_all_notes_off(void)
+{
+    midi_send_cc(&midi_device, 0, 0x7B, 0);
+}
+
+#endif // MIDI_BASIC
+
+#ifdef MIDI_ADVANCED
+
+#include "timer.h"
 
-#define MIDI_TONE_COUNT (MIDI_TONE_MAX - MIDI_TONE_MIN + 1)
 static uint8_t tone_status[MIDI_TONE_COUNT];
 
 static uint8_t midi_modulation;
@@ -29,7 +54,8 @@ inline uint8_t compute_velocity(uint8_t setting)
 
 void midi_init(void)
 {
-    midi_config.octave = MI_OCT_0 - MIDI_OCTAVE_MIN;
+    midi_config.octave = MI_OCT_2 - MIDI_OCTAVE_MIN;
+    midi_config.transpose = 0;
     midi_config.velocity = (MIDI_VELOCITY_MAX - MIDI_VELOCITY_MIN);
     midi_config.channel = 0;
     midi_config.modulation_interval = 8;
@@ -68,6 +94,11 @@ void midi_task(void)
     }
 }
 
+uint8_t midi_compute_note(uint16_t keycode)
+{
+    return 12 * midi_config.octave + (keycode - MIDI_TONE_MIN) + midi_config.transpose;
+}
+
 bool process_midi(uint16_t keycode, keyrecord_t *record)
 {
     switch (keycode) {
@@ -77,7 +108,7 @@ bool process_midi(uint16_t keycode, keyrecord_t *record)
             uint8_t tone = keycode - MIDI_TONE_MIN;
             uint8_t velocity = compute_velocity(midi_config.velocity);
             if (record->event.pressed) {
-                uint8_t note = 12 * midi_config.octave + tone;
+                uint8_t note = midi_compute_note(keycode);
                 midi_send_noteon(&midi_device, channel, note, velocity);
                 dprintf("midi noteon channel:%d note:%d velocity:%d\n", channel, note, velocity);
                 tone_status[tone] = note;
@@ -111,6 +142,27 @@ bool process_midi(uint16_t keycode, keyrecord_t *record)
                 dprintf("midi octave %d\n", midi_config.octave);
             }
             return false;
+        case MIDI_TRANSPOSE_MIN ... MIDI_TRANSPOSE_MAX:
+            if (record->event.pressed) {
+                midi_config.transpose = keycode - MI_TRNS_0;
+                dprintf("midi transpose %d\n", midi_config.transpose);
+            }
+            return false;
+        case MI_TRNSD:
+            if (record->event.pressed && midi_config.transpose > (MIDI_TRANSPOSE_MIN - MI_TRNS_0)) {
+                midi_config.transpose--;
+                dprintf("midi transpose %d\n", midi_config.transpose);
+            }
+            return false;
+        case MI_TRNSU:
+            if (record->event.pressed && midi_config.transpose < (MIDI_TRANSPOSE_MAX - MI_TRNS_0)) {
+                const bool positive = midi_config.transpose > 0;
+                midi_config.transpose++;
+                if (positive && midi_config.transpose < 0)
+                    midi_config.transpose--;
+                dprintf("midi transpose %d\n", midi_config.transpose);
+            }
+            return false;
         case MIDI_VELOCITY_MIN ... MIDI_VELOCITY_MAX:
             if (record->event.pressed) {
                 midi_config.velocity = keycode - MIDI_VELOCITY_MIN;
@@ -147,10 +199,10 @@ bool process_midi(uint16_t keycode, keyrecord_t *record)
                 dprintf("midi channel %d\n", midi_config.channel);
             }
             return false;
-        case MI_OFF:
+        case MI_ALLOFF:
             if (record->event.pressed) {
                 midi_send_cc(&midi_device, midi_config.channel, 0x7B, 0);
-                dprintf("midi off\n");
+                dprintf("midi all notes off\n");
             }
             return false;
         case MI_SUS:
@@ -195,3 +247,7 @@ bool process_midi(uint16_t keycode, keyrecord_t *record)
 
     return true;
 }
+
+#endif // MIDI_ADVANCED
+
+#endif // MIDI_ENABLE