Class: Google::Cloud::Speech::Stream

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/google/cloud/speech/stream.rb

Overview

# Stream

A resource that represents the streaming requests and responses.

Examples:

require "google/cloud/speech"

speech = Google::Cloud::Speech.new

stream = audio.stream encoding: :raw, sample_rate: 16000

# register callback for when a result is returned
stream.on_result do |results|
  result = results.first
  puts result.transcript # "how old is the Brooklyn Bridge"
  puts result.confidence # 0.9826789498329163
end

# Stream 5 seconds of audio from the microhone
# Actual implementation of microphone input varies by platform
5.times.do
  stream.send MicrophoneInput.read(32000)
end

stream.stop

Defined Under Namespace

Classes: EnumeratorQueue

Instance Method Summary collapse

Constructor Details

#initialize(service, streaming_recognize_request) ⇒ Stream

This must always be private, since it may change as the implementation changes over time.



57
58
59
60
61
62
63
# File 'lib/google/cloud/speech/stream.rb', line 57

def initialize service, streaming_recognize_request
  @service = service
  @streaming_recognize_request = streaming_recognize_request
  @results = []
  @callbacks = Hash.new { |h, k| h[k] = [] }
  super() # to init MonitorMixin
end

Instance Method Details

#add_result!(result_index, result_grpc) ⇒ Object



254
255
256
257
258
259
260
# File 'lib/google/cloud/speech/stream.rb', line 254

def add_result!result_index, result_grpc
  synchronize do
    @results[result_index] = Result.from_grpc result_grpc
  end
  # callback for final result received
  result!
end

#complete!Object

depending on how the Stream is configured, this can be on the reception of :END_OF_AUDIO or :END_OF_UTTERANCE.



387
388
389
390
391
# File 'lib/google/cloud/speech/stream.rb', line 387

def complete!
  synchronize do
    @callbacks[:complete].each(&:call)
  end
end

#error!(err) ⇒ Object



474
475
476
477
478
# File 'lib/google/cloud/speech/stream.rb', line 474

def error! err
  synchronize do
    @callbacks[:error].each { |c| c.call err }
  end
end

#interim!(interim_results) ⇒ Object

non-final/incomplete result



213
214
215
216
217
# File 'lib/google/cloud/speech/stream.rb', line 213

def interim! interim_results
  synchronize do
    @callbacks[:interim].each { |c| c.call results, interim_results }
  end
end

#on_complete {|callback| ... } ⇒ Object

Register to be notified when the end of the audio stream has been reached.

Examples:

require "google/cloud/speech"

speech = Google::Cloud::Speech.new

stream = audio.stream encoding: :raw, sample_rate: 16000

# register callback for when audio has ended.
stream.on_complete do
  puts "Audio has ended."
end

# Stream 5 seconds of audio from the microhone
# Actual implementation of microphone input varies by platform
5.times.do
  stream.send MicrophoneInput.read(32000)
end

stream.stop

Yields:

  • (callback)

    The block to be called when the end of the audio stream has been reached.



378
379
380
381
382
# File 'lib/google/cloud/speech/stream.rb', line 378

def on_complete &block
  synchronize do
    @callbacks[:complete] << block
  end
end

#on_error {|callback| ... } ⇒ Object

Register to be notified of an error recieved during the stream.

Examples:

require "google/cloud/speech"

speech = Google::Cloud::Speech.new

stream = audio.stream encoding: :raw, sample_rate: 16000

# register callback for when an error is returned
stream.on_error do |error|
  puts "The following error occurred while streaming: #{error}"
  stream.stop
end

# Stream 5 seconds of audio from the microhone
# Actual implementation of microphone input varies by platform
5.times.do
  stream.send MicrophoneInput.read(32000)
end

stream.stop

Yields:

  • (callback)

    The block for accessing final results.

Yield Parameters:

  • error (Exception)

    The error raised.



467
468
469
470
471
# File 'lib/google/cloud/speech/stream.rb', line 467

def on_error &block
  synchronize do
    @callbacks[:error] << block
  end
end

#on_interim {|callback| ... } ⇒ Object

Register to be notified on the reception of an interim result.

Examples:

require "google/cloud/speech"

