2222class Sample (object ):
2323 """Representation of an audio sample to be used with Google Speech API.
2424
25- :type content: bytes or file
26- :param content: (Optional) Bytes object containing audio or file-like
27- object which yields bytes.
25+ :type content: bytes
26+ :param content: (Optional) Bytes object containing audio data.
2827
2928 :type source_uri: str
3029 :param source_uri: (Optional) URI that points to a file that contains
@@ -33,6 +32,9 @@ class Sample(object):
3332 supported, which must be specified in the following
3433 format: ``gs://bucket_name/object_name``.
3534
35+ :type stream: file
36+ :param stream: (Optional) File like object to stream.
37+
3638 :type encoding: str
3739 :param encoding: encoding of audio data sent in all RecognitionAudio
3840 messages, can be one of: :attr:`~.Encoding.LINEAR16`,
@@ -53,17 +55,21 @@ class Sample(object):
5355 default_encoding = Encoding .FLAC
5456 default_sample_rate = 16000
5557
56- def __init__ (self , content = None , source_uri = None ,
58+ def __init__ (self , content = None , source_uri = None , stream = None ,
5759 encoding = None , sample_rate = None , client = None ):
5860 self ._client = client
5961
60- no_source = content is None and source_uri is None
61- both_source = content is not None and source_uri is not None
62+ no_source = content is None and source_uri is None and stream is None
63+ both_source = (content is not None
64+ and source_uri is not None
65+ and stream is not None )
6266 if no_source or both_source :
63- raise ValueError ('Supply one of \' content\' or \' source_uri\' ' )
67+ raise ValueError ('Supply one of '
68+ '\' content\' , \' source_uri\' , \' stream\' ' )
6469
6570 self ._content = content
6671 self ._source_uri = source_uri
72+ self ._stream = stream
6773
6874 if sample_rate is not None and not 8000 <= sample_rate <= 48000 :
6975 raise ValueError ('The value of sample_rate must be between 8000'
@@ -97,13 +103,10 @@ def source_uri(self):
97103 def content (self ):
98104 """Bytes of audio content.
99105
100- :rtype: bytes or file
101- :returns: Byte stream of audio content or file like object .
106+ :rtype: bytes
107+ :returns: Byte stream of audio content.
102108 """
103- try :
104- return self ._content .read ()
105- except AttributeError :
106- return self ._content
109+ return self ._content
107110
108111 @property
109112 def sample_rate (self ):
@@ -121,10 +124,9 @@ def stream(self):
121124 :rtype: file
122125 :returns: File like object to stream.
123126 """
124- stream = self ._content
125- if getattr (stream , 'read' , None ) is None :
127+ if getattr (self ._stream , 'read' , None ) is None :
126128 return None
127- return stream
129+ return self . _stream
128130
129131 @property
130132 def encoding (self ):
0 commit comments