|
DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
|
Tempo and beat tracking with an offline grid and a causal readout. More...
#include <BeatTracker.h>
Classes | |
| struct | Result |
| What analyze() returns: one tempo, one grid, one number saying how much of the signal that grid explains, and the metrical alternative that lost. More... | |
Public Member Functions | |
| BeatTracker () | |
| void | prepare (const AudioSpec &spec) |
| Allocates all causal state and prepares the shared front end. | |
| void | setTempoRange (T minBpm, T maxBpm) noexcept |
| Restricts the searched tempo range, in BPM. Lock free, no alloc. | |
| void | applyTempoRange () noexcept |
| Selects the part of the bank the requested range covers. | |
| void | setTightness (T alpha) noexcept |
| Sets the dynamic-programming tightness (Ellis alpha). | |
| T | getTightness () const noexcept |
| Tightness currently in force. | |
| Result | analyze (AudioBufferView< const T > whole) |
| Tracks tempo and beats over a whole mono buffer (channel 0). | |
| void | beginOffline (int64_t expectedSamples=0) |
| Opens an incremental offline analysis. | |
| void | pushOffline (std::span< const T > samples) |
| Feeds the next piece of an offline session (channel-0 samples). No-op outside one. | |
| Result | finishOffline () |
| Closes the session and tracks tempo and beats over everything pushed, as analyze() does. All-zero Result when no session is open. | |
| void | processBlock (AudioBufferView< const T > in) noexcept |
| Feeds a mono block; reads channel 0 only. Const, never mutated. Lock free and allocation free. Safe no-op before prepare(). | |
| void | pushSamples (std::span< const T > samples) noexcept |
| Feeds a mono stream of samples. Lock free, allocation free. | |
| T | getBeatTempoBpm () const noexcept |
| Tempo the causal beat clock runs at, in BPM; 0 before it has one. Lock-free. | |
| T | getRunningTempoBpm () const noexcept |
| Running tempo estimate in BPM, 0 before the first frame. | |
| T | getConfidence () const noexcept |
| Coherence of the envelope with the running tempo, in [0,1]. | |
| void | getTempoAndConfidence (T &bpmOut, T &confidenceOut) const noexcept |
| Running tempo and its confidence, from ONE load. | |
| bool | beatNow () const noexcept |
| True if a beat was reported during the most recent call. | |
| int64_t | getLastBeatSample () const noexcept |
| Sample index the most recent causal beat is attributed to; -1 before the first one. | |
| int | getLatencySamples () const noexcept |
| Delay between a beat and the call in which it is announced. | |
| double | getFrameRate () const noexcept |
| Analysis frames per second of the shared envelope. | |
| const OnsetDetector< T > & | getOnsetDetector () const noexcept |
| Read-only access to the front end, for callers that also want onsets and would otherwise run a second copy of the same STFT. | |
| void | reset () noexcept |
| Clears all streaming state and abandons an open offline session. Not concurrent with pushSamples(). | |
Static Public Member Functions | |
| static constexpr T | defaultTightness () noexcept |
| The tightness in force until setTightness() is called. | |
Friends | |
| struct | detail::OfflineBeatEngine< T > |
Tempo and beat tracking with an offline grid and a causal readout.
Role: analysis readout. It consumes const audio and never mutates it. All heap use for the causal path happens in prepare(); processBlock() allocates nothing, takes no lock and throws nothing. analyze() is offline and allocates input-dependent envelope and grid working storage.
| T | Sample type (float or double). |
Definition at line 432 of file BeatTracker.h.
|
inline |
Definition at line 439 of file BeatTracker.h.
|
inline |
Tracks tempo and beats over a whole mono buffer (channel 0).
Allocates; not an audio-thread call, and not concurrent with the audio path – it drives the same front end, so it CLEARS the causal state on entry and leaves it cleared.
Returns an all-zero Result with an empty grid when the input is shorter than the analysis needs (kMinAnalysisBeats beats at the slowest searched tempo), rather than a tempo fitted to nothing.
Definition at line 648 of file BeatTracker.h.
|
inlinenoexcept |
Selects the part of the bank the requested range covers.
Definition at line 541 of file BeatTracker.h.
|
inlinenoexcept |
True if a beat was reported during the most recent call.
Acquire, paired with the release the processing call ends on: a reader that sees this true and then reads getLastBeatSample() is guaranteed the position written in that same call or a later one, never an earlier one.
That is the order to use, and it is the only safe one. Reading the two the other way round – position first, latch second – is not covered by the pairing: the two loads are separate, the processing call may run between them, and the reader is then told a beat just happened while holding the position of an earlier one. Measured, that order returns a stale position on essentially every beat it observes, by one full beat period and by more when the reader's two loads are further apart than one beat. See the threading notes in the file header.
Definition at line 874 of file BeatTracker.h.
|
inline |
Opens an incremental offline analysis.
For material that arrives in pieces – a file decoded block by block, a recording too long to hold – without concatenating it first. Feed it with pushOffline() in blocks of any size and close it with finishOffline(); the Result is bit-identical to analyze() over the concatenation, whatever the blocking. Only the onset envelope is kept between calls (a few values per 5 ms hop), not the audio.
Same threading as analyze(): it drives the shared front end, so it clears the causal state on entry and must not be interleaved with processBlock() / pushSamples() or run while the audio path is running.
| expectedSamples | Optional length hint, used only to reserve the envelope up front. |
Definition at line 674 of file BeatTracker.h.
|
inlinestaticconstexprnoexcept |
The tightness in force until setTightness() is called.
Definition at line 630 of file BeatTracker.h.
|
inline |
Closes the session and tracks tempo and beats over everything pushed, as analyze() does. All-zero Result when no session is open.
Definition at line 717 of file BeatTracker.h.
|
inlinenoexcept |
Tempo the causal beat clock runs at, in BPM; 0 before it has one. Lock-free.
The beats follow the metrical level of the comb stage (see the file header), which reads how the envelope repeats; getRunningTempoBpm() reports the pulse that carries the most onset mass in phase. They agree on most material and differ by a metrical factor where an off-beat carries as much onset mass as the beat: then this one names the rate the beats are announced at.
Definition at line 795 of file BeatTracker.h.
|
inlinenoexcept |
Coherence of the envelope with the running tempo, in [0,1].
The share of onset strength that falls in phase with the delivered grid, discounted by how nearly another metrical level explained the same envelope. See the file header for the definition and for the closed form it can be checked against.
WHAT IT DOES NOT TELL YOU. It is not a detector of half and double tempo, and no number computed from the signal can be: a click train with alternate events attenuated is at once a backbeat at the fast rate and straight eighths at the slow one, so the two cases that a caller would want told apart are the same waveform. Measured on straight eighths, the tracker reports the eighth level above the amplitude ratios in the file header and reports it with a confidence of 0.99, because that grid does explain nearly all of the onset strength – which is what the number says, and it is true. Check secondaryTempoBpm whenever the metrical level matters; it holds the other reading at every one of those points.
A LOW value means one of two things and they are worth separating: the grid explains little of what happened, or another level explains it just as well. The second is the case the discount produces, and there the number falls to zero rather than to something middling – measured 0.000 on straight eighths at the amplitude where the two readings are level, against 0.99 on either side of it.
Definition at line 834 of file BeatTracker.h.
|
inlinenoexcept |
Analysis frames per second of the shared envelope.
Definition at line 912 of file BeatTracker.h.
|
inlinenoexcept |
Sample index the most recent causal beat is attributed to; -1 before the first one.
This is where the beat WAS, in the caller's own timeline, not when it was announced. The two differ by getLatencySamples() and a caller that aligns anything to the grid wants this one: the announcement carries the analysis delay, the attribution does not.
Relaxed on its own. To pair it with beatNow(), call beatNow() FIRST and this second; the reverse order can hand back the position of an earlier beat than the one the latch announced. See beatNow().
Definition at line 892 of file BeatTracker.h.
|
inlinenoexcept |
Delay between a beat and the call in which it is announced.
Half the analysis frame less the front end's group-delay compensation, plus two hops: a beat is announced on the frame after the one it is placed on and attributed one hop before that frame (see advanceBeatClock()). Fixed for a given sample rate and independent of block size: 770 samples at 44.1 kHz.
Definition at line 906 of file BeatTracker.h.
|
inlinenoexcept |
Read-only access to the front end, for callers that also want onsets and would otherwise run a second copy of the same STFT.
Definition at line 916 of file BeatTracker.h.
|
inlinenoexcept |
Running tempo estimate in BPM, 0 before the first frame.
Definition at line 801 of file BeatTracker.h.
|
inlinenoexcept |
Running tempo and its confidence, from ONE load.
The pair is published as a single word, so this hands back two numbers that were measured at the same instant. Calling the two single getters one after the other can straddle an update and pair a fresh tempo with the confidence of the previous one, which is the reading that would let a caller trust a tempo the tracker had already stopped believing.
Definition at line 850 of file BeatTracker.h.
|
inlinenoexcept |
Tightness currently in force.
Definition at line 624 of file BeatTracker.h.
|
inline |
Allocates all causal state and prepares the shared front end.
Not real-time safe. An invalid spec (non-finite or non-positive sample rate) is ignored and the previous configuration is preserved.
The resonator bank is built once here, over the whole supported tempo span (kBankMinBpm..kBankMaxBpm) rather than over the range in force, so that setTempoRange() can stay allocation free and callable from the control thread while audio runs. The span reaches an octave BELOW the slowest searchable tempo on purpose: the octave discriminator needs the candidate's own subharmonic, which is a resonator that must exist.
The tempo range and tightness survive it, whether they were set before the first prepare() or between two.
Definition at line 488 of file BeatTracker.h.
|
inlinenoexcept |
Feeds a mono block; reads channel 0 only. Const, never mutated. Lock free and allocation free. Safe no-op before prepare().
Definition at line 731 of file BeatTracker.h.
|
inline |
Feeds the next piece of an offline session (channel-0 samples). No-op outside one.
Definition at line 696 of file BeatTracker.h.
|
inlinenoexcept |
Feeds a mono stream of samples. Lock free, allocation free.
The stream is handed to the front end in pieces that end exactly on its analysis-frame boundaries, so this call knows precisely which frames it caused and can read each one's envelope value as it appears. The result does not depend on how the caller blocks the stream.
Definition at line 747 of file BeatTracker.h.
|
inlinenoexcept |
Clears all streaming state and abandons an open offline session. Not concurrent with pushSamples().
Definition at line 923 of file BeatTracker.h.
|
inlinenoexcept |
Restricts the searched tempo range, in BPM. Lock free, no alloc.
Clamped into the bank's supported span and to min <= max; a non-finite or inverted request is ignored and the previous range kept. Takes effect on the next analysis frame for the causal path and on the next call for analyze(). The published running tempo stays inside it.
Definition at line 526 of file BeatTracker.h.
|
inlinenoexcept |
Sets the dynamic-programming tightness (Ellis alpha).
Weight of the interval-consistency term against the onset evidence: larger holds the grid closer to its period, smaller lets it follow the envelope. Offline path only – the causal path has no transition cost to weigh. Non-finite values are ignored; values are clamped to a positive floor because a tightness of zero removes the term the recursion is built on and leaves the grid free to place a beat at every envelope peak.
WHAT IT HOLDS. The grid is laid in two passes (see layGrid()). The first follows the tempo, at the lesser of this value and 25. The second is held at this value to the period the first one kept, wherever the first one kept it, and follows at the first pass's value where it did not. So the number is the tightness of the passages at a steady tempo, and a value of 25 or less lays both passes alike: one loose grid.
Default 400, the value the method's paper names as its default.
MEASURED on annotated music – 698 excerpts of dance music, 243 takes of a drum kit alone, 360 of a guitar alone – as the mean F-measure at +/-70 ms against the best reading of the annotation (as annotated, twice or half as fast, on the off-beats, in threes):
alpha 25 100 200 400 800 1600 dance 0.859 0.898 0.912 0.919 0.919 0.912 drums 0.869 0.906 0.920 0.925 0.926 0.923 guitar 0.690 0.752 0.773 0.786 0.787 0.784
and against the annotation as it is, 0.789, 0.819, 0.828, 0.830, 0.832 and 0.826; 0.768, 0.794, 0.803, 0.805, 0.805 and 0.802; 0.525, 0.542, 0.553, 0.559, 0.557 and 0.558. Below the default the grid leaves the beat for the notes beside it: at 25 the share of its intervals within an eighth of their median is 0.955, 0.955 and 0.893, against 0.998, 0.997 and 0.995 at 400. The default sits where the three rows level off, and the paper's value is on that level; 800 is level with it and 1600 is lower on all three.
And on clicks, where the truth is exact, F at +/-70 ms: a tempo swinging by 15% every sixteen beats and every nine, and a bar whose loudest note is a sixteenth before a beat that carries nothing, read 1.000, 1.000 and 1.000 at the default, and at 1600 as well: the grid is held only where the tempo holds. With the term deleted (0.001) the third reads 0.765.
A CAUTION ON MEASURING IT. On isochronous clicks this parameter does next to nothing: every interval is the target period, so the cost it weighs is zero, and the grids at alpha = 0.001 – which deletes the term outright – and at alpha = 4000 name the same beats within two analysis frames of each other. A sweep run only on steady clicks measures nothing and will report no spread whatever the code does.
Definition at line 616 of file BeatTracker.h.
|
friend |
Definition at line 432 of file BeatTracker.h.