3
3
"""Library for performing speech recognition with support for Google Speech Recognition, Wit.ai, IBM Speech to Text, and AT&T Speech to Text."""
4
4
5
5
__author__ = "Anthony Zhang (Uberi)"
6
- __version__ = "3.3.1 "
6
+ __version__ = "3.3.2 "
7
7
__license__ = "BSD"
8
8
9
9
import io , os , subprocess , wave , base64
@@ -107,10 +107,12 @@ def __enter__(self):
107
107
assert self .stream is None , "This audio source is already inside a context manager"
108
108
self .audio = self .pyaudio_module .PyAudio ()
109
109
try :
110
- self .stream = self .audio .open (
111
- input_device_index = self .device_index , channels = 1 ,
112
- format = self .format , rate = self .SAMPLE_RATE , frames_per_buffer = self .CHUNK ,
113
- input = True , # stream is an input stream
110
+ self .stream = Microphone .MicrophoneStream (
111
+ self .audio .open (
112
+ input_device_index = self .device_index , channels = 1 ,
113
+ format = self .format , rate = self .SAMPLE_RATE , frames_per_buffer = self .CHUNK ,
114
+ input = True , # stream is an input stream
115
+ )
114
116
)
115
117
except :
116
118
self .audio .terminate ()
@@ -119,14 +121,25 @@ def __enter__(self):
119
121
120
122
def __exit__ (self , exc_type , exc_value , traceback ):
121
123
try :
122
- if not self .stream .is_stopped ():
123
- self .stream .stop_stream ()
124
+ self .stream .close ()
124
125
finally :
126
+ self .stream = None
127
+ self .audio .terminate ()
128
+
129
+ class MicrophoneStream (object ):
130
+ def __init__ (self , pyaudio_stream ):
131
+ self .pyaudio_stream = pyaudio_stream
132
+
133
+ def read (self , size ):
134
+ return self .pyaudio_stream .read (size , exception_on_overflow = False )
135
+
136
+ def close (self ):
125
137
try :
126
- self .stream .close ()
138
+ # sometimes, if the stream isn't stopped, closing the stream throws an exception
139
+ if not self .pyaudio_stream .is_stopped ():
140
+ self .pyaudio_stream .stop_stream ()
127
141
finally :
128
- self .stream = None
129
- self .audio .terminate ()
142
+ self .pyaudio_stream .close ()
130
143
131
144
class WavFile (AudioSource ):
132
145
"""
@@ -374,7 +387,7 @@ def listen(self, source, timeout = None):
374
387
if timeout and elapsed_time > timeout : # handle timeout if specified
375
388
raise WaitTimeoutError ("listening timed out" )
376
389
377
- buffer = source .stream .read (source .CHUNK , exception_on_overflow = False )
390
+ buffer = source .stream .read (source .CHUNK )
378
391
if len (buffer ) == 0 : break # reached end of the stream
379
392
frames .append (buffer )
380
393
if len (frames ) > non_speaking_buffer_count : # ensure we only keep the needed amount of non-speaking buffers
@@ -395,7 +408,7 @@ def listen(self, source, timeout = None):
395
408
while True :
396
409
elapsed_time += seconds_per_buffer
397
410
398
- buffer = source .stream .read (source .CHUNK , exception_on_overflow = False )
411
+ buffer = source .stream .read (source .CHUNK )
399
412
if len (buffer ) == 0 : break # reached end of the stream
400
413
frames .append (buffer )
401
414
phrase_count += 1
0 commit comments