From 7fbbbfce44f76f3eaff0fe97a6c9f0eba0b61b8d Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Fri, 9 Mar 2018 14:29:08 -0500 Subject: [PATCH] Router audio stream from Bridge to AudioIO --- include/audio.hpp | 2 +- src/Core/AudioInterface.cpp | 10 +++++----- src/bridge.cpp | 15 +++++++++++++-- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/include/audio.hpp b/include/audio.hpp index 7dcde1db..5a59cd2e 100644 --- a/include/audio.hpp +++ b/include/audio.hpp @@ -51,7 +51,7 @@ struct AudioIO { std::vector getSampleRates(); - virtual void processStream(const float *input, float *output, int length) {} + virtual void processStream(const float *input, float *output, int frames) {} virtual void onCloseStream() {} virtual void onOpenStream() {} json_t *toJson(); diff --git a/src/Core/AudioInterface.cpp b/src/Core/AudioInterface.cpp index ca059414..3e90d21c 100644 --- a/src/Core/AudioInterface.cpp +++ b/src/Core/AudioInterface.cpp @@ -40,10 +40,10 @@ struct AudioInterfaceIO : AudioIO { setDevice(-1, 0); } - void processStream(const float *input, float *output, int length) override { + void processStream(const float *input, float *output, int frames) override { if (numInputs > 0) { // TODO Do we need to wait on the input to be consumed here? Experimentally, it works fine if we don't. - for (int i = 0; i < length; i++) { + for (int i = 0; i < frames; i++) { if (inputBuffer.full()) break; Frame f; @@ -56,19 +56,19 @@ struct AudioInterfaceIO : AudioIO { if (numOutputs > 0) { std::unique_lock lock(audioMutex); auto cond = [&] { - return (outputBuffer.size() >= (size_t) length); + return (outputBuffer.size() >= (size_t) frames); }; auto timeout = std::chrono::milliseconds(100); if (audioCv.wait_for(lock, timeout, cond)) { // Consume audio block - for (int i = 0; i < length; i++) { + for (int i = 0; i < frames; i++) { Frame f = outputBuffer.shift(); memcpy(&output[numOutputs * i], &f, numOutputs * sizeof(float)); } } else { // Timed out, fill output with zeros - memset(output, 0, length * numOutputs * sizeof(float)); + memset(output, 0, frames * numOutputs * sizeof(float)); } } diff --git a/src/bridge.cpp b/src/bridge.cpp index f613f891..65c92bcc 100644 --- a/src/bridge.cpp +++ b/src/bridge.cpp @@ -139,9 +139,12 @@ struct BridgeClientConnection { } else { if (recvQueue.size() >= (size_t) audioBufferLength) { - // TODO Do something with the data - recvQueue.start += audioBufferLength; debug("Received %d audio samples", audioBufferLength); + float input[audioBufferLength]; + float output[audioBufferLength] = {}; + recvQueue.shiftBuffer((uint8_t*) input, sizeof(float) * audioBufferLength); + int frames = audioBufferLength / 2; + processStream(input, output, frames); audioBufferLength = -1; currentCommand = NO_COMMAND; return true; @@ -180,6 +183,14 @@ struct BridgeClientConnection { // Loop the state machine until it returns false while (step()) {} } + + void processStream(const float *input, float *output, int length) { + if (!(0 <= channel && channel < BRIDGE_CHANNELS)) + return; + if (!audioListeners[channel]) + return; + audioListeners[channel]->processStream(input, output, length); + } };