diff --git a/packages/react-native-audio-api/common/cpp/core/AudioArray.cpp b/packages/react-native-audio-api/common/cpp/core/AudioArray.cpp index c137db8..d149e7d 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioArray.cpp +++ b/packages/react-native-audio-api/common/cpp/core/AudioArray.cpp @@ -5,11 +5,11 @@ namespace audioapi { AudioArray::AudioArray(int size) : size_(size) { resize(size); -}; +} AudioArray::~AudioArray() { delete[] data_; -}; +} int AudioArray::getSize() const { return size_; @@ -54,7 +54,7 @@ void AudioArray::copy(const AudioArray* source) { float AudioArray::getMaxAbsValue() const { return VectorMath::maximumMagnitude(data_, size_); -}; +} void AudioArray::normalize() { float maxAbsValue = getMaxAbsValue(); @@ -64,15 +64,15 @@ void AudioArray::normalize() { } VectorMath::multiplyByScalar(data_, 1.0f / maxAbsValue, data_, size_); -}; +} void AudioArray::scale(float value) { VectorMath::multiplyByScalar(data_, value, data_, size_); -}; +} void AudioArray::sum(const AudioArray* source) { VectorMath::add(data_, source->getData(), data_, size_); -}; +} } // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/core/AudioArray.h b/packages/react-native-audio-api/common/cpp/core/AudioArray.h index 0b79060..f61636d 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioArray.h +++ b/packages/react-native-audio-api/common/cpp/core/AudioArray.h @@ -5,31 +5,31 @@ namespace audioapi { class AudioArray { - public: - explicit AudioArray(int size); - ~AudioArray(); + public: + explicit AudioArray(int size); + ~AudioArray(); - [[nodiscard]] int getSize() const; - float* getData() const; + [[nodiscard]] int getSize() const; + float* getData() const; - float& operator[](int index); - const float& operator[](int index) const; + float& operator[](int index); + const float& operator[](int index) const; - void zero(); - void resize(int size); - void copy(const AudioArray* source); + void zero(); + void resize(int size); + void copy(const AudioArray* source); - float getMaxAbsValue() const; + float getMaxAbsValue() const; - void normalize(); - void scale(float value); - void sum(const AudioArray* source); + void normalize(); + void scale(float value); + void sum(const AudioArray* source); - private: - float *data_; - int size_; + private: + float *data_; + int size_; }; -} +} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/core/AudioBuffer.cpp b/packages/react-native-audio-api/common/cpp/core/AudioBuffer.cpp index 0a7a3e7..bef4f1c 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioBuffer.cpp +++ b/packages/react-native-audio-api/common/cpp/core/AudioBuffer.cpp @@ -7,7 +7,6 @@ AudioBuffer::AudioBuffer(int numberOfChannels, int length, int sampleRate) length_(length), sampleRate_(sampleRate), duration_(static_cast(length) / sampleRate) { - } } // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/core/AudioBuffer.h b/packages/react-native-audio-api/common/cpp/core/AudioBuffer.h index 642519e..98df077 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioBuffer.h +++ b/packages/react-native-audio-api/common/cpp/core/AudioBuffer.h @@ -32,7 +32,6 @@ class AudioBuffer : public std::enable_shared_from_this { int startInChannel); private: - int numberOfChannels_; int length_; int sampleRate_; diff --git a/packages/react-native-audio-api/common/cpp/core/AudioBus.cpp b/packages/react-native-audio-api/common/cpp/core/AudioBus.cpp index 4963185..fba622e 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioBus.cpp +++ b/packages/react-native-audio-api/common/cpp/core/AudioBus.cpp @@ -1,3 +1,4 @@ +#include #include "AudioBus.h" #include "Constants.h" @@ -22,7 +23,7 @@ AudioBus::AudioBus(BaseAudioContext *context) createChannels(); -}; +} AudioBus::AudioBus(BaseAudioContext *context, int numberOfChannels) : context_(context), numberOfChannels_(numberOfChannels) { @@ -30,19 +31,18 @@ AudioBus::AudioBus(BaseAudioContext *context, int numberOfChannels) size_ = context_->getBufferSizeInFrames(); createChannels(); -}; +} AudioBus::AudioBus(BaseAudioContext *context, int numberOfChannels, int size) : context_(context), numberOfChannels_(numberOfChannels), size_(size) { sampleRate_ = context_->getSampleRate(); createChannels(); -}; - +} AudioBus::~AudioBus() { channels_.clear(); -}; +} /** * Public interfaces - getters @@ -50,19 +50,19 @@ AudioBus::~AudioBus() { int AudioBus::getNumberOfChannels() const { return numberOfChannels_; -}; +} int AudioBus::getSampleRate() const { return sampleRate_; -}; +} int AudioBus::getSize() const { return size_; -}; +} AudioArray* AudioBus::getChannel(int index) const { return channels_[index].get(); -}; +} AudioArray* AudioBus::getChannelByType(int channelType) const { switch (getNumberOfChannels()) { @@ -111,7 +111,7 @@ AudioArray* AudioBus::getChannelByType(int channelType) const { default: return 0; } -}; +} /** * Public interfaces - audio processing and setters @@ -121,7 +121,7 @@ void AudioBus::zero() { for (auto it = channels_.begin(); it != channels_.end(); it += 1) { it->get()->zero(); } -}; +} void AudioBus::normalize() { float maxAbsValue = this->maxAbsValue(); @@ -132,13 +132,13 @@ void AudioBus::normalize() { float scale = 1.0f / maxAbsValue; this->scale(scale); -}; +} void AudioBus::scale(float value) { for (auto it = channels_.begin(); it != channels_.end(); it += 1) { it->get()->scale(value); } -}; +} float AudioBus::maxAbsValue() const { float maxAbsValue = 0.0f; @@ -149,7 +149,7 @@ float AudioBus::maxAbsValue() const { } return maxAbsValue; -}; +} void AudioBus::copy(const AudioBus &source) { if (&source == this) { @@ -159,7 +159,7 @@ void AudioBus::copy(const AudioBus &source) { // zero + sum is equivalent to copy, but takes care of up/down-mixing. zero(); sum(source); -}; +} void AudioBus::sum(const AudioBus &source) { if (&source == this) { @@ -186,7 +186,7 @@ void AudioBus::sum(const AudioBus &source) { for (int i = 0; i < numberOfChannels_; i++) { channels_[i]->sum(source.getChannel(i)); } -}; +} /** * Internal tooling - channel initialization @@ -195,7 +195,7 @@ void AudioBus::sum(const AudioBus &source) { void AudioBus::createChannels() { // TODO: verify if its correct way of memory allocation channels_ = std::vector>(numberOfChannels_, std::make_unique(size_)); -}; +} /** * Internal tooling - channel summing @@ -209,7 +209,7 @@ void AudioBus::discreteSum(const AudioBus &source) { for (int i = 0; i < numberOfChannels; i++) { getChannel(i)->sum(source.getChannel(i)); } -}; +} void AudioBus::sumByUpMixing(const AudioBus &source) { int numberOfSourceChannels = source.getNumberOfChannels(); @@ -234,7 +234,6 @@ void AudioBus::sumByUpMixing(const AudioBus &source) { // Stereo 2 to stereo 4 or 5.1 (2 -> 4, 6) if (numberOfSourceChannels == 2 && (numberOfChannels == 4 || numberOfChannels == 6)) { - getChannelByType(ChannelLeft)->sum(source.getChannelByType(ChannelLeft)); getChannelByType(ChannelRight)->sum(source.getChannelByType(ChannelRight)); return; @@ -250,7 +249,7 @@ void AudioBus::sumByUpMixing(const AudioBus &source) { } discreteSum(source); -}; +} void AudioBus::sumByDownMixing(const AudioBus &source) { int numberOfSourceChannels = source.getNumberOfChannels(); @@ -336,6 +335,6 @@ void AudioBus::sumByDownMixing(const AudioBus &source) { } discreteSum(source); -}; +} } // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/core/AudioBus.h b/packages/react-native-audio-api/common/cpp/core/AudioBus.h index 1c74b17..62981a5 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioBus.h +++ b/packages/react-native-audio-api/common/cpp/core/AudioBus.h @@ -9,49 +9,49 @@ class BaseAudioContext; class AudioArray; class AudioBus { - public: - enum { - ChannelMono = 0, - ChannelLeft = 0, - ChannelRight = 1, - ChannelCenter = 2, - ChannelLFE = 3, - ChannelSurroundLeft = 4, - ChannelSurroundRight = 5, - }; - - explicit AudioBus(BaseAudioContext *context); - explicit AudioBus(BaseAudioContext *context, int numberOfChannels); - explicit AudioBus(BaseAudioContext *context, int numberOfChannels, int size); - ~AudioBus(); - - [[nodiscard]] int getNumberOfChannels() const; - [[nodiscard]] int getSampleRate() const; - [[nodiscard]] int getSize() const; - AudioArray* getChannel(int index) const; - AudioArray* getChannelByType(int channelType) const; - - void zero(); - void normalize(); - void scale(float value); - float maxAbsValue() const; - - void copy(const AudioBus &source); - void sum(const AudioBus &source); - - private: - BaseAudioContext *context_; - std::vector> channels_; - - - int numberOfChannels_; - int sampleRate_; - int size_; - - void createChannels(); - void discreteSum(const AudioBus &source); - void sumByUpMixing(const AudioBus &source); - void sumByDownMixing(const AudioBus &source); + public: + enum { + ChannelMono = 0, + ChannelLeft = 0, + ChannelRight = 1, + ChannelCenter = 2, + ChannelLFE = 3, + ChannelSurroundLeft = 4, + ChannelSurroundRight = 5, + }; + + explicit AudioBus(BaseAudioContext *context); + explicit AudioBus(BaseAudioContext *context, int numberOfChannels); + explicit AudioBus(BaseAudioContext *context, int numberOfChannels, int size); + ~AudioBus(); + + [[nodiscard]] int getNumberOfChannels() const; + [[nodiscard]] int getSampleRate() const; + [[nodiscard]] int getSize() const; + AudioArray* getChannel(int index) const; + AudioArray* getChannelByType(int channelType) const; + + void zero(); + void normalize(); + void scale(float value); + float maxAbsValue() const; + + void copy(const AudioBus &source); + void sum(const AudioBus &source); + + private: + BaseAudioContext *context_; + std::vector> channels_; + + + int numberOfChannels_; + int sampleRate_; + int size_; + + void createChannels(); + void discreteSum(const AudioBus &source); + void sumByUpMixing(const AudioBus &source); + void sumByDownMixing(const AudioBus &source); }; -} +} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/core/AudioNode.cpp b/packages/react-native-audio-api/common/cpp/core/AudioNode.cpp index 6ee29a7..27d2b15 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioNode.cpp +++ b/packages/react-native-audio-api/common/cpp/core/AudioNode.cpp @@ -1,10 +1,11 @@ +#include "AudioBus.h" #include "AudioNode.h" #include "BaseAudioContext.h" namespace audioapi { AudioNode::AudioNode(BaseAudioContext *context) : context_(context) { - buffer_ = std::make_unique(context_, channelCount_); + audioBus_ = std::make_unique(context_, channelCount_); } AudioNode::~AudioNode() { @@ -25,46 +26,73 @@ int AudioNode::getChannelCount() const { std::string AudioNode::getChannelCountMode() const { return AudioNode::toString(channelCountMode_); -} +}; std::string AudioNode::getChannelInterpretation() const { return AudioNode::toString(channelInterpretation_); -} +}; void AudioNode::connect(const std::shared_ptr &node) { - if (numberOfOutputs_ > outputNodes_.size() && - node->getNumberOfInputs() > node->inputNodes_.size()) { - outputNodes_.push_back(node); - node->inputNodes_.push_back(shared_from_this()); - } -} + outputNodes_.push_back(node); + node->inputNodes_.push_back(shared_from_this()); +}; void AudioNode::disconnect(const std::shared_ptr &node) { outputNodes_.erase( std::remove(outputNodes_.begin(), outputNodes_.end(), node), outputNodes_.end()); + if (auto sharedThis = shared_from_this()) { node->inputNodes_.erase( std::remove( node->inputNodes_.begin(), node->inputNodes_.end(), sharedThis), node->inputNodes_.end()); } +}; + + +std::string AudioNode::toString(ChannelCountMode mode) { + switch (mode) { + case ChannelCountMode::MAX: + return "max"; + case ChannelCountMode::CLAMPED_MAX: + return "clamped-max"; + case ChannelCountMode::EXPLICIT: + return "explicit"; + default: + throw std::invalid_argument("Unknown channel count mode"); + } } +std::string AudioNode::toString(ChannelInterpretation interpretation) { + switch (interpretation) { + case ChannelInterpretation::SPEAKERS: + return "speakers"; + case ChannelInterpretation::DISCRETE: + return "discrete"; + default: + throw std::invalid_argument("Unknown channel interpretation"); + } +} + +bool AudioNode::processAudio(AudioBus *outputBus, int framesToProcess) { + // bool isPlaying = false; + // for (auto &node : inputNodes_) { + // if (node->processAudio(buffer_.get(), framesToProcess)) { + // isPlaying = true; + // } + // } + + // if (isPlaying) { + // outputBus->copyFrom(*buffer_.get(), 0, 0, framesToProcess); + // } + + // return isPlaying; +}; + void AudioNode::cleanup() { outputNodes_.clear(); inputNodes_.clear(); } -// bool AudioNode::processAudio(float *audioData, int32_t numFrames) { -// bool isPlaying = false; -// for (auto &node : inputNodes_) { -// if (node->processAudio(audioData, numFrames)) { -// isPlaying = true; -// } -// } - -// return isPlaying; -// } - } // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/core/AudioNode.h b/packages/react-native-audio-api/common/cpp/core/AudioNode.h index d96210c..f75b309 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioNode.h +++ b/packages/react-native-audio-api/common/cpp/core/AudioNode.h @@ -3,9 +3,6 @@ #include #include #include -#include "Constants.h" - -// channelCount always equal to 2 namespace audioapi { @@ -13,66 +10,43 @@ class BaseAudioContext; class AudioBus; class AudioNode : public std::enable_shared_from_this { - public: - explicit AudioNode(BaseAudioContext *context); - virtual ~AudioNode(); - int getNumberOfInputs() const; - int getNumberOfOutputs() const; - int getChannelCount() const; - std::string getChannelCountMode() const; - std::string getChannelInterpretation() const; - void connect(const std::shared_ptr &node); - void disconnect(const std::shared_ptr &node); - - // Change public to protected - // virtual bool processAudio(float *audioData, int32_t numFrames); - - protected: - enum class ChannelCountMode { MAX, CLAMPED_MAX, EXPLICIT }; - - static std::string toString(ChannelCountMode mode) { - switch (mode) { - case ChannelCountMode::MAX: - return "max"; - case ChannelCountMode::CLAMPED_MAX: - return "clamped-max"; - case ChannelCountMode::EXPLICIT: - return "explicit"; - default: - throw std::invalid_argument("Unknown channel count mode"); - } - } - - enum class ChannelInterpretation { SPEAKERS, DISCRETE }; - - static std::string toString(ChannelInterpretation interpretation) { - switch (interpretation) { - case ChannelInterpretation::SPEAKERS: - return "speakers"; - case ChannelInterpretation::DISCRETE: - return "discrete"; - default: - throw std::invalid_argument("Unknown channel interpretation"); - } - } - - protected: - BaseAudioContext *context_; - std::unique_ptr buffer_; - - int numberOfInputs_ = 1; - int numberOfOutputs_ = 1; - int channelCount_ = CHANNEL_COUNT; - - ChannelCountMode channelCountMode_ = ChannelCountMode::MAX; - ChannelInterpretation channelInterpretation_ = - ChannelInterpretation::SPEAKERS; - - std::vector> inputNodes_ = {}; - std::vector> outputNodes_ = {}; - - private: - void cleanup(); + public: + explicit AudioNode(BaseAudioContext *context); + virtual ~AudioNode(); + int getNumberOfInputs() const; + int getNumberOfOutputs() const; + int getChannelCount() const; + std::string getChannelCountMode() const; + std::string getChannelInterpretation() const; + void connect(const std::shared_ptr &node); + void disconnect(const std::shared_ptr &node); + + protected: + enum class ChannelCountMode { MAX, CLAMPED_MAX, EXPLICIT }; + enum class ChannelInterpretation { SPEAKERS, DISCRETE }; + + static std::string toString(ChannelCountMode mode); + static std::string toString(ChannelInterpretation interpretation); + + protected: + BaseAudioContext *context_; + std::unique_ptr audioBus_; + + int numberOfInputs_ = 1; + int numberOfOutputs_ = 1; + int channelCount_ = CHANNEL_COUNT; + + ChannelCountMode channelCountMode_ = ChannelCountMode::MAX; + ChannelInterpretation channelInterpretation_ = + ChannelInterpretation::SPEAKERS; + + std::vector> inputNodes_ = {}; + std::vector> outputNodes_ = {}; + + bool processAudio(AudioBus *outputBus, int framesToProcess); + + private: + void cleanup(); }; } // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/utils/VectorMath.cpp b/packages/react-native-audio-api/common/cpp/utils/VectorMath.cpp index 9e2692e..4195b10 100644 --- a/packages/react-native-audio-api/common/cpp/utils/VectorMath.cpp +++ b/packages/react-native-audio-api/common/cpp/utils/VectorMath.cpp @@ -115,8 +115,7 @@ float maximumMagnitude( return maximumValue; } -void multiplyByScalarThenAddToOutput(const float* inputVector, float scalar, float* outputVector, size_t numberOfElementsToProcess) -{ +void multiplyByScalarThenAddToOutput(const float* inputVector, float scalar, float* outputVector, size_t numberOfElementsToProcess) { vDSP_vsma(inputVector, 1, &scalar, outputVector, 1, outputVector, 1, numberOfElementsToProcess); } @@ -610,8 +609,7 @@ float maximumMagnitude( return max; } -void multiplyByScalarThenAddToOutput(const float* inputVector, float scalar, float* outputVector, size_t numberOfElementsToProcess) -{ +void multiplyByScalarThenAddToOutput(const float* inputVector, float scalar, float* outputVector, size_t numberOfElementsToProcess) { size_t n = numberOfElementsToProcess; #if HAVE_X86_SSE2