-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolly.py
77 lines (65 loc) · 2.76 KB
/
polly.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
68
69
70
71
72
73
74
75
76
77
import boto3
import pygame
import os
import time
import io
import hashlib
import sys
import os.path
class Polly():
OUTPUT_FORMAT='mp3'
DIRECTORY='/home/pi/tts/ttscache'
def __init__(self, voiceId):
self.polly = boto3.client('polly') #access amazon web service
self.VOICE_ID = voiceId
def processText(self, text):
ssml = "<speak> "+text+"</speak>"
fname = self.getFileName(ssml)
if not os.path.isfile(fname):
self.saveToFile(ssml,fname)
self.playMp3(fname)
return fname
def getFileName(self,text):
hash = hashlib.md5(text.encode()).hexdigest()
fname = self.DIRECTORY + "/tts_"+hash+".mp3"
return fname
def saveToFile(self, textToSpeech, fileName): #get polly response and save to file
print("Calling polly")
pollyResponse = self.polly.synthesize_speech(Text=textToSpeech, OutputFormat=self.OUTPUT_FORMAT, VoiceId=self.VOICE_ID, TextType="ssml")
print("Polly Response")
with open(fileName, 'wb') as f:
f.write(pollyResponse['AudioStream'].read())
f.close()
def playMp3(self, fileName):
#cmd = 'omxplayer -o local '+fileName;
cmd = 'mpg321 -g 100 '+fileName
print("CMD: "+cmd)
os.system(cmd)
def cleanup(self):
now = time.time()
cutoff = now - (3 * 86400)
cachedir = self.DIRECTORY
files = os.listdir(cachedir)
for xfile in files:
if os.path.isfile(cachedir+"/" + xfile ):
t = os.stat(cachedir+"/" + xfile )
c = t.st_ctime
# delete file if older than a week
if c < cutoff:
os.remove(cachedir+"/" + xfile)
def say(self, textToSpeech): #get polly response and play directly
pollyResponse = self.polly.synthesize_speech(Text=textToSpeech, OutputFormat=self.OUTPUT_FORMAT, VoiceId=self.VOICE_ID)
pygame.mixer.init()
pygame.init() # this is needed for pygame.event.* and needs to be called after mixer.init() otherwise no sound is played
if os.name != 'nt':
pygame.display.set_mode((1, 1)) #doesn't work on windows, required on linux
with io.BytesIO() as f: # use a memory stream
f.write(pollyResponse['AudioStream'].read()) #read audiostream from polly
f.seek(0)
pygame.mixer.music.load(f)
pygame.mixer.music.set_endevent(pygame.USEREVENT)
pygame.event.set_allowed(pygame.USEREVENT)
pygame.mixer.music.play()
pygame.event.wait() # play() is asynchronous. This wait forces the speaking to be finished before closing
while pygame.mixer.music.get_busy() == True:
pass