Consume clap calibration events in LocalCast
This commit is contained in:
parent
3ef78937a8
commit
4b06c2b50a
4 changed files with 273 additions and 2 deletions
|
|
@ -23,8 +23,11 @@ public sealed class LocalCastRuntime : IAquariumRuntime
|
|||
private float timeSeconds;
|
||||
private float sceneTimelineSeconds;
|
||||
private long lastFrameId = -1;
|
||||
private long lastClapFrameId = -1;
|
||||
private int latestPointCount;
|
||||
private int latestClapCount;
|
||||
private string latestStatus = "waiting";
|
||||
private AquariumCalibrationEventFrame latestCalibrationEvents = AquariumCalibrationEventFrame.Empty;
|
||||
|
||||
public LocalCastRuntime(AquariumRuntimeOptions options)
|
||||
{
|
||||
|
|
@ -36,6 +39,7 @@ public sealed class LocalCastRuntime : IAquariumRuntime
|
|||
panel.Section("Camera Field");
|
||||
panel.Readout("Cache", () => reader.Path);
|
||||
panel.Readout("Frame", () => lastFrameId >= 0 ? $"{lastFrameId} / {latestPointCount} splats" : latestStatus);
|
||||
panel.Readout("Claps", () => lastClapFrameId >= 0 ? $"{lastClapFrameId} / {latestClapCount} events" : "none");
|
||||
panel.Readout("History", () => $"{gpuFusionAccumulator.TrackCount} tracked seeds");
|
||||
panel.Readout("Timeline", () => $"{sceneTimelineSeconds:0.000}s");
|
||||
});
|
||||
|
|
@ -66,6 +70,7 @@ public sealed class LocalCastRuntime : IAquariumRuntime
|
|||
TemporalGaussianField = gpuFusionField.Seeds.Count == 0
|
||||
? temporalField
|
||||
: AquariumTemporalGaussianField.Empty,
|
||||
CalibrationEventFrame = latestCalibrationEvents,
|
||||
GpuFusionField = gpuFusionField,
|
||||
};
|
||||
|
||||
|
|
@ -109,6 +114,13 @@ public sealed class LocalCastRuntime : IAquariumRuntime
|
|||
latestStatus = "live";
|
||||
}
|
||||
|
||||
if (reader.TryReadLatestClapEvents(out var clapFrame) && clapFrame.FrameId != lastClapFrameId)
|
||||
{
|
||||
latestCalibrationEvents = MapClapEvents(clapFrame);
|
||||
lastClapFrameId = clapFrame.FrameId;
|
||||
latestClapCount = clapFrame.Events.Count;
|
||||
}
|
||||
|
||||
sceneTimelineSeconds = MathF.Max(
|
||||
sceneTimelineSeconds + Math.Max(deltaSeconds, 0.0f),
|
||||
gpuFusionMapper.ToTimelineSeconds(frame.PresentTimeNs));
|
||||
|
|
@ -146,6 +158,23 @@ public sealed class LocalCastRuntime : IAquariumRuntime
|
|||
? Math.Clamp(seconds, 1.0f, 120.0f)
|
||||
: 18.0f;
|
||||
}
|
||||
|
||||
private static AquariumCalibrationEventFrame MapClapEvents(LocalCastClapCalibrationFrame frame)
|
||||
{
|
||||
return new AquariumCalibrationEventFrame
|
||||
{
|
||||
ClapEvents = frame.Events
|
||||
.Select(item => new AquariumClapCalibrationEvent(
|
||||
item.StableKey,
|
||||
item.PositionMeters,
|
||||
item.AcousticOracleNs,
|
||||
item.VisualObservedNs,
|
||||
item.TimingUncertaintyMicroseconds,
|
||||
item.VisualConfidence,
|
||||
item.AcousticConfidence))
|
||||
.ToArray(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class LocalCastRuntimeFactory : IAquariumRuntimeFactory
|
||||
|
|
|
|||
|
|
@ -34,3 +34,23 @@ public readonly record struct LocalCastVisualPoint(
|
|||
Vector4 ColorOpacity,
|
||||
float Confidence,
|
||||
long SourceTimestampNs);
|
||||
|
||||
public sealed class LocalCastClapCalibrationFrame
|
||||
{
|
||||
public required string SchemaVersion { get; init; }
|
||||
|
||||
public required long FrameId { get; init; }
|
||||
|
||||
public required long CreatedMonotonicNs { get; init; }
|
||||
|
||||
public required IReadOnlyList<LocalCastClapCalibrationEvent> Events { get; init; }
|
||||
}
|
||||
|
||||
public readonly record struct LocalCastClapCalibrationEvent(
|
||||
string StableKey,
|
||||
Vector3 PositionMeters,
|
||||
long AcousticOracleNs,
|
||||
long VisualObservedNs,
|
||||
float TimingUncertaintyMicroseconds,
|
||||
float VisualConfidence,
|
||||
float AcousticConfidence);
|
||||
|
|
|
|||
|
|
@ -9,6 +9,9 @@ public sealed class LocalCastVisualStateReader
|
|||
public const string LiveRenderFrameKey = "localcast.visual.render-frame.live";
|
||||
public const string RenderFrameType = "localcast.visual.render_frame";
|
||||
public const string RenderFrameSchemaId = "gamecult.localcast.visual.render_frame.v1";
|
||||
public const string LiveClapEventsKey = "localcast.calibration.clap-events.live";
|
||||
public const string ClapEventsType = "localcast.calibration.clap_events";
|
||||
public const string ClapEventsSchemaId = "gamecult.localcast.calibration.clap_events.v1";
|
||||
|
||||
private readonly string path;
|
||||
|
||||
|
|
@ -66,6 +69,53 @@ public sealed class LocalCastVisualStateReader
|
|||
}
|
||||
}
|
||||
|
||||
public bool TryReadLatestClapEvents(out LocalCastClapCalibrationFrame frame)
|
||||
{
|
||||
frame = null!;
|
||||
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
byte[] bytes;
|
||||
try
|
||||
{
|
||||
using var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
|
||||
bytes = new byte[stream.Length];
|
||||
var read = stream.Read(bytes, 0, bytes.Length);
|
||||
if (read != bytes.Length)
|
||||
{
|
||||
Array.Resize(ref bytes, read);
|
||||
}
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
catch (UnauthorizedAccessException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return TryParseClapEventsStore(bytes, out frame);
|
||||
}
|
||||
catch (MessagePackSerializationException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
catch (EndOfStreamException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool TryParseStore(ReadOnlyMemory<byte> payload, out LocalCastVisualFrame frame)
|
||||
{
|
||||
frame = null!;
|
||||
|
|
@ -112,6 +162,52 @@ public sealed class LocalCastVisualStateReader
|
|||
return false;
|
||||
}
|
||||
|
||||
public static bool TryParseClapEventsStore(ReadOnlyMemory<byte> payload, out LocalCastClapCalibrationFrame frame)
|
||||
{
|
||||
frame = null!;
|
||||
var reader = new MessagePackReader(payload);
|
||||
var recordCount = reader.ReadArrayHeader();
|
||||
|
||||
for (var recordIndex = 0; recordIndex < recordCount; recordIndex++)
|
||||
{
|
||||
var mapCount = reader.ReadMapHeader();
|
||||
string? key = null;
|
||||
string? type = null;
|
||||
byte[]? documentPayload = null;
|
||||
|
||||
for (var mapIndex = 0; mapIndex < mapCount; mapIndex++)
|
||||
{
|
||||
var field = reader.ReadString();
|
||||
switch (field)
|
||||
{
|
||||
case "key":
|
||||
key = reader.ReadString();
|
||||
break;
|
||||
case "type":
|
||||
type = reader.ReadString();
|
||||
break;
|
||||
case "payload":
|
||||
var bytes = reader.ReadBytes();
|
||||
documentPayload = bytes.HasValue ? bytes.Value.ToArray() : null;
|
||||
break;
|
||||
default:
|
||||
reader.Skip();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (string.Equals(key, LiveClapEventsKey, StringComparison.Ordinal)
|
||||
&& string.Equals(type, ClapEventsType, StringComparison.Ordinal)
|
||||
&& documentPayload is not null)
|
||||
{
|
||||
frame = ParseClapEvents(documentPayload);
|
||||
return string.Equals(frame.SchemaVersion, ClapEventsSchemaId, StringComparison.Ordinal);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static LocalCastVisualFrame ParseRenderFrame(ReadOnlyMemory<byte> payload)
|
||||
{
|
||||
var reader = new MessagePackReader(payload);
|
||||
|
|
@ -144,6 +240,31 @@ public sealed class LocalCastVisualStateReader
|
|||
return frame;
|
||||
}
|
||||
|
||||
public static LocalCastClapCalibrationFrame ParseClapEvents(ReadOnlyMemory<byte> payload)
|
||||
{
|
||||
var reader = new MessagePackReader(payload);
|
||||
var fieldCount = reader.ReadArrayHeader();
|
||||
if (fieldCount < 4)
|
||||
{
|
||||
throw new InvalidOperationException($"Clap events payload has {fieldCount} fields; expected at least 4.");
|
||||
}
|
||||
|
||||
var frame = new LocalCastClapCalibrationFrame
|
||||
{
|
||||
SchemaVersion = reader.ReadString() ?? string.Empty,
|
||||
FrameId = reader.ReadInt64(),
|
||||
CreatedMonotonicNs = reader.ReadInt64(),
|
||||
Events = ReadClapEvents(ref reader),
|
||||
};
|
||||
|
||||
for (var fieldIndex = 4; fieldIndex < fieldCount; fieldIndex++)
|
||||
{
|
||||
reader.Skip();
|
||||
}
|
||||
|
||||
return frame;
|
||||
}
|
||||
|
||||
private static IReadOnlyList<LocalCastVisualPoint> ReadPoints(ref MessagePackReader reader)
|
||||
{
|
||||
var count = reader.ReadArrayHeader();
|
||||
|
|
@ -179,6 +300,44 @@ public sealed class LocalCastVisualStateReader
|
|||
return points;
|
||||
}
|
||||
|
||||
private static IReadOnlyList<LocalCastClapCalibrationEvent> ReadClapEvents(ref MessagePackReader reader)
|
||||
{
|
||||
var count = reader.ReadArrayHeader();
|
||||
var events = new LocalCastClapCalibrationEvent[count];
|
||||
for (var index = 0; index < count; index++)
|
||||
{
|
||||
var fieldCount = reader.ReadArrayHeader();
|
||||
if (fieldCount < 8)
|
||||
{
|
||||
throw new InvalidOperationException($"Clap event payload has {fieldCount} fields; expected at least 8.");
|
||||
}
|
||||
|
||||
var stableKey = reader.ReadString() ?? string.Empty;
|
||||
var position = ReadVector3(ref reader);
|
||||
var acousticOracleNs = reader.ReadInt64();
|
||||
var visualObservedNs = reader.ReadInt64();
|
||||
var timingUncertaintyUs = reader.ReadSingle();
|
||||
var visualConfidence = reader.ReadSingle();
|
||||
var acousticConfidence = reader.ReadSingle();
|
||||
reader.Skip();
|
||||
for (var fieldIndex = 8; fieldIndex < fieldCount; fieldIndex++)
|
||||
{
|
||||
reader.Skip();
|
||||
}
|
||||
|
||||
events[index] = new LocalCastClapCalibrationEvent(
|
||||
stableKey,
|
||||
position,
|
||||
acousticOracleNs,
|
||||
visualObservedNs,
|
||||
timingUncertaintyUs,
|
||||
Math.Clamp(visualConfidence, 0.0f, 1.0f),
|
||||
Math.Clamp(acousticConfidence, 0.0f, 1.0f));
|
||||
}
|
||||
|
||||
return events;
|
||||
}
|
||||
|
||||
private static Vector3 ReadVector3(ref MessagePackReader reader)
|
||||
{
|
||||
var count = reader.ReadArrayHeader();
|
||||
|
|
|
|||
|
|
@ -19,12 +19,27 @@ public sealed class LocalCastVisualStateReaderTests
|
|||
Assert.Equal(0.03f, point.RadiusMeters, 6);
|
||||
}
|
||||
|
||||
private static byte[] WriteStore()
|
||||
[Fact]
|
||||
public void ParsesPythonCultCacheClapEventsStore()
|
||||
{
|
||||
var store = WriteStore(includeClapEvents: true);
|
||||
|
||||
Assert.True(LocalCastVisualStateReader.TryParseClapEventsStore(store, out var frame));
|
||||
Assert.Equal(12, frame.FrameId);
|
||||
var clap = Assert.Single(frame.Events);
|
||||
Assert.Equal("clap:host:0001", clap.StableKey);
|
||||
Assert.Equal(123_456_789_000L, clap.AcousticOracleNs);
|
||||
Assert.Equal(1.8f, clap.TimingUncertaintyMicroseconds, 6);
|
||||
Assert.True(clap.AcousticConfidence > clap.VisualConfidence);
|
||||
}
|
||||
|
||||
private static byte[] WriteStore(bool includeClapEvents = false)
|
||||
{
|
||||
var payload = WriteRenderFramePayload();
|
||||
var recordCount = includeClapEvents ? 2 : 1;
|
||||
var buffer = new ArrayBufferWriter<byte>();
|
||||
var writer = new MessagePackWriter(buffer);
|
||||
writer.WriteArrayHeader(1);
|
||||
writer.WriteArrayHeader(recordCount);
|
||||
writer.WriteMapHeader(4);
|
||||
writer.Write("key");
|
||||
writer.Write(LocalCastVisualStateReader.LiveRenderFrameKey);
|
||||
|
|
@ -34,6 +49,18 @@ public sealed class LocalCastVisualStateReaderTests
|
|||
writer.Write("2026-05-18T18:35:15Z");
|
||||
writer.Write("payload");
|
||||
writer.Write(payload);
|
||||
if (includeClapEvents)
|
||||
{
|
||||
writer.WriteMapHeader(4);
|
||||
writer.Write("key");
|
||||
writer.Write(LocalCastVisualStateReader.LiveClapEventsKey);
|
||||
writer.Write("type");
|
||||
writer.Write(LocalCastVisualStateReader.ClapEventsType);
|
||||
writer.Write("storedAt");
|
||||
writer.Write("2026-05-18T18:35:15Z");
|
||||
writer.Write("payload");
|
||||
writer.Write(WriteClapEventsPayload());
|
||||
}
|
||||
writer.Flush();
|
||||
return buffer.WrittenMemory.ToArray();
|
||||
}
|
||||
|
|
@ -71,4 +98,40 @@ public sealed class LocalCastVisualStateReaderTests
|
|||
writer.Flush();
|
||||
return buffer.WrittenMemory.ToArray();
|
||||
}
|
||||
|
||||
private static byte[] WriteClapEventsPayload()
|
||||
{
|
||||
var buffer = new ArrayBufferWriter<byte>();
|
||||
var writer = new MessagePackWriter(buffer);
|
||||
writer.WriteArrayHeader(4);
|
||||
writer.Write(LocalCastVisualStateReader.ClapEventsSchemaId);
|
||||
writer.Write(12L);
|
||||
writer.Write(1_000_000L);
|
||||
writer.WriteArrayHeader(1);
|
||||
writer.WriteArrayHeader(8);
|
||||
writer.Write("clap:host:0001");
|
||||
writer.WriteArrayHeader(3);
|
||||
writer.Write(0.12f);
|
||||
writer.Write(0.4f);
|
||||
writer.Write(1.35f);
|
||||
writer.Write(123_456_789_000L);
|
||||
writer.Write(123_456_805_000L);
|
||||
writer.Write(1.8f);
|
||||
writer.Write(0.82f);
|
||||
writer.Write(0.97f);
|
||||
writer.WriteArrayHeader(1);
|
||||
writer.WriteMapHeader(4);
|
||||
writer.Write("sensorId");
|
||||
writer.Write("kiyo-pro");
|
||||
writer.Write("timestampNs");
|
||||
writer.Write(123_456_805_000L);
|
||||
writer.Write("uv");
|
||||
writer.WriteArrayHeader(2);
|
||||
writer.Write(320.0f);
|
||||
writer.Write(240.0f);
|
||||
writer.Write("score");
|
||||
writer.Write(0.9f);
|
||||
writer.Flush();
|
||||
return buffer.WrittenMemory.ToArray();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue