DSPark 1.6.1
Header-only audio DSP framework in pure C++20 — zero dependencies
Loading...
Searching...
No Matches
dspark::PitchDetector< T > Class Template Reference

Thread-safe YIN pitch detector with lock-free readout. More...

#include <PitchDetector.h>

Public Member Functions

void prepare (double sampleRate, int windowSize=2048, int hopSize=512)
 Prepares the detector and allocates internal structures.
 
void pushSamples (std::span< const T > samples) noexcept
 Pushes audio samples into the analysis buffer.
 
getFrequencyHz () const noexcept
 Returns the detected frequency in Hz safely from any thread.
 
getConfidence () const noexcept
 Returns the detection confidence [0.0 - 1.0] safely from any thread.
 
int getMidiNote () const noexcept
 Returns nearest MIDI note (69 = A4), or -1 if unvoiced.
 
getCentsOffset () const noexcept
 Returns cent offset from the nearest MIDI note [-50, +50].
 
void setThreshold (T threshold) noexcept
 Sets the sensitivity threshold (clamped to 0.01 - 0.5). Lower is stricter. Non-finite values are ignored.
 
getThreshold () const noexcept
 Returns the sensitivity threshold.
 
void reset () noexcept
 Resets state buffers. Not thread-safe with pushSamples().
 

Detailed Description

template<FloatType T>
class dspark::PitchDetector< T >

Thread-safe YIN pitch detector with lock-free readout.

A non-finite stretch in the input signal simply reads as "unvoiced" (frequency 0, confidence 0) and flushes out of the analysis window on its own: the pipeline holds no recursive state, so the detector self-recovers once clean samples refill the window.

Template Parameters
TSample type (float or double).

Definition at line 53 of file PitchDetector.h.

Member Function Documentation

◆ getCentsOffset()

template<FloatType T>
T dspark::PitchDetector< T >::getCentsOffset ( ) const
inlinenoexcept

Returns cent offset from the nearest MIDI note [-50, +50].

Definition at line 166 of file PitchDetector.h.

◆ getConfidence()

template<FloatType T>
T dspark::PitchDetector< T >::getConfidence ( ) const
inlinenoexcept

Returns the detection confidence [0.0 - 1.0] safely from any thread.

Definition at line 152 of file PitchDetector.h.

◆ getFrequencyHz()

template<FloatType T>
T dspark::PitchDetector< T >::getFrequencyHz ( ) const
inlinenoexcept

Returns the detected frequency in Hz safely from any thread.

Definition at line 146 of file PitchDetector.h.

◆ getMidiNote()

template<FloatType T>
int dspark::PitchDetector< T >::getMidiNote ( ) const
inlinenoexcept

Returns nearest MIDI note (69 = A4), or -1 if unvoiced.

Definition at line 158 of file PitchDetector.h.

◆ getThreshold()

template<FloatType T>
T dspark::PitchDetector< T >::getThreshold ( ) const
inlinenoexcept

Returns the sensitivity threshold.

Definition at line 185 of file PitchDetector.h.

◆ prepare()

template<FloatType T>
void dspark::PitchDetector< T >::prepare ( double  sampleRate,
int  windowSize = 2048,
int  hopSize = 512 
)
inline

Prepares the detector and allocates internal structures.

Must be called before audio processing begins. Zero allocations happen after this point. Release-safe: a non-finite or non-positive sample rate is ignored (no-op keeping the previous configuration); windowSize is clamped to [64, 1 << 20].

Parameters
sampleRateThe system sample rate in Hz.
windowSizeAnalysis window size in samples (default: 2048).
hopSizeNumber of samples between detections (overlap). Lower is smoother.

Definition at line 68 of file PitchDetector.h.

◆ pushSamples()

template<FloatType T>
void dspark::PitchDetector< T >::pushSamples ( std::span< const T >  samples)
inlinenoexcept

Pushes audio samples into the analysis buffer.

Automatically triggers pitch detection when the hop size is reached. Lock-free and allocation-free. No-op before prepare().

Note
The difference function runs as a frequency-domain cross-correlation (YIN-FFT): O(N log N) per detection - roughly 20x faster than the direct O(windowSize^2) form at the 2048 default and generally fine on the audio thread. For very low-latency callbacks you can still feed an SPSC queue (Core/SpscQueue.h) and detect on a worker.
Parameters
samplesSpan of input audio data (mono).

Definition at line 119 of file PitchDetector.h.

◆ reset()

template<FloatType T>
void dspark::PitchDetector< T >::reset ( )
inlinenoexcept

Resets state buffers. Not thread-safe with pushSamples().

Definition at line 191 of file PitchDetector.h.

◆ setThreshold()

template<FloatType T>
void dspark::PitchDetector< T >::setThreshold ( threshold)
inlinenoexcept

Sets the sensitivity threshold (clamped to 0.01 - 0.5). Lower is stricter. Non-finite values are ignored.

Definition at line 178 of file PitchDetector.h.


The documentation for this class was generated from the following file: