-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWS_Test.py
More file actions
48 lines (39 loc) · 1.42 KB
/
Copy pathWS_Test.py
File metadata and controls
48 lines (39 loc) · 1.42 KB
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
#!/usr/bin/env python3
import asyncio
from asyncio import timeout
import websockets
from websockets.exceptions import ConnectionClosedOK
import sys
import wave
import ujson
async def run_test(uri):
async with websockets.connect(uri) as websocket:
wait_null_answers = False
# wf = wave.open("trash//2724.1726990043.1324706.wav", "rb")
wf = wave.open("trash//q.wav", "rb")
config = {
"sample_rate": wf.getframerate(),
"wait_null_answers": wait_null_answers
}
await websocket.send(ujson.dumps({'config':config}, ensure_ascii=True))
buffer_size = 12800 # 0.8 seconds of audio, don't make it too small otherwise compute will be slow
while True:
data = wf.readframes(buffer_size)
if len(data) == 0:
break
else:
await websocket.send(data)
try:
print(await asyncio.wait_for(websocket.recv(), 0.01))
# logger.info(f"Послушали")
except TimeoutError:
pass
await websocket.send('{"eof" : 1}')
while True:
try:
print(await websocket.recv())
except ConnectionClosedOK:
print("Connection closed from outer client")
break
asyncio.run(run_test('ws://192.168.100.29:49152/ws'))
# asyncio.run(run_test('ws://127.0.0.1:49152/ws'))