Skip to content

Commit

Permalink
feat: audio params
Browse files Browse the repository at this point in the history
  • Loading branch information
michalsek committed Dec 10, 2024
1 parent 928bb1e commit a91f955
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 46 deletions.
2 changes: 1 addition & 1 deletion apps/common-app/src/components/Select.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react';
import React, { useState } from 'react';
import { ScrollView } from 'react-native-gesture-handler';
import { Modal, View, Text, Pressable, StyleSheet } from 'react-native';

Expand Down
40 changes: 20 additions & 20 deletions apps/fabric-example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1242,7 +1242,7 @@ PODS:
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- Yoga
- react-native-safe-area-context (4.14.0):
- react-native-safe-area-context (4.14.1):
- DoubleConversion
- glog
- hermes-engine
Expand All @@ -1255,8 +1255,8 @@ PODS:
- React-featureflags
- React-graphics
- React-ImageManager
- react-native-safe-area-context/common (= 4.14.0)
- react-native-safe-area-context/fabric (= 4.14.0)
- react-native-safe-area-context/common (= 4.14.1)
- react-native-safe-area-context/fabric (= 4.14.1)
- React-NativeModulesApple
- React-RCTFabric
- React-rendererdebug
Expand All @@ -1265,7 +1265,7 @@ PODS:
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- Yoga
- react-native-safe-area-context/common (4.14.0):
- react-native-safe-area-context/common (4.14.1):
- DoubleConversion
- glog
- hermes-engine
Expand All @@ -1286,7 +1286,7 @@ PODS:
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- Yoga
- react-native-safe-area-context/fabric (4.14.0):
- react-native-safe-area-context/fabric (4.14.1):
- DoubleConversion
- glog
- hermes-engine
Expand All @@ -1308,7 +1308,7 @@ PODS:
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- Yoga
- react-native-skia (1.5.3):
- react-native-skia (1.7.0):
- DoubleConversion
- glog
- hermes-engine
Expand Down Expand Up @@ -1619,7 +1619,7 @@ PODS:
- React-logger (= 0.76.0)
- React-perflogger (= 0.76.0)
- React-utils (= 0.76.0)
- RNAudioAPI (0.2.0):
- RNAudioAPI (0.3.0-rc1):
- DoubleConversion
- glog
- hermes-engine
Expand All @@ -1640,7 +1640,7 @@ PODS:
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- Yoga
- RNGestureHandler (2.21.1):
- RNGestureHandler (2.21.2):
- DoubleConversion
- glog
- hermes-engine
Expand All @@ -1661,7 +1661,7 @@ PODS:
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- Yoga
- RNReanimated (3.16.1):
- RNReanimated (3.16.3):
- DoubleConversion
- glog
- hermes-engine
Expand All @@ -1681,10 +1681,10 @@ PODS:
- ReactCodegen
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- RNReanimated/reanimated (= 3.16.1)
- RNReanimated/worklets (= 3.16.1)
- RNReanimated/reanimated (= 3.16.3)
- RNReanimated/worklets (= 3.16.3)
- Yoga
- RNReanimated/reanimated (3.16.1):
- RNReanimated/reanimated (3.16.3):
- DoubleConversion
- glog
- hermes-engine
Expand All @@ -1704,9 +1704,9 @@ PODS:
- ReactCodegen
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- RNReanimated/reanimated/apple (= 3.16.1)
- RNReanimated/reanimated/apple (= 3.16.3)
- Yoga
- RNReanimated/reanimated/apple (3.16.1):
- RNReanimated/reanimated/apple (3.16.3):
- DoubleConversion
- glog
- hermes-engine
Expand All @@ -1727,7 +1727,7 @@ PODS:
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- Yoga
- RNReanimated/worklets (3.16.1):
- RNReanimated/worklets (3.16.3):
- DoubleConversion
- glog
- hermes-engine
Expand Down Expand Up @@ -2053,8 +2053,8 @@ SPEC CHECKSUMS:
React-logger: d1a89c7d9b3c847eb63eb85724c54b06cae2c939
React-Mapbuffer: b0b4ace5b62b269f3838df26ba2d8b4f39f90783
React-microtasksnativemodule: 0b7db04c18f6bb01ef5b1f9007c3229abecc35dd
react-native-safe-area-context: 2500e4fe998caad50ad3bc51ec23ef951308569e
react-native-skia: b5216ffad62ec47294094cb25630332bb8f96493
react-native-safe-area-context: 782fec0553f14a630a2bac87b116e512a9f35500
react-native-skia: bcbabbd748dd4c9ec02f33df07fa49d1d76fc8c0
react-native-vector-icons: 1c0923f822a6d103e0f3583b533c930803f21f4b
React-nativeconfig: 72c10ff34863148ef90df9c9c8eacba99d2faaaa
React-NativeModulesApple: 5ec49182fa000b2215ee1bed03e2867f8323ccf5
Expand Down Expand Up @@ -2083,9 +2083,9 @@ SPEC CHECKSUMS:
React-utils: d9624101245ebaab39c9f1bd786132da0b4f27ff
ReactCodegen: dbfef1fef26f42c900bb1884fa149d49d501d64d
ReactCommon: 429ca28cd813c31359c73ffac6dc24f93347d522
RNAudioAPI: adb400fcab1b85cafaff4e0880be5d4616fd4727
RNGestureHandler: a3822e519fd1e9885b92d8c9bd82a7a0be305fe3
RNReanimated: 77242c6d67416988a2fd9f5cf574bb3e60016362
RNAudioAPI: 988ab6ec4fc80771859b0dd0dd86ea1571ccde09
RNGestureHandler: 0e5ae8d72ef4afb855e98dcdbe60f27d938abe13
RNReanimated: 006a5d3961bf09c1e96d62ed436e02b2e43b89bb
RNScreens: e389d6a6a66a4f0d3662924ecae803073ccce8ec
SocketRocket: d4aabe649be1e368d1318fdf28a022d714d65748
Yoga: 1d66db49f38fd9e576a1d7c3b081e46ab4c28b9e
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
#include "AudioBufferSourceNodeHostObject.h"
#include "AudioParamHostObject.h"

