NEW DEMO - Google Text To Speech

This commit is contained in:
MikeTheWatchGuy 2018-10-24 16:54:05 -04:00
parent 202dd550a6
commit ff79a74f82
1 changed files with 48 additions and 0 deletions

48
Demo_Google_TTS.py Normal file
View File

@ -0,0 +1,48 @@
import PySimpleGUI 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=(40,10), enter_submits=True)],
[sg.RButton('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