]> git.donarmstrong.com Git - qmk_firmware.git/blobdiff - quantum/process_keycode/process_music.c
Adds support for Planck Rev 6 (#2666)
[qmk_firmware.git] / quantum / process_keycode / process_music.c
index 63841d1e8754dd746ae31eb51e7bb886d1139840..697aa237facdb8dd383a396966d82706b2efffa3 100644 (file)
 #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;
+uint8_t music_mode = MUSIC_MODE_MAJOR;
 
 // music sequencer
 static bool music_sequence_recording = false;
@@ -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,36 +74,40 @@ static uint16_t music_sequence_interval = 100;
   };
   float music_on_song[][2] = MUSIC_ON_SONG;
   float music_off_song[][2] = MUSIC_OFF_SONG;
-#endif
-
-#ifndef MUSIC_MASK
-  #define MUSIC_MASK keycode < 0xFF
+  float midi_on_song[][2] = MIDI_ON_SONG;
+  float midi_off_song[][2] = MIDI_OFF_SONG;
 #endif
 
 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
 }
 
@@ -121,12 +132,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();
@@ -167,17 +197,26 @@ bool process_music(uint16_t keycode, keyrecord_t *record) {
         }
       }
 
-      uint8_t note;
-      if (music_mode == MUSIC_MODE_CHROMATIC) 
-        note = (music_starting_note + record->event.key.col + music_offset - 3)+12*(MATRIX_ROWS - record->event.key.row);
-      else if (music_mode == MUSIC_MODE_GUITAR)
-        note = (music_starting_note + record->event.key.col + music_offset + 32)+5*(MATRIX_ROWS - record->event.key.row);
-      else if (music_mode == MUSIC_MODE_VIOLIN)
-        note = (music_starting_note + record->event.key.col + music_offset + 32)+7*(MATRIX_ROWS - record->event.key.row);
-      else if (music_mode == MUSIC_MODE_MAJOR)
-        note = (music_starting_note + SCALE[record->event.key.col + music_offset] - 3)+12*(MATRIX_ROWS - record->event.key.row);
-      else
-        note = music_starting_note;
+      uint8_t note = 36;
+      #ifdef MUSIC_MAP
+        if (music_mode == MUSIC_MODE_CHROMATIC) {
+          note = music_starting_note + music_offset + 36 + music_map[record->event.key.row][record->event.key.col];
+        } else {
+          uint8_t position = music_map[record->event.key.row][record->event.key.col];
+          note = music_starting_note + music_offset + 36 + SCALE[position % 12] + (position / 12)*12;
+        }
+      #else
+        if (music_mode == MUSIC_MODE_CHROMATIC)
+          note = (music_starting_note + record->event.key.col + music_offset - 3)+12*(MATRIX_ROWS - record->event.key.row);
+        else if (music_mode == MUSIC_MODE_GUITAR)
+          note = (music_starting_note + record->event.key.col + music_offset + 32)+5*(MATRIX_ROWS - record->event.key.row);
+        else if (music_mode == MUSIC_MODE_VIOLIN)
+          note = (music_starting_note + record->event.key.col + music_offset + 32)+7*(MATRIX_ROWS - record->event.key.row);
+        else if (music_mode == MUSIC_MODE_MAJOR)
+          note = (music_starting_note + SCALE[record->event.key.col + music_offset] - 3)+12*(MATRIX_ROWS - record->event.key.row);
+        else
+          note = music_starting_note;
+      #endif
 
       if (record->event.pressed) {
         music_noteon(note);
@@ -189,13 +228,31 @@ bool process_music(uint16_t keycode, keyrecord_t *record) {
         music_noteoff(note);
       }
 
-      if (MUSIC_MASK)
+      if (music_mask(keycode))
         return false;
     }
 
     return true;
 }
 
+bool music_mask(uint16_t keycode) {
+  #ifdef MUSIC_MASK
+    return MUSIC_MASK;
+  #else
+    return music_mask_kb(keycode);
+  #endif
+}
+
+__attribute__((weak))
+bool music_mask_kb(uint16_t keycode) {
+  return music_mask_user(keycode);
+}
+
+__attribute__((weak))
+bool music_mask_user(uint16_t keycode) {
+  return keycode < 0xFF;
+}
+
 bool is_music_on(void) {
     return (music_activated != 0);
 }
@@ -224,6 +281,36 @@ 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) {
+    #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
+      process_midi_all_notes_off();
+    #endif
+    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,7 +335,10 @@ void matrix_scan_music(void) {
 __attribute__ ((weak))
 void music_on_user() {}
 
+__attribute__ ((weak))
+void midi_on_user() {}
+
 __attribute__ ((weak))
 void music_scale_user() {}
 
-#endif // defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
\ No newline at end of file
+#endif // defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))