-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrecord1.py
50 lines (36 loc) · 1.13 KB
/
record1.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
#!/usr/bin/env python3
import asyncio
import websockets
import numpy as np
import scipy.io.wavfile
sr = 8000
encode_float = True
async def audio_stream(websocket, path):
sound = np.array([], dtype=np.int16)
if encode_float == True:
sound = np.array([], dtype=np.float32)
print(f"starting recording")
while True:
try:
message = await websocket.recv()
if isinstance(message, str):
continue
audio = np.frombuffer(message, dtype=np.int16)
if encode_float == True:
audio = audio.astype(np.float32)/32767.0
sound = np.append(sound, audio)
except websockets.ConnectionClosed:
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()