diff --git a/apps/fabric-example/ios/Podfile.lock b/apps/fabric-example/ios/Podfile.lock index e3a5a319..6f04e1a0 100644 --- a/apps/fabric-example/ios/Podfile.lock +++ b/apps/fabric-example/ios/Podfile.lock @@ -2157,7 +2157,7 @@ SPEC CHECKSUMS: RNReanimated: 006a5d3961bf09c1e96d62ed436e02b2e43b89bb RNScreens: e389d6a6a66a4f0d3662924ecae803073ccce8ec SocketRocket: d4aabe649be1e368d1318fdf28a022d714d65748 - Yoga: 1d66db49f38fd9e576a1d7c3b081e46ab4c28b9e + Yoga: f8ec45ce98bba1bc93dd28f2ee37215180e6d2b6 PODFILE CHECKSUM: d4b0da00bac00e3eab4bc0a377fa848fc7bb5276 diff --git a/packages/react-native-audio-api/android/CMakeLists.txt b/packages/react-native-audio-api/android/CMakeLists.txt index 47cce158..f15a8abd 100644 --- a/packages/react-native-audio-api/android/CMakeLists.txt +++ b/packages/react-native-audio-api/android/CMakeLists.txt @@ -21,12 +21,12 @@ file(GLOB_RECURSE SOURCE_FILES "../common/cpp/*.h" "../common/cpp/core/*.cpp" "../common/cpp/core/*.h" - "../common/cpp/wrappers/*.cpp" - "../common/cpp/wrappers/*.h" "../common/cpp/HostObjects/*.cpp" "../common/cpp/HostObjects/*.h" "../common/cpp/utils/*.cpp" "../common/cpp/utils/*.h" + "../common/cpp/jsi/*.h" + "../common/cpp/jsi/*.cpp" "../common/cpp/types/*.h" ) diff --git a/packages/react-native-audio-api/android/src/main/cpp/AudioAPIInstaller/AudioAPIInstaller.cpp b/packages/react-native-audio-api/android/src/main/cpp/AudioAPIInstaller/AudioAPIInstaller.cpp index d2951c5b..4aba5426 100644 --- a/packages/react-native-audio-api/android/src/main/cpp/AudioAPIInstaller/AudioAPIInstaller.cpp +++ b/packages/react-native-audio-api/android/src/main/cpp/AudioAPIInstaller/AudioAPIInstaller.cpp @@ -13,14 +13,8 @@ AudioAPIInstaller::AudioAPIInstaller( jsCallInvoker_(jsCallInvoker) {} void AudioAPIInstaller::install() { - auto audioAPIInstallerWrapper = - std::make_shared(this); - AudioAPIInstallerHostObject::createAndInstallFromWrapper( - audioAPIInstallerWrapper, rnRuntime_, jsCallInvoker_); + auto hostObject = + std::make_shared(rnRuntime_, jsCallInvoker_); + hostObject->install(); } - -std::shared_ptr AudioAPIInstaller::createAudioContext() { - return std::make_shared(); -} - } // namespace audioapi diff --git a/packages/react-native-audio-api/android/src/main/cpp/AudioAPIInstaller/AudioAPIInstaller.h b/packages/react-native-audio-api/android/src/main/cpp/AudioAPIInstaller/AudioAPIInstaller.h index e42abaec..9399232b 100644 --- a/packages/react-native-audio-api/android/src/main/cpp/AudioAPIInstaller/AudioAPIInstaller.h +++ b/packages/react-native-audio-api/android/src/main/cpp/AudioAPIInstaller/AudioAPIInstaller.h @@ -8,7 +8,6 @@ #include #include "AudioAPIInstallerHostObject.h" -#include "AudioAPIInstallerWrapper.h" #include "AudioContext.h" namespace audioapi { @@ -38,7 +37,6 @@ class AudioAPIInstaller : public jni::HybridClass { }); } - std::shared_ptr createAudioContext(); void install(); private: 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 0b08d710..00000000 --- a/packages/react-native-audio-api/common/cpp/AudioAPIInstaller/AudioAPIInstallerHostObject.cpp +++ /dev/null @@ -1,58 +0,0 @@ -#include "AudioAPIInstallerHostObject.h" - -namespace audioapi { -using namespace facebook; - -AudioAPIInstallerHostObject::AudioAPIInstallerHostObject( - const std::shared_ptr &wrapper, - jsi::Runtime *runtime, - const std::shared_ptr &jsInvoker) - : wrapper_(wrapper) { - 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 = wrapper_->createAudioContext(); - auto audioContextHostObject = - AudioContextHostObject::createFromWrapper( - 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 e6526ddc..576c4842 100644 --- a/packages/react-native-audio-api/common/cpp/AudioAPIInstaller/AudioAPIInstallerHostObject.h +++ b/packages/react-native-audio-api/common/cpp/AudioAPIInstaller/AudioAPIInstallerHostObject.h @@ -6,46 +6,43 @@ #include #include -#include "AudioAPIInstallerWrapper.h" +#include +#include #include "AudioContextHostObject.h" -#include "JsiPromise.h" namespace audioapi { using namespace facebook; -class AudioAPIInstallerWrapper; - -class AudioAPIInstallerHostObject : public jsi::HostObject { +class AudioAPIInstallerHostObject + : public JsiHostObject, + public std::enable_shared_from_this { public: explicit AudioAPIInstallerHostObject( - const std::shared_ptr &wrapper, jsi::Runtime *runtime, - const std::shared_ptr &jsInvoker); - -#ifdef ANDROID - static void createAndInstallFromWrapper( - const std::shared_ptr &wrapper, - jsi::Runtime *rnRuntime, - const std::shared_ptr &jsInvoker) { - auto hostObject = std::make_shared( - wrapper, rnRuntime, jsInvoker); - auto object = jsi::Object::createFromHostObject(*rnRuntime, hostObject); - rnRuntime->global().setProperty( - *rnRuntime, "__AudioAPIInstaller", std::move(object)); - } -#endif + const std::shared_ptr &jsInvoker) + : rnRuntime_(runtime) { + promiseVendor_ = std::make_shared(runtime, jsInvoker); - jsi::Value get(jsi::Runtime &runtime, const jsi::PropNameID &name) override; + addFunctions( + JSI_EXPORT_FUNCTION(AudioAPIInstallerHostObject, createAudioContext)); + } - void set( - jsi::Runtime &runtime, - const jsi::PropNameID &name, - const jsi::Value &value) override; + void install() { + auto object = + jsi::Object::createFromHostObject(*rnRuntime_, shared_from_this()); + rnRuntime_->global().setProperty( + *rnRuntime_, "__AudioAPIInstaller", std::move(object)); + } - std::vector getPropertyNames(jsi::Runtime &rt) override; + 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 wrapper_; - std::shared_ptr promiseVendor_; + std::shared_ptr promiseVendor_; + jsi::Runtime *rnRuntime_; }; } // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/AudioAPIInstaller/AudioAPIInstallerWrapper.h b/packages/react-native-audio-api/common/cpp/AudioAPIInstaller/AudioAPIInstallerWrapper.h deleted file mode 100644 index bd64076f..00000000 --- a/packages/react-native-audio-api/common/cpp/AudioAPIInstaller/AudioAPIInstallerWrapper.h +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once - -#include -#include -#include - -#include "AudioContextWrapper.h" - -#ifdef ANDROID -#include "AudioAPIInstaller.h" -#else -#include "AudioContext.h" -#endif - -namespace audioapi { - -#ifdef ANDROID -class AudioAPIInstaller; -#endif - -class AudioAPIInstallerWrapper { -#ifdef ANDROID - - public: - explicit AudioAPIInstallerWrapper(AudioAPIInstaller *audioAPIInstaller); - - private: - AudioAPIInstaller *audioAPIInstaller_; -#else - - public: - AudioAPIInstallerWrapper() = default; -#endif - - public: - std::shared_ptr createAudioContext() const; -}; -} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/AudioAPIInstaller/android/AudioAPIInstallerWrapper.cpp b/packages/react-native-audio-api/common/cpp/AudioAPIInstaller/android/AudioAPIInstallerWrapper.cpp deleted file mode 100644 index 4805e934..00000000 --- a/packages/react-native-audio-api/common/cpp/AudioAPIInstaller/android/AudioAPIInstallerWrapper.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#ifdef ANDROID -#include "AudioAPIInstallerWrapper.h" - -namespace audioapi { - -AudioAPIInstallerWrapper::AudioAPIInstallerWrapper( - AudioAPIInstaller *audioAPIInstaller) - : audioAPIInstaller_(audioAPIInstaller) {} - -std::shared_ptr -AudioAPIInstallerWrapper::createAudioContext() const { - auto audioContext = audioAPIInstaller_->createAudioContext(); - return std::make_shared(audioContext); -} -} // namespace audioapi -#endif diff --git a/packages/react-native-audio-api/common/cpp/AudioAPIInstaller/ios/AudioAPIInstallerWrapper.cpp b/packages/react-native-audio-api/common/cpp/AudioAPIInstaller/ios/AudioAPIInstallerWrapper.cpp deleted file mode 100644 index ceb8a88c..00000000 --- a/packages/react-native-audio-api/common/cpp/AudioAPIInstaller/ios/AudioAPIInstallerWrapper.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef ANDROID -#include "AudioAPIInstallerWrapper.h" - -namespace audioapi { - -std::shared_ptr -AudioAPIInstallerWrapper::createAudioContext() const { - auto audioContext = std::make_shared(); - return std::make_shared(audioContext); -} -} // namespace audioapi -#endif 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 5629661a..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 &wrapper) - : wrapper_(wrapper) {} - -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 {wrapper_->getSampleRate()}; - } - - if (propName == "length") { - return {wrapper_->getLength()}; - } - - if (propName == "duration") { - return {wrapper_->getDuration()}; - } - - if (propName == "numberOfChannels") { - return {wrapper_->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 = wrapper_->getChannelData(channel); - - auto array = jsi::Array(rt, wrapper_->getLength()); - for (int i = 0; i < wrapper_->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]; - - wrapper_->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()); - } - - wrapper_->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 0672c783..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,30 +4,96 @@ #include #include -#include "AudioBufferWrapper.h" +#include +#include "AudioBuffer.h" namespace audioapi { using namespace facebook; -class AudioBufferHostObject : public jsi::HostObject { +class AudioBufferHostObject : public JsiHostObject { public: - std::shared_ptr wrapper_; + std::shared_ptr audioBuffer_; explicit AudioBufferHostObject( - const std::shared_ptr &wrapper); + 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)); + } + + JSI_PROPERTY_GETTER(sampleRate) { + return {audioBuffer_->getSampleRate()}; + } + + 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]; - void set( - jsi::Runtime &runtime, - const jsi::PropNameID &name, - const jsi::Value &value) override; + for (int i = 0; i < sourceLength; i++) { + sourceData[i] = + static_cast(source.getValueAtIndex(runtime, i).getNumber()); + } - std::vector getPropertyNames(jsi::Runtime &rt) override; + audioBuffer_->copyToChannel( + sourceData, sourceLength, channelNumber, startInChannel); - static std::shared_ptr createFromWrapper( - const std::shared_ptr &wrapper) { - return std::make_shared(wrapper); + 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 73f9e6ca..00000000 --- a/packages/react-native-audio-api/common/cpp/HostObjects/AudioBufferSourceNodeHostObject.cpp +++ /dev/null @@ -1,121 +0,0 @@ -#include "AudioBufferSourceNodeHostObject.h" -#include "AudioParamHostObject.h" - -namespace audioapi { -using namespace facebook; - -AudioBufferSourceNodeHostObject::AudioBufferSourceNodeHostObject( - const std::shared_ptr &wrapper) - : AudioScheduledSourceNodeHostObject(wrapper) { - auto detuneParam = wrapper->getDetuneParam(); - detuneParam_ = AudioParamHostObject::createFromWrapper(detuneParam); - auto playbackRateParam = wrapper->getPlaybackRateParam(); - playbackRateParam_ = AudioParamHostObject::createFromWrapper(playbackRateParam); -} - -std::shared_ptr AudioBufferSourceNodeHostObject:: - getAudioBufferSourceNodeWrapperFromAudioNodeWrapper() { - return std::static_pointer_cast(wrapper_); -} - -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, "loopStart")); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "loopEnd")); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "detune")); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "playbackRate")); - 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 = getAudioBufferSourceNodeWrapperFromAudioNodeWrapper(); - auto loop = wrapper->getLoop(); - return {loop}; - } - - if (propName == "loopStart") { - auto wrapper = getAudioBufferSourceNodeWrapperFromAudioNodeWrapper(); - auto loopStart = wrapper->getLoopStart(); - return {loopStart}; - } - - if (propName == "loopEnd") { - auto wrapper = getAudioBufferSourceNodeWrapperFromAudioNodeWrapper(); - auto loopEnd = wrapper->getLoopEnd(); - return {loopEnd}; - } - - if (propName == "detune") { - return jsi::Object::createFromHostObject(runtime, detuneParam_); - } - - if (propName == "playbackRate") { - return jsi::Object::createFromHostObject(runtime, playbackRateParam_); - } - - if (propName == "buffer") { - auto wrapper = getAudioBufferSourceNodeWrapperFromAudioNodeWrapper(); - auto buffer = wrapper->getBuffer(); - - if (!buffer) { - return jsi::Value::null(); - } - - auto bufferHostObject = AudioBufferHostObject::createFromWrapper(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 = getAudioBufferSourceNodeWrapperFromAudioNodeWrapper(); - wrapper->setLoop(value.getBool()); - return; - } - - if (propName == "loopStart") { - auto wrapper = getAudioBufferSourceNodeWrapperFromAudioNodeWrapper(); - wrapper->setLoopStart(value.getNumber()); - return; - } - - if (propName == "loopEnd") { - auto wrapper = getAudioBufferSourceNodeWrapperFromAudioNodeWrapper(); - wrapper->setLoopEnd(value.getNumber()); - return; - } - - if (propName == "buffer") { - auto wrapper = getAudioBufferSourceNodeWrapperFromAudioNodeWrapper(); - - if (value.isNull()) { - wrapper->setBuffer(std::shared_ptr(nullptr)); - return; - } - - auto bufferHostObject = - value.getObject(runtime).asHostObject(runtime); - wrapper->setBuffer(bufferHostObject->wrapper_); - 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 3090dd75..b86f7346 100644 --- a/packages/react-native-audio-api/common/cpp/HostObjects/AudioBufferSourceNodeHostObject.h +++ b/packages/react-native-audio-api/common/cpp/HostObjects/AudioBufferSourceNodeHostObject.h @@ -4,40 +4,113 @@ #include #include "AudioBufferHostObject.h" -#include "AudioBufferSourceNodeWrapper.h" +#include "AudioBufferSourceNode.h" +#include "AudioParamHostObject.h" #include "AudioScheduledSourceNodeHostObject.h" namespace audioapi { using namespace facebook; -class AudioParamHostObject; - class AudioBufferSourceNodeHostObject : public AudioScheduledSourceNodeHostObject { public: explicit AudioBufferSourceNodeHostObject( - const std::shared_ptr &wrapper); + const std::shared_ptr &node) + : AudioScheduledSourceNodeHostObject(node) { + addGetters( + JSI_EXPORT_PROPERTY_GETTER(AudioBufferSourceNodeHostObject, loop), + JSI_EXPORT_PROPERTY_GETTER(AudioBufferSourceNodeHostObject, buffer), + JSI_EXPORT_PROPERTY_GETTER(AudioBufferSourceNodeHostObject, loopStart), + JSI_EXPORT_PROPERTY_GETTER(AudioBufferSourceNodeHostObject, loopEnd), + JSI_EXPORT_PROPERTY_GETTER(AudioBufferSourceNodeHostObject, detune), + JSI_EXPORT_PROPERTY_GETTER( + AudioBufferSourceNodeHostObject, playbackRate)); + addSetters( + JSI_EXPORT_PROPERTY_SETTER(AudioBufferSourceNodeHostObject, loop), + JSI_EXPORT_PROPERTY_SETTER(AudioBufferSourceNodeHostObject, buffer)); + } + + JSI_PROPERTY_GETTER(loop) { + auto audioBufferSourceNode = + std::static_pointer_cast(node_); + auto loop = audioBufferSourceNode->getLoop(); + return {loop}; + } + + JSI_PROPERTY_GETTER(buffer) { + auto audioBufferSourceNode = + std::static_pointer_cast(node_); + auto buffer = audioBufferSourceNode->getBuffer(); + + if (!buffer) { + return jsi::Value::null(); + } + + auto bufferHostObject = std::make_shared(buffer); + return jsi::Object::createFromHostObject(runtime, bufferHostObject); + } + + JSI_PROPERTY_GETTER(loopStart) { + auto audioBufferSourceNode = + std::static_pointer_cast(node_); + auto loopStart = audioBufferSourceNode->getLoopStart(); + return {loopStart}; + } - jsi::Value get(jsi::Runtime &runtime, const jsi::PropNameID &name) override; + JSI_PROPERTY_GETTER(loopEnd) { + auto audioBufferSourceNode = + std::static_pointer_cast(node_); + auto loopEnd = audioBufferSourceNode->getLoopEnd(); + return {loopEnd}; + } - void set( - jsi::Runtime &runtime, - const jsi::PropNameID &name, - const jsi::Value &value) override; + JSI_PROPERTY_GETTER(detune) { + auto audioBufferSourceNode = + std::static_pointer_cast(node_); + auto detune = audioBufferSourceNode->getDetuneParam(); + auto detuneHostObject = std::make_shared(detune); + return jsi::Object::createFromHostObject(runtime, detuneHostObject); + } - std::vector getPropertyNames(jsi::Runtime &rt) override; + JSI_PROPERTY_GETTER(playbackRate) { + auto audioBufferSourceNode = + std::static_pointer_cast(node_); + auto playbackRate = audioBufferSourceNode->getPlaybackRateParam(); + auto playbackRateHostObject = + std::make_shared(playbackRate); + return jsi::Object::createFromHostObject(runtime, playbackRateHostObject); + } - static std::shared_ptr createFromWrapper( - const std::shared_ptr &wrapper) { - return std::make_shared(wrapper); + JSI_PROPERTY_SETTER(loop) { + auto audioBufferSourceNode = + std::static_pointer_cast(node_); + audioBufferSourceNode->setLoop(value.getBool()); } - private: - std::shared_ptr detuneParam_; - std::shared_ptr playbackRateParam_; + JSI_PROPERTY_SETTER(buffer) { + auto audioBufferSourceNode = + std::static_pointer_cast(node_); + if (value.isNull()) { + audioBufferSourceNode->setBuffer(std::shared_ptr(nullptr)); + return; + } - std::shared_ptr - getAudioBufferSourceNodeWrapperFromAudioNodeWrapper(); + auto bufferHostObject = + value.getObject(runtime).asHostObject(runtime); + audioBufferSourceNode->setBuffer(bufferHostObject->audioBuffer_); + } + + JSI_PROPERTY_SETTER(loopStart) { + auto audioBufferSourceNode = + std::static_pointer_cast(node_); + audioBufferSourceNode->setLoopStart(value.getNumber()); + } + + JSI_PROPERTY_SETTER(loopEnd) { + auto audioBufferSourceNode = + std::static_pointer_cast(node_); + audioBufferSourceNode->setLoopEnd(value.getNumber()); + } }; } // 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 ab9deb39..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 &wrapper, - std::shared_ptr promiseVendor) - : BaseAudioContextHostObject(wrapper, 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 { - getAudioContextWrapperFromBaseAudioContextWrapper()->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::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 2d8b6288..815767ba 100644 --- a/packages/react-native-audio-api/common/cpp/HostObjects/AudioContextHostObject.h +++ b/packages/react-native-audio-api/common/cpp/HostObjects/AudioContextHostObject.h @@ -4,9 +4,8 @@ #include #include -#include "AudioContextWrapper.h" +#include "AudioContext.h" #include "BaseAudioContextHostObject.h" -#include "JsiPromise.h" namespace audioapi { using namespace facebook; @@ -14,26 +13,16 @@ using namespace facebook; class AudioContextHostObject : public BaseAudioContextHostObject { public: explicit AudioContextHostObject( - const std::shared_ptr &wrapper, - 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; - - static std::shared_ptr createFromWrapper( - const std::shared_ptr &wrapper, - std::shared_ptr promiseVendor) { - return std::make_shared(wrapper, promiseVendor); + const std::shared_ptr &audioContext, + const std::shared_ptr &promiseVendor) + : BaseAudioContextHostObject(audioContext, promiseVendor) { + addFunctions(JSI_EXPORT_FUNCTION(AudioContextHostObject, close)); } - private: - std::shared_ptr - getAudioContextWrapperFromBaseAudioContextWrapper(); + 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 1db74acf..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 &wrapper) - : AudioNodeHostObject(wrapper) {} - -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 68cf08cb..59afbf02 100644 --- a/packages/react-native-audio-api/common/cpp/HostObjects/AudioDestinationNodeHostObject.h +++ b/packages/react-native-audio-api/common/cpp/HostObjects/AudioDestinationNodeHostObject.h @@ -3,7 +3,7 @@ #include #include -#include "AudioDestinationNodeWrapper.h" +#include "AudioDestinationNode.h" #include "AudioNodeHostObject.h" namespace audioapi { @@ -12,20 +12,7 @@ using namespace facebook; class AudioDestinationNodeHostObject : public AudioNodeHostObject { public: explicit AudioDestinationNodeHostObject( - 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; - - static std::shared_ptr createFromWrapper( - const std::shared_ptr &wrapper) { - return std::make_shared(wrapper); - } + 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 7a78256f..00000000 --- a/packages/react-native-audio-api/common/cpp/HostObjects/AudioNodeHostObject.cpp +++ /dev/null @@ -1,102 +0,0 @@ -#include "AudioNodeHostObject.h" -#include "AudioContextHostObject.h" - -namespace audioapi { -using namespace facebook; - -AudioNodeHostObject::AudioNodeHostObject( - const std::shared_ptr &wrapper) - : wrapper_(wrapper) {} - -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); - wrapper_->connect( - std::shared_ptr(node)->wrapper_); - 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); - wrapper_->disconnect( - std::shared_ptr(node)->wrapper_); - return jsi::Value::undefined(); - }); - } - - if (propName == "numberOfInputs") { - return {wrapper_->getNumberOfInputs()}; - } - - if (propName == "numberOfOutputs") { - return {wrapper_->getNumberOfOutputs()}; - } - - if (propName == "channelCount") { - return {wrapper_->getChannelCount()}; - } - - if (propName == "channelCountMode") { - return jsi::String::createFromUtf8( - runtime, wrapper_->getChannelCountMode()); - } - - if (propName == "channelInterpretation") { - return jsi::String::createFromUtf8( - runtime, wrapper_->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 59d0d20b..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,26 +4,63 @@ #include #include -#include "AudioNodeWrapper.h" +#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 &wrapper); + 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 wrapper_; + std::shared_ptr node_; }; } // namespace audioapi 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 079d23d4..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 &wrapper) - : wrapper_(wrapper) {} - -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 {wrapper_->getValue()}; - } - - if (propName == "defaultValue") { - return {wrapper_->getDefaultValue()}; - } - - if (propName == "minValue") { - return {wrapper_->getMinValue()}; - } - - if (propName == "maxValue") { - return {wrapper_->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 { - double value = args[0].getNumber(); - double startTime = args[1].getNumber(); - wrapper_->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 { - double value = args[0].getNumber(); - double endTime = args[1].getNumber(); - wrapper_->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 { - double value = args[0].getNumber(); - double endTime = args[1].getNumber(); - wrapper_->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") { - double paramValue = value.getNumber(); - wrapper_->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 1221db65..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,31 +4,73 @@ #include #include -#include "AudioParamWrapper.h" +#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 &wrapper); + 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)); + } + + JSI_PROPERTY_GETTER(value) { + return {param_->getValue()}; + } + + JSI_PROPERTY_GETTER(defaultValue) { + return {param_->getDefaultValue()}; + } + + JSI_PROPERTY_GETTER(minValue) { + return {param_->getMinValue()}; + } - std::vector getPropertyNames(jsi::Runtime &rt) override; + 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(); + } - static std::shared_ptr createFromWrapper( - const std::shared_ptr &wrapper) { - return std::make_shared(wrapper); + JSI_PROPERTY_SETTER(value) { + param_->setValue(static_cast(value.getNumber())); } private: - std::shared_ptr wrapper_; + std::shared_ptr param_; }; } // namespace audioapi 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 884e2e55..00000000 --- a/packages/react-native-audio-api/common/cpp/HostObjects/AudioScheduledSourceNodeHostObject.cpp +++ /dev/null @@ -1,73 +0,0 @@ -#include "AudioScheduledSourceNodeHostObject.h" - -namespace audioapi { -using namespace facebook; - -std::shared_ptr -AudioScheduledSourceNodeHostObject:: - getAudioScheduledSourceNodeWrapperFromAudioNodeWrapper() { - return std::static_pointer_cast(wrapper_); -} - -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 wrapper = - getAudioScheduledSourceNodeWrapperFromAudioNodeWrapper(); - wrapper->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 wrapper = - getAudioScheduledSourceNodeWrapperFromAudioNodeWrapper(); - wrapper->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 22db5ef2..f09c83e4 100644 --- a/packages/react-native-audio-api/common/cpp/HostObjects/AudioScheduledSourceNodeHostObject.h +++ b/packages/react-native-audio-api/common/cpp/HostObjects/AudioScheduledSourceNodeHostObject.h @@ -4,7 +4,7 @@ #include #include "AudioNodeHostObject.h" -#include "AudioScheduledSourceNodeWrapper.h" +#include "AudioScheduledSourceNode.h" namespace audioapi { using namespace facebook; @@ -12,20 +12,27 @@ using namespace facebook; class AudioScheduledSourceNodeHostObject : public AudioNodeHostObject { public: explicit AudioScheduledSourceNodeHostObject( - const std::shared_ptr &wrapper) - : AudioNodeHostObject(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; - - private: - std::shared_ptr - getAudioScheduledSourceNodeWrapperFromAudioNodeWrapper(); + const std::shared_ptr &node) + : 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 032e1818..00000000 --- a/packages/react-native-audio-api/common/cpp/HostObjects/BaseAudioContextHostObject.cpp +++ /dev/null @@ -1,250 +0,0 @@ -#include - -#include "BaseAudioContextHostObject.h" - -namespace audioapi { -using namespace facebook; - -BaseAudioContextHostObject::BaseAudioContextHostObject( - const std::shared_ptr &wrapper, - std::shared_ptr promiseVendor) - : wrapper_(wrapper), promiseVendor_(promiseVendor) { - 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")); - 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, 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 == "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 = wrapper_->createPeriodicWave( - realData, imagData, disableNormalization, length); - auto periodicWaveHostObject = - PeriodicWaveHostObject::createFromWrapper(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 = wrapper_->decodeAudioDataSource(sourcePath); - auto audioBufferHostObject = - AudioBufferHostObject::createFromWrapper(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 f247219e..5b8ab893 100644 --- a/packages/react-native-audio-api/common/cpp/HostObjects/BaseAudioContextHostObject.h +++ b/packages/react-native-audio-api/common/cpp/HostObjects/BaseAudioContextHostObject.h @@ -5,13 +5,15 @@ #include #include +#include +#include + #include "AudioBufferHostObject.h" #include "AudioBufferSourceNodeHostObject.h" #include "AudioDestinationNodeHostObject.h" -#include "BaseAudioContextWrapper.h" +#include "BaseAudioContext.h" #include "BiquadFilterNodeHostObject.h" #include "GainNodeHostObject.h" -#include "JsiPromise.h" #include "OscillatorNodeHostObject.h" #include "PeriodicWaveHostObject.h" #include "StereoPannerNodeHostObject.h" @@ -19,24 +21,139 @@ namespace audioapi { using namespace facebook; -class BaseAudioContextHostObject : public jsi::HostObject { +class BaseAudioContextHostObject : public JsiHostObject { public: explicit BaseAudioContextHostObject( - const std::shared_ptr &wrapper, - std::shared_ptr promiseVendor); + const std::shared_ptr &context, + 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)); + + 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)); + } + + JSI_PROPERTY_GETTER(destination) { + auto destination = std::make_shared( + context_->getDestination()); + return jsi::Object::createFromHostObject(runtime, destination); + } + + 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); - jsi::Value get(jsi::Runtime &runtime, const jsi::PropNameID &name) override; + 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); - void set( - jsi::Runtime &runtime, - const jsi::PropNameID &name, - const jsi::Value &value) override; + promise->resolve(jsi::Object::createFromHostObject( + runtime, audioBufferHostObject)); + }).detach(); + }); - std::vector getPropertyNames(jsi::Runtime &rt) override; + return promise; + } protected: - std::shared_ptr wrapper_; - std::shared_ptr destination_; - std::shared_ptr promiseVendor_; + std::shared_ptr context_; + 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 4de215b3..00000000 --- a/packages/react-native-audio-api/common/cpp/HostObjects/BiquadFilterNodeHostObject.cpp +++ /dev/null @@ -1,125 +0,0 @@ -#include "BiquadFilterNodeHostObject.h" - -namespace audioapi { -using namespace facebook; - -std::shared_ptr -BiquadFilterNodeHostObject::getBiquadFilterNodeWrapperFromAudioNodeWrapper() { - return std::static_pointer_cast(wrapper_); -} - -BiquadFilterNodeHostObject::BiquadFilterNodeHostObject( - const std::shared_ptr &wrapper) - : AudioNodeHostObject(wrapper) { - auto frequencyParam = wrapper->getFrequencyParam(); - frequencyParam_ = AudioParamHostObject::createFromWrapper(frequencyParam); - auto detuneParam = wrapper->getDetuneParam(); - detuneParam_ = AudioParamHostObject::createFromWrapper(detuneParam); - auto QParam = wrapper->getQParam(); - QParam_ = AudioParamHostObject::createFromWrapper(QParam); - auto gainParam = wrapper->getGainParam(); - gainParam_ = AudioParamHostObject::createFromWrapper(gainParam); -} - -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 wrapper = getBiquadFilterNodeWrapperFromAudioNodeWrapper(); - auto waveType = wrapper->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 wrapper = getBiquadFilterNodeWrapperFromAudioNodeWrapper(); - wrapper->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 wrapper = getBiquadFilterNodeWrapperFromAudioNodeWrapper(); - wrapper->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 309ffe16..53f8cf98 100644 --- a/packages/react-native-audio-api/common/cpp/HostObjects/BiquadFilterNodeHostObject.h +++ b/packages/react-native-audio-api/common/cpp/HostObjects/BiquadFilterNodeHostObject.h @@ -6,7 +6,7 @@ #include "AudioNodeHostObject.h" #include "AudioParamHostObject.h" -#include "BiquadFilterNodeWrapper.h" +#include "BiquadFilterNode.h" namespace audioapi { using namespace facebook; @@ -14,29 +14,87 @@ using namespace facebook; class BiquadFilterNodeHostObject : public AudioNodeHostObject { public: explicit BiquadFilterNodeHostObject( - const std::shared_ptr &wrapper); + 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)); + } + + JSI_PROPERTY_GETTER(frequency) { + auto biquadFilterNode = std::static_pointer_cast(node_); + auto frequencyParam_ = std::make_shared( + biquadFilterNode->getFrequencyParam()); + return jsi::Object::createFromHostObject(runtime, frequencyParam_); + } + + JSI_PROPERTY_GETTER(detune) { + auto biquadFilterNode = std::static_pointer_cast(node_); + auto detuneParam_ = std::make_shared( + biquadFilterNode->getDetuneParam()); + return jsi::Object::createFromHostObject(runtime, detuneParam_); + } + + JSI_PROPERTY_GETTER(Q) { + auto biquadFilterNode = std::static_pointer_cast(node_); + auto QParam_ = + std::make_shared(biquadFilterNode->getQParam()); + return jsi::Object::createFromHostObject(runtime, QParam_); + } - std::vector getPropertyNames(jsi::Runtime &rt) override; + JSI_PROPERTY_GETTER(gain) { + auto biquadFilterNode = std::static_pointer_cast(node_); + auto gainParam_ = std::make_shared( + biquadFilterNode->getGainParam()); + return jsi::Object::createFromHostObject(runtime, gainParam_); + } - static std::shared_ptr createFromWrapper( - const std::shared_ptr &wrapper) { - return std::make_shared(wrapper); + JSI_PROPERTY_GETTER(type) { + auto biquadFilterNode = std::static_pointer_cast(node_); + auto type = biquadFilterNode->getType(); + return jsi::String::createFromUtf8(runtime, type); } - private: - std::shared_ptr frequencyParam_; - std::shared_ptr detuneParam_; - std::shared_ptr QParam_; - std::shared_ptr gainParam_; + 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::shared_ptr - getBiquadFilterNodeWrapperFromAudioNodeWrapper(); + 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 03e34066..00000000 --- a/packages/react-native-audio-api/common/cpp/HostObjects/GainNodeHostObject.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include "GainNodeHostObject.h" - -namespace audioapi { -using namespace facebook; - -GainNodeHostObject::GainNodeHostObject( - const std::shared_ptr &wrapper) - : AudioNodeHostObject(wrapper) { - auto gainParamWrapper = wrapper->getGainParam(); - gainParam_ = AudioParamHostObject::createFromWrapper(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 9e56b371..043d7771 100644 --- a/packages/react-native-audio-api/common/cpp/HostObjects/GainNodeHostObject.h +++ b/packages/react-native-audio-api/common/cpp/HostObjects/GainNodeHostObject.h @@ -5,28 +5,23 @@ #include "AudioNodeHostObject.h" #include "AudioParamHostObject.h" -#include "GainNodeWrapper.h" +#include "GainNode.h" namespace audioapi { using namespace facebook; class GainNodeHostObject : public AudioNodeHostObject { - protected: - std::shared_ptr gainParam_; - public: - explicit GainNodeHostObject(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; + explicit GainNodeHostObject(const std::shared_ptr &node) + : AudioNodeHostObject(node) { + addGetters(JSI_EXPORT_PROPERTY_GETTER(GainNodeHostObject, gain)); + } - static std::shared_ptr createFromWrapper( - const std::shared_ptr &wrapper) { - return std::make_shared(wrapper); + 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 44d1695c..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::getOscillatorNodeWrapperFromAudioNodeWrapper() { - return std::static_pointer_cast(wrapper_); -} - -OscillatorNodeHostObject::OscillatorNodeHostObject( - const std::shared_ptr &wrapper) - : AudioScheduledSourceNodeHostObject(wrapper) { - auto frequencyParam = wrapper->getFrequencyParam(); - frequencyParam_ = AudioParamHostObject::createFromWrapper(frequencyParam); - auto detuneParam = wrapper->getDetuneParam(); - detuneParam_ = AudioParamHostObject::createFromWrapper(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 wrapper = getOscillatorNodeWrapperFromAudioNodeWrapper(); - auto waveType = wrapper->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 wrapper = getOscillatorNodeWrapperFromAudioNodeWrapper(); - auto periodicWaveHostObject = - args[0].getObject(rt).asHostObject(rt); - - wrapper->setPeriodicWave(periodicWaveHostObject->wrapper_); - 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 wrapper = getOscillatorNodeWrapperFromAudioNodeWrapper(); - wrapper->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 31084ffb..de5b4f11 100644 --- a/packages/react-native-audio-api/common/cpp/HostObjects/OscillatorNodeHostObject.h +++ b/packages/react-native-audio-api/common/cpp/HostObjects/OscillatorNodeHostObject.h @@ -6,7 +6,7 @@ #include "AudioParamHostObject.h" #include "AudioScheduledSourceNodeHostObject.h" -#include "OscillatorNodeWrapper.h" +#include "OscillatorNode.h" #include "PeriodicWaveHostObject.h" namespace audioapi { @@ -14,28 +14,51 @@ using namespace facebook; class OscillatorNodeHostObject : public AudioScheduledSourceNodeHostObject { public: - explicit OscillatorNodeHostObject( - const std::shared_ptr &wrapper); + 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)); + } + + JSI_PROPERTY_GETTER(frequency) { + auto oscillatorNode = std::static_pointer_cast(node_); + auto frequencyParam_ = std::make_shared( + oscillatorNode->getFrequencyParam()); + return jsi::Object::createFromHostObject(runtime, frequencyParam_); + } - std::vector getPropertyNames(jsi::Runtime &rt) override; + JSI_PROPERTY_GETTER(detune) { + auto oscillatorNode = std::static_pointer_cast(node_); + auto detuneParam_ = std::make_shared( + oscillatorNode->getDetuneParam()); + return jsi::Object::createFromHostObject(runtime, detuneParam_); + } - static std::shared_ptr createFromWrapper( - const std::shared_ptr &wrapper) { - return std::make_shared(wrapper); + JSI_PROPERTY_GETTER(type) { + auto oscillatorNode = std::static_pointer_cast(node_); + auto waveType = oscillatorNode->getType(); + return jsi::String::createFromUtf8(runtime, waveType); } - private: - std::shared_ptr frequencyParam_; - std::shared_ptr detuneParam_; + 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(); + } - std::shared_ptr - getOscillatorNodeWrapperFromAudioNodeWrapper(); + 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 79b6c80b..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 &wrapper) - : wrapper_(wrapper) {} - -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 b4a252da..31f29af2 100644 --- a/packages/react-native-audio-api/common/cpp/HostObjects/PeriodicWaveHostObject.h +++ b/packages/react-native-audio-api/common/cpp/HostObjects/PeriodicWaveHostObject.h @@ -4,30 +4,17 @@ #include #include -#include "PeriodicWaveWrapper.h" +#include "PeriodicWave.h" namespace audioapi { using namespace facebook; class PeriodicWaveHostObject : public jsi::HostObject { public: - std::shared_ptr wrapper_; + std::shared_ptr periodicWave_; explicit PeriodicWaveHostObject( - 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; - - static std::shared_ptr createFromWrapper( - const std::shared_ptr &wrapper) { - return std::make_shared(wrapper); - } + 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 eaffb83e..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 &wrapper) - : AudioNodeHostObject(wrapper) { - auto panParam = wrapper->getPanParam(); - panParam_ = AudioParamHostObject::createFromWrapper(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 e5e2eaf0..9f594266 100644 --- a/packages/react-native-audio-api/common/cpp/HostObjects/StereoPannerNodeHostObject.h +++ b/packages/react-native-audio-api/common/cpp/HostObjects/StereoPannerNodeHostObject.h @@ -6,7 +6,7 @@ #include "AudioNodeHostObject.h" #include "AudioParamHostObject.h" -#include "StereoPannerNodeWrapper.h" +#include "StereoPannerNode.h" namespace audioapi { using namespace facebook; @@ -14,23 +14,16 @@ using namespace facebook; class StereoPannerNodeHostObject : public AudioNodeHostObject { public: explicit StereoPannerNodeHostObject( - 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; - - static std::shared_ptr createFromWrapper( - const std::shared_ptr &wrapper) { - return std::make_shared(wrapper); + const std::shared_ptr &node) + : AudioNodeHostObject(node) { + addGetters(JSI_EXPORT_PROPERTY_GETTER(StereoPannerNodeHostObject, pan)); } - private: - std::shared_ptr panParam_; + 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/core/AudioBufferSourceNode.cpp b/packages/react-native-audio-api/common/cpp/core/AudioBufferSourceNode.cpp index 9f84330b..bfe43b22 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioBufferSourceNode.cpp +++ b/packages/react-native-audio-api/common/cpp/core/AudioBufferSourceNode.cpp @@ -11,17 +11,18 @@ namespace audioapi { AudioBufferSourceNode::AudioBufferSourceNode(BaseAudioContext *context) - : AudioScheduledSourceNode(context), loop_(false), vReadIndex_(0.0), loopStart_(0), loopEnd_(0) { + : AudioScheduledSourceNode(context), + loop_(false), + loopStart_(0), + loopEnd_(0), + vReadIndex_(0.0) { numberOfInputs_ = 0; buffer_ = std::shared_ptr(nullptr); - detuneParam_ = std::make_shared(context, 0.0, -MAX_DETUNE, MAX_DETUNE); + detuneParam_ = + std::make_shared(context, 0.0, -MAX_DETUNE, MAX_DETUNE); playbackRateParam_ = std::make_shared( - context, - 1.0, - MOST_NEGATIVE_SINGLE_FLOAT, - MOST_POSITIVE_SINGLE_FLOAT - ); + context, 1.0, MOST_NEGATIVE_SINGLE_FLOAT, MOST_POSITIVE_SINGLE_FLOAT); isInitialized_ = true; } @@ -42,7 +43,8 @@ std::shared_ptr AudioBufferSourceNode::getDetuneParam() const { return detuneParam_; } -std::shared_ptr AudioBufferSourceNode::getPlaybackRateParam() const { +std::shared_ptr AudioBufferSourceNode::getPlaybackRateParam() + const { return playbackRateParam_; } @@ -71,13 +73,16 @@ void AudioBufferSourceNode::setBuffer( } buffer_ = buffer; - alignedBus_ = std::make_shared(context_->getSampleRate(), buffer_->getLength()); + alignedBus_ = std::make_shared( + context_->getSampleRate(), buffer_->getLength()); alignedBus_->zero(); alignedBus_->sum(buffer_->bus_.get()); } -void AudioBufferSourceNode::processNode(AudioBus *processingBus, int framesToProcess) { +void AudioBufferSourceNode::processNode( + AudioBus *processingBus, + int framesToProcess) { size_t startOffset = 0; size_t offsetLength = 0; @@ -85,15 +90,18 @@ void AudioBufferSourceNode::processNode(AudioBus *processingBus, int framesToPro float playbackRate = getPlaybackRateValue(startOffset); // No audio data to fill, zero the output and return. - if (!isPlaying() || !alignedBus_ || alignedBus_->getSize() == 0 || !playbackRate) { + if (!isPlaying() || !alignedBus_ || alignedBus_->getSize() == 0 || + !playbackRate) { processingBus->zero(); return; } if (std::fabs(playbackRate) == 1.0) { - processWithoutInterpolation(processingBus, startOffset, offsetLength, playbackRate); + processWithoutInterpolation( + processingBus, startOffset, offsetLength, playbackRate); } else { - processWithInterpolation(processingBus, startOffset, offsetLength, playbackRate); + processWithInterpolation( + processingBus, startOffset, offsetLength, playbackRate); } } @@ -102,18 +110,17 @@ void AudioBufferSourceNode::processNode(AudioBus *processingBus, int framesToPro */ void AudioBufferSourceNode::processWithoutInterpolation( - AudioBus *processingBus, - size_t startOffset, - size_t offsetLength, - float playbackRate -) { + AudioBus *processingBus, + size_t startOffset, + size_t offsetLength, + float playbackRate) { size_t direction = playbackRate < 0 ? -1 : 1; - size_t readIndex = static_cast(vReadIndex_); + auto readIndex = static_cast(vReadIndex_); size_t writeIndex = startOffset; - size_t frameStart = static_cast(getVirtualStartFrame()); - size_t frameEnd = static_cast(getVirtualEndFrame()); + auto frameStart = static_cast(getVirtualStartFrame()); + auto frameEnd = static_cast(getVirtualEndFrame()); size_t frameDelta = frameEnd - frameStart; size_t framesLeft = offsetLength; @@ -125,15 +132,17 @@ void AudioBufferSourceNode::processWithoutInterpolation( while (framesLeft > 0) { size_t framesToEnd = frameEnd - readIndex; size_t framesToCopy = std::min(framesToEnd, framesLeft); - framesToCopy = std::max(framesToCopy, 0ul); + framesToCopy = framesToCopy > 0 ? framesToCopy : 0; // Direction is forward, we can normally copy the data if (direction == 1) { - processingBus->copy(alignedBus_.get(), readIndex, writeIndex, framesToCopy); + processingBus->copy( + alignedBus_.get(), readIndex, writeIndex, framesToCopy); } else { for (int i = 0; i < framesToCopy; i += 1) { for (int j = 0; j < processingBus->getNumberOfChannels(); j += 1) { - (*processingBus->getChannel(j))[writeIndex + i] = (*alignedBus_->getChannel(j))[readIndex - i]; + (*processingBus->getChannel(j))[writeIndex + i] = + (*alignedBus_->getChannel(j))[readIndex - i]; } } } @@ -159,11 +168,10 @@ void AudioBufferSourceNode::processWithoutInterpolation( } void AudioBufferSourceNode::processWithInterpolation( - AudioBus *processingBus, - size_t startOffset, - size_t offsetLength, - float playbackRate -) { + AudioBus *processingBus, + size_t startOffset, + size_t offsetLength, + float playbackRate) { size_t direction = playbackRate < 0 ? -1 : 1; size_t writeIndex = startOffset; @@ -172,18 +180,19 @@ void AudioBufferSourceNode::processWithInterpolation( double vFrameEnd = getVirtualEndFrame(); double vFrameDelta = vFrameEnd - vFrameStart; - size_t frameStart = static_cast(vFrameStart); - size_t frameEnd = static_cast(vFrameEnd); + auto frameStart = static_cast(vFrameStart); + auto frameEnd = static_cast(vFrameEnd); size_t framesLeft = offsetLength; // Wrap to the start of the loop if necessary if (loop_ && (vReadIndex_ >= vFrameEnd || vReadIndex_ < vFrameStart)) { - vReadIndex_ = vFrameStart + std::fmod(vReadIndex_ - vFrameStart, vFrameDelta); + vReadIndex_ = + vFrameStart + std::fmod(vReadIndex_ - vFrameStart, vFrameDelta); } while (framesLeft > 0) { - size_t readIndex = static_cast(vReadIndex_); + auto readIndex = static_cast(vReadIndex_); size_t nextReadIndex = readIndex + 1; float factor = vReadIndex_ - readIndex; @@ -192,15 +201,11 @@ void AudioBufferSourceNode::processWithInterpolation( } for (int i = 0; i < processingBus->getNumberOfChannels(); i += 1) { - float* destination = processingBus->getChannel(i)->getData(); - const float* source = alignedBus_->getChannel(i)->getData(); + float *destination = processingBus->getChannel(i)->getData(); + const float *source = alignedBus_->getChannel(i)->getData(); destination[writeIndex] = AudioUtils::linearInterpolate( - source, - readIndex, - nextReadIndex, - factor - ); + source, readIndex, nextReadIndex, factor); } writeIndex += 1; @@ -220,19 +225,19 @@ void AudioBufferSourceNode::processWithInterpolation( } } -float AudioBufferSourceNode::getPlaybackRateValue(size_t& startOffset) { - double time = context_->getCurrentTime() + startOffset / context_->getSampleRate(); +float AudioBufferSourceNode::getPlaybackRateValue(size_t &startOffset) { + double time = + context_->getCurrentTime() + startOffset / context_->getSampleRate(); - return playbackRateParam_->getValueAtTime(time) - * std::pow(2.0f, detuneParam_->getValueAtTime(time) / 1200.0f); + return playbackRateParam_->getValueAtTime(time) * + std::pow(2.0f, detuneParam_->getValueAtTime(time) / 1200.0f); } double AudioBufferSourceNode::getVirtualStartFrame() { double loopStartFrame = loopStart_ * context_->getSampleRate(); - return loop_ && loopStartFrame >= 0 && loopStart_ < loopEnd_ - ? loopStartFrame - : 0.0; + return loop_ && loopStartFrame >= 0 && loopStart_ < loopEnd_ ? loopStartFrame + : 0.0; } double AudioBufferSourceNode::getVirtualEndFrame() { @@ -240,8 +245,8 @@ double AudioBufferSourceNode::getVirtualEndFrame() { double loopEndFrame = loopEnd_ * context_->getSampleRate(); return loop_ && loopEndFrame > 0 && loopStart_ < loopEnd_ - ? std::min(loopEndFrame, inputBufferLength) - : inputBufferLength; + ? std::min(loopEndFrame, inputBufferLength) + : inputBufferLength; } } // namespace audioapi 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 2c6f9102..1a3e6d54 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioBufferSourceNode.h +++ b/packages/react-native-audio-api/common/cpp/core/AudioBufferSourceNode.h @@ -49,7 +49,7 @@ class AudioBufferSourceNode : public AudioScheduledSourceNode { std::shared_ptr buffer_; std::shared_ptr alignedBus_; - float getPlaybackRateValue(size_t& startOffset); + float getPlaybackRateValue(size_t &startOffset); double getVirtualStartFrame(); double getVirtualEndFrame(); 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 9d9a13aa..c8ff0a90 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioDestinationNode.h +++ b/packages/react-native-audio-api/common/cpp/core/AudioDestinationNode.h @@ -23,7 +23,7 @@ class AudioDestinationNode : public AudioNode { protected: // DestinationNode is triggered by AudioContext using renderAudio // processNode function is not necessary and is never called. - void processNode(AudioBus *, int) final{}; + void processNode(AudioBus *, int) final {}; private: std::size_t currentSampleFrame_; 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 baa0cfc5..1dafb3ed 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioScheduledSourceNode.cpp +++ b/packages/react-native-audio-api/common/cpp/core/AudioScheduledSourceNode.cpp @@ -1,14 +1,17 @@ +#include "AudioScheduledSourceNode.h" #include "AudioArray.h" #include "AudioBus.h" -#include "AudioUtils.h" -#include "AudioScheduledSourceNode.h" #include "AudioNodeManager.h" +#include "AudioUtils.h" #include "BaseAudioContext.h" namespace audioapi { AudioScheduledSourceNode::AudioScheduledSourceNode(BaseAudioContext *context) - : AudioNode(context), playbackState_(PlaybackState::UNSCHEDULED), startTime_(-1.0), stopTime_(-1.0) { + : AudioNode(context), + playbackState_(PlaybackState::UNSCHEDULED), + startTime_(-1.0), + stopTime_(-1.0) { numberOfInputs_ = 0; } @@ -31,7 +34,6 @@ bool AudioScheduledSourceNode::isScheduled() { return playbackState_ == PlaybackState::SCHEDULED; } - bool AudioScheduledSourceNode::isPlaying() { return playbackState_ == PlaybackState::PLAYING; } @@ -40,7 +42,11 @@ bool AudioScheduledSourceNode::isFinished() { return playbackState_ == PlaybackState::FINISHED; } -void AudioScheduledSourceNode::updatePlaybackInfo(AudioBus *processingBus, int framesToProcess, size_t& startOffset, size_t& nonSilentFramesToProcess) { +void AudioScheduledSourceNode::updatePlaybackInfo( + AudioBus *processingBus, + int framesToProcess, + size_t &startOffset, + size_t &nonSilentFramesToProcess) { if (!isInitialized_) { startOffset = 0; nonSilentFramesToProcess = 0; @@ -52,10 +58,12 @@ void AudioScheduledSourceNode::updatePlaybackInfo(AudioBus *processingBus, int f size_t firstFrame = context_->getCurrentSampleFrame(); size_t lastFrame = firstFrame + framesToProcess; - size_t startFrame = std::max(AudioUtils::timeToSampleFrame(startTime_, sampleRate), firstFrame); + size_t startFrame = std::max( + AudioUtils::timeToSampleFrame(startTime_, sampleRate), firstFrame); size_t stopFrame = stopTime_ == -1.0 - ? std::numeric_limits::max() - : std::max(AudioUtils::timeToSampleFrame(stopTime_, sampleRate), firstFrame); + ? std::numeric_limits::max() + : std::max( + AudioUtils::timeToSampleFrame(stopTime_, sampleRate), firstFrame); if (isUnscheduled() || isFinished()) { startOffset = 0; @@ -74,8 +82,11 @@ void AudioScheduledSourceNode::updatePlaybackInfo(AudioBus *processingBus, int f // start playing // zero first frames before starting frame playbackState_ = PlaybackState::PLAYING; - startOffset = std::max(0ul, std::max(startFrame, firstFrame) - firstFrame); - nonSilentFramesToProcess = std::min(lastFrame, stopFrame) - startFrame - startOffset; + startOffset = std::max(startFrame, firstFrame) - firstFrame > 0 + ? std::max(startFrame, firstFrame) - firstFrame + : 0; + nonSilentFramesToProcess = + std::min(lastFrame, stopFrame) - startFrame - startOffset; processingBus->zero(0, startOffset); return; } 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 c0b1e5b7..5a2b41ea 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioScheduledSourceNode.h +++ b/packages/react-native-audio-api/common/cpp/core/AudioScheduledSourceNode.h @@ -6,6 +6,8 @@ #include #include #include +#include +#include #include "AudioNode.h" @@ -26,7 +28,11 @@ class AudioScheduledSourceNode : public AudioNode { protected: std::atomic playbackState_; - void updatePlaybackInfo(AudioBus *processingBus, int framesToProcess, size_t& startOffset, size_t& nonSilentFramesToProcess); + void updatePlaybackInfo( + AudioBus *processingBus, + int framesToProcess, + size_t &startOffset, + size_t &nonSilentFramesToProcess); private: double startTime_; 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 2fdce43b..05110775 100644 --- a/packages/react-native-audio-api/common/cpp/core/OscillatorNode.cpp +++ b/packages/react-native-audio-api/common/cpp/core/OscillatorNode.cpp @@ -45,7 +45,6 @@ void OscillatorNode::processNode(AudioBus *processingBus, int framesToProcess) { updatePlaybackInfo(processingBus, framesToProcess, startOffset, offsetLength); - if (!isPlaying()) { processingBus->zero(); return; diff --git a/packages/react-native-audio-api/common/cpp/jsi/JsiHostObject.cpp b/packages/react-native-audio-api/common/cpp/jsi/JsiHostObject.cpp new file mode 100644 index 00000000..42fefcfd --- /dev/null +++ b/packages/react-native-audio-api/common/cpp/jsi/JsiHostObject.cpp @@ -0,0 +1,87 @@ +#include "JsiHostObject.h" + +namespace audioapi { +JsiHostObject::JsiHostObject() { + propertyGetters_ = std::make_unique>(); + propertyFunctions_ = std::make_unique>(); + propertySetters_ = std::make_unique>(); + + propertyGetters_->insert(JSI_EXPORT_PROPERTY_GETTER(JsiHostObject, then)); +} + +std::vector JsiHostObject::getPropertyNames(jsi::Runtime &rt) { + std::vector propertyNames; + propertyNames.reserve( + propertyGetters_->size() + propertyFunctions_->size() + + propertySetters_->size()); + + for (const auto &it : *propertyGetters_) { + propertyNames.push_back(jsi::PropNameID::forUtf8(rt, it.first)); + } + + for (const auto &it : *propertyFunctions_) { + propertyNames.push_back(jsi::PropNameID::forAscii(rt, it.first)); + } + + for (const auto &it : *propertySetters_) { + propertyNames.push_back(jsi::PropNameID::forAscii(rt, it.first)); + } + + return propertyNames; +} + +jsi::Value JsiHostObject::get( + jsi::Runtime &runtime, + const jsi::PropNameID &name) { + auto nameAsString = name.utf8(runtime); + + auto propertyGetter = propertyGetters_->find(nameAsString); + if (propertyGetter != propertyGetters_->end()) { + auto dispatcher = [this, &propertyGetter](jsi::Runtime &runtime) { + return (this->*(propertyGetter->second))(runtime); + }; + + return dispatcher(runtime); + } + + auto propertyFunction = propertyFunctions_->find(nameAsString); + if (propertyFunction != propertyFunctions_->end()) { + auto dispatcher = [this, &propertyFunction]( + jsi::Runtime &runtime, + const jsi::Value &thisVal, + const jsi::Value *args, + size_t count) -> jsi::Value { + return (this->*(propertyFunction->second))(runtime, thisVal, args, count); + }; + + return jsi::Function::createFromHostFunction(runtime, name, 0, dispatcher); + } + + return jsi::Value::undefined(); +} + +void JsiHostObject::set( + jsi::Runtime &runtime, + const jsi::PropNameID &name, + const jsi::Value &value) { + auto nameAsString = name.utf8(runtime); + + auto propertySetter = propertySetters_->find(nameAsString); + + if (propertySetter != propertySetters_->end()) { + auto dispatcher = [this, &propertySetter]( + jsi::Runtime &runtime, const jsi::Value &value) { + return (this->*(propertySetter->second))(runtime, value); + }; + + return dispatcher(runtime, value); + } +} +} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/jsi/JsiHostObject.h b/packages/react-native-audio-api/common/cpp/jsi/JsiHostObject.h new file mode 100644 index 00000000..4b0d2676 --- /dev/null +++ b/packages/react-native-audio-api/common/cpp/jsi/JsiHostObject.h @@ -0,0 +1,98 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +#define JSI_HOST_FUNCTION(NAME) \ + jsi::Value NAME( \ + jsi::Runtime &runtime, \ + const jsi::Value &thisVal, \ + const jsi::Value *args, \ + size_t count) + +#define JSI_EXPORT_FUNCTION(CLASS, FUNCTION) \ + std::make_pair( \ + std::string(#FUNCTION), \ + static_cast( \ + &CLASS::FUNCTION)) + +#define JSI_PROPERTY_GETTER(name) jsi::Value name(jsi::Runtime &runtime) + +#define JSI_EXPORT_PROPERTY_GETTER(CLASS, FUNCTION) \ + std::make_pair( \ + std::string(#FUNCTION), \ + static_cast( \ + &CLASS::FUNCTION)) + +#define JSI_PROPERTY_SETTER(name) \ + void name(jsi::Runtime &runtime, const jsi::Value &value) + +#define JSI_EXPORT_PROPERTY_SETTER(CLASS, FUNCTION) \ + std::make_pair( \ + std::string(#FUNCTION), \ + static_cast(&CLASS::FUNCTION)) + +namespace audioapi { + +using namespace facebook; + +class JsiHostObject : public jsi::HostObject { + public: + JsiHostObject(); + + std::vector getPropertyNames(jsi::Runtime &rt) 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; + + template + void addGetters(Args... args) { + (propertyGetters_->insert(args), ...); + } + + template + void addSetters(Args... args) { + (propertySetters_->insert(args), ...); + } + + template + void addFunctions(Args... args) { + (propertyFunctions_->insert(args), ...); + } + + JSI_PROPERTY_GETTER(then) { + return jsi::Value::undefined(); + } + + protected: + std::unique_ptr> + propertyGetters_; + + std::unique_ptr> + propertyFunctions_; + + std::unique_ptr> + propertySetters_; +}; + +} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/utils/JsiPromise.cpp b/packages/react-native-audio-api/common/cpp/jsi/JsiPromise.cpp similarity index 86% rename from packages/react-native-audio-api/common/cpp/utils/JsiPromise.cpp rename to packages/react-native-audio-api/common/cpp/jsi/JsiPromise.cpp index b902278d..663676cc 100644 --- a/packages/react-native-audio-api/common/cpp/utils/JsiPromise.cpp +++ b/packages/react-native-audio-api/common/cpp/jsi/JsiPromise.cpp @@ -4,17 +4,18 @@ #include #include -namespace JsiPromise { +namespace audioapi { using namespace facebook; jsi::Value PromiseVendor::createPromise( - std::function)> func) { - if (_runtime == nullptr) { - throw new std::runtime_error("Runtime was null!"); + const std::function)> &function) { + if (runtime_ == nullptr) { + throw std::runtime_error("Runtime was null!"); } - auto &runtime = *_runtime; - auto callInvoker = _callInvoker; + + auto &runtime = *runtime_; + auto callInvoker = callInvoker_; // get Promise constructor auto promiseCtor = runtime.global().getPropertyAsFunction(runtime, "Promise"); @@ -24,7 +25,7 @@ jsi::Value PromiseVendor::createPromise( runtime, jsi::PropNameID::forUtf8(runtime, "runPromise"), 2, - [callInvoker, func]( + [callInvoker, function]( jsi::Runtime &runtime, const jsi::Value &thisValue, const jsi::Value *arguments, @@ -52,7 +53,7 @@ jsi::Value PromiseVendor::createPromise( }; auto promise = std::make_shared(resolveWrapper, rejectWrapper); - func(promise); + function(promise); return jsi::Value::undefined(); }); @@ -61,4 +62,4 @@ jsi::Value PromiseVendor::createPromise( return promiseCtor.callAsConstructor(runtime, runPromise); } -} // namespace JsiPromise +} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/jsi/JsiPromise.h b/packages/react-native-audio-api/common/cpp/jsi/JsiPromise.h new file mode 100644 index 00000000..6a70010b --- /dev/null +++ b/packages/react-native-audio-api/common/cpp/jsi/JsiPromise.h @@ -0,0 +1,48 @@ +#pragma once + +#include +#include +#include +#include +#include + +namespace audioapi { + +using namespace facebook; + +class Promise { + public: + Promise( + std::function resolve, + std::function reject) + : resolve_(std::move(resolve)), reject_(std::move(reject)) {} + + void resolve(jsi::Value &&value) { + resolve_(std::forward(value)); + } + void reject(const std::string &errorMessage) { + reject_(errorMessage); + } + + private: + std::function resolve_; + std::function reject_; +}; + +class PromiseVendor { + public: + PromiseVendor( + jsi::Runtime *runtime, + const std::shared_ptr &callInvoker) + : runtime_(runtime), callInvoker_(callInvoker) {} + + public: + jsi::Value createPromise( + const std::function)> &function); + + private: + jsi::Runtime *runtime_; + std::shared_ptr callInvoker_; +}; + +} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/utils/AudioUtils.cpp b/packages/react-native-audio-api/common/cpp/utils/AudioUtils.cpp new file mode 100644 index 00000000..229a83c3 --- /dev/null +++ b/packages/react-native-audio-api/common/cpp/utils/AudioUtils.cpp @@ -0,0 +1,26 @@ +#include "AudioUtils.h" + +namespace audioapi::AudioUtils { +size_t timeToSampleFrame(double time, int sampleRate) { + return static_cast(time * sampleRate); +} + +double sampleFrameToTime(int sampleFrame, int sampleRate) { + return static_cast(sampleFrame) / sampleRate; +} + +float linearInterpolate( + const float *source, + size_t firstIndex, + size_t secondIndex, + float factor) { + if (firstIndex == secondIndex && firstIndex >= 1) { + return source[firstIndex] + + factor * (source[firstIndex] - source[firstIndex - 1]); + } + + return source[firstIndex] + + factor * (source[secondIndex] - source[firstIndex]); +} + +} // namespace audioapi::AudioUtils diff --git a/packages/react-native-audio-api/common/cpp/utils/AudioUtils.h b/packages/react-native-audio-api/common/cpp/utils/AudioUtils.h index d3453fc9..4b8c0790 100644 --- a/packages/react-native-audio-api/common/cpp/utils/AudioUtils.h +++ b/packages/react-native-audio-api/common/cpp/utils/AudioUtils.h @@ -1,19 +1,16 @@ +#pragma once -namespace audioapi::AudioUtils { - size_t timeToSampleFrame(double time, int sampleRate) { - return static_cast(time * sampleRate); - } +#include - double sampleFrameToTime(int sampleFrame, int sampleRate) { - return static_cast(sampleFrame) / sampleRate; - } +namespace audioapi::AudioUtils { +size_t timeToSampleFrame(double time, int sampleRate); - float linearInterpolate(const float* source, size_t firstIndex, size_t secondIndex, float factor) { - if (firstIndex == secondIndex && firstIndex >= 1) { - return source[firstIndex] + factor * (source[firstIndex] - source[firstIndex - 1]); - } +double sampleFrameToTime(int sampleFrame, int sampleRate); - return source[firstIndex] + factor * (source[secondIndex] - source[firstIndex]); - } +float linearInterpolate( + const float *source, + size_t firstIndex, + size_t secondIndex, + float factor); } // namespace audioapi::AudioUtils diff --git a/packages/react-native-audio-api/common/cpp/utils/JsiPromise.h b/packages/react-native-audio-api/common/cpp/utils/JsiPromise.h deleted file mode 100644 index c1a84ed1..00000000 --- a/packages/react-native-audio-api/common/cpp/utils/JsiPromise.h +++ /dev/null @@ -1,48 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include - -namespace JsiPromise { - -using namespace facebook; - -class Promise { - public: - Promise( - std::function resolve, - std::function reject) - : _resolve(std::move(resolve)), _reject(std::move(reject)) {} - - bool isResolved; - void resolve(jsi::Value &&value) { - _resolve(std::forward(value)); - } - void reject(const std::string &errorMessage) { - _reject(errorMessage); - } - - private: - std::function _resolve; - std::function _reject; -}; - -class PromiseVendor { - public: - PromiseVendor( - jsi::Runtime *runtime, - std::shared_ptr callInvoker) - : _runtime(runtime), _callInvoker(callInvoker) {} - - public: - jsi::Value createPromise(std::function)> func); - - private: - jsi::Runtime *_runtime; - std::shared_ptr _callInvoker; -}; - -} // namespace JsiPromise diff --git a/packages/react-native-audio-api/common/cpp/wrappers/AudioBufferSourceNodeWrapper.cpp b/packages/react-native-audio-api/common/cpp/wrappers/AudioBufferSourceNodeWrapper.cpp deleted file mode 100644 index 52d51660..00000000 --- a/packages/react-native-audio-api/common/cpp/wrappers/AudioBufferSourceNodeWrapper.cpp +++ /dev/null @@ -1,81 +0,0 @@ -#include "AudioParamWrapper.h" -#include "AudioBufferSourceNodeWrapper.h" - -namespace audioapi { - -AudioBufferSourceNodeWrapper::AudioBufferSourceNodeWrapper( - const std::shared_ptr &audioBufferSourceNode) - : AudioScheduledSourceNodeWrapper(audioBufferSourceNode) { - auto detuneParam = audioBufferSourceNode->getDetuneParam(); - detuneParam_ = std::make_shared(detuneParam); - - auto playbackRateParam = audioBufferSourceNode->getPlaybackRateParam(); - playbackRateParam_ = std::make_shared(playbackRateParam); -} - -std::shared_ptr -AudioBufferSourceNodeWrapper::getAudioBufferSourceNodeFromAudioNode() { - return std::static_pointer_cast(node_); -} - -bool AudioBufferSourceNodeWrapper::getLoop() { - auto audioBufferSourceNode = getAudioBufferSourceNodeFromAudioNode(); - return audioBufferSourceNode->getLoop(); -} - -double AudioBufferSourceNodeWrapper::getLoopStart() { - auto audioBufferSourceNode = getAudioBufferSourceNodeFromAudioNode(); - return audioBufferSourceNode->getLoopStart(); -} - -double AudioBufferSourceNodeWrapper::getLoopEnd() { - auto audioBufferSourceNode = getAudioBufferSourceNodeFromAudioNode(); - return audioBufferSourceNode->getLoopEnd(); -} - -std::shared_ptr AudioBufferSourceNodeWrapper::getDetuneParam() { - return detuneParam_; -} - -std::shared_ptr AudioBufferSourceNodeWrapper::getPlaybackRateParam() { - return playbackRateParam_; -} - -std::shared_ptr AudioBufferSourceNodeWrapper::getBuffer() { - auto audioBufferSourceNode = getAudioBufferSourceNodeFromAudioNode(); - auto buffer = audioBufferSourceNode->getBuffer(); - - if (!buffer) { - return {nullptr}; - } - - return std::make_shared(buffer); -} - -void AudioBufferSourceNodeWrapper::setLoop(bool loop) { - auto audioBufferSourceNode = getAudioBufferSourceNodeFromAudioNode(); - audioBufferSourceNode->setLoop(loop); -} - -void AudioBufferSourceNodeWrapper::setLoopStart(double loopStart) { - auto audioBufferSourceNode = getAudioBufferSourceNodeFromAudioNode(); - audioBufferSourceNode->setLoopStart(loopStart); -} - -void AudioBufferSourceNodeWrapper::setLoopEnd(double loopEnd) { - auto audioBufferSourceNode = getAudioBufferSourceNodeFromAudioNode(); - audioBufferSourceNode->setLoopEnd(loopEnd); -} - -void AudioBufferSourceNodeWrapper::setBuffer( - const std::shared_ptr &buffer) { - auto audioBufferSourceNode = getAudioBufferSourceNodeFromAudioNode(); - if (!buffer) { - audioBufferSourceNode->setBuffer(std::shared_ptr(nullptr)); - return; - } - - audioBufferSourceNode->setBuffer(buffer->audioBuffer_); -} - -} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/wrappers/AudioBufferSourceNodeWrapper.h b/packages/react-native-audio-api/common/cpp/wrappers/AudioBufferSourceNodeWrapper.h deleted file mode 100644 index d210583e..00000000 --- a/packages/react-native-audio-api/common/cpp/wrappers/AudioBufferSourceNodeWrapper.h +++ /dev/null @@ -1,41 +0,0 @@ -#pragma once - -#include -#include - -#include "AudioBufferSourceNode.h" -#include "AudioBufferWrapper.h" -#include "AudioScheduledSourceNodeWrapper.h" - -namespace audioapi { - -class AudioParamWrapper; - -class AudioBufferSourceNodeWrapper : public AudioScheduledSourceNodeWrapper { - public: - explicit AudioBufferSourceNodeWrapper( - const std::shared_ptr &audioBufferSourceNode); - - [[nodiscard]] bool getLoop(); - [[nodiscard]] double getLoopStart(); - [[nodiscard]] double getLoopEnd(); - - [[nodiscard]] std::shared_ptr getDetuneParam(); - [[nodiscard]] std::shared_ptr getPlaybackRateParam(); - - [[nodiscard]] std::shared_ptr getBuffer(); - - void setLoop(bool loop); - void setLoopStart(double loopStart); - void setLoopEnd(double loopEnd); - - void setBuffer(const std::shared_ptr &buffer); - - private: - std::shared_ptr - getAudioBufferSourceNodeFromAudioNode(); - - std::shared_ptr detuneParam_; - std::shared_ptr playbackRateParam_; -}; -} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/wrappers/AudioBufferWrapper.cpp b/packages/react-native-audio-api/common/cpp/wrappers/AudioBufferWrapper.cpp deleted file mode 100644 index 6e89c0b7..00000000 --- a/packages/react-native-audio-api/common/cpp/wrappers/AudioBufferWrapper.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include "AudioBufferWrapper.h" - -namespace audioapi { - -AudioBufferWrapper::AudioBufferWrapper( - const std::shared_ptr &audioBuffer) - : audioBuffer_(audioBuffer) {} - -int AudioBufferWrapper::getSampleRate() const { - return audioBuffer_->getSampleRate(); -} - -int AudioBufferWrapper::getLength() const { - return audioBuffer_->getLength(); -} - -double AudioBufferWrapper::getDuration() const { - return audioBuffer_->getDuration(); -} - -int AudioBufferWrapper::getNumberOfChannels() const { - return audioBuffer_->getNumberOfChannels(); -} - -float *AudioBufferWrapper::getChannelData(int channel) const { - return audioBuffer_->getChannelData(channel); -} - -void AudioBufferWrapper::copyFromChannel( - float *destination, - int destinationLength, - int channelNumber, - int startInChannel) const { - audioBuffer_->copyFromChannel( - destination, destinationLength, channelNumber, startInChannel); -} - -void AudioBufferWrapper::copyToChannel( - float *source, - int sourceLength, - int channelNumber, - int startInChannel) const { - audioBuffer_->copyToChannel( - source, sourceLength, channelNumber, startInChannel); -} -} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/wrappers/AudioBufferWrapper.h b/packages/react-native-audio-api/common/cpp/wrappers/AudioBufferWrapper.h deleted file mode 100644 index caf6a53d..00000000 --- a/packages/react-native-audio-api/common/cpp/wrappers/AudioBufferWrapper.h +++ /dev/null @@ -1,30 +0,0 @@ -#pragma once - -#include - -#include "AudioBuffer.h" - -namespace audioapi { - -class AudioBufferWrapper { - public: - explicit AudioBufferWrapper(const std::shared_ptr &audioBuffer); - - std::shared_ptr audioBuffer_; - [[nodiscard]] int getNumberOfChannels() const; - [[nodiscard]] int getLength() const; - [[nodiscard]] double getDuration() const; - [[nodiscard]] int getSampleRate() const; - [[nodiscard]] float *getChannelData(int channel) const; - void copyFromChannel( - float *destination, - int destinationLength, - int channelNumber, - int startInChannel = 0) const; - void copyToChannel( - float *source, - int sourceLength, - int channelNumber, - int startInChannel = 0) const; -}; -} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/wrappers/AudioContextWrapper.cpp b/packages/react-native-audio-api/common/cpp/wrappers/AudioContextWrapper.cpp deleted file mode 100644 index 78dadc7f..00000000 --- a/packages/react-native-audio-api/common/cpp/wrappers/AudioContextWrapper.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include "AudioContextWrapper.h" - -namespace audioapi { - -AudioContextWrapper::AudioContextWrapper( - const std::shared_ptr &context) - : BaseAudioContextWrapper(context) {} - -void AudioContextWrapper::close() { - getAudioContextFromBaseAudioContext()->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 deleted file mode 100644 index f390f495..00000000 --- a/packages/react-native-audio-api/common/cpp/wrappers/AudioContextWrapper.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include - -#include "AudioContext.h" -#include "BaseAudioContextWrapper.h" - -namespace audioapi { - -class AudioContextWrapper : public BaseAudioContextWrapper { - public: - explicit AudioContextWrapper(const std::shared_ptr &context); - - void close(); - - private: - std::shared_ptr getAudioContextFromBaseAudioContext(); -}; -} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/wrappers/AudioDestinationNodeWrapper.h b/packages/react-native-audio-api/common/cpp/wrappers/AudioDestinationNodeWrapper.h deleted file mode 100644 index 5d3b7fd3..00000000 --- a/packages/react-native-audio-api/common/cpp/wrappers/AudioDestinationNodeWrapper.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include - -#include "AudioDestinationNode.h" -#include "AudioNodeWrapper.h" - -namespace audioapi { - -class AudioDestinationNodeWrapper : public AudioNodeWrapper { - public: - explicit AudioDestinationNodeWrapper( - const std::shared_ptr &destinationNode) - : AudioNodeWrapper(destinationNode) {} -}; -} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/wrappers/AudioNodeWrapper.cpp b/packages/react-native-audio-api/common/cpp/wrappers/AudioNodeWrapper.cpp deleted file mode 100644 index cf997e3c..00000000 --- a/packages/react-native-audio-api/common/cpp/wrappers/AudioNodeWrapper.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include "AudioNodeWrapper.h" - -namespace audioapi { - -AudioNodeWrapper::AudioNodeWrapper(const std::shared_ptr &node) - : node_(node) {} - -int AudioNodeWrapper::getNumberOfInputs() const { - return node_->getNumberOfInputs(); -} - -int AudioNodeWrapper::getNumberOfOutputs() const { - return node_->getNumberOfOutputs(); -} - -int AudioNodeWrapper::getChannelCount() const { - return node_->getChannelCount(); -} - -std::string AudioNodeWrapper::getChannelCountMode() const { - return node_->getChannelCountMode(); -} - -std::string AudioNodeWrapper::getChannelInterpretation() const { - return node_->getChannelInterpretation(); -} - -void AudioNodeWrapper::connect( - const std::shared_ptr &node) const { - node_->connect(node->node_); -} - -void AudioNodeWrapper::disconnect( - const std::shared_ptr &node) const { - node_->disconnect(node->node_); -} -} // 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 deleted file mode 100644 index 3c7e0c24..00000000 --- a/packages/react-native-audio-api/common/cpp/wrappers/AudioNodeWrapper.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once - -#include -#include - -#include "AudioNode.h" - -namespace audioapi { - -class AudioNodeWrapper { - public: - explicit AudioNodeWrapper(const std::shared_ptr &node); - - [[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; - - protected: - std::shared_ptr node_; -}; -} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/wrappers/AudioParamWrapper.cpp b/packages/react-native-audio-api/common/cpp/wrappers/AudioParamWrapper.cpp deleted file mode 100644 index a0c6d303..00000000 --- a/packages/react-native-audio-api/common/cpp/wrappers/AudioParamWrapper.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "AudioParamWrapper.h" - -namespace audioapi { - -AudioParamWrapper::AudioParamWrapper(const std::shared_ptr ¶m) - : param_(param) {} - -float AudioParamWrapper::getValue() const { - return param_->getValue(); -} - -void AudioParamWrapper::setValue(float value) const { - param_->setValue(value); -} - -float AudioParamWrapper::getDefaultValue() const { - return param_->getDefaultValue(); -} - -float AudioParamWrapper::getMinValue() const { - return param_->getMinValue(); -} - -float AudioParamWrapper::getMaxValue() const { - return param_->getMaxValue(); -} - -void AudioParamWrapper::setValueAtTime(float value, double startTime) const { - param_->setValueAtTime(value, startTime); -} - -void AudioParamWrapper::linearRampToValueAtTime(float value, double endTime) - const { - param_->linearRampToValueAtTime(value, endTime); -} - -void AudioParamWrapper::exponentialRampToValueAtTime( - float value, - double endTime) const { - param_->exponentialRampToValueAtTime(value, endTime); -} -} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/wrappers/AudioParamWrapper.h b/packages/react-native-audio-api/common/cpp/wrappers/AudioParamWrapper.h deleted file mode 100644 index ec3da4fb..00000000 --- a/packages/react-native-audio-api/common/cpp/wrappers/AudioParamWrapper.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once - -#include - -#include "AudioParam.h" - -namespace audioapi { - -class AudioParamWrapper { - public: - explicit AudioParamWrapper(const std::shared_ptr ¶m); - - [[nodiscard]] float getValue() const; - void setValue(float value) 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; - - private: - std::shared_ptr param_; -}; -} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/wrappers/AudioScheduledSourceNodeWrapper.cpp b/packages/react-native-audio-api/common/cpp/wrappers/AudioScheduledSourceNodeWrapper.cpp deleted file mode 100644 index 1a372013..00000000 --- a/packages/react-native-audio-api/common/cpp/wrappers/AudioScheduledSourceNodeWrapper.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include "AudioScheduledSourceNodeWrapper.h" - -namespace audioapi { - -AudioScheduledSourceNodeWrapper::AudioScheduledSourceNodeWrapper( - const std::shared_ptr &audioScheduledSourceNode) - : AudioNodeWrapper(audioScheduledSourceNode) {} - -std::shared_ptr -AudioScheduledSourceNodeWrapper::getAudioScheduledSourceNodeFromAudioNode() { - return std::static_pointer_cast(node_); -} - -void AudioScheduledSourceNodeWrapper::start(double time) { - auto audioScheduledSourceNode = getAudioScheduledSourceNodeFromAudioNode(); - audioScheduledSourceNode->start(time); -} - -void AudioScheduledSourceNodeWrapper::stop(double time) { - auto audioScheduledSourceNode = getAudioScheduledSourceNodeFromAudioNode(); - audioScheduledSourceNode->stop(time); -} -} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/wrappers/AudioScheduledSourceNodeWrapper.h b/packages/react-native-audio-api/common/cpp/wrappers/AudioScheduledSourceNodeWrapper.h deleted file mode 100644 index 673730dc..00000000 --- a/packages/react-native-audio-api/common/cpp/wrappers/AudioScheduledSourceNodeWrapper.h +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - -#include - -#include "AudioNodeWrapper.h" -#include "AudioScheduledSourceNode.h" - -namespace audioapi { - -class AudioScheduledSourceNodeWrapper : public AudioNodeWrapper { - public: - explicit AudioScheduledSourceNodeWrapper( - const std::shared_ptr - &audioScheduledSourceNode); - - void start(double time); - void stop(double time); - - private: - std::shared_ptr - getAudioScheduledSourceNodeFromAudioNode(); -}; -} // 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 deleted file mode 100644 index f57824f5..00000000 --- a/packages/react-native-audio-api/common/cpp/wrappers/BaseAudioContextWrapper.cpp +++ /dev/null @@ -1,83 +0,0 @@ -#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::string BaseAudioContextWrapper::getState() const { - return context_->getState(); -} - -int BaseAudioContextWrapper::getSampleRate() const { - return context_->getSampleRate(); -} - -double BaseAudioContextWrapper::getCurrentTime() const { - return context_->getCurrentTime(); -} - -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::shared_ptr -BaseAudioContextWrapper::createPeriodicWave( - float *real, - float *imag, - bool disableNormalization, - int length) { - auto periodicWave = - context_->createPeriodicWave(real, imag, disableNormalization, length); - return std::make_shared(periodicWave); -} - -std::shared_ptr -BaseAudioContextWrapper::decodeAudioDataSource(const std::string &path) { - return std::make_shared( - context_->decodeAudioDataSource(path)); -} - -} // 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 deleted file mode 100644 index 19e5120d..00000000 --- a/packages/react-native-audio-api/common/cpp/wrappers/BaseAudioContextWrapper.h +++ /dev/null @@ -1,51 +0,0 @@ -#pragma once - -#include -#include -#include - -#include "AudioBufferSourceNodeWrapper.h" -#include "AudioBufferWrapper.h" -#include "AudioDestinationNodeWrapper.h" -#include "BaseAudioContext.h" -#include "BiquadFilterNodeWrapper.h" -#include "GainNodeWrapper.h" -#include "OscillatorNodeWrapper.h" -#include "PeriodicWaveWrapper.h" -#include "StereoPannerNodeWrapper.h" - -namespace audioapi { - -class BaseAudioContextWrapper { - public: - explicit BaseAudioContextWrapper( - const std::shared_ptr &context); - - [[nodiscard]] std::shared_ptr getDestination() - const; - [[nodiscard]] std::string getState() const; - [[nodiscard]] int getSampleRate() const; - [[nodiscard]] double getCurrentTime() 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; - [[nodiscard]] std::shared_ptr createPeriodicWave( - float *real, - float *imag, - bool disableNormalization, - int length); - std::shared_ptr decodeAudioDataSource( - const std::string &path); - - protected: - std::shared_ptr destination_; - std::shared_ptr context_; -}; -} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/wrappers/BiquadFilterNodeWrapper.cpp b/packages/react-native-audio-api/common/cpp/wrappers/BiquadFilterNodeWrapper.cpp deleted file mode 100644 index 7e4f6e3a..00000000 --- a/packages/react-native-audio-api/common/cpp/wrappers/BiquadFilterNodeWrapper.cpp +++ /dev/null @@ -1,60 +0,0 @@ -#include "BiquadFilterNodeWrapper.h" - -namespace audioapi { - -std::shared_ptr -BiquadFilterNodeWrapper::getBiquadFilterNodeFromAudioNode() { - return std::static_pointer_cast(node_); -} - -BiquadFilterNodeWrapper::BiquadFilterNodeWrapper( - const std::shared_ptr &biquadFilterNode) - : AudioNodeWrapper(biquadFilterNode) { - auto frequencyParam = biquadFilterNode->getFrequencyParam(); - frequencyParam_ = std::make_shared(frequencyParam); - auto detuneParam = biquadFilterNode->getDetuneParam(); - detuneParam_ = std::make_shared(detuneParam); - auto QParam = biquadFilterNode->getQParam(); - QParam_ = std::make_shared(QParam); - auto gainParam = biquadFilterNode->getGainParam(); - gainParam_ = std::make_shared(gainParam); -} - -std::shared_ptr BiquadFilterNodeWrapper::getFrequencyParam() - const { - return frequencyParam_; -} - -std::shared_ptr BiquadFilterNodeWrapper::getDetuneParam() - const { - return detuneParam_; -} - -std::shared_ptr BiquadFilterNodeWrapper::getQParam() const { - return QParam_; -} - -std::shared_ptr BiquadFilterNodeWrapper::getGainParam() - const { - return gainParam_; -} - -std::string BiquadFilterNodeWrapper::getType() { - auto biquadFilterNode_ = getBiquadFilterNodeFromAudioNode(); - return biquadFilterNode_->getType(); -} - -void BiquadFilterNodeWrapper::setType(const std::string &filterType) { - auto biquadFilterNode_ = getBiquadFilterNodeFromAudioNode(); - biquadFilterNode_->setType(filterType); -} - -void BiquadFilterNodeWrapper::getFrequencyResponse( - const std::vector &frequencyArray, - std::vector &magResponseOutput, - std::vector &phaseResponseOutput) { - auto biquadFilterNode_ = getBiquadFilterNodeFromAudioNode(); - biquadFilterNode_->getFrequencyResponse( - frequencyArray, magResponseOutput, phaseResponseOutput); -} -} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/wrappers/BiquadFilterNodeWrapper.h b/packages/react-native-audio-api/common/cpp/wrappers/BiquadFilterNodeWrapper.h deleted file mode 100644 index 90e6f41d..00000000 --- a/packages/react-native-audio-api/common/cpp/wrappers/BiquadFilterNodeWrapper.h +++ /dev/null @@ -1,37 +0,0 @@ -#pragma once - -#include -#include -#include - -#include "AudioNodeWrapper.h" -#include "AudioParamWrapper.h" -#include "BiquadFilterNode.h" - -namespace audioapi { - -class BiquadFilterNodeWrapper : public AudioNodeWrapper { - public: - explicit BiquadFilterNodeWrapper( - const std::shared_ptr &biquadFilterNode); - - [[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); - void getFrequencyResponse( - const std::vector &frequencyArray, - std::vector &magResponseOutput, - std::vector &phaseResponseOutput); - - private: - std::shared_ptr frequencyParam_; - std::shared_ptr detuneParam_; - std::shared_ptr QParam_; - std::shared_ptr gainParam_; - - std::shared_ptr getBiquadFilterNodeFromAudioNode(); -}; -} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/wrappers/GainNodeWrapper.cpp b/packages/react-native-audio-api/common/cpp/wrappers/GainNodeWrapper.cpp deleted file mode 100644 index 6faf5fe9..00000000 --- a/packages/react-native-audio-api/common/cpp/wrappers/GainNodeWrapper.cpp +++ /dev/null @@ -1,14 +0,0 @@ -#include "GainNodeWrapper.h" - -namespace audioapi { - -GainNodeWrapper::GainNodeWrapper(const std::shared_ptr &gainNode) - : AudioNodeWrapper(gainNode) { - auto gainParam = gainNode->getGainParam(); - gainParam_ = std::make_shared(gainParam); -} - -std::shared_ptr GainNodeWrapper::getGainParam() const { - return gainParam_; -} -} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/wrappers/GainNodeWrapper.h b/packages/react-native-audio-api/common/cpp/wrappers/GainNodeWrapper.h deleted file mode 100644 index 77581f6f..00000000 --- a/packages/react-native-audio-api/common/cpp/wrappers/GainNodeWrapper.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once - -#include - -#include "AudioNodeWrapper.h" -#include "AudioParamWrapper.h" -#include "GainNode.h" - -namespace audioapi { - -class GainNodeWrapper : public AudioNodeWrapper { - public: - explicit GainNodeWrapper(const std::shared_ptr &gainNode); - - [[nodiscard]] std::shared_ptr getGainParam() const; - - private: - std::shared_ptr gainParam_; -}; -} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/wrappers/OscillatorNodeWrapper.cpp b/packages/react-native-audio-api/common/cpp/wrappers/OscillatorNodeWrapper.cpp deleted file mode 100644 index f85841af..00000000 --- a/packages/react-native-audio-api/common/cpp/wrappers/OscillatorNodeWrapper.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include "OscillatorNodeWrapper.h" - -namespace audioapi { - -std::shared_ptr -OscillatorNodeWrapper::getOscillatorNodeFromAudioNode() { - return std::static_pointer_cast(node_); -} - -OscillatorNodeWrapper::OscillatorNodeWrapper( - const std::shared_ptr &oscillator) - : AudioScheduledSourceNodeWrapper(oscillator) { - auto frequencyParam = oscillator->getFrequencyParam(); - frequencyParam_ = std::make_shared(frequencyParam); - auto detuneParam = oscillator->getDetuneParam(); - detuneParam_ = std::make_shared(detuneParam); -} - -std::shared_ptr OscillatorNodeWrapper::getFrequencyParam() - const { - return frequencyParam_; -} - -std::shared_ptr OscillatorNodeWrapper::getDetuneParam() - const { - return detuneParam_; -} - -std::string OscillatorNodeWrapper::getType() { - auto oscillatorNode_ = getOscillatorNodeFromAudioNode(); - return oscillatorNode_->getType(); -} - -void OscillatorNodeWrapper::setType(const std::string &type) { - auto oscillatorNode_ = getOscillatorNodeFromAudioNode(); - oscillatorNode_->setType(type); -} - -void OscillatorNodeWrapper::setPeriodicWave( - const std::shared_ptr &periodicWave) { - auto oscillatorNode_ = getOscillatorNodeFromAudioNode(); - oscillatorNode_->setPeriodicWave(periodicWave->periodicWave_); -} -} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/wrappers/OscillatorNodeWrapper.h b/packages/react-native-audio-api/common/cpp/wrappers/OscillatorNodeWrapper.h deleted file mode 100644 index a37aea07..00000000 --- a/packages/react-native-audio-api/common/cpp/wrappers/OscillatorNodeWrapper.h +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once - -#include -#include - -#include "AudioParamWrapper.h" -#include "AudioScheduledSourceNodeWrapper.h" -#include "OscillatorNode.h" -#include "PeriodicWaveWrapper.h" - -namespace audioapi { - -class OscillatorNodeWrapper : public AudioScheduledSourceNodeWrapper { - public: - explicit OscillatorNodeWrapper( - const std::shared_ptr &oscillatorNode); - - [[nodiscard]] std::shared_ptr getFrequencyParam() const; - [[nodiscard]] std::shared_ptr getDetuneParam() const; - std::string getType(); - void setType(const std::string &type); - void setPeriodicWave( - const std::shared_ptr &periodicWave); - - private: - std::shared_ptr frequencyParam_; - std::shared_ptr detuneParam_; - - std::shared_ptr getOscillatorNodeFromAudioNode(); -}; -} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/wrappers/PeriodicWaveWrapper.h b/packages/react-native-audio-api/common/cpp/wrappers/PeriodicWaveWrapper.h deleted file mode 100644 index a381e537..00000000 --- a/packages/react-native-audio-api/common/cpp/wrappers/PeriodicWaveWrapper.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#include - -#include "PeriodicWave.h" - -namespace audioapi { - -class PeriodicWaveWrapper { - public: - explicit PeriodicWaveWrapper( - const std::shared_ptr &periodicWave) - : periodicWave_(periodicWave) {} - - std::shared_ptr periodicWave_; -}; -} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/wrappers/StereoPannerNodeWrapper.cpp b/packages/react-native-audio-api/common/cpp/wrappers/StereoPannerNodeWrapper.cpp deleted file mode 100644 index 78d4ec36..00000000 --- a/packages/react-native-audio-api/common/cpp/wrappers/StereoPannerNodeWrapper.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "StereoPannerNodeWrapper.h" - -namespace audioapi { - -StereoPannerNodeWrapper::StereoPannerNodeWrapper( - const std::shared_ptr &stereoPannerNode) - : AudioNodeWrapper(stereoPannerNode) { - auto panParam = stereoPannerNode->getPanParam(); - panParam_ = std::make_shared(panParam); -} - -std::shared_ptr StereoPannerNodeWrapper::getPanParam() - const { - return panParam_; -} -} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/wrappers/StereoPannerNodeWrapper.h b/packages/react-native-audio-api/common/cpp/wrappers/StereoPannerNodeWrapper.h deleted file mode 100644 index ad7fa408..00000000 --- a/packages/react-native-audio-api/common/cpp/wrappers/StereoPannerNodeWrapper.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#include - -#include "AudioNodeWrapper.h" -#include "AudioParamWrapper.h" -#include "StereoPannerNode.h" - -namespace audioapi { - -class StereoPannerNodeWrapper : public AudioNodeWrapper { - public: - explicit StereoPannerNodeWrapper( - const std::shared_ptr &stereoPannerNode); - - [[nodiscard]] std::shared_ptr getPanParam() const; - - private: - std::shared_ptr panParam_; -}; -} // namespace audioapi diff --git a/packages/react-native-audio-api/ios/AudioAPIModule.mm b/packages/react-native-audio-api/ios/AudioAPIModule.mm index 8473c6ff..c6451481 100644 --- a/packages/react-native-audio-api/ios/AudioAPIModule.mm +++ b/packages/react-native-audio-api/ios/AudioAPIModule.mm @@ -7,7 +7,6 @@ #import #import "AudioAPIInstallerHostObject.h" -#import "AudioAPIInstallerWrapper.h" @implementation AudioAPIModule @@ -34,11 +33,8 @@ @implementation AudioAPIModule auto &runtime = *jsRuntime; - auto wrapper = std::make_shared(); - auto hostObject = - std::make_shared(wrapper, jsRuntime, cxxBridge.jsCallInvoker); - auto object = jsi::Object::createFromHostObject(runtime, hostObject); - runtime.global().setProperty(runtime, "__AudioAPIInstaller", std::move(object)); + auto hostObject = std::make_shared(jsRuntime, cxxBridge.jsCallInvoker); + hostObject->install(); NSLog(@"Successfully installed JSI bindings for react-native-audio-api!"); return @true; diff --git a/packages/react-native-audio-api/ios/AudioDecoder/AudioDecoder.m b/packages/react-native-audio-api/ios/AudioDecoder/AudioDecoder.m index a6d57471..619a8d06 100644 --- a/packages/react-native-audio-api/ios/AudioDecoder/AudioDecoder.m +++ b/packages/react-native-audio-api/ios/AudioDecoder/AudioDecoder.m @@ -51,16 +51,15 @@ - (AVAudioPCMBuffer *)convertBuffer:(AVAudioPCMBuffer *)buffer ToFormat:(AVAudio [[AVAudioPCMBuffer alloc] initWithPCMFormat:format frameCapacity:(AVAudioFrameCount)buffer.frameCapacity]; AVAudioConverterInputBlock inputBlock = - ^AVAudioBuffer *(AVAudioPacketCount inNumberOfPackets, AVAudioConverterInputStatus *outStatus) - { - if (buffer.frameLength > 0) { - *outStatus = AVAudioConverterInputStatus_HaveData; - return buffer; - } else { - *outStatus = AVAudioConverterInputStatus_NoDataNow; - return nil; - } - }; + ^AVAudioBuffer *(AVAudioPacketCount inNumberOfPackets, AVAudioConverterInputStatus *outStatus) { + if (buffer.frameLength > 0) { + *outStatus = AVAudioConverterInputStatus_HaveData; + return buffer; + } else { + *outStatus = AVAudioConverterInputStatus_NoDataNow; + return nil; + } + }; [converter convertToBuffer:convertedBuffer error:&error withInputFromBlock:inputBlock]; diff --git a/packages/react-native-audio-api/src/index.ts b/packages/react-native-audio-api/src/index.ts index 42ac2036..29c2462c 100644 --- a/packages/react-native-audio-api/src/index.ts +++ b/packages/react-native-audio-api/src/index.ts @@ -6,6 +6,7 @@ export class AudioBuffer { readonly sampleRate: number; readonly numberOfChannels: number; + /** @internal */ public readonly buffer: globalThis.AudioBuffer; constructor(buffer: globalThis.AudioBuffer) {