Skip to content

Instantly share code, notes, and snippets.

Created June 22, 2011 05:15
Show Gist options
  • Save anonymous/1039553 to your computer and use it in GitHub Desktop.
Save anonymous/1039553 to your computer and use it in GitHub Desktop.
twitter_animal_play
#!ruby -Ks
require 'rubygems'
require 'ffi'
require 'Win32API'
module VoiceText
extend FFI::Library
ffi_lib('vt_jpn')
class INT < FFI::Struct
layout :val, :int
end
attach_function(:VT_GetTTSInfo_JPN, [:int, :pointer, :pointer, :int], :int)
attach_function(:VT_LOADTTS_JPN, [:ulong, :int, :pointer, :pointer], :short)
attach_function(:VT_TextToFile_JPN, [:int, :pointer, :pointer, :int, :int, :int, :int, :int, :int, :int], :short)
attach_function(:VT_UNLOADTTS_JPN, [:int], :void)
VT_LOAD_SUCCESS_CODE = 4
VOICES = {
:haruka => [6, 'synthesis/Haruka/'],
:show => [1, 'synthesis/Show/']
}
def self.save(text, file, s=:show)
speaker = VOICES[s]
if speaker
infoval = INT.new
if VT_GetTTSInfo_JPN(VT_LOAD_SUCCESS_CODE, nil, infoval, infoval.size) == 0
if VT_LOADTTS_JPN(0, speaker[0], speaker[1], nil) == infoval[:val]
begin
VT_TextToFile_JPN(VT_FILE_API_FMT_S16PCM_WAVE, text, file, speaker[0], -1, -1, -1, -1, -1, -1)
#VT_PLAYTTS_JPN(nil, 0, text, speaker[0], -1, -1, -1, -1, -1, -1)
ensure
VT_UNLOADTTS_JPN(speaker[0])
end
else
warn "error VT_LOADTTS_JPN"
end
end
else
warn "unknow speaker"
end
end #def
end #module
class Select
def self.one(option_list, message=nil)
visual_list = ''
option_list.each_with_index do |option, i|
visual_list += (i + 1).to_s + '. ' + option.to_s + "\n"
end
if message != nil then puts message end
print "Please select NUMBER from following one.\n" + visual_list + "#=>"
response = gets()
puts '---------------'
if (response.to_i == 0) or response.to_i > option_list.size then
if response.to_s != '' then
puts response.to_s + ' is wrong number.'
end
response = one(option_list)
else
return option_list[response.to_i - 1]
end
end
end
speaker = Select.one(['haruka', 'show'], '次の中からひとつ選んで下さい。').to_s
#WIN32OLEはシングルスレッドでしか動作しない点に注意 Ruby 1.9.2
END_OF_TERM = 'exit'
loop do
print '>'
text = gets
if text.chomp == END_OF_TERM then break end
timestamp = Time.now.strftime("%Y-%m-%d_%H-%M-%S")
file = "output-#{timestamp}.wav"
VoiceText.save(text, file, speaker.intern)
playSoundAPI=Win32API.new('winmm','PlaySound','ppl','i')
playSoundAPI.call(file,nil,0)
File.delete(file)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment