Skip to content

Commit adf85cb

Browse files
committed
Fix microphone streaming and bump version
1 parent b9a1ab8 commit adf85cb

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

setup.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ def run(self):
2222
if os.path.basename(output_path) in FILES_TO_MARK_EXECUTABLE:
2323
log.info("setting executable permissions on {}".format(output_path))
2424
stat_info = os.stat(output_path)
25-
os.chmod(output_path, stat_info.st_mode | stat.S_IEXEC)
25+
os.chmod(
26+
output_path,
27+
stat_info.st_mode |
28+
stat.S_IRUSR | stat.S_IXUSR | # owner can read/execute
29+
stat.S_IRGRP | stat.S_IXGRP | # group can read/execute
30+
stat.S_IROTH | stat.S_IXOTH # everyone else can read/execute
31+
)
2632

2733
setup(
2834
name = "SpeechRecognition",

speech_recognition/__init__.py

+25-12
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""Library for performing speech recognition with support for Google Speech Recognition, Wit.ai, IBM Speech to Text, and AT&T Speech to Text."""
44

55
__author__ = "Anthony Zhang (Uberi)"
6-
__version__ = "3.3.1"
6+
__version__ = "3.3.2"
77
__license__ = "BSD"
88

99
import io, os, subprocess, wave, base64
@@ -107,10 +107,12 @@ def __enter__(self):
107107
assert self.stream is None, "This audio source is already inside a context manager"
108108
self.audio = self.pyaudio_module.PyAudio()
109109
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+
)
114116
)
115117
except:
116118
self.audio.terminate()
@@ -119,14 +121,25 @@ def __enter__(self):
119121

120122
def __exit__(self, exc_type, exc_value, traceback):
121123
try:
122-
if not self.stream.is_stopped():
123-
self.stream.stop_stream()
124+
self.stream.close()
124125
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):
125137
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()
127141
finally:
128-
self.stream = None
129-
self.audio.terminate()
142+
self.pyaudio_stream.close()
130143

131144
class WavFile(AudioSource):
132145
"""
@@ -374,7 +387,7 @@ def listen(self, source, timeout = None):
374387
if timeout and elapsed_time > timeout: # handle timeout if specified
375388
raise WaitTimeoutError("listening timed out")
376389

377-
buffer = source.stream.read(source.CHUNK, exception_on_overflow = False)
390+
buffer = source.stream.read(source.CHUNK)
378391
if len(buffer) == 0: break # reached end of the stream
379392
frames.append(buffer)
380393
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):
395408
while True:
396409
elapsed_time += seconds_per_buffer
397410

398-
buffer = source.stream.read(source.CHUNK, exception_on_overflow = False)
411+
buffer = source.stream.read(source.CHUNK)
399412
if len(buffer) == 0: break # reached end of the stream
400413
frames.append(buffer)
401414
phrase_count += 1

0 commit comments

Comments
 (0)