From 472515943b848feaa1607d59021afe2edb404272 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Tue, 19 May 2026 11:08:19 +0100 Subject: [PATCH] Move reservoir window clipping into core --- localcast/diagnostics/visual_producer.py | 23 ++++-------------- localcast/sensor_fusion/audio_overlay.py | 10 ++++---- localcast/sensor_fusion/reservoir_window.py | 26 +++++++++++++++++++++ state/evidence.jsonl | 1 + tests/test_sensor_fusion.py | 4 +--- 5 files changed, 39 insertions(+), 25 deletions(-) create mode 100644 localcast/sensor_fusion/reservoir_window.py diff --git a/localcast/diagnostics/visual_producer.py b/localcast/diagnostics/visual_producer.py index b7cdcd2..572af9d 100644 --- a/localcast/diagnostics/visual_producer.py +++ b/localcast/diagnostics/visual_producer.py @@ -49,12 +49,14 @@ from localcast.diagnostics.visual_cache import ( from localcast.diagnostics.lod_json import write_lod_cache_json from localcast.sensor_fusion.camera_control import AdaptiveCameraController, OpenCvCameraSettingPort from localcast.sensor_fusion.adapters import LatestFramePump, OpenCvCaptureConfig, OpenCvFrameSource +from localcast.sensor_fusion.reservoir_window import ( + DEFAULT_RESERVOIR_NS, + evidence_in_reservoir, + render_points_in_reservoir, +) from audio_field.cultcache_audio import frame_to_numpy, get_live_audio_phase_field, get_live_spatial_audio_frame -DEFAULT_RESERVOIR_NS = 5_000_000_000 - - def acquire_runtime_lock(path: Path) -> BinaryIO | None: import msvcrt @@ -72,21 +74,6 @@ def acquire_runtime_lock(path: Path) -> BinaryIO | None: return handle -def reservoir_window_ns(latest_timestamp_ns: int, reservoir_ns: int) -> tuple[int, int]: - end_ns = int(latest_timestamp_ns) - return end_ns - int(reservoir_ns), end_ns - - -def evidence_in_reservoir(evidence: tuple, *, latest_timestamp_ns: int, reservoir_ns: int) -> tuple: - start_ns, end_ns = reservoir_window_ns(latest_timestamp_ns, reservoir_ns) - return tuple(item for item in evidence if start_ns <= int(item.timestamp_ns) <= end_ns) - - -def render_points_in_reservoir(points: tuple[RenderPointPacket, ...], *, latest_timestamp_ns: int, reservoir_ns: int) -> tuple[RenderPointPacket, ...]: - start_ns, end_ns = reservoir_window_ns(latest_timestamp_ns, reservoir_ns) - return tuple(point for point in points if start_ns <= int(point.source_timestamp_ns) <= end_ns) - - def latest_audio_time_ns(audio_cache: Path) -> int | None: if not audio_cache.exists(): return None diff --git a/localcast/sensor_fusion/audio_overlay.py b/localcast/sensor_fusion/audio_overlay.py index 0d88734..51afe48 100644 --- a/localcast/sensor_fusion/audio_overlay.py +++ b/localcast/sensor_fusion/audio_overlay.py @@ -8,6 +8,7 @@ import numpy as np from .render_bridge import RenderFramePacket, RenderPointPacket from .media_artifacts import RemoteVideoArtifact +from .reservoir_window import DEFAULT_RESERVOIR_NS, render_points_in_reservoir ROOT = Path(__file__).resolve().parents[2] @@ -17,9 +18,6 @@ if str(ROOT) not in sys.path: from audio_field.cultcache_audio import CultAudioSourceEvents, CultSpatialAudioFrame # noqa: E402 -DEFAULT_RESERVOIR_NS = 5_000_000_000 - - @dataclass(frozen=True) class AudioVisualSyncStatus: visual_frame_id: int @@ -90,7 +88,11 @@ def clamp_frame_to_audio_reservoir( return frame edge_ns = max(int(frame.source_time_max_ns), int(audio_frame.audio_time_ns)) start_ns = edge_ns - int(reservoir_ns) - points = tuple(point for point in frame.points if start_ns <= int(point.source_timestamp_ns) <= edge_ns) + points = render_points_in_reservoir( + frame.points, + latest_timestamp_ns=edge_ns, + reservoir_ns=reservoir_ns, + ) return RenderFramePacket( schema=frame.schema, frame_id=frame.frame_id, diff --git a/localcast/sensor_fusion/reservoir_window.py b/localcast/sensor_fusion/reservoir_window.py new file mode 100644 index 0000000..4b5c2d0 --- /dev/null +++ b/localcast/sensor_fusion/reservoir_window.py @@ -0,0 +1,26 @@ +"""Shared reservoir-window clipping rules for visual evidence.""" + +from .render_bridge import RenderPointPacket + + +DEFAULT_RESERVOIR_NS = 5_000_000_000 + + +def reservoir_window_ns(latest_timestamp_ns: int, reservoir_ns: int) -> tuple[int, int]: + end_ns = int(latest_timestamp_ns) + return end_ns - int(reservoir_ns), end_ns + + +def evidence_in_reservoir(evidence: tuple, *, latest_timestamp_ns: int, reservoir_ns: int) -> tuple: + start_ns, end_ns = reservoir_window_ns(latest_timestamp_ns, reservoir_ns) + return tuple(item for item in evidence if start_ns <= int(item.timestamp_ns) <= end_ns) + + +def render_points_in_reservoir( + points: tuple[RenderPointPacket, ...], + *, + latest_timestamp_ns: int, + reservoir_ns: int, +) -> tuple[RenderPointPacket, ...]: + start_ns, end_ns = reservoir_window_ns(latest_timestamp_ns, reservoir_ns) + return tuple(point for point in points if start_ns <= int(point.source_timestamp_ns) <= end_ns) diff --git a/state/evidence.jsonl b/state/evidence.jsonl index 7771ac4..8757296 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -74,3 +74,4 @@ {"ts": "2026-05-19T10:00:32+00:00", "type": "implementation", "status": "ok", "note": "AmbiX spatial-audio Python publisher moved from scripts into localcast.diagnostics.spatial_audio; docs and handoff now treat it as replay smoke-test code while live spatial/stem production remains native/Faust through LocalcastProducer."} {"ts": "2026-05-19T10:03:37+00:00", "type": "implementation", "status": "ok", "note": "Native reservoir ABI now includes LocalcastAudioBlockDescriptor and localcast_producer_push_audio_block: audio producers pass caller-owned float32 interleaved block descriptors as payload handles while the reservoir keeps timing/retention ownership only."} {"ts": "2026-05-19T10:05:55+00:00", "type": "implementation", "status": "ok", "note": "Aquarium-Engine commit 34694e6 adds the C# LocalCast audio block descriptor binding and layout tests for the native LocalcastAudioBlockDescriptor/localcast_producer_push_audio_block ABI."} +{"ts": "2026-05-19T10:07:55+00:00", "type": "implementation", "status": "ok", "note": "Reservoir-window clipping moved out of localcast.diagnostics.visual_producer into localcast.sensor_fusion.reservoir_window; audio overlay and tests now use the shared core invariant instead of importing diagnostic launcher code."} diff --git a/tests/test_sensor_fusion.py b/tests/test_sensor_fusion.py index 0587e77..065c9f0 100644 --- a/tests/test_sensor_fusion.py +++ b/tests/test_sensor_fusion.py @@ -64,16 +64,14 @@ from localcast.diagnostics.render_math import ( rasterize_frame_rgba, ) from localcast.diagnostics.visual_producer import ( - DEFAULT_RESERVOIR_NS, LiveClapCalibrator, - evidence_in_reservoir, leap_channel_motion_points, - render_points_in_reservoir, rgb_dense_camera, rgb_dense_stereo_splats, rgb_room_splats, unpack_leap_packed_channels, ) +from localcast.sensor_fusion.reservoir_window import DEFAULT_RESERVOIR_NS, evidence_in_reservoir, render_points_in_reservoir from localcast.sensor_fusion.active_illumination import ActiveIlluminationController, IlluminationPulsePlan from audio_field.cultcache_audio import make_audio_source_events, make_spatial_audio_frame, put_live_spatial_audio_frame from localcast.sensor_fusion.adapters import read_raw_bgr_frames