Skip to content

armgit / moss

Public Imported from GitHub
0

MOSS: a small, mostly 3D-printed litter-picking rover. Cleaner streets, one maker at a time.

Python

sounds: a silent keep-awake clip every 4 min so the Bluetooth speaker in the bin never sleeps between cues

metroxa3d3d6a7cba4
diff --git a/assets/sounds/keep_awake.wav b/assets/sounds/keep_awake.wav
new file mode 100644
index 0000000..72c6b58
Binary files /dev/null and b/assets/sounds/keep_awake.wav differ
diff --git a/benches/bench_moss_sounds_cold.py b/benches/bench_moss_sounds_cold.py
index e26f7a7..3e97940 100644
--- a/benches/bench_moss_sounds_cold.py
+++ b/benches/bench_moss_sounds_cold.py
@@ -40,6 +40,13 @@ check(len(manifest["assets"]) == 60 and not missing, f"60 assets named and prese
check(len(manifest["events"]) == 20 and all(len(e["variants"]) == 3 for e in manifest["events"].values()),
"20 event families, three variants each")
check("date" not in manifest and "hardware_context" not in manifest, "the manifest carries no date and no workshop context")
+import wave as _wave # noqa: E402
+with _wave.open(str(ASSETS / "keep_awake.wav"), "rb") as _w:
+ check(_w.getnchannels() == 1 and _w.getframerate() == 44100 and 0.4 < _w.getnframes() / 44100 < 1.0
+ and max(abs(x) for x in __import__("struct").unpack("<" + "h" * _w.getnframes(), _w.readframes(_w.getnframes()))) == 0,
+ "keep_awake.wav: mono 44.1 kHz, about half a second, all zeros (silent)")
+from moss_dimos.sounds import KEEP_AWAKE_S # noqa: E402
+check(KEEP_AWAKE_S == 240.0, "the keep-awake breath is every 4 min")
cmd = playback_command(ASSETS / "boot_ready_01.wav", backend="paplay", target="bt_sink", volume=0.5) if __import__("shutil").which("paplay") else None
check(cmd is None or (cmd[0] == "paplay" and "--device=bt_sink" in cmd and cmd[-1].endswith("boot_ready_01.wav")),
f"paplay command names the sink and the file: {cmd}")
diff --git a/moss_dimos/sounds.py b/moss_dimos/sounds.py
index e42d662..3282021 100644
--- a/moss_dimos/sounds.py
+++ b/moss_dimos/sounds.py
@@ -32,6 +32,8 @@ from __future__ import annotations
import json
import os
import shutil
+import subprocess
+import threading
import time
from pathlib import Path
from typing import Any
@@ -57,6 +59,8 @@ BATTERY_CRITICAL_MV = 10400 # the flight check refuses to launch below 10.8
BATTERY_SAMPLES = 6 # consecutive telemetry lines (3 s at 2 Hz) below a threshold before it counts
RECOVERY_WINDOW_S = 20.0 # a stall followed by motion inside this window = a recovery
RECOVERY_MIN_TICKS = 100 # encoder ticks that count as "moving again" after a stall
+KEEP_AWAKE_S = 240.0 # a Bluetooth speaker sleeps after minutes of silence and the cues are rare: a silent
+ # 0.6 s clip every KEEP_AWAKE_S keeps it on (assets/sounds/keep_awake.wav); 0 = off
def sound_volume(default: float, env: dict | None = None) -> float:
@@ -199,9 +203,36 @@ class MossSounds(Module):
logger.info(f"MOSS sounds: up, {len(self._player.rules)} event families, backend {backend}, "
f"sink {self._sink or 'default'}, volume {self._player.volume:.2f}, assets {self._assets_dir}")
self._play(["boot_ready"])
+ self._keep_awake(backend)
+
+ def _keep_awake(self, backend: str) -> None:
+ """A silent clip every KEEP_AWAKE_S on the same output, so a Bluetooth speaker in the
+ bin never goes to sleep between two cues (it did on the floor, and the cues after
+ that went nowhere). A daemon thread, stopped with the module."""
+ clip = self._assets_dir / "keep_awake.wav"
+ if KEEP_AWAKE_S <= 0 or not clip.is_file():
+ return
+ self._awake_stop = threading.Event()
+
+ def loop() -> None:
+ from moss_dimos.sound_pack.player import playback_command
+ while not self._awake_stop.wait(KEEP_AWAKE_S):
+ if self._player is None:
+ return
+ try:
+ cmd = playback_command(clip, backend, self._sink, 0.05)
+ subprocess.run(cmd, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
+ stderr=subprocess.DEVNULL, timeout=10)
+ except Exception as exc: # noqa: BLE001 - a missed breath is not an error
+ logger.warning(f"MOSS sounds: keep-awake clip failed ({type(exc).__name__}: {exc})")
+
+ threading.Thread(target=loop, name="moss-sounds-keep-awake", daemon=True).start()
@rpc
def stop(self) -> None:
+ stop_evt = getattr(self, "_awake_stop", None)
+ if stop_evt is not None:
+ stop_evt.set()
if self._player is not None:
try:
self._player.close()