Class: Google::Cloud::Speech::Audio

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/speech/audio.rb

Overview

Audio

Represents a source of audio data, with related metadata such as the audio encoding, sample rate, and language.

See Project#audio.

Examples:

require "google/cloud"

gcloud = Google::Cloud.new
speech = gcloud.speech

audio = speech.audio "path/to/audio.raw",
                     encoding: :raw, sample_rate: 16000
results = audio.recognize

result = results.first
result.transcript #=> "how old is the Brooklyn Bridge"
result.confidence #=> 88.15

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#encodingObject

Returns the value of attribute encoding



55
56
57
# File 'lib/google/cloud/speech/audio.rb', line 55

def encoding
  @encoding
end

#languageObject

Returns the value of attribute language



57
58
59
# File 'lib/google/cloud/speech/audio.rb', line 57

def language
  @language
end

#sample_rateObject

Returns the value of attribute sample_rate



56
57
58
# File 'lib/google/cloud/speech/audio.rb', line 56

def sample_rate
  @sample_rate
end

Instance Method Details

#recognize(max_alternatives: nil, profanity_filter: nil, phrases: nil) ⇒ Array<Result>

Performs synchronous speech recognition. Sends audio data to the Speech API, which performs recognition on that data, and returns results only after all audio has been processed. Limited to audio data of 1 minute or less in duration.

The Speech API will take roughly the same amount of time to process audio data sent synchronously as the duration of the supplied audio data. That is, if you send audio data of 30 seconds in length, expect the synchronous request to take approximately 30 seconds to return results.

Examples:

require "google/cloud"

gcloud = Google::Cloud.new
speech = gcloud.speech

audio = speech.audio "path/to/audio.raw",
                     encoding: :raw, sample_rate: 16000
results = audio.recognize

result = results.first
result.transcript #=> "how old is the Brooklyn Bridge"
result.confidence #=> 88.15

Parameters:

  • max_alternatives (String)

    The Maximum number of recognition hypotheses to be returned. Default is 1. The service may return fewer. Valid values are 0-30. Defaults to 1. Optional.

  • profanity_filter (Boolean)

    When true, the service will attempt to filter out profanities, replacing all but the initial character in each filtered word with asterisks, e.g. "f***". Default is false.

  • phrases (Array<String>)

    A list of strings containing words and phrases "hints" so that the speech recognition is more likely to recognize them. See usage limits. Optional.

Returns:

  • (Array<Result>)

    The transcribed text of audio recognized.

See Also:



124
125
126
127
128
129
130
131
132
# File 'lib/google/cloud/speech/audio.rb', line 124

def recognize max_alternatives: nil, profanity_filter: nil, phrases: nil
  ensure_speech!

  speech.recognize self, encoding: encoding, sample_rate: sample_rate,
                         language: language,
                         max_alternatives: max_alternatives,
                         profanity_filter: profanity_filter,
                         phrases: phrases
end

#recognize_job(max_alternatives: nil, profanity_filter: nil, phrases: nil) ⇒ Job

Performs asynchronous speech recognition. Requests are processed asynchronously, meaning a Job is returned once the audio data has been sent, and can be refreshed to retrieve recognition results once the audio data has been processed.

Examples:

require "google/cloud"

gcloud = Google::Cloud.new
speech = gcloud.speech

audio = speech.audio "path/to/audio.raw",
                     encoding: :raw, sample_rate: 16000
job = audio.recognize_job

job.done? #=> false
job.reload!
job.done? #=> true
results = job.results

Parameters:

  • max_alternatives (String)

    The Maximum number of recognition hypotheses to be returned. Default is 1. The service may return fewer. Valid values are 0-30. Defaults to 1. Optional.

  • profanity_filter (Boolean)

    When true, the service will attempt to filter out profanities, replacing all but the initial character in each filtered word with asterisks, e.g. "f***". Default is false.

  • phrases (Array<String>)

    A list of strings containing words and phrases "hints" so that the speech recognition is more likely to recognize them. See usage limits. Optional.

Returns:

  • (Job)

    A resource represents the long-running, asynchronous processing of a speech-recognition operation.

See Also:



173
174
175
176
177
178
179
180
181
182
183
# File 'lib/google/cloud/speech/audio.rb', line 173

def recognize_job max_alternatives: nil, profanity_filter: nil,
                  phrases: nil
  ensure_speech!

  speech.recognize_job self, encoding: encoding,
                             sample_rate: sample_rate,
                             language: language,
                             max_alternatives: max_alternatives,
                             profanity_filter: profanity_filter,
                             phrases: phrases
end