#pragma once /* * Realtime beat tracker, plain C with no ESP-IDF dependencies so it can be * tested on a PC. Spectral-flux onsets -> autocorrelation tempo -> comb-filter * phase tracking that predicts beats from the last few bars -> bar position * (downbeats and 3/4 vs 4/4) from bass hits and harmonic changes per beat. */ #include #include #include #define BEAT_FFT_SIZE 512 // 32 ms window at 16 kHz #define BEAT_HOP 256 // 16 ms between onset frames #define BEAT_NV_LEN 512 // onset history (~8 s), power of two #define BEAT_MIN_BPM 60.0f #define BEAT_MAX_BPM 200.0f #define BEAT_AC_LEN (BEAT_NV_LEN / 2) // autocorrelation lags kept #define BEAT_BANDS 16 // log-spaced bands for harmonic change #define BEAT_METERS 2 // 3/4 and 4/4 #define BEAT_EV_ONSET 0x1 #define BEAT_EV_BEAT 0x2 #define BEAT_EV_DOWNBEAT 0x4 // with BEAT_EV_BEAT, when the bar is sure typedef struct { // Settings (from beat_dsp_init) float frame_rate; // onset frames per second float gate_rms; // frames quieter than this are treated as silence int min_lag, max_lag; // beat period search range, frames int lead_frames; // emit predicted beats this early // Onset detection float in[BEAT_FFT_SIZE]; size_t in_fill; float re[BEAT_FFT_SIZE], im[BEAT_FFT_SIZE]; // FFT work, reused as scratch float prev_log[BEAT_FFT_SIZE / 2 + 1]; float prev_bass; float flux_mean, bass_mean, nv_mean; float nv[BEAT_NV_LEN]; // novelty (onset strength) ring, indexed by frame float tempo_nv[BEAT_NV_LEN]; // log flux only, for the tempo estimate float bass_nv[BEAT_NV_LEN]; // bass rise only, for downbeats uint32_t t; // frames processed uint32_t last_active, last_onset; // Tempo float ac[BEAT_AC_LEN]; float period; // frames per beat, 0 until found float confidence; // 0..1 periodicity of the onset envelope float pending_period; int pending_count; // Beat phase float salience; // on-beat vs. average onset energy, 1 = no beat uint32_t salience_n; int prev_phase; uint32_t last_beat; bool locked; // Bar position. Grid beats are numbered whether or not they are emitted; // each one is analysed a couple of frames after it lands. uint32_t grid_n; // grid beats so far uint32_t pending_frame; // frame to analyse the latest beat at uint32_t pending_n; // its grid number bool pending; float band_now[BEAT_BANDS]; // log band energy of the current frame float band_acc[BEAT_BANDS]; // ...summed over the start of the latest beat float prev_span[BEAT_BANDS]; // ...averaged over the start of the one before int band_frames; uint32_t span_n; // grid number of the beat band_acc belongs to float kick_mean, change_mean; float bar_score[BEAT_METERS][4]; // cue strength per meter and position int down_pos[BEAT_METERS]; // grid_n % meter of the downbeat int meter; // 3 or 4 float bar_contrast; // how clearly one position stands out int bar_beat; // 1..meter of the last grid beat (best guess), 0 early on bool bar_sure; // bar_beat is reliable; downbeats only go out then uint32_t tap_hold; // grid beats left that a tap holds the bar for } beat_dsp_t; // lead_s fires predicted beats early to cover capture and output latency void beat_dsp_init(beat_dsp_t *d, float sample_rate, float gate_rms, float lead_s); // Feed mono samples scaled to +/-1. Calls cb once per frame that produced // events (BEAT_EV_* bits). Beats are predicted, so they arrive lead_s ahead // of the frame they belong to; onsets need one frame of lookahead. typedef void (*beat_dsp_cb_t)(void *ctx, int events, const beat_dsp_t *d); void beat_dsp_feed(beat_dsp_t *d, const float *x, size_t n, beat_dsp_cb_t cb, void *ctx); // The user tapped beat 1 frames_ago frames back: make the nearest grid beat // beat 1 and hold that for a while before the audio can move it again void beat_dsp_tap(beat_dsp_t *d, float frames_ago); static inline float beat_dsp_bpm(const beat_dsp_t *d) { return d->period > 0.0f ? 60.0f * d->frame_rate / d->period : 0.0f; }