-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvoice_handler.py
More file actions
49 lines (41 loc) · 1.41 KB
/
voice_handler.py
File metadata and controls
49 lines (41 loc) · 1.41 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
import os
import time
import speech_recognition as sr
import pyttsx3
def init_engine():
return pyttsx3.init()
def check_all_voices(engine):
voices = engine.getProperty('voices')
for voice in voices:
print("Voice:")
print(" - ID: %s" % voice.id)
print(" - Name: %s" % voice.name)
print(" - Languages: %s" % voice.languages)
print(" - Gender: %s" % voice.gender)
def speak(text, voice, engine):
engine.setProperty('voice', voice.id)
engine.setProperty('rate', 130) # Speed percent can go over 100
engine.say(text)
engine.runAndWait()
def get_voice(engine):
voices = engine.getProperty('voices')
# Windows voice check (for Mac first call check_all_voices)
# and choose a proper voice that you like
for voice in voices:
if "Zira" in voice.name:
desired_voice = voice
break
return desired_voice
def get_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source) # this gives a little delay but handles ambient noise
audio = r.listen(source)
said = ""
try:
said = r.recognize_google(audio)
except sr.RequestError:
print("API was unreachable or unresponsive")
except sr.UnknownValueError:
print("Unable to recognize speech")
return said