google-cloud-speech 0.40.2 → 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.yardopts +2 -1
- data/AUTHENTICATION.md +51 -59
- data/LICENSE.md +203 -0
- data/MIGRATING.md +367 -0
- data/README.md +35 -49
- data/lib/google-cloud-speech.rb +19 -0
- data/lib/google/cloud/speech.rb +87 -142
- data/lib/google/cloud/speech/version.rb +1 -1
- metadata +77 -69
- data/LICENSE +0 -201
- data/lib/google/cloud/speech/v1.rb +0 -166
- data/lib/google/cloud/speech/v1/cloud_speech_pb.rb +0 -192
- data/lib/google/cloud/speech/v1/cloud_speech_services_pb.rb +0 -58
- data/lib/google/cloud/speech/v1/credentials.rb +0 -41
- data/lib/google/cloud/speech/v1/doc/google/cloud/speech/v1/cloud_speech.rb +0 -698
- data/lib/google/cloud/speech/v1/doc/google/longrunning/operations.rb +0 -51
- data/lib/google/cloud/speech/v1/doc/google/protobuf/any.rb +0 -131
- data/lib/google/cloud/speech/v1/doc/google/protobuf/duration.rb +0 -91
- data/lib/google/cloud/speech/v1/doc/google/rpc/status.rb +0 -39
- data/lib/google/cloud/speech/v1/helpers.rb +0 -136
- data/lib/google/cloud/speech/v1/speech_client.rb +0 -343
- data/lib/google/cloud/speech/v1/speech_client_config.json +0 -41
- data/lib/google/cloud/speech/v1/stream.rb +0 -615
- data/lib/google/cloud/speech/v1p1beta1.rb +0 -166
- data/lib/google/cloud/speech/v1p1beta1/cloud_speech_pb.rb +0 -200
- data/lib/google/cloud/speech/v1p1beta1/cloud_speech_services_pb.rb +0 -58
- data/lib/google/cloud/speech/v1p1beta1/credentials.rb +0 -41
- data/lib/google/cloud/speech/v1p1beta1/doc/google/cloud/speech/v1p1beta1/cloud_speech.rb +0 -758
- data/lib/google/cloud/speech/v1p1beta1/doc/google/longrunning/operations.rb +0 -51
- data/lib/google/cloud/speech/v1p1beta1/doc/google/protobuf/any.rb +0 -131
- data/lib/google/cloud/speech/v1p1beta1/doc/google/protobuf/duration.rb +0 -91
- data/lib/google/cloud/speech/v1p1beta1/doc/google/rpc/status.rb +0 -39
- data/lib/google/cloud/speech/v1p1beta1/helpers.rb +0 -136
- data/lib/google/cloud/speech/v1p1beta1/speech_client.rb +0 -343
- data/lib/google/cloud/speech/v1p1beta1/speech_client_config.json +0 -41
- data/lib/google/cloud/speech/v1p1beta1/stream.rb +0 -615
@@ -1,343 +0,0 @@
|
|
1
|
-
# Copyright 2020 Google LLC
|
2
|
-
#
|
3
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
-
# you may not use this file except in compliance with the License.
|
5
|
-
# You may obtain a copy of the License at
|
6
|
-
#
|
7
|
-
# https://www.apache.org/licenses/LICENSE-2.0
|
8
|
-
#
|
9
|
-
# Unless required by applicable law or agreed to in writing, software
|
10
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
-
# See the License for the specific language governing permissions and
|
13
|
-
# limitations under the License.
|
14
|
-
#
|
15
|
-
# EDITING INSTRUCTIONS
|
16
|
-
# This file was generated from the file
|
17
|
-
# https://github.com/googleapis/googleapis/blob/master/google/cloud/speech/v1/cloud_speech.proto,
|
18
|
-
# and updates to that file get reflected here through a refresh process.
|
19
|
-
# For the short term, the refresh process will only be runnable by Google
|
20
|
-
# engineers.
|
21
|
-
|
22
|
-
|
23
|
-
require "json"
|
24
|
-
require "pathname"
|
25
|
-
|
26
|
-
require "google/gax"
|
27
|
-
require "google/gax/operation"
|
28
|
-
require "google/longrunning/operations_client"
|
29
|
-
|
30
|
-
require "google/cloud/speech/v1/cloud_speech_pb"
|
31
|
-
require "google/cloud/speech/v1/credentials"
|
32
|
-
require "google/cloud/speech/version"
|
33
|
-
|
34
|
-
module Google
|
35
|
-
module Cloud
|
36
|
-
module Speech
|
37
|
-
module V1
|
38
|
-
# Service that implements Google Cloud Speech API.
|
39
|
-
#
|
40
|
-
# @!attribute [r] speech_stub
|
41
|
-
# @return [Google::Cloud::Speech::V1::Speech::Stub]
|
42
|
-
class SpeechClient
|
43
|
-
# @private
|
44
|
-
attr_reader :speech_stub
|
45
|
-
|
46
|
-
# The default address of the service.
|
47
|
-
SERVICE_ADDRESS = "speech.googleapis.com".freeze
|
48
|
-
|
49
|
-
# The default port of the service.
|
50
|
-
DEFAULT_SERVICE_PORT = 443
|
51
|
-
|
52
|
-
# The default set of gRPC interceptors.
|
53
|
-
GRPC_INTERCEPTORS = []
|
54
|
-
|
55
|
-
DEFAULT_TIMEOUT = 30
|
56
|
-
|
57
|
-
# The scopes needed to make gRPC calls to all of the methods defined in
|
58
|
-
# this service.
|
59
|
-
ALL_SCOPES = [
|
60
|
-
"https://www.googleapis.com/auth/cloud-platform"
|
61
|
-
].freeze
|
62
|
-
|
63
|
-
# @private
|
64
|
-
class OperationsClient < Google::Longrunning::OperationsClient
|
65
|
-
self::SERVICE_ADDRESS = SpeechClient::SERVICE_ADDRESS
|
66
|
-
self::GRPC_INTERCEPTORS = SpeechClient::GRPC_INTERCEPTORS
|
67
|
-
end
|
68
|
-
|
69
|
-
# @param credentials [Google::Auth::Credentials, String, Hash, GRPC::Core::Channel, GRPC::Core::ChannelCredentials, Proc]
|
70
|
-
# Provides the means for authenticating requests made by the client. This parameter can
|
71
|
-
# be many types.
|
72
|
-
# A `Google::Auth::Credentials` uses a the properties of its represented keyfile for
|
73
|
-
# authenticating requests made by this client.
|
74
|
-
# A `String` will be treated as the path to the keyfile to be used for the construction of
|
75
|
-
# credentials for this client.
|
76
|
-
# A `Hash` will be treated as the contents of a keyfile to be used for the construction of
|
77
|
-
# credentials for this client.
|
78
|
-
# A `GRPC::Core::Channel` will be used to make calls through.
|
79
|
-
# A `GRPC::Core::ChannelCredentials` for the setting up the RPC client. The channel credentials
|
80
|
-
# should already be composed with a `GRPC::Core::CallCredentials` object.
|
81
|
-
# A `Proc` will be used as an updater_proc for the Grpc channel. The proc transforms the
|
82
|
-
# metadata for requests, generally, to give OAuth credentials.
|
83
|
-
# @param scopes [Array<String>]
|
84
|
-
# The OAuth scopes for this service. This parameter is ignored if
|
85
|
-
# an updater_proc is supplied.
|
86
|
-
# @param client_config [Hash]
|
87
|
-
# A Hash for call options for each method. See
|
88
|
-
# Google::Gax#construct_settings for the structure of
|
89
|
-
# this data. Falls back to the default config if not specified
|
90
|
-
# or the specified config is missing data points.
|
91
|
-
# @param timeout [Numeric]
|
92
|
-
# The default timeout, in seconds, for calls made through this client.
|
93
|
-
# @param metadata [Hash]
|
94
|
-
# Default metadata to be sent with each request. This can be overridden on a per call basis.
|
95
|
-
# @param service_address [String]
|
96
|
-
# Override for the service hostname, or `nil` to leave as the default.
|
97
|
-
# @param service_port [Integer]
|
98
|
-
# Override for the service port, or `nil` to leave as the default.
|
99
|
-
# @param exception_transformer [Proc]
|
100
|
-
# An optional proc that intercepts any exceptions raised during an API call to inject
|
101
|
-
# custom error handling.
|
102
|
-
def initialize \
|
103
|
-
credentials: nil,
|
104
|
-
scopes: ALL_SCOPES,
|
105
|
-
client_config: {},
|
106
|
-
timeout: DEFAULT_TIMEOUT,
|
107
|
-
metadata: nil,
|
108
|
-
service_address: nil,
|
109
|
-
service_port: nil,
|
110
|
-
exception_transformer: nil,
|
111
|
-
lib_name: nil,
|
112
|
-
lib_version: ""
|
113
|
-
# These require statements are intentionally placed here to initialize
|
114
|
-
# the gRPC module only when it's required.
|
115
|
-
# See https://github.com/googleapis/toolkit/issues/446
|
116
|
-
require "google/gax/grpc"
|
117
|
-
require "google/cloud/speech/v1/cloud_speech_services_pb"
|
118
|
-
|
119
|
-
credentials ||= Google::Cloud::Speech::V1::Credentials.default
|
120
|
-
|
121
|
-
@operations_client = OperationsClient.new(
|
122
|
-
credentials: credentials,
|
123
|
-
scopes: scopes,
|
124
|
-
client_config: client_config,
|
125
|
-
timeout: timeout,
|
126
|
-
lib_name: lib_name,
|
127
|
-
service_address: service_address,
|
128
|
-
service_port: service_port,
|
129
|
-
lib_version: lib_version,
|
130
|
-
metadata: metadata,
|
131
|
-
)
|
132
|
-
|
133
|
-
if credentials.is_a?(String) || credentials.is_a?(Hash)
|
134
|
-
updater_proc = Google::Cloud::Speech::V1::Credentials.new(credentials).updater_proc
|
135
|
-
end
|
136
|
-
if credentials.is_a?(GRPC::Core::Channel)
|
137
|
-
channel = credentials
|
138
|
-
end
|
139
|
-
if credentials.is_a?(GRPC::Core::ChannelCredentials)
|
140
|
-
chan_creds = credentials
|
141
|
-
end
|
142
|
-
if credentials.is_a?(Proc)
|
143
|
-
updater_proc = credentials
|
144
|
-
end
|
145
|
-
if credentials.is_a?(Google::Auth::Credentials)
|
146
|
-
updater_proc = credentials.updater_proc
|
147
|
-
end
|
148
|
-
|
149
|
-
package_version = Google::Cloud::Speech::VERSION
|
150
|
-
|
151
|
-
google_api_client = "gl-ruby/#{RUBY_VERSION}"
|
152
|
-
google_api_client << " #{lib_name}/#{lib_version}" if lib_name
|
153
|
-
google_api_client << " gapic/#{package_version} gax/#{Google::Gax::VERSION}"
|
154
|
-
google_api_client << " grpc/#{GRPC::VERSION}"
|
155
|
-
google_api_client.freeze
|
156
|
-
|
157
|
-
headers = { :"x-goog-api-client" => google_api_client }
|
158
|
-
headers.merge!(metadata) unless metadata.nil?
|
159
|
-
client_config_file = Pathname.new(__dir__).join(
|
160
|
-
"speech_client_config.json"
|
161
|
-
)
|
162
|
-
defaults = client_config_file.open do |f|
|
163
|
-
Google::Gax.construct_settings(
|
164
|
-
"google.cloud.speech.v1.Speech",
|
165
|
-
JSON.parse(f.read),
|
166
|
-
client_config,
|
167
|
-
Google::Gax::Grpc::STATUS_CODE_NAMES,
|
168
|
-
timeout,
|
169
|
-
errors: Google::Gax::Grpc::API_ERRORS,
|
170
|
-
metadata: headers
|
171
|
-
)
|
172
|
-
end
|
173
|
-
|
174
|
-
# Allow overriding the service path/port in subclasses.
|
175
|
-
service_path = service_address || self.class::SERVICE_ADDRESS
|
176
|
-
port = service_port || self.class::DEFAULT_SERVICE_PORT
|
177
|
-
interceptors = self.class::GRPC_INTERCEPTORS
|
178
|
-
@speech_stub = Google::Gax::Grpc.create_stub(
|
179
|
-
service_path,
|
180
|
-
port,
|
181
|
-
chan_creds: chan_creds,
|
182
|
-
channel: channel,
|
183
|
-
updater_proc: updater_proc,
|
184
|
-
scopes: scopes,
|
185
|
-
interceptors: interceptors,
|
186
|
-
&Google::Cloud::Speech::V1::Speech::Stub.method(:new)
|
187
|
-
)
|
188
|
-
|
189
|
-
@recognize = Google::Gax.create_api_call(
|
190
|
-
@speech_stub.method(:recognize),
|
191
|
-
defaults["recognize"],
|
192
|
-
exception_transformer: exception_transformer
|
193
|
-
)
|
194
|
-
@long_running_recognize = Google::Gax.create_api_call(
|
195
|
-
@speech_stub.method(:long_running_recognize),
|
196
|
-
defaults["long_running_recognize"],
|
197
|
-
exception_transformer: exception_transformer
|
198
|
-
)
|
199
|
-
@streaming_recognize = Google::Gax.create_api_call(
|
200
|
-
@speech_stub.method(:streaming_recognize),
|
201
|
-
defaults["streaming_recognize"],
|
202
|
-
exception_transformer: exception_transformer
|
203
|
-
)
|
204
|
-
end
|
205
|
-
|
206
|
-
# Service calls
|
207
|
-
|
208
|
-
# Performs synchronous speech recognition: receive results after all audio
|
209
|
-
# has been sent and processed.
|
210
|
-
#
|
211
|
-
# @param config [Google::Cloud::Speech::V1::RecognitionConfig | Hash]
|
212
|
-
# Required. Provides information to the recognizer that specifies how to
|
213
|
-
# process the request.
|
214
|
-
# A hash of the same form as `Google::Cloud::Speech::V1::RecognitionConfig`
|
215
|
-
# can also be provided.
|
216
|
-
# @param audio [Google::Cloud::Speech::V1::RecognitionAudio | Hash]
|
217
|
-
# Required. The audio data to be recognized.
|
218
|
-
# A hash of the same form as `Google::Cloud::Speech::V1::RecognitionAudio`
|
219
|
-
# can also be provided.
|
220
|
-
# @param options [Google::Gax::CallOptions]
|
221
|
-
# Overrides the default settings for this call, e.g, timeout,
|
222
|
-
# retries, etc.
|
223
|
-
# @yield [result, operation] Access the result along with the RPC operation
|
224
|
-
# @yieldparam result [Google::Cloud::Speech::V1::RecognizeResponse]
|
225
|
-
# @yieldparam operation [GRPC::ActiveCall::Operation]
|
226
|
-
# @return [Google::Cloud::Speech::V1::RecognizeResponse]
|
227
|
-
# @raise [Google::Gax::GaxError] if the RPC is aborted.
|
228
|
-
# @example
|
229
|
-
# require "google/cloud/speech"
|
230
|
-
#
|
231
|
-
# speech_client = Google::Cloud::Speech.new(version: :v1)
|
232
|
-
# encoding = :FLAC
|
233
|
-
# sample_rate_hertz = 44100
|
234
|
-
# language_code = "en-US"
|
235
|
-
# config = {
|
236
|
-
# encoding: encoding,
|
237
|
-
# sample_rate_hertz: sample_rate_hertz,
|
238
|
-
# language_code: language_code
|
239
|
-
# }
|
240
|
-
# uri = "gs://bucket_name/file_name.flac"
|
241
|
-
# audio = { uri: uri }
|
242
|
-
# response = speech_client.recognize(config, audio)
|
243
|
-
|
244
|
-
def recognize \
|
245
|
-
config,
|
246
|
-
audio,
|
247
|
-
options: nil,
|
248
|
-
&block
|
249
|
-
req = {
|
250
|
-
config: config,
|
251
|
-
audio: audio
|
252
|
-
}.delete_if { |_, v| v.nil? }
|
253
|
-
req = Google::Gax::to_proto(req, Google::Cloud::Speech::V1::RecognizeRequest)
|
254
|
-
@recognize.call(req, options, &block)
|
255
|
-
end
|
256
|
-
|
257
|
-
# Performs asynchronous speech recognition: receive results via the
|
258
|
-
# google.longrunning.Operations interface. Returns either an
|
259
|
-
# `Operation.error` or an `Operation.response` which contains
|
260
|
-
# a `LongRunningRecognizeResponse` message.
|
261
|
-
# For more information on asynchronous speech recognition, see the
|
262
|
-
# [how-to](https://cloud.google.com/speech-to-text/docs/async-recognize).
|
263
|
-
#
|
264
|
-
# @param config [Google::Cloud::Speech::V1::RecognitionConfig | Hash]
|
265
|
-
# Required. Provides information to the recognizer that specifies how to
|
266
|
-
# process the request.
|
267
|
-
# A hash of the same form as `Google::Cloud::Speech::V1::RecognitionConfig`
|
268
|
-
# can also be provided.
|
269
|
-
# @param audio [Google::Cloud::Speech::V1::RecognitionAudio | Hash]
|
270
|
-
# Required. The audio data to be recognized.
|
271
|
-
# A hash of the same form as `Google::Cloud::Speech::V1::RecognitionAudio`
|
272
|
-
# can also be provided.
|
273
|
-
# @param options [Google::Gax::CallOptions]
|
274
|
-
# Overrides the default settings for this call, e.g, timeout,
|
275
|
-
# retries, etc.
|
276
|
-
# @return [Google::Gax::Operation]
|
277
|
-
# @raise [Google::Gax::GaxError] if the RPC is aborted.
|
278
|
-
# @example
|
279
|
-
# require "google/cloud/speech"
|
280
|
-
#
|
281
|
-
# speech_client = Google::Cloud::Speech.new(version: :v1)
|
282
|
-
# encoding = :FLAC
|
283
|
-
# sample_rate_hertz = 44100
|
284
|
-
# language_code = "en-US"
|
285
|
-
# config = {
|
286
|
-
# encoding: encoding,
|
287
|
-
# sample_rate_hertz: sample_rate_hertz,
|
288
|
-
# language_code: language_code
|
289
|
-
# }
|
290
|
-
# uri = "gs://bucket_name/file_name.flac"
|
291
|
-
# audio = { uri: uri }
|
292
|
-
#
|
293
|
-
# # Register a callback during the method call.
|
294
|
-
# operation = speech_client.long_running_recognize(config, audio) do |op|
|
295
|
-
# raise op.results.message if op.error?
|
296
|
-
# op_results = op.results
|
297
|
-
# # Process the results.
|
298
|
-
#
|
299
|
-
# metadata = op.metadata
|
300
|
-
# # Process the metadata.
|
301
|
-
# end
|
302
|
-
#
|
303
|
-
# # Or use the return value to register a callback.
|
304
|
-
# operation.on_done do |op|
|
305
|
-
# raise op.results.message if op.error?
|
306
|
-
# op_results = op.results
|
307
|
-
# # Process the results.
|
308
|
-
#
|
309
|
-
# metadata = op.metadata
|
310
|
-
# # Process the metadata.
|
311
|
-
# end
|
312
|
-
#
|
313
|
-
# # Manually reload the operation.
|
314
|
-
# operation.reload!
|
315
|
-
#
|
316
|
-
# # Or block until the operation completes, triggering callbacks on
|
317
|
-
# # completion.
|
318
|
-
# operation.wait_until_done!
|
319
|
-
|
320
|
-
def long_running_recognize \
|
321
|
-
config,
|
322
|
-
audio,
|
323
|
-
options: nil
|
324
|
-
req = {
|
325
|
-
config: config,
|
326
|
-
audio: audio
|
327
|
-
}.delete_if { |_, v| v.nil? }
|
328
|
-
req = Google::Gax::to_proto(req, Google::Cloud::Speech::V1::LongRunningRecognizeRequest)
|
329
|
-
operation = Google::Gax::Operation.new(
|
330
|
-
@long_running_recognize.call(req, options),
|
331
|
-
@operations_client,
|
332
|
-
Google::Cloud::Speech::V1::LongRunningRecognizeResponse,
|
333
|
-
Google::Cloud::Speech::V1::LongRunningRecognizeMetadata,
|
334
|
-
call_options: options
|
335
|
-
)
|
336
|
-
operation.on_done { |operation| yield(operation) } if block_given?
|
337
|
-
operation
|
338
|
-
end
|
339
|
-
end
|
340
|
-
end
|
341
|
-
end
|
342
|
-
end
|
343
|
-
end
|
@@ -1,41 +0,0 @@
|
|
1
|
-
{
|
2
|
-
"interfaces": {
|
3
|
-
"google.cloud.speech.v1.Speech": {
|
4
|
-
"retry_codes": {
|
5
|
-
"idempotent": [
|
6
|
-
"DEADLINE_EXCEEDED",
|
7
|
-
"UNAVAILABLE"
|
8
|
-
],
|
9
|
-
"non_idempotent": []
|
10
|
-
},
|
11
|
-
"retry_params": {
|
12
|
-
"default": {
|
13
|
-
"initial_retry_delay_millis": 100,
|
14
|
-
"retry_delay_multiplier": 1.3,
|
15
|
-
"max_retry_delay_millis": 60000,
|
16
|
-
"initial_rpc_timeout_millis": 1000000,
|
17
|
-
"rpc_timeout_multiplier": 1.0,
|
18
|
-
"max_rpc_timeout_millis": 1000000,
|
19
|
-
"total_timeout_millis": 5000000
|
20
|
-
}
|
21
|
-
},
|
22
|
-
"methods": {
|
23
|
-
"Recognize": {
|
24
|
-
"timeout_millis": 60000,
|
25
|
-
"retry_codes_name": "idempotent",
|
26
|
-
"retry_params_name": "default"
|
27
|
-
},
|
28
|
-
"LongRunningRecognize": {
|
29
|
-
"timeout_millis": 60000,
|
30
|
-
"retry_codes_name": "non_idempotent",
|
31
|
-
"retry_params_name": "default"
|
32
|
-
},
|
33
|
-
"StreamingRecognize": {
|
34
|
-
"timeout_millis": 60000,
|
35
|
-
"retry_codes_name": "non_idempotent",
|
36
|
-
"retry_params_name": "default"
|
37
|
-
}
|
38
|
-
}
|
39
|
-
}
|
40
|
-
}
|
41
|
-
}
|
@@ -1,615 +0,0 @@
|
|
1
|
-
# Copyright 2018 Google LLC
|
2
|
-
#
|
3
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
-
# you may not use this file except in compliance with the License.
|
5
|
-
# You may obtain a copy of the License at
|
6
|
-
#
|
7
|
-
# https://www.apache.org/licenses/LICENSE-2.0
|
8
|
-
#
|
9
|
-
# Unless required by applicable law or agreed to in writing, software
|
10
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
-
# See the License for the specific language governing permissions and
|
13
|
-
# limitations under the License.
|
14
|
-
|
15
|
-
|
16
|
-
require "google/cloud/speech/v1"
|
17
|
-
require "monitor"
|
18
|
-
require "forwardable"
|
19
|
-
|
20
|
-
module Google
|
21
|
-
module Cloud
|
22
|
-
module Speech
|
23
|
-
module V1
|
24
|
-
##
|
25
|
-
# # Stream
|
26
|
-
#
|
27
|
-
# A resource that represents the streaming requests and responses.
|
28
|
-
#
|
29
|
-
# @example
|
30
|
-
# require "google/cloud/speech"
|
31
|
-
#
|
32
|
-
# speech_client = Google::Cloud::Speech.new
|
33
|
-
# streaming_config = {
|
34
|
-
# config: {
|
35
|
-
# encoding: :linear16,
|
36
|
-
# language_code: "en-US",
|
37
|
-
# sample_rate_hertz: 16000
|
38
|
-
# }
|
39
|
-
# }
|
40
|
-
# stream = speech_client.streaming_recognize(streaming_config)
|
41
|
-
#
|
42
|
-
# # Stream 5 seconds of audio from the microphone
|
43
|
-
# # Actual implementation of microphone input varies by platform
|
44
|
-
# 5.times do
|
45
|
-
# stream.send MicrophoneInput.read(32000)
|
46
|
-
# end
|
47
|
-
#
|
48
|
-
# stream.stop
|
49
|
-
# stream.wait_until_complete!
|
50
|
-
#
|
51
|
-
# results = stream.results
|
52
|
-
# result = results.first.alternatives.first
|
53
|
-
# result.transcript #=> "how old is the Brooklyn Bridge"
|
54
|
-
# result.confidence #=> 0.9826789498329163
|
55
|
-
#
|
56
|
-
class Stream
|
57
|
-
include MonitorMixin
|
58
|
-
##
|
59
|
-
# @private Creates a new Speech Stream instance.
|
60
|
-
# This must always be private, since it may change as the implementation
|
61
|
-
# changes over time.
|
62
|
-
def initialize streaming_config, streaming_call
|
63
|
-
super() # to init MonitorMixin
|
64
|
-
|
65
|
-
@streaming_call = streaming_call
|
66
|
-
@streaming_recognize_request = {
|
67
|
-
streaming_config: streaming_config
|
68
|
-
}
|
69
|
-
@results = []
|
70
|
-
@callbacks = Hash.new { |h, k| h[k] = [] }
|
71
|
-
end
|
72
|
-
|
73
|
-
##
|
74
|
-
# Starts the stream. The stream will be started in the first #send call.
|
75
|
-
def start
|
76
|
-
return if @request_queue
|
77
|
-
@request_queue = EnumeratorQueue.new(self)
|
78
|
-
@request_queue.push @streaming_recognize_request
|
79
|
-
|
80
|
-
Thread.new { background_run }
|
81
|
-
end
|
82
|
-
|
83
|
-
##
|
84
|
-
# Checks if the stream has been started.
|
85
|
-
#
|
86
|
-
# @return [boolean] `true` when started, `false` otherwise.
|
87
|
-
def started?
|
88
|
-
synchronize do
|
89
|
-
!(!@request_queue)
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
##
|
94
|
-
# Sends audio content to the server.
|
95
|
-
#
|
96
|
-
# @param [String] bytes A string of binary audio data to be recognized.
|
97
|
-
# The data should be encoded as `ASCII-8BIT`.
|
98
|
-
#
|
99
|
-
# @example
|
100
|
-
# require "google/cloud/speech"
|
101
|
-
#
|
102
|
-
# speech_client = Google::Cloud::Speech.new
|
103
|
-
# streaming_config = {
|
104
|
-
# config: {
|
105
|
-
# encoding: :linear16,
|
106
|
-
# language_code: "en-US",
|
107
|
-
# sample_rate_hertz: 16000
|
108
|
-
# }
|
109
|
-
# }
|
110
|
-
# stream = speech_client.streaming_recognize(streaming_config)
|
111
|
-
#
|
112
|
-
# # Stream 5 seconds of audio from the microphone
|
113
|
-
# # Actual implementation of microphone input varies by platform
|
114
|
-
# 5.times do
|
115
|
-
# stream.send MicrophoneInput.read(32000)
|
116
|
-
# end
|
117
|
-
#
|
118
|
-
# stream.stop
|
119
|
-
# stream.wait_until_complete!
|
120
|
-
#
|
121
|
-
# results = stream.results
|
122
|
-
# result = results.first.alternatives.first
|
123
|
-
# result.transcript #=> "how old is the Brooklyn Bridge"
|
124
|
-
# result.confidence #=> 0.9826789498329163
|
125
|
-
#
|
126
|
-
def send bytes
|
127
|
-
start # lazily call start if the stream wasn't started yet
|
128
|
-
# TODO: do not send if stopped?
|
129
|
-
synchronize do
|
130
|
-
req = V1::StreamingRecognizeRequest.new(
|
131
|
-
audio_content: bytes.encode(Encoding::ASCII_8BIT)
|
132
|
-
)
|
133
|
-
@request_queue.push req
|
134
|
-
end
|
135
|
-
end
|
136
|
-
|
137
|
-
##
|
138
|
-
# Stops the stream. Signals to the server that no more data will be
|
139
|
-
# sent.
|
140
|
-
def stop
|
141
|
-
synchronize do
|
142
|
-
return if @request_queue.nil?
|
143
|
-
@request_queue.push self
|
144
|
-
@stopped = true
|
145
|
-
end
|
146
|
-
end
|
147
|
-
|
148
|
-
##
|
149
|
-
# Checks if the stream has been stopped.
|
150
|
-
#
|
151
|
-
# @return [boolean] `true` when stopped, `false` otherwise.
|
152
|
-
def stopped?
|
153
|
-
synchronize do
|
154
|
-
@stopped
|
155
|
-
end
|
156
|
-
end
|
157
|
-
|
158
|
-
##
|
159
|
-
# The speech recognition results for the audio.
|
160
|
-
#
|
161
|
-
# @return [Array<Result>] The transcribed text of audio recognized.
|
162
|
-
#
|
163
|
-
# @example
|
164
|
-
# require "google/cloud/speech"
|
165
|
-
#
|
166
|
-
# speech_client = Google::Cloud::Speech.new
|
167
|
-
# streaming_config = {
|
168
|
-
# config: {
|
169
|
-
# encoding: :linear16,
|
170
|
-
# language_code: "en-US",
|
171
|
-
# sample_rate_hertz: 16000
|
172
|
-
# }
|
173
|
-
# }
|
174
|
-
# stream = speech_client.streaming_recognize(streaming_config)
|
175
|
-
#
|
176
|
-
# # Stream 5 seconds of audio from the microphone
|
177
|
-
# # Actual implementation of microphone input varies by platform
|
178
|
-
# 5.times do
|
179
|
-
# stream.send MicrophoneInput.read(32000)
|
180
|
-
# end
|
181
|
-
#
|
182
|
-
# stream.stop
|
183
|
-
#
|
184
|
-
# results = stream.results
|
185
|
-
# results.each do |result|
|
186
|
-
# result.alternatives.each do |alternative|
|
187
|
-
# puts alternative.transcript
|
188
|
-
# puts alternative.confidence
|
189
|
-
# end
|
190
|
-
# end
|
191
|
-
#
|
192
|
-
def results
|
193
|
-
synchronize do
|
194
|
-
@results
|
195
|
-
end
|
196
|
-
end
|
197
|
-
|
198
|
-
##
|
199
|
-
# Whether all speech recognition results have been returned.
|
200
|
-
#
|
201
|
-
# @return [Boolean] All speech recognition results have been returned.
|
202
|
-
#
|
203
|
-
# @example
|
204
|
-
# require "google/cloud/speech"
|
205
|
-
#
|
206
|
-
# speech_client = Google::Cloud::Speech.new
|
207
|
-
# streaming_config = {
|
208
|
-
# config: {
|
209
|
-
# encoding: :linear16,
|
210
|
-
# language_code: "en-US",
|
211
|
-
# sample_rate_hertz: 16000
|
212
|
-
# }
|
213
|
-
# }
|
214
|
-
# stream = speech_client.streaming_recognize(streaming_config)
|
215
|
-
#
|
216
|
-
# # Stream 5 seconds of audio from the microphone
|
217
|
-
# # Actual implementation of microphone input varies by platform
|
218
|
-
# 5.times do
|
219
|
-
# stream.send MicrophoneInput.read(32000)
|
220
|
-
# end
|
221
|
-
#
|
222
|
-
# stream.stop
|
223
|
-
#
|
224
|
-
# stream.wait_until_complete!
|
225
|
-
# stream.complete? #=> true
|
226
|
-
#
|
227
|
-
# results = stream.results
|
228
|
-
# results.each do |result|
|
229
|
-
# result.alternatives.each do |alternative|
|
230
|
-
# puts alternative.transcript
|
231
|
-
# puts alternative.confidence
|
232
|
-
# end
|
233
|
-
# end
|
234
|
-
#
|
235
|
-
def complete?
|
236
|
-
synchronize do
|
237
|
-
@complete
|
238
|
-
end
|
239
|
-
end
|
240
|
-
|
241
|
-
##
|
242
|
-
# Blocks until all speech recognition results have been returned.
|
243
|
-
#
|
244
|
-
# @example
|
245
|
-
# require "google/cloud/speech"
|
246
|
-
#
|
247
|
-
# speech_client = Google::Cloud::Speech.new
|
248
|
-
# streaming_config = {
|
249
|
-
# config: {
|
250
|
-
# encoding: :linear16,
|
251
|
-
# language_code: "en-US",
|
252
|
-
# sample_rate_hertz: 16000
|
253
|
-
# }
|
254
|
-
# }
|
255
|
-
# stream = speech_client.streaming_recognize(streaming_config)
|
256
|
-
#
|
257
|
-
# # Stream 5 seconds of audio from the microphone
|
258
|
-
# # Actual implementation of microphone input varies by platform
|
259
|
-
# 5.times do
|
260
|
-
# stream.send MicrophoneInput.read(32000)
|
261
|
-
# end
|
262
|
-
#
|
263
|
-
# stream.stop
|
264
|
-
#
|
265
|
-
# stream.wait_until_complete!
|
266
|
-
# stream.complete? #=> true
|
267
|
-
#
|
268
|
-
# results = stream.results
|
269
|
-
# results.each do |result|
|
270
|
-
# result.alternatives.each do |alternative|
|
271
|
-
# puts alternative.transcript
|
272
|
-
# puts alternative.confidence
|
273
|
-
# end
|
274
|
-
# end
|
275
|
-
#
|
276
|
-
def wait_until_complete!
|
277
|
-
complete_check = nil
|
278
|
-
synchronize { complete_check = @complete }
|
279
|
-
while complete_check.nil?
|
280
|
-
sleep 1
|
281
|
-
synchronize { complete_check = @complete }
|
282
|
-
end
|
283
|
-
end
|
284
|
-
|
285
|
-
##
|
286
|
-
# Register to be notified on the reception of an interim result.
|
287
|
-
#
|
288
|
-
# @yield [callback] The block for accessing final and interim results.
|
289
|
-
# @yieldparam [Array<Result>] final_results The final results.
|
290
|
-
# @yieldparam [Array<Result>] interim_results The interim results.
|
291
|
-
#
|
292
|
-
# @example
|
293
|
-
# require "google/cloud/speech"
|
294
|
-
#
|
295
|
-
# speech_client = Google::Cloud::Speech.new
|
296
|
-
# streaming_config = {
|
297
|
-
# config: {
|
298
|
-
# encoding: :linear16,
|
299
|
-
# language_code: "en-US",
|
300
|
-
# sample_rate_hertz: 16000
|
301
|
-
# },
|
302
|
-
# interim_results: true
|
303
|
-
# }
|
304
|
-
# stream = speech_client.streaming_recognize(streaming_config)
|
305
|
-
#
|
306
|
-
# # register callback for when an interim result is returned
|
307
|
-
# stream.on_interim do |final_results, interim_results|
|
308
|
-
# interim_result = interim_results.first
|
309
|
-
# interim_alternative = interim_result.alternatives.first
|
310
|
-
# puts interim_alternative.transcript # "how old is the Brooklyn Bridge"
|
311
|
-
# puts interim_alternative.confidence # 0.9826789498329163
|
312
|
-
# puts interim_result.stability # 0.8999
|
313
|
-
# end
|
314
|
-
#
|
315
|
-
# # Stream 5 seconds of audio from the microphone
|
316
|
-
# # Actual implementation of microphone input varies by platform
|
317
|
-
# 5.times do
|
318
|
-
# stream.send MicrophoneInput.read(32000)
|
319
|
-
# end
|
320
|
-
#
|
321
|
-
# stream.stop
|
322
|
-
#
|
323
|
-
def on_interim &block
|
324
|
-
synchronize do
|
325
|
-
@callbacks[:interim] << block
|
326
|
-
end
|
327
|
-
end
|
328
|
-
|
329
|
-
##
|
330
|
-
# @private yields two arguments, all final results and the
|
331
|
-
# non-final/incomplete result
|
332
|
-
def pass_interim! interim_results
|
333
|
-
synchronize do
|
334
|
-
@callbacks[:interim].each { |c| c.call results, interim_results }
|
335
|
-
end
|
336
|
-
end
|
337
|
-
|
338
|
-
##
|
339
|
-
# Register to be notified on the reception of a final result.
|
340
|
-
#
|
341
|
-
# @yield [callback] The block for accessing final results.
|
342
|
-
# @yieldparam [Array<Result>] results The final results.
|
343
|
-
#
|
344
|
-
# @example
|
345
|
-
# require "google/cloud/speech"
|
346
|
-
#
|
347
|
-
# speech_client = Google::Cloud::Speech.new
|
348
|
-
# streaming_config = {
|
349
|
-
# config: {
|
350
|
-
# encoding: :linear16,
|
351
|
-
# language_code: "en-US",
|
352
|
-
# sample_rate_hertz: 16000
|
353
|
-
# }
|
354
|
-
# }
|
355
|
-
# stream = speech_client.streaming_recognize(streaming_config)
|
356
|
-
#
|
357
|
-
# # register callback for when a final result has been received
|
358
|
-
# stream.on_result do |results|
|
359
|
-
# results.each do |result|
|
360
|
-
# puts result.alternatives.first.transcript
|
361
|
-
# end
|
362
|
-
# end
|
363
|
-
#
|
364
|
-
# # Stream 5 seconds of audio from the microphone
|
365
|
-
# # Actual implementation of microphone input varies by platform
|
366
|
-
# 5.times do
|
367
|
-
# stream.send MicrophoneInput.read(32000)
|
368
|
-
# end
|
369
|
-
#
|
370
|
-
# stream.stop
|
371
|
-
# stream.wait_until_complete!
|
372
|
-
#
|
373
|
-
# results = stream.results
|
374
|
-
# result = results.first.alternatives.first
|
375
|
-
# result.transcript #=> "how old is the Brooklyn Bridge"
|
376
|
-
# result.confidence #=> 0.9826789498329163
|
377
|
-
#
|
378
|
-
def on_result &block
|
379
|
-
synchronize do
|
380
|
-
@callbacks[:result] << block
|
381
|
-
end
|
382
|
-
end
|
383
|
-
|
384
|
-
##
|
385
|
-
# @private add a result object, and call the callbacks
|
386
|
-
def pass_result! result_grpc
|
387
|
-
synchronize do
|
388
|
-
@results << result_grpc
|
389
|
-
@callbacks[:result].each { |c| c.call @results }
|
390
|
-
end
|
391
|
-
end
|
392
|
-
|
393
|
-
##
|
394
|
-
# Register to be notified when the end of the audio stream has been
|
395
|
-
# reached.
|
396
|
-
#
|
397
|
-
# @yield [callback] The block to be called when the end of the audio
|
398
|
-
# stream has been reached.
|
399
|
-
#
|
400
|
-
# @example
|
401
|
-
# require "google/cloud/speech"
|
402
|
-
#
|
403
|
-
# speech_client = Google::Cloud::Speech.new
|
404
|
-
# streaming_config = {
|
405
|
-
# config: {
|
406
|
-
# encoding: :linear16,
|
407
|
-
# language_code: "en-US",
|
408
|
-
# sample_rate_hertz: 16000
|
409
|
-
# }
|
410
|
-
# }
|
411
|
-
# stream = speech_client.streaming_recognize(streaming_config)
|
412
|
-
#
|
413
|
-
# # register callback for when stream has ended.
|
414
|
-
# stream.on_complete do
|
415
|
-
# puts "Stream has ended."
|
416
|
-
# end
|
417
|
-
#
|
418
|
-
# # Stream 5 seconds of audio from the microphone
|
419
|
-
# # Actual implementation of microphone input varies by platform
|
420
|
-
# 5.times do
|
421
|
-
# stream.send MicrophoneInput.read(32000)
|
422
|
-
# end
|
423
|
-
#
|
424
|
-
# stream.stop
|
425
|
-
#
|
426
|
-
def on_complete &block
|
427
|
-
synchronize do
|
428
|
-
@callbacks[:complete] << block
|
429
|
-
end
|
430
|
-
end
|
431
|
-
|
432
|
-
##
|
433
|
-
# @private yields when the end of the audio stream has been reached.
|
434
|
-
def pass_complete!
|
435
|
-
synchronize do
|
436
|
-
@complete = true
|
437
|
-
@callbacks[:complete].each(&:call)
|
438
|
-
end
|
439
|
-
end
|
440
|
-
|
441
|
-
##
|
442
|
-
# Register to be notified when the server has detected the end of the
|
443
|
-
# user's speech utterance and expects no additional speech. Therefore,
|
444
|
-
# the server will not process additional audio. The client should stop
|
445
|
-
# sending additional audio data. This event only occurs when `utterance`
|
446
|
-
# is `true`.
|
447
|
-
#
|
448
|
-
# @yield [callback] The block to be called when the end of the audio
|
449
|
-
# stream has been reached.
|
450
|
-
#
|
451
|
-
# @example
|
452
|
-
# require "google/cloud/speech"
|
453
|
-
#
|
454
|
-
# speech_client = Google::Cloud::Speech.new
|
455
|
-
# streaming_config = {
|
456
|
-
# config: {
|
457
|
-
# encoding: :linear16,
|
458
|
-
# language_code: "en-US",
|
459
|
-
# sample_rate_hertz: 16000
|
460
|
-
# },
|
461
|
-
# single_utterance: true
|
462
|
-
# }
|
463
|
-
# stream = speech_client.streaming_recognize(streaming_config)
|
464
|
-
#
|
465
|
-
# # register callback for when utterance has occurred.
|
466
|
-
# stream.on_utterance do
|
467
|
-
# puts "Utterance has occurred."
|
468
|
-
# stream.stop
|
469
|
-
# end
|
470
|
-
#
|
471
|
-
# # Stream 5 seconds of audio from the microphone
|
472
|
-
# # Actual implementation of microphone input varies by platform
|
473
|
-
# 5.times do
|
474
|
-
# stream.send MicrophoneInput.read(32000)
|
475
|
-
# end
|
476
|
-
#
|
477
|
-
# stream.stop unless stream.stopped?
|
478
|
-
#
|
479
|
-
def on_utterance &block
|
480
|
-
synchronize do
|
481
|
-
@callbacks[:utterance] << block
|
482
|
-
end
|
483
|
-
end
|
484
|
-
|
485
|
-
##
|
486
|
-
# @private returns single final result once :END_OF_SINGLE_UTTERANCE is
|
487
|
-
# received.
|
488
|
-
def pass_utterance!
|
489
|
-
synchronize do
|
490
|
-
@callbacks[:utterance].each(&:call)
|
491
|
-
end
|
492
|
-
end
|
493
|
-
|
494
|
-
##
|
495
|
-
# Register to be notified of an error received during the stream.
|
496
|
-
#
|
497
|
-
# @yield [callback] The block for accessing final results.
|
498
|
-
# @yieldparam [Exception] error The error raised.
|
499
|
-
#
|
500
|
-
# @example
|
501
|
-
# require "google/cloud/speech"
|
502
|
-
#
|
503
|
-
# speech_client = Google::Cloud::Speech.new
|
504
|
-
# streaming_config = {
|
505
|
-
# config: {
|
506
|
-
# encoding: :linear16,
|
507
|
-
# language_code: "en-US",
|
508
|
-
# sample_rate_hertz: 16000
|
509
|
-
# }
|
510
|
-
# }
|
511
|
-
# stream = speech_client.streaming_recognize(streaming_config)
|
512
|
-
#
|
513
|
-
# # register callback for when an error is returned
|
514
|
-
# stream.on_error do |error|
|
515
|
-
# puts "The following error occurred while streaming: #{error}"
|
516
|
-
# stream.stop
|
517
|
-
# end
|
518
|
-
#
|
519
|
-
# # Stream 5 seconds of audio from the microphone
|
520
|
-
# # Actual implementation of microphone input varies by platform
|
521
|
-
# 5.times do
|
522
|
-
# stream.send MicrophoneInput.read(32000)
|
523
|
-
# end
|
524
|
-
#
|
525
|
-
# stream.stop
|
526
|
-
#
|
527
|
-
def on_error &block
|
528
|
-
synchronize do
|
529
|
-
@callbacks[:error] << block
|
530
|
-
end
|
531
|
-
end
|
532
|
-
|
533
|
-
# @private returns error object from the stream thread.
|
534
|
-
def error! err
|
535
|
-
synchronize do
|
536
|
-
@callbacks[:error].each { |c| c.call err }
|
537
|
-
end
|
538
|
-
end
|
539
|
-
|
540
|
-
protected
|
541
|
-
|
542
|
-
def background_run
|
543
|
-
response_enum = @streaming_call.call(@request_queue.each_item)
|
544
|
-
response_enum.each do |response|
|
545
|
-
begin
|
546
|
-
background_results response
|
547
|
-
background_event_type response.speech_event_type
|
548
|
-
background_error response.error
|
549
|
-
rescue StandardError => e
|
550
|
-
error! e
|
551
|
-
end
|
552
|
-
end
|
553
|
-
rescue StandardError => e
|
554
|
-
error! e
|
555
|
-
ensure
|
556
|
-
pass_complete!
|
557
|
-
Thread.pass
|
558
|
-
end
|
559
|
-
|
560
|
-
def background_results response
|
561
|
-
# Handle the results (StreamingRecognitionResult)
|
562
|
-
return unless response.results && response.results.any?
|
563
|
-
|
564
|
-
final_grpc = response.results.select { |res| res.is_final }
|
565
|
-
# Only one final result
|
566
|
-
final_grpc = if final_grpc.any? then final_grpc.first else nil end
|
567
|
-
|
568
|
-
interim_grpc = response.results.select { |res| !res.is_final }
|
569
|
-
|
570
|
-
# callback for interim results received
|
571
|
-
pass_interim! interim_grpc if interim_grpc.any?
|
572
|
-
# callback for final results received, if any
|
573
|
-
pass_result! final_grpc if final_grpc
|
574
|
-
end
|
575
|
-
|
576
|
-
def background_event_type event_type
|
577
|
-
# Handle the event_type by raising events
|
578
|
-
# TODO: do we automatically call stop here?
|
579
|
-
pass_utterance! if event_type == :END_OF_SINGLE_UTTERANCE
|
580
|
-
end
|
581
|
-
|
582
|
-
def background_error error
|
583
|
-
return if error.nil?
|
584
|
-
|
585
|
-
require "grpc/errors"
|
586
|
-
raise GRPC::BadStatus.new(error.code, error.message)
|
587
|
-
end
|
588
|
-
|
589
|
-
# @private
|
590
|
-
class EnumeratorQueue
|
591
|
-
extend Forwardable
|
592
|
-
def_delegators :@q, :push
|
593
|
-
|
594
|
-
# @private
|
595
|
-
def initialize sentinel
|
596
|
-
@q = Queue.new
|
597
|
-
@sentinel = sentinel
|
598
|
-
end
|
599
|
-
|
600
|
-
# @private
|
601
|
-
def each_item
|
602
|
-
return enum_for(:each_item) unless block_given?
|
603
|
-
loop do
|
604
|
-
r = @q.pop
|
605
|
-
break if r.equal? @sentinel
|
606
|
-
raise r if r.is_a? Exception
|
607
|
-
yield r
|
608
|
-
end
|
609
|
-
end
|
610
|
-
end
|
611
|
-
end
|
612
|
-
end
|
613
|
-
end
|
614
|
-
end
|
615
|
-
end
|