Files
skiff/firmware/src/mod.c
T
illuminus5 5bbd35da7a Skiff 1.2: more sounds, a list by type, and new ways to shape them
143 factory sounds (100 before), at least 8 for every engine, each a BASS,
LEAD, PAD, KEYS, PLUCK, DRUM or FX sound: SAVE > PRESETS' LIST steps through
one type across every engine. Presets for all of DRUM's kits and eight new
DRUM-X kits. FX > DRIVE (four DIST types and a TONE), ENV 2 in the MOD
matrix, DRUM's lane TUNE and DECAY; a HALL reverb, the chorus as FLANGER or
PHASER with FEEDBACK, and GATE, DUB, FLANGE and VINYL on the FX layer. SLICE
has a new BREAK and a second loop, FUNK, made with the YuE2 music model.
HOME opened from the sequencer's first page no longer draws that page's
hints under its own (#3). Projects saved with 1.2 open on 1.1 without the
new settings.
2026-10-15 21:49:38 -05:00

255 lines
11 KiB
C

/* SPDX-License-Identifier: GPL-3.0-only
* Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments */
/* Modulation matrix: per track four slots, each SRC -> DST by AMT (-64..63), added to the fixed
* routings (ENV DEST, LFO DEST). Runs in the audio ISR (fx.c mix_part, voice.c track_render).
*
* Sources, Q15:
* LFO the track LFO with its FADE (as LFO DEST), bipolar
* ENV the amp envelope of the voice (after the engine's own amp curve, as ENV DEST), 0..1
* VEL the note-on velocity (before UNISON's level scaling), 0..1
* KEY the note relative to C4, bipolar: +-64 semitones = +-1 (an octave = 0.19)
* RAND a new random value per note-on (its own generator: the shared rng is not touched), bipolar
* MODW MIDI CC1, AT channel aftertouch, EXPR CC11 of the track's channel (seq.c), 0..1; until a
* controller arrives (and after CC121 RESET ALL CONTROLLERS) MODW and AT are 0, EXPR 1
* Destinations:
* per voice (vmod_t, like the fixed routings):
* PITCH AMT 63 at a full source = +-11.8 semitones (as LFO DEST PIT)
* CUT m->cutoff, SHP m->shape: AMT 63 = +-63 steps (as ENV DEST); the sums are clamped to what
* the fixed routings can reach, so the engines see no new range
* AMP a gain <= 1: AMT > 0 the source opens it (AMT 64: silent at 0, full at 1), AMT < 0 closes it
* (bipolar sources as 0..1, as LFO DEST AMP); several AMP slots multiply
* per track, once per block: PAN, DIST CHO DLY REV (the sends), RATE (LFO rate), VIB (LFO DEST PIT, the
* vibrato depth), E1..E8 (the engine's parameters P_E0..P_E7, named by the engine): AMT 64 at a full
* source = the parameter's whole range; the sum is clamped to its range
* A per-voice source on a per-block destination takes the latest note-on's value (VEL, KEY, RAND), ENV the
* envelope of the latest note-on's voice (one block late, 0 when it has ended).
*
* Per-block destinations are modulated in place: mod_begin writes the modulated values into t->p for the
* part's block (its render and its mix in fx.c mix_part) and mod_end puts the stored values back. Both run
* in the audio ISR, so nothing outside it ever sees a modulated value (the UI, the editor, projects and
* presets read and write t->p from the main loop, which the ISR preempts; TIMER5 only fills the MIDI rings),
* and the engines read t->p as before. An engine reads its parameters in note_on (events_block) unmodulated.
* Nothing active (every slot with SRC, DST or AMT at 0): no work, and the sound is bit-identical. */
enum { MS_OFF, MS_LFO, MS_ENV, MS_VEL, MS_KEY, MS_RAND, MS_MODW, MS_AT, MS_EXPR, MS_ENV2, MS_N }; /* (append only) */
enum { MD_OFF, MD_PITCH, MD_CUT, MD_SHP, MD_AMP, MD_PAN, MD_DIST, MD_CHO, MD_DLY, MD_REV, MD_RATE, MD_VIB, MD_E1,
MD_N = MD_E1 + 8 };
_Static_assert(NELEM(N_MSRC) == MS_N && NELEM(N_MDST) == MD_N, "N_MSRC / N_MDST == MS_* / MD_*");
_Static_assert(P_M2SRC == P_M1SRC + 3 && P_M4AMT == P_M1SRC + 11 && P_M4AMT + 1 == P_FM1_ATK, "matrix slots: 3 ids each");
#define NMSLOT 4u
#define MS_VOICE(s) ((1u << (s)) & (1u << MS_ENV | 1u << MS_VEL | 1u << MS_KEY | 1u << MS_RAND | 1u << MS_ENV2)) /* a
* value per voice */
#define MS_BIPOLAR(s) ((s) == MS_LFO || (s) == MS_KEY || (s) == MS_RAND)
/* the track parameter of a per-block destination */
static uint32_t mod_param(uint32_t d)
{
static const uint8_t ID[MD_E1] = {[MD_PAN] = P_PAN, [MD_DIST] = P_DIST, [MD_CHO] = P_CHOR, [MD_DLY] = P_DLY,
[MD_REV] = P_REV, [MD_RATE] = P_LRATE, [MD_VIB] = P_LD_PIT};
return d >= MD_E1 ? P_E0 + d - MD_E1 : ID[d];
}
static struct { /* the part in mix_part (one at a time) */
uint8_t on; /* a slot is active: track_render calls mod_voice */
uint8_t env2; /* (Skiff phase 9) a slot's source is ENV2: track_render ticks it (voice.c env2_tick) */
uint8_t amp; /* an AMP slot: the voice gain applies */
uint8_t nv, nk; /* voice terms, per-block parameters held */
int32_t pit, cut, shp, gain; /* per voice, from the per-track sources (gain Q15) */
uint8_t vsrc[NMSLOT], vdst[NMSLOT];
int8_t vamt[NMSLOT];
uint8_t kid[NMSLOT]; /* per-block: the parameter, its stored value */
int16_t keep[NMSLOT];
} mod;
static uint32_t mod_seed = 0x2545F491u;
static int16_t mod_rand(void) /* RAND of a note-on: xorshift32, bipolar */
{
mod_seed ^= mod_seed << 13;
mod_seed ^= mod_seed >> 17;
mod_seed ^= mod_seed << 5;
return (int16_t)(mod_seed >> 16);
}
static int32_t mod_key(uint32_t note) { return clamp(((int32_t)note - 60) * 512, -32767, 32767); }
/* source s of the track: the per-track ones, the per-voice ones of the latest note-on */
static int32_t mod_tsrc(const track_t *t, uint32_t s, int32_t lfo)
{
switch (s) {
case MS_LFO:
return lfo;
case MS_ENV:
return t->m_env;
case MS_VEL:
return t->m_vel * 258;
case MS_KEY:
return mod_key(t->m_key);
case MS_RAND:
return t->m_rnd;
case MS_ENV2: /* ((Skiff phase 9) the latest note's ENV 2) */
return t->m_env2;
case MS_MODW:
return t->mw * 258;
case MS_AT:
return t->at * 258;
default:
return (127 - t->ex_off) * 258;
}
}
/* AMP: the gain (Q15, 0..32767) of source x (bipolar: as 0..1) at amount a */
static int32_t mod_gain(uint32_t s, int32_t x, int32_t a)
{
int32_t u = MS_BIPOLAR(s) ? (x + 32768) >> 1 : x;
return clamp(32767 - (a > 0 ? ((32767 - u) * a) >> 6 : (u * -a) >> 6), 0, 32767);
}
/* a per-voice destination d by source value x, amount a */
static void mod_voice_add(uint32_t s, uint32_t d, int32_t x, int32_t a, int32_t *pit, int32_t *cut, int32_t *shp,
int32_t *gain)
{
if (d == MD_PITCH)
*pit += (x * a * 3) >> 15;
else if (d == MD_CUT)
*cut += (x * a) >> 7;
else if (d == MD_SHP)
*shp += (x * a) >> 7;
else
*gain = mulq15(*gain, mod_gain(s, x, a));
}
/* before a part's block (fx.c mix_part): the per-track offsets, the per-block destinations into t->p.
* 0 = no slot active (then nothing changed) */
static __attribute__((noinline)) int mod_begin(track_t *t)
{
const int16_t *p = t->p;
int32_t lfo, off[NMSLOT];
uint32_t k, j;
mod.on = 0;
for (k = 0; k < NMSLOT; k++)
if (p[P_M1SRC + 3u * k] && p[P_M1DST + 3u * k] && p[P_M1AMT + 3u * k])
break;
if (k == NMSLOT)
return 0;
lfo = mulq15(t->lfo_val, t->lfo_fade); /* (the LFO of the block before: track_render ticks it) */
mod.on = 1;
mod.amp = 0;
mod.env2 = p[P_M1SRC] == MS_ENV2 || p[P_M2SRC] == MS_ENV2 || p[P_M3SRC] == MS_ENV2 || p[P_M4SRC] == MS_ENV2; /* (a
* slot off too: a tick for nothing, the loop below as it was) */
mod.nv = mod.nk = 0;
mod.pit = mod.cut = mod.shp = 0;
mod.gain = 32767;
for (; k < NMSLOT; k++) {
uint32_t s = (uint32_t)p[P_M1SRC + 3u * k], d = (uint32_t)p[P_M1DST + 3u * k];
int32_t a = p[P_M1AMT + 3u * k], x;
if (!s || !d || !a || s >= MS_N || d >= MD_N)
continue;
if (d <= MD_AMP) { /* per voice */
mod.amp |= d == MD_AMP;
if (MS_VOICE(s)) {
mod.vsrc[mod.nv] = (uint8_t)s;
mod.vdst[mod.nv] = (uint8_t)d;
mod.vamt[mod.nv++] = (int8_t)a;
} else {
mod_voice_add(s, d, mod_tsrc(t, s, lfo), a, &mod.pit, &mod.cut, &mod.shp, &mod.gain);
}
continue;
}
{ /* per block: AMT 64 = the whole range */
uint32_t id = mod_param(d);
const param_desc_t *pd = id >= P_E0 ? &ENGINES[t->engine]->edit[id - P_E0] : &TP[id];
x = mod_tsrc(t, s, lfo);
for (j = 0; j < mod.nk && mod.kid[j] != id; j++)
;
if (j == mod.nk) {
mod.kid[j] = (uint8_t)id;
mod.keep[j] = p[id];
off[j] = 0;
mod.nk++;
}
off[j] += (((x * a) >> 6) * (pd->max - pd->min)) >> 15;
}
}
for (j = 0; j < mod.nk; j++) {
uint32_t id = mod.kid[j];
const param_desc_t *pd = id >= P_E0 ? &ENGINES[t->engine]->edit[id - P_E0] : &TP[id];
t->p[id] = (int16_t)clamp(mod.keep[j] + off[j], pd->min, pd->max);
}
return 1;
}
/* after the part's block (when mod.on): the stored values back */
static __attribute__((noinline)) void mod_end(track_t *t)
{
uint32_t j;
for (j = 0; j < mod.nk; j++)
t->p[mod.kid[j]] = mod.keep[j];
mod.on = 0;
}
/* a voice's modulation (voice.c track_render, when mod.on), onto its vmod_t m as track_render made it:
* pitch (the increment again, with the fine factor fine as there), cutoff and shape (clamped to what the
* fixed routings can reach), the amplitude (and v->env_out, the next block's start). env: its envelope */
static __attribute__((noinline)) void mod_voice(track_t *t, voice_t *v, vmod_t *m, int32_t fine)
{
int32_t pit = mod.pit, cut = mod.cut, shp = mod.shp, gain = mod.gain, env = m->envq15;
uint32_t k;
if (v == &t->v[t->m_vi]) {
t->m_env = env; /* ENV of the latest note, for per-block destinations */
t->m_env2 = v->env2 >> 9; /* ((Skiff phase 9) its ENV 2 too) */
}
for (k = 0; k < mod.nv; k++) {
uint32_t s = mod.vsrc[k];
int32_t x = s == MS_ENV ? env : s == MS_VEL ? v->mvel * 258 : s == MS_KEY ? mod_key(v->note) :
s == MS_ENV2 ? v->env2 >> 9 : v->mrnd;
mod_voice_add(s, mod.vdst[k], x, mod.vamt[k], &pit, &cut, &shp, &gain);
}
if (pit) {
m->pitch16 = clamp(m->pitch16 + pit, 0, 2047);
m->inc = pitch_inc(m->pitch16);
if (fine)
m->inc += (uint32_t)((int32_t)(m->inc >> 12) * fine);
}
m->cutoff = clamp(m->cutoff + cut, -150 * 256, 150 * 256);
m->shape = clamp(m->shape + shp, -62 * 256, 190 * 256);
if (mod.amp) {
m->amp1 = mulq15(m->amp1, gain);
v->env_out = m->amp1;
}
}
/* a note-on of track t (voice.c trk_note_on): the per-voice sources of the latest note */
static void mod_note(track_t *t, uint32_t note, uint32_t vel)
{
t->m_vel = (uint8_t)vel;
t->m_key = (uint8_t)note;
t->m_rnd = mod_rand();
}
/* a MIDI controller for track t (seq.c events_block): CC1 / CC11 / channel aftertouch; CC121 resets them */
static __attribute__((noinline)) void mod_midi(track_t *t, uint32_t st, uint32_t d1, uint32_t d2)
{
if (st == 0xD0u) {
t->at = (uint8_t)d1;
} else if (st == 0xB0u) {
if (d1 == 1u)
t->mw = (uint8_t)d2;
else if (d1 == 11u)
t->ex_off = (uint8_t)(127u - d2);
else if (d1 == 121u)
t->mw = t->at = t->ex_off = 0;
}
}
/* the name of destination value v for track t (UI, <= 5 characters): E1..E8 by the engine's labels (of the
* engine the UI shows), "E5" for a parameter the engine does not use */
static const char *mod_dst_name(const track_t *t, int32_t v)
{
v = clamp(v, 0, MD_N - 1);
if (v >= MD_E1) {
const char *l = track_desc(t, P_E0 + (uint32_t)(v - MD_E1))->label;
if (l && l[0] && l[0] != '-')
return l;
}
return N_MDST[v];
}