43#include "../Core/DspMath.h"
89 void prepare(
double sampleRate,
double targetFreqHz,
int blockSize)
noexcept
91 assert(sampleRate > 0.0 &&
"Sample rate must be positive");
92 assert(blockSize > 0 &&
"Block size must be strictly positive");
93 assert(targetFreqHz >= 0.0 && targetFreqHz <= (sampleRate * 0.5) &&
"Frequency must be within Nyquist limit");
95 if (!std::isfinite(sampleRate) || sampleRate <= 0.0
96 || !std::isfinite(targetFreqHz) || blockSize <= 0)
99 sampleRate_ = sampleRate;
100 targetFreq_ = std::clamp(targetFreqHz, 0.0, sampleRate * 0.5);
101 blockSize_ = blockSize;
104 double omega = 2.0 * std::numbers::pi * targetFreq_ / sampleRate_;
106 coeff_ = 2.0 * std::cos(omega);
107 cosOmega_ = std::cos(omega);
108 sinOmega_ = std::sin(omega);
113 const bool isDc = targetFreq_ <= 0.001;
114 const bool isNyquist = targetFreq_ >= sampleRate_ * 0.5 * (1.0 - 1e-12);
115 normalisationFactor_ = (isDc || isNyquist) ? 1.0 / blockSize_ : 2.0 / blockSize_;
131 if (data ==
nullptr || numSamples <= 0)
return;
136 const double coeff = coeff_;
138 for (
int i = 0; i < numSamples; ++i)
140 double s0 =
static_cast<double>(data[i]) + coeff * s1 - s2;
144 if (++sampleCount_ >= blockSize_)
146 computeResult(s1, s2);
166 double s0 =
static_cast<double>(sample) + coeff_ * s1_ - s2_;
170 if (++sampleCount_ >= blockSize_)
172 computeResult(s1_, s2_);
189 computeResult(s1_, s2_);
209 hasNewResult_ =
false;
221 return std::sqrt(real_ * real_ + imag_ * imag_);
230 return real_ * real_ + imag_ * imag_;
254 return std::atan2(imag_, real_);
281 hasNewResult_ =
false;
288 void computeResult(
double s1,
double s2)
noexcept
290 real_ =
static_cast<T
>((s1 - s2 * cosOmega_) * normalisationFactor_);
291 imag_ =
static_cast<T
>((s2 * sinOmega_) * normalisationFactor_);
292 hasNewResult_ =
true;
295 double sampleRate_ = 48000.0;
296 double targetFreq_ = 440.0;
297 int blockSize_ = 2048;
301 double cosOmega_ = 0.0;
302 double sinOmega_ = 0.0;
303 double normalisationFactor_ = 0.0;
308 int sampleCount_ = 0;
313 bool hasNewResult_ =
false;
Single-frequency magnitude detector using the Goertzel algorithm.
double getSampleRate() const noexcept
Returns the configured sample rate in Hz.
T getPower() const noexcept
Returns the power at the target frequency.
void processBlock(const T *data, int numSamples) noexcept
Processes a block of audio samples, updating the internal state.
double getTargetFrequency() const noexcept
Returns the currently configured target frequency.
void reset() noexcept
Resets the internal IIR state and counters to zero.
void forceCompute() noexcept
Manually forces the computation of the result before N samples are reached.
T getPhase() const noexcept
Returns the phase angle at the target frequency.
bool checkNewResultAvailable() noexcept
Checks if a new result has been computed.
T getMagnitude() const noexcept
Returns the magnitude at the target frequency (linear scale).
int getBlockSize() const noexcept
Returns the configured analysis block size in samples.
bool pushSample(T sample) noexcept
Feeds a single sample into the running Goertzel computation.
T getMagnitudeDb() const noexcept
Returns the magnitude in decibels.
void prepare(double sampleRate, double targetFreqHz, int blockSize) noexcept
Prepares the detector for a specific frequency.
Main namespace for the DSPark framework.
T gainToDecibels(T gain, T minusInfinityDb=T(-100)) noexcept
Converts a linear gain value to decibels.