feat(audio): microphone-only sync delay, on by default at 175 ms

The webcam's frames reach the compositor later than the mic's samples reach the audio
pipe, so the voice runs ahead of the lips. The existing lip-sync offset could not fix
that: it delays the WHOLE mix, which drags desktop and alert audio — neither of which
has a camera to follow — out of sync in the process.

So the delay line learned channels (mono for a source, stereo for the mix, offset always
in milliseconds so the frame count is what stays constant), and FillAndMix runs a second
line over micChunk before the mix. Slider-side: presets off / 150 / 200 in the LIP-SYNC
bay under the whole-mix control, persisted as Audio.MicSyncOffsetMs.

Default 175 ms is the creator's own number for a C920 pipeline (they asked for
+150..+200). It is a setting, not a constant, because the right figure belongs to the
camera and the machine — a faster camera wants less, no camera wants none.

Both delay lines are reconfigured every tick, so the 2026-09-12 trap still applies and is
documented at both sites: a Configure that reallocates discards the buffered voice every
10 ms.

673 pass, 0 warnings. Ear check pending: this is an audio change.
This commit is contained in:
2026-11-12 11:15:50 -08:00
parent f6fdab3bf2
commit 7f89534309
9 changed files with 135 additions and 13 deletions
+24 -1
View File
@@ -34,6 +34,9 @@ public sealed class AudioMixer : IDisposable
private readonly TimeSpan _mixInterval;
private readonly Func<int>? _syncOffsetMs;
private readonly AudioSyncDelay _syncDelay;
private readonly AudioSyncDelay _micSyncDelay;
private readonly Func<int>? _micSyncOffsetMs;
private float[]? _delayedMic;
private int _advanceSamplesRemaining;
private bool _started;
@@ -97,7 +100,8 @@ public sealed class AudioMixer : IDisposable
Func<double>? micGain = null,
Func<double>? loopbackGain = null,
Func<int>? syncOffsetMs = null,
TimeSpan? mixInterval = null)
TimeSpan? mixInterval = null,
Func<int>? micSyncOffsetMs = null)
{
_mic = mic;
_loopback = loopback;
@@ -108,6 +112,11 @@ public sealed class AudioMixer : IDisposable
_loopbackGain = loopbackGain;
_syncOffsetMs = syncOffsetMs;
_syncDelay = new AudioSyncDelay(OutputSampleRate);
// The microphone alone, mono: a camera's frames reach the compositor later than the mic's
// samples reach the pipe, so the voice has to wait for the picture. Loopback and alert audio
// are not tied to a camera frame and must not move with it.
_micSyncOffsetMs = micSyncOffsetMs;
_micSyncDelay = new AudioSyncDelay(OutputSampleRate, channels: 1);
_mixInterval = mixInterval ?? DefaultMixInterval;
var seconds = _mixInterval.TotalSeconds;
@@ -506,6 +515,20 @@ public sealed class AudioMixer : IDisposable
Array.Clear(alertChunk, alertCount, alertChunk.Length - alertCount);
}
// Mic-only delay: applied to the source, before the mix, so the desktop and alert audio keep
// their timing. Reconfigured every tick like the mix line above (same 2026-09-12 trap: a
// reallocating Configure would discard the buffered voice every 10 ms).
var micDelayMs = _micSyncOffsetMs?.Invoke() ?? 0;
if (micDelayMs > 0)
{
_micSyncDelay.Configure(micDelayMs);
var delayedMic = _delayedMic;
if (delayedMic is null || delayedMic.Length < micChunk.Length)
delayedMic = _delayedMic = new float[micChunk.Length];
_micSyncDelay.Process(micChunk, delayedMic);
Array.Copy(delayedMic, micChunk, micChunk.Length);
}
var duck = _ducker.Update(micRms);
var micGain = (float)(_micGain?.Invoke() ?? 1.0);
var loopGain = (float)(_loopbackGain?.Invoke() ?? 1.0) * duck;
+15 -10
View File
@@ -3,8 +3,8 @@ using System;
namespace ytLive.Services.Audio;
/// <summary>
/// TASK 22: a fixed end-to-end audio delay applied to the interleaved stereo
/// mix in <see cref="AudioMixer"/>. This is OBS's documented fix for the
/// TASK 22: a fixed end-to-end audio delay applied either to the interleaved
/// stereo mix or to one mono source in <see cref="AudioMixer"/>. This is OBS's documented fix for the
/// "audio is ahead of video" lip-sync case: delay the audio by a few
/// milliseconds so it lands on the video. It is a pure delay line — input in,
/// the same audio N ms later out — so it is trivially testable and carries no
@@ -20,14 +20,19 @@ namespace ytLive.Services.Audio;
public sealed class AudioSyncDelay
{
private readonly int _sampleRate;
private readonly int _channels;
private float[] _buffer = Array.Empty<float>();
private int _delaySamples;
private int _writePos;
public AudioSyncDelay(int sampleRate)
/// <param name="channels">Samples per frame in the chunks passed to <see cref="Process"/>: 2 for
/// the interleaved stereo mix, 1 for a mono source such as the microphone. The offset is always
/// specified in milliseconds, so the frame count — not the sample count — is what stays constant.</param>
public AudioSyncDelay(int sampleRate, int channels = 2)
{
_sampleRate = Math.Max(1, sampleRate);
_channels = Math.Clamp(channels, 1, 2);
Configure(0);
}
@@ -45,13 +50,13 @@ public sealed class AudioSyncDelay
var delaySamples = Frames(delayMs);
if (delaySamples == _delaySamples && _buffer.Length > 0) return;
_delaySamples = delaySamples;
_buffer = new float[(_delaySamples + _sampleRate) * 2 + 1024];
_buffer = new float[(_delaySamples + _sampleRate) * _channels + 1024];
_writePos = 0;
}
/// <summary>Delays an interleaved stereo chunk by the configured offset.
/// The returned length equals <paramref name="input"/>'s; the head is
/// silence until the line fills.</summary>
/// <summary>Delays one interleaved chunk (stereo or mono, per the constructor) by the configured
/// offset. The returned length equals <paramref name="input"/>'s; the head is silence until the
/// line fills.</summary>
public void Process(ReadOnlySpan<float> input, Span<float> output)
{
if (output.Length < input.Length)
@@ -87,8 +92,8 @@ public sealed class AudioSyncDelay
private int Frames(int delayMs)
{
var clamped = Math.Clamp(delayMs, 0, 500);
// Interleaved stereo: each frame is two samples (L, R), so the delay
// in buffer positions is twice the frame count.
return (int)Math.Round(_sampleRate / 1000.0 * clamped) * 2;
// A frame is `_channels` samples, so the delay in buffer positions is
// the frame count times the channel count.
return (int)Math.Round(_sampleRate / 1000.0 * clamped) * _channels;
}
}