feat(collector): добавлен сбор медианной частоты и громкости

This commit is contained in:
2025-12-28 23:07:49 +03:00
parent bcc94b40fe
commit 734c65253d
2 changed files with 162 additions and 31 deletions

View File

@@ -1,12 +1,16 @@
#!/usr/bin/env python3
"""FastAPI WebSocket endpoint for live audio streaming."""
"""FastAPI WebSocket endpoint with rate limiting support."""
from __future__ import annotations
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
import logging
from fastapi import FastAPI, WebSocket, WebSocketDisconnect, Query
from ws_manager import ConnectionManager
logger = logging.getLogger(__name__)
app = FastAPI(title="Audio Analyzer WebSocket")
manager = ConnectionManager()
@@ -18,16 +22,28 @@ async def health() -> dict[str, str]:
@app.websocket("/ws/live")
async def ws_live(websocket: WebSocket) -> None:
"""WebSocket endpoint for real-time audio data streaming."""
await manager.connect(websocket)
async def ws_live(
websocket: WebSocket,
hz: float = Query(default=10.0, ge=0.1, le=100.0, description="Update rate in Hz"),
) -> None:
"""
WebSocket endpoint for real-time audio data streaming.
Query parameters:
hz: Update rate in Hz (0.1 - 100.0, default: 10.0)
Examples:
- ws://localhost:8001/ws/live?hz=10 → 10 messages/sec
- ws://localhost:8001/ws/live?hz=1 → 1 message/sec
- ws://localhost:8001/ws/live?hz=30 → 30 messages/sec
"""
await manager.connect(websocket, rate_hz=hz)
try:
while True:
# Keep connection alive
# Keep connection alive; client can send pings
await websocket.receive_text()
except WebSocketDisconnect:
pass
except Exception:
pass
except Exception as e:
logger.debug("WS connection error: %s", e)
finally:
await manager.disconnect(websocket)