Browse Source

Move processMutex from RtAudioDevice to audio::Device.

tags/v2.0.2
Andrew Belt 2 years ago
parent
commit
884389bc7b
3 changed files with 7 additions and 15 deletions
  1. +4
    -0
      include/audio.hpp
  2. +3
    -0
      src/audio.cpp
  3. +0
    -15
      src/rtaudio.cpp

+ 4
- 0
include/audio.hpp View File

@@ -1,6 +1,7 @@
#pragma once
#include <vector>
#include <set>
#include <mutex>

#include <jansson.h>

@@ -72,6 +73,9 @@ Methods throw `rack::Exception` if the driver API has an exception.
*/
struct Device {
std::set<Port*> subscribed;
/** Ensures that ports do not subscribe/unsubscribe while processBuffer() is called. */
std::mutex processMutex;

virtual ~Device() {}

virtual std::string getName() {


+ 3
- 0
src/audio.cpp View File

@@ -19,10 +19,12 @@ static std::vector<std::pair<int, Driver*>> drivers;
////////////////////

void Device::subscribe(Port* port) {
std::lock_guard<std::mutex> lock(processMutex);
subscribed.insert(port);
}

void Device::unsubscribe(Port* port) {
std::lock_guard<std::mutex> 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<std::mutex> 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);


+ 0
- 15
src/rtaudio.cpp View File

@@ -1,6 +1,5 @@
#include <map>
#include <algorithm>
#include <mutex>

#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<std::mutex> lock(processMutex);
Device::subscribe(port);
}

void unsubscribe(audio::Port* port) override {
std::lock_guard<std::mutex> 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<std::mutex> lock(that->processMutex);

system::setThreadName("RtAudio");

int inputStride = that->getNumInputs();


Loading…
Cancel
Save