From 0e6556a83ba7a8c05b82ae61ab97686218d70106 Mon Sep 17 00:00:00 2001 From: Maciej Makowski Date: Thu, 12 Dec 2024 17:34:56 +0100 Subject: [PATCH] feat: introduced JsiHostObject class as a base for all HostObjects --- .../AudioAPIInstallerHostObject.cpp | 57 ---- .../AudioAPIInstallerHostObject.h | 26 +- .../cpp/HostObjects/AudioBufferHostObject.cpp | 150 ----------- .../cpp/HostObjects/AudioBufferHostObject.h | 87 +++++- .../AudioBufferSourceNodeHostObject.cpp | 79 ------ .../AudioBufferSourceNodeHostObject.h | 54 +++- .../HostObjects/AudioContextHostObject.cpp | 55 ---- .../cpp/HostObjects/AudioContextHostObject.h | 24 +- .../AudioDestinationNodeHostObject.cpp | 33 --- .../AudioDestinationNodeHostObject.h | 12 +- .../cpp/HostObjects/AudioNodeHostObject.cpp | 98 ------- .../cpp/HostObjects/AudioNodeHostObject.h | 54 +++- .../cpp/HostObjects/AudioParamHostObject.cpp | 115 -------- .../cpp/HostObjects/AudioParamHostObject.h | 65 ++++- .../AudioScheduledSourceNodeHostObject.cpp | 70 ----- .../AudioScheduledSourceNodeHostObject.h | 35 ++- .../BaseAudioContextHostObject.cpp | 249 ------------------ .../HostObjects/BaseAudioContextHostObject.h | 135 +++++++++- .../BiquadFilterNodeHostObject.cpp | 122 --------- .../HostObjects/BiquadFilterNodeHostObject.h | 90 ++++++- .../cpp/HostObjects/GainNodeHostObject.cpp | 40 --- .../cpp/HostObjects/GainNodeHostObject.h | 20 +- .../HostObjects/OscillatorNodeHostObject.cpp | 88 ------- .../HostObjects/OscillatorNodeHostObject.h | 53 +++- .../HostObjects/PeriodicWaveHostObject.cpp | 33 --- .../cpp/HostObjects/PeriodicWaveHostObject.h | 12 +- .../StereoPannerNodeHostObject.cpp | 41 --- .../HostObjects/StereoPannerNodeHostObject.h | 24 +- .../common/cpp/utils/JsiPromise.h | 2 +- 29 files changed, 542 insertions(+), 1381 deletions(-) delete mode 100644 packages/react-native-audio-api/common/cpp/AudioAPIInstaller/AudioAPIInstallerHostObject.cpp delete mode 100644 packages/react-native-audio-api/common/cpp/HostObjects/AudioBufferHostObject.cpp delete mode 100644 packages/react-native-audio-api/common/cpp/HostObjects/AudioBufferSourceNodeHostObject.cpp delete mode 100644 packages/react-native-audio-api/common/cpp/HostObjects/AudioContextHostObject.cpp delete mode 100644 packages/react-native-audio-api/common/cpp/HostObjects/AudioDestinationNodeHostObject.cpp delete mode 100644 packages/react-native-audio-api/common/cpp/HostObjects/AudioNodeHostObject.cpp delete mode 100644 packages/react-native-audio-api/common/cpp/HostObjects/AudioParamHostObject.cpp delete mode 100644 packages/react-native-audio-api/common/cpp/HostObjects/AudioScheduledSourceNodeHostObject.cpp delete mode 100644 packages/react-native-audio-api/common/cpp/HostObjects/BaseAudioContextHostObject.cpp delete mode 100644 packages/react-native-audio-api/common/cpp/HostObjects/BiquadFilterNodeHostObject.cpp delete mode 100644 packages/react-native-audio-api/common/cpp/HostObjects/GainNodeHostObject.cpp delete mode 100644 packages/react-native-audio-api/common/cpp/HostObjects/OscillatorNodeHostObject.cpp delete mode 100644 packages/react-native-audio-api/common/cpp/HostObjects/PeriodicWaveHostObject.cpp delete mode 100644 packages/react-native-audio-api/common/cpp/HostObjects/StereoPannerNodeHostObject.cpp diff --git a/packages/react-native-audio-api/common/cpp/AudioAPIInstaller/AudioAPIInstallerHostObject.cpp b/packages/react-native-audio-api/common/cpp/AudioAPIInstaller/AudioAPIInstallerHostObject.cpp deleted file mode 100644 index d999aaf0..00000000 --- a/packages/react-native-audio-api/common/cpp/AudioAPIInstaller/AudioAPIInstallerHostObject.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include "AudioAPIInstallerHostObject.h" - -namespace audioapi { -using namespace facebook; - -AudioAPIInstallerHostObject::AudioAPIInstallerHostObject( - jsi::Runtime *runtime, - const std::shared_ptr &jsInvoker) - : rnRuntime_(runtime) { - promiseVendor_ = - std::make_shared(runtime, jsInvoker); -} - -std::vector AudioAPIInstallerHostObject::getPropertyNames( - jsi::Runtime &runtime) { - std::vector propertyNames; - propertyNames.push_back( - jsi::PropNameID::forUtf8(runtime, "createAudioContext")); - return propertyNames; -} - -jsi::Value AudioAPIInstallerHostObject::get( - jsi::Runtime &runtime, - const jsi::PropNameID &propNameId) { - auto propName = propNameId.utf8(runtime); - - if (propName == "createAudioContext") { - 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 audioContext = std::make_shared(); - auto audioContextHostObject = - std::make_shared( - audioContext, promiseVendor_); - return jsi::Object::createFromHostObject( - runtime, audioContextHostObject); - }); - } - - throw std::runtime_error("Not yet implemented!"); -} - -void AudioAPIInstallerHostObject::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/AudioAPIInstaller/AudioAPIInstallerHostObject.h b/packages/react-native-audio-api/common/cpp/AudioAPIInstaller/AudioAPIInstallerHostObject.h index 12c8dca3..430c5324 100644 --- a/packages/react-native-audio-api/common/cpp/AudioAPIInstaller/AudioAPIInstallerHostObject.h +++ b/packages/react-native-audio-api/common/cpp/AudioAPIInstaller/AudioAPIInstallerHostObject.h @@ -6,6 +6,7 @@ #include #include +#include #include "AudioContextHostObject.h" #include "JsiPromise.h" @@ -13,21 +14,19 @@ namespace audioapi { using namespace facebook; class AudioAPIInstallerHostObject - : public jsi::HostObject, + : public JsiHostObject, public std::enable_shared_from_this { public: explicit AudioAPIInstallerHostObject( jsi::Runtime *runtime, - const std::shared_ptr &jsInvoker); + const std::shared_ptr &jsInvoker) + : rnRuntime_(runtime) { + promiseVendor_ = + std::make_shared(runtime, jsInvoker); - 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; + addFunctions( + JSI_EXPORT_FUNCTION(AudioAPIInstallerHostObject, createAudioContext)); + } void install() { auto object = @@ -36,6 +35,13 @@ class AudioAPIInstallerHostObject *rnRuntime_, "__AudioAPIInstaller", std::move(object)); } + JSI_HOST_FUNCTION(createAudioContext) { + auto audioContext = std::make_shared(); + auto audioContextHostObject = + std::make_shared(audioContext, promiseVendor_); + return jsi::Object::createFromHostObject(runtime, audioContextHostObject); + } + private: std::shared_ptr promiseVendor_; jsi::Runtime *rnRuntime_; diff --git a/packages/react-native-audio-api/common/cpp/HostObjects/AudioBufferHostObject.cpp b/packages/react-native-audio-api/common/cpp/HostObjects/AudioBufferHostObject.cpp deleted file mode 100644 index 850541d2..00000000 --- a/packages/react-native-audio-api/common/cpp/HostObjects/AudioBufferHostObject.cpp +++ /dev/null @@ -1,150 +0,0 @@ -#include "AudioBufferHostObject.h" - -namespace audioapi { -using namespace facebook; - -AudioBufferHostObject::AudioBufferHostObject( - const std::shared_ptr &audioBuffer) - : audioBuffer_(audioBuffer) {} - -std::vector AudioBufferHostObject::getPropertyNames( - jsi::Runtime &runtime) { - std::vector propertyNames; - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "buffer")); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "sampleRate")); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "length")); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "duration")); - propertyNames.push_back( - jsi::PropNameID::forAscii(runtime, "numberOfChannels")); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "getChannelData")); - propertyNames.push_back( - jsi::PropNameID::forAscii(runtime, "copyFromChannel")); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "copyToChannel")); - return propertyNames; -} - -jsi::Value AudioBufferHostObject::get( - jsi::Runtime &runtime, - const jsi::PropNameID &propNameId) { - auto propName = propNameId.utf8(runtime); - - if (propName == "sampleRate") { - return {audioBuffer_->getSampleRate()}; - } - - if (propName == "length") { - return {audioBuffer_->getLength()}; - } - - if (propName == "duration") { - return {audioBuffer_->getDuration()}; - } - - if (propName == "numberOfChannels") { - return {audioBuffer_->getNumberOfChannels()}; - } - - if (propName == "getChannelData") { - return jsi::Function::createFromHostFunction( - runtime, - propNameId, - 1, - [this]( - jsi::Runtime &rt, - const jsi::Value &thisVal, - const jsi::Value *args, - size_t count) -> jsi::Value { - int channel = static_cast(args[0].getNumber()); - float *channelData = audioBuffer_->getChannelData(channel); - - auto array = jsi::Array(rt, audioBuffer_->getLength()); - for (int i = 0; i < audioBuffer_->getLength(); i++) { - array.setValueAtIndex(rt, i, jsi::Value(channelData[i])); - } - - return array; - }); - } - - if (propName == "copyFromChannel") { - return jsi::Function::createFromHostFunction( - runtime, - propNameId, - 3, - [this]( - jsi::Runtime &rt, - const jsi::Value &thisVal, - const jsi::Value *args, - size_t count) -> jsi::Value { - auto destination = args[0].getObject(rt).asArray(rt); - auto destinationLength = static_cast( - destination.getProperty(rt, "length").asNumber()); - auto channelNumber = static_cast(args[1].getNumber()); - auto startInChannel = static_cast(args[2].getNumber()); - - auto *destinationData = new float[destinationLength]; - - audioBuffer_->copyFromChannel( - destinationData, - destinationLength, - channelNumber, - startInChannel); - - for (int i = 0; i < destinationLength; i++) { - destination.setValueAtIndex(rt, i, jsi::Value(destinationData[i])); - } - - return jsi::Value::undefined(); - }); - } - - if (propName == "copyToChannel") { - return jsi::Function::createFromHostFunction( - runtime, - propNameId, - 3, - [this]( - jsi::Runtime &rt, - const jsi::Value &thisVal, - const jsi::Value *args, - size_t count) -> jsi::Value { - auto source = args[0].getObject(rt).asArray(rt); - auto sourceLength = - static_cast(source.getProperty(rt, "length").asNumber()); - auto channelNumber = static_cast(args[1].getNumber()); - auto startInChannel = static_cast(args[2].getNumber()); - - auto *sourceData = new float[sourceLength]; - - for (int i = 0; i < sourceLength; i++) { - sourceData[i] = - static_cast(source.getValueAtIndex(rt, i).getNumber()); - } - - audioBuffer_->copyToChannel( - sourceData, sourceLength, channelNumber, startInChannel); - - return jsi::Value::undefined(); - }); - } - - // `decodeAudioData` is a method that returns a promise to - // AudioBufferHostObject It seems that async/await checks for the presence of - // `then` method on the object - if (propName == "then") { - return jsi::Value::undefined(); - } - - throw std::runtime_error("Not yet implemented!"); -} - -void AudioBufferHostObject::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/AudioBufferHostObject.h b/packages/react-native-audio-api/common/cpp/HostObjects/AudioBufferHostObject.h index bba78243..e38fc7c5 100644 --- a/packages/react-native-audio-api/common/cpp/HostObjects/AudioBufferHostObject.h +++ b/packages/react-native-audio-api/common/cpp/HostObjects/AudioBufferHostObject.h @@ -4,25 +4,96 @@ #include #include +#include #include "AudioBuffer.h" namespace audioapi { using namespace facebook; -class AudioBufferHostObject : public jsi::HostObject { +class AudioBufferHostObject : public JsiHostObject { public: std::shared_ptr audioBuffer_; explicit AudioBufferHostObject( - const std::shared_ptr &audioBuffer); + const std::shared_ptr &audioBuffer) + : audioBuffer_(audioBuffer) { + addGetters( + JSI_EXPORT_PROPERTY_GETTER(AudioBufferHostObject, sampleRate), + JSI_EXPORT_PROPERTY_GETTER(AudioBufferHostObject, length), + JSI_EXPORT_PROPERTY_GETTER(AudioBufferHostObject, duration), + JSI_EXPORT_PROPERTY_GETTER(AudioBufferHostObject, numberOfChannels)); - jsi::Value get(jsi::Runtime &runtime, const jsi::PropNameID &name) override; + addFunctions( + JSI_EXPORT_FUNCTION(AudioBufferHostObject, getChannelData), + JSI_EXPORT_FUNCTION(AudioBufferHostObject, copyFromChannel), + JSI_EXPORT_FUNCTION(AudioBufferHostObject, copyToChannel)); + } - void set( - jsi::Runtime &runtime, - const jsi::PropNameID &name, - const jsi::Value &value) override; + JSI_PROPERTY_GETTER(sampleRate) { + return {audioBuffer_->getSampleRate()}; + } - std::vector getPropertyNames(jsi::Runtime &rt) override; + JSI_PROPERTY_GETTER(length) { + return {audioBuffer_->getLength()}; + } + + JSI_PROPERTY_GETTER(duration) { + return {audioBuffer_->getDuration()}; + } + + JSI_PROPERTY_GETTER(numberOfChannels) { + return {audioBuffer_->getNumberOfChannels()}; + } + + JSI_HOST_FUNCTION(getChannelData) { + int channel = static_cast(args[0].getNumber()); + float *channelData = audioBuffer_->getChannelData(channel); + + auto array = jsi::Array(runtime, audioBuffer_->getLength()); + for (int i = 0; i < audioBuffer_->getLength(); i++) { + array.setValueAtIndex(runtime, i, jsi::Value(channelData[i])); + } + + return array; + } + + JSI_HOST_FUNCTION(copyFromChannel) { + auto destination = args[0].getObject(runtime).asArray(runtime); + auto destinationLength = + static_cast(destination.getProperty(runtime, "length").asNumber()); + auto channelNumber = static_cast(args[1].getNumber()); + auto startInChannel = static_cast(args[2].getNumber()); + + auto *destinationData = new float[destinationLength]; + + audioBuffer_->copyFromChannel( + destinationData, destinationLength, channelNumber, startInChannel); + + for (int i = 0; i < destinationLength; i++) { + destination.setValueAtIndex(runtime, i, jsi::Value(destinationData[i])); + } + + return jsi::Value::undefined(); + } + + JSI_HOST_FUNCTION(copyToChannel) { + auto source = args[0].getObject(runtime).asArray(runtime); + auto sourceLength = + static_cast(source.getProperty(runtime, "length").asNumber()); + auto channelNumber = static_cast(args[1].getNumber()); + auto startInChannel = static_cast(args[2].getNumber()); + + auto *sourceData = new float[sourceLength]; + + for (int i = 0; i < sourceLength; i++) { + sourceData[i] = + static_cast(source.getValueAtIndex(runtime, i).getNumber()); + } + + audioBuffer_->copyToChannel( + sourceData, sourceLength, channelNumber, startInChannel); + + return jsi::Value::undefined(); + } }; } // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/HostObjects/AudioBufferSourceNodeHostObject.cpp b/packages/react-native-audio-api/common/cpp/HostObjects/AudioBufferSourceNodeHostObject.cpp deleted file mode 100644 index 4b5218e4..00000000 --- a/packages/react-native-audio-api/common/cpp/HostObjects/AudioBufferSourceNodeHostObject.cpp +++ /dev/null @@ -1,79 +0,0 @@ -#include "AudioBufferSourceNodeHostObject.h" - -namespace audioapi { -using namespace facebook; - -AudioBufferSourceNodeHostObject::AudioBufferSourceNodeHostObject( - const std::shared_ptr &node) - : AudioScheduledSourceNodeHostObject(node) {} - -std::shared_ptr -AudioBufferSourceNodeHostObject::getAudioBufferSourceNodeFromAudioNode() { - return std::static_pointer_cast(node_); -} - -std::vector AudioBufferSourceNodeHostObject::getPropertyNames( - jsi::Runtime &runtime) { - std::vector propertyNames = - AudioScheduledSourceNodeHostObject::getPropertyNames(runtime); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "loop")); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "buffer")); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "resetBuffer")); - return propertyNames; -} - -jsi::Value AudioBufferSourceNodeHostObject::get( - jsi::Runtime &runtime, - const jsi::PropNameID &propNameId) { - auto propName = propNameId.utf8(runtime); - - if (propName == "loop") { - auto wrapper = getAudioBufferSourceNodeFromAudioNode(); - auto loop = wrapper->getLoop(); - return {loop}; - } - - if (propName == "buffer") { - auto wrapper = getAudioBufferSourceNodeFromAudioNode(); - auto buffer = wrapper->getBuffer(); - - if (!buffer) { - return jsi::Value::null(); - } - - auto bufferHostObject = std::make_shared(buffer); - return jsi::Object::createFromHostObject(runtime, bufferHostObject); - } - - return AudioScheduledSourceNodeHostObject::get(runtime, propNameId); -} - -void AudioBufferSourceNodeHostObject::set( - jsi::Runtime &runtime, - const jsi::PropNameID &propNameId, - const jsi::Value &value) { - auto propName = propNameId.utf8(runtime); - - if (propName == "loop") { - auto wrapper = getAudioBufferSourceNodeFromAudioNode(); - wrapper->setLoop(value.getBool()); - return; - } - - if (propName == "buffer") { - auto bufferSource = getAudioBufferSourceNodeFromAudioNode(); - - if (value.isNull()) { - bufferSource->setBuffer(std::shared_ptr(nullptr)); - return; - } - - auto bufferHostObject = - value.getObject(runtime).asHostObject(runtime); - bufferSource->setBuffer(bufferHostObject->audioBuffer_); - return; - } - - AudioScheduledSourceNodeHostObject::set(runtime, propNameId, value); -} -} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/HostObjects/AudioBufferSourceNodeHostObject.h b/packages/react-native-audio-api/common/cpp/HostObjects/AudioBufferSourceNodeHostObject.h index a0e51878..2255e8d2 100644 --- a/packages/react-native-audio-api/common/cpp/HostObjects/AudioBufferSourceNodeHostObject.h +++ b/packages/react-native-audio-api/common/cpp/HostObjects/AudioBufferSourceNodeHostObject.h @@ -14,19 +14,53 @@ class AudioBufferSourceNodeHostObject : public AudioScheduledSourceNodeHostObject { public: explicit AudioBufferSourceNodeHostObject( - const std::shared_ptr &node); + const std::shared_ptr &node) + : AudioScheduledSourceNodeHostObject(node) { + addGetters( + JSI_EXPORT_PROPERTY_GETTER(AudioBufferSourceNodeHostObject, loop), + JSI_EXPORT_PROPERTY_GETTER(AudioBufferSourceNodeHostObject, buffer)); + addSetters( + JSI_EXPORT_PROPERTY_SETTER(AudioBufferSourceNodeHostObject, loop), + JSI_EXPORT_PROPERTY_SETTER(AudioBufferSourceNodeHostObject, buffer)); + } - jsi::Value get(jsi::Runtime &runtime, const jsi::PropNameID &name) override; + JSI_PROPERTY_GETTER(loop) { + auto audioBufferSourceNode = + std::static_pointer_cast(node_); + auto loop = audioBufferSourceNode->getLoop(); + return {loop}; + } - void set( - jsi::Runtime &runtime, - const jsi::PropNameID &name, - const jsi::Value &value) override; + JSI_PROPERTY_GETTER(buffer) { + auto audioBufferSourceNode = + std::static_pointer_cast(node_); + auto buffer = audioBufferSourceNode->getBuffer(); - std::vector getPropertyNames(jsi::Runtime &rt) override; + if (!buffer) { + return jsi::Value::null(); + } - private: - std::shared_ptr - getAudioBufferSourceNodeFromAudioNode(); + auto bufferHostObject = std::make_shared(buffer); + return jsi::Object::createFromHostObject(runtime, bufferHostObject); + } + + JSI_PROPERTY_SETTER(loop) { + auto audioBufferSourceNode = + std::static_pointer_cast(node_); + audioBufferSourceNode->setLoop(value.getBool()); + } + + JSI_PROPERTY_SETTER(buffer) { + auto audioBufferSourceNode = + std::static_pointer_cast(node_); + if (value.isNull()) { + audioBufferSourceNode->setBuffer(std::shared_ptr(nullptr)); + return; + } + + auto bufferHostObject = + value.getObject(runtime).asHostObject(runtime); + audioBufferSourceNode->setBuffer(bufferHostObject->audioBuffer_); + } }; } // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/HostObjects/AudioContextHostObject.cpp b/packages/react-native-audio-api/common/cpp/HostObjects/AudioContextHostObject.cpp deleted file mode 100644 index ff1654cc..00000000 --- a/packages/react-native-audio-api/common/cpp/HostObjects/AudioContextHostObject.cpp +++ /dev/null @@ -1,55 +0,0 @@ -#include "AudioContextHostObject.h" - -namespace audioapi { -using namespace facebook; - -AudioContextHostObject::AudioContextHostObject( - const std::shared_ptr &audioContext, - const std::shared_ptr &promiseVendor) - : BaseAudioContextHostObject(audioContext, promiseVendor) {} - -std::vector AudioContextHostObject::getPropertyNames( - jsi::Runtime &runtime) { - std::vector propertyNames = - BaseAudioContextHostObject::getPropertyNames(runtime); - propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "close")); - return propertyNames; -} - -jsi::Value AudioContextHostObject::get( - jsi::Runtime &runtime, - const jsi::PropNameID &propNameId) { - auto propName = propNameId.utf8(runtime); - - if (propName == "close") { - return jsi::Function::createFromHostFunction( - runtime, - propNameId, - 0, - [this]( - jsi::Runtime &runtime, - const jsi::Value &thisValue, - const jsi::Value *arguments, - size_t count) -> jsi::Value { - getAudioContextFromBaseAudioContext()->close(); - return jsi::Value::undefined(); - }); - } - - return BaseAudioContextHostObject::get(runtime, propNameId); -} - -void AudioContextHostObject::set( - jsi::Runtime &runtime, - const jsi::PropNameID &propNameId, - const jsi::Value &value) { - auto propName = propNameId.utf8(runtime); - - return BaseAudioContextHostObject::set(runtime, propNameId, value); -} - -std::shared_ptr -AudioContextHostObject::getAudioContextFromBaseAudioContext() { - return std::static_pointer_cast(context_); -} -} // 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 d774ffc3..d9177c3d 100644 --- a/packages/react-native-audio-api/common/cpp/HostObjects/AudioContextHostObject.h +++ b/packages/react-native-audio-api/common/cpp/HostObjects/AudioContextHostObject.h @@ -6,7 +6,6 @@ #include "AudioContext.h" #include "BaseAudioContextHostObject.h" -#include "JsiPromise.h" namespace audioapi { using namespace facebook; @@ -15,18 +14,15 @@ class AudioContextHostObject : public BaseAudioContextHostObject { public: explicit AudioContextHostObject( const std::shared_ptr &audioContext, - const std::shared_ptr &promiseVendor); - - 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; - - private: - std::shared_ptr getAudioContextFromBaseAudioContext(); + const std::shared_ptr &promiseVendor) + : BaseAudioContextHostObject(audioContext, promiseVendor) { + addFunctions(JSI_EXPORT_FUNCTION(AudioContextHostObject, close)); + } + + JSI_HOST_FUNCTION(close) { + auto audioContext = std::static_pointer_cast(context_); + audioContext->close(); + return jsi::Value::undefined(); + } }; } // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/HostObjects/AudioDestinationNodeHostObject.cpp b/packages/react-native-audio-api/common/cpp/HostObjects/AudioDestinationNodeHostObject.cpp deleted file mode 100644 index 9a4e7e69..00000000 --- a/packages/react-native-audio-api/common/cpp/HostObjects/AudioDestinationNodeHostObject.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include "AudioDestinationNodeHostObject.h" - -namespace audioapi { -using namespace facebook; - -AudioDestinationNodeHostObject::AudioDestinationNodeHostObject( - const std::shared_ptr &node) - : AudioNodeHostObject(node) {} - -std::vector AudioDestinationNodeHostObject::getPropertyNames( - jsi::Runtime &runtime) { - std::vector propertyNames; - return propertyNames; -} - -jsi::Value AudioDestinationNodeHostObject::get( - jsi::Runtime &runtime, - const jsi::PropNameID &propNameId) { - auto propName = propNameId.utf8(runtime); - - return AudioNodeHostObject::get(runtime, propNameId); -} - -void AudioDestinationNodeHostObject::set( - jsi::Runtime &runtime, - const jsi::PropNameID &propNameId, - const jsi::Value &value) { - auto propName = propNameId.utf8(runtime); - - return AudioNodeHostObject::set(runtime, propNameId, value); -} - -} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/HostObjects/AudioDestinationNodeHostObject.h b/packages/react-native-audio-api/common/cpp/HostObjects/AudioDestinationNodeHostObject.h index eb9bffe3..59afbf02 100644 --- a/packages/react-native-audio-api/common/cpp/HostObjects/AudioDestinationNodeHostObject.h +++ b/packages/react-native-audio-api/common/cpp/HostObjects/AudioDestinationNodeHostObject.h @@ -12,15 +12,7 @@ using namespace facebook; class AudioDestinationNodeHostObject : public AudioNodeHostObject { public: explicit AudioDestinationNodeHostObject( - const std::shared_ptr &node); - - 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; + const std::shared_ptr &node) + : AudioNodeHostObject(node) {} }; } // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/HostObjects/AudioNodeHostObject.cpp b/packages/react-native-audio-api/common/cpp/HostObjects/AudioNodeHostObject.cpp deleted file mode 100644 index 4cc355ab..00000000 --- a/packages/react-native-audio-api/common/cpp/HostObjects/AudioNodeHostObject.cpp +++ /dev/null @@ -1,98 +0,0 @@ -#include "AudioNodeHostObject.h" -#include "AudioContextHostObject.h" - -namespace audioapi { -using namespace facebook; - -AudioNodeHostObject::AudioNodeHostObject(const std::shared_ptr &node) - : node_(node) {} - -std::vector AudioNodeHostObject::getPropertyNames( - jsi::Runtime &runtime) { - std::vector propertyNames; - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "connect")); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "disconnect")); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "numberOfInputs")); - propertyNames.push_back( - jsi::PropNameID::forAscii(runtime, "numberOfOutputs")); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "channelCount")); - propertyNames.push_back( - jsi::PropNameID::forAscii(runtime, "channelCountMode")); - propertyNames.push_back( - jsi::PropNameID::forAscii(runtime, "channelInterpretation")); - return propertyNames; -} - -jsi::Value AudioNodeHostObject::get( - jsi::Runtime &runtime, - const jsi::PropNameID &propNameId) { - auto propName = propNameId.utf8(runtime); - - if (propName == "connect") { - return jsi::Function::createFromHostFunction( - runtime, - propNameId, - 1, - [this]( - jsi::Runtime &rt, - const jsi::Value &thisValue, - const jsi::Value *args, - size_t count) -> jsi::Value { - auto node = - args[0].getObject(rt).getHostObject(rt); - node_->connect(std::shared_ptr(node)->node_); - return jsi::Value::undefined(); - }); - } - - if (propName == "disconnect") { - return jsi::Function::createFromHostFunction( - runtime, - propNameId, - 1, - [this]( - jsi::Runtime &rt, - const jsi::Value &thisValue, - const jsi::Value *args, - size_t count) -> jsi::Value { - auto node = - args[0].getObject(rt).getHostObject(rt); - node_->disconnect(std::shared_ptr(node)->node_); - return jsi::Value::undefined(); - }); - } - - if (propName == "numberOfInputs") { - return {node_->getNumberOfInputs()}; - } - - if (propName == "numberOfOutputs") { - return {node_->getNumberOfOutputs()}; - } - - if (propName == "channelCount") { - return {node_->getChannelCount()}; - } - - if (propName == "channelCountMode") { - return jsi::String::createFromUtf8(runtime, node_->getChannelCountMode()); - } - - if (propName == "channelInterpretation") { - return jsi::String::createFromUtf8( - runtime, node_->getChannelInterpretation()); - } - - throw std::runtime_error("Not yet implemented!"); -} - -void AudioNodeHostObject::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/AudioNodeHostObject.h b/packages/react-native-audio-api/common/cpp/HostObjects/AudioNodeHostObject.h index 5be9b870..f3afda1f 100644 --- a/packages/react-native-audio-api/common/cpp/HostObjects/AudioNodeHostObject.h +++ b/packages/react-native-audio-api/common/cpp/HostObjects/AudioNodeHostObject.h @@ -4,23 +4,61 @@ #include #include +#include #include "AudioNode.h" namespace audioapi { using namespace facebook; -class AudioNodeHostObject : public jsi::HostObject { +class AudioNodeHostObject : public JsiHostObject { public: - explicit AudioNodeHostObject(const std::shared_ptr &node); + explicit AudioNodeHostObject(const std::shared_ptr &node) + : node_(node) { + addGetters( + JSI_EXPORT_PROPERTY_GETTER(AudioNodeHostObject, numberOfInputs), + JSI_EXPORT_PROPERTY_GETTER(AudioNodeHostObject, numberOfOutputs), + JSI_EXPORT_PROPERTY_GETTER(AudioNodeHostObject, channelCount), + JSI_EXPORT_PROPERTY_GETTER(AudioNodeHostObject, channelCountMode), + JSI_EXPORT_PROPERTY_GETTER(AudioNodeHostObject, channelInterpretation)); + addFunctions( + JSI_EXPORT_FUNCTION(AudioNodeHostObject, connect), + JSI_EXPORT_FUNCTION(AudioNodeHostObject, disconnect)); + } - jsi::Value get(jsi::Runtime &runtime, const jsi::PropNameID &name) override; + JSI_PROPERTY_GETTER(numberOfInputs) { + return {node_->getNumberOfInputs()}; + } - void set( - jsi::Runtime &runtime, - const jsi::PropNameID &name, - const jsi::Value &value) override; + JSI_PROPERTY_GETTER(numberOfOutputs) { + return {node_->getNumberOfOutputs()}; + } - std::vector getPropertyNames(jsi::Runtime &rt) override; + JSI_PROPERTY_GETTER(channelCount) { + return {node_->getChannelCount()}; + } + + JSI_PROPERTY_GETTER(channelCountMode) { + return jsi::String::createFromUtf8(runtime, node_->getChannelCountMode()); + } + + JSI_PROPERTY_GETTER(channelInterpretation) { + return jsi::String::createFromUtf8( + runtime, node_->getChannelInterpretation()); + } + + JSI_HOST_FUNCTION(connect) { + auto node = + args[0].getObject(runtime).getHostObject(runtime); + node_->connect(std::shared_ptr(node)->node_); + return jsi::Value::undefined(); + } + + JSI_HOST_FUNCTION(disconnect) { + auto node = + args[0].getObject(runtime).getHostObject(runtime); + node_->disconnect(std::shared_ptr(node)->node_); + return jsi::Value::undefined(); + } protected: std::shared_ptr node_; diff --git a/packages/react-native-audio-api/common/cpp/HostObjects/AudioParamHostObject.cpp b/packages/react-native-audio-api/common/cpp/HostObjects/AudioParamHostObject.cpp deleted file mode 100644 index c8936a1c..00000000 --- a/packages/react-native-audio-api/common/cpp/HostObjects/AudioParamHostObject.cpp +++ /dev/null @@ -1,115 +0,0 @@ -#include "AudioParamHostObject.h" - -namespace audioapi { -using namespace facebook; - -AudioParamHostObject::AudioParamHostObject( - const std::shared_ptr ¶m) - : param_(param) {} - -std::vector AudioParamHostObject::getPropertyNames( - jsi::Runtime &runtime) { - std::vector propertyNames; - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "value")); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "defaultValue")); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "minValue")); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "maxValue")); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "setValueAtTime")); - propertyNames.push_back( - jsi::PropNameID::forAscii(runtime, "linearRampToValueAtTime")); - propertyNames.push_back( - jsi::PropNameID::forAscii(runtime, "exponentialRampToValueAtTime")); - return propertyNames; -} - -jsi::Value AudioParamHostObject::get( - jsi::Runtime &runtime, - const jsi::PropNameID &propNameId) { - auto propName = propNameId.utf8(runtime); - - if (propName == "value") { - return {param_->getValue()}; - } - - if (propName == "defaultValue") { - return {param_->getDefaultValue()}; - } - - if (propName == "minValue") { - return {param_->getMinValue()}; - } - - if (propName == "maxValue") { - return {param_->getMaxValue()}; - } - - if (propName == "setValueAtTime") { - return jsi::Function::createFromHostFunction( - runtime, - propNameId, - 2, - [this]( - jsi::Runtime &rt, - const jsi::Value &thisVal, - const jsi::Value *args, - size_t count) -> jsi::Value { - auto value = static_cast(args[0].getNumber()); - double startTime = args[1].getNumber(); - param_->setValueAtTime(value, startTime); - return jsi::Value::undefined(); - }); - } - - if (propName == "linearRampToValueAtTime") { - return jsi::Function::createFromHostFunction( - runtime, - propNameId, - 2, - [this]( - jsi::Runtime &rt, - const jsi::Value &thisVal, - const jsi::Value *args, - size_t count) -> jsi::Value { - auto value = static_cast(args[0].getNumber()); - double endTime = args[1].getNumber(); - param_->linearRampToValueAtTime(value, endTime); - return jsi::Value::undefined(); - }); - } - - if (propName == "exponentialRampToValueAtTime") { - return jsi::Function::createFromHostFunction( - runtime, - propNameId, - 2, - [this]( - jsi::Runtime &rt, - const jsi::Value &thisVal, - const jsi::Value *args, - size_t count) -> jsi::Value { - auto value = static_cast(args[0].getNumber()); - double endTime = args[1].getNumber(); - param_->exponentialRampToValueAtTime(value, endTime); - return jsi::Value::undefined(); - }); - } - - throw std::runtime_error("Not yet implemented!"); -} - -void AudioParamHostObject::set( - jsi::Runtime &runtime, - const jsi::PropNameID &propNameId, - const jsi::Value &value) { - auto propName = propNameId.utf8(runtime); - - if (propName == "value") { - auto paramValue = static_cast(value.getNumber()); - param_->setValue(paramValue); - return; - } - - throw std::runtime_error("Not yet implemented!"); -} - -} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/HostObjects/AudioParamHostObject.h b/packages/react-native-audio-api/common/cpp/HostObjects/AudioParamHostObject.h index 904e70df..066f3a61 100644 --- a/packages/react-native-audio-api/common/cpp/HostObjects/AudioParamHostObject.h +++ b/packages/react-native-audio-api/common/cpp/HostObjects/AudioParamHostObject.h @@ -4,24 +4,71 @@ #include #include +#include #include "AudioParam.h" namespace audioapi { using namespace facebook; -class AudioParamHostObject : public jsi::HostObject { +class AudioParamHostObject : public JsiHostObject { public: - explicit AudioParamHostObject( - const std::shared_ptr &wrapparamper); + explicit AudioParamHostObject(const std::shared_ptr ¶m) + : param_(param) { + addGetters( + JSI_EXPORT_PROPERTY_GETTER(AudioParamHostObject, value), + JSI_EXPORT_PROPERTY_GETTER(AudioParamHostObject, defaultValue), + JSI_EXPORT_PROPERTY_GETTER(AudioParamHostObject, minValue), + JSI_EXPORT_PROPERTY_GETTER(AudioParamHostObject, maxValue)); - jsi::Value get(jsi::Runtime &runtime, const jsi::PropNameID &name) override; + addFunctions( + JSI_EXPORT_FUNCTION(AudioParamHostObject, setValueAtTime), + JSI_EXPORT_FUNCTION(AudioParamHostObject, linearRampToValueAtTime), + JSI_EXPORT_FUNCTION( + AudioParamHostObject, exponentialRampToValueAtTime)); - void set( - jsi::Runtime &runtime, - const jsi::PropNameID &name, - const jsi::Value &value) override; + addSetters(JSI_EXPORT_PROPERTY_SETTER(AudioParamHostObject, value)); + } - std::vector getPropertyNames(jsi::Runtime &rt) override; + JSI_PROPERTY_GETTER(value) { + return {param_->getValue()}; + } + + JSI_PROPERTY_GETTER(defaultValue) { + return {param_->getDefaultValue()}; + } + + JSI_PROPERTY_GETTER(minValue) { + return {param_->getMinValue()}; + } + + JSI_PROPERTY_GETTER(maxValue) { + return {param_->getMaxValue()}; + } + + JSI_HOST_FUNCTION(setValueAtTime) { + auto value = static_cast(args[0].getNumber()); + double startTime = args[1].getNumber(); + param_->setValueAtTime(value, startTime); + return jsi::Value::undefined(); + } + + JSI_HOST_FUNCTION(linearRampToValueAtTime) { + auto value = static_cast(args[0].getNumber()); + double endTime = args[1].getNumber(); + param_->linearRampToValueAtTime(value, endTime); + return jsi::Value::undefined(); + } + + JSI_HOST_FUNCTION(exponentialRampToValueAtTime) { + auto value = static_cast(args[0].getNumber()); + double endTime = args[1].getNumber(); + param_->exponentialRampToValueAtTime(value, endTime); + return jsi::Value::undefined(); + } + + JSI_PROPERTY_SETTER(value) { + param_->setValue(static_cast(value.getNumber())); + } private: std::shared_ptr param_; diff --git a/packages/react-native-audio-api/common/cpp/HostObjects/AudioScheduledSourceNodeHostObject.cpp b/packages/react-native-audio-api/common/cpp/HostObjects/AudioScheduledSourceNodeHostObject.cpp deleted file mode 100644 index ba769770..00000000 --- a/packages/react-native-audio-api/common/cpp/HostObjects/AudioScheduledSourceNodeHostObject.cpp +++ /dev/null @@ -1,70 +0,0 @@ -#include "AudioScheduledSourceNodeHostObject.h" - -namespace audioapi { -using namespace facebook; - -std::shared_ptr -AudioScheduledSourceNodeHostObject::getAudioScheduledSourceNodeFromAudioNode() { - return std::static_pointer_cast(node_); -} - -std::vector -AudioScheduledSourceNodeHostObject::getPropertyNames(jsi::Runtime &runtime) { - std::vector propertyNames = - AudioNodeHostObject::getPropertyNames(runtime); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "start")); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "stop")); - return propertyNames; -} - -jsi::Value AudioScheduledSourceNodeHostObject::get( - jsi::Runtime &runtime, - const jsi::PropNameID &propNameId) { - auto propName = propNameId.utf8(runtime); - - if (propName == "start") { - return jsi::Function::createFromHostFunction( - runtime, - propNameId, - 1, - [this]( - jsi::Runtime &rt, - const jsi::Value &thisValue, - const jsi::Value *args, - size_t count) -> jsi::Value { - auto time = args[0].getNumber(); - auto node = getAudioScheduledSourceNodeFromAudioNode(); - node->start(time); - return jsi::Value::undefined(); - }); - } - - if (propName == "stop") { - return jsi::Function::createFromHostFunction( - runtime, - propNameId, - 1, - [this]( - jsi::Runtime &rt, - const jsi::Value &thisValue, - const jsi::Value *args, - size_t count) -> jsi::Value { - auto time = args[0].getNumber(); - auto node = getAudioScheduledSourceNodeFromAudioNode(); - node->stop(time); - return jsi::Value::undefined(); - }); - } - - return AudioNodeHostObject::get(runtime, propNameId); -} - -void AudioScheduledSourceNodeHostObject::set( - jsi::Runtime &runtime, - const jsi::PropNameID &propNameId, - const jsi::Value &value) { - auto propName = propNameId.utf8(runtime); - - return AudioNodeHostObject::set(runtime, propNameId, value); -} -} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/HostObjects/AudioScheduledSourceNodeHostObject.h b/packages/react-native-audio-api/common/cpp/HostObjects/AudioScheduledSourceNodeHostObject.h index 19fe8514..f09c83e4 100644 --- a/packages/react-native-audio-api/common/cpp/HostObjects/AudioScheduledSourceNodeHostObject.h +++ b/packages/react-native-audio-api/common/cpp/HostObjects/AudioScheduledSourceNodeHostObject.h @@ -13,19 +13,26 @@ class AudioScheduledSourceNodeHostObject : public AudioNodeHostObject { public: explicit AudioScheduledSourceNodeHostObject( const std::shared_ptr &node) - : AudioNodeHostObject(node) {} - - 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; - - private: - std::shared_ptr - getAudioScheduledSourceNodeFromAudioNode(); + : AudioNodeHostObject(node) { + addFunctions( + JSI_EXPORT_FUNCTION(AudioScheduledSourceNodeHostObject, start), + JSI_EXPORT_FUNCTION(AudioScheduledSourceNodeHostObject, stop)); + } + + JSI_HOST_FUNCTION(start) { + auto time = args[0].getNumber(); + auto audioScheduleSourceNode = + std::static_pointer_cast(node_); + audioScheduleSourceNode->start(time); + return jsi::Value::undefined(); + } + + JSI_HOST_FUNCTION(stop) { + auto time = args[0].getNumber(); + auto audioScheduleSourceNode = + std::static_pointer_cast(node_); + audioScheduleSourceNode->stop(time); + return jsi::Value::undefined(); + } }; } // 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 deleted file mode 100644 index 5d7b8daa..00000000 --- a/packages/react-native-audio-api/common/cpp/HostObjects/BaseAudioContextHostObject.cpp +++ /dev/null @@ -1,249 +0,0 @@ -#include - -#include "BaseAudioContextHostObject.h" - -namespace audioapi { -using namespace facebook; - -BaseAudioContextHostObject::BaseAudioContextHostObject( - const std::shared_ptr &context, - const std::shared_ptr &promiseVendor) - : context_(context), promiseVendor_(promiseVendor) { - destination_ = std::make_shared( - context->getDestination()); -} - -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")); - propertyNames.push_back( - jsi::PropNameID::forUtf8(runtime, "createPeriodicWave")); - propertyNames.push_back( - jsi::PropNameID::forUtf8(runtime, "decodeAudioDataSource")); - 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, context_->getState()); - } - - if (propName == "sampleRate") { - return {context_->getSampleRate()}; - } - - if (propName == "currentTime") { - return {context_->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 = context_->createOscillator(); - auto oscillatorHostObject = - std::make_shared(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 = context_->createGain(); - auto gainHostObject = std::make_shared(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 = context_->createStereoPanner(); - auto stereoPannerHostObject = - std::make_shared(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 = context_->createBiquadFilter(); - auto biquadFilterHostObject = - std::make_shared(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 = context_->createBufferSource(); - auto bufferSourceHostObject = - std::make_shared(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 = - context_->createBuffer(numberOfChannels, length, sampleRate); - auto bufferHostObject = - std::make_shared(buffer); - return jsi::Object::createFromHostObject(runtime, bufferHostObject); - }); - } - - if (propName == "createPeriodicWave") { - 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 real = arguments[0].getObject(runtime).getArray(runtime); - auto imag = arguments[1].getObject(runtime).getArray(runtime); - auto disableNormalization = arguments[2].getBool(); - auto length = - static_cast(real.getProperty(runtime, "length").asNumber()); - - auto *realData = new float[length]; - auto *imagData = new float[length]; - - for (size_t i = 0; i < real.length(runtime); i++) { - realData[i] = static_cast( - real.getValueAtIndex(runtime, i).getNumber()); - } - for (size_t i = 0; i < imag.length(runtime); i++) { - realData[i] = static_cast( - imag.getValueAtIndex(runtime, i).getNumber()); - } - - auto periodicWave = context_->createPeriodicWave( - realData, imagData, disableNormalization, length); - auto periodicWaveHostObject = - std::make_shared(periodicWave); - return jsi::Object::createFromHostObject( - runtime, periodicWaveHostObject); - }); - } - - if (propName == "decodeAudioDataSource") { - auto decode = [this]( - jsi::Runtime &runtime, - const jsi::Value &, - const jsi::Value *arguments, - size_t count) -> jsi::Value { - auto sourcePath = arguments[0].getString(runtime).utf8(runtime); - - auto promise = promiseVendor_->createPromise( - [this, &runtime, sourcePath]( - std::shared_ptr promise) { - std::thread([this, - &runtime, - sourcePath, - promise = std::move(promise)]() { - auto results = context_->decodeAudioDataSource(sourcePath); - auto audioBufferHostObject = - std::make_shared(results); - - promise->resolve(jsi::Object::createFromHostObject( - runtime, audioBufferHostObject)); - }).detach(); - }); - - return promise; - }; - - return jsi::Function::createFromHostFunction( - runtime, propNameId, 1, decode); - } - - 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 f593eab8..802f9580 100644 --- a/packages/react-native-audio-api/common/cpp/HostObjects/BaseAudioContextHostObject.h +++ b/packages/react-native-audio-api/common/cpp/HostObjects/BaseAudioContextHostObject.h @@ -5,6 +5,7 @@ #include #include +#include #include "AudioBufferHostObject.h" #include "AudioBufferSourceNodeHostObject.h" #include "AudioDestinationNodeHostObject.h" @@ -19,24 +20,140 @@ namespace audioapi { using namespace facebook; -class BaseAudioContextHostObject : public jsi::HostObject { +class BaseAudioContextHostObject : public JsiHostObject { public: explicit BaseAudioContextHostObject( const std::shared_ptr &context, - const std::shared_ptr &promiseVendor); + const std::shared_ptr &promiseVendor) + : context_(context), promiseVendor_(promiseVendor) { + addGetters( + JSI_EXPORT_PROPERTY_GETTER(BaseAudioContextHostObject, destination), + JSI_EXPORT_PROPERTY_GETTER(BaseAudioContextHostObject, state), + JSI_EXPORT_PROPERTY_GETTER(BaseAudioContextHostObject, sampleRate), + JSI_EXPORT_PROPERTY_GETTER(BaseAudioContextHostObject, currentTime)); - jsi::Value get(jsi::Runtime &runtime, const jsi::PropNameID &name) override; + addFunctions( + JSI_EXPORT_FUNCTION(BaseAudioContextHostObject, createOscillator), + JSI_EXPORT_FUNCTION(BaseAudioContextHostObject, createGain), + JSI_EXPORT_FUNCTION(BaseAudioContextHostObject, createStereoPanner), + JSI_EXPORT_FUNCTION(BaseAudioContextHostObject, createBiquadFilter), + JSI_EXPORT_FUNCTION(BaseAudioContextHostObject, createBufferSource), + JSI_EXPORT_FUNCTION(BaseAudioContextHostObject, createBuffer), + JSI_EXPORT_FUNCTION(BaseAudioContextHostObject, createPeriodicWave), + JSI_EXPORT_FUNCTION(BaseAudioContextHostObject, decodeAudioDataSource)); + } - void set( - jsi::Runtime &runtime, - const jsi::PropNameID &name, - const jsi::Value &value) override; + JSI_PROPERTY_GETTER(destination) { + auto destination = std::make_shared( + context_->getDestination()); + return jsi::Object::createFromHostObject(runtime, destination); + } - std::vector getPropertyNames(jsi::Runtime &rt) override; + JSI_PROPERTY_GETTER(state) { + return jsi::String::createFromUtf8(runtime, context_->getState()); + } + + JSI_PROPERTY_GETTER(sampleRate) { + return {context_->getSampleRate()}; + } + + JSI_PROPERTY_GETTER(currentTime) { + return {context_->getCurrentTime()}; + } + + JSI_HOST_FUNCTION(createOscillator) { + auto oscillator = context_->createOscillator(); + auto oscillatorHostObject = + std::make_shared(oscillator); + return jsi::Object::createFromHostObject(runtime, oscillatorHostObject); + } + + JSI_HOST_FUNCTION(createGain) { + auto gain = context_->createGain(); + auto gainHostObject = std::make_shared(gain); + return jsi::Object::createFromHostObject(runtime, gainHostObject); + } + + JSI_HOST_FUNCTION(createStereoPanner) { + auto stereoPanner = context_->createStereoPanner(); + auto stereoPannerHostObject = + std::make_shared(stereoPanner); + return jsi::Object::createFromHostObject(runtime, stereoPannerHostObject); + } + + JSI_HOST_FUNCTION(createBiquadFilter) { + auto biquadFilter = context_->createBiquadFilter(); + auto biquadFilterHostObject = + std::make_shared(biquadFilter); + return jsi::Object::createFromHostObject(runtime, biquadFilterHostObject); + } + + JSI_HOST_FUNCTION(createBufferSource) { + auto bufferSource = context_->createBufferSource(); + auto bufferSourceHostObject = + std::make_shared(bufferSource); + return jsi::Object::createFromHostObject(runtime, bufferSourceHostObject); + } + + JSI_HOST_FUNCTION(createBuffer) { + auto numberOfChannels = static_cast(args[0].getNumber()); + auto length = static_cast(args[1].getNumber()); + auto sampleRate = static_cast(args[2].getNumber()); + auto buffer = context_->createBuffer(numberOfChannels, length, sampleRate); + auto bufferHostObject = std::make_shared(buffer); + return jsi::Object::createFromHostObject(runtime, bufferHostObject); + } + + JSI_HOST_FUNCTION(createPeriodicWave) { + auto real = args[0].getObject(runtime).getArray(runtime); + auto imag = args[1].getObject(runtime).getArray(runtime); + auto disableNormalization = args[2].getBool(); + auto length = + static_cast(real.getProperty(runtime, "length").asNumber()); + + auto *realData = new float[length]; + auto *imagData = new float[length]; + + for (size_t i = 0; i < real.length(runtime); i++) { + realData[i] = + static_cast(real.getValueAtIndex(runtime, i).getNumber()); + } + for (size_t i = 0; i < imag.length(runtime); i++) { + realData[i] = + static_cast(imag.getValueAtIndex(runtime, i).getNumber()); + } + + auto periodicWave = context_->createPeriodicWave( + realData, imagData, disableNormalization, length); + auto periodicWaveHostObject = + std::make_shared(periodicWave); + return jsi::Object::createFromHostObject(runtime, periodicWaveHostObject); + } + + JSI_HOST_FUNCTION(decodeAudioDataSource) { + auto sourcePath = args[0].getString(runtime).utf8(runtime); + + auto promise = promiseVendor_->createPromise( + [this, &runtime, sourcePath]( + std::shared_ptr promise) { + std::thread([this, + &runtime, + sourcePath, + promise = std::move(promise)]() { + auto results = context_->decodeAudioDataSource(sourcePath); + auto audioBufferHostObject = + std::make_shared(results); + + promise->resolve(jsi::Object::createFromHostObject( + runtime, audioBufferHostObject)); + }).detach(); + }); + + return promise; + } protected: std::shared_ptr context_; - std::shared_ptr destination_; std::shared_ptr promiseVendor_; }; } // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/HostObjects/BiquadFilterNodeHostObject.cpp b/packages/react-native-audio-api/common/cpp/HostObjects/BiquadFilterNodeHostObject.cpp deleted file mode 100644 index 4c32e432..00000000 --- a/packages/react-native-audio-api/common/cpp/HostObjects/BiquadFilterNodeHostObject.cpp +++ /dev/null @@ -1,122 +0,0 @@ -#include "BiquadFilterNodeHostObject.h" - -namespace audioapi { -using namespace facebook; - -std::shared_ptr -BiquadFilterNodeHostObject::getBiquadFilterNodeFromAudioNode() { - return std::static_pointer_cast(node_); -} - -BiquadFilterNodeHostObject::BiquadFilterNodeHostObject( - const std::shared_ptr &node) - : AudioNodeHostObject(node) { - frequencyParam_ = - std::make_shared(node->getFrequencyParam()); - detuneParam_ = std::make_shared(node->getDetuneParam()); - QParam_ = std::make_shared(node->getQParam()); - gainParam_ = std::make_shared(node->getGainParam()); -} - -std::vector BiquadFilterNodeHostObject::getPropertyNames( - jsi::Runtime &runtime) { - std::vector propertyNames = - AudioNodeHostObject::getPropertyNames(runtime); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "frequency")); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "detune")); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "Q")); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "gain")); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "type")); - propertyNames.push_back( - jsi::PropNameID::forAscii(runtime, "getFrequencyResponse")); - return propertyNames; -} - -jsi::Value BiquadFilterNodeHostObject::get( - jsi::Runtime &runtime, - const jsi::PropNameID &propNameId) { - auto propName = propNameId.utf8(runtime); - - if (propName == "frequency") { - return jsi::Object::createFromHostObject(runtime, frequencyParam_); - } - - if (propName == "detune") { - return jsi::Object::createFromHostObject(runtime, detuneParam_); - } - - if (propName == "Q") { - return jsi::Object::createFromHostObject(runtime, QParam_); - } - - if (propName == "gain") { - return jsi::Object::createFromHostObject(runtime, gainParam_); - } - - if (propName == "type") { - auto node = getBiquadFilterNodeFromAudioNode(); - auto waveType = node->getType(); - return jsi::String::createFromUtf8(runtime, waveType); - } - - if (propName == "getFrequencyResponse") { - return jsi::Function::createFromHostFunction( - runtime, - propNameId, - 3, - [this]( - jsi::Runtime &rt, - const jsi::Value &thisVal, - const jsi::Value *args, - size_t count) -> jsi::Value { - auto frequencyArray = args[0].getObject(rt).asArray(rt); - auto magResponseOut = args[1].getObject(rt).asArray(rt); - auto phaseResponseOut = args[2].getObject(rt).asArray(rt); - - std::vector frequencyArrayVector(frequencyArray.length(rt)); - for (size_t i = 0; i < frequencyArray.length(rt); i++) { - frequencyArrayVector[i] = static_cast( - frequencyArray.getValueAtIndex(rt, i).getNumber()); - } - - std::vector magResponseOutVector(magResponseOut.length(rt)); - std::vector phaseResponseOutVector( - phaseResponseOut.length(rt)); - - auto node = getBiquadFilterNodeFromAudioNode(); - node->getFrequencyResponse( - frequencyArrayVector, - magResponseOutVector, - phaseResponseOutVector); - - for (size_t i = 0; i < magResponseOutVector.size(); i++) { - magResponseOut.setValueAtIndex(rt, i, magResponseOutVector[i]); - } - - for (size_t i = 0; i < phaseResponseOutVector.size(); i++) { - phaseResponseOut.setValueAtIndex(rt, i, phaseResponseOutVector[i]); - } - - return jsi::Value::undefined(); - }); - } - - return AudioNodeHostObject::get(runtime, propNameId); -} - -void BiquadFilterNodeHostObject::set( - jsi::Runtime &runtime, - const jsi::PropNameID &propNameId, - const jsi::Value &value) { - auto propName = propNameId.utf8(runtime); - - if (propName == "type") { - std::string filterType = value.getString(runtime).utf8(runtime); - auto node = getBiquadFilterNodeFromAudioNode(); - node->setType(filterType); - return; - } - - return AudioNodeHostObject::set(runtime, propNameId, value); -} -} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/HostObjects/BiquadFilterNodeHostObject.h b/packages/react-native-audio-api/common/cpp/HostObjects/BiquadFilterNodeHostObject.h index 895fc543..53f8cf98 100644 --- a/packages/react-native-audio-api/common/cpp/HostObjects/BiquadFilterNodeHostObject.h +++ b/packages/react-native-audio-api/common/cpp/HostObjects/BiquadFilterNodeHostObject.h @@ -14,23 +14,87 @@ using namespace facebook; class BiquadFilterNodeHostObject : public AudioNodeHostObject { public: explicit BiquadFilterNodeHostObject( - const std::shared_ptr &node); + const std::shared_ptr &node) + : AudioNodeHostObject(node) { + addGetters( + JSI_EXPORT_PROPERTY_GETTER(BiquadFilterNodeHostObject, frequency), + JSI_EXPORT_PROPERTY_GETTER(BiquadFilterNodeHostObject, detune), + JSI_EXPORT_PROPERTY_GETTER(BiquadFilterNodeHostObject, Q), + JSI_EXPORT_PROPERTY_GETTER(BiquadFilterNodeHostObject, gain), + JSI_EXPORT_PROPERTY_GETTER(BiquadFilterNodeHostObject, type)); - jsi::Value get(jsi::Runtime &runtime, const jsi::PropNameID &name) override; + addFunctions( + JSI_EXPORT_FUNCTION(BiquadFilterNodeHostObject, getFrequencyResponse)); - void set( - jsi::Runtime &runtime, - const jsi::PropNameID &name, - const jsi::Value &value) override; + addSetters(JSI_EXPORT_PROPERTY_SETTER(BiquadFilterNodeHostObject, type)); + } - std::vector getPropertyNames(jsi::Runtime &rt) override; + JSI_PROPERTY_GETTER(frequency) { + auto biquadFilterNode = std::static_pointer_cast(node_); + auto frequencyParam_ = std::make_shared( + biquadFilterNode->getFrequencyParam()); + return jsi::Object::createFromHostObject(runtime, frequencyParam_); + } - private: - std::shared_ptr frequencyParam_; - std::shared_ptr detuneParam_; - std::shared_ptr QParam_; - std::shared_ptr gainParam_; + JSI_PROPERTY_GETTER(detune) { + auto biquadFilterNode = std::static_pointer_cast(node_); + auto detuneParam_ = std::make_shared( + biquadFilterNode->getDetuneParam()); + return jsi::Object::createFromHostObject(runtime, detuneParam_); + } - std::shared_ptr getBiquadFilterNodeFromAudioNode(); + JSI_PROPERTY_GETTER(Q) { + auto biquadFilterNode = std::static_pointer_cast(node_); + auto QParam_ = + std::make_shared(biquadFilterNode->getQParam()); + return jsi::Object::createFromHostObject(runtime, QParam_); + } + + JSI_PROPERTY_GETTER(gain) { + auto biquadFilterNode = std::static_pointer_cast(node_); + auto gainParam_ = std::make_shared( + biquadFilterNode->getGainParam()); + return jsi::Object::createFromHostObject(runtime, gainParam_); + } + + JSI_PROPERTY_GETTER(type) { + auto biquadFilterNode = std::static_pointer_cast(node_); + auto type = biquadFilterNode->getType(); + return jsi::String::createFromUtf8(runtime, type); + } + + JSI_HOST_FUNCTION(getFrequencyResponse) { + auto frequencyArray = args[0].getObject(runtime).asArray(runtime); + auto magResponseOut = args[1].getObject(runtime).asArray(runtime); + auto phaseResponseOut = args[2].getObject(runtime).asArray(runtime); + + std::vector frequencyArrayVector(frequencyArray.length(runtime)); + for (size_t i = 0; i < frequencyArray.length(runtime); i++) { + frequencyArrayVector[i] = static_cast( + frequencyArray.getValueAtIndex(runtime, i).getNumber()); + } + + std::vector magResponseOutVector(magResponseOut.length(runtime)); + std::vector phaseResponseOutVector(phaseResponseOut.length(runtime)); + + auto biquadFilterNode = std::static_pointer_cast(node_); + biquadFilterNode->getFrequencyResponse( + frequencyArrayVector, magResponseOutVector, phaseResponseOutVector); + + for (size_t i = 0; i < magResponseOutVector.size(); i++) { + magResponseOut.setValueAtIndex(runtime, i, magResponseOutVector[i]); + } + + for (size_t i = 0; i < phaseResponseOutVector.size(); i++) { + phaseResponseOut.setValueAtIndex(runtime, i, phaseResponseOutVector[i]); + } + + return jsi::Value::undefined(); + } + + JSI_PROPERTY_SETTER(type) { + auto biquadFilterNode = std::static_pointer_cast(node_); + biquadFilterNode->setType(value.getString(runtime).utf8(runtime)); + } }; } // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/HostObjects/GainNodeHostObject.cpp b/packages/react-native-audio-api/common/cpp/HostObjects/GainNodeHostObject.cpp deleted file mode 100644 index 5e5eb6a1..00000000 --- a/packages/react-native-audio-api/common/cpp/HostObjects/GainNodeHostObject.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include "GainNodeHostObject.h" - -namespace audioapi { -using namespace facebook; - -GainNodeHostObject::GainNodeHostObject(const std::shared_ptr &node) - : AudioNodeHostObject(node) { - auto gainParamWrapper = node->getGainParam(); - gainParam_ = std::make_shared(gainParamWrapper); -} - -std::vector GainNodeHostObject::getPropertyNames( - jsi::Runtime &runtime) { - std::vector propertyNames = - AudioNodeHostObject::getPropertyNames(runtime); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "gain")); - return propertyNames; -} - -jsi::Value GainNodeHostObject::get( - jsi::Runtime &runtime, - const jsi::PropNameID &propNameId) { - auto propName = propNameId.utf8(runtime); - - if (propName == "gain") { - return jsi::Object::createFromHostObject(runtime, gainParam_); - } - - return AudioNodeHostObject::get(runtime, propNameId); -} - -void GainNodeHostObject::set( - jsi::Runtime &runtime, - const jsi::PropNameID &propNameId, - const jsi::Value &value) { - auto propName = propNameId.utf8(runtime); - - return AudioNodeHostObject::set(runtime, propNameId, value); -} -} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/HostObjects/GainNodeHostObject.h b/packages/react-native-audio-api/common/cpp/HostObjects/GainNodeHostObject.h index 73c4cddf..043d7771 100644 --- a/packages/react-native-audio-api/common/cpp/HostObjects/GainNodeHostObject.h +++ b/packages/react-native-audio-api/common/cpp/HostObjects/GainNodeHostObject.h @@ -11,17 +11,17 @@ namespace audioapi { using namespace facebook; class GainNodeHostObject : public AudioNodeHostObject { - protected: - std::shared_ptr gainParam_; - public: - explicit GainNodeHostObject(const std::shared_ptr &node); + explicit GainNodeHostObject(const std::shared_ptr &node) + : AudioNodeHostObject(node) { + addGetters(JSI_EXPORT_PROPERTY_GETTER(GainNodeHostObject, gain)); + } - 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; + JSI_PROPERTY_GETTER(gain) { + auto gainNode = std::static_pointer_cast(node_); + auto gainParam_ = + std::make_shared(gainNode->getGainParam()); + return jsi::Object::createFromHostObject(runtime, gainParam_); + } }; } // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/HostObjects/OscillatorNodeHostObject.cpp b/packages/react-native-audio-api/common/cpp/HostObjects/OscillatorNodeHostObject.cpp deleted file mode 100644 index ab49f214..00000000 --- a/packages/react-native-audio-api/common/cpp/HostObjects/OscillatorNodeHostObject.cpp +++ /dev/null @@ -1,88 +0,0 @@ -#include "OscillatorNodeHostObject.h" - -namespace audioapi { -using namespace facebook; - -std::shared_ptr -OscillatorNodeHostObject::getOscillatorNodeFromAudioNode() { - return std::static_pointer_cast(node_); -} - -OscillatorNodeHostObject::OscillatorNodeHostObject( - const std::shared_ptr &node) - : AudioScheduledSourceNodeHostObject(node) { - auto frequencyParam = node->getFrequencyParam(); - frequencyParam_ = std::make_shared(frequencyParam); - auto detuneParam = node->getDetuneParam(); - detuneParam_ = std::make_shared(detuneParam); -} - -std::vector OscillatorNodeHostObject::getPropertyNames( - jsi::Runtime &runtime) { - std::vector propertyNames = - AudioScheduledSourceNodeHostObject::getPropertyNames(runtime); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "frequency")); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "detune")); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "type")); - propertyNames.push_back( - jsi::PropNameID::forAscii(runtime, "setPeriodicWave")); - return propertyNames; -} - -jsi::Value OscillatorNodeHostObject::get( - jsi::Runtime &runtime, - const jsi::PropNameID &propNameId) { - auto propName = propNameId.utf8(runtime); - - if (propName == "frequency") { - return jsi::Object::createFromHostObject(runtime, frequencyParam_); - } - - if (propName == "detune") { - return jsi::Object::createFromHostObject(runtime, detuneParam_); - } - - if (propName == "type") { - auto node = getOscillatorNodeFromAudioNode(); - auto waveType = node->getType(); - return jsi::String::createFromUtf8(runtime, waveType); - } - - if (propName == "setPeriodicWave") { - return jsi::Function::createFromHostFunction( - runtime, - propNameId, - 1, - [this]( - jsi::Runtime &rt, - const jsi::Value &thisVal, - const jsi::Value *args, - size_t count) -> jsi::Value { - auto node = getOscillatorNodeFromAudioNode(); - auto periodicWaveHostObject = - args[0].getObject(rt).asHostObject(rt); - - node->setPeriodicWave(periodicWaveHostObject->periodicWave_); - return jsi::Value::undefined(); - }); - } - - return AudioScheduledSourceNodeHostObject::get(runtime, propNameId); -} - -void OscillatorNodeHostObject::set( - jsi::Runtime &runtime, - const jsi::PropNameID &propNameId, - const jsi::Value &value) { - auto propName = propNameId.utf8(runtime); - - if (propName == "type") { - std::string waveType = value.getString(runtime).utf8(runtime); - auto node = getOscillatorNodeFromAudioNode(); - node->setType(waveType); - return; - } - - return AudioScheduledSourceNodeHostObject::set(runtime, propNameId, value); -} -} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/HostObjects/OscillatorNodeHostObject.h b/packages/react-native-audio-api/common/cpp/HostObjects/OscillatorNodeHostObject.h index ffb5b89d..de5b4f11 100644 --- a/packages/react-native-audio-api/common/cpp/HostObjects/OscillatorNodeHostObject.h +++ b/packages/react-native-audio-api/common/cpp/HostObjects/OscillatorNodeHostObject.h @@ -14,22 +14,51 @@ using namespace facebook; class OscillatorNodeHostObject : public AudioScheduledSourceNodeHostObject { public: - explicit OscillatorNodeHostObject( - const std::shared_ptr &node); + explicit OscillatorNodeHostObject(const std::shared_ptr &node) + : AudioScheduledSourceNodeHostObject(node) { + addGetters( + JSI_EXPORT_PROPERTY_GETTER(OscillatorNodeHostObject, frequency), + JSI_EXPORT_PROPERTY_GETTER(OscillatorNodeHostObject, detune), + JSI_EXPORT_PROPERTY_GETTER(OscillatorNodeHostObject, type)); - jsi::Value get(jsi::Runtime &runtime, const jsi::PropNameID &name) override; + addFunctions( + JSI_EXPORT_FUNCTION(OscillatorNodeHostObject, setPeriodicWave)); - void set( - jsi::Runtime &runtime, - const jsi::PropNameID &name, - const jsi::Value &value) override; + addSetters(JSI_EXPORT_PROPERTY_SETTER(OscillatorNodeHostObject, type)); + } - std::vector getPropertyNames(jsi::Runtime &rt) override; + JSI_PROPERTY_GETTER(frequency) { + auto oscillatorNode = std::static_pointer_cast(node_); + auto frequencyParam_ = std::make_shared( + oscillatorNode->getFrequencyParam()); + return jsi::Object::createFromHostObject(runtime, frequencyParam_); + } - private: - std::shared_ptr frequencyParam_; - std::shared_ptr detuneParam_; + JSI_PROPERTY_GETTER(detune) { + auto oscillatorNode = std::static_pointer_cast(node_); + auto detuneParam_ = std::make_shared( + oscillatorNode->getDetuneParam()); + return jsi::Object::createFromHostObject(runtime, detuneParam_); + } - std::shared_ptr getOscillatorNodeFromAudioNode(); + JSI_PROPERTY_GETTER(type) { + auto oscillatorNode = std::static_pointer_cast(node_); + auto waveType = oscillatorNode->getType(); + return jsi::String::createFromUtf8(runtime, waveType); + } + + JSI_HOST_FUNCTION(setPeriodicWave) { + auto oscillatorNode = std::static_pointer_cast(node_); + auto periodicWave = + args[0].getObject(runtime).getHostObject( + runtime); + oscillatorNode->setPeriodicWave(periodicWave->periodicWave_); + return jsi::Value::undefined(); + } + + JSI_PROPERTY_SETTER(type) { + auto oscillatorNode = std::static_pointer_cast(node_); + oscillatorNode->setType(value.getString(runtime).utf8(runtime)); + } }; } // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/HostObjects/PeriodicWaveHostObject.cpp b/packages/react-native-audio-api/common/cpp/HostObjects/PeriodicWaveHostObject.cpp deleted file mode 100644 index 1ee90e7b..00000000 --- a/packages/react-native-audio-api/common/cpp/HostObjects/PeriodicWaveHostObject.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include "PeriodicWaveHostObject.h" - -namespace audioapi { -using namespace facebook; - -PeriodicWaveHostObject::PeriodicWaveHostObject( - const std::shared_ptr &periodicWave) - : periodicWave_(periodicWave) {} - -std::vector PeriodicWaveHostObject::getPropertyNames( - jsi::Runtime &runtime) { - std::vector propertyNames; - return propertyNames; -} - -jsi::Value PeriodicWaveHostObject::get( - jsi::Runtime &runtime, - const jsi::PropNameID &propNameId) { - auto propName = propNameId.utf8(runtime); - - throw std::runtime_error("Not yet implemented!"); -} - -void PeriodicWaveHostObject::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/PeriodicWaveHostObject.h b/packages/react-native-audio-api/common/cpp/HostObjects/PeriodicWaveHostObject.h index f70a7c1b..31f29af2 100644 --- a/packages/react-native-audio-api/common/cpp/HostObjects/PeriodicWaveHostObject.h +++ b/packages/react-native-audio-api/common/cpp/HostObjects/PeriodicWaveHostObject.h @@ -14,15 +14,7 @@ class PeriodicWaveHostObject : public jsi::HostObject { std::shared_ptr periodicWave_; explicit PeriodicWaveHostObject( - const std::shared_ptr &periodicWave); - - 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; + const std::shared_ptr &periodicWave) + : periodicWave_(periodicWave) {} }; } // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/HostObjects/StereoPannerNodeHostObject.cpp b/packages/react-native-audio-api/common/cpp/HostObjects/StereoPannerNodeHostObject.cpp deleted file mode 100644 index 3bae2f73..00000000 --- a/packages/react-native-audio-api/common/cpp/HostObjects/StereoPannerNodeHostObject.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include "StereoPannerNodeHostObject.h" - -namespace audioapi { -using namespace facebook; - -StereoPannerNodeHostObject::StereoPannerNodeHostObject( - const std::shared_ptr &node) - : AudioNodeHostObject(node) { - auto panParam = node->getPanParam(); - panParam_ = std::make_shared(panParam); -} - -std::vector StereoPannerNodeHostObject::getPropertyNames( - jsi::Runtime &runtime) { - std::vector propertyNames = - AudioNodeHostObject::getPropertyNames(runtime); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "pan")); - return propertyNames; -} - -jsi::Value StereoPannerNodeHostObject::get( - jsi::Runtime &runtime, - const jsi::PropNameID &propNameId) { - auto propName = propNameId.utf8(runtime); - - if (propName == "pan") { - return jsi::Object::createFromHostObject(runtime, panParam_); - } - - return AudioNodeHostObject::get(runtime, propNameId); -} - -void StereoPannerNodeHostObject::set( - jsi::Runtime &runtime, - const jsi::PropNameID &propNameId, - const jsi::Value &value) { - auto propName = propNameId.utf8(runtime); - - return AudioNodeHostObject::set(runtime, propNameId, value); -} -} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/HostObjects/StereoPannerNodeHostObject.h b/packages/react-native-audio-api/common/cpp/HostObjects/StereoPannerNodeHostObject.h index 6fd4a470..9f594266 100644 --- a/packages/react-native-audio-api/common/cpp/HostObjects/StereoPannerNodeHostObject.h +++ b/packages/react-native-audio-api/common/cpp/HostObjects/StereoPannerNodeHostObject.h @@ -14,18 +14,16 @@ using namespace facebook; class StereoPannerNodeHostObject : public AudioNodeHostObject { public: explicit StereoPannerNodeHostObject( - const std::shared_ptr &node); - - 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; - - private: - std::shared_ptr panParam_; + const std::shared_ptr &node) + : AudioNodeHostObject(node) { + addGetters(JSI_EXPORT_PROPERTY_GETTER(StereoPannerNodeHostObject, pan)); + } + + JSI_PROPERTY_GETTER(pan) { + auto stereoPannerNode = std::static_pointer_cast(node_); + auto panParam_ = + std::make_shared(stereoPannerNode->getPanParam()); + return jsi::Object::createFromHostObject(runtime, panParam_); + } }; } // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/utils/JsiPromise.h b/packages/react-native-audio-api/common/cpp/utils/JsiPromise.h index c1a84ed1..1bc78834 100644 --- a/packages/react-native-audio-api/common/cpp/utils/JsiPromise.h +++ b/packages/react-native-audio-api/common/cpp/utils/JsiPromise.h @@ -34,7 +34,7 @@ class PromiseVendor { public: PromiseVendor( jsi::Runtime *runtime, - std::shared_ptr callInvoker) + const std::shared_ptr &callInvoker) : _runtime(runtime), _callInvoker(callInvoker) {} public: