class TextToVoice
Constants
- EMOTION_LEVEL
- ENDPOINT
- VERSION
Public Class Methods
new(api_key)
click to toggle source
# File lib/text2voice.rb, line 12 def initialize(api_key) @api_key = api_key @speaker = "haruka" @pitch = "100" @speed = "100" @volume = "100" end
Public Instance Methods
emotion(emotion:, level: :normal)
click to toggle source
# File lib/text2voice.rb, line 25 def emotion(emotion:, level: :normal) @emotion = emotion.to_s @emotion_level = EMOTION_LEVEL[level] self end
pitch(param)
click to toggle source
# File lib/text2voice.rb, line 31 def pitch(param) @pitch = param.to_s self end
save_as(filename)
click to toggle source
# File lib/text2voice.rb, line 51 def save_as(filename) res = send_request() case res when Net::HTTPOK binary_to_wav(filename, res.body) when Net::HTTPBadRequest raise BadRequest.new(res.body) when Net::HTTPUnauthorized raise Unauthoeized.new(res.body) else raise StandardError.new(res.body) end end
speak(text)
click to toggle source
# File lib/text2voice.rb, line 46 def speak(text) @text = text self end
speaker(speaker_name)
click to toggle source
# File lib/text2voice.rb, line 20 def speaker(speaker_name) @speaker = speaker_name self end
speed(param)
click to toggle source
# File lib/text2voice.rb, line 36 def speed(param) @speed = param.to_s self end
volume(param)
click to toggle source
# File lib/text2voice.rb, line 41 def volume(param) @volume = param.to_s self end
Private Instance Methods
binary_to_wav(filename, binary)
click to toggle source
# File lib/text2voice.rb, line 95 def binary_to_wav(filename, binary) File.open("#{filename}", "w") do |io| io.binmode io.write binary end end
create_request(text, speaker, emotion, emotion_level, pitch, speed, volume)
click to toggle source
# File lib/text2voice.rb, line 68 def create_request(text, speaker, emotion, emotion_level, pitch, speed, volume) req = Net::HTTP::Post.new(ENDPOINT.path) req.basic_auth(@api_key, '') data = "text=#{text}" data << ";speaker=#{speaker}" data << ";emotion=#{emotion}" data << ";emotion_level=#{emotion_level}" data << ";pitch=#{pitch}" data << ";speed=#{speed}" data << ";volume=#{volume}" req.body = data return req end
send_request()
click to toggle source
# File lib/text2voice.rb, line 83 def send_request res = nil https = Net::HTTP.new(ENDPOINT.host, 443) https.use_ssl = true https.start do |https| req = create_request(@text, @speaker, @emotion, @emotion_level, @pitch, @speed, @volume) res = https.request(req) end return res end