seqplay: clock beats only while the simulated input is selected

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
jimmyandClaude Haiku 5.5 committed 2026-11-17 16:49:01 +13:00
1 parent b678800b62
commit 376e98b64a
1 file changed
+164 -53
+164 -53
View File
@@ -119,6 +119,13 @@ static int simulated_bpm(void)
return bpm < BPM_MIN ? BPM_MIN : (bpm > BPM_MAX ? BPM_MAX : bpm);
}
// The input device is "simulated" (settings.audio_device_select): the clock sets the beat. Call with the store locked.
bool seqplay_sim_selected(void)
{
const cJSON *sel = cJSON_GetObjectItem(store_settings(), "audio_device_select");
return cJSON_IsString(sel) && !strcmp(sel->valuestring, "simulated");
}
static bool audio_drives(void)
{
beat_info_t bi;
@@ -603,15 +610,21 @@ static void anchor_bar(void)
}
}
static void clear_drivers(const char *zone)
// Turn off what a sequence lit: its own targets only (they may lie outside its zone). Sequences never use the zone's
// groups; those belong to presets played directly in a zone
static void clear_sequence(const player_t *p)
{
cJSON *groups = cJSON_CreateArray();
char t[ZONE_TARGETS_MAX][GROUP_ID_MAX];
int n = engine_zone_targets(zone, t, ZONE_TARGETS_MAX);
for (int i = 0; i < n; i++) {
add_id(groups, t[i]);
for (int i = 0; i < p->ntargets; i++) {
for (int k = 0; k < p->target[i].n; k++) {
add_id(groups, p->target[i].g[k]);
}
cJSON *m = message(groups); // never clear every driver in range
}
if (!cJSON_GetArraySize(groups)) {
cJSON_Delete(groups);
return;
}
cJSON *m = message(groups);
if (m) {
cJSON_AddTrueToObject(m, "clear_presets");
cJSON_AddTrueToObject(m, "save");
@@ -752,54 +765,141 @@ static void covered_by(const player_t *p, macs_t *out)
}
}
// Bring back what the drivers in `drivers` showed before the one-shot: the sequence's current steps,
// else the zone's last preset, else off
static void restore(const macs_t *drivers, const char *zone)
/* What each driver a one-shot lights showed when the one-shot started, so it can go back to it after.
Noted per driver (a group's drivers may sit in other groups too); each goes back by its own d<mac> group. */
enum { WAS_OFF, WAS_PRESET, WAS_BASE };
typedef struct {
char mac[13];
uint8_t kind; // WAS_BASE: under the running (looping) sequence
char preset[ID_MAX];
uint32_t seq; // engine_driver_current's order number when noted (0: none of its zones had played)
} was_t;
static was_t s_was[MACS_MAX];
static int s_nwas;
static was_t *was_find(const char *mac)
{
for (int i = 0; i < s_nwas; i++) {
if (!strcmp(s_was[i].mac, mac)) {
return &s_was[i];
}
}
return NULL;
}
static void was_drop(const char *mac)
{
was_t *w = was_find(mac);
if (w) {
*w = s_was[--s_nwas];
}
}
// What a driver shows now: the running sequence if it lights the driver, else its zones' last preset, else off
static void was_now(was_t *w, const char *mac, const macs_t *base)
{
snprintf(w->mac, sizeof(w->mac), "%s", mac);
w->seq = 0;
w->preset[0] = 0;
bool has = engine_driver_current(mac, w->preset, sizeof(w->preset), &w->seq);
if (macs_has(base, mac)) {
w->kind = WAS_BASE;
} else {
w->kind = has && w->preset[0] && strcmp(w->preset, "off") ? WAS_PRESET : WAS_OFF;
}
}
// Note what the drivers a one-shot covers show now. Drivers already noted (a one-shot replacing another) keep
// what they showed before the first one
static void note_was(const macs_t *covered)
{
macs_t base = { 0 };
if (s_base.active) {
covered_by(&s_base, &base);
}
for (int i = 0; i < covered->n && s_nwas < MACS_MAX; i++) {
if (strlen(covered->m[i]) == 12 && !was_find(covered->m[i])) {
was_now(&s_was[s_nwas++], covered->m[i], &base);
}
}
}
// Put the drivers in `drivers` back to what they showed when the one-shot started: the running sequence, a
// preset, or off. A preset played or stopped on one of their zones since then wins. Wi-Fi drivers (hostname
// ids) are not handled here
static void restore(const macs_t *drivers)
{
if (!drivers->n) {
return;
}
s_restore = *drivers;
s_restoring = true;
if (s_base_clear_pending) {
if (s_base_clear_pending) { // the sequence ended under the one-shot
s_base_clear_pending = false;
clear_drivers(s_base.zone);
} else if (s_base.active) {
clear_sequence(&s_base);
}
macs_t base = { 0 }, back_on_base = { 0 };
if (s_base.active) {
covered_by(&s_base, &base);
}
static target_t t[MACS_MAX];
static char show[MACS_MAX][ID_MAX];
int nt = 0;
for (int i = 0; i < drivers->n; i++) {
const char *mac = drivers->m[i];
if (strlen(mac) != 12) {
continue;
}
was_t now, *w = was_find(mac);
was_now(&now, mac, &base);
if (now.kind == WAS_BASE) { // the running sequence lights it (it ran then, or started since)
macs_add(&back_on_base, mac);
continue;
}
if (!w || now.seq > w->seq) {
w = &now; // not noted, or its zone got a newer preset or stop
}
// A noted WAS_BASE here means that sequence has stopped or no longer lights it: off
snprintf(show[nt], ID_MAX, "%s", w->kind == WAS_PRESET ? w->preset : "off");
t[nt].n = 1;
snprintf(t[nt].g[0], GROUP_ID_MAX, "d%s", mac);
nt++;
}
// One send per preset: upload it, then select it on its drivers
player_t pt = { .target = t, .ntargets = nt };
uint32_t done = 0;
for (int i = 0; i < nt; i++) {
if (done & bit(i)) {
continue;
}
uint32_t mask = 0;
for (int j = i; j < nt; j++) {
if (!strcmp(show[j], show[i])) {
mask |= bit(j);
}
}
done |= mask;
ESP_LOGI(TAG, "one-shot over: %d driver(s) back to %s", __builtin_popcount(mask), show[i]);
if (strcmp(show[i], "off")) {
cJSON *presets = cJSON_CreateObject();
cJSON *pl = engine_preset_payload(show[i]);
if (pl) {
cJSON_AddItemToObject(presets, show[i], pl);
}
upload(&pt, mask, presets);
}
select_on(&pt, mask, show[i]);
}
if (back_on_base.n) {
ESP_LOGI(TAG, "one-shot over: %d driver(s) back to sequence %s", back_on_base.n, s_base.seq);
s_restore = back_on_base;
for (int i = 0; i < s_base.nlanes; i++) {
s_base.lane[i].last[0] = 0; // they may have missed its upload
send_lane(&s_base, i);
}
} else {
// The drivers the one-shot lit go back to their background: the zone's last preset if the driver is in
// the zone, else off (a driver outside the zone has no background). One target each, by its own group
// (d<mac>). Wi-Fi drivers (hostname ids) are not handled here
static target_t tin[MACS_MAX], tout[MACS_MAX];
int nin = 0, nout = 0;
}
for (int i = 0; i < drivers->n; i++) {
if (strlen(drivers->m[i]) != 12) {
continue;
}
target_t *tt = engine_mac_in_zone(drivers->m[i], zone) ? &tin[nin++] : &tout[nout++];
tt->n = 1;
snprintf(tt->g[0], GROUP_ID_MAX, "d%s", drivers->m[i]);
}
player_t pin = { .target = tin, .ntargets = nin };
player_t pout = { .target = tout, .ntargets = nout };
uint32_t allin = nin >= 32 ? 0xffffffffu : (1u << nin) - 1;
uint32_t allout = nout >= 32 ? 0xffffffffu : (1u << nout) - 1;
const char *cur = engine_zone_current(zone);
if (nin && cur && strcmp(cur, "off")) {
cJSON *presets = cJSON_CreateObject();
cJSON *pl = engine_preset_payload(cur);
if (pl) {
cJSON_AddItemToObject(presets, cur, pl);
}
upload(&pin, allin, presets);
select_on(&pin, allin, cur);
} else {
select_on(&pin, allin, "off");
}
select_on(&pout, allout, "off");
was_drop(drivers->m[i]);
}
s_restoring = false;
s_restore.n = 0;
@@ -814,7 +914,8 @@ static void overlay_end(void)
ESP_LOGI(TAG, "one-shot %s over", s_ovl.seq);
macs_t was = s_covered;
s_covered.n = 0;
restore(&was, s_ovl.zone);
restore(&was);
s_nwas = 0;
}
// Play sequence seq_id once over whatever runs (a one-shot). A new one replaces the one playing; drivers the new one
@@ -829,17 +930,19 @@ static void fire(const char *seq_id, const char *zone_id, const char *profile_id
s_covered.n = 0;
if (load(&s_ovl, seq_id, zone_id, profile_id, synced, &msg) != ESP_OK) {
ESP_LOGW(TAG, "one-shot %s did not start: %s", seq_id, msg ? msg : "not found");
restore(&was, zone_id);
restore(&was);
s_nwas = 0;
return;
}
covered_by(&s_ovl, &s_covered);
note_was(&s_covered);
macs_t back = { 0 };
for (int i = 0; i < was.n; i++) {
if (!macs_has(&s_covered, was.m[i])) {
macs_add(&back, was.m[i]);
}
}
restore(&back, zone_id);
restore(&back);
ESP_LOGI(TAG, "one-shot %s in zone %s: %d lane(s), %d driver(s)", seq_id, zone_id, s_ovl.nlanes, s_covered.n);
}
@@ -907,7 +1010,7 @@ static void finish_if_done(player_t *p)
if (s_ovl.active) {
s_base_clear_pending = true; // the one-shot still shows; clear once it is over
} else {
clear_drivers(p->zone);
clear_sequence(p);
}
ESP_LOGI(TAG, "sequence %s finished", p->seq);
}
@@ -1065,9 +1168,12 @@ static void beat_task(void *arg)
sub_tick();
continue;
}
if (next <= now) { // clock beat, counted only while the mic is not timing
if (next <= now) { // clock beat, counted only while the mic is not timing and "simulated" is the input
next = (next + interval > now) ? next + interval : now + interval;
if (!audio_drives()) {
store_lock();
bool sim = seqplay_sim_selected();
store_unlock();
if (sim && !audio_drives()) {
bool down = s_sim_beats % BEATS_PER_BAR == 0;
s_sim_beats++;
// Drivers get the clock's beats too, so manual (beat-stepped) presets keep moving
@@ -1195,13 +1301,13 @@ void seqplay_overlay_stop(void)
void seqplay_stop(bool clear)
{
store_lock();
bool was = s_base.active || s_ovl.active;
const char *zone = s_base.active ? s_base.zone : s_ovl.zone;
bool base_was = s_base.active, ovl_was = s_ovl.active;
s_pend.set = false;
s_ovl_pend.set = false;
s_base.active = false;
s_ovl.active = false; // everything stops: the zone's presets are cleared below
s_ovl.active = false; // everything stops: what the sequences lit is cleared below
s_covered.n = 0;
s_nwas = 0;
s_base_clear_pending = false;
bool finished = true;
for (int i = 0; i < s_base.nlanes; i++) {
@@ -1210,8 +1316,13 @@ void seqplay_stop(bool clear)
if (!finished) {
s_done_readout[0] = 0;
}
if (was && clear) {
clear_drivers(zone);
if (clear) {
if (base_was) {
clear_sequence(&s_base);
}
if (ovl_was) {
clear_sequence(&s_ovl);
}
}
store_unlock();
}