Skip to content

Commit

Permalink
fix: slightly better injection of AudioCpntext
Browse files Browse the repository at this point in the history
  • Loading branch information
Maciej Makowski committed Jul 19, 2024
1 parent fc190c8 commit 573e2b8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
22 changes: 12 additions & 10 deletions android/src/main/cpp/AudioContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ namespace audiocontext

using namespace facebook::jni;

AudioContext::AudioContext(jni::alias_ref<AudioContext::jhybridobject> &jThis,
jlong jsContext) : javaObject_(make_global(jThis)), jsContext_(jsContext)
{
auto audioContextWrapper = std::make_shared<AudioContextWrapper>(std::shared_ptr<AudioContext>(this));
auto runtime = reinterpret_cast<jsi::Runtime *>(jsContext);
auto hostObject = AudioContextHostObject::createFromWrapper(audioContextWrapper);

auto object = jsi::Object::createFromHostObject(*runtime, hostObject);
runtime->global().setProperty(*runtime, "__AudioContextProxy", std::move(object));
}
AudioContext::AudioContext(jni::alias_ref<AudioContext::jhybridobject> &jThis,
jlong jsContext) : javaObject_(make_global(jThis)), jsContext_(jsContext) {}

void AudioContext::install()
{
auto audioContextWrapper = std::make_shared<AudioContextWrapper>(std::shared_ptr<AudioContext>(this));
auto runtime = reinterpret_cast<jsi::Runtime *>(jsContext_);
auto hostObject = AudioContextHostObject::createFromWrapper(audioContextWrapper);

auto object = jsi::Object::createFromHostObject(*runtime, hostObject);
runtime->global().setProperty(*runtime, "__AudioContextProxy", std::move(object));
}

std::shared_ptr<OscillatorNode> AudioContext::createOscillator()
{
Expand Down
6 changes: 6 additions & 0 deletions android/src/main/cpp/AudioContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@ namespace audiocontext
registerHybrid({
makeNativeMethod("initHybrid", AudioContext::initHybrid),
});

javaClassLocal()->registerNatives({
makeNativeMethod("install", AudioContext::install),
});
}

std::shared_ptr<OscillatorNode> createOscillator();
std::shared_ptr<AudioDestinationNode> getDestination();

void install();

private:
friend HybridBase;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package com.audiocontext.context

import android.media.AudioTrack
import com.audiocontext.nodes.AudioDestinationNode
import com.audiocontext.nodes.AudioNode
import com.audiocontext.nodes.oscillator.OscillatorNode
import com.facebook.jni.HybridData
import com.facebook.react.bridge.ReactApplicationContext
import java.util.concurrent.CopyOnWriteArrayList

class AudioContext(private val reactContext: ReactApplicationContext) : BaseAudioContext {
class AudioContext(reactContext: ReactApplicationContext) : BaseAudioContext {
override val sampleRate: Int = 44100
override val destination: AudioDestinationNode = AudioDestinationNode(this)
get() = field
Expand All @@ -27,6 +24,8 @@ class AudioContext(private val reactContext: ReactApplicationContext) : BaseAudi

external fun initHybrid(l: Long): HybridData?

external fun install()

override fun createOscillator(): OscillatorNode {
return OscillatorNode(this)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package com.audiocontext.nativemodules

import com.audiocontext.context.AudioContext
import com.facebook.jni.HybridData
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod

class AudioContextModule(private val reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {

override fun getName(): String {
return "AudioContextModule"
return NAME
}

@ReactMethod(isBlockingSynchronousMethod = true)
fun initAudioContext() {
AudioContext(reactContext)
val audioContext = AudioContext(reactContext)
audioContext.install()
}

companion object {
const val NAME: String = "AudioContextModule"
}
}

0 comments on commit 573e2b8

Please sign in to comment.