Clamp passive sync confidence
This commit is contained in:
parent
6d45d1c9e8
commit
8237102a4d
7 changed files with 95 additions and 12 deletions
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Reference in a new issue