Skip to content
Closed
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
Expand Up @@ -67,17 +67,17 @@ void ConvolverNodeHostObject::setBuffer(const std::shared_ptr<AudioBuffer> &buff
}

auto threadPool = std::make_shared<ThreadPool>(4);
std::vector<Convolver> convolvers;
std::vector<std::unique_ptr<Convolver>> convolvers;
for (size_t i = 0; i < copiedBuffer->getNumberOfChannels(); ++i) {
AudioArray channelData(*copiedBuffer->getChannel(i));
convolvers.emplace_back();
convolvers.back().init(RENDER_QUANTUM_SIZE, channelData, copiedBuffer->getSize());
convolvers.back()->init(RENDER_QUANTUM_SIZE, channelData, copiedBuffer->getSize());
}
if (copiedBuffer->getNumberOfChannels() == 1) {
// add one more convolver, because right now input is always stereo
AudioArray channelData(*copiedBuffer->getChannel(0));
convolvers.emplace_back();
convolvers.back().init(RENDER_QUANTUM_SIZE, channelData, copiedBuffer->getSize());
convolvers.back()->init(RENDER_QUANTUM_SIZE, channelData, copiedBuffer->getSize());
}

auto internalBuffer = std::make_shared<DSPAudioBuffer>(
Expand All @@ -87,7 +87,7 @@ void ConvolverNodeHostObject::setBuffer(const std::shared_ptr<AudioBuffer> &buff

struct SetupData {
std::shared_ptr<AudioBuffer> buffer;
std::vector<Convolver> convolvers;
std::vector<std::unique_ptr<Convolver>> convolvers;
std::shared_ptr<ThreadPool> threadPool;
std::shared_ptr<DSPAudioBuffer> internalBuffer;
std::shared_ptr<DSPAudioBuffer> intermediateBuffer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <audioapi/core/types/ChannelCountMode.h>
#include <audioapi/core/types/ChannelInterpretation.h>
#include <audioapi/core/utils/Constants.h>
#include <audioapi/core/utils/graph/GraphObject.hpp>
#include <audioapi/types/NodeOptions.h>
#include <audioapi/utils/AudioBuffer.hpp>

Expand All @@ -18,7 +19,7 @@ namespace audioapi {

class AudioParam;

class AudioNode : public std::enable_shared_from_this<AudioNode> {
class AudioNode : public utils::graph::GraphObject, public std::enable_shared_from_this<AudioNode> {
public:
explicit AudioNode(
const std::shared_ptr<BaseAudioContext> &context,
Expand Down Expand Up @@ -62,7 +63,15 @@ class AudioNode : public std::enable_shared_from_this<AudioNode> {
return false;
}

virtual bool canBeDestructed() const;
bool canBeDestructed() const override;

[[nodiscard]] AudioNode *asAudioNode() override {
return this;
}

[[nodiscard]] const AudioNode *asAudioNode() const override {
return this;
}

protected:
friend class AudioGraphManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <audioapi/core/types/ParamChangeEventType.h>
#include <audioapi/core/utils/AudioParamEventQueue.h>
#include <audioapi/core/utils/ParamChangeEvent.hpp>
#include <audioapi/core/utils/graph/GraphObject.hpp>
#include <audioapi/utils/AudioBuffer.hpp>

#include <audioapi/utils/CrossThreadEventScheduler.hpp>
Expand All @@ -15,7 +16,7 @@

namespace audioapi {

class AudioParam {
class AudioParam : public utils::graph::GraphObject {
public:
explicit AudioParam(
float defaultValue,
Expand Down Expand Up @@ -79,6 +80,19 @@ class AudioParam {
return false;
}

/// @brief Temporary lifecycle policy for GraphObject-based graph storage.
[[nodiscard]] bool canBeDestructed() const override {
return true;
}

[[nodiscard]] AudioParam *asAudioParam() override {
return this;
}

[[nodiscard]] const AudioParam *asAudioParam() const override {
return this;
}

/// Audio-Thread only methods
/// These methods are called only from the Audio rendering thread.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ BaseAudioContext::BaseAudioContext(
const RuntimeRegistry &runtimeRegistry)
: state_(ContextState::SUSPENDED),
sampleRate_(sampleRate),
graphManager_(std::make_shared<AudioGraphManager>()),
audioEventHandlerRegistry_(audioEventHandlerRegistry),
runtimeRegistry_(runtimeRegistry),
audioEventScheduler_(AUDIO_SCHEDULER_CAPACITY) {}
audioEventScheduler_(AUDIO_SCHEDULER_CAPACITY),
disposer_(
std::make_unique<utils::DisposerImpl<DISPOSER_PAYLOAD_SIZE>>(AUDIO_SCHEDULER_CAPACITY)),
graphManager_(std::make_unique<AudioGraphManager>(this)) {}

void BaseAudioContext::initialize() {
destination_ = std::make_shared<AudioDestinationNode>(shared_from_this());
Expand Down Expand Up @@ -244,8 +246,8 @@ std::shared_ptr<PeriodicWave> BaseAudioContext::getBasicWaveForm(OscillatorType
}
}

std::shared_ptr<AudioGraphManager> BaseAudioContext::getGraphManager() const {
return graphManager_;
AudioGraphManager *BaseAudioContext::getGraphManager() const {
return graphManager_.get();
}

std::shared_ptr<IAudioEventHandlerRegistry> BaseAudioContext::getAudioEventHandlerRegistry() const {
Expand All @@ -256,4 +258,8 @@ const RuntimeRegistry &BaseAudioContext::getRuntimeRegistry() const {
return runtimeRegistry_;
}

utils::DisposerImpl<DISPOSER_PAYLOAD_SIZE> *BaseAudioContext::getDisposer() const {
return disposer_.get();
}

} // namespace audioapi
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <audioapi/core/types/ContextState.h>
#include <audioapi/core/types/OscillatorType.h>
#include <audioapi/core/utils/Constants.h>
#include <audioapi/core/utils/Disposer.hpp>
#include <audioapi/core/utils/worklets/SafeIncludes.h>
#include <audioapi/utils/AudioBuffer.hpp>
#include <audioapi/utils/CrossThreadEventScheduler.hpp>
Expand Down Expand Up @@ -104,9 +106,10 @@ class BaseAudioContext : public std::enable_shared_from_this<BaseAudioContext> {
std::shared_ptr<WaveShaperNode> createWaveShaper(const WaveShaperOptions &options);

std::shared_ptr<PeriodicWave> getBasicWaveForm(OscillatorType type);
std::shared_ptr<AudioGraphManager> getGraphManager() const;
AudioGraphManager *getGraphManager() const;
std::shared_ptr<IAudioEventHandlerRegistry> getAudioEventHandlerRegistry() const;
const RuntimeRegistry &getRuntimeRegistry() const;
utils::DisposerImpl<DISPOSER_PAYLOAD_SIZE> *getDisposer() const;

virtual void initialize();

Expand All @@ -131,7 +134,6 @@ class BaseAudioContext : public std::enable_shared_from_this<BaseAudioContext> {
private:
std::atomic<ContextState> state_;
std::atomic<float> sampleRate_;
std::shared_ptr<AudioGraphManager> graphManager_;
std::shared_ptr<IAudioEventHandlerRegistry> audioEventHandlerRegistry_;
RuntimeRegistry runtimeRegistry_;

Expand All @@ -142,6 +144,8 @@ class BaseAudioContext : public std::enable_shared_from_this<BaseAudioContext> {

static constexpr size_t AUDIO_SCHEDULER_CAPACITY = 1024;
CrossThreadEventScheduler<BaseAudioContext> audioEventScheduler_;
std::unique_ptr<utils::DisposerImpl<DISPOSER_PAYLOAD_SIZE>> disposer_;
std::unique_ptr<AudioGraphManager> graphManager_;

[[nodiscard]] virtual bool isDriverRunning() const = 0;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <audioapi/core/BaseAudioContext.h>
#include <audioapi/core/effects/ConvolverNode.h>
#include <audioapi/core/utils/AudioGraphManager.h>
#include <audioapi/core/utils/Constants.h>
#include <audioapi/types/NodeOptions.h>
#include <audioapi/utils/AudioArray.hpp>
Expand All @@ -27,7 +26,7 @@ ConvolverNode::ConvolverNode(

void ConvolverNode::setBuffer(
const std::shared_ptr<AudioBuffer> &buffer,
std::vector<Convolver> convolvers,
std::vector<std::unique_ptr<Convolver>> convolvers,
const std::shared_ptr<ThreadPool> &threadPool,
const std::shared_ptr<DSPAudioBuffer> &internalBuffer,
const std::shared_ptr<DSPAudioBuffer> &intermediateBuffer,
Expand All @@ -37,13 +36,25 @@ void ConvolverNode::setBuffer(
return;
}

auto graphManager = context->getGraphManager();
if (buffer_ != nullptr) {
context->getDisposer()->dispose(std::move(buffer_));
}

if (threadPool_ != nullptr) {
context->getDisposer()->dispose(std::move(threadPool_));
}

if (buffer_) {
graphManager->addAudioBufferForDestruction(std::move(buffer_));
for (auto it = convolvers_.begin(); it != convolvers_.end(); ++it) {
context->getDisposer()->dispose(std::move(*it));
}

// TODO move convolvers, thread pool and DSPAudioBuffers destruction to graph manager as well
if (internalBuffer_ != nullptr) {
context->getDisposer()->dispose(std::move(internalBuffer_));
}

if (intermediateBuffer_ != nullptr) {
context->getDisposer()->dispose(std::move(intermediateBuffer_));
}

buffer_ = buffer;
convolvers_ = std::move(convolvers);
Expand Down Expand Up @@ -86,7 +97,7 @@ void ConvolverNode::onInputDisabled() {
numberOfEnabledInputNodes_ -= 1;
if (isEnabled() && numberOfEnabledInputNodes_ == 0) {
signalledToStop_ = true;
remainingSegments_ = convolvers_.at(0).getSegCount();
remainingSegments_ = convolvers_.at(0)->getSegCount();
}
}

Expand Down Expand Up @@ -144,7 +155,7 @@ void ConvolverNode::performConvolution(const std::shared_ptr<DSPAudioBuffer> &pr
if (processingBuffer->getNumberOfChannels() == 1) {
for (int i = 0; i < convolvers_.size(); ++i) {
threadPool_->schedule([&, i] {
convolvers_[i].process(
convolvers_[i]->process(
*processingBuffer->getChannel(0), *intermediateBuffer_->getChannel(i));
});
}
Expand All @@ -160,7 +171,7 @@ void ConvolverNode::performConvolution(const std::shared_ptr<DSPAudioBuffer> &pr
}
for (int i = 0; i < convolvers_.size(); ++i) {
threadPool_->schedule([this, i, inputChannelMap, outputChannelMap, &processingBuffer] {
convolvers_[i].process(
convolvers_[i]->process(
*processingBuffer->getChannel(inputChannelMap[i]),
*intermediateBuffer_->getChannel(outputChannelMap[i]));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ConvolverNode : public AudioNode {
/// @note Audio Thread only
void setBuffer(
const std::shared_ptr<AudioBuffer> &buffer,
std::vector<Convolver> convolvers,
std::vector<std::unique_ptr<Convolver>> convolvers,
const std::shared_ptr<ThreadPool> &threadPool,
const std::shared_ptr<DSPAudioBuffer> &internalBuffer,
const std::shared_ptr<DSPAudioBuffer> &intermediateBuffer,
Expand Down Expand Up @@ -58,7 +58,7 @@ class ConvolverNode : public AudioNode {
// buffer to hold internal processed data
std::shared_ptr<DSPAudioBuffer> internalBuffer_;
// vectors of convolvers, one per channel
std::vector<Convolver> convolvers_;
std::vector<std::unique_ptr<Convolver>> convolvers_;
std::shared_ptr<ThreadPool> threadPool_;

void performConvolution(const std::shared_ptr<DSPAudioBuffer> &processingBuffer);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <audioapi/core/AudioParam.h>
#include <audioapi/core/BaseAudioContext.h>
#include <audioapi/core/sources/AudioBufferQueueSourceNode.h>
#include <audioapi/core/utils/AudioGraphManager.h>
#include <audioapi/core/utils/Constants.h>
#include <audioapi/core/utils/Locker.h>
#include <audioapi/dsp/AudioUtils.hpp>
Expand Down Expand Up @@ -78,10 +77,8 @@ void AudioBufferQueueSourceNode::dequeueBuffer(const size_t bufferId) {
return;
}

auto graphManager = context->getGraphManager();

if (buffers_.front().first == bufferId) {
graphManager->addAudioBufferForDestruction(std::move(buffers_.front().second));
context->getDisposer()->dispose(std::move(buffers_.front().second));
buffers_.pop_front();
vReadIndex_ = 0.0;
return;
Expand All @@ -91,7 +88,7 @@ void AudioBufferQueueSourceNode::dequeueBuffer(const size_t bufferId) {
// And keep vReadIndex_ at the same position.
for (auto it = std::next(buffers_.begin()); it != buffers_.end(); ++it) {
if (it->first == bufferId) {
graphManager->addAudioBufferForDestruction(std::move(it->second));
context->getDisposer()->dispose(std::move(it->second));
buffers_.erase(it);
return;
}
Expand All @@ -102,7 +99,7 @@ void AudioBufferQueueSourceNode::dequeueBuffer(const size_t bufferId) {
void AudioBufferQueueSourceNode::clearBuffers() {
if (auto context = context_.lock()) {
for (auto it = buffers_.begin(); it != buffers_.end(); ++it) {
context->getGraphManager()->addAudioBufferForDestruction(std::move(it->second));
context->getDisposer()->dispose(std::move(it->second));
}

buffers_.clear();
Expand Down Expand Up @@ -215,15 +212,15 @@ void AudioBufferQueueSourceNode::processWithoutInterpolation(
buffers_.emplace_back(bufferId, tailBuffer_);
addExtraTailFrames_ = false;
} else {
context->getGraphManager()->addAudioBufferForDestruction(std::move(buffer));
context->getDisposer()->dispose(std::move(buffer));
processingBuffer->zero(writeIndex, framesLeft);
readIndex = 0;

break;
}
}

context->getGraphManager()->addAudioBufferForDestruction(std::move(buffer));
context->getDisposer()->dispose(std::move(buffer));
data = buffers_.front();
bufferId = data.first;
buffer = data.second;
Expand Down Expand Up @@ -296,14 +293,14 @@ void AudioBufferQueueSourceNode::processWithInterpolation(
sendOnBufferEndedEvent(bufferId, buffers_.empty());

if (buffers_.empty()) {
context->getGraphManager()->addAudioBufferForDestruction(std::move(buffer));
context->getDisposer()->dispose(std::move(buffer));
processingBuffer->zero(writeIndex, framesLeft);
vReadIndex_ = 0.0;
break;
}

context->getGraphManager()->addAudioBufferForDestruction(std::move(buffer));
vReadIndex_ = vReadIndex_ - buffer->getSize();
context->getDisposer()->dispose(std::move(buffer));
data = buffers_.front();
bufferId = data.first;
buffer = data.second;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <audioapi/core/AudioParam.h>
#include <audioapi/core/BaseAudioContext.h>
#include <audioapi/core/sources/AudioBufferSourceNode.h>
#include <audioapi/core/utils/AudioGraphManager.h>
#include <audioapi/core/utils/Constants.h>
#include <audioapi/core/utils/Locker.h>
#include <audioapi/dsp/AudioUtils.hpp>
Expand Down Expand Up @@ -55,13 +54,17 @@ void AudioBufferSourceNode::setBuffer(
return;
}

auto graphManager = context->getGraphManager();

if (buffer_ != nullptr) {
graphManager->addAudioBufferForDestruction(std::move(buffer_));
context->getDisposer()->dispose(std::move(buffer_));
}

if (playbackRateBuffer_ != nullptr) {
context->getDisposer()->dispose(std::move(playbackRateBuffer_));
}

// TODO move DSPAudioBuffers destruction to graph manager as well
if (audioBuffer_ != nullptr) {
context->getDisposer()->dispose(std::move(audioBuffer_));
}

if (buffer == nullptr) {
loopEnd_ = 0;
Expand Down
Loading
Loading