shared/output/cloud/speech_v1/lib/google/cloud/speech/v1/speech/client.rb
##
# Performs asynchronous speech recognition: receive results via the
# google.longrunning.Operations interface. Returns either an
# `Operation.error` or an `Operation.response` which contains
# a `LongRunningRecognizeResponse` message.
# For more information on asynchronous speech recognition, see the
# [how-to](https://cloud.google.com/speech-to-text/docs/async-recognize).
#
# @overload long_running_recognize(request, options = nil)
#   Pass arguments to `long_running_recognize` via a request object, either of type
#   {::Google::Cloud::Speech::V1::LongRunningRecognizeRequest} or an equivalent Hash.
#
#   @param request [::Google::Cloud::Speech::V1::LongRunningRecognizeRequest, ::Hash]
#     A request object representing the call parameters. Required. To specify no
#     parameters, or to keep all the default parameter values, pass an empty Hash.
#   @param options [::Gapic::CallOptions, ::Hash]
#     Overrides the default settings for this call, e.g, timeout, retries, etc. Optional.
#
# @overload long_running_recognize(config: nil, audio: nil, output_config: nil)
#   Pass arguments to `long_running_recognize` via keyword arguments. Note that at
#   least one keyword argument is required. To specify no parameters, or to keep all
#   the default parameter values, pass an empty Hash as a request object (see above).
#
#   @param config [::Google::Cloud::Speech::V1::RecognitionConfig, ::Hash]
#     Required. Provides information to the recognizer that specifies how to
#     process the request.
#   @param audio [::Google::Cloud::Speech::V1::RecognitionAudio, ::Hash]
#     Required. The audio data to be recognized.
#   @param output_config [::Google::Cloud::Speech::V1::TranscriptOutputConfig, ::Hash]
#     Optional. Specifies an optional destination for the recognition results.
#
# @yield [response, operation] Access the result along with the RPC operation
# @yieldparam response [::Gapic::Operation]
# @yieldparam operation [::GRPC::ActiveCall::Operation]
#
# @return [::Gapic::Operation]
#
# @raise [::Google::Cloud::Error] if the RPC is aborted.
#
# @example Transcribe a long audio file using asynchronous speech recognition
#   require "google/cloud/speech/v1/speech"
#
#   client = ::Google::Cloud::Speech::V1::Speech::Client.new
#
#   local_file_path = File.read "resources/brooklyn_bridge.raw"
#
#   audio = {
#     # Path to local audio file, e.g. /path/audio.wav
#     content: local_file_path
#   }
#   config = {
#     # The language of the supplied audio
#     language_code: "en-US",
#     # Sample rate in Hertz of the audio data sent
#     sample_rate_hertz: 16000,
#     # Encoding of audio data sent. This sample sets this explicitly.
#     # This field is optional for FLAC and WAV audio formats.
#     encoding: :LINEAR16
#   }
#
#   response = client.long_running_recognize audio: audio, config: config
#
#   # Wait until the long running operation is done
#   response.wait_until_done!
#   response.results.each do |result|
#     # First alternative is the most probable result
#     alternative = result.alternatives[0]
#     puts "Transcript: #{alternative.transcript}"
#   end
#
# @example Transcribe long audio file from Cloud Storage using asynchronous speech recognition
#   require "google/cloud/speech/v1/speech"
#
#   client = ::Google::Cloud::Speech::V1::Speech::Client.new
#
#   storage_uri = "gs://cloud-samples-data/speech/brooklyn_bridge.raw"
#
#   audio = {
#     # URI for audio file in Cloud Storage, e.g. gs://[BUCKET]/[FILE]
#     uri: storage_uri
#   }
#   config = {
#     # Sample rate in Hertz of the audio data sent
#     sample_rate_hertz: 16000,
#     # The language of the supplied audio
#     language_code: "en-US",
#     # Encoding of audio data sent. This sample sets this explicitly.
#     # This field is optional for FLAC and WAV audio formats.
#     encoding: :LINEAR16
#   }
#
#   response = client.long_running_recognize audio: audio, config: config
#
#   # Wait until the long running operation is done
#   response.wait_until_done!
#   response.results.each do |result|
#     # First alternative is the most probable result
#     alternative = result.alternatives[0]
#     puts "Transcript: #{alternative.transcript}"
#   end
#
# @example Print start and end time of each word spoken in audio file from Cloud Storage
#   require "google/cloud/speech/v1/speech"
#
#   client = ::Google::Cloud::Speech::V1::Speech::Client.new
#
#   storage_uri = "gs://cloud-samples-data/speech/brooklyn_bridge.flac"
#
#   audio = {
#     # URI for audio file in Cloud Storage, e.g. gs://[BUCKET]/[FILE]
#     uri: storage_uri
#   }
#   config = {
#     # When enabled, the first result returned by the API will include a list
#     # of words and the start and end time offsets (timestamps) for those words.
#     enable_word_time_offsets: true,
#     # The language of the supplied audio
#     language_code: "en-US"
#   }
#
#   response = client.long_running_recognize audio: audio, config: config
#
#   # Wait until the long running operation is done
#   response.wait_until_done!
#
#   # The first result includes start and end time word offsets
#   result = response.results[0]
#
#   # First alternative is the most probable result
#   alternative = result.alternatives[0]
#   puts "Transcript: #{alternative.transcript}"
#
#   # Print the start and end time of each word
#   alternative.words.each do |word|
#     puts "Word: #{word.word}"
#     puts "Start time: #{word.start_time.seconds} seconds #{word.start_time.nanos} nanos"
#     puts "End time: #{word.end_time.seconds} seconds #{word.end_time.nanos} nanos"
#   end
#
Performs asynchronous speech recognition: receive results via the
google.longrunning.Operations interface.
Performs asynchronous speech recognition : receive results via the google . longrunning . Operations interface .
def long_running_recognize request, options = nil
              raise ::ArgumentError, "request must be provided" if request.nil?
              request = ::Gapic::Protobuf.coerce request, to: ::Google::Cloud::Speech::V1::LongRunningRecognizeRequest
              options = ::Gapic::CallOptions.new(**options.to_h) if options.respond_to? :to_h
              metadata = @config.rpcs.long_running_recognize.metadata.to_h
              metadata[:"x-goog-api-client"] ||= ::Gapic::Headers.x_goog_api_client \
                lib_name: @config.lib_name, lib_version: @config.lib_version,
                gapic_version: ::Google::Cloud::Speech::V1::VERSION
              metadata[:"x-goog-user-project"] = @quota_project_id if @quota_project_id
              options.apply_defaults timeout:      @config.rpcs.long_running_recognize.timeout,
                                     metadata:     metadata,
                                     retry_policy: @config.rpcs.long_running_recognize.retry_policy
              options.apply_defaults timeout:      @config.timeout,
                                     metadata:     @config.metadata,
                                     retry_policy: @config.retry_policy
              @speech_stub.call_rpc :long_running_recognize, request, options: options do |response, operation|
                response = ::Gapic::Operation.new response, @operations_client, options: options
                yield response, operation if block_given?
                return response
              end
            rescue ::GRPC::BadStatus => e
              raise ::Google::Cloud::Error.from_error(e)
            end
