]> git.donarmstrong.com Git - qmk_firmware.git/commitdiff
Factor basic note processing into respective processors
authorGabriel Young <gabeplaysdrums@live.com>
Sun, 26 Feb 2017 03:25:33 +0000 (19:25 -0800)
committerGabriel Young <gabeplaysdrums@live.com>
Sun, 26 Feb 2017 03:25:33 +0000 (19:25 -0800)
build_keyboard.mk
quantum/process_keycode/process_audio.c [new file with mode: 0644]
quantum/process_keycode/process_audio.h [new file with mode: 0644]
quantum/process_keycode/process_midi.c
quantum/process_keycode/process_midi.h
quantum/process_keycode/process_music.c
quantum/process_keycode/process_music.h
quantum/quantum.c
quantum/quantum.h

index eea8d5919a11f52b25ca2b2433a39b5c3f594d37..07dfe85b4561274e763d918d8007ca41f80a2590 100644 (file)
@@ -157,6 +157,7 @@ endif
 ifeq ($(strip $(AUDIO_ENABLE)), yes)
     OPT_DEFS += -DAUDIO_ENABLE
        SRC += $(QUANTUM_DIR)/process_keycode/process_music.c
+       SRC += $(QUANTUM_DIR)/process_keycode/process_audio.c
        SRC += $(QUANTUM_DIR)/audio/audio.c
        SRC += $(QUANTUM_DIR)/audio/voices.c
        SRC += $(QUANTUM_DIR)/audio/luts.c
diff --git a/quantum/process_keycode/process_audio.c b/quantum/process_keycode/process_audio.c
new file mode 100644 (file)
index 0000000..5b5da54
--- /dev/null
@@ -0,0 +1,62 @@
+#include "process_audio.h"
+#include "audio.h"
+
+static float compute_freq_for_midi_note(uint8_t note)
+{
+    // https://en.wikipedia.org/wiki/MIDI_tuning_standard
+    return pow(2.0, (note - 69) / 12.0) * 440.0f;
+}
+
+bool process_audio(uint16_t keycode, keyrecord_t *record) {
+
+    if (keycode == AU_ON && record->event.pressed) {
+      audio_on();
+      return false;
+    }
+
+    if (keycode == AU_OFF && record->event.pressed) {
+      audio_off();
+      return false;
+    }
+
+    if (keycode == AU_TOG && record->event.pressed) {
+        if (is_audio_on())
+        {
+            audio_off();
+        }
+        else
+        {
+            audio_on();
+        }
+      return false;
+    }
+
+    if (keycode == MUV_IN && record->event.pressed) {
+        voice_iterate();
+        music_scale_user();
+        return false;
+    }
+
+    if (keycode == MUV_DE && record->event.pressed) {
+        voice_deiterate();
+        music_scale_user();
+        return false;
+    }
+
+    return true
+}
+
+void process_audio_noteon(uint8_t note) {
+    play_note(compute_freq_for_midi_note(note), 0xF);
+}
+
+void process_audio_noteoff(uint8_t note) {
+    stop_note(compute_freq_for_midi_note(note));
+}
+
+void process_audio_stop_all_notes(void) {
+    stop_all_notes();
+}
+
+__attribute__ ((weak))
+void audio_on_user() {}
\ No newline at end of file
diff --git a/quantum/process_keycode/process_audio.h b/quantum/process_keycode/process_audio.h
new file mode 100644 (file)
index 0000000..59a1772
--- /dev/null
@@ -0,0 +1,11 @@
+#ifndef PROCESS_AUDIO_H
+#define PROCESS_AUDIO_H
+
+bool process_audio(uint16_t keycode, keyrecord_t *record);
+void process_audio_noteon(uint8_t note);
+void process_audio_noteoff(uint8_t note);
+void process_audio_stop_all_notes(void);
+
+void audio_on_user(void);
+
+#endif
\ No newline at end of file
index 161f04a245d1e0317ec4676c36771b659e94fa40..214bba90203f732e7d23688e04dd96d46e674093 100644 (file)
@@ -1,6 +1,28 @@
 #include "process_midi.h"
 
