Skip to content

Instantly share code, notes, and snippets.

@juliensimon
Created December 1, 2016 01:32
Show Gist options
  • Save juliensimon/94192584e6ba8a755b82f580d67af739 to your computer and use it in GitHub Desktop.
Save juliensimon/94192584e6ba8a755b82f580d67af739 to your computer and use it in GitHub Desktop.
Polly
import os, boto3
defaultRegion = 'us-east-1'
defaultUrl = 'https://polly.us-east-1.amazonaws.com'
def connectToPolly(regionName=defaultRegion, endpointUrl=defaultUrl):
return boto3.client('polly', region_name=regionName, endpoint_url=endpointUrl)
def speak(polly, text, format='mp3', voice='Brian'):
resp = polly.synthesize_speech(OutputFormat=format, Text=text, VoiceId=voice)
soundfile = open('/tmp/sound.mp3', 'w')
soundBytes = resp['AudioStream'].read()
soundfile.write(soundBytes)
soundfile.close()
os.system('afplay /tmp/sound.mp3') # Works only on Mac OS, sorry
os.remove('/tmp/sound.mp3')
polly = connectToPolly()
speak(polly, "Hello world, I'm Polly. Or Brian. Or anything you want, really.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment