-
Notifications
You must be signed in to change notification settings - Fork 0
/
speech.py
48 lines (35 loc) · 1.67 KB
/
speech.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
import subprocess
import speech_recognition as sr
def get(phrase_time=5, timeout=30, debug=False):
r = sr.Recognizer()
value = ''
with sr.Microphone() as source:
if debug: print "[DEBUG] adjusting for ambient noise..."
r.adjust_for_ambient_noise(source, duration=0.7)
try: audio = r.listen(source, timeout=timeout, phrase_time_limit=phrase_time) # if a lot of sound is around, this will keep it from waiting forever.
except sr.WaitTimeoutError: return False
try:
value = r.recognize_google(audio).lower()
if debug: print "\n[DEBUG] Command: " + value
return value
except sr.UnknownValueError: return False
except sr.RequestError as e:
if debug: print "[DEBUG] google error: {0}".format(e)
return False
except KeyError as e:
# only error so far is `missing key: confidence`, meaning it didn't know what was said
if debug: print "[DEBUG] key error: {0}".format(e)
return False
def say(value, debug=False):
if str(type(value)) not in ["<type 'str'>", "<type 'unicode' >"]:
raise TypeError("stt.say argument 'value' only accepts string/unicode types")
if debug is True:
print "[DEBUG] stt.say received speak value '{0}'".format(value)
proc = subprocess.Popen(["tts.exe", value],
shell=False,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, error = proc.communicate()
if len(error) > 0 and debug is True:
print "[DEBUG] stt.say error: " + error