-
Notifications
You must be signed in to change notification settings - Fork 716
/
live.py
44 lines (38 loc) · 1.28 KB
/
live.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
"""
Recognize live speech from the default audio device.
"""
# MIT license (c) 2022, see LICENSE for more information.
# Author: David Huggins-Daines <[email protected]>
from pocketsphinx import Endpointer, Decoder, set_loglevel
import subprocess
import sys
import os
def main():
set_loglevel("INFO")
ep = Endpointer()
decoder = Decoder(
samprate=ep.sample_rate,
)
soxcmd = f"sox -q -r {ep.sample_rate} -c 1 -b 16 -e signed-integer -d -t raw -"
sox = subprocess.Popen(soxcmd.split(), stdout=subprocess.PIPE)
while True:
frame = sox.stdout.read(ep.frame_bytes)
prev_in_speech = ep.in_speech
speech = ep.process(frame)
if speech is not None:
if not prev_in_speech:
print("Speech start at %.2f" % (ep.speech_start), file=sys.stderr)
decoder.start_utt()
decoder.process_raw(speech)
hyp = decoder.hyp()
if hyp is not None:
print("PARTIAL RESULT:", hyp.hypstr, file=sys.stderr)
if not ep.in_speech:
print("Speech end at %.2f" % (ep.speech_end), file=sys.stderr)
decoder.end_utt()
print(decoder.hyp().hypstr)
try:
main()
except KeyboardInterrupt:
pass