5#include "AudioStreams.h"
8#define M_PI (3.14159265358979323846f)
77 this->config = config;
78 this->sample_count = 0;
86 coeff = 2.0f * cos(omega);
100 float s0 = sample + coeff * s1 - s2;
113 magnitude_squared = (real * real) + (imag * imag);
114 magnitude = sqrt(magnitude_squared);
142 bool isDetected(
float threshold)
const {
return magnitude > threshold; }
157 magnitude_squared = 0.0f;
175 void setReference(
void* ref) { this->reference = ref; }
177 void* getReference() {
return reference; }
180 GoertzelConfig config;
182 void* reference =
nullptr;
187 int sample_count = 0;
190 float magnitude = 0.0f;
191 float magnitude_squared = 0.0f;
284 LOGE(
"GoertzelStream: channel %d out of range (max %d)", config.
channel,
379 size_t write(
const uint8_t* data,
size_t len)
override {
384 if (
p_print ==
nullptr)
return len;
387 return p_print->write(data, len);
405 size_t result =
p_stream->readBytes(data, len);
493 if (magnitude > 0.0f)
498 void* reference = detector.getReference();
499 if (reference ==
nullptr) reference =
ref;
519 template <
typename T>
522 const T* samples =
reinterpret_cast<const T*
>(data);
523 size_t num_samples = data_len /
sizeof(T);
525 for (
size_t i = 0; i < num_samples; i++) {
528 float normalized =
clip(NumberConverter::toFloatT<T>(samples[i]) *
530 LOGD(
"sample: %f", normalized);
533 if (detector.processSample(normalized)) {
553 if (value > 1.0f)
return 1.0f;
554 if (value < -1.0f)
return -1.0f;
582 processSamplesOfType<uint8_t>(data, data_len, channels);
585 processSamplesOfType<int16_t>(data, data_len, channels);
588 processSamplesOfType<int24_t>(data, data_len, channels);
591 processSamplesOfType<int32_t>(data, data_len, channels);