Skip to content

Commit

Permalink
Split MIDI functionality into MIDI_BASIC and MIDI_ADVANCED
Browse files Browse the repository at this point in the history
MIDI_ENABLE = no

   text	   data	    bss	    dec	    hex	filename
      0	  17080	      0	  17080	   42b8	satan_midi.hex

MIDI_ENABLE = yes
MIDI_BASIC undefined
MIDI_ADVANCED undefined

   text	   data	    bss	    dec	    hex	filename
      0	  19494	      0	  19494	   4c26	satan_midi.hex

MIDI_ENABLE = yes
#define MIDI_BASIC
MIDI_ADVANCED undefined

   text	   data	    bss	    dec	    hex	filename
      0	  19788	      0	  19788	   4d4c	satan_midi.hex

MIDI_ENABLE = yes
MIDI_BASIC undefined
#define MIDI_ADVANCED

   text	   data	    bss	    dec	    hex	filename
      0	  20846	      0	  20846	   516e	satan_midi.hex

MIDI_ENABLE = yes
#define MIDI_BASIC
#define MIDI_ADVANCED

   text	   data	    bss	    dec	    hex	filename
      0	  21140	      0	  21140	   5294	satan_midi.hex
  • Loading branch information
Gabriel Young committed Feb 25, 2017
1 parent 64eecfc commit 525be99
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 11 deletions.
1 change: 1 addition & 0 deletions build_keyboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ endif

ifeq ($(strip $(MIDI_ENABLE)), yes)
OPT_DEFS += -DMIDI_ENABLE
SRC += $(QUANTUM_DIR)/process_keycode/process_music.c
SRC += $(QUANTUM_DIR)/process_keycode/process_midi.c
endif

Expand Down
17 changes: 16 additions & 1 deletion keyboards/satan/keymaps/midi/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,22 @@

#include "../../config.h"

// place overrides here
/*
* MIDI options
*/

/* enable basic MIDI features:
- MIDI notes can be sent when in Music mode is on
*/
#define MIDI_BASIC

/* enable advanced MIDI features:
- MIDI notes can be added to the keymap
- Octave shift and transpose
- Virtual sustain, portamento, and modulation wheel
- etc.
*/
#define MIDI_ADVANCED

/* override number of MIDI tone keycodes (each octave adds 12 keycodes and allocates 12 bytes) */
#define MIDI_TONE_KEYCODE_OCTAVES 2
Expand Down
4 changes: 2 additions & 2 deletions keyboards/satan/keymaps/midi/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
KC_LSFT, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM,KC_DOT, KC_SLSH, KC_RSFT, \
KC_LCTL, KC_LGUI,KC_LALT, KC_SPC, KC_RALT,KC_RGUI, TG(_ML), KC_RCTL),

