Created
October 15, 2012 01:44
-
-
Save pricees/3890415 to your computer and use it in GitHub Desktop.
Using Ivona TTS Saas service with Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'digest/md5' | |
require 'savon' | |
require 'open-uri' | |
# Public: Various methods to get information from the Ivona cloud service | |
# | |
# | |
# Examples | |
# | |
# url = IvonaTts.synthesize(user: "[your user name]", | |
# password: "[your user password]", | |
# text: "This is a demo!") | |
# # => "http://www.ivona.com/online/fileSaas.php?fi=7H3aq8JLbf&.." | |
# | |
# IvonaTts.download(url, "/tmp/out.mp3") | |
# # => <File object | |
# | |
# IvonaTts.wsdl = http://example.net/wsdl | |
# # => "http://example.net/wsdl" | |
# | |
# | |
module IvonaTts | |
extend self | |
# Wsdl as of October 1st, 2012 | |
WSDL = "http://www.ivona.com/saasapiwsdl.php" | |
# Public: Sets the wsdl | |
# | |
# Returns a string | |
def wsdl=(val) | |
@wsdl = val | |
end | |
# Public: Returns the wsdl content if no wsdl set | |
# | |
# Returns a string | |
def wsdl | |
@wsdl ||= WSDL | |
end | |
# Public: Mashals SOAP message to get an Ivona authenticity token to get a | |
# next action | |
# | |
# user - User name registed with Ivona SaaS TTS Service | |
# | |
# Examples | |
# IvonaTts.token("john.smith@gmail.com") | |
# # => "f0986ed43d0b920d41c4ea52eb1267c8" | |
# | |
# Returns a string | |
def token(user) | |
@res = client.request(:get_token) do | |
soap.body = { :user => user } | |
end | |
@res.body[:get_token_response][:token] | |
end | |
# Public: Returns last response from soap | |
# | |
# Examples | |
# IvonaTts.last_response | |
# # => <SOAP:Respose> | |
# | |
# Returns object | |
def last_response | |
@res | |
end | |
# Public: Marshalls SOAP message to convert text to sound file | |
# | |
# params - Parameters passed to Ivona cloud | |
# :user - user account with Ivona | |
# :password - password to authenticate user account | |
# :text - Text to convert into file | |
# :text_type - MIME format of text (defaults to "text/plain") | |
# :voice_id - Voice to use for speech (defaults to "us_salli") | |
# :codec_id - Format/bitrate for sound file (defaults to "mp3/22050") | |
# :options - Extra options (defaults to nil) | |
# | |
# Examples | |
# IvonaTts.synthesize(params) | |
# # => "http://www.ivona.com/online/fileSaas.php?fi=7H3aq8JLbf&.." | |
# | |
# Returns a string to the url | |
# | |
# Raises Exception if soap problem | |
def synthesize(params) | |
user = params[:user] | |
password = params[:password] | |
text = params[:text] | |
text_type = params[:text_type] || "text/plain" | |
voice_id = params[:voice_id] || "us_salli" | |
codec_id = params[:codec_id] || "mp3/22050" | |
options = params[:options] | |
_token = token(user) | |
md5 = Digest::MD5.hexdigest(Digest::MD5.hexdigest(password) + _token) | |
res = client.request(:create_speech_file) do | |
soap.body = { | |
token: _token, | |
md5: md5, | |
text: text, | |
contentType: text_type, | |
voice_id: voice_id, | |
codec_id: codec_id, | |
params: options, | |
} | |
end | |
@res = res | |
res[:create_speech_file_response][:sound_url] | |
end | |
# Public: List Avaible SOAP actions | |
# | |
# Returns array | |
def actions | |
client.wsdl.soap_actions | |
end | |
# Public: Download file to disk, from url | |
# | |
# url - Internet url as source of file | |
# fn - filename, with path, to download file to. (Def. to [EPOCH time].mp3) | |
# | |
# Examples | |
# | |
# url = "http://www.ivona.com/online/fileSaas.php?fi=7H3aq8JLbf&..." | |
# fn = "/tmp/temp_file.mp3" | |
# | |
# IvonaTts.download(url, fn) | |
# # => <File...> | |
# | |
# Returns file object | |
def download(url, filename = "#{Time.now.to_i}.mp3") | |
open(filename, 'wb') do |file| | |
file << open(url).read | |
end | |
end | |
private | |
# Private: Get the soap client | |
# | |
# Returns a SOAP client object | |
def client | |
@client ||= Savon.client(wsdl) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment