-
-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/android/oscillator #24
Changes from 3 commits
e602011
c2b37c0
b8d17ac
0f002fd
4992eff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include <fbjni/fbjni.h> | ||
#include "Oscillator.h" | ||
#include "OscillatorHostObject.h" | ||
|
||
using namespace audiocontext; | ||
|
||
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) | ||
{ | ||
return facebook::jni::initialize(vm, [] { | ||
Oscillator::registerNatives(); | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include "Oscillator.h" | ||
#include <fbjni/fbjni.h> | ||
#include <jsi/jsi.h> | ||
|
||
namespace audiocontext { | ||
|
||
using namespace facebook::jni; | ||
|
||
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); | ||
auto object = jsi::Object::createFromHostObject(*runtime, hostObject); | ||
runtime->global().setProperty(*runtime, "__OscillatorProxy", std::move(object)); | ||
} | ||
|
||
void Oscillator::start() { | ||
static const auto method = javaClassStatic()->getMethod<void()>("start"); | ||
method(javaObject_.get()); | ||
} | ||
|
||
void Oscillator::stop() { | ||
static const auto method = javaClassStatic()->getMethod<void()>("stop"); | ||
method(javaObject_.get()); | ||
} | ||
|
||
} // namespace audiocontext |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#pragma once | ||
|
||
#include <fbjni/fbjni.h> | ||
#include <jsi/jsi.h> | ||
#include <react/jni/CxxModuleWrapper.h> | ||
#include <react/jni/JMessageQueueThread.h> | ||
#include "OscillatorHostObject.h" | ||
|
||
namespace audiocontext { | ||
|
||
using namespace facebook; | ||
using namespace facebook::jni; | ||
|
||
class Oscillator : public jni::HybridClass<Oscillator> { | ||
public: | ||
static auto constexpr kJavaDescriptor = "Lcom/audiocontext/Oscillator;"; | ||
|
||
static jni::local_ref<Oscillator::jhybriddata> initHybrid(jni::alias_ref<jhybridobject> jThis, jlong jsContext) | ||
{ | ||
return makeCxxInstance(jThis, jsContext); | ||
} | ||
|
||
static void registerNatives() { | ||
registerHybrid({ | ||
makeNativeMethod("initHybrid", Oscillator::initHybrid), | ||
}); | ||
} | ||
|
||
void start(); | ||
void stop(); | ||
|
||
private: | ||
friend HybridBase; | ||
|
||
global_ref<Oscillator::javaobject> javaObject_; | ||
|
||
explicit Oscillator(jni::alias_ref<Oscillator::jhybridobject>& jThis, jlong jsContext); | ||
}; | ||
|
||
} // namespace audiocontext |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,82 @@ | ||||||||||
package com.audiocontext | ||||||||||
|
||||||||||
import android.media.AudioFormat | ||||||||||
import android.media.AudioManager | ||||||||||
import android.media.AudioTrack | ||||||||||
import com.audiocontext.nodes.oscillator.WaveType | ||||||||||
import com.facebook.jni.HybridData | ||||||||||
import com.facebook.react.bridge.ReactApplicationContext | ||||||||||
import kotlin.math.abs | ||||||||||
import kotlin.math.floor | ||||||||||
import kotlin.math.sin | ||||||||||
|
||||||||||
class Oscillator(reactContext: ReactApplicationContext) { | ||||||||||
val numberOfInputs: Int = 0 | ||||||||||
val numberOfOutputs: Int = 1 | ||||||||||
private var frequency: Double = 440.0 | ||||||||||
private var detune: Double = 0.0 | ||||||||||
private var waveType: WaveType = WaveType.SINE | ||||||||||
|
||||||||||
private val audioTrack: AudioTrack | ||||||||||
@Volatile private var isPlaying: Boolean = false | ||||||||||
private var playbackThread: Thread? = null | ||||||||||
private var buffer: ShortArray = ShortArray(1024) | ||||||||||
|
||||||||||
private val mHybridData: HybridData?; | ||||||||||
|
||||||||||
companion object { | ||||||||||
init { | ||||||||||
System.loadLibrary("react-native-audio-context") | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
init { | ||||||||||
mHybridData = initHybrid(reactContext.javaScriptContextHolder!!.get()) | ||||||||||
|
||||||||||
val bufferSize = AudioTrack.getMinBufferSize( | ||||||||||
44100, | ||||||||||
AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT) | ||||||||||
this.audioTrack = AudioTrack( | ||||||||||
AudioManager.STREAM_MUSIC, 44100, AudioFormat.CHANNEL_OUT_MONO, | ||||||||||
AudioFormat.ENCODING_PCM_16BIT, bufferSize, AudioTrack.MODE_STREAM | ||||||||||
) | ||||||||||
} | ||||||||||
|
||||||||||
external fun initHybrid(l: Long): HybridData? | ||||||||||
|
||||||||||
fun start() { | ||||||||||
if(isPlaying) return | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
:) |
||||||||||
isPlaying = true | ||||||||||
audioTrack.play() | ||||||||||
playbackThread = Thread { generateSound() }.apply{ start()} | ||||||||||
} | ||||||||||
|
||||||||||
fun stop() { | ||||||||||
if(!isPlaying) return | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
:) |
||||||||||
isPlaying = false | ||||||||||
audioTrack.stop() | ||||||||||
playbackThread?.join() | ||||||||||
} | ||||||||||
|
||||||||||
private fun generateSound() { | ||||||||||
var wavePhase = 0.0 | ||||||||||
var phaseChange: Double | ||||||||||
|
||||||||||
while(isPlaying) { | ||||||||||
phaseChange = 2 * Math.PI * (frequency + detune) / 44100 | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and use it also here :) |
||||||||||
|
||||||||||
for(i in buffer.indices) { | ||||||||||
buffer[i] = when(waveType) { | ||||||||||
WaveType.SINE -> (sin(wavePhase) * Short.MAX_VALUE).toInt().toShort() | ||||||||||
WaveType.SQUARE -> ((if (sin(wavePhase) >= 0) 1 else -1) * Short.MAX_VALUE).toShort() | ||||||||||
WaveType.SAWTOOTH -> ((2 * (wavePhase / (2 * Math.PI) - floor(wavePhase / (2 * Math.PI) + 0.5))) * Short.MAX_VALUE).toInt().toShort() | ||||||||||
WaveType.TRIANGLE -> ((2 * abs(2 * (wavePhase / (2 * Math.PI) - floor(wavePhase / (2 * Math.PI) + 0.5))) - 1) * Short.MAX_VALUE).toInt().toShort() | ||||||||||
} | ||||||||||
wavePhase += phaseChange | ||||||||||
} | ||||||||||
|
||||||||||
audioTrack.write(buffer, 0, buffer.size) | ||||||||||
} | ||||||||||
audioTrack.flush() | ||||||||||
} | ||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.audiocontext.nodes.oscillator | ||
|
||
enum class WaveType { | ||
SINE, | ||
SQUARE, | ||
SAWTOOTH, | ||
TRIANGLE | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.audiocontext.nativemodules | ||
|
||
import com.audiocontext.Oscillator | ||
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" | ||
} | ||
|
||
@ReactMethod(isBlockingSynchronousMethod = true) | ||
fun createOscillator() { | ||
Oscillator(reactContext) | ||
} | ||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would to be good to move
44100
to be a constant