#ifdef MIDI_ENABLE
#if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
/* Keymap _ML: MIDI Layer
* ,------------------------------------------------------------------------.
* | Exit | | | | | | | | | | | | | |
Expand All @@ -51,6 +51,6 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
MI_CHU, XXXXXXX, MI_Cs, MI_Ds, XXXXXXX, MI_Fs, MI_Gs, MI_As, XXXXXXX, MI_Cs_1, MI_Ds_1, XXXXXXX, XXXXXXX, XXXXXXX, \
MI_MOD, MI_C, MI_D, MI_E, MI_F, MI_G, MI_A, MI_B, MI_C_1, MI_D_1, MI_E_1, MI_F_1, _______, \
MI_SUS, MI_OCTD, MI_OCTU, MI_MODSD, MI_MODSU, XXXXXXX, XXXXXXX, XXXXXXX, MI_TRNSD, MI_TRNSU, MI_TRNS_0, MI_SUS, \
_______, _______, _______, MI_OFF, _______, _______, _______, _______),
_______, _______, _______, MI_ALLOFF, _______, _______, _______, _______),
#endif
};
9 changes: 6 additions & 3 deletions quantum/process_keycode/process_midi.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#define MIDI_TONE_KEYCODE_OCTAVES 2

#include "process_midi.h"

#if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)

#include "timer.h"

static uint8_t tone_status[MIDI_TONE_COUNT];
Expand Down Expand Up @@ -161,7 +162,7 @@ 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");
Expand Down Expand Up @@ -209,3 +210,5 @@ bool process_midi(uint16_t keycode, keyrecord_t *record)

return true;
}

#endif // MIDI_ADVANCED
22 changes: 22 additions & 0 deletions quantum/process_keycode/process_music.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ static uint16_t music_sequence_interval = 100;

bool process_music(uint16_t keycode, keyrecord_t *record) {

#ifdef AUDIO_ENABLE
if (keycode == AU_ON && record->event.pressed) {
audio_on();
return false;
Expand All @@ -38,6 +39,7 @@ bool process_music(uint16_t keycode, keyrecord_t *record) {
}
return false;
}
#endif // AUDIO_ENABLE

if (keycode == MU_ON && record->event.pressed) {
music_on();
Expand All @@ -61,6 +63,7 @@ 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();
Expand All @@ -72,11 +75,14 @@ bool process_music(uint16_t keycode, keyrecord_t *record) {
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_sequence_recording = true;
music_sequence_recorded = false;
music_sequence_playing = false;
Expand All @@ -85,7 +91,9 @@ 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
if (music_sequence_recording) { // was recording
music_sequence_recorded = true;
}
Expand All @@ -95,7 +103,9 @@ 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_sequence_recording = false;
music_sequence_playing = true;
music_sequence_position = 0;
Expand All @@ -116,6 +126,7 @@ bool process_music(uint16_t keycode, keyrecord_t *record) {
}
#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));
#elif defined(MUSIC_MODE_GUITAR)
Expand All @@ -125,15 +136,20 @@ bool process_music(uint16_t keycode, keyrecord_t *record) {
#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));
#endif
#endif // AUDIO_ENABLE

if (record->event.pressed) {
#ifdef AUDIO_ENABLE
play_note(freq, 0xF);
if (music_sequence_recording) {
music_sequence[music_sequence_count] = freq;
music_sequence_count++;
}
#endif
} else {
#ifdef AUDIO_ENABLE
stop_note(freq);
#endif
}

if (keycode < 0xFF) // ignores all normal keycodes, but lets RAISE, LOWER, etc through
Expand Down Expand Up @@ -161,15 +177,19 @@ void music_on(void) {

void music_off(void) {
music_activated = 0;
#ifdef AUDIO_ENABLE
stop_all_notes();
#endif
}


__attribute__ ((weak))
void music_on_user() {}

#ifdef AUDIO_ENABLE
__attribute__ ((weak))
void audio_on_user() {}
#endif

__attribute__ ((weak))
void music_scale_user() {}
Expand All @@ -178,8 +198,10 @@ 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
music_sequence_position = (music_sequence_position + 1) % music_sequence_count;
}
}
Expand Down
4 changes: 2 additions & 2 deletions quantum/quantum.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ bool process_record_quantum(keyrecord_t *record) {

if (!(
process_record_kb(keycode, record) &&
#ifdef MIDI_ENABLE
#if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
process_midi(keycode, record) &&
#endif
#ifdef AUDIO_ENABLE
#if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
process_music(keycode, record) &&
#endif
#ifdef TAP_DANCE_ENABLE
Expand Down
13 changes: 10 additions & 3 deletions quantum/quantum_keycodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#ifndef QUANTUM_KEYCODES_H
#define QUANTUM_KEYCODES_H

#ifdef MIDI_ENABLE
#if defined(MIDI_ENABLE) && defined(MIDI_ADVANCED)
#ifndef MIDI_TONE_KEYCODE_OCTAVES
#define MIDI_TONE_KEYCODE_OCTAVES 3
#endif
Expand Down Expand Up @@ -116,6 +116,12 @@ enum quantum_keycodes {
#ifdef MIDI_ENABLE
// Midi

#ifdef MIDI_BASIC
MI_ON, // send midi notes when music mode is enabled
MI_OFF, // don't send midi notes when music mode is enabled
#endif

#ifdef MIDI_ADVANCED
MIDI_TONE_MIN,

#if MIDI_TONE_KEYCODE_OCTAVES > 0
Expand Down Expand Up @@ -321,7 +327,7 @@ enum quantum_keycodes {
MI_CHD, // previous channel
MI_CHU, // next channel

MI_OFF, // all notes off
MI_ALLOFF, // all notes off

MI_SUS, // sustain
MI_PORT, // portamento
Expand All @@ -332,7 +338,8 @@ enum quantum_keycodes {
MI_MOD, // modulation
MI_MODSD, // decrease modulation speed
MI_MODSU, // increase modulation speed
#endif
#endif // MIDI_ADVANCED
#endif // MIDI_ENABLE

// Backlight functionality
BL_0,
Expand Down
17 changes: 17 additions & 0 deletions quantum/template/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,23 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
//#define NO_ACTION_MACRO
//#define NO_ACTION_FUNCTION

/*
* MIDI options
*/

/* enable basic MIDI features:
- MIDI notes can be sent when in Music mode is on
*/
//#define MIDI_BASIC

/* enable advanced MIDI features:
- MIDI notes can be added to the keymap
- Octave shift and transpose
- Virtual sustain, portamento, and modulation wheel
- etc.
*/
//#define MIDI_ADVANCED

/* override number of MIDI tone keycodes (each octave adds 12 keycodes and allocates 12 bytes) */
//#define MIDI_TONE_KEYCODE_OCTAVES 1

Expand Down
4 changes: 4 additions & 0 deletions tmk_core/protocol/lufa/lufa.c
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,9 @@ void sysex_callback(MidiDevice * device,

void setup_midi(void)
{
#ifdef MIDI_ADVANCED
midi_init();
#endif
midi_device_init(&midi_device);
midi_device_set_send_func(&midi_device, usb_send_func);
midi_device_set_pre_input_process_func(&midi_device, usb_get_midi);
Expand Down Expand Up @@ -1180,8 +1182,10 @@ int main(void)

#ifdef MIDI_ENABLE
midi_device_process(&midi_device);
#ifdef MIDI_ADVANCED
midi_task();
#endif
#endif

#if defined(RGBLIGHT_ANIMATIONS) & defined(RGBLIGHT_ENABLE)
rgblight_task();
Expand Down

0 comments on commit 525be99

Please sign in to comment.