Mute the mic while playing the listening "beep" (#2042)

This also extends the stream mute to actually stopping the recording stream while muted.
pull/2045/head
Åke 2019-03-07 20:42:00 +01:00 committed by Steve Penrod
parent e6fa104fc4
commit 526d326ecf
1 changed files with 15 additions and 3 deletions

View File

@ -47,16 +47,23 @@ class MutableStream:
def __init__(self, wrapped_stream, format, muted=False):
assert wrapped_stream is not None
self.wrapped_stream = wrapped_stream
self.muted = muted
if muted:
self.mute()
self.SAMPLE_WIDTH = pyaudio.get_sample_size(format)
self.muted_buffer = b''.join([b'\x00' * self.SAMPLE_WIDTH])
def mute(self):
""" Stop the stream and set the muted flag """
self.muted = True
self.wrapped_stream.stop_stream()
def unmute(self):
""" Start the stream and clear the muted flag """
self.muted = False
self.wrapped_stream.start_stream()
def read(self, size, of_exc=False):
"""
@ -73,6 +80,11 @@ class MutableStream:
frames = collections.deque()
remaining = size
while remaining > 0:
# If muted during read return empty buffer. This ensures no
# reads occur while the stream is stopped
if self.muted:
return self.muted_buffer
to_read = min(self.wrapped_stream.get_read_available(), remaining)
if to_read == 0:
sleep(.01)
@ -82,8 +94,6 @@ class MutableStream:
frames.append(result)
remaining -= to_read
if self.muted:
return self.muted_buffer
input_latency = self.wrapped_stream.get_input_latency()
if input_latency > 0.2:
LOG.warning("High input latency: %f" % input_latency)
@ -503,7 +513,9 @@ class ResponsiveRecognizer(speech_recognition.Recognizer):
file = resolve_resource_file(
self.config.get('sounds').get('start_listening'))
if file:
play_wav(file)
source.mute()
play_wav(file).wait()
source.unmute()
frame_data = self._record_phrase(source, sec_per_buffer)
audio_data = self._create_audio_data(frame_data, source)