speech = Google::Cloud::Speech.new

stream = audio.stream encoding: :raw, sample_rate: 16000

# register callback for when an interim result is returned
stream.on_interim do |final_results, interim_results|
  interim_result = interim_results.first
  puts interim_result.transcript # "how old is the Brooklyn Bridge"
  puts interim_result.confidence # 0.9826789498329163
  puts interim_result.stability # 0.8999
end

# Stream 5 seconds of audio from the microhone
# Actual implementation of microphone input varies by platform
5.times.do
  stream.send MicrophoneInput.read(32000)
end

stream.stop

Yields:

  • (callback)

    The block for accessing final and interim results.

Yield Parameters:

  • final_results (Array<Result>)

    The final results.

  • interim_results (Array<Result>)

    The interim results.



205
206
207
208
209
# File 'lib/google/cloud/speech/stream.rb', line 205

def on_interim &block
  synchronize do
    @callbacks[:interim] << block
  end
end

#on_result {|callback| ... } ⇒ Object

Register to be notified on the reception of a final result.

Examples:

require "google/cloud/speech"

speech = Google::Cloud::Speech.new

stream = audio.stream encoding: :raw, sample_rate: 16000

# register callback for when an interim result is returned
stream.on_result do |results|
  result = results.first
  puts result.transcript # "how old is the Brooklyn Bridge"
  puts result.confidence # 0.9826789498329163
end

# Stream 5 seconds of audio from the microhone
# Actual implementation of microphone input varies by platform
5.times.do
  stream.send MicrophoneInput.read(32000)
end

stream.stop

Yields:

  • (callback)

    The block for accessing final results.

Yield Parameters:

  • results (Array<Result>)

    The final results.



247
248
249
250
251
# File 'lib/google/cloud/speech/stream.rb', line 247

def on_result &block
  synchronize do
    @callbacks[:result] << block
  end
end

#on_speech_end {|callback| ... } ⇒ Object

Register to be notified when speech has ceased to be detected in the audio stream.

Examples:

require "google/cloud/speech"

speech = Google::Cloud::Speech.new

stream = audio.stream encoding: :raw, sample_rate: 16000

# register callback for when speech has ended.
stream.on_speech_end do
  puts "Speech has ended."
end

# Stream 5 seconds of audio from the microhone
# Actual implementation of microphone input varies by platform
5.times.do
  stream.send MicrophoneInput.read(32000)
end

stream.stop

Yields:

  • (callback)

    The block to be called when speech has ceased to be detected in the audio stream.



337
338
339
340
341
# File 'lib/google/cloud/speech/stream.rb', line 337

def on_speech_end &block
  synchronize do
    @callbacks[:speech_end] << block
  end
end

#on_speech_start {|callback| ... } ⇒ Object

Register to be notified when speech has been detected in the audio stream.

Examples:

require "google/cloud/speech"

speech = Google::Cloud::Speech.new

stream = audio.stream encoding: :raw, sample_rate: 16000

# register callback for when speech has started.
stream.on_speech_start do
  puts "Speech has started."
end

# Stream 5 seconds of audio from the microhone
# Actual implementation of microphone input varies by platform
5.times.do
  stream.send MicrophoneInput.read(32000)
end

stream.stop

Yields:

  • (callback)

    The block to be called when speech has been detected in the audio stream.



296
297
298
299
300
# File 'lib/google/cloud/speech/stream.rb', line 296

def on_speech_start &block
  synchronize do
    @callbacks[:speech_start] << block
  end
end

#on_utterance {|callback| ... } ⇒ Object

