-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextToSpeech.py
67 lines (54 loc) · 1.52 KB
/
textToSpeech.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import tempfile
from playsound import playsound
# offline text to speech
import pyttsx3
# online text to speech
import gtts
# check internet connection
import urllib3
# check internet connection
def internet_on():
try:
http = urllib3.PoolManager()
http.request('GET', 'http://google.com')
return True
except:
return False
#text to speech
def speak(text):
"""
text to speech
@param text: str
@return: Bool (True/False)
"""
# put all of below code in try block
try:
# if internet on
if internet_on():
# online text to speech
tts = gtts.gTTS(text=text, lang='en', slow=False, lang_check=True)
# save to tmp file
tmp = tempfile.NamedTemporaryFile()
temporaryfilename = tmp.name + '.mp3'
tts.save(temporaryfilename)
# play tmp file
playsound(temporaryfilename)
# remove tmp file
tmp.close()
else:
# offline text to speech
engine = pyttsx3.init()
# set rate and volume
engine.setProperty('rate', 150)
engine.setProperty('volume', 1.0)
engine.say(text)
engine.runAndWait()
return True
except Exception as e:
print(e)
return False
# unit testing
if __name__=="__main__":
speak("आपका नाम क्या है?")
print(".................................")
speak("may I help you")