From 7454c4146395b32008a47c5fa0893a1bbe58c9c7 Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Sun, 29 May 2022 13:36:24 +0800 Subject: [PATCH] Add channel config Allow MIDI input to be filtered through MIDI channels. --- docs/reference/files/config.ini | 1 + src/packages/game/input/IZ_midi.c | 20 ++++++++++++++++++-- src/packages/game/input/IZ_midi.h | 1 + 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/docs/reference/files/config.ini b/docs/reference/files/config.ini index b7dff61..c583719 100644 --- a/docs/reference/files/config.ini +++ b/docs/reference/files/config.ini @@ -53,4 +53,5 @@ Action7=G4 Action8="G#4" Action9=A4 [MIDIInput.0] +Channel=0 DeviceID=0 diff --git a/src/packages/game/input/IZ_midi.c b/src/packages/game/input/IZ_midi.c index 7845d42..e4f712a 100644 --- a/src/packages/game/input/IZ_midi.c +++ b/src/packages/game/input/IZ_midi.c @@ -63,12 +63,18 @@ uint8_t IZ_GetMIDINoteFromName(char* name) { void IZ_HandleMIDINoteOnOffEvents(PmEvent e, IZ_MIDIInputState* state, IZ_Action* action) { uint32_t message = e.message; uint8_t status = message & 0xF0u; - // uint8_t channel = message & 0x0Fu; + uint8_t channel = message & 0x0Fu; uint8_t data1 = (message >> 8) & 0xFFu; // uint8_t data2 = (message >> 16) & 0xFFu; for (uint8_t i = 0; i < CONTROLS; i += 1) { - if (data1 == state->config.control_mapping[i]) { + if ( + data1 == state->config.control_mapping[i] + && ( + (state->config.channel < 16 && channel == state->config.channel) + || state->config.channel >= 16 + ) + ) { const uint16_t bitflag = (0x1 << i); if (status == IZ_MIDI_NOTE_ON) { *action |= bitflag; @@ -108,6 +114,15 @@ IZ_ProcedureResult IZ_SaveMIDIInputConfig(const char* config_path, IZ_MIDIInputS } sprintf_s(main_section_name, 12, "MIDIInput.%d", player_index); + if (!ini_putl( + main_section_name, + "Channel", + state[player_index]->config.channel, + config_path + )) { + problem |= (1 << player_index); + } + if (!ini_putl( main_section_name, "DeviceID", @@ -142,6 +157,7 @@ void IZ_LoadMIDIInputConfig(const char* config_path, IZ_MIDIInputState(* state)[ } sprintf_s(main_section_name, 12, "MIDIInput.%d", player_index); + state[player_index]->config.channel = ini_getl(main_section_name, "Channel", player_index, config_path); state[player_index]->config.device_id = ini_getl(main_section_name, "DeviceID", player_index, config_path); } } diff --git a/src/packages/game/input/IZ_midi.h b/src/packages/game/input/IZ_midi.h index 5face3e..63d1f3f 100644 --- a/src/packages/game/input/IZ_midi.h +++ b/src/packages/game/input/IZ_midi.h @@ -16,6 +16,7 @@ static const uint8_t IZ_MIDI_NOTE_OFF = 0x80u; typedef struct { PmDeviceID device_id; + uint8_t channel; IZ_MIDINote control_mapping[CONTROLS]; } IZ_MIDIInputConfig;