Skip to content

Commit

Permalink
Merge branch 'feat/android/connect-method' into feat/android/wavetype
Browse files Browse the repository at this point in the history
  • Loading branch information
Maciej Makowski committed Jul 17, 2024
2 parents 4b3e83e + 2e04a4b commit 6380a76
Show file tree
Hide file tree
Showing 11 changed files with 225 additions and 188 deletions.
58 changes: 31 additions & 27 deletions android/src/main/cpp/AudioContext.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
#include "AudioContext.h"

namespace audiocontext {

using namespace facebook::jni;

AudioContext::AudioContext(jni::alias_ref<AudioContext::jhybridobject> &jThis,
jlong jsContext): javaObject_(make_global(jThis)), jsContext_(jsContext) {
auto runtime = reinterpret_cast<jsi::Runtime *>(jsContext);
auto hostObject = std::make_shared<AudioContextHostObject>(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<OscillatorNode()>("createOscillator");
auto oscillator = method(javaObject_.get());
auto oscillatorCppInstance = oscillator->cthis();

return oscillatorCppInstance->createOscillatorNodeHostObject();
}

jsi::Object AudioContext::getDestination() {
static const auto method = javaClassLocal()->getMethod<AudioDestinationNode()>("getDestination");
auto destination = method(javaObject_.get());
auto destinationCppInstance = destination->cthis();

return destinationCppInstance->createAudioDestinationHostObject();
}
namespace audiocontext
{

using namespace facebook::jni;

AudioContext::AudioContext(jni::alias_ref<AudioContext::jhybridobject> &jThis,
jlong jsContext) : javaObject_(make_global(jThis)), jsContext_(jsContext)
{
auto runtime = reinterpret_cast<jsi::Runtime *>(jsContext);
auto hostObject = std::make_shared<AudioContextHostObject>(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<OscillatorNode()>("createOscillator");
auto oscillator = method(javaObject_.get());
auto oscillatorCppInstance = oscillator->cthis();

return oscillatorCppInstance->createOscillatorNodeHostObject();
}

jsi::Object AudioContext::getDestination()
{
static const auto method = javaClassLocal()->getMethod<AudioDestinationNode()>("getDestination");
auto destination = method(javaObject_.get());
auto destinationCppInstance = destination->cthis();

return destinationCppInstance->createAudioDestinationHostObject();
}

} // namespace audiocontext
49 changes: 26 additions & 23 deletions android/src/main/cpp/AudioContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<AudioContext> {
public:
static auto constexpr kJavaDescriptor = "Lcom/audiocontext/context/AudioContext;";
class AudioContext : public jni::HybridClass<AudioContext>
{
public:
static auto constexpr kJavaDescriptor = "Lcom/audiocontext/context/AudioContext;";

static jni::local_ref<AudioContext::jhybriddata> initHybrid(jni::alias_ref<jhybridobject> jThis, jlong jsContext)
{
return makeCxxInstance(jThis, jsContext);
}
static jni::local_ref<AudioContext::jhybriddata> initHybrid(jni::alias_ref<jhybridobject> 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<AudioContext::javaobject> javaObject_;
jlong jsContext_;
global_ref<AudioContext::javaobject> javaObject_;
jlong jsContext_;

explicit AudioContext(jni::alias_ref<AudioContext::jhybridobject>& jThis, jlong jsContext);
};
explicit AudioContext(jni::alias_ref<AudioContext::jhybridobject> &jThis, jlong jsContext);
};

} // namespace audiocontext
205 changes: 112 additions & 93 deletions cpp/OscillatorNodeHostObject.cpp
Original file line number Diff line number Diff line change
@@ -1,118 +1,137 @@
#include "OscillatorNodeHostObject.h"
#include <android/log.h>

namespace audiocontext {
using namespace facebook;

std::vector<jsi::PropNameID> OscillatorNodeHostObject::getPropertyNames(jsi::Runtime& runtime) {
std::vector<jsi::PropNameID> 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<jsi::PropNameID> OscillatorNodeHostObject::getPropertyNames(jsi::Runtime &runtime)
{
std::vector<jsi::PropNameID> 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<AudioDestinationNodeHostObject>(rt);
oscillator_->connect(*destination->destination_);
return jsi::Value::undefined();
});
}
return jsi::Value::undefined(); });
}
}
10 changes: 8 additions & 2 deletions example/ios/AudioContextExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions example/ios/Podfile
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
Loading

0 comments on commit 6380a76

Please sign in to comment.