Skip to content

Commit

Permalink
Merge pull request googleapis#2514 from daspecster/tighten-speech-asy…
Browse files Browse the repository at this point in the history
…nc-encoding

Tighten Speech Asynchronous encoding support.
  • Loading branch information
daspecster authored Oct 7, 2016
2 parents 71426cb + de0d045 commit e7fd064
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
8 changes: 6 additions & 2 deletions docs/speech-usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ Speech API and initiates a Long Running Operation. Using this operation, you
can periodically poll for recognition results. Use asynchronous requests for
audio data of any duration up to 80 minutes.

.. note::

Only the :attr:`Encoding.LINEAR16` encoding type is supported by
asynchronous recognition.

See: `Speech Asynchronous Recognize`_


Expand All @@ -49,7 +54,7 @@ See: `Speech Asynchronous Recognize`_
>>> from google.cloud.speech.encoding import Encoding
>>> client = speech.Client()
>>> sample = client.sample(source_uri='gs://my-bucket/recording.flac',
... encoding=Encoding.FLAC,
... encoding=Encoding.LINEAR16,
... sample_rate=44100)
>>> operation = client.async_recognize(sample, max_alternatives=2)
>>> retry_count = 100
Expand Down Expand Up @@ -140,6 +145,5 @@ words to the vocabulary of the recognizer.
transcript: Hello, this is a test
confidence: 0.81
.. _sync_recognize: https://cloud.google.com/speech/reference/rest/v1beta1/speech/syncrecognize
.. _Speech Asynchronous Recognize: https://cloud.google.com/speech/reference/rest/v1beta1/speech/asyncrecognize
4 changes: 4 additions & 0 deletions speech/google/cloud/speech/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from google.cloud._helpers import _to_bytes
from google.cloud import client as client_module
from google.cloud.speech.connection import Connection
from google.cloud.speech.encoding import Encoding
from google.cloud.speech.operation import Operation
from google.cloud.speech.sample import Sample

Expand Down Expand Up @@ -89,6 +90,9 @@ def async_recognize(self, sample, language_code=None,
:rtype: `~google.cloud.speech.operation.Operation`
:returns: ``Operation`` for asynchronous request to Google Speech API.
"""
if sample.encoding is not Encoding.LINEAR16:
raise ValueError('Only LINEAR16 encoding is supported by '
'asynchronous speech requests.')

data = _build_request_data(sample, language_code, max_alternatives,
profanity_filter, speech_context)
Expand Down
16 changes: 15 additions & 1 deletion speech/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,20 @@ def test_sync_recognize_with_empty_results(self):
sample_rate=self.SAMPLE_RATE)
client.sync_recognize(sample)

def test_async_supported_encodings(self):
from google.cloud.speech.encoding import Encoding
from google.cloud.speech.sample import Sample

credentials = _Credentials()
client = self._makeOne(credentials=credentials)
client.connection = _Connection({})

sample = Sample(source_uri=self.AUDIO_SOURCE_URI,
encoding=Encoding.FLAC,
sample_rate=self.SAMPLE_RATE)
with self.assertRaises(ValueError):
client.async_recognize(sample)

def test_async_recognize(self):
from unit_tests._fixtures import ASYNC_RECOGNIZE_RESPONSE
from google.cloud.speech.encoding import Encoding
Expand All @@ -172,7 +186,7 @@ def test_async_recognize(self):
client.connection = _Connection(RETURNED)

sample = Sample(source_uri=self.AUDIO_SOURCE_URI,
encoding=Encoding.FLAC,
encoding=Encoding.LINEAR16,
sample_rate=self.SAMPLE_RATE)
operation = client.async_recognize(sample)
self.assertIsInstance(operation, Operation)
Expand Down

0 comments on commit e7fd064

Please sign in to comment.