1
- require 'open3.rb'
2
-
3
1
module ESpeak
4
2
class Speech
5
3
attr_reader :options , :text
@@ -21,37 +19,40 @@ def initialize(text, options={})
21
19
# Speaks text
22
20
#
23
21
def speak
24
- system ( espeak_command ( command_options ) )
22
+ IO . popen ( espeak_command ( command_options ) , 'r' ) . read
25
23
end
26
24
27
25
# Generates mp3 file as a result of
28
26
# Text-To-Speech conversion.
29
27
#
30
28
def save ( filename )
31
- system ( espeak_command ( command_options , "--stdout" ) + " | " + lame_command ( filename , command_options ) )
29
+ speech = bytes_wav
30
+ res = IO . popen ( lame_command ( filename , command_options ) , 'r+' ) do |process |
31
+ process . write ( speech )
32
+ process . close_write
33
+ process . read
34
+ end
35
+ res . to_s
32
36
end
33
37
34
38
# Returns mp3 file bytes as a result of
35
39
# Text-To-Speech conversion.
36
40
#
37
41
def bytes ( )
38
- stdout_str , stderr_str , process = Open3 . capture3 ( espeak_command ( command_options , "--stdout" ) + " | " + std_lame_command ( command_options ) )
39
- stdout_str
42
+ speech = bytes_wav
43
+ res = IO . popen ( std_lame_command ( command_options ) , 'r+' ) do |process |
44
+ process . write ( speech )
45
+ process . close_write
46
+ process . read
47
+ end
48
+ res . to_s
40
49
end
41
50
42
51
# Returns wav file bytes as a result of
43
52
# Text-To-Speech conversion.
44
53
#
45
54
def bytes_wav ( )
46
- stdout_str , stderr_str , process = Open3 . capture3 ( espeak_command ( command_options , "--stdout" ) )
47
- stdout_str
48
- end
49
-
50
- # espeak dies handling some chars
51
- # this function sanitizes text
52
- #
53
- def sanitized_text
54
- @text . gsub ( /(!|\? |"|`|\\ )/ , ' ' ) . strip
55
+ IO . popen ( espeak_command ( command_options , "--stdout" ) , 'r' ) . read
55
56
end
56
57
57
58
private
@@ -73,15 +74,15 @@ def default_options
73
74
end
74
75
75
76
def espeak_command ( options , flags = "" )
76
- %| espeak "#{ sanitized_text } " #{ flags } -v#{ options [ :voice ] } -p#{ options [ :pitch ] } -k#{ options [ :capital ] } -s#{ options [ :speed ] } |
77
+ [ ' espeak' , "#{ @text } " , " #{ flags } " , " -v#{ options [ :voice ] } " , " -p#{ options [ :pitch ] } " , " -k#{ options [ :capital ] } " , " -s#{ options [ :speed ] } " ]
77
78
end
78
79
79
80
def std_lame_command ( options )
80
81
lame_command ( "-" , options )
81
82
end
82
83
83
84
def lame_command ( filename , options )
84
- " lame -V2 - #{ filename } #{ '--quiet' if options [ :quiet ] == true } "
85
+ [ ' lame' , ' -V2' , '-' , " #{ filename } " , " #{ '--quiet' if options [ :quiet ] == true } "]
85
86
end
86
87
87
88
def symbolize_keys ( hash )
0 commit comments