Files
LlamaCasty/ytLive.Tests/AudioSyncDelayTests.cs
T
gramps 7f89534309 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.
2026-11-12 11:15:50 -08:00

130 lines
4.8 KiB
C#

using Xunit;
using ytLive.Services.Audio;
namespace ytLive.Tests;
/// <summary>
/// TASK 22: the global audio-sync delay line. It is a pure fixed positive
/// delay on the interleaved stereo mix — the OBS fix for audio running ahead
/// of video. Positive-only: the line clamps negatives to 0; the NEGATIVE
/// (advance) side — audio running behind video, the OBS "eat the buffer head"
/// case — is covered by <see cref="AudioPipelineTests.StartLive_NegativeOffset_AdvancesAudio_ByDroppingTheStreamHead"/>.
/// </summary>
public class AudioSyncDelayTests
{
[Fact]
public void ZeroDelay_PassesThroughUnchanged()
{
var delay = new AudioSyncDelay(48000);
delay.Configure(0);
var input = new float[16];
for (var i = 0; i < input.Length; i++)
input[i] = i - 8;
var output = new float[16];
delay.Process(input, output);
Assert.Equal(input, output);
}
[Fact]
public void ConfigureClampsNegativeToZero_AndBeyond500msTo500ms()
{
var delay = new AudioSyncDelay(48000);
delay.Configure(-50);
Assert.Equal(0, MarkerShift(delay, 1000));
// 500 ms at 48 kHz = 24000 frames = 48000 interleaved samples.
delay.Configure(6000);
Assert.Equal(48000, MarkerShift(delay, 48000));
}
[Fact]
public void TenMsDelay_EmitsTheSignal960SamplesLater()
{
// 10 ms at 48 kHz = 480 frames; interleaved stereo = 960 samples.
var delay = new AudioSyncDelay(48000);
Assert.Equal(960, MarkerShift(delay, 480 * 2, configureMs: 10));
}
/// <summary>2026-09-12 total-silence regression: <see cref="AudioMixer.FillAndMix"/>
/// calls <c>Configure(_syncOffsetMs?.Invoke() ?? 0)</c> on EVERY ~10ms tick (it
/// re-reads the live UI setting each mix, not just on change) — the old
/// `Configure` unconditionally reallocated and zeroed the delay line and reset
/// `_writePos` every single call, discarding the buffered marker before the
/// delay offset could ever read it back. Any persisted non-zero
/// `Audio.SyncOffsetMs` (this repo's default is 0, but TASK 22 testing/a user
/// slider left 500 saved) turned the WHOLE recording into digital silence:
/// mic/loop level meters (sampled upstream of this stage) stayed honest while
/// the delay line fed the encoder pipe pure zeros forever. This test
/// reconfigures with the SAME delay before every Process call — exactly the
/// mixer's calling pattern — and the marker must still survive.</summary>
[Fact]
public void RepeatedConfigureWithSameDelay_EveryTick_StillDelivers_TheMarker()
{
const int chunkLen = 960; // 10ms of 48kHz interleaved stereo
var delay = new AudioSyncDelay(48000);
var chunk = new float[chunkLen];
var output = new float[chunkLen];
chunk[0] = 1f; // sole non-zero: left of frame 0
var foundAt = -1;
var total = 0;
for (var c = 0; c < 6 && foundAt < 0; c++)
{
delay.Configure(10); // the mixer calls this every tick, not just on change
delay.Process(chunk, output);
for (var i = 0; i < chunkLen; i++)
{
if (output[i] != 0f) { foundAt = total + i; break; }
}
total += chunkLen;
chunk[0] = 0f; // only the first tick carries the marker
}
Assert.Equal(960, foundAt); // same 10ms-delay shift as the single-Configure test
}
[Fact]
public void AMonoSourceIsDelayedInFrames_NotInDoubledSamples()
{
// 48 kHz, 175 ms = 8400 frames. For the microphone (mono) that is 8400 samples…
var mic = new AudioSyncDelay(48000, channels: 1);
Assert.Equal(8400, MarkerShift(mic, 9600, configureMs: 175));
// …while the same class in its stereo role, guarding the whole mix, counts frames in pairs:
// the same 175 ms must still be 175 ms, not 87.5.
var mix = new AudioSyncDelay(48000);
Assert.Equal(16800, MarkerShift(mix, 19200, configureMs: 175));
}
private static int MarkerShift(AudioSyncDelay delay, int chunkLen, int configureMs = -1)
{
if (configureMs >= 0)
delay.Configure(configureMs);
var chunk = new float[chunkLen];
var output = new float[chunkLen];
chunk[0] = 1f; // sole non-zero: left of frame 0
var foundAt = -1;
var total = 0;
for (var c = 0; c < 4 && foundAt < 0; c++)
{
delay.Process(chunk, output);
for (var i = 0; i < chunkLen; i++)
{
if (output[i] != 0f)
{
foundAt = total + i;
break;
}
}
total += chunkLen;
}
Assert.True(foundAt >= 0, "marker never appeared in the delayed output");
return foundAt;
}
}