Skip to content

Commit 2f8833f

Browse files
committed
GoertzelStream: freq specific reference
1 parent 1198c87 commit 2f8833f

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/AudioTools/CoreAudio/GoerzelStream.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,14 @@ class GoertzelDetector {
173173
*/
174174
const GoertzelConfig& getConfig() const { return config; }
175175

176+
void setReference(void* ref){ this->reference = ref; }
177+
178+
void *getReference(){ return reference; }
179+
176180
protected:
177181
GoertzelConfig config;
178182
float coeff = 0.0f;
183+
void *reference = nullptr;
179184

180185
// State variables
181186
float s1 = 0.0f;
@@ -279,6 +284,9 @@ class GoertzelStream : public AudioStream {
279284
GoertzelConfig cfg = default_config;
280285
cfg.target_frequency = freq;
281286
GoertzelDetector detector;
287+
if (i++ < references.size()){
288+
detector.setReference(references[i]);
289+
}
282290
detector.begin(cfg);
283291
detectors.push_back(detector);
284292
}
@@ -406,10 +414,26 @@ class GoertzelStream : public AudioStream {
406414
*/
407415
void addFrequency(float freq) { frequencies.push_back(freq); }
408416

417+
/**
418+
* @brief Add a frequency to the detection list with a custom reference pointer
419+
*
420+
* This method allows you to associate a user-defined reference (context pointer)
421+
* with a specific frequency. The reference will be passed to the detection callback
422+
* when this frequency is detected, enabling per-frequency context handling.
423+
*
424+
* @param freq Frequency in Hz to add to the detection list
425+
* @param ref Pointer to user-defined context object for this frequency
426+
*/
427+
void addFrequency(float freq, void* ref) {
428+
frequencies.push_back(freq);
429+
references.push_back(ref);
430+
}
431+
409432
protected:
410433
// Core detection components
411434
Vector<GoertzelDetector> detectors; ///< One detector per frequency in frequencies
412435
Vector<float> frequencies; ///< List of frequencies to detect
436+
Vector<void*> references; ///< List of frequencies to detect
413437
GoertzelConfig default_config; ///< Current algorithm configuration
414438
// Stream I/O components
415439
Stream* p_stream = nullptr; ///< Input stream for reading audio data
@@ -430,9 +454,11 @@ class GoertzelStream : public AudioStream {
430454
float magnitude = detector.getMagnitude();
431455
if (magnitude > default_config.threshold) {
432456
float frequency = detector.getTargetFrequency();
457+
void *reference = detector.getReference();
458+
if (reference==nullptr) reference = ref;
433459
if (frequency_detection_callback != nullptr) {
434460
frequency_detection_callback(default_config.channel, frequency,
435-
magnitude, ref);
461+
magnitude, reference);
436462
}
437463
}
438464
}

0 commit comments

Comments
 (0)