73 lines
3.3 KiB
Swift
73 lines
3.3 KiB
Swift
// BeatTrackConstants.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
import Foundation
|
||
|
||
/// Number of ``BeatTrack`` output rows: beat, eighth, sixteenth, tempo.
|
||
public let beatTrackOutputRowCount: Int = 4
|
||
|
||
/// Onset-detection-function (ODF) decimation target rate in Hz. The per-block
|
||
/// ODF is reduced to one feature per `round(sampleRate / beatTrackFeatureRateHz)`
|
||
/// samples so the autocorrelation window length is independent of block size.
|
||
/// `≈ 86 Hz` matches the conventional 512-sample STFT hop at 44.1 kHz.
|
||
public let beatTrackFeatureRateHz: Double = 86.0
|
||
|
||
/// Length of the ODF history (seconds) retained for autocorrelation. Six seconds
|
||
/// at `beatTrackFeatureRateHz` is ≈ 516 features — long enough to resolve tempi
|
||
/// down to ``beatTrackMinBPM`` with several beat periods in the window.
|
||
public let beatTrackMaxWindowSeconds: Double = 6.0
|
||
|
||
/// Slowest detectable tempo (BPM). Sets the longest autocorrelation lag.
|
||
public let beatTrackMinBPM: Double = 60.0
|
||
|
||
/// Fastest detectable tempo (BPM). Sets the shortest autocorrelation lag.
|
||
public let beatTrackMaxBPM: Double = 200.0
|
||
|
||
/// Centre of the tempo-preference weight and the pre-lock default tempo (BPM).
|
||
/// The weight biases the autocorrelation peak toward perceptually likely tempi,
|
||
/// resolving the octave ambiguity between a pulse and its harmonics.
|
||
public let beatTrackPreferredBPM: Double = 120.0
|
||
|
||
/// Width (in octaves) of the log-Gaussian tempo-preference weight.
|
||
public let beatTrackPreferenceWidthOctaves: Double = 0.9
|
||
|
||
/// One-pole smoothing factor applied to the beat period across successive
|
||
/// analyses. `0` = instant; `1` = frozen. Higher values steady the tempo output.
|
||
public let beatTrackTempoSmoothing: Double = 0.9
|
||
|
||
/// Re-run tempo induction (autocorrelation + phase realignment) every N emitted
|
||
/// ODF features. A small power of two amortises the analysis cost.
|
||
public let beatTrackAnalysisHopFeatures: Int = 8
|
||
|
||
/// Time constant (seconds) of the running-mean subtraction used to whiten the
|
||
/// ODF before autocorrelation. Removes DC offset / slow drift so a non-zero
|
||
/// baseline does not bias the periodicity estimate.
|
||
public let beatTrackMeanAdaptSeconds: Double = 1.0
|
||
|
||
/// Minimum normalised autocorrelation peak required to update the tempo estimate.
|
||
/// Below this the input is treated as having no clear pulse (silence / noise);
|
||
/// the previous period is held and the phase free-runs.
|
||
public let beatTrackMinPeakStrength: Double = 1.0e-9
|
||
|
||
/// Seconds per minute — used in the BPM ↔ feature-lag conversions.
|
||
internal let beatTrackSecondsPerMinute: Double = 60.0
|
||
|
||
/// Eighth-note subdivisions per beat (row 1 fires at this multiple of the beat rate).
|
||
internal let beatTrackEighthsPerBeat: Double = 2.0
|
||
|
||
/// Sixteenth-note subdivisions per beat (row 2 fires at this multiple of the beat rate).
|
||
internal let beatTrackSixteenthsPerBeat: Double = 4.0
|
||
|
||
/// Coefficient `½` in the log-Gaussian preference exponent `−½ · z²`.
|
||
internal let beatTrackGaussianExponentCoeff: Double = 0.5
|
||
|
||
/// Coefficient `½` in the parabolic peak-interpolation offset `½ · (yₘ − yₚ) / denom`.
|
||
internal let beatTrackParabolicCoeff: Double = 0.5
|
||
|
||
/// Minimum parabola curvature magnitude for peak refinement; below this the
|
||
/// interpolation is degenerate and the integer lag is returned unchanged.
|
||
internal let beatTrackParabolicEpsilon: Double = 1.0e-12
|