Files
fmini-1/firmware/src/upreset.c
T
Claude 232264d571 FMini-1 v1.7.1: fmug is now fmini everywhere (files, identifiers, engine name, editor, docs)
The project's first name, FMUG, is gone. Nothing about the sound or the stored data changes.

- Files: eng_fmini.c, fmini_app.c, ui_fmini.c, editor_fmini.c, eng_fmini_filter.h, eng_fmini_presets.h, the tests, the
  tools (fmini_bank.py, fmini_filter.py, fmini_retune.py, fmini_levels.json, gen_fmini_presets.py) and
  web/make_fmini_installer.py.
- Identifiers: fmug_ / FMUG_ / FELUCCA_FMUG_ONLY became fmini_ / FMINI_ / FELUCCA_FMINI_ONLY; the editor commands are
  FMINI_GET / FMINI_PUT / FMINI_PRESET (numbers 82-84 unchanged). The engine reports the name "FMINI" (was "FMUG") on the
  device and in the editor's engine list; the editor still accepts "FMUG" from firmware up to 1.7.0.
- Short forms fmg_ / GR_FMG0..6 and FG_* are left as they are.
- Tables: tests/golden.txt and tests/target_budget.txt only had their keys renamed.
- Docs: CLAUDE.md and DEVELOPING.md say there is one branch (main) and record the rename; the test-suite notes now describe
  the second regress section (FELUCCA_CPU2=0: 41 FMINI patches differ from the golden, as on 1.7.0).
- Site: docs/emu and docs/index.html rebuilt for v1.7.1; the splash pictures show the new version.

Checked: the firmware's instructions are identical to 1.7.0 (only the addresses of string literals move); the 100 FMINI golden
hashes are unchanged; every screen of the README shots is pixel-identical except the splash; the preset lab's metrics for the
100 patches are identical; the host suite fails in the same seven sections as before (fm4, ui_render, three regress, union_test,
web pages) and nowhere else. Not tested on a real FM-1.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QUF6pQ1NkTwmBC8SR8AK94
2026-10-20 19:35:44 -03:00

629 lines
26 KiB
C

