feat: styles own their ringing tone (simple beeps, blink plays music)

Move audio ownership out of the shared runner and into each style via an
injected Sound capability (play_music(path) / play_beep()). The runner now
only owns button sampling and cleanup; the style kicks off its own tone in
start() and drives the LED.

- simple: a repeating square-wave beep (ordinary alarm-clock tone), no music
  file required. The beep is synthesised in memory as signed-16-bit stereo
  PCM (stdlib array+math) and played via pygame.mixer.Sound — no shipped
  audio asset, no new dependency.
- blink: unchanged behaviour — music on an endless loop from --music-file /
  MUSIC_FILE / the default track, via pygame.mixer.music.
- AlarmStyle contract gains sound + music_file in __init__ and a start()
  lifecycle hook; AlarmSound Protocol documents the audio seam for future
  styles that handle their own tone.
- setup() no longer loads music (that is the style's job now).

README updated: per-style ringing tone, the --music-file note (blink only),
and the extended plugin contract.
This commit is contained in:
2026-08-02 16:29:40 +02:00
parent 88a57dab6e
commit 92eba762ce
8 changed files with 291 additions and 87 deletions
+23 -14
View File
@@ -1,3 +1,5 @@
from unittest.mock import MagicMock
from styles.blink import (
STATE_BLINKING,
STATE_EVALUATING,
@@ -8,23 +10,31 @@ from styles.blink import (
)
def _led():
"""Return (calls list, set_led callable recording each call)."""
def _make_blink(music_file="music.mp3"):
"""Return (style, sound_mock) for a BlinkStyle ready to start."""
sound = MagicMock()
style = BlinkStyle(lambda on: None, sound, music_file)
return style, sound
def test_start_plays_music_and_led_off():
calls = []
return calls, lambda on: calls.append(on)
sound = MagicMock()
style = BlinkStyle(lambda on: calls.append(on), sound, "track.mp3")
style.start()
sound.play_music.assert_called_once_with("track.mp3")
assert calls == [False] # LED off while music plays until a press starts the puzzle
assert style.state == 0 # STATE_RINGING until a press
def test_press_starts_puzzle():
_, set_led = _led()
style = BlinkStyle(set_led)
style, _ = _make_blink()
assert style.update(100.0, True) is True
assert style.state == STATE_WAIT_BEFORE_BLINK
def test_blink_correct_sequence_stops(monkeypatch):
_, set_led = _led()
style = BlinkStyle(set_led)
style, _ = _make_blink()
style.update(100.0, True) # press -> start puzzle
assert style.state == STATE_WAIT_BEFORE_BLINK
@@ -48,8 +58,7 @@ def test_blink_correct_sequence_stops(monkeypatch):
def test_blink_incorrect_retries():
_, set_led = _led()
style = BlinkStyle(set_led)
style, _ = _make_blink()
style.state = STATE_WAIT_FOR_INPUT
style.target_blinks = 3
@@ -65,8 +74,9 @@ def test_blink_incorrect_retries():
def test_blinking_non_blocking():
calls, set_led = _led()
style = BlinkStyle(set_led)
calls = []
style, _ = _make_blink()
style.set_led = calls.append
style.state = STATE_BLINKING
style.target_blinks = 2
@@ -84,8 +94,7 @@ def test_blinking_non_blocking():
def test_blinking_advances_time():
_, set_led = _led()
style = BlinkStyle(set_led)
style, _ = _make_blink()
style.state = STATE_BLINKING
style.target_blinks = 4