def long_running_recognize request , options = nil raise :: ArgumentError , "request must be provided" if request . nil? request = :: Gapic :: Protobuf . coerce request , to : :: Google :: Cloud :: Speech :: V1 :: LongRunningRecognizeRequest options = :: Gapic :: CallOptions . new ( ** options . to_h ) if options . respond_to? :to_h metadata = @config . rpcs . long_running_recognize . metadata . to_h metadata [ :" x-goog-api-client " ] ||= :: Gapic :: Headers . x_goog_api_client lib_name : @config . lib_name , lib_version : @config . lib_version , gapic_version : :: Google :: Cloud :: Speech :: V1 :: VERSION metadata [ :" x-goog-user-project " ] = @quota_project_id if @quota_project_id options . apply_defaults timeout : @config . rpcs . long_running_recognize . timeout , metadata : metadata , retry_policy : @config . rpcs . long_running_recognize . retry_policy options . apply_defaults timeout : @config . timeout , metadata : @config . metadata , retry_policy : @config . retry_policy @speech_stub . call_rpc :long_running_recognize , request , options : options do | response , operation | response = :: Gapic :: Operation . new response , @operations_client , options : options yield response , operation if block_given? return response end rescue :: GRPC :: BadStatus => e raise :: Google :: Cloud :: Error . from_error ( e ) end
Performs asynchronous speech recognition: receive results via the
google.longrunning.Operations interface.
Performs asynchronous speech recognition : receive results via the google . longrunning . Operations interface .
# Converts hash and nil to an options object # Customize the options with defaults # Set x-goog-api-client and x-goog-user-project headers
None
::Gapic::Operation
[::Google::Cloud::Error] if the RPC is aborted.
[ :: Google :: Cloud :: Error ] if the RPC is aborted .
request
<not_specific>
options
<not_specific>
]
<not_specific>
Access the result along with the RPC operation
Access the result along with the RPC operation
<not_specific>
None
overload
long_running_recognize(request, options = nil) Pass arguments to `long_running_recognize` via a request object, either of type {::Google::Cloud::Speech::V1::LongRunningRecognizeRequest} or an equivalent Hash. @param request [::Google::Cloud::Speech::V1::LongRunningRecognizeRequest, ::Hash] A request object representing the call parameters. Required. To specify no parameters, or to keep all the default parameter values, pass an empty Hash. @param options [::Gapic::CallOptions, ::Hash] Overrides the default settings for this call, e.g, timeout, retries, etc. Optional.
long_running_recognize ( request options = nil ) Pass arguments to ` long_running_recognize ` via a request object either of type { :: Google :: Cloud :: Speech :: V1 :: LongRunningRecognizeRequest } or an equivalent Hash . @param request [ :: Google :: Cloud :: Speech :: V1 :: LongRunningRecognizeRequest :: Hash ] A request object representing the call parameters . Required . To specify no parameters or to keep all the default parameter values pass an empty Hash . @param options [ :: Gapic :: CallOptions :: Hash ] Overrides the default settings for this call e . g timeout retries etc . Optional .
overload
nil, audio: nil, output_config: nil) Pass arguments to `long_running_recognize` via keyword arguments. Note that at least one keyword argument is required. To specify no parameters, or to keep all the default parameter values, pass an empty Hash as a request object . @param config [::Google::Cloud::Speech::V1::RecognitionConfig, ::Hash] Required. Provides information to the recognizer that specifies how to process the request. @param audio [::Google::Cloud::Speech::V1::RecognitionAudio, ::Hash] Required. The audio data to be recognized. @param output_config [::Google::Cloud::Speech::V1::TranscriptOutputConfig, ::Hash] Optional. Specifies an optional destination for the recognition results.
nil audio : nil output_config : nil ) Pass arguments to ` long_running_recognize ` via keyword arguments . Note that at least one keyword argument is required . To specify no parameters or to keep all the default parameter values pass an empty Hash as a request object . @param config [ :: Google :: Cloud :: Speech :: V1 :: RecognitionConfig :: Hash ] Required . Provides information to the recognizer that specifies how to process the request . @param audio [ :: Google :: Cloud :: Speech :: V1 :: RecognitionAudio :: Hash ] Required . The audio data to be recognized . @param output_config [ :: Google :: Cloud :: Speech :: V1 :: TranscriptOutputConfig :: Hash ] Optional . Specifies an optional destination for the recognition results .
example
Transcribe a long audio file using asynchronous speech recognition require "google/cloud/speech/v1/speech" client = ::Google::Cloud::Speech::V1::Speech::Client.new local_file_path = File.read "resources/brooklyn_bridge.raw" audio = { # Path to local audio file, e.g. /path/audio.wav content: local_file_path } config = { # The language of the supplied audio language_code: "en-US", # Sample rate in Hertz of the audio data sent sample_rate_hertz: 16000, # Encoding of audio data sent. This sample sets this explicitly. # This field is optional for FLAC and WAV audio formats. encoding: :LINEAR16 } response = client.long_running_recognize audio: audio, config: config # Wait until the long running operation is done response.wait_until_done. response.results.each do |result| # First alternative is the most probable result alternative = result.alternatives[0] puts "Transcript: #{alternative.transcript}" end
Transcribe a long audio file using asynchronous speech recognition require " google / cloud / speech / v1 / speech " client = :: Google :: Cloud :: Speech :: V1 :: Speech :: Client . new local_file_path = File . read " resources / brooklyn_bridge . raw " audio = { # Path to local audio file e . g . / path / audio . wav content : local_file_path } config = { # The language of the supplied audio language_code : " en - US " # Sample rate in Hertz of the audio data sent sample_rate_hertz : 16000 # Encoding of audio data sent . This sample sets this explicitly . # This field is optional for FLAC and WAV audio formats . encoding : : LINEAR16 } response = client . long_running_recognize audio : audio config : config # Wait until the long running operation is done response . wait_until_done . response . results . each do |result| # First alternative is the most probable result alternative = result . alternatives [ 0 ] puts " Transcript : # { alternative . transcript } " end
example
Transcribe long audio file from Cloud Storage using asynchronous speech recognition require "google/cloud/speech/v1/speech" client = ::Google::Cloud::Speech::V1::Speech::Client.new storage_uri = "gs://cloud-samples-data/speech/brooklyn_bridge.raw" audio = { # URI for audio file in Cloud Storage, e.g. gs://[BUCKET]/[FILE] uri: storage_uri } config = { # Sample rate in Hertz of the audio data sent sample_rate_hertz: 16000, # The language of the supplied audio language_code: "en-US", # Encoding of audio data sent. This sample sets this explicitly. # This field is optional for FLAC and WAV audio formats. encoding: :LINEAR16 } response = client.long_running_recognize audio: audio, config: config # Wait until the long running operation is done response.wait_until_done. response.results.each do |result| # First alternative is the most probable result alternative = result.alternatives[0] puts "Transcript: #{alternative.transcript}" end
Transcribe long audio file from Cloud Storage using asynchronous speech recognition require " google / cloud / speech / v1 / speech " client = :: Google :: Cloud :: Speech :: V1 :: Speech :: Client . new storage_uri = " gs : // cloud - samples - data / speech / brooklyn_bridge . raw " audio = { # URI for audio file in Cloud Storage e . g . gs : // [ BUCKET ] / [ FILE ] uri : storage_uri } config = { # Sample rate in Hertz of the audio data sent sample_rate_hertz : 16000 # The language of the supplied audio language_code : " en - US " # Encoding of audio data sent . This sample sets this explicitly . # This field is optional for FLAC and WAV audio formats . encoding : : LINEAR16 } response = client . long_running_recognize audio : audio config : config # Wait until the long running operation is done response . wait_until_done . response . results . each do |result| # First alternative is the most probable result alternative = result . alternatives [ 0 ] puts " Transcript : # { alternative . transcript } " end
example
Print start and end time of each word spoken in audio file from Cloud Storage require "google/cloud/speech/v1/speech" client = ::Google::Cloud::Speech::V1::Speech::Client.new storage_uri = "gs://cloud-samples-data/speech/brooklyn_bridge.flac" audio = { # URI for audio file in Cloud Storage, e.g. gs://[BUCKET]/[FILE] uri: storage_uri } config = { # When enabled, the first result returned by the API will include a list # of words and the start and end time offsets (timestamps) for those words. enable_word_time_offsets: true, # The language of the supplied audio language_code: "en-US" } response = client.long_running_recognize audio: audio, config: config # Wait until the long running operation is done response.wait_until_done. # The first result includes start and end time word offsets result = response.results[0] # First alternative is the most probable result alternative = result.alternatives[0] puts "Transcript: #{alternative.transcript}" # Print the start and end time of each word alternative.words.each do |word| puts "Word: #{word.word}" puts "Start time: #{word.start_time.seconds} seconds #{word.start_time.nanos} nanos" puts "End time: #{word.end_time.seconds} seconds #{word.end_time.nanos} nanos" end
Print start and end time of each word spoken in audio file from Cloud Storage require " google / cloud / speech / v1 / speech " client = :: Google :: Cloud :: Speech :: V1 :: Speech :: Client . new storage_uri = " gs : // cloud - samples - data / speech / brooklyn_bridge . flac " audio = { # URI for audio file in Cloud Storage e . g . gs : // [ BUCKET ] / [ FILE ] uri : storage_uri } config = { # When enabled the first result returned by the API will include a list # of words and the start and end time offsets ( timestamps ) for those words . enable_word_time_offsets : true # The language of the supplied audio language_code : " en - US " } response = client . long_running_recognize audio : audio config : config # Wait until the long running operation is done response . wait_until_done . # The first result includes start and end time word offsets result = response . results [ 0 ] # First alternative is the most probable result alternative = result . alternatives [ 0 ] puts " Transcript : # { alternative . transcript } " # Print the start and end time of each word alternative . words . each do |word| puts " Word : # { word . word } " puts " Start time : # { word . start_time . seconds } seconds # { word . start_time . nanos } nanos " puts " End time : # { word . end_time . seconds } seconds # { word . end_time . nanos } nanos " end