/* SPDX-License-Identifier: GPL-3.0-only
* Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments */
/* User presets (editor protocol v2, cmds 16-21; the SAVE > USER page): 32
* slots in two storage.c objects (OBJ_UPRESET0/1, A/B sectors at
* 0xDC000..0xDFFFF), 16 records each, mirrored in RAM so browsing never
* reads flash. A record: engine, name, the instrument parameters, a 16-step
* pattern (factory PATTERNS[] format). An FM6 sound's patch is kept beside the record (up_fm6.c, since 1.0.3:
* the record itself is unchanged). Loading one loads the sound only; its pattern
* is offered by SAVE > PHRASES ("U07", up_pat_load). The format is unchanged.
*
* Versions: a bank whose magic, record size or slot count differ reads as
* empty; so does a record with another layout version. A record keeps np =
* the P_COUNT it was stored with; when that differs from today's it is
* mapped by count: its last 8 values are P_E0..P_E7, the first np - 8 are
* P_LEVEL.. in order, and parameters it does not have take their defaults.
* That holds as long as common parameters are only ever added just before
* P_E0 (else bump UP_VER and translate). Version 1 (before 1.0) has the
* same layout; only its PHYS MODEL 2 meant DUST, which up_values loads as
* MODAL bowed (eng_phys.c phys_legacy). A record of PHYS MODEL 4 (DRUM, version 2
* until the kit became the DRUM engine) is that engine: up_migrate rewrites it
* in the RAM mirror when a bank is read and when UP_PUT sends one (core.h
* drum_from_phys); flash keeps the old record until its bank is written again,
* which stores the new one. No version bump: today's PHYS has no MODEL 4, so
* such a record cannot be anything else, and older firmware keeps reading the bank.
* A record of SAMPLE SET 4 (PERC, the GM kit, retired after 1.0.2) is migrated the same way to the DRUM engine
* with its default kit (core.h drum_from_perc: its E values the kit's, the rest of the sound and its pattern as
* stored; the GM notes play the same drums).
* Version 3 (UP_VER_GRID, since the DRUM grid) is the same record with a drum grid as its pattern: note[i] is
* step i's lane hits (bit l = lane l), flags[i] their accents. A DRUM track whose first 16 steps strike a
* lane is stored so (its notes on their lanes); every other sound as version 2, which older firmware reads.
* Older firmware shows a version 3 record as empty and keeps its bytes.
* A note pattern keeps each step's RATCH in its flags (core.h SF_RATCH, 8 | 16; 0 = x1, every older record):
* bits firmware before RATCH drops when it loads the pattern, so the record is the same version. A drum grid
* has no room for it (its flags are the accents): it loads x1.
* 1.2 (P_COUNT 103: LFO 2's SYNC TRIG POL, QUANTIZE): no version bump, records map by count as above (one of 99
* gets the four at their defaults, which play as before); firmware before reads a record of 103 by count too (its
* 91 parameters before P_E0 and the engine's 8: the four new ones are left out). A pattern keeps no step's NUDGE (core.h
* SF_NUDGE: its bit 4 would read as the tie here): the step on its start.
* SPREAD (P_SPRD, #148, before 1.2's release) made it P_COUNT 104 the same way: a record of 103 gets SPREAD 0.
* 1.5: the INSERT (P_ITYPE .. P_IMIX), ANALOG's filter TYPE and ENV SYNC (P_FTYPE, P_ESYNC) made it 111 the same way:
* a record of 104 gets the INSERT OFF, LP and OFF; 1.2 .. 1.4 read a record of 111 by count (the seven left out).
* A record of engine 1 (DIGITAL, retired in 1.0) stays as it is (UP_PUT takes it too): its values are DIGITAL's,
* and every load converts them to an FM6 sound with its own patch (ui.c fm4_apply, fm4_convert.c); lists count it
* with FM6's (up_engine).
*
* With -DUP_HOST (host test) only the part above #ifndef UP_HOST is built;
* it needs nothing but core.h. */
#define UP_PER_BANK 16u
#define UP_PMAX 72u /* room for P_COUNT to grow */
#define UP_USED 0xA5u
#define UP_VER 4u /* 2 since 1.0; 1 is read too (PHYS MODEL 2 was DUST) */
#define UP_VER_GRID 5u /* 2 with a drum grid as the pattern (see the top) */
#define UP_BANK_MAGIC 0x31425055u /* "UPB1" */
typedef struct {
uint8_t used, ver, engine, np; /* UP_USED, UP_VER, engine, P_COUNT when stored */
char name[12]; /* ASCII 32..126, 0-padded (no 0 when 12 long) */
union { int16_t p[UP_PMAX]; uint8_t packed[UP_PMAX * 2u]; };
uint8_t note[16], flags[16]; /* note 0 = rest; flags 1 accent, 2 slide, 4 tie, SF_RATCH
* (UP_VER_GRID: lane hits, their accents) */
} up_rec_t;
typedef struct {
uint32_t magic;
uint16_t rsize, nslot;
up_rec_t r[UP_PER_BANK];
} up_bank_t;
_Static_assert(sizeof(up_rec_t) == 192, "user preset record layout");
_Static_assert(P_COUNT <= UP_PMAX * 2u && P_COUNT < 128, "user preset record: P_COUNT");
static up_bank_t up_bank[UP_SLOTS / UP_PER_BANK] __attribute__((section(".pool"))); /* (main loop only) */
static up_rec_t *up_rec(uint32_t k) { return &up_bank[k / UP_PER_BANK].r[k % UP_PER_BANK]; }
/* 1.2 (Discussion #90): the sound's category (category.c CAT_*) in the record's last byte, packed[UP_CAT_AT]: a
* version 4 / 5 record's values take np = P_COUNT (< 128) of its 144 bytes and nothing reads the others (up_valid
* neither), so every record of before holds 0 there (CAT_NONE: listed by its engine, cat_of_engine) and firmware
* before keeps the byte (a bank is written whole) without reading it: no format change, no version bump. A record of
* versions 1..3 (a 16-bit value in every pair) has none; giving it one (a rename) rewrites it as version 4 / 5 when its
* values fit a byte (up_to_v4; not a version 1 PHYS sound, whose MODEL 2 means DUST) */
#define UP_CAT_AT (UP_PMAX * 2u - 1u)
#define UP_BPM_AT (UP_CAT_AT - 1u) /* FMini-1: the tempo (BPM, 40..240) a preset was saved with; 0 = none (as every record before) */
#define UP_OCT_AT (UP_CAT_AT - 6u) /* FMini-1: the octave (OCT- / OCT+, -3..3) + 4; 0 = none */
#define UP_FX2_AT (UP_CAT_AT - 14u) /* FMini-1: the FX pop-ups' other eight (G_RTYPE G_RDAMP G_DTIME G_DFDBK G_ENGGO G_CDEPTH G_INFO G_MIDI) + 1; 0 = none (a preset of before: their defaults) */
#define UP_FX_AT (UP_CAT_AT - 5u) /* FMini-1: the FX page's DECAY TIME RATE TONE (G_RSIZE G_DRREV G_CRATE G_DRLVL) + 1; 0 = none */
static const uint8_t UP_FXG[4] = {G_RSIZE, G_DRREV, G_CRATE, G_DRLVL};
static const uint8_t UP_FXG2[8] = {G_RTYPE, G_RDAMP, G_DTIME, G_DFDBK, G_ENGGO, G_CDEPTH, G_INFO, G_MIDI};
#include "category.c"
static uint32_t up_cat_of(const up_rec_t *r)
{
uint32_t c = r->ver >= 4u && r->np <= UP_CAT_AT ? r->packed[UP_CAT_AT] : 0u;
return c && c < CAT_N ? c : cat_of_engine(r->engine);
}
static int up_to_v4(up_rec_t *r) /* 1 = r is version 4 / 5 (now) */
{
int16_t v[UP_PMAX];
uint32_t i;
if (r->ver >= 4u)
return 1;
if (r->ver == 1u && r->engine == ENGI_PHYS)
return 0;
for (i = 0; i < r->np; i++)
if ((v[i] = r->p[i]) < -64 || v[i] > 127)
return 0;
memset(r->packed, 0, sizeof r->packed);
r->ver = r->ver == 3u ? UP_VER_GRID : UP_VER;
for (i = 0; i < r->np; i++)
r->packed[i] = (uint8_t)(v[i] + 64);
return 1;
}
static void up_set_cat(up_rec_t *r, uint32_t c) /* c CAT_BASS..CAT_OTHER; a record that cannot hold one keeps none */
{
if (c && c < CAT_N && up_to_v4(r))
r->packed[UP_CAT_AT] = (uint8_t)c;
}
static int up_valid(const up_rec_t *r)
{
if (!(r->used == UP_USED && r->ver >= 1u && r->ver <= UP_VER_GRID && r->engine < NENGINES &&
r->np >= 8u && r->np <= (r->ver >= 4u ? UP_PMAX * 2u : UP_PMAX) && r->name[0])) return 0;
if (r->ver >= 4u) for (uint32_t i = 0; i < r->np; i++) if (r->packed[i] > 191u) return 0;
return 1;
}
static int up_used(uint32_t k) { return k < UP_SLOTS && up_valid(up_rec(k)); }
static int up_grid(const up_rec_t *r) { return r->ver == 3u || r->ver == UP_VER_GRID; }
static int16_t up_value(const up_rec_t *r, uint32_t k) { return r->ver >= 4u ? (int16_t)r->packed[k] - 64 : r->p[k]; }
static void up_set_value(up_rec_t *r, uint32_t k, int16_t v)
{
if (r->ver >= 4u) r->packed[k] = (uint8_t)(v + 64); else r->p[k] = v;
}
/* Legacy PHYS drums map through decoded parameters, not their disk representation. */
static void up_migrate(up_rec_t *r)
{
int16_t e[8]; uint32_t k;
if (!up_valid(r)) return;
for (k = 0; k < 8u; k++) e[k] = up_value(r, r->np - 8u + k);
if (drum_from_phys(r->engine, e) || drum_from_perc(r->engine, e)) { /* (SAMPLE PERC: see the top) */
r->engine = ENGI_DRUM;
for (k = 0; k < 8u; k++) up_set_value(r, r->np - 8u + k, e[k]);
}
}
static void up_pat_norm(uint8_t *note, uint8_t *flags);
/* after loading bank b (len bytes, -1 = none): wrong shape -> empty; each record's pattern inside its fields, as
* UP_PUT and SAVE store it (flash or a restored backup may hold anything there) */
static void up_bank_check(uint32_t b, int len)
{
up_bank_t *bk = &up_bank[b];
uint32_t i, k;
if (len != (int)sizeof *bk || bk->magic != UP_BANK_MAGIC || bk->rsize != sizeof(up_rec_t) ||
bk->nslot != UP_PER_BANK)
memset(bk, 0, sizeof *bk);
for (i = 0; i < UP_PER_BANK; i++) {
up_rec_t *r = &bk->r[i];
if (!up_valid(r))
continue;
up_migrate(r);
for (k = 0; k < 16u; k++)
if (up_grid(r))
r->flags[k] &= r->note[k]; /* a drum grid: accents only on its hits */
else
up_pat_norm(&r->note[k], &r->flags[k]);
}
}
/* the record's values in today's P_* order (mapped by count, see above); def = the defaults */
static void up_params(const up_rec_t *r, int16_t *out, const int16_t *def)
{
int16_t values[UP_PMAX * 2u];
for (uint32_t i = 0; i < r->np && i < NELEM(values); i++) values[i] = up_value(r, i);
params_by_count(out, values, r->np, def);
}
static int up_name_ok(const uint8_t *s, uint32_t n) /* 1..12 printable ASCII */
{
uint32_t i;
if (!n || n > 12u)
return 0;
for (i = 0; i < n; i++)
if (s[i] < 32u || s[i] > 126u)
return 0;
return 1;
}
static void up_name(uint32_t k, char *b) /* upper case, 0-terminated: b holds 13 */
{
const up_rec_t *r = up_rec(k);
uint32_t i;
for (i = 0; i < 12u && r->name[i]; i++)
b[i] = r->name[i] >= 'a' && r->name[i] <= 'z' ? (char)(r->name[i] - 32) : r->name[i];
b[i] = 0;
}
static void up_pat_norm(uint8_t *note, uint8_t *flags) /* tie: no note; rest: no flags */
{
*note &= 127u;
if (*flags & 4u) {
*note = 0;
*flags = 4;
} else {
*flags = *note ? (uint8_t)(*flags & (SF_ACCENT | SF_SLIDE | SF_RATCH)) : 0u;
}
}
static void up_pat_from(up_rec_t *r, const step_t *st) /* the first 16 steps -> the pattern */
{
uint32_t i;
for (i = 0; i < 16u; i++) {
r->note[i] = st[i].time == ST_NOTE && st[i].n ? st[i].note[0] : 0u;
r->flags[i] = st[i].time == ST_TIE ? 4u : (uint8_t)(st[i].flags & ~SF_NUDGE); /* (no nudge: 4 is the tie here) */
up_pat_norm(&r->note[i], &r->flags[i]);
}
}
static int up_pat_empty(const up_rec_t *r)
{
uint32_t i;
for (i = 0; i < 16u; i++)
if (r->note[i])
return 0;
return 1;
}
/* UP_PUT arguments: slot, engine, name, P_COUNT x v14, 16 x (note, flags) [, kind, 16 x hi [, category]] -> *r
* (values not yet clamped); 0 ok, 1 bad arguments. *slot gets the slot byte when there is one. kind 1 (and its 16
* bytes): a drum grid, the pairs the low 7 bits of each step's hits and accents, hi bit 0 / 1 their bit 7 (lane 8).
* category (1.2, category.c CAT_*, 0 none): after the kind's 16 bytes (kind 0 sends them too, ignored) */
static int up_parse(const uint8_t *a, uint32_t na, up_rec_t *r, uint32_t *slot)
{
uint32_t i, n, k, end;
if (na < 3u)
return 1;
*slot = a[0];
for (n = 0; 2u + n < na && a[2 + n]; n++)
;
k = 3u + n; /* after the name's 0 */
end = k + 2u * P_COUNT + 32u;
if (a[0] >= UP_SLOTS || a[1] >= NENGINES || 2u + n >= na || !up_name_ok(a + 2, n) ||
(na != end && ((na != end + 17u && na != end + 18u) || a[end] > 1u)) || (na == end + 18u && a[end + 17u] >= CAT_N))
return 1;
up_rec_t staged, *target = r;
r = &staged;
memset(r, 0, sizeof *r);
r->used = UP_USED;
r->ver = UP_VER;
r->engine = a[1];
r->np = P_COUNT;
for (i = 0; i < n; i++)
r->name[i] = (char)a[2 + i];
for (i = 0; i < P_COUNT; i++, k += 2u)
{
int32_t value = (int32_t)((a[k] & 127u) | (a[k + 1] & 127u) << 7) - 8192;
if (value < -64 || value > 127) return 1;
up_set_value(r, i, (int16_t)value);
}
if (na >= k + 33u + 16u && a[k + 32u] == 1u) { /* a drum grid */
r->ver = UP_VER_GRID;
for (i = 0; i < 16u; i++) {
uint32_t hi = a[k + 33u + i];
r->note[i] = (uint8_t)((a[k + 2u * i] & 127u) | (hi & 1u) << 7);
r->flags[i] = (uint8_t)(((a[k + 2u * i + 1u] & 127u) | (hi & 2u) << 6) & r->note[i]);
}
if (na == end + 18u)
up_set_cat(r, a[end + 17u]);
*target = *r;
return 0;
}
for (i = 0; i < 16u; i++, k += 2u) {
r->note[i] = a[k];
r->flags[i] = a[k + 1];
up_pat_norm(&r->note[i], &r->flags[i]);
}
if (na == end + 18u)
up_set_cat(r, a[end + 17u]);
up_migrate(r); /* (an editor of before the DRUM engine) */
*target = *r;
return 0;
}
#ifndef UP_HOST
static void up_values(const up_rec_t *r, int16_t *v) /* mapped and clamped for its engine */
{
int16_t def[P_COUNT];
uint32_t i;
for (i = 0; i < P_COUNT; i++)
def[i] = param_desc_of(r->engine, i)->def;
up_params(r, v, def);
if (r->ver == 1u && ENGINES[r->engine] == &ENG_PHYS) /* (before 1.0: MODEL 2 was DUST) */
phys_legacy(&v[P_E0]);
for (i = 0; i < P_COUNT; i++)
v[i] = (int16_t)param_fit(param_desc_of(r->engine, i), v[i]); /* (a retired KIT: the kit it plays) */
}
#include "up_fm6.c" /* the FM6 user presets' patches: the same kind of store */
#include "fm6_vbank.c" /* FM6's voice bank (SLOT B1..B32, 1.4.1) */
static void up_boot(void) /* persist_boot: the banks from flash */
{
#if FELUCCA_FLASH
uint32_t b;
for (b = 0; b < UP_SLOTS / UP_PER_BANK; b++)
up_bank_check(b, flash_ok ? st_load(OBJ_UPRESET0 + b, &up_bank[b], sizeof up_bank[b]) : -1);
#endif
upf_boot(); /* (after the banks: it may move the retired FM6 bank's patches) */
fvb_boot(); /* FM6's voice bank: a staged one finished, its voices */
#ifdef FELUCCA_FAVORITES
for (uint32_t k = 0; k < UP_SLOTS; k++)
if (!up_used(k)) favorite_set(NENGINES, k, 0);
#endif
}
/* record k = *r (0: erase), then the bank to flash: 0 ok, 1 bad slot, 2 flash error or the transport runs (nothing
* written), 3 no flash (kept in RAM) */
static int up_put(uint32_t k, const up_rec_t *r)
{
up_bank_t *bk;
#if FELUCCA_FLASH
up_rec_t old;
uint32_t magic;
uint16_t rsize, nslot;
#endif
if (k >= UP_SLOTS)
return 1;
if (transport_busy()) { /* no flash erase while playing (project_save) */
ui_message("STOP TO SAVE");
return 2;
}
bk = &up_bank[k / UP_PER_BANK];
#if FELUCCA_FLASH
old = *up_rec(k);
magic = bk->magic;
rsize = bk->rsize;
nslot = bk->nslot;
#endif
bk->magic = UP_BANK_MAGIC;
bk->rsize = sizeof(up_rec_t);
bk->nslot = UP_PER_BANK;
if (r)
*up_rec(k) = *r;
else
memset(up_rec(k), 0, sizeof(up_rec_t));
#if FELUCCA_FLASH
if (flash_ok && st_save(OBJ_UPRESET0 + k / UP_PER_BANK, bk, sizeof *bk)) {
*up_rec(k) = old;
bk->magic = magic;
bk->rsize = rsize;
bk->nslot = nslot;
return 2;
}
#endif
if (!r) {
#ifdef FELUCCA_FAVORITES
if (favorite_set(NENGINES, k, 0)) settings_save();
#endif
uint32_t i;
for (i = 0; i < NTRK; i++)
if (trk[i].user == k + 1u)
trk[i].user = 0;
}
up_gen++;
#if FELUCCA_FLASH
if (flash_ok)
return 0;
#endif
return 3;
}
static void up_slot_label(char *b, uint32_t k) /* "U07" */
{
b[0] = 'U';
b[1] = (char)('0' + (k + 1u) / 10u);
b[2] = (char)('0' + (k + 1u) % 10u);
b[3] = 0;
}
/* the automatic name of engine e's sound in slot k: engine name + slot number ("ANALOG 07"); b holds 13 */
static void up_auto_name(char *b, uint32_t e, uint32_t k)
{
char l[4];
e %= NENGINES;
str_cpy(b, ENGINES[eng_ok(e) ? e : ENGI_FM6]->name, 9); /* (a DIGITAL record plays as FM6) */
up_slot_label(l, k);
str_cpy(b + str_len(b), " ", 2);
str_cpy(b + str_len(b), l + 1, 3);
}
/* the record's name = name (at most 12), 0 or "": the automatic one */
static void up_set_name(up_rec_t *r, uint32_t k, const char *name)
{
char b[16];
uint32_t i;
if (!name || !name[0]) {
up_auto_name(b, r->engine, k);
name = b;
}
memset(r->name, 0, sizeof r->name);
for (i = 0; i < 12u && name[i]; i++)
r->name[i] = name[i];
}
static uint32_t up_cat(uint32_t k) { return up_cat_of(up_rec(k % UP_SLOTS)); } /* a used slot's category */
static uint32_t track_cat(const track_t *t); /* (ui.c: the category of the sound a track plays) */
/* the selected part's sound -> slot k; name 0 or "": the automatic name (up_auto_name); cat its category (CAT_*), 0:
* the one of the sound it came from (track_cat); up_put's result */
static int up_store_cat(uint32_t k, const char *name, uint32_t cat)
{
up_rec_t r;
uint32_t i;
memset(&r, 0, sizeof r);
r.used = UP_USED;
r.ver = UP_VER;
r.engine = TSEL->eng_req;
r.np = P_COUNT;
up_set_name(&r, k, name);
for (i = 0; i < P_COUNT; i++)
up_set_value(&r, i, motion_base_value(TSEL, i));
up_pat_from(&r, TSEL->step);
up_set_cat(&r, cat ? cat : track_cat(TSEL));
#if FELUCCA_FMINI_ONLY
r.packed[UP_BPM_AT] = (uint8_t)clamp(song.g[G_BPM], 40, 240); /* (np = P_COUNT < UP_BPM_AT: no value there) */
r.packed[UP_OCT_AT] = (uint8_t)(clamp(song.octave, -3, 3) + 4);
for (i = 0; i < 8u; i++)
r.packed[UP_FX2_AT + i] = (uint8_t)(clamp(song.g[UP_FXG2[i]], 0, 127) + 1);
for (i = 0; i < 4u; i++)
r.packed[UP_FX_AT + i] = (uint8_t)(clamp(song.g[UP_FXG[i]], 0, 127) + 1);
#endif
if (drum_track(TSEL)) { /* a DRUM track that strikes a lane: its grid */
uint32_t any = 0;
for (i = 0; i < 16u; i++)
any |= step_lanes(&TSEL->step[i]);
if (any) {
r.ver = UP_VER_GRID;
for (i = 0; i < 16u; i++) {
r.note[i] = (uint8_t)step_lanes(&TSEL->step[i]);
r.flags[i] = (uint8_t)step_accents(&TSEL->step[i]);
}
}
}
{
int rc = up_put(k, &r); /* the record first, then its FM6 patch (up_fm6.c) */
if ((rc == 0 || rc == 3) && (r.engine == ENGI_FM6 || r.engine == ENGI_FMINI)) {
int u = upf_store(k, (uint32_t)(TSEL - trk));
if (u == 2)
rc = 2;
}
return rc;
}
}
static int up_store(uint32_t k, const char *name) { return up_store_cat(k, name, 0); }
/* slot k renamed (name 0 or "": the automatic one) and given category cat (0: kept), the sound and its pattern as
* they are; up_put's result, 1 for an empty slot */
static int up_rename_cat(uint32_t k, const char *name, uint32_t cat)
{
up_rec_t r;
if (!up_used(k))
return 1;
r = *up_rec(k);
up_set_name(&r, k, name);
up_set_cat(&r, cat);
return up_put(k, &r);
}
static int up_rename(uint32_t k, const char *name) { return up_rename_cat(k, name, 0); }
/* slot k -> the selected part's sound: engine and every parameter except the track's own (param_kept:
* the mix, ARP, SCL, the pattern parameters, the SLICER). The steps stay: the record's pattern is
* loaded only from SAVE > PHRASES (up_pat_load). 0 ok, 1 empty */
static int up_load(uint32_t k)
{
const up_rec_t *r;
int16_t v[P_COUNT];
uint32_t i;
track_t *t = TSEL;
if (!up_used(k))
return 1;
r = up_rec(k);
up_values(r, v);
load_begin(t, UNDO_SOUND); /* (ui.c: the copy for SAVE held = undo) */
panic_req |= (uint8_t)(1u << song.sel);
#if !FELUCCA_FM4
if (r->engine == ENGI_DIGITAL) { /* a DIGITAL sound (kept as it was stored): FM6 */
int16_t p[P_COUNT];
for (i = 0; i < P_COUNT; i++)
p[i] = param_kept(i) ? t->p[i] : v[i];
fm4_apply(t, p);
} else
#endif
{
fm1_irq_off(); /* the audio ISR must not see half a sound */
t->eng_req = r->engine;
for (i = 0; i < P_COUNT; i++)
if (!param_kept(i))
t->p[i] = v[i];
t->preset = 0;
fm1_irq_on();
upf_track_load(t, k); /* FM6: the preset's own patch (up_fm6.c) */
}
t->user = (uint8_t)(k + 1u);
#if FELUCCA_FMINI_ONLY
if (r->ver >= 4u && r->np <= UP_BPM_AT && r->packed[UP_BPM_AT] >= 40u && r->packed[UP_BPM_AT] <= 240u)
song.g[G_BPM] = r->packed[UP_BPM_AT]; /* the preset's tempo */
if (r->ver >= 4u && r->np <= UP_OCT_AT && r->packed[UP_OCT_AT] >= 1u && r->packed[UP_OCT_AT] <= 7u)
song.octave = (int8_t)((int)r->packed[UP_OCT_AT] - 4); /* its octave */
if (r->ver >= 4u && r->np <= UP_FX2_AT)
for (i = 0; i < 8u; i++)
song.g[UP_FXG2[i]] = (int16_t)(r->packed[UP_FX2_AT + i] >= 1u && r->packed[UP_FX2_AT + i] <= 128u
? r->packed[UP_FX2_AT + i] - 1u : (uint32_t)GP[UP_FXG2[i]].def); /* its types and tones */
if (r->ver >= 4u && r->np <= UP_FX_AT)
for (i = 0; i < 4u; i++)
if (r->packed[UP_FX_AT + i] >= 1u && r->packed[UP_FX_AT + i] <= 128u)
song.g[UP_FXG[i]] = (int16_t)(r->packed[UP_FX_AT + i] - 1u); /* its effect settings */
#endif
load_end(t);
sync_reload = 1;
ui.force = 1;
return 0;
}
/* the patterns of the user presets (SAVE > PHRASES lists them after the factory ones, ui.c pat_count) */
static int up_has_pat(uint32_t k) { return up_used(k) && !up_pat_empty(up_rec(k)); }
static uint32_t up_pat_count(void)
{
uint32_t k, n = 0;
for (k = 0; k < UP_SLOTS; k++)
n += (uint32_t)up_has_pat(k);
return n;
}
static uint32_t up_pat_nth(uint32_t n) /* slot of the n-th one that holds a pattern (n < up_pat_count()) */
{
uint32_t k;
for (k = 0; k < UP_SLOTS; k++)
if (up_has_pat(k) && !n--)
return k;
return 0;
}
static uint32_t up_pat_rank(uint32_t slot) /* ones that hold a pattern before it */
{
uint32_t k, n = 0;
for (k = 0; k < slot && k < UP_SLOTS; k++)
n += (uint32_t)up_has_pat(k);
return n;
}
/* slot k's pattern -> track t's steps 1..16 (the rest cleared), with the record's LEN (at most 16), DIV,
* SWING and GATE; the sound stays (ui.c pat_load: the undo copy) */
static void up_pat_load(track_t *t, uint32_t k)
{
const up_rec_t *r;
int16_t v[P_COUNT];
uint32_t i;
if (!up_has_pat(k))
return;
r = up_rec(k);
up_values(r, v);
if (up_grid(r))
load_grid16(t, r->note, r->flags);
else
load_pat16(t, r->note, r->flags);
for (i = P_SDIV; i <= P_SGATE; i++)
t->p[i] = v[i];
t->p[P_SLEN] = (int16_t)clamp(v[P_SLEN], 1, 16); /* (the pattern has 16 steps) */
}
/* the engine a used slot's sound plays on (a DIGITAL record: FM6, without FELUCCA_FM4) */
static uint32_t up_engine(uint32_t k) { return eng_ok(up_rec(k)->engine) ? up_rec(k)->engine : ENGI_FM6; }
static uint32_t up_count(void) /* used slots */
{
uint32_t k, n = 0;
for (k = 0; k < UP_SLOTS; k++)
n += (uint32_t)up_used(k);
return n;
}
static uint32_t up_nth(uint32_t n) /* slot of the n-th used one (n < up_count()) */
{
uint32_t k;
for (k = 0; k < UP_SLOTS; k++)
if (up_used(k) && !n--)
return k;
return 0;
}
static uint32_t up_rank(uint32_t slot) /* used slots before it */
{
uint32_t k, n = 0;
for (k = 0; k < slot && k < UP_SLOTS; k++)
n += (uint32_t)up_used(k);
return n;
}
/* SAVE > USER page actions, with the message in the top bar. name: the save's or the rename's (0 or "": automatic);
* cat: its category (0: the sound's, a rename keeps it) */
static void up_ui_named(uint32_t op, uint32_t k, const char *name, uint32_t cat) /* 0 load, 1 erase, 2 save, 3 rename */
{
char l[4];
int rc;
up_slot_label(l, k);
if ((op < 2u || op == 3u) && !up_used(k)) {
ui_message("EMPTY SLOT");
return;
}
if (op && (song.playing || chain_busy() || transport_req == 1u)) { /* (up_put refuses too) */
ui_message("STOP TO SAVE");
return;
}
if (op == 0u) {
up_load(k);
if (up_has_pat(k)) /* SAVE > PHRASES starts at its pattern */
ui.ppick = (uint8_t)(NPATTERNS + up_pat_rank(k));
ui_say("LOADED ", l);
return;
}
rc = op == 1u ? up_put(k, 0) : op == 3u ? up_rename_cat(k, name, cat) : up_store_cat(k, name, cat);
if (rc == 3)
ui_message(op == 1u ? "ERASED (RAM)" : op == 3u ? "RENAMED (RAM)" : "SAVED (RAM)");
else if (rc)
ui_message(op == 1u ? "ERASE ERROR" : "SAVE ERROR");
else
ui_say(op == 1u ? "ERASED " : op == 3u ? "RENAMED " : "SAVED ", l);
ui.force = 1;
}
static void up_ui(uint32_t op, uint32_t k) { up_ui_named(op, k, 0, 0); }
#endif