]> git.donarmstrong.com Git - qmk_firmware.git/blobdiff - quantum/process_keycode/process_midi.c
Merge ChibiOS and LUFA descriptor support (#2362)
[qmk_firmware.git] / quantum / process_keycode / process_midi.c
index 4fbb288162d5a9d4d3b2c06c59b88b284489378a..9728076dfd3d58b063d14a339608b2a44d977a6c 100644 (file)
@@ -1,50 +1,54 @@
+/* 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"
 
-#if 0
-bool midi_activated = false;
-uint8_t midi_starting_note = 0x0C;
-int midi_offset = 7;
-#endif
-
-typedef union {
-  uint16_t raw;
-  struct {
-    uint8_t octave   :4;
-    uint8_t velocity :4;
-    uint8_t channel  :4;
-  };
-} midi_config_t;
-
-midi_config_t midi_config;
-
-#define MIDI_INVALID_NOTE 0xFF
+#ifdef MIDI_ENABLE
+#include <LUFA/Drivers/USB/USB.h>
+#include "midi.h"
+#include "qmk_midi.h"
 
-#define MIDI_USE_NOTE_ON_ARRAY
+#ifdef MIDI_BASIC
 
-#ifdef MIDI_USE_NOTE_ON_ARRAY
+void process_midi_basic_noteon(uint8_t note)
+{
+    midi_send_noteon(&midi_device, 0, note, 128);
+}
 
-#define MIDI_MAX_NOTES_ON 10
+void process_midi_basic_noteoff(uint8_t note)
+{
+    midi_send_noteoff(&midi_device, 0, note, 0);
+}
 
-typedef struct {
-    uint8_t note;
-    uint8_t tone;
-} midi_notes_on_array_entry_t;
+void process_midi_all_notes_off(void)
+{
+    midi_send_cc(&midi_device, 0, 0x7B, 0);
+}
 
-typedef struct {
-    uint8_t length;
-    midi_notes_on_array_entry_t values[MIDI_MAX_NOTES_ON];
-} midi_notes_on_array_t;
+#endif // MIDI_BASIC
 
-static midi_notes_on_array_t notes_on;
+#ifdef MIDI_ADVANCED
 
-#else
+#include "timer.h"
 
-#define MIDI_TONE_COUNT (MIDI_TONE_MAX - MIDI_TONE_MIN + 1)
 static uint8_t tone_status[MIDI_TONE_COUNT];
 
-#endif
-
-
+static uint8_t midi_modulation;
+static int8_t midi_modulation_step;
+static uint16_t midi_modulation_timer;
+midi_config_t midi_config;
 
 inline uint8_t compute_velocity(uint8_t setting)
 {
@@ -53,17 +57,25 @@ 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;
-    #ifdef MIDI_USE_NOTE_ON_ARRAY
-    notes_on.length = 0;
-    #else
+    midi_config.modulation_interval = 8;
+
     for (uint8_t i = 0; i < MIDI_TONE_COUNT; i++)
     {
         tone_status[i] = MIDI_INVALID_NOTE;
     }
-    #endif
+
+    midi_modulation = 0;
+    midi_modulation_step = 0;
+    midi_modulation_timer = 0;
+}
+
+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)
@@ -74,49 +86,13 @@ bool process_midi(uint16_t keycode, keyrecord_t *record)
             uint8_t channel = midi_config.channel;
             uint8_t tone = keycode - MIDI_TONE_MIN;
             uint8_t velocity = compute_velocity(midi_config.velocity);
-            #ifdef MIDI_USE_NOTE_ON_ARRAY
-            if (record->event.pressed && notes_on.length < MIDI_MAX_NOTES_ON) {
-            #else
             if (record->event.pressed) {
-            #endif
-                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);
-
-                #ifdef MIDI_USE_NOTE_ON_ARRAY
-                
-                notes_on.values[notes_on.length].note = note;
-                notes_on.values[notes_on.length].tone = tone;
-                notes_on.length++;
-                
-                #else
-
                 tone_status[tone] = note;
-
-                #endif
             }
             else {
-                
-                #ifdef MIDI_USE_NOTE_ON_ARRAY
-
-                for (uint8_t i = 0; i < notes_on.length; i++) {
-                    uint8_t note = notes_on.values[i].note;
-                    if (tone == notes_on.values[i].tone) {
-                        midi_send_noteoff(&midi_device, channel, note, velocity);
-                        dprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, velocity);
-
-                        for (uint8_t j=i; j < notes_on.length - 1; j++)
-                        {
-                            notes_on.values[j] = notes_on.values[j + 1];
-                        }
-
-                        notes_on.length--;
-                        break;
-                    }
-                }
-
-                #else
-
                 uint8_t note = tone_status[tone];
                 if (note != MIDI_INVALID_NOTE)
                 {
@@ -124,112 +100,162 @@ bool process_midi(uint16_t keycode, keyrecord_t *record)
                     dprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, velocity);
                 }
                 tone_status[tone] = MIDI_INVALID_NOTE;
-
-                #endif
             }
             return false;
         }
         case MIDI_OCTAVE_MIN ... MIDI_OCTAVE_MAX:
-            if (record->event.pressed)
+            if (record->event.pressed) {
                 midi_config.octave = keycode - MIDI_OCTAVE_MIN;
+                dprintf("midi octave %d\n", midi_config.octave);
+            }
             return false;
         case MI_OCTD:
-            if (record->event.pressed && midi_config.octave > 0)
+            if (record->event.pressed && midi_config.octave > 0) {
                 midi_config.octave--;
+                dprintf("midi octave %d\n", midi_config.octave);
+            }
             return false;
         case MI_OCTU:
-            if (record->event.pressed && midi_config.octave < (MIDI_OCTAVE_MAX - MIDI_OCTAVE_MIN))
+            if (record->event.pressed && midi_config.octave < (MIDI_OCTAVE_MAX - MIDI_OCTAVE_MIN)) {
                 midi_config.octave++;
+                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)
+            if (record->event.pressed) {
                 midi_config.velocity = keycode - MIDI_VELOCITY_MIN;
+                dprintf("midi velocity %d\n", midi_config.velocity);
+            }
             return false;
         case MI_VELD:
-            if (record->event.pressed && midi_config.velocity > 0)
+            if (record->event.pressed && midi_config.velocity > 0) {
                 midi_config.velocity--;
+                dprintf("midi velocity %d\n", midi_config.velocity);
+            }
             return false;
         case MI_VELU:
-            if (record->event.pressed)
+            if (record->event.pressed) {
                 midi_config.velocity++;
+                dprintf("midi velocity %d\n", midi_config.velocity);
+            }
             return false;
         case MIDI_CHANNEL_MIN ... MIDI_CHANNEL_MAX:
-            if (record->event.pressed)
+            if (record->event.pressed) {
                 midi_config.channel = keycode - MIDI_CHANNEL_MIN;
+                dprintf("midi channel %d\n", midi_config.channel);
+            }
             return false;
         case MI_CHD:
-            if (record->event.pressed)
+            if (record->event.pressed) {
                 midi_config.channel--;
+                dprintf("midi channel %d\n", midi_config.channel);
+            }
             return false;
         case MI_CHU:
-            if (record->event.pressed)
+            if (record->event.pressed) {
                 midi_config.channel++;
+                dprintf("midi channel %d\n", midi_config.channel);
+            }
+            return false;
+        case MI_ALLOFF:
+            if (record->event.pressed) {
+                midi_send_cc(&midi_device, midi_config.channel, 0x7B, 0);
+                dprintf("midi all notes off\n");
+            }
             return false;
         case MI_SUS:
-            //TODO
+            midi_send_cc(&midi_device, midi_config.channel, 0x40, record->event.pressed ? 127 : 0);
+            dprintf("midi sustain %d\n", record->event.pressed);
+            return false;
+        case MI_PORT:
+            midi_send_cc(&midi_device, midi_config.channel, 0x41, record->event.pressed ? 127 : 0);
+            dprintf("midi portamento %d\n", record->event.pressed);
+            return false;
+        case MI_SOST:
+            midi_send_cc(&midi_device, midi_config.channel, 0x42, record->event.pressed ? 127 : 0);
+            dprintf("midi sostenuto %d\n", record->event.pressed);
+            return false;
+        case MI_SOFT:
+            midi_send_cc(&midi_device, midi_config.channel, 0x43, record->event.pressed ? 127 : 0);
+            dprintf("midi soft %d\n", record->event.pressed);
+            return false;
+        case MI_LEG:
+            midi_send_cc(&midi_device, midi_config.channel, 0x43, record->event.pressed ? 127 : 0);
+            dprintf("midi legato %d\n", record->event.pressed);
+            return false;
+        case MI_MOD:
+            midi_modulation_step = record->event.pressed ? 1 : -1;
+            return false;
+        case MI_MODSD:
+            if (record->event.pressed) {
+                midi_config.modulation_interval++;
+                // prevent overflow
+                if (midi_config.modulation_interval == 0)
+                    midi_config.modulation_interval--;
+                dprintf("midi modulation interval %d\n", midi_config.modulation_interval);
+            }
+            return false;
+        case MI_MODSU:
+            if (record->event.pressed && midi_config.modulation_interval > 0) {
+                midi_config.modulation_interval--;
+                dprintf("midi modulation interval %d\n", midi_config.modulation_interval);
+            }
             return false;
     };
 
-#if 0
-    if (keycode == MI_ON && record->event.pressed) {
-      midi_activated = true;
-#ifdef AUDIO_ENABLE
-      music_scale_user();
-#endif
-      return false;
-    }
+    return true;
+}
 
-    if (keycode == MI_OFF && record->event.pressed) {
-      midi_activated = false;
-      midi_send_cc(&midi_device, 0, 0x7B, 0);
-      return false;
-    }
+#endif // MIDI_ADVANCED
+
+void midi_task(void)
+{
+    midi_device_process(&midi_device);
+#ifdef MIDI_ADVANCED
+    if (timer_elapsed(midi_modulation_timer) < midi_config.modulation_interval)
+        return;
+    midi_modulation_timer = timer_read();
 
-    if (midi_activated) {
-      if (record->event.key.col == (MATRIX_COLS - 1) && record->event.key.row == (MATRIX_ROWS - 1)) {
-          if (record->event.pressed) {
-              midi_starting_note++; // Change key
-              midi_send_cc(&midi_device, 0, 0x7B, 0);
-          }
-          return false;
-      }
-      if (record->event.key.col == (MATRIX_COLS - 2) && record->event.key.row == (MATRIX_ROWS - 1)) {
-          if (record->event.pressed) {
-              midi_starting_note--; // Change key
-              midi_send_cc(&midi_device, 0, 0x7B, 0);
-          }
-          return false;
-      }
-      if (record->event.key.col == (MATRIX_COLS - 3) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) {
-          midi_offset++; // Change scale
-          midi_send_cc(&midi_device, 0, 0x7B, 0);
-          return false;
-      }
-      if (record->event.key.col == (MATRIX_COLS - 4) && record->event.key.row == (MATRIX_ROWS - 1) && record->event.pressed) {
-          midi_offset--; // Change scale
-          midi_send_cc(&midi_device, 0, 0x7B, 0);
-          return false;
-      }
-      // basic
-      // uint8_t note = (midi_starting_note + SCALE[record->event.key.col + midi_offset])+12*(MATRIX_ROWS - record->event.key.row);
-      // advanced
-      // uint8_t note = (midi_starting_note + record->event.key.col + midi_offset)+12*(MATRIX_ROWS - record->event.key.row);
-      // guitar
-      uint8_t note = (midi_starting_note + record->event.key.col + midi_offset)+5*(MATRIX_ROWS - record->event.key.row);
-      // violin
-      // uint8_t note = (midi_starting_note + record->event.key.col + midi_offset)+7*(MATRIX_ROWS - record->event.key.row);
-
-      if (record->event.pressed) {
-        // midi_send_noteon(&midi_device, record->event.key.row, midi_starting_note + SCALE[record->event.key.col], 127);
-        midi_send_noteon(&midi_device, 0, note, 127);
-      } else {
-        // midi_send_noteoff(&midi_device, record->event.key.row, midi_starting_note + SCALE[record->event.key.col], 127);
-        midi_send_noteoff(&midi_device, 0, note, 127);
-      }
-
-      if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through
-        return false;
+    if (midi_modulation_step != 0)
+    {
+        dprintf("midi modulation %d\n", midi_modulation);
+        midi_send_cc(&midi_device, midi_config.channel, 0x1, midi_modulation);
+
+        if (midi_modulation_step < 0 && midi_modulation < -midi_modulation_step) {
+            midi_modulation = 0;
+            midi_modulation_step = 0;
+            return;
+        }
+
+        midi_modulation += midi_modulation_step;
+
+        if (midi_modulation > 127)
+            midi_modulation = 127;
     }
 #endif
-    return true;
 }
+
+
+
+#endif // MIDI_ENABLE