From 50e32081b1cfeb479c9e7687ac8a2dcb14459a57 Mon Sep 17 00:00:00 2001 From: Giulio Zausa Date: Wed, 3 Apr 2024 21:01:54 +0200 Subject: [PATCH] Portmidi --- .gitmodules | 6 ++-- 3rdparty/portmidi | 1 + 3rdparty/rtmidi | 1 - CMakeLists.txt | 26 ++++++----------- cmake/RtMidi.cmake | 15 ---------- src/mcu.cpp | 2 ++ src/midi.h | 1 + src/midi_portmidi.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++ src/midi_rtmidi.cpp | 60 -------------------------------------- src/midi_win32.cpp | 4 +++ 10 files changed, 86 insertions(+), 97 deletions(-) create mode 160000 3rdparty/portmidi delete mode 160000 3rdparty/rtmidi delete mode 100644 cmake/RtMidi.cmake create mode 100644 src/midi_portmidi.cpp delete mode 100644 src/midi_rtmidi.cpp diff --git a/.gitmodules b/.gitmodules index d3346f2..f3a76a4 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ -[submodule "3rdparty/rtmidi"] - path = 3rdparty/rtmidi - url = https://github.com/Wohlstand/rtmidi.git +[submodule "3rdparty/portmidi"] + path = 3rdparty/portmidi + url = https://github.com/PortMidi/portmidi.git diff --git a/3rdparty/portmidi b/3rdparty/portmidi new file mode 160000 index 0000000..3920301 --- /dev/null +++ b/3rdparty/portmidi @@ -0,0 +1 @@ +Subproject commit 3920301ce1234031007550cd0b99b4d6649e3283 diff --git a/3rdparty/rtmidi b/3rdparty/rtmidi deleted file mode 160000 index 7227033..0000000 --- a/3rdparty/rtmidi +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 7227033bcc0df36d4815c9ab57500bb320bf8171 diff --git a/CMakeLists.txt b/CMakeLists.txt index c9d56e8..3003881 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -172,18 +172,14 @@ endif() include(3rdparty/Utf8Main/utf8main.cmake) if(NOT WIN32 AND PKG_CONFIG_FOUND) - set(USE_RTMIDI TRUE) - option(USE_SYSTEM_RTMIDI "Use system libraries for RtMidi" OFF) + set(USE_PORTMIDI TRUE) + option(USE_SYSTEM_PORTMIDI "Use system libraries for PortMidi" OFF) endif() find_package(SDL2 REQUIRED) -if(USE_RTMIDI) - if(USE_SYSTEM_RTMIDI) - pkg_check_modules(SYSTEM_RTMIDI rtmidi REQUIRED) - else() - include(RtMidi) - endif() +if(USE_PORTMIDI) + add_subdirectory(3rdparty/portmidi) endif() @@ -200,8 +196,8 @@ set(SC55_SRC src/utils/files.cpp src/utils/files.h ) -if(USE_RTMIDI) - list(APPEND SC55_SRC src/midi_rtmidi.cpp) +if(USE_PORTMIDI) + list(APPEND SC55_SRC src/midi_portmidi.cpp) elseif(WIN32) list(APPEND SC55_SRC src/midi_win32.cpp) endif() @@ -217,14 +213,8 @@ else() target_link_libraries(nuked-sc55 PRIVATE ${SDL2_LIBRARIES}) endif() -if(USE_RTMIDI) - if(USE_SYSTEM_RTMIDI) - target_compile_options(nuked-sc55 PRIVATE ${SYSTEM_RTMIDI_CFLAGS}) - target_include_directories(nuked-sc55 PRIVATE ${SYSTEM_RTMIDI_INCLUDE_DIRS}) - target_link_libraries(nuked-sc55 PRIVATE ${SYSTEM_RTMIDI_LIBRARIES}) - else() - target_link_libraries(nuked-sc55 PRIVATE RtMidi) - endif() +if(USE_PORTMIDI) + target_link_libraries(nuked-sc55 PRIVATE portmidi) endif() if(WIN32) diff --git a/cmake/RtMidi.cmake b/cmake/RtMidi.cmake deleted file mode 100644 index b44583b..0000000 --- a/cmake/RtMidi.cmake +++ /dev/null @@ -1,15 +0,0 @@ -add_library(RtMidi STATIC EXCLUDE_FROM_ALL "3rdparty/rtmidi/RtMidi.cpp") -target_include_directories(RtMidi PUBLIC "3rdparty/rtmidi") - -if(CMAKE_SYSTEM_NAME STREQUAL "Linux") - target_compile_definitions(RtMidi PUBLIC "__LINUX_ALSA__") - target_link_libraries(RtMidi PUBLIC "asound") -elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows") - target_compile_definitions(RtMidi PUBLIC "__WINDOWS_MM__") - target_link_libraries(RtMidi PUBLIC "winmm") -elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin") - target_compile_definitions(RtMidi PUBLIC "__MACOSX_CORE__") - find_library(COREMIDI_LIBRARY "CoreMIDI") - find_library(FOUNDATION_LIBRARY "Foundation") - target_link_libraries(RtMidi PUBLIC "${COREMIDI_LIBRARY}" "${FOUNDATION_LIBRARY}") -endif() diff --git a/src/mcu.cpp b/src/mcu.cpp index 991f1ed..631a86a 100644 --- a/src/mcu.cpp +++ b/src/mcu.cpp @@ -625,6 +625,8 @@ int SDLCALL work_thread(void* data) SM_Update(mcu.cycles); MCU_UpdateAnalog(mcu.cycles); + + MIDI_Update(); } MCU_WorkThread_Unlock(); diff --git a/src/midi.h b/src/midi.h index 8d42fc4..3301a11 100644 --- a/src/midi.h +++ b/src/midi.h @@ -35,4 +35,5 @@ int MIDI_Init(void); void MIDI_Quit(void); +void MIDI_Update(void); diff --git a/src/midi_portmidi.cpp b/src/midi_portmidi.cpp new file mode 100644 index 0000000..0312b20 --- /dev/null +++ b/src/midi_portmidi.cpp @@ -0,0 +1,67 @@ +#include +#include "mcu.h" +#include "submcu.h" +#include "midi.h" +#include + +static PmStream *midiInStream; +static bool sysExMode = false; + +void MIDI_Update() +{ + PmEvent event; + while (Pm_Read(midiInStream, &event, 1)) { + uint8_t command = (Pm_MessageStatus(event.message) >> 4); + + SM_PostUART(Pm_MessageStatus(event.message)); + + if (sysExMode && Pm_MessageStatus(event.message) == 0xF7) { + sysExMode = false; + } else if (sysExMode || Pm_MessageStatus(event.message) == 0xF0) { + sysExMode = true; + SM_PostUART(Pm_MessageData1(event.message)); + if (Pm_MessageData1(event.message) == 0xF7) { + sysExMode = false; + return; + } + SM_PostUART(Pm_MessageData2(event.message)); + if (Pm_MessageData2(event.message) == 0xF7) { + sysExMode = false; + return; + } + SM_PostUART((((event.message) >> 24) & 0xFF)); + if ((((event.message) >> 24) & 0xFF) == 0xF7) { + sysExMode = false; + return; + } + } else if (command == 0xC || command == 0xD || Pm_MessageStatus(event.message) == 0xF3) { + SM_PostUART(Pm_MessageData1(event.message)); + } else if (command == 0x8 || command == 0x9 || command == 0xA || command == 0xB || command == 0xE){ + SM_PostUART(Pm_MessageData1(event.message)); + SM_PostUART(Pm_MessageData2(event.message)); + } + } +} + +int MIDI_Init(void) +{ + Pm_Initialize(); + + int in_id = Pm_CreateVirtualInput("Virtual SC55", NULL, NULL); + + Pm_OpenInput(&midiInStream, in_id, NULL, 0, NULL, NULL); + Pm_SetFilter(midiInStream, PM_FILT_ACTIVE | PM_FILT_CLOCK); + + // Empty the buffer, just in case anything got through + PmEvent receiveBuffer[1]; + while (Pm_Poll(midiInStream)) { + Pm_Read(midiInStream, receiveBuffer, 1); + } + + return 1; +} + +void MIDI_Quit() +{ + Pm_Terminate(); +} diff --git a/src/midi_rtmidi.cpp b/src/midi_rtmidi.cpp deleted file mode 100644 index 03bd2d3..0000000 --- a/src/midi_rtmidi.cpp +++ /dev/null @@ -1,60 +0,0 @@ -#include -#include "mcu.h" -#include "submcu.h" -#include "midi.h" -#include - -static RtMidiIn *s_midi_in = nullptr; - - -static void MidiOnReceive(double, std::vector *message, void *) -{ - uint8_t *beg = message->data(); - uint8_t *end = message->data() + message->size(); - - while(beg < end) - SM_PostUART(*beg++); -} - -static void MidiOnError(RtMidiError::Type, const std::string &errorText, void *) -{ - fprintf(stderr, "RtMidi: Error has occured: %s\n", errorText.c_str()); - fflush(stderr); -} - -int MIDI_Init(void) -{ - if (s_midi_in) - { - printf("MIDI already running\n"); - return 0; // Already running - } - - s_midi_in = new RtMidiIn(RtMidi::UNSPECIFIED, "Virtual SC55", 1024); - s_midi_in->setCallback(&MidiOnReceive, nullptr); // FIXME: (local bug) Fix the linking error - s_midi_in->setErrorCallback(&MidiOnError, nullptr); - - unsigned count = s_midi_in->getPortCount(); - - if (count == 0) - { - printf("No midi input\n"); - delete s_midi_in; - s_midi_in = nullptr; - return 0; - } - - s_midi_in->openPort(0, "Virtual SC55"); - - return 1; -} - -void MIDI_Quit() -{ - if (s_midi_in) - { - s_midi_in->closePort(); - delete s_midi_in; - s_midi_in = nullptr; - } -} diff --git a/src/midi_win32.cpp b/src/midi_win32.cpp index 8756d90..1c6fd5e 100644 --- a/src/midi_win32.cpp +++ b/src/midi_win32.cpp @@ -145,3 +145,7 @@ void MIDI_Quit() midi_handle = 0; } } + +void MIDI_Update() +{ +}