Skip to content

Commit

Permalink
start audio reading + stream
Browse files Browse the repository at this point in the history
  • Loading branch information
MrPicklePinosaur committed Jun 8, 2022
1 parent c7ac4e4 commit 824eaeb
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ Next, download required nltk data
$ python bin/download.py
```

Start the `vosk` server for speech recognition
```
$ docker run -d -p 2700:2700 alphacep/kaldi-en:latest-en:latest
```

## TODO

- [x] properly load intents data from json
Expand Down
3 changes: 3 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@

# api
API_URL = "http://localhost:8000"

# voice stream
VOSK_URL = "ws://localhost:2700"
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
asyncio==3.4.3
certifi==2022.5.18.1
charset-normalizer==2.0.12
click==8.1.3
Expand All @@ -13,4 +14,5 @@ torch==1.11.0
tqdm==4.64.0
typing_extensions==4.2.0
urllib3==1.26.9
websockets==10.3
zipp==3.8.0
44 changes: 44 additions & 0 deletions stream.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# stream audio to vosk server
import asyncio
import websockets
import sounddevice as sd

import config


def callback():
# TODO push audio block to queue
pass


def open_device(device_num):
print(sd.query_devices())
samplerate = 80
blocksize = 8000
return sd.RawInputStream(
samplerate=samplerate,
blocksize=blocksize,
device=device_num,
dtype='int16',
channels=1,
callback=callback
)


async def run(uri):
audio_queue = asyncio.Queue()
with open_device(1) as device:
async with websockets.connect(uri) as sock:
await sock.connect('{ "config": {"sample_rate": %d }' % (device.samplerate))

while True:
data = await audio_queue.get()
await sock.send(data)
await sock.recv()

await sock.send('{"eof": 1}')
await sock.recv()

if __name__ == "__main__":
asyncio.run(run(config.VOSK_URL))

0 comments on commit 824eaeb

Please sign in to comment.