|
DSPark 1.8.0
Header-only C++20 DSP for real-time and offline audio
|
Monophonic-buffer chord recognition with confidence gating. More...
#include <ChordDetector.h>
Classes | |
| struct | Result |
| One detection result. More... | |
Public Types | |
| enum class | ChordType : std::uint8_t { None = 0 , Major , Minor , Diminished , Augmented , Sus2 , Sus4 , Dominant7 , Major7 , Minor7 , HalfDim7 } |
| Recognized chord families. More... | |
Public Member Functions | |
| bool | prepare (const AudioSpec &spec, int windowSize=0) |
| Prepares the analysis pipeline. | |
| void | reset () noexcept |
| Clears the analysis ring and forgets the held chord. | |
| void | setConfidenceThreshold (float threshold) noexcept |
| Confidence below which the previous chord is held (default 0.55). | |
| float | getConfidenceThreshold () const noexcept |
| int | getWindowSize () const noexcept |
| int | getHopSize () const noexcept |
| std::uint64_t | getFrameCount () const noexcept |
| Number of analysis frames produced since prepare()/reset(). | |
| const std::array< T, 12 > & | getChroma () const noexcept |
| The chroma vector of the most recent analysis frame. | |
| void | processBlock (AudioBufferView< const T > buffer) noexcept |
| Feeds a block (channels averaged to mono). | |
| void | pushSamples (std::span< const T > samples) noexcept |
| Feeds mono samples directly. | |
| Result | getChord () const noexcept |
Static Public Member Functions | |
| static int | getChordName (const Result &result, char *dest, int size) noexcept |
| Writes a human-readable chord name ("C", "F#m7", "Bbsus4"...). | |
Monophonic-buffer chord recognition with confidence gating.
Threading: getChord() is the atomic foreign-thread publication. getChroma() is a stream-owner reference readout and is valid only on the thread that owns processBlock() / pushSamples(), between that thread's own calls. Calling getChroma() from another thread while processing runs would race the non-atomic chroma storage.
| T | Sample type (float or double). |
Definition at line 80 of file ChordDetector.h.
|
strong |
Recognized chord families.
| Enumerator | |
|---|---|
| None | |
| Major | |
| Minor | |
| Diminished | |
| Augmented | |
| Sus2 | |
| Sus4 | |
| Dominant7 | |
| Major7 | |
| Minor7 | |
| HalfDim7 | |
Definition at line 84 of file ChordDetector.h.
|
inlinenoexcept |
Definition at line 330 of file ChordDetector.h.
|
inlinestaticnoexcept |
Writes a human-readable chord name ("C", "F#m7", "Bbsus4"...).
| result | Chord to name. |
| dest | Destination buffer. |
| size | Capacity of dest (8+ recommended). |
Definition at line 342 of file ChordDetector.h.
|
inlinenoexcept |
The chroma vector of the most recent analysis frame.
Twelve bins of summed note ENERGY (Goertzel magnitude squared) over MIDI 36..83, folded by pitch class with index 0 = C. Raw, unnormalized and in the units the analysis produces, because a consumer that accumulates frames needs to choose its own weighting – normalizing here would destroy the frame-to-frame level information and pre-empt that choice. All zeros before the first frame.
The register the numbers are trustworthy in is the register documented on prepare(): energy below the leakage floor or above the MIDI 83 bin ceiling is not present in these bins, and adjacent-semitone leakage is.
stream-owner reference readout: the reference is to state owned by the thread that calls processBlock()/pushSamples(), and it is valid on that thread only, between that thread's own calls. A caller on any other thread would be reading these words while the owning thread writes them. getChord() is the readout for any other thread.
Definition at line 297 of file ChordDetector.h.
|
inlinenoexcept |
Definition at line 239 of file ChordDetector.h.
|
inlinenoexcept |
Number of analysis frames produced since prepare()/reset().
Increments once per hop, so a caller can tell a fresh chroma frame from the one it already consumed.
Reads plain state owned by the thread that pushes samples: call it from that thread only. It is NOT a cross-thread readout – getChord() is the one that is.
Definition at line 275 of file ChordDetector.h.
|
inlinenoexcept |
A second consumer of this front end feeds it in chunks of at most this many samples and polls getFrameCount(): frames are exactly this far apart, so a chunk that long spans at most one of them and none can be missed.
Definition at line 261 of file ChordDetector.h.
|
inlinenoexcept |
Definition at line 251 of file ChordDetector.h.
|
inline |
Prepares the analysis pipeline.
| spec | Audio environment specification. |
| windowSize | Analysis window in samples. Values <= 0 (the default) select the AUTOMATIC window: the smallest power of two in [1024, 16384] spanning at least 4096/48000 s (~85 ms) at spec.sampleRate – 4096 at 44.1/48 kHz, 8192 at 88.2/96 kHz, 16384 at 176.4/192 kHz. Explicit values are clamped to [1024, 16384]. Rates that cannot place the highest analysed note (MIDI 83, B5) below Nyquist are rejected and leave a previous valid configuration untouched. |
REGISTER BOUNDS – every number below is measured (a sweep of root-position pure-tone major triads, roots MIDI 36..84); "reliable" means correct root AND chord type through the 0.55 confidence gate:
Definition at line 161 of file ChordDetector.h.
|
inlinenoexcept |
Feeds a block (channels averaged to mono).
Definition at line 302 of file ChordDetector.h.
|
inlinenoexcept |
Feeds mono samples directly.
Definition at line 320 of file ChordDetector.h.
|
inlinenoexcept |
Clears the analysis ring and forgets the held chord.
Allocation-free, but it rewrites the stream state: call it from the thread that owns the stream (or while processing is stopped), not concurrently with processBlock()/pushSamples().
Definition at line 215 of file ChordDetector.h.
|
inlinenoexcept |
Confidence below which the previous chord is held (default 0.55).
Callable from any thread. Non-finite values are ignored (a NaN would make every comparison false and freeze the detector on the held chord forever).
Definition at line 232 of file ChordDetector.h.