Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow requests for voices in mp3 format #6

Merged
merged 1 commit into from
May 20, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions pyvona.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Voice(object):
speech_rate = None
sentence_break = None
paragraph_break = None
_codec = "ogg"
region_options = {
'us-east': 'us-east-1',
'us-west': 'us-west-2',
Expand All @@ -73,10 +74,29 @@ def region(self, region_name):
self._region = self.region_options.get(region_name, 'us-east-1')
self._host = 'tts.{}.ivonacloud.com'.format(self._region)

@property
def codec(self):
return self._codec

@codec.setter
def codec(self, codec):
if codec not in ["mp3", "ogg"]:
raise PyvonaException("Invalid codec specified. Please choose 'mp3' or 'ogg'")
self._codec = codec

def fetch_voice_ogg(self, text_to_speak, filename):
"""Fetch an ogg file for given text and save it to the given file name
"""
filename += ".ogg" if not filename.endswith(".ogg") else ""
current_codec = self.codec
self.codec = "ogg"
self.fetch_voice(text_to_speak, filename)
self.codec = current_codec

def fetch_voice(self, text_to_speak, filename):
"""Fetch a voice file for given text and save it to the given file name
"""
file_extension = ".{codec}".format(codec=self.codec)
filename += file_extension if not filename.endswith(file_extension) else ""
r = self._send_amazon_auth_packet_v4(
'POST', 'tts', 'application/json', '/CreateSpeech', '',
self._generate_payload(text_to_speak), self._region, self._host)
Expand Down Expand Up @@ -115,7 +135,7 @@ def _generate_payload(self, text_to_speak):
'Data': text_to_speak
},
'OutputFormat': {
'Codec': 'OGG'
'Codec': self.codec.upper()
},
'Parameters': {
'Rate': self.speech_rate,
Expand Down