dejan-espeak-ruby 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -15,23 +15,25 @@ espeak-ruby is small Ruby API for utilizing 'espeak' and 'lame' to create Text-T
15
15
  require 'espeak-ruby'
16
16
 
17
17
  include ESpeak
18
-
19
- espeak("Hello World")
20
- espeak("Guten Tag", :v => "de")
18
+
19
+ # Creates hello.mp3 file
20
+ espeak("hello.mp3", :text => "Hello World")
21
+
22
+ # Creates /home/espeak/hello-de.mp3 file
23
+ espeak("/home/espeak/hello-de.mp3", :text => "Hallo Welt", :voice => "de")
21
24
 
22
25
  == Features
23
26
 
24
27
  Currently only subset of espeak features is supported.
25
28
 
26
- default_options = {
27
- :v => 'en', # use voice file of this name from espeak-data/voices
28
- :p => 50, # pitch adjustment, 0 to 99
29
- :s => 170 # speed in words per minute, 80 to 370
30
- }
29
+ :voice => 'en', # use voice file of this name from espeak-data/voices
30
+ :pitch => 50, # pitch adjustment, 0 to 99
31
+ :speed => 170 # speed in words per minute, 80 to 370
32
+
31
33
 
32
34
  This can be easily overriden:
33
35
 
34
- espeak("Zdravo svete", :v => "sr", :p => 90, :s => 200)
36
+ espeak("zdravo.mp3", :text => "Zdravo svete", :voice => "sr", :pitch => 90, :speed => 200)
35
37
 
36
38
  == Requirements
37
39
 
@@ -1,5 +1,5 @@
1
1
  ---
2
2
  :major: 0
3
- :minor: 2
4
- :patch: 3
3
+ :minor: 3
4
+ :patch: 0
5
5
 
@@ -1,24 +1,60 @@
1
1
  require 'rubygems'
2
- require 'digest/sha1'
3
2
  require File.dirname(__FILE__) + "/hash_ext.rb"
4
3
 
5
4
  module ESpeak
