Skip to content

fix: added mutex in param for thread safety #457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <audioapi/core/AudioParam.h>
#include <audioapi/core/BaseAudioContext.h>
#include <audioapi/core/utils/Locker.h>
#include <audioapi/dsp/AudioUtils.h>
#include <audioapi/dsp/VectorMath.h>
#include <audioapi/utils/AudioArray.h>
Expand Down Expand Up @@ -47,11 +48,16 @@ float AudioParam::getMaxValue() const {
return maxValue_;
}

std::mutex &AudioParam::getQueueLock() {
return queueLock_;
}

void AudioParam::setValue(float value) {
value_ = std::clamp(value, minValue_, maxValue_);
}

float AudioParam::getValueAtTime(double time) {
Locker lock(getQueueLock());
if (endTime_ < time && !eventsQueue_.empty()) {
auto event = eventsQueue_.front();
startTime_ = event.getStartTime();
Expand Down Expand Up @@ -84,6 +90,7 @@ void AudioParam::setValueAtTime(float value, double startTime) {
return endValue;
};

Locker lock(getQueueLock());
auto event = ParamChangeEvent(
startTime,
startTime,
Expand Down Expand Up @@ -117,6 +124,7 @@ void AudioParam::linearRampToValueAtTime(float value, double endTime) {
return endValue;
};

Locker lock(getQueueLock());
auto event = ParamChangeEvent(
getQueueEndTime(),
endTime,
Expand Down Expand Up @@ -151,6 +159,7 @@ void AudioParam::exponentialRampToValueAtTime(float value, double endTime) {
return endValue;
};

Locker lock(getQueueLock());
auto event = ParamChangeEvent(
getQueueEndTime(),
endTime,
Expand Down Expand Up @@ -181,6 +190,7 @@ void AudioParam::setTargetAtTime(
(startValue - target) * exp(-(time - startTime) / timeConstant));
};

Locker lock(getQueueLock());
auto event = ParamChangeEvent(
startTime,
startTime,
Expand Down Expand Up @@ -225,6 +235,7 @@ void AudioParam::setValueCurveAtTime(
return endValue;
};

Locker lock(getQueueLock());
auto event = ParamChangeEvent(
startTime,
startTime + duration,
Expand All @@ -236,6 +247,7 @@ void AudioParam::setValueCurveAtTime(
}

void AudioParam::cancelScheduledValues(double cancelTime) {
Locker lock(getQueueLock());
auto it = eventsQueue_.rbegin();
while (it->getEndTime() >= cancelTime) {
if (it->getStartTime() >= cancelTime ||
Expand All @@ -248,6 +260,7 @@ void AudioParam::cancelScheduledValues(double cancelTime) {
}

void AudioParam::cancelAndHoldAtTime(double cancelTime) {
Locker lock(getQueueLock());
auto it = eventsQueue_.rbegin();
while (it->getEndTime() >= cancelTime) {
if (it->getStartTime() >= cancelTime) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <vector>
#include <unordered_set>
#include <cstddef>
#include <mutex>

namespace audioapi {

Expand Down Expand Up @@ -50,6 +51,7 @@ class AudioParam {
std::deque<ParamChangeEvent> eventsQueue_;
std::unordered_set<AudioNode *> inputNodes_;
std::shared_ptr<AudioBus> audioBus_;
std::mutex queueLock_;

double startTime_;
double endTime_;
Expand All @@ -58,6 +60,7 @@ class AudioParam {
std::function<float(double, double, float, float, double)> calculateValue_;
std::vector<std::shared_ptr<AudioBus>> inputBuses_ = {};

std::mutex &getQueueLock();
double getQueueEndTime();
float getQueueEndValue();
void updateQueue(ParamChangeEvent &event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class AudioNodeManager {
AudioNodeManager() = default;
~AudioNodeManager();

std::mutex &getGraphLock();

void preProcessGraph();

void addPendingNodeConnection(
Expand Down Expand Up @@ -62,6 +60,7 @@ class AudioNodeManager {
ConnectionType>>
audioParamToConnect_;

std::mutex &getGraphLock();
void settlePendingConnections();
void cleanupNode(const std::shared_ptr<AudioNode> &node);
void prepareNodesForDestruction();
Expand Down
Loading