Add stream proof calibration profile
This commit is contained in:
parent
be787735c9
commit
1ea8100a7b
15 changed files with 1733 additions and 13 deletions
|
|
@ -0,0 +1,168 @@
|
|||
using System.Collections.Concurrent;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Mimir.Runtime.Synchronization;
|
||||
|
||||
public sealed record MimirFfmpegPcmAudioStreamSourceOptions(
|
||||
string Command,
|
||||
IReadOnlyList<string> Arguments,
|
||||
int SampleRate,
|
||||
int Channels,
|
||||
MimirAudioSampleFormat SampleFormat,
|
||||
int BlockFrames = 960);
|
||||
|
||||
public sealed class MimirFfmpegPcmAudioStreamSource : IMimirStreamSource
|
||||
{
|
||||
private readonly ConcurrentQueue<MimirStreamSample> samples = new();
|
||||
private readonly CancellationTokenSource cancellation = new();
|
||||
private readonly Process process;
|
||||
private readonly Task stdoutTask;
|
||||
private readonly Task stderrTask;
|
||||
private readonly int blockBytes;
|
||||
private ulong sequence;
|
||||
private bool disposed;
|
||||
|
||||
public MimirFfmpegPcmAudioStreamSource(
|
||||
MimirStreamDescriptor descriptor,
|
||||
MimirFfmpegPcmAudioStreamSourceOptions options)
|
||||
{
|
||||
if (descriptor.Kind != MimirStreamKind.Audio)
|
||||
{
|
||||
throw new ArgumentException("Raw FFmpeg PCM sources require an audio descriptor.", nameof(descriptor));
|
||||
}
|
||||
|
||||
if (options.SampleRate <= 0 || options.Channels <= 0 || options.BlockFrames <= 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(options), "Raw PCM audio sources require positive sample rate, channels, and block frame count.");
|
||||
}
|
||||
|
||||
var bytesPerSample = BytesPerSample(options.SampleFormat);
|
||||
if (bytesPerSample <= 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(options), "Raw PCM audio sources require a known sample format.");
|
||||
}
|
||||
|
||||
Descriptor = descriptor;
|
||||
Options = options;
|
||||
blockBytes = checked(options.BlockFrames * options.Channels * bytesPerSample);
|
||||
process = StartProcess(options);
|
||||
stdoutTask = Task.Run(() => ReadStdout(cancellation.Token));
|
||||
stderrTask = Task.Run(() => DrainStderr(cancellation.Token));
|
||||
}
|
||||
|
||||
public MimirStreamDescriptor Descriptor { get; }
|
||||
|
||||
public MimirFfmpegPcmAudioStreamSourceOptions Options { get; }
|
||||
|
||||
public bool TryRead(out MimirStreamSample sample)
|
||||
{
|
||||
return samples.TryDequeue(out sample);
|
||||
}
|
||||
|
||||
private static Process StartProcess(MimirFfmpegPcmAudioStreamSourceOptions options)
|
||||
{
|
||||
var startInfo = new ProcessStartInfo(options.Command)
|
||||
{
|
||||
UseShellExecute = false,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
CreateNoWindow = true,
|
||||
};
|
||||
|
||||
foreach (var argument in options.Arguments)
|
||||
{
|
||||
startInfo.ArgumentList.Add(argument);
|
||||
}
|
||||
|
||||
return Process.Start(startInfo)
|
||||
?? throw new InvalidOperationException($"Could not start FFmpeg raw PCM source process: {options.Command}");
|
||||
}
|
||||
|
||||
private async Task ReadStdout(CancellationToken token)
|
||||
{
|
||||
var stream = process.StandardOutput.BaseStream;
|
||||
while (!token.IsCancellationRequested)
|
||||
{
|
||||
var buffer = new byte[blockBytes];
|
||||
var offset = 0;
|
||||
while (offset < buffer.Length)
|
||||
{
|
||||
var read = await stream.ReadAsync(buffer.AsMemory(offset, buffer.Length - offset), token).ConfigureAwait(false);
|
||||
if (read <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
offset += read;
|
||||
}
|
||||
|
||||
var timestampNs = StopwatchTicksToNs(Stopwatch.GetTimestamp());
|
||||
samples.Enqueue(new MimirStreamSample(
|
||||
Descriptor.SourceId,
|
||||
Descriptor.Kind,
|
||||
Descriptor.Origin,
|
||||
timestampNs,
|
||||
timestampNs,
|
||||
sequence++,
|
||||
0,
|
||||
buffer.Length,
|
||||
buffer,
|
||||
AudioBlock: new MimirAudioBlockDescriptor(
|
||||
Options.SampleRate,
|
||||
Options.Channels,
|
||||
Options.SampleFormat,
|
||||
Options.BlockFrames,
|
||||
timestampNs)));
|
||||
}
|
||||
}
|
||||
|
||||
private async Task DrainStderr(CancellationToken token)
|
||||
{
|
||||
while (!token.IsCancellationRequested)
|
||||
{
|
||||
var line = await process.StandardError.ReadLineAsync(token).ConfigureAwait(false);
|
||||
if (line == null)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static int BytesPerSample(MimirAudioSampleFormat format) =>
|
||||
format switch
|
||||
{
|
||||
MimirAudioSampleFormat.Float32 or MimirAudioSampleFormat.Int32 => 4,
|
||||
MimirAudioSampleFormat.Int24 => 3,
|
||||
MimirAudioSampleFormat.Int16 => 2,
|
||||
_ => 0,
|
||||
};
|
||||
|
||||
private static long StopwatchTicksToNs(long ticks)
|
||||
{
|
||||
return checked((long)(ticks * (1_000_000_000.0 / Stopwatch.Frequency)));
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (disposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
disposed = true;
|
||||
cancellation.Cancel();
|
||||
try
|
||||
{
|
||||
if (!process.HasExited)
|
||||
{
|
||||
process.Kill(entireProcessTree: true);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
|
||||
process.Dispose();
|
||||
cancellation.Dispose();
|
||||
}
|
||||
}
|
||||
|
|
@ -255,6 +255,25 @@ public sealed class MimirRuntimeConfiguration
|
|||
stream.StrideBytes)));
|
||||
}
|
||||
|
||||
if (string.Equals(stream.Adapter, "ffmpeg-pcmaudio", StringComparison.OrdinalIgnoreCase)
|
||||
|| string.Equals(stream.Adapter, "pcmaudio-process", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(stream.Command))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new MimirStreamSourceFactory(descriptor, () => new MimirFfmpegPcmAudioStreamSource(
|
||||
descriptor,
|
||||
new MimirFfmpegPcmAudioStreamSourceOptions(
|
||||
ResolveCommand(stream.Command, configDirectory),
|
||||
stream.Arguments,
|
||||
stream.SampleRate,
|
||||
stream.Channels,
|
||||
ParseAudioSampleFormat(stream.SampleFormat),
|
||||
stream.BlockFrames)));
|
||||
}
|
||||
|
||||
if (!string.Equals(stream.Adapter, "process", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return null;
|
||||
|
|
@ -324,6 +343,20 @@ public sealed class MimirRuntimeConfiguration
|
|||
_ => MimirVideoPixelFormat.Unknown,
|
||||
};
|
||||
}
|
||||
|
||||
private static MimirAudioSampleFormat ParseAudioSampleFormat(string value)
|
||||
{
|
||||
return string.IsNullOrWhiteSpace(value)
|
||||
? MimirAudioSampleFormat.Unknown
|
||||
: value.Trim().ToUpperInvariant() switch
|
||||
{
|
||||
"FLOAT32" or "F32" or "IEEE_FLOAT" => MimirAudioSampleFormat.Float32,
|
||||
"INT16" or "PCM16" or "S16" => MimirAudioSampleFormat.Int16,
|
||||
"INT24" or "PCM24" or "S24" => MimirAudioSampleFormat.Int24,
|
||||
"INT32" or "PCM32" or "S32" => MimirAudioSampleFormat.Int32,
|
||||
_ => MimirAudioSampleFormat.Unknown,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public sealed record MimirStreamSourceFactory(
|
||||
|
|
@ -381,7 +414,7 @@ public sealed class MimirAudioSyncConfig
|
|||
? fallback.BioacousticWitnessProfileId
|
||||
: BioacousticWitnessProfileId.Trim(),
|
||||
EnableComplexContourRuntime = EnableComplexContourRuntime || fallback.EnableComplexContourRuntime,
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
private static long StopwatchTicksToNs(long ticks)
|
||||
|
|
@ -426,6 +459,12 @@ public sealed class MimirStreamConfig
|
|||
|
||||
public int SampleRate { get; set; } = 192_000;
|
||||
|
||||
public int Channels { get; set; } = 2;
|
||||
|
||||
public string SampleFormat { get; set; } = "Float32";
|
||||
|
||||
public int BlockFrames { get; set; } = 960;
|
||||
|
||||
public string DriverClsid { get; set; } = "";
|
||||
|
||||
public string PathNeedle { get; set; } = "";
|
||||
|
|
|
|||
Loading…
Reference in a new issue