6
-
7
- def espeak(text, opts = {})
8
- opts.symbolize_keys!
9
- options = {
10
- :v => 'en', # use voice file of this name from espeak-data/voices
11
- :p => 50, # pitch adjustment, 0 to 99
12
- :s => 170 # speed in words per minute, 80 to 370
13
- }.merge(opts)
14
-
15
- sanitized_text = text.gsub(/(!|\?|"|`|\\)/, ' ')
16
- filename = Digest::SHA1.hexdigest(text + options.to_s + ".mp3")
17
-
18
- if system(%$espeak "#{sanitized_text}" --stdout -v#{options[:v]} -p#{options[:p]} -s#{options[:s]} | lame -V2 - #{filename}$)
19
- filename
5
+
6
+ #
7
+ # Generates mp3 file as a result of Text-To-Speech conversion.
8
+ #
9
+ # filename - The file that will be generated
10
+ # options - Posible key, values
11
+ # :voice - use voice file of this name from espeak-data/voices. ie 'en', 'de', ...
12
+ # :pitch - pitch adjustment, 0 to 99
13
+ # :speed - speed in words per minute, 80 to 370
14
+ #
15
+ def espeak(filename, options)
16
+ if execute_system_command(filename, prepare_options(options))
17
+ nil
20
18
  else
21
- raise "Error while running espeak. You don't seem to have espeak or lame installed ..."
19
+ raise "Error while running espeak. You don't seem to have espeak or lame installed ..."
22
20
  end
23
21
  end
24
- end
22
+
23
+ private
24
+
25
+ def prepare_options(options)
26
+ options.symbolize_keys!
27
+ raise "You must provide value for :text key in options" unless options[:text]
28
+ sanitize_text!(options[:text])
29
+ default_espeak_options.merge(options)
30
+ end
31
+
32
+ # espeak has problems handling
33
+ # some characters (it dies)
34
+ #
35
+ def sanitize_text!(text)
36
+ text.gsub!(/(!|\?|"|`|\\)/, ' ')
37
+ end
38
+
39
+ # Although espeak itself has default options
40
+ # I'm defining them here for easier generating
41
+ # command (with simple hash.merge)
42
+ #
43
+ def default_espeak_options
44
+ { :voice => 'en',
45
+ :pitch => 50,
46
+ :speed => 170 }
47
+ end
48
+
49
+ def execute_system_command(filename, options)
50
+ system([espeak_command(options), lame_command(filename)] * " | ")
51
+ end
52
+
53
+ def espeak_command(options)
54
+ %|espeak "#{options[:text]}" --stdout -v#{options[:voice]} -p#{options[:pitch]} -s#{options[:speed]}|
55
+ end
56
+
57
+ def lame_command(filename)
58
+ "lame -V2 - #{filename}"
59
+ end
60
+ end
@@ -0,0 +1,61 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe ESpeak do
4
+
5
+ before :each do
6
+ @obj = Object.new
7
+ @obj.class_eval { include ESpeak }
8
+ end
9
+
10
+
11
+ describe "espeak" do
12
+ before :each do
13
+ @obj.stub!(:prepare_options)
14
+ end
15
+
16
+ it "should return nil if successfull" do
17
+ @obj.should_receive(:execute_system_command).and_return true
18
+ @obj.espeak("whatever", {}).should == nil
19
+ end
20
+
21
+ it "should raise if not successfull" do
22
+ @obj.should_receive(:execute_system_command).and_return false
23
+ lambda {
24
+ @obj.espeak("whatever", {})
25
+ }.should raise_error
26
+ end
27
+ end
28
+
29
+
30
+ describe "prepare_options" do
31
+ before :each do
32
+ @options = {:text => "Hello", 'speed' => 100}
33
+ @obj.stub!(:execute_system_command).and_return true
34
+ end
35
+
36
+ it "should symbolize keys" do
37
+ @obj.espeak("whatever", @options)
38
+ @options.should == @options.symbolize_keys
39
+ end
40
+
41
+ it "should raise if no value for :text key is provided" do
42
+ @options.delete(:text)
43
+ lambda {
44
+ @obj.espeak("whatever", @options)
45
+ }.should raise_error
46
+ end
47
+
48
+ it "should sanatize text" do
49
+ @obj.should_receive(:sanitize_text!).with(@options[:text])
50
+ @obj.espeak("whatever", @options)
51
+ end
52
+
53
+ it "should merge default_options with provided options" do
54
+ @default_options = {}
55
+ @obj.should_receive(:default_espeak_options).and_return(@default_options)
56
+ @default_options.should_receive(:merge).with(@options)
57
+ @obj.espeak("whatever", @options)
58
+ end
59
+ end
60
+
61
+ end
@@ -0,0 +1,26 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe Hash do
4
+ before :each do
5
+ @hash = {"a" => 1, "b" => 2, :c => "3"}
6
+ end
7
+
8
+ describe "symbolize_keys" do
9
+ it "should not alter instance" do
10
+ lambda {
11
+ @hash.symbolize_keys
12
+ }.should_not change { @hash.inspect }
13
+ end
14
+
15
+ it "should return hash with same values as itself contains, but with keys as symbols" do
16
+ @hash.symbolize_keys.should == {:a => 1, :b => 2, :c => "3"}
17
+ end
18
+ end
19
+
20
+ describe "symbolize_keys!" do
21
+ it "should alter instance such that all keys are symbols, and" do
22
+ @hash.symbolize_keys!
23
+ @hash.should == {:a => 1, :b => 2, :c => "3"}
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,4 @@
1
+ require File.dirname(__FILE__) + '/../lib/espeak-ruby.rb'
2
+
3
+ require 'rubygems'
4
+ require 'active_support'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dejan-espeak-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dejan Simic
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-02-18 00:00:00 -08:00
12
+ date: 2009-02-21 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -26,6 +26,9 @@ files:
26
26
  - VERSION.yml
27
27
  - lib/espeak-ruby.rb
28
28
  - lib/hash_ext.rb
29
+ - spec/espeak_ruby_spec.rb
30
+ - spec/hash_ext_spec.rb
31
+ - spec/spec_helper.rb
29
32
  has_rdoc: true
30
33
  homepage: http://github.com/dejan/espeak-ruby
31
34
  post_install_message: