Clamp passive sync confidence
This commit is contained in:
parent
6d45d1c9e8
commit
8237102a4d
7 changed files with 95 additions and 12 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -54,6 +54,11 @@ public sealed class MimirAudioSynchronizationStateTracker
|
|||
}
|
||||
}
|
||||
|
||||
public void Remove(string sourceId)
|
||||
{
|
||||
states.Remove(sourceId);
|
||||
}
|
||||
|
||||
private static MimirAudioSynchronizationState NewState(
|
||||
MimirAudioSynchronizationReport report,
|
||||
double smoothedDelaySamples,
|
||||
|
|
|
|||
|
|
@ -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<float> 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;
|
||||
|
|
|
|||
|
|
@ -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<MimirAudioSynchronizationReport> reports)
|
||||
private void StoreAudioSynchronizationReports(
|
||||
IReadOnlyList<MimirAudioSynchronizationReport> reports,
|
||||
IEnumerable<string> analyzedCandidateIds)
|
||||
{
|
||||
var reportIds = new HashSet<string>(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()
|
||||
|
|
|
|||
|
|
@ -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."}
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
Loading…
Reference in a new issue