2026-05-21 13:34:03 +01:00
using Aquarium.Engine ;
using Aquarium.Engine.Audio ;
using Aquarium.Engine.Input ;
using Aquarium.Engine.Render ;
using Aquarium.Engine.Ui ;
2026-05-25 20:34:14 +01:00
using CultMath ;
2026-05-21 13:34:03 +01:00
using Mimir.Runtime.Synchronization ;
2026-05-27 20:19:26 +01:00
using System.Buffers.Binary ;
2026-05-22 18:09:17 +01:00
using System.Diagnostics ;
2026-05-23 22:45:53 +01:00
using System.Numerics ;
2026-05-25 07:06:25 +01:00
using System.Text ;
2026-05-21 13:34:03 +01:00
namespace Mimir.Runtime ;
2026-05-27 13:29:48 +01:00
public sealed class MimirRuntime : IAquariumRuntime , IAquariumRuntimeServicesReceiver
2026-05-21 13:34:03 +01:00
{
2026-05-22 18:09:17 +01:00
private const float DefaultAudioSyncUpdateIntervalSeconds = 0.1f ;
2026-05-22 19:09:11 +01:00
private const double HybridPassiveConfidenceThreshold = 0.12 ;
2026-05-22 17:35:21 +01:00
private const double CalibrationStartSeconds = 0.5 ;
2026-05-25 07:06:25 +01:00
private const int CalibrationBatchSegments = 4 ;
2026-05-22 19:59:09 +01:00
private const int HybridWatermarkIntervalSegments = 4 ;
2026-05-25 07:06:25 +01:00
private const float DefaultSpectrumUpdateIntervalSeconds = 0.2f ;
2026-05-25 13:48:06 +01:00
private const int SpectrumHistoryWindowCount = 40 ;
2026-05-25 13:19:13 +01:00
private const float SpectrumChannelSeparation = 1.75f ;
2026-05-27 11:39:07 +01:00
private const float SpectrumWindowDepthSeparation = 0.1f ;
2026-05-25 13:19:13 +01:00
private const float SpectrumWidth = 12.0f ;
private const float SpectrumAmplitudeHeight = 1.1f ;
2026-05-25 20:34:14 +01:00
private const float SpectrumCameraDefaultDistanceMultiplier = 10.0f ;
2026-05-25 21:14:10 +01:00
private const float SpectrumCameraDefaultAngleDegrees = 25.0f ;
private const float SpectrumCameraMinimumAngleDegrees = 0.0f ;
private const float SpectrumCameraMaximumAngleDegrees = 45.0f ;
2026-05-25 20:34:14 +01:00
private const float SpectrumCameraFitPadding = 1.12f ;
private const float SpectrumFrustumMinimumNear = 0.01f ;
private const float SpectrumSplineTubePadding = 0.18f ;
2026-05-27 11:48:18 +01:00
private const int DefaultSpectrumSourceLaneCapacity = 8 ;
2026-05-26 23:51:13 +01:00
private const string SpectrumFieldResourceKey = "mimir:resource:spectrum:field-upload" ;
private const string SpectrumRampResourceKey = "aquarium:resource:ramp:blackbody" ;
2026-05-28 14:18:38 +01:00
private const string SpectrumRampTexturePath = @"E:\Projects\Aetheria\Assets\Resources\Gradients\blackbody.png" ;
2026-05-21 13:34:03 +01:00
private readonly MimirSynchronizationHub synchronization ;
2026-05-26 14:51:06 +01:00
private readonly MimirFensalirFieldLowering fieldLowering ;
2026-05-25 07:06:25 +01:00
private readonly MimirAudioSpectrumAnalyzer spectrumAnalyzer ;
2026-05-27 18:03:25 +01:00
private readonly MimirAlignmentActuatorBank audioActuatorBank = new ( ) ;
2026-05-27 20:34:09 +01:00
private readonly MimirObsStemPublicationState obsStemPublication = new ( MimirObsPublicationConfigurations . AlignmentActuatorStemBus ) ;
2026-05-27 20:55:48 +01:00
private readonly MimirObsStemSharedMemoryPublisher ? obsStemPublisher ;
2026-05-25 07:13:09 +01:00
private readonly IReadOnlyList < MimirStreamSourceFactory > sourceFactories ;
2026-05-21 13:34:03 +01:00
private readonly AquariumUiDocument ui ;
2026-05-23 22:45:53 +01:00
private readonly AquariumAudioDocument audio = new ( ) ;
2026-05-22 18:49:01 +01:00
private readonly MimirAudioSynchronizationSettings audioSyncSettings ;
2026-05-22 13:15:38 +01:00
private readonly float telemetryIntervalSeconds ;
2026-05-22 18:09:17 +01:00
private readonly float audioSyncUpdateIntervalSeconds ;
2026-05-25 07:06:25 +01:00
private readonly float spectrumUpdateIntervalSeconds ;
2026-05-27 11:42:03 +01:00
private readonly int spectrumTubeSubdivisions ;
2026-05-27 11:48:18 +01:00
private readonly int spectrumSourceLaneCapacity ;
2026-05-27 23:34:44 +01:00
private readonly bool obsProofVisualEnabled ;
2026-05-22 17:35:21 +01:00
private readonly float calibrationGain ;
2026-05-22 19:59:09 +01:00
private readonly float watermarkGain ;
2026-05-25 18:26:42 +01:00
private readonly bool syntheticSpectrumPreview ;
2026-05-28 14:01:54 +01:00
private readonly bool syntheticSingleTubePreview ;
2026-05-25 06:28:20 +01:00
private readonly MimirBioacousticContestantRenderer ? complexContourWitness ;
2026-05-21 13:34:03 +01:00
private int lastPollCount ;
2026-05-22 11:40:56 +01:00
private float runtimeSeconds ;
2026-05-22 18:09:17 +01:00
private float nextAudioSyncSeconds ;
2026-05-25 07:06:25 +01:00
private float nextSpectrumSeconds ;
2026-05-22 13:15:38 +01:00
private float nextTelemetrySeconds ;
2026-05-25 07:06:25 +01:00
private bool sceneReady ;
2026-05-22 13:24:38 +01:00
private ulong calibrationSegmentIndex ;
2026-05-22 19:59:09 +01:00
private long lastHybridWatermarkSegment = - 1 ;
2026-05-22 18:09:17 +01:00
private double lastAudioSyncAnalysisMilliseconds ;
2026-05-25 07:06:25 +01:00
private double lastSpectrumAnalysisMilliseconds ;
2026-05-22 19:09:11 +01:00
private double lastPassiveSynchronizationConfidence ;
2026-05-27 11:48:18 +01:00
private int lastDroppedSpectrumSourceLaneCount ;
2026-05-25 20:34:14 +01:00
private float spectrumCameraDistanceMultiplier = SpectrumCameraDefaultDistanceMultiplier ;
private float spectrumCameraAngleDegrees = SpectrumCameraDefaultAngleDegrees ;
private AquariumCameraFrustum lastSpectrumFrustum = AquariumCameraFrustum . Default ;
private Vector3 lastSpectrumCameraPosition ;
private Vector3 lastSpectrumCameraTarget ;
private ( Vector3 Min , Vector3 Max ) lastSpectrumAabb = ( Vector3 . Zero , Vector3 . One ) ;
2026-05-22 13:15:38 +01:00
private IReadOnlyList < MimirAudioSynchronizationReport > lastAudioSynchronizationReports = [ ] ;
2026-05-25 07:06:25 +01:00
private IReadOnlyList < MimirAudioSpectrumSnapshot > lastAudioSpectra = [ ] ;
2026-05-27 20:34:09 +01:00
private MimirObsStemPublicationSnapshot lastObsStemPublication = MimirObsStemPublicationSnapshot . Empty ;
2026-05-26 11:14:02 +01:00
private long spectrumHistorySequence ;
2026-05-27 18:07:04 +01:00
private long audioActuatorFrameSequence ;
2026-05-27 18:55:27 +01:00
private bool audioActuatorProgramQueued ;
2026-05-26 11:14:02 +01:00
private readonly Queue < MimirSpectrumHistoryFrame > spectrumHistory = new ( ) ;
2026-05-27 13:29:48 +01:00
private AquariumRuntimeServices runtimeServices = AquariumRuntimeServices . Empty ;
2026-05-27 18:03:25 +01:00
private MimirActuatorFrame lastAudioActuatorFrame = MimirActuatorFrame . Empty ;
2026-05-21 13:34:03 +01:00
public MimirRuntime ( AquariumRuntimeOptions options )
2026-05-21 18:27:39 +01:00
: this ( options , MimirRuntimeConfiguration . Load ( ) )
{
}
public MimirRuntime ( AquariumRuntimeOptions options , MimirRuntimeConfiguration configuration )
2026-05-25 07:13:09 +01:00
: this ( options , configuration . Settings , configuration . SourceFactories )
2026-05-21 13:34:03 +01:00
{
}
public MimirRuntime ( AquariumRuntimeOptions options , MimirSynchronizationSettings settings )
2026-05-25 07:13:09 +01:00
: this ( options , settings , Array . Empty < IMimirStreamSource > ( ) )
2026-05-21 13:34:03 +01:00
{
}
public MimirRuntime (
AquariumRuntimeOptions options ,
MimirSynchronizationSettings settings ,
IEnumerable < IMimirStreamSource > streamSources )
2026-05-25 07:13:09 +01:00
: this ( options , settings , Array . Empty < MimirStreamSourceFactory > ( ) )
{
foreach ( var source in streamSources )
{
synchronization . AddSource ( source ) ;
}
}
private MimirRuntime (
AquariumRuntimeOptions options ,
MimirSynchronizationSettings settings ,
IEnumerable < MimirStreamSourceFactory > sourceFactories )
2026-05-21 13:34:03 +01:00
{
Options = options ;
synchronization = new MimirSynchronizationHub ( settings ) ;
2026-05-26 14:51:06 +01:00
fieldLowering = new MimirFensalirFieldLowering ( new MimirFensalirLoweringOptions (
settings . BufferDuration . TotalSeconds ,
settings . BufferDuration . TotalSeconds ) ) ;
2026-05-25 23:02:44 +01:00
spectrumAnalyzer = new MimirAudioSpectrumAnalyzer ( ParseSpectrumFftSize ( ) , ParseSpectrumBandCount ( ) ) ;
2026-05-25 07:13:09 +01:00
this . sourceFactories = sourceFactories . ToArray ( ) ;
2026-05-22 18:49:01 +01:00
audioSyncSettings = settings . Audio ;
2026-05-22 13:15:38 +01:00
telemetryIntervalSeconds = ParseTelemetryIntervalSeconds ( ) ;
2026-05-22 18:09:17 +01:00
audioSyncUpdateIntervalSeconds = ParseAudioSyncIntervalSeconds ( ) ;
2026-05-25 07:06:25 +01:00
spectrumUpdateIntervalSeconds = ParseSpectrumUpdateIntervalSeconds ( ) ;
2026-05-27 11:42:03 +01:00
spectrumTubeSubdivisions = ParseSpectrumTubeSubdivisions ( ) ;
2026-05-27 11:48:18 +01:00
spectrumSourceLaneCapacity = ParseSpectrumSourceLaneCapacity ( ) ;
2026-05-27 23:34:44 +01:00
obsProofVisualEnabled = IsTruthy ( Environment . GetEnvironmentVariable ( "MIMIR_OBS_PROOF_VISUAL" ) ) ;
2026-05-22 18:49:01 +01:00
calibrationGain = settings . Audio . CalibrationGain ;
2026-05-22 19:59:09 +01:00
watermarkGain = settings . Audio . WatermarkGain ;
2026-05-25 18:26:42 +01:00
syntheticSpectrumPreview = IsTruthy ( Environment . GetEnvironmentVariable ( "MIMIR_SYNTHETIC_SPECTRUM_PREVIEW" ) ) ;
2026-05-28 14:01:54 +01:00
syntheticSingleTubePreview = IsTruthy ( Environment . GetEnvironmentVariable ( "MIMIR_SYNTHETIC_SINGLE_TUBE_PREVIEW" ) ) ;
if ( syntheticSingleTubePreview )
{
GraphicsSettings = new GraphicsSettings (
RenderDebugMode : 0 ,
SceneExposure : 1.0f ,
BloomIntensity : 0.075f ,
BloomVeilIntensity : 0.0f ) ;
}
2026-05-27 20:55:48 +01:00
obsStemPublisher = CreateObsStemPublisher ( ) ;
2026-05-25 06:28:20 +01:00
complexContourWitness = settings . Audio . EnableComplexContourRuntime
? new MimirBioacousticContestantRenderer (
MimirBioacousticContestants . BuiltIn . FirstOrDefault ( profile = >
string . Equals ( profile . Id , settings . Audio . BioacousticWitnessProfileId , StringComparison . OrdinalIgnoreCase ) )
? ? MimirBioacousticContestants . CanaryPacketTrill )
: null ;
2026-05-22 18:09:17 +01:00
nextAudioSyncSeconds = audioSyncUpdateIntervalSeconds ;
2026-05-22 13:15:38 +01:00
nextTelemetrySeconds = telemetryIntervalSeconds ;
2026-05-21 13:34:03 +01:00
ui = CreateUi ( ) ;
}
public AquariumRuntimeOptions Options { get ; }
2026-05-25 13:19:13 +01:00
public AquariumFrame Frame = > CreateFrame ( ) ;
2026-05-21 13:34:03 +01:00
2026-05-23 22:45:53 +01:00
public GraphicsSettings GraphicsSettings { get ; set ; } = GraphicsSettings . Default ;
2026-05-21 13:34:03 +01:00
2026-05-23 22:45:53 +01:00
public AquariumRenderPlan RenderPlan { get ; } = CreateRenderPlan ( ) ;
2026-05-21 13:34:03 +01:00
public AquariumUiDocument Ui = > ui ;
2026-05-23 22:45:53 +01:00
public AquariumAudioDocument Audio = > audio ;
2026-05-21 13:34:03 +01:00
2026-05-27 13:29:48 +01:00
public void AttachServices ( AquariumRuntimeServices services )
{
runtimeServices = services ;
2026-05-27 13:32:38 +01:00
synchronization . AttachTextureLeaseClient ( new MimirFensalirTextureLeaseClient ( services . FieldResources ) ) ;
2026-05-27 13:29:48 +01:00
}
2026-05-25 13:19:13 +01:00
private AquariumFrame CreateFrame ( )
{
var channelCount = Math . Max ( 1 , lastAudioSpectra . Count ) ;
2026-05-26 14:51:06 +01:00
var fieldEvidenceFrame = BuildFieldEvidenceFrame ( ) ;
2026-05-25 20:34:14 +01:00
var windowCount = Math . Max ( 1 , spectrumHistory . Count ) ;
2026-05-28 14:01:54 +01:00
var ( splineMin , splineMax ) = syntheticSingleTubePreview
? ( new Vector3 ( - 5.76643f , - 1.0f , 0.0f ) , new Vector3 ( 5.76643f , 1.0f , 0.0f ) )
: SpectrumSplineAabb ( channelCount , windowCount ) ;
2026-05-25 20:34:14 +01:00
var cameraTarget = ( splineMin + splineMax ) * 0.5f ;
2026-05-28 14:01:54 +01:00
var cameraPosition = syntheticSingleTubePreview
? new Vector3 ( 0.0f , 0.0f , - 18.367355f )
: SpectrumCameraPosition (
channelCount ,
windowCount ,
cameraTarget ,
spectrumCameraDistanceMultiplier ,
spectrumCameraAngleDegrees ) ;
var cameraFrustum = syntheticSingleTubePreview
? new AquariumCameraFrustum ( - 0.036f , 0.036f , - 0.02025f , 0.02025f , 0.1f , 100.0f )
: FitSpectrumCameraFrustum ( splineMin , splineMax , cameraPosition , cameraTarget ) ;
2026-05-25 20:34:14 +01:00
lastSpectrumFrustum = cameraFrustum ;
lastSpectrumCameraPosition = cameraPosition ;
lastSpectrumCameraTarget = cameraTarget ;
lastSpectrumAabb = ( splineMin , splineMax ) ;
2026-05-25 13:19:13 +01:00
return new AquariumFrame (
2026-05-25 20:34:14 +01:00
new ViewFrame ( Vector2 . Zero , 24.0f ) { Frustum = cameraFrustum } ,
2026-05-25 15:55:47 +01:00
cameraPosition ,
cameraTarget ,
2026-05-25 13:19:13 +01:00
runtimeSeconds ,
Vector2 . Zero ,
new AquariumSceneState
{
TraceHeightFieldSurface = false ,
2026-05-28 14:01:54 +01:00
UseStudioBackground = ! syntheticSingleTubePreview ,
2026-05-27 23:34:44 +01:00
UseStarfieldBackground = obsProofVisualEnabled ,
SdfObjects = obsProofVisualEnabled ? BuildObsProofSdfObjects ( runtimeSeconds ) : [ ] ,
SdfLights = obsProofVisualEnabled ? BuildObsProofSdfLights ( runtimeSeconds ) : [ ] ,
2026-05-26 14:51:06 +01:00
FieldEvidenceFrame = fieldEvidenceFrame ,
2026-05-26 10:50:37 +01:00
BufferFieldFrame = AquariumBufferFieldFrame . Empty ,
2026-05-26 23:25:16 +01:00
SplineFrame = AquariumSplineFrame . Empty ,
2026-05-25 13:19:13 +01:00
} ) ;
}
2026-05-27 23:34:44 +01:00
private static AquariumSdfObject [ ] BuildObsProofSdfObjects ( float timeSeconds )
{
var center = new Vector3 ( MathF . Sin ( timeSeconds * 0.8f ) * 1.2f , 0.25f , 0.0f ) ;
return
[
new AquariumSdfObject (
new Vector4 ( center , 1.2f ) ,
new Vector4 ( center , 0.0f ) ,
new Vector4 ( 1.0f , timeSeconds , 0.0f , 0.0f ) )
] ;
}
private static AquariumSdfLight [ ] BuildObsProofSdfLights ( float timeSeconds )
{
var lightOrbit = new Vector3 ( MathF . Cos ( timeSeconds ) * 2.0f , 1.8f , MathF . Sin ( timeSeconds ) * 2.0f ) ;
return
[
new AquariumSdfLight (
new Vector4 ( lightOrbit , 3.0f ) ,
new Vector4 ( 4.0f , 0.9f , 0.2f , 10.0f ) ) ,
new AquariumSdfLight (
new Vector4 ( - 1.8f , 1.0f , 2.5f , 2.0f ) ,
new Vector4 ( 0.1f , 1.4f , 3.6f , 10.0f ) )
] ;
}
2026-05-26 14:51:06 +01:00
private AquariumFieldEvidenceFrame BuildFieldEvidenceFrame ( )
{
var buffers = synchronization . Buffers . Buffers ;
var windows = new List < MimirRollingStreamWindow > ( buffers . Count ) ;
var observations = new List < MimirObservation > ( buffers . Count ) ;
MimirFensalirBridgeMapper . MapWindows ( buffers , windows ) ;
MimirFensalirBridgeMapper . MapLatestObservations ( buffers , observations ) ;
2026-05-27 13:29:48 +01:00
CommitProducerLeases ( windows ) ;
2026-05-26 14:51:06 +01:00
var constraints = new List < MimirCalibrationConstraint > ( synchronization . AudioSynchronizationStates . Count ) ;
MimirFensalirBridgeMapper . MapCalibrationConstraints ( synchronization . AudioSynchronizationStates , constraints ) ;
var observationsByWindow = observations . ToDictionary ( observation = > observation . WindowId , StringComparer . Ordinal ) ;
var intents = new List < MimirSurfaceIntent > ( observations . Count ) ;
foreach ( var window in windows )
{
if ( ! observationsByWindow . TryGetValue ( window . WindowId , out var observation ) )
{
continue ;
}
2026-05-26 23:37:35 +01:00
if ( window . SourceKind = = MimirStreamKind . Audio )
{
continue ;
}
2026-05-27 12:44:01 +01:00
if ( ! window . Payload . HasResource )
{
continue ;
}
2026-05-26 14:51:06 +01:00
intents . Add ( BuildSurfaceIntent ( window , observation ) ) ;
}
2026-05-26 23:37:35 +01:00
var frame = fieldLowering . BuildFieldEvidenceFrame ( windows , observations , constraints , intents ) ;
return AddSpectrumFieldEvidence ( frame ) ;
2026-05-26 14:51:06 +01:00
}
2026-05-27 13:29:48 +01:00
private void CommitProducerLeases ( IReadOnlyList < MimirRollingStreamWindow > windows )
{
foreach ( var window in windows )
{
if ( window . SourceKind ! = MimirStreamKind . Video | |
string . IsNullOrWhiteSpace ( window . Payload . ResourceKey ) | |
window . Payload . ProducerFenceValue = = 0 )
{
continue ;
}
runtimeServices . FieldResources . CommitLeaseVersion (
window . Payload . ResourceKey ,
window . SequenceId ,
window . Payload . ProducerFenceValue ) ;
}
}
2026-05-26 14:51:06 +01:00
private static MimirSurfaceIntent BuildSurfaceIntent ( MimirRollingStreamWindow window , MimirObservation observation )
{
if ( window . SourceKind = = MimirStreamKind . Audio )
{
return new MimirSurfaceIntent (
IntentKey : $"{window.WindowId}:spectrum" ,
SourceObservationKeys : [ observation . ObservationKey ] ,
Domain : MimirSurfaceDomain . AudioSpectrum ,
Axes : new MimirSurfaceAxes ( "frequency" , "amplitude" , "time-age" , "stream" ) ,
SupportPolicy : new MimirSurfaceSupportPolicy ( "rolling-spectrum" , 0.0 , window . Duration . TotalSeconds ) ,
MaterialGraph : new MimirSurfaceMaterialIntent ( "spectrum-evidence" , "source-evidence" , observation . Confidence ) ,
UpdateBudget : new MimirSurfaceUpdateBudget ( 0.0 , 1 , Math . Max ( 0 , window . Payload . ByteLength ) ) ,
Purpose : MimirSurfaceIntentPurpose . Debug ) ;
}
return MimirFensalirBridgeMapper . MapDefaultSurfaceIntent ( window , observation ) ;
}
2026-05-26 23:37:35 +01:00
private AquariumFieldEvidenceFrame AddSpectrumFieldEvidence ( AquariumFieldEvidenceFrame frame )
{
if ( lastAudioSpectra . Count = = 0 )
{
return frame ;
}
var spectra = lastAudioSpectra
. Where ( static spectrum = > spectrum . BandDecibels . Count > = 2 )
. OrderBy ( static spectrum = > spectrum . SourceId , StringComparer . Ordinal )
. ToArray ( ) ;
if ( spectra . Length = = 0 )
{
return frame ;
}
2026-05-27 11:39:07 +01:00
var historyFrames = spectrumHistory . Count > 0
? spectrumHistory . Reverse ( ) . ToArray ( )
: [ new MimirSpectrumHistoryFrame ( spectrumHistorySequence , runtimeSeconds , spectra ) ] ;
2026-05-27 11:48:18 +01:00
var allSourceIds = historyFrames
2026-05-27 11:39:07 +01:00
. SelectMany ( static history = > history . Spectra )
. Where ( static spectrum = > spectrum . BandDecibels . Count > = 2 )
. Select ( static spectrum = > spectrum . SourceId )
. Distinct ( StringComparer . Ordinal )
. OrderBy ( static sourceId = > sourceId , StringComparer . Ordinal )
. ToArray ( ) ;
2026-05-27 11:48:18 +01:00
var sourceIds = allSourceIds
. Take ( spectrumSourceLaneCapacity )
. ToArray ( ) ;
2026-05-27 11:39:07 +01:00
if ( sourceIds . Length = = 0 )
2026-05-26 23:37:35 +01:00
{
2026-05-27 11:48:18 +01:00
lastDroppedSpectrumSourceLaneCount = 0 ;
2026-05-27 11:39:07 +01:00
return frame ;
}
2026-05-27 11:48:18 +01:00
lastDroppedSpectrumSourceLaneCount = allSourceIds . Length - sourceIds . Length ;
2026-05-27 11:39:07 +01:00
var sourceIndexById = sourceIds
. Select ( ( sourceId , index ) = > ( sourceId , index ) )
. ToDictionary ( static item = > item . sourceId , static item = > item . index , StringComparer . Ordinal ) ;
var width = Math . Max ( 2 , historyFrames
. SelectMany ( static history = > history . Spectra )
. Where ( static spectrum = > spectrum . BandDecibels . Count > = 2 )
. Select ( static spectrum = > spectrum . BandDecibels . Count )
. DefaultIfEmpty ( 2 )
. Max ( ) ) ;
var historyCount = historyFrames . Length ;
var sourceCount = sourceIds . Length ;
2026-05-28 14:01:54 +01:00
var renderedHistoryCount = syntheticSingleTubePreview ? 1 : historyCount ;
var activeColumnCount = checked ( sourceCount * renderedHistoryCount ) ;
2026-05-27 11:48:18 +01:00
var resourceColumnCapacity = checked ( spectrumSourceLaneCapacity * SpectrumHistoryWindowCount ) ;
2026-05-27 11:55:46 +01:00
var newestHistorySlot = PositiveModulo ( - historyFrames [ 0 ] . Sequence , SpectrumHistoryWindowCount ) ;
var rollingOffset = checked ( newestHistorySlot * spectrumSourceLaneCapacity ) ;
2026-05-27 12:22:30 +01:00
var resourceUploads = new List < AquariumFieldResourceUpload > ( 1 ) ;
for ( var historyIndex = 0 ; historyIndex < Math . Min ( 1 , historyFrames . Length ) ; historyIndex + + )
2026-05-27 11:39:07 +01:00
{
2026-05-27 11:55:46 +01:00
var historySlot = PositiveModulo ( - historyFrames [ historyIndex ] . Sequence , SpectrumHistoryWindowCount ) ;
2026-05-27 12:01:34 +01:00
var uploadColumnOffset = checked ( historySlot * spectrumSourceLaneCapacity ) ;
var samples = new float [ width * spectrumSourceLaneCapacity ] ;
2026-05-27 11:39:07 +01:00
foreach ( var spectrum in historyFrames [ historyIndex ] . Spectra )
{
if ( ! sourceIndexById . TryGetValue ( spectrum . SourceId , out var sourceIndex ) )
{
continue ;
}
2026-05-27 12:01:34 +01:00
WriteNormalizedSpectrum ( spectrum , samples . AsSpan ( sourceIndex * width , width ) ) ;
2026-05-27 11:39:07 +01:00
}
2026-05-27 12:01:34 +01:00
resourceUploads . Add ( new AquariumFieldResourceUpload
{
ResourceKey = SpectrumFieldResourceKey ,
Version = unchecked ( ( ulong ) Math . Max ( 0 , spectrumHistorySequence ) ) ,
ElementOffset = checked ( uploadColumnOffset * width ) ,
Float32Data = samples ,
} ) ;
2026-05-26 23:37:35 +01:00
}
const string domainKey = "mimir:domain:spectrum:field-upload" ;
var version = unchecked ( ( ulong ) Math . Max ( 0 , spectrumHistorySequence ) ) ;
var confidence = ( float ) Math . Clamp ( spectra . Average ( static spectrum = > spectrum . Rms > 0.0 ? 1.0 : 0.65 ) , 0.0 , 1.0 ) ;
2026-05-27 11:39:07 +01:00
var maxY = Math . Max ( 0 , sourceCount - 1 ) * SpectrumChannelSeparation + SpectrumAmplitudeHeight ;
var maxZ = Math . Max ( 0 , SpectrumHistoryWindowCount - 1 ) * SpectrumWindowDepthSeparation ;
2026-05-26 23:37:35 +01:00
var support = new AquariumFieldSupport (
2026-05-27 11:39:07 +01:00
Center : new Vector3 ( 0.0f , maxY * 0.5f , maxZ * 0.5f ) ,
Radius : new Vector3 ( 5.0f , Math . Max ( SpectrumAmplitudeHeight , maxY ) , Math . Max ( 0.1f , maxZ ) ) ,
2026-05-26 23:37:35 +01:00
LocalFrame : Matrix4x4 . Identity ,
ConservativeRadius : 5.0f ,
ProjectedError : 0.0f ,
Curvature : 0.0f ,
TemporalUncertainty : ( float ) Math . Max ( 0.0 , spectrumUpdateIntervalSeconds ) ) ;
var proposal = new AquariumFieldProposalPolicy (
AquariumFieldProposalKind . DebugIntent ,
SourcePdf : 1.0f ,
TargetContribution : confidence ,
2026-05-27 11:39:07 +01:00
RepresentedCandidateCount : activeColumnCount ,
2026-05-26 23:37:35 +01:00
Seed : 0x5EC7 _0001u ) ;
var resource = new AquariumFieldResourceDeclaration (
2026-05-26 23:51:13 +01:00
ResourceKey : SpectrumFieldResourceKey ,
2026-05-26 23:37:35 +01:00
Kind : AquariumFieldResourceKind . StructuredBuffer ,
Residency : AquariumFieldResourceResidency . GpuResident ,
Access : AquariumFieldShaderAccess . ShaderResource ,
Format : "Float32" ,
Width : width ,
2026-05-27 11:39:07 +01:00
Height : resourceColumnCapacity ,
2026-05-27 12:01:34 +01:00
DepthOrCount : checked ( width * resourceColumnCapacity ) ,
2026-05-26 23:37:35 +01:00
StrideBytes : sizeof ( float ) ,
ValidFromNs : 0 ,
ValidUntilNs : 0 ,
Version : version ,
NativeHandle : IntPtr . Zero ,
NativeHandleKind : "fensalir-owned-spectrum-upload" ) ;
2026-05-26 23:51:13 +01:00
var ramp = AquariumFieldResourceDeclaration . LocalTexture2D (
SpectrumRampResourceKey ,
SpectrumRampTexturePath ,
version : 1 ) ;
2026-05-28 14:01:54 +01:00
var axisLength = syntheticSingleTubePreview ? 11.53286f : 10.0f ;
var axisStepX = width > 1 ? axisLength / ( width - 1 ) : axisLength ;
2026-05-27 11:39:07 +01:00
var claims = new List < AquariumFieldClaim > ( sourceCount ) ;
var candidates = new List < AquariumFieldCandidate > ( sourceCount ) ;
var lowerings = new List < AquariumFieldTubeSplineLowering > ( sourceCount ) ;
for ( var sourceIndex = 0 ; sourceIndex < sourceIds . Length ; sourceIndex + + )
{
var sourceId = sourceIds [ sourceIndex ] ;
var claimKey = $"intent:mimir:spectrum:field-upload:{sourceId}" ;
var sourceSupport = support with
{
Center = new Vector3 ( 0.0f , sourceIndex * SpectrumChannelSeparation + SpectrumAmplitudeHeight * 0.5f , maxZ * 0.5f ) ,
Radius = new Vector3 ( 5.0f , SpectrumAmplitudeHeight , Math . Max ( 0.1f , maxZ ) ) ,
} ;
var sourceProposal = proposal with
{
2026-05-28 14:01:54 +01:00
RepresentedCandidateCount = renderedHistoryCount ,
2026-05-27 11:39:07 +01:00
Seed = unchecked ( proposal . Seed + ( uint ) sourceIndex ) ,
} ;
claims . Add ( new AquariumFieldClaim (
ClaimKey : claimKey ,
DomainKey : domainKey ,
ProducerKey : "Mimir.Runtime" ,
Layer : AquariumFieldLayer . Form ,
Encoding : AquariumFieldEncoding . Tube ,
Support : sourceSupport ,
Proposal : sourceProposal ,
PayloadHandle : SpectrumFieldResourceKey ,
ObservedTimeNs : 0 ,
Confidence : confidence ) ) ;
candidates . Add ( new AquariumFieldCandidate (
$"{claimKey}:candidate" ,
claimKey ,
AquariumFieldLayer . Form ,
AquariumFieldEncoding . Tube ,
sourceProposal ,
AquariumFieldGuide . Valid ( confidence , spectrumUpdateIntervalSeconds ) ) ) ;
lowerings . Add ( new AquariumFieldTubeSplineLowering (
LoweringKey : $"tube-spline:mimir:spectrum:field-upload:{sourceId}" ,
ClaimKey : claimKey ,
ResourceKey : SpectrumFieldResourceKey ,
Width : width ,
Height : resourceColumnCapacity ,
StrideBytes : sizeof ( float ) ,
FirstColumn : sourceIndex ,
2026-05-28 14:01:54 +01:00
ColumnCount : renderedHistoryCount ,
2026-05-27 11:48:18 +01:00
ColumnStride : spectrumSourceLaneCapacity ,
2026-05-27 11:39:07 +01:00
RollingModulo : resourceColumnCapacity ,
2026-05-27 11:55:46 +01:00
RollingOffset : rollingOffset ,
2026-05-28 14:01:54 +01:00
Origin : syntheticSingleTubePreview
? new Vector3 ( - axisLength * 0.5f , 0.0f , 0.0f )
: new Vector3 ( - 5.0f , sourceIndex * SpectrumChannelSeparation , 0.0f ) ,
2026-05-27 11:39:07 +01:00
AxisStep : new Vector3 ( axisStepX , 0.0f , 0.0f ) ,
ColumnStep : new Vector3 ( 0.0f , 0.0f , SpectrumWindowDepthSeparation ) ,
AmplitudePower : 2.0f ,
2026-05-28 14:01:54 +01:00
AmplitudeScale : syntheticSingleTubePreview ? 0.0f : SpectrumAmplitudeHeight ,
2026-05-27 11:39:07 +01:00
NormalizeMin : 0.0f ,
NormalizeMax : 1.0f ,
2026-05-28 14:01:54 +01:00
BaseRadius : syntheticSingleTubePreview ? 1.0f : 0.012f ,
RadiusScale : syntheticSingleTubePreview ? 0.0f : 0.030f ,
Alpha : 1.0f ,
Feather : syntheticSingleTubePreview ? 0.02f : 0.20f ,
2026-05-27 11:39:07 +01:00
RampTexturePath : SpectrumRampTexturePath ,
RampResourceKey : SpectrumRampResourceKey ,
2026-05-28 14:01:54 +01:00
EmissionScale : syntheticSingleTubePreview ? 16.0f : 10.0f ,
CatmullRomSubdivisions : syntheticSingleTubePreview ? 1 : spectrumTubeSubdivisions ) . Normalized ( ) ) ;
2026-05-27 11:39:07 +01:00
}
2026-05-26 23:37:35 +01:00
return new AquariumFieldEvidenceFrame
{
Domains =
[
. . frame . Domains ,
new AquariumFieldDomain (
domainKey ,
"" ,
AquariumFieldDomainKind . RollingBuffer ,
Matrix4x4 . Identity ,
Matrix4x4 . Identity ,
new Vector3 ( - 5.0f , 0.0f , 0.0f ) ,
2026-05-27 11:39:07 +01:00
new Vector3 ( 5.0f , maxY , Math . Max ( 0.1f , maxZ ) ) ,
2026-05-26 23:37:35 +01:00
Vector3 . Zero ,
"Mimir.Runtime" ) ,
] ,
2026-05-27 11:39:07 +01:00
Claims = [ . . frame . Claims , . . claims ] ,
Candidates = [ . . frame . Candidates , . . candidates ] ,
2026-05-26 23:37:35 +01:00
BackendPackets = frame . BackendPackets ,
2026-05-26 23:51:13 +01:00
Resources = [ . . frame . Resources , resource , ramp ] ,
2026-05-26 23:37:35 +01:00
ResourceUploads =
[
. . frame . ResourceUploads ,
2026-05-27 12:01:34 +01:00
. . resourceUploads ,
2026-05-26 23:37:35 +01:00
] ,
2026-05-27 11:39:07 +01:00
TubeSplineLowerings = [ . . frame . TubeSplineLowerings , . . lowerings ] ,
2026-05-26 23:37:35 +01:00
AccumulationWindowSeconds = frame . AccumulationWindowSeconds ,
PresentationDelaySeconds = frame . PresentationDelaySeconds ,
} ;
}
private static void WriteNormalizedSpectrum ( MimirAudioSpectrumSnapshot spectrum , Span < float > destination )
{
destination . Clear ( ) ;
var bands = spectrum . BandDecibels ;
if ( bands . Count = = 0 )
{
return ;
}
var floor = Math . Min ( spectrum . NoiseFloorDb , bands . Min ( ) ) ;
var ceiling = Math . Max ( - 24.0 , bands . Max ( ) ) ;
var span = Math . Max ( 18.0 , ceiling - floor ) ;
var count = Math . Min ( destination . Length , bands . Count ) ;
for ( var index = 0 ; index < count ; index + + )
{
destination [ index ] = ( float ) Math . Clamp ( ( bands [ index ] - floor ) / span , 0.0 , 1.0 ) ;
}
}
2026-05-27 11:55:46 +01:00
private static int PositiveModulo ( long value , int modulo )
{
var remainder = value % modulo ;
return ( int ) ( remainder < 0 ? remainder + modulo : remainder ) ;
}
2026-05-25 20:34:14 +01:00
private static Vector3 SpectrumCameraPosition (
int spectrumCount ,
int windowCount ,
Vector3 target ,
float distanceMultiplier ,
float angleDegrees )
{
var oldDistance = math . sqrt ( 2.0f ) * math . max ( 1.0f , spectrumCount ) * SpectrumChannelSeparation ;
var aabbDepth = math . max ( 1.0f , windowCount ) * SpectrumWindowDepthSeparation ;
var distance = math . max ( oldDistance * math . clamp ( distanceMultiplier , 1.0f , 80.0f ) , aabbDepth + 0.1f ) ;
2026-05-25 21:14:10 +01:00
var angleRadians = math . radians ( math . clamp (
angleDegrees ,
SpectrumCameraMinimumAngleDegrees ,
SpectrumCameraMaximumAngleDegrees ) ) ;
var viewBack = math . normalize ( new float3 ( 0.0f , math . sin ( angleRadians ) , - math . cos ( angleRadians ) ) ) ;
2026-05-25 20:34:14 +01:00
return ToVector3 ( ToFloat3 ( target ) + viewBack * distance ) ;
}
private static ( Vector3 Min , Vector3 Max ) SpectrumSplineAabb ( int spectrumCount , int windowCount )
{
var halfWidth = SpectrumWidth * 0.5f ;
var maxY = Math . Max ( 0 , spectrumCount - 1 ) * SpectrumChannelSeparation + SpectrumAmplitudeHeight ;
var maxZ = Math . Max ( 0 , windowCount - 1 ) * SpectrumWindowDepthSeparation ;
var padding = new Vector3 ( SpectrumSplineTubePadding , SpectrumSplineTubePadding , SpectrumSplineTubePadding ) ;
return (
new Vector3 ( - halfWidth , 0.0f , 0.0f ) - padding ,
new Vector3 ( halfWidth , maxY , maxZ ) + padding ) ;
}
private static AquariumCameraFrustum FitSpectrumCameraFrustum (
Vector3 aabbMin ,
Vector3 aabbMax ,
Vector3 cameraPosition ,
Vector3 cameraTarget )
{
var camera = ToFloat3 ( cameraPosition ) ;
var target = ToFloat3 ( cameraTarget ) ;
SpectrumCameraBasis ( camera , target , out var forward , out var right , out var up ) ;
var maxSlopeX = 0.001f ;
var maxSlopeY = 0.001f ;
var minZ = float . PositiveInfinity ;
var maxZ = float . NegativeInfinity ;
var corners = SpectrumAabbCorners ( aabbMin , aabbMax ) . ToArray ( ) ;
foreach ( var corner in corners )
{
var delta = ToFloat3 ( corner ) - camera ;
var z = math . max ( math . dot ( delta , forward ) , SpectrumFrustumMinimumNear ) ;
minZ = math . min ( minZ , z ) ;
maxZ = math . max ( maxZ , z ) ;
maxSlopeX = math . max ( maxSlopeX , math . abs ( math . dot ( delta , right ) / z ) ) ;
maxSlopeY = math . max ( maxSlopeY , math . abs ( math . dot ( delta , up ) / z ) ) ;
}
var depthPadding = math . max ( ( maxZ - minZ ) * 0.08f , SpectrumSplineTubePadding ) ;
var near = math . max ( SpectrumFrustumMinimumNear , minZ - depthPadding ) ;
var far = math . max ( near + 0.001f , maxZ + depthPadding ) ;
var halfWidth = maxSlopeX * near * SpectrumCameraFitPadding ;
var halfHeight = maxSlopeY * near * SpectrumCameraFitPadding ;
return new AquariumCameraFrustum (
- halfWidth ,
halfWidth ,
- halfHeight ,
halfHeight ,
near ,
far ) . Normalized ( ) ;
}
private static void SpectrumCameraBasis (
float3 cameraPosition ,
float3 cameraTarget ,
out float3 forward ,
out float3 right ,
out float3 up )
{
forward = math . normalize ( cameraTarget - cameraPosition ) ;
var worldUp = math . abs ( forward . y ) > 0.96f ? new float3 ( 0.0f , 0.0f , 1.0f ) : new float3 ( 0.0f , 1.0f , 0.0f ) ;
right = math . normalize ( math . cross ( worldUp , forward ) ) ;
up = math . normalize ( math . cross ( forward , right ) ) ;
}
private static float4 ProjectSpectrumPoint (
Vector3 worldPosition ,
Vector3 cameraPosition ,
Vector3 cameraTarget ,
AquariumCameraFrustum frustum )
{
var camera = ToFloat3 ( cameraPosition ) ;
SpectrumCameraBasis ( camera , ToFloat3 ( cameraTarget ) , out var forward , out var right , out var up ) ;
var view = ToCameraSpace ( ToFloat3 ( worldPosition ) , camera , forward , right , up ) ;
return ProjectCameraSpace ( view , frustum ) ;
}
private static float3 ToCameraSpace ( float3 world , float3 camera , float3 forward , float3 right , float3 up )
2026-05-25 19:17:25 +01:00
{
2026-05-25 20:34:14 +01:00
var delta = world - camera ;
return new float3 ( math . dot ( delta , right ) , math . dot ( delta , up ) , math . dot ( delta , forward ) ) ;
2026-05-25 19:17:25 +01:00
}
2026-05-25 20:34:14 +01:00
private static float4 ProjectCameraSpace ( float3 view , AquariumCameraFrustum frustum )
2026-05-25 19:17:25 +01:00
{
2026-05-25 20:34:14 +01:00
var normalized = frustum . Normalized ( ) ;
var z = math . max ( view . z , 0.0001f ) ;
var slope = view . xy / z ;
var min = new float2 ( normalized . Left / normalized . Near , normalized . Bottom / normalized . Near ) ;
var max = new float2 ( normalized . Right / normalized . Near , normalized . Top / normalized . Near ) ;
var uv = ( slope - min ) / math . max ( max - min , new float2 ( 0.0001f , 0.0001f ) ) ;
return new float4 ( uv * 2.0f - 1.0f , math . saturate ( z / math . max ( normalized . Far , 0.0001f ) ) , 1.0f ) ;
}
private static float3 ToFloat3 ( Vector3 value ) = > new ( value . X , value . Y , value . Z ) ;
private static Vector3 ToVector3 ( float3 value ) = > new ( value . x , value . y , value . z ) ;
private static IEnumerable < Vector3 > SpectrumAabbCorners ( Vector3 min , Vector3 max )
{
for ( var x = 0 ; x < 2 ; x + + )
{
for ( var y = 0 ; y < 2 ; y + + )
{
for ( var z = 0 ; z < 2 ; z + + )
{
yield return new Vector3 (
x = = 0 ? min . X : max . X ,
y = = 0 ? min . Y : max . Y ,
z = = 0 ? min . Z : max . Z ) ;
}
}
}
2026-05-25 19:17:25 +01:00
}
2026-05-23 22:45:53 +01:00
public AquariumSynthDocument Synth = > AquariumSynthDocument . Empty ;
2026-05-21 13:34:03 +01:00
public void RegisterStreamSource ( IMimirStreamSource source )
{
synchronization . AddSource ( source ) ;
}
public void Start ( )
{
2026-05-22 18:49:01 +01:00
Console . WriteLine ( $"Mimir runtime sync buffers: {synchronization.Summary()} @ {synchronization.Settings.BufferDuration.TotalSeconds:0.###}s audioSync={audioSyncSettings.Mode} reference={audioSyncSettings.ReferenceSourceId}" ) ;
2026-05-23 22:45:53 +01:00
Console . WriteLine ( "Mimir empty scene runtime booted." ) ;
2026-05-21 13:34:03 +01:00
}
public void Update ( float deltaSeconds , InputState input )
{
2026-05-25 07:13:09 +01:00
if ( ! sceneReady )
2026-05-25 07:06:25 +01:00
{
2026-05-25 07:13:09 +01:00
return ;
2026-05-25 07:06:25 +01:00
}
2026-05-25 07:13:09 +01:00
runtimeSeconds + = Math . Max ( deltaSeconds , 0.0f ) ;
lastPollCount = synchronization . PollSources ( ) ;
2026-05-27 20:34:09 +01:00
UpdateObsStemPublication ( ) ;
2026-05-25 07:13:09 +01:00
QueueCalibrationTimeline ( ) ;
UpdateAudioSpectra ( ) ;
UpdateAudioSynchronization ( ) ;
2026-05-22 13:15:38 +01:00
EmitTelemetry ( ) ;
2026-05-21 13:34:03 +01:00
}
2026-05-25 07:06:25 +01:00
public void OnSceneReady ( )
{
if ( sceneReady )
{
return ;
}
sceneReady = true ;
2026-05-25 07:13:09 +01:00
StartConfiguredSources ( ) ;
2026-05-27 18:55:27 +01:00
QueueAudioActuatorProgram ( ) ;
2026-05-25 07:06:25 +01:00
nextAudioSyncSeconds = runtimeSeconds + audioSyncUpdateIntervalSeconds ;
nextSpectrumSeconds = runtimeSeconds ;
Console . WriteLine ( $"Mimir Fensalir scene ready at {runtimeSeconds:0.000}s; runtime audio tests and spectra enabled." ) ;
}
2026-05-21 13:34:03 +01:00
public AquariumFrame ComposeFrame ( AquariumFrame frame , AquariumFrameInput input )
{
2026-05-23 22:45:53 +01:00
return frame ;
2026-05-21 13:34:03 +01:00
}
public void FlushState ( )
{
}
public void Dispose ( )
{
2026-05-27 20:55:48 +01:00
obsStemPublisher ? . Dispose ( ) ;
2026-05-21 13:34:03 +01:00
synchronization . Dispose ( ) ;
}
private AquariumUiDocument CreateUi ( )
{
return new AquariumUiDocument ( )
. Panel ( "Mimir Sync" , 18.0f , 82.0f , 390.0f , panel = >
{
panel . Section ( "Rolling Buffers" ) ;
panel . Readout ( "Window" , ( ) = > $"{synchronization.Settings.BufferDuration.TotalSeconds:0.###}s" ) ;
panel . Readout ( "Streams" , synchronization . Summary ) ;
panel . Readout ( "Sources" , ( ) = > $"{synchronization.SourceCount}" ) ;
panel . Readout ( "Last poll" , ( ) = > $"{lastPollCount} samples" ) ;
panel . Readout ( "Ingested" , ( ) = > $"{synchronization.IngestedSamples}" ) ;
2026-05-22 00:37:24 +01:00
panel . Readout ( "Buffer details" , DescribeBuffers ) ;
2026-05-22 01:22:36 +01:00
panel . Readout ( "Audio sync" , DescribeAudioSync ) ;
2026-05-22 12:46:51 +01:00
panel . Readout ( "Audio sync state" , DescribeAudioSyncState ) ;
2026-05-22 11:40:56 +01:00
panel . Readout ( "Aligned audio" , DescribeAlignedAudio ) ;
2026-05-22 12:54:56 +01:00
panel . Readout ( "Chirplet reference" , DescribeChirpletReference ) ;
2026-05-25 07:06:25 +01:00
panel . Section ( "Live FFT" ) ;
panel . Readout ( "Spectrum cadence" , ( ) = > sceneReady
? $"{lastAudioSpectra.Count} spectra fft={lastAudioSpectra.FirstOrDefault()?.FftSize ?? 0} analyze={lastSpectrumAnalysisMilliseconds:0.00}ms"
: "waiting for Fensalir scene ready" ) ;
2026-05-25 20:34:14 +01:00
panel . Slider (
"Perspective" ,
( ) = > spectrumCameraDistanceMultiplier ,
value = > spectrumCameraDistanceMultiplier = Math . Clamp ( value , 1.0f , 80.0f ) ,
1.0f ,
80.0f ,
"0.0x" ,
"Dolly distance multiplier. The projection frustum is fitted to the full spectrum trail AABB." ) ;
panel . Slider (
"Angle" ,
( ) = > spectrumCameraAngleDegrees ,
2026-05-25 21:14:10 +01:00
value = > spectrumCameraAngleDegrees = Math . Clamp (
value ,
SpectrumCameraMinimumAngleDegrees ,
SpectrumCameraMaximumAngleDegrees ) ,
SpectrumCameraMinimumAngleDegrees ,
SpectrumCameraMaximumAngleDegrees ,
2026-05-25 20:34:14 +01:00
"0.0 deg" ,
2026-05-25 21:14:10 +01:00
"Polar camera pitch from -Z toward +Y around the spectrum trail AABB." ) ;
2026-05-25 20:34:14 +01:00
panel . Readout ( "Frustum" , DescribeSpectrumFrustum ) ;
2026-05-27 11:42:03 +01:00
panel . Readout (
"Tube budget" ,
2026-05-27 11:48:18 +01:00
( ) = >
{
var dropped = lastDroppedSpectrumSourceLaneCount > 0
? $" dropped={lastDroppedSpectrumSourceLaneCount}"
: "" ;
return $"{spectrumHistory.Count}/{SpectrumHistoryWindowCount} age columns x {Math.Min(lastAudioSpectra.Count, spectrumSourceLaneCapacity)}/{spectrumSourceLaneCapacity} lanes x {spectrumTubeSubdivisions} subdivisions{dropped}" ;
} ,
2026-05-27 11:42:03 +01:00
"Current Mimir-side TubeField subdivision cost before Fensalir applies its fixed geometry budget." ) ;
2026-05-25 07:06:25 +01:00
panel . TextBox (
"Spectra" ,
DescribeSpectra ,
_ = > { } ,
lines : 12 ,
acceptsReturn : false ,
monospace : true ,
tooltip : "Cached FFT spectra from the runtime rolling audio buffers." ) ;
2026-05-21 13:34:03 +01:00
} ) ;
}
2026-05-22 00:37:24 +01:00
2026-05-23 22:45:53 +01:00
private static AquariumRenderPlan CreateRenderPlan ( )
{
var app = new AquariumApp ( ) ;
var scene = app . RenderTargets . Hdr ( "scene" ) ;
app . Cameras . Perspective ( "main" ) ;
app . Graph . Pass ( "scene" ) . Fullscreen ( ) ;
app . Features . Presentation ( scene . Color ) ;
app . Features . DirectWriteOverlay ( ) ;
app . Debug . View ( "Scene" , scene . Color ) ;
return app . Plan ;
}
2026-05-22 13:24:38 +01:00
private void QueueCalibrationTimeline ( )
2026-05-22 11:40:56 +01:00
{
2026-05-22 18:49:01 +01:00
if ( ! ShouldEmitCalibrationTimeline ( ) )
{
return ;
}
2026-05-22 19:59:09 +01:00
var currentSegmentIndex = CurrentCalibrationSegmentIndex ( ) ;
if ( audioSyncSettings . Mode = = MimirAudioSyncMode . Hybrid )
{
calibrationSegmentIndex = currentSegmentIndex ;
if ( calibrationSegmentIndex % HybridWatermarkIntervalSegments ! = 0 UL | |
( long ) calibrationSegmentIndex = = lastHybridWatermarkSegment )
{
return ;
}
}
else if ( calibrationSegmentIndex + 1 UL < currentSegmentIndex )
{
calibrationSegmentIndex = currentSegmentIndex ;
}
2026-05-24 06:13:45 +01:00
var nextSegmentSeconds = CalibrationStartSeconds + calibrationSegmentIndex * MimirBioacousticTimeline . SegmentSeconds ;
2026-05-22 17:35:21 +01:00
if ( runtimeSeconds < nextSegmentSeconds )
2026-05-22 11:40:56 +01:00
{
2026-05-22 17:35:21 +01:00
return ;
2026-05-22 11:40:56 +01:00
}
2026-05-22 17:35:21 +01:00
2026-05-22 19:09:11 +01:00
var segmentCount = audioSyncSettings . Mode = = MimirAudioSyncMode . Hybrid ? 1 : CalibrationBatchSegments ;
2026-05-22 19:59:09 +01:00
var outputGain = audioSyncSettings . Mode = = MimirAudioSyncMode . Hybrid ? watermarkGain : calibrationGain ;
2026-05-24 09:48:46 +01:00
var leftBatch = RenderCalibrationBatchPcm16Base64 (
2026-05-22 19:50:02 +01:00
calibrationSegmentIndex ,
segmentCount ,
2026-05-24 09:48:46 +01:00
MimirBioacousticSpeaker . Left ,
2026-05-22 19:50:02 +01:00
out var peak ) ;
2026-05-24 09:48:46 +01:00
var rightBatch = RenderCalibrationBatchPcm16Base64 (
calibrationSegmentIndex ,
segmentCount ,
MimirBioacousticSpeaker . Right ,
out var rightPeak ) ;
peak = Math . Max ( peak , rightPeak ) ;
audio . EnqueuePcm16Base64 (
leftBatch ,
MimirBioacousticTimeline . SampleRate ,
channels : 1 ,
gain : outputGain ,
pan : - 1.0f ) ;
2026-05-23 22:45:53 +01:00
audio . EnqueuePcm16Base64 (
2026-05-24 09:48:46 +01:00
rightBatch ,
2026-05-24 06:13:45 +01:00
MimirBioacousticTimeline . SampleRate ,
2026-05-22 17:35:21 +01:00
channels : 1 ,
2026-05-24 09:48:46 +01:00
gain : outputGain ,
pan : 1.0f ) ;
2026-05-22 17:35:21 +01:00
Console . WriteLine (
2026-05-24 09:48:46 +01:00
$"mimir-bioacoustic-batch mode={DescribeAudioSyncMode()} firstSegment={calibrationSegmentIndex} segments={segmentCount} seconds={segmentCount * MimirBioacousticTimeline.SegmentSeconds:0.00} peak={peak:0.000000} gain={outputGain:0.###} leftBase64Bytes={leftBatch.Length} rightBase64Bytes={rightBatch.Length}" ) ;
2026-05-22 19:59:09 +01:00
if ( audioSyncSettings . Mode = = MimirAudioSyncMode . Hybrid )
{
lastHybridWatermarkSegment = ( long ) calibrationSegmentIndex ;
}
else
{
calibrationSegmentIndex + = ( ulong ) segmentCount ;
}
}
private ulong CurrentCalibrationSegmentIndex ( )
{
if ( runtimeSeconds < = CalibrationStartSeconds )
{
return 0 UL ;
}
2026-05-24 06:13:45 +01:00
return ( ulong ) Math . Floor ( ( runtimeSeconds - CalibrationStartSeconds ) / MimirBioacousticTimeline . SegmentSeconds ) ;
2026-05-22 11:40:56 +01:00
}
2026-05-22 12:54:56 +01:00
private string DescribeChirpletReference ( )
{
2026-05-22 18:49:01 +01:00
if ( ! ShouldEmitCalibrationTimeline ( ) )
{
2026-05-22 19:09:11 +01:00
return $"{audioSyncSettings.ReferenceSourceId} passive mode; calibration emission disabled" ;
2026-05-22 18:49:01 +01:00
}
2026-05-24 06:13:45 +01:00
var emittedUntilSeconds = calibrationSegmentIndex * MimirBioacousticTimeline . SegmentSeconds ;
return $"{audioSyncSettings.ReferenceSourceId} {DescribeAudioSyncMode()} bioacoustic timeline {MimirBioacousticTimeline.SegmentSeconds:0.00}s segments emitted to {emittedUntilSeconds:0.00}s passiveConfidence={lastPassiveSynchronizationConfidence:0.000}" ;
2026-05-22 17:35:21 +01:00
}
2026-05-25 06:28:20 +01:00
private string RenderCalibrationBatchPcm16Base64 (
2026-05-22 19:50:02 +01:00
ulong firstSegment ,
int segmentCount ,
2026-05-24 09:48:46 +01:00
MimirBioacousticSpeaker speaker ,
2026-05-22 19:50:02 +01:00
out float peak )
2026-05-22 17:35:21 +01:00
{
2026-05-24 06:13:45 +01:00
var samplesPerSegment = ( int ) Math . Round ( MimirBioacousticTimeline . SegmentSeconds * MimirBioacousticTimeline . SampleRate ) ;
2026-05-22 17:35:21 +01:00
var bytes = new byte [ samplesPerSegment * Math . Max ( 1 , segmentCount ) * sizeof ( short ) ] ;
var byteIndex = 0 ;
peak = 0.0f ;
for ( var segment = 0 ; segment < segmentCount ; segment + + )
{
2026-05-25 06:28:20 +01:00
var samples = complexContourWitness = = null
? MimirBioacousticTimeline . Default . RenderSegmentMonoFloat (
firstSegment + ( ulong ) segment ,
MimirBioacousticTimeline . SampleRate ,
speaker )
: complexContourWitness . RenderSegmentMonoFloat (
firstSegment + ( ulong ) segment ,
MimirBioacousticTimeline . SegmentSeconds ,
MimirBioacousticTimeline . SampleRate ,
speaker ) ;
2026-05-22 17:35:21 +01:00
for ( var index = 0 ; index < samples . Length ; index + + )
{
peak = Math . Max ( peak , Math . Abs ( samples [ index ] ) ) ;
var sample = ( short ) Math . Round ( Math . Clamp ( samples [ index ] , - 1.0f , 1.0f ) * short . MaxValue ) ;
bytes [ byteIndex + + ] = ( byte ) ( sample & 0xff ) ;
bytes [ byteIndex + + ] = ( byte ) ( ( sample > > 8 ) & 0xff ) ;
}
}
return Convert . ToBase64String ( bytes ) ;
2026-05-22 12:54:56 +01:00
}
2026-05-22 00:37:24 +01:00
private string DescribeBuffers ( )
{
return string . Join ( " | " , synchronization . Buffers . Buffers . Select ( DescribeBuffer ) ) ;
}
2026-05-22 13:15:38 +01:00
private void UpdateAudioSynchronization ( )
{
if ( runtimeSeconds < nextAudioSyncSeconds )
{
return ;
}
2026-05-22 18:09:17 +01:00
var stopwatch = Stopwatch . StartNew ( ) ;
2026-05-22 19:09:11 +01:00
synchronization . AnalyzeAudioSynchronizationStep ( audioSyncSettings . ReferenceSourceId , audioSyncSettings . Mode ) ;
2026-05-25 06:28:20 +01:00
if ( synchronization . ComplexContourRuntimeEnabled )
{
synchronization . AnalyzeComplexContourSynchronizationStep ( audioSyncSettings . ReferenceSourceId , runtimeSeconds ) ;
}
2026-05-22 18:09:17 +01:00
stopwatch . Stop ( ) ;
lastAudioSyncAnalysisMilliseconds = stopwatch . Elapsed . TotalMilliseconds ;
2026-05-22 14:16:50 +01:00
lastAudioSynchronizationReports = synchronization . AudioSynchronizationReports ;
2026-05-22 19:09:11 +01:00
UpdatePassiveSynchronizationConfidence ( lastAudioSynchronizationReports ) ;
2026-05-27 18:03:25 +01:00
lastAudioActuatorFrame = audioActuatorBank . Update (
synchronization . AudioSynchronizationStates ,
audioSyncUpdateIntervalSeconds ) ;
2026-05-27 18:07:04 +01:00
PublishAudioActuatorFrame ( lastAudioActuatorFrame ) ;
2026-05-22 18:09:17 +01:00
nextAudioSyncSeconds = runtimeSeconds + audioSyncUpdateIntervalSeconds ;
2026-05-22 13:15:38 +01:00
}
2026-05-27 18:07:04 +01:00
private void PublishAudioActuatorFrame ( MimirActuatorFrame frame )
{
if ( frame . Commands . Count = = 0 )
{
return ;
}
2026-05-27 18:55:27 +01:00
QueueAudioActuatorProgram ( ) ;
2026-05-27 18:07:04 +01:00
audio . EnqueueControlFrame ( new AquariumAudioControlFrame (
MimirAlignmentActuatorProfile . SixSourceFaust . Id ,
frame . ReferenceSourceId ,
frame . ReferenceHoldbackSamples ,
frame . Commands
. Select ( command = > new AquariumAudioControlCommand (
command . SourceId ,
command . TargetDelaySamples ,
command . ResampleRatio ,
command . Confidence ,
command . FaustControls ) )
. ToArray ( ) ,
frame . TruncatedSourceCount ,
+ + audioActuatorFrameSequence ) ) ;
2026-05-27 20:19:26 +01:00
QueueStreamingActuatorAudioBlock ( frame , audioActuatorFrameSequence ) ;
2026-05-27 18:07:04 +01:00
}
2026-05-27 18:55:27 +01:00
private void QueueAudioActuatorProgram ( )
{
if ( audioActuatorProgramQueued )
{
return ;
}
var profile = MimirAlignmentActuatorProfile . SixSourceFaust ;
var path = ResolveRuntimeAssetPath ( profile . FaustDspPath ) ;
if ( path = = null )
{
Console . WriteLine ( $"Mimir audio actuator DSP not queued; `{profile.FaustDspPath}` was not found." ) ;
return ;
}
audio . EnqueueStreamingDspProgram ( new AquariumStreamingDspProgram (
profile . Id ,
"mimir_alignment_actuator" ,
File . ReadAllText ( path ) ,
2026-05-27 20:29:52 +01:00
unchecked ( ( int ) File . GetLastWriteTimeUtc ( path ) . Ticks ) ,
OutputStems : Enumerable . Range ( 0 , profile . SourceCount )
. Select ( index = > new AquariumStreamingDspOutputStem (
index ,
$"aligned_source_{index}" ,
$"Aligned source {index}" ) )
. ToArray ( ) ) ) ;
2026-05-27 18:55:27 +01:00
audioActuatorProgramQueued = true ;
}
2026-05-27 20:19:26 +01:00
private void QueueStreamingActuatorAudioBlock ( MimirActuatorFrame frame , long sequence )
{
var commands = frame . Commands . OrderBy ( command = > command . SourceId , StringComparer . Ordinal ) . ToArray ( ) ;
if ( commands . Length = = 0 )
{
return ;
}
var buffers = synchronization . Buffers . Buffers
. Where ( buffer = > buffer . Descriptor . Kind = = MimirStreamKind . Audio & & buffer . Latest ? . AudioBlock ! = null )
. ToDictionary ( buffer = > buffer . Descriptor . SourceId , StringComparer . Ordinal ) ;
var channels = new List < AquariumStreamingAudioChannel > ( commands . Length ) ;
var frameCount = int . MaxValue ;
var sampleRate = 0 ;
for ( var index = 0 ; index < commands . Length ; index + + )
{
if ( ! buffers . TryGetValue ( commands [ index ] . SourceId , out var buffer ) | |
buffer . Latest is not { } latest | |
latest . AudioBlock is not { } block | |
latest . Data . IsEmpty )
{
continue ;
}
if ( sampleRate = = 0 )
{
sampleRate = block . SampleRate ;
}
else if ( block . SampleRate ! = sampleRate )
{
continue ;
}
var samples = ExtractMonoAudioBlock ( latest , block ) ;
if ( samples . Length = = 0 )
{
continue ;
}
frameCount = Math . Min ( frameCount , samples . Length ) ;
var channelIndex = FaustSourceIndex ( commands [ index ] ) ;
if ( channelIndex < 0 )
{
continue ;
}
channels . Add ( new AquariumStreamingAudioChannel ( channelIndex , commands [ index ] . SourceId , samples ) ) ;
}
if ( channels . Count = = 0 | | sampleRate < = 0 | | frameCount = = int . MaxValue )
{
return ;
}
if ( channels . Any ( channel = > channel . Samples . Length ! = frameCount ) )
{
channels = channels
. Select ( channel = > channel . Samples . Length = = frameCount
? channel
: channel with { Samples = channel . Samples . AsSpan ( 0 , frameCount ) . ToArray ( ) } )
. ToList ( ) ;
}
audio . EnqueueStreamingAudioBlock ( new AquariumStreamingAudioBlock (
MimirAlignmentActuatorProfile . SixSourceFaust . Id ,
channels ,
frameCount ,
sampleRate ,
sequence ) ) ;
}
2026-05-27 20:34:09 +01:00
private void UpdateObsStemPublication ( )
{
var consumed = false ;
foreach ( var stemFrame in runtimeServices . AudioStems . DrainPublishedFrames ( ) )
{
obsStemPublication . Consume ( stemFrame ) ;
consumed = true ;
}
if ( consumed )
{
lastObsStemPublication = obsStemPublication . Capture ( ) ;
2026-05-27 20:55:48 +01:00
obsStemPublisher ? . Publish ( lastObsStemPublication ) ;
2026-05-27 20:34:09 +01:00
}
}
2026-05-25 07:06:25 +01:00
private void UpdateAudioSpectra ( )
{
if ( runtimeSeconds < nextSpectrumSeconds )
{
return ;
}
var stopwatch = Stopwatch . StartNew ( ) ;
lastAudioSpectra = spectrumAnalyzer . Analyze ( synchronization . Buffers . Buffers , audioSyncSettings . ReferenceSourceId ) ;
2026-05-28 14:01:54 +01:00
if ( lastAudioSpectra . Count = = 0 & & ( syntheticSpectrumPreview | | syntheticSingleTubePreview ) )
2026-05-25 18:26:42 +01:00
{
lastAudioSpectra = GenerateSyntheticSpectrumPreview ( runtimeSeconds ) ;
}
2026-05-25 13:19:13 +01:00
if ( lastAudioSpectra . Count > 0 )
{
2026-05-26 11:14:02 +01:00
spectrumHistory . Enqueue ( new MimirSpectrumHistoryFrame (
spectrumHistorySequence + + ,
runtimeSeconds ,
lastAudioSpectra
. OrderBy ( spectrum = > spectrum . SourceId , StringComparer . Ordinal )
. ToArray ( ) ) ) ;
2026-05-25 13:19:13 +01:00
while ( spectrumHistory . Count > SpectrumHistoryWindowCount )
{
spectrumHistory . Dequeue ( ) ;
}
}
2026-05-25 07:06:25 +01:00
stopwatch . Stop ( ) ;
lastSpectrumAnalysisMilliseconds = stopwatch . Elapsed . TotalMilliseconds ;
nextSpectrumSeconds = runtimeSeconds + spectrumUpdateIntervalSeconds ;
}
2026-05-25 07:13:09 +01:00
private void StartConfiguredSources ( )
{
foreach ( var factory in sourceFactories )
{
var source = factory . Create ( ) ;
if ( source ! = null )
{
synchronization . AddSource ( source ) ;
}
}
}
2026-05-22 13:15:38 +01:00
private void EmitTelemetry ( )
{
if ( telemetryIntervalSeconds < = 0.0f | | runtimeSeconds < nextTelemetrySeconds )
{
return ;
}
var loopback = synchronization . Buffers . Buffers . FirstOrDefault ( buffer = >
2026-05-22 18:49:01 +01:00
string . Equals ( buffer . Descriptor . SourceId , audioSyncSettings . ReferenceSourceId , StringComparison . Ordinal ) ) ;
2026-05-22 13:15:38 +01:00
var states = synchronization . AudioSynchronizationStates ;
Console . WriteLine (
2026-05-22 18:49:01 +01:00
$"mimir-sync-telemetry t={runtimeSeconds:0.00}s audioSync={audioSyncSettings.Mode} loopbackCount={loopback?.Count ?? 0} loopbackEdgeNs={loopback?.EdgeNs ?? 0} reports={lastAudioSynchronizationReports.Count} states={states.Count} analyzeMs={lastAudioSyncAnalysisMilliseconds:0.0} aligned={DescribeAlignedAudio()}" ) ;
2026-05-27 20:34:09 +01:00
if ( lastObsStemPublication . ReadyStems . Count > 0 | | lastObsStemPublication . MissingStemIds . Count > 0 )
{
Console . WriteLine (
$"mimir-stem-telemetry profile={lastObsStemPublication.ConfigurationId} ready={lastObsStemPublication.ReadyStems.Count} missing={lastObsStemPublication.MissingStemIds.Count} unconfigured={lastObsStemPublication.UnconfiguredStemIds.Count} seq={lastObsStemPublication.LatestSequence}" ) ;
}
2026-05-22 13:15:38 +01:00
Console . WriteLine ( $"mimir-sync-buffers {DescribeAudioBuffers()}" ) ;
2026-05-25 20:34:14 +01:00
Console . WriteLine ( $"mimir-spectrum-frustum {DescribeSpectrumFrustum()}" ) ;
2026-05-25 07:06:25 +01:00
foreach ( var spectrum in lastAudioSpectra . OrderBy ( snapshot = > snapshot . SourceId , StringComparer . Ordinal ) )
{
Console . WriteLine (
2026-05-25 07:56:08 +01:00
$"mimir-spectrum {spectrum.SourceId} label=\" { spectrum . Label } \ " rate={spectrum.SampleRate} fft={spectrum.FftSize} rms={spectrum.Rms:0.000000} peak={spectrum.Peak:0.000000} floorDb={spectrum.NoiseFloorDb:0.0} peaks={DescribeSpectrumPeaks(spectrum)}" ) ;
2026-05-25 07:06:25 +01:00
}
2026-05-22 13:15:38 +01:00
foreach ( var report in lastAudioSynchronizationReports . OrderBy ( report = > report . SourceId , StringComparer . Ordinal ) )
{
Console . WriteLine (
2026-05-23 10:27:16 +01:00
$"mimir-sync-report {report.ReferenceSourceId}->{report.SourceId} evidence={report.EvidenceKind} delaySamples={report.FractionalDelaySamples:0.000000} delayUs={report.DelayMicroseconds:0.000} delayMs={report.DelayMilliseconds:0.000} confidence={report.Confidence:0.000} timelineEvents={report.TimelineMatchedEvents} timelineConfidence={report.TimelineConfidence:0.000}" ) ;
2026-05-22 13:15:38 +01:00
}
2026-05-25 06:28:20 +01:00
foreach ( var report in synchronization . ComplexContourSynchronizationReports . OrderBy ( report = > report . SourceId , StringComparer . Ordinal ) )
{
Console . WriteLine (
$"mimir-complex-contour-report {report.ReferenceSourceId}->{report.SourceId} delaySamples={report.FractionalDelaySamples:0.000000} delayUs={report.DelayMicroseconds:0.000} confidence={report.Confidence:0.000} directHits={report.TimelineMatchedEvents}" ) ;
}
2026-05-22 13:15:38 +01:00
foreach ( var state in states )
{
Console . WriteLine (
2026-05-23 10:27:16 +01:00
$"mimir-sync-state {state.ReferenceSourceId}->{state.SourceId} delaySamples={state.SmoothedDelaySamples:0.000000} delayUs={state.DelayMicroseconds:0.000} delayMs={state.DelayMilliseconds:0.000} sroPpm={state.SamplingRateOffsetPpm:0.000} confidence={state.Confidence:0.000}" ) ;
2026-05-22 13:15:38 +01:00
}
2026-05-27 18:03:25 +01:00
if ( lastAudioActuatorFrame . Commands . Count > 0 )
{
Console . WriteLine (
$"mimir-audio-actuator reference={lastAudioActuatorFrame.ReferenceSourceId} referenceHoldbackSamples={lastAudioActuatorFrame.ReferenceHoldbackSamples:0.000000} commands={lastAudioActuatorFrame.Commands.Count} truncated={lastAudioActuatorFrame.TruncatedSourceCount}" ) ;
foreach ( var command in lastAudioActuatorFrame . Commands . OrderBy ( command = > command . SourceId , StringComparer . Ordinal ) )
{
Console . WriteLine (
$"mimir-audio-actuator-command source={command.SourceId} delaySamples={command.TargetDelaySamples:0.000000} ratio={command.ResampleRatio:0.000000000} confidence={command.Confidence:0.000} controls={command.FaustControls.Count}" ) ;
}
}
2026-05-22 17:35:21 +01:00
foreach ( var trace in synchronization . AudioSynchronizationDecodeTraces . OrderBy ( trace = > trace . SourceId , StringComparer . Ordinal ) )
{
Console . WriteLine (
2026-05-22 18:09:17 +01:00
$"mimir-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 13:15:38 +01:00
nextTelemetrySeconds + = telemetryIntervalSeconds ;
}
private static float ParseTelemetryIntervalSeconds ( )
{
return float . TryParse ( Environment . GetEnvironmentVariable ( "MIMIR_SYNC_TELEMETRY_SECONDS" ) , out var seconds )
? Math . Clamp ( seconds , 0.0f , 60.0f )
: 0.0f ;
}
2026-05-27 20:55:48 +01:00
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 ;
}
}
2026-05-27 18:55:27 +01:00
private static string? ResolveRuntimeAssetPath ( string relativePath )
{
if ( File . Exists ( relativePath ) )
{
return Path . GetFullPath ( relativePath ) ;
}
var current = AppContext . BaseDirectory ;
while ( ! string . IsNullOrWhiteSpace ( current ) )
{
var candidate = Path . Combine ( current , relativePath ) ;
if ( File . Exists ( candidate ) )
{
return candidate ;
}
current = Directory . GetParent ( current ) ? . FullName ;
}
return null ;
}
2026-05-27 20:19:26 +01:00
private static float [ ] ExtractMonoAudioBlock ( MimirStreamSample sample , MimirAudioBlockDescriptor block )
{
var bytesPerSample = BytesPerSample ( block . SampleFormat ) ;
if ( bytesPerSample = = 0 | | block . Channels < = 0 | | block . FrameCount < = 0 )
{
return [ ] ;
}
var data = sample . Data . Span ;
var stride = block . Channels * bytesPerSample ;
if ( data . Length < stride )
{
return [ ] ;
}
var frameCount = Math . Min ( block . FrameCount , data . Length / stride ) ;
var output = new float [ frameCount ] ;
for ( var frame = 0 ; frame < frameCount ; frame + + )
{
var sum = 0.0f ;
var frameOffset = frame * stride ;
for ( var channel = 0 ; channel < block . Channels ; channel + + )
{
var offset = frameOffset + channel * bytesPerSample ;
sum + = ReadPcmSample ( data . Slice ( offset , bytesPerSample ) , block . SampleFormat ) ;
}
output [ frame ] = Math . Clamp ( sum / block . Channels , - 1.0f , 1.0f ) ;
}
return output ;
}
private static int FaustSourceIndex ( MimirActuatorCommand command )
{
foreach ( var key in command . FaustControls . Keys )
{
const string prefix = "source" ;
const string suffix = "/delay_samples" ;
if ( ! key . StartsWith ( prefix , StringComparison . Ordinal ) | |
! key . EndsWith ( suffix , StringComparison . Ordinal ) )
{
continue ;
}
var indexText = key [ prefix . Length . . ^ suffix . Length ] ;
if ( int . TryParse ( indexText , out var index ) )
{
return index ;
}
}
return - 1 ;
}
private static int BytesPerSample ( MimirAudioSampleFormat format ) = >
format switch
{
MimirAudioSampleFormat . Float32 = > 4 ,
MimirAudioSampleFormat . Int16 = > 2 ,
MimirAudioSampleFormat . Int24 = > 3 ,
MimirAudioSampleFormat . Int32 = > 4 ,
_ = > 0
} ;
private static float ReadPcmSample ( ReadOnlySpan < byte > data , MimirAudioSampleFormat format ) = >
format switch
{
MimirAudioSampleFormat . Float32 = > BitConverter . Int32BitsToSingle ( BinaryPrimitives . ReadInt32LittleEndian ( data ) ) ,
MimirAudioSampleFormat . Int16 = > BinaryPrimitives . ReadInt16LittleEndian ( data ) / 32768.0f ,
MimirAudioSampleFormat . Int24 = > ReadInt24LittleEndian ( data ) / 8388608.0f ,
MimirAudioSampleFormat . Int32 = > BinaryPrimitives . ReadInt32LittleEndian ( data ) / 2147483648.0f ,
_ = > 0.0f
} ;
private static int ReadInt24LittleEndian ( ReadOnlySpan < byte > data )
{
var value = data [ 0 ] | ( data [ 1 ] < < 8 ) | ( data [ 2 ] < < 16 ) ;
return ( value & 0x800000 ) = = 0 ? value : value | unchecked ( ( int ) 0xff000000 ) ;
}
2026-05-22 18:09:17 +01:00
private static float ParseAudioSyncIntervalSeconds ( )
{
return float . TryParse ( Environment . GetEnvironmentVariable ( "MIMIR_AUDIO_SYNC_INTERVAL_SECONDS" ) , out var seconds )
? Math . Clamp ( seconds , 0.1f , 10.0f )
: DefaultAudioSyncUpdateIntervalSeconds ;
}
2026-05-25 07:06:25 +01:00
private static float ParseSpectrumUpdateIntervalSeconds ( )
{
return float . TryParse ( Environment . GetEnvironmentVariable ( "MIMIR_SPECTRUM_INTERVAL_SECONDS" ) , out var seconds )
? Math . Clamp ( seconds , 0.05f , 10.0f )
: DefaultSpectrumUpdateIntervalSeconds ;
}
private static int ParseSpectrumFftSize ( )
{
return int . TryParse ( Environment . GetEnvironmentVariable ( "MIMIR_SPECTRUM_FFT_SIZE" ) , out var value )
? Math . Clamp ( value , 1024 , 32768 )
: 8192 ;
}
2026-05-25 23:02:44 +01:00
private static int ParseSpectrumBandCount ( )
{
return int . TryParse ( Environment . GetEnvironmentVariable ( "MIMIR_SPECTRUM_BANDS" ) , out var value )
? Math . Clamp ( value , 32 , 192 )
: 96 ;
}
2026-05-27 11:42:03 +01:00
private static int ParseSpectrumTubeSubdivisions ( )
{
return int . TryParse ( Environment . GetEnvironmentVariable ( "MIMIR_SPECTRUM_TUBE_SUBDIVISIONS" ) , out var value )
? Math . Clamp ( value , 1 , 16 )
: 4 ;
}
2026-05-27 11:48:18 +01:00
private static int ParseSpectrumSourceLaneCapacity ( )
{
return int . TryParse ( Environment . GetEnvironmentVariable ( "MIMIR_SPECTRUM_SOURCE_LANES" ) , out var value )
? Math . Clamp ( value , 1 , 32 )
: DefaultSpectrumSourceLaneCapacity ;
}
2026-05-25 18:26:42 +01:00
private static bool IsTruthy ( string? value ) = >
string . Equals ( value , "1" , StringComparison . OrdinalIgnoreCase ) | |
string . Equals ( value , "true" , StringComparison . OrdinalIgnoreCase ) | |
string . Equals ( value , "yes" , StringComparison . OrdinalIgnoreCase ) | |
string . Equals ( value , "on" , StringComparison . OrdinalIgnoreCase ) ;
private static IReadOnlyList < MimirAudioSpectrumSnapshot > GenerateSyntheticSpectrumPreview ( float timeSeconds )
{
2026-05-28 14:01:54 +01:00
if ( IsTruthy ( Environment . GetEnvironmentVariable ( "MIMIR_SYNTHETIC_SINGLE_TUBE_PREVIEW" ) ) )
{
const int singleTubeBandCount = 2 ;
var bands = Enumerable . Repeat ( - 40.0 , singleTubeBandCount ) . ToArray ( ) ;
return
[
new MimirAudioSpectrumSnapshot (
"preview-tube" ,
"Single Tube Proof" ,
192000 ,
8192 ,
8192 ,
0.24 ,
0.56 ,
- 78.0 ,
[] ,
bands ,
( long ) ( timeSeconds * 1_000_000_000.0 ) ) ,
] ;
}
2026-05-25 18:26:42 +01:00
const int bandCount = 48 ;
var snapshots = new MimirAudioSpectrumSnapshot [ 4 ] ;
for ( var channel = 0 ; channel < snapshots . Length ; channel + + )
{
var bands = new double [ bandCount ] ;
for ( var band = 0 ; band < bandCount ; band + + )
{
var x = band / ( double ) Math . Max ( 1 , bandCount - 1 ) ;
var drift = timeSeconds * ( 0.18 + channel * 0.035 ) ;
var formantA = Math . Exp ( - Math . Pow ( ( x - ( 0.18 + 0.05 * Math . Sin ( drift + channel ) ) ) / 0.055 , 2.0 ) ) ;
var formantB = Math . Exp ( - Math . Pow ( ( x - ( 0.48 + 0.08 * Math . Sin ( drift * 1.7 + channel * 0.4 ) ) ) / 0.075 , 2.0 ) ) ;
var formantC = Math . Exp ( - Math . Pow ( ( x - ( 0.78 + 0.03 * Math . Cos ( drift * 2.1 + channel ) ) ) / 0.045 , 2.0 ) ) ;
var ripple = 0.5 + 0.5 * Math . Sin ( ( band * 0.73 ) + drift * 9.0 + channel ) ;
var energy = Math . Clamp ( formantA * 0.8 + formantB * 0.95 + formantC * 0.7 + ripple * 0.10 , 0.0 , 1.0 ) ;
bands [ band ] = - 78.0 + energy * 58.0 ;
}
snapshots [ channel ] = new MimirAudioSpectrumSnapshot (
$"preview-ch{channel + 1}" ,
$"Synthetic Preview {channel + 1}" ,
192000 ,
8192 ,
8192 ,
0.16 + channel * 0.02 ,
0.42 + channel * 0.03 ,
- 78.0 ,
[] ,
bands ,
( long ) ( timeSeconds * 1_000_000_000.0 ) ) ;
}
return snapshots ;
}
2026-05-22 18:49:01 +01:00
private bool ShouldEmitCalibrationTimeline ( )
2026-05-22 17:35:21 +01:00
{
2026-05-23 22:45:53 +01:00
if ( ! HasReferenceAudioBuffer ( ) )
{
return false ;
}
2026-05-22 19:09:11 +01:00
return audioSyncSettings . Mode switch
{
MimirAudioSyncMode . ChirpOnly = > true ,
MimirAudioSyncMode . Passive = > false ,
MimirAudioSyncMode . Hybrid = > lastPassiveSynchronizationConfidence < HybridPassiveConfidenceThreshold ,
_ = > true ,
} ;
2026-05-22 18:49:01 +01:00
}
2026-05-23 22:45:53 +01:00
private bool HasReferenceAudioBuffer ( )
{
return synchronization . Buffers . Buffers . Any ( buffer = >
buffer . Descriptor . Kind = = MimirStreamKind . Audio & &
string . Equals ( buffer . Descriptor . SourceId , audioSyncSettings . ReferenceSourceId , StringComparison . Ordinal ) ) ;
}
2026-05-22 18:49:01 +01:00
private string DescribeAudioSyncMode ( )
{
return audioSyncSettings . Mode switch
{
MimirAudioSyncMode . ChirpOnly = > "chirp-only" ,
2026-05-22 19:09:11 +01:00
MimirAudioSyncMode . Passive = > "passive" ,
MimirAudioSyncMode . Hybrid = > "hybrid" ,
2026-05-22 18:49:01 +01:00
_ = > audioSyncSettings . Mode . ToString ( ) ,
} ;
2026-05-22 17:35:21 +01:00
}
2026-05-22 13:15:38 +01:00
private string DescribeAudioBuffers ( )
{
return string . Join ( " | " , synchronization . Buffers . Buffers
. Where ( buffer = > buffer . Descriptor . Kind = = MimirStreamKind . Audio )
. OrderBy ( buffer = > buffer . Descriptor . SourceId , StringComparer . Ordinal )
2026-05-25 07:56:08 +01:00
. Select ( buffer = > $"{buffer.Descriptor.Label}[{buffer.Descriptor.SourceId}]:{buffer.Count}@{buffer.EdgeNs}" ) ) ;
2026-05-22 13:15:38 +01:00
}
2026-05-25 07:06:25 +01:00
private string DescribeSpectra ( )
{
if ( ! sceneReady )
{
return "Fensalir scene not ready." ;
}
if ( lastAudioSpectra . Count = = 0 )
{
return "No sample-bearing audio buffers yet." ;
}
var builder = new StringBuilder ( ) ;
foreach ( var spectrum in lastAudioSpectra )
{
builder
2026-05-25 07:56:08 +01:00
. Append ( spectrum . Label )
. Append ( " [" )
2026-05-25 07:06:25 +01:00
. Append ( spectrum . SourceId )
2026-05-25 07:56:08 +01:00
. Append ( "]" )
2026-05-25 07:06:25 +01:00
. Append ( " rms=" )
. Append ( spectrum . Rms . ToString ( "0.000000" ) )
. Append ( " peak=" )
. Append ( spectrum . Peak . ToString ( "0.000000" ) )
. Append ( " " )
. Append ( SpectrumBars ( spectrum ) )
. AppendLine ( ) ;
builder
. Append ( " " )
. Append ( DescribeSpectrumPeaks ( spectrum ) )
. AppendLine ( ) ;
}
return builder . ToString ( ) . TrimEnd ( ) ;
}
2026-05-25 20:34:14 +01:00
private string DescribeSpectrumFrustum ( )
{
var frustum = lastSpectrumFrustum . Normalized ( ) ;
var clip = SpectrumClipBounds (
lastSpectrumAabb . Min ,
lastSpectrumAabb . Max ,
lastSpectrumCameraPosition ,
lastSpectrumCameraTarget ,
frustum ) ;
return $"dolly={spectrumCameraDistanceMultiplier:0.0}x angle={spectrumCameraAngleDegrees:0.0}deg L/R={frustum.Left:0.###}/{frustum.Right:0.###} B/T={frustum.Bottom:0.###}/{frustum.Top:0.###} N/F={frustum.Near:0.###}/{frustum.Far:0.###} clip={clip.Min.x:0.###},{clip.Min.y:0.###}->{clip.Max.x:0.###},{clip.Max.y:0.###}" ;
}
private static ( float2 Min , float2 Max ) SpectrumClipBounds (
Vector3 aabbMin ,
Vector3 aabbMax ,
Vector3 cameraPosition ,
Vector3 cameraTarget ,
AquariumCameraFrustum frustum )
{
var min = new float2 ( float . PositiveInfinity , float . PositiveInfinity ) ;
var max = new float2 ( float . NegativeInfinity , float . NegativeInfinity ) ;
foreach ( var corner in SpectrumAabbCorners ( aabbMin , aabbMax ) )
{
var clip = ProjectSpectrumPoint ( corner , cameraPosition , cameraTarget , frustum ) ;
min = math . min ( min , clip . xy ) ;
max = math . max ( max , clip . xy ) ;
}
return ( min , max ) ;
}
2026-05-25 07:06:25 +01:00
private static string SpectrumBars ( MimirAudioSpectrumSnapshot spectrum )
{
const string ramp = " .:-=+*#%@" ;
if ( spectrum . BandDecibels . Count = = 0 )
{
return "" ;
}
var floor = Math . Min ( spectrum . NoiseFloorDb , spectrum . BandDecibels . Min ( ) ) ;
var ceiling = Math . Max ( - 24.0 , spectrum . BandDecibels . Max ( ) ) ;
var span = Math . Max ( 12.0 , ceiling - floor ) ;
var chars = spectrum . BandDecibels . Select ( value = >
{
var normalized = Math . Clamp ( ( value - floor ) / span , 0.0 , 1.0 ) ;
return ramp [ ( int ) Math . Round ( normalized * ( ramp . Length - 1 ) ) ] ;
} ) ;
return new string ( chars . ToArray ( ) ) ;
}
private static string DescribeSpectrumPeaks ( MimirAudioSpectrumSnapshot spectrum )
{
return spectrum . Peaks . Count = = 0
? "no FFT peaks"
: string . Join ( ", " , spectrum . Peaks . Select ( peak = > $"{peak.FrequencyHz / 1000.0:0.00}kHz {peak.Decibels:0.0}dB" ) ) ;
}
2026-05-22 19:09:11 +01:00
private void UpdatePassiveSynchronizationConfidence ( IReadOnlyList < MimirAudioSynchronizationReport > reports )
{
var passiveConfidence = reports
. Where ( report = > string . Equals ( report . EvidenceKind , "passive" , StringComparison . Ordinal ) )
. Select ( report = > report . Confidence )
. DefaultIfEmpty ( 0.0 )
. Max ( ) ;
lastPassiveSynchronizationConfidence = passiveConfidence > 0.0
? passiveConfidence
: lastPassiveSynchronizationConfidence * 0.95 ;
}
2026-05-22 00:37:24 +01:00
private static string DescribeBuffer ( MimirRollingStreamBuffer buffer )
{
var latest = buffer . Latest ;
if ( latest ? . VideoFrame is { } frame )
{
2026-05-25 07:56:08 +01:00
return $"{buffer.Descriptor.Label} [{buffer.Descriptor.SourceId}]: {buffer.Count} {frame.Width}x{frame.Height} {frame.PixelFormat} bytes {latest.Value.ByteLength} edge {buffer.EdgeNs}" ;
2026-05-22 00:37:24 +01:00
}
2026-05-22 01:07:30 +01:00
if ( latest ? . AudioBlock is { } block )
{
2026-05-25 07:56:08 +01:00
return $"{buffer.Descriptor.Label} [{buffer.Descriptor.SourceId}]: {buffer.Count} {block.Channels}ch {block.SampleRate}Hz {block.SampleFormat} frames {block.FrameCount} bytes {latest.Value.ByteLength} edge {buffer.EdgeNs}" ;
2026-05-22 01:07:30 +01:00
}
2026-05-25 07:56:08 +01:00
return $"{buffer.Descriptor.Label} [{buffer.Descriptor.SourceId}]: {buffer.Count} edge {buffer.EdgeNs}" ;
2026-05-22 00:37:24 +01:00
}
2026-05-22 01:22:36 +01:00
private string DescribeAudioSync ( )
{
2026-05-22 19:09:11 +01:00
if ( audioSyncSettings . Mode = = MimirAudioSyncMode . Passive )
2026-05-22 18:49:01 +01:00
{
2026-05-22 19:09:11 +01:00
return "passive mode; waiting for program-audio coherence" ;
2026-05-22 18:49:01 +01:00
}
2026-05-22 14:16:50 +01:00
var reports = synchronization . AudioSynchronizationReports ;
2026-05-22 01:22:36 +01:00
return reports . Count = = 0
? "no payload windows"
2026-05-23 10:27:16 +01:00
: string . Join ( " | " , reports . Select ( report = > $"{report.SourceId}: {report.FractionalDelaySamples:0.000} samples {report.DelayMicroseconds:0.0}us c={report.Confidence:0.00} {report.EvidenceKind} events={report.TimelineMatchedEvents}" ) ) ;
2026-05-22 01:22:36 +01:00
}
2026-05-22 11:40:56 +01:00
private string DescribeAlignedAudio ( )
{
2026-05-27 18:03:25 +01:00
var commands = lastAudioActuatorFrame . Commands ;
return commands . Count = = 0
2026-05-22 14:16:50 +01:00
? "no aligned state"
2026-05-27 18:03:25 +01:00
: $"{commands.Count + 1}ch commands-ready refHold={lastAudioActuatorFrame.ReferenceHoldbackSamples:0.000} samples" ;
2026-05-22 11:40:56 +01:00
}
2026-05-22 12:46:51 +01:00
private string DescribeAudioSyncState ( )
{
var states = synchronization . AudioSynchronizationStates ;
return states . Count = = 0
? "no sync state"
2026-05-23 10:27:16 +01:00
: string . Join ( " | " , states . Select ( state = > $"{state.SourceId}: {state.SmoothedDelaySamples:0.000} samples {state.DelayMicroseconds:0.0}us sro={state.SamplingRateOffsetPpm:0.0}ppm c={state.Confidence:0.00}" ) ) ;
2026-05-22 12:46:51 +01:00
}
2026-05-26 11:14:02 +01:00
private readonly record struct MimirSpectrumHistoryFrame (
long Sequence ,
float RuntimeSeconds ,
IReadOnlyList < MimirAudioSpectrumSnapshot > Spectra ) ;
2026-05-21 13:34:03 +01:00
}