Test chirp-bin timing at ASIO rates
This commit is contained in:
parent
699b20f961
commit
ba966a4f86
5 changed files with 54 additions and 16 deletions
|
|
@ -4,7 +4,7 @@ namespace Mimir.Runtime.Synchronization;
|
|||
|
||||
public sealed class MimirAudioSynchronizationAnalyzer
|
||||
{
|
||||
private const int MaxWindowSamples = 48_000 * 2;
|
||||
private const double MaxWindowSeconds = 2.0;
|
||||
private const double MinPassiveWindowSeconds = 2.0;
|
||||
private const double MinChirpletWindowSeconds = 2.0;
|
||||
private const double MinChirpBinWindowSeconds = 0.40;
|
||||
|
|
@ -215,7 +215,7 @@ public sealed class MimirAudioSynchronizationAnalyzer
|
|||
|
||||
private static int MinWindowSamples(int sampleRate, double seconds)
|
||||
{
|
||||
return Math.Min(MaxWindowSamples, (int)Math.Ceiling(sampleRate * seconds));
|
||||
return (int)Math.Ceiling(sampleRate * seconds);
|
||||
}
|
||||
|
||||
private static MimirAudioSynchronizationDecodeTrace InsufficientWindowTrace(
|
||||
|
|
@ -316,7 +316,8 @@ public sealed class MimirAudioSynchronizationAnalyzer
|
|||
return [];
|
||||
}
|
||||
|
||||
var samples = new List<float>(MaxWindowSamples);
|
||||
var maxWindowSamples = (int)Math.Ceiling(latestBlock.SampleRate * MaxWindowSeconds);
|
||||
var samples = new List<float>(maxWindowSamples);
|
||||
foreach (var sample in buffer.Snapshot()
|
||||
.Where(sample => sample.AudioBlock != null && !sample.Data.IsEmpty && sample.TimestampNs <= endNs)
|
||||
.Reverse())
|
||||
|
|
@ -328,12 +329,12 @@ public sealed class MimirAudioSynchronizationAnalyzer
|
|||
}
|
||||
|
||||
var mono = ExtractFirstChannel(sample.Data.Span, block.Channels, block.SampleFormat);
|
||||
for (var index = mono.Length - 1; index >= 0 && samples.Count < MaxWindowSamples; index--)
|
||||
for (var index = mono.Length - 1; index >= 0 && samples.Count < maxWindowSamples; index--)
|
||||
{
|
||||
samples.Add(mono[index]);
|
||||
}
|
||||
|
||||
if (samples.Count >= MaxWindowSamples)
|
||||
if (samples.Count >= maxWindowSamples)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,12 +42,15 @@ public sealed class MimirChirpBinTimeline
|
|||
public IReadOnlyList<MimirChirpBinSymbolDefinition> Codebook => Symbols;
|
||||
|
||||
public float[] RenderSegmentMonoFloat(ulong segmentIndex)
|
||||
=> RenderSegmentMonoFloat(segmentIndex, SampleRate);
|
||||
|
||||
public float[] RenderSegmentMonoFloat(ulong segmentIndex, int sampleRate)
|
||||
{
|
||||
var segmentStartSeconds = segmentIndex * SegmentSeconds;
|
||||
var samples = new float[(int)Math.Round(SegmentSeconds * SampleRate)];
|
||||
var samples = new float[(int)Math.Round(SegmentSeconds * sampleRate)];
|
||||
foreach (var timelineEvent in EventsOverlapping(segmentStartSeconds, SegmentSeconds))
|
||||
{
|
||||
AddChirp(samples, SampleRate, timelineEvent, segmentStartSeconds);
|
||||
AddChirp(samples, sampleRate, timelineEvent, segmentStartSeconds);
|
||||
}
|
||||
|
||||
return samples;
|
||||
|
|
@ -165,7 +168,7 @@ public sealed class MimirChirpBinTimeline
|
|||
{
|
||||
var candidates = ScoreBins(samples.Slice(offset, chirpSamples), sampleRate, offset);
|
||||
var energy = candidates.Length == 0 ? 0.0 : candidates[0].Energy;
|
||||
if (energy > bestEnergy || (energy >= bestEnergy * 0.995 && offset > bestOffset))
|
||||
if (energy > bestEnergy || PreferLaterPlateauOffset(sampleRate, energy, bestEnergy, offset, bestOffset))
|
||||
{
|
||||
bestEnergy = energy;
|
||||
bestOffset = offset;
|
||||
|
|
@ -179,7 +182,7 @@ public sealed class MimirChirpBinTimeline
|
|||
{
|
||||
var candidates = ScoreBins(samples.Slice(offset, chirpSamples), sampleRate, offset);
|
||||
var energy = candidates.Length == 0 ? 0.0 : candidates[0].Energy;
|
||||
if (energy > bestEnergy || (energy >= bestEnergy * 0.995 && offset > bestOffset))
|
||||
if (energy > bestEnergy || PreferLaterPlateauOffset(sampleRate, energy, bestEnergy, offset, bestOffset))
|
||||
{
|
||||
bestEnergy = energy;
|
||||
bestOffset = offset;
|
||||
|
|
@ -297,6 +300,18 @@ public sealed class MimirChirpBinTimeline
|
|||
return bestOffset + Math.Clamp(delta, -0.5, 0.5);
|
||||
}
|
||||
|
||||
private static bool PreferLaterPlateauOffset(
|
||||
int sampleRate,
|
||||
double energy,
|
||||
double bestEnergy,
|
||||
int offset,
|
||||
int bestOffset)
|
||||
{
|
||||
return sampleRate <= SampleRate &&
|
||||
energy >= bestEnergy * 0.995 &&
|
||||
offset > bestOffset;
|
||||
}
|
||||
|
||||
private static double DechirpedBin(ReadOnlySpan<float> samples, ChirpBinKernel kernel)
|
||||
{
|
||||
var real = 0.0;
|
||||
|
|
|
|||
Loading…
Reference in a new issue