feat(sequences): multi-lane playback and per-lane manual beats
- Add sequence_playback with beat and time advance, zone targeting fixes - Per-lane manual beat routing in beat_driver_route (parallel lanes) - Sequence API, editor JS, fix sequence model filename, tests Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -6,9 +6,13 @@ import asyncio
|
||||
import json
|
||||
import os
|
||||
import threading
|
||||
from typing import Any, Dict, List, Optional, Set
|
||||
from typing import Any, Dict, List, Optional, Set, Tuple
|
||||
|
||||
_route_lock = threading.Lock()
|
||||
# Per-lane manual routes: key ``-1`` = legacy single-route (preset push / UI); keys ``0..n`` =
|
||||
# zone sequence lanes so every manual lane gets its own stride counter and wire.
|
||||
_lane_manual: Dict[int, Dict[str, Any]] = {}
|
||||
# Public mirror for ``get_beat_route`` / header UI (derived from lane table).
|
||||
_beat_route: Dict[str, Any] = {
|
||||
"enabled": False,
|
||||
"device_names": [],
|
||||
@@ -18,6 +22,7 @@ _beat_route: Dict[str, Any] = {
|
||||
"manual_beat_n": 1,
|
||||
}
|
||||
_beat_counter: int = 0
|
||||
_preset_session_beats: int = 0
|
||||
_main_loop: Optional[asyncio.AbstractEventLoop] = None
|
||||
|
||||
|
||||
@@ -26,16 +31,65 @@ def set_beat_route_main_loop(loop: asyncio.AbstractEventLoop) -> None:
|
||||
_main_loop = loop
|
||||
|
||||
|
||||
def _pick_display_lane_key() -> Optional[int]:
|
||||
"""Lane key used for header stride readout (prefer sequence lane 0)."""
|
||||
if not _lane_manual:
|
||||
return None
|
||||
if 0 in _lane_manual:
|
||||
return 0
|
||||
seq_keys = [k for k in _lane_manual if isinstance(k, int) and k >= 0]
|
||||
if seq_keys:
|
||||
return min(seq_keys)
|
||||
if -1 in _lane_manual:
|
||||
return -1
|
||||
return min(_lane_manual.keys())
|
||||
|
||||
|
||||
def _sync_public_beat_route_from_lane_table() -> None:
|
||||
"""Mirror ``_lane_manual`` into legacy ``_beat_route`` shape for API consumers."""
|
||||
global _beat_route, _beat_counter
|
||||
pick = _pick_display_lane_key()
|
||||
if pick is None:
|
||||
_beat_route = {
|
||||
"enabled": False,
|
||||
"device_names": [],
|
||||
"wire_preset_id": "2",
|
||||
"is_manual": False,
|
||||
"pattern": "",
|
||||
"manual_beat_n": 1,
|
||||
}
|
||||
_beat_counter = 0
|
||||
return
|
||||
e = _lane_manual[pick]
|
||||
_beat_route = {
|
||||
"enabled": True,
|
||||
"device_names": list(e.get("device_names") or []),
|
||||
"wire_preset_id": str(e.get("wire_preset_id") or "2"),
|
||||
"is_manual": True,
|
||||
"pattern": str(e.get("pattern") or ""),
|
||||
"manual_beat_n": int(e.get("manual_beat_n") or 1),
|
||||
}
|
||||
_beat_counter = int(e.get("beat_counter", 0))
|
||||
|
||||
|
||||
def update_beat_route(payload: Dict[str, Any]) -> None:
|
||||
"""Internal: set or clear routing from explicit fields (tests / future APIs)."""
|
||||
global _beat_route, _beat_counter
|
||||
global _lane_manual, _beat_route, _beat_counter, _preset_session_beats
|
||||
if not isinstance(payload, dict):
|
||||
return
|
||||
with _route_lock:
|
||||
if payload.get("enabled") is False:
|
||||
_beat_route = {**_beat_route, "enabled": False}
|
||||
_lane_manual.clear()
|
||||
_beat_route = {
|
||||
**_beat_route,
|
||||
"enabled": False,
|
||||
"is_manual": False,
|
||||
"device_names": [],
|
||||
}
|
||||
_beat_counter = 0
|
||||
_preset_session_beats = 0
|
||||
return
|
||||
old = dict(_beat_route)
|
||||
names = payload.get("device_names")
|
||||
if not isinstance(names, list):
|
||||
names = []
|
||||
@@ -44,15 +98,20 @@ def update_beat_route(payload: Dict[str, Any]) -> None:
|
||||
except (TypeError, ValueError):
|
||||
n_raw = 1
|
||||
manual_n = max(1, min(64, n_raw))
|
||||
_beat_route = {
|
||||
"enabled": bool(payload.get("enabled", False)),
|
||||
"device_names": [str(n).strip() for n in names if str(n).strip()],
|
||||
"wire_preset_id": str(payload.get("wire_preset_id") or "2"),
|
||||
"is_manual": bool(payload.get("is_manual", False)),
|
||||
new_wire = str(payload.get("wire_preset_id") or "2")
|
||||
old_wire = str(old.get("wire_preset_id") or "2")
|
||||
if not old.get("enabled") or old_wire != new_wire:
|
||||
_preset_session_beats = 0
|
||||
clean_names = [str(n).strip() for n in names if str(n).strip()]
|
||||
_lane_manual.clear()
|
||||
_lane_manual[-1] = {
|
||||
"device_names": clean_names,
|
||||
"wire_preset_id": new_wire,
|
||||
"pattern": str(payload.get("pattern") or "").strip(),
|
||||
"manual_beat_n": manual_n,
|
||||
"beat_counter": 0,
|
||||
}
|
||||
_beat_counter = 0
|
||||
_sync_public_beat_route_from_lane_table()
|
||||
|
||||
|
||||
def get_beat_route() -> Dict[str, Any]:
|
||||
@@ -60,6 +119,44 @@ def get_beat_route() -> Dict[str, Any]:
|
||||
return dict(_beat_route)
|
||||
|
||||
|
||||
def manual_beat_stride_status() -> Dict[str, Any]:
|
||||
"""Audio-beat stride for a live manual preset (not sequence). For UI readout with BPM.
|
||||
|
||||
``beat_in_stride`` is always in ``1..stride_n`` when ``active`` (1-based within the stride).
|
||||
With multiple sequence manual lanes, reflects lane 0 (or the smallest lane index).
|
||||
"""
|
||||
with _route_lock:
|
||||
pick = _pick_display_lane_key()
|
||||
if pick is None or pick not in _lane_manual:
|
||||
wid = str(_beat_route.get("wire_preset_id") or "").strip()
|
||||
return {"active": False, "preset_session_beats": 0, "wire_preset_id": wid}
|
||||
e = _lane_manual[pick]
|
||||
c = int(e.get("beat_counter", 0))
|
||||
psb = int(_preset_session_beats)
|
||||
wid = str(e.get("wire_preset_id") or "").strip()
|
||||
try:
|
||||
n = int(e.get("manual_beat_n") or 1)
|
||||
except (TypeError, ValueError):
|
||||
n = 1
|
||||
n = max(1, min(64, n))
|
||||
if c <= 0:
|
||||
return {
|
||||
"active": True,
|
||||
"beat_in_stride": 1,
|
||||
"stride_n": n,
|
||||
"preset_session_beats": psb,
|
||||
"wire_preset_id": wid,
|
||||
}
|
||||
beat_in_stride = ((c - 1) % n) + 1
|
||||
return {
|
||||
"active": True,
|
||||
"beat_in_stride": beat_in_stride,
|
||||
"stride_n": n,
|
||||
"preset_session_beats": psb,
|
||||
"wire_preset_id": wid,
|
||||
}
|
||||
|
||||
|
||||
def _coerce_manual_beat_n(body: Any) -> int:
|
||||
"""Beats between audio-triggered selects (led-controller only); default 1 = every beat."""
|
||||
if not isinstance(body, dict):
|
||||
@@ -137,33 +234,99 @@ def _apply_manual_beat_route(
|
||||
preset_body: Any,
|
||||
) -> None:
|
||||
"""Enable audio→driver routing for one manual preset, or disable if invalid."""
|
||||
global _lane_manual
|
||||
if not device_names:
|
||||
update_beat_route({"enabled": False})
|
||||
with _route_lock:
|
||||
_lane_manual.clear()
|
||||
_sync_public_beat_route_from_lane_table()
|
||||
return
|
||||
if not isinstance(preset_body, dict):
|
||||
update_beat_route({"enabled": False})
|
||||
with _route_lock:
|
||||
_lane_manual.clear()
|
||||
_sync_public_beat_route_from_lane_table()
|
||||
return
|
||||
if _coerce_auto_from_body(preset_body):
|
||||
update_beat_route({"enabled": False})
|
||||
with _route_lock:
|
||||
_lane_manual.clear()
|
||||
_sync_public_beat_route_from_lane_table()
|
||||
return
|
||||
pattern = str(preset_body.get("pattern") or preset_body.get("p") or "").strip()
|
||||
if pattern and not _pattern_supports_manual(pattern):
|
||||
update_beat_route({"enabled": False})
|
||||
with _route_lock:
|
||||
_lane_manual.clear()
|
||||
_sync_public_beat_route_from_lane_table()
|
||||
return
|
||||
update_beat_route(
|
||||
{
|
||||
"enabled": True,
|
||||
"device_names": device_names,
|
||||
"wire_preset_id": wire_preset_id,
|
||||
"is_manual": True,
|
||||
names = [str(n).strip() for n in device_names if str(n).strip()]
|
||||
with _route_lock:
|
||||
_lane_manual.clear()
|
||||
_lane_manual[-1] = {
|
||||
"device_names": names,
|
||||
"wire_preset_id": str(wire_preset_id).strip(),
|
||||
"pattern": pattern,
|
||||
"manual_beat_n": _coerce_manual_beat_n(preset_body),
|
||||
"beat_counter": 0,
|
||||
}
|
||||
)
|
||||
_sync_public_beat_route_from_lane_table()
|
||||
|
||||
|
||||
def set_sequence_manual_lane_route(
|
||||
lane_index: int,
|
||||
device_names: List[str],
|
||||
wire_preset_id: str,
|
||||
preset_body: Any,
|
||||
) -> None:
|
||||
"""Register or update one sequence lane's manual beat route (parallel lanes, independent strides)."""
|
||||
global _lane_manual
|
||||
names = [str(n).strip() for n in (device_names or []) if str(n).strip()]
|
||||
if not names or not isinstance(preset_body, dict) or _coerce_auto_from_body(preset_body):
|
||||
with _route_lock:
|
||||
if lane_index in _lane_manual:
|
||||
del _lane_manual[lane_index]
|
||||
_sync_public_beat_route_from_lane_table()
|
||||
return
|
||||
pattern = str(preset_body.get("pattern") or preset_body.get("p") or "").strip()
|
||||
if pattern and not _pattern_supports_manual(pattern):
|
||||
with _route_lock:
|
||||
if lane_index in _lane_manual:
|
||||
del _lane_manual[lane_index]
|
||||
_sync_public_beat_route_from_lane_table()
|
||||
return
|
||||
mn = _coerce_manual_beat_n(preset_body)
|
||||
wid = str(wire_preset_id).strip()
|
||||
with _route_lock:
|
||||
old = _lane_manual.get(lane_index)
|
||||
bc = 0
|
||||
if (
|
||||
old
|
||||
and str(old.get("wire_preset_id") or "") == wid
|
||||
and int(old.get("manual_beat_n") or 1) == mn
|
||||
and set(old.get("device_names") or []) == set(names)
|
||||
):
|
||||
bc = int(old.get("beat_counter", 0))
|
||||
_lane_manual[lane_index] = {
|
||||
"device_names": names,
|
||||
"wire_preset_id": wid,
|
||||
"pattern": pattern,
|
||||
"manual_beat_n": mn,
|
||||
"beat_counter": bc,
|
||||
}
|
||||
_sync_public_beat_route_from_lane_table()
|
||||
|
||||
|
||||
def clear_sequence_manual_lane_route(lane_index: int) -> None:
|
||||
"""Remove beat routing for one sequence lane (e.g. step switched to auto)."""
|
||||
global _lane_manual
|
||||
with _route_lock:
|
||||
if lane_index in _lane_manual:
|
||||
del _lane_manual[lane_index]
|
||||
_sync_public_beat_route_from_lane_table()
|
||||
|
||||
|
||||
def sync_beat_route_from_push_sequence(
|
||||
sequence: List[Any], target_macs: Optional[List[str]] = None
|
||||
sequence: List[Any],
|
||||
target_macs: Optional[List[str]] = None,
|
||||
*,
|
||||
preserve_manual_beat_route_on_auto_select: bool = False,
|
||||
) -> None:
|
||||
"""
|
||||
Update beat routing from a ``/presets/push`` body ``sequence`` (list of v1 dicts).
|
||||
@@ -173,6 +336,10 @@ def sync_beat_route_from_push_sequence(
|
||||
Without ``select`` (e.g. manual preset loaded without immediate select): if ``target_macs``
|
||||
is set and the merged ``presets`` contain exactly one manual preset, enable routing using
|
||||
registry names for those MACs so the first advance is on the next audio beat.
|
||||
|
||||
When ``preserve_manual_beat_route_on_auto_select`` is true (zone sequence playback), an
|
||||
auto preset in ``select`` does not clear manual routing — other lanes may still need
|
||||
``notify_beat_detected`` for manual patterns in parallel.
|
||||
"""
|
||||
merged_presets: Dict[str, Any] = {}
|
||||
last_select: Optional[Dict[str, Any]] = None
|
||||
@@ -214,6 +381,13 @@ def sync_beat_route_from_push_sequence(
|
||||
if str(k).strip() == wire_preset_id:
|
||||
preset_body = v
|
||||
break
|
||||
if preset_body is None:
|
||||
update_beat_route({"enabled": False})
|
||||
return
|
||||
if _coerce_auto_from_body(preset_body):
|
||||
if not preserve_manual_beat_route_on_auto_select:
|
||||
update_beat_route({"enabled": False})
|
||||
return
|
||||
_apply_manual_beat_route(device_names, wire_preset_id, preset_body)
|
||||
return
|
||||
|
||||
@@ -247,25 +421,30 @@ def _pattern_supports_manual(pattern_key: str) -> bool:
|
||||
|
||||
def remap_beat_route_device_name(old_name: str, new_name: str) -> None:
|
||||
"""Update cached audio-beat target names after a device registry rename."""
|
||||
global _beat_route
|
||||
global _lane_manual
|
||||
o = str(old_name or "").strip()
|
||||
n = str(new_name or "").strip()
|
||||
if not o or not n or o == n:
|
||||
return
|
||||
with _route_lock:
|
||||
if not _beat_route.get("enabled"):
|
||||
return
|
||||
names = _beat_route.get("device_names") or []
|
||||
new_list: List[str] = []
|
||||
changed = False
|
||||
for item in names:
|
||||
if str(item).strip() == o:
|
||||
new_list.append(n)
|
||||
changed = True
|
||||
else:
|
||||
new_list.append(str(item))
|
||||
if changed:
|
||||
_beat_route = {**_beat_route, "device_names": new_list}
|
||||
any_changed = False
|
||||
for e in _lane_manual.values():
|
||||
names = e.get("device_names") or []
|
||||
if not isinstance(names, list):
|
||||
continue
|
||||
new_list: List[str] = []
|
||||
row_changed = False
|
||||
for item in names:
|
||||
if str(item).strip() == o:
|
||||
new_list.append(n)
|
||||
row_changed = True
|
||||
else:
|
||||
new_list.append(str(item))
|
||||
if row_changed:
|
||||
e["device_names"] = new_list
|
||||
any_changed = True
|
||||
if any_changed:
|
||||
_sync_public_beat_route_from_lane_table()
|
||||
|
||||
|
||||
async def _deliver_select(device_names: List[str], wire_preset_id: str) -> None:
|
||||
@@ -302,35 +481,45 @@ async def _deliver_select(device_names: List[str], wire_preset_id: str) -> None:
|
||||
print(f"[beat-route] deliver failed: {e}")
|
||||
|
||||
|
||||
async def _deliver_select_batch(pairs: List[Tuple[List[str], str]]) -> None:
|
||||
for names, pid in pairs:
|
||||
await _deliver_select(names, pid)
|
||||
|
||||
|
||||
def notify_beat_detected() -> None:
|
||||
"""Invoked from the audio thread when a beat is detected."""
|
||||
global _beat_counter
|
||||
global _preset_session_beats
|
||||
work: List[Tuple[List[str], str]] = []
|
||||
with _route_lock:
|
||||
r = dict(_beat_route)
|
||||
if not r.get("enabled"):
|
||||
if not _lane_manual:
|
||||
return
|
||||
if not r.get("is_manual"):
|
||||
return
|
||||
pattern = r.get("pattern") or ""
|
||||
if pattern and not _pattern_supports_manual(pattern):
|
||||
return
|
||||
names = r.get("device_names") or []
|
||||
if not names:
|
||||
return
|
||||
try:
|
||||
n = int(r.get("manual_beat_n") or 1)
|
||||
except (TypeError, ValueError):
|
||||
n = 1
|
||||
n = max(1, min(64, n))
|
||||
_beat_counter += 1
|
||||
if ((_beat_counter - 1) % n) != 0:
|
||||
return
|
||||
preset_id = str(r.get("wire_preset_id") or "2")
|
||||
names_copy = list(names)
|
||||
work = []
|
||||
for key in sorted(_lane_manual.keys()):
|
||||
e = _lane_manual[key]
|
||||
names = e.get("device_names") or []
|
||||
if not isinstance(names, list) or not names:
|
||||
continue
|
||||
pattern = str(e.get("pattern") or "")
|
||||
if pattern and not _pattern_supports_manual(pattern):
|
||||
continue
|
||||
try:
|
||||
n = int(e.get("manual_beat_n") or 1)
|
||||
except (TypeError, ValueError):
|
||||
n = 1
|
||||
n = max(1, min(64, n))
|
||||
e["beat_counter"] = int(e.get("beat_counter", 0)) + 1
|
||||
c = int(e["beat_counter"])
|
||||
if (c - 1) % n != 0:
|
||||
continue
|
||||
work.append((list(names), str(e.get("wire_preset_id") or "2")))
|
||||
if work:
|
||||
_preset_session_beats += 1
|
||||
if not work:
|
||||
return
|
||||
loop = _main_loop
|
||||
if loop is None:
|
||||
return
|
||||
try:
|
||||
asyncio.run_coroutine_threadsafe(_deliver_select(names_copy, preset_id), loop)
|
||||
asyncio.run_coroutine_threadsafe(_deliver_select_batch(work), loop)
|
||||
except Exception as e:
|
||||
print(f"[beat-route] schedule failed: {e}")
|
||||
|
||||
Reference in New Issue
Block a user