2018-10-24 23:45:18 +00:00
|
|
|
#!/usr/bin/env python
|
2019-10-23 20:10:03 +00:00
|
|
|
import PySimpleGUI as sg
|
2018-10-24 20:54:05 +00:00
|
|
|
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?')],
|
2019-10-23 20:10:03 +00:00
|
|
|
[sg.MLine(size=(60,10), enter_submits=True)],
|
2018-10-29 00:01:03 +00:00
|
|
|
[sg.Button('Speak', bind_return_key=True), sg.Exit()]]
|
2018-10-24 20:54:05 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
window = sg.Window('Google Text to Speech', layout)
|
2018-10-24 20:54:05 +00:00
|
|
|
|
|
|
|
i = 0
|
|
|
|
mixer.init()
|
|
|
|
while True:
|
2019-10-23 20:10:03 +00:00
|
|
|
event, values = window.read()
|
|
|
|
if event in (None, 'Exit'):
|
2018-10-24 20:54:05 +00:00
|
|
|
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
|
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
window.close()
|
|
|
|
|
2018-10-24 20:54:05 +00:00
|
|
|
# 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
|