Skip to content
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

Refactor/base audio context #179

Merged
merged 6 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,12 @@ using namespace facebook;

AudioContextHostObject::AudioContextHostObject(
const std::shared_ptr<AudioContextWrapper> &wrapper)
: wrapper_(wrapper) {
auto destinationNodeWrapper = wrapper_->getDestination();
destination_ =
AudioDestinationNodeHostObject::createFromWrapper(destinationNodeWrapper);
}
: BaseAudioContextHostObject(wrapper) {}

std::vector<jsi::PropNameID> AudioContextHostObject::getPropertyNames(
jsi::Runtime &runtime) {
std::vector<jsi::PropNameID> propertyNames;
propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "destination"));
propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "state"));
propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "sampleRate"));
propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "currentTime"));
propertyNames.push_back(
jsi::PropNameID::forUtf8(runtime, "createOscillator"));
propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "createGain"));
propertyNames.push_back(
jsi::PropNameID::forUtf8(runtime, "createStereoPanner"));
propertyNames.push_back(
jsi::PropNameID::forUtf8(runtime, "createBiquadFilter"));
propertyNames.push_back(
jsi::PropNameID::forUtf8(runtime, "createBufferSource"));
propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "createBuffer"));
std::vector<jsi::PropNameID> propertyNames =
BaseAudioContextHostObject::getPropertyNames(runtime);
propertyNames.push_back(jsi::PropNameID::forUtf8(runtime, "close"));
return propertyNames;
}
Expand All @@ -37,131 +20,6 @@ jsi::Value AudioContextHostObject::get(
const jsi::PropNameID &propNameId) {
auto propName = propNameId.utf8(runtime);

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

if (propName == "state") {
return jsi::String::createFromUtf8(runtime, wrapper_->getState());
}

if (propName == "sampleRate") {
return {wrapper_->getSampleRate()};
}

if (propName == "currentTime") {
return {wrapper_->getCurrentTime()};
}

if (propName == "createOscillator") {
return jsi::Function::createFromHostFunction(
runtime,
propNameId,
0,
[this](
jsi::Runtime &runtime,
const jsi::Value &thisValue,
const jsi::Value *arguments,
size_t count) -> jsi::Value {
auto oscillator = wrapper_->createOscillator();
auto oscillatorHostObject =
OscillatorNodeHostObject::createFromWrapper(oscillator);
return jsi::Object::createFromHostObject(
runtime, oscillatorHostObject);
});
}

if (propName == "createGain") {
return jsi::Function::createFromHostFunction(
runtime,
propNameId,
0,
[this](
jsi::Runtime &runtime,
const jsi::Value &thisValue,
const jsi::Value *arguments,
size_t count) -> jsi::Value {
auto gain = wrapper_->createGain();
auto gainHostObject = GainNodeHostObject::createFromWrapper(gain);
return jsi::Object::createFromHostObject(runtime, gainHostObject);
});
}

if (propName == "createStereoPanner") {
return jsi::Function::createFromHostFunction(
runtime,
propNameId,
0,
[this](
jsi::Runtime &runtime,
const jsi::Value &thisValue,
const jsi::Value *arguments,
size_t count) -> jsi::Value {
auto stereoPanner = wrapper_->createStereoPanner();
auto stereoPannerHostObject =
StereoPannerNodeHostObject::createFromWrapper(stereoPanner);
return jsi::Object::createFromHostObject(
runtime, stereoPannerHostObject);
});
}

if (propName == "createBiquadFilter") {
return jsi::Function::createFromHostFunction(
runtime,
propNameId,
0,
[this](
jsi::Runtime &runtime,
const jsi::Value &thisValue,
const jsi::Value *arguments,
size_t count) -> jsi::Value {
auto biquadFilter = wrapper_->createBiquadFilter();
auto biquadFilterHostObject =
BiquadFilterNodeHostObject::createFromWrapper(biquadFilter);
return jsi::Object::createFromHostObject(
runtime, biquadFilterHostObject);
});
}

if (propName == "createBufferSource") {
return jsi::Function::createFromHostFunction(
runtime,
propNameId,
0,
[this](
jsi::Runtime &runtime,
const jsi::Value &thisValue,
const jsi::Value *arguments,
size_t count) -> jsi::Value {
auto bufferSource = wrapper_->createBufferSource();
auto bufferSourceHostObject =
AudioBufferSourceNodeHostObject::createFromWrapper(bufferSource);
return jsi::Object::createFromHostObject(
runtime, bufferSourceHostObject);
});
}

if (propName == "createBuffer") {
return jsi::Function::createFromHostFunction(
runtime,
propNameId,
3,
[this](
jsi::Runtime &runtime,
const jsi::Value &thisValue,
const jsi::Value *arguments,
size_t count) -> jsi::Value {
auto numberOfChannels = static_cast<int>(arguments[0].getNumber());
auto length = static_cast<int>(arguments[1].getNumber());
auto sampleRate = static_cast<int>(arguments[2].getNumber());
auto buffer =
wrapper_->createBuffer(numberOfChannels, length, sampleRate);
auto bufferHostObject =
AudioBufferHostObject::createFromWrapper(buffer);
return jsi::Object::createFromHostObject(runtime, bufferHostObject);
});
}

if (propName == "close") {
return jsi::Function::createFromHostFunction(
runtime,
Expand All @@ -172,12 +30,12 @@ jsi::Value AudioContextHostObject::get(
const jsi::Value &thisValue,
const jsi::Value *arguments,
size_t count) -> jsi::Value {
wrapper_->close();
getAudioContextWrapperFromBaseAudioContextWrapper()->close();
return jsi::Value::undefined();
});
}

throw std::runtime_error("Not yet implemented!");
return BaseAudioContextHostObject::get(runtime, propNameId);
}

void AudioContextHostObject::set(
Expand All @@ -186,6 +44,11 @@ void AudioContextHostObject::set(
const jsi::Value &value) {
auto propName = propNameId.utf8(runtime);

throw std::runtime_error("Not yet implemented!");
return BaseAudioContextHostObject::set(runtime, propNameId, value);
}

std::shared_ptr<AudioContextWrapper>
AudioContextHostObject::getAudioContextWrapperFromBaseAudioContextWrapper() {
return std::static_pointer_cast<AudioContextWrapper>(wrapper_);
}
} // namespace audioapi
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,15 @@

#include <jsi/jsi.h>
#include <memory>
#include <utility>
#include <vector>

#include "AudioBufferHostObject.h"
#include "AudioBufferSourceNodeHostObject.h"
#include "AudioContextWrapper.h"
#include "AudioDestinationNodeHostObject.h"
#include "BiquadFilterNodeHostObject.h"
#include "GainNodeHostObject.h"
#include "OscillatorNodeHostObject.h"
#include "StereoPannerNodeHostObject.h"
#include "BaseAudioContextHostObject.h"

namespace audioapi {
using namespace facebook;

class AudioContextHostObject : public jsi::HostObject {
class AudioContextHostObject : public BaseAudioContextHostObject {
public:
explicit AudioContextHostObject(
const std::shared_ptr<AudioContextWrapper> &wrapper);
Expand All @@ -37,7 +30,7 @@ class AudioContextHostObject : public jsi::HostObject {
}

private:
std::shared_ptr<AudioContextWrapper> wrapper_;
std::shared_ptr<AudioDestinationNodeHostObject> destination_;
std::shared_ptr<AudioContextWrapper>
getAudioContextWrapperFromBaseAudioContextWrapper();
};
} // namespace audioapi
Loading
Loading