Tuesday, February 07, 2017

Amazon Polly Just For Laughs

If you've ever had a burning desire to create custom dialog for Consuela the housekeeper in Family Guy..

 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
#! /usr/bin/python
import boto3
import io
from subprocess import call

client = boto3.client('polly', region_name='us-east-1')

# aws polly describe-voices --region us-east-1|grep Name
response = client.synthesize_speech(
    OutputFormat='mp3',
    SampleRate='16000',
    Text='<speak><p>Hola. Doggie a puera!</p></speak>',
    TextType='ssml',
    VoiceId='Conchita'
)

# Access the audio stream from the response
if 'AudioStream' in response:
    sb = response.get('AudioStream')
    data = sb.read()
    fd = io.open('hello.mp3', mode='wb', closefd=True)
    fd.write(data)
    fd.close()

    call(["afplay", "hello.mp3"])