-
Notifications
You must be signed in to change notification settings - Fork 2
/
record2.py
44 lines (31 loc) · 930 Bytes
/
record2.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
#!/usr/bin/env python3
import asyncio
import websockets
import numpy as np
import scipy.io.wavfile
sr = 8000
async def audio_stream(websocket, path):
frames = []
while True:
try:
message = await websocket.recv()
if isinstance(message, str):
continue
audio = np.frombuffer(message, dtype=np.int16)
frames.append(audio)
except websockets.ConnectionClosed:
sound = np.hstack(frames)
l = sound.shape[0] / sr
print(f"length {l}")
scipy.io.wavfile.write("mysound.wav", sr, sound)
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()