From a7c61f2947528f79383dfe63e654346afce0d14a Mon Sep 17 00:00:00 2001
From: Jack Humbert <jack.humb@gmail.com>
Date: Sun, 10 Dec 2017 10:57:52 -0500
Subject: fix up midi stuff w/music mode

---
 quantum/process_keycode/process_music.c | 81 +++++++++++++++++++++++++++++----
 quantum/process_keycode/process_music.h |  6 +++
 quantum/quantum.c                       |  2 +-
 quantum/quantum_keycodes.h              |  5 +-
 4 files changed, 83 insertions(+), 11 deletions(-)

(limited to 'quantum')

diff --git a/quantum/process_keycode/process_music.c b/quantum/process_keycode/process_music.c
index 63841d1e8..f69d13ff5 100644
--- a/quantum/process_keycode/process_music.c
+++ b/quantum/process_keycode/process_music.c
@@ -25,6 +25,7 @@
 #if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
 
 bool music_activated = false;
+bool midi_activated = false;
 uint8_t music_starting_note = 0x0C;
 int music_offset = 7;
 uint8_t music_mode = MUSIC_MODE_CHROMATIC;
@@ -47,6 +48,12 @@ static uint16_t music_sequence_interval = 100;
   #ifndef MUSIC_OFF_SONG
     #define MUSIC_OFF_SONG SONG(MUSIC_OFF_SOUND)
   #endif
+  #ifndef MIDI_ON_SONG
+    #define MIDI_ON_SONG SONG(MUSIC_ON_SOUND)
+  #endif
+  #ifndef MIDI_OFF_SONG
+    #define MIDI_OFF_SONG SONG(MUSIC_OFF_SOUND)
+  #endif
   #ifndef CHROMATIC_SONG
     #define CHROMATIC_SONG SONG(CHROMATIC_SOUND)
   #endif
@@ -67,6 +74,8 @@ static uint16_t music_sequence_interval = 100;
   };
   float music_on_song[][2] = MUSIC_ON_SONG;
   float music_off_song[][2] = MUSIC_OFF_SONG;
+  float midi_on_song[][2] = MIDI_ON_SONG;
+  float midi_off_song[][2] = MIDI_OFF_SONG;
 #endif
 
 #ifndef MUSIC_MASK
@@ -75,28 +84,34 @@ static uint16_t music_sequence_interval = 100;
 
 static void music_noteon(uint8_t note) {
     #ifdef AUDIO_ENABLE
-    process_audio_noteon(note);
+    if (music_activated)
+      process_audio_noteon(note);
     #endif
     #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
-    process_midi_basic_noteon(note);
+    if (midi_activated)
+      process_midi_basic_noteon(note);
     #endif
 }
 
 static void music_noteoff(uint8_t note) {
     #ifdef AUDIO_ENABLE
-    process_audio_noteoff(note);
+    if (music_activated)
+      process_audio_noteoff(note);
     #endif
     #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
-    process_midi_basic_noteoff(note);
+    if (midi_activated)
+      process_midi_basic_noteoff(note);
     #endif
 }
 
 void music_all_notes_off(void) {
     #ifdef AUDIO_ENABLE
-    process_audio_all_notes_off();
+    if (music_activated)
+      process_audio_all_notes_off();
     #endif
     #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
-    process_midi_all_notes_off();
+    if (midi_activated)
+      process_midi_all_notes_off();
     #endif
 }
 
@@ -105,7 +120,7 @@ bool process_music(uint16_t keycode, keyrecord_t *record) {
     if (keycode == MU_ON && record->event.pressed) {
         music_on();
         return false;
-    }
+    }    
 
     if (keycode == MU_OFF && record->event.pressed) {
         music_off();
@@ -121,12 +136,31 @@ bool process_music(uint16_t keycode, keyrecord_t *record) {
         return false;
     }
 
+    if (keycode == MI_ON && record->event.pressed) {
+        midi_on();
+        return false;
+    }    
+
+    if (keycode == MI_OFF && record->event.pressed) {
+        midi_off();
+        return false;
+    }
+
+    if (keycode == MI_TOG && record->event.pressed) {
+        if (midi_activated) {
+            midi_off();
+        } else {
+            midi_on();
+        }
+        return false;
+    }
+
     if (keycode == MU_MOD && record->event.pressed) {
       music_mode_cycle();
       return false;
     }
 
-    if (music_activated) {
+    if (music_activated || midi_activated) {
       if (record->event.pressed) {
         if (keycode == KC_LCTL) { // Start recording
           music_all_notes_off();
@@ -224,6 +258,34 @@ void music_off(void) {
     #endif
 }
 
+bool is_midi_on(void) {
+    return (midi_activated != 0);
+}
+
+void midi_toggle(void) {
+    if (!midi_activated) {
+        midi_on();
+    } else {
+        midi_off();
+    }
+}
+
+void midi_on(void) {
+    midi_activated = 1;
+    #ifdef AUDIO_ENABLE
+      PLAY_SONG(midi_on_song);
+    #endif
+    midi_on_user();
+}
+
+void midi_off(void) {
+    process_midi_all_notes_off();
+    midi_activated = 0;
+    #ifdef AUDIO_ENABLE
+      PLAY_SONG(midi_off_song);
+    #endif
+}
+
 void music_mode_cycle(void) {
   music_all_notes_off();
   music_mode = (music_mode + 1) % NUMBER_OF_MODES;
@@ -248,6 +310,9 @@ void matrix_scan_music(void) {
 __attribute__ ((weak))
 void music_on_user() {}
 
+__attribute__ ((weak))
+void midi_on_user() {}
+
 __attribute__ ((weak))
 void music_scale_user() {}
 
diff --git a/quantum/process_keycode/process_music.h b/quantum/process_keycode/process_music.h
index ee027197c..773bbfa6b 100644
--- a/quantum/process_keycode/process_music.h
+++ b/quantum/process_keycode/process_music.h
@@ -36,7 +36,13 @@ void music_toggle(void);
 void music_on(void);
 void music_off(void);
 
+bool is_midi_on(void);
+void midi_toggle(void);
+void midi_on(void);
+void midi_off(void);
+
 void music_on_user(void);
+void midi_on_user(void);
 void music_scale_user(void);
 void music_all_notes_off(void);
 void music_mode_cycle(void);
diff --git a/quantum/quantum.c b/quantum/quantum.c
index dee2fc4b6..e2280b707 100644
--- a/quantum/quantum.c
+++ b/quantum/quantum.c
@@ -132,7 +132,7 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) {
 
 void reset_keyboard(void) {
   clear_keyboard();
-#if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_ENABLE_BASIC))
+#if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
   music_all_notes_off();
   uint16_t timer_start = timer_read();
   PLAY_SONG(goodbye_song);
diff --git a/quantum/quantum_keycodes.h b/quantum/quantum_keycodes.h
index 048da3267..f2cb25718 100644
--- a/quantum/quantum_keycodes.h
+++ b/quantum/quantum_keycodes.h
@@ -152,8 +152,9 @@ enum quantum_keycodes {
 
     // Midi
 #if !MIDI_ENABLE_STRICT || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
-    MI_ON,  // send midi notes when music mode is enabled
-    MI_OFF, // don't send midi notes when music mode is enabled
+    MI_ON,
+    MI_OFF,
+    MI_TOG,
 #endif
 
 #if !MIDI_ENABLE_STRICT || (defined(MIDI_ENABLE) && defined(MIDI_ADVANCED))
-- 
cgit v1.2.3