-#if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
+#ifdef MIDI_ENABLE
+#include "midi.h"
+
+#ifdef MIDI_BASIC
+
+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);
+}
+
+void process_midi_basic_stop_all_notes(void)
+{
+    midi_send_cc(&midi_device, 0, 0x7B, 0);
+}
+
+#endif // MIDI_BASIC
+
+#ifdef MIDI_ADVANCED
 
 #include "timer.h"
 
@@ -165,7 +187,7 @@ bool process_midi(uint16_t keycode, keyrecord_t *record)
         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:
@@ -212,3 +234,5 @@ bool process_midi(uint16_t keycode, keyrecord_t *record)
 }
 
 #endif // MIDI_ADVANCED
+
+#endif // MIDI_ENABLE
index ffd41579f228773c453ec78e7f1821bce07f60c2..0f559ec23a556805b52979249c0b5354509ec557 100644 (file)
@@ -2,8 +2,16 @@
 #define PROCESS_MIDI_H
 
 #include "quantum.h"
-#include "midi.h"
 
+#ifdef MIDI_ENABLE
+
+#ifdef MIDI_BASIC
+void process_midi_basic_noteon(uint8_t note);
+void process_midi_basic_noteoff(uint8_t note);
+void process_midi_basic_stop_all_notes(void);
+#endif
+
+#ifdef MIDI_ADVANCED
 typedef union {
   uint32_t raw;
   struct {
@@ -25,5 +33,8 @@ bool process_midi(uint16_t keycode, keyrecord_t *record);
 #define MIDI_TONE_COUNT (MIDI_TONE_MAX - MIDI_TONE_MIN + 1)
 
 uint8_t midi_compute_note(uint16_t keycode);
+#endif // MIDI_ADVANCED
+
+#endif // MIDI_ENABLE
 
 #endif
\ No newline at end of file
index ac906b628111533ea0bd81f987ed7fefe5c242e3..a1e270df177bb89e8e6c7d7f4fc78b352893747b 100644 (file)
@@ -1,5 +1,14 @@
 #include "process_music.h"
 
+#ifdef AUDIO_ENABLE
+#include "process_audio.h"
+#endif
+#if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
+#include "process_midi.h"
+#endif
+
+#if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
+
 bool music_activated = false;
 uint8_t music_starting_note = 0x0C;
 int music_offset = 7;
@@ -8,38 +17,41 @@ int music_offset = 7;
 static bool music_sequence_recording = false;
 static bool music_sequence_recorded = false;
 static bool music_sequence_playing = false;
-static float music_sequence[16] = {0};
+static uint8_t music_sequence[16] = {0};
 static uint8_t music_sequence_count = 0;
 static uint8_t music_sequence_position = 0;
 
 static uint16_t music_sequence_timer = 0;
 static uint16_t music_sequence_interval = 100;
 
-bool process_music(uint16_t keycode, keyrecord_t *record) {
+static void music_noteon(uint8_t note) {
+    #ifdef AUDIO_ENABLE
+    process_audio_noteon(note);
+    #endif
+    #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
+    process_midi_basic_noteon(note);
+    #endif
+}
 
-       #ifdef AUDIO_ENABLE
-    if (keycode == AU_ON && record->event.pressed) {
-      audio_on();
-      return false;
-    }
+static void music_noteoff(uint8_t note) {
+    #ifdef AUDIO_ENABLE
+    process_audio_noteoff(note);
+    #endif
+    #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
+    process_midi_basic_noteoff(note);
+    #endif
+}
 
-    if (keycode == AU_OFF && record->event.pressed) {
-      audio_off();
-      return false;
-    }
+static void music_all_notes_off(void) {
+    #ifdef AUDIO_ENABLE
+    process_audio_stop_all_notes();
+    #endif
+    #if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
+    process_midi_basic_stop_all_notes();
+    #endif
+}
 
-    if (keycode == AU_TOG && record->event.pressed) {
-        if (is_audio_on())
-        {
-            audio_off();
-        }
-        else
-        {
-            audio_on();
-        }
-      return false;
-    }
-       #endif // AUDIO_ENABLE
+bool process_music(uint16_t keycode, keyrecord_t *record) {
 
     if (keycode == MU_ON && record->event.pressed) {
         music_on();
@@ -63,26 +75,10 @@ bool process_music(uint16_t keycode, keyrecord_t *record) {
         return false;
     }
 
-       #ifdef AUDIO_ENABLE
-    if (keycode == MUV_IN && record->event.pressed) {
-        voice_iterate();
-        music_scale_user();
-        return false;
-    }
-
-    if (keycode == MUV_DE && record->event.pressed) {
-        voice_deiterate();
-        music_scale_user();
-        return false;
-    }
-       #endif // AUDIO_ENABLE
-
     if (music_activated) {
 
       if (keycode == KC_LCTL && record->event.pressed) { // Start recording
-               #ifdef AUDIO_ENABLE
-        stop_all_notes();
-        #endif
+        music_all_notes_off();
         music_sequence_recording = true;
         music_sequence_recorded = false;
         music_sequence_playing = false;
@@ -91,9 +87,7 @@ bool process_music(uint16_t keycode, keyrecord_t *record) {
       }
 
       if (keycode == KC_LALT && record->event.pressed) { // Stop recording/playing
-        #ifdef AUDIO_ENABLE
-        stop_all_notes();
-        #endif
+        music_all_notes_off();
         if (music_sequence_recording) { // was recording
           music_sequence_recorded = true;
         }
@@ -103,9 +97,7 @@ bool process_music(uint16_t keycode, keyrecord_t *record) {
       }
 
       if (keycode == KC_LGUI && record->event.pressed && music_sequence_recorded) { // Start playing
-        #ifdef AUDIO_ENABLE
-        stop_all_notes();
-        #endif
+        music_all_notes_off();
         music_sequence_recording = false;
         music_sequence_playing = true;
         music_sequence_position = 0;
@@ -124,32 +116,27 @@ bool process_music(uint16_t keycode, keyrecord_t *record) {
             music_sequence_interval+=10;
         return false;
       }
+
       #define MUSIC_MODE_GUITAR
 
-      #ifdef AUDIO_ENABLE
       #ifdef MUSIC_MODE_CHROMATIC
-      float freq = ((float)220.0)*pow(2.0, -5.0)*pow(2.0,(music_starting_note + record->event.key.col + music_offset)/12.0+(MATRIX_ROWS - record->event.key.row));
+      uint8_t note = (music_starting_note + record->event.key.col + music_offset - 3)+12*(MATRIX_ROWS - record->event.key.row);
       #elif defined(MUSIC_MODE_GUITAR)
-      float freq = ((float)220.0)*pow(2.0, -5.0)*pow(2.0,(music_starting_note + record->event.key.col + music_offset)/12.0+(float)(MATRIX_ROWS - record->event.key.row + 7)*5.0/12);
+      uint8_t note = (music_starting_note + record->event.key.col + music_offset + 32)+5*(MATRIX_ROWS - record->event.key.row);
       #elif defined(MUSIC_MODE_VIOLIN)
-      float freq = ((float)220.0)*pow(2.0, -5.0)*pow(2.0,(music_starting_note + record->event.key.col + music_offset)/12.0+(float)(MATRIX_ROWS - record->event.key.row + 5)*7.0/12);
+      uint8_t note = (music_starting_note + record->event.key.col + music_offset + 32)+7*(MATRIX_ROWS - record->event.key.row);
       #else
-      float freq = ((float)220.0)*pow(2.0, -5.0)*pow(2.0,(music_starting_note + SCALE[record->event.key.col + music_offset])/12.0+(MATRIX_ROWS - record->event.key.row));
+      uint8_t note = (music_starting_note + SCALE[record->event.key.col + music_offset] - 3)+12*(MATRIX_ROWS - record->event.key.row);
       #endif
-      #endif // AUDIO_ENABLE
 
       if (record->event.pressed) {
-       #ifdef AUDIO_ENABLE
-        play_note(freq, 0xF);
+        music_noteon(note);
         if (music_sequence_recording) {
-          music_sequence[music_sequence_count] = freq;
+          music_sequence[music_sequence_count] = note;
           music_sequence_count++;
         }
-        #endif
       } else {
-       #ifdef AUDIO_ENABLE
-        stop_note(freq);
-        #endif
+        music_noteoff(note);
       }
 
       if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through
@@ -177,32 +164,26 @@ void music_on(void) {
 
 void music_off(void) {
     music_activated = 0;
-    #ifdef AUDIO_ENABLE
-    stop_all_notes();
-    #endif
+    music_all_notes_off();
 }
 
-
-__attribute__ ((weak))
-void music_on_user() {}
-
-#ifdef AUDIO_ENABLE
-__attribute__ ((weak))
-void audio_on_user() {}
-#endif
-
-__attribute__ ((weak))
-void music_scale_user() {}
-
 void matrix_scan_music(void) {
   if (music_sequence_playing) {
     if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) {
       music_sequence_timer = timer_read();
-      #ifdef AUDIO_ENABLE
-      stop_note(music_sequence[(music_sequence_position - 1 < 0)?(music_sequence_position - 1 + music_sequence_count):(music_sequence_position - 1)]);
-      play_note(music_sequence[music_sequence_position], 0xF);
-      #endif
+      uint8_t prev_note = music_sequence[(music_sequence_position - 1 < 0)?(music_sequence_position - 1 + music_sequence_count):(music_sequence_position - 1)];
+      uint8_t next_note = music_sequence[music_sequence_position];
+      music_noteoff(prev_note);
+      music_noteon(next_note);
       music_sequence_position = (music_sequence_position + 1) % music_sequence_count;
     }
   }
 }
+
+__attribute__ ((weak))
+void music_on_user() {}
+
+__attribute__ ((weak))
+void music_scale_user() {}
+
+#endif // defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
\ No newline at end of file
index 318b3e38759f4df97335f6df0ef10a564acba4df..69913b2761c329c338e5e845384496e319d44ae4 100644 (file)
@@ -3,6 +3,8 @@
 
 #include "quantum.h"
 
+#if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
+
 bool process_music(uint16_t keycode, keyrecord_t *record);
 
 bool is_music_on(void);
@@ -10,7 +12,6 @@ void music_toggle(void);
 void music_on(void);
 void music_off(void);
 
-void audio_on_user(void);
 void music_on_user(void);
 void music_scale_user(void);
 
@@ -24,4 +25,6 @@ void matrix_scan_music(void);
                            0 + (12*4), 2 + (12*4), 4 + (12*4), 5 + (12*4), 7 + (12*4), 9 + (12*4), 11 + (12*4), }
 #endif
 
+#endif // defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
+
 #endif
\ No newline at end of file
index 83fa87708866007abeb4d5d965ead6f3a56d9de6..7a27a568ac147da97c503a96fa2241193f4b6574 100644 (file)
@@ -153,6 +153,9 @@ bool process_record_quantum(keyrecord_t *record) {
   #if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
     process_midi(keycode, record) &&
   #endif
+  #ifdef AUDIO_ENABLE
+    process_audio(keycode, record) &&
+  #endif
   #if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
     process_music(keycode, record) &&
   #endif
index 580d51202a429cb59914cb098f665b2d13bb6ddc..77732d43f2db05d3f7f8a7f3e72e74aecef005b7 100644 (file)
@@ -35,11 +35,16 @@ extern uint32_t default_layer_state;
 
 #ifdef MIDI_ENABLE
        #include <lufa.h>
+#ifdef MIDI_ADVANCED
        #include "process_midi.h"
 #endif
+#endif // MIDI_ENABLE
 
 #ifdef AUDIO_ENABLE
-       #include "audio.h"
+       #include "process_audio.h"
+#endif
+
+#if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
        #include "process_music.h"
 #endif