-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvoice.py
41 lines (29 loc) · 1021 Bytes
/
voice.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
import pyttsx3
import speech_recognition as sr
class DoVoice:
"""This DoVoice class is for voice related function."""
def take_voice(self) -> str:
"""
This function returns a string taking input from the user's microphone.
"""
r = sr.Recognizer()
with sr.Microphone() as source:
r.pause_threshold = 1
self.speak("Listening")
audio = r.listen(source)
try:
query = r.recognize_google(audio, language="en-in")
return query #Returning string said by user.
except Exception as e:
return e
@staticmethod
def speak(string:str) -> None:
"""
This function speaks the give string.
"""
# Code for tuning voice of assistant into a famale voice...
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)
engine.say(string)
engine.runAndWait()