feat: mute the TV while the box listens, if the brain turns that on
check / check (push) Waiting to run

whisper hears people better without the show behind them. The brain
sets a mute code (and an unmute code, if not the same) with
mute_while_listening on its ir capability, or turns it off; ir-server
keeps the setting in its state directory.

Mute is often a toggle, and the camera can't reliably tell whether the
sound is off, so the box senses nothing: the setting is the brain's
to turn on or off.

The button service holds a connection to ir-server's conversation
socket for each conversation: ir-server mutes when it opens and
unmutes, with the same codes, when it closes. The socket's group is
the button's alone, and the button still can't send any code.

Generated-by: Claude Opus 5.5
This commit is contained in:
dckc committed 2026-12-03 16:49:58 -06:00
1 parent 5191ee9931
commit f50c871521
10 files changed
+269 -22

No files matched your search

+14 -8
View File
@@ -13,7 +13,8 @@ While the brain looks through the camera (../look.js), the box taps and flashes
One event loop, no threads. Only _script_io() imports authority (the DisciplinedPython
recipe); main() narrows it into the parts, each of which gets only what it needs:
button and light (gpio.py, button.py, light.py), mics (hearing.py), speaker and voice
(voice.py), the volume (mixer.py), hear and brain (brain.py), status (status.py), and the look signal.
(voice.py), the volume (mixer.py), muting the TV while it listens (muting.py), hear and
brain (brain.py), status (status.py), and the look signal.
"""
import asyncio
import logging
@@ -28,6 +29,7 @@ from hearing import ARECORD_ARGS, Mics
from light import Light
from look_signal import LookSignal
from mixer import Mixer
from muting import Muting
from status import Status, ignore, poster
from voice import Speaker, Voice
@@ -46,16 +48,17 @@ def activated_socket(environ, getpid, socket):
async def main(argv, environ, getpid, cwd, request_lines, socket, create_subprocess_exec, open_connection,
start_unix_server, sleep, monotonic, now):
"""box.py APLAY AMIXER ARECORD ESPEAK HEAR_CLIENT SPEAK_CLIENT SPEECH_URL ASK_CLIENT ASK_URL EARCONS STATE_DIR [STATUS_URL]
open_unix_connection, start_unix_server, sleep, monotonic, now):
"""box.py APLAY AMIXER ARECORD ESPEAK HEAR_CLIENT SPEAK_CLIENT SPEECH_URL ASK_CLIENT ASK_URL MUTE_SOCKET EARCONS STATE_DIR [STATUS_URL]
HEAR_CLIENT SPEECH_URL < clip prints its transcript; SPEAK_CLIENT SPEECH_URL < text prints
a WAV of it; ASK_CLIENT ASK_URL CONVERSATION < text prints the brain's answer as JSON lines.
MUTE_SOCKET is the ir service's conversation socket, held open for each conversation (muting.py).
EARCONS is a directory of WAVs (voice.EARCONS); STATE_DIR keeps the latest clip.
STATUS_URL, if any, is a status page, told of each conversation and look, with a heartbeat.
"""
(aplay, amixer, arecord, espeak, hear_client, speak_client, speech_url, ask_client, ask_url,
earcons, state_dir, *status_url) = argv[1:]
mute_socket, earcons, state_dir, *status_url) = argv[1:]
command = partial(Command, create_subprocess_exec)
loop = asyncio.get_running_loop()
@@ -69,6 +72,7 @@ async def main(argv, environ, getpid, cwd, request_lines, socket, create_subproc
hear = Hear(command(hear_client, speech_url))
voice = Voice(command(speak_client, speech_url), command(espeak, "--stdout"), speaker, button)
brain = Brain(command(ask_client, ask_url), button)
muting = Muting(partial(open_unix_connection, mute_socket))
background = [asyncio.create_task(light.run())]
note = ignore
@@ -89,9 +93,10 @@ async def main(argv, environ, getpid, cwd, request_lines, socket, create_subproc
convo = now().strftime("%Y%m%d-%H%M%S")
logging.info(f"{convo}: a conversation starts")
note("conversation")
await converse(convo, partial(mics.listen, button=button), hear.transcribe, brain.ask,
voice.speak, speaker.play, mixer.louder, light.mics, light.thinking, brain.end,
logging.info, note)
async with muting.during():
await converse(convo, partial(mics.listen, button=button), hear.transcribe, brain.ask,
voice.speak, speaker.play, mixer.louder, light.mics, light.thinking, brain.end,
logging.info, note)
button.pressed() # presses during goodbye don't start another
for task in background:
if task.done():
@@ -114,7 +119,8 @@ if __name__ == "__main__":
argv=list(sys.argv), environ=dict(os.environ), getpid=os.getpid, cwd=Path.cwd(),
request_lines=gpiod.request_lines, socket=socket.socket,
create_subprocess_exec=asyncio.create_subprocess_exec,
open_connection=asyncio.open_connection, start_unix_server=asyncio.start_unix_server,
open_connection=asyncio.open_connection, open_unix_connection=asyncio.open_unix_connection,
start_unix_server=asyncio.start_unix_server,
sleep=asyncio.sleep, monotonic=time.monotonic, now=datetime.now,
))
+56
View File
@@ -0,0 +1,56 @@
"""Muting the TV while the box listens: the button service's side.
For the whole of a conversation, the button service holds a connection to the ir service's
conversation socket (../ir-server.js); if the brain has turned muting on, the ir service mutes
the TV when the connection opens and unmutes it when it closes. This side can't send any code,
nor tell whether muting is on: the connection says only that a conversation is going on.
"""
import logging
from contextlib import asynccontextmanager
class Muting:
"""during(): for the length of a conversation, the ir service may mute the TV.
open_unix_connection is asyncio's, bound to the ir service's conversation socket; log, logging.info.
>>> import asyncio
>>> class Writer:
... def close(self): print("hang up")
... async def wait_closed(self): pass
>>> async def connect(): return print("connect") or (None, Writer())
>>> async def talk(muting):
... async with muting.during():
... print("conversation")
>>> asyncio.run(talk(Muting(connect)))
connect
conversation
hang up
Without the ir service, the conversation goes on, unmuted:
>>> async def refused(): raise ConnectionRefusedError("no ir service")
>>> asyncio.run(talk(Muting(refused, log=print)))
mute: can't reach the ir service: no ir service
conversation
"""
def __init__(self, open_unix_connection, log=logging.info):
self.__connect, self.__log = open_unix_connection, log
@asynccontextmanager
async def during(self):
writer = None
try:
_, writer = await self.__connect()
except OSError as err:
self.__log(f"mute: can't reach the ir service: {err}")
try:
yield
finally:
if writer:
try:
writer.close()
await writer.wait_closed()
except OSError as err:
self.__log(f"mute: hanging up: {err}")