diff --git a/android/src/main/cpp/AudioContext.cpp b/android/src/main/cpp/AudioContext.cpp index d0be76de..157ea32a 100644 --- a/android/src/main/cpp/AudioContext.cpp +++ b/android/src/main/cpp/AudioContext.cpp @@ -1,31 +1,35 @@ #include "AudioContext.h" -namespace audiocontext { - - using namespace facebook::jni; - - AudioContext::AudioContext(jni::alias_ref &jThis, - jlong jsContext): javaObject_(make_global(jThis)), jsContext_(jsContext) { - auto runtime = reinterpret_cast(jsContext); - auto hostObject = std::make_shared(this); - auto object = jsi::Object::createFromHostObject(*runtime, hostObject); - runtime->global().setProperty(*runtime, "__AudioContextProxy", std::move(object)); - } - - jsi::Object AudioContext::createOscillator() { - static const auto method = javaClassLocal()->getMethod("createOscillator"); - auto oscillator = method(javaObject_.get()); - auto oscillatorCppInstance = oscillator->cthis(); - - return oscillatorCppInstance->createOscillatorNodeHostObject(); - } - - jsi::Object AudioContext::getDestination() { - static const auto method = javaClassLocal()->getMethod("getDestination"); - auto destination = method(javaObject_.get()); - auto destinationCppInstance = destination->cthis(); - - return destinationCppInstance->createAudioDestinationHostObject(); - } +namespace audiocontext +{ + + using namespace facebook::jni; + + AudioContext::AudioContext(jni::alias_ref &jThis, + jlong jsContext) : javaObject_(make_global(jThis)), jsContext_(jsContext) + { + auto runtime = reinterpret_cast(jsContext); + auto hostObject = std::make_shared(this); + auto object = jsi::Object::createFromHostObject(*runtime, hostObject); + runtime->global().setProperty(*runtime, "__AudioContextProxy", std::move(object)); + } + + jsi::Object AudioContext::createOscillator() + { + static const auto method = javaClassLocal()->getMethod("createOscillator"); + auto oscillator = method(javaObject_.get()); + auto oscillatorCppInstance = oscillator->cthis(); + + return oscillatorCppInstance->createOscillatorNodeHostObject(); + } + + jsi::Object AudioContext::getDestination() + { + static const auto method = javaClassLocal()->getMethod("getDestination"); + auto destination = method(javaObject_.get()); + auto destinationCppInstance = destination->cthis(); + + return destinationCppInstance->createAudioDestinationHostObject(); + } } // namespace audiocontext diff --git a/android/src/main/cpp/AudioContext.h b/android/src/main/cpp/AudioContext.h index b7d02a38..dad880fe 100644 --- a/android/src/main/cpp/AudioContext.h +++ b/android/src/main/cpp/AudioContext.h @@ -8,36 +8,39 @@ #include "OscillatorNode.h" #include "AudioDestinationNode.h" -namespace audiocontext { +namespace audiocontext +{ - using namespace facebook; - using namespace facebook::jni; + using namespace facebook; + using namespace facebook::jni; - class AudioContext : public jni::HybridClass { - public: - static auto constexpr kJavaDescriptor = "Lcom/audiocontext/context/AudioContext;"; + class AudioContext : public jni::HybridClass + { + public: + static auto constexpr kJavaDescriptor = "Lcom/audiocontext/context/AudioContext;"; - static jni::local_ref initHybrid(jni::alias_ref jThis, jlong jsContext) - { - return makeCxxInstance(jThis, jsContext); - } + static jni::local_ref initHybrid(jni::alias_ref jThis, jlong jsContext) + { + return makeCxxInstance(jThis, jsContext); + } - static void registerNatives() { - registerHybrid({ - makeNativeMethod("initHybrid", AudioContext::initHybrid), - }); - } + static void registerNatives() + { + registerHybrid({ + makeNativeMethod("initHybrid", AudioContext::initHybrid), + }); + } - jsi::Object createOscillator(); - jsi::Object getDestination(); + jsi::Object createOscillator(); + jsi::Object getDestination(); - private: - friend HybridBase; + private: + friend HybridBase; - global_ref javaObject_; - jlong jsContext_; + global_ref javaObject_; + jlong jsContext_; - explicit AudioContext(jni::alias_ref& jThis, jlong jsContext); - }; + explicit AudioContext(jni::alias_ref &jThis, jlong jsContext); + }; } // namespace audiocontext diff --git a/cpp/OscillatorNodeHostObject.cpp b/cpp/OscillatorNodeHostObject.cpp index 514ad5f5..dc55083b 100644 --- a/cpp/OscillatorNodeHostObject.cpp +++ b/cpp/OscillatorNodeHostObject.cpp @@ -1,118 +1,137 @@ #include "OscillatorNodeHostObject.h" #include -namespace audiocontext { - using namespace facebook; - - std::vector OscillatorNodeHostObject::getPropertyNames(jsi::Runtime& runtime) { - std::vector propertyNames; - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "start")); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "stop")); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "frequency")); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "detune")); - propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "type")); - return propertyNames; +namespace audiocontext +{ + using namespace facebook; + + std::vector OscillatorNodeHostObject::getPropertyNames(jsi::Runtime &runtime) + { + std::vector propertyNames; + propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "start")); + propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "stop")); + propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "frequency")); + propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "detune")); + propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "type")); + return propertyNames; + } + + jsi::Value OscillatorNodeHostObject::get(jsi::Runtime &runtime, const jsi::PropNameID &propNameId) + { + auto propName = propNameId.utf8(runtime); + + if (propName == "start") + { + return start(runtime, propNameId); } - jsi::Value OscillatorNodeHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& propNameId) { - auto propName = propNameId.utf8(runtime); - - if (propName == "start") { - return start(runtime, propNameId); - } - - if (propName == "stop") { - return stop(runtime, propNameId); - } - - if (propName == "frequency") { - return frequency(runtime, propNameId); - } - - if (propName == "detune") { - return detune(runtime, propNameId); - } - - if (propName == "type") { - return waveType(runtime, propNameId); - } + if (propName == "stop") + { + return stop(runtime, propNameId); + } - if (propName == "connect") { - return connect(runtime, propNameId); - } + if (propName == "frequency") + { + return frequency(runtime, propNameId); + } - throw std::runtime_error("Prop not yet implemented!"); + if (propName == "detune") + { + return detune(runtime, propNameId); } - void OscillatorNodeHostObject::set(jsi::Runtime& runtime, const jsi::PropNameID& propNameId, const jsi::Value& value) { - auto propName = propNameId.utf8(runtime); + if (propName == "type") + { + return waveType(runtime, propNameId); + } - if (propName == "frequency") { - auto frequency = value.asNumber(); - oscillator_->setFrequency(frequency); - return; - } + if (propName == "connect") + { + return connect(runtime, propNameId); + } - if (propName == "detune") { - auto detune = value.asNumber(); - oscillator_->setDetune(detune); - return; - } + throw std::runtime_error("Prop not yet implemented!"); + } - if (propName == "type") { - auto waveType = value.asString(runtime).utf8(runtime); - oscillator_->setWaveType(waveType); - return; - } + void OscillatorNodeHostObject::set(jsi::Runtime &runtime, const jsi::PropNameID &propNameId, const jsi::Value &value) + { + auto propName = propNameId.utf8(runtime); - throw std::runtime_error("Not yet implemented!"); + if (propName == "frequency") + { + auto frequency = value.asNumber(); + oscillator_->setFrequency(frequency); + return; } - jsi::Value OscillatorNodeHostObject::start(jsi::Runtime& runtime, const jsi::PropNameID& propNameId) { - return jsi::Function::createFromHostFunction(runtime, propNameId, 0, [this](jsi::Runtime& rt, const jsi::Value& thisValue, const jsi::Value* args, size_t count) -> jsi::Value { - oscillator_->start(); - return jsi::Value::undefined(); - }); + if (propName == "detune") + { + auto detune = value.asNumber(); + oscillator_->setDetune(detune); + return; } - jsi::Value OscillatorNodeHostObject::stop(jsi::Runtime &runtime, - const jsi::PropNameID &propNameId) { - return jsi::Function::createFromHostFunction(runtime, propNameId, 0, [this](jsi::Runtime& rt, const jsi::Value& thisValue, const jsi::Value* args, size_t count) -> jsi::Value { - oscillator_->stop(); - return jsi::Value::undefined(); - }); + if (propName == "type") + { + auto waveType = value.asString(runtime).utf8(runtime); + oscillator_->setWaveType(waveType); + return; } - jsi::Value OscillatorNodeHostObject::frequency(jsi::Runtime &runtime, - const jsi::PropNameID &propNameId) { - return jsi::Function::createFromHostFunction(runtime, propNameId, 0, [this](jsi::Runtime& rt, const jsi::Value& thisValue, const jsi::Value* args, size_t count) -> jsi::Value { - auto frequency = oscillator_->getFrequency(); - return jsi::Value(frequency); - }); - } + throw std::runtime_error("Not yet implemented!"); + } - jsi::Value OscillatorNodeHostObject::detune(jsi::Runtime &runtime, - const jsi::PropNameID &propNameId) { - return jsi::Function::createFromHostFunction(runtime, propNameId, 0, [this](jsi::Runtime& rt, const jsi::Value& thisValue, const jsi::Value* args, size_t count) -> jsi::Value { + jsi::Value OscillatorNodeHostObject::start(jsi::Runtime &runtime, const jsi::PropNameID &propNameId) + { + return jsi::Function::createFromHostFunction(runtime, propNameId, 0, [this](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) -> jsi::Value + { + oscillator_->start(); + return jsi::Value::undefined(); }); + } + + jsi::Value OscillatorNodeHostObject::stop(jsi::Runtime &runtime, + const jsi::PropNameID &propNameId) + { + return jsi::Function::createFromHostFunction(runtime, propNameId, 0, [this](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) -> jsi::Value + { + oscillator_->stop(); + return jsi::Value::undefined(); }); + } + + jsi::Value OscillatorNodeHostObject::frequency(jsi::Runtime &runtime, + const jsi::PropNameID &propNameId) + { + return jsi::Function::createFromHostFunction(runtime, propNameId, 0, [this](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) -> jsi::Value + { + auto frequency = oscillator_->getFrequency(); + return jsi::Value(frequency); }); + } + + jsi::Value OscillatorNodeHostObject::detune(jsi::Runtime &runtime, + const jsi::PropNameID &propNameId) + { + return jsi::Function::createFromHostFunction(runtime, propNameId, 0, [this](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) -> jsi::Value + { auto detune = oscillator_->getDetune(); - return jsi::Value(detune); - }); - } - - jsi::Value - OscillatorNodeHostObject::waveType(jsi::Runtime &runtime, const jsi::PropNameID &propNameId) { - return jsi::Function::createFromHostFunction(runtime, propNameId, 0, [this](jsi::Runtime& rt, const jsi::Value& thisValue, const jsi::Value* args, size_t count) -> jsi::Value { + return jsi::Value(detune); }); + } + + jsi::Value + OscillatorNodeHostObject::waveType(jsi::Runtime &runtime, const jsi::PropNameID &propNameId) + { + return jsi::Function::createFromHostFunction(runtime, propNameId, 0, [this](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) -> jsi::Value + { auto waveType = oscillator_->getWaveType(); - return jsi::String::createFromUtf8(rt, waveType); - }); - } - - jsi::Value OscillatorNodeHostObject::connect(jsi::Runtime &runtime, - const jsi::PropNameID &propNameId) { - return jsi::Function::createFromHostFunction(runtime, propNameId, 1, [this](jsi::Runtime& rt, const jsi::Value& thisValue, const jsi::Value* args, size_t count) -> jsi::Value { + return jsi::String::createFromUtf8(rt, waveType); }); + } + + jsi::Value OscillatorNodeHostObject::connect(jsi::Runtime &runtime, + const jsi::PropNameID &propNameId) + { + 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 destination = args[0].asObject(rt).asHostObject(rt); oscillator_->connect(*destination->destination_); - return jsi::Value::undefined(); - }); - } + return jsi::Value::undefined(); }); + } } diff --git a/example/ios/AudioContextExample.xcodeproj/project.pbxproj b/example/ios/AudioContextExample.xcodeproj/project.pbxproj index ed428ae8..f35d8b5c 100644 --- a/example/ios/AudioContextExample.xcodeproj/project.pbxproj +++ b/example/ios/AudioContextExample.xcodeproj/project.pbxproj @@ -592,7 +592,10 @@ "-DFOLLY_CFG_NO_COROUTINES=1", "-DFOLLY_HAVE_CLOCK_GETTIME=1", ); - OTHER_LDFLAGS = "$(inherited) "; + OTHER_LDFLAGS = ( + "$(inherited)", + " ", + ); REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native"; SDKROOT = iphoneos; USE_HERMES = true; @@ -664,7 +667,10 @@ "-DFOLLY_CFG_NO_COROUTINES=1", "-DFOLLY_HAVE_CLOCK_GETTIME=1", ); - OTHER_LDFLAGS = "$(inherited) "; + OTHER_LDFLAGS = ( + "$(inherited)", + " ", + ); REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native"; SDKROOT = iphoneos; USE_HERMES = true; diff --git a/example/ios/Podfile b/example/ios/Podfile index 5dd276ef..bbce5e3e 100644 --- a/example/ios/Podfile +++ b/example/ios/Podfile @@ -1,5 +1,3 @@ -ENV['RCT_NEW_ARCH_ENABLED'] = '1' - # Resolve react_native_pods.rb with node to allow for hoisting require Pod::Executable.execute_command('node', ['-p', 'require.resolve( diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index b3eff311..841860c1 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -1395,15 +1395,15 @@ SPEC CHECKSUMS: React-jsitracing: 6b3c8c98313642140530f93c46f5a6ca4530b446 React-logger: fa92ba4d3a5d39ac450f59be2a3cec7b099f0304 React-Mapbuffer: 9f68550e7c6839d01411ac8896aea5c868eff63a - react-native-audio-context: 11ae1a36b7d91d7cbd04cf5d5e27a6a9fb145eb8 + react-native-audio-context: 4184c3164b1911b9a2632be8dfeecc0cb3d41c9b React-nativeconfig: fa5de9d8f4dbd5917358f8ad3ad1e08762f01dcb React-NativeModulesApple: 585d1b78e0597de364d259cb56007052d0bda5e5 React-perflogger: 7bb9ba49435ff66b666e7966ee10082508a203e8 React-RCTActionSheet: a2816ae2b5c8523c2bc18a8f874a724a096e6d97 React-RCTAnimation: e78f52d7422bac13e1213e25e9bcbf99be872e1a - React-RCTAppDelegate: 4843f73d1089552a7d7f4ec6d29e9942c8f5e161 + React-RCTAppDelegate: 24f46de486cfa3a9f46e4b0786eaf17d92e1e0c6 React-RCTBlob: 9f9d6599d1b00690704dadc4a4bc33a7e76938be - React-RCTFabric: 56eb7973b13cd9d7be03ca06f621ed0edd124b81 + React-RCTFabric: 609e66bb0371b9082c62ed677ee0614efe711bf2 React-RCTImage: 39dd5aee6b92213845e1e7a7c41865801dc33493 React-RCTLinking: 35d742a982f901f9ea416d772763e2da65c2dc7d React-RCTNetwork: b078576c0c896c71905f841716b9f9f5922111dc @@ -1422,6 +1422,6 @@ SPEC CHECKSUMS: SocketRocket: abac6f5de4d4d62d24e11868d7a2f427e0ef940d Yoga: 88480008ccacea6301ff7bf58726e27a72931c8d -PODFILE CHECKSUM: 1b2da41d5518b36ddce0d4e029682759a82955cc +PODFILE CHECKSUM: 122f19e943aa833c1ad02dd5fd0e0364e65fe9ce COCOAPODS: 1.14.3 diff --git a/ios/AudioContext.h b/ios/AudioContext.h deleted file mode 100644 index de2a3b10..00000000 --- a/ios/AudioContext.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifdef __cplusplus -#import "react-native-audio-context.h" -#endif - -#ifdef RCT_NEW_ARCH_ENABLED -#import "RNAudioContextSpec.h" - -@interface AudioContext : NSObject -#else -#import - -@interface AudioContext : NSObject -#endif - -@end diff --git a/ios/AudioContext.mm b/ios/AudioContext.mm deleted file mode 100644 index dca8c82e..00000000 --- a/ios/AudioContext.mm +++ /dev/null @@ -1,21 +0,0 @@ -#import "AudioContext.h" - -@implementation AudioContext -RCT_EXPORT_MODULE() - -// Don't compile this code when we build for the old architecture. -#ifdef RCT_NEW_ARCH_ENABLED -- (NSNumber *)multiply:(double)a b:(double)b { - NSNumber *result = @(audiocontext::multiply(a, b)); - - return result; -} - -- (std::shared_ptr)getTurboModule: - (const facebook::react::ObjCTurboModule::InitParams &)params -{ - return std::make_shared(params); -} -#endif - -@end diff --git a/ios/JSIExampleModule.h b/ios/JSIExampleModule.h new file mode 100644 index 00000000..bcf1dd02 --- /dev/null +++ b/ios/JSIExampleModule.h @@ -0,0 +1,5 @@ +#import + +@interface JSIExampleModule : NSObject + +@end diff --git a/ios/JSIExampleModule.mm b/ios/JSIExampleModule.mm new file mode 100644 index 00000000..0e3fb779 --- /dev/null +++ b/ios/JSIExampleModule.mm @@ -0,0 +1,38 @@ +#import "JSIExampleModule.h" + +#import +#import +#import + +#import "../cpp/JSIExampleHostObject.h" + +@implementation JSIExampleModule + +RCT_EXPORT_MODULE(JSIExample) + +RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(install) +{ + NSLog(@"Installing JSI bindings for react-native-audio-context..."); + RCTBridge* bridge = [RCTBridge currentBridge]; + RCTCxxBridge* cxxBridge = (RCTCxxBridge*)bridge; + if (cxxBridge == nil) { + return @false; + } + + using namespace facebook; + + auto jsiRuntime = (jsi::Runtime*) cxxBridge.runtime; + if (jsiRuntime == nil) { + return @false; + } + auto& runtime = *jsiRuntime; + + auto hostObject = std::make_shared(); + auto object = jsi::Object::createFromHostObject(runtime, hostObject); + runtime.global().setProperty(runtime, "__JSIExampleProxy", std::move(object)); + + NSLog(@"Successfully installed JSI bindings for react-native-audio-context!"); + return @true; +} + +@end diff --git a/package.json b/package.json index 32814572..2b2771ac 100644 --- a/package.json +++ b/package.json @@ -164,7 +164,7 @@ "jsSrcsDir": "src" }, "create-react-native-library": { - "type": "module-new", + "type": "module-legacy", "languages": "cpp", "version": "0.37.1" }