namespace audioapi {
using namespace facebook;

AudioBufferSourceNodeHostObject::AudioBufferSourceNodeHostObject(
const std::shared_ptr<AudioBufferSourceNodeWrapper> &wrapper)
: AudioScheduledSourceNodeHostObject(wrapper) {}
: AudioScheduledSourceNodeHostObject(wrapper) {
auto detuneParam = wrapper->getDetuneParam();
detuneParam_ = AudioParamHostObject::createFromWrapper(detuneParam);
auto playbackRateParam = wrapper->getPlaybackRateParam();
playbackRateParam_ = AudioParamHostObject::createFromWrapper(playbackRateParam);
}

std::shared_ptr<AudioBufferSourceNodeWrapper> AudioBufferSourceNodeHostObject::
getAudioBufferSourceNodeWrapperFromAudioNodeWrapper() {
Expand All @@ -17,6 +23,10 @@ std::vector<jsi::PropNameID> AudioBufferSourceNodeHostObject::getPropertyNames(
std::vector<jsi::PropNameID> propertyNames =
AudioScheduledSourceNodeHostObject::getPropertyNames(runtime);
propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "loop"));
propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "loopStart"));
propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "loopEnd"));
propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "detune"));
propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "playbackRate"));
propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "buffer"));
propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "resetBuffer"));
return propertyNames;
Expand All @@ -33,6 +43,26 @@ jsi::Value AudioBufferSourceNodeHostObject::get(
return {loop};
}

if (propName == "loopStart") {
auto wrapper = getAudioBufferSourceNodeWrapperFromAudioNodeWrapper();
auto loopStart = wrapper->getLoopStart();
return {loopStart};
}

if (propName == "loopEnd") {
auto wrapper = getAudioBufferSourceNodeWrapperFromAudioNodeWrapper();
auto loopEnd = wrapper->getLoopEnd();
return {loopEnd};
}

if (propName == "detune") {
return jsi::Object::createFromHostObject(runtime, detuneParam_);
}

if (propName == "playbackRate") {
return jsi::Object::createFromHostObject(runtime, playbackRateParam_);
}

