-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathspeak.py
More file actions
27 lines (24 loc) · 736 Bytes
/
speak.py
File metadata and controls
27 lines (24 loc) · 736 Bytes
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
import boto3
import sys
from contextlib import closing
from playsound import playsound
def speak(text):
client = boto3.client('polly', region_name='us-east-1')
response = client.synthesize_speech(
OutputFormat='mp3',
Text=text,
TextType='text',
VoiceId='Brian'
)
if "AudioStream" in response:
with closing(response["AudioStream"]) as stream:
try:
file = open('/tmp/speech.mp3', 'wb')
file.write(stream.read())
file.close()
playAudioFile('/tmp/speech.mp3')
except IOError as error:
print(error)
sys.exit(-1)
def playAudioFile(path):
playsound(path, True)