Make mic mute counting instead of boolean

====  Tech Notes ====
Mute was previously a simple boolean meaning that if two parts of the
code wanted to mute it it would unmute the mic as soon as the first of
the two pieces of code wanted to unmute.

For example tts output will mute the mic and unmute it when the audio
output is complete. During first start up of a mark-1 the enclosure
mutes the mic and expects it to be muted until the pairing is complete.
This doesn't work. The mic get's unmuted as soon as the first sentence
has been spoken.

This counts the number of times mute is called and will not unmute until
as many unmute calls has been made.
pull/1075/head
Åke Forslund 2017-09-12 16:14:23 +02:00 committed by Steve Penrod
parent 65fa735a3a
commit e9f8d85040
2 changed files with 15 additions and 8 deletions

View File

@ -79,13 +79,11 @@ def handle_wake_up(event):
def handle_mic_mute(event):
if not loop.is_muted():
loop.mute()
loop.mute()
def handle_mic_unmute(event):
if loop.is_muted():
loop.unmute()
loop.unmute()
def handle_paired(event):

View File

@ -52,12 +52,17 @@ class MutableStream(object):
self.muted = muted
self.SAMPLE_WIDTH = pyaudio.get_sample_size(format)
self.muted_buffer = b''.join([b'\x00' * self.SAMPLE_WIDTH])
self.mute_calls = 0
def mute(self):
self.mute_calls = self.mute_calls + 1
self.muted = True
def unmute(self):
self.muted = False
self.mute_calls = self.mute_calls - 1
if self.mute_calls <= 0:
self.mute_calls = 0
self.muted = False
def read(self, size):
frames = collections.deque()
@ -95,7 +100,13 @@ class MutableMicrophone(Microphone):
Microphone.__init__(
self, device_index=device_index, sample_rate=sample_rate,
chunk_size=chunk_size)
self.muted = False
@property
def muted(self):
if self.stream:
return self.stream.muted
else:
return False
def __enter__(self):
assert self.stream is None, \
@ -117,12 +128,10 @@ class MutableMicrophone(Microphone):
self.audio.terminate()
def mute(self):
self.muted = True
if self.stream:
self.stream.mute()
def unmute(self):
self.muted = False
if self.stream:
self.stream.unmute()