if (propName == "buffer") {
auto wrapper = getAudioBufferSourceNodeWrapperFromAudioNodeWrapper();
auto buffer = wrapper->getBuffer();
Expand Down Expand Up @@ -60,6 +90,18 @@ void AudioBufferSourceNodeHostObject::set(
return;
}

if (propName == "loopStart") {
auto wrapper = getAudioBufferSourceNodeWrapperFromAudioNodeWrapper();
wrapper->setLoopStart(value.getNumber());
return;
}

if (propName == "loopEnd") {
auto wrapper = getAudioBufferSourceNodeWrapperFromAudioNodeWrapper();
wrapper->setLoopEnd(value.getNumber());
return;
}

if (propName == "buffer") {
auto wrapper = getAudioBufferSourceNodeWrapperFromAudioNodeWrapper();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
namespace audioapi {
using namespace facebook;

class AudioParamHostObject;

class AudioBufferSourceNodeHostObject
: public AudioScheduledSourceNodeHostObject {
public:
Expand All @@ -31,7 +33,11 @@ class AudioBufferSourceNodeHostObject
}

private:
std::shared_ptr<AudioParamHostObject> detuneParam_;
std::shared_ptr<AudioParamHostObject> playbackRateParam_;

std::shared_ptr<AudioBufferSourceNodeWrapper>
getAudioBufferSourceNodeWrapperFromAudioNodeWrapper();
};

} // namespace audioapi
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ constexpr int SAMPLE_RATE = 44100;
constexpr int CHANNEL_COUNT = 2;
constexpr float MOST_POSITIVE_SINGLE_FLOAT =
static_cast<float>(std::numeric_limits<float>::max());
constexpr float MOST_NEGATIVE_SINGLE_FLOAT =
static_cast<float>(std::numeric_limits<float>::lowest());
constexpr float NYQUIST_FREQUENCY = SAMPLE_RATE / 2.0;
static float MAX_DETUNE = 1200 * std::log2(MOST_POSITIVE_SINGLE_FLOAT);
constexpr float MAX_GAIN = MOST_POSITIVE_SINGLE_FLOAT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,25 @@
#include "AudioArray.h"
#include "AudioBufferSourceNode.h"
#include "AudioBus.h"
#include "AudioParam.h"
#include "BaseAudioContext.h"
#include "Constants.h"

namespace audioapi {

AudioBufferSourceNode::AudioBufferSourceNode(BaseAudioContext *context)
: AudioScheduledSourceNode(context), loop_(false), bufferIndex_(0) {
numberOfInputs_ = 0;
buffer_ = std::shared_ptr<AudioBuffer>(nullptr);

detuneParam_ = std::make_shared<AudioParam>(context, 0.0, -MAX_DETUNE, MAX_DETUNE);
playbackRateParam_ = std::make_shared<AudioParam>(
context,
1.0,
MOST_NEGATIVE_SINGLE_FLOAT,
MOST_POSITIVE_SINGLE_FLOAT
);

isInitialized_ = true;
}

Expand All @@ -26,12 +37,12 @@ double AudioBufferSourceNode::getLoopEnd() const {
return loopEnd_;
}

double AudioBufferSourceNode::getDetune() const {
return detune_;
std::shared_ptr<AudioParam> AudioBufferSourceNode::getDetuneParam() const {
return detuneParam_;
}

double AudioBufferSourceNode::getPlaybackRate() const {
return playbackRate_;
std::shared_ptr<AudioParam> AudioBufferSourceNode::getPlaybackRateParam() const {
return playbackRateParam_;
}

std::shared_ptr<AudioBuffer> AudioBufferSourceNode::getBuffer() const {
Expand All @@ -50,14 +61,6 @@ void AudioBufferSourceNode::setLoopEnd(double loopEnd) {
loopEnd_ = loopEnd;
}

void AudioBufferSourceNode::setDetune(double detune) {
detune_ = detune;
}

void AudioBufferSourceNode::setPlaybackRate(double playbackRate) {
playbackRate_ = playbackRate;
}

void AudioBufferSourceNode::setBuffer(
const std::shared_ptr<AudioBuffer> &buffer) {
if (!buffer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace audioapi {

class AudioBus;
class AudioParam;

class AudioBufferSourceNode : public AudioScheduledSourceNode {
public:
Expand All @@ -17,18 +18,15 @@ class AudioBufferSourceNode : public AudioScheduledSourceNode {
[[nodiscard]] double getLoopStart() const;
[[nodiscard]] double getLoopEnd() const;

[[nodiscard]] double getDetune() const;
[[nodiscard]] double getPlaybackRate() const;
[[nodiscard]] std::shared_ptr<AudioParam> getDetuneParam() const;
[[nodiscard]] std::shared_ptr<AudioParam> getPlaybackRateParam() const;

[[nodiscard]] std::shared_ptr<AudioBuffer> getBuffer() const;

void setLoop(bool loop);
void setLoopStart(double loopStart);
void setLoopEnd(double loopEnd);

void setDetune(double detune);
void setPlaybackRate(double playbackRate);

void setBuffer(const std::shared_ptr<AudioBuffer> &buffer);

protected:
Expand All @@ -40,9 +38,9 @@ class AudioBufferSourceNode : public AudioScheduledSourceNode {
double loopEnd_;
double loopStart_;

// playback rate aka pitch change properties
double detune_;
double playbackRate_;
// playback rate aka pitch change params
std::shared_ptr<AudioParam> detuneParam_;
std::shared_ptr<AudioParam> playbackRateParam_;

// internal helpers
int bufferIndex_;
Expand Down
Loading

0 comments on commit a91f955

Please sign in to comment.