Skip to content

Commit

Permalink
feat: added analyser constants to Constants.h
Browse files Browse the repository at this point in the history
  • Loading branch information
Maciej Makowski committed Jan 7, 2025
1 parent c37e640 commit 9eaebdb
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 27 deletions.
60 changes: 39 additions & 21 deletions packages/react-native-audio-api/common/cpp/core/AnalyserNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@

namespace audioapi {
AnalyserNode::AnalyserNode(audioapi::BaseAudioContext *context)
: AudioNode(context) {
channelCount_ = 1;
fftSize_ = 2048;
minDecibels_ = -100;
maxDecibels_ = -30;
smoothingTimeConstant_ = 0.8;
inputBus_ =
std::make_unique<AudioBus>(context->getSampleRate(), MAX_FFT_SIZE * 2, 1);
: AudioNode(context),
fftSize_(DEFAULT_FFT_SIZE),
minDecibels_(DEFAULT_MIN_DECIBELS),
maxDecibels_(DEFAULT_MAX_DECIBELS),
smoothingTimeConstant_(DEFAULT_SMOOTHING_TIME_CONSTANT),
vWriteIndex_(0) {
inputBuffer_ = std::make_unique<AudioArray>(MAX_FFT_SIZE * 2);
}

int AnalyserNode::getFftSize() const {
Expand All @@ -36,6 +35,13 @@ double AnalyserNode::getSmoothingTimeConstant() const {
}

void AnalyserNode::setFftSize(int fftSize) {
int log2size = static_cast<int>(log2(fftSize));
bool isPowerOfTwo(1UL << log2size == fftSize);

if (!isPowerOfTwo || fftSize < MIN_FFT_SIZE || fftSize > MAX_FFT_SIZE) {
return;
}

fftSize_ = fftSize;
}

Expand All @@ -51,25 +57,37 @@ void AnalyserNode::setSmoothingTimeConstant(double smoothingTimeConstant) {
smoothingTimeConstant_ = smoothingTimeConstant;
}

float *AnalyserNode::getFloatFrequencyData() {
return nullptr;
}
void AnalyserNode::getFloatFrequencyData(float *data) {}

uint8_t *AnalyserNode::getByteFrequencyData() {
return nullptr;
}
void AnalyserNode::getByteFrequencyData(float *data) {}

float *AnalyserNode::getFloatTimeDomainData() {
return nullptr;
}
void AnalyserNode::getFloatTimeDomainData(float *data) {}

uint8_t *AnalyserNode::getByteTimeDomainData() {
return nullptr;
}
void AnalyserNode::getByteTimeDomainData(float *data) {}

void AnalyserNode::processNode(
audioapi::AudioBus *processingBus,
int framesToProcess) {
// m_analyser.writeInput(inputBus, framesToProcess);
if (!isInitialized_) {
processingBus->zero();
return;
}

if (downMixBus_ == nullptr) {
downMixBus_ = std::make_unique<AudioBus>(
context_->getSampleRate(), processingBus->getSize(), 1);
}

downMixBus_->copy(processingBus);

memcpy(
inputBuffer_->getData() + vWriteIndex_,
downMixBus_->getChannel(0)->getData(),
framesToProcess * sizeof(float));

vWriteIndex_ += framesToProcess;
if (vWriteIndex_ >= inputBuffer_->getSize()) {
vWriteIndex_ = 0;
}
}
} // namespace audioapi
13 changes: 8 additions & 5 deletions packages/react-native-audio-api/common/cpp/core/AnalyserNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace audioapi {

class AudioBus;
class AudioArray;

class AnalyserNode : public AudioNode {
public:
Expand All @@ -23,10 +24,10 @@ class AnalyserNode : public AudioNode {
void setMaxDecibels(double maxDecibels);
void setSmoothingTimeConstant(double smoothingTimeConstant);

float *getFloatFrequencyData();
uint8_t *getByteFrequencyData();
float *getFloatTimeDomainData();
uint8_t *getByteTimeDomainData();
void getFloatFrequencyData(float *data);
void getByteFrequencyData(float *data);
void getFloatTimeDomainData(float *data);
void getByteTimeDomainData(float *data);

protected:
void processNode(AudioBus *processingBus, int framesToProcess) override;
Expand All @@ -37,7 +38,9 @@ class AnalyserNode : public AudioNode {
double maxDecibels_;
double smoothingTimeConstant_;

std::unique_ptr<AudioBus> inputBus_;
std::unique_ptr<AudioArray> inputBuffer_;
std::unique_ptr<AudioBus> downMixBus_;
int vWriteIndex_;
};

} // namespace audioapi
8 changes: 7 additions & 1 deletion packages/react-native-audio-api/common/cpp/core/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
// https://webaudio.github.io/web-audio-api/

namespace audioapi {
constexpr int SAMPLE_RATE = 44100;
constexpr int SAMPLE_RATE = 48000;
constexpr int CHANNEL_COUNT = 2;

constexpr float MOST_POSITIVE_SINGLE_FLOAT =
static_cast<float>(std::numeric_limits<float>::max());
constexpr float MOST_NEGATIVE_SINGLE_FLOAT =
static_cast<float>(std::numeric_limits<float>::lowest());

constexpr float NYQUIST_FREQUENCY = SAMPLE_RATE / 2.0;
static float MAX_DETUNE = 1200 * std::log2(MOST_POSITIVE_SINGLE_FLOAT);
constexpr float MAX_GAIN = MOST_POSITIVE_SINGLE_FLOAT;
Expand All @@ -24,4 +26,8 @@ constexpr float MIN_FILTER_GAIN = -MAX_GAIN;

constexpr int MAX_FFT_SIZE = 32768;
constexpr int MIN_FFT_SIZE = 32;
constexpr int DEFAULT_FFT_SIZE = 2048;
constexpr double DEFAULT_MAX_DECIBELS = -30;
constexpr double DEFAULT_MIN_DECIBELS = -100;
const double DEFAULT_SMOOTHING_TIME_CONSTANT = 0.8;
} // namespace audioapi

0 comments on commit 9eaebdb

Please sign in to comment.