Skip to content

Commit

Permalink
feat: implemented working oscillator node using jni and jsi
Browse files Browse the repository at this point in the history
  • Loading branch information
Maciej Makowski committed Jul 11, 2024
1 parent e602011 commit c2b37c0
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 107 deletions.
2 changes: 1 addition & 1 deletion android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ include_directories(
)

add_library(react-native-audio-context SHARED
src/main/cpp/OnLoad.cpp
src/main/cpp/Oscillator
../cpp/OscillatorHostObject
cpp-adapter.cpp
)

find_package(ReactAndroid REQUIRED CONFIG)
Expand Down
20 changes: 0 additions & 20 deletions android/cpp-adapter.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion android/src/main/cpp/Oscillator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace audiocontext {

using namespace facebook::jni;

Oscillator::Oscillator(const jni::alias_ref<Oscillator::jhybridobject> &jThis,
Oscillator::Oscillator(jni::alias_ref<Oscillator::jhybridobject> &jThis,
jlong jsContext): javaObject_(make_global(jThis)) {
auto runtime = reinterpret_cast<jsi::Runtime *>(jsContext);
auto hostObject = std::make_shared<OscillatorHostObject>(this);
Expand Down
10 changes: 5 additions & 5 deletions android/src/main/cpp/Oscillator.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ namespace audiocontext {
public:
static auto constexpr kJavaDescriptor = "Lcom/audiocontext/Oscillator;";

static jni::local_ref<jhybriddata> initHybrid(jni::alias_ref<jhybridobject> jThis, jlong jsContext)
static jni::local_ref<Oscillator::jhybriddata> initHybrid(jni::alias_ref<jhybridobject> jThis, jlong jsContext)
{
return makeCxxInstance(jThis, jsContext);
}

static void registerNatives() {
javaClassStatic()->registerNatives({
makeNativeMethod("initHybrid", Oscillator::initHybrid),
});
registerHybrid({
makeNativeMethod("initHybrid", Oscillator::initHybrid),
});
}

void start();
Expand All @@ -34,7 +34,7 @@ namespace audiocontext {

global_ref<Oscillator::javaobject> javaObject_;

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

} // namespace audiocontext
6 changes: 6 additions & 0 deletions android/src/main/java/com/audiocontext/Oscillator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ class Oscillator(reactContext: ReactApplicationContext) {

private val mHybridData: HybridData?;

companion object {
init {
System.loadLibrary("react-native-audio-context")
}
}

init {
mHybridData = initHybrid(reactContext.javaScriptContextHolder!!.get())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class AudioContextModule(private val reactContext: ReactApplicationContext) : Re
}

@ReactMethod(isBlockingSynchronousMethod = true)
fun createOscillatorNode() {
fun createOscillator() {
Oscillator(reactContext)
}
}
10 changes: 7 additions & 3 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
/* eslint-disable react/react-in-jsx-scope */
import { Button, StyleSheet, Text, View } from 'react-native';
//import { Oscillator } from '../../src/jsiOscillator';
import { AudioContext } from 'react-native-audio-context';
import type { OscillatorWrapper } from 'react-native-audio-context';

const App = () => {
AudioContext.createOscillator();
const oscillator = global.__OscillatorProxy as OscillatorWrapper;

const startOscillator = () => {
//Oscillator.start();
oscillator.start();
};

const stopOscillator = () => {
//Oscillator.stop();
oscillator.stop();
};

return (
Expand Down
8 changes: 8 additions & 0 deletions src/AudioContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { NativeModules } from 'react-native';
const { AudioContextModule } = NativeModules;

interface AudioContextInterface {
createOscillator(): void;
}

export const AudioContext = AudioContextModule as AudioContextInterface;
76 changes: 0 additions & 76 deletions src/AudioContext.tsx

This file was deleted.

11 changes: 11 additions & 0 deletions src/Oscillator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export interface OscillatorWrapper {
start(): undefined;
stop(): undefined;
}

declare global {
function nativeCallSyncHook(): unknown;
var __OscillatorProxy: OscillatorWrapper | undefined;
}

export const Oscillator = global.__OscillatorProxy as OscillatorWrapper;
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './Oscillator';
export * from './AudioContext';

0 comments on commit c2b37c0

Please sign in to comment.