Add Well OBS daemon

This commit is contained in:
Meta Vens 2026-06-02 16:18:15 +01:00
commit 7790b8acda
6 changed files with 557 additions and 0 deletions

View file

@ -7,6 +7,7 @@
<Project Path="src/Mimir.EveDashboard/Mimir.EveDashboard.csproj" />
<Project Path="src/Mimir.EveSensorReceiver/Mimir.EveSensorReceiver.csproj" />
<Project Path="src/Mimir.Well/Mimir.Well.csproj" />
<Project Path="src/Mimir.WellObsDaemon/Mimir.WellObsDaemon.csproj" />
<Project Path="src/Mimir.VerseRecorder/Mimir.VerseRecorder.csproj" />
<Project Path="src/Mimir.PsMoveProbe/Mimir.PsMoveProbe.csproj" />
<Project Path="src/Mimir.WasapiLoopback/Mimir.WasapiLoopback.csproj" />

View file

@ -0,0 +1,74 @@
param(
[string]$WellLog = "",
[string]$MapName = "Local\MimirObsStemBus",
[string]$CompositeStemId = "well_composite",
[int]$PollMs = 25,
[switch]$FromStart
)
$ErrorActionPreference = "Stop"
$repo = Split-Path -Parent $PSScriptRoot
if ([string]::IsNullOrWhiteSpace($WellLog)) {
$runtime = Join-Path $repo "artifacts\runtime"
$latest = Get-ChildItem -Path $runtime -Filter "verse-relay.out.log" -Recurse -ErrorAction SilentlyContinue |
Sort-Object LastWriteTimeUtc -Descending |
Select-Object -First 1
if (-not $latest) {
throw "No verse-relay.out.log found under $runtime. Pass -WellLog explicitly."
}
$WellLog = $latest.FullName
}
$runDir = Join-Path $repo ("artifacts\runtime\well-obs-daemon-" + (Get-Date -Format "yyyyMMdd-HHmmss"))
New-Item -ItemType Directory -Force -Path $runDir | Out-Null
$stdout = Join-Path $runDir "well-obs-daemon.out.log"
$stderr = Join-Path $runDir "well-obs-daemon.err.log"
$daemonExe = Join-Path $repo "src\Mimir.WellObsDaemon\bin\Debug\net10.0-windows\Mimir.WellObsDaemon.exe"
if (-not (Test-Path $daemonExe)) {
dotnet build (Join-Path $repo "src\Mimir.WellObsDaemon\Mimir.WellObsDaemon.csproj")
}
$daemonArgs = @(
"--well-log",
$WellLog,
"--map",
$MapName,
"--composite-stem-id",
$CompositeStemId,
"--poll-ms",
"$PollMs"
)
if ($FromStart) {
$daemonArgs += "--from-start"
}
$process = Start-Process `
-FilePath $daemonExe `
-ArgumentList $daemonArgs `
-WorkingDirectory $repo `
-WindowStyle Hidden `
-RedirectStandardOutput $stdout `
-RedirectStandardError $stderr `
-PassThru
$manifest = [ordered]@{
document = "mimir.well_obs_daemon.supervisor.v1"
pid = $process.Id
wellLog = $WellLog
mapName = $MapName
compositeStemId = $CompositeStemId
stdout = $stdout
stderr = $stderr
startedAt = (Get-Date).ToUniversalTime().ToString("O")
}
$manifestPath = Join-Path $runDir "supervisor.json"
$manifest | ConvertTo-Json -Depth 6 | Set-Content -Path $manifestPath -Encoding UTF8
Write-Output "Mimir Well OBS daemon PID $($process.Id)"
Write-Output "Supervisor manifest: $manifestPath"
Write-Output "Requires Well capture pages with inline audio bodies enabled."
Write-Output "Poll log: Get-Content -Tail 20 '$stderr'"

View file

@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0-windows</TargetFramework>
<AssemblyName>Mimir.WellObsDaemon</AssemblyName>
<RootNamespace>Mimir.WellObsDaemon</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Mimir.Runtime\Mimir.Runtime.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,454 @@
using System.Buffers.Binary;
using System.Globalization;
using System.Text;
using System.Text.Json;
using Mimir.Runtime.Synchronization;
var options = WellObsOptions.Parse(args);
if (string.IsNullOrWhiteSpace(options.WellLogPath))
{
Console.Error.WriteLine("Mimir.WellObsDaemon requires --well-log <path> for this cut.");
return 2;
}
using var stopping = new CancellationTokenSource();
Console.CancelKeyPress += (_, eventArgs) =>
{
eventArgs.Cancel = true;
stopping.Cancel();
};
using var publisher = new MimirObsStemSharedMemoryPublisher(options.MapName);
var state = new WellLogTailState(options.WellLogPath, options.FromStart);
var lastPublishedCaptureSequence = -1L;
var lastStatus = DateTimeOffset.MinValue;
Console.Error.WriteLine($"Mimir Well OBS daemon drinking {options.WellLogPath}");
Console.Error.WriteLine($"Mimir Well OBS daemon publishing stems to {options.MapName}");
while (!stopping.IsCancellationRequested)
{
foreach (var document in state.ReadNewDocuments())
{
using var _ = document;
var publication = WellObsPublication.TryBuild(document, options);
if (publication == null || publication.CaptureSequence <= lastPublishedCaptureSequence)
{
continue;
}
lastPublishedCaptureSequence = publication.CaptureSequence;
publisher.Publish(publication.Snapshot);
Console.Error.WriteLine(
$"mimir-well-obs published capture={publication.CaptureSequence} stems={publication.Snapshot.ReadyStems.Count} compositeFrames={publication.CompositeFrameCount} sources={string.Join(",", publication.SourceIds)}");
if (options.Once)
{
return 0;
}
}
if (DateTimeOffset.UtcNow - lastStatus >= TimeSpan.FromSeconds(options.StatusSeconds))
{
lastStatus = DateTimeOffset.UtcNow;
Console.Error.WriteLine($"mimir-well-obs status lastCapture={lastPublishedCaptureSequence} offset={state.Position}");
}
try
{
await Task.Delay(options.PollMs, stopping.Token).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
break;
}
}
return 0;
internal sealed record WellObsOptions(
string WellLogPath,
string MapName,
string CompositeStemId,
string PerSourceStemPrefix,
int PollMs,
int StatusSeconds,
bool FromStart,
bool Once)
{
public static WellObsOptions Parse(IReadOnlyList<string> args) => new(
ParseString(args, "--well-log", ""),
ParseString(args, "--map", MimirObsStemSharedMemoryPublisher.DefaultMapName),
ParseString(args, "--composite-stem-id", "well_composite"),
ParseString(args, "--source-stem-prefix", "well_"),
Math.Max(5, ParseInt(args, "--poll-ms", 50)),
Math.Max(1, ParseInt(args, "--status-seconds", 5)),
ParseBool(args, "--from-start", false),
ParseBool(args, "--once", false));
private static string ParseString(IReadOnlyList<string> args, string name, string fallback)
{
for (var index = 0; index < args.Count; index++)
{
if (string.Equals(args[index], name, StringComparison.OrdinalIgnoreCase) && index + 1 < args.Count)
{
return args[index + 1];
}
if (args[index].StartsWith(name + "=", StringComparison.OrdinalIgnoreCase))
{
return args[index][(name.Length + 1)..];
}
}
return fallback;
}
private static int ParseInt(IReadOnlyList<string> args, string name, int fallback) =>
int.TryParse(ParseString(args, name, ""), NumberStyles.Integer, CultureInfo.InvariantCulture, out var value) ? value : fallback;
private static bool ParseBool(IReadOnlyList<string> args, string name, bool fallback)
{
for (var index = 0; index < args.Count; index++)
{
if (!string.Equals(args[index], name, StringComparison.OrdinalIgnoreCase))
{
continue;
}
if (index + 1 >= args.Count || args[index + 1].StartsWith("--", StringComparison.Ordinal))
{
return true;
}
return bool.TryParse(args[index + 1], out var value) ? value : fallback;
}
return fallback;
}
}
internal sealed class WellLogTailState
{
private readonly string path;
private long position;
public WellLogTailState(string path, bool fromStart)
{
this.path = path;
position = !fromStart && File.Exists(path) ? new FileInfo(path).Length : 0;
}
public long Position => position;
public IEnumerable<JsonDocument> ReadNewDocuments()
{
if (!File.Exists(path))
{
yield break;
}
using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
if (position > stream.Length)
{
position = 0;
}
stream.Seek(position, SeekOrigin.Begin);
using var reader = new StreamReader(stream, Encoding.UTF8);
string? line;
while ((line = reader.ReadLine()) != null)
{
if (string.IsNullOrWhiteSpace(line))
{
continue;
}
JsonDocument? parsed = null;
try
{
parsed = JsonDocument.Parse(line);
}
catch (JsonException)
{
}
if (parsed != null)
{
yield return parsed;
}
}
position = stream.Position;
}
}
internal sealed record WellObsPublication(
long CaptureSequence,
int CompositeFrameCount,
IReadOnlyList<string> SourceIds,
MimirObsStemPublicationSnapshot Snapshot)
{
public static WellObsPublication? TryBuild(JsonDocument document, WellObsOptions options)
{
var root = document.RootElement;
var kind = JsonText(root, "document");
if (kind is not "mimir.well_capture_page.v1")
{
return null;
}
var captureSequence = (long)JsonNumber(root, "captureSequence");
var feeds = ParseAudioFeeds(root).ToArray();
var samples = ParseAudioSamples(root).ToArray();
if (feeds.Length == 0 || samples.Length == 0)
{
return null;
}
var selectedFeeds = SelectFeeds(feeds);
var selectedSamples = selectedFeeds
.Select(feed => (Feed: feed, Sample: samples.LastOrDefault(sample => string.Equals(sample.SourceId, feed.SourceId, StringComparison.Ordinal))))
.Where(pair => pair.Sample != null)
.Select(pair => (pair.Feed, Sample: pair.Sample!))
.ToArray();
if (selectedSamples.Length == 0)
{
return null;
}
var sampleRate = selectedSamples[0].Sample.SampleRate;
selectedSamples = selectedSamples
.Where(pair => pair.Sample.SampleRate == sampleRate)
.ToArray();
if (selectedSamples.Length == 0)
{
return null;
}
var frameCount = selectedSamples.Min(pair => pair.Sample.Samples.Length);
if (frameCount <= 0)
{
return null;
}
var stems = new List<MimirObsPublishedAudioStem>();
var mix = new float[frameCount];
var mixWeight = 0.0;
var sequence = 0L;
foreach (var (feed, sample) in selectedSamples)
{
var sourceSamples = sample.Samples.AsSpan(0, frameCount).ToArray();
var gain = Math.Clamp(feed.Gain, 0.0, 8.0);
for (var index = 0; index < sourceSamples.Length; index++)
{
sourceSamples[index] = (float)Math.Clamp(sourceSamples[index] * gain, -1.0, 1.0);
mix[index] += sourceSamples[index];
}
mixWeight += Math.Max(0.000001, gain);
sequence = Math.Max(sequence, sample.Sequence);
stems.Add(new MimirObsPublishedAudioStem(
options.PerSourceStemPrefix + StableStemId(sample.SourceId),
feed.DisplayName,
sample.SourceId,
0,
frameCount,
sampleRate,
sample.Sequence,
Configured: true,
sourceSamples));
}
if (mixWeight > 0.0)
{
for (var index = 0; index < mix.Length; index++)
{
mix[index] = (float)Math.Clamp(mix[index] / mixWeight, -1.0, 1.0);
}
}
stems.Insert(0, new MimirObsPublishedAudioStem(
options.CompositeStemId,
"Well configured composite",
string.Join("+", selectedSamples.Select(static pair => pair.Sample.SourceId)),
0,
frameCount,
sampleRate,
sequence,
Configured: true,
mix));
return new WellObsPublication(
captureSequence,
frameCount,
selectedSamples.Select(static pair => pair.Sample.SourceId).ToArray(),
new MimirObsStemPublicationSnapshot(
"well-tightest-configured-composite",
MimirObsAudioPublicationKind.FaustStemBus,
stems,
[],
[],
sequence));
}
private static IEnumerable<WellAudioFeed> ParseAudioFeeds(JsonElement root)
{
var audio = JsonArray(JsonGet(JsonGet(root, "configuredComposite"), "audio"));
foreach (var item in audio)
{
yield return new WellAudioFeed(
JsonText(item, "SourceId", "sourceId") ?? "unknown",
JsonText(item, "DisplayName", "displayName") ?? JsonText(item, "SourceId", "sourceId") ?? "unknown",
JsonBool(item, "Muted", "muted"),
JsonBool(item, "Solo", "solo"),
JsonNumber(item, "Gain", "gain", fallback: 1.0));
}
}
private static IEnumerable<WellAudioSample> ParseAudioSamples(JsonElement root)
{
foreach (var item in JsonArray(JsonGet(root, "samples")))
{
var slice = JsonGet(item, "slice");
var sample = JsonGet(item, "sample");
if (!string.Equals(JsonText(slice, "Kind", "kind"), "Audio", StringComparison.OrdinalIgnoreCase))
{
continue;
}
if (!string.Equals(JsonText(slice, "Status", "status"), "Ready", StringComparison.OrdinalIgnoreCase))
{
continue;
}
var audio = JsonGet(sample, "audio");
var body = JsonGet(item, "body");
if (!string.Equals(JsonText(body, "status"), "inline", StringComparison.OrdinalIgnoreCase) ||
!string.Equals(JsonText(body, "encoding"), "base64", StringComparison.OrdinalIgnoreCase))
{
continue;
}
var bytes = Convert.FromBase64String(JsonText(body, "data") ?? "");
var sourceId = JsonText(sample, "SourceId", "sourceId") ?? JsonText(slice, "SourceId", "sourceId") ?? "unknown";
var sampleRate = (int)JsonNumber(audio, "SampleRate", "sampleRate");
var channels = Math.Max(1, (int)JsonNumber(audio, "Channels", "channels", fallback: 1.0));
var format = JsonText(audio, "SampleFormat", "sampleFormat") ?? "Float32";
var decoded = DecodeMono(bytes, format, channels);
if (sampleRate > 0 && decoded.Length > 0)
{
yield return new WellAudioSample(
sourceId,
sampleRate,
(long)JsonNumber(sample, "Sequence", "sequence"),
decoded);
}
}
}
private static IReadOnlyList<WellAudioFeed> SelectFeeds(IReadOnlyList<WellAudioFeed> feeds)
{
var unmuted = feeds.Where(static feed => !feed.Muted).ToArray();
var solo = unmuted.Where(static feed => feed.Solo).ToArray();
return solo.Length > 0 ? solo : unmuted;
}
private static float[] DecodeMono(byte[] bytes, string format, int channels)
{
var sampleSize = format switch
{
"Float32" => 4,
"Int32" or "Int32LSB" => 4,
"Int16" => 2,
_ => 0,
};
if (sampleSize == 0)
{
return [];
}
var frameCount = bytes.Length / sampleSize / Math.Max(1, channels);
var output = new float[frameCount];
for (var frame = 0; frame < frameCount; frame++)
{
var sum = 0.0;
for (var channel = 0; channel < channels; channel++)
{
var offset = (frame * channels + channel) * sampleSize;
sum += format switch
{
"Float32" => BitConverter.ToSingle(bytes, offset),
"Int32" or "Int32LSB" => BinaryPrimitives.ReadInt32LittleEndian(bytes.AsSpan(offset, 4)) / 2147483648.0,
"Int16" => BinaryPrimitives.ReadInt16LittleEndian(bytes.AsSpan(offset, 2)) / 32768.0,
_ => 0.0,
};
}
output[frame] = (float)Math.Clamp(sum / channels, -1.0, 1.0);
}
return output;
}
private static string StableStemId(string sourceId)
{
var builder = new StringBuilder(sourceId.Length);
foreach (var ch in sourceId)
{
builder.Append(char.IsLetterOrDigit(ch) ? char.ToLowerInvariant(ch) : '_');
}
return builder.ToString().Trim('_');
}
private static JsonElement JsonGet(JsonElement element, params string[] names)
{
if (element.ValueKind != JsonValueKind.Object)
{
return default;
}
foreach (var name in names)
{
if (element.TryGetProperty(name, out var value))
{
return value;
}
}
return default;
}
private static IEnumerable<JsonElement> JsonArray(JsonElement element) =>
element.ValueKind == JsonValueKind.Array ? element.EnumerateArray() : [];
private static string? JsonText(JsonElement element, params string[] names)
{
var value = names.Length == 0 ? element : JsonGet(element, names);
return value.ValueKind == JsonValueKind.String ? value.GetString() : null;
}
private static double JsonNumber(JsonElement element, params string[] names) =>
JsonNumber(element, names, 0.0);
private static double JsonNumber(JsonElement element, string name1, string name2, double fallback) =>
JsonNumber(element, [name1, name2], fallback);
private static double JsonNumber(JsonElement element, string name, double fallback) =>
JsonNumber(element, [name], fallback);
private static double JsonNumber(JsonElement element, string[] names, double fallback)
{
var value = names.Length == 0 ? element : JsonGet(element, names);
return value.ValueKind == JsonValueKind.Number && value.TryGetDouble(out var number) ? number : fallback;
}
private static bool JsonBool(JsonElement element, params string[] names)
{
var value = JsonGet(element, names);
return value.ValueKind == JsonValueKind.True || (value.ValueKind == JsonValueKind.String && bool.TryParse(value.GetString(), out var parsed) && parsed);
}
}
internal sealed record WellAudioFeed(string SourceId, string DisplayName, bool Muted, bool Solo, double Gain);
internal sealed record WellAudioSample(string SourceId, int SampleRate, long Sequence, float[] Samples);

View file

@ -357,3 +357,4 @@
{"ts": "2026-06-02T15:12:44+01:00", "type": "verification", "status": "ok", "note": "Adaptive Well/runtime dashboard cut builds clean: Mimir.Runtime, Mimir.Well, and Mimir.EveDashboard compile after canonical clock mapping, ASIO multiplex drain, adaptive latency telemetry, feature-signal mirror, and richer Eve dashboard lowering. Rebooted lightweight Eve broker/Odin projection on 2026-06-02; health ok at 127.0.0.1:8795 and 8797 without restarting heavy Well capture."}
{"ts": "2026-06-02T15:15:52+01:00", "type": "verification", "status": "ok", "note": "Fixed EveDashboard Well sparsity regression: MimirLiveStatsSnapshot now searches telemetry lines as well as observation lines for mimir.well_snapshot.v1. Nightwing tmux pane gamecult-visible-ops:ops.1 then lowered Well 8/8 sources, seq 108, frame ready 6/6, poll/publish pressure, buffers, and clock-domain rows from the saved Well relay."}
{"ts": "2026-06-02T15:18:04+01:00", "type": "verification", "status": "ok", "note": "Fixed EveDashboard Move Score sparsity regression: fallback trace discovery now scans the tail of the newest move online-sync.jsonl for the latest parseable live_score/score_gesture frame instead of trusting the last line. Nightwing TUI then rendered Move Score with bpm 119, F# major/F# chord, source score summaries, and active voices from the saved move-sync trace."}
{"ts": "2026-06-02T16:14:03+01:00", "type": "implementation", "status": "ok", "note": "Added Mimir.WellObsDaemon and scripts/start-well-obs-daemon.ps1. The daemon tails Well capture pages with inline audio bodies, applies configuredComposite audio mute/solo/gain selection, publishes well_composite plus per-source well_<source> stems into the existing Local\\\\MimirObsStemBus OBS plugin ABI, and refuses metadata-only bodies. Smoke fixture published well_composite/well_asio_ch0/well_asio_ch1 into a test map with expected Float32 samples; launcher smoke recorded real daemon PID and supervisor manifest."}

View file

@ -679,6 +679,17 @@ accepted_design:
through the native plugin, raw feeds remain debug inputs, and the ledger
records the live texture, texture ring, producer-fence, and consumer-fence
proofs.
`Mimir.WellObsDaemon` is the first Well-to-OBS daemon for the tightest
configured composite. It tails a Well relay/log, reads synchronized
`mimir.well_capture_page.v1` pages with inline audio bodies, applies the
page's configured audio mute/solo/gain selection, publishes a mono
`well_composite` stem plus per-source `well_<source>` stems into the
existing `Local\MimirObsStemBus` ABI, and refuses to invent audio when the
Well only emitted metadata-only bodies. `scripts/start-well-obs-daemon.ps1`
starts it detached with a supervisor manifest and real daemon PID. Video
remains owned by the existing Fensalir shared-texture path; a future cut can
add a Well/Fensalir texture producer, but this daemon does not create a
second OBS video authority.
`MimirSynchronizedBufferPlanner` is the first aligned-buffer readout
primitive: it chooses one canonical presentation time inside the rolling
window and returns per-stream slices for local cameras, network display