speaker 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 David William
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Speaker
2
+
3
+ Flexible text to speech using Google Translator
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'speaker'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install speaker
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/lib/speaker.rb ADDED
@@ -0,0 +1,20 @@
1
+ require "speaker/version"
2
+ require "speaker/base"
3
+ require "speaker/configuration"
4
+
5
+ module Speaker
6
+
7
+ class << self
8
+ attr_accessor :configuration
9
+ end
10
+
11
+ def self.configure
12
+ self.configuration ||= Configuration.new
13
+ yield(configuration)
14
+ end
15
+
16
+ def self.new(params)
17
+ Base.new(params)
18
+ end
19
+
20
+ end
@@ -0,0 +1,128 @@
1
+ require 'open-uri'
2
+ require 'cgi'
3
+ require 'net/http'
4
+ require 'mechanize'
5
+
6
+ module Speaker
7
+
8
+ class Base
9
+
10
+ attr_accessor :language, :text, :audio_file_path
11
+
12
+ FILE_NAME = 'audio'
13
+ GOOGLE_TRANSLATOR_PATH = "http://translate.google.com/translate_tts?"
14
+ DEFAULT_LANGUAGE = 'en'
15
+ DEFAULT_TEXT = 'Nothing to say'
16
+
17
+ def initialize(options={})
18
+ @language = options[:language] || DEFAULT_LANGUAGE
19
+ @text = options[:text] || DEFAULT_TEXT
20
+ delete_audio_file
21
+ end
22
+
23
+ def tts
24
+ to_audio
25
+ play
26
+ end
27
+
28
+ def to_audio
29
+ split_sentence
30
+ build_mp3
31
+ join_audio_files
32
+ delete_audio_files
33
+ nil
34
+ end
35
+
36
+ def has_audio?
37
+ File.exist?(audio_file)
38
+ end
39
+
40
+ def play
41
+ if has_audio?
42
+ `afplay #{audio_file}`
43
+ @text
44
+ else
45
+ "There is no audio file yet"
46
+ end
47
+ end
48
+
49
+ def audio_file
50
+ "#{audio_file_path}/#{FILE_NAME}.mp3"
51
+ end
52
+
53
+ private
54
+
55
+ def words
56
+ text.split(" ")
57
+ end
58
+
59
+ def split_sentence
60
+ words = @text.split(" ")
61
+ block = 0
62
+ data = []
63
+ @content = {block => []}
64
+
65
+ words.each_with_index do |word,i|
66
+ if (@content[block] + [word]).join(' ').size >= 100
67
+ block += 1
68
+ @content.merge!({block => [word]})
69
+ else
70
+ @content[block] << word
71
+ end
72
+ end
73
+
74
+ @block_loops = block + 1
75
+ end
76
+
77
+ def build_mp3
78
+ @block_loops.times do |i|
79
+ sentence = @content[i].join(" ")
80
+ audio_file = "#{FILE_NAME}-#{i+1}.mp3"
81
+ url = GOOGLE_TRANSLATOR_PATH + "tl=#{language}&q=#{sentence}"
82
+ type_sentence_and_get_audio(url,audio_file)
83
+ end
84
+ end
85
+
86
+ def type_sentence_and_get_audio(url,audio_file)
87
+ a = Mechanize.new { |agent|
88
+ agent.user_agent_alias = 'Mac Safari'
89
+ }
90
+
91
+ a.get(url) do |page|
92
+ File.open(audio_file, 'wb') do |file|
93
+ file << page.content
94
+ end
95
+ end
96
+
97
+ end
98
+
99
+ def audio_files
100
+ files = []
101
+ @block_loops.times do |i|
102
+ files << "#{audio_file_path}/#{FILE_NAME}-#{i+1}.mp3"
103
+ end
104
+ files
105
+ end
106
+
107
+ def join_audio_files
108
+ inline_files = audio_files.join(" ")
109
+ `ruby -pe '' #{inline_files} > #{audio_file}`
110
+ end
111
+
112
+ def delete_audio_files
113
+ audio_files.each do |file|
114
+ File.delete(file)
115
+ end
116
+ end
117
+
118
+ def audio_file_path
119
+ Speaker.configuration ? Speaker.configuration.audio_file_path : Configuration.new.audio_file_path
120
+ end
121
+
122
+ def delete_audio_file
123
+ File.delete(audio_file) if has_audio?
124
+ end
125
+
126
+ end
127
+
128
+ end
@@ -0,0 +1,11 @@
1
+ module Speaker
2
+
3
+ class Configuration
4
+ attr_accessor :audio_file_path
5
+
6
+ def initialize
7
+ @audio_file_path = Dir.pwd
8
+ end
9
+ end
10
+
11
+ end
@@ -0,0 +1,3 @@
1
+ module Speaker
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: speaker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David William
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mechanize
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Flexible text to speech using Google Translator
31
+ email:
32
+ - david@webhall.com.br
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/speaker/base.rb
38
+ - lib/speaker/configuration.rb
39
+ - lib/speaker/version.rb
40
+ - lib/speaker.rb
41
+ - LICENSE
42
+ - Rakefile
43
+ - README.md
44
+ homepage: ''
45
+ licenses: []
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 1.8.22
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Enjoy the power and flexibility of Google Translator by transforming any
68
+ text in audio.
69
+ test_files: []