Skip to content

Commit

Permalink
feat: channel mixer
Browse files Browse the repository at this point in the history
  • Loading branch information
michalsek committed Nov 15, 2024
1 parent 7ca0ce7 commit 3b4c9c7
Show file tree
Hide file tree
Showing 12 changed files with 550 additions and 169 deletions.
16 changes: 10 additions & 6 deletions packages/react-native-audio-api/common/cpp/core/AudioArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ int AudioArray::getSize() const {
return size_;
}

float* AudioArray::getData() const {
return data_;
}

float& AudioArray::operator[](int index) {
return data_[index];
}
Expand All @@ -40,12 +44,12 @@ void AudioArray::resize(int size) {
zero();
}

void AudioArray::copy(const AudioArray &source) {
if (size_ != source.size_) {
resize(source.size_);
void AudioArray::copy(const AudioArray* source) {
if (size_ != source->getSize()) {
resize(source->getSize());
}

memcpy(data_, source.data_, size_ * sizeof(float));
memcpy(data_, source->getData(), size_ * sizeof(float));
}

float AudioArray::getMaxAbsValue() const {
Expand All @@ -66,8 +70,8 @@ void AudioArray::scale(float value) {
VectorMath::multiplyByScalar(data_, value, data_, size_);
};

void AudioArray::sum(const AudioArray &source) {
VectorMath::add(data_, source.data_, data_, size_);
void AudioArray::sum(const AudioArray* source) {
VectorMath::add(data_, source->getData(), data_, size_);
};

} // namespace audioapi
Expand Down
6 changes: 4 additions & 2 deletions packages/react-native-audio-api/common/cpp/core/AudioArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@ class AudioArray {
~AudioArray();

[[nodiscard]] int getSize() const;
float* getData() const;


float& operator[](int index);
const float& operator[](int index) const;

void zero();
void resize(int size);
void copy(const AudioArray &source);
void copy(const AudioArray* source);

float getMaxAbsValue() const;

void normalize();
void scale(float value);
void sum(const AudioArray &source);
void sum(const AudioArray* source);


private:
float *data_;
Expand Down
146 changes: 78 additions & 68 deletions packages/react-native-audio-api/common/cpp/core/AudioBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,81 +7,91 @@ AudioBuffer::AudioBuffer(int numberOfChannels, int length, int sampleRate)
length_(length),
sampleRate_(sampleRate),
duration_(static_cast<double>(length) / sampleRate) {
channels_ = new float *[numberOfChannels];

for (int i = 0; i < numberOfChannels; i++) {
channels_[i] = new float[length];

for (int j = 0; j < length; j++) {
channels_[i][j] = 0.0f;
}
}
}

int AudioBuffer::getNumberOfChannels() const {
return numberOfChannels_;
}
} // namespace audioapi

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

int AudioBuffer::getSampleRate() const {
return sampleRate_;
}
// AudioBuffer::AudioBuffer(int numberOfChannels, int length, int sampleRate)
// : numberOfChannels_(numberOfChannels),
// length_(length),
// sampleRate_(sampleRate),
// duration_(static_cast<double>(length) / sampleRate) {
// channels_ = new float *[numberOfChannels];

double AudioBuffer::getDuration() const {
return duration_;
}
// for (int i = 0; i < numberOfChannels; i++) {
// channels_[i] = new float[length];

float *AudioBuffer::getChannelData(int channel) const {
return channels_[channel];
}
// for (int j = 0; j < length; j++) {
// channels_[i][j] = 0.0f;
// }
// }
// }

std::shared_ptr<AudioBuffer> AudioBuffer::mix(int outputNumberOfChannels) {
if (outputNumberOfChannels == numberOfChannels_) {
return shared_from_this();
}

auto mixedBuffer = std::make_shared<AudioBuffer>(
outputNumberOfChannels, length_, sampleRate_);

switch (this->numberOfChannels_) {
case 1:
mixedBuffer->copyToChannel(this->channels_[0], length_, 0, 0);
mixedBuffer->copyToChannel(this->channels_[0], length_, 1, 0);
break;
case 2:
for (int i = 0; i < length_; i++) {
mixedBuffer->channels_[0][i] =
(this->channels_[0][i] + this->channels_[1][i]) / 2;
}
break;
}

return mixedBuffer;
}
// int AudioBuffer::getNumberOfChannels() const {
// return numberOfChannels_;
// }

void AudioBuffer::copyFromChannel(
float *destination,
int destinationLength,
int channelNumber,
int startInChannel) const {
std::copy(
channels_[channelNumber] + startInChannel,
channels_[channelNumber] + startInChannel +
std::min(destinationLength, length_ - startInChannel),
destination);
}
// int AudioBuffer::getLength() const {
// return length_;
// }

void AudioBuffer::copyToChannel(
const float *source,
int sourceLength,
int channelNumber,
int startInChannel) {
std::copy(
source,
source + std::min(sourceLength, length_ - startInChannel),
channels_[channelNumber] + startInChannel);
}
} // namespace audioapi
// int AudioBuffer::getSampleRate() const {
// return sampleRate_;
// }

// double AudioBuffer::getDuration() const {
// return duration_;
// }

// float *AudioBuffer::getChannelData(int channel) const {
// return channels_[channel];
// }

// std::shared_ptr<AudioBuffer> AudioBuffer::mix(int outputNumberOfChannels) {
// if (outputNumberOfChannels == numberOfChannels_) {
// return shared_from_this();
// }

// auto mixedBuffer = std::make_shared<AudioBuffer>(
// outputNumberOfChannels, length_, sampleRate_);

// switch (this->numberOfChannels_) {
// case 1:
// mixedBuffer->copyToChannel(this->channels_[0], length_, 0, 0);
// mixedBuffer->copyToChannel(this->channels_[0], length_, 1, 0);
// break;
// case 2:
// for (int i = 0; i < length_; i++) {
// mixedBuffer->channels_[0][i] =
// (this->channels_[0][i] + this->channels_[1][i]) / 2;
// }
// break;
// }

// return mixedBuffer;
// }

// void AudioBuffer::copyFromChannel(
// float *destination,
// int destinationLength,
// int channelNumber,
// int startInChannel) const {
// std::copy(
// channels_[channelNumber] + startInChannel,
// channels_[channelNumber] + startInChannel +
// std::min(destinationLength, length_ - startInChannel),
// destination);
// }

// void AudioBuffer::copyToChannel(
// const float *source,
// int sourceLength,
// int channelNumber,
// int startInChannel) {
// std::copy(
// source,
// source + std::min(sourceLength, length_ - startInChannel),
// channels_[channelNumber] + startInChannel);
// }
9 changes: 5 additions & 4 deletions packages/react-native-audio-api/common/cpp/core/AudioBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@

namespace audioapi {

class AudioBus;

class AudioBuffer : public std::enable_shared_from_this<AudioBuffer> {
public:
explicit AudioBuffer(int numberOfChannels, int length, int sampleRate);

[[nodiscard]] int getNumberOfChannels() const;
[[nodiscard]] int getLength() const;
[[nodiscard]] int getSampleRate() const;
[[nodiscard]] double getDuration() const;

[[nodiscard]] int getNumberOfChannels() const;
[[nodiscard]] float *getChannelData(int channel) const;

void copyFromChannel(
Expand All @@ -34,9 +37,7 @@ class AudioBuffer : public std::enable_shared_from_this<AudioBuffer> {
int length_;
int sampleRate_;
double duration_;
float **channels_;

std::shared_ptr<AudioBuffer> mix(int outputNumberOfChannels);
std::unique_ptr<AudioBus> bus_;
};

} // namespace audioapi
Loading

0 comments on commit 3b4c9c7

Please sign in to comment.