yandex_speech_api 1.1.2 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 503bb8b1169230590382c2bed35232e8c8b0ad8c
4
- data.tar.gz: b4e5a11c0494ff5b43a3831a44427d7a0b75d26a
3
+ metadata.gz: 4803f68de7df8f2a7e56b0d066856deef1ee102e
4
+ data.tar.gz: 258e60515cd83dca48d9bc20312c45e43e600d57
5
5
  SHA512:
6
- metadata.gz: 08aedbd6436613262ce70410b9c5b9f0b476dd6a6dd1758f9531a7064518d5827854832c7e9afe8a2cbd1a82f66c5bd436a888377c03ad7ce3a574f2179e5c4a
7
- data.tar.gz: caecae0b89060595daf24261389e533f67712b48a976da40067d58e11688212ce70a95d42f6208eb3738757e6fd2bfb74d342352a416e0a017fe25d783c54b73
6
+ metadata.gz: 7225076cb91d407f43ca9e6dd21032c521cedc5524bbace603804343260d6628f6757e58db9e5c8b56942b8c7c8e920ced52442911b0caeac3afe1b2888a53db
7
+ data.tar.gz: 3df6e6b517c3ef5b45226b2ededb5af7dee29d7cf72896adcf9febc14b711a8b1ce23f3ce6324ac1d35ed4568b4cc55b98b287b05e7e294cce9a6ced175eae0a
data/Gemfile CHANGED
@@ -11,6 +11,11 @@ group :development do
11
11
  gem 'rspec', '~> 3.5.0'
12
12
  end
13
13
 
14
+ group :guard do
15
+ gem 'rb-readline', '~> 0.5.3', require: false
16
+ gem 'guard-rspec', '~> 4.7.3', require: false
17
+ end
18
+
14
19
  group :test do
15
20
  gem 'coveralls', require: false
16
21
  end
data/README.md CHANGED
@@ -1,101 +1,55 @@
1
- # YandexSpeechApi [![Gem Version](https://badge.fury.io/rb/yandex_speech_api.svg)](https://badge.fury.io/rb/yandex_speech_api) [![Build Status](https://travis-ci.org/Medvedu/Yandex-Speech-API.svg?branch=master)](https://travis-ci.org/Medvedu/Yandex-Speech-API) [![Code Climate](https://codeclimate.com/github/Medvedu/Yandex-Speech-API/badges/gpa.svg)](https://codeclimate.com/github/Medvedu/Yandex-Speech-API) [![Inline docs](http://inch-ci.org/github/Medvedu/Yandex-Speech-API.svg?branch=master)](http://inch-ci.org/github/Medvedu/Yandex-Speech-API) [![Coverage Status](https://coveralls.io/repos/github/Medvedu/Yandex-Speech-API/badge.svg)](https://coveralls.io/github/Medvedu/Yandex-Speech-API)
2
-
3
- ## Описание
4
-
5
- Wrapper для синтезатора речи, основанного на технологиях Yandex SpeechKit Cloud API. Предоставляет собой интерфейс для перевода машинного текста (на русском, английском, украинском или турецком язык) в речь. Может использоваться как для прямого воспроизведения текста, так и для записи речи в файл.
6
-
7
- ## Установка
8
-
9
- 1. Перед использованием необходимо получить ключ разработчика. Подробнее на официальном сайте: https://tech.yandex.ru/speechkit/cloud
10
- 2. Добавьте yandex_speech_api в Gemfile.
11
- 3. bundle install
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 [![Gem Version](https://badge.fury.io/rb/yandex_speech_api.svg)](https://badge.fury.io/rb/yandex_speech_api) [![Build Status](https://travis-ci.org/Medvedu/Yandex-Speech-API.svg?branch=master)](https://travis-ci.org/Medvedu/Yandex-Speech-API) [![Code Climate](https://codeclimate.com/github/Medvedu/Yandex-Speech-API/badges/gpa.svg)](https://codeclimate.com/github/Medvedu/Yandex-Speech-API) [![Inline docs](http://inch-ci.org/github/Medvedu/Yandex-Speech-API.svg?branch=master)](http://inch-ci.org/github/Medvedu/Yandex-Speech-API) [![Coverage Status](https://coveralls.io/repos/github/Medvedu/Yandex-Speech-API/badge.svg?branch=master)](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
- key = File.open('secret key/key').readline.strip
30
-
31
- speaker = YandexSpeechApi::Speaker.init key: key, language: 'russian'
32
- speaker.save_to_file "Не будите спящего кота."
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
- ### Пример 2
36
-
37
- _Когда это неоходимо, конструктор позволяет переписывать параметры по умолчанию, например, так можно выбрать язык:_
38
-
39
- ```ruby
40
- YandexSpeechApi::Key.global_key = File.open('secret key/key').readline.strip
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
- message = "Як поїдеш в об'їзд, то будеш і на обід, а як навпростець, то увечері."
57
-
58
- speaker = YandexSpeechApi::Speaker.init
59
- speaker.key = key
60
- speaker.voice = 'Alyss'
61
- speaker.language = 'Ukrain'
62
- speaker.speed = 1.2
63
- speaker.emotion = 'good'
64
- speaker.save_to_file message, '~/downloads/sound'
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
- ### Пример 4
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
- ### Linux-специфичные зависимости:
48
+ ## Dependencies
96
49
 
50
+ * Ruby 2.0.0 or higher.
97
51
  * mpg123
98
52
 
99
- ## Лицензия
53
+ ## License
100
54
 
101
- Данный код распространяется под лицензией MIT, подробнее смотрите [LICENSE](./LICENSE). Остальные права принадлежат их владельцам.
55
+ Released under the MIT License. See the [LICENSE](./LICENSE) file for further details.
@@ -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.init key: key, language: 'russian'
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
- # speaker = YandexSpeechApi::Speaker.init({ key: key, language: 'english', voice: :zahar, speed: 0.23 })
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.init do |s|
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 this key from official site: https://tech.yandex.ru/speechkit
47
- #
48
- # Note: Yandex provide many services throw this site. Your need exactly
49
- # +Yandex SpeechKit Cloud+ key. Other keys will not work!
50
- #
51
- # Do not share this key to third party.
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
- class << self
55
- ##
56
- # Creates +Speaker+ instance.
57
- #
58
- # @param [Proc] callback
59
- # Used to set object state throw {do ... end} block.
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
- # Determines dictor speech speed.
67
+ # Dictor speech speed.
87
68
  #
88
- # @return [Speed]
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 [Voice]
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 [Emotion]
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 right language for incoming text)
95
+ # recommend to select correct language for text)
112
96
  #
113
- # @return [Language]
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 [Format]
108
+ # @return [Symbol]
109
+ # Any from those: "mp3", "wav", "opus"
124
110
 
125
111
  attr_reader :format
126
112
 
127
- ##
128
- # Defines setters for +@voice+, +@language+,+@format+, +@emotion+,
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
- # @setter #emotion=(other)
143
- # @param [Symbol] other
144
- # Expecting values are
145
- # :good, :evil, :neutral
146
- #
147
- # @setter #language=(other)
148
- # @param [Symbol] other
149
- # Expecting values are
150
- # :russian, :english, :ukrain, :turkey
151
- #
152
- # @setter #format=(other)
153
- # @param [Symbol] other
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
- # Speeches text.
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 = Format.new :mp3
198
- binary_data = request text, format: format
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 ('~/downloads')
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
- binary_data = request text
235
- absolute_path = "#{File.expand_path(path_to_file)}.#{format.type}"
236
- File.open(absolute_path, 'w') { |f| f.write binary_data }
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