From 8237102a4d23a175c6cd6b77d328f5bc17b3bd14 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 22 May 2026 19:33:36 +0100 Subject: [PATCH] Clamp passive sync confidence --- docs/audio-field.md | 5 ++- src/Mimir.BufferSmoke/Program.cs | 23 ++++++++++-- .../MimirAudioSynchronizationState.cs | 5 +++ ...mirPassiveAudioSynchronizationEstimator.cs | 37 +++++++++++++++++-- .../MimirSynchronizationHub.cs | 32 ++++++++++++++-- state/evidence.jsonl | 1 + state/map.yaml | 4 +- 7 files changed, 95 insertions(+), 12 deletions(-) diff --git a/docs/audio-field.md b/docs/audio-field.md index d68e783..16c5f10 100644 --- a/docs/audio-field.md +++ b/docs/audio-field.md @@ -66,7 +66,10 @@ The passive estimator is the first real program-audio path, not the final DSP actuator. It removes DC, pre-emphasizes the window, applies a Hann taper, runs a PHAT-weighted cross spectrum, and reports the strongest loopback-to-mic lag inside a bounded window. Positive delay still means the candidate mic is late -relative to loopback. +relative to loopback. Negative lags are treated as contradictory passive +evidence and carry zero confidence. A single passive window is also capped below +certainty; full confidence belongs to repeated coherent state over time, not +one attractive correlation peak. ## Next Cut diff --git a/src/Mimir.BufferSmoke/Program.cs b/src/Mimir.BufferSmoke/Program.cs index eea14de..48ed7b1 100644 --- a/src/Mimir.BufferSmoke/Program.cs +++ b/src/Mimir.BufferSmoke/Program.cs @@ -169,12 +169,20 @@ static int RunPassiveSyncSelfTest() const int sampleCount = 48_000; var reference = new float[sampleCount]; var candidate = new float[sampleCount]; + var random = new Random(1729); + var noise = new double[sampleCount]; + for (var index = 0; index < sampleCount; index++) + { + noise[index] = random.NextDouble() * 2.0 - 1.0; + } + for (var index = 0; index < sampleCount; index++) { reference[index] = (float)( 0.45 * Math.Sin(2.0 * Math.PI * 611.0 * index / sampleRate) + 0.28 * Math.Sin(2.0 * Math.PI * 1471.0 * index / sampleRate) + - 0.12 * Math.Sin(2.0 * Math.PI * 3253.0 * index / sampleRate)); + 0.12 * Math.Sin(2.0 * Math.PI * 3253.0 * index / sampleRate) + + 0.08 * noise[index]); var source = index - delaySamples; candidate[index] = source >= 0 ? reference[source] : 0.0f; } @@ -184,13 +192,22 @@ static int RunPassiveSyncSelfTest() var error = estimate.DelaySamples - delaySamples; Console.WriteLine( $"passive-sync-self-test delaySamples={estimate.DelaySamples:0.000} expected={delaySamples} " + - $"error={error:0.000} confidence={estimate.Confidence:0.000} peak={estimate.Peak:0.000000} floor={estimate.NoiseFloor:0.000000} status={estimate.Status}"); - if (Math.Abs(error) > 1.0 || estimate.Confidence < 0.08) + $"error={error:0.000} confidence={estimate.Confidence:0.000} peak={estimate.Peak:0.000000} secondPeak={estimate.SecondPeak:0.000000} floor={estimate.NoiseFloor:0.000000} status={estimate.Status}"); + if (Math.Abs(error) > 1.0 || estimate.Confidence < 0.08 || estimate.Confidence >= 1.0) { Console.Error.WriteLine("passive sync self-test failed: delayed program signal did not produce a confident passive estimate"); return 1; } + var impossible = estimator.Estimate(candidate, reference, sampleRate); + Console.WriteLine( + $"passive-sync-negative-test delaySamples={impossible.DelaySamples:0.000} confidence={impossible.Confidence:0.000} status={impossible.Status}"); + if (impossible.DelaySamples < 0.0 && impossible.Confidence > 0.0) + { + Console.Error.WriteLine("passive sync self-test failed: negative lag kept confidence"); + return 1; + } + return 0; } diff --git a/src/Mimir.Runtime/Synchronization/MimirAudioSynchronizationState.cs b/src/Mimir.Runtime/Synchronization/MimirAudioSynchronizationState.cs index bc96321..819d464 100644 --- a/src/Mimir.Runtime/Synchronization/MimirAudioSynchronizationState.cs +++ b/src/Mimir.Runtime/Synchronization/MimirAudioSynchronizationState.cs @@ -54,6 +54,11 @@ public sealed class MimirAudioSynchronizationStateTracker } } + public void Remove(string sourceId) + { + states.Remove(sourceId); + } + private static MimirAudioSynchronizationState NewState( MimirAudioSynchronizationReport report, double smoothedDelaySamples, diff --git a/src/Mimir.Runtime/Synchronization/MimirPassiveAudioSynchronizationEstimator.cs b/src/Mimir.Runtime/Synchronization/MimirPassiveAudioSynchronizationEstimator.cs index accbb9f..f56201f 100644 --- a/src/Mimir.Runtime/Synchronization/MimirPassiveAudioSynchronizationEstimator.cs +++ b/src/Mimir.Runtime/Synchronization/MimirPassiveAudioSynchronizationEstimator.cs @@ -7,12 +7,14 @@ public sealed record MimirPassiveAudioSynchronizationEstimate( double Confidence, double Peak, double NoiseFloor, + double SecondPeak, int ComparedSamples, string Status); public sealed class MimirPassiveAudioSynchronizationEstimator { private const int TargetWindowSamples = 32_768; + private const double MaxSingleWindowConfidence = 0.85; private const double Preemphasis = 0.97; public MimirPassiveAudioSynchronizationEstimate Estimate( @@ -23,7 +25,7 @@ public sealed class MimirPassiveAudioSynchronizationEstimator var compared = Math.Min(reference.Length, candidate.Length); if (compared < 4096 || sampleRate <= 0) { - return new MimirPassiveAudioSynchronizationEstimate(0.0, 0.0, 0.0, 0.0, compared, "passive-insufficient-window"); + return new MimirPassiveAudioSynchronizationEstimate(0.0, 0.0, 0.0, 0.0, 0.0, compared, "passive-insufficient-window"); } var windowSamples = Math.Min(TargetWindowSamples, compared); @@ -63,16 +65,26 @@ public sealed class MimirPassiveAudioSynchronizationEstimator } } + var secondBest = FindSecondPeak(referenceSpectrum, -maxLag, maxLag, bestIndex); var mean = sum / Math.Max(1, count); var variance = Math.Max(0.0, sumSquares / Math.Max(1, count) - mean * mean); var sigma = Math.Sqrt(variance); var refinedLag = bestIndex + RefinePeak(referenceSpectrum, bestIndex); var peakRatio = mean > 1.0e-12 ? best / mean : 0.0; var zScore = sigma > 1.0e-12 ? (best - mean) / sigma : 0.0; + var peakDominance = secondBest > 1.0e-12 ? (best - secondBest) / best : 1.0; + var positiveLagConfidence = refinedLag >= 0.0 ? 1.0 : 0.0; var confidence = Math.Clamp((peakRatio - 1.5) / 8.0, 0.0, 1.0) * - Math.Clamp(zScore / 16.0, 0.0, 1.0); - var status = confidence > 0.08 ? "passive-report" : "passive-low-confidence"; - return new MimirPassiveAudioSynchronizationEstimate(refinedLag, confidence, best, mean, compared, status); + Math.Clamp(zScore / 16.0, 0.0, 1.0) * + Math.Clamp(peakDominance / 0.35, 0.0, 1.0) * + positiveLagConfidence; + confidence = Math.Min(confidence, MaxSingleWindowConfidence); + var status = refinedLag < 0.0 + ? "passive-negative-lag" + : confidence > 0.08 + ? "passive-report" + : "passive-low-confidence"; + return new MimirPassiveAudioSynchronizationEstimate(refinedLag, confidence, best, mean, secondBest, compared, status); } private static void FillWindow(ReadOnlySpan source, Complex[] destination, int count) @@ -112,6 +124,23 @@ public sealed class MimirPassiveAudioSynchronizationEstimator : Math.Clamp(0.5 * (left - right) / denominator, -0.5, 0.5); } + private static double FindSecondPeak(Complex[] correlation, int firstLag, int lastLag, int bestLag) + { + var second = 0.0; + var exclusionSamples = 256; + for (var lag = firstLag; lag <= lastLag; lag++) + { + if (Math.Abs(lag - bestLag) <= exclusionSamples) + { + continue; + } + + second = Math.Max(second, Math.Abs(CorrelationAt(correlation, lag))); + } + + return second; + } + private static int NextPowerOfTwo(int value) { var power = 1; diff --git a/src/Mimir.Runtime/Synchronization/MimirSynchronizationHub.cs b/src/Mimir.Runtime/Synchronization/MimirSynchronizationHub.cs index 2f1596c..4d19691 100644 --- a/src/Mimir.Runtime/Synchronization/MimirSynchronizationHub.cs +++ b/src/Mimir.Runtime/Synchronization/MimirSynchronizationHub.cs @@ -75,8 +75,15 @@ public sealed class MimirSynchronizationHub : IDisposable MimirAudioSyncMode mode = MimirAudioSyncMode.ChirpOnly) { ObjectDisposedException.ThrowIf(disposed, this); + var candidates = Buffers.Buffers + .Where(buffer => + buffer.Descriptor.Kind == MimirStreamKind.Audio && + !string.Equals(buffer.Descriptor.SourceId, referenceSourceId, StringComparison.Ordinal) && + buffer.Latest?.AudioBlock != null) + .Select(buffer => buffer.Descriptor.SourceId) + .ToArray(); var reports = audioSynchronization.Analyze(Buffers.Buffers, referenceSourceId, mode); - StoreAudioSynchronizationReports(reports); + StoreAudioSynchronizationReports(reports, candidates); return reports; } @@ -111,20 +118,39 @@ public sealed class MimirSynchronizationHub : IDisposable referenceSourceId, mode, selected); - StoreAudioSynchronizationReports(reports); + StoreAudioSynchronizationReports(reports, selected); return reports; } private bool disposed; - private void StoreAudioSynchronizationReports(IReadOnlyList reports) + private void StoreAudioSynchronizationReports( + IReadOnlyList reports, + IEnumerable analyzedCandidateIds) { + var reportIds = new HashSet(reports.Select(report => report.SourceId), StringComparer.Ordinal); + foreach (var sourceId in analyzedCandidateIds) + { + if (!reportIds.Contains(sourceId)) + { + audioSynchronizationReports.Remove(sourceId); + } + } + foreach (var report in reports) { audioSynchronizationReports[report.SourceId] = report; } audioSynchronizationState.Update(reports); + foreach (var trace in audioSynchronization.LastDecodeTraces) + { + if (string.Equals(trace.Status, "passive-negative-lag", StringComparison.Ordinal)) + { + audioSynchronizationReports.Remove(trace.SourceId); + audioSynchronizationState.Remove(trace.SourceId); + } + } } public void Dispose() diff --git a/state/evidence.jsonl b/state/evidence.jsonl index 29e6baa..7e6eac0 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -55,3 +55,4 @@ {"ts": "2026-05-22T18:47:44+01:00", "type": "implementation", "status": "ok", "note": "Added runtime-owned audio sync mode selection for chirp-only, no-chirp, and hybrid. Config/env now expose MIMIR_AUDIO_SYNC_MODE plus audioSync.mode/referenceSourceId/calibrationGain; no-chirp disables active calibration and reports passive sync as not wired yet, while hybrid currently uses the active pilot until passive confidence gating and shaped watermark emission are implemented. dotnet build and chirplet self-test passed."} {"ts": "2026-05-22T19:08:10+01:00", "type": "implementation", "status": "ok", "note": "Renamed the public no-chirp mode to passive and started the real hybrid sync machine. Added MimirPassiveAudioSynchronizationEstimator using PHAT-weighted cross-spectrum delay estimation over loopback/program audio; MimirAudioSynchronizationAnalyzer now reports passive evidence in passive/hybrid modes and falls back to chirplet anchors when hybrid passive confidence is weak. Hybrid runtime emission now queues short active pilot chunks only while passive confidence stays below threshold. dotnet build, passive sync self-test, and chirplet self-test passed."} {"ts": "2026-05-22T19:08:56+01:00", "type": "verification", "status": "partial", "note": "Passive live smoke with MIMIR_AUDIO_SYNC_MODE=passive and config/mimir-runtime.probes.example.json emitted no calibration audio and produced loopback/program-audio reports: Focusrite delaySamples=6653 confidence=0.805, Kiyo Pro delaySamples=6804 confidence=0.227, USB Camera 0 delaySamples=-882 confidence=1.000 over 88200 compared 44.1k samples. This proves the passive path can lock on real loopback/music, but the negative Eye delay needs timestamp/window-sign inspection before driving an actuator."} +{"ts": "2026-05-22T19:33:21+01:00", "type": "verification", "status": "ok", "note": "Fixed passive sync confidence bug. Negative passive lags now produce status=passive-negative-lag with confidence=0, single-window passive confidence is capped at 0.85, and hub invalidates cached reports/state for negative-lag traces. Passive self-test now proves both positive delay recovery and reversed-input zero confidence. A 14s live passive smoke showed negative Focusrite evidence at confidence 0 without leaving a report/state, while positive passive reports stayed capped at 0.85."} diff --git a/state/map.yaml b/state/map.yaml index 93cc23e..c077219 100644 --- a/state/map.yaml +++ b/state/map.yaml @@ -99,7 +99,9 @@ accepted_design: and `hybrid` emits active pilot chunks only while passive confidence is weak. `MimirPassiveAudioSynchronizationEstimator` is the first passive path: it uses PHAT-weighted cross-spectrum correlation between loopback and mic - windows before any watermark is needed. + windows before any watermark is needed. Negative passive lags are + contradictory evidence and carry zero confidence; single-window passive + confidence is capped below certainty until repeated state earns it. - `IMimirVideoCaptureDriver` and `MimirVideoCaptureDriverSource` are the live seam for Leap, Media Foundation, DirectShow, libusb, LeapC, or shared texture capture.