arnavam commited on
Commit
09083dc
·
1 Parent(s): 956dd89

Auto-create audio stream on WebSocket connect

Browse files
Files changed (2) hide show
  1. server.py +5 -0
  2. test/test_audio_api.py +54 -0
server.py CHANGED
@@ -658,6 +658,11 @@ async def websocket_audio_stream(websocket: WebSocket, session_id: str):
658
  await websocket.accept()
659
  logger.info(f"Audio WebSocket connection established for session {session_id}")
660
 
 
 
 
 
 
661
  try:
662
  while True:
663
  message = await websocket.receive()
 
658
  await websocket.accept()
659
  logger.info(f"Audio WebSocket connection established for session {session_id}")
660
 
661
+ # Auto-create stream if not exists
662
+ if session_id not in audio_processor.active_streams:
663
+ audio_processor.create_audio_stream(session_id)
664
+ logger.info(f"Auto-created audio stream for session {session_id}")
665
+
666
  try:
667
  while True:
668
  message = await websocket.receive()
test/test_audio_api.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # test_audio_api.py
2
+ import asyncio
3
+ import websockets
4
+ import json
5
+ import base64
6
+
7
+ # Test locally - change to HF URL when deployed
8
+ BASE_URL = "localhost:8000"
9
+ # BASE_URL = "arnavam-afs-backend.hf.space"
10
+
11
+ async def test_audio():
12
+ session_id = "test-session-123"
13
+
14
+ # WebSocket URI
15
+ ws_protocol = "ws" if "localhost" in BASE_URL else "wss"
16
+ uri = f"{ws_protocol}://{BASE_URL}/ws/audio/{session_id}"
17
+
18
+ print(f"Connecting to {uri}...")
19
+
20
+ try:
21
+ async with websockets.connect(uri, ping_timeout=10) as ws:
22
+ print("Connected!")
23
+
24
+ # Send audio chunk with angle
25
+ audio_bytes = b'\x00\x00' * 1000 # dummy audio data
26
+ payload = {
27
+ "audio_data": base64.b64encode(audio_bytes).decode(),
28
+ "angle": 45.5
29
+ }
30
+ print(f"Sending audio chunk with angle {payload['angle']}...")
31
+ await ws.send(json.dumps(payload))
32
+ response = await ws.recv()
33
+ print(f"Response: {response}")
34
+
35
+ # Send another chunk with different angle
36
+ payload["angle"] = 90.0
37
+ print(f"Sending audio chunk with angle {payload['angle']}...")
38
+ await ws.send(json.dumps(payload))
39
+ response = await ws.recv()
40
+ print(f"Response: {response}")
41
+
42
+ # Stop stream
43
+ print("Stopping stream...")
44
+ await ws.send(json.dumps({"command": "stop"}))
45
+ response = await ws.recv()
46
+ print(f"Response: {response}")
47
+
48
+ except websockets.exceptions.ConnectionClosedError as e:
49
+ print(f"Connection closed: {e}")
50
+ except Exception as e:
51
+ print(f"Error: {e}")
52
+
53
+ if __name__ == "__main__":
54
+ asyncio.run(test_audio())