- led-simulator: the rig runs the C++ led-driver as WebAssembly (sim-driver/), one single-threaded worker per bar, no cross-origin isolation needed. Rooms (one JSON per room) with an on-page editor, and a pattern gallery. The Python server, Pyodide boot and ring buffer are removed. - sim-controller sim_rig.c: serves /sim/ from an embedded pack, the page's WebSocket and the rooms/flash API. The desktop build embeds it too. - bridge.c: a "browser" bridge kind that carries ESP-NOW frames to the page; devices announced through it are marked sim. - UI: Simulator in the Menu, split view with resize and pop-out, swipe between controller and rig on phones; SIM/REAL badges in Devices. - Fades: preset pushes and Wi-Fi driver bodies always carry fade and beat_time, so "off" no longer inherits an earlier fade. - Also the sequences-v2 work in progress: desktop port, bridge firmware, serial bridge, Wi-Fi drivers, sequence timeline, audio and API changes. - led-driver: ESP-NOW link, beat sync, control messages, sim hooks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1474 lines
48 KiB
C
1474 lines
48 KiB
C
#include "seqplay.h"
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <strings.h>
|
|
#include "api_util.h"
|
|
#include "beat.h"
|
|
#include "engine.h"
|
|
#include "espnow_tx.h"
|
|
#include "bench.h"
|
|
#include "esp_heap_caps.h"
|
|
#include "esp_log.h"
|
|
#include "esp_random.h"
|
|
#include "esp_timer.h"
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/queue.h"
|
|
#include "freertos/task.h"
|
|
#include "store.h"
|
|
|
|
static const char *TAG = "seqplay";
|
|
|
|
#define LANES_MAX 16
|
|
#define STEPS_MAX 64
|
|
#define TARGETS_MAX 32
|
|
#define TGROUPS_MAX 8 // group ids per target
|
|
#define MACS_MAX 32
|
|
#define ID_MAX 24
|
|
#define BPM_MIN 60
|
|
#define BPM_MAX 200
|
|
#define BEATS_PER_BAR 4
|
|
#define UNITS 4 // beat lanes count in quarter beats
|
|
|
|
/* A target is one entry of a sequence's ordered driver list: group ids, "z<zone>" ids and drivers' own
|
|
"d<mac>" groups. Lanes and steps name targets by a bit mask. Older records (lanes_group_ids, a step's
|
|
group_ids, the zone's groups) become extra targets when they load. */
|
|
typedef struct {
|
|
char g[TGROUPS_MAX][GROUP_ID_MAX];
|
|
int n;
|
|
} target_t;
|
|
|
|
typedef struct {
|
|
char id[ID_MAX]; // preset id, or an animation's (sequence) id when anim
|
|
bool anim; // fire animation `id` over the sequence
|
|
bool rest; // nothing lit: the lane's targets go off
|
|
bool others_off; // the lane's targets this step does not light go off
|
|
int units; // beat lanes: quarter beats the step lasts
|
|
int ms; // time lanes: how long the step lasts
|
|
uint32_t mask; // targets the step lights; 0 = the lane's
|
|
} step_t;
|
|
|
|
enum { CHASE_FORWARD, CHASE_REVERSE, CHASE_BOUNCE, CHASE_RANDOM };
|
|
|
|
typedef struct {
|
|
step_t step[STEPS_MAX];
|
|
int n, idx, count; // count: quarter beats into the step
|
|
int wait; // beat lanes: quarter beats before the lane starts (offset)
|
|
int64_t t0; // time lanes: when the step started, or (before the start) when the lane starts
|
|
bool timed, started, done;
|
|
uint32_t mask; // the lane's targets
|
|
uint8_t order[TARGETS_MAX];
|
|
int norder; // the lane's targets in order (chase)
|
|
bool chase, chase_off;
|
|
int corder, width, pos, dir;
|
|
char last[ID_MAX]; // preset last uploaded to the lane's targets
|
|
} lane_t;
|
|
|
|
typedef struct {
|
|
bool active, overlay;
|
|
char seq[ID_MAX], zone[ID_MAX], profile[ID_MAX];
|
|
lane_t *lane;
|
|
int nlanes;
|
|
target_t *target;
|
|
int ntargets;
|
|
bool loop;
|
|
bool synced; // a beat has been counted since the start (sub-beat ticks count from then)
|
|
bool anchor_next; // the next beat starts a bar
|
|
bool lane0_looped;
|
|
int loop_beat;
|
|
bool pending_switch; // a queued start takes over on the next beat; stop advancing meanwhile
|
|
} player_t;
|
|
|
|
typedef struct {
|
|
char m[MACS_MAX][13];
|
|
int n;
|
|
} macs_t;
|
|
|
|
// Everything below is guarded by the store lock (the beat task and the HTTP handlers both take it).
|
|
// Lanes and targets live in PSRAM, allocated by seqplay_init.
|
|
static player_t s_base; // the sequence
|
|
static player_t s_ovl; // a one-shot sequence over it
|
|
static macs_t s_covered; // drivers the one-shot lights; the sequence's sends skip them
|
|
static macs_t s_restore; // while set (n > 0), sends go only to these drivers
|
|
static bool s_restoring;
|
|
static bool s_base_clear_pending; // the sequence ended under the one-shot: clear once it is over
|
|
|
|
static struct {
|
|
bool set;
|
|
char seq[ID_MAX], zone[ID_MAX], profile[ID_MAX];
|
|
} s_pend;
|
|
|
|
static struct {
|
|
bool set, downbeat;
|
|
char seq[ID_MAX], zone[ID_MAX], profile[ID_MAX];
|
|
} s_ovl_pend;
|
|
|
|
static char s_done_readout[24]; // "6/6" kept after a sequence ran to its end
|
|
static bool s_audio = true;
|
|
static uint32_t s_sim_beats; // simulated clock beats, for its bar phase
|
|
static int64_t s_last_beat_us;
|
|
static int64_t s_beat_iv = 500000; // last beat interval (us), for sub-beat ticks
|
|
static int s_sub = UNITS - 1; // sub-beat ticks fired since the last beat
|
|
|
|
/* ------------------------------------------------------------- settings */
|
|
|
|
static int simulated_bpm(void)
|
|
{
|
|
const cJSON *v = cJSON_GetObjectItem(store_settings(), "audio_simulated_bpm");
|
|
int bpm = cJSON_IsNumber(v) ? (int)v->valuedouble : 120;
|
|
return bpm < BPM_MIN ? BPM_MIN : (bpm > BPM_MAX ? BPM_MAX : bpm);
|
|
}
|
|
|
|
static bool audio_drives(void)
|
|
{
|
|
beat_info_t bi;
|
|
beat_get_info(&bi);
|
|
return s_audio && bi.locked;
|
|
}
|
|
|
|
// "downbeat" only while the mic is timing (the clock's bar is a guess); otherwise "beat"
|
|
static bool wait_for_downbeat(void)
|
|
{
|
|
const cJSON *v = cJSON_GetObjectItem(store_settings(), "sequence_switch_wait");
|
|
return audio_drives() && cJSON_IsString(v) && !strcasecmp(v->valuestring, "downbeat");
|
|
}
|
|
|
|
static double num(const cJSON *o, const char *k, double def)
|
|
{
|
|
const cJSON *v = cJSON_GetObjectItem(o, k);
|
|
return cJSON_IsNumber(v) ? v->valuedouble : def;
|
|
}
|
|
|
|
static bool str_is(const cJSON *o, const char *k, const char *want)
|
|
{
|
|
const cJSON *v = cJSON_GetObjectItem(o, k);
|
|
return cJSON_IsString(v) && !strcmp(v->valuestring, want);
|
|
}
|
|
|
|
// A sequence loops unless its `loop` is off. Older records: kind "animation" with no `loop` plays once.
|
|
static bool seq_oneshot(const cJSON *seq)
|
|
{
|
|
const cJSON *loop = cJSON_GetObjectItem(seq, "loop");
|
|
if (cJSON_IsBool(loop)) {
|
|
return cJSON_IsFalse(loop);
|
|
}
|
|
if (cJSON_IsNumber(loop)) {
|
|
return loop->valuedouble == 0;
|
|
}
|
|
return str_is(seq, "kind", "animation");
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- targets */
|
|
|
|
static void add_id(cJSON *arr, const char *id)
|
|
{
|
|
const cJSON *it;
|
|
cJSON_ArrayForEach(it, arr)
|
|
{
|
|
if (!strcmp(it->valuestring, id)) {
|
|
return;
|
|
}
|
|
}
|
|
cJSON_AddItemToArray(arr, cJSON_CreateString(id));
|
|
}
|
|
|
|
static bool macs_has(const macs_t *s, const char *mac)
|
|
{
|
|
for (int i = 0; i < s->n; i++) {
|
|
if (!strcmp(s->m[i], mac)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
static void macs_add(macs_t *s, const char *mac)
|
|
{
|
|
if (s->n < MACS_MAX && !macs_has(s, mac)) {
|
|
snprintf(s->m[s->n++], 13, "%s", mac);
|
|
}
|
|
}
|
|
|
|
// Add a target with these group ids (or find the same one); its index, or -1 when the table is full
|
|
static int target_add(player_t *p, char g[][GROUP_ID_MAX], int n)
|
|
{
|
|
for (int i = 0; i < p->ntargets; i++) {
|
|
const target_t *t = &p->target[i];
|
|
bool same = t->n == n;
|
|
for (int k = 0; same && k < n; k++) {
|
|
same = !strcmp(t->g[k], g[k]);
|
|
}
|
|
if (same) {
|
|
return i;
|
|
}
|
|
}
|
|
if (p->ntargets == TARGETS_MAX) {
|
|
return -1;
|
|
}
|
|
target_t *t = &p->target[p->ntargets];
|
|
t->n = n < TGROUPS_MAX ? n : TGROUPS_MAX;
|
|
for (int k = 0; k < t->n; k++) {
|
|
snprintf(t->g[k], GROUP_ID_MAX, "%s", g[k]);
|
|
}
|
|
return p->ntargets++;
|
|
}
|
|
|
|
// A target made from a JSON list of group ids; -1 when the list is empty
|
|
static int target_from_list(player_t *p, const cJSON *list)
|
|
{
|
|
char g[TGROUPS_MAX][GROUP_ID_MAX];
|
|
int n = 0;
|
|
const cJSON *it;
|
|
cJSON_ArrayForEach(it, list)
|
|
{
|
|
if (n < TGROUPS_MAX && api_id(it, g[n], GROUP_ID_MAX)) {
|
|
n++;
|
|
}
|
|
}
|
|
return n ? target_add(p, g, n) : -1;
|
|
}
|
|
|
|
static int target_zone(player_t *p)
|
|
{
|
|
char t[ZONE_TARGETS_MAX][GROUP_ID_MAX];
|
|
int n = engine_zone_targets(p->zone, t, ZONE_TARGETS_MAX);
|
|
return n > 0 ? target_add(p, t, n) : -1;
|
|
}
|
|
|
|
static uint32_t bit(int i)
|
|
{
|
|
return i >= 0 && i < TARGETS_MAX ? 1u << i : 0;
|
|
}
|
|
|
|
// The sequence's own target list: {group_ids, devices}. Always the first entries of the table.
|
|
static void load_targets(player_t *p, const cJSON *seq)
|
|
{
|
|
p->ntargets = 0;
|
|
const cJSON *t;
|
|
cJSON_ArrayForEach(t, cJSON_GetObjectItem(seq, "targets"))
|
|
{
|
|
if (p->ntargets == TARGETS_MAX) {
|
|
break;
|
|
}
|
|
target_t *tt = &p->target[p->ntargets++];
|
|
tt->n = 0;
|
|
const cJSON *it;
|
|
cJSON_ArrayForEach(it, cJSON_GetObjectItem(t, "group_ids"))
|
|
{
|
|
if (tt->n < TGROUPS_MAX && api_id(it, tt->g[tt->n], GROUP_ID_MAX)) {
|
|
tt->n++;
|
|
}
|
|
}
|
|
cJSON_ArrayForEach(it, cJSON_GetObjectItem(t, "devices"))
|
|
{
|
|
char mac[13];
|
|
if (tt->n < TGROUPS_MAX && api_mac(it, mac)) {
|
|
tt->g[tt->n][0] = 'd';
|
|
memcpy(tt->g[tt->n++] + 1, mac, 13);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Mask of the sequence's targets named by indexes in list
|
|
static uint32_t mask_of(const player_t *p, const cJSON *list, int nuser)
|
|
{
|
|
uint32_t m = 0;
|
|
const cJSON *it;
|
|
cJSON_ArrayForEach(it, list)
|
|
{
|
|
if (cJSON_IsNumber(it) && it->valuedouble >= 0 && it->valuedouble < nuser) {
|
|
m |= bit((int)it->valuedouble);
|
|
}
|
|
}
|
|
return m;
|
|
}
|
|
|
|
// Group ids for a target mask; while an animation plays over the sequence, or while drivers are being
|
|
// restored after it, they become the drivers' own groups for just the drivers that may be sent to.
|
|
static cJSON *groups_for(const player_t *p, uint32_t mask)
|
|
{
|
|
cJSON *out = cJSON_CreateArray();
|
|
for (int i = 0; i < p->ntargets; i++) {
|
|
if (mask & bit(i)) {
|
|
for (int k = 0; k < p->target[i].n; k++) {
|
|
add_id(out, p->target[i].g[k]);
|
|
}
|
|
}
|
|
}
|
|
bool exclude = p == &s_base && s_ovl.active && s_covered.n;
|
|
if (!s_restoring && !exclude) {
|
|
return out;
|
|
}
|
|
cJSON *drivers = cJSON_CreateArray();
|
|
const cJSON *g;
|
|
cJSON_ArrayForEach(g, out)
|
|
{
|
|
char macs[MACS_MAX][13];
|
|
int n = engine_group_macs(g->valuestring, macs, MACS_MAX);
|
|
if (!n && !s_restoring) { // no known members: nothing to leave out
|
|
add_id(drivers, g->valuestring);
|
|
}
|
|
for (int i = 0; i < n; i++) {
|
|
bool keep = s_restoring ? macs_has(&s_restore, macs[i]) : !macs_has(&s_covered, macs[i]);
|
|
if (keep) {
|
|
char gid[GROUP_ID_MAX] = "d";
|
|
memcpy(gid + 1, macs[i], 13); // 12 hex digits and the NUL
|
|
add_id(drivers, gid);
|
|
}
|
|
}
|
|
}
|
|
cJSON_Delete(out);
|
|
return drivers;
|
|
}
|
|
|
|
static void send_msg(cJSON *msg)
|
|
{
|
|
cJSON *messages = cJSON_CreateArray();
|
|
cJSON_AddItemToArray(messages, msg);
|
|
engine_push(messages, NULL);
|
|
cJSON_Delete(messages);
|
|
}
|
|
|
|
// A v1 message to groups; NULL (and groups freed) when there are none, so nothing is broadcast to
|
|
// every driver in range
|
|
static cJSON *message(cJSON *groups)
|
|
{
|
|
if (!groups || !groups->child) {
|
|
cJSON_Delete(groups);
|
|
return NULL;
|
|
}
|
|
cJSON *m = cJSON_CreateObject();
|
|
cJSON_AddStringToObject(m, "v", "1");
|
|
cJSON_AddItemToObject(m, "groups", groups);
|
|
return m;
|
|
}
|
|
|
|
static void upload(const player_t *p, uint32_t mask, cJSON *presets)
|
|
{
|
|
if (!presets->child) {
|
|
cJSON_Delete(presets);
|
|
return;
|
|
}
|
|
cJSON *m = message(groups_for(p, mask));
|
|
if (!m) {
|
|
cJSON_Delete(presets);
|
|
return;
|
|
}
|
|
cJSON_AddItemToObject(m, "presets", presets);
|
|
send_msg(m);
|
|
}
|
|
|
|
// Upload presets of steps [from, to) of a lane (each once) to all of the lane's targets
|
|
static void send_presets(const player_t *p, int lane, int from, int to)
|
|
{
|
|
const lane_t *l = &p->lane[lane];
|
|
cJSON *presets = cJSON_CreateObject();
|
|
for (int i = from; i < to && i < l->n; i++) {
|
|
const step_t *st = &l->step[i];
|
|
if (st->anim || st->rest || !st->id[0] || cJSON_GetObjectItem(presets, st->id)) {
|
|
continue;
|
|
}
|
|
cJSON *pl = engine_preset_payload(st->id);
|
|
if (pl) {
|
|
cJSON_AddItemToObject(presets, st->id, pl);
|
|
}
|
|
}
|
|
upload(p, l->mask, presets);
|
|
}
|
|
|
|
static void select_on(const player_t *p, uint32_t mask, const char *preset)
|
|
{
|
|
if (!mask) {
|
|
return;
|
|
}
|
|
cJSON *m = message(groups_for(p, mask));
|
|
if (m) {
|
|
cJSON *sel = cJSON_AddArrayToObject(m, "select");
|
|
cJSON_AddItemToArray(sel, cJSON_CreateString(preset));
|
|
send_msg(m);
|
|
}
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ lanes */
|
|
|
|
static uint32_t chase_mask(const lane_t *l)
|
|
{
|
|
uint32_t m = 0;
|
|
for (int k = 0; k < l->width && k < l->norder; k++) {
|
|
m |= bit(l->order[(l->pos + k) % l->norder]);
|
|
}
|
|
return m;
|
|
}
|
|
|
|
static void chase_move(lane_t *l)
|
|
{
|
|
int n = l->norder;
|
|
if (n < 2) {
|
|
return;
|
|
}
|
|
switch (l->corder) {
|
|
case CHASE_REVERSE:
|
|
l->pos = (l->pos + n - 1) % n;
|
|
break;
|
|
case CHASE_BOUNCE:
|
|
if (l->pos + l->dir < 0 || l->pos + l->dir >= n) {
|
|
l->dir = -l->dir;
|
|
}
|
|
l->pos += l->dir;
|
|
break;
|
|
case CHASE_RANDOM: {
|
|
int next = (int)(esp_random() % (uint32_t)(n - 1));
|
|
l->pos = next >= l->pos ? next + 1 : next;
|
|
break;
|
|
}
|
|
default:
|
|
l->pos = (l->pos + 1) % n;
|
|
}
|
|
}
|
|
|
|
static void fire(const char *seq_id, const char *zone_id, const char *profile_id, bool synced);
|
|
|
|
// Show a lane's current step: upload its preset unless already there, select it on the targets it
|
|
// lights, and turn off the lane's other targets when the step asks for that
|
|
static void send_lane(player_t *p, int lane)
|
|
{
|
|
lane_t *l = &p->lane[lane];
|
|
if (!l->started || l->done || l->idx >= l->n) {
|
|
return;
|
|
}
|
|
const step_t *st = &l->step[l->idx];
|
|
if (st->anim) {
|
|
if (!p->overlay && !s_restoring) {
|
|
fire(st->id, p->zone, p->profile, true);
|
|
}
|
|
return;
|
|
}
|
|
uint32_t lit = st->rest ? 0 : st->mask ? st->mask : l->chase ? chase_mask(l) : l->mask;
|
|
if (lit && st->id[0]) {
|
|
if (strcmp(l->last, st->id)) {
|
|
send_presets(p, lane, l->idx, l->idx + 1);
|
|
memcpy(l->last, st->id, ID_MAX);
|
|
}
|
|
select_on(p, lit, st->id);
|
|
}
|
|
if (st->rest || st->others_off || (l->chase && l->chase_off)) {
|
|
select_on(p, l->mask & ~lit, "off");
|
|
}
|
|
}
|
|
|
|
// The sequence's lanes as steps (main's _normalize_sequence_lanes); empty lanes are dropped. Older
|
|
// records' groups become targets: lanes_group_ids[lane], else group_ids, else (single lane) the
|
|
// step's group_ids, else the zone's groups.
|
|
static int load_lanes(player_t *p, const cJSON *seq)
|
|
{
|
|
const cJSON *lanes = cJSON_GetObjectItem(seq, "lanes");
|
|
bool any = false;
|
|
const cJSON *l;
|
|
cJSON_ArrayForEach(l, lanes)
|
|
{
|
|
any = any || cJSON_GetArraySize(l) > 0;
|
|
}
|
|
cJSON *one = NULL;
|
|
if (!any && cJSON_GetArraySize(cJSON_GetObjectItem(seq, "steps"))) {
|
|
one = cJSON_CreateArray();
|
|
cJSON_AddItemReferenceToArray(one, cJSON_GetObjectItem(seq, "steps"));
|
|
lanes = one;
|
|
}
|
|
int nonempty = 0;
|
|
cJSON_ArrayForEach(l, lanes)
|
|
{
|
|
nonempty += cJSON_IsArray(l) && cJSON_GetArraySize(l) > 0;
|
|
}
|
|
const int nuser = p->ntargets;
|
|
const cJSON *lgs = cJSON_GetObjectItem(seq, "lanes_group_ids");
|
|
const cJSON *opts = cJSON_GetObjectItem(seq, "lane_opts");
|
|
// A one-shot is always timed in seconds, whatever its advance_mode or lane modes say
|
|
const bool seq_timed = seq_oneshot(seq) || str_is(seq, "advance_mode", "time");
|
|
int n = 0, src = -1;
|
|
cJSON_ArrayForEach(l, lanes)
|
|
{
|
|
src++;
|
|
if (n == LANES_MAX || !cJSON_IsArray(l) || !cJSON_GetArraySize(l)) {
|
|
continue;
|
|
}
|
|
const cJSON *o = cJSON_GetArrayItem(opts, src);
|
|
lane_t *lane = &p->lane[n];
|
|
memset(lane, 0, sizeof(*lane));
|
|
const cJSON *mode = cJSON_GetObjectItem(o, "mode");
|
|
lane->timed = seq_oneshot(seq) || (cJSON_IsString(mode) ? !strcmp(mode->valuestring, "time") : seq_timed);
|
|
double rate = num(o, "rate", 1);
|
|
rate = rate < 0.25 ? 0.25 : (rate > 4 ? 4 : rate);
|
|
// Targets: the lane's own list, else the older group fields, else the zone's
|
|
lane->mask = mask_of(p, cJSON_GetObjectItem(o, "targets"), nuser);
|
|
bool legacy_steps = false;
|
|
if (!lane->mask) {
|
|
int ti = -1;
|
|
if (cJSON_IsArray(lgs) && n < cJSON_GetArraySize(lgs)) {
|
|
ti = target_from_list(p, cJSON_GetArrayItem(lgs, n));
|
|
} else if (!(nonempty > 1 && cJSON_IsArray(lgs))) {
|
|
ti = target_from_list(p, cJSON_GetObjectItem(seq, "group_ids"));
|
|
legacy_steps = ti < 0 && nonempty == 1;
|
|
}
|
|
lane->mask = bit(ti >= 0 ? ti : target_zone(p));
|
|
}
|
|
for (int i = 0; i < p->ntargets && lane->norder < TARGETS_MAX; i++) {
|
|
if (lane->mask & bit(i)) {
|
|
lane->order[lane->norder++] = i;
|
|
}
|
|
}
|
|
const cJSON *ord = cJSON_GetObjectItem(o, "targets"); // the lane's own order wins for a chase
|
|
if (cJSON_GetArraySize(ord)) {
|
|
lane->norder = 0;
|
|
const cJSON *it;
|
|
cJSON_ArrayForEach(it, ord)
|
|
{
|
|
int i = cJSON_IsNumber(it) ? (int)it->valuedouble : -1;
|
|
if (i >= 0 && i < nuser && lane->norder < TARGETS_MAX) {
|
|
lane->order[lane->norder++] = i;
|
|
}
|
|
}
|
|
}
|
|
const cJSON *chase = cJSON_GetObjectItem(o, "chase");
|
|
if (cJSON_IsObject(chase) && lane->norder > 1) {
|
|
lane->chase = true;
|
|
lane->chase_off = !str_is(chase, "others", "hold");
|
|
lane->corder = str_is(chase, "order", "reverse") ? CHASE_REVERSE
|
|
: str_is(chase, "order", "bounce") ? CHASE_BOUNCE
|
|
: str_is(chase, "order", "random") ? CHASE_RANDOM
|
|
: CHASE_FORWARD;
|
|
int w = (int)num(chase, "width", 1);
|
|
lane->width = w < 1 ? 1 : (w > lane->norder ? lane->norder : w);
|
|
lane->dir = 1;
|
|
lane->pos = lane->corder == CHASE_REVERSE ? lane->norder - 1
|
|
: lane->corder == CHASE_RANDOM ? (int)(esp_random() % (uint32_t)lane->norder)
|
|
: 0;
|
|
}
|
|
double off = num(o, "offset", 0);
|
|
off = off < 0 ? 0 : (off > 3600 ? 3600 : off);
|
|
lane->wait = lane->timed ? 0 : (int)(off * UNITS + 0.5);
|
|
lane->t0 = lane->timed ? (int64_t)(off * 1000000) : 0; // made absolute at the start
|
|
const cJSON *s;
|
|
cJSON_ArrayForEach(s, l)
|
|
{
|
|
if (lane->n == STEPS_MAX || !cJSON_IsObject(s)) {
|
|
continue;
|
|
}
|
|
step_t *st = &lane->step[lane->n++];
|
|
memset(st, 0, sizeof(*st));
|
|
const cJSON *aid = cJSON_GetObjectItem(s, "animation_id");
|
|
if (api_id(aid, st->id, sizeof(st->id))) {
|
|
st->anim = true;
|
|
} else {
|
|
const cJSON *pid = cJSON_GetObjectItem(s, "preset_id");
|
|
api_id(pid ? pid : cJSON_GetObjectItem(s, "presetId"), st->id, sizeof(st->id));
|
|
}
|
|
st->rest = cJSON_IsTrue(cJSON_GetObjectItem(s, "rest"));
|
|
st->others_off = str_is(s, "others", "off");
|
|
double beats = num(s, "beats", 1);
|
|
beats = beats < 0.25 ? 1 : beats;
|
|
double secs = num(s, "seconds", 3);
|
|
secs = secs < 0.1 ? 0.1 : (secs > 3600 ? 3600 : secs);
|
|
if (st->anim && str_is(s, "length", "animation")) {
|
|
seqplay_length(st->id, &beats, &secs);
|
|
}
|
|
int u = (int)(beats * UNITS / rate + 0.5);
|
|
st->units = u < 1 ? 1 : u;
|
|
st->ms = (int)(secs * 1000 / rate);
|
|
st->ms = st->ms < 50 ? 50 : st->ms;
|
|
st->mask = mask_of(p, cJSON_GetObjectItem(s, "targets"), nuser) & lane->mask;
|
|
if (legacy_steps && cJSON_GetArraySize(cJSON_GetObjectItem(s, "group_ids"))) {
|
|
int ti = target_from_list(p, cJSON_GetObjectItem(s, "group_ids"));
|
|
if (ti >= 0) {
|
|
st->mask = bit(ti);
|
|
lane->mask |= bit(ti); // uploads reach it too
|
|
}
|
|
}
|
|
}
|
|
n += lane->n > 0;
|
|
}
|
|
cJSON_Delete(one);
|
|
return n;
|
|
}
|
|
|
|
/* --------------------------------------------------------------- playback */
|
|
|
|
static void anchor_bar(void)
|
|
{
|
|
s_sim_beats = 0; // the next clock beat is a downbeat
|
|
if (audio_drives()) {
|
|
beat_tap(); // the beat just counted is beat 1 of the bar
|
|
}
|
|
}
|
|
|
|
static void clear_drivers(const char *zone)
|
|
{
|
|
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]);
|
|
}
|
|
cJSON *m = message(groups); // never clear every driver in range
|
|
if (m) {
|
|
cJSON_AddTrueToObject(m, "clear_presets");
|
|
cJSON_AddTrueToObject(m, "save");
|
|
send_msg(m);
|
|
}
|
|
}
|
|
|
|
// Every driver the zone reaches gets its combined brightness with the zone's slider value
|
|
static void zone_brightness(const char *zone_id)
|
|
{
|
|
const cJSON *zone = cJSON_GetObjectItem(store_coll("zone"), zone_id);
|
|
const cJSON *zb = cJSON_GetObjectItem(zone, "brightness");
|
|
int zone_b = cJSON_IsNumber(zb) ? (int)zb->valuedouble : 255;
|
|
zone_b = zone_b < 0 ? 0 : (zone_b > 255 ? 255 : zone_b);
|
|
macs_t macs = { 0 };
|
|
char t[ZONE_TARGETS_MAX][GROUP_ID_MAX];
|
|
int n = engine_zone_targets(zone_id, t, ZONE_TARGETS_MAX);
|
|
for (int i = 0; i < n; i++) {
|
|
char m[MACS_MAX][13];
|
|
int k = engine_group_macs(t[i], m, MACS_MAX);
|
|
for (int j = 0; j < k; j++) {
|
|
macs_add(&macs, m[j]);
|
|
}
|
|
}
|
|
for (int i = 0; i < macs.n; i++) {
|
|
cJSON *m = cJSON_CreateObject();
|
|
cJSON_AddStringToObject(m, "v", "1");
|
|
cJSON_AddNumberToObject(m, "b", api_effective_brightness(macs.m[i], zone_b));
|
|
cJSON_AddTrueToObject(m, "save");
|
|
cJSON *messages = cJSON_CreateArray(), *targets = cJSON_CreateArray();
|
|
cJSON_AddItemToArray(messages, m);
|
|
cJSON_AddItemToArray(targets, cJSON_CreateString(macs.m[i]));
|
|
engine_push(messages, targets);
|
|
cJSON_Delete(messages);
|
|
cJSON_Delete(targets);
|
|
}
|
|
}
|
|
|
|
// Load and show a player's sequence. synced: started on a beat, so the sub-beat ticks after it count.
|
|
static esp_err_t load(player_t *p, const char *seq_id, const char *zone_id, const char *profile_id, bool synced,
|
|
const char **msg)
|
|
{
|
|
const cJSON *seq = cJSON_GetObjectItem(store_coll("sequence"), seq_id);
|
|
if (!seq) {
|
|
return ESP_ERR_NOT_FOUND;
|
|
}
|
|
if (!cJSON_GetObjectItem(store_coll("zone"), zone_id)) {
|
|
*msg = "zone not found";
|
|
return ESP_ERR_INVALID_ARG;
|
|
}
|
|
p->active = false;
|
|
snprintf(p->seq, sizeof(p->seq), "%s", seq_id);
|
|
snprintf(p->zone, sizeof(p->zone), "%s", zone_id);
|
|
snprintf(p->profile, sizeof(p->profile), "%s", profile_id);
|
|
load_targets(p, seq);
|
|
p->nlanes = load_lanes(p, seq);
|
|
if (!p->nlanes) {
|
|
*msg = "sequence has no steps";
|
|
return ESP_ERR_INVALID_SIZE;
|
|
}
|
|
// Over the zone (a one-shot, or a clip in another sequence) it always plays once
|
|
p->loop = !p->overlay && !seq_oneshot(seq);
|
|
p->synced = synced;
|
|
p->loop_beat = 0;
|
|
p->lane0_looped = false;
|
|
p->pending_switch = false;
|
|
int64_t now = esp_timer_get_time();
|
|
for (int i = 0; i < p->nlanes; i++) {
|
|
lane_t *l = &p->lane[i];
|
|
l->t0 = now + (l->timed ? l->t0 : 0);
|
|
l->started = l->timed ? l->t0 <= now : l->wait == 0;
|
|
}
|
|
// Step-0 presets of the lanes that start now, their selects, then every other preset (main's
|
|
// _prime_all_lanes)
|
|
for (int i = 0; i < p->nlanes; i++) {
|
|
lane_t *l = &p->lane[i];
|
|
if (l->started) {
|
|
send_presets(p, i, 0, 1);
|
|
snprintf(l->last, ID_MAX, "%s", l->step[0].rest || l->step[0].anim ? "" : l->step[0].id);
|
|
}
|
|
}
|
|
p->active = true;
|
|
for (int i = 0; i < p->nlanes; i++) {
|
|
send_lane(p, i);
|
|
}
|
|
for (int i = 0; i < p->nlanes; i++) {
|
|
lane_t *l = &p->lane[i];
|
|
send_presets(p, i, l->started ? 1 : 0, l->n);
|
|
}
|
|
return ESP_OK;
|
|
}
|
|
|
|
static esp_err_t start_now(const char *seq_id, const char *zone_id, const char *profile_id, bool handoff,
|
|
bool downbeat, const char **msg)
|
|
{
|
|
s_pend.set = false;
|
|
s_done_readout[0] = 0;
|
|
s_base_clear_pending = false;
|
|
esp_err_t err = load(&s_base, seq_id, zone_id, profile_id, handoff, msg);
|
|
if (err != ESP_OK) {
|
|
return err;
|
|
}
|
|
s_base.anchor_next = handoff ? downbeat : true;
|
|
zone_brightness(zone_id);
|
|
ESP_LOGI(TAG, "sequence %s in zone %s: %d lane(s), %d target(s)", seq_id, zone_id, s_base.nlanes,
|
|
s_base.ntargets);
|
|
return ESP_OK;
|
|
}
|
|
|
|
// Drivers a one-shot may light: every target its lanes name
|
|
static void covered_by(const player_t *p, macs_t *out)
|
|
{
|
|
out->n = 0;
|
|
uint32_t mask = 0;
|
|
for (int i = 0; i < p->nlanes; i++) {
|
|
mask |= p->lane[i].mask;
|
|
}
|
|
for (int i = 0; i < p->ntargets; i++) {
|
|
for (int k = 0; (mask & bit(i)) && k < p->target[i].n; k++) {
|
|
char m[MACS_MAX][13];
|
|
int n = engine_group_macs(p->target[i].g[k], m, MACS_MAX);
|
|
for (int j = 0; j < n; j++) {
|
|
macs_add(out, m[j]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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)
|
|
{
|
|
if (!drivers->n) {
|
|
return;
|
|
}
|
|
s_restore = *drivers;
|
|
s_restoring = true;
|
|
if (s_base_clear_pending) {
|
|
s_base_clear_pending = false;
|
|
clear_drivers(s_base.zone);
|
|
} else if (s_base.active) {
|
|
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 zone's last preset, on these drivers only (a pseudo player with one target)
|
|
static target_t t;
|
|
player_t one = { .target = &t, .ntargets = 1 };
|
|
t.n = 0;
|
|
char z[ZONE_TARGETS_MAX][GROUP_ID_MAX];
|
|
int n = engine_zone_targets(zone, z, ZONE_TARGETS_MAX);
|
|
for (int i = 0; i < n && t.n < TGROUPS_MAX; i++) {
|
|
snprintf(t.g[t.n++], GROUP_ID_MAX, "%s", z[i]);
|
|
}
|
|
const char *cur = engine_zone_current(zone);
|
|
if (cur && strcmp(cur, "off")) {
|
|
cJSON *presets = cJSON_CreateObject();
|
|
cJSON *pl = engine_preset_payload(cur);
|
|
if (pl) {
|
|
cJSON_AddItemToObject(presets, cur, pl);
|
|
}
|
|
upload(&one, 1, presets);
|
|
select_on(&one, 1, cur);
|
|
} else {
|
|
select_on(&one, 1, "off");
|
|
}
|
|
}
|
|
s_restoring = false;
|
|
s_restore.n = 0;
|
|
}
|
|
|
|
static void overlay_end(void)
|
|
{
|
|
if (!s_ovl.active) {
|
|
return;
|
|
}
|
|
s_ovl.active = false;
|
|
ESP_LOGI(TAG, "one-shot %s over", s_ovl.seq);
|
|
macs_t was = s_covered;
|
|
s_covered.n = 0;
|
|
restore(&was, s_ovl.zone);
|
|
}
|
|
|
|
// Play sequence seq_id once over whatever runs (a one-shot). A new one replaces the one playing; drivers the new one
|
|
// leaves alone go back to what they showed.
|
|
static void fire(const char *seq_id, const char *zone_id, const char *profile_id, bool synced)
|
|
{
|
|
s_ovl_pend.set = false;
|
|
macs_t was = s_ovl.active ? s_covered : (macs_t){ 0 };
|
|
s_ovl.active = false;
|
|
const char *msg = NULL;
|
|
// Work out what the one-shot covers before it sends anything, so the sequence keeps off those drivers
|
|
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);
|
|
return;
|
|
}
|
|
covered_by(&s_ovl, &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);
|
|
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);
|
|
}
|
|
|
|
// Move a lane on once its step is over
|
|
static void step_lane(player_t *p, int i, int64_t now)
|
|
{
|
|
lane_t *l = &p->lane[i];
|
|
if (l->idx + 1 >= l->n) {
|
|
if (p->loop) {
|
|
if (i == 0) {
|
|
p->lane0_looped = true;
|
|
p->anchor_next = true;
|
|
}
|
|
l->count = 0;
|
|
l->idx = 0;
|
|
l->t0 = now;
|
|
l->last[0] = 0; // step 0 goes up again on each wrap
|
|
if (l->chase) {
|
|
chase_move(l);
|
|
}
|
|
send_lane(p, i);
|
|
} else {
|
|
l->count = l->step[l->idx].units;
|
|
l->done = true;
|
|
}
|
|
} else {
|
|
l->count = 0;
|
|
l->idx++;
|
|
l->t0 = now;
|
|
if (l->chase) {
|
|
chase_move(l);
|
|
}
|
|
send_lane(p, i);
|
|
}
|
|
}
|
|
|
|
static int readout(char *buf, size_t len); // forward
|
|
|
|
static void finish_if_done(player_t *p)
|
|
{
|
|
if (!p->active) {
|
|
return;
|
|
}
|
|
for (int i = 0; i < p->nlanes; i++) {
|
|
if (!p->lane[i].done) {
|
|
return;
|
|
}
|
|
}
|
|
if (p->overlay) {
|
|
overlay_end();
|
|
return;
|
|
}
|
|
readout(s_done_readout, sizeof(s_done_readout));
|
|
p->active = false;
|
|
if (s_ovl.active) {
|
|
s_base_clear_pending = true; // the one-shot still shows; clear once it is over
|
|
} else {
|
|
clear_drivers(p->zone);
|
|
}
|
|
ESP_LOGI(TAG, "sequence %s finished", p->seq);
|
|
}
|
|
|
|
// Time lanes: called often; they move on when their step's time is up
|
|
static void advance_time(player_t *p)
|
|
{
|
|
if (!p->active || p->pending_switch) {
|
|
return;
|
|
}
|
|
int64_t now = esp_timer_get_time();
|
|
for (int i = 0; i < p->nlanes && p->active; i++) {
|
|
lane_t *l = &p->lane[i];
|
|
if (!l->timed || l->done || !l->n) {
|
|
continue;
|
|
}
|
|
if (!l->started) {
|
|
if (now >= l->t0) {
|
|
l->started = true;
|
|
l->t0 = now;
|
|
send_lane(p, i);
|
|
}
|
|
} else if (now - l->t0 >= (int64_t)l->step[l->idx].ms * 1000) {
|
|
step_lane(p, i, now);
|
|
}
|
|
}
|
|
finish_if_done(p);
|
|
}
|
|
|
|
// Beat lanes: one quarter beat. beat: this tick is the beat itself (not a sub-beat tick).
|
|
static void tick(player_t *p, bool beat)
|
|
{
|
|
if (!p->active || p->pending_switch) {
|
|
return;
|
|
}
|
|
if (!p->synced) {
|
|
if (!beat) {
|
|
return; // sub-beat ticks count from the first beat after the start
|
|
}
|
|
// The time from the start to the first beat counts as a beat (as main's first ++count): the
|
|
// quarter beats before this one are added without moving any lane past a step
|
|
p->synced = true;
|
|
for (int i = 0; i < p->nlanes; i++) {
|
|
lane_t *l = &p->lane[i];
|
|
if (l->timed || l->done || !l->n) {
|
|
continue;
|
|
}
|
|
if (l->started) {
|
|
int lim = l->step[l->idx].units - 1;
|
|
l->count = l->count + UNITS - 1 > lim ? lim : l->count + UNITS - 1;
|
|
} else {
|
|
l->wait = l->wait - (UNITS - 1) < 1 ? 1 : l->wait - (UNITS - 1);
|
|
}
|
|
}
|
|
}
|
|
if (beat && !p->overlay) {
|
|
if (p->anchor_next && !p->lane[0].timed) {
|
|
anchor_bar();
|
|
}
|
|
p->anchor_next = false;
|
|
}
|
|
int64_t now = esp_timer_get_time();
|
|
for (int i = 0; i < p->nlanes && p->active; i++) {
|
|
lane_t *l = &p->lane[i];
|
|
if (l->timed || l->done || !l->n) {
|
|
continue;
|
|
}
|
|
if (!l->started) {
|
|
if (--l->wait <= 0) {
|
|
l->started = true;
|
|
l->count = 0;
|
|
send_lane(p, i);
|
|
}
|
|
} else if (++l->count >= l->step[l->idx].units) {
|
|
step_lane(p, i, now);
|
|
}
|
|
}
|
|
if (beat) {
|
|
p->loop_beat = p->lane0_looped ? 1 : p->loop_beat + 1;
|
|
p->lane0_looped = false;
|
|
}
|
|
finish_if_done(p);
|
|
}
|
|
|
|
static void sub_tick(void)
|
|
{
|
|
store_lock();
|
|
tick(&s_base, false);
|
|
tick(&s_ovl, false);
|
|
store_unlock();
|
|
}
|
|
|
|
static void on_beat(bool downbeat)
|
|
{
|
|
int64_t now = esp_timer_get_time();
|
|
if (now - s_last_beat_us < 60LL * 1000000 / BPM_MAX) { // never faster than BPM_MAX
|
|
return;
|
|
}
|
|
// Sub-beat ticks the last beat still owes (the tempo went up) go first, so every beat has UNITS
|
|
while (s_sub < UNITS - 1) {
|
|
s_sub++;
|
|
sub_tick();
|
|
}
|
|
int64_t iv = now - s_last_beat_us;
|
|
s_beat_iv = iv < 60LL * 1000000 / BPM_MAX ? 60LL * 1000000 / BPM_MAX : (iv > 2000000 ? 2000000 : iv);
|
|
s_last_beat_us = now;
|
|
s_sub = 0;
|
|
store_lock();
|
|
bool switched = false;
|
|
if (s_pend.set && (downbeat || !wait_for_downbeat())) {
|
|
const char *msg = NULL;
|
|
char seq[ID_MAX], zone[ID_MAX], profile[ID_MAX];
|
|
snprintf(seq, sizeof(seq), "%s", s_pend.seq);
|
|
snprintf(zone, sizeof(zone), "%s", s_pend.zone);
|
|
snprintf(profile, sizeof(profile), "%s", s_pend.profile);
|
|
if (start_now(seq, zone, profile, true, downbeat, &msg) != ESP_OK) {
|
|
ESP_LOGW(TAG, "queued sequence %s did not start: %s", seq, msg ? msg : "not found");
|
|
}
|
|
switched = true;
|
|
}
|
|
bool fired = false;
|
|
if (s_ovl_pend.set && (downbeat || !s_ovl_pend.downbeat)) {
|
|
char seq[ID_MAX], zone[ID_MAX], profile[ID_MAX];
|
|
snprintf(seq, sizeof(seq), "%s", s_ovl_pend.seq);
|
|
snprintf(zone, sizeof(zone), "%s", s_ovl_pend.zone);
|
|
snprintf(profile, sizeof(profile), "%s", s_ovl_pend.profile);
|
|
fire(seq, zone, profile, true);
|
|
fired = true;
|
|
}
|
|
if (!switched) {
|
|
tick(&s_base, true);
|
|
}
|
|
if (!fired) {
|
|
tick(&s_ovl, true);
|
|
}
|
|
store_unlock();
|
|
}
|
|
|
|
static void beat_task(void *arg)
|
|
{
|
|
QueueHandle_t q = xQueueCreate(8, sizeof(beat_event_t));
|
|
if (!q || beat_subscribe(q) != ESP_OK) {
|
|
ESP_LOGE(TAG, "no beat subscription; the clock alone times sequences");
|
|
}
|
|
int64_t next = esp_timer_get_time();
|
|
for (;;) {
|
|
store_lock();
|
|
advance_time(&s_base);
|
|
advance_time(&s_ovl);
|
|
int64_t interval = 60LL * 1000000 / simulated_bpm();
|
|
store_unlock();
|
|
int64_t now = esp_timer_get_time();
|
|
if (s_sub < UNITS - 1 && now >= s_last_beat_us + (s_sub + 1) * s_beat_iv / UNITS) {
|
|
s_sub++;
|
|
sub_tick();
|
|
continue;
|
|
}
|
|
if (next <= now) { // clock beat, counted only while the mic is not timing
|
|
next = (next + interval > now) ? next + interval : now + interval;
|
|
if (!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
|
|
if (!bench_running()) {
|
|
espnow_tx_beat(60e6f / interval, down, (int)((s_sim_beats - 1) % BEATS_PER_BAR) + 1);
|
|
}
|
|
on_beat(down);
|
|
}
|
|
continue;
|
|
}
|
|
beat_event_t ev;
|
|
int64_t until = next;
|
|
if (s_sub < UNITS - 1) {
|
|
int64_t sub = s_last_beat_us + (s_sub + 1) * s_beat_iv / UNITS;
|
|
until = sub < until ? sub : until;
|
|
}
|
|
int64_t wait_ms = (until - now) / 1000;
|
|
if (wait_ms > 20) { // wake often enough to time steps in seconds
|
|
wait_ms = 20;
|
|
}
|
|
if (q && xQueueReceive(q, &ev, pdMS_TO_TICKS(wait_ms) + 1) == pdTRUE && ev == BEAT_EVENT_BEAT &&
|
|
audio_drives()) {
|
|
beat_info_t bi;
|
|
beat_get_info(&bi);
|
|
on_beat(bi.bar_beat == 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
/* --------------------------------------------------------------------- API */
|
|
|
|
esp_err_t seqplay_init(void)
|
|
{
|
|
player_t *ps[] = { &s_base, &s_ovl };
|
|
for (int i = 0; i < 2; i++) {
|
|
ps[i]->lane = heap_caps_calloc(LANES_MAX, sizeof(lane_t), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
|
ps[i]->target = heap_caps_calloc(TARGETS_MAX, sizeof(target_t), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
|
|
if (!ps[i]->lane || !ps[i]->target) {
|
|
return ESP_ERR_NO_MEM;
|
|
}
|
|
}
|
|
s_ovl.overlay = true;
|
|
store_lock();
|
|
const cJSON *a = cJSON_GetObjectItem(store_settings(), "audio_timing");
|
|
s_audio = !cJSON_IsFalse(a);
|
|
store_unlock();
|
|
espnow_tx_set_mic_beats(s_audio);
|
|
return xTaskCreate(beat_task, "seqplay", 8192, NULL, 4, NULL) == pdPASS ? ESP_OK : ESP_ERR_NO_MEM;
|
|
}
|
|
|
|
bool seqplay_is_oneshot(const char *seq_id)
|
|
{
|
|
store_lock();
|
|
const cJSON *seq = cJSON_GetObjectItem(store_coll("sequence"), seq_id);
|
|
bool one = seq && seq_oneshot(seq);
|
|
store_unlock();
|
|
return one;
|
|
}
|
|
|
|
static bool owned(const char *seq_id, const char *profile_id)
|
|
{
|
|
const cJSON *seq = cJSON_GetObjectItem(store_coll("sequence"), seq_id);
|
|
char owner[ID_MAX];
|
|
return seq && api_id(cJSON_GetObjectItem(seq, "profile_id"), owner, sizeof(owner)) && !strcmp(owner, profile_id);
|
|
}
|
|
|
|
esp_err_t seqplay_start(const char *seq_id, const char *zone_id, const char *profile_id, const char **msg)
|
|
{
|
|
*msg = NULL;
|
|
if (!owned(seq_id, profile_id)) {
|
|
*msg = "sequence not found";
|
|
return ESP_ERR_NOT_FOUND;
|
|
}
|
|
if (!s_base.active) {
|
|
return start_now(seq_id, zone_id, profile_id, false, false, msg);
|
|
}
|
|
// Another sequence plays: switch on the next beat (or downbeat)
|
|
s_pend.set = true;
|
|
snprintf(s_pend.seq, sizeof(s_pend.seq), "%s", seq_id);
|
|
snprintf(s_pend.zone, sizeof(s_pend.zone), "%s", zone_id);
|
|
snprintf(s_pend.profile, sizeof(s_pend.profile), "%s", profile_id);
|
|
s_base.pending_switch = true;
|
|
return ESP_OK;
|
|
}
|
|
|
|
esp_err_t seqplay_fire(const char *seq_id, const char *zone_id, const char *profile_id, const char *wait,
|
|
const char **msg)
|
|
{
|
|
*msg = NULL;
|
|
if (!owned(seq_id, profile_id)) {
|
|
*msg = "sequence not found";
|
|
return ESP_ERR_NOT_FOUND;
|
|
}
|
|
if (!zone_id || !zone_id[0]) {
|
|
zone_id = s_base.active ? s_base.zone : NULL;
|
|
}
|
|
if (!zone_id || !cJSON_GetObjectItem(store_coll("zone"), zone_id)) {
|
|
*msg = "zone not found";
|
|
return ESP_ERR_INVALID_ARG;
|
|
}
|
|
if (wait && (!strcmp(wait, "beat") || !strcmp(wait, "downbeat"))) {
|
|
s_ovl_pend.set = true;
|
|
s_ovl_pend.downbeat = !strcmp(wait, "downbeat") && audio_drives();
|
|
snprintf(s_ovl_pend.seq, sizeof(s_ovl_pend.seq), "%s", seq_id);
|
|
snprintf(s_ovl_pend.zone, sizeof(s_ovl_pend.zone), "%s", zone_id);
|
|
snprintf(s_ovl_pend.profile, sizeof(s_ovl_pend.profile), "%s", profile_id);
|
|
return ESP_OK;
|
|
}
|
|
fire(seq_id, zone_id, profile_id, false);
|
|
if (!s_ovl.active) {
|
|
*msg = "sequence has no steps";
|
|
return ESP_ERR_INVALID_SIZE;
|
|
}
|
|
return ESP_OK;
|
|
}
|
|
|
|
void seqplay_overlay_stop(void)
|
|
{
|
|
store_lock();
|
|
s_ovl_pend.set = false;
|
|
overlay_end();
|
|
store_unlock();
|
|
}
|
|
|
|
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;
|
|
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_covered.n = 0;
|
|
s_base_clear_pending = false;
|
|
bool finished = true;
|
|
for (int i = 0; i < s_base.nlanes; i++) {
|
|
finished = finished && s_base.lane[i].done;
|
|
}
|
|
if (!finished) {
|
|
s_done_readout[0] = 0;
|
|
}
|
|
if (was && clear) {
|
|
clear_drivers(zone);
|
|
}
|
|
store_unlock();
|
|
}
|
|
|
|
void seqplay_end_base_in(const char *zone_id)
|
|
{
|
|
store_lock();
|
|
if (s_pend.set && !strcmp(s_pend.zone, zone_id)) {
|
|
s_pend.set = false;
|
|
s_base.pending_switch = false;
|
|
}
|
|
if (s_base.active && !strcmp(s_base.zone, zone_id)) {
|
|
s_base.active = false;
|
|
s_base_clear_pending = false;
|
|
ESP_LOGI(TAG, "sequence %s in zone %s: a preset took over", s_base.seq, zone_id);
|
|
}
|
|
store_unlock();
|
|
}
|
|
|
|
void seqplay_stop_if(const char *seq_id)
|
|
{
|
|
store_lock();
|
|
bool playing = s_base.active && !strcmp(s_base.seq, seq_id);
|
|
bool anim = s_ovl.active && !strcmp(s_ovl.seq, seq_id);
|
|
store_unlock();
|
|
if (playing) {
|
|
seqplay_stop(true);
|
|
} else if (anim) {
|
|
seqplay_overlay_stop();
|
|
}
|
|
}
|
|
|
|
bool seqplay_sync(const char *mode)
|
|
{
|
|
store_lock();
|
|
bool ok = s_base.active;
|
|
int64_t now = esp_timer_get_time();
|
|
if (ok && (!strcasecmp(mode, "pass") || !strcasecmp(mode, "sequence") || !strcasecmp(mode, "restart"))) {
|
|
for (int i = 0; i < s_base.nlanes; i++) {
|
|
lane_t *l = &s_base.lane[i];
|
|
l->idx = l->count = 0;
|
|
l->done = false;
|
|
l->started = true;
|
|
l->t0 = now;
|
|
}
|
|
s_base.loop_beat = 0;
|
|
for (int i = 0; i < s_base.nlanes; i++) {
|
|
send_lane(&s_base, i);
|
|
}
|
|
} else if (ok) {
|
|
for (int i = 0; i < s_base.nlanes; i++) {
|
|
if (!s_base.lane[i].done) {
|
|
s_base.lane[i].count = 0;
|
|
s_base.lane[i].t0 = now;
|
|
}
|
|
}
|
|
}
|
|
if (ok) {
|
|
anchor_bar();
|
|
}
|
|
store_unlock();
|
|
return ok;
|
|
}
|
|
|
|
void seqplay_length(const char *seq_id, double *beats, double *secs)
|
|
{
|
|
const cJSON *seq = cJSON_GetObjectItem(store_coll("sequence"), seq_id);
|
|
const cJSON *opts = cJSON_GetObjectItem(seq, "lane_opts");
|
|
bool seq_timed = seq_oneshot(seq) || str_is(seq, "advance_mode", "time");
|
|
double b = 0, s = 0;
|
|
int src = -1;
|
|
const cJSON *l;
|
|
cJSON_ArrayForEach(l, cJSON_GetObjectItem(seq, "lanes"))
|
|
{
|
|
src++;
|
|
const cJSON *o = cJSON_GetArrayItem(opts, src);
|
|
const cJSON *mode = cJSON_GetObjectItem(o, "mode");
|
|
bool timed = seq_oneshot(seq) || (cJSON_IsString(mode) ? !strcmp(mode->valuestring, "time") : seq_timed);
|
|
double rate = num(o, "rate", 1);
|
|
rate = rate < 0.25 ? 0.25 : (rate > 4 ? 4 : rate);
|
|
double total = 0;
|
|
const cJSON *st;
|
|
cJSON_ArrayForEach(st, l)
|
|
{
|
|
total += timed ? num(st, "seconds", 3) : num(st, "beats", 1);
|
|
}
|
|
double one = num(o, "offset", 0) + total / rate;
|
|
if (timed) {
|
|
s = one > s ? one : s;
|
|
} else {
|
|
b = one > b ? one : b;
|
|
}
|
|
}
|
|
double beat_s = (double)s_beat_iv / 1e6;
|
|
double bb = b > s / beat_s ? b : s / beat_s, ss = s > b * beat_s ? s : b * beat_s;
|
|
if (bb > 0) {
|
|
*beats = bb;
|
|
*secs = ss;
|
|
}
|
|
}
|
|
|
|
// Lane 0's position in the pass, in beats: so far and per pass
|
|
static void pass_position(int *at, int *per_pass)
|
|
{
|
|
const lane_t *l = &s_base.lane[0];
|
|
int u_at = 0, u_tot = 0;
|
|
for (int i = 0; i < l->n; i++) {
|
|
u_tot += l->step[i].units;
|
|
}
|
|
if (l->done) {
|
|
u_at = u_tot;
|
|
} else {
|
|
for (int i = 0; i < l->idx && i < l->n; i++) {
|
|
u_at += l->step[i].units;
|
|
}
|
|
int bt = l->idx < l->n ? l->step[l->idx].units : 1;
|
|
u_at += l->count < 0 ? 0 : (l->count > bt ? bt : l->count);
|
|
}
|
|
*at = u_at / UNITS;
|
|
*per_pass = (u_tot + UNITS - 1) / UNITS;
|
|
}
|
|
|
|
static int readout(char *buf, size_t len)
|
|
{
|
|
int at, tot;
|
|
buf[0] = 0;
|
|
if (s_base.lane[0].timed) { // seconds, not beats: no n/n readout
|
|
return 0;
|
|
}
|
|
pass_position(&at, &tot);
|
|
if (tot > 0 && s_base.lane[0].done) {
|
|
return snprintf(buf, len, "%d/%d", tot, tot);
|
|
}
|
|
if (tot > 0 && at > 0) { // on the last beat of the pass show n/n
|
|
return snprintf(buf, len, "%d/%d", at == tot - 1 ? tot : (at < tot ? at : tot), tot);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static cJSON *lanes_status(const player_t *p)
|
|
{
|
|
cJSON *arr = cJSON_CreateArray();
|
|
int64_t now = esp_timer_get_time();
|
|
for (int i = 0; i < p->nlanes; i++) {
|
|
const lane_t *l = &p->lane[i];
|
|
cJSON *o = cJSON_CreateObject();
|
|
cJSON_AddNumberToObject(o, "step", l->done ? l->n : l->idx);
|
|
cJSON_AddNumberToObject(o, "steps", l->n);
|
|
cJSON_AddStringToObject(o, "mode", l->timed ? "time" : "beats");
|
|
cJSON_AddBoolToObject(o, "started", l->started);
|
|
cJSON_AddBoolToObject(o, "done", l->done);
|
|
if (l->chase) {
|
|
cJSON_AddNumberToObject(o, "pos", l->order[l->pos % (l->norder ? l->norder : 1)]);
|
|
}
|
|
if (l->idx < l->n) {
|
|
if (l->timed) {
|
|
cJSON_AddNumberToObject(o, "elapsed_ms", l->started ? (double)((now - l->t0) / 1000) : 0);
|
|
cJSON_AddNumberToObject(o, "duration_ms", l->step[l->idx].ms);
|
|
} else {
|
|
cJSON_AddNumberToObject(o, "beat_in_step", (double)l->count / UNITS);
|
|
cJSON_AddNumberToObject(o, "beats_in_step", (double)l->step[l->idx].units / UNITS);
|
|
}
|
|
}
|
|
cJSON_AddItemToArray(arr, o);
|
|
}
|
|
return arr;
|
|
}
|
|
|
|
static cJSON *overlay_status(void)
|
|
{
|
|
cJSON *o = cJSON_CreateObject();
|
|
cJSON_AddBoolToObject(o, "active", s_ovl.active);
|
|
cJSON_AddBoolToObject(o, "pending", s_ovl_pend.set);
|
|
if (s_ovl_pend.set) {
|
|
cJSON_AddStringToObject(o, "pending_id", s_ovl_pend.seq);
|
|
}
|
|
if (s_ovl.active) {
|
|
cJSON_AddStringToObject(o, "sequence_id", s_ovl.seq);
|
|
cJSON_AddStringToObject(o, "zone_id", s_ovl.zone);
|
|
cJSON_AddNumberToObject(o, "drivers", s_covered.n);
|
|
// Progress: the furthest lane, in steps
|
|
double best = 0;
|
|
for (int i = 0; i < s_ovl.nlanes; i++) {
|
|
const lane_t *l = &s_ovl.lane[i];
|
|
double f = l->n ? (l->done ? 1.0 : (double)l->idx / l->n) : 1.0;
|
|
best = f > best ? f : best;
|
|
}
|
|
cJSON_AddNumberToObject(o, "progress", best);
|
|
cJSON_AddItemToObject(o, "lanes", lanes_status(&s_ovl));
|
|
}
|
|
return o;
|
|
}
|
|
|
|
cJSON *seqplay_status(void)
|
|
{
|
|
cJSON *o = cJSON_CreateObject();
|
|
store_lock();
|
|
cJSON_AddItemToObject(o, "overlay", overlay_status());
|
|
if (!s_base.active) {
|
|
cJSON_AddFalseToObject(o, "active");
|
|
cJSON_AddStringToObject(o, "beat_readout", "");
|
|
store_unlock();
|
|
return o;
|
|
}
|
|
const lane_t *l = &s_base.lane[0];
|
|
int total = 0;
|
|
for (int i = 0; i < s_base.nlanes; i++) {
|
|
total += s_base.lane[i].n;
|
|
}
|
|
int at, per_pass;
|
|
pass_position(&at, &per_pass);
|
|
int bps = !l->done && l->idx < l->n ? (l->step[l->idx].units + UNITS - 1) / UNITS : 1;
|
|
int beat_in = l->done ? 0 : (l->count / UNITS > bps ? bps : l->count / UNITS);
|
|
const char *pid = !l->done && l->idx < l->n && !l->step[l->idx].anim ? l->step[l->idx].id : "";
|
|
const cJSON *pdoc = pid[0] ? cJSON_GetObjectItem(store_coll("preset"), pid) : NULL;
|
|
const cJSON *pname = cJSON_GetObjectItem(pdoc, "name");
|
|
char ro[24];
|
|
readout(ro, sizeof(ro));
|
|
cJSON_AddTrueToObject(o, "active");
|
|
cJSON_AddStringToObject(o, "advance_mode", l->timed ? "time" : "beats");
|
|
if (l->timed && !l->done && l->idx < l->n) {
|
|
cJSON_AddNumberToObject(o, "lane0_step_elapsed_ms",
|
|
l->started ? (double)((esp_timer_get_time() - l->t0) / 1000) : 0);
|
|
cJSON_AddNumberToObject(o, "lane0_step_duration_ms", l->step[l->idx].ms);
|
|
}
|
|
cJSON_AddStringToObject(o, "sequence_id", s_base.seq);
|
|
cJSON_AddStringToObject(o, "zone_id", s_base.zone);
|
|
cJSON_AddNumberToObject(o, "num_lanes", s_base.nlanes);
|
|
cJSON_AddNumberToObject(o, "total_sequence_steps", total);
|
|
cJSON_AddNumberToObject(o, "lane0_current_step", l->done ? l->n : l->idx + 1);
|
|
cJSON_AddNumberToObject(o, "lane0_lane_length", l->n);
|
|
cJSON_AddNumberToObject(o, "lane0_beat_in_step", beat_in);
|
|
cJSON_AddNumberToObject(o, "lane0_beats_per_step", bps);
|
|
cJSON_AddStringToObject(o, "lane0_preset_id", pid);
|
|
cJSON_AddStringToObject(o, "lane0_preset_name",
|
|
cJSON_IsString(pname) && pname->valuestring[0] ? pname->valuestring : pid);
|
|
cJSON_AddNumberToObject(o, "sequence_beat_at", at);
|
|
cJSON_AddNumberToObject(o, "sequence_beats_per_pass", per_pass);
|
|
cJSON_AddNumberToObject(o, "sequence_loop_beat", s_base.loop_beat);
|
|
cJSON_AddStringToObject(o, "beat_readout", ro);
|
|
cJSON_AddItemToObject(o, "lanes", lanes_status(&s_base));
|
|
store_unlock();
|
|
return o;
|
|
}
|
|
|
|
cJSON *seqplay_pending(void)
|
|
{
|
|
cJSON *o = cJSON_CreateObject();
|
|
store_lock();
|
|
cJSON_AddBoolToObject(o, "pending", s_pend.set);
|
|
if (s_pend.set) {
|
|
cJSON_AddStringToObject(o, "wait_for", wait_for_downbeat() ? "downbeat" : "beat");
|
|
cJSON_AddStringToObject(o, "sequence_id", s_pend.seq);
|
|
cJSON_AddStringToObject(o, "zone_id", s_pend.zone);
|
|
}
|
|
store_unlock();
|
|
return o;
|
|
}
|
|
|
|
const char *seqplay_done_readout(void)
|
|
{
|
|
return s_done_readout;
|
|
}
|
|
|
|
void seqplay_set_audio(bool on)
|
|
{
|
|
store_lock();
|
|
s_audio = on;
|
|
espnow_tx_set_mic_beats(on);
|
|
api_set(store_settings(), "audio_timing", cJSON_CreateBool(on));
|
|
store_save("settings");
|
|
store_unlock();
|
|
}
|
|
|
|
bool seqplay_audio(void)
|
|
{
|
|
return s_audio;
|
|
}
|
|
|
|
const char *seqplay_switch_wait(void)
|
|
{
|
|
store_lock();
|
|
bool down = wait_for_downbeat();
|
|
store_unlock();
|
|
return down ? "downbeat" : "beat";
|
|
}
|
|
|
|
void seqplay_clock(int *bar_beat, uint32_t *tick)
|
|
{
|
|
*tick = s_sim_beats;
|
|
*bar_beat = s_sim_beats ? (int)((s_sim_beats - 1) % BEATS_PER_BAR) + 1 : 1;
|
|
}
|