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