-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeech_util.py
29 lines (24 loc) · 875 Bytes
/
speech_util.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
import pyglet, os
from gtts import gTTS
import time
import pyttsx
class SpeechUtil(object):
def __init__(self):
self.player = pyglet.app
'''
using the Google Text-to-Speech API the string passed_text is processed by the same software
used in Google Translate, and is then saved into an mp3 file for later playback.
'''
def read_the_given_text(self, passed_text):
text = gTTS(text=passed_text, lang='en')
text.save(os.getcwd() + os.sep + 'text.mp3')
'''
the text is firstly processed and saved as an mp3 and then played using the pyglet player
'''
def say(self, passed_text):
self.read_the_given_text(passed_text)
f = pyglet.media.load(os.getcwd() + os.sep + 'text.mp3', streaming=False)
f.play()
return f.duration
def stop(self):
self.player.exit()