From 884389bc7b65b557804a3e9d5514b3a54ad386e9 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Mon, 6 Dec 2021 03:34:37 -0500 Subject: [PATCH] Move processMutex from RtAudioDevice to audio::Device. --- include/audio.hpp | 4 ++++ src/audio.cpp | 3 +++ src/rtaudio.cpp | 15 --------------- 3 files changed, 7 insertions(+), 15 deletions(-) diff --git a/include/audio.hpp b/include/audio.hpp index 558b3125..a4941baa 100644 --- a/include/audio.hpp +++ b/include/audio.hpp @@ -1,6 +1,7 @@ #pragma once #include #include +#include #include @@ -72,6 +73,9 @@ Methods throw `rack::Exception` if the driver API has an exception. */ struct Device { std::set subscribed; + /** Ensures that ports do not subscribe/unsubscribe while processBuffer() is called. */ + std::mutex processMutex; + virtual ~Device() {} virtual std::string getName() { diff --git a/src/audio.cpp b/src/audio.cpp index 447fdf7f..1f0b023f 100644 --- a/src/audio.cpp +++ b/src/audio.cpp @@ -19,10 +19,12 @@ static std::vector> drivers; //////////////////// void Device::subscribe(Port* port) { + std::lock_guard lock(processMutex); subscribed.insert(port); } void Device::unsubscribe(Port* port) { + std::lock_guard lock(processMutex); auto it = subscribed.find(port); if (it != subscribed.end()) subscribed.erase(it); @@ -32,6 +34,7 @@ void Device::processBuffer(const float* input, int inputStride, float* output, i // Zero output in case no Port writes values to it. std::memset(output, 0, frames * outputStride * sizeof(float)); + std::lock_guard lock(processMutex); for (Port* port : subscribed) { // Setting the thread context should probably be the responsibility of Port, but because processInput() etc are overridden, this is the only good place for it. contextSet(port->context); diff --git a/src/rtaudio.cpp b/src/rtaudio.cpp index fe6cf7f1..76c874c4 100644 --- a/src/rtaudio.cpp +++ b/src/rtaudio.cpp @@ -1,6 +1,5 @@ #include #include -#include #pragma GCC diagnostic push #ifndef __clang__ @@ -43,8 +42,6 @@ struct RtAudioDevice : audio::Device { RtAudio::StreamOptions options; int blockSize = 0; float sampleRate = 0; - /** Ensures that ports do not subscribe/unsubscribe while processBuffer() is called. */ - std::mutex processMutex; RtAudioDevice(RtAudio::Api api, int deviceId) { this->api = api; @@ -212,16 +209,6 @@ struct RtAudioDevice : audio::Device { openStream(); } - void subscribe(audio::Port* port) override { - std::lock_guard lock(processMutex); - Device::subscribe(port); - } - - void unsubscribe(audio::Port* port) override { - std::lock_guard lock(processMutex); - Device::unsubscribe(port); - } - static int rtAudioCallback(void* outputBuffer, void* inputBuffer, unsigned int nFrames, double streamTime, RtAudioStreamStatus status, void* userData) { // fprintf(stderr, "."); // fflush(stderr); @@ -229,8 +216,6 @@ struct RtAudioDevice : audio::Device { RtAudioDevice* that = (RtAudioDevice*) userData; assert(that); - std::lock_guard lock(that->processMutex); - system::setThreadName("RtAudio"); int inputStride = that->getNumInputs();