feat(Player): segmented mode "infinite" via duration + pause_duration (F-077, v2.5.0)

"infinite" now optionally plays duration ms, pauses pause_duration ms and
repeats until playback.stop, the same as "interval" with total_duration 0,
which is what people expect "infinite" to mean. Sending neither keeps the
old back-to-back looping; sending only one is rejected, since a duration
without a pause flickers paused/playing and a pause without a duration
never triggers. total_duration is never read for infinite.

api-reference.md now documents the exact timing of every mode: 500 ms
tick, loops never cut short, paused/stopping status transitions, pause
measured from the loop end, total_duration including pauses, and the
~0.5 s engine gap between loops when the run has no end time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-28 10:44:04 +02:00
co-authored by Claude Opus 5.5
parent ced38fda0b
commit 5d8600d6bf
4 changed files with 55 additions and 13 deletions
+22 -1
View File
@@ -89,6 +89,7 @@ Check `git log -- src/Version.hpp` for the real bump history before tagging a ne
| F-074 | Fix: `mode: "infinite"` no longer flips status playing/paused every ~0.5 s | v2.4.0 | Stable |
| F-075 | Play/stop command replies no longer wait for telemetry SD writes (deferred to TelemetryTask) | v2.4.1 | Stable |
| F-076 | Fix: a `pause_duration` shorter than one melody loop no longer gets skipped | v2.4.2 | Stable |
| F-077 | `mode: "infinite"` accepts optional `duration` + `pause_duration` for play/pause cycles forever | v2.5.0 | Stable |
---
@@ -345,7 +346,10 @@ V2 play commands replace the ambiguous `continuous_loop` / `segment_duration` /
| `"single"` | Play exactly one melody loop, then stop | none |
| `"timed"` | Loop continuously for `duration` ms, stop at end of a full loop | `duration` |
| `"interval"` | Loop for `duration` ms, pause for `pause_duration` ms, repeat until `total_duration` ms elapsed | `duration`, `pause_duration`, `total_duration` |
| `"infinite"` | Loop forever until a `stop` command | none |
| `"infinite"` | Loop forever until a `stop` command; optionally in `duration`/`pause_duration` segments (F-077) | none, or `duration` + `pause_duration` |
Exact per-mode timing (500 ms tick, loops never cut short, pause/status transitions, the ~0.5 s gap between loops):
see `docs/reference/api-reference.md` → `playback.play` → Modes.
V1 timing fields (`segment_duration`, `continuous_loop`, `total_duration`) are still parsed when `mode` is absent, so the fix from F-024 continues to apply to V1 clients.
@@ -1585,3 +1589,20 @@ playing when the pause was flagged has ended. The silence now always lasts `paus
500 ms timer tick), measured from the end of that loop. Pauses longer than one loop already worked this way.
Subsystem: `src/Player/Player.cpp`
### F-077 — Segmented `mode: "infinite"`
**Why.** "Play/pause forever" was only possible as `interval` with `total_duration: 0`, which nobody guesses.
`infinite` reads as "never stops on its own", and the pauses should be an option of it.
**Change.** `infinite` now reads `duration` and `pause_duration`:
| Sent | Behaviour |
|---|---|
| neither (or both `0`) | Unchanged: back-to-back loops (with the ~0.5 s engine gap) until `playback.stop`. |
| both > 0 | Play `duration` ms, pause `pause_duration` ms, repeat until `playback.stop`. Same internal state as `interval` with `total_duration: 0`. |
| only one > 0 | Rejected: `Invalid infinite segments: duration and pause_duration must both be > 0, or both omitted`. A duration with no pause would pause and resume on the same tick, and a pause with no duration never triggers. |
`total_duration` is never read for `infinite`. `interval` with `total_duration: 0` still works as before.
Subsystem: `src/Player/Player.cpp` (`validatePlayParams`, `setMelodyAttributes`)
+19 -7
View File
@@ -180,20 +180,29 @@ The SUCCESS reply means the board **accepted** the command, not that bells are r
| `speed` | int | no | ms per beat, 0–65535. Boot default 500. `0` is coerced to 300. |
| `note_assignments` | int[] | no | Up to 16 integers, each 0–16. Index = melody note (0-based), value = **1-based bell channel** to ring for that note, `0` = silent. Boot default `[1,2,…,16]`. A shorter array leaves the remaining entries at their previous values. |
| `mode` | string | no | `"single"` \| `"timed"` \| `"interval"` \| `"infinite"` — anything else is rejected. Selects the timing model — see below. |
| `duration` | int | no | ms, ≥ 0. **Read only when `mode` is present**; must be > 0 for `timed`/`interval`. |
| `pause_duration` | int | no | ms between segments. Read when `mode` is `"interval"`, or when `mode` is absent. |
| `duration` | int | no | ms, ≥ 0. **Read only when `mode` is present**; must be > 0 for `timed`/`interval`, optional for `infinite` (segment length). |
| `pause_duration` | int | no | ms between segments. Read when `mode` is `"interval"` or `"infinite"`, or when `mode` is absent. |
| `total_duration` | int | no | ms. Read when `mode` is `"interval"`, or (with `continuous_loop: true`) when `mode` is absent. |
| `segment_duration` | int | no | ms. Legacy — read only when `mode` is absent. |
| `continuous_loop` | bool | no | Legacy — read only when `mode` is absent. Defaults to `false` on every command (not sticky). |
**How `mode` and `duration` interact** (`mode` present — `segment_duration` and `continuous_loop` are ignored):
**Modes** (`mode` present — `segment_duration` and `continuous_loop` are ignored):
| `mode` | Behaviour | Fields read |
|---|---|---|
| `"single"` | Play the melody once, then stop. | — (`duration` ignored) |
| `"timed"` | Loop the melody; when `duration` ms have elapsed, finish the current loop and stop (status goes `stopping` → `idle`). | `duration` (> 0) |
| `"interval"` | Play for `duration` ms, pause `pause_duration` ms, repeat; stop after `total_duration` ms overall (`0` = forever). | `duration` (> 0), `pause_duration`, `total_duration` |
| `"infinite"` | Loop until `playback.stop`. | — |
| `"single"` | Play the melody once, start to end, then stop. | none (`duration`, `pause_duration` and `total_duration` are ignored) |
| `"timed"` | Loop the melody back-to-back. When `duration` ms have passed, finish the current loop, then stop. No pauses. | `duration` (> 0) |
| `"interval"` | Play for `duration` ms, pause for `pause_duration` ms, repeat. Stop once `total_duration` ms have passed since the start (pauses included); `0` = never stop. | `duration` (> 0), `pause_duration`, `total_duration` |
| `"infinite"` | Never stops on its own; runs until `playback.stop`. With no `duration`/`pause_duration`, loops the melody continuously. With both, plays for `duration` ms, pauses for `pause_duration` ms, and repeats forever (same as `interval` with `total_duration: 0`). | `duration` + `pause_duration` (both > 0, or both omitted). `total_duration` is ignored. |
Exact timing, for all modes:
- **Timing is checked every 500 ms**, so each `duration`, `pause_duration` and `total_duration` boundary is hit up to 0.5 s late.
- **A loop is never cut short** (only `playback.stop` and the overload guard do that). When `duration` or `total_duration` runs out mid-loop, the current loop finishes first.
- **Status during that last loop:** at a segment end, `status/playback` goes to `paused` straight away while the loop finishes ringing. At the end of a `timed`/`interval` run it goes to `stopping`, then `idle` when the loop ends. `single` goes from `playing` directly to `idle`.
- **The pause is measured from the end of that loop:** the bells are silent for the full `pause_duration`, then `status/playback` goes back to `playing` and a new `duration` segment starts.
- **`total_duration` (interval) counts from the start of the play, pauses included.** If it runs out during a pause, playback stops at once (`paused` → `idle`).
- **Gap between loops:** whenever the run has no end time (`single`, `infinite`, `interval` with `total_duration: 0`), the engine waits ~0.5 s after each loop before starting the next one. `timed` and `interval` with a `total_duration` loop with no gap.
**Legacy timing** (`mode` absent):
@@ -217,6 +226,7 @@ Errors are checked in this order; the first that applies is returned. Every one
| `Invalid total_duration: must be a non-negative integer (ms)` | same, for `total_duration` |
| `Invalid mode: must be single, timed, interval or infinite` | Unrecognised `mode` |
| `Invalid duration: mode timed and interval require duration > 0` | `mode` is `timed`/`interval` and `duration` is missing or 0 |
| `Invalid infinite segments: duration and pause_duration must both be > 0, or both omitted` | `mode` is `infinite` and only one of `duration` / `pause_duration` is > 0 |
| `Playback blocked: bell mechanism is disabled (bells.enable)` | Bells master switch off (`bells.disable`) |
| `Playback blocked: bell overload protection active — wait for the bells to cool` | Load guard enabled and any bell is over its max load (the guard would force-stop immediately) |
| `Melody failed to load: file is empty or corrupt` | `/melodies/{pid}` exists on SD but is empty or has an odd byte count |
@@ -235,6 +245,8 @@ The ACK itself is always sent synchronously from the command's own dispatch, so
{ "v": 2, "req_id": "a1", "cmd": "playback.play", "contents": { "pid": "westminster", "speed": 400, "mode": "single" } }
{ "v": 2, "cmd": "playback.play", "contents": { "pid": "ABC123", "url": "https://example.com/ABC123.bin", "mode": "timed", "duration": 120000 } }
{ "v": 2, "cmd": "playback.play", "contents": { "pid": "ABC123", "mode": "interval", "duration": 15000, "pause_duration": 5000, "total_duration": 180000 } }
{ "v": 2, "cmd": "playback.play", "contents": { "pid": "ABC123", "mode": "infinite" } }
{ "v": 2, "cmd": "playback.play", "contents": { "pid": "ABC123", "mode": "infinite", "duration": 30000, "pause_duration": 10000 } }
{ "v": 2, "cmd": "playback.play", "contents": { "pid": "ABC123", "segment_duration": 15000, "pause_duration": 5000, "continuous_loop": true } }
```