-
Notifications
You must be signed in to change notification settings - Fork 2
/
ws.py
43 lines (33 loc) · 956 Bytes
/
ws.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
#!/usr/bin/env python3
import asyncio
import websockets
import numpy
import pyaudio
p = pyaudio.PyAudio()
async def audio_stream(websocket, path):
stream = p.open(format=pyaudio.paInt16,
channels=1,
rate=8000,
output=True)
while True:
try:
message = await websocket.recv()
if isinstance(message, str):
continue
audio = numpy.frombuffer(message, numpy.int8)
stream.write(audio)
except websockets.ConnectionClosed:
stream.stop_stream()
stream.close()
break
def start():
loop = asyncio.get_event_loop()
listen_addr = "0.0.0.0"
listen_port = 2700
start_server = websockets.serve(
audio_stream, listen_addr, listen_port)
loop.run_until_complete(start_server)
loop.run_forever()
if __name__ == '__main__':
start()
p.terminate()