-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
58 lines (48 loc) · 1.97 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from fastapi.middleware.cors import CORSMiddleware
from starlette.websockets import WebSocketState
import asyncio
import uvicorn
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
app = FastAPI()
# Configure CORS - if you need it...
app.add_middleware(
CORSMiddleware,
allow_origins=["http://127.0.0.1:5500"], # Allow your frontend origin
allow_credentials=True,
allow_methods=["*"], # Allow all HTTP methods (GET, POST, etc.)
allow_headers=["*"], # Allow all headers
)
# API to retrieve Simli API key and face ID from .env
@app.get("/api/keys")
async def get_keys():
api_key = os.getenv("SIMLI_API_KEY")
face_id = os.getenv("SIMLI_FACE_ID")
return {"api_key": api_key, "face_id": face_id}
# WebSocket endpoint for handling incoming audio streams
@app.websocket("/")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
print("WebSocket connection established on server")
try:
# FFMPEG command for resampling audio to 16kHz mono
decodeTask = await asyncio.subprocess.create_subprocess_exec(
"ffmpeg", "-i", "pipe:0", "-f", "s16le", "-ar", "16000", "-ac", "1", "-acodec", "pcm_s16le", "-",
stdin=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE
)
while websocket.client_state == WebSocketState.CONNECTED:
data = await websocket.receive_bytes() # Receive audio data from client
print(f"Received {len(data)} bytes of audio data")
# Send data to ffmpeg for resampling
decodeTask.stdin.write(data)
await decodeTask.stdin.drain() # Ensure the data is flushed properly
except WebSocketDisconnect:
print("WebSocket disconnected")
finally:
# Clean up the WebSocket connection
await websocket.close()
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=8081)