-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtexttospeech.py
More file actions
49 lines (40 loc) · 1.43 KB
/
texttospeech.py
File metadata and controls
49 lines (40 loc) · 1.43 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 pyttsx3
import speech_recognition as sr
def texttospeech(content):
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice',voices[0].id)
engine.setProperty('rate',150)
engine.say(content)
engine.runAndWait()
def speechtotext():
r = sr.Recognizer()
def SpeakText(command):
engine = pyttsx3.init()
engine.say(command)
engine.runAndWait()
while(1):
try:
with sr.Microphone() as source2:
r.adjust_for_ambient_noise(source2, duration=0.2)
audio2 = r.listen(source2)
MyText = r.recognize_google(audio2)
MyText = MyText.lower()
print("Did you say '"+MyText+"'")
SpeakText(MyText)
if MyText == "stop":
return None
elif MyText == "speech to text" or MyText == "text to speech":
return MyText
except sr.RequestError as e:
print("Could not request results; {0}".format(e))
except sr.UnknownValueError:
print("unknown error occured")
if __name__ == "__main__":
print("What do you want to do : 'Speech to Text' or 'Text to Speech'")
choice = speechtotext()
if choice == 'speech to text':
speechtotext()
else:
text = str(input("Please Enter text to speak : "))
texttospeech(text)