Skip to content

Commit

Permalink
Merge pull request #32 from software-mansion-labs/feat/android/wavetype
Browse files Browse the repository at this point in the history
Feat/android/wavetype
  • Loading branch information
maciejmakowski2003 authored Jul 17, 2024
2 parents eb97dff + 6380a76 commit 8b3e4ba
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 23 deletions.
38 changes: 25 additions & 13 deletions android/src/main/cpp/OscillatorNode.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "OscillatorNode.h"
#include "android/log.h"

namespace audiocontext {

Expand All @@ -14,33 +15,44 @@ namespace audiocontext {
}

void OscillatorNode::start() {
static const auto method = javaClassStatic()->getMethod<void()>("start");
static const auto method = javaClassLocal()->getMethod<void()>("start");
method(javaObject_.get());
}

void OscillatorNode::stop() {
static const auto method = javaClassStatic()->getMethod<void()>("stop");
static const auto method = javaClassLocal()->getMethod<void()>("stop");
method(javaObject_.get());
}

void OscillatorNode::setFrequency(jdouble frequency) {
static const auto method = javaClassStatic()->getMethod<void(jdouble)>("setFrequency");
double OscillatorNode::getFrequency() {
static const auto method = javaClassLocal()->getMethod<jdouble()>("getFrequency");
return method(javaObject_.get());
}

double OscillatorNode::getDetune() {
static const auto method = javaClassLocal()->getMethod<jdouble()>("getDetune");
return method(javaObject_.get());
}

std::string OscillatorNode::getWaveType() {
static const auto method = javaClassLocal()->getMethod<JString()>("getWaveType");
return method(javaObject_.get())->toStdString();
}

void OscillatorNode::setFrequency(double frequency) {
static const auto method = javaClassLocal()->getMethod<void(jdouble)>("setFrequency");
method(javaObject_.get(), frequency);
}

void OscillatorNode::setDetune(jdouble detune) {
static const auto method = javaClassStatic()->getMethod<void(jdouble)>("setDetune");
void OscillatorNode::setDetune(double detune) {
static const auto method = javaClassLocal()->getMethod<void(jdouble)>("setDetune");
method(javaObject_.get(), detune);
}

jdouble OscillatorNode::getFrequency() {
static const auto method = javaClassLocal()->getMethod<jdouble()>("getFrequency");
return method(javaObject_.get());
}
void OscillatorNode::setWaveType(const std::string& waveType) {
static const auto method = javaClassLocal()->getMethod<void(JString)>("setWaveType");
method(javaObject_.get(), *make_jstring(waveType));

jdouble OscillatorNode::getDetune() {
static const auto method = javaClassStatic()->getMethod<jdouble()>("getDetune");
return method(javaObject_.get());
}

void OscillatorNode::connect(const AudioDestinationNode &destination) {
Expand Down
10 changes: 6 additions & 4 deletions android/src/main/cpp/OscillatorNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ namespace audiocontext {

void start();
void stop();
void setFrequency(jdouble frequency);
void setDetune(jdouble detune);
jdouble getFrequency();
jdouble getDetune();
double getFrequency();
double getDetune();
std::string getWaveType();
void setFrequency(double frequency);
void setDetune(double detune);
void setWaveType(const std::string& waveType);
void connect(const AudioDestinationNode &destination);

jsi::Object createOscillatorNodeHostObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ class OscillatorNode(context: BaseAudioContext, reactContext: ReactApplicationCo

external fun initHybrid(l: Long): HybridData?

fun getWaveType(): String {
return WaveType.toString(waveType)
}

fun setWaveType(type: String) {
waveType = WaveType.fromString(type)
}

override fun start() {
if(isPlaying) {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,24 @@ enum class WaveType {

companion object {
fun fromString(type: String): WaveType {
return when (type.uppercase()) {
"SINE" -> SINE
"SQUARE" -> SQUARE
"SAWTOOTH" -> SAWTOOTH
"TRIANGLE" -> TRIANGLE
return when (type.lowercase()) {
"sine" -> SINE
"square" -> SQUARE
"sawtooth" -> SAWTOOTH
"triangle" -> TRIANGLE
else -> throw IllegalArgumentException("Unknown wave type: $type")
}
}

fun toString(type: WaveType): String {
return when (type) {
SINE -> "sine"
SQUARE -> "square"
SAWTOOTH -> "sawtooth"
TRIANGLE -> "triangle"
}
}

fun getWaveBufferElement(wavePhase: Double, waveType: WaveType): Short {
return waveType.getWaveValue(wavePhase)
}
Expand Down
22 changes: 22 additions & 0 deletions cpp/OscillatorNodeHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace audiocontext
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;
}

Expand Down Expand Up @@ -39,6 +40,11 @@ namespace audiocontext
return detune(runtime, propNameId);
}

if (propName == "type")
{
return waveType(runtime, propNameId);
}

if (propName == "connect")
{
return connect(runtime, propNameId);
Expand All @@ -65,6 +71,13 @@ namespace audiocontext
return;
}

if (propName == "type")
{
auto waveType = value.asString(runtime).utf8(runtime);
oscillator_->setWaveType(waveType);
return;
}

throw std::runtime_error("Not yet implemented!");
}

Expand Down Expand Up @@ -103,6 +116,15 @@ namespace audiocontext
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)
{
Expand Down
1 change: 1 addition & 0 deletions cpp/OscillatorNodeHostObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace audiocontext {
jsi::Value stop(jsi::Runtime& runtime, const jsi::PropNameID& propNameId);
jsi::Value frequency(jsi::Runtime& runtime, const jsi::PropNameID& propNameId);
jsi::Value detune(jsi::Runtime& runtime, const jsi::PropNameID& propNameId);
jsi::Value waveType(jsi::Runtime& runtime, const jsi::PropNameID& propNameId);
jsi::Value connect(jsi::Runtime& runtime, const jsi::PropNameID& propNameId);
};
} // namespace audiocontext
1 change: 1 addition & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const App = () => {
oscillatorRef.current = audioContextRef.current.createOscillator();
secondaryOscillatorRef.current = audioContextRef.current.createOscillator();
secondaryOscillatorRef.current.frequency = 300;
secondaryOscillatorRef.current.type = 'square';

const destination = audioContextRef.current.destination();
oscillatorRef.current.connect(destination);
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ type WaveType = 'sine' | 'square' | 'sawtooth' | 'triangle';

export interface Oscillator extends AudioScheduledSourceNode {
frequency: number;
wave: WaveType;
type: WaveType;
detune: number;
}

0 comments on commit 8b3e4ba

Please sign in to comment.