yandex_speech_api 1.1.2 → 1.4.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 +4 -4
- data/Gemfile +5 -0
- data/README.md +42 -88
- data/lib/yandex_speech.rb +61 -146
- data/lib/yandex_speech/connection.rb +21 -39
- data/lib/yandex_speech/project_structure.rb +3 -15
- data/lib/yandex_speech/setters.rb +78 -0
- data/lib/yandex_speech/sounds.rb +38 -0
- data/lib/yandex_speech/speaker.rb +26 -67
- data/spec/yandex_speech/mp3_player/base_spec.rb +1 -2
- data/spec/yandex_speech_spec.rb +21 -38
- metadata +4 -23
- data/lib/yandex_speech/emotion.rb +0 -39
- data/lib/yandex_speech/format.rb +0 -38
- data/lib/yandex_speech/helpers.rb +0 -52
- data/lib/yandex_speech/key.rb +0 -106
- data/lib/yandex_speech/language.rb +0 -59
- data/lib/yandex_speech/mp3_player.rb +0 -33
- data/lib/yandex_speech/speed.rb +0 -60
- data/lib/yandex_speech/voice.rb +0 -45
- data/spec/yandex_speech/emotion_spec.rb +0 -34
- data/spec/yandex_speech/format_spec.rb +0 -30
- data/spec/yandex_speech/key_spec.rb +0 -58
- data/spec/yandex_speech/language_spec.rb +0 -36
- data/spec/yandex_speech/mp3_player_spec.rb +0 -23
- data/spec/yandex_speech/speed_spec.rb +0 -33
- data/spec/yandex_speech/voice_spec.rb +0 -28
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4803f68de7df8f2a7e56b0d066856deef1ee102e
|
|
4
|
+
data.tar.gz: 258e60515cd83dca48d9bc20312c45e43e600d57
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 7225076cb91d407f43ca9e6dd21032c521cedc5524bbace603804343260d6628f6757e58db9e5c8b56942b8c7c8e920ced52442911b0caeac3afe1b2888a53db
|
|
7
|
+
data.tar.gz: 3df6e6b517c3ef5b45226b2ededb5af7dee29d7cf72896adcf9febc14b711a8b1ce23f3ce6324ac1d35ed4568b4cc55b98b287b05e7e294cce9a6ced175eae0a
|
data/Gemfile
CHANGED
data/README.md
CHANGED
|
@@ -1,101 +1,55 @@
|
|
|
1
|
-
# YandexSpeechApi [](https://badge.fury.io/rb/yandex_speech_api) [](https://travis-ci.org/Medvedu/Yandex-Speech-API) [](https://codeclimate.com/github/Medvedu/Yandex-Speech-API) [](http://inch-ci.org/github/Medvedu/Yandex-Speech-API) [](https://coveralls.io/github/Medvedu/Yandex-Speech-API)
|
|
2
|
-
|
|
3
|
-
##
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
4. Добавьте в проект:
|
|
13
|
-
|
|
14
|
-
```ruby
|
|
15
|
-
# ...
|
|
16
|
-
require 'yandex_speech'
|
|
17
|
-
# ...
|
|
18
|
-
````
|
|
19
|
-
|
|
20
|
-
Воспроизведение звука поддерживается для UNIX-подобных операционных систем (Mac + Linux).
|
|
21
|
-
|
|
22
|
-
### Примеры использования
|
|
23
|
-
|
|
24
|
-
### Пример 1
|
|
25
|
-
|
|
26
|
-
_Для начала работы с api достаточно указать ключ:_
|
|
1
|
+
# YandexSpeechApi [](https://badge.fury.io/rb/yandex_speech_api) [](https://travis-ci.org/Medvedu/Yandex-Speech-API) [](https://codeclimate.com/github/Medvedu/Yandex-Speech-API) [](http://inch-ci.org/github/Medvedu/Yandex-Speech-API) [](https://coveralls.io/github/Medvedu/Yandex-Speech-API?branch=master)
|
|
2
|
+
|
|
3
|
+
## Description
|
|
4
|
+
Converts English, Ukrain, Russian or Turkey text to speech. Solution based on Yandex SpeechKit Cloud technology.
|
|
5
|
+
|
|
6
|
+
## About Key
|
|
7
|
+
|
|
8
|
+
Before usage you need to get an api key.
|
|
9
|
+
Official site: https://developer.tech.yandex.ru (look for SpeechKit Cloud key)
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
27
12
|
|
|
28
13
|
```ruby
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
14
|
+
require 'yandex_speech'
|
|
15
|
+
|
|
16
|
+
key = File.open('secret key/key').readline.strip
|
|
17
|
+
|
|
18
|
+
speaker = YandexSpeechApi::Speaker.new key: key, language: 'russian'
|
|
19
|
+
speaker.save_to_file 'Не будите спящего кота.'
|
|
33
20
|
```
|
|
34
21
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
message = "Don't trouble trouble until trouble troubles you"
|
|
43
|
-
speaker = YandexSpeechApi::Speaker.init(voice: :zahar, speed: 0.23)
|
|
44
|
-
speaker.say message
|
|
22
|
+
```ruby
|
|
23
|
+
YandexSpeechApi.key = File.open('secret key/key').readline.strip
|
|
24
|
+
|
|
25
|
+
message = "Don't trouble trouble until trouble troubles you"
|
|
26
|
+
speaker = YandexSpeechApi::Speaker.new voice: :zahar, speed: 1.1
|
|
27
|
+
speaker.say message
|
|
45
28
|
```
|
|
46
|
-
|
|
47
|
-
Обратите внимание, передавать key каждый раз нет никакой необходимости. Достаточно один раз установить YandexSpeechApi::Key.global_key.
|
|
48
|
-
|
|
49
|
-
### Пример 3
|
|
50
|
-
|
|
51
|
-
_Также поддерживаются геттеры и сеттеры:_
|
|
52
|
-
|
|
29
|
+
|
|
53
30
|
```ruby
|
|
54
|
-
key = File.open('secret key/key').readline.strip
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
speaker.
|
|
31
|
+
key = File.open('secret key/key').readline.strip
|
|
32
|
+
message = 'one two three. one two three. one two three four.'
|
|
33
|
+
|
|
34
|
+
speaker = YandexSpeechApi::Speaker.new do |s|
|
|
35
|
+
s.key = key
|
|
36
|
+
s.voice = :jane
|
|
37
|
+
s.language = :english
|
|
38
|
+
s.emotion = :good
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
speaker.say message
|
|
65
42
|
```
|
|
43
|
+
|
|
44
|
+
## Notes
|
|
66
45
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
_И, наконец, установка параметров через блок:_
|
|
70
|
-
|
|
71
|
-
```ruby
|
|
72
|
-
key = File.open('secret key/key').readline.strip
|
|
73
|
-
message = "one two three. one two three. one two three four."
|
|
74
|
-
|
|
75
|
-
speaker = YandexSpeechApi::Speaker.init do |s|
|
|
76
|
-
s.key = key
|
|
77
|
-
s.voice = :jane
|
|
78
|
-
s.language = :english
|
|
79
|
-
s.speed = :slow
|
|
80
|
-
s.emotion = :good
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
speaker.say message
|
|
84
|
-
```
|
|
85
|
-
|
|
86
|
-
## Примечания
|
|
87
|
-
|
|
88
|
-
1. За один запрос озвучивается текст, длинной до 2000 знаков.
|
|
89
|
-
2. Выбирая язык, учтите: вы ограничены его фонетикой, так, например, английский переводчик не умеет озвучивать русские тексты и т.п. (На самом деле это не совсем точно — хотя официальная документация и не рекомендует выставлять некорректные пары язык-текст, в то же время, _русский_ переводчик фактически может воспроизводить английские тексты)
|
|
90
|
-
|
|
91
|
-
## Зависимости
|
|
92
|
-
|
|
93
|
-
* Ruby 2.0.0 или выше
|
|
46
|
+
2000 symbols per request.
|
|
94
47
|
|
|
95
|
-
|
|
48
|
+
## Dependencies
|
|
96
49
|
|
|
50
|
+
* Ruby 2.0.0 or higher.
|
|
97
51
|
* mpg123
|
|
98
52
|
|
|
99
|
-
##
|
|
53
|
+
## License
|
|
100
54
|
|
|
101
|
-
|
|
55
|
+
Released under the MIT License. See the [LICENSE](./LICENSE) file for further details.
|
data/lib/yandex_speech.rb
CHANGED
|
@@ -10,32 +10,31 @@
|
|
|
10
10
|
# @license MIT
|
|
11
11
|
module YandexSpeechApi
|
|
12
12
|
require_relative 'yandex_speech/project_structure'
|
|
13
|
-
|
|
14
|
-
# Speaker a class that convert English, Ukrain, Russian or Turkey text to
|
|
15
|
-
# speech. Solution based on Yandex SpeechKit technology.
|
|
13
|
+
|
|
14
|
+
# Speaker is a class that convert English, Ukrain, Russian or Turkey text to
|
|
15
|
+
# speech. Solution based on Yandex SpeechKit Cloud technology.
|
|
16
16
|
#
|
|
17
17
|
# @example Basic usage
|
|
18
18
|
# key = File.open('secret key/key').readline.strip
|
|
19
19
|
#
|
|
20
|
-
# speaker = YandexSpeechApi::Speaker.
|
|
21
|
-
# speaker.save_to_file
|
|
20
|
+
# speaker = YandexSpeechApi::Speaker.new key: key, language: 'russian'
|
|
21
|
+
# speaker.save_to_file 'Не будите спящего кота.'
|
|
22
22
|
#
|
|
23
23
|
# @example Hash syntax
|
|
24
24
|
# key = File.open('secret key/key').readline.strip
|
|
25
|
-
# message = "Don't trouble trouble until trouble troubles you"
|
|
26
25
|
#
|
|
27
|
-
#
|
|
26
|
+
# message = "Don't trouble trouble until trouble troubles you"
|
|
27
|
+
# speaker = YandexSpeechApi::Speaker.new voice: :zahar, speed: 1.1
|
|
28
28
|
# speaker.say message
|
|
29
29
|
#
|
|
30
30
|
# @example Block syntax
|
|
31
31
|
# key = File.open('secret key/key').readline.strip
|
|
32
32
|
# message = "one two three. one two three. one two three four."
|
|
33
33
|
#
|
|
34
|
-
# speaker = YandexSpeechApi::Speaker.
|
|
34
|
+
# speaker = YandexSpeechApi::Speaker.new do |s|
|
|
35
35
|
# s.key = key
|
|
36
36
|
# s.voice = :jane
|
|
37
37
|
# s.language = :english
|
|
38
|
-
# s.speed = :slow
|
|
39
38
|
# s.emotion = :good
|
|
40
39
|
# end
|
|
41
40
|
# speaker.say message
|
|
@@ -43,63 +42,48 @@ module YandexSpeechApi
|
|
|
43
42
|
# Before usage you need to get an api key. It is free for non-commercial
|
|
44
43
|
# purposes (at least for now).
|
|
45
44
|
#
|
|
46
|
-
# You can get
|
|
47
|
-
#
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
45
|
+
# You can get the key from official site:
|
|
46
|
+
# https://developer.tech.yandex.ru (SpeechKit Cloud key)
|
|
47
|
+
|
|
48
|
+
class << self
|
|
49
|
+
##
|
|
50
|
+
# Authorization key, overrides Speaker.key when used. Optional.
|
|
51
|
+
#
|
|
52
|
+
# @return [String, NilClass]
|
|
53
|
+
|
|
54
|
+
attr_accessor :key
|
|
55
|
+
end # class << self
|
|
56
|
+
|
|
53
57
|
class Speaker
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
# @param [HASH] settings
|
|
62
|
-
# Overrides default settings.
|
|
63
|
-
# @option settings [Symbol] :speed (:standard)
|
|
64
|
-
# @see Speaker#speed for details.
|
|
65
|
-
# @option settings [Symbol] :voice (:alyss)
|
|
66
|
-
# @see Speaker#voice for details.
|
|
67
|
-
# @option settings [Symbol] :emotion (:neutral)
|
|
68
|
-
# @see Speaker#emotion for details.
|
|
69
|
-
# @option settings [Symbol] :language (:english)
|
|
70
|
-
# @see Speaker#language for details.
|
|
71
|
-
# @option settings [Symbol] :format (:mp3)
|
|
72
|
-
# @see Speaker#format for details.
|
|
73
|
-
# @option settings [Symbol] :key (:unknown)
|
|
74
|
-
# @see Speaker#key for details.
|
|
75
|
-
#
|
|
76
|
-
# @return [YandexSpeech]
|
|
77
|
-
|
|
78
|
-
def init(settings = {}, &callback)
|
|
79
|
-
new settings, &callback
|
|
80
|
-
end
|
|
81
|
-
end # class << self
|
|
82
|
-
|
|
83
|
-
private_class_method :new
|
|
58
|
+
include Setters
|
|
59
|
+
|
|
60
|
+
# @return [String, NilClass]
|
|
61
|
+
|
|
62
|
+
def key
|
|
63
|
+
YandexSpeechApi.key || @key
|
|
64
|
+
end
|
|
84
65
|
|
|
85
66
|
##
|
|
86
|
-
#
|
|
67
|
+
# Dictor speech speed.
|
|
87
68
|
#
|
|
88
|
-
# @return [
|
|
69
|
+
# @return [Float]
|
|
70
|
+
# Any from those - (0.1)..3
|
|
89
71
|
|
|
90
72
|
attr_reader :speed
|
|
91
73
|
|
|
92
74
|
##
|
|
93
75
|
# Preferred dictor voice.
|
|
94
76
|
#
|
|
95
|
-
# @return [
|
|
77
|
+
# @return [Symbol]
|
|
78
|
+
# Any from those: :jane, :oksana, :alyss, :omazh, :zahar, :ermil
|
|
96
79
|
|
|
97
80
|
attr_reader :voice
|
|
98
81
|
|
|
99
82
|
##
|
|
100
83
|
# How emotional dictor should speak.
|
|
101
84
|
#
|
|
102
|
-
# @return [
|
|
85
|
+
# @return [String]
|
|
86
|
+
# Any from those: "good", "evil", "neutral"
|
|
103
87
|
|
|
104
88
|
attr_reader :emotion
|
|
105
89
|
|
|
@@ -108,9 +92,10 @@ module YandexSpeechApi
|
|
|
108
92
|
#
|
|
109
93
|
# Speaker with +russian+ language can't translate, or even synthesize
|
|
110
94
|
# +english+ text (actually it can, but official documentation strongly
|
|
111
|
-
# recommend to select
|
|
95
|
+
# recommend to select correct language for text)
|
|
112
96
|
#
|
|
113
|
-
# @return [
|
|
97
|
+
# @return [String]
|
|
98
|
+
# Any from those: "russian", "english", "ukrain", "turkey"
|
|
114
99
|
|
|
115
100
|
attr_reader :language
|
|
116
101
|
|
|
@@ -120,72 +105,30 @@ module YandexSpeechApi
|
|
|
120
105
|
# Do not use +:wav+ format for large texts. Result audio file will be
|
|
121
106
|
# too big, and service truncates resulted file.
|
|
122
107
|
#
|
|
123
|
-
# @return [
|
|
108
|
+
# @return [Symbol]
|
|
109
|
+
# Any from those: "mp3", "wav", "opus"
|
|
124
110
|
|
|
125
111
|
attr_reader :format
|
|
126
112
|
|
|
127
|
-
|
|
128
|
-
#
|
|
129
|
-
# +@speed+, +@key+ attributes.
|
|
130
|
-
#
|
|
131
|
-
# @setter #speed=(other)
|
|
132
|
-
# @param [Symbol, Integer, Float] other
|
|
133
|
-
# Expecting values are
|
|
134
|
-
# :slowest, :slow, :standard, :fast, :fastest
|
|
135
|
-
# or any Integer/Float from (0.1)..3 range.
|
|
136
|
-
#
|
|
137
|
-
# @setter #voice=(other)
|
|
138
|
-
# @param [Symbol] other
|
|
139
|
-
# Expecting values are
|
|
140
|
-
# :jane, :oksana, :alyss, :omazh, :zahar, :ermil
|
|
113
|
+
# @param [Proc] callback
|
|
114
|
+
# Used to set object attributes through {do...end} block.
|
|
141
115
|
#
|
|
142
|
-
# @
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
# Expecting values are
|
|
155
|
-
# :mp3, :wav, :opus
|
|
156
|
-
#
|
|
157
|
-
# @setter #key=(new_key)
|
|
158
|
-
# @param [String] new_key
|
|
159
|
-
|
|
160
|
-
%i(voice language format emotion speed key).each do |symbol|
|
|
161
|
-
define_method "#{symbol}=" do |other|
|
|
162
|
-
method_name = __method__.to_s.chop
|
|
163
|
-
klass = YandexSpeechApi.const_get method_name.capitalize
|
|
164
|
-
variable =
|
|
165
|
-
if other.is_a? klass
|
|
166
|
-
other
|
|
167
|
-
else
|
|
168
|
-
klass.new(other)
|
|
169
|
-
end
|
|
170
|
-
instance_variable_set "@#{method_name}", variable
|
|
171
|
-
end
|
|
116
|
+
# @return [YandexSpeechApi::Speaker]
|
|
117
|
+
|
|
118
|
+
def initialize(settings = {}, &callback)
|
|
119
|
+
yield self if block_given?
|
|
120
|
+
|
|
121
|
+
self.key = settings[:key]
|
|
122
|
+
|
|
123
|
+
self.voice ||= settings[:voice] || "jane"
|
|
124
|
+
self.speed ||= settings[:speed] || 1.0
|
|
125
|
+
self.emotion ||= settings[:emotion] || "good"
|
|
126
|
+
self.language ||= settings[:language] || "english"
|
|
127
|
+
self.format ||= settings[:format] || "mp3"
|
|
172
128
|
end
|
|
173
129
|
|
|
174
130
|
##
|
|
175
|
-
#
|
|
176
|
-
#
|
|
177
|
-
# @example #Say usage
|
|
178
|
-
# key = File.open('secret key/key').readline.strip
|
|
179
|
-
# message = "one two three. one two three. one two three four."
|
|
180
|
-
#
|
|
181
|
-
# speaker = YandexSpeechApi::Speaker.init do |s|
|
|
182
|
-
# s.key = key
|
|
183
|
-
# s.voice = :jane
|
|
184
|
-
# s.language = :english
|
|
185
|
-
# s.speed = :slow
|
|
186
|
-
# s.emotion = :good
|
|
187
|
-
# end
|
|
188
|
-
# speaker.say message
|
|
131
|
+
# Speaks the text
|
|
189
132
|
#
|
|
190
133
|
# @param [String] text
|
|
191
134
|
# Something that should been said.
|
|
@@ -194,16 +137,9 @@ module YandexSpeechApi
|
|
|
194
137
|
# You hear the sound.
|
|
195
138
|
|
|
196
139
|
def say(text)
|
|
197
|
-
format =
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
file = Tempfile.new ['yandex_speech_temp_file', ".#{format.type}"]
|
|
201
|
-
file.write binary_data
|
|
202
|
-
|
|
203
|
-
player = MP3_Player.init
|
|
204
|
-
player.play file.path
|
|
205
|
-
|
|
206
|
-
file.close(true) if file
|
|
140
|
+
self.format = :mp3
|
|
141
|
+
row_data = request text
|
|
142
|
+
Sounds.play row_data
|
|
207
143
|
|
|
208
144
|
return nil
|
|
209
145
|
end
|
|
@@ -211,19 +147,10 @@ module YandexSpeechApi
|
|
|
211
147
|
##
|
|
212
148
|
# Saves synthesized voice to audio-file.
|
|
213
149
|
#
|
|
214
|
-
# If +path_to_file+ is empty it saves audio-file to '~/downloads'
|
|
215
|
-
#
|
|
216
|
-
# @example #save_to_file usage
|
|
217
|
-
# key = File.open('secret key/key').readline.strip
|
|
218
|
-
#
|
|
219
|
-
# speaker = YandexSpeechApi::Speaker.init key: key, language: 'russian'
|
|
220
|
-
# speaker.save_to_file "Не будите спящего кота.", 'let_cat_sleep'
|
|
221
|
-
#
|
|
222
150
|
# @param [String] text
|
|
223
151
|
# Something that should been said.
|
|
224
152
|
#
|
|
225
|
-
# @param [String] path_to_file (
|
|
226
|
-
# Path to new file (without file extension).
|
|
153
|
+
# @param [String] path_to_file (Dir.pwd)
|
|
227
154
|
#
|
|
228
155
|
# @return [String]
|
|
229
156
|
# Absolute path to created file.
|
|
@@ -231,23 +158,11 @@ module YandexSpeechApi
|
|
|
231
158
|
def save_to_file(text, path_to_file = '')
|
|
232
159
|
path_to_file = generate_path if path_to_file.empty?
|
|
233
160
|
|
|
234
|
-
|
|
235
|
-
absolute_path = "#{File.expand_path(path_to_file)}.#{format
|
|
236
|
-
File.open(absolute_path, 'w') { |f| f.write
|
|
161
|
+
row_data = request text
|
|
162
|
+
absolute_path = "#{File.expand_path(path_to_file)}.#{format}"
|
|
163
|
+
File.open(absolute_path, 'w') { |f| f.write row_data }
|
|
237
164
|
|
|
238
165
|
return absolute_path
|
|
239
166
|
end
|
|
240
|
-
|
|
241
|
-
private
|
|
242
|
-
|
|
243
|
-
##
|
|
244
|
-
# Yandex Speech ApiKey.
|
|
245
|
-
#
|
|
246
|
-
# Key is not something that should be shared with any other class. Setter
|
|
247
|
-
# method is public anyway.
|
|
248
|
-
#
|
|
249
|
-
# @return [Key]
|
|
250
|
-
|
|
251
|
-
attr_reader :key
|
|
252
167
|
end # class Speaker
|
|
253
168
|
end # module YandexSpeechApi
|