Register to be notified when the server has detected the end of the user’s speech utterance and expects no additional speech. Therefore, the server will not process additional audio. The client should stop sending additional audio data. This event only occurs when ‘utterance` is `true`.

Examples:

require "google/cloud/speech"

speech = Google::Cloud::Speech.new

stream = audio.stream encoding: :raw,
                      sample_rate: 16000,
                      utterance: true

# register callback for when utterance has occurred.
stream.on_utterance do
  puts "Utterance has occurred."
  stream.stop
end

# Stream 5 seconds of audio from the microhone
# Actual implementation of microphone input varies by platform
5.times.do
  stream.send MicrophoneInput.read(32000)
end

stream.stop unless stream.stopped?

Yields:

  • (callback)

    The block to be called when the end of the audio stream has been reached.



426
427
428
429
430
# File 'lib/google/cloud/speech/stream.rb', line 426

def on_utterance &block
  synchronize do
    @callbacks[:utterance] << block
  end
end

#result!Object



263
264
265
266
267
# File 'lib/google/cloud/speech/stream.rb', line 263

def result!
  synchronize do
    @callbacks[:result].each { |c| c.call results }
  end
end

#resultsArray<Result>

The speech recognition results for the audio.

Examples:

require "google/cloud/speech"

speech = Google::Cloud::Speech.new

stream = audio.stream encoding: :raw, sample_rate: 16000

# Stream 5 seconds of audio from the microhone
# Actual implementation of microphone input varies by platform
5.times.do
  stream.send MicrophoneInput.read(32000)
end

stream.stop

results = stream.results
result = results.first
puts result.transcript # "how old is the Brooklyn Bridge"
puts result.confidence # 0.9826789498329163

Returns:

  • (Array<Result>)

    The transcribed text of audio recognized.



169
170
171
172
173
# File 'lib/google/cloud/speech/stream.rb', line 169

def results
  synchronize do
    @results
  end
end

#send(bytes) ⇒ Object

Sends audio content to the server.

Examples:

require "google/cloud/speech"

speech = Google::Cloud::Speech.new

stream = speech.stream encoding: :raw, sample_rate: 16000

# register callback for when a result is returned
stream.on_result do |results|
  result = results.first
  puts result.transcript # "how old is the Brooklyn Bridge"
  puts result.confidence # 0.9826789498329163
end

# Stream 5 seconds of audio from the microhone
# Actual implementation of microphone input varies by platform
5.times.do
  stream.send MicrophoneInput.read(32000)
end

stream.stop

Parameters:

  • bytes (String)

    A string of binary audio data to be recognized. The data should be encoded as ‘ASCII-8BIT`.



113
114
115
116
117
118
119
120
121
# File 'lib/google/cloud/speech/stream.rb', line 113

def send bytes
  start # lazily call start if the stream wasn't started yet
  # TODO: do not send if stopped?
  synchronize do
    req = V1beta1::StreamingRecognizeRequest.new(
      audio_content: bytes.encode("ASCII-8BIT"))
    @request_queue.push req
  end
end

#speech_end!Object

recieved.



345
346
347
348
349
# File 'lib/google/cloud/speech/stream.rb', line 345

def speech_end!
  synchronize do
    @callbacks[:speech_end].each(&:call)
  end
end

#speech_start!Object

recieved.



304
305
306
307
308
# File 'lib/google/cloud/speech/stream.rb', line 304

def speech_start!
  synchronize do
    @callbacks[:speech_start].each(&:call)
  end
end

#startObject

Starts the stream. The stream will be started in the first #send call.



67
68
69
70
71
72
73
# File 'lib/google/cloud/speech/stream.rb', line 67

def start
  return if @request_queue
  @request_queue = EnumeratorQueue.new(self)
  @request_queue.push @streaming_recognize_request

  Thread.new { background_run }
end

#started?boolean

Checks if the stream has been started.

Returns:

  • (boolean)

    ‘true` when started, `false` otherwise.



79
80
81
82
83
# File 'lib/google/cloud/speech/stream.rb', line 79

def started?
  synchronize do
    !(!@request_queue)
  end
end

#stopObject

Stops the stream. Signals to the server that no more data will be sent.



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

def stop
  synchronize do
    return if @request_queue.nil?
    @request_queue.push self
    @stopped = true
  end
end

#stopped?boolean

Checks if the stream has been stopped.

Returns:

  • (boolean)

    ‘true` when stopped, `false` otherwise.



138
139
140
141
142
# File 'lib/google/cloud/speech/stream.rb', line 138

def stopped?
  synchronize do
    @stopped
  end
end

#utterance!Object

recieved.



434
435
436
437
438
# File 'lib/google/cloud/speech/stream.rb', line 434

def utterance!
  synchronize do
    @callbacks[:utterance].each(&:call)
  end
end