@@ -173,9 +173,14 @@ class GoertzelDetector {
173
173
*/
174
174
const GoertzelConfig& getConfig () const { return config; }
175
175
176
+ void setReference (void * ref){ this ->reference = ref; }
177
+
178
+ void *getReference (){ return reference; }
179
+
176
180
protected:
177
181
GoertzelConfig config;
178
182
float coeff = 0 .0f ;
183
+ void *reference = nullptr ;
179
184
180
185
// State variables
181
186
float s1 = 0 .0f ;
@@ -279,6 +284,9 @@ class GoertzelStream : public AudioStream {
279
284
GoertzelConfig cfg = default_config;
280
285
cfg.target_frequency = freq;
281
286
GoertzelDetector detector;
287
+ if (i++ < references.size ()){
288
+ detector.setReference (references[i]);
289
+ }
282
290
detector.begin (cfg);
283
291
detectors.push_back (detector);
284
292
}
@@ -406,10 +414,26 @@ class GoertzelStream : public AudioStream {
406
414
*/
407
415
void addFrequency (float freq) { frequencies.push_back (freq); }
408
416
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
+
409
432
protected:
410
433
// Core detection components
411
434
Vector<GoertzelDetector> detectors; // /< One detector per frequency in frequencies
412
435
Vector<float > frequencies; // /< List of frequencies to detect
436
+ Vector<void *> references; // /< List of frequencies to detect
413
437
GoertzelConfig default_config; // /< Current algorithm configuration
414
438
// Stream I/O components
415
439
Stream* p_stream = nullptr ; // /< Input stream for reading audio data
@@ -430,9 +454,11 @@ class GoertzelStream : public AudioStream {
430
454
float magnitude = detector.getMagnitude ();
431
455
if (magnitude > default_config.threshold ) {
432
456
float frequency = detector.getTargetFrequency ();
457
+ void *reference = detector.getReference ();
458
+ if (reference==nullptr ) reference = ref;
433
459
if (frequency_detection_callback != nullptr ) {
434
460
frequency_detection_callback (default_config.channel , frequency,
435
- magnitude, ref );
461
+ magnitude, reference );
436
462
}
437
463
}
438
464
}
0 commit comments