Skip to content

Commit e7fd064

Browse files
authored
Merge pull request googleapis#2514 from daspecster/tighten-speech-async-encoding
Tighten Speech Asynchronous encoding support.
2 parents 71426cb + de0d045 commit e7fd064

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

docs/speech-usage.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ Speech API and initiates a Long Running Operation. Using this operation, you
3939
can periodically poll for recognition results. Use asynchronous requests for
4040
audio data of any duration up to 80 minutes.
4141

42+
.. note::
43+
44+
Only the :attr:`Encoding.LINEAR16` encoding type is supported by
45+
asynchronous recognition.
46+
4247
See: `Speech Asynchronous Recognize`_
4348

4449

@@ -49,7 +54,7 @@ See: `Speech Asynchronous Recognize`_
4954
>>> from google.cloud.speech.encoding import Encoding
5055
>>> client = speech.Client()
5156
>>> sample = client.sample(source_uri='gs://my-bucket/recording.flac',
52-
... encoding=Encoding.FLAC,
57+
... encoding=Encoding.LINEAR16,
5358
... sample_rate=44100)
5459
>>> operation = client.async_recognize(sample, max_alternatives=2)
5560
>>> retry_count = 100
@@ -140,6 +145,5 @@ words to the vocabulary of the recognizer.
140145
transcript: Hello, this is a test
141146
confidence: 0.81
142147
143-
144148
.. _sync_recognize: https://cloud.google.com/speech/reference/rest/v1beta1/speech/syncrecognize
145149
.. _Speech Asynchronous Recognize: https://cloud.google.com/speech/reference/rest/v1beta1/speech/asyncrecognize

speech/google/cloud/speech/client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from google.cloud._helpers import _to_bytes
2020
from google.cloud import client as client_module
2121
from google.cloud.speech.connection import Connection
22+
from google.cloud.speech.encoding import Encoding
2223
from google.cloud.speech.operation import Operation
2324
from google.cloud.speech.sample import Sample
2425

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

9397
data = _build_request_data(sample, language_code, max_alternatives,
9498
profanity_filter, speech_context)

speech/unit_tests/test_client.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,20 @@ def test_sync_recognize_with_empty_results(self):
160160
sample_rate=self.SAMPLE_RATE)
161161
client.sync_recognize(sample)
162162

163+
def test_async_supported_encodings(self):
164+
from google.cloud.speech.encoding import Encoding
165+
from google.cloud.speech.sample import Sample
166+
167+
credentials = _Credentials()
168+
client = self._makeOne(credentials=credentials)
169+
client.connection = _Connection({})
170+
171+
sample = Sample(source_uri=self.AUDIO_SOURCE_URI,
172+
encoding=Encoding.FLAC,
173+
sample_rate=self.SAMPLE_RATE)
174+
with self.assertRaises(ValueError):
175+
client.async_recognize(sample)
176+
163177
def test_async_recognize(self):
164178
from unit_tests._fixtures import ASYNC_RECOGNIZE_RESPONSE
165179
from google.cloud.speech.encoding import Encoding
@@ -172,7 +186,7 @@ def test_async_recognize(self):
172186
client.connection = _Connection(RETURNED)
173187

174188
sample = Sample(source_uri=self.AUDIO_SOURCE_URI,
175-
encoding=Encoding.FLAC,
189+
encoding=Encoding.LINEAR16,
176190
sample_rate=self.SAMPLE_RATE)
177191
operation = client.async_recognize(sample)
178192
self.assertIsInstance(operation, Operation)

0 commit comments

Comments
 (0)