-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVoiceAssistant.py
More file actions
105 lines (86 loc) · 3.28 KB
/
VoiceAssistant.py
File metadata and controls
105 lines (86 loc) · 3.28 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import speech_recognition as sr
from multiprocessing import Process
from run_commands import exec_commands, contains_key
from libs.helper import print_listening, create_obj_manager, thread_print, pycharm_hosted
r = sr.Recognizer()
sample_rate = 8000
chunk_size = 128
def listen(obj_manager):
"""
Listens to the microphone and appending the audio clip to the audio manager.
:param audio: an multiprocessing manager list
:return: Null
"""
audio = obj_manager["audio"]
# continue forever to listen to the microphone
while True:
try:
# its slightly more efficient to have a nested while true within the try.
while True:
with sr.Microphone(sample_rate=sample_rate, chunk_size=chunk_size) as source:
if not pycharm_hosted():
print_listening(obj_manager)
sound_file = r.listen(source, timeout=0.1, phrase_time_limit=20)
audio.append(sound_file)
# if an error occurs ignore it and continue
except sr.WaitTimeoutError:
None
def translate(obj_manager):
"""
Translates an audio file stored in a manager list to text using Google API
:param audio: the audio manager list.
:return: Null
"""
audio = obj_manager["audio"]
# continue forever to translate audio
while True:
try:
# extract the most recent audio clip from the list
audio_file = audio.pop()
# convert it to string
output = r.recognize_google(audio_file)
# if something goes wrong set the output to empty string
if output.__class__ != "".__class__:
output = ""
except (IndexError, sr.UnknownValueError) as e:
# if something goes wrong set the output to empty string
output = ""
# if the output is not empty display it to the user
if output != "":
thread_print(": {}".format(output), obj_manager)
if pycharm_hosted():
print("Listening ...")
# add output to obj_manager
obj_manager["output"].append(output.lower())
if __name__ == "__main__":
# Create the object manager which enables multiple threads to have access to the same variables
obj_manager = create_obj_manager()
thread_print("Calibrating microphone, please do not talk for 5 seconds...", obj_manager)
# calibrate the mic
with sr.Microphone(sample_rate=sample_rate, chunk_size=chunk_size) as source:
r.adjust_for_ambient_noise(source, duration=5)
# enable constant update for microphone threshold
r.dynamic_energy_threshold = True
thread_print("Calibrated", obj_manager)
# start listening for audio on separate thread
listener = Process(target=listen, args=(obj_manager, ))
listener.daemon = True
listener.start()
# start translating audio to text if audio exists
translator = Process(target=translate, args=(obj_manager, ))
translator.daemon = True
translator.start()
# print listening if ran from pycharm but not if ran from command line
if pycharm_hosted():
print("Listening ...")
# look for the key word in the latest text and run libs within that text if found
while True:
try:
output = obj_manager["output"].pop()
# check if the key or something close to the key was spoken in the string
if contains_key(output):
# if key is present, convert key into command
exec_commands(output, obj_manager)
except IndexError:
# if obj_manager["output"] cannot pop then do nothing
None