-
Notifications
You must be signed in to change notification settings - Fork 2
/
record3.py
43 lines (30 loc) · 896 Bytes
/
record3.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 as np
import wave
async def audio_stream(websocket, path):
wavefile = wave.open("mysound.wav", 'wb')
wavefile.setnchannels(1)
wavefile.setsampwidth(2)
wavefile.setframerate(8000)
while True:
try:
message = await websocket.recv()
if isinstance(message, str):
continue
audio = np.frombuffer(message, dtype=np.uint8)
wavefile.writeframes(audio)
except websockets.ConnectionClosed:
wavefile.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()