Created
December 18, 2015 09:57
-
-
Save ssig33/d3056eac469b974b1148 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
file = open(ARGV[0]) | |
require 'tempfile' | |
require 'fileutils' | |
ary = [] | |
file.each_line do |line| | |
text = '' | |
line.split(' ').each do |word| | |
if word =~ /[\p{Han}\p{Hiragana}\p{Katakana},.、。ー・]+/ | |
if text != '' | |
ary << {lang: 'en', text: text.chop} | |
end | |
text = '' | |
ary << {lang: 'jp', text: word} | |
else | |
text += word + ' ' | |
end | |
end | |
if text != '' | |
ary << {lang: 'en', text: text.chop} | |
end | |
end | |
ary.each_with_index do |hash,i| | |
wav = "#{i}.wav" | |
puts "#{hash[:lang]}: #{hash[:text]} @#{wav} #{i.to_i+1}/#{ary.count}" | |
case hash[:lang] | |
when 'en' | |
file = Tempfile.create 'base' | |
file.puts hash[:text] | |
file.close | |
system 'text2wave', file.path, '-o', wav | |
when 'jp' | |
file = Tempfile.create 'base' | |
file.puts hash[:text].sub(/^ /, '') | |
file.close | |
system 'open_jtalk', '-ow', wav, '-x', '/var/lib/mecab/dic/open-jtalk/naist-jdic', '-m', '/usr/share/hts-voice/nitech-jp-atr503-m001/nitech_jp_atr503_m001.htsvoice', '-s', '48000', '-r', '1.5', file.path | |
end | |
end | |
(0..ary.count-1).each{|i| | |
wav = "#{i}.wav" | |
if File.exists? wav | |
system 'sox', wav, '-r', '48000', 'shhhhhhhhhhhhhhhhit.wav' | |
FileUtils.rm wav | |
FileUtils.mv 'shhhhhhhhhhhhhhhhit.wav', wav rescue nil | |
end | |
puts "#{i.to_i+1}/#{ary.count}" | |
} | |
system "sox #{(0..ary.count-1).map{|i| "#{i}.wav" }.select{|x| File.exists? x}.join(" ")} '#{ARGV[1]}'" | |
(0..ary.count-1).each{|i| FileUtils.rm "#{i}.wav" rescue nil} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment