Created
June 7, 2020 11:57
-
-
Save eugeneRover/e0af283ae30789aefe730f87e39bc4bb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# -*- coding: utf-8 -*- | |
import asyncio | |
import websockets | |
import json | |
from pyaudio import PyAudio, paInt16 | |
WS_ROOT = 'ws://localhost:2700' | |
sample_rate = 16000 | |
ftr = int(sample_rate/1) # frames to read at one time | |
async def doit(): | |
p = PyAudio() | |
# open input stream from default input device with given sample rate and 1/2 second buffer | |
stream = p.open(rate=sample_rate, channels=1,format=paInt16,input=True, frames_per_buffer=ftr) | |
resp = None | |
async with websockets.connect(WS_ROOT, compression=None) as websocket: | |
await websocket.send('{"config" : { "sample_rate" : '+str(sample_rate)+'}}') | |
while True: | |
data = stream.read(ftr) | |
await websocket.send(data) | |
resp = await websocket.recv() | |
if '"result"' in resp: # non-partial | |
break | |
await websocket.send('{"eof" : 1}') | |
await websocket.recv() # typically {"text":""} | |
stream.stop_stream() | |
stream.close() | |
p.terminate() | |
return json.loads(resp)['text'] | |
print (asyncio.get_event_loop().run_until_complete(doit())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment