From baadbda40d79008d1eacffdeecfdad221d42c8a5 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Thu, 15 Mar 2018 07:17:26 -0400 Subject: [PATCH] Bridge port mapping --- src/Core/AudioInterface.cpp | 4 ++++ src/audio.cpp | 9 +++------ src/bridge.cpp | 25 +++++++++++++++++-------- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/Core/AudioInterface.cpp b/src/Core/AudioInterface.cpp index 6edf373f..e81f6f49 100644 --- a/src/Core/AudioInterface.cpp +++ b/src/Core/AudioInterface.cpp @@ -78,6 +78,10 @@ struct AudioInterfaceIO : AudioIO { inputBuffer.clear(); outputBuffer.clear(); } + + void onChannelsChange() override { + debug("Channels changed %d %d", numOutputs, numInputs); + } }; diff --git a/src/audio.cpp b/src/audio.cpp index c7df42d9..1ac78196 100644 --- a/src/audio.cpp +++ b/src/audio.cpp @@ -203,8 +203,7 @@ void AudioIO::openStream() { if (rtAudio->isStreamOpen()) return; - numOutputs = clamp((int) deviceInfo.outputChannels - offset, 0, maxChannels); - numInputs = clamp((int) deviceInfo.inputChannels - offset, 0, maxChannels); + setChannels(clamp((int) deviceInfo.outputChannels - offset, 0, maxChannels), clamp((int) deviceInfo.inputChannels - offset, 0, maxChannels)); if (numOutputs == 0 && numInputs == 0) { warn("RtAudio device %d has 0 inputs and 0 outputs"); @@ -258,8 +257,7 @@ void AudioIO::openStream() { onOpenStream(); } if (driver == BRIDGE_DRIVER) { - numOutputs = 0; - numInputs = 0; + setChannels(0, 0); // TEMP sampleRate = 44100; blockSize = 256; @@ -268,8 +266,7 @@ void AudioIO::openStream() { } void AudioIO::closeStream() { - numOutputs = 0; - numInputs = 0; + setChannels(0, 0); if (rtAudio) { if (rtAudio->isStreamRunning()) { diff --git a/src/bridge.cpp b/src/bridge.cpp index 7063ff23..47409dfa 100644 --- a/src/bridge.cpp +++ b/src/bridge.cpp @@ -57,6 +57,10 @@ struct BridgeClientConnection { int audioBufferLength = -1; bool audioActive = false; + ~BridgeClientConnection() { + setPort(-1); + } + void send(const uint8_t *buffer, int length) { if (length <= 0) return; @@ -66,7 +70,8 @@ struct BridgeClientConnection { int sendFlags = 0; #endif ssize_t written = ::send(client, (const char*) buffer, length, sendFlags); - if (written < 0) + // We must write the entire buffer + if (written < length) closeRequested = true; } @@ -163,7 +168,7 @@ struct BridgeClientConnection { if (recvQueue.size() >= 3) { uint8_t midiBuffer[3]; recvQueue.shiftBuffer(midiBuffer, 3); - debug("MIDI: %02x %02x %02x", midiBuffer[0], midiBuffer[1], midiBuffer[2]); + // debug("MIDI: %02x %02x %02x", midiBuffer[0], midiBuffer[1], midiBuffer[2]); currentCommand = NO_COMMAND; return true; } @@ -225,12 +230,14 @@ struct BridgeClientConnection { case AUDIO_ACTIVATE: { audioActive = true; refreshAudioActive(); + currentCommand = NO_COMMAND; return true; } break; case AUDIO_DEACTIVATE: { audioActive = false; refreshAudioActive(); + currentCommand = NO_COMMAND; return true; } break; @@ -245,20 +252,18 @@ struct BridgeClientConnection { } void setPort(int newPort) { - if (!(0 <= newPort && newPort < BRIDGE_NUM_PORTS)) - return; // Unbind from existing port - if (connections[port] == this) { + if (port >= 0 && connections[port] == this) { if (audioListeners[port]) audioListeners[port]->setChannels(0, 0); connections[port] = NULL; } - port = newPort; // Bind to new port - if (!connections[port]) { - connections[port] = this; + if (newPort >= 0 && !connections[newPort]) { + connections[newPort] = this; refreshAudioActive(); + port = newPort; } else { port = -1; @@ -271,9 +276,12 @@ struct BridgeClientConnection { if (!audioListeners[port]) return; audioListeners[port]->processStream(input, output, frames); + debug("%d frames", frames); } void refreshAudioActive() { + if (!(0 <= port && port < BRIDGE_NUM_PORTS)) + return; if (!audioListeners[port]) return; if (audioActive) @@ -426,6 +434,7 @@ void bridgeDestroy() { void bridgeAudioSubscribe(int port, AudioIO *audio) { if (!(0 <= port && port < BRIDGE_NUM_PORTS)) return; + // Check if an Audio is already subscribed on the port if (audioListeners[port]) return; audioListeners[port] = audio;