Spaces:
Build error

File size: 552 Bytes
23546a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from fastapi import FastAPI, Response, WebSocket
import sounddevice as sd
import numpy as np

app = FastAPI()

@app.websocket("/audio-stream")
async def audio_stream(websocket: WebSocket):
    try:
        with sd.InputStream(callback=websocket.send) as stream:
            while True:
                data, _ = stream.read(1024)
                if not websocket.application_state:
                    break
    except Exception as e:
        print(e)

@app.get("/audio-stream")
async def audio_stream():
    return Response(content_type="audio/x-wav")