Skip to content

Add speakerguard-xlim, a look-ahead excursion and peak limiter - #9

Closed
RyanTheTide wants to merge 1 commit into
mainfrom
phase3/xlim-20261024
Closed

RyanTheTide wants to merge 1 commit into
mainfrom
phase3/xlim-20261024

Conversation

@RyanTheTide

Copy link
Copy Markdown
Contributor

Adds speakerguard-xlim, a look-ahead limiter for the MacBook Neo (J700) speaker graph, in filter/. It is a LADSPA plugin for PipeWire's filter-chain. The daemon is unchanged.

What it does

The plugin predicts the cone displacement of every sample from the driver's lumped model and lowers the gain just enough, ahead of time, to keep the cone under 0.9 of Xmax and every sample under full scale.

  • The model: a closed box, no coil inductance, bilinear-mapped and prewarped at resonance.
  • The driver values: Bl 0.74 T m, Mms 0.16 g, Kms 1720 N/m, Rms 0.14 kg/s, Re 4.3 ohm, resonance at 522 Hz.
  • Xmax is 0.565 mm, and the amplifier's full scale is 7.30 and 7.33 V.
  • It only ever lowers the gain, and the two channels share one gain.
  • Below its limits the output is the input delayed, bit for bit.
  • A velocity limit is built in, off by default.

Timing

  • The attack is a 3.333 ms ramp. It ends 4 ms before the frame that needs it goes out, which is the cone's memory: its ringing decays by e in 1.2 ms.
  • Without that hold the cone passed Xmax by up to 13 % on random programs, because a gain falling while the cone still rings is not a gain applied to it.
  • The prediction is held at 0.89 of Xmax, so the gain changes left inside the memory keep the true cone under 0.9.
  • The release is 200 ms.
  • The latency is 351 frames at 48 kHz (7.3 ms), inside a 352-frame budget. The plugin reports it on its latency port.

Real-time safety

  • The audio thread never allocates. The memory is taken at instantiation, and a control change only reconfigures.
  • An invalid control keeps the last valid set. Before any valid set the output is silence.
  • A non-finite sample is taken as silence.

Tests

make filter-test passes on x86_64 and aarch64 Linux.

  • test_random checks the limiter against an independent model of the cone: its continuous equation of motion, integrated by Runge-Kutta at eight steps a sample.
  • Its inputs are random programs plus 172 worst-case shapes: steps, impulse trains around resonance, square waves at it and its odd fractions, and quiet-to-full-scale jumps.
  • Over 87000 random programs (seeds 1-2000, 5000-19999, 100000-119999, 300000-349999) and the shapes, the worst cone was 0.8956 of Xmax.
  • The test asserts 0.9 for every case. It fails with the prediction held at 0.9 instead of 0.89, or with a 2 ms hold.
  • test_xlim covers:
    • the model's DC gain and resonance;
    • transparency;
    • loud tones and bursts;
    • the impulse delay against the reported latency and the budget;
    • non-finite input;
    • ten minutes without drift.
  • test_ladspa loads the plugin as a host would and covers:
    • limiting;
    • the latency port;
    • control changes with no allocation in run() (glibc);
    • invalid controls;
    • silence before a valid set.

Using it

make install-filter installs $(LIBDIR)/ladspa/speakerguard-xlim.so. The J700 graph that uses it is a separate, off-by-default variant in aurora-silicon/asahi-audio (fleet2/j700-xlim).

Draft: nothing has played through it yet. The first listen on the Neo starts at -40 dBFS for transparency, and anything louder waits for an attended session.

Assisted-by: LLM

🤖 Generated with Claude Code

This adds a LADSPA plugin for the J700 speaker graph in PipeWire's
filter-chain.  It predicts the cone displacement of every sample from
the driver's lumped model.  The model is a closed box with no coil
inductance:

    X/V = (Bl/Re) / (Mms s^2 + (Rms + Bl^2/Re) s + Kms)

It is bilinear-mapped and prewarped at resonance.  The plugin lowers the
gain just enough, ahead of time, that the cone stays under 0.9 of Xmax
and no sample passes full scale.  A velocity limit is there too, off by
default.

The gain is a sliding minimum of each frame's requirement, ramped by a
running mean over 3.333 ms.  The ramp ends 4 ms before the frame that
needs it goes out.  Those 4 ms are the cone's memory: its ringing at
resonance decays by e in 1.2 ms, and a gain falling while the cone still
rings is not a gain applied to it.  Without the hold the cone passed
Xmax by up to 13 % on random programs.  The model's prediction is held
at 0.89 of Xmax, so the gain changes left inside the memory keep the
true cone under 0.9.  The release is 200 ms.  The two channels share
one gain.

The latency is 351 frames at 48 kHz (7.3 ms), reported on the latency
port.  The defaults are the MacBook Neo's driver values: Bl 0.74 T m,
Mms 0.16 g, Kms 1720 N/m, Rms 0.14 kg/s, Re 4.3 ohm, 7.30/7.33 V at full
scale, Xmax 0.565 mm.

The audio thread never allocates.  The memory for the longest window
and hold is taken at instantiation, and a control change only
reconfigures.  An invalid control keeps the last valid set; before any
valid set the output is silence.  A sample that is not finite is taken
as silence.

Tests (make filter-test, on x86_64 and aarch64 Linux):
- the model's DC gain and resonance;
- transparency: below the limits the output is the delayed input, bit
  for bit;
- the limits on loud tones and bursts;
- an impulse delayed by exactly the reported latency, within a 352-frame
  budget;
- non-finite input;
- no drift after ten minutes of limiting;
- no allocation in run() (glibc);
- invalid controls.
test_random drives the limiter against an independent model of the
cone: its continuous equation of motion, integrated by Runge-Kutta at
eight steps a sample.  The inputs are random programs (tones, bursts,
chirps, square waves, steps, impulses, noise, random block sizes) plus
172 worst-case shapes: steps, impulse trains around resonance, square
waves at it and its odd fractions, and quiet-to-full-scale jumps.  Over
87000 random programs and the shapes, the worst cone was 0.8956 of
Xmax.  With the prediction held at 0.9 instead, or with a 2 ms hold,
the test fails.

make filter and make install-filter build and install the plugin apart
from the daemon.  The daemon is unchanged.

Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
Assisted-by: LLM
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Superseded by #27: this PR's head is already contained in the 0.1.13 release candidate branch (fu-speakerguardd-rc), so it lands with that PR. Closing to keep one release PR (fewer, bigger PRs).

@RyanTheTide RyanTheTide closed this Nov 4, 2026
@RyanTheTide
RyanTheTide deleted the phase3/xlim-20261024 branch November 6, 2026 03:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant