Skip to content
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
2 changes: 1 addition & 1 deletion apps/fabric-example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3081,7 +3081,7 @@ SPEC CHECKSUMS:
ReactAppDependencyProvider: c91900fa724baee992f01c05eeb4c9e01a807f78
ReactCodegen: 8125d6ee06ea06f48f156cbddec5c2ca576d62e6
ReactCommon: 116d6ee71679243698620d8cd9a9042541e44aa6
RNAudioAPI: e08a4157527a2e87879a7bb61880276a0dfc77f6
RNAudioAPI: d86d59666cfd7544d3cb0030239dc698404fe117
RNGestureHandler: 3a73f098d74712952870e948b3d9cf7b6cae9961
RNReanimated: a035264789d1f64cb5adba7085d6aac6e9ec70a7
RNScreens: 6ced6ae8a526512a6eef6e28c2286e1fc2d378c3
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <audioapi/HostObjects/sources/AudioBufferHostObject.h>

#include <audioapi/jsi/AudioArrayBuffer.h>
#include <audioapi/utils/AudioBus.h>

namespace audioapi {

Expand Down Expand Up @@ -42,14 +43,8 @@ JSI_PROPERTY_GETTER_IMPL(AudioBufferHostObject, numberOfChannels) {

JSI_HOST_FUNCTION_IMPL(AudioBufferHostObject, getChannelData) {
auto channel = static_cast<int>(args[0].getNumber());
auto channelData =
reinterpret_cast<uint8_t *>(audioBuffer_->getChannelData(channel));
auto length = static_cast<int>(audioBuffer_->getLength());
auto size = static_cast<int>(length * sizeof(float));

// reading or writing from this ArrayBuffer could cause a crash
// if underlying channelData is deallocated
auto audioArrayBuffer = std::make_shared<AudioArrayBuffer>(channelData, size);
auto audioArrayBuffer = std::make_shared<AudioArrayBuffer>(
audioBuffer_->bus_->getSharedChannel(channel));
auto arrayBuffer = jsi::ArrayBuffer(runtime, audioArrayBuffer);

auto float32ArrayCtor =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#pragma once

#include <audioapi/HostObjects/sources/AudioBufferHostObject.h>
#include <audioapi/HostObjects/utils/AudioDecoderHostObject.h>
#include <audioapi/core/utils/AudioDecoder.h>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#pragma once

#include <audioapi/HostObjects/sources/AudioBufferHostObject.h>
#include <audioapi/HostObjects/utils/AudioStretcherHostObject.h>
#include <audioapi/core/utils/AudioStretcher.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,16 @@ WorkletNode::WorkletNode(
size_t bufferLength,
size_t inputChannelCount)
: AudioNode(context),
buffRealLength_(bufferLength * sizeof(float)),
bufferLength_(bufferLength),
workletRunner_(runtime),
shareableWorklet_(worklet),
bufferLength_(bufferLength),
inputChannelCount_(inputChannelCount),
curBuffIndex_(0) {
buffs_.reserve(inputChannelCount_);
for (size_t i = 0; i < inputChannelCount_; ++i) {
buffs_.emplace_back(new uint8_t[buffRealLength_]);
}
bus_ = std::make_shared<AudioBus>(
bufferLength, inputChannelCount, context->getSampleRate());
isInitialized_ = true;
}

WorkletNode::~WorkletNode() {
for (auto &buff : buffs_) {
delete[] buff;
}
}

std::shared_ptr<AudioBus> WorkletNode::processNode(
const std::shared_ptr<AudioBus> &processingBus,
int framesToProcess) {
Expand All @@ -40,17 +31,11 @@ std::shared_ptr<AudioBus> WorkletNode::processNode(
size_t needsToProcess = framesToProcess - processed;
size_t shouldProcess = std::min(framesToWorkletInvoke, needsToProcess);

for (size_t ch = 0; ch < channelCount_; ch++) {
/// here we copy
/// to uint8_t* [curBuffIndex_, curBuffIndex_ + shouldProcess]
/// from float* [processed, processed + shouldProcess]
/// so as the we need to copy shouldProcess * sizeof(float) bytes
auto channelData = processingBus->getChannel(ch)->getData();
std::memcpy(
/* dest */ buffs_[ch] + curBuffIndex_ * sizeof(float),
/* src */ reinterpret_cast<const uint8_t *>(channelData + processed),
/* size */ shouldProcess * sizeof(float));
}
/// here we copy
/// to [curBuffIndex_, curBuffIndex_ + shouldProcess]
/// from [processed, processed + shouldProcess]
bus_->copy(processingBus.get(), processed, curBuffIndex_, shouldProcess);

processed += shouldProcess;
curBuffIndex_ += shouldProcess;

Expand All @@ -63,16 +48,18 @@ std::shared_ptr<AudioBus> WorkletNode::processNode(
/// Arguments preparation
auto jsArray = jsi::Array(uiRuntimeRaw, channelCount_);
for (size_t ch = 0; ch < channelCount_; ch++) {
uint8_t *buffPtr = buffs_[ch];
buffs_[ch] = new uint8_t[buffRealLength_];
auto audioArray = std::make_shared<AudioArray>(bufferLength_);
audioArray->copy(bus_->getChannel(ch));
auto sharedAudioArray =
std::make_shared<AudioArrayBuffer>(buffPtr, buffRealLength_);
std::make_shared<AudioArrayBuffer>(audioArray);
auto arrayBuffer =
jsi::ArrayBuffer(uiRuntimeRaw, std::move(sharedAudioArray));
arrayBuffer.setExternalMemoryPressure(
uiRuntimeRaw, sharedAudioArray->size());
jsArray.setValueAtIndex(uiRuntimeRaw, ch, std::move(arrayBuffer));
}
jsArray.setExternalMemoryPressure(
uiRuntimeRaw, channelCount_ * buffRealLength_);

bus_->zero();

workletRunner_.executeWorklet(
shareableWorklet_,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class WorkletNode : public AudioNode {
size_t inputChannelCount
);

~WorkletNode() override;
~WorkletNode() override = default;

protected:
std::shared_ptr<AudioBus> processNode(const std::shared_ptr<AudioBus>& processingBus, int framesToProcess) override;
Expand All @@ -51,10 +51,9 @@ class WorkletNode : public AudioNode {
private:
WorkletsRunner workletRunner_;
std::shared_ptr<worklets::SerializableWorklet> shareableWorklet_;
std::vector<uint8_t*> buffs_;
std::shared_ptr<AudioBus> bus_;

/// @brief Length of the byte buffer that will be passed to the AudioArrayBuffer
size_t buffRealLength_;
size_t bufferLength_;
size_t inputChannelCount_;
size_t curBuffIndex_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ WorkletProcessingNode::WorkletProcessingNode(
outputBuffsHandles_.resize(maxChannelCount);

for (size_t i = 0; i < maxChannelCount; ++i) {
auto inputBuff = new uint8_t[RENDER_QUANTUM_SIZE * sizeof(float)];
inputBuffsHandles_[i] = std::make_shared<AudioArrayBuffer>(
inputBuff, RENDER_QUANTUM_SIZE * sizeof(float));
auto inputAudioArray = std::make_shared<AudioArray>(RENDER_QUANTUM_SIZE);
inputBuffsHandles_[i] = std::make_shared<AudioArrayBuffer>(inputAudioArray);

auto outputBuff = new uint8_t[RENDER_QUANTUM_SIZE * sizeof(float)];
outputBuffsHandles_[i] = std::make_shared<AudioArrayBuffer>(
outputBuff, RENDER_QUANTUM_SIZE * sizeof(float));
auto outputAudioArray = std::make_shared<AudioArray>(RENDER_QUANTUM_SIZE);
outputBuffsHandles_[i] =
std::make_shared<AudioArrayBuffer>(outputAudioArray);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class AudioBuffer : public std::enable_shared_from_this<AudioBuffer> {
private:
friend class AudioBufferSourceNode;
friend class AudioBufferQueueSourceNode;
friend class AudioBufferHostObject;

std::shared_ptr<AudioBus> bus_;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ WorkletSourceNode::WorkletSourceNode(
size_t outputChannelCount = this->getChannelCount();
outputBuffsHandles_.resize(outputChannelCount);
for (size_t i = 0; i < outputChannelCount; ++i) {
auto buff = new uint8_t[RENDER_QUANTUM_SIZE * sizeof(float)];
outputBuffsHandles_[i] = std::make_shared<AudioArrayBuffer>(
buff, RENDER_QUANTUM_SIZE * sizeof(float));
auto audioArray = std::make_shared<AudioArray>(RENDER_QUANTUM_SIZE);
outputBuffsHandles_[i] = std::make_shared<AudioArrayBuffer>(audioArray);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace audioapi {

size_t AudioArrayBuffer::size() const {
return size_;
return audioArray_->getSize() * sizeof(float);
}

uint8_t *AudioArrayBuffer::data() {
return data_;
return reinterpret_cast<uint8_t *>(audioArray_->getData());
}

} // namespace audioapi
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#pragma once

#include <jsi/jsi.h>
#include <audioapi/utils/AudioArray.h>

#include <memory>
#include <utility>

namespace audioapi {

using namespace facebook;

class AudioArrayBuffer : public jsi::MutableBuffer {
public:
AudioArrayBuffer(uint8_t *data, size_t size): data_(data), size_(size) {}
~AudioArrayBuffer() override {
if (data_ == nullptr) {
return;
}
delete[] data_;
}
explicit AudioArrayBuffer(const std::shared_ptr<AudioArray> &audioArray): audioArray_(audioArray) {}
~AudioArrayBuffer() override = default;

AudioArrayBuffer(AudioArrayBuffer &&other) noexcept
: data_(other.data_), size_(other.size_) {
other.data_ = nullptr;
: audioArray_(std::move(other.audioArray_)) {
other.audioArray_ = nullptr;
}

AudioArrayBuffer(const AudioArrayBuffer &) = delete;
Expand All @@ -28,8 +28,7 @@ class AudioArrayBuffer : public jsi::MutableBuffer {
uint8_t *data() override;

private:
uint8_t *data_;
const size_t size_;
std::shared_ptr<AudioArray> audioArray_;
};

} // namespace audioapi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ AudioArray *AudioBus::getChannelByType(int channelType) const {
}
}

std::shared_ptr<AudioArray> AudioBus::getSharedChannel(int index) const {
return channels_[index];
}

AudioArray &AudioBus::operator[](size_t index) {
return *channels_[index];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class AudioBus {
[[nodiscard]] size_t getSize() const;
[[nodiscard]] AudioArray *getChannel(int index) const;
[[nodiscard]] AudioArray *getChannelByType(int channelType) const;
[[nodiscard]] std::shared_ptr<AudioArray> getSharedChannel(int index) const;

AudioArray &operator[](size_t index);
const AudioArray &operator[](size_t index) const;
Expand Down