Skip to content

Commit

Permalink
feat: introduced JsiHostObject class as a base for all HostObjects
Browse files Browse the repository at this point in the history
  • Loading branch information
Maciej Makowski committed Dec 12, 2024
1 parent 4a8d3dd commit 0e6556a
Show file tree
Hide file tree
Showing 29 changed files with 542 additions and 1,381 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,27 @@
#include <utility>
#include <vector>

#include <JsiHostObject.h>
#include "AudioContextHostObject.h"
#include "JsiPromise.h"

namespace audioapi {
using namespace facebook;

class AudioAPIInstallerHostObject
: public jsi::HostObject,
: public JsiHostObject,
public std::enable_shared_from_this<AudioAPIInstallerHostObject> {
public:
explicit AudioAPIInstallerHostObject(
jsi::Runtime *runtime,
const std::shared_ptr<react::CallInvoker> &jsInvoker);
const std::shared_ptr<react::CallInvoker> &jsInvoker)
: rnRuntime_(runtime) {
promiseVendor_ =
std::make_shared<JsiPromise::PromiseVendor>(runtime, jsInvoker);

jsi::Value get(jsi::Runtime &runtime, const jsi::PropNameID &name) override;

void set(
jsi::Runtime &runtime,
const jsi::PropNameID &name,
const jsi::Value &value) override;

std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime &rt) override;
addFunctions(
JSI_EXPORT_FUNCTION(AudioAPIInstallerHostObject, createAudioContext));
}

void install() {
auto object =
Expand All @@ -36,6 +35,13 @@ class AudioAPIInstallerHostObject
*rnRuntime_, "__AudioAPIInstaller", std::move(object));
}

JSI_HOST_FUNCTION(createAudioContext) {
auto audioContext = std::make_shared<AudioContext>();
auto audioContextHostObject =
std::make_shared<AudioContextHostObject>(audioContext, promiseVendor_);
return jsi::Object::createFromHostObject(runtime, audioContextHostObject);
}

private:
std::shared_ptr<JsiPromise::PromiseVendor> promiseVendor_;
jsi::Runtime *rnRuntime_;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,96 @@
#include <memory>
#include <vector>

#include <JsiHostObject.h>
#include "AudioBuffer.h"

namespace audioapi {
using namespace facebook;

class AudioBufferHostObject : public jsi::HostObject {
class AudioBufferHostObject : public JsiHostObject {
public:
std::shared_ptr<AudioBuffer> audioBuffer_;

explicit AudioBufferHostObject(
const std::shared_ptr<AudioBuffer> &audioBuffer);
const std::shared_ptr<AudioBuffer> &audioBuffer)
: audioBuffer_(audioBuffer) {
addGetters(
JSI_EXPORT_PROPERTY_GETTER(AudioBufferHostObject, sampleRate),
JSI_EXPORT_PROPERTY_GETTER(AudioBufferHostObject, length),
JSI_EXPORT_PROPERTY_GETTER(AudioBufferHostObject, duration),
JSI_EXPORT_PROPERTY_GETTER(AudioBufferHostObject, numberOfChannels));

jsi::Value get(jsi::Runtime &runtime, const jsi::PropNameID &name) override;
addFunctions(
JSI_EXPORT_FUNCTION(AudioBufferHostObject, getChannelData),
JSI_EXPORT_FUNCTION(AudioBufferHostObject, copyFromChannel),
JSI_EXPORT_FUNCTION(AudioBufferHostObject, copyToChannel));
}

void set(
jsi::Runtime &runtime,
const jsi::PropNameID &name,
const jsi::Value &value) override;
JSI_PROPERTY_GETTER(sampleRate) {
return {audioBuffer_->getSampleRate()};
}

std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime &rt) override;
JSI_PROPERTY_GETTER(length) {
return {audioBuffer_->getLength()};
}

JSI_PROPERTY_GETTER(duration) {
return {audioBuffer_->getDuration()};
}

JSI_PROPERTY_GETTER(numberOfChannels) {
return {audioBuffer_->getNumberOfChannels()};
}

JSI_HOST_FUNCTION(getChannelData) {
int channel = static_cast<int>(args[0].getNumber());
float *channelData = audioBuffer_->getChannelData(channel);

auto array = jsi::Array(runtime, audioBuffer_->getLength());
for (int i = 0; i < audioBuffer_->getLength(); i++) {
array.setValueAtIndex(runtime, i, jsi::Value(channelData[i]));
}

return array;
}

JSI_HOST_FUNCTION(copyFromChannel) {
auto destination = args[0].getObject(runtime).asArray(runtime);
auto destinationLength =
static_cast<int>(destination.getProperty(runtime, "length").asNumber());
auto channelNumber = static_cast<int>(args[1].getNumber());
auto startInChannel = static_cast<int>(args[2].getNumber());

auto *destinationData = new float[destinationLength];

audioBuffer_->copyFromChannel(
destinationData, destinationLength, channelNumber, startInChannel);

for (int i = 0; i < destinationLength; i++) {
destination.setValueAtIndex(runtime, i, jsi::Value(destinationData[i]));
}

return jsi::Value::undefined();
}

JSI_HOST_FUNCTION(copyToChannel) {
auto source = args[0].getObject(runtime).asArray(runtime);
auto sourceLength =
static_cast<int>(source.getProperty(runtime, "length").asNumber());
auto channelNumber = static_cast<int>(args[1].getNumber());
auto startInChannel = static_cast<int>(args[2].getNumber());

auto *sourceData = new float[sourceLength];

for (int i = 0; i < sourceLength; i++) {
sourceData[i] =
static_cast<float>(source.getValueAtIndex(runtime, i).getNumber());
}

audioBuffer_->copyToChannel(
sourceData, sourceLength, channelNumber, startInChannel);

return jsi::Value::undefined();
}
};
} // namespace audioapi
Loading

0 comments on commit 0e6556a

Please sign in to comment.