From 6c99018e41244b5f3da65ab9b325d56be014229c Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Sun, 25 Mar 2018 15:08:36 -0400 Subject: [PATCH] Tweak build system, midi.hpp structure --- Makefile | 4 ++-- include/bridge.hpp | 4 ++-- include/midi.hpp | 6 +++-- src/bridge.cpp | 41 ++++++++++++++++++--------------- src/midi.cpp | 56 +++++++++++++++++++++++++++++----------------- 5 files changed, 66 insertions(+), 45 deletions(-) diff --git a/Makefile b/Makefile index c0a9cfb2..c49c0aa9 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ FLAGS += \ -Idep/include -Idep/lib/libzip/include ifdef RELEASE - FLAGS += -DRELEASE=$(RELEASE) + FLAGS += -DRELEASE endif include arch.mk @@ -29,7 +29,7 @@ endif ifeq ($(ARCH), mac) SOURCES += dep/osdialog/osdialog_mac.m - CXXFLAGS += -DAPPLE -stdlib=libc++ + CXXFLAGS += -stdlib=libc++ LDFLAGS += -stdlib=libc++ -lpthread -ldl \ -framework Cocoa -framework OpenGL -framework IOKit -framework CoreVideo \ -Ldep/lib -lGLEW -lglfw -ljansson -lspeexdsp -lcurl -lzip -lrtaudio -lrtmidi -lcrypto -lssl diff --git a/include/bridge.hpp b/include/bridge.hpp index 6452a7cd..28dfbc06 100644 --- a/include/bridge.hpp +++ b/include/bridge.hpp @@ -9,8 +9,8 @@ namespace rack { void bridgeInit(); void bridgeDestroy(); -void bridgeMidiSubscribe(int channel, MidiIO *midi); -void bridgeMidiUnsubscribe(int channel, MidiIO *midi); +void bridgeMidiSubscribe(int channel, MidiInput *midi); +void bridgeMidiUnsubscribe(int channel, MidiInput *midi); void bridgeAudioSubscribe(int channel, AudioIO *audio); void bridgeAudioUnsubscribe(int channel, AudioIO *audio); diff --git a/include/midi.hpp b/include/midi.hpp index 88d2d097..3e9309e3 100644 --- a/include/midi.hpp +++ b/include/midi.hpp @@ -55,12 +55,11 @@ struct MidiIO { int getDeviceCount(); std::string getDeviceName(int device); - void setDevice(int device); + virtual void setDevice(int device) {} std::string getChannelName(int channel); json_t *toJson(); void fromJson(json_t *rootJ); - virtual void onMessage(MidiMessage message) {} }; @@ -69,6 +68,8 @@ struct MidiInput : MidiIO { MidiInput(); ~MidiInput(); void setDriver(int driver) override; + void setDevice(int device) override; + virtual void onMessage(MidiMessage message) {} }; @@ -86,6 +87,7 @@ struct MidiOutput : MidiIO { MidiOutput(); ~MidiOutput(); void setDriver(int driver) override; + void setDevice(int device) override; }; diff --git a/src/bridge.cpp b/src/bridge.cpp index 74cc0f82..50327c04 100644 --- a/src/bridge.cpp +++ b/src/bridge.cpp @@ -3,7 +3,7 @@ #include "dsp/ringbuffer.hpp" #include -#ifdef ARCH_WIN +#if ARCH_WIN #include #else #include @@ -23,7 +23,7 @@ namespace rack { struct BridgeClientConnection; static BridgeClientConnection *connections[BRIDGE_NUM_PORTS] = {}; static AudioIO *audioListeners[BRIDGE_NUM_PORTS] = {}; -static MidiIO *midiListeners[BRIDGE_NUM_PORTS] = {}; +static MidiInput *midiListeners[BRIDGE_NUM_PORTS] = {}; static std::thread serverThread; static bool serverRunning = false; @@ -44,7 +44,7 @@ struct BridgeClientConnection { if (length <= 0) return false; -#ifdef ARCH_LIN +#if ARCH_LIN int flags = MSG_NOSIGNAL; #else int flags = 0; @@ -71,7 +71,7 @@ struct BridgeClientConnection { if (length <= 0) return false; -#ifdef ARCH_LIN +#if ARCH_LIN int flags = MSG_NOSIGNAL; #else int flags = 0; @@ -188,7 +188,7 @@ struct BridgeClientConnection { void setPort(int port) { // Unbind from existing port - if (this->port >= 0 && connections[this->port] == this) { + if (0 <= this->port && connections[this->port] == this) { connections[this->port] = NULL; } @@ -239,7 +239,7 @@ struct BridgeClientConnection { static void clientRun(int client) { defer({ -#ifdef ARCH_WIN +#if ARCH_WIN if (shutdown(client, SD_SEND)) { warn("Bridge client shutdown() failed"); } @@ -253,7 +253,7 @@ static void clientRun(int client) { #endif }); -#ifdef ARCH_MAC +#if ARCH_MAC // Avoid SIGPIPE int flag = 1; if (setsockopt(client, SOL_SOCKET, SO_NOSIGPIPE, &flag, sizeof(int))) { @@ -263,7 +263,7 @@ static void clientRun(int client) { #endif // Disable non-blocking -#ifdef ARCH_WIN +#if ARCH_WIN unsigned long blockingMode = 0; if (ioctlsocket(client, FIONBIO, &blockingMode)) { warn("Bridge client ioctlsocket() failed"); @@ -284,7 +284,7 @@ static void clientRun(int client) { static void serverConnect() { // Initialize sockets -#ifdef ARCH_WIN +#if ARCH_WIN WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData)) { warn("Bridge server WSAStartup() failed"); @@ -300,7 +300,7 @@ static void serverConnect() { memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(BRIDGE_PORT); -#ifdef ARCH_WIN +#if ARCH_WIN addr.sin_addr.s_addr = inet_addr(BRIDGE_HOST); #else inet_pton(AF_INET, BRIDGE_HOST, &addr.sin_addr); @@ -313,9 +313,18 @@ static void serverConnect() { return; } defer({ - close(server); + if (close(server)) { + warn("Bridge server close() failed"); + return; + } + info("Bridge server closed"); }); +#if ARCH_MAC || ARCH_LIN + int reuseAddrFlag = 1; + setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &reuseAddrFlag, sizeof(reuseAddrFlag)); +#endif + // Bind socket to address if (bind(server, (struct sockaddr*) &addr, sizeof(addr))) { warn("Bridge server bind() failed"); @@ -330,7 +339,7 @@ static void serverConnect() { info("Bridge server started"); // Enable non-blocking -#ifdef ARCH_WIN +#if ARCH_WIN unsigned long blockingMode = 1; if (ioctlsocket(server, FIONBIO, &blockingMode)) { warn("Bridge server ioctlsocket() failed"); @@ -354,8 +363,6 @@ static void serverConnect() { std::thread clientThread(clientRun, client); clientThread.detach(); } - - info("Bridge server closed"); } static void serverRun() { @@ -375,18 +382,16 @@ void bridgeDestroy() { serverThread.join(); } -void bridgeMidiSubscribe(int port, MidiIO *midi) { +void bridgeMidiSubscribe(int port, MidiInput *midi) { if (!(0 <= port && port < BRIDGE_NUM_PORTS)) return; // Check if a Midi is already subscribed on the port if (midiListeners[port]) return; midiListeners[port] = midi; - if (connections[port]) - connections[port]->refreshAudio(); } -void bridgeMidiUnsubscribe(int port, MidiIO *midi) { +void bridgeMidiUnsubscribe(int port, MidiInput *midi) { if (!(0 <= port && port < BRIDGE_NUM_PORTS)) return; if (midiListeners[port] != midi) diff --git a/src/midi.cpp b/src/midi.cpp index 9a7f314f..a0c2af1c 100644 --- a/src/midi.cpp +++ b/src/midi.cpp @@ -61,27 +61,6 @@ std::string MidiIO::getDeviceName(int device) { return ""; } -void MidiIO::setDevice(int device) { - if (rtMidi) { - rtMidi->closePort(); - - if (device >= 0) { - rtMidi->openPort(device); - deviceName = rtMidi->getPortName(device); - } - this->device = device; - } - else if (driver == BRIDGE_DRIVER) { - if (device >= 0) { - bridgeMidiSubscribe(device, this); - } - else { - bridgeMidiUnsubscribe(device, this); - } - this->device = device; - } -} - std::string MidiIO::getChannelName(int channel) { if (channel == -1) return "All channels"; @@ -170,6 +149,27 @@ void MidiInput::setDriver(int driver) { } } +void MidiInput::setDevice(int device) { + if (rtMidi) { + rtMidi->closePort(); + + if (device >= 0) { + rtMidi->openPort(device); + deviceName = rtMidi->getPortName(device); + } + this->device = device; + } + else if (driver == BRIDGE_DRIVER) { + if (device >= 0) { + bridgeMidiSubscribe(device, this); + } + else { + bridgeMidiUnsubscribe(device, this); + } + this->device = device; + } +} + void MidiInputQueue::onMessage(MidiMessage message) { // Filter channel if (channel >= 0) { @@ -218,5 +218,19 @@ void MidiOutput::setDriver(int driver) { } } +void MidiOutput::setDevice(int device) { + if (rtMidi) { + rtMidi->closePort(); + + if (device >= 0) { + rtMidi->openPort(device); + deviceName = rtMidi->getPortName(device); + } + this->device = device; + } + else if (driver == BRIDGE_DRIVER) { + } +} + } // namespace rack