feat: mute the TV while the box listens, if the brain turns that on
check / check (push) Waiting to run
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:
1 parent
5191ee9931
commit
f50c871521
10 files changed
+269
-22
No files matched your search
@@ -64,6 +64,75 @@ export const parseHeard = (out) => {
|
||||
return { codes, unparsed: lines.filter((ln) => ln.startsWith('unparsed:')) };
|
||||
};
|
||||
|
||||
/**
|
||||
* A code as send takes it, normalized; or a TypeError.
|
||||
* @param {unknown} code
|
||||
*/
|
||||
export const checkCode = (code) => {
|
||||
const norm = typeof code === 'string' ? normalize(code) : '';
|
||||
if (!CODE.test(norm) && !isRaw(norm)) {
|
||||
throw new TypeError(`code: hex bytes, like "20 DF 10 EF" or "samsung E0 E0 40 BF"; or "raw" and an odd number of microseconds, up to ${MAX_RAW}, each up to 65535`);
|
||||
}
|
||||
return norm;
|
||||
};
|
||||
|
||||
/** @typedef {{ mute: string, unmute: string }} MuteCodes */
|
||||
|
||||
/**
|
||||
* Whether to mute the TV while the box listens: a setting, and nothing sensed. Mute is often a toggle,
|
||||
* and a camera can't reliably tell whether the TV is muted, so whoever holds the ir capability
|
||||
* turns this on or off, and the box doesn't guess.
|
||||
*/
|
||||
export class Muting {
|
||||
/** @type {MuteCodes | undefined} */
|
||||
#codes;
|
||||
#save;
|
||||
|
||||
/**
|
||||
* @param {unknown} saved what save was last given, if anything
|
||||
* @param {(codes: MuteCodes | undefined) => Promise<void>} save keeps the setting across restarts
|
||||
*/
|
||||
constructor(saved, save) {
|
||||
const s = /** @type {any} */ (saved);
|
||||
this.#codes = s?.mute ? { mute: checkCode(s.mute), unmute: checkCode(s.unmute ?? s.mute) } : undefined;
|
||||
this.#save = save;
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn it on, with the code that mutes (and the one that unmutes, if not the same); or off, with no code.
|
||||
* @param {string} [code] @param {string} [unmuteCode]
|
||||
*/
|
||||
async set(code, unmuteCode) {
|
||||
if (!code) {
|
||||
this.#codes = undefined;
|
||||
await this.#save(undefined);
|
||||
return 'off: the box no longer mutes anything while it listens';
|
||||
}
|
||||
const codes = { mute: checkCode(code), unmute: unmuteCode ? checkCode(unmuteCode) : checkCode(code) };
|
||||
this.#codes = codes;
|
||||
await this.#save(codes);
|
||||
return codes.mute === codes.unmute
|
||||
? `on: the box sends ${codes.mute} when a conversation starts, and again when it ends`
|
||||
: `on: the box sends ${codes.mute} when a conversation starts, and ${codes.unmute} when it ends`;
|
||||
}
|
||||
|
||||
/** @returns {MuteCodes | undefined} */
|
||||
get codes() { return this.#codes; }
|
||||
|
||||
/**
|
||||
* A conversation starts: mute, if the setting is on. Resolves to what unmutes at its end, with the
|
||||
* codes it muted with, even if the setting changes meanwhile.
|
||||
* @param {(code: string) => Promise<string>} send
|
||||
* @returns {Promise<() => Promise<string>>}
|
||||
*/
|
||||
async start(send) {
|
||||
const codes = this.#codes;
|
||||
if (!codes) return async () => 'muting is off';
|
||||
await send(codes.mute);
|
||||
return () => send(codes.unmute);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* listen(seconds) reports the codes a remote sent meanwhile; send(code, times, gapMs) sends one,
|
||||
* times times, gapMs apart: quick enough that a TV's input menu stays open between presses.
|
||||
@@ -74,10 +143,25 @@ export class Ir extends RpcTarget {
|
||||
/** @type {Promise<unknown>} the job before this one */
|
||||
#last = Promise.resolve();
|
||||
|
||||
/** @param {(args: string[]) => Promise<string>} pico runs pico_tty.py's verbs: listen SECONDS, repeat N GAP_MS CODE... */
|
||||
constructor(pico) {
|
||||
#muting;
|
||||
|
||||
/**
|
||||
* @param {(args: string[]) => Promise<string>} pico runs pico_tty.py's verbs: listen SECONDS, repeat N GAP_MS CODE...
|
||||
* @param {Muting} [muting] whether to mute the TV while the box listens
|
||||
*/
|
||||
constructor(pico, muting = undefined) {
|
||||
super();
|
||||
this.#pico = pico;
|
||||
this.#muting = muting;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mute the TV while the box listens, with these codes; or, with no code, don't.
|
||||
* @param {string} [code] @param {string} [unmuteCode] if not the same: mute is often a toggle
|
||||
*/
|
||||
muteWhileListening(code, unmuteCode) {
|
||||
if (!this.#muting) throw new Error('this box has no mute setting');
|
||||
return this.#muting.set(code, unmuteCode);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -106,10 +190,7 @@ export class Ir extends RpcTarget {
|
||||
* @param {number} [gapMs] between presses, 100 to 2000
|
||||
*/
|
||||
send(code, times = 1, gapMs = 400) {
|
||||
const norm = typeof code === 'string' ? normalize(code) : '';
|
||||
if (!CODE.test(norm) && !isRaw(norm)) {
|
||||
throw new TypeError(`code: hex bytes, like "20 DF 10 EF" or "samsung E0 E0 40 BF"; or "raw" and an odd number of microseconds, up to ${MAX_RAW}, each up to 65535`);
|
||||
}
|
||||
const norm = checkCode(code);
|
||||
if (!Number.isInteger(times) || times < 1 || times > MAX_TIMES) {
|
||||
throw new RangeError(`times: a whole number, 1 to ${MAX_TIMES}`);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user