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 2c06d562..b88028f0 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioArray.cpp +++ b/packages/react-native-audio-api/common/cpp/core/AudioArray.cpp @@ -1,4 +1,5 @@ #include "AudioArray.h" +#include "VectorMath.h" namespace audioapi { @@ -47,4 +48,27 @@ void AudioArray::copy(const AudioArray &source) { memcpy(data_, source.data_, size_ * sizeof(float)); } -} +float AudioArray::getMaxAbsValue() const { + return VectorMath::maximumMagnitude(data_, size_); +}; + +void AudioArray::normalize() { + float maxAbsValue = getMaxAbsValue(); + + if (maxAbsValue == 0.0f || maxAbsValue == 1.0f) { + return; + } + + 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.data_, 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 564721c3..a1cd1e30 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioArray.h +++ b/packages/react-native-audio-api/common/cpp/core/AudioArray.h @@ -19,6 +19,12 @@ class AudioArray { void resize(int size); void copy(const AudioArray &source); + float getMaxAbsValue() const; + + void normalize(); + void scale(float value); + void sum(const AudioArray &source); + private: float *data_; int size_; diff --git a/packages/react-native-audio-api/common/cpp/core/AudioChannel.cpp b/packages/react-native-audio-api/common/cpp/core/AudioChannel.cpp index e69de29b..a0880f53 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioChannel.cpp +++ b/packages/react-native-audio-api/common/cpp/core/AudioChannel.cpp @@ -0,0 +1,18 @@ +#include "AudioChannel.h" +#include "AudioArray.h" + +namespace audioapi { + +explicit AudioChannel::AudioChannel(int length, int sampleRate) : length_(length), sampleRate_(sampleRate) { + data_ = std::make_unique(length_); +}; + +int AudioChannel::getLength() const { + return length_; +}; + +int AudioChannel::getSampleRate() const { + return sampleRate_; +}; + +} // namespace audioapi diff --git a/packages/react-native-audio-api/common/cpp/core/AudioChannel.h b/packages/react-native-audio-api/common/cpp/core/AudioChannel.h index add540d4..78054fc5 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioChannel.h +++ b/packages/react-native-audio-api/common/cpp/core/AudioChannel.h @@ -13,12 +13,18 @@ class AudioChannel { [[nodiscard]] int getSampleRate() const; void zero(); - void scale(float scale); + void copy(const AudioChannel &source); + + float& operator[](int index); + const float& operator[](int index) const; + - void sumFrom(const AudioChannel &source); - void copyFrom(const AudioChannel &source); + float getMaxAbsValue() const; + + void normalize(); + void scale(float scale); + void sum(const AudioChannel &source); - float maxAbsValue() const; private: diff --git a/packages/react-native-audio-api/common/cpp/core/AudioDestinationNode.cpp b/packages/react-native-audio-api/common/cpp/core/AudioDestinationNode.cpp index d3bb6327..8ab3d4e6 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioDestinationNode.cpp +++ b/packages/react-native-audio-api/common/cpp/core/AudioDestinationNode.cpp @@ -1,5 +1,6 @@ #include "AudioDestinationNode.h" #include "BaseAudioContext.h" +#include "VectorMath.h" namespace audioapi { diff --git a/packages/react-native-audio-api/common/cpp/core/AudioDestinationNode.h b/packages/react-native-audio-api/common/cpp/core/AudioDestinationNode.h index 8ee32082..048dc3bd 100644 --- a/packages/react-native-audio-api/common/cpp/core/AudioDestinationNode.h +++ b/packages/react-native-audio-api/common/cpp/core/AudioDestinationNode.h @@ -5,7 +5,6 @@ #include #include "AudioNode.h" -#include "VectorMath.h" namespace audioapi { diff --git a/packages/react-native-audio-api/common/cpp/core/StereoPannerNode.h b/packages/react-native-audio-api/common/cpp/core/StereoPannerNode.h index 36fb7286..d49ad1a4 100644 --- a/packages/react-native-audio-api/common/cpp/core/StereoPannerNode.h +++ b/packages/react-native-audio-api/common/cpp/core/StereoPannerNode.h @@ -5,7 +5,6 @@ #include "AudioNode.h" #include "AudioParam.h" -#include "VectorMath.h" namespace audioapi {