diff --git a/meshterm/context.py b/meshterm/context.py index 7542d9d..d06cf7a 100644 --- a/meshterm/context.py +++ b/meshterm/context.py @@ -52,6 +52,7 @@ if TYPE_CHECKING: from .services.places import PlaceService from .services.rooms import RoomService from .services.site_suggest import SiteSuggester + from .services.sound import SoundService from .services.watchtower import WatchtowerService from .ui.surface import Ui @@ -202,6 +203,7 @@ class AppContext: _clock_sync: ClockSync | None = field(default=None, init=False, repr=False) _courier: CourierService | None = field(default=None, init=False, repr=False) _battery: BatteryService | None = field(default=None, init=False, repr=False) + _sound: SoundService | None = field(default=None, init=False, repr=False) _places: PlaceService | None = field(default=None, init=False, repr=False) _site_suggest: SiteSuggester | None = field(default=None, init=False, repr=False) _routes: RouteKeeper | None = field(default=None, init=False, repr=False) @@ -558,6 +560,20 @@ class AppContext: self._battery = BatteryService(self) return self._battery + @property + def sound(self) -> SoundService: + """Return the sound service of the session, and create it at first use. + + It plays the sound of a notification, and it flashes the title bar (refer to + :mod:`meshterm.services.sound`). It has no task. It acts only when a caller tells it + about an event. + """ + if self._sound is None: + from .services.sound import SoundService + + self._sound = SoundService(self) + return self._sound + @property def devstate(self) -> DeviceState: """Return the device-state cache of the session, and create it at first use. diff --git a/meshterm/services/sound.py b/meshterm/services/sound.py new file mode 100644 index 0000000..32cfa73 --- /dev/null +++ b/meshterm/services/sound.py @@ -0,0 +1,341 @@ +# SPDX-License-Identifier: Apache-2.0 +"""The sound service: play a sound, or flash the title bar, for a notification. + +A notification is an event that the user wants to know about now: a direct message, a +mention of our node, a channel message, a room post, a low battery, or (if the user wants +it) each packet heard. The callers decide *whether* an event notifies (the rules of each +conversation are in :class:`~meshterm.services.chat_service.ChatService`). This service +decides *how*: which sound, how loud, through which player, and whether the title bar +flashes. + +MeshTerm has no audio dependency. It plays a WAV file through the player of the operating +system: + +* **Windows**: ``winsound``, from the standard library. +* **macOS**: ``afplay``. +* **Linux** (the Cardputer Zero too): the first of ``pw-play``, ``paplay``, and ``aplay`` + that is installed. + +The volume is in MeshTerm, not in the player: ``winsound`` has no volume control. Thus the +service writes a scaled copy of each sound into a cache folder, one time for each volume, +and plays the copy. The ``bell`` theme plays no file. It sends the terminal bell (BEL), +which rings on the computer of the user, also over SSH. + +A player can fail: no audio device, no player installed, a server that is not running. +The first failure goes to the log one time. After it, the service does not try the player +again in this session, and each notification counts as silent. Thus the flash (in the +``silent`` mode) replaces the sound. A failure never stops the app. + +The PicoCalc has no sound (:attr:`~meshterm.platforms.Platform.sound`). There, each +notification is silent, and only the flash can show it. +""" + +from __future__ import annotations + +import logging +import os +import shutil +import subprocess +import sys +import time +import wave +from array import array +from collections.abc import Callable +from enum import Enum +from pathlib import Path +from typing import TYPE_CHECKING + +from ..platforms import get_platform + +if TYPE_CHECKING: + from ..context import AppContext + +log = logging.getLogger(__name__) + + +class SoundEvent(str, Enum): + """The events that can play a sound.""" + + DIRECT_MESSAGE = "direct_message" + MENTION = "mention" + CHANNEL_MESSAGE = "channel_message" + ROOM_POST = "room_post" + BATTERY_LOW = "battery_low" + PACKET_TICK = "packet_tick" + + +#: The folder of the sound files in the package. +SOUNDS_DIR = Path(__file__).resolve().parent.parent / "assets" / "sounds" + +#: The file of each event, for each theme that plays files. The keys are the values of the +#: ``sound_theme`` preference. JP selected the sounds of ``android`` by ear on 2026-11-20 +#: (refer to ``meshterm/assets/sounds/android/README.md``). +THEMES: dict[str, dict[SoundEvent, str]] = { + "android": { + SoundEvent.DIRECT_MESSAGE: "android/Carme.wav", + SoundEvent.MENTION: "android/Ariel.wav", + SoundEvent.CHANNEL_MESSAGE: "android/InCallNotification.wav", + SoundEvent.ROOM_POST: "android/Ceres.wav", + SoundEvent.BATTERY_LOW: "android/LowBattery.wav", + SoundEvent.PACKET_TICK: "android/Effect_Tick.wav", + }, +} + +#: The minimum seconds between the start of a sound and a packet tick. It limits the tick +#: to four each second on a busy mesh, and a tick does not play over the start of another +#: sound (JP, 2026-11-20). +TICK_GAP_S = 0.25 + + +def gain(volume_pct: int) -> float: + """The factor for the samples at a volume, on a curve that follows the ear. + + The ear hears loudness approximately on a logarithmic scale. A straight factor puts + almost all of the change in the lowest part of the range. The square of the fraction + is a simple curve that is near the ear: 50% is a factor of 0.25 (-12 dB). + + Args: + volume_pct: The volume, from 0 to 100. + + Returns: + The factor, from 0.0 to 1.0. + """ + fraction = max(0, min(100, volume_pct)) / 100 + return fraction * fraction + + +def scaled(source: Path, volume_pct: int, cache_dir: Path) -> Path: + """A copy of a 16-bit WAV file at a volume, made one time and kept in a cache. + + Args: + source: The WAV file at full volume. It must be 16-bit PCM. + volume_pct: The volume, from 0 to 100. At 100, the function returns ``source``. + cache_dir: The folder of the scaled copies. + + Returns: + The path of the file to play. + """ + if volume_pct >= 100: + return source + target = cache_dir / f"{source.parent.name}-{source.stem}-{volume_pct}.wav" + if target.exists(): + return target + with wave.open(str(source), "rb") as reader: + params = reader.getparams() + frames = reader.readframes(params.nframes) + if params.sampwidth != 2: + return source + samples = array("h") + samples.frombytes(frames) + if sys.byteorder == "big": + samples.byteswap() # WAV data is little-endian. + factor = gain(volume_pct) + out = array("h", (int(s * factor) for s in samples)) + if sys.byteorder == "big": + out.byteswap() + cache_dir.mkdir(parents=True, exist_ok=True) + partial = target.with_suffix(".part") + with wave.open(str(partial), "wb") as writer: + writer.setparams(params) + writer.writeframes(out.tobytes()) + os.replace(partial, target) + return target + + +class Player: + """Plays a WAV file through the player of the operating system, without a wait. + + The first failure goes to the log, and then the player stays off for the session. + """ + + def __init__(self) -> None: + """Make a player. It finds its command at the first sound.""" + self.broken = False + self._command: list[str] | None = None + self._running: list[subprocess.Popen[bytes]] = [] + + def play(self, path: Path) -> bool: + """Start the sound, and return at once. + + Args: + path: The WAV file. + + Returns: + ``True`` if the sound started. ``False`` if the player is off or failed. + """ + if self.broken: + return False + try: + if sys.platform == "win32": + import winsound + + flags = winsound.SND_FILENAME | winsound.SND_ASYNC | winsound.SND_NODEFAULT + winsound.PlaySound(str(path), flags) + return True + return self._spawn(path) + except Exception as error: # noqa: BLE001 - any failure of the player makes it off. + log.warning("sound: the player failed, sounds are off for this session: %s", error) + self.broken = True + return False + + def _spawn(self, path: Path) -> bool: + """Start the command of the player on a file (macOS and Linux).""" + if self._command is None: + self._command = _player_command() + if not self._command: + log.warning("sound: no player found (afplay, pw-play, paplay, or aplay)") + self.broken = True + return False + # Collect the players that stopped, so that no process stays as a zombie. + self._running = [p for p in self._running if p.poll() is None] + self._running.append( + subprocess.Popen( # noqa: S603 - a fixed command and a file of the package. + [*self._command, str(path)], + stdin=subprocess.DEVNULL, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + ) + ) + return True + + +def _player_command() -> list[str]: + """The command that plays a WAV file on this system, or an empty list.""" + if sys.platform == "darwin": + return ["afplay"] if shutil.which("afplay") else [] + for command in (["pw-play"], ["paplay"], ["aplay", "-q"]): + if shutil.which(command[0]): + return command + return [] + + +class SoundService: + """Plays the sound of a notification, and flashes the title bar if the user wants it. + + The interactive session gives the service two hooks: :meth:`set_hooks` names the + function that rings the terminal bell and the function that flashes the title bar. + Without them (a scripted run, a test), the bell and the flash do nothing. + """ + + def __init__( + self, + ctx: AppContext, + player: Player | None = None, + clock: Callable[[], float] = time.monotonic, + ) -> None: + """Make the service. + + Args: + ctx: The shared application context, read for the preferences and the config + folder. + player: The player. Tests give a fake one. + clock: The clock of the tick limit. Tests give a fake one. + """ + self._ctx = ctx + self._player = player or Player() + self._clock = clock + self._last_sound = float("-inf") + self._bell: Callable[[], None] | None = None + self._flash: Callable[[], None] | None = None + + def set_hooks( + self, + bell: Callable[[], None] | None = None, + flash: Callable[[], None] | None = None, + ) -> None: + """Name the functions that ring the terminal bell and flash the title bar.""" + self._bell = bell + self._flash = flash + + @property + def _prefs(self): # noqa: ANN202 - the Preferences of the context. + return self._ctx.preferences + + @property + def available(self) -> bool: + """Whether this platform can play a sound.""" + return get_platform().sound + + @property + def enabled(self) -> bool: + """Whether sounds are on: the platform can play them, and the user turned them on.""" + return self.available and bool(self._prefs.sounds) + + @property + def audible_theme(self) -> bool: + """Whether the theme makes a sound (``android`` or ``bell``, not ``none``).""" + return self._prefs.sound_theme != "none" + + def toggle(self) -> bool: + """Turn sounds on or off (the ^S key), and save the preference. + + Returns: + ``True`` if sounds are now on. + """ + now_on = not bool(self._prefs.sounds) + self._prefs.set("sounds", now_on) + try: + self._prefs.save() + except OSError as error: + log.warning("sound: could not save the preferences: %s", error) + return now_on + + def notify(self, event: SoundEvent) -> bool: + """Play the sound of an event, and flash the title bar if the preference says so. + + Args: + event: The event. + + Returns: + ``True`` if a sound started (a file or the bell). + """ + tick = event is SoundEvent.PACKET_TICK + if tick: + if not self._prefs.packet_tick: + return False + if self._clock() - self._last_sound < TICK_GAP_S: + return False + audible = self.enabled and self._emit(event) + if not tick: + mode = self._prefs.notify_flash + if mode == "on" or (mode == "silent" and not audible): + if self._flash is not None: + self._flash() + return audible + + def preview(self, event: SoundEvent = SoundEvent.CHANNEL_MESSAGE) -> bool: + """Play the sound of an event at the current volume, also when sounds are off. + + The Preferences page calls it while the user changes the volume or the theme + (JP, 2026-11-20: "while adjusting the volume, play the channel message sound"). + + Returns: + ``True`` if a sound started. + """ + return self.available and self._emit(event) + + def _emit(self, event: SoundEvent) -> bool: + """Play the sound of an event in the current theme, with no rules.""" + theme = self._prefs.sound_theme + if theme == "bell": + if self._bell is None: + return False + self._bell() + self._last_sound = self._clock() + return True + files = THEMES.get(theme) + if not files or event not in files: + return False + try: + path = scaled( + SOUNDS_DIR / files[event], + int(self._prefs.sound_volume_pct), + self._ctx.settings.config_dir / "sound-cache", + ) + except (OSError, wave.Error, EOFError) as error: + log.warning("sound: could not prepare %s: %s", files[event], error) + return False + if not self._player.play(path): + return False + self._last_sound = self._clock() + return True diff --git a/tests/test_sound.py b/tests/test_sound.py new file mode 100644 index 0000000..c5d1442 --- /dev/null +++ b/tests/test_sound.py @@ -0,0 +1,218 @@ +# SPDX-License-Identifier: Apache-2.0 +"""The sound service: the themes, the volume, the tick limit, the flash, and failures.""" + +from __future__ import annotations + +import wave +from pathlib import Path +from types import SimpleNamespace + +import pytest + +from meshterm.core.preferences import Preferences +from meshterm.platforms import PICOCALC_LYRA, REGULAR, get_platform, set_platform +from meshterm.services import sound +from meshterm.services.sound import SOUNDS_DIR, THEMES, SoundEvent, SoundService + + +class FakePlayer: + """Records each file that it plays, and can act as a failed player.""" + + def __init__(self, works: bool = True) -> None: + """Make a player that works, or one that fails.""" + self.played: list[Path] = [] + self.works = works + + def play(self, path: Path) -> bool: + """Record the file, and say if the sound started.""" + self.played.append(path) + return self.works + + +class Clock: + """A clock that the test moves.""" + + def __init__(self) -> None: + """Start at a fixed time.""" + self.now = 100.0 + + def __call__(self) -> float: + """The time now.""" + return self.now + + +def _service(tmp_path: Path, works: bool = True, **prefs: object): + ctx = SimpleNamespace( + preferences=Preferences(values=prefs), + settings=SimpleNamespace(config_dir=tmp_path), + ) + player, clock = FakePlayer(works), Clock() + service = SoundService(ctx, player=player, clock=clock) # type: ignore[arg-type] + flashes: list[int] = [] + bells: list[int] = [] + service.set_hooks(bell=lambda: bells.append(1), flash=lambda: flashes.append(1)) + return service, player, clock, flashes, bells + + +@pytest.fixture(autouse=True) +def _regular(): + previous = get_platform() + set_platform(REGULAR) + yield + set_platform(previous) + + +def test_each_theme_file_exists_and_is_16_bit_pcm() -> None: + """Each file that a theme names is in the package, and the volume code can read it.""" + for files in THEMES.values(): + assert set(files) == set(SoundEvent) + for name in files.values(): + with wave.open(str(SOUNDS_DIR / name), "rb") as reader: + assert reader.getsampwidth() == 2 + + +def test_sounds_are_off_by_default(tmp_path: Path) -> None: + """A new user hears nothing, and the default flash replaces the sound.""" + service, player, _, flashes, _ = _service(tmp_path) + assert service.notify(SoundEvent.DIRECT_MESSAGE) is False + assert player.played == [] + assert flashes == [1] + + +def test_an_event_plays_its_file_of_the_theme(tmp_path: Path) -> None: + """An event plays its own file of the theme, and the flash stays off.""" + service, player, _, flashes, _ = _service(tmp_path, sounds=True) + assert service.notify(SoundEvent.MENTION) is True + assert player.played[0].name.startswith("android-Ariel-") + assert flashes == [] # when silent: a sound played, so no flash + + +def test_the_volume_scales_a_cached_copy(tmp_path: Path) -> None: + """A volume below 100 plays a scaled copy from the cache: 50% is a quarter.""" + service, player, _, _, _ = _service(tmp_path, sounds=True, sound_volume_pct=50) + service.notify(SoundEvent.DIRECT_MESSAGE) + copy = player.played[0] + assert copy.parent == tmp_path / "sound-cache" + with wave.open(str(SOUNDS_DIR / "android/Carme.wav"), "rb") as full: + loud = max( + abs(int.from_bytes(full.readframes(4000)[i : i + 2], "little", signed=True)) + for i in range(0, 8000, 2) + ) + with wave.open(str(copy), "rb") as quiet: + soft = max( + abs(int.from_bytes(quiet.readframes(4000)[i : i + 2], "little", signed=True)) + for i in range(0, 8000, 2) + ) + assert soft == pytest.approx(loud * 0.25, abs=2) + + +def test_full_volume_plays_the_file_of_the_package(tmp_path: Path) -> None: + """At 100%, the service plays the file of the package, with no copy.""" + service, player, _, _, _ = _service(tmp_path, sounds=True, sound_volume_pct=100) + service.notify(SoundEvent.ROOM_POST) + assert player.played == [SOUNDS_DIR / "android/Ceres.wav"] + + +def test_the_gain_follows_the_square_of_the_volume() -> None: + """The gain is the square of the volume fraction.""" + assert sound.gain(100) == 1.0 + assert sound.gain(50) == 0.25 + assert sound.gain(0) == 0.0 + + +def test_the_bell_theme_rings_the_bell(tmp_path: Path) -> None: + """The bell theme rings the terminal bell and plays no file.""" + service, player, _, _, bells = _service(tmp_path, sounds=True, sound_theme="bell") + assert service.notify(SoundEvent.CHANNEL_MESSAGE) is True + assert bells == [1] + assert player.played == [] + + +def test_the_none_theme_is_silent_and_flashes(tmp_path: Path) -> None: + """The none theme plays nothing, so the default flash shows the event.""" + service, player, _, flashes, _ = _service(tmp_path, sounds=True, sound_theme="none") + assert service.notify(SoundEvent.DIRECT_MESSAGE) is False + assert player.played == [] + assert flashes == [1] + + +def test_the_tick_is_off_by_default(tmp_path: Path) -> None: + """The packet tick is off until the user turns it on.""" + service, player, _, flashes, _ = _service(tmp_path, sounds=True) + assert service.notify(SoundEvent.PACKET_TICK) is False + assert player.played == [] + assert flashes == [] # a tick never flashes + + +def test_the_tick_plays_at_most_one_each_quarter_second(tmp_path: Path) -> None: + """The tick plays at most one time each 250 ms.""" + service, player, clock, _, _ = _service(tmp_path, sounds=True, packet_tick=True) + assert service.notify(SoundEvent.PACKET_TICK) is True + clock.now += 0.1 + assert service.notify(SoundEvent.PACKET_TICK) is False + clock.now += 0.2 + assert service.notify(SoundEvent.PACKET_TICK) is True + assert len(player.played) == 2 + + +def test_no_tick_right_after_another_sound(tmp_path: Path) -> None: + """A tick does not play over the start of another sound.""" + service, player, clock, _, _ = _service(tmp_path, sounds=True, packet_tick=True) + service.notify(SoundEvent.DIRECT_MESSAGE) + clock.now += 0.1 + assert service.notify(SoundEvent.PACKET_TICK) is False + assert len(player.played) == 1 + + +def test_a_failed_player_counts_as_silent(tmp_path: Path) -> None: + """A player that fails counts as silent, so the flash shows the event.""" + service, _, _, flashes, _ = _service(tmp_path, works=False, sounds=True) + assert service.notify(SoundEvent.DIRECT_MESSAGE) is False + assert flashes == [1] + + +@pytest.mark.parametrize( + ("mode", "sounds", "flashes"), + [ + ("off", False, 0), + ("off", True, 0), + ("on", False, 1), + ("on", True, 1), + ("silent", False, 1), + ("silent", True, 0), + ], +) +def test_the_flash_modes(tmp_path: Path, mode: str, sounds: bool, flashes: int) -> None: + """The flash follows its preference: off, on, or only when no sound plays.""" + service, _, _, flashed, _ = _service(tmp_path, sounds=sounds, notify_flash=mode) + service.notify(SoundEvent.DIRECT_MESSAGE) + assert len(flashed) == flashes + + +def test_the_picocalc_is_silent_and_flashes(tmp_path: Path) -> None: + """The PicoCalc plays no sound, also for a preview, and it flashes.""" + set_platform(PICOCALC_LYRA) + service, player, _, flashes, _ = _service(tmp_path, sounds=True) + assert service.notify(SoundEvent.DIRECT_MESSAGE) is False + assert service.preview() is False + assert player.played == [] + assert flashes == [1] + + +def test_the_preview_plays_also_when_sounds_are_off(tmp_path: Path) -> None: + """The preview plays the channel message sound also when sounds are off.""" + service, player, _, flashes, _ = _service(tmp_path) + assert service.preview() is True + assert player.played[0].name.startswith("android-InCallNotification-") + assert flashes == [] + + +def test_toggle_changes_and_saves_the_preference(tmp_path: Path) -> None: + """The toggle changes the preference and saves it to the file.""" + prefs = Preferences(path=tmp_path / "preferences.toml") + ctx = SimpleNamespace(preferences=prefs, settings=SimpleNamespace(config_dir=tmp_path)) + service = SoundService(ctx, player=FakePlayer()) # type: ignore[arg-type] + assert service.toggle() is True + assert Preferences.load(tmp_path / "preferences.toml").sounds is True + assert service.toggle() is False + assert Preferences.load(tmp_path / "preferences.toml").sounds is False