Skip to content
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
8 changes: 4 additions & 4 deletions apps/fabric-example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2675,7 +2675,7 @@ PODS:
- RNWorklets
- SocketRocket
- Yoga
- RNScreens (4.17.1):
- RNScreens (4.18.0):
- boost
- DoubleConversion
- fast_float
Expand All @@ -2702,10 +2702,10 @@ PODS:
- ReactCodegen
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- RNScreens/common (= 4.17.1)
- RNScreens/common (= 4.18.0)
- SocketRocket
- Yoga
- RNScreens/common (4.17.1):
- RNScreens/common (4.18.0):
- boost
- DoubleConversion
- fast_float
Expand Down Expand Up @@ -3219,7 +3219,7 @@ SPEC CHECKSUMS:
RNAudioAPI: c7dc7b491a0e4b23535a55fd9b4a00d0f803f4bb
RNGestureHandler: f1dd7f92a0faa2868a919ab53bb9d66eb4ebfcf5
RNReanimated: e4993dd98196c698cbacc1441a4ac5b855ae56dc
RNScreens: 833237c48c756d40764540246a501b47dadb2cac
RNScreens: d821082c6dd1cb397cc0c98b026eeafaa68be479
RNSVG: 8c0bbfa480a24b24468f1c76bd852a4aac3178e6
RNWorklets: d4553da98908962b6b834d5f2d26525b0d6840ad
SocketRocket: d4aabe649be1e368d1318fdf28a022d714d65748
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class AudioAPIModuleInstaller {

audioContext = std::make_shared<AudioContext>(
sampleRate, audioEventHandlerRegistry, runtimeRegistry);
audioContext->initialize();

auto audioContextHostObject =
std::make_shared<AudioContextHostObject>(audioContext, &runtime, jsCallInvoker);
Expand Down Expand Up @@ -117,6 +118,7 @@ class AudioAPIModuleInstaller {

auto offlineAudioContext = std::make_shared<OfflineAudioContext>(
numberOfChannels, length, sampleRate, audioEventHandlerRegistry, runtimeRegistry);
offlineAudioContext->initialize();

auto audioContextHostObject = std::make_shared<OfflineAudioContextHostObject>(
offlineAudioContext, &runtime, jsCallInvoker);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ DelayNodeHostObject::DelayNodeHostObject(const std::shared_ptr<DelayNode> &node)

size_t DelayNodeHostObject::getSizeInBytes() const {
auto delayNode = std::static_pointer_cast<DelayNode>(node_);
return sizeof(float) * delayNode->context_->getSampleRate() *
delayNode->getDelayTimeParam()->getMaxValue();
auto base = sizeof(float) * delayNode->getDelayTimeParam()->getMaxValue();
if (std::shared_ptr<BaseAudioContext> context = delayNode->context_.lock()) {
return base * context->getSampleRate();
} else {
return base * 44100; // Fallback to common sample rate
}
}

JSI_PROPERTY_GETTER_IMPL(DelayNodeHostObject, delayTime) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,8 @@ AudioContext::AudioContext(
float sampleRate,
const std::shared_ptr<IAudioEventHandlerRegistry> &audioEventHandlerRegistry,
const RuntimeRegistry &runtimeRegistry)
: BaseAudioContext(audioEventHandlerRegistry, runtimeRegistry) {
#ifdef ANDROID
audioPlayer_ = std::make_shared<AudioPlayer>(
this->renderAudio(), sampleRate, destination_->getChannelCount());
#else
audioPlayer_ = std::make_shared<IOSAudioPlayer>(
this->renderAudio(), sampleRate, destination_->getChannelCount());
#endif

: BaseAudioContext(audioEventHandlerRegistry, runtimeRegistry), playerHasBeenStarted_(false) {
sampleRate_ = sampleRate;
playerHasBeenStarted_ = false;
state_ = ContextState::SUSPENDED;
}

Expand All @@ -35,6 +26,17 @@ AudioContext::~AudioContext() {
}
}

void AudioContext::initialize() {
BaseAudioContext::initialize();
#ifdef ANDROID
audioPlayer_ = std::make_shared<AudioPlayer>(
this->renderAudio(), sampleRate_, destination_->getChannelCount());
#else
audioPlayer_ = std::make_shared<IOSAudioPlayer>(
this->renderAudio(), sampleRate_, destination_->getChannelCount());
#endif
}

void AudioContext::close() {
state_ = ContextState::CLOSED;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class AudioContext : public BaseAudioContext {
bool resume();
bool suspend();
bool start();
void initialize() override;

private:
#ifdef ANDROID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@

namespace audioapi {

AudioNode::AudioNode(BaseAudioContext *context) : context_(context) {
audioBus_ =
std::make_shared<AudioBus>(RENDER_QUANTUM_SIZE, channelCount_, context->getSampleRate());
}
AudioNode::AudioNode(std::shared_ptr<BaseAudioContext> context)
: context_(context),
audioBus_(
std::make_shared<AudioBus>(
RENDER_QUANTUM_SIZE,
channelCount_,
context->getSampleRate())) {}

AudioNode::~AudioNode() {
if (isInitialized_) {
Expand Down Expand Up @@ -42,28 +45,38 @@ std::string AudioNode::getChannelInterpretation() const {
}

void AudioNode::connect(const std::shared_ptr<AudioNode> &node) {
context_->getNodeManager()->addPendingNodeConnection(
shared_from_this(), node, AudioNodeManager::ConnectionType::CONNECT);
if (std::shared_ptr<BaseAudioContext> context = context_.lock()) {
context->getNodeManager()->addPendingNodeConnection(
shared_from_this(), node, AudioNodeManager::ConnectionType::CONNECT);
}
}

void AudioNode::connect(const std::shared_ptr<AudioParam> &param) {
context_->getNodeManager()->addPendingParamConnection(
shared_from_this(), param, AudioNodeManager::ConnectionType::CONNECT);
if (std::shared_ptr<BaseAudioContext> context = context_.lock()) {
context->getNodeManager()->addPendingParamConnection(
shared_from_this(), param, AudioNodeManager::ConnectionType::CONNECT);
}
}

void AudioNode::disconnect() {
context_->getNodeManager()->addPendingNodeConnection(
shared_from_this(), nullptr, AudioNodeManager::ConnectionType::DISCONNECT_ALL);
if (std::shared_ptr<BaseAudioContext> context = context_.lock()) {
context->getNodeManager()->addPendingNodeConnection(
shared_from_this(), nullptr, AudioNodeManager::ConnectionType::DISCONNECT_ALL);
}
}

void AudioNode::disconnect(const std::shared_ptr<AudioNode> &node) {
context_->getNodeManager()->addPendingNodeConnection(
shared_from_this(), node, AudioNodeManager::ConnectionType::DISCONNECT);
if (std::shared_ptr<BaseAudioContext> context = context_.lock()) {
context->getNodeManager()->addPendingNodeConnection(
shared_from_this(), node, AudioNodeManager::ConnectionType::DISCONNECT);
}
}

void AudioNode::disconnect(const std::shared_ptr<AudioParam> &param) {
context_->getNodeManager()->addPendingParamConnection(
shared_from_this(), param, AudioNodeManager::ConnectionType::DISCONNECT);
if (std::shared_ptr<BaseAudioContext> context = context_.lock()) {
context->getNodeManager()->addPendingParamConnection(
shared_from_this(), param, AudioNodeManager::ConnectionType::DISCONNECT);
}
}

bool AudioNode::isEnabled() const {
Expand Down Expand Up @@ -147,23 +160,25 @@ std::shared_ptr<AudioBus> AudioNode::processAudio(

// Finally, process the node itself.
return processNode(processingBus, framesToProcess);
;
}

bool AudioNode::isAlreadyProcessed() {
assert(context_ != nullptr);
if (std::shared_ptr<BaseAudioContext> context = context_.lock()) {
std::size_t currentSampleFrame = context->getCurrentSampleFrame();

// check if the node has already been processed for this rendering quantum
if (currentSampleFrame == lastRenderedFrame_) {
return true;
}

std::size_t currentSampleFrame = context_->getCurrentSampleFrame();
// Update the last rendered frame before processing node and its inputs.
lastRenderedFrame_ = currentSampleFrame;

// check if the node has already been processed for this rendering quantum
if (currentSampleFrame == lastRenderedFrame_) {
return true;
return false;
}

// Update the last rendered frame before processing node and its inputs.
lastRenderedFrame_ = currentSampleFrame;

return false;
// If context is invalid, consider it as already processed to avoid processing
return true;
}

std::shared_ptr<AudioBus> AudioNode::processInputs(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class AudioParam;

class AudioNode : public std::enable_shared_from_this<AudioNode> {
public:
explicit AudioNode(BaseAudioContext *context);
explicit AudioNode(std::shared_ptr<BaseAudioContext> context);
virtual ~AudioNode();

int getNumberOfInputs() const;
Expand Down Expand Up @@ -47,13 +47,13 @@ class AudioNode : public std::enable_shared_from_this<AudioNode> {
friend class AudioDestinationNode;
friend class ConvolverNode;
friend class DelayNodeHostObject;
int channelCount_ = 2;

BaseAudioContext *context_;
std::weak_ptr<BaseAudioContext> context_;
std::shared_ptr<AudioBus> audioBus_;

int numberOfInputs_ = 1;
int numberOfOutputs_ = 1;
int channelCount_ = 2;
ChannelCountMode channelCountMode_ = ChannelCountMode::MAX;
ChannelInterpretation channelInterpretation_ =

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ AudioParam::AudioParam(
float defaultValue,
float minValue,
float maxValue,
BaseAudioContext *context)
std::shared_ptr<BaseAudioContext> context)
: context_(context),
value_(defaultValue),
defaultValue_(defaultValue),
minValue_(minValue),
maxValue_(maxValue),
eventsQueue_(),
eventScheduler_(32),
startTime_(0),
endTime_(0),
startValue_(defaultValue),
endValue_(defaultValue),
audioBus_(std::make_shared<AudioBus>(RENDER_QUANTUM_SIZE, 1, context->getSampleRate())) {
inputBuses_.reserve(4);
inputNodes_.reserve(4);
startTime_ = 0;
endTime_ = 0;
startValue_ = value_;
endValue_ = value_;
// Default calculation function just returns the static value
calculateValue_ = [this](double, double, float, float, double) {
return value_;
Expand Down Expand Up @@ -258,7 +258,10 @@ std::shared_ptr<AudioBus> AudioParam::processARateParam(int framesToProcess, dou
processScheduledEvents();
auto processingBus = calculateInputs(audioBus_, framesToProcess);

float sampleRate = context_->getSampleRate();
std::shared_ptr<BaseAudioContext> context = context_.lock();
if (context == nullptr)
return processingBus;
float sampleRate = context->getSampleRate();
float *busData = processingBus->getChannel(0)->getData();
float timeCache = time;
float timeStep = 1.0f / sampleRate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class AudioParam {
float defaultValue,
float minValue,
float maxValue,
BaseAudioContext *context);
std::shared_ptr<BaseAudioContext> context);

/// JS-Thread only methods
/// These methods are called only from HostObjects invoked on the JS thread.
Expand Down Expand Up @@ -93,7 +93,7 @@ class AudioParam {

private:
// Core parameter state
BaseAudioContext *context_;
std::weak_ptr<BaseAudioContext> context_;
float value_;
float defaultValue_;
float minValue_;
Expand Down
Loading