/* SPDX-License-Identifier: GPL-3.0-only * Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments * Portions Copyright (C) 2026 Juan Ignacio Antonucci (Juan Jiant) */ /* Editor protocol: SysEx for the web editor (web/EDITOR_PROTOCOL.md; v2 = user presets + live sync, * v3 = four tracks: the v1 / v2 commands act on the selected track, cmds 27-30 reach any track; * v4 = TRACK_PARAM (31) and the TRACK_CHANGED push (32), enabled by WATCH bit 1). * F0 7D 46 4C cmd args.. F7 (7D = non-commercial ID, "FL") * Values are 14 bit, two 7-bit bytes LSB first, offset by 8192 (so -8192..8191). * Every request gets a reply with the same cmd; 23/24/26 are also pushed * while watched. Frames arrive through sx_frame (usb.c), replies leave * through ota_wire_send(). */ #define ED_HDR0 0x7D #define ED_HDR1 0x46 #define ED_HDR2 0x4C enum { ED_INFO = 1, ED_GET, ED_SET, ED_DUMP, ED_DESC, ED_STEP_GET, ED_STEP_SET, ED_PRESET, ED_PROJECT, ED_NAMES, ED_UP_LIST = 16, ED_UP_GET, ED_UP_PUT, ED_UP_STORE, ED_UP_LOAD, ED_UP_ERASE, /* v2: user presets */ ED_WATCH, ED_CHANGED, ED_RELOAD, ED_PING, ED_STEP_CHANGED, /* v2: live sync */ ED_TRACK, ED_TRACK_MIX, ED_TRACK_DUMP, ED_TRACK_STEP, /* v3: tracks */ ED_TRACK_PARAM, ED_TRACK_CHANGED, ED_SONG, ED_UI_STATE, ED_UI_SET, ED_UI_PALETTES, ED_FAV_GET, ED_FAV_SET, ED_LYRIC, /* (JIANT 0.8.5) VOICE's lyric */ ED_MOTION = 64, ED_BACKUP_LIST, ED_BACKUP_GET, ED_BACKUP_PUT }; /* v6: song chain */ /* (SMK-37, ports/) INFO's 'D' tag: which device answers, so the editor can say so (an FM-1 sends no tag: its * binary stays as it was). 1 SMK-37 Pro, 2 SMK-37 Elite, 3 a computer (a plugin, the standalone app), 4 the MPC Sample * app, 5 the MPC plugin; the core's host names itself (ports/core jiant_editor_host). 'H' (big CPUs only): bit 0 * SAMPLE (engine 4), bit 1 GRAIN (engine 8), bit 2 DRUM-X's sample layer, bit 3 the values beyond EDIT's eight that * DESC names by the track's engine (engines.c hp_xdesc) */ #if BOARD_SMK37 #define ED_BOARD (SMK_MODEL[7] == 'E' ? 2u : 1u) #elif FELUCCA_HOST_POWER static uint8_t ed_board = 3u; #define ED_BOARD ed_board #endif #define ED_HP_CAPS 15u static uint8_t ed_out[600]; static uint32_t ed_n; static void ed_begin(uint32_t cmd) { ed_out[0] = 0xF0; ed_out[1] = ED_HDR0; ed_out[2] = ED_HDR1; ed_out[3] = ED_HDR2; ed_out[4] = (uint8_t)cmd; ed_n = 5; } static void ed_b(uint32_t v) { if (ed_n < sizeof ed_out - 1u) ed_out[ed_n++] = (uint8_t)(v & 0x7Fu); } static void ed_v(int32_t v) { uint32_t u = (uint32_t)(clamp(v, -8192, 8191) + 8192); ed_b(u); ed_b(u >> 7); } static void ed_str(const char *s, uint32_t max) /* ASCII, 0-terminated */ { uint32_t i; for (i = 0; s && s[i] && i < max; i++) ed_b((uint8_t)s[i] & 0x7Fu); ed_b(0); } static void ed_send(void) { ed_out[ed_n++] = 0xF7; ota_wire_send(ed_out, ed_n); } static int32_t ed_rv(const uint8_t *p) { return (int32_t)(p[0] | p[1] << 7) - 8192; } /* 7-bit packing (the backup's PUT): groups of a msb byte, then up to 7 bytes */ static uint32_t ed_unpack7(const uint8_t *a, uint32_t na, uint8_t *out, uint32_t max) { uint32_t n = 0; /* groups: msb byte, then up to 7 bytes */ while (na && n < max) { uint32_t m = *a++, j; na--; uint32_t k = na > 7u ? 7u : na; if (!k || n + k > max || (m >> k)) return 0; /* incomplete group, overflow, or unused mask bits */ for (j = 0; j < k; j++, na--) out[n++] = (uint8_t)(*a++ | ((m >> j) & 1u) << 7); } return na ? 0u : n; } /* the engine byte of DUMP / RELOAD / TRACK */ static uint32_t ed_eng(const track_t *t) { return trk_eng(t); } /* ---- live sync (v2): while the editor WATCHes, device-side changes are pushed. * Shadows of the selected track's p[] + song.g[] and of its steps are kept in step * with what the editor knows (its own SET / STEP_SET update them); the main loop * compares and pushes CHANGED / STEP_CHANGED, or RELOAD after a load or when another * track was selected (v3: RELOAD and STEP_CHANGED carry the selected track). Pushes * go out only into a half-empty SysEx ring, so they never wait. */ #define ED_PUSH_MAX 4u /* frames per pass */ #define ED_NV (P_COUNT + G_COUNT) /* v4: the parameters of the tracks that are not selected which TRACK_CHANGED follows (the mixer) */ static const uint8_t ED_TIDS[3] = {P_LEVEL, P_PAN, P_MUTE}; #define ED_NT (NTRK * 3u) static struct { uint8_t on, eng, preset, sel; uint8_t v4; /* WATCH bit 1: TRACK_CHANGED pushes too */ uint32_t pos, last_ms, run_ms, resets; int16_t v[ED_NV]; /* TSEL->p[], then song.g[] */ uint16_t t[ED_NV]; /* ms (low 16 bits) of the last push */ uint32_t st[NSTEP]; /* step signatures */ int16_t tv[ED_NT]; /* v4: trk[k].p[ED_TIDS[j]] at k * 3 + j */ uint16_t tt[ED_NT]; uint32_t tpos; } ed_w; static int16_t *ed_val(uint32_t i) { return i < P_COUNT ? &TSEL->p[i] : &song.g[i - P_COUNT]; } static uint32_t ed_step_sig(const step_t *s) { return ((uint32_t)s->note[0] | (uint32_t)s->note[1] << 7 | (uint32_t)s->note[2] << 14 | (uint32_t)s->note[3] << 21) ^ ((uint32_t)s->n << 28) ^ ((uint32_t)s->time * 0x9E3779B1u) ^ ((uint32_t)s->flags * 0x85EBCA6Bu) ^ ((uint32_t)s->vel * 0xC2B2AE35u) ^ ((uint32_t)(s->hit | s->acc << 8) * 0x27D4EB2Fu) ^ ((uint32_t)s->probability * 0x165667B1u); } static void ed_shadow(void) /* the editor is in sync */ { uint32_t i; for (i = 0; i < ED_NV; i++) ed_w.v[i] = *ed_val(i); for (i = 0; i < NSTEP; i++) ed_w.st[i] = ed_step_sig(&seq_steps(TSEL)[i]); for (i = 0; i < ED_NT; i++) ed_w.tv[i] = trk[i / 3u].p[ED_TIDS[i % 3u]]; ed_w.eng = (uint8_t)ed_eng(TSEL); ed_w.preset = TSEL->preset; ed_w.sel = song.sel; sync_reload = 0; } /* the editor's own sound load on the selected track (PRESET, SET of G_ENGSEL): the editor re-reads DUMP after * the reply, so the shadow takes the load (engine, preset, the parameters it changed) and no RELOAD echoes back * (INFO 53 01 bit 1). A RELOAD already due before it (a load or selection on the device) still goes out. */ typedef struct { int16_t p[P_COUNT]; uint8_t eng, preset, due; } ed_load_t; static void ed_load_before(ed_load_t *b) { memcpy(b->p, TSEL->p, sizeof b->p); b->eng = (uint8_t)ed_eng(TSEL); b->preset = TSEL->preset; b->due = sync_reload || b->eng != ed_w.eng || b->preset != ed_w.preset || song.sel != ed_w.sel; } static void ed_load_after(const ed_load_t *b) { uint32_t i; if (!ed_w.on || b->due) return; sync_reload = 0; ed_w.eng = (uint8_t)ed_eng(TSEL); ed_w.preset = TSEL->preset; for (i = 0; i < P_COUNT; i++) /* the load's values; a pending push of another stays */ if (TSEL->p[i] != b->p[i]) ed_w.v[i] = TSEL->p[i]; } static int ed_room(void) { return so_w - so_r + 8u <= SXQ / 2u; } static void ed_known(uint32_t k, uint32_t id) /* the editor's own change of trk[k].p[id]: no push */ { uint32_t j; if (k == song.sel) { ed_w.v[id] = trk[k].p[id]; return; } for (j = 0; j < 3u; j++) if (ED_TIDS[j] == id) ed_w.tv[k * 3u + j] = trk[k].p[id]; } static void ed_sync(void) /* main loop */ { uint32_t i, n = 0, now = fm1_ms; if (!ed_w.on || now - ed_w.run_ms < 5u) return; ed_w.run_ms = now; if (!usb.config || usb.resets != ed_w.resets || now - ed_w.last_ms > 3000u) { ed_w.on = 0; /* no host, USB reset, or 3 s without a request */ return; } if (sync_reload || ed_eng(TSEL) != ed_w.eng || TSEL->preset != ed_w.preset || song.sel != ed_w.sel) { if (!ed_room()) return; ed_shadow(); ed_begin(ED_RELOAD); ed_b(ed_eng(TSEL)); ed_b(TSEL->preset); ed_b(song.sel); ed_send(); return; } for (i = 0; i < NSTEP && n < ED_PUSH_MAX; i++) { uint32_t h = ed_step_sig(&seq_steps(TSEL)[i]); if (h == ed_w.st[i]) continue; if (!ed_room()) return; ed_w.st[i] = h; ed_begin(ED_STEP_CHANGED); ed_b(i); ed_b(song.sel); ed_send(); n++; } for (i = 0; i < ED_NV && n < ED_PUSH_MAX; i++) { /* round robin: nothing starves */ uint32_t k = (ed_w.pos + i) % ED_NV; int16_t v = *ed_val(k); if (v == ed_w.v[k] || (uint16_t)(now - ed_w.t[k]) < 20u) continue; /* coalesced: the latest value goes out later */ if (!ed_room()) return; ed_w.v[k] = v; ed_w.t[k] = (uint16_t)now; ed_begin(ED_CHANGED); ed_b(k < P_COUNT ? 0u : 1u); ed_b(k < P_COUNT ? k : k - P_COUNT); ed_v(v); ed_send(); n++; ed_w.pos = k + 1u; } for (i = 0; ed_w.v4 && i < ED_NT && n < ED_PUSH_MAX; i++) { /* v4: the other tracks' mix */ uint32_t k = (ed_w.tpos + i) % ED_NT, tr = k / 3u, id = ED_TIDS[k % 3u]; int16_t v = trk[tr].p[id]; if (tr == song.sel || v == ed_w.tv[k] || (uint16_t)(now - ed_w.tt[k]) < 20u) continue; /* (the selected track: CHANGED above) */ if (!ed_room()) return; ed_w.tv[k] = v; ed_w.tt[k] = (uint16_t)now; ed_begin(ED_TRACK_CHANGED); ed_b(tr); ed_b(id); ed_v(v); ed_send(); n++; ed_w.tpos = k + 1u; } } /* descriptor of parameter id of track t: the engine parameters of the engine it asked for * (t->engine follows in the audio ISR, after a short fade) */ static const param_desc_t *ed_tdesc(const track_t *t, uint32_t id) { #if FELUCCA_HOST_POWER /* (ports/) SAMPLE's and GRAIN's values beyond EDIT, DRUM-X's sample layer: in other engines' slots, named by the * track's engine (engines.c hp_xdesc), so DESC, SET's clamp and TRACK_PARAM take them as the device's pages do */ const param_desc_t *x = id < P_COUNT ? hp_xdesc(t, id) : 0; if (x) return x; #endif return param_desc_of(trk_eng(t), id); } /* descriptor and value slot of (scope, id): scope 0 = the selected track, 1 = global */ static const param_desc_t *ed_desc(uint32_t scope, uint32_t id, int16_t **vp) { if (scope == 0 && id < P_COUNT) { *vp = &TSEL->p[id]; return ed_tdesc(TSEL, id); } if (scope == 1 && id < G_COUNT) { *vp = &song.g[id]; return &GP[id]; } return 0; } /* a step as STEP_SET / TRACK_STEP send it (na bytes): n, 4 notes, time, flags, vel, then (v5) the lane hits * and their accents: hit & 127, acc & 127, bit 7 of each (bit 0 hit, bit 1 acc), (v7) the chance, (RATCH, INFO * 52 01) the hits 1..4; each value made valid. 8 bytes (an editor of before the grid): the hits stay; without * the chance or the ratchet the step keeps its own (flags carry accent and slide only) */ static void ed_step_put(step_t *st, const uint8_t *a, uint32_t na) { uint32_t i; st->n = (uint8_t)(a[0] > 4u ? 4u : a[0]); for (i = 0; i < 4u; i++) st->note[i] = a[1 + i] & 0x7Fu; st->time = (uint8_t)(a[5] > ST_REST ? ST_REST : a[5]); st->flags = (uint8_t)((a[6] & (SF_ACCENT | SF_SLIDE)) | (st->flags & (SF_RATCH | SF_MFINE))); /* (0.9.1: its MICRO kept) */ st->vel = a[7] & 0x7Fu; if (na >= 11u) { st->hit = (uint8_t)((a[8] & 0x7Fu) | (a[10] & 1u) << 7); st->acc = (uint8_t)(((a[9] & 0x7Fu) | (a[10] & 2u) << 6) & st->hit); } if (na >= 12u) step_set_chance(st, a[11] <= 100u ? a[11] : 100u); if (na >= 13u) step_set_ratchet(st, a[12]); if (na >= 21u) /* (0.8.5, INFO 4F 01 07) each lane's mode, 0..6 (core.h LM_*) */ for (i = 0; i < NLANE; i++) step_set_lmode(st, i, a[13 + i] < LM_N ? a[13 + i] : LM_NONE); ui.force = 1; } static void ed_step_reply(const step_t *st, int lanes) /* the same 13 bytes; (0.8.5) then the 8 lanes' modes, when the * request asked for them (a write with them, a read with its * flag 1): every reply of before as it was */ { uint32_t i; ed_b(st->n); for (i = 0; i < 4u; i++) ed_b(st->note[i]); ed_b(st->time); ed_b(st->flags & (SF_ACCENT | SF_SLIDE)); ed_b(st->vel); ed_b(st->hit & 0x7Fu); ed_b(st->acc & 0x7Fu); ed_b((uint32_t)(st->hit >> 7) | (uint32_t)(st->acc >> 7) << 1); ed_b(step_chance(st)); ed_b(step_ratchet(st)); for (i = 0; lanes && i < NLANE; i++) ed_b(step_lmode(st, i)); } /* a flash write from the editor while the transport runs: stop it first (the erase silences the audio and * stalls the sequencer; the device's own SAVE refuses with "STOP TO SAVE", the editor's confirmed save stops) */ static int ed_flash_stop(void) { uint32_t t0 = fm1_ms; if (!transport_busy()) return 0; transport_req = 2; while ((song.playing || chain_busy()) && fm1_ms - t0 < 100u) /* the audio ISR stops it at its next block */ fm1_wdt_feed(); return song.playing || chain_busy(); } #include "editor_preferences.c" #include "editor_backup.c" #include "editor_fm6.c" #include "editor_menu.c" #include "editor_screen.c" /* the screen mirror (78, 79) */ /* MOTION's reply: the records of track k as (step, id, v14), a lock's id without its MOTION_LOCK bit (a 7-bit SysEx * byte); kinds (1.1: the ops 5..7): then one byte per record, in the same order, 0 automation, 1 lock. The query and * the ops 1..4 reply as before 1.1 (an editor of before reads a lock as an automation event) */ static void ed_motion_reply(uint32_t k, uint32_t rc, uint32_t kinds) { track_t *t = &trk[k]; uint32_t i; ed_b(k); ed_b(rc); ed_b(motion_enabled(t)); ed_b(motion_count(t)); ed_b(MOTION_MAX); for (i = 0; i < motion.count; i++) { const motion_event_t *e = &motion.event[i]; if (MOTION_TRK(e) != k) continue; ed_b(MOTION_STEP(e)); ed_b(MOTION_ID(e)); ed_v(MOTION_VAL(e)); } for (i = 0; kinds && i < motion.count; i++) if (MOTION_TRK(&motion.event[i]) == k) ed_b((motion.event[i].param & MOTION_LOCK) != 0u); } static int ed_lm_ok(const uint8_t *a) /* (0.8.5) eight lane modes, each 0..6 */ { uint32_t i; for (i = 0; i < NLANE; i++) if (a[i] >= LM_N) return 0; return 1; } static int ed_args_ok(uint32_t cmd, const uint8_t *a, uint32_t n) { switch (cmd) { case ED_INFO: case ED_DUMP: case ED_PING: case ED_UI_STATE: case ED_UI_PALETTES: return !n; case ED_GET: case ED_DESC: case ED_PRESET: case ED_PROJECT: case ED_UP_LIST: case ED_TRACK_PARAM: return n == 2u || (cmd == ED_TRACK_PARAM && n == 4u); case ED_SET: return n == 4u; case ED_STEP_GET: return n == 1u || (n == 2u && a[1] == 1u); /* (0.8.5: 1, the lanes' modes too) */ case ED_NAMES: case ED_UP_GET: case ED_UP_LOAD: case ED_UP_ERASE: case ED_WATCH: case ED_TRACK_DUMP: case ED_MENU_DESC: return n == 1u; case ED_MENU_SET: return n == 3u; case ED_STEP_SET: return n == 9u || n == 12u || (n == 13u && a[12] <= 100u) || /* notes only, or the complete grid extension, */ (n == 14u && a[12] <= 100u && a[13] >= 1u && a[13] <= 4u) || /* its chance, its ratchet, */ (n == 22u && a[12] <= 100u && a[13] >= 1u && a[13] <= 4u && ed_lm_ok(a + 14)); /* (0.8.5) the lanes' modes */ case ED_TRACK: return n <= 1u; case ED_TRACK_MIX: return n == 1u || n == 4u; case ED_TRACK_STEP: return n == 2u || (n == 3u && a[2] == 1u) || n == 10u || n == 13u || (n == 14u && a[13] <= 100u) || (n == 15u && a[13] <= 100u && a[14] >= 1u && a[14] <= 4u) || (n == 23u && a[13] <= 100u && a[14] >= 1u && a[14] <= 4u && ed_lm_ok(a + 15)); case ED_MOTION: return (n == 1u || (n == 2u && (a[1] == 2u || a[1] == 7u)) || (n == 3u && a[1] == 1u && a[2] <= 1u) || (n == 6u && (a[1] == 3u || a[1] == 5u)) || (n == 4u && a[1] == 4u) || (n == 3u && a[1] == 6u)) && a[0] < NTRK; case ED_SONG: return n && (a[0] == 1u || n == 1u); default: return 1; /* variable payloads validate in their handler */ } } static void ed_handle(const uint8_t *f, uint32_t n) /* f: the bytes between F0 and F7 */ { if (n < 4u) return; uint32_t cmd = f[3], i; const uint8_t *a = f + 4; uint32_t na = n - 4u; int16_t *vp; const param_desc_t *d; if (!ed_args_ok(cmd, a, na)) return; if (cmd >= ED_BACKUP_LIST && cmd <= ED_BACKUP_PUT) autosave_hold(); /* (a transfer: no autosave meanwhile, project.c) */ ed_begin(cmd); if (ed_ui_handle(cmd, a, na)) { ed_send(); return; } if (ed_backup_handle(cmd, a, na)) { ed_send(); return; } if (ed_fm6_handle(cmd, a, na)) { ed_send(); return; } if (ed_menu_handle(cmd, a, na)) { ed_send(); return; } if (ed_screen_handle(cmd, a, na)) return; /* (it sends its own reply: editor_screen.c) */ switch (cmd) { case ED_MOTION: { track_t *t = &trk[a[0]]; uint32_t rc = 0; if (na > 1u && a[1] == 7u) { /* (the query with the kinds) */ } else if (na > 1u && chain_busy()) rc = 3; else if (na > 1u) { if (a[1] == 1u) motion_set_enabled(t, a[2]); else if (a[1] == 2u) { load_begin(t, LOAD_PAT); motion_clear(t); load_end(t); } else if (a[1] == 3u) rc = motion_set_event(t, a[2], a[3], (int16_t)ed_rv(a + 4)); else if (a[1] == 4u && a[2] < NSTEP && motion_param(a[3])) motion_delete_event(t, a[2], a[3]); else if (a[1] == 5u) rc = motion_set_lock(t, a[2], a[3], (int16_t)ed_rv(a + 4)); else if (a[1] == 6u && (a[2] < NSTEP || a[2] == 127u)) (void)motion_clear_locks(t, a[2]); else rc = 1; ui.force = 1; } ed_motion_reply(a[0], rc, na > 1u && a[1] >= 5u); break; } case ED_INFO: ed_str("JIANT " FELUCCA_VERSION, 24); /* (0.7.0: was "FELUCCA v..": the web tools take both) */ ed_b(NENGINES); ed_b(P_COUNT); ed_b(G_COUNT); ed_b(NSTEP); ed_b(P_E0); for (i = 0; i < NENGINES; i++) ed_str(ENGINES[i]->name, 8); ed_b(NTRK); /* v3 */ ed_b(CHAIN_ROWS); /* v6 */ ed_b(0x55); ed_b(1); ed_b(ed_ui_caps()); /* tagged preferences v1: commands 34..38 */ ed_b(0x4d); ed_b(1); ed_b(MOTION_MAX); ed_b(1); /* motion + chance v1 */ ed_b(0x42); ed_b(1); ed_b(3); /* bounded full-backup read + restore */ ed_b(0x46); ed_b(1); ed_b(FM6_NFACTORY); ed_b(0); /* FM6 patches: cmds 68..71 (no bank since 1.0.3) */ ed_b(0x53); ed_b(1); ed_b(3); /* live sync: bit 0 WATCH while on keeps the shadow, bit 1 no RELOAD echo */ ed_b(0x50); ed_b(1); ed_b(3); /* FM6 patches v2: bit 0 no bank (SLOT F1..F8, 8 OWN), bit 1 user preset * patches (FM6 target 3, backup id 9) */ ed_b(0x4E); ed_b(1); ed_b(ED_MENU_N); /* MENU settings: cmds 72, 73; the items MENU_DESC offers */ ed_b(0x52); ed_b(1); ed_b(4); /* RATCH: a step's ratchet (1..4 hits) after its chance */ ed_b(0x4C); ed_b(1); ed_b(1); /* 1.1 parameter locks: MOTION ops 5..7, the kinds after the records */ ed_b(0x56); ed_b(1); ed_b(FM6_NBANK); /* (JIANT 0.6.11, Felucca 1.4.1) FM6's voice bank: cmds 74..77, SLOT 9..40 */ ed_b(0x4A); ed_b(1); ed_b(1); /* (JIANT 0.6.62) 'J': this is JIANT (the web installer writes over it, web/index_pkg.html) */ ed_b(0x4F); ed_b(1); ed_b(LM_N); /* (0.8.5) 'O': the DRUM-X octaves, a step's lane modes 0..6 after its ratchet */ #if BOARD_SMK37 || FELUCCA_HOST_POWER ed_b(0x44); ed_b(1); ed_b(ED_BOARD); /* 'D': the device answering (no tag: an FM-1); EDITOR_PROTOCOL.md "Device" */ #endif #if FELUCCA_HOST_POWER ed_b(0x48); ed_b(2); ed_b(ED_HP_CAPS); ed_b(SMP_NSLOT); /* 'H': a big CPU's engines (ports/): SAMPLE, GRAIN, DRUM-X's * samples and their values beyond EDIT; the slots */ #endif ed_b(0x59); ed_b(1); ed_b(LYR_LEN - 1u); /* (JIANT 0.8.5) 'Y': LYRIC (cmd 39), a track's lyric, at most 63 bytes */ break; case ED_GET: case ED_SET: if (na < 2u || !(d = ed_desc(a[0], a[1], &vp))) return; if (cmd == ED_SET && na >= 4u && !(chain_busy() && !a[0] && a[1] >= P_SLEN && a[1] <= P_SGATE)) { if (a[0] == 1 && a[1] == G_ENGSEL) { /* engine change: the safe path (1 without FELUCCA_FM4: * DIGITAL's first preset, as FM6) */ ed_load_t b; ed_load_before(&b); set_engine((uint32_t)clamp(ed_rv(a + 2), 0, NENGINES - 1)); ed_load_after(&b); } else if (a[0] == 1 && a[1] == G_DJF) { /* (0.9.1) the DJ FILTER: recorded as its knob is */ motion_djf_set(TSEL, (int16_t)clamp(ed_rv(a + 2), d->min, d->max)); } else if (d->max > d->min) { *vp = (int16_t)enum_orig(d, clamp(ed_rv(a + 2), d->min, d->max)); if (a[0] == 0) (void)motion_capture(TSEL, a[1], *vp); } ui.force = 1; ed_w.v[a[0] ? P_COUNT + a[1] : a[1]] = *vp; /* the editor's own change: no push */ } ed_b(a[0]); ed_b(a[1]); ed_v(*vp); break; case ED_DUMP: for (i = 0; i < ED_NV; i++) /* the editor gets them all here */ ed_w.v[i] = *ed_val(i); ed_b(ed_eng(TSEL)); ed_b(TSEL->preset); for (i = 0; i < P_COUNT; i++) ed_v(motion_base_value(TSEL, i)); for (i = 0; i < G_COUNT; i++) ed_v(song.g[i]); break; case ED_DESC: if (na < 2u || !(d = ed_desc(a[0], a[1], &vp))) return; ed_b(a[0]); ed_b(a[1]); ed_b(d->fmt); ed_v(d->min); ed_v(d->max); ed_v(d->def); ed_str(d->label, 8); ed_str(d->unit, 8); if (d->fmt == F_ENUM && d->names) /* up to 32 (0.7.0: the matrix DST has 30; was 24) */ for (i = 0; i <= (uint32_t)(d->max - d->min) && i < 32u; i++) #if FELUCCA_HOST_POWER ed_str(d->names[d->min + (int32_t)i], 8); /* (ports/) names by value, as param_format: DRUM-X's SLOT 112.. */ #else ed_str(d->names[i], 8); #endif break; case ED_STEP_GET: case ED_STEP_SET: { step_t *st; if (na < 1u || a[0] >= NSTEP) return; st = &TSEL->step[a[0]]; if (cmd == ED_STEP_SET && na >= 9u && !chain_busy()) { ed_step_put(st, a + 1, na - 1u); } ed_w.st[a[0]] = ed_step_sig(&seq_steps(TSEL)[a[0]]); ed_b(a[0]); ed_step_reply(&seq_steps(TSEL)[a[0]], na == 22u || (cmd == ED_STEP_GET && na == 2u)); break; } case ED_PRESET: { /* engine, preset */ ed_load_t b; if (na < 2u || a[0] >= NENGINES) return; ed_load_before(&b); #if !FELUCCA_FM4 if (a[0] == ENGI_DIGITAL) /* a DIGITAL preset (retired): its sound, as FM6 */ fm4_load_preset(TSEL, a[1]); else #endif { if (a[0] != TSEL->eng_req) set_engine(a[0]); apply_preset(a[1]); } ed_load_after(&b); ui.force = 1; ed_b(ed_eng(TSEL)); ed_b(TSEL->preset); break; } case ED_PROJECT: /* 0 = load, 1 = save, 2 = query; slot 0..3 */ if (na < 2u || a[0] > 2u || a[1] >= 4u) return; if (a[0] == 1u) { if (ed_flash_stop() || project_save(a[1])) return; /* the legacy reply has no failure bit: do not confirm a failed save */ } else if (a[0] == 0u) project_load(a[1] & 3u); ed_b(a[0]); ed_b(a[1] & 3u); ed_b(project_used(a[1] & 3u)); break; case ED_NAMES: /* preset names of an engine */ if (na < 1u || a[0] >= NENGINES) return; ed_b(a[0]); ed_b(ENGINES[a[0]]->npresets); for (i = 0; i < ENGINES[a[0]]->npresets; i++) ed_str(ENGINES[a[0]]->presets[i].name, 12); for (i = 0; i < 2u; i++) /* then the two edit-page titles */ ed_str(ENGINES[a[0]]->page_title[i], 8); break; case ED_UP_LIST: { /* start, count -> start, count, total, per slot: used, engine, name */ uint32_t s0, cnt; if (na < 2u) return; s0 = a[0]; cnt = a[1] > 16u ? 16u : a[1]; if (s0 >= UP_SLOTS) cnt = 0; else if (s0 + cnt > UP_SLOTS) cnt = UP_SLOTS - s0; ed_b(s0); ed_b(cnt); ed_b(UP_SLOTS); for (i = s0; i < s0 + cnt; i++) { char nm[13] = {0}; uint32_t j, u = (uint32_t)up_used(i); for (j = 0; u && j < 12u; j++) nm[j] = up_rec(i)->name[j]; ed_b(u); ed_b(u ? up_rec(i)->engine : 0u); ed_str(nm, 12); } break; } case ED_UP_GET: { /* slot -> slot, used, engine, name, P_COUNT x v14, 16 x (note, * flags), (v5) kind [, 16 x hi] (upreset.c up_parse) */ int16_t v[P_COUNT]; char nm[13] = {0}; const up_rec_t *r; uint32_t u; if (na < 1u || a[0] >= UP_SLOTS) return; r = up_rec(a[0]); u = (uint32_t)up_used(a[0]); if (u) up_values(r, v); /* today's P_* order, clamped */ for (i = 0; u && i < 12u; i++) nm[i] = r->name[i]; ed_b(a[0]); ed_b(u); ed_b(u ? r->engine : 0u); ed_str(nm, 12); for (i = 0; i < P_COUNT; i++) ed_v(u ? v[i] : 0); for (i = 0; i < 16u; i++) { ed_b(u ? r->note[i] & 0x7Fu : 0u); ed_b(u ? r->flags[i] & 0x7Fu : 0u); } ed_b(u && up_grid(r)); /* kind: 1 = a drum grid, then bit 7 of each step's */ for (i = 0; u && up_grid(r) && i < 16u; i++) /* hits (bit 0) and accents (bit 1) */ ed_b((uint32_t)(r->note[i] >> 7) | (uint32_t)(r->flags[i] >> 7) << 1); break; } case ED_UP_PUT: { /* slot, engine, name, values, pattern -> slot, rc */ static up_rec_t r; uint32_t slot = 0, rc; if (na < 1u) return; rc = (uint32_t)up_parse(a, na, &r, &slot); if (!rc && ed_flash_stop()) rc = 2; if (!rc) { int16_t v[P_COUNT]; up_values(&r, v); /* each value inside its range */ for (i = 0; i < P_COUNT; i++) up_set_value(&r, i, v[i]); /* the record's own form (v4: a byte each) */ rc = up_put(slot, &r) ? 2u : 0u; } ed_b(a[0]); ed_b(rc); break; } case ED_UP_STORE: { /* slot, name -> slot, rc */ char nm[13] = {0}; uint32_t n0, rc = 1; if (na < 1u) return; for (n0 = 0; 1u + n0 < na && a[1 + n0] && n0 < 13u; n0++) ; if (a[0] < UP_SLOTS && (!n0 || up_name_ok(a + 1, n0)) && (1u + n0 == na || (2u + n0 == na && !a[1u + n0]))) { /* "" = automatic name */ for (i = 0; i < n0; i++) nm[i] = (char)a[1 + i]; int r; r = ed_flash_stop() ? 2 : up_store(a[0], nm); rc = r ? 2u : 0u; } ed_b(a[0]); ed_b(rc); break; } case ED_UP_LOAD: /* slot -> slot, rc */ if (na < 1u) return; ed_b(a[0]); ed_b(a[0] < UP_SLOTS && !up_load(a[0]) ? 0u : 1u); break; case ED_UP_ERASE: if (na < 1u) return; ed_b(a[0]); ed_b(a[0] >= UP_SLOTS ? 1u : ed_flash_stop() || up_put(a[0], 0) ? 2u : 0u); break; case ED_WATCH: { /* on -> on */ uint32_t keep, v4was = ed_w.v4; if (na < 1u) return; keep = ed_w.on && ed_w.resets == usb.resets && (a[0] & 1u); /* already watching: the changes not yet */ ed_w.on = a[0] & 1u; /* pushed stay pending (INFO 53 01 bit 0) */ ed_w.v4 = (uint8_t)(ed_w.on && (a[0] & 2u)); /* v4: also TRACK_CHANGED; the reply says it is known */ ed_w.resets = usb.resets; if (ed_w.on && !keep) ed_shadow(); else if (keep && ed_w.v4 && !v4was) for (i = 0; i < ED_NT; i++) /* TRACK_CHANGED newly asked for: from the mix as it is */ ed_w.tv[i] = trk[i / 3u].p[ED_TIDS[i % 3u]]; ed_b(ed_w.on | ed_w.v4 << 1); break; } case ED_PING: ed_b(0); break; case ED_TRACK: /* [track] -> selected, NTRK, per track: engine, preset, level, mute, armed */ if (na >= 1u && a[0] < NTRK && a[0] != song.sel) { track_select(a[0]); if (ed_w.on) ed_shadow(); /* the editor re-reads it: no RELOAD for this */ } ed_b(song.sel); ed_b(NTRK); for (i = 0; i < NTRK; i++) { ed_b(ed_eng(&trk[i])); ed_b(trk[i].preset); ed_v(trk[i].p[P_LEVEL]); ed_b(trk[i].p[P_MUTE] != 0); ed_b((song.rec >> i) & 1u); } break; case ED_TRACK_MIX: { /* track [, level v14, mute] -> track, level, mute */ track_t *t; int16_t *lv; if (na < 1u || a[0] >= NTRK) return; t = &trk[a[0]]; lv = &t->p[P_LEVEL]; if (na >= 4u) { *lv = (int16_t)clamp(ed_rv(a + 1), 0, 127); t->p[P_MUTE] = (int16_t)(a[3] ? 1 : 0); ed_known(a[0], P_LEVEL); /* the editor's own change: no push */ ed_known(a[0], P_MUTE); ui.force = 1; } ed_b(a[0]); ed_v(*lv); ed_b(t->p[P_MUTE] != 0); break; } case ED_TRACK_DUMP: /* track -> track, engine, preset, P_COUNT x v14 */ if (na < 1u || a[0] >= NTRK) return; ed_b(a[0]); ed_b(ed_eng(&trk[a[0]])); ed_b(trk[a[0]].preset); for (i = 0; i < P_COUNT; i++) ed_v(motion_base_value(&trk[a[0]], i)); break; case ED_TRACK_STEP: { /* track, index [, step] -> track, index, step (as STEP_GET) */ step_t *st; if (na < 2u || a[0] >= NTRK || a[1] >= NSTEP) return; st = &trk[a[0]].step[a[1]]; if (na >= 10u && !chain_busy()) ed_step_put(st, a + 2, na - 2u); if (a[0] == song.sel) ed_w.st[a[1]] = ed_step_sig(&seq_steps(&trk[a[0]])[a[1]]); ed_b(a[0]); ed_b(a[1]); ed_step_reply(&seq_steps(&trk[a[0]])[a[1]], na == 23u || na == 3u); break; } case ED_TRACK_PARAM: { /* track, id [, v14] -> track, id, v14 */ track_t *t; if (na < 2u || a[0] >= NTRK || a[1] >= P_COUNT) return; t = &trk[a[0]]; d = ed_tdesc(t, a[1]); if (na >= 4u && !(chain_busy() && a[1] >= P_SLEN && a[1] <= P_SGATE)) { if (d->max > d->min) /* as SET: clamped; a fixed value stays */ t->p[a[1]] = (int16_t)enum_orig(d, clamp(ed_rv(a + 2), d->min, d->max)); (void)motion_capture(t, a[1], t->p[a[1]]); ed_known(a[0], a[1]); ui.force = 1; } ed_b(a[0]); ed_b(a[1]); ed_v(t->p[a[1]]); break; } case ED_LYRIC: { /* (0.8.5) track [, ASCII .. 0] -> track, rc, the lyric */ char b[LYR_LEN]; uint32_t k = 0, rc = 0; if (!na || a[0] >= NTRK) return; if (na > 1u) { for (i = 1; i < na && a[i] && k + 1u < LYR_LEN; i++) b[k++] = (char)(a[i] >= 32u && a[i] <= 126u ? a[i] : ' '); b[k] = 0; if (i < na && a[i] && k + 1u >= LYR_LEN) rc = 1; /* (longer: cut at 63) */ lyr_set(a[0], b); ui.force = 1; } ed_b(a[0]); ed_b(rc); ed_str(lyr_get(a[0]), LYR_LEN - 1u); break; } case ED_SONG: { uint32_t rc = 0, op; chain_config_t c; if (!na || a[0] > 3u) return; op = a[0]; if (op == 1u) { chain_defaults(&c); if (na < 2u || a[1] > CHAIN_ROWS || na != 2u + 2u * a[1]) rc = 1; else { c.count = a[1]; for (i = 0; i < c.count; i++) { c.row[i].slot = a[2u + 2u * i]; c.row[i].bars = a[3u + 2u * i]; } if (!chain_valid(&c)) rc = 1; else if (chain_busy()) rc = 2; else { chain_config = c; ui.song_row = 0; ui.force = 1; } } } else if (op == 2u) rc = chain_prepare(); else if (op == 3u) transport_req = 2; ed_b(op); ed_b(rc); ed_b(chain_config.count); ed_b(chain.running); ed_b(chain.row); ed_b(chain.remaining); for (i = 0; i < chain_config.count; i++) { ed_b(chain_config.row[i].slot); ed_b(chain_config.row[i].bars); } break; } default: return; } ed_send(); } /* main loop: editor frames first; anything else stays for ota_service() */ static void ed_service(void) { const uint8_t *p; uint32_t n; ed_sync(); /* v2 pushes (while watched) */ #if SCR_HAS_LCD if (scr_on && fm1_ms - ed_w.last_ms > SCR_IDLE_MS) scr_on = 0; /* the screen mirror: the editor went quiet (editor_screen.c) */ #endif if (!ota_frame_get(&p, &n) || n < 4u || p[0] != ED_HDR0 || p[1] != ED_HDR1 || p[2] != ED_HDR2) return; ed_w.last_ms = fm1_ms; /* any request keeps WATCH alive */ if (p[3] >= ED_UP_LIST) { /* large frames: handled in place, then freed */ ed_handle(p, n); ota_frame_done(); return; } { static uint8_t f[64]; uint32_t k = n > sizeof f ? sizeof f : n, i; for (i = 0; i < k; i++) f[i] = p[i]; ota_frame_done(); /* free the frame buffer before replying */ ed_handle(f, k); } }