From 11cf2633e81d80d49065205a286c97c132adfe77 Mon Sep 17 00:00:00 2001 From: Maciej Makowski Date: Thu, 24 Oct 2024 13:10:09 +0200 Subject: [PATCH 1/5] feat: implemented BaseAudioContext --- .../BaseAudioContextHostObject.cpp | 175 ++++++++++++++++++ .../HostObjects/BaseAudioContextHostObject.h | 38 ++++ .../common/cpp/core/BaseAudioContext.cpp | 79 ++++++++ .../common/cpp/core/BaseAudioContext.h | 73 ++++++++ .../cpp/wrappers/BaseAudioContextWrapper.cpp | 66 +++++++ .../cpp/wrappers/BaseAudioContextWrapper.h | 39 ++++ 6 files changed, 470 insertions(+) create mode 100644 packages/react-native-audio-api/common/cpp/HostObjects/BaseAudioContextHostObject.cpp create mode 100644 packages/react-native-audio-api/common/cpp/HostObjects/BaseAudioContextHostObject.h create mode 100644 packages/react-native-audio-api/common/cpp/core/BaseAudioContext.cpp create mode 100644 packages/react-native-audio-api/common/cpp/core/BaseAudioContext.h create mode 100644 packages/react-native-audio-api/common/cpp/wrappers/BaseAudioContextWrapper.cpp create mode 100644 packages/react-native-audio-api/common/cpp/wrappers/BaseAudioContextWrapper.h diff --git a/packages/react-native-audio-api/common/cpp/HostObjects/BaseAudioContextHostObject.cpp b/packages/react-native-audio-api/common/cpp/HostObjects/BaseAudioContextHostObject.cpp new file mode 100644 index 00000000..f983f4ba --- /dev/null +++ b/packages/react-native-audio-api/common/cpp/HostObjects/BaseAudioContextHostObject.cpp @@ -0,0 +1,175 @@ +#include "BaseAudioContextHostObject.h" + +namespace audioapi { + using namespace facebook; + + BaseAudioContextHostObject::BaseAudioContextHostObject( + const std::shared_ptr &wrapper) + : wrapper_(wrapper) { + auto destinationNodeWrapper = wrapper_->getDestination(); + destination_ = + AudioDestinationNodeHostObject::createFromWrapper(destinationNodeWrapper); + } + + std::vector BaseAudioContextHostObject::getPropertyNames( + jsi::Runtime &runtime) { + std::vector propertyNames; + propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "destination")); + propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "state")); + propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "sampleRate")); + propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "currentTime")); + propertyNames.push_back( + jsi::PropNameID::forUtf8(runtime, "createOscillator")); + propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "createGain")); + propertyNames.push_back( + jsi::PropNameID::forUtf8(runtime, "createStereoPanner")); + propertyNames.push_back( + jsi::PropNameID::forUtf8(runtime, "createBiquadFilter")); + propertyNames.push_back( + jsi::PropNameID::forUtf8(runtime, "createBufferSource")); + propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "createBuffer")); + return propertyNames; + } + + jsi::Value BaseAudioContextHostObject::get( + jsi::Runtime &runtime, + const jsi::PropNameID &propNameId) { + auto propName = propNameId.utf8(runtime); + + if (propName == "destination") { + return jsi::Object::createFromHostObject(runtime, destination_); + } + + if (propName == "state") { + return jsi::String::createFromUtf8(runtime, wrapper_->getState()); + } + + if (propName == "sampleRate") { + return {wrapper_->getSampleRate()}; + } + + if (propName == "currentTime") { + return {wrapper_->getCurrentTime()}; + } + + if (propName == "createOscillator") { + return jsi::Function::createFromHostFunction( + runtime, + propNameId, + 0, + [this]( + jsi::Runtime &runtime, + const jsi::Value &thisValue, + const jsi::Value *arguments, + size_t count) -> jsi::Value { + auto oscillator = wrapper_->createOscillator(); + auto oscillatorHostObject = + OscillatorNodeHostObject::createFromWrapper(oscillator); + return jsi::Object::createFromHostObject( + runtime, oscillatorHostObject); + }); + } + + if (propName == "createGain") { + return jsi::Function::createFromHostFunction( + runtime, + propNameId, + 0, + [this]( + jsi::Runtime &runtime, + const jsi::Value &thisValue, + const jsi::Value *arguments, + size_t count) -> jsi::Value { + auto gain = wrapper_->createGain(); + auto gainHostObject = GainNodeHostObject::createFromWrapper(gain); + return jsi::Object::createFromHostObject(runtime, gainHostObject); + }); + } + + if (propName == "createStereoPanner") { + return jsi::Function::createFromHostFunction( + runtime, + propNameId, + 0, + [this]( + jsi::Runtime &runtime, + const jsi::Value &thisValue, + const jsi::Value *arguments, + size_t count) -> jsi::Value { + auto stereoPanner = wrapper_->createStereoPanner(); + auto stereoPannerHostObject = + StereoPannerNodeHostObject::createFromWrapper(stereoPanner); + return jsi::Object::createFromHostObject( + runtime, stereoPannerHostObject); + }); + } + + if (propName == "createBiquadFilter") { + return jsi::Function::createFromHostFunction( + runtime, + propNameId, + 0, + [this]( + jsi::Runtime &runtime, + const jsi::Value &thisValue, + const jsi::Value *arguments, + size_t count) -> jsi::Value { + auto biquadFilter = wrapper_->createBiquadFilter(); + auto biquadFilterHostObject = + BiquadFilterNodeHostObject::createFromWrapper(biquadFilter); + return jsi::Object::createFromHostObject( + runtime, biquadFilterHostObject); + }); + } + + if (propName == "createBufferSource") { + return jsi::Function::createFromHostFunction( + runtime, + propNameId, + 0, + [this]( + jsi::Runtime &runtime, + const jsi::Value &thisValue, + const jsi::Value *arguments, + size_t count) -> jsi::Value { + auto bufferSource = wrapper_->createBufferSource(); + auto bufferSourceHostObject = + AudioBufferSourceNodeHostObject::createFromWrapper(bufferSource); + return jsi::Object::createFromHostObject( + runtime, bufferSourceHostObject); + }); + } + + if (propName == "createBuffer") { + return jsi::Function::createFromHostFunction( + runtime, + propNameId, + 3, + [this]( + jsi::Runtime &runtime, + const jsi::Value &thisValue, + const jsi::Value *arguments, + size_t count) -> jsi::Value { + auto numberOfChannels = static_cast(arguments[0].getNumber()); + auto length = static_cast(arguments[1].getNumber()); + auto sampleRate = static_cast(arguments[2].getNumber()); + auto buffer = + wrapper_->createBuffer(numberOfChannels, length, sampleRate); + auto bufferHostObject = + AudioBufferHostObject::createFromWrapper(buffer); + return jsi::Object::createFromHostObject(runtime, bufferHostObject); + }); + } + + throw std::runtime_error("Not yet implemented!"); + } + + void BaseAudioContextHostObject::set( + jsi::Runtime &runtime, + const jsi::PropNameID &propNameId, + const jsi::Value &value) { + auto propName = propNameId.utf8(runtime); + + throw std::runtime_error("Not yet implemented!"); + } +} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/HostObjects/BaseAudioContextHostObject.h b/packages/react-native-audio-api/common/cpp/HostObjects/BaseAudioContextHostObject.h new file mode 100644 index 00000000..e83099c8 --- /dev/null +++ b/packages/react-native-audio-api/common/cpp/HostObjects/BaseAudioContextHostObject.h @@ -0,0 +1,38 @@ +#pragma once + +#include +#include +#include +#include + +#include "AudioBufferHostObject.h" +#include "AudioBufferSourceNodeHostObject.h" +#include "BaseAudioContextWrapper.h" +#include "AudioDestinationNodeHostObject.h" +#include "BiquadFilterNodeHostObject.h" +#include "GainNodeHostObject.h" +#include "OscillatorNodeHostObject.h" +#include "StereoPannerNodeHostObject.h" + +namespace audioapi { + using namespace facebook; + + class BaseAudioContextHostObject : public jsi::HostObject { + public: + explicit BaseAudioContextHostObject( + const std::shared_ptr &wrapper); + + jsi::Value get(jsi::Runtime &runtime, const jsi::PropNameID &name) override; + + void set( + jsi::Runtime &runtime, + const jsi::PropNameID &name, + const jsi::Value &value) override; + + std::vector getPropertyNames(jsi::Runtime &rt) override; + + protected: + std::shared_ptr wrapper_; + std::shared_ptr destination_; + }; +} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/core/BaseAudioContext.cpp b/packages/react-native-audio-api/common/cpp/core/BaseAudioContext.cpp new file mode 100644 index 00000000..42f76068 --- /dev/null +++ b/packages/react-native-audio-api/common/cpp/core/BaseAudioContext.cpp @@ -0,0 +1,79 @@ +#include "BaseAudioContext.h" + +namespace audioapi { + + BaseAudioContext::BaseAudioContext() { +#ifdef ANDROID + audioPlayer_ = std::make_shared(this->renderAudio()); +#else + audioPlayer_ = std::make_shared(this->renderAudio()); +#endif + destination_ = std::make_shared(this); + + sampleRate_ = audioPlayer_->getSampleRate(); + + auto now = std::chrono::high_resolution_clock ::now(); + contextStartTime_ = + static_cast(std::chrono::duration_cast( + now.time_since_epoch()) + .count()); + + audioPlayer_->start(); + } + + std::string BaseAudioContext::getState() { + return BaseAudioContext::toString(state_); + } + + int BaseAudioContext::getSampleRate() const { + return sampleRate_; + } + + double BaseAudioContext::getCurrentTime() const { + auto now = std::chrono::high_resolution_clock ::now(); + auto currentTime = + static_cast(std::chrono::duration_cast( + now.time_since_epoch()) + .count()); + return (currentTime - contextStartTime_) / 1e9; + } + + std::shared_ptr BaseAudioContext::getDestination() { + return destination_; + } + + std::shared_ptr BaseAudioContext::createOscillator() { + return std::make_shared(this); + } + + std::shared_ptr BaseAudioContext::createGain() { + return std::make_shared(this); + } + + std::shared_ptr BaseAudioContext::createStereoPanner() { + return std::make_shared(this); + } + + std::shared_ptr BaseAudioContext::createBiquadFilter() { + return std::make_shared(this); + } + + std::shared_ptr BaseAudioContext::createBufferSource() { + return std::make_shared(this); + } + + std::shared_ptr + BaseAudioContext::createBuffer(int numberOfChannels, int length, int sampleRate) { + return std::make_shared(numberOfChannels, length, sampleRate); + } + + std::function BaseAudioContext::renderAudio() { + if (state_ == State::CLOSED) { + return [](float *, int) {}; + } + + return [this](float *data, int frames) { + destination_->renderAudio(data, frames); + }; + } +} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/core/BaseAudioContext.h b/packages/react-native-audio-api/common/cpp/core/BaseAudioContext.h new file mode 100644 index 00000000..d7d55ee2 --- /dev/null +++ b/packages/react-native-audio-api/common/cpp/core/BaseAudioContext.h @@ -0,0 +1,73 @@ +#pragma once + +#include +#include +#include +#include +#include + +#include "AudioBuffer.h" +#include "AudioBufferSourceNode.h" +#include "AudioDestinationNode.h" +#include "AudioScheduledSourceNode.h" +#include "BiquadFilterNode.h" +#include "Constants.h" +#include "GainNode.h" +#include "OscillatorNode.h" +#include "StereoPannerNode.h" + +#ifdef ANDROID +#include "AudioPlayer.h" +#else +#include "IOSAudioPlayer.h" +#endif + +namespace audioapi { + + class BaseAudioContext { + public: + BaseAudioContext(); + std::string getState(); + int getSampleRate() const; + double getCurrentTime() const; + + std::shared_ptr getDestination(); + std::shared_ptr createOscillator(); + std::shared_ptr createGain(); + std::shared_ptr createStereoPanner(); + std::shared_ptr createBiquadFilter(); + std::shared_ptr createBufferSource(); + static std::shared_ptr + createBuffer(int numberOfChannels, int length, int sampleRate); + std::function renderAudio(); + + protected: + enum class State { SUSPENDED, RUNNING, CLOSED }; + + static std::string toString(State state) { + switch (state) { + case State::SUSPENDED: + return "suspended"; + case State::RUNNING: + return "running"; + case State::CLOSED: + return "closed"; + default: + throw std::invalid_argument("Unknown context state"); + } + } + + protected: + std::shared_ptr destination_; +#ifdef ANDROID + std::shared_ptr audioPlayer_; +#else + std::shared_ptr audioPlayer_; +#endif + State state_ = State::RUNNING; + int sampleRate_; + double contextStartTime_; + }; + +} // namespace audioapi + diff --git a/packages/react-native-audio-api/common/cpp/wrappers/BaseAudioContextWrapper.cpp b/packages/react-native-audio-api/common/cpp/wrappers/BaseAudioContextWrapper.cpp new file mode 100644 index 00000000..12fe1617 --- /dev/null +++ b/packages/react-native-audio-api/common/cpp/wrappers/BaseAudioContextWrapper.cpp @@ -0,0 +1,66 @@ +#include "BaseAudioContextWrapper.h" + +namespace audioapi { + + BaseAudioContextWrapper::BaseAudioContextWrapper( + const std::shared_ptr &context) + : context_(context) { + auto destination = context_->getDestination(); + destination_ = std::make_shared(destination); + } + + std::shared_ptr + BaseAudioContextWrapper::getDestination() const { + return destination_; + } + + std::shared_ptr BaseAudioContextWrapper::createOscillator() + const { + auto oscillator = context_->createOscillator(); + return std::make_shared(oscillator); + } + + std::shared_ptr BaseAudioContextWrapper::createGain() const { + auto gain = context_->createGain(); + return std::make_shared(gain); + } + + std::shared_ptr + BaseAudioContextWrapper::createStereoPanner() const { + auto panner = context_->createStereoPanner(); + return std::make_shared(panner); + } + + std::shared_ptr + BaseAudioContextWrapper::createBiquadFilter() const { + auto filter = context_->createBiquadFilter(); + return std::make_shared(filter); + } + + std::shared_ptr + BaseAudioContextWrapper::createBufferSource() const { + auto bufferSource = context_->createBufferSource(); + return std::make_shared(bufferSource); + } + + std::shared_ptr BaseAudioContextWrapper::createBuffer( + int numberOfChannels, + int length, + int sampleRate) const { + auto buffer = + context_->createBuffer(numberOfChannels, length, sampleRate); + return std::make_shared(buffer); + } + + std::string BaseAudioContextWrapper::getState() const { + return context_->getState(); + } + + int BaseAudioContextWrapper::getSampleRate() const { + return context_->getSampleRate(); + } + + double BaseAudioContextWrapper::getCurrentTime() const { + return context_->getCurrentTime(); + } +} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/wrappers/BaseAudioContextWrapper.h b/packages/react-native-audio-api/common/cpp/wrappers/BaseAudioContextWrapper.h new file mode 100644 index 00000000..7e893c9d --- /dev/null +++ b/packages/react-native-audio-api/common/cpp/wrappers/BaseAudioContextWrapper.h @@ -0,0 +1,39 @@ +#pragma once + +#include +#include +#include + +#include "AudioBufferSourceNodeWrapper.h" +#include "AudioBufferWrapper.h" +#include "BaseAudioContext.h" +#include "AudioDestinationNodeWrapper.h" +#include "BiquadFilterNodeWrapper.h" +#include "GainNodeWrapper.h" +#include "OscillatorNodeWrapper.h" +#include "StereoPannerNodeWrapper.h" + +namespace audioapi { + + class BaseAudioContextWrapper { + public: + explicit BaseAudioContextWrapper( + const std::shared_ptr &context); + + std::shared_ptr getDestination() const; + std::shared_ptr createOscillator() const; + std::shared_ptr createGain() const; + std::shared_ptr createStereoPanner() const; + std::shared_ptr createBiquadFilter() const; + std::shared_ptr createBufferSource() const; + std::shared_ptr + createBuffer(int numberOfChannels, int length, int sampleRate) const; + std::string getState() const; + int getSampleRate() const; + double getCurrentTime() const; + + protected: + std::shared_ptr destination_; + std::shared_ptr context_; + }; +} // namespace audioapi From 8e0ca82ec9c7c71ea6a9ef41083804da3912b09b Mon Sep 17 00:00:00 2001 From: Maciej Makowski Date: Thu, 24 Oct 2024 13:10:33 +0200 Subject: [PATCH 2/5] refactor: refactored AudioContext and other classes to use BaseAudioContext --- .../HostObjects/AudioContextHostObject.cpp | 156 ++---------------- .../cpp/HostObjects/AudioContextHostObject.h | 15 +- .../common/cpp/core/AudioBufferSourceNode.cpp | 4 +- .../common/cpp/core/AudioBufferSourceNode.h | 2 +- .../common/cpp/core/AudioContext.cpp | 75 +-------- .../common/cpp/core/AudioContext.h | 63 +------ .../common/cpp/core/AudioDestinationNode.cpp | 4 +- .../common/cpp/core/AudioDestinationNode.h | 2 +- .../common/cpp/core/AudioNode.cpp | 4 +- .../common/cpp/core/AudioNode.h | 6 +- .../common/cpp/core/AudioParam.cpp | 4 +- .../common/cpp/core/AudioParam.h | 6 +- .../cpp/core/AudioScheduledSourceNode.cpp | 4 +- .../cpp/core/AudioScheduledSourceNode.h | 2 +- .../common/cpp/core/BiquadFilterNode.cpp | 4 +- .../common/cpp/core/BiquadFilterNode.h | 2 +- .../common/cpp/core/GainNode.cpp | 4 +- .../common/cpp/core/GainNode.h | 2 +- .../common/cpp/core/OscillatorNode.cpp | 4 +- .../common/cpp/core/OscillatorNode.h | 2 +- .../common/cpp/core/StereoPannerNode.cpp | 4 +- .../common/cpp/core/StereoPannerNode.h | 2 +- .../cpp/wrappers/AudioContextWrapper.cpp | 66 +------- .../common/cpp/wrappers/AudioContextWrapper.h | 33 +--- 24 files changed, 59 insertions(+), 411 deletions(-) diff --git a/packages/react-native-audio-api/common/cpp/HostObjects/AudioContextHostObject.cpp b/packages/react-native-audio-api/common/cpp/HostObjects/AudioContextHostObject.cpp index 6e38b82d..24ac73c0 100644 --- a/packages/react-native-audio-api/common/cpp/HostObjects/AudioContextHostObject.cpp +++ b/packages/react-native-audio-api/common/cpp/HostObjects/AudioContextHostObject.cpp @@ -5,29 +5,12 @@ using namespace facebook; AudioContextHostObject::AudioContextHostObject( const std::shared_ptr &wrapper) - : wrapper_(wrapper) { - auto destinationNodeWrapper = wrapper_->getDestination(); - destination_ = - AudioDestinationNodeHostObject::createFromWrapper(destinationNodeWrapper); -} + : BaseAudioContextHostObject(wrapper) {} std::vector AudioContextHostObject::getPropertyNames( jsi::Runtime &runtime) { - std::vector propertyNames; - propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "destination")); - propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "state")); - propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "sampleRate")); - propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "currentTime")); - propertyNames.push_back( - jsi::PropNameID::forUtf8(runtime, "createOscillator")); - propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "createGain")); - propertyNames.push_back( - jsi::PropNameID::forUtf8(runtime, "createStereoPanner")); - propertyNames.push_back( - jsi::PropNameID::forUtf8(runtime, "createBiquadFilter")); - propertyNames.push_back( - jsi::PropNameID::forUtf8(runtime, "createBufferSource")); - propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "createBuffer")); + std::vector propertyNames = + BaseAudioContextHostObject::getPropertyNames(runtime); propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "close")); return propertyNames; } @@ -37,130 +20,7 @@ jsi::Value AudioContextHostObject::get( const jsi::PropNameID &propNameId) { auto propName = propNameId.utf8(runtime); - if (propName == "destination") { - return jsi::Object::createFromHostObject(runtime, destination_); - } - - if (propName == "state") { - return jsi::String::createFromUtf8(runtime, wrapper_->getState()); - } - - if (propName == "sampleRate") { - return {wrapper_->getSampleRate()}; - } - - if (propName == "currentTime") { - return {wrapper_->getCurrentTime()}; - } - - if (propName == "createOscillator") { - return jsi::Function::createFromHostFunction( - runtime, - propNameId, - 0, - [this]( - jsi::Runtime &runtime, - const jsi::Value &thisValue, - const jsi::Value *arguments, - size_t count) -> jsi::Value { - auto oscillator = wrapper_->createOscillator(); - auto oscillatorHostObject = - OscillatorNodeHostObject::createFromWrapper(oscillator); - return jsi::Object::createFromHostObject( - runtime, oscillatorHostObject); - }); - } - - if (propName == "createGain") { - return jsi::Function::createFromHostFunction( - runtime, - propNameId, - 0, - [this]( - jsi::Runtime &runtime, - const jsi::Value &thisValue, - const jsi::Value *arguments, - size_t count) -> jsi::Value { - auto gain = wrapper_->createGain(); - auto gainHostObject = GainNodeHostObject::createFromWrapper(gain); - return jsi::Object::createFromHostObject(runtime, gainHostObject); - }); - } - - if (propName == "createStereoPanner") { - return jsi::Function::createFromHostFunction( - runtime, - propNameId, - 0, - [this]( - jsi::Runtime &runtime, - const jsi::Value &thisValue, - const jsi::Value *arguments, - size_t count) -> jsi::Value { - auto stereoPanner = wrapper_->createStereoPanner(); - auto stereoPannerHostObject = - StereoPannerNodeHostObject::createFromWrapper(stereoPanner); - return jsi::Object::createFromHostObject( - runtime, stereoPannerHostObject); - }); - } - - if (propName == "createBiquadFilter") { - return jsi::Function::createFromHostFunction( - runtime, - propNameId, - 0, - [this]( - jsi::Runtime &runtime, - const jsi::Value &thisValue, - const jsi::Value *arguments, - size_t count) -> jsi::Value { - auto biquadFilter = wrapper_->createBiquadFilter(); - auto biquadFilterHostObject = - BiquadFilterNodeHostObject::createFromWrapper(biquadFilter); - return jsi::Object::createFromHostObject( - runtime, biquadFilterHostObject); - }); - } - - if (propName == "createBufferSource") { - return jsi::Function::createFromHostFunction( - runtime, - propNameId, - 0, - [this]( - jsi::Runtime &runtime, - const jsi::Value &thisValue, - const jsi::Value *arguments, - size_t count) -> jsi::Value { - auto bufferSource = wrapper_->createBufferSource(); - auto bufferSourceHostObject = - AudioBufferSourceNodeHostObject::createFromWrapper(bufferSource); - return jsi::Object::createFromHostObject( - runtime, bufferSourceHostObject); - }); - } - if (propName == "createBuffer") { - return jsi::Function::createFromHostFunction( - runtime, - propNameId, - 3, - [this]( - jsi::Runtime &runtime, - const jsi::Value &thisValue, - const jsi::Value *arguments, - size_t count) -> jsi::Value { - auto numberOfChannels = static_cast(arguments[0].getNumber()); - auto length = static_cast(arguments[1].getNumber()); - auto sampleRate = static_cast(arguments[2].getNumber()); - auto buffer = - wrapper_->createBuffer(numberOfChannels, length, sampleRate); - auto bufferHostObject = - AudioBufferHostObject::createFromWrapper(buffer); - return jsi::Object::createFromHostObject(runtime, bufferHostObject); - }); - } if (propName == "close") { return jsi::Function::createFromHostFunction( @@ -172,12 +32,12 @@ jsi::Value AudioContextHostObject::get( const jsi::Value &thisValue, const jsi::Value *arguments, size_t count) -> jsi::Value { - wrapper_->close(); + getAudioContextWrapperFromBaseAudioContextWrapper()->close(); return jsi::Value::undefined(); }); } - throw std::runtime_error("Not yet implemented!"); + return BaseAudioContextHostObject::get(runtime, propNameId); } void AudioContextHostObject::set( @@ -186,6 +46,10 @@ void AudioContextHostObject::set( const jsi::Value &value) { auto propName = propNameId.utf8(runtime); - throw std::runtime_error("Not yet implemented!"); + return BaseAudioContextHostObject::set(runtime, propNameId, value); +} + +std::shared_ptr AudioContextHostObject::getAudioContextWrapperFromBaseAudioContextWrapper() { + return std::static_pointer_cast(wrapper_); } } // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/HostObjects/AudioContextHostObject.h b/packages/react-native-audio-api/common/cpp/HostObjects/AudioContextHostObject.h index 82aeeaa0..92e56d39 100644 --- a/packages/react-native-audio-api/common/cpp/HostObjects/AudioContextHostObject.h +++ b/packages/react-native-audio-api/common/cpp/HostObjects/AudioContextHostObject.h @@ -2,22 +2,14 @@ #include #include -#include -#include -#include "AudioBufferHostObject.h" -#include "AudioBufferSourceNodeHostObject.h" +#include "BaseAudioContextHostObject.h" #include "AudioContextWrapper.h" -#include "AudioDestinationNodeHostObject.h" -#include "BiquadFilterNodeHostObject.h" -#include "GainNodeHostObject.h" -#include "OscillatorNodeHostObject.h" -#include "StereoPannerNodeHostObject.h" namespace audioapi { using namespace facebook; -class AudioContextHostObject : public jsi::HostObject { +class AudioContextHostObject : public BaseAudioContextHostObject { public: explicit AudioContextHostObject( const std::shared_ptr &wrapper); @@ -37,7 +29,6 @@ class AudioContextHostObject : public jsi::HostObject { } private: - std::shared_ptr wrapper_; - std::shared_ptr destination_; + std::shared_ptr getAudioContextWrapperFromBaseAudioContextWrapper(); }; } // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/core/AudioBufferSourceNode.cpp b/packages/react-native-audio-api/common/cpp/core/AudioBufferSourceNode.cpp index b29121d0..fea86797 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioBufferSourceNode.cpp +++ b/packages/react-native-audio-api/common/cpp/core/AudioBufferSourceNode.cpp @@ -1,9 +1,9 @@ #include "AudioBufferSourceNode.h" -#include "AudioContext.h" +#include "BaseAudioContext.h" namespace audioapi { -AudioBufferSourceNode::AudioBufferSourceNode(AudioContext *context) +AudioBufferSourceNode::AudioBufferSourceNode(BaseAudioContext *context) : AudioScheduledSourceNode(context), loop_(false), bufferIndex_(0) { numberOfInputs_ = 0; buffer_ = std::nullopt; diff --git a/packages/react-native-audio-api/common/cpp/core/AudioBufferSourceNode.h b/packages/react-native-audio-api/common/cpp/core/AudioBufferSourceNode.h index 6fb69fb9..7fe6197d 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioBufferSourceNode.h +++ b/packages/react-native-audio-api/common/cpp/core/AudioBufferSourceNode.h @@ -10,7 +10,7 @@ namespace audioapi { class AudioBufferSourceNode : public AudioScheduledSourceNode { public: - explicit AudioBufferSourceNode(AudioContext *context); + explicit AudioBufferSourceNode(BaseAudioContext *context); bool getLoop() const; std::shared_ptr getBuffer() const; diff --git a/packages/react-native-audio-api/common/cpp/core/AudioContext.cpp b/packages/react-native-audio-api/common/cpp/core/AudioContext.cpp index 1eef3f3d..9c92567d 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioContext.cpp +++ b/packages/react-native-audio-api/common/cpp/core/AudioContext.cpp @@ -2,41 +2,7 @@ namespace audioapi { -AudioContext::AudioContext() { -#ifdef ANDROID - audioPlayer_ = std::make_shared(this->renderAudio()); -#else - audioPlayer_ = std::make_shared(this->renderAudio()); -#endif - destination_ = std::make_shared(this); - - sampleRate_ = audioPlayer_->getSampleRate(); - - auto now = std::chrono::high_resolution_clock ::now(); - contextStartTime_ = - static_cast(std::chrono::duration_cast( - now.time_since_epoch()) - .count()); - - audioPlayer_->start(); -} - -std::string AudioContext::getState() { - return AudioContext::toString(state_); -} - -int AudioContext::getSampleRate() const { - return sampleRate_; -} - -double AudioContext::getCurrentTime() const { - auto now = std::chrono::high_resolution_clock ::now(); - auto currentTime = - static_cast(std::chrono::duration_cast( - now.time_since_epoch()) - .count()); - return (currentTime - contextStartTime_) / 1e9; -} +AudioContext::AudioContext() : BaseAudioContext() {} void AudioContext::close() { state_ = State::CLOSED; @@ -48,43 +14,4 @@ void AudioContext::close() { audioPlayer_.reset(); destination_.reset(); } - -std::shared_ptr AudioContext::getDestination() { - return destination_; -} - -std::shared_ptr AudioContext::createOscillator() { - return std::make_shared(this); -} - -std::shared_ptr AudioContext::createGain() { - return std::make_shared(this); -} - -std::shared_ptr AudioContext::createStereoPanner() { - return std::make_shared(this); -} - -std::shared_ptr AudioContext::createBiquadFilter() { - return std::make_shared(this); -} - -std::shared_ptr AudioContext::createBufferSource() { - return std::make_shared(this); -} - -std::shared_ptr -AudioContext::createBuffer(int numberOfChannels, int length, int sampleRate) { - return std::make_shared(numberOfChannels, length, sampleRate); -} - -std::function AudioContext::renderAudio() { - if (state_ == State::CLOSED) { - return [](float *, int) {}; - } - - return [this](float *data, int frames) { - destination_->renderAudio(data, frames); - }; -} } // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/core/AudioContext.h b/packages/react-native-audio-api/common/cpp/core/AudioContext.h index e0079353..a9cc0b47 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioContext.h +++ b/packages/react-native-audio-api/common/cpp/core/AudioContext.h @@ -1,73 +1,16 @@ #pragma once -#include #include -#include -#include -#include -#include "AudioBuffer.h" -#include "AudioBufferSourceNode.h" -#include "AudioDestinationNode.h" -#include "AudioScheduledSourceNode.h" -#include "BiquadFilterNode.h" -#include "Constants.h" -#include "GainNode.h" -#include "OscillatorNode.h" -#include "StereoPannerNode.h" - -#ifdef ANDROID -#include "AudioPlayer.h" -#else -#include "IOSAudioPlayer.h" -#endif +#include "BaseAudioContext.h" namespace audioapi { -class AudioContext { +class AudioContext : public BaseAudioContext { public: AudioContext(); - std::string getState(); - int getSampleRate() const; - double getCurrentTime() const; - void close(); - - std::shared_ptr getDestination(); - std::shared_ptr createOscillator(); - std::shared_ptr createGain(); - std::shared_ptr createStereoPanner(); - std::shared_ptr createBiquadFilter(); - std::shared_ptr createBufferSource(); - static std::shared_ptr - createBuffer(int numberOfChannels, int length, int sampleRate); - std::function renderAudio(); - private: - enum class State { SUSPENDED, RUNNING, CLOSED }; - - static std::string toString(State state) { - switch (state) { - case State::SUSPENDED: - return "suspended"; - case State::RUNNING: - return "running"; - case State::CLOSED: - return "closed"; - default: - throw std::invalid_argument("Unknown context state"); - } - } - - private: - std::shared_ptr destination_; -#ifdef ANDROID - std::shared_ptr audioPlayer_; -#else - std::shared_ptr audioPlayer_; -#endif - State state_ = State::RUNNING; - int sampleRate_; - double contextStartTime_; + void close(); }; } // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/core/AudioDestinationNode.cpp b/packages/react-native-audio-api/common/cpp/core/AudioDestinationNode.cpp index 065dcd5d..8e55e926 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioDestinationNode.cpp +++ b/packages/react-native-audio-api/common/cpp/core/AudioDestinationNode.cpp @@ -1,9 +1,9 @@ #include "AudioDestinationNode.h" -#include "AudioContext.h" +#include "BaseAudioContext.h" namespace audioapi { -AudioDestinationNode::AudioDestinationNode(AudioContext *context) +AudioDestinationNode::AudioDestinationNode(BaseAudioContext *context) : AudioNode(context) { numberOfOutputs_ = 0; numberOfInputs_ = INT_MAX; diff --git a/packages/react-native-audio-api/common/cpp/core/AudioDestinationNode.h b/packages/react-native-audio-api/common/cpp/core/AudioDestinationNode.h index 3124e704..7767fd7b 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioDestinationNode.h +++ b/packages/react-native-audio-api/common/cpp/core/AudioDestinationNode.h @@ -11,7 +11,7 @@ namespace audioapi { class AudioDestinationNode : public AudioNode { public: - explicit AudioDestinationNode(AudioContext *context); + explicit AudioDestinationNode(BaseAudioContext *context); void renderAudio(float *audioData, int32_t numFrames); diff --git a/packages/react-native-audio-api/common/cpp/core/AudioNode.cpp b/packages/react-native-audio-api/common/cpp/core/AudioNode.cpp index 94b5bfc4..c7fff825 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioNode.cpp +++ b/packages/react-native-audio-api/common/cpp/core/AudioNode.cpp @@ -1,9 +1,9 @@ #include "AudioNode.h" -#include "AudioContext.h" +#include "BaseAudioContext.h" namespace audioapi { -AudioNode::AudioNode(AudioContext *context) : context_(context) {} +AudioNode::AudioNode(BaseAudioContext *context) : context_(context) {} AudioNode::~AudioNode() { cleanup(); diff --git a/packages/react-native-audio-api/common/cpp/core/AudioNode.h b/packages/react-native-audio-api/common/cpp/core/AudioNode.h index 3ca9ccc9..071d4f67 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioNode.h +++ b/packages/react-native-audio-api/common/cpp/core/AudioNode.h @@ -9,11 +9,11 @@ namespace audioapi { -class AudioContext; +class BaseAudioContext; class AudioNode : public std::enable_shared_from_this { public: - explicit AudioNode(AudioContext *context); + explicit AudioNode(BaseAudioContext *context); virtual ~AudioNode(); int getNumberOfInputs() const; int getNumberOfOutputs() const; @@ -56,7 +56,7 @@ class AudioNode : public std::enable_shared_from_this { } protected: - AudioContext *context_; + BaseAudioContext *context_; int numberOfInputs_ = 1; int numberOfOutputs_ = 1; int channelCount_ = CHANNEL_COUNT; diff --git a/packages/react-native-audio-api/common/cpp/core/AudioParam.cpp b/packages/react-native-audio-api/common/cpp/core/AudioParam.cpp index 336293e3..97cd628f 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioParam.cpp +++ b/packages/react-native-audio-api/common/cpp/core/AudioParam.cpp @@ -1,10 +1,10 @@ #include "AudioParam.h" -#include "AudioContext.h" +#include "BaseAudioContext.h" namespace audioapi { AudioParam::AudioParam( - AudioContext *context, + BaseAudioContext *context, float defaultValue, float minValue, float maxValue) diff --git a/packages/react-native-audio-api/common/cpp/core/AudioParam.h b/packages/react-native-audio-api/common/cpp/core/AudioParam.h index 3ae30dd7..2adab364 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioParam.h +++ b/packages/react-native-audio-api/common/cpp/core/AudioParam.h @@ -8,12 +8,12 @@ namespace audioapi { -class AudioContext; +class BaseAudioContext; class AudioParam { public: explicit AudioParam( - AudioContext *context, + BaseAudioContext *context, float defaultValue, float minValue, float maxValue); @@ -33,7 +33,7 @@ class AudioParam { float defaultValue_; float minValue_; float maxValue_; - AudioContext *context_; + BaseAudioContext *context_; std::set changesQueue_; double startTime_; diff --git a/packages/react-native-audio-api/common/cpp/core/AudioScheduledSourceNode.cpp b/packages/react-native-audio-api/common/cpp/core/AudioScheduledSourceNode.cpp index 75345f1e..741e09c1 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioScheduledSourceNode.cpp +++ b/packages/react-native-audio-api/common/cpp/core/AudioScheduledSourceNode.cpp @@ -1,9 +1,9 @@ #include "AudioScheduledSourceNode.h" -#include "AudioContext.h" +#include "BaseAudioContext.h" namespace audioapi { -AudioScheduledSourceNode::AudioScheduledSourceNode(AudioContext *context) +AudioScheduledSourceNode::AudioScheduledSourceNode(BaseAudioContext *context) : AudioNode(context), isPlaying_(false) { numberOfInputs_ = 0; } diff --git a/packages/react-native-audio-api/common/cpp/core/AudioScheduledSourceNode.h b/packages/react-native-audio-api/common/cpp/core/AudioScheduledSourceNode.h index 768ffc4c..85691aef 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioScheduledSourceNode.h +++ b/packages/react-native-audio-api/common/cpp/core/AudioScheduledSourceNode.h @@ -13,7 +13,7 @@ namespace audioapi { class AudioScheduledSourceNode : public AudioNode { public: - explicit AudioScheduledSourceNode(AudioContext *context); + explicit AudioScheduledSourceNode(BaseAudioContext *context); void start(double time); void stop(double time); diff --git a/packages/react-native-audio-api/common/cpp/core/BiquadFilterNode.cpp b/packages/react-native-audio-api/common/cpp/core/BiquadFilterNode.cpp index cbe5ed7c..fcfd5e98 100644 --- a/packages/react-native-audio-api/common/cpp/core/BiquadFilterNode.cpp +++ b/packages/react-native-audio-api/common/cpp/core/BiquadFilterNode.cpp @@ -1,5 +1,5 @@ #include "BiquadFilterNode.h" -#include "AudioContext.h" +#include "BaseAudioContext.h" // https://webaudio.github.io/Audio-EQ-Cookbook/audio-eq-cookbook.html - math // formulas for filters @@ -8,7 +8,7 @@ namespace audioapi { -BiquadFilterNode::BiquadFilterNode(AudioContext *context) : AudioNode(context) { +BiquadFilterNode::BiquadFilterNode(BaseAudioContext *context) : AudioNode(context) { frequencyParam_ = std::make_shared( context, 350.0, MIN_FILTER_FREQUENCY, MAX_FILTER_FREQUENCY); detuneParam_ = diff --git a/packages/react-native-audio-api/common/cpp/core/BiquadFilterNode.h b/packages/react-native-audio-api/common/cpp/core/BiquadFilterNode.h index 8984ef36..1d3a9d87 100644 --- a/packages/react-native-audio-api/common/cpp/core/BiquadFilterNode.h +++ b/packages/react-native-audio-api/common/cpp/core/BiquadFilterNode.h @@ -13,7 +13,7 @@ namespace audioapi { class BiquadFilterNode : public AudioNode { public: - explicit BiquadFilterNode(AudioContext *context); + explicit BiquadFilterNode(BaseAudioContext *context); std::string getType() const; void setType(const std::string &type); diff --git a/packages/react-native-audio-api/common/cpp/core/GainNode.cpp b/packages/react-native-audio-api/common/cpp/core/GainNode.cpp index 948d0368..2435b385 100644 --- a/packages/react-native-audio-api/common/cpp/core/GainNode.cpp +++ b/packages/react-native-audio-api/common/cpp/core/GainNode.cpp @@ -1,9 +1,9 @@ #include "GainNode.h" -#include "AudioContext.h" +#include "BaseAudioContext.h" namespace audioapi { -GainNode::GainNode(AudioContext *context) : AudioNode(context) { +GainNode::GainNode(BaseAudioContext *context) : AudioNode(context) { gainParam_ = std::make_shared(context, 1.0, -MAX_GAIN, MAX_GAIN); } diff --git a/packages/react-native-audio-api/common/cpp/core/GainNode.h b/packages/react-native-audio-api/common/cpp/core/GainNode.h index 1f44c16c..87130d7f 100644 --- a/packages/react-native-audio-api/common/cpp/core/GainNode.h +++ b/packages/react-native-audio-api/common/cpp/core/GainNode.h @@ -9,7 +9,7 @@ namespace audioapi { class GainNode : public AudioNode { public: - explicit GainNode(AudioContext *context); + explicit GainNode(BaseAudioContext *context); std::shared_ptr getGainParam() const; diff --git a/packages/react-native-audio-api/common/cpp/core/OscillatorNode.cpp b/packages/react-native-audio-api/common/cpp/core/OscillatorNode.cpp index 9e5d2c50..b8e6806c 100644 --- a/packages/react-native-audio-api/common/cpp/core/OscillatorNode.cpp +++ b/packages/react-native-audio-api/common/cpp/core/OscillatorNode.cpp @@ -1,9 +1,9 @@ #include "OscillatorNode.h" -#include "AudioContext.h" +#include "BaseAudioContext.h" namespace audioapi { -OscillatorNode::OscillatorNode(AudioContext *context) +OscillatorNode::OscillatorNode(BaseAudioContext *context) : AudioScheduledSourceNode(context) { frequencyParam_ = std::make_shared( context, 444.0, -NYQUIST_FREQUENCY, NYQUIST_FREQUENCY); diff --git a/packages/react-native-audio-api/common/cpp/core/OscillatorNode.h b/packages/react-native-audio-api/common/cpp/core/OscillatorNode.h index ab94488f..a0ddce4b 100644 --- a/packages/react-native-audio-api/common/cpp/core/OscillatorNode.h +++ b/packages/react-native-audio-api/common/cpp/core/OscillatorNode.h @@ -11,7 +11,7 @@ namespace audioapi { class OscillatorNode : public AudioScheduledSourceNode { public: - explicit OscillatorNode(AudioContext *context); + explicit OscillatorNode(BaseAudioContext *context); std::shared_ptr getFrequencyParam() const; std::shared_ptr getDetuneParam() const; diff --git a/packages/react-native-audio-api/common/cpp/core/StereoPannerNode.cpp b/packages/react-native-audio-api/common/cpp/core/StereoPannerNode.cpp index b02c2b3d..5512eef4 100644 --- a/packages/react-native-audio-api/common/cpp/core/StereoPannerNode.cpp +++ b/packages/react-native-audio-api/common/cpp/core/StereoPannerNode.cpp @@ -1,11 +1,11 @@ #include "StereoPannerNode.h" -#include "AudioContext.h" +#include "BaseAudioContext.h" // https://webaudio.github.io/web-audio-api/#stereopanner-algorithm namespace audioapi { -StereoPannerNode::StereoPannerNode(AudioContext *context) : AudioNode(context) { +StereoPannerNode::StereoPannerNode(BaseAudioContext *context) : AudioNode(context) { channelCountMode_ = ChannelCountMode::CLAMPED_MAX; panParam_ = std::make_shared(context, 0.0, -MAX_PAN, MAX_PAN); } diff --git a/packages/react-native-audio-api/common/cpp/core/StereoPannerNode.h b/packages/react-native-audio-api/common/cpp/core/StereoPannerNode.h index f3e8e6d3..6c2b1bd2 100644 --- a/packages/react-native-audio-api/common/cpp/core/StereoPannerNode.h +++ b/packages/react-native-audio-api/common/cpp/core/StereoPannerNode.h @@ -11,7 +11,7 @@ namespace audioapi { class StereoPannerNode : public AudioNode { public: - explicit StereoPannerNode(AudioContext *context); + explicit StereoPannerNode(BaseAudioContext *context); std::shared_ptr getPanParam() const; protected: diff --git a/packages/react-native-audio-api/common/cpp/wrappers/AudioContextWrapper.cpp b/packages/react-native-audio-api/common/cpp/wrappers/AudioContextWrapper.cpp index e08c5871..8aa999c5 100644 --- a/packages/react-native-audio-api/common/cpp/wrappers/AudioContextWrapper.cpp +++ b/packages/react-native-audio-api/common/cpp/wrappers/AudioContextWrapper.cpp @@ -2,69 +2,13 @@ namespace audioapi { -AudioContextWrapper::AudioContextWrapper( - const std::shared_ptr &audiocontext) - : audioContext_(audiocontext) { - auto destination = audioContext_->getDestination(); - destination_ = std::make_shared(destination); -} - -std::shared_ptr -AudioContextWrapper::getDestination() const { - return destination_; -} - -std::shared_ptr AudioContextWrapper::createOscillator() - const { - auto oscillator = audioContext_->createOscillator(); - return std::make_shared(oscillator); -} - -std::shared_ptr AudioContextWrapper::createGain() const { - auto gain = audioContext_->createGain(); - return std::make_shared(gain); -} - -std::shared_ptr -AudioContextWrapper::createStereoPanner() const { - auto panner = audioContext_->createStereoPanner(); - return std::make_shared(panner); -} - -std::shared_ptr -AudioContextWrapper::createBiquadFilter() const { - auto filter = audioContext_->createBiquadFilter(); - return std::make_shared(filter); -} - -std::shared_ptr -AudioContextWrapper::createBufferSource() const { - auto bufferSource = audioContext_->createBufferSource(); - return std::make_shared(bufferSource); -} - -std::shared_ptr AudioContextWrapper::createBuffer( - int numberOfChannels, - int length, - int sampleRate) const { - auto buffer = - audioContext_->createBuffer(numberOfChannels, length, sampleRate); - return std::make_shared(buffer); -} - -std::string AudioContextWrapper::getState() const { - return audioContext_->getState(); -} - -int AudioContextWrapper::getSampleRate() const { - return audioContext_->getSampleRate(); -} +AudioContextWrapper::AudioContextWrapper(const std::shared_ptr &context) : BaseAudioContextWrapper(context) {} -double AudioContextWrapper::getCurrentTime() const { - return audioContext_->getCurrentTime(); +void AudioContextWrapper::close() { + getAudioContextFromBaseAudioContext()->close(); } -void AudioContextWrapper::close() const { - audioContext_->close(); +std::shared_ptr AudioContextWrapper::getAudioContextFromBaseAudioContext() { + return std::static_pointer_cast(context_); } } // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/wrappers/AudioContextWrapper.h b/packages/react-native-audio-api/common/cpp/wrappers/AudioContextWrapper.h index 6f62fffb..108b7c39 100644 --- a/packages/react-native-audio-api/common/cpp/wrappers/AudioContextWrapper.h +++ b/packages/react-native-audio-api/common/cpp/wrappers/AudioContextWrapper.h @@ -1,40 +1,19 @@ #pragma once #include -#include -#include -#include "AudioBufferSourceNodeWrapper.h" -#include "AudioBufferWrapper.h" +#include "BaseAudioContextWrapper.h" #include "AudioContext.h" -#include "AudioDestinationNodeWrapper.h" -#include "BiquadFilterNodeWrapper.h" -#include "GainNodeWrapper.h" -#include "OscillatorNodeWrapper.h" -#include "StereoPannerNodeWrapper.h" namespace audioapi { -class AudioContextWrapper { +class AudioContextWrapper : public BaseAudioContextWrapper { public: - explicit AudioContextWrapper( - const std::shared_ptr &audiocontext); + explicit AudioContextWrapper(const std::shared_ptr &context); - std::shared_ptr getDestination() const; - std::shared_ptr createOscillator() const; - std::shared_ptr createGain() const; - std::shared_ptr createStereoPanner() const; - std::shared_ptr createBiquadFilter() const; - std::shared_ptr createBufferSource() const; - std::shared_ptr - createBuffer(int numberOfChannels, int length, int sampleRate) const; - std::string getState() const; - int getSampleRate() const; - double getCurrentTime() const; - void close() const; + void close(); - private: - std::shared_ptr destination_; - std::shared_ptr audioContext_; +private: + std::shared_ptr getAudioContextFromBaseAudioContext(); }; } // namespace audioapi From 41bdb5c4128200dea9856f1796eeb854bf084700 Mon Sep 17 00:00:00 2001 From: Maciej Makowski Date: Thu, 24 Oct 2024 13:11:02 +0200 Subject: [PATCH 3/5] ci: yarn format --- .../HostObjects/AudioContextHostObject.cpp | 9 +- .../cpp/HostObjects/AudioContextHostObject.h | 5 +- .../BaseAudioContextHostObject.cpp | 338 +++++++++--------- .../HostObjects/BaseAudioContextHostObject.h | 32 +- .../common/cpp/core/BaseAudioContext.cpp | 144 ++++---- .../common/cpp/core/BaseAudioContext.h | 75 ++-- .../common/cpp/core/BiquadFilterNode.cpp | 3 +- .../common/cpp/core/StereoPannerNode.cpp | 3 +- .../cpp/wrappers/AudioContextWrapper.cpp | 7 +- .../common/cpp/wrappers/AudioContextWrapper.h | 6 +- .../cpp/wrappers/BaseAudioContextWrapper.cpp | 101 +++--- .../cpp/wrappers/BaseAudioContextWrapper.h | 40 +-- 12 files changed, 384 insertions(+), 379 deletions(-) diff --git a/packages/react-native-audio-api/common/cpp/HostObjects/AudioContextHostObject.cpp b/packages/react-native-audio-api/common/cpp/HostObjects/AudioContextHostObject.cpp index 24ac73c0..0a2cdb51 100644 --- a/packages/react-native-audio-api/common/cpp/HostObjects/AudioContextHostObject.cpp +++ b/packages/react-native-audio-api/common/cpp/HostObjects/AudioContextHostObject.cpp @@ -9,8 +9,8 @@ AudioContextHostObject::AudioContextHostObject( std::vector AudioContextHostObject::getPropertyNames( jsi::Runtime &runtime) { - std::vector propertyNames = - BaseAudioContextHostObject::getPropertyNames(runtime); + std::vector propertyNames = + BaseAudioContextHostObject::getPropertyNames(runtime); propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "close")); return propertyNames; } @@ -20,8 +20,6 @@ jsi::Value AudioContextHostObject::get( const jsi::PropNameID &propNameId) { auto propName = propNameId.utf8(runtime); - - if (propName == "close") { return jsi::Function::createFromHostFunction( runtime, @@ -49,7 +47,8 @@ void AudioContextHostObject::set( return BaseAudioContextHostObject::set(runtime, propNameId, value); } -std::shared_ptr AudioContextHostObject::getAudioContextWrapperFromBaseAudioContextWrapper() { +std::shared_ptr +AudioContextHostObject::getAudioContextWrapperFromBaseAudioContextWrapper() { return std::static_pointer_cast(wrapper_); } } // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/HostObjects/AudioContextHostObject.h b/packages/react-native-audio-api/common/cpp/HostObjects/AudioContextHostObject.h index 92e56d39..81d2899c 100644 --- a/packages/react-native-audio-api/common/cpp/HostObjects/AudioContextHostObject.h +++ b/packages/react-native-audio-api/common/cpp/HostObjects/AudioContextHostObject.h @@ -3,8 +3,8 @@ #include #include -#include "BaseAudioContextHostObject.h" #include "AudioContextWrapper.h" +#include "BaseAudioContextHostObject.h" namespace audioapi { using namespace facebook; @@ -29,6 +29,7 @@ class AudioContextHostObject : public BaseAudioContextHostObject { } private: - std::shared_ptr getAudioContextWrapperFromBaseAudioContextWrapper(); + std::shared_ptr + getAudioContextWrapperFromBaseAudioContextWrapper(); }; } // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/HostObjects/BaseAudioContextHostObject.cpp b/packages/react-native-audio-api/common/cpp/HostObjects/BaseAudioContextHostObject.cpp index f983f4ba..128b47f9 100644 --- a/packages/react-native-audio-api/common/cpp/HostObjects/BaseAudioContextHostObject.cpp +++ b/packages/react-native-audio-api/common/cpp/HostObjects/BaseAudioContextHostObject.cpp @@ -1,175 +1,175 @@ #include "BaseAudioContextHostObject.h" namespace audioapi { - using namespace facebook; - - BaseAudioContextHostObject::BaseAudioContextHostObject( - const std::shared_ptr &wrapper) - : wrapper_(wrapper) { - auto destinationNodeWrapper = wrapper_->getDestination(); - destination_ = - AudioDestinationNodeHostObject::createFromWrapper(destinationNodeWrapper); - } - - std::vector BaseAudioContextHostObject::getPropertyNames( - jsi::Runtime &runtime) { - std::vector propertyNames; - propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "destination")); - propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "state")); - propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "sampleRate")); - propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "currentTime")); - propertyNames.push_back( - jsi::PropNameID::forUtf8(runtime, "createOscillator")); - propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "createGain")); - propertyNames.push_back( - jsi::PropNameID::forUtf8(runtime, "createStereoPanner")); - propertyNames.push_back( - jsi::PropNameID::forUtf8(runtime, "createBiquadFilter")); - propertyNames.push_back( - jsi::PropNameID::forUtf8(runtime, "createBufferSource")); - propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "createBuffer")); - return propertyNames; - } - - jsi::Value BaseAudioContextHostObject::get( +using namespace facebook; + +BaseAudioContextHostObject::BaseAudioContextHostObject( + const std::shared_ptr &wrapper) + : wrapper_(wrapper) { + auto destinationNodeWrapper = wrapper_->getDestination(); + destination_ = + AudioDestinationNodeHostObject::createFromWrapper(destinationNodeWrapper); +} + +std::vector BaseAudioContextHostObject::getPropertyNames( + jsi::Runtime &runtime) { + std::vector propertyNames; + propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "destination")); + propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "state")); + propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "sampleRate")); + propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "currentTime")); + propertyNames.push_back( + jsi::PropNameID::forUtf8(runtime, "createOscillator")); + propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "createGain")); + propertyNames.push_back( + jsi::PropNameID::forUtf8(runtime, "createStereoPanner")); + propertyNames.push_back( + jsi::PropNameID::forUtf8(runtime, "createBiquadFilter")); + propertyNames.push_back( + jsi::PropNameID::forUtf8(runtime, "createBufferSource")); + propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "createBuffer")); + return propertyNames; +} + +jsi::Value BaseAudioContextHostObject::get( + jsi::Runtime &runtime, + const jsi::PropNameID &propNameId) { + auto propName = propNameId.utf8(runtime); + + if (propName == "destination") { + return jsi::Object::createFromHostObject(runtime, destination_); + } + + if (propName == "state") { + return jsi::String::createFromUtf8(runtime, wrapper_->getState()); + } + + if (propName == "sampleRate") { + return {wrapper_->getSampleRate()}; + } + + if (propName == "currentTime") { + return {wrapper_->getCurrentTime()}; + } + + if (propName == "createOscillator") { + return jsi::Function::createFromHostFunction( + runtime, + propNameId, + 0, + [this]( jsi::Runtime &runtime, - const jsi::PropNameID &propNameId) { - auto propName = propNameId.utf8(runtime); - - if (propName == "destination") { - return jsi::Object::createFromHostObject(runtime, destination_); - } - - if (propName == "state") { - return jsi::String::createFromUtf8(runtime, wrapper_->getState()); - } - - if (propName == "sampleRate") { - return {wrapper_->getSampleRate()}; - } - - if (propName == "currentTime") { - return {wrapper_->getCurrentTime()}; - } - - if (propName == "createOscillator") { - return jsi::Function::createFromHostFunction( - runtime, - propNameId, - 0, - [this]( - jsi::Runtime &runtime, - const jsi::Value &thisValue, - const jsi::Value *arguments, - size_t count) -> jsi::Value { - auto oscillator = wrapper_->createOscillator(); - auto oscillatorHostObject = - OscillatorNodeHostObject::createFromWrapper(oscillator); - return jsi::Object::createFromHostObject( - runtime, oscillatorHostObject); - }); - } - - if (propName == "createGain") { - return jsi::Function::createFromHostFunction( - runtime, - propNameId, - 0, - [this]( - jsi::Runtime &runtime, - const jsi::Value &thisValue, - const jsi::Value *arguments, - size_t count) -> jsi::Value { - auto gain = wrapper_->createGain(); - auto gainHostObject = GainNodeHostObject::createFromWrapper(gain); - return jsi::Object::createFromHostObject(runtime, gainHostObject); - }); - } - - if (propName == "createStereoPanner") { - return jsi::Function::createFromHostFunction( - runtime, - propNameId, - 0, - [this]( - jsi::Runtime &runtime, - const jsi::Value &thisValue, - const jsi::Value *arguments, - size_t count) -> jsi::Value { - auto stereoPanner = wrapper_->createStereoPanner(); - auto stereoPannerHostObject = - StereoPannerNodeHostObject::createFromWrapper(stereoPanner); - return jsi::Object::createFromHostObject( - runtime, stereoPannerHostObject); - }); - } - - if (propName == "createBiquadFilter") { - return jsi::Function::createFromHostFunction( - runtime, - propNameId, - 0, - [this]( - jsi::Runtime &runtime, - const jsi::Value &thisValue, - const jsi::Value *arguments, - size_t count) -> jsi::Value { - auto biquadFilter = wrapper_->createBiquadFilter(); - auto biquadFilterHostObject = - BiquadFilterNodeHostObject::createFromWrapper(biquadFilter); - return jsi::Object::createFromHostObject( - runtime, biquadFilterHostObject); - }); - } - - if (propName == "createBufferSource") { - return jsi::Function::createFromHostFunction( - runtime, - propNameId, - 0, - [this]( - jsi::Runtime &runtime, - const jsi::Value &thisValue, - const jsi::Value *arguments, - size_t count) -> jsi::Value { - auto bufferSource = wrapper_->createBufferSource(); - auto bufferSourceHostObject = - AudioBufferSourceNodeHostObject::createFromWrapper(bufferSource); - return jsi::Object::createFromHostObject( - runtime, bufferSourceHostObject); - }); - } - - if (propName == "createBuffer") { - return jsi::Function::createFromHostFunction( - runtime, - propNameId, - 3, - [this]( - jsi::Runtime &runtime, - const jsi::Value &thisValue, - const jsi::Value *arguments, - size_t count) -> jsi::Value { - auto numberOfChannels = static_cast(arguments[0].getNumber()); - auto length = static_cast(arguments[1].getNumber()); - auto sampleRate = static_cast(arguments[2].getNumber()); - auto buffer = - wrapper_->createBuffer(numberOfChannels, length, sampleRate); - auto bufferHostObject = - AudioBufferHostObject::createFromWrapper(buffer); - return jsi::Object::createFromHostObject(runtime, bufferHostObject); - }); - } - - throw std::runtime_error("Not yet implemented!"); - } - - void BaseAudioContextHostObject::set( + const jsi::Value &thisValue, + const jsi::Value *arguments, + size_t count) -> jsi::Value { + auto oscillator = wrapper_->createOscillator(); + auto oscillatorHostObject = + OscillatorNodeHostObject::createFromWrapper(oscillator); + return jsi::Object::createFromHostObject( + runtime, oscillatorHostObject); + }); + } + + if (propName == "createGain") { + return jsi::Function::createFromHostFunction( + runtime, + propNameId, + 0, + [this]( jsi::Runtime &runtime, - const jsi::PropNameID &propNameId, - const jsi::Value &value) { - auto propName = propNameId.utf8(runtime); - - throw std::runtime_error("Not yet implemented!"); - } + const jsi::Value &thisValue, + const jsi::Value *arguments, + size_t count) -> jsi::Value { + auto gain = wrapper_->createGain(); + auto gainHostObject = GainNodeHostObject::createFromWrapper(gain); + return jsi::Object::createFromHostObject(runtime, gainHostObject); + }); + } + + if (propName == "createStereoPanner") { + return jsi::Function::createFromHostFunction( + runtime, + propNameId, + 0, + [this]( + jsi::Runtime &runtime, + const jsi::Value &thisValue, + const jsi::Value *arguments, + size_t count) -> jsi::Value { + auto stereoPanner = wrapper_->createStereoPanner(); + auto stereoPannerHostObject = + StereoPannerNodeHostObject::createFromWrapper(stereoPanner); + return jsi::Object::createFromHostObject( + runtime, stereoPannerHostObject); + }); + } + + if (propName == "createBiquadFilter") { + return jsi::Function::createFromHostFunction( + runtime, + propNameId, + 0, + [this]( + jsi::Runtime &runtime, + const jsi::Value &thisValue, + const jsi::Value *arguments, + size_t count) -> jsi::Value { + auto biquadFilter = wrapper_->createBiquadFilter(); + auto biquadFilterHostObject = + BiquadFilterNodeHostObject::createFromWrapper(biquadFilter); + return jsi::Object::createFromHostObject( + runtime, biquadFilterHostObject); + }); + } + + if (propName == "createBufferSource") { + return jsi::Function::createFromHostFunction( + runtime, + propNameId, + 0, + [this]( + jsi::Runtime &runtime, + const jsi::Value &thisValue, + const jsi::Value *arguments, + size_t count) -> jsi::Value { + auto bufferSource = wrapper_->createBufferSource(); + auto bufferSourceHostObject = + AudioBufferSourceNodeHostObject::createFromWrapper(bufferSource); + return jsi::Object::createFromHostObject( + runtime, bufferSourceHostObject); + }); + } + + if (propName == "createBuffer") { + return jsi::Function::createFromHostFunction( + runtime, + propNameId, + 3, + [this]( + jsi::Runtime &runtime, + const jsi::Value &thisValue, + const jsi::Value *arguments, + size_t count) -> jsi::Value { + auto numberOfChannels = static_cast(arguments[0].getNumber()); + auto length = static_cast(arguments[1].getNumber()); + auto sampleRate = static_cast(arguments[2].getNumber()); + auto buffer = + wrapper_->createBuffer(numberOfChannels, length, sampleRate); + auto bufferHostObject = + AudioBufferHostObject::createFromWrapper(buffer); + return jsi::Object::createFromHostObject(runtime, bufferHostObject); + }); + } + + throw std::runtime_error("Not yet implemented!"); +} + +void BaseAudioContextHostObject::set( + jsi::Runtime &runtime, + const jsi::PropNameID &propNameId, + const jsi::Value &value) { + auto propName = propNameId.utf8(runtime); + + throw std::runtime_error("Not yet implemented!"); +} } // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/HostObjects/BaseAudioContextHostObject.h b/packages/react-native-audio-api/common/cpp/HostObjects/BaseAudioContextHostObject.h index e83099c8..1c591488 100644 --- a/packages/react-native-audio-api/common/cpp/HostObjects/BaseAudioContextHostObject.h +++ b/packages/react-native-audio-api/common/cpp/HostObjects/BaseAudioContextHostObject.h @@ -7,32 +7,32 @@ #include "AudioBufferHostObject.h" #include "AudioBufferSourceNodeHostObject.h" -#include "BaseAudioContextWrapper.h" #include "AudioDestinationNodeHostObject.h" +#include "BaseAudioContextWrapper.h" #include "BiquadFilterNodeHostObject.h" #include "GainNodeHostObject.h" #include "OscillatorNodeHostObject.h" #include "StereoPannerNodeHostObject.h" namespace audioapi { - using namespace facebook; +using namespace facebook; - class BaseAudioContextHostObject : public jsi::HostObject { - public: - explicit BaseAudioContextHostObject( - const std::shared_ptr &wrapper); +class BaseAudioContextHostObject : public jsi::HostObject { + public: + explicit BaseAudioContextHostObject( + const std::shared_ptr &wrapper); - jsi::Value get(jsi::Runtime &runtime, const jsi::PropNameID &name) override; + jsi::Value get(jsi::Runtime &runtime, const jsi::PropNameID &name) override; - void set( - jsi::Runtime &runtime, - const jsi::PropNameID &name, - const jsi::Value &value) override; + void set( + jsi::Runtime &runtime, + const jsi::PropNameID &name, + const jsi::Value &value) override; - std::vector getPropertyNames(jsi::Runtime &rt) override; + std::vector getPropertyNames(jsi::Runtime &rt) override; - protected: - std::shared_ptr wrapper_; - std::shared_ptr destination_; - }; + protected: + std::shared_ptr wrapper_; + std::shared_ptr destination_; +}; } // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/core/BaseAudioContext.cpp b/packages/react-native-audio-api/common/cpp/core/BaseAudioContext.cpp index 42f76068..d2cb745b 100644 --- a/packages/react-native-audio-api/common/cpp/core/BaseAudioContext.cpp +++ b/packages/react-native-audio-api/common/cpp/core/BaseAudioContext.cpp @@ -2,78 +2,80 @@ namespace audioapi { - BaseAudioContext::BaseAudioContext() { +BaseAudioContext::BaseAudioContext() { #ifdef ANDROID - audioPlayer_ = std::make_shared(this->renderAudio()); + audioPlayer_ = std::make_shared(this->renderAudio()); #else - audioPlayer_ = std::make_shared(this->renderAudio()); + audioPlayer_ = std::make_shared(this->renderAudio()); #endif - destination_ = std::make_shared(this); - - sampleRate_ = audioPlayer_->getSampleRate(); - - auto now = std::chrono::high_resolution_clock ::now(); - contextStartTime_ = - static_cast(std::chrono::duration_cast( - now.time_since_epoch()) - .count()); - - audioPlayer_->start(); - } - - std::string BaseAudioContext::getState() { - return BaseAudioContext::toString(state_); - } - - int BaseAudioContext::getSampleRate() const { - return sampleRate_; - } - - double BaseAudioContext::getCurrentTime() const { - auto now = std::chrono::high_resolution_clock ::now(); - auto currentTime = - static_cast(std::chrono::duration_cast( - now.time_since_epoch()) - .count()); - return (currentTime - contextStartTime_) / 1e9; - } - - std::shared_ptr BaseAudioContext::getDestination() { - return destination_; - } - - std::shared_ptr BaseAudioContext::createOscillator() { - return std::make_shared(this); - } - - std::shared_ptr BaseAudioContext::createGain() { - return std::make_shared(this); - } - - std::shared_ptr BaseAudioContext::createStereoPanner() { - return std::make_shared(this); - } - - std::shared_ptr BaseAudioContext::createBiquadFilter() { - return std::make_shared(this); - } - - std::shared_ptr BaseAudioContext::createBufferSource() { - return std::make_shared(this); - } - - std::shared_ptr - BaseAudioContext::createBuffer(int numberOfChannels, int length, int sampleRate) { - return std::make_shared(numberOfChannels, length, sampleRate); - } - - std::function BaseAudioContext::renderAudio() { - if (state_ == State::CLOSED) { - return [](float *, int) {}; - } - - return [this](float *data, int frames) { - destination_->renderAudio(data, frames); - }; - } + destination_ = std::make_shared(this); + + sampleRate_ = audioPlayer_->getSampleRate(); + + auto now = std::chrono::high_resolution_clock ::now(); + contextStartTime_ = + static_cast(std::chrono::duration_cast( + now.time_since_epoch()) + .count()); + + audioPlayer_->start(); +} + +std::string BaseAudioContext::getState() { + return BaseAudioContext::toString(state_); +} + +int BaseAudioContext::getSampleRate() const { + return sampleRate_; +} + +double BaseAudioContext::getCurrentTime() const { + auto now = std::chrono::high_resolution_clock ::now(); + auto currentTime = + static_cast(std::chrono::duration_cast( + now.time_since_epoch()) + .count()); + return (currentTime - contextStartTime_) / 1e9; +} + +std::shared_ptr BaseAudioContext::getDestination() { + return destination_; +} + +std::shared_ptr BaseAudioContext::createOscillator() { + return std::make_shared(this); +} + +std::shared_ptr BaseAudioContext::createGain() { + return std::make_shared(this); +} + +std::shared_ptr BaseAudioContext::createStereoPanner() { + return std::make_shared(this); +} + +std::shared_ptr BaseAudioContext::createBiquadFilter() { + return std::make_shared(this); +} + +std::shared_ptr BaseAudioContext::createBufferSource() { + return std::make_shared(this); +} + +std::shared_ptr BaseAudioContext::createBuffer( + int numberOfChannels, + int length, + int sampleRate) { + return std::make_shared(numberOfChannels, length, sampleRate); +} + +std::function BaseAudioContext::renderAudio() { + if (state_ == State::CLOSED) { + return [](float *, int) {}; + } + + return [this](float *data, int frames) { + destination_->renderAudio(data, frames); + }; +} } // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/core/BaseAudioContext.h b/packages/react-native-audio-api/common/cpp/core/BaseAudioContext.h index d7d55ee2..ac65f596 100644 --- a/packages/react-native-audio-api/common/cpp/core/BaseAudioContext.h +++ b/packages/react-native-audio-api/common/cpp/core/BaseAudioContext.h @@ -24,50 +24,49 @@ namespace audioapi { - class BaseAudioContext { - public: - BaseAudioContext(); - std::string getState(); - int getSampleRate() const; - double getCurrentTime() const; +class BaseAudioContext { + public: + BaseAudioContext(); + std::string getState(); + int getSampleRate() const; + double getCurrentTime() const; - std::shared_ptr getDestination(); - std::shared_ptr createOscillator(); - std::shared_ptr createGain(); - std::shared_ptr createStereoPanner(); - std::shared_ptr createBiquadFilter(); - std::shared_ptr createBufferSource(); - static std::shared_ptr - createBuffer(int numberOfChannels, int length, int sampleRate); - std::function renderAudio(); + std::shared_ptr getDestination(); + std::shared_ptr createOscillator(); + std::shared_ptr createGain(); + std::shared_ptr createStereoPanner(); + std::shared_ptr createBiquadFilter(); + std::shared_ptr createBufferSource(); + static std::shared_ptr + createBuffer(int numberOfChannels, int length, int sampleRate); + std::function renderAudio(); - protected: - enum class State { SUSPENDED, RUNNING, CLOSED }; + protected: + enum class State { SUSPENDED, RUNNING, CLOSED }; - static std::string toString(State state) { - switch (state) { - case State::SUSPENDED: - return "suspended"; - case State::RUNNING: - return "running"; - case State::CLOSED: - return "closed"; - default: - throw std::invalid_argument("Unknown context state"); - } - } + static std::string toString(State state) { + switch (state) { + case State::SUSPENDED: + return "suspended"; + case State::RUNNING: + return "running"; + case State::CLOSED: + return "closed"; + default: + throw std::invalid_argument("Unknown context state"); + } + } - protected: - std::shared_ptr destination_; + protected: + std::shared_ptr destination_; #ifdef ANDROID - std::shared_ptr audioPlayer_; + std::shared_ptr audioPlayer_; #else - std::shared_ptr audioPlayer_; + std::shared_ptr audioPlayer_; #endif - State state_ = State::RUNNING; - int sampleRate_; - double contextStartTime_; - }; + State state_ = State::RUNNING; + int sampleRate_; + double contextStartTime_; +}; } // namespace audioapi - diff --git a/packages/react-native-audio-api/common/cpp/core/BiquadFilterNode.cpp b/packages/react-native-audio-api/common/cpp/core/BiquadFilterNode.cpp index fcfd5e98..1642a47d 100644 --- a/packages/react-native-audio-api/common/cpp/core/BiquadFilterNode.cpp +++ b/packages/react-native-audio-api/common/cpp/core/BiquadFilterNode.cpp @@ -8,7 +8,8 @@ namespace audioapi { -BiquadFilterNode::BiquadFilterNode(BaseAudioContext *context) : AudioNode(context) { +BiquadFilterNode::BiquadFilterNode(BaseAudioContext *context) + : AudioNode(context) { frequencyParam_ = std::make_shared( context, 350.0, MIN_FILTER_FREQUENCY, MAX_FILTER_FREQUENCY); detuneParam_ = diff --git a/packages/react-native-audio-api/common/cpp/core/StereoPannerNode.cpp b/packages/react-native-audio-api/common/cpp/core/StereoPannerNode.cpp index 5512eef4..42b5aacd 100644 --- a/packages/react-native-audio-api/common/cpp/core/StereoPannerNode.cpp +++ b/packages/react-native-audio-api/common/cpp/core/StereoPannerNode.cpp @@ -5,7 +5,8 @@ namespace audioapi { -StereoPannerNode::StereoPannerNode(BaseAudioContext *context) : AudioNode(context) { +StereoPannerNode::StereoPannerNode(BaseAudioContext *context) + : AudioNode(context) { channelCountMode_ = ChannelCountMode::CLAMPED_MAX; panParam_ = std::make_shared(context, 0.0, -MAX_PAN, MAX_PAN); } diff --git a/packages/react-native-audio-api/common/cpp/wrappers/AudioContextWrapper.cpp b/packages/react-native-audio-api/common/cpp/wrappers/AudioContextWrapper.cpp index 8aa999c5..78dadc7f 100644 --- a/packages/react-native-audio-api/common/cpp/wrappers/AudioContextWrapper.cpp +++ b/packages/react-native-audio-api/common/cpp/wrappers/AudioContextWrapper.cpp @@ -2,13 +2,16 @@ namespace audioapi { -AudioContextWrapper::AudioContextWrapper(const std::shared_ptr &context) : BaseAudioContextWrapper(context) {} +AudioContextWrapper::AudioContextWrapper( + const std::shared_ptr &context) + : BaseAudioContextWrapper(context) {} void AudioContextWrapper::close() { getAudioContextFromBaseAudioContext()->close(); } -std::shared_ptr AudioContextWrapper::getAudioContextFromBaseAudioContext() { +std::shared_ptr +AudioContextWrapper::getAudioContextFromBaseAudioContext() { return std::static_pointer_cast(context_); } } // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/wrappers/AudioContextWrapper.h b/packages/react-native-audio-api/common/cpp/wrappers/AudioContextWrapper.h index 108b7c39..f390f495 100644 --- a/packages/react-native-audio-api/common/cpp/wrappers/AudioContextWrapper.h +++ b/packages/react-native-audio-api/common/cpp/wrappers/AudioContextWrapper.h @@ -2,8 +2,8 @@ #include -#include "BaseAudioContextWrapper.h" #include "AudioContext.h" +#include "BaseAudioContextWrapper.h" namespace audioapi { @@ -13,7 +13,7 @@ class AudioContextWrapper : public BaseAudioContextWrapper { void close(); -private: - std::shared_ptr getAudioContextFromBaseAudioContext(); + private: + std::shared_ptr getAudioContextFromBaseAudioContext(); }; } // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/wrappers/BaseAudioContextWrapper.cpp b/packages/react-native-audio-api/common/cpp/wrappers/BaseAudioContextWrapper.cpp index 12fe1617..479e8591 100644 --- a/packages/react-native-audio-api/common/cpp/wrappers/BaseAudioContextWrapper.cpp +++ b/packages/react-native-audio-api/common/cpp/wrappers/BaseAudioContextWrapper.cpp @@ -2,65 +2,64 @@ namespace audioapi { - BaseAudioContextWrapper::BaseAudioContextWrapper( - const std::shared_ptr &context) - : context_(context) { - auto destination = context_->getDestination(); - destination_ = std::make_shared(destination); - } +BaseAudioContextWrapper::BaseAudioContextWrapper( + const std::shared_ptr &context) + : context_(context) { + auto destination = context_->getDestination(); + destination_ = std::make_shared(destination); +} - std::shared_ptr - BaseAudioContextWrapper::getDestination() const { - return destination_; - } +std::shared_ptr +BaseAudioContextWrapper::getDestination() const { + return destination_; +} - std::shared_ptr BaseAudioContextWrapper::createOscillator() - const { - auto oscillator = context_->createOscillator(); - return std::make_shared(oscillator); - } +std::shared_ptr +BaseAudioContextWrapper::createOscillator() const { + auto oscillator = context_->createOscillator(); + return std::make_shared(oscillator); +} - std::shared_ptr BaseAudioContextWrapper::createGain() const { - auto gain = context_->createGain(); - return std::make_shared(gain); - } +std::shared_ptr BaseAudioContextWrapper::createGain() const { + auto gain = context_->createGain(); + return std::make_shared(gain); +} - std::shared_ptr - BaseAudioContextWrapper::createStereoPanner() const { - auto panner = context_->createStereoPanner(); - return std::make_shared(panner); - } +std::shared_ptr +BaseAudioContextWrapper::createStereoPanner() const { + auto panner = context_->createStereoPanner(); + return std::make_shared(panner); +} - std::shared_ptr - BaseAudioContextWrapper::createBiquadFilter() const { - auto filter = context_->createBiquadFilter(); - return std::make_shared(filter); - } +std::shared_ptr +BaseAudioContextWrapper::createBiquadFilter() const { + auto filter = context_->createBiquadFilter(); + return std::make_shared(filter); +} - std::shared_ptr - BaseAudioContextWrapper::createBufferSource() const { - auto bufferSource = context_->createBufferSource(); - return std::make_shared(bufferSource); - } +std::shared_ptr +BaseAudioContextWrapper::createBufferSource() const { + auto bufferSource = context_->createBufferSource(); + return std::make_shared(bufferSource); +} - std::shared_ptr BaseAudioContextWrapper::createBuffer( - int numberOfChannels, - int length, - int sampleRate) const { - auto buffer = - context_->createBuffer(numberOfChannels, length, sampleRate); - return std::make_shared(buffer); - } +std::shared_ptr BaseAudioContextWrapper::createBuffer( + int numberOfChannels, + int length, + int sampleRate) const { + auto buffer = context_->createBuffer(numberOfChannels, length, sampleRate); + return std::make_shared(buffer); +} - std::string BaseAudioContextWrapper::getState() const { - return context_->getState(); - } +std::string BaseAudioContextWrapper::getState() const { + return context_->getState(); +} - int BaseAudioContextWrapper::getSampleRate() const { - return context_->getSampleRate(); - } +int BaseAudioContextWrapper::getSampleRate() const { + return context_->getSampleRate(); +} - double BaseAudioContextWrapper::getCurrentTime() const { - return context_->getCurrentTime(); - } +double BaseAudioContextWrapper::getCurrentTime() const { + return context_->getCurrentTime(); +} } // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/wrappers/BaseAudioContextWrapper.h b/packages/react-native-audio-api/common/cpp/wrappers/BaseAudioContextWrapper.h index 7e893c9d..11ab9b5d 100644 --- a/packages/react-native-audio-api/common/cpp/wrappers/BaseAudioContextWrapper.h +++ b/packages/react-native-audio-api/common/cpp/wrappers/BaseAudioContextWrapper.h @@ -6,8 +6,8 @@ #include "AudioBufferSourceNodeWrapper.h" #include "AudioBufferWrapper.h" -#include "BaseAudioContext.h" #include "AudioDestinationNodeWrapper.h" +#include "BaseAudioContext.h" #include "BiquadFilterNodeWrapper.h" #include "GainNodeWrapper.h" #include "OscillatorNodeWrapper.h" @@ -15,25 +15,25 @@ namespace audioapi { - class BaseAudioContextWrapper { - public: - explicit BaseAudioContextWrapper( - const std::shared_ptr &context); +class BaseAudioContextWrapper { + public: + explicit BaseAudioContextWrapper( + const std::shared_ptr &context); - std::shared_ptr getDestination() const; - std::shared_ptr createOscillator() const; - std::shared_ptr createGain() const; - std::shared_ptr createStereoPanner() const; - std::shared_ptr createBiquadFilter() const; - std::shared_ptr createBufferSource() const; - std::shared_ptr - createBuffer(int numberOfChannels, int length, int sampleRate) const; - std::string getState() const; - int getSampleRate() const; - double getCurrentTime() const; + std::shared_ptr getDestination() const; + std::shared_ptr createOscillator() const; + std::shared_ptr createGain() const; + std::shared_ptr createStereoPanner() const; + std::shared_ptr createBiquadFilter() const; + std::shared_ptr createBufferSource() const; + std::shared_ptr + createBuffer(int numberOfChannels, int length, int sampleRate) const; + std::string getState() const; + int getSampleRate() const; + double getCurrentTime() const; - protected: - std::shared_ptr destination_; - std::shared_ptr context_; - }; + protected: + std::shared_ptr destination_; + std::shared_ptr context_; +}; } // namespace audioapi From 88865daa4ed1c4b6d11746eb35ec00fab756c122 Mon Sep 17 00:00:00 2001 From: Maciej Makowski Date: Thu, 24 Oct 2024 16:07:20 +0200 Subject: [PATCH 4/5] refactor: added [[nodiscard]] annotation --- .../common/cpp/core/AudioBuffer.h | 10 ++++---- .../common/cpp/core/AudioBufferSourceNode.h | 6 ++--- .../common/cpp/core/AudioParam.h | 8 +++---- .../common/cpp/core/BaseAudioContext.h | 4 ++-- .../common/cpp/core/BiquadFilterNode.h | 10 ++++---- .../common/cpp/core/GainNode.h | 2 +- .../common/cpp/core/OscillatorNode.h | 6 ++--- .../common/cpp/core/ParamChange.h | 12 +++++----- .../common/cpp/core/StereoPannerNode.h | 3 ++- .../wrappers/AudioBufferSourceNodeWrapper.h | 4 ++-- .../common/cpp/wrappers/AudioBufferWrapper.h | 10 ++++---- .../common/cpp/wrappers/AudioNodeWrapper.h | 10 ++++---- .../common/cpp/wrappers/AudioParamWrapper.h | 8 +++---- .../cpp/wrappers/BaseAudioContextWrapper.h | 24 +++++++++++-------- .../cpp/wrappers/BiquadFilterNodeWrapper.h | 8 +++---- .../common/cpp/wrappers/GainNodeWrapper.h | 2 +- .../cpp/wrappers/OscillatorNodeWrapper.h | 4 ++-- .../cpp/wrappers/StereoPannerNodeWrapper.h | 2 +- 18 files changed, 69 insertions(+), 64 deletions(-) diff --git a/packages/react-native-audio-api/common/cpp/core/AudioBuffer.h b/packages/react-native-audio-api/common/cpp/core/AudioBuffer.h index ba2e5514..6391eba8 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioBuffer.h +++ b/packages/react-native-audio-api/common/cpp/core/AudioBuffer.h @@ -11,11 +11,11 @@ class AudioBuffer : public std::enable_shared_from_this { public: explicit AudioBuffer(int numberOfChannels, int length, int sampleRate); - int getNumberOfChannels() const; - int getLength() const; - int getSampleRate() const; - double getDuration() const; - float *getChannelData(int channel) const; + [[nodiscard]] int getNumberOfChannels() const; + [[nodiscard]] int getLength() const; + [[nodiscard]] int getSampleRate() const; + [[nodiscard]] double getDuration() const; + [[nodiscard]] float *getChannelData(int channel) const; void setChannelData(int channel, const float *data, int length); private: diff --git a/packages/react-native-audio-api/common/cpp/core/AudioBufferSourceNode.h b/packages/react-native-audio-api/common/cpp/core/AudioBufferSourceNode.h index 7fe6197d..7ca9511c 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioBufferSourceNode.h +++ b/packages/react-native-audio-api/common/cpp/core/AudioBufferSourceNode.h @@ -12,11 +12,11 @@ class AudioBufferSourceNode : public AudioScheduledSourceNode { public: explicit AudioBufferSourceNode(BaseAudioContext *context); - bool getLoop() const; - std::shared_ptr getBuffer() const; + [[nodiscard]] bool getLoop() const; + [[nodiscard]] std::shared_ptr getBuffer() const; void setLoop(bool loop); void setBuffer(const std::shared_ptr &buffer); - bool processAudio(float *audioData, int32_t numFrames) override; + [[nodiscard]] bool processAudio(float *audioData, int32_t numFrames) override; private: bool loop_; diff --git a/packages/react-native-audio-api/common/cpp/core/AudioParam.h b/packages/react-native-audio-api/common/cpp/core/AudioParam.h index 2adab364..4e4874d5 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioParam.h +++ b/packages/react-native-audio-api/common/cpp/core/AudioParam.h @@ -18,12 +18,12 @@ class AudioParam { float minValue, float maxValue); - float getValue() const; + [[nodiscard]] float getValue() const; float getValueAtTime(double time); void setValue(float value); - float getDefaultValue() const; - float getMinValue() const; - float getMaxValue() const; + [[nodiscard]] float getDefaultValue() const; + [[nodiscard]] float getMinValue() const; + [[nodiscard]] float getMaxValue() const; void setValueAtTime(float value, double startTime); void linearRampToValueAtTime(float value, double endTime); void exponentialRampToValueAtTime(float value, double endTime); diff --git a/packages/react-native-audio-api/common/cpp/core/BaseAudioContext.h b/packages/react-native-audio-api/common/cpp/core/BaseAudioContext.h index ac65f596..d19b1933 100644 --- a/packages/react-native-audio-api/common/cpp/core/BaseAudioContext.h +++ b/packages/react-native-audio-api/common/cpp/core/BaseAudioContext.h @@ -28,8 +28,8 @@ class BaseAudioContext { public: BaseAudioContext(); std::string getState(); - int getSampleRate() const; - double getCurrentTime() const; + [[nodiscard]] int getSampleRate() const; + [[nodiscard]] double getCurrentTime() const; std::shared_ptr getDestination(); std::shared_ptr createOscillator(); diff --git a/packages/react-native-audio-api/common/cpp/core/BiquadFilterNode.h b/packages/react-native-audio-api/common/cpp/core/BiquadFilterNode.h index 1d3a9d87..18ff4ad3 100644 --- a/packages/react-native-audio-api/common/cpp/core/BiquadFilterNode.h +++ b/packages/react-native-audio-api/common/cpp/core/BiquadFilterNode.h @@ -15,12 +15,12 @@ class BiquadFilterNode : public AudioNode { public: explicit BiquadFilterNode(BaseAudioContext *context); - std::string getType() const; + [[nodiscard]] std::string getType() const; void setType(const std::string &type); - std::shared_ptr getFrequencyParam() const; - std::shared_ptr getDetuneParam() const; - std::shared_ptr getQParam() const; - std::shared_ptr getGainParam() const; + [[nodiscard]] std::shared_ptr getFrequencyParam() const; + [[nodiscard]] std::shared_ptr getDetuneParam() const; + [[nodiscard]] std::shared_ptr getQParam() const; + [[nodiscard]] std::shared_ptr getGainParam() const; protected: bool processAudio(float *audioData, int32_t numFrames) override; diff --git a/packages/react-native-audio-api/common/cpp/core/GainNode.h b/packages/react-native-audio-api/common/cpp/core/GainNode.h index 87130d7f..7a74c203 100644 --- a/packages/react-native-audio-api/common/cpp/core/GainNode.h +++ b/packages/react-native-audio-api/common/cpp/core/GainNode.h @@ -11,7 +11,7 @@ class GainNode : public AudioNode { public: explicit GainNode(BaseAudioContext *context); - std::shared_ptr getGainParam() const; + [[nodiscard]] std::shared_ptr getGainParam() const; protected: bool processAudio(float *audioData, int32_t numFrames) override; diff --git a/packages/react-native-audio-api/common/cpp/core/OscillatorNode.h b/packages/react-native-audio-api/common/cpp/core/OscillatorNode.h index a0ddce4b..b9688ff3 100644 --- a/packages/react-native-audio-api/common/cpp/core/OscillatorNode.h +++ b/packages/react-native-audio-api/common/cpp/core/OscillatorNode.h @@ -13,9 +13,9 @@ class OscillatorNode : public AudioScheduledSourceNode { public: explicit OscillatorNode(BaseAudioContext *context); - std::shared_ptr getFrequencyParam() const; - std::shared_ptr getDetuneParam() const; - std::string getType(); + [[nodiscard]] std::shared_ptr getFrequencyParam() const; + [[nodiscard]] std::shared_ptr getDetuneParam() const; + [[nodiscard]] std::string getType(); void setType(const std::string &type); protected: diff --git a/packages/react-native-audio-api/common/cpp/core/ParamChange.h b/packages/react-native-audio-api/common/cpp/core/ParamChange.h index 2f2340e5..f431edc4 100644 --- a/packages/react-native-audio-api/common/cpp/core/ParamChange.h +++ b/packages/react-native-audio-api/common/cpp/core/ParamChange.h @@ -15,12 +15,12 @@ class ParamChange { std::function calculateValue); - double getEndTime() const; - double getStartTime() const; - float getEndValue() const; - float getStartValue() const; - std::function getCalculateValue() - const; + [[nodiscard]] double getEndTime() const; + [[nodiscard]] double getStartTime() const; + [[nodiscard]] float getEndValue() const; + [[nodiscard]] float getStartValue() const; + [[nodiscard]] std::function + getCalculateValue() const; bool operator<(const ParamChange &other) const; private: diff --git a/packages/react-native-audio-api/common/cpp/core/StereoPannerNode.h b/packages/react-native-audio-api/common/cpp/core/StereoPannerNode.h index 6c2b1bd2..a3450ed2 100644 --- a/packages/react-native-audio-api/common/cpp/core/StereoPannerNode.h +++ b/packages/react-native-audio-api/common/cpp/core/StereoPannerNode.h @@ -12,7 +12,8 @@ namespace audioapi { class StereoPannerNode : public AudioNode { public: explicit StereoPannerNode(BaseAudioContext *context); - std::shared_ptr getPanParam() const; + + [[nodiscard]] std::shared_ptr getPanParam() const; protected: bool processAudio(float *audioData, int32_t numFrames) override; diff --git a/packages/react-native-audio-api/common/cpp/wrappers/AudioBufferSourceNodeWrapper.h b/packages/react-native-audio-api/common/cpp/wrappers/AudioBufferSourceNodeWrapper.h index 66b6e7bf..42b1eb9c 100644 --- a/packages/react-native-audio-api/common/cpp/wrappers/AudioBufferSourceNodeWrapper.h +++ b/packages/react-native-audio-api/common/cpp/wrappers/AudioBufferSourceNodeWrapper.h @@ -14,8 +14,8 @@ class AudioBufferSourceNodeWrapper : public AudioScheduledSourceNodeWrapper { const std::shared_ptr &audioBufferSourceNode); void setLoop(bool loop); - bool getLoop(); - std::shared_ptr getBuffer(); + [[nodiscard]] bool getLoop(); + [[nodiscard]] std::shared_ptr getBuffer(); void setBuffer(const std::shared_ptr &buffer); private: diff --git a/packages/react-native-audio-api/common/cpp/wrappers/AudioBufferWrapper.h b/packages/react-native-audio-api/common/cpp/wrappers/AudioBufferWrapper.h index 7b08b0a0..e280f92d 100644 --- a/packages/react-native-audio-api/common/cpp/wrappers/AudioBufferWrapper.h +++ b/packages/react-native-audio-api/common/cpp/wrappers/AudioBufferWrapper.h @@ -11,11 +11,11 @@ class AudioBufferWrapper { explicit AudioBufferWrapper(const std::shared_ptr &audioBuffer); std::shared_ptr audioBuffer_; - int getNumberOfChannels() const; - int getLength() const; - double getDuration() const; - int getSampleRate() const; - float *getChannelData(int channel) const; + [[nodiscard]] int getNumberOfChannels() const; + [[nodiscard]] int getLength() const; + [[nodiscard]] double getDuration() const; + [[nodiscard]] int getSampleRate() const; + [[nodiscard]] float *getChannelData(int channel) const; void setChannelData(int channel, float *data, int length) const; }; } // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/wrappers/AudioNodeWrapper.h b/packages/react-native-audio-api/common/cpp/wrappers/AudioNodeWrapper.h index 723690cf..3c7e0c24 100644 --- a/packages/react-native-audio-api/common/cpp/wrappers/AudioNodeWrapper.h +++ b/packages/react-native-audio-api/common/cpp/wrappers/AudioNodeWrapper.h @@ -11,11 +11,11 @@ class AudioNodeWrapper { public: explicit AudioNodeWrapper(const std::shared_ptr &node); - int getNumberOfInputs() const; - int getNumberOfOutputs() const; - int getChannelCount() const; - std::string getChannelCountMode() const; - std::string getChannelInterpretation() const; + [[nodiscard]] int getNumberOfInputs() const; + [[nodiscard]] int getNumberOfOutputs() const; + [[nodiscard]] int getChannelCount() const; + [[nodiscard]] std::string getChannelCountMode() const; + [[nodiscard]] std::string getChannelInterpretation() const; void connect(const std::shared_ptr &node) const; void disconnect(const std::shared_ptr &node) const; diff --git a/packages/react-native-audio-api/common/cpp/wrappers/AudioParamWrapper.h b/packages/react-native-audio-api/common/cpp/wrappers/AudioParamWrapper.h index 6bad672a..ec3da4fb 100644 --- a/packages/react-native-audio-api/common/cpp/wrappers/AudioParamWrapper.h +++ b/packages/react-native-audio-api/common/cpp/wrappers/AudioParamWrapper.h @@ -10,11 +10,11 @@ class AudioParamWrapper { public: explicit AudioParamWrapper(const std::shared_ptr ¶m); - float getValue() const; + [[nodiscard]] float getValue() const; void setValue(float value) const; - float getDefaultValue() const; - float getMinValue() const; - float getMaxValue() const; + [[nodiscard]] float getDefaultValue() const; + [[nodiscard]] float getMinValue() const; + [[nodiscard]] float getMaxValue() const; void setValueAtTime(float value, double startTime) const; void linearRampToValueAtTime(float value, double endTime) const; void exponentialRampToValueAtTime(float value, double endTime) const; diff --git a/packages/react-native-audio-api/common/cpp/wrappers/BaseAudioContextWrapper.h b/packages/react-native-audio-api/common/cpp/wrappers/BaseAudioContextWrapper.h index 11ab9b5d..0be08009 100644 --- a/packages/react-native-audio-api/common/cpp/wrappers/BaseAudioContextWrapper.h +++ b/packages/react-native-audio-api/common/cpp/wrappers/BaseAudioContextWrapper.h @@ -20,17 +20,21 @@ class BaseAudioContextWrapper { explicit BaseAudioContextWrapper( const std::shared_ptr &context); - std::shared_ptr getDestination() const; - std::shared_ptr createOscillator() const; - std::shared_ptr createGain() const; - std::shared_ptr createStereoPanner() const; - std::shared_ptr createBiquadFilter() const; - std::shared_ptr createBufferSource() const; - std::shared_ptr + [[nodiscard]] std::shared_ptr getDestination() + const; + [[nodiscard]] std::shared_ptr createOscillator() const; + [[nodiscard]] std::shared_ptr createGain() const; + [[nodiscard]] std::shared_ptr createStereoPanner() + const; + [[nodiscard]] std::shared_ptr createBiquadFilter() + const; + [[nodiscard]] std::shared_ptr + createBufferSource() const; + [[nodiscard]] std::shared_ptr createBuffer(int numberOfChannels, int length, int sampleRate) const; - std::string getState() const; - int getSampleRate() const; - double getCurrentTime() const; + [[nodiscard]] std::string getState() const; + [[nodiscard]] int getSampleRate() const; + [[nodiscard]] double getCurrentTime() const; protected: std::shared_ptr destination_; diff --git a/packages/react-native-audio-api/common/cpp/wrappers/BiquadFilterNodeWrapper.h b/packages/react-native-audio-api/common/cpp/wrappers/BiquadFilterNodeWrapper.h index adf49d8f..68583e71 100644 --- a/packages/react-native-audio-api/common/cpp/wrappers/BiquadFilterNodeWrapper.h +++ b/packages/react-native-audio-api/common/cpp/wrappers/BiquadFilterNodeWrapper.h @@ -14,10 +14,10 @@ class BiquadFilterNodeWrapper : public AudioNodeWrapper { explicit BiquadFilterNodeWrapper( const std::shared_ptr &biquadFilterNode); - std::shared_ptr getFrequencyParam() const; - std::shared_ptr getDetuneParam() const; - std::shared_ptr getQParam() const; - std::shared_ptr getGainParam() const; + [[nodiscard]] std::shared_ptr getFrequencyParam() const; + [[nodiscard]] std::shared_ptr getDetuneParam() const; + [[nodiscard]] std::shared_ptr getQParam() const; + [[nodiscard]] std::shared_ptr getGainParam() const; std::string getType(); void setType(const std::string &filterType); diff --git a/packages/react-native-audio-api/common/cpp/wrappers/GainNodeWrapper.h b/packages/react-native-audio-api/common/cpp/wrappers/GainNodeWrapper.h index aea112ae..77581f6f 100644 --- a/packages/react-native-audio-api/common/cpp/wrappers/GainNodeWrapper.h +++ b/packages/react-native-audio-api/common/cpp/wrappers/GainNodeWrapper.h @@ -12,7 +12,7 @@ class GainNodeWrapper : public AudioNodeWrapper { public: explicit GainNodeWrapper(const std::shared_ptr &gainNode); - std::shared_ptr getGainParam() const; + [[nodiscard]] std::shared_ptr getGainParam() const; private: std::shared_ptr gainParam_; diff --git a/packages/react-native-audio-api/common/cpp/wrappers/OscillatorNodeWrapper.h b/packages/react-native-audio-api/common/cpp/wrappers/OscillatorNodeWrapper.h index 76ce1140..e00f80bb 100644 --- a/packages/react-native-audio-api/common/cpp/wrappers/OscillatorNodeWrapper.h +++ b/packages/react-native-audio-api/common/cpp/wrappers/OscillatorNodeWrapper.h @@ -14,8 +14,8 @@ class OscillatorNodeWrapper : public AudioScheduledSourceNodeWrapper { explicit OscillatorNodeWrapper( const std::shared_ptr &oscillatorNode); - std::shared_ptr getFrequencyParam() const; - std::shared_ptr getDetuneParam() const; + [[nodiscard]] std::shared_ptr getFrequencyParam() const; + [[nodiscard]] std::shared_ptr getDetuneParam() const; std::string getType(); void setType(const std::string &type); diff --git a/packages/react-native-audio-api/common/cpp/wrappers/StereoPannerNodeWrapper.h b/packages/react-native-audio-api/common/cpp/wrappers/StereoPannerNodeWrapper.h index 27632876..ad7fa408 100644 --- a/packages/react-native-audio-api/common/cpp/wrappers/StereoPannerNodeWrapper.h +++ b/packages/react-native-audio-api/common/cpp/wrappers/StereoPannerNodeWrapper.h @@ -13,7 +13,7 @@ class StereoPannerNodeWrapper : public AudioNodeWrapper { explicit StereoPannerNodeWrapper( const std::shared_ptr &stereoPannerNode); - std::shared_ptr getPanParam() const; + [[nodiscard]] std::shared_ptr getPanParam() const; private: std::shared_ptr panParam_; From 08571d773ff56cb81b5d3422fd7957b0f69261a9 Mon Sep 17 00:00:00 2001 From: Maciej Makowski Date: Thu, 24 Oct 2024 16:16:30 +0200 Subject: [PATCH 5/5] refactor: added necessary include --- .../common/cpp/HostObjects/AudioContextHostObject.h | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/react-native-audio-api/common/cpp/HostObjects/AudioContextHostObject.h b/packages/react-native-audio-api/common/cpp/HostObjects/AudioContextHostObject.h index 81d2899c..37b46d37 100644 --- a/packages/react-native-audio-api/common/cpp/HostObjects/AudioContextHostObject.h +++ b/packages/react-native-audio-api/common/cpp/HostObjects/AudioContextHostObject.h @@ -2,6 +2,7 @@ #include #include +#include #include "AudioContextWrapper.h" #include "BaseAudioContextHostObject.h"