azure_tts 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e36642fc5540cf7e824334fdddff7eec1217157cec2deba3ede4e29f6df02fa8
4
+ data.tar.gz: d7288b1332dbd57aaa17fdd70700515e9a2172447b9ed396f204aae980794e0d
5
+ SHA512:
6
+ metadata.gz: 9bc703b411cf7b6f574cc2f707ed0b9d253e3488c1b267b6519e530460266b0f99baee3b45f0a7d13187ecf8a9d57041ac5f01189176c9ae3e2df70b6faffabf
7
+ data.tar.gz: b23867a2d1668c9716658b3d62013284c00f9080687b0f55d88a3914a62271cbb63d2b9dcfa7bc7fe90341b6c803e3939ded1216f0252e8d417fc8345d9adc22
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.2
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in azure_tts.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
8
+ gem "ruby_speech"
data/Gemfile.lock ADDED
@@ -0,0 +1,65 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ azure_tts (0.1.0)
5
+ ruby_speech
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (7.1.4)
11
+ base64
12
+ bigdecimal
13
+ concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ connection_pool (>= 2.2.5)
15
+ drb
16
+ i18n (>= 1.6, < 2)
17
+ minitest (>= 5.1)
18
+ mutex_m
19
+ tzinfo (~> 2.0)
20
+ base64 (0.2.0)
21
+ bigdecimal (3.1.8)
22
+ concurrent-ruby (1.3.4)
23
+ connection_pool (2.4.1)
24
+ diff-lcs (1.5.1)
25
+ drb (2.2.1)
26
+ i18n (1.14.5)
27
+ concurrent-ruby (~> 1.0)
28
+ mini_portile2 (2.8.7)
29
+ minitest (5.25.1)
30
+ mutex_m (0.2.0)
31
+ nokogiri (1.15.6)
32
+ mini_portile2 (~> 2.8.2)
33
+ racc (~> 1.4)
34
+ racc (1.8.1)
35
+ rake (12.3.3)
36
+ rspec (3.13.0)
37
+ rspec-core (~> 3.13.0)
38
+ rspec-expectations (~> 3.13.0)
39
+ rspec-mocks (~> 3.13.0)
40
+ rspec-core (3.13.1)
41
+ rspec-support (~> 3.13.0)
42
+ rspec-expectations (3.13.3)
43
+ diff-lcs (>= 1.2.0, < 2.0)
44
+ rspec-support (~> 3.13.0)
45
+ rspec-mocks (3.13.1)
46
+ diff-lcs (>= 1.2.0, < 2.0)
47
+ rspec-support (~> 3.13.0)
48
+ rspec-support (3.13.1)
49
+ ruby_speech (3.0.1)
50
+ activesupport (>= 3.0.7)
51
+ nokogiri (~> 1.8, >= 1.8.3)
52
+ tzinfo (2.0.6)
53
+ concurrent-ruby (~> 1.0)
54
+
55
+ PLATFORMS
56
+ ruby
57
+
58
+ DEPENDENCIES
59
+ azure_tts!
60
+ rake (~> 12.0)
61
+ rspec (~> 3.0)
62
+ ruby_speech
63
+
64
+ BUNDLED WITH
65
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Grayson Chen
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,73 @@
1
+ # AzureTTS
2
+
3
+ AzureTTS is a Ruby gem that provides an interface to Microsoft Azure's Text-to-Speech service. It allows you
4
+ to convert text to speech and save it as an audio file or integrate it with Rails' Active Storage.
5
+
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'azure_tts'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install azure_tts
22
+
23
+ ## Usage
24
+
25
+
26
+ Use the AzureTTS::Client to generate a speech file:
27
+ ```
28
+ # config/initializers/azure_tts.rb
29
+ AzureTTS.configure do |config|
30
+ config.region = "your_azure_region"
31
+ config.api_key = "your_azure_api_key"
32
+ end
33
+ ```
34
+
35
+ ```
36
+ client = AzureTTS::Client.new
37
+ client.speak_file("Hello, world!", "output.wav", {gender: :female, name: 'en-US-JennyNeural', language: 'en-US'})
38
+ ```
39
+
40
+
41
+ Setup Active Storage in the Model
42
+
43
+ ```
44
+ class MyModel < ApplicationRecord
45
+ has_one_attached :audio_file
46
+ end
47
+ ```
48
+
49
+ ```
50
+ my_model = MyModel.new
51
+ client.speak_to_active_storage("Hello, world!", my_model.audio_file, {gender: :female, name: 'en-US-JennyNeural', language: 'en-US'})
52
+ my_model.save
53
+ ```
54
+
55
+ ## Development
56
+
57
+ 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.
58
+
59
+ 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).
60
+
61
+ ## Contributing
62
+
63
+ Bug reports and pull requests are welcome on GitHub at https://github.com/graysonchen/azure_tts.
64
+
65
+ ## Reference
66
+
67
+ - https://github.com/csmpfo/Ruby-Lib-for-Microsoft-Azure-Text-2-Speech/tree/master
68
+ - https://github.com/adhearsion/ruby_speech
69
+ - https://github.com/cristianbica/azure-tts
70
+
71
+ ## License
72
+
73
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
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/azure_tts.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ require_relative 'lib/azure_tts/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "azure_tts"
5
+ spec.version = AzureTts::VERSION
6
+ spec.authors = ["Grayson Chen"]
7
+ spec.email = ["cgg5207@sina.com"]
8
+
9
+ spec.summary = %q{Microsoft-Azure-Text-2-Speech}
10
+ spec.description = %q{Microsoft-Azure-Text-2-Speech}
11
+ spec.homepage = "https://github.com/graysonchen/azure_tts"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/graysonchen/azure_tts"
19
+ spec.metadata["changelog_uri"] = "https://github.com/graysonchen/azure_tts"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+
27
+ spec.add_dependency "ruby_speech"
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "azure_tts"
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(__FILE__)
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
@@ -0,0 +1,3 @@
1
+ module AzureTts
2
+ VERSION = "0.1.0"
3
+ end
data/lib/azure_tts.rb ADDED
@@ -0,0 +1,104 @@
1
+ require "azure_tts/version"
2
+ require 'net/http'
3
+ require 'net/https'
4
+ require 'uri'
5
+ require 'json'
6
+ require 'ruby_speech'
7
+
8
+ module AzureTTS
9
+ class Error < StandardError; end
10
+
11
+ class << self
12
+ attr_accessor :configuration
13
+ end
14
+
15
+ def self.configure
16
+ self.configuration ||= Configuration.new
17
+ yield(configuration)
18
+ end
19
+
20
+ class Configuration
21
+ attr_accessor :api_key, :region
22
+
23
+ def initialize
24
+ @api_key = nil
25
+ @region = nil
26
+ end
27
+ end
28
+
29
+ class Client
30
+ def initialize(api_key = nil, region = nil)
31
+ @api_key = api_key || AzureTTS.configuration.api_key
32
+ @region = region || AzureTTS.configuration.region
33
+ raise Error, "API key and region must be set" if @api_key.nil? || @region.nil?
34
+ @access_token = fetch_access_token
35
+ end
36
+
37
+ def speak(text, voice_options = {})
38
+ tts_request(text, voice_options)
39
+ end
40
+
41
+ def speak_file(text, output_file = "output.wav", voice_options = {})
42
+ audio_data = tts_request(text, voice_options)
43
+ File.open(output_file, 'wb') { |file| file.write(audio_data) }
44
+ output_file
45
+ end
46
+
47
+ def speak_to_active_storage(text, attachment, voice_options = {})
48
+ audio_data = speak(text, voice_options)
49
+ temp_file = Tempfile.new(['azure_tts', '.wav'])
50
+ temp_file.binmode
51
+ temp_file.write(audio_data)
52
+ temp_file.rewind
53
+
54
+ attachment.attach(
55
+ io: temp_file,
56
+ filename: "tts_output_#{Time.now.to_i}.wav",
57
+ content_type: 'audio/wav'
58
+ )
59
+ ensure
60
+ temp_file.close
61
+ temp_file.unlink
62
+ end
63
+
64
+ private
65
+
66
+ def fetch_access_token
67
+ url = URI.parse("https://#{@region}.api.cognitive.microsoft.com/sts/v1.0/issueToken")
68
+ http = Net::HTTP.new(url.host, url.port)
69
+ http.use_ssl = true
70
+ headers = {
71
+ 'Ocp-Apim-Subscription-Key' => @api_key
72
+ }
73
+
74
+ resp = http.post(url.path, "", headers)
75
+ raise Error, "Failed to fetch access token: #{resp.body}" unless resp.is_a?(Net::HTTPSuccess)
76
+
77
+ resp.body
78
+ end
79
+
80
+ def tts_request(text, voice_options)
81
+ url = URI.parse("https://#{@region}.tts.speech.microsoft.com/cognitiveservices/v1")
82
+ http = Net::HTTP.new(url.host, url.port)
83
+ http.use_ssl = true
84
+
85
+ headers = {
86
+ 'content-type' => 'application/ssml+xml',
87
+ 'X-Microsoft-OutputFormat' => 'audio-16khz-128kbitrate-mono-mp3',
88
+ 'Authorization' => "Bearer #{@access_token}",
89
+ 'User-Agent' => 'AzureTTSRubyGem'
90
+ }
91
+
92
+ data = RubySpeech::SSML.draw do
93
+ voice voice_options do
94
+ string text
95
+ end
96
+ end
97
+
98
+ resp = http.post(url.path, data.to_s, headers)
99
+ raise Error, "TTS request failed: #{resp.body}" unless resp.is_a?(Net::HTTPSuccess)
100
+
101
+ resp.body
102
+ end
103
+ end
104
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: azure_tts
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Grayson Chen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-09-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruby_speech
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Microsoft-Azure-Text-2-Speech
28
+ email:
29
+ - cgg5207@sina.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".rspec"
36
+ - ".travis.yml"
37
+ - Gemfile
38
+ - Gemfile.lock
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - azure_tts.gemspec
43
+ - bin/console
44
+ - bin/setup
45
+ - lib/azure_tts.rb
46
+ - lib/azure_tts/version.rb
47
+ homepage: https://github.com/graysonchen/azure_tts
48
+ licenses:
49
+ - MIT
50
+ metadata:
51
+ allowed_push_host: https://rubygems.org
52
+ homepage_uri: https://github.com/graysonchen/azure_tts
53
+ source_code_uri: https://github.com/graysonchen/azure_tts
54
+ changelog_uri: https://github.com/graysonchen/azure_tts
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 2.3.0
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.5.9
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Microsoft-Azure-Text-2-Speech
74
+ test_files: []