On-demand audio visualizer capture + UI fixes
- Audio capture starts only when first client subscribes, stops when last client unsubscribes (saves CPU/battery) - Add lifecycle lock to AudioAnalyzer for thread-safe start/stop - Status badge uses local visualizer state instead of server flag - Fix script name vertical text break on narrow screens - Fix script grid minimum column width on small viewports Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -46,10 +46,16 @@ class ConnectionManager:
|
||||
logger.debug("Failed to send initial status: %s", e)
|
||||
|
||||
async def disconnect(self, websocket: WebSocket) -> None:
|
||||
"""Remove a WebSocket connection."""
|
||||
"""Remove a WebSocket connection. Stops audio capture if last visualizer subscriber."""
|
||||
should_stop = False
|
||||
async with self._lock:
|
||||
self._active_connections.discard(websocket)
|
||||
was_subscriber = websocket in self._visualizer_subscribers
|
||||
self._visualizer_subscribers.discard(websocket)
|
||||
if was_subscriber and len(self._visualizer_subscribers) == 0:
|
||||
should_stop = True
|
||||
if should_stop:
|
||||
await self._maybe_stop_capture()
|
||||
logger.info(
|
||||
"WebSocket client disconnected. Total: %d", len(self._active_connections)
|
||||
)
|
||||
@@ -90,23 +96,50 @@ class ConnectionManager:
|
||||
logger.info("Broadcast sent: links_changed")
|
||||
|
||||
async def subscribe_visualizer(self, websocket: WebSocket) -> None:
|
||||
"""Subscribe a client to audio visualizer data."""
|
||||
"""Subscribe a client to audio visualizer data. Starts capture on first subscriber."""
|
||||
should_start = False
|
||||
async with self._lock:
|
||||
self._visualizer_subscribers.add(websocket)
|
||||
if len(self._visualizer_subscribers) == 1 and self._audio_analyzer:
|
||||
should_start = True
|
||||
if should_start:
|
||||
await self._maybe_start_capture()
|
||||
logger.debug("Visualizer subscriber added. Total: %d", len(self._visualizer_subscribers))
|
||||
|
||||
async def unsubscribe_visualizer(self, websocket: WebSocket) -> None:
|
||||
"""Unsubscribe a client from audio visualizer data."""
|
||||
"""Unsubscribe a client from audio visualizer data. Stops capture on last subscriber."""
|
||||
should_stop = False
|
||||
async with self._lock:
|
||||
self._visualizer_subscribers.discard(websocket)
|
||||
if len(self._visualizer_subscribers) == 0:
|
||||
should_stop = True
|
||||
if should_stop:
|
||||
await self._maybe_stop_capture()
|
||||
logger.debug("Visualizer subscriber removed. Total: %d", len(self._visualizer_subscribers))
|
||||
|
||||
async def _maybe_start_capture(self) -> None:
|
||||
"""Start audio capture if not already running (called on first subscriber)."""
|
||||
if self._audio_analyzer and not self._audio_analyzer.running:
|
||||
loop = asyncio.get_event_loop()
|
||||
started = await loop.run_in_executor(None, self._audio_analyzer.start)
|
||||
if started:
|
||||
logger.info("Audio capture started (first subscriber)")
|
||||
else:
|
||||
logger.warning("Audio capture failed to start")
|
||||
|
||||
async def _maybe_stop_capture(self) -> None:
|
||||
"""Stop audio capture if running (called when last subscriber leaves)."""
|
||||
if self._audio_analyzer and self._audio_analyzer.running:
|
||||
loop = asyncio.get_event_loop()
|
||||
await loop.run_in_executor(None, self._audio_analyzer.stop)
|
||||
logger.info("Audio capture stopped (no subscribers)")
|
||||
|
||||
async def start_audio_monitor(self, analyzer) -> None:
|
||||
"""Start audio frequency broadcasting if analyzer is available."""
|
||||
"""Register the audio analyzer. Capture starts on-demand when clients subscribe."""
|
||||
self._audio_analyzer = analyzer
|
||||
if analyzer and analyzer.running:
|
||||
if analyzer and analyzer.available:
|
||||
self._audio_task = asyncio.create_task(self._audio_broadcast_loop())
|
||||
logger.info("Audio visualizer broadcast started")
|
||||
logger.info("Audio visualizer broadcast loop started (capture on-demand)")
|
||||
|
||||
async def stop_audio_monitor(self) -> None:
|
||||
"""Stop audio frequency broadcasting."""
|
||||
@@ -153,12 +186,8 @@ class ConnectionManager:
|
||||
results = await asyncio.gather(*(_send(ws) for ws in subscribers))
|
||||
|
||||
failed = [ws for ws in results if ws is not None]
|
||||
if failed:
|
||||
async with self._lock:
|
||||
for ws in failed:
|
||||
self._visualizer_subscribers.discard(ws)
|
||||
for ws in failed:
|
||||
await self.disconnect(ws)
|
||||
for ws in failed:
|
||||
await self.disconnect(ws)
|
||||
|
||||
await asyncio.sleep(interval)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user