Skip to content

Commit

Permalink
feat: more work
Browse files Browse the repository at this point in the history
  • Loading branch information
michalsek committed Nov 13, 2024
1 parent ea5cd2e commit 7ca0ce7
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 7 deletions.
26 changes: 25 additions & 1 deletion packages/react-native-audio-api/common/cpp/core/AudioArray.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "AudioArray.h"
#include "VectorMath.h"

namespace audioapi {

Expand Down Expand Up @@ -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

6 changes: 6 additions & 0 deletions packages/react-native-audio-api/common/cpp/core/AudioArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down
18 changes: 18 additions & 0 deletions packages/react-native-audio-api/common/cpp/core/AudioChannel.cpp
Original file line number Diff line number Diff line change
@@ -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<AudioArray>(length_);
};

int AudioChannel::getLength() const {
return length_;
};

int AudioChannel::getSampleRate() const {
return sampleRate_;
};

} // namespace audioapi
14 changes: 10 additions & 4 deletions packages/react-native-audio-api/common/cpp/core/AudioChannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "AudioDestinationNode.h"
#include "BaseAudioContext.h"
#include "VectorMath.h"

namespace audioapi {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <vector>

#include "AudioNode.h"
#include "VectorMath.h"

namespace audioapi {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include "AudioNode.h"
#include "AudioParam.h"
#include "VectorMath.h"

namespace audioapi {

Expand Down

0 comments on commit 7ca0ce7

Please sign in to comment.