Add lock to mute unmute of mic stream
This should alleviate a potential (or possibly an existing) problem if
mute is triggered while reading of the mic occurs.
@@ -56,14 +56,17 @@ class MutableStream:
self.SAMPLE_WIDTH = pyaudio.get_sample_size(format)
self.muted_buffer = b''.join([b'\x00' * self.SAMPLE_WIDTH])
+ self.read_lock = Lock()
def mute(self):
""" Stop the stream and set the muted flag """
+ with self.read_lock:
self.muted = True
self.wrapped_stream.stop_stream()
def unmute(self):
""" Start the stream and clear the muted flag """
+ with self.read_lock:
self.muted = False
self.wrapped_stream.start_stream()
@@ -81,13 +84,15 @@ class MutableStream:
"""
frames = deque()
remaining = size
+ with self.read_lock:
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)
+ to_read = min(self.wrapped_stream.get_read_available(),
+ remaining)
if to_read == 0:
sleep(.01)
continue
