ssml2mp3 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 21b132ef0982c2114fd73b8ae7028d87596cc4db
4
+ data.tar.gz: 06c9cdcbacffb669c363c867997ec31359e09038
5
+ SHA512:
6
+ metadata.gz: '09d24955dd856ed0dd0c8f718333bce810c185f901fa2ad11cd75d271104539ec152dc5f61c477d8bd6efe88efcec75b48757b93bc3136530d397158d1870649'
7
+ data.tar.gz: e512530da283d805911df4d955e8cb42a24bbaa196762fc821d1033a50d24ad93a55fd4738708d64a7dc032d256d49b5ddab5eee0fbc5c7496167106fd11da64
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.1.10
5
+ - 2.2.6
6
+ - 2.3.3
7
+ - 2.4.0
8
+ before_install: gem install bundler -v 1.13.7
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ssml2mp3.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 hogelog
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Ssml2mp3
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ssml2mp3`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'ssml2mp3'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install ssml2mp3
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ssml2mp3.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ssml2mp3"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/ssml2mp3 ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
4
+ require 'ssml2mp3'
5
+
6
+ if ARGV.size == 0
7
+ puts "#$0 ssml mp3"
8
+ exit
9
+ end
10
+
11
+ ssml_path, mp3_path = ARGV
12
+
13
+ Ssml2mp3.synthesize_file(ssml_path, mp3_path)
@@ -0,0 +1,136 @@
1
+ require "aws-sdk-polly"
2
+ require "logger"
3
+ require "nokogiri"
4
+ require "htmlentities"
5
+ require "expeditor"
6
+ require "concurrent"
7
+ require "tmpdir"
8
+
9
+ module Ssml2mp3
10
+ class Builder
11
+ attr_reader :options, :sample_rate, :client, :logger, :expeditor_service
12
+
13
+ POLLY_TEXT_LENGTH_LIMIT = 1000
14
+
15
+ def initialize(options={})
16
+ @options = options.dup
17
+ @logger = @options.delete(:logger) || Logger.new(STDOUT)
18
+ @sample_rate = @options.delete(:sample_rate) || "16000"
19
+ @max_threads = @options.delete(:max_threads) || 10
20
+ @options[:region] ||= "us-west-2"
21
+
22
+ @client = Aws::Polly::Client.new(@options)
23
+ @expeditor_service = Expeditor::Service.new(
24
+ executor: Concurrent::ThreadPoolExecutor.new(
25
+ min_threads: 0,
26
+ max_threads: @max_threads,
27
+ )
28
+ )
29
+ @htmlentities = HTMLEntities.new
30
+ end
31
+
32
+ def synthesize_file(ssml_path, mp3_path)
33
+ basename = File.basename(mp3_path, ".mp3")
34
+ ssml = File.read(ssml_path)
35
+ File.open(mp3_path, "wb") do |output|
36
+ synthesize(ssml, basename, output)
37
+ end
38
+ logger.info("Generated: #{mp3_path}") if logger
39
+ end
40
+
41
+ def synthesize(ssml, basename, output)
42
+ ssmls = split_ssml(ssml)
43
+
44
+ tmp_files = []
45
+ commands = []
46
+
47
+ Dir.mktmpdir("foo") do |tmpdir|
48
+ ssmls.each_with_index do |ssml, i|
49
+ tmp_ssml_path = File.join(tmpdir, "#{basename}-#{i}.ssml")
50
+ File.write(tmp_ssml_path, ssml)
51
+ tmp_path = File.join(tmpdir, "#{basename}-#{i}.mp3")
52
+ command = Expeditor::Command.new(service: expeditor_service) do
53
+ logger.info("#{tmp_path}...") if logger
54
+ begin
55
+ client.synthesize_speech(
56
+ response_target: tmp_path,
57
+ output_format: "mp3",
58
+ sample_rate: sample_rate,
59
+ text: ssml,
60
+ text_type: "ssml",
61
+ voice_id: "Mizuki",
62
+ )
63
+ rescue => e
64
+ logger.error("#{e.message}\n#{ssml}")
65
+ logger.error("#{e.message}: #{tmp_ssml_path}\n#{ssml}")
66
+ raise e
67
+ end
68
+ end
69
+ command.start
70
+ commands << command
71
+ tmp_files << tmp_path
72
+ end
73
+ commands.each{|command| command.get }
74
+
75
+ tmp_files.each do |tmp_path|
76
+ File.open(tmp_path, "rb") do |tmp_file|
77
+ IO.copy_stream(tmp_file, output)
78
+ end
79
+ end
80
+ output.flush
81
+ end
82
+ end
83
+
84
+ def split_ssml(ssml)
85
+ doc = Nokogiri::XML.parse(tweak_ssml(ssml))
86
+ elements = doc.root.children
87
+
88
+ header = (%r((.+<speak[^>]+>))m === ssml && $1)
89
+ split_ssml_(elements, "", []).map do |body_ssml|
90
+ header + body_ssml + "</speak>"
91
+ end
92
+ end
93
+
94
+ def split_ssml_(elements, buffer, results)
95
+ if elements.empty?
96
+ return buffer.size > 0 ? results << buffer : results
97
+ end
98
+
99
+ element = elements.shift
100
+
101
+ case element
102
+ when Nokogiri::XML::Text
103
+ text = @htmlentities.encode(element.text)
104
+ when String
105
+ text = @htmlentities.encode(element)
106
+ else
107
+ return split_ssml_(elements, buffer + element.to_s, results)
108
+ end
109
+
110
+ if text_size(text) > POLLY_TEXT_LENGTH_LIMIT
111
+ split_texts = text.chars.each_slice(POLLY_TEXT_LENGTH_LIMIT).map(&:join)
112
+ elements = split_texts + elements
113
+ return split_ssml_(split_texts + elements, buffer, results)
114
+ end
115
+
116
+ if text_size(buffer + text) > POLLY_TEXT_LENGTH_LIMIT
117
+ results << buffer
118
+ buffer = ""
119
+ end
120
+
121
+ split_ssml_(elements, buffer + text, results)
122
+ end
123
+
124
+ def text_size(text)
125
+ text.gsub("</?[^>]+>", '').size
126
+ end
127
+
128
+ def tweak_ssml(ssml)
129
+ ssml.
130
+ gsub("\n", "").
131
+ gsub("<p>", "").
132
+ gsub("</p>", '<break strength="strong"/>').
133
+ gsub(/([」】)』])/, '\1<break strength="strong"/>')
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,3 @@
1
+ module Ssml2mp3
2
+ VERSION = "0.1.0"
3
+ end
data/lib/ssml2mp3.rb ADDED
@@ -0,0 +1,18 @@
1
+ require "ssml2mp3/version"
2
+ require "ssml2mp3/builder"
3
+
4
+ require "logger"
5
+
6
+ module Ssml2mp3
7
+ def self.builder
8
+ @builder ||= ::Ssml2mp3::Builder.new(logger: Logger.new(STDOUT))
9
+ end
10
+
11
+ def self.synthesize(*args)
12
+ builder.synthesize(*args)
13
+ end
14
+
15
+ def self.synthesize_file(*args)
16
+ builder.synthesize_file(*args)
17
+ end
18
+ end
data/ssml2mp3.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ssml2mp3/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ssml2mp3"
8
+ spec.version = Ssml2mp3::VERSION
9
+ spec.authors = ["hogelog"]
10
+ spec.email = ["konbu.komuro@gmail.com"]
11
+
12
+ spec.summary = %q{SSML to mp3 synthesizer powered by Amazon Polly}
13
+ spec.homepage = "https://github.com/hogelog/ssml2mp3"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "aws-sdk-polly", "1.0.0.rc2"
23
+ spec.add_dependency "nokogiri", ">= 1.6"
24
+ spec.add_dependency "htmlentities"
25
+ spec.add_dependency "expeditor", "~> 0.5.0"
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.13"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ spec.add_development_dependency "pry"
31
+ end
metadata ADDED
@@ -0,0 +1,170 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ssml2mp3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - hogelog
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk-polly
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.0.rc2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.0.rc2
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: htmlentities
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: expeditor
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.5.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.5.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.13'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.13'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description:
126
+ email:
127
+ - konbu.komuro@gmail.com
128
+ executables:
129
+ - ssml2mp3
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".gitignore"
134
+ - ".rspec"
135
+ - ".travis.yml"
136
+ - Gemfile
137
+ - LICENSE.txt
138
+ - README.md
139
+ - Rakefile
140
+ - bin/console
141
+ - bin/setup
142
+ - exe/ssml2mp3
143
+ - lib/ssml2mp3.rb
144
+ - lib/ssml2mp3/builder.rb
145
+ - lib/ssml2mp3/version.rb
146
+ - ssml2mp3.gemspec
147
+ homepage: https://github.com/hogelog/ssml2mp3
148
+ licenses: []
149
+ metadata: {}
150
+ post_install_message:
151
+ rdoc_options: []
152
+ require_paths:
153
+ - lib
154
+ required_ruby_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ required_rubygems_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ requirements: []
165
+ rubyforge_project:
166
+ rubygems_version: 2.6.8
167
+ signing_key:
168
+ specification_version: 4
169
+ summary: SSML to mp3 synthesizer powered by Amazon Polly
170
+ test_files: []