2026-05-22 00:52:43 +01:00
|
|
|
using Mimir.Runtime.Synchronization;
|
|
|
|
|
|
2026-05-22 16:46:50 +01:00
|
|
|
if (args.Any(arg => string.Equals(arg, "--chirplet-self-test", StringComparison.OrdinalIgnoreCase)))
|
|
|
|
|
{
|
|
|
|
|
return RunChirpletSelfTest();
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 19:50:02 +01:00
|
|
|
if (args.Any(arg => string.Equals(arg, "--chirp-bin-self-test", StringComparison.OrdinalIgnoreCase)))
|
|
|
|
|
{
|
|
|
|
|
return RunChirpBinSelfTest();
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 19:09:11 +01:00
|
|
|
if (args.Any(arg => string.Equals(arg, "--passive-sync-self-test", StringComparison.OrdinalIgnoreCase)))
|
|
|
|
|
{
|
|
|
|
|
return RunPassiveSyncSelfTest();
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 20:34:12 +01:00
|
|
|
if (args.Any(arg => string.Equals(arg, "--hybrid-sync-self-test", StringComparison.OrdinalIgnoreCase)))
|
|
|
|
|
{
|
|
|
|
|
return RunHybridSyncSelfTest();
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 00:52:43 +01:00
|
|
|
var duration = TimeSpan.FromSeconds(ParseDoubleOption(args, "--seconds", 10.0));
|
|
|
|
|
var pollDelay = TimeSpan.FromMilliseconds(ParseDoubleOption(args, "--poll-ms", 10.0));
|
|
|
|
|
var requireSamples = args.Any(arg => string.Equals(arg, "--require-samples", StringComparison.OrdinalIgnoreCase));
|
2026-05-22 11:40:56 +01:00
|
|
|
var syncReference = ParseStringOption(args, "--sync-reference", "loopback-scarlett-speakers");
|
2026-05-22 00:52:43 +01:00
|
|
|
using var configuration = new DisposableConfiguration(MimirRuntimeConfiguration.Load());
|
|
|
|
|
using var hub = new MimirSynchronizationHub(configuration.Value.Settings);
|
2026-05-22 19:09:11 +01:00
|
|
|
var syncMode = configuration.Value.Settings.Audio.Mode;
|
2026-05-22 00:52:43 +01:00
|
|
|
|
|
|
|
|
foreach (var source in configuration.Value.Sources)
|
|
|
|
|
{
|
|
|
|
|
configuration.Detach(source);
|
|
|
|
|
hub.AddSource(source);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var deadline = DateTimeOffset.UtcNow + duration;
|
2026-05-22 12:46:51 +01:00
|
|
|
var nextSyncPoll = DateTimeOffset.UtcNow + TimeSpan.FromSeconds(1);
|
2026-05-22 00:52:43 +01:00
|
|
|
while (DateTimeOffset.UtcNow < deadline)
|
|
|
|
|
{
|
|
|
|
|
hub.PollSources(maxSamplesPerSource: 4096);
|
2026-05-22 12:46:51 +01:00
|
|
|
if (DateTimeOffset.UtcNow >= nextSyncPoll)
|
|
|
|
|
{
|
2026-05-22 19:09:11 +01:00
|
|
|
hub.AnalyzeAudioSynchronization(syncReference, syncMode);
|
2026-05-22 12:46:51 +01:00
|
|
|
nextSyncPoll += TimeSpan.FromSeconds(1);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 00:52:43 +01:00
|
|
|
await Task.Delay(pollDelay).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hub.PollSources(maxSamplesPerSource: 16384);
|
|
|
|
|
|
|
|
|
|
Console.WriteLine($"sources={hub.SourceCount} ingested={hub.IngestedSamples} buffers={hub.Buffers.Buffers.Count}");
|
|
|
|
|
var emptyBuffers = new List<string>();
|
|
|
|
|
foreach (var buffer in hub.Buffers.Buffers.OrderBy(buffer => buffer.Descriptor.SourceId, StringComparer.Ordinal))
|
|
|
|
|
{
|
|
|
|
|
var latest = buffer.Latest;
|
|
|
|
|
if (latest?.VideoFrame is { } frame)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(
|
|
|
|
|
$"{buffer.Descriptor.SourceId}: count={buffer.Count} edgeNs={buffer.EdgeNs} latest={frame.Width}x{frame.Height} {frame.PixelFormat} bytes={latest.Value.ByteLength}");
|
|
|
|
|
}
|
2026-05-22 01:07:30 +01:00
|
|
|
else if (latest?.AudioBlock is { } block)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(
|
|
|
|
|
$"{buffer.Descriptor.SourceId}: count={buffer.Count} edgeNs={buffer.EdgeNs} latest={block.Channels}ch {block.SampleRate}Hz {block.SampleFormat} frames={block.FrameCount} bytes={latest.Value.ByteLength}");
|
|
|
|
|
}
|
2026-05-22 00:52:43 +01:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"{buffer.Descriptor.SourceId}: count={buffer.Count} edgeNs={buffer.EdgeNs}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (buffer.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
emptyBuffers.Add(buffer.Descriptor.SourceId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 19:09:11 +01:00
|
|
|
var reports = hub.AnalyzeAudioSynchronization(syncReference, syncMode);
|
2026-05-22 01:22:36 +01:00
|
|
|
foreach (var report in reports.OrderBy(report => report.SourceId, StringComparer.Ordinal))
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(
|
2026-05-23 10:27:16 +01:00
|
|
|
$"sync {report.ReferenceSourceId}->{report.SourceId}: evidence={report.EvidenceKind} delaySamples={report.DelaySamples} fractionalDelaySamples={report.FractionalDelaySamples:0.000000} delayUs={report.DelayMicroseconds:0.000} delayMs={report.DelayMilliseconds:0.000} confidence={report.Confidence:0.000} bands={DescribeBands(report.BandResponses)} compared={report.ComparedSamples}");
|
2026-05-22 01:22:36 +01:00
|
|
|
}
|
|
|
|
|
|
2026-05-22 12:46:51 +01:00
|
|
|
foreach (var state in hub.AudioSynchronizationStates)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(
|
2026-05-23 10:27:16 +01:00
|
|
|
$"sync-state {state.ReferenceSourceId}->{state.SourceId}: smoothedDelaySamples={state.SmoothedDelaySamples:0.000000} delayUs={state.DelayMicroseconds:0.000} delayMs={state.DelayMilliseconds:0.000} sroPpm={state.SamplingRateOffsetPpm:0.000} confidence={state.Confidence:0.000} bands={DescribeBands(state.BandResponses)}");
|
2026-05-22 12:46:51 +01:00
|
|
|
}
|
|
|
|
|
|
2026-05-22 17:35:21 +01:00
|
|
|
foreach (var trace in hub.AudioSynchronizationDecodeTraces.OrderBy(trace => trace.SourceId, StringComparer.Ordinal))
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(
|
2026-05-22 18:09:17 +01:00
|
|
|
$"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}");
|
2026-05-22 17:35:21 +01:00
|
|
|
}
|
|
|
|
|
|
2026-05-22 00:52:43 +01:00
|
|
|
if (requireSamples && emptyBuffers.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
Console.Error.WriteLine($"empty buffers: {string.Join(", ", emptyBuffers)}");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
static double ParseDoubleOption(IReadOnlyList<string> args, string name, double fallback)
|
|
|
|
|
{
|
|
|
|
|
for (var index = 0; index < args.Count - 1; index++)
|
|
|
|
|
{
|
|
|
|
|
if (string.Equals(args[index], name, StringComparison.OrdinalIgnoreCase)
|
|
|
|
|
&& double.TryParse(args[index + 1], out var parsed)
|
|
|
|
|
&& parsed > 0)
|
|
|
|
|
{
|
|
|
|
|
return parsed;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fallback;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 11:40:56 +01:00
|
|
|
static string ParseStringOption(IReadOnlyList<string> args, string name, string fallback)
|
|
|
|
|
{
|
|
|
|
|
for (var index = 0; index < args.Count - 1; index++)
|
|
|
|
|
{
|
|
|
|
|
if (string.Equals(args[index], name, StringComparison.OrdinalIgnoreCase)
|
|
|
|
|
&& !string.IsNullOrWhiteSpace(args[index + 1]))
|
|
|
|
|
{
|
|
|
|
|
return args[index + 1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fallback;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 12:40:30 +01:00
|
|
|
static string DescribeBands(IReadOnlyList<MimirChirpletBandResponse> bands)
|
|
|
|
|
{
|
|
|
|
|
return bands.Count == 0
|
|
|
|
|
? "none"
|
|
|
|
|
: string.Join(",", bands.Select(band => $"{band.CenterHz:0}Hz:{band.Energy:0.000}"));
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 16:46:50 +01:00
|
|
|
static int RunChirpletSelfTest()
|
|
|
|
|
{
|
|
|
|
|
var timeline = MimirChirpletTimeline.Default;
|
|
|
|
|
var decoder = new MimirChirpletStreamDecoder(windowDuration: TimeSpan.FromSeconds(2));
|
|
|
|
|
MimirChirpletStreamDecode decode = new([], [], [], null);
|
|
|
|
|
for (ulong segment = 0; segment < 4; segment++)
|
|
|
|
|
{
|
|
|
|
|
decode = decoder.Append(timeline.RenderSegmentMonoFloat(segment));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var meanAbsoluteError = decode.ClockFit?.MeanAbsoluteErrorSamples ?? double.PositiveInfinity;
|
|
|
|
|
Console.WriteLine(
|
|
|
|
|
$"chirplet-self-test frames={decode.Frames.Count} symbols={decode.Symbols.Count} anchors={decode.Anchors.Count} " +
|
|
|
|
|
$"clock={(decode.ClockFit is null ? "none" : decode.ClockFit.EffectiveSampleRate.ToString("0.000000"))} " +
|
|
|
|
|
$"confidence={(decode.ClockFit?.Confidence ?? 0.0):0.000} mae={meanAbsoluteError:0.000000}");
|
|
|
|
|
|
|
|
|
|
foreach (var anchor in decode.Anchors)
|
|
|
|
|
{
|
|
|
|
|
var expected = timeline.EventForIndex(anchor.EventIndex).StartSeconds * MimirChirpletTimeline.SampleRate;
|
|
|
|
|
Console.WriteLine(
|
|
|
|
|
$"chirplet-anchor event={anchor.EventIndex} actual={anchor.SampleOffset:0.000} expected={expected:0.000} " +
|
|
|
|
|
$"error={anchor.SampleOffset - expected:0.000} confidence={anchor.Confidence:0.000}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (decode.ClockFit == null || decode.Anchors.Count < 12 || meanAbsoluteError > 0.25)
|
|
|
|
|
{
|
|
|
|
|
Console.Error.WriteLine("chirplet self-test failed: canonical timeline did not decode to sub-frame anchors");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 19:50:02 +01:00
|
|
|
static int RunChirpBinSelfTest()
|
|
|
|
|
{
|
|
|
|
|
var timeline = MimirChirpBinTimeline.Default;
|
|
|
|
|
var samples = new List<float>();
|
|
|
|
|
for (ulong segment = 0; segment < 6; segment++)
|
|
|
|
|
{
|
|
|
|
|
samples.AddRange(timeline.RenderSegmentMonoFloat(segment));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var decode = timeline.DecodeStreamWindow(samples.ToArray(), MimirChirpBinTimeline.SampleRate);
|
|
|
|
|
var meanAbsoluteError = decode.ClockFit?.MeanAbsoluteErrorSamples ?? double.PositiveInfinity;
|
|
|
|
|
Console.WriteLine(
|
|
|
|
|
$"chirp-bin-self-test frames={decode.Frames.Count} symbols={decode.Symbols.Count} anchors={decode.Anchors.Count} " +
|
|
|
|
|
$"clock={(decode.ClockFit is null ? "none" : decode.ClockFit.EffectiveSampleRate.ToString("0.000000"))} " +
|
|
|
|
|
$"confidence={(decode.ClockFit?.Confidence ?? 0.0):0.000} mae={meanAbsoluteError:0.000000}");
|
|
|
|
|
Console.WriteLine("chirp-bin-expected " + string.Join(",", Enumerable.Range(0, 12).Select(index => timeline.EventForIndex((ulong)index).SymbolId)));
|
|
|
|
|
Console.WriteLine("chirp-bin-symbols " + string.Join(",", decode.Symbols.Take(12).Select(symbol => $"{symbol.SymbolId}@{symbol.SampleOffset:0}:{symbol.Energy:0.000}")));
|
|
|
|
|
|
|
|
|
|
foreach (var anchor in decode.Anchors.Take(16))
|
|
|
|
|
{
|
|
|
|
|
var expected = timeline.EventForIndex(anchor.EventIndex).StartSeconds * MimirChirpBinTimeline.SampleRate;
|
|
|
|
|
Console.WriteLine(
|
|
|
|
|
$"chirp-bin-anchor event={anchor.EventIndex} actual={anchor.SampleOffset:0.000} expected={expected:0.000} " +
|
|
|
|
|
$"error={anchor.SampleOffset - expected:0.000} confidence={anchor.Confidence:0.000}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (decode.ClockFit == null || decode.Anchors.Count < 16 || meanAbsoluteError > 2.0)
|
|
|
|
|
{
|
|
|
|
|
Console.Error.WriteLine("chirp-bin self-test failed: dechirp/bin decoder did not recover stable timeline anchors");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 19:09:11 +01:00
|
|
|
static int RunPassiveSyncSelfTest()
|
|
|
|
|
{
|
|
|
|
|
const int sampleRate = 48_000;
|
|
|
|
|
const int delaySamples = 1732;
|
|
|
|
|
const int sampleCount = 48_000;
|
|
|
|
|
var reference = new float[sampleCount];
|
|
|
|
|
var candidate = new float[sampleCount];
|
2026-05-22 19:33:36 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 19:09:11 +01:00
|
|
|
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) +
|
2026-05-22 19:33:36 +01:00
|
|
|
0.12 * Math.Sin(2.0 * Math.PI * 3253.0 * index / sampleRate) +
|
|
|
|
|
0.08 * noise[index]);
|
2026-05-22 19:09:11 +01:00
|
|
|
var source = index - delaySamples;
|
|
|
|
|
candidate[index] = source >= 0 ? reference[source] : 0.0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var estimator = new MimirPassiveAudioSynchronizationEstimator();
|
|
|
|
|
var estimate = estimator.Estimate(reference, candidate, sampleRate);
|
|
|
|
|
var error = estimate.DelaySamples - delaySamples;
|
|
|
|
|
Console.WriteLine(
|
|
|
|
|
$"passive-sync-self-test delaySamples={estimate.DelaySamples:0.000} expected={delaySamples} " +
|
2026-05-22 19:33:36 +01:00
|
|
|
$"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)
|
2026-05-22 19:09:11 +01:00
|
|
|
{
|
|
|
|
|
Console.Error.WriteLine("passive sync self-test failed: delayed program signal did not produce a confident passive estimate");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 19:33:36 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 19:09:11 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 20:34:12 +01:00
|
|
|
static int RunHybridSyncSelfTest()
|
|
|
|
|
{
|
|
|
|
|
const string referenceSourceId = "loopback-test";
|
|
|
|
|
const string candidateSourceId = "mic-test";
|
2026-05-23 10:27:16 +01:00
|
|
|
const double delaySamples = 317.375;
|
2026-05-22 20:34:12 +01:00
|
|
|
|
|
|
|
|
var firstSegment = MimirChirpBinTimeline.Default.RenderSegmentMonoFloat(0);
|
|
|
|
|
var secondSegment = MimirChirpBinTimeline.Default.RenderSegmentMonoFloat(1);
|
|
|
|
|
var reference = new float[firstSegment.Length + secondSegment.Length];
|
|
|
|
|
Array.Copy(firstSegment, 0, reference, 0, firstSegment.Length);
|
|
|
|
|
Array.Copy(secondSegment, 0, reference, firstSegment.Length, secondSegment.Length);
|
2026-05-23 10:27:16 +01:00
|
|
|
var candidate = ApplyFractionalDelay(reference, delaySamples);
|
2026-05-22 20:34:12 +01:00
|
|
|
|
|
|
|
|
var referenceBuffer = new MimirRollingStreamBuffer(
|
|
|
|
|
new MimirStreamDescriptor(referenceSourceId, MimirStreamKind.Audio, MimirStreamOrigin.LocalDevice),
|
|
|
|
|
TimeSpan.FromSeconds(5));
|
|
|
|
|
var candidateBuffer = new MimirRollingStreamBuffer(
|
|
|
|
|
new MimirStreamDescriptor(candidateSourceId, MimirStreamKind.Audio, MimirStreamOrigin.LocalDevice),
|
|
|
|
|
TimeSpan.FromSeconds(5));
|
|
|
|
|
|
|
|
|
|
AppendFloatBlock(referenceBuffer, referenceSourceId, reference, MimirChirpBinTimeline.SampleRate);
|
|
|
|
|
AppendFloatBlock(candidateBuffer, candidateSourceId, candidate, MimirChirpBinTimeline.SampleRate);
|
|
|
|
|
|
|
|
|
|
var analyzer = new MimirAudioSynchronizationAnalyzer();
|
|
|
|
|
var reports = analyzer.Analyze([referenceBuffer, candidateBuffer], referenceSourceId, MimirAudioSyncMode.Hybrid);
|
|
|
|
|
var report = reports.SingleOrDefault();
|
|
|
|
|
foreach (var trace in analyzer.LastDecodeTraces)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(
|
|
|
|
|
$"hybrid-sync-trace {trace.ReferenceSourceId}->{trace.SourceId}: status={trace.Status} compared={trace.ComparedSamples} refFrames={trace.ReferenceFrames} refAnchors={trace.ReferenceAnchors} candFrames={trace.CandidateFrames} candAnchors={trace.CandidateAnchors} matched={trace.MatchedEvents} confidence={trace.Confidence:0.000}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (report == null)
|
|
|
|
|
{
|
|
|
|
|
Console.Error.WriteLine("hybrid sync self-test failed: analyzer did not report from a short chirp-bin window");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var error = Math.Abs(report.FractionalDelaySamples - delaySamples);
|
|
|
|
|
Console.WriteLine(
|
2026-05-23 10:27:16 +01:00
|
|
|
$"hybrid-sync-self-test evidence={report.EvidenceKind} delaySamples={report.FractionalDelaySamples:0.000000} delayUs={report.DelayMicroseconds:0.000} expected={delaySamples:0.000000} errorSamples={error:0.000000} errorUs={error * 1_000_000.0 / report.SampleRate:0.000} confidence={report.Confidence:0.000} events={report.TimelineMatchedEvents} compared={report.ComparedSamples}");
|
2026-05-22 20:34:12 +01:00
|
|
|
return string.Equals(report.EvidenceKind, "chirp-bin", StringComparison.Ordinal) &&
|
|
|
|
|
report.TimelineMatchedEvents >= 3 &&
|
|
|
|
|
report.Confidence > 0.70 &&
|
2026-05-23 10:27:16 +01:00
|
|
|
error * 1_000_000.0 / report.SampleRate < 1.0
|
2026-05-22 20:34:12 +01:00
|
|
|
? 0
|
|
|
|
|
: 1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 10:27:16 +01:00
|
|
|
static float[] ApplyFractionalDelay(float[] source, double delaySamples)
|
|
|
|
|
{
|
|
|
|
|
var delayed = new float[source.Length];
|
|
|
|
|
for (var index = 0; index < delayed.Length; index++)
|
|
|
|
|
{
|
|
|
|
|
var sourcePosition = index - delaySamples;
|
|
|
|
|
if (sourcePosition < 0.0 || sourcePosition >= source.Length - 1)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var left = (int)Math.Floor(sourcePosition);
|
|
|
|
|
var fraction = sourcePosition - left;
|
|
|
|
|
delayed[index] = (float)(source[left] + (source[left + 1] - source[left]) * fraction);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return delayed;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 20:34:12 +01:00
|
|
|
static void AppendFloatBlock(MimirRollingStreamBuffer buffer, string sourceId, float[] samples, int sampleRate)
|
|
|
|
|
{
|
|
|
|
|
var bytes = new byte[samples.Length * sizeof(float)];
|
|
|
|
|
for (var index = 0; index < samples.Length; index++)
|
|
|
|
|
{
|
|
|
|
|
BitConverter.TryWriteBytes(bytes.AsSpan(index * sizeof(float), sizeof(float)), samples[index]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buffer.Append(new MimirStreamSample(
|
|
|
|
|
sourceId,
|
|
|
|
|
MimirStreamKind.Audio,
|
|
|
|
|
MimirStreamOrigin.LocalDevice,
|
|
|
|
|
1_000_000_000L,
|
|
|
|
|
1_000_000_000L,
|
|
|
|
|
1,
|
|
|
|
|
0,
|
|
|
|
|
bytes.Length,
|
|
|
|
|
bytes,
|
|
|
|
|
AudioBlock: new MimirAudioBlockDescriptor(
|
|
|
|
|
sampleRate,
|
|
|
|
|
1,
|
|
|
|
|
MimirAudioSampleFormat.Float32,
|
|
|
|
|
samples.Length,
|
|
|
|
|
1_000_000_000L)));
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-22 00:52:43 +01:00
|
|
|
internal sealed class DisposableConfiguration : IDisposable
|
|
|
|
|
{
|
|
|
|
|
private readonly List<IMimirStreamSource> ownedSources;
|
|
|
|
|
|
|
|
|
|
public DisposableConfiguration(MimirRuntimeConfiguration value)
|
|
|
|
|
{
|
|
|
|
|
Value = value;
|
|
|
|
|
ownedSources = value.Sources.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MimirRuntimeConfiguration Value { get; }
|
|
|
|
|
|
|
|
|
|
public void Detach(IMimirStreamSource source)
|
|
|
|
|
{
|
|
|
|
|
ownedSources.Remove(source);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
foreach (var source in ownedSources)
|
|
|
|
|
{
|
|
|
|
|
source.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|