forked from PySimpleGUI/PySimpleGUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemo_Google_TTS.py
More file actions
53 lines (47 loc) · 1.31 KB
/
Demo_Google_TTS.py
File metadata and controls
53 lines (47 loc) · 1.31 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
#!/usr/bin/env python
import sys
if sys.version_info[0] >= 3:
import PySimpleGUI as sg
else:
import PySimpleGUI27 as sg
from gtts import gTTS
from pygame import mixer
import time
import os
'''
Simple demonstration of using Google Text to Speech
Get a multi-line string
Convert to speech
Play back the speech
Note that there are 2 temp files created. The program tries to delete them but will fail on one of them
'''
layout = [[sg.Text('What would you like me to say?')],
[sg.Multiline(size=(60,10), enter_submits=True)],
[sg.Button('Speak', bind_return_key=True), sg.Exit()]]
window = sg.Window('Google Text to Speech').Layout(layout)
i = 0
mixer.init()
while True:
event, values = window.Read()
if event is None or event == 'Exit':
break
# Get the text and convert to mp3 file
tts = gTTS(text=values[0], lang='en',slow=False)
tts.save('speech{}.mp3'.format(i%2))
# playback the speech
mixer.music.load('speech{}.mp3'.format(i%2))
mixer.music.play()
# wait for playback to end
while mixer.music.get_busy():
time.sleep(.1)
mixer.stop()
i += 1
# try to remove the temp files. You'll likely be left with 1 to clean up
try:
os.remove('speech0.mp3')
except:
pass
try:
os.remove('speech1.mp3')
except:
pass