Inside the BDK speaker DSP
The speaker’s DSP is integer-only: Q31 samples, precomputed coefficient tables, no floating point and no heap. Follow one stereo frame from the render task to the DAC. You can drive this exact code on the virtual speaker.
Code shown from ESP32-A2DP-SINK-WITH-CODECS-UPDATED@4aaaefd. Every excerpt links to its exact lines.
1 · Init: pick a coefficient table, not trig
Every filter for 13 sample rates (8 kHz to 192 kHz) is precomputed into fixed_dsp_tables.inc, so init only finds the rate’s row and copies the crossover coefficients. It also sets a 20 ms fade length and decimates the analysis path to about 11 kHz so metering costs almost nothing at 96 or 192 kHz.
bool fixed_dsp_init(fixed_dsp_t *dsp, uint32_t rate, uint32_t low_pass_hz,
uint32_t high_pass_hz, uint32_t reference_frames) {
if (dsp == 0 || low_pass_hz != FDSP_TABLE_LP_HZ || high_pass_hz != FDSP_TABLE_HP_HZ) return false;
int index = -1;
for (unsigned i = 0; i < FDSP_RATE_COUNT; ++i) if (fdsp_rates[i] == rate) index = (int)i;
if (index < 0) return false;
uint8_t *bytes = (uint8_t *)dsp;
for (unsigned i = 0; i < sizeof(*dsp); ++i) bytes[i] = 0;
dsp->sample_rate = rate;
dsp->rate_index = index;
dsp->fade_frames = rate / 50u; /* 20 ms, independent of codec rate. */
if (dsp->fade_frames == 0) dsp->fade_frames = 1;
dsp->decimation = (uint16_t)((rate + 5512u) / 11025u);
if (dsp->decimation == 0) dsp->decimation = 1;
uint32_t effective_rate = rate / dsp->decimation;
uint32_t window = (reference_frames * effective_rate + 22050u) / 44100u;
dsp->window_frames = (uint16_t)(window < 64 ? 64 : window > 4096 ? 4096 : window);
for (unsigned i = 0; i < 13; ++i) dsp->analysis_coefficients[i] = fdsp_analysis[index][i];
for (unsigned band = 0; band < 2; ++band)
for (unsigned coefficient = 0; coefficient < 5; ++coefficient)
dsp->cross_coefficients[band][coefficient] = fdsp_cross[index][band][coefficient];
fixed_dsp_controls(dsp, 0, 0, 0, 127, FDSP_ANALYSIS, true);
return true;
}2 · Controls: targets that ramp over 20 ms
EQ gains index straight into the table (gain + 12, so ±12 phone steps). Nothing jumps: each coefficient set, the volume, bypass and flip get a target and walk there over 20 ms, which is why moving a slider never clicks. A branch that was fully silent has its history cleared before it fades back in.
void fixed_dsp_controls(fixed_dsp_t *dsp, int8_t bass, int8_t mid, int8_t treble,
uint8_t volume, uint8_t flags, bool immediate) {
static const int32_t identity[5] = {COEFF_ONE, 0, 0, 0, 0};
int8_t gains[3] = {clamp_gain(bass), clamp_gain(mid), clamp_gain(treble)};
if (volume > 127) volume = 127;
for (unsigned i = 0; i < 3; ++i) {
dsp->gains[i] = gains[i];
coefficient_target(&dsp->filters[i], fdsp_eq[dsp->rate_index][i][gains[i] + 12], dsp->fade_frames, immediate);
}
coefficient_target(&dsp->filters[3], fdsp_comp[dsp->rate_index][volume], dsp->fade_frames, immediate);
coefficient_target(&dsp->filters[4], flags & FDSP_BASS_BOOST ? fdsp_boost[dsp->rate_index] : identity,
dsp->fade_frames, immediate);
/* A fully inaudible branch is not processed. Clear only the branch being
* reactivated; the audible path and common EQ history stay intact. Its
* contribution is introduced through the existing 20 ms bypass crossfade.
* A reversal during a fade keeps both already-running histories. */
if (!dsp->bypass.remaining) {
if ((flags & FDSP_BYPASS) && dsp->bypass.value == 0) {
dsp->boost_history[0] = (fdsp_history_t){0};
dsp->boost_history[1] = (fdsp_history_t){0};
} else if (!(flags & FDSP_BYPASS) && dsp->bypass.value == UNITY_GAIN) {
dsp->cross_history[0] = (fdsp_history_t){0};
dsp->cross_history[1] = (fdsp_history_t){0};
dsp->boost_history[2] = (fdsp_history_t){0};
}
}
int32_t gain = (int32_t)((int64_t)volume * UNITY_GAIN / 127);
ramp_target(&dsp->volume, gain, dsp->fade_frames, immediate);
ramp_target(&dsp->bypass, flags & FDSP_BYPASS ? UNITY_GAIN : 0, dsp->fade_frames, immediate);
ramp_target(&dsp->flip, flags & FDSP_CHANNEL_FLIP ? UNITY_GAIN : 0, dsp->fade_frames, immediate);
if (!(flags & FDSP_ANALYSIS) && (dsp->flags & FDSP_ANALYSIS)) fixed_dsp_zero_analysis(dsp);
dsp->flags = flags;
dsp->requested_volume = volume;
}
static uint64_t DSP_BLOCK_HOT integer_sqrt(uint64_t value) {
uint64_t result = 0, bit = (uint64_t)1 << 62;3 · One frame through the chain
The analysis tap reads the input mono mix first. Samples drop 4 bits of headroom (Q31 → Q27), then pass four biquads: bass, mid, treble and volume-based bass compensation. For the 2-way speaker, the left channel is low-passed at 90 Hz (woofer, plus the bass-boost shelf) and the right high-passed at 500 Hz (tweeter), both with 1.41× make-up. Bypass and flip are blends, so switching crossfades instead of popping. Volume and the limiter come last.
bt_audio_sink/main/dsp/fixed_dsp.c:392–431 ↗void DSP_HOT fixed_dsp_process(fixed_dsp_t *dsp, int32_t *left, int32_t *right) {
bool full = dsp->bypass.value != 0 || dsp->bypass.remaining != 0;
bool split = dsp->bypass.value != UNITY_GAIN || dsp->bypass.remaining != 0;
FIXED_DSP_STAGE_MARK(0);
analysis(dsp, (int32_t)(((int64_t)*left + *right) / 2));
FIXED_DSP_STAGE_MARK(1);
for (unsigned i = 0; i < 5; ++i) coefficients_advance(&dsp->filters[i]);
ramp_advance(&dsp->volume); ramp_advance(&dsp->bypass); ramp_advance(&dsp->flip);
int32_t l = *left >> 4, r = *right >> 4;
FIXED_DSP_STAGE_MARK(2);
for (unsigned i = 0; i < 4; ++i) {
l = filter(dsp->filters[i].value, &dsp->channel_history[i][0], l);
r = filter(dsp->filters[i].value, &dsp->channel_history[i][1], r);
}
FIXED_DSP_STAGE_MARK(3);
int32_t lp = 0, hp = 0;
if (split) {
lp = filter(dsp->cross_coefficients[0], &dsp->cross_history[0], l);
hp = filter(dsp->cross_coefficients[1], &dsp->cross_history[1], r);
lp = filter(dsp->filters[4].value, &dsp->boost_history[2], lp);
lp = clamp32(((int64_t)lp * 189246996) >> 27); /* 1.41 crossover makeup */
hp = clamp32(((int64_t)hp * 189246996) >> 27);
}
if (full) {
l = filter(dsp->filters[4].value, &dsp->boost_history[0], l);
r = filter(dsp->filters[4].value, &dsp->boost_history[1], r);
}
FIXED_DSP_STAGE_MARK(4);
if (split) {
int32_t split_l = blend(lp, hp, dsp->flip.value);
int32_t split_r = blend(hp, lp, dsp->flip.value);
l = full ? blend(split_l, l, dsp->bypass.value) : split_l;
r = full ? blend(split_r, r, dsp->bypass.value) : split_r;
}
if (dsp->volume.value != UNITY_GAIN) {
l = clamp32(((int64_t)l * dsp->volume.value) >> 31);
r = clamp32(((int64_t)r * dsp->volume.value) >> 31);
}
*left = clamp32((int64_t)limit(dsp, l) * 16);
*right = clamp32((int64_t)limit(dsp, r) * 16);4 · A soft limiter in integer math
Below the threshold the sample passes untouched. Above it, the excess is compressed asymptotically toward full scale, so loud EQ boosts bend instead of hard-clipping. It also counts limited and over-full-scale samples, which the virtual speaker shows as “limiter engaged”.
bt_audio_sink/main/dsp/fixed_dsp.c:377–392 ↗static inline int32_t DSP_BLOCK_HOT limit(fixed_dsp_t *dsp, int32_t value) {
uint32_t magnitude = (uint32_t)(value < 0 ? -(int64_t)value : value);
if (magnitude > dsp->peak_q27) dsp->peak_q27 = magnitude;
if (magnitude <= CLIP_THRESHOLD) return value;
++dsp->limited;
if (magnitude >= AUDIO_ONE) ++dsp->over_full_scale;
uint32_t excess = magnitude - CLIP_THRESHOLD;
#ifdef FIXED_DSP_FORCE_WIDE_LIMITER
int32_t output = CLIP_THRESHOLD + (int32_t)((uint64_t)CLIP_RANGE * excess / (CLIP_RANGE + excess));
#else
int32_t output = AUDIO_ONE - (int32_t)limiter_tail_ceil(CLIP_RANGE + excess);
#endif
return value < 0 ? -output : output;
}
void DSP_HOT fixed_dsp_process(fixed_dsp_t *dsp, int32_t *left, int32_t *right) {5 · The analysis tap: Goertzel and envelopes
The input is averaged down to about 11 kHz, then three resonators run in parallel: Goertzel bins for beat detection and one-pole envelope meters for the level display. Every window the Goertzel energies are normalised into levels. These feed the BLE meters, the beat detector and all 22 LED effects.
bt_audio_sink/main/dsp/fixed_dsp.c:297–330 ↗static void DSP_BLOCK_HOT analysis(fixed_dsp_t *dsp, int32_t mono) {
if (!(dsp->flags & FDSP_ANALYSIS)) return;
dsp->analysis_sum += mono;
if (++dsp->analysis_count < dsp->decimation) return;
int32_t sample = analysis_average(dsp->analysis_sum,dsp->decimation);
dsp->analysis_sum = 0; dsp->analysis_count = 0;
int32_t q15 = sample >> 16;
for (unsigned i = 0; i < 3; ++i) {
int64_t s0 = q15 + (((int64_t)dsp->analysis_coefficients[i] * dsp->resonators[i][0]) >> 29) - dsp->resonators[i][1];
const int64_t bound = (int64_t)1 << 31;
if (s0 > bound) s0 = bound;
if (s0 < -bound) s0 = -bound;
dsp->resonators[i][1] = dsp->resonators[i][0]; dsp->resonators[i][0] = s0;
int64_t delta = (int64_t)sample - dsp->meter_states[i];
dsp->meter_states[i] = clamp32((int64_t)dsp->meter_states[i] + (delta * dsp->analysis_coefficients[9 + i] >> 31));
uint32_t amplitude = (uint32_t)(dsp->meter_states[i] < 0 ? -(int64_t)dsp->meter_states[i] : dsp->meter_states[i]);
if (amplitude >= dsp->envelopes[i]) dsp->envelopes[i] = amplitude;
else dsp->envelopes[i] = amplitude + (uint32_t)(((uint64_t)(dsp->envelopes[i] - amplitude) * (uint32_t)dsp->analysis_coefficients[12]) >> 31);
dsp->meter_levels[i] = dsp->envelopes[i];
}
if (++dsp->window_count < dsp->window_frames) return;
for (unsigned i = 0; i < 3; ++i) {
int64_t real = dsp->resonators[i][0] - ((dsp->resonators[i][1] * dsp->analysis_coefficients[3 + i]) >> 30);
int64_t imaginary = (dsp->resonators[i][1] * dsp->analysis_coefficients[6 + i]) >> 30;
unsigned shift = 0;
while (real > (1 << 30) || real < -(1 << 30) || imaginary > (1 << 30) || imaginary < -(1 << 30)) {
real >>= 1; imaginary >>= 1; ++shift;
}
uint64_t power = (uint64_t)(real * real) + (uint64_t)(imaginary * imaginary);
uint64_t magnitude = integer_sqrt(power) << shift;
uint64_t normalized = (magnitude << 17) / dsp->window_frames;
dsp->goertzel_levels[i] = normalized > 2147483648u ? 2147483648u : (uint32_t)normalized;
dsp->resonators[i][0] = dsp->resonators[i][1] = 0;
}6 · Lock-free controls from BLE to the render task
BLE commands and the encoder knob write gains and options into atomics guarded by a generation counter. The render task, which owns the DSP, takes a consistent snapshot only when the generation is even and unchanged, then calls fixed_dsp_controls. The first snapshot of a fresh stream is applied immediately (nothing to crossfade); later ones ramp.
static void controls(dbt_audio_t *a){
for(unsigned i=0;i<3;++i){
uint32_t generation=atomic_load_explicit(&a->controls_generation,memory_order_seq_cst);
if((generation&1u) || generation==a->applied_generation)return;
uint32_t gains=atomic_load_explicit(&a->controls_gains,memory_order_seq_cst);
uint32_t options=atomic_load_explicit(&a->controls_options,memory_order_seq_cst);
if(generation!=atomic_load_explicit(&a->controls_generation,memory_order_seq_cst))continue;
/* A fresh stream has no audible history to crossfade. Restore its
* first policy before DSP begins; live changes keep the normal ramps.
* The persistent flag also remains correct across counter wrap. */
fixed_dsp_controls(&a->dsp,(int8_t)gains,(int8_t)(gains>>8),(int8_t)(gains>>16),(uint8_t)options,(uint8_t)(options>>8),!a->dsp_started);
a->applied_generation=generation;return;
}
}