Files
LlamaCasty/Services/Audio/MasterLimiter.cs
T
gramps 47f409f0e4 fix(audio): drive the mix from the clock — the audio track was 2.1% longer than the show
Measured on the creator's recording (ty-20261112-1118-423-0000-2.mp4), which drifts
out of sync over ten seconds:

  wall clock (pump start -> audio stop)   9.875 s
  video: 594 frames, uniform 1/60         9.888 s   correct
  audio: 485,376 samples @ 48 kHz        10.112 s   +2.1%, 224 ms long

So it was not VFR and not wall-clock PTS: the video is CFR and matches the clock (the
only PTS anomaly in the file is one 38 ms gap from two lost frames). The audio track is
what was too long, and a static offset cannot fix a rate error — which is why the opening
looked aligned (the 175 ms mic delay cancels the camera's constant pipeline latency) and
the end did not.

Cause: LiveLoopAsync wrote a fixed 480 frames per iteration and trusted that each
iteration was 10 ms. Under a raised timer resolution they land near 9.8 ms — 102 ticks
per second where 100 are expected, i.e. 102% of an audio second per wall second. FFmpeg
stamps audio by sample count, so every second of show pushed the sound ~2% later against
a correct picture. Video never had the bug because the pump paces off a stopwatch.

Fix: Services/Audio/AudioClock asks the clock how many frames are owed
(expected - produced, clamped to one 200 ms chunk) and the loop produces exactly that,
padding on underrun. A stalled loop pays its debt over the following ticks instead of
dumping one huge write. FillAndMix takes a frame count, and the master limiter/meter take
spans, so a partial chunk is processed without touching stale buffer tail.

674 tests (one log-rotation flake under full-suite load while the app itself is running
and writing startup.log; passes alone), 0 warnings. Ear check pending.
2026-11-12 11:28:28 -08:00

46 lines
1.5 KiB
C#

namespace ytLive.Services.Audio;
/// <summary>
/// Master output limiter — the last stage before the live mix reaches the
/// encoder pipe: a peak ceiling of -1 dBFS so mic + loopback (game + TRAX music)
/// can never sum past 0 dBFS and clip in the AAC encode. Instant attack per frame
/// (a hot frame is scaled exactly to the ceiling — no overshoot), smoothed release
/// toward unity so loud passages don't pump. Pure — unit-tested.
/// </summary>
public sealed class MasterLimiter
{
/// <summary>-1 dBFS: standard streaming headroom for encoder overshoot.</summary>
public const float Ceiling = 0.891f;
private const float Release = 0.005f;
private float _gain = 1f;
/// <summary>The current gain multiplier (≤ 1, so the limiter never boosts).</summary>
public float Gain => _gain;
/// <summary>Scales the frame by the current gain. Gain drops instantly to
/// <c>ceiling/peak</c> when the frame exceeds the ceiling and recovers
/// toward unity slowly.</summary>
public void Process(Span<float> samples)
{
var peak = 0f;
foreach (var s in samples)
{
var abs = MathF.Abs(s);
if (abs > peak)
peak = abs;
}
var target = peak > Ceiling ? Ceiling / peak : 1f;
if (target < _gain)
_gain = target;
else
_gain += (target - _gain) * Release;
for (var i = 0; i < samples.Length; i++)
samples[i] *= _gain;
}
public void Reset() => _gain = 1f;
}