Add chirp-bin calibration profiles
This commit is contained in:
parent
353031858a
commit
3a7410d510
10 changed files with 195 additions and 38 deletions
|
|
@ -133,6 +133,11 @@ foreach (var trace in hub.AudioSynchronizationDecodeTraces.OrderBy(trace => trac
|
|||
$"sync-decode {trace.ReferenceSourceId}->{trace.SourceId}: status={trace.Status} compared={trace.ComparedSamples} rate={trace.SampleRate} refFrames={trace.ReferenceFrames} refAnchors={trace.ReferenceAnchors} refClock={trace.ReferenceClockConfidence:0.000} refEnergy={trace.ReferenceBestEnergy:0.000} candFrames={trace.CandidateFrames} candAnchors={trace.CandidateAnchors} candClock={trace.CandidateClockConfidence:0.000} candEnergy={trace.CandidateBestEnergy:0.000} matched={trace.MatchedEvents} confidence={trace.Confidence:0.000}");
|
||||
}
|
||||
|
||||
foreach (var profile in hub.AudioChirpBinCalibrationProfiles)
|
||||
{
|
||||
Console.WriteLine(DescribeCalibrationProfile("sync-calibration", profile));
|
||||
}
|
||||
|
||||
if (requireSamples && emptyBuffers.Count > 0)
|
||||
{
|
||||
Console.Error.WriteLine($"empty buffers: {string.Join(", ", emptyBuffers)}");
|
||||
|
|
@ -177,6 +182,18 @@ static string DescribeBands(IReadOnlyList<MimirChirpletBandResponse> bands)
|
|||
: string.Join(",", bands.Select(band => $"{band.CenterHz:0}Hz:{band.Energy:0.000}"));
|
||||
}
|
||||
|
||||
static string DescribeCalibrationProfile(string prefix, MimirChirpBinCalibrationProfile profile)
|
||||
{
|
||||
var top = profile.Bands.Count == 0
|
||||
? "none"
|
||||
: string.Join(",", profile.StrongestBands(8).Select(band =>
|
||||
$"{band.CenterHz:0}Hz:mean={band.MeanEnergy:0.000}:rel={band.RelativeGain:0.000}:n={band.ObservationCount}"));
|
||||
var mae = double.IsFinite(profile.MeanAnchorErrorSamples)
|
||||
? profile.MeanAnchorErrorSamples.ToString("0.000")
|
||||
: "none";
|
||||
return $"{prefix} {profile.SourceId}: sampleRate={profile.SampleRate} frames={profile.FrameCount} anchors={profile.AnchorCount} clock={profile.ClockConfidence:0.000} maeSamples={mae} usableBins={profile.UsableBandCount}/{profile.Bands.Count} top={top}";
|
||||
}
|
||||
|
||||
static int RunChirpletSelfTest()
|
||||
{
|
||||
var timeline = MimirChirpletTimeline.Default;
|
||||
|
|
@ -525,6 +542,11 @@ static int AnalyzeAsioFloat32(string inputPath, int sampleRate, int channels, in
|
|||
$"asio-f32-trace {trace.ReferenceSourceId}->{trace.SourceId}: status={trace.Status} compared={trace.ComparedSamples} refFrames={trace.ReferenceFrames} refAnchors={trace.ReferenceAnchors} refClock={trace.ReferenceClockConfidence:0.000} candFrames={trace.CandidateFrames} candAnchors={trace.CandidateAnchors} candClock={trace.CandidateClockConfidence:0.000} matched={trace.MatchedEvents} confidence={trace.Confidence:0.000}");
|
||||
}
|
||||
|
||||
foreach (var profile in analyzer.LastCalibrationProfiles)
|
||||
{
|
||||
Console.WriteLine(DescribeCalibrationProfile("asio-f32-calibration", profile));
|
||||
}
|
||||
|
||||
foreach (var report in reports.OrderBy(report => report.SourceId, StringComparer.Ordinal))
|
||||
{
|
||||
Console.WriteLine(
|
||||
|
|
|
|||
|
|
@ -11,10 +11,14 @@ public sealed class MimirAudioSynchronizationAnalyzer
|
|||
private const double MaxPairClockFitDelaySeconds = 10.0;
|
||||
private const double PassiveReportConfidence = 0.08;
|
||||
private readonly List<MimirAudioSynchronizationDecodeTrace> lastDecodeTraces = [];
|
||||
private readonly Dictionary<string, MimirChirpBinCalibrationProfile> lastCalibrationProfiles = new(StringComparer.Ordinal);
|
||||
private readonly MimirPassiveAudioSynchronizationEstimator passiveEstimator = new();
|
||||
|
||||
public IReadOnlyList<MimirAudioSynchronizationDecodeTrace> LastDecodeTraces => lastDecodeTraces;
|
||||
|
||||
public IReadOnlyList<MimirChirpBinCalibrationProfile> LastCalibrationProfiles =>
|
||||
lastCalibrationProfiles.Values.OrderBy(profile => profile.SourceId, StringComparer.Ordinal).ToArray();
|
||||
|
||||
public IReadOnlyList<MimirAudioSynchronizationReport> Analyze(
|
||||
IEnumerable<MimirRollingStreamBuffer> buffers,
|
||||
string referenceSourceId,
|
||||
|
|
@ -46,6 +50,7 @@ public sealed class MimirAudioSynchronizationAnalyzer
|
|||
|
||||
var reports = new List<MimirAudioSynchronizationReport>();
|
||||
lastDecodeTraces.Clear();
|
||||
lastCalibrationProfiles.Clear();
|
||||
foreach (var buffer in audioBuffers)
|
||||
{
|
||||
if (ReferenceEquals(buffer, reference))
|
||||
|
|
@ -168,6 +173,12 @@ public sealed class MimirAudioSynchronizationAnalyzer
|
|||
var candidateDecode = useChirpBinTimeline
|
||||
? MimirChirpBinTimeline.Default.DecodeStreamWindow(candidateWindow, activeReferenceBlock.SampleRate)
|
||||
: MimirChirpletTimeline.Default.DecodeStreamWindow(candidateWindow, activeReferenceBlock.SampleRate);
|
||||
if (useChirpBinTimeline)
|
||||
{
|
||||
StoreCalibrationProfile(reference.Descriptor.SourceId, activeReferenceBlock.SampleRate, comparedReferenceDecode);
|
||||
StoreCalibrationProfile(buffer.Descriptor.SourceId, activeReferenceBlock.SampleRate, candidateDecode);
|
||||
}
|
||||
|
||||
var deterministicFit = EstimateDelayFromDecodedTimeline(comparedReferenceDecode, candidateDecode);
|
||||
lastDecodeTraces.Add(new MimirAudioSynchronizationDecodeTrace(
|
||||
reference.Descriptor.SourceId,
|
||||
|
|
@ -217,6 +228,14 @@ public sealed class MimirAudioSynchronizationAnalyzer
|
|||
return reports;
|
||||
}
|
||||
|
||||
private void StoreCalibrationProfile(
|
||||
string sourceId,
|
||||
int sampleRate,
|
||||
MimirChirpletStreamDecode decode)
|
||||
{
|
||||
lastCalibrationProfiles[sourceId] = MimirChirpBinCalibrationProfile.FromDecode(sourceId, sampleRate, decode);
|
||||
}
|
||||
|
||||
private static int MinWindowSamples(int sampleRate, double seconds)
|
||||
{
|
||||
return (int)Math.Ceiling(sampleRate * seconds);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,77 @@
|
|||
namespace Mimir.Runtime.Synchronization;
|
||||
|
||||
public sealed record MimirChirpBinCalibrationBand(
|
||||
double CenterHz,
|
||||
double MeanEnergy,
|
||||
double PeakEnergy,
|
||||
double RelativeGain,
|
||||
int ObservationCount,
|
||||
bool Usable);
|
||||
|
||||
public sealed record MimirChirpBinCalibrationProfile(
|
||||
string SourceId,
|
||||
int SampleRate,
|
||||
int FrameCount,
|
||||
int AnchorCount,
|
||||
double ClockConfidence,
|
||||
double MeanAnchorErrorSamples,
|
||||
IReadOnlyList<MimirChirpBinCalibrationBand> Bands)
|
||||
{
|
||||
private const double UsableRelativeGain = 0.20;
|
||||
private const double UsableMeanEnergy = 0.015;
|
||||
|
||||
public int UsableBandCount => Bands.Count(band => band.Usable);
|
||||
|
||||
public IReadOnlyList<MimirChirpBinCalibrationBand> StrongestBands(int count) =>
|
||||
Bands
|
||||
.OrderByDescending(band => band.MeanEnergy)
|
||||
.ThenBy(band => band.CenterHz)
|
||||
.Take(Math.Max(0, count))
|
||||
.ToArray();
|
||||
|
||||
public static MimirChirpBinCalibrationProfile FromDecode(
|
||||
string sourceId,
|
||||
int sampleRate,
|
||||
MimirChirpletStreamDecode decode)
|
||||
{
|
||||
var grouped = decode.Frames
|
||||
.SelectMany(frame => frame.BestCandidate.BandResponses ?? [])
|
||||
.GroupBy(response => response.CenterHz)
|
||||
.Select(group => new
|
||||
{
|
||||
CenterHz = group.Key,
|
||||
MeanEnergy = group.Average(response => response.Energy),
|
||||
PeakEnergy = group.Max(response => response.Energy),
|
||||
ObservationCount = group.Count(),
|
||||
})
|
||||
.OrderBy(band => band.CenterHz)
|
||||
.ToArray();
|
||||
var strongestMean = grouped.Length == 0
|
||||
? 0.0
|
||||
: grouped.Max(band => band.MeanEnergy);
|
||||
var bands = grouped
|
||||
.Select(band =>
|
||||
{
|
||||
var relativeGain = strongestMean <= 1.0e-12
|
||||
? 0.0
|
||||
: band.MeanEnergy / strongestMean;
|
||||
return new MimirChirpBinCalibrationBand(
|
||||
band.CenterHz,
|
||||
band.MeanEnergy,
|
||||
band.PeakEnergy,
|
||||
relativeGain,
|
||||
band.ObservationCount,
|
||||
relativeGain >= UsableRelativeGain && band.MeanEnergy >= UsableMeanEnergy);
|
||||
})
|
||||
.ToArray();
|
||||
|
||||
return new MimirChirpBinCalibrationProfile(
|
||||
sourceId,
|
||||
sampleRate,
|
||||
decode.Frames.Count,
|
||||
decode.Anchors.Count,
|
||||
decode.ClockFit?.Confidence ?? 0.0,
|
||||
decode.ClockFit?.MeanAbsoluteErrorSamples ?? double.NaN,
|
||||
bands);
|
||||
}
|
||||
}
|
||||
|
|
@ -36,6 +36,9 @@ public sealed class MimirSynchronizationHub : IDisposable
|
|||
public IReadOnlyList<MimirAudioSynchronizationDecodeTrace> AudioSynchronizationDecodeTraces =>
|
||||
audioSynchronization.LastDecodeTraces;
|
||||
|
||||
public IReadOnlyList<MimirChirpBinCalibrationProfile> AudioChirpBinCalibrationProfiles =>
|
||||
audioSynchronization.LastCalibrationProfiles;
|
||||
|
||||
public void AddSource(IMimirStreamSource source)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(disposed, this);
|
||||
|
|
|
|||
Loading…
Reference in a new issue