Scaffold OBS audio stem source
This commit is contained in:
parent
0c39921f96
commit
71234f04ce
12 changed files with 522 additions and 9 deletions
|
|
@ -79,6 +79,11 @@ if (args.Any(arg => string.Equals(arg, "--audio-stem-publication-smoke", StringC
|
|||
return RunAudioStemPublicationSmoke();
|
||||
}
|
||||
|
||||
if (args.Any(arg => string.Equals(arg, "--obs-stem-shared-memory-smoke", StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
return RunObsStemSharedMemorySmoke();
|
||||
}
|
||||
|
||||
if (args.Any(arg => string.Equals(arg, "--complex-contour-tracker-self-test", StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
return RunComplexContourTrackerSelfTest(
|
||||
|
|
@ -1917,6 +1922,49 @@ static int RunAudioStemPublicationSmoke()
|
|||
: 1;
|
||||
}
|
||||
|
||||
static int RunObsStemSharedMemorySmoke()
|
||||
{
|
||||
if (!OperatingSystem.IsWindows())
|
||||
{
|
||||
Console.WriteLine("obs-stem-shared-memory-smoke skipped=non-windows");
|
||||
return 0;
|
||||
}
|
||||
|
||||
var mapName = $"Local\\MimirObsStemBusSmoke{Environment.ProcessId}";
|
||||
var publication = new MimirObsStemPublicationState(MimirObsPublicationConfigurations.AlignmentActuatorStemBus);
|
||||
publication.Consume(new AquariumAudioStemFrame(
|
||||
MimirAlignmentActuatorProfile.SixSourceFaust.Id,
|
||||
[new AquariumAudioStemChannel(0, "aligned_source_0", "Aligned source 0", "mic-0", [0.125f, -0.25f, 0.5f])],
|
||||
FrameCount: 3,
|
||||
SampleRate: 48_000,
|
||||
Sequence: 11));
|
||||
|
||||
using var publisher = new MimirObsStemSharedMemoryPublisher(mapName);
|
||||
publisher.Publish(publication.Capture());
|
||||
using var file = System.IO.MemoryMappedFiles.MemoryMappedFile.OpenExisting(mapName, System.IO.MemoryMappedFiles.MemoryMappedFileRights.Read);
|
||||
using var view = file.CreateViewAccessor(0, MimirObsStemSharedMemoryPublisher.MapBytes, System.IO.MemoryMappedFiles.MemoryMappedFileAccess.Read);
|
||||
var magic = view.ReadUInt64(0);
|
||||
var generation = view.ReadUInt32(12);
|
||||
var stemCount = view.ReadInt32(16);
|
||||
var sampleRate = view.ReadInt32(64 + 8);
|
||||
var frameCount = view.ReadInt32(64 + 12);
|
||||
var sampleOffset = view.ReadInt32(64 + 16);
|
||||
var firstSample = view.ReadSingle(sampleOffset);
|
||||
|
||||
Console.WriteLine(
|
||||
$"obs-stem-shared-memory-smoke stems={stemCount} generation={generation} sampleRate={sampleRate} frames={frameCount} first={firstSample:0.000}");
|
||||
|
||||
return magic == 0x4D545352494D494DUL &&
|
||||
generation > 0 &&
|
||||
(generation & 1u) == 0 &&
|
||||
stemCount == 1 &&
|
||||
sampleRate == 48_000 &&
|
||||
frameCount == 3 &&
|
||||
Math.Abs(firstSample - 0.125f) < 0.0001f
|
||||
? 0
|
||||
: 1;
|
||||
}
|
||||
|
||||
static MimirAudioSynchronizationState NewSyncState(string sourceId, double delaySamples, double confidence) =>
|
||||
new(
|
||||
"loopback-scarlett-speakers",
|
||||
|
|
|
|||
|
|
@ -41,6 +41,7 @@ public sealed class MimirRuntime : IAquariumRuntime, IAquariumRuntimeServicesRec
|
|||
private readonly MimirAudioSpectrumAnalyzer spectrumAnalyzer;
|
||||
private readonly MimirAlignmentActuatorBank audioActuatorBank = new();
|
||||
private readonly MimirObsStemPublicationState obsStemPublication = new(MimirObsPublicationConfigurations.AlignmentActuatorStemBus);
|
||||
private readonly MimirObsStemSharedMemoryPublisher? obsStemPublisher;
|
||||
private readonly IReadOnlyList<MimirStreamSourceFactory> sourceFactories;
|
||||
private readonly AquariumUiDocument ui;
|
||||
private readonly AquariumAudioDocument audio = new();
|
||||
|
|
@ -130,6 +131,7 @@ public sealed class MimirRuntime : IAquariumRuntime, IAquariumRuntimeServicesRec
|
|||
calibrationGain = settings.Audio.CalibrationGain;
|
||||
watermarkGain = settings.Audio.WatermarkGain;
|
||||
syntheticSpectrumPreview = IsTruthy(Environment.GetEnvironmentVariable("MIMIR_SYNTHETIC_SPECTRUM_PREVIEW"));
|
||||
obsStemPublisher = CreateObsStemPublisher();
|
||||
complexContourWitness = settings.Audio.EnableComplexContourRuntime
|
||||
? new MimirBioacousticContestantRenderer(
|
||||
MimirBioacousticContestants.BuiltIn.FirstOrDefault(profile =>
|
||||
|
|
@ -689,6 +691,7 @@ public sealed class MimirRuntime : IAquariumRuntime, IAquariumRuntimeServicesRec
|
|||
|
||||
public void Dispose()
|
||||
{
|
||||
obsStemPublisher?.Dispose();
|
||||
synchronization.Dispose();
|
||||
}
|
||||
|
||||
|
|
@ -1051,6 +1054,7 @@ public sealed class MimirRuntime : IAquariumRuntime, IAquariumRuntimeServicesRec
|
|||
if (consumed)
|
||||
{
|
||||
lastObsStemPublication = obsStemPublication.Capture();
|
||||
obsStemPublisher?.Publish(lastObsStemPublication);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1169,6 +1173,27 @@ public sealed class MimirRuntime : IAquariumRuntime, IAquariumRuntimeServicesRec
|
|||
: 0.0f;
|
||||
}
|
||||
|
||||
private static MimirObsStemSharedMemoryPublisher? CreateObsStemPublisher()
|
||||
{
|
||||
if (!OperatingSystem.IsWindows())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var mapName = Environment.GetEnvironmentVariable("MIMIR_OBS_STEM_MAP");
|
||||
try
|
||||
{
|
||||
return new MimirObsStemSharedMemoryPublisher(string.IsNullOrWhiteSpace(mapName)
|
||||
? MimirObsStemSharedMemoryPublisher.DefaultMapName
|
||||
: mapName);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Mimir OBS stem shared-memory publisher disabled: {ex.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static string? ResolveRuntimeAssetPath(string relativePath)
|
||||
{
|
||||
if (File.Exists(relativePath))
|
||||
|
|
|
|||
|
|
@ -43,7 +43,8 @@ public sealed record MimirObsPublishedAudioStem(
|
|||
int FrameCount,
|
||||
int SampleRate,
|
||||
long Sequence,
|
||||
bool Configured);
|
||||
bool Configured,
|
||||
float[] Samples);
|
||||
|
||||
public sealed record MimirObsStemPublicationSnapshot(
|
||||
string ConfigurationId,
|
||||
|
|
@ -80,7 +81,8 @@ public sealed class MimirObsStemPublicationState(MimirObsPublicationConfiguratio
|
|||
frame.FrameCount,
|
||||
frame.SampleRate,
|
||||
frame.Sequence,
|
||||
isConfigured);
|
||||
isConfigured,
|
||||
channel.Samples);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
113
src/Mimir.Runtime/Synchronization/MimirObsStemSharedMemory.cs
Normal file
113
src/Mimir.Runtime/Synchronization/MimirObsStemSharedMemory.cs
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
using System.IO.MemoryMappedFiles;
|
||||
using System.Runtime.Versioning;
|
||||
using System.Text;
|
||||
|
||||
namespace Mimir.Runtime.Synchronization;
|
||||
|
||||
public sealed class MimirObsStemSharedMemoryPublisher : IDisposable
|
||||
{
|
||||
public const string DefaultMapName = "Local\\MimirObsStemBus";
|
||||
public const int MaxStems = 8;
|
||||
public const int MaxFramesPerStem = 4096;
|
||||
public const int StemIdBytes = 64;
|
||||
public const int DisplayNameBytes = 64;
|
||||
public const int SourceIdBytes = 64;
|
||||
public const int HeaderBytes = 64;
|
||||
public const int StemRecordBytes = 224;
|
||||
public const int MapBytes = HeaderBytes + (MaxStems * StemRecordBytes) + (MaxStems * MaxFramesPerStem * sizeof(float));
|
||||
private const ulong Magic = 0x4D545352494D494DUL;
|
||||
private const int Version = 1;
|
||||
|
||||
private readonly MemoryMappedFile file;
|
||||
private readonly MemoryMappedViewAccessor view;
|
||||
private uint generation;
|
||||
private bool disposed;
|
||||
|
||||
[SupportedOSPlatform("windows")]
|
||||
public MimirObsStemSharedMemoryPublisher(string mapName = DefaultMapName)
|
||||
{
|
||||
file = MemoryMappedFile.CreateOrOpen(mapName, MapBytes, MemoryMappedFileAccess.ReadWrite);
|
||||
view = file.CreateViewAccessor(0, MapBytes, MemoryMappedFileAccess.ReadWrite);
|
||||
view.Write(0, Magic);
|
||||
view.Write(8, Version);
|
||||
view.Write(20, MaxStems);
|
||||
view.Write(24, MaxFramesPerStem);
|
||||
view.Write(28, HeaderBytes);
|
||||
view.Write(32, StemRecordBytes);
|
||||
view.Write(36, MapBytes);
|
||||
}
|
||||
|
||||
public void Publish(MimirObsStemPublicationSnapshot snapshot)
|
||||
{
|
||||
if (disposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var stems = snapshot.ReadyStems
|
||||
.Where(stem => stem.FrameCount > 0 && stem.SampleRate > 0)
|
||||
.OrderBy(stem => stem.StemId, StringComparer.Ordinal)
|
||||
.Take(MaxStems)
|
||||
.ToArray();
|
||||
var beginGeneration = unchecked(++generation) | 1u;
|
||||
view.Write(12, beginGeneration);
|
||||
view.Write(16, stems.Length);
|
||||
var sampleOffset = HeaderBytes + (MaxStems * StemRecordBytes);
|
||||
for (var index = 0; index < MaxStems; index++)
|
||||
{
|
||||
var recordOffset = HeaderBytes + (index * StemRecordBytes);
|
||||
if (index >= stems.Length)
|
||||
{
|
||||
ClearRecord(recordOffset);
|
||||
continue;
|
||||
}
|
||||
|
||||
var stem = stems[index];
|
||||
var frameCount = Math.Min(stem.FrameCount, MaxFramesPerStem);
|
||||
WriteRecord(recordOffset, stem, sampleOffset, frameCount);
|
||||
view.WriteArray(sampleOffset, stem.Samples, 0, Math.Min(frameCount, stem.Samples.Length));
|
||||
sampleOffset += MaxFramesPerStem * sizeof(float);
|
||||
}
|
||||
|
||||
view.Write(12, unchecked(beginGeneration + 1u));
|
||||
}
|
||||
|
||||
private void ClearRecord(int offset)
|
||||
{
|
||||
view.Write(offset, -1);
|
||||
view.Write(offset + 4, 0);
|
||||
view.Write(offset + 8, 0);
|
||||
view.Write(offset + 12, 0);
|
||||
view.Write(offset + 16, 0);
|
||||
WriteFixedString(offset + 32, StemIdBytes, "");
|
||||
WriteFixedString(offset + 96, DisplayNameBytes, "");
|
||||
WriteFixedString(offset + 160, SourceIdBytes, "");
|
||||
}
|
||||
|
||||
private void WriteRecord(int offset, MimirObsPublishedAudioStem stem, int sampleOffset, int frameCount)
|
||||
{
|
||||
view.Write(offset, stem.ChannelIndex);
|
||||
view.Write(offset + 4, stem.Configured ? 1 : 0);
|
||||
view.Write(offset + 8, stem.SampleRate);
|
||||
view.Write(offset + 12, frameCount);
|
||||
view.Write(offset + 16, sampleOffset);
|
||||
WriteFixedString(offset + 32, StemIdBytes, stem.StemId);
|
||||
WriteFixedString(offset + 96, DisplayNameBytes, stem.DisplayName);
|
||||
WriteFixedString(offset + 160, SourceIdBytes, stem.SourceId);
|
||||
}
|
||||
|
||||
private void WriteFixedString(int offset, int byteCount, string value)
|
||||
{
|
||||
var buffer = new byte[byteCount];
|
||||
var bytes = Encoding.UTF8.GetBytes(value);
|
||||
Array.Copy(bytes, buffer, Math.Min(bytes.Length, byteCount - 1));
|
||||
view.WriteArray(offset, buffer, 0, byteCount);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
disposed = true;
|
||||
view.Dispose();
|
||||
file.Dispose();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue