Skip to content

Commit

Permalink
Merge pull request #44 from software-mansion-labs/feat/android/audio-…
Browse files Browse the repository at this point in the history
…param

Feat/android/audio param
  • Loading branch information
maciejmakowski2003 authored Jul 29, 2024
2 parents 628b2fe + e79caf2 commit 256a620
Show file tree
Hide file tree
Showing 50 changed files with 396 additions and 196 deletions.
28 changes: 21 additions & 7 deletions android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,38 @@ include_directories(
../cpp/AudioNode
../cpp/GainNode
../cpp/StereoPannerNode
../cpp/AudioParam

src/main/cpp
src/main/cpp/AudioContext
src/main/cpp/AudioDestinationNode
src/main/cpp/OscillatorNode
src/main/cpp/AudioNode
src/main/cpp/GainNode
src/main/cpp/StereoPannerNode
src/main/cpp/AudioParam

../node_modules/react-native/ReactCommon/jsi
../node_modules/react-native/ReactAndroid/src/main/jni/react/jni
../node_modules/react-native/ReactAndroid/src/main/jni/third-party/folly
)

add_library(react-native-audio-context SHARED
src/main/cpp/OnLoad.cpp
src/main/cpp/AudioContext
src/main/cpp/OscillatorNode
src/main/cpp/AudioDestinationNode.h
src/main/cpp/AudioNode
src/main/cpp/GainNode
src/main/cpp/StereoPannerNode
src/main/cpp/AudioContext/AudioContext
src/main/cpp/OscillatorNode/OscillatorNode
src/main/cpp/AudioDestinationNode/AudioDestinationNode.h
src/main/cpp/AudioNode/AudioNode
src/main/cpp/GainNode/GainNode
src/main/cpp/StereoPannerNode/StereoPannerNode
src/main/cpp/AudioParam/AudioParam

../cpp/AudioContext/AudioContextHostObject
../cpp/AudioContext/AudioContextWrapper.h
../cpp/AudioContext/android/AudioContextWrapper.cpp

../cpp/AudioDestinationNode/AudioDestinationNodeHostObject
../cpp/AudioDestinationNode/AudioDestinationNodeWrapper.h
../cpp/AudioDestinationNode/android/AudioDestinationNodeWrapper.cpp

../cpp/OscillatorNode/OscillatorNodeHostObject
../cpp/OscillatorNode/OscillatorNodeWrapper.h
Expand All @@ -57,6 +67,10 @@ add_library(react-native-audio-context SHARED
../cpp/StereoPannerNode/StereoPannerNodeHostObject
../cpp/StereoPannerNode/StereoPannerNodeWrapper.h
../cpp/StereoPannerNode/android/StereoPannerNodeWrapper.cpp

../cpp/AudioParam/AudioParamHostObject
../cpp/AudioParam/AudioParamWrapper.h
../cpp/AudioParam/android/AudioParamWrapper.cpp
)

find_package(ReactAndroid REQUIRED CONFIG)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ namespace audiocontext {

protected:
friend HybridBase;
friend class AudioNode;

global_ref<AudioNode::javaobject> javaObject_;

Expand Down
16 changes: 16 additions & 0 deletions android/src/main/cpp/AudioParam/AudioParam.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "AudioParam.h"

namespace audiocontext {

AudioParam::AudioParam(alias_ref<AudioParam::jhybridobject> &jThis): javaObject_(make_global(jThis)) {}

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

void AudioParam::setValue(double value) {
static const auto method = javaClassLocal()->getMethod<void(double)>("setValue");
method(javaObject_.get(), value);
}
}
39 changes: 39 additions & 0 deletions android/src/main/cpp/AudioParam/AudioParam.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include <fbjni/fbjni.h>
#include <react/jni/CxxModuleWrapper.h>
#include <react/jni/JMessageQueueThread.h>
#include <memory>

namespace audiocontext {

using namespace facebook;
using namespace facebook::jni;

class AudioParam : public jni::HybridClass<AudioParam> {
public:
static auto constexpr kJavaDescriptor = "Lcom/audiocontext/nodes/parameters/AudioParam;";

static jni::local_ref<AudioParam::jhybriddata> initHybrid(jni::alias_ref<jhybridobject> jThis)
{
return makeCxxInstance(jThis);
}

static void registerNatives() {
registerHybrid({
makeNativeMethod("initHybrid", AudioParam::initHybrid),
});
}

double getValue();
void setValue(double value);

protected:
friend HybridBase;

global_ref<AudioParam::javaobject> javaObject_;

explicit AudioParam(jni::alias_ref<AudioParam::jhybridobject>& jThis);
};

} // namespace audiocontext
16 changes: 0 additions & 16 deletions android/src/main/cpp/GainNode.cpp

This file was deleted.

14 changes: 14 additions & 0 deletions android/src/main/cpp/GainNode/GainNode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "GainNode.h"

namespace audiocontext{

using namespace facebook::jni;

std::shared_ptr<AudioParam> GainNode::getGainParam() {
static const auto method = javaClassLocal()->getMethod<AudioParam()>("getGain");
auto gain = method(javaObject_.get());
auto gainCppInstance = gain->cthis();

return std::shared_ptr<AudioParam>(gainCppInstance);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <react/jni/JMessageQueueThread.h>
#include <memory>
#include "AudioNode.h"
#include "AudioParam.h"

namespace audiocontext {

Expand All @@ -15,9 +16,7 @@ namespace audiocontext {
public:
static auto constexpr kJavaDescriptor = "Lcom/audiocontext/nodes/GainNode;";

double getGain();

void setGain(double gain);
std::shared_ptr<AudioParam> getGainParam();
};

} // namespace audiocontext
6 changes: 2 additions & 4 deletions android/src/main/cpp/OnLoad.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
#include <fbjni/fbjni.h>
#include "OscillatorNode.h"
#include "AudioContext.h"
#include "AudioDestinationNode.h"
#include "AudioNode.h"
#include "AudioParam.h"

using namespace audiocontext;

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
{
return facebook::jni::initialize(vm, [] {
OscillatorNode::registerNatives();
AudioContext::registerNatives();
AudioDestinationNode::registerNatives();
AudioNode::registerNatives();
AudioParam::registerNatives();
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,27 @@ namespace audiocontext {
method(javaObject_.get(), time);
}

double OscillatorNode::getFrequency() {
static const auto method = javaClassLocal()->getMethod<jdouble()>("getFrequency");
return method(javaObject_.get());
std::shared_ptr<AudioParam> OscillatorNode::getFrequencyParam() {
static const auto method = javaClassLocal()->getMethod<AudioParam()>("getFrequency");
auto frquency = method(javaObject_.get());
auto frquencyCppInstance = frquency->cthis();

return std::shared_ptr<AudioParam>(frquencyCppInstance);
}

double OscillatorNode::getDetune() {
static const auto method = javaClassLocal()->getMethod<jdouble()>("getDetune");
return method(javaObject_.get());
std::shared_ptr<AudioParam> OscillatorNode::getDetuneParam() {
static const auto method = javaClassLocal()->getMethod<AudioParam()>("getDetune");
auto detune = method(javaObject_.get());
auto detuneCppInstance = detune->cthis();

return std::shared_ptr<AudioParam>(detuneCppInstance);
}

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(double detune) {
static const auto method = javaClassLocal()->getMethod<void(jdouble)>("setDetune");
method(javaObject_.get(), detune);
}

void OscillatorNode::setWaveType(const std::string& waveType) {
static const auto method = javaClassLocal()->getMethod<void(JString)>("setWaveType");
method(javaObject_.get(), *make_jstring(waveType));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <react/jni/JMessageQueueThread.h>
#include <memory>
#include "AudioNode.h"
#include "AudioParam.h"

namespace audiocontext {

Expand All @@ -19,16 +20,12 @@ namespace audiocontext {

void stop(double time);

double getFrequency();
std::shared_ptr<AudioParam> getFrequencyParam();

double getDetune();
std::shared_ptr<AudioParam> getDetuneParam();

std::string getWaveType();

void setFrequency(double frequency);

void setDetune(double detune);

void setWaveType(const std::string &waveType);
};
}// namespace audiocontext
16 changes: 0 additions & 16 deletions android/src/main/cpp/StereoPannerNode.cpp

This file was deleted.

14 changes: 14 additions & 0 deletions android/src/main/cpp/StereoPannerNode/StereoPannerNode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "StereoPannerNode.h"

namespace audiocontext{

using namespace facebook::jni;

std::shared_ptr<AudioParam> StereoPannerNode::getPanParam() {
static const auto method = javaClassLocal()->getMethod<AudioParam()>("getPan");
auto pan = method(javaObject_.get());
auto panCppInstance = pan->cthis();

return std::shared_ptr<AudioParam>(panCppInstance);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <react/jni/JMessageQueueThread.h>
#include <memory>
#include "AudioNode.h"
#include "AudioParam.h"

namespace audiocontext {

Expand All @@ -15,9 +16,7 @@ namespace audiocontext {
public:
static auto constexpr kJavaDescriptor = "Lcom/audiocontext/nodes/StereoPannerNode;";

double getPan();

void setPan(double pan);
std::shared_ptr<AudioParam> getPanParam();
};

} // namespace audiocontext
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.audiocontext.nodes

import com.audiocontext.context.BaseAudioContext
import com.audiocontext.nodes.parameters.PlaybackParameters
import com.facebook.jni.HybridData


Expand Down
1 change: 1 addition & 0 deletions android/src/main/java/com/audiocontext/nodes/AudioNode.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.audiocontext.nodes

import com.audiocontext.context.BaseAudioContext
import com.audiocontext.nodes.parameters.PlaybackParameters
import com.facebook.jni.HybridData


Expand Down
6 changes: 4 additions & 2 deletions android/src/main/java/com/audiocontext/nodes/GainNode.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.audiocontext.nodes

import com.audiocontext.context.BaseAudioContext
import com.audiocontext.nodes.parameters.AudioParam
import com.audiocontext.nodes.parameters.PlaybackParameters
import com.facebook.jni.HybridData

class GainNode(context: BaseAudioContext): AudioNode(context) {
override val numberOfInputs: Int = 1
override val numberOfOutputs: Int = 1
private var gain: Double = 1.0
private var gain: AudioParam = AudioParam(1.0, 1.0, 0.0)
get() = field
set(value) {
field = value
Expand All @@ -15,7 +17,7 @@ class GainNode(context: BaseAudioContext): AudioNode(context) {
private val mHybridData: HybridData? = initHybrid();

override fun process(playbackParameters: PlaybackParameters) {
playbackParameters.gain = gain
playbackParameters.gain = gain.getValue()
super.process(playbackParameters)
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.audiocontext.nodes

import com.audiocontext.context.BaseAudioContext
import com.audiocontext.nodes.parameters.AudioParam
import com.audiocontext.nodes.parameters.PlaybackParameters
import com.facebook.jni.HybridData
import kotlin.math.min

class StereoPannerNode(context: BaseAudioContext): AudioNode(context) {
override val numberOfInputs: Int = 1
override val numberOfOutputs: Int = 1
private var pan: Double = 0.0
private var pan: AudioParam = AudioParam(0.0, 1.0, -1.0)
get() = field
set(value) {
field = value
Expand All @@ -16,8 +18,8 @@ class StereoPannerNode(context: BaseAudioContext): AudioNode(context) {
private val mHybridData: HybridData? = initHybrid();

override fun process(playbackParameters: PlaybackParameters) {
playbackParameters.leftPan = min(1.0 - pan, 1.0)
playbackParameters.rightPan = min(1.0 + pan, 1.0)
playbackParameters.leftPan = min(1.0 - pan.getValue(), 1.0)
playbackParameters.rightPan = min(1.0 + pan.getValue(), 1.0)
super.process(playbackParameters)
}
}
Loading

0 comments on commit 256a620

Please sign in to comment.