Files
skiff/firmware/src/fx.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

596 lines
29 KiB
C

/* SPDX-License-Identifier: GPL-3.0-only
* Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments */
/* Effects: per-track DIST insert, then sends into three
* shared buses (chorus, tempo delay, reverb). Mono buses, stereo dry mix. */
#define DLY_LEN 65536u /* 1.49 s: 1/4 at 40 BPM fits */
#define CHO_LEN 2048u
static int16_t dly_buf[DLY_LEN] __attribute__((section(".pool")));
static int16_t cho_buf[CHO_LEN] __attribute__((section(".pool")));
static const uint16_t REV_COMB[4] = {1116, 1188, 1277, 1356};
static const uint16_t REV_AP[2] = {556, 441};
static int16_t rev_comb[1116 + 1188 + 1277 + 1356] __attribute__((section(".pool")));
static union { /* ROOM's allpasses; SPRING's allpass chain (int32: no clamps) */
int16_t ap[556 + 441];
int32_t sp[(556 + 441) / 2];
} rev_u __attribute__((section(".pool")));
#define rev_ap (rev_u.ap)
static struct {
uint32_t dly_w, cho_w, cho_ph;
int32_t dly_lp;
uint16_t comb_i[4], ap_i[2];
int32_t comb_lp[4];
uint8_t rtype; /* the reverb model running (G_RTYPE: 0 ROOM, 1 SPRING, (Skiff phase 9) 2 HALL) */
uint16_t sp_w; /* SPRING: the loop's write index (SP_MASK) */
int32_t sp_lp, sp_hp, sp_he, sp_size; /* .. its loop low-pass, low cut (and its remainder), the loop
* length (Q8, glides) */
uint32_t sp_ph; /* .. the output tap's wobble */
int32_t hall_y; /* (Skiff phase 9) HALL: its last half-rate output (the line between) */
int32_t cho_y; /* (Skiff phase 9) the CHORUS bus: its last output (FEEDBACK), */
int32_t ph_s[4]; /* .. PHASER's allpasses */
} fx;
/* SPRING (G_RTYPE 1): one spring of a spring tank, mono like the other buses, in the ROOM's own buffers (no
* RAM of its own): the input and the loop's return -> a low cut (~110 Hz: a spring carries little bass) ->
* SP_N stretched first-order allpasses, (a + z^-4) / (1 + a z^-4) (after Valimaki, Parker and Abel: below
* fs / 8 = 5.5 kHz the group delay rises with frequency, the chirp; each pass round the loop adds more of
* it: the "boing", the drips) -> the loop's delay line (rev_comb, SP_LEN) -> back through a one-pole
* low-pass (DAMP) and the decay gain (SIZE). The output: the spring's far end, half way along the loop
* (the first sound 15 .. 30 ms after the send: the tank's own pre-delay; a slow wobble of a sample or two
* on it), plus a second, quieter pickup at three quarters (a shorter spring beside it: denser). SIZE sets
* the loop's length (30 .. 60 ms) and its decay; DAMP the loop's low-pass. The allpasses' states: 4
* samples each (int32), in rev_ap's memory. Changing the model fades the old one's block out and clears both buffers. */
#define SP_LEN 4096u /* the loop's line in rev_comb (4937 samples) */
#define SP_MASK (SP_LEN - 1u)
#define SP_N 10u /* allpass stages */
#define SP_A 2867 /* their coefficient, Q12 (0.7: Q12 keeps (x - o) * a in 32 bits up to
* |x - o| < 749000, far past any peak the chain reaches) */
_Static_assert(sizeof rev_comb / 2u >= SP_LEN && sizeof rev_u.sp / 4u >= 4u * (SP_N + 1u), "SPRING in ROOM's buffers");
/* (Skiff phase 9) FX > DRIVE: DIST's kind (P_DTYPE) at the same drive (P_DIST), and its TONE (P_DTONE, -64..63: the
* tone low-pass moved from where the drive puts it; 0 as before). SOFT: the soft clip below, as before. HARD: the
* drive into a flat clip at 3/4 of full scale (odd harmonics: a buzz). FOLD: the drive into a triangle wavefolder, a
* fold every 4 x 16384 (the louder, the more folds: bright, hollow). BITS: the drive as bits and rate taken away, up
* to 11 bits, each sample held 1 2 4 or 8 (a run starts every block: CTL is a multiple of 8; aliasing, grit), its
* low-pass closing less. Each after the low cut, before the two-pole tone and the make-up, as SOFT */
enum { DT_SOFT, DT_HARD, DT_FOLD, DT_BITS };
#define DIST_TONE(k, t) clamp((k) + (t)->p[P_DTONE] * 150, 2000, 32700) /* TONE's move of the low-pass (Q15) */
static __attribute__((noinline)) void dist_hard(track_t *t, int32_t *b, uint32_t n, int32_t g, int32_t k, int32_t mk)
{
for (uint32_t i = 0; i < n; i++) {
int32_t x = b[i], y;
t->dist_hp += (x - t->dist_hp + 64) >> 7;
x = clamp(x - t->dist_hp, -230000, 230000);
y = clamp(((x >> 2) * g) >> 10, -24576, 24576);
t->dist_dc += (y - t->dist_dc + 256) >> 9; /* (~14 Hz: a clip of an uneven wave has a mean, kept out of */
y -= t->dist_dc; /* the buses' feedback) */
t->dist_lp1 += mulq15(y - t->dist_lp1, k);
t->dist_lp2 += mulq15(t->dist_lp1 - t->dist_lp2, k);
b[i] = mulq15(t->dist_lp2, mk);
}
}
static __attribute__((noinline)) void dist_fold(track_t *t, int32_t *b, uint32_t n, int32_t g, int32_t k, int32_t mk)
{
for (uint32_t i = 0; i < n; i++) {
int32_t x = b[i], y, u;
t->dist_hp += (x - t->dist_hp + 64) >> 7;
x = clamp(x - t->dist_hp, -230000, 230000);
u = ((((x >> 2) * g) >> 10) + 16384) & 65535; /* (two's complement: the mask is the fold's period) */
y = (u < 32768 ? u - 16384 : 49152 - u) * 2; /* -32768 .. 32768, the wave itself inside */
t->dist_dc += (y - t->dist_dc + 256) >> 9; /* (as HARD's) */
y -= t->dist_dc;
t->dist_lp1 += mulq15(y - t->dist_lp1, k);
t->dist_lp2 += mulq15(t->dist_lp1 - t->dist_lp2, k);
b[i] = mulq15(t->dist_lp2, mk);
}
}
static __attribute__((noinline)) void dist_bits(track_t *t, int32_t *b, uint32_t n, int32_t d, int32_t k, int32_t mk)
{
uint32_t s = (uint32_t)d / 11u, hm = (1u << ((uint32_t)d >> 5)) - 1u; /* bits taken 0..11, held 1 2 4 8 */
int32_t h = 0, r = s ? 1 << (s - 1u) : 0;
for (uint32_t i = 0; i < n; i++) {
int32_t x = b[i];
t->dist_hp += (x - t->dist_hp + 64) >> 7;
x = clamp(x - t->dist_hp, -230000, 230000);
if (!(i & hm))
h = ((x + r) >> s) << s; /* (rounded: no offset) */
t->dist_lp1 += mulq15(h - t->dist_lp1, k);
t->dist_lp2 += mulq15(t->dist_lp1 - t->dist_lp2, k);
b[i] = mulq15(t->dist_lp2, mk);
}
}
/* DIST: low cut -> drive (1x..8x, exponential) -> asymmetric soft clip
* (a little bias = even harmonics) -> tone low-pass that closes with drive ->
* make-up gain (straight into tanh over the full band, it would sound like a
* broken digital fuzz). State per part (track_t dist_*). (Skiff phase 9) DRIVE's TYPE and TONE: above */
static void track_dist(track_t *t, int32_t *b, uint32_t n)
{
int32_t d = t->p[P_DIST], i, g, k, mk, bias = 2400, b0;
if (!d)
return; /* states kept: switching on does not click */
g = 4096 + d * d * 2; /* Q12: 1x .. ~9x, gentle at first */
k = DIST_TONE(32000 - d * 95, t); /* tone: transparent at low drive .. ~3 kHz, Q15 */
mk = 30000 - d * 120; /* make-up */
switch (t->p[P_DTYPE]) {
case DT_HARD: dist_hard(t, b, n, g, k, mk); return;
case DT_FOLD: dist_fold(t, b, n, g, k, mk); return;
case DT_BITS: dist_bits(t, b, n, d, DIST_TONE(32700 - d * 40, t), 32000 - d * 20); return;
default: break;
}
b0 = softclip(bias);
for (i = 0; i < (int32_t)n; i++) {
int32_t x = b[i], y;
t->dist_hp += (x - t->dist_hp + 64) >> 7; /* ~55 Hz low cut: keep the bass out of the clipper */
x = clamp(x - t->dist_hp, -230000, 230000); /* (x >> 2) * g fits 32 bits; the clip is flat out there */
y = softclip((((x >> 2) * g) >> 10) + bias) - b0; /* >> 2 first: no overflow for loud poly */
t->dist_lp1 += mulq15(y - t->dist_lp1, k); /* two poles: tames the fizz */
t->dist_lp2 += mulq15(t->dist_lp1 - t->dist_lp2, k);
b[i] = mulq15(t->dist_lp2, mk);
}
}
/* master: peak limiter in front of the soft clipper. Fast attack (~0.1 ms),
* ~150 ms release, threshold where tanh is still nearly linear, so chords
* get quieter instead of crushed. */
#define LIM_T 18000
static int32_t lim_env = LIM_T;
/* MENU > USB LEVEL FIXED (for #42: record over USB with the speaker turned down): the mix goes to master_out at the
* full MASTER level, USB audio takes that (audio.c uac_tap), and only then does MASTER scale what the DAC gets
* (usb_fixed_dac). MASTER (0, the default): MASTER before master_out, as always (USB follows the knob) */
static volatile uint8_t fx_usb_fixed;
#define MASTER_FULL 4096 /* main.c: the MASTER knob's top, Q12 */
/* the MASTER pot (ADC 0..1023, smoothed in main.c) to its level, square law: 0 .. 4088 (Q12) */
static inline uint32_t master_of_pot(uint32_t k10) { return (k10 * k10) >> 8; }
static __attribute__((noinline)) void usb_fixed_dac(int32_t *out, uint32_t n) /* audio ISR, after uac_tap */
{
uint32_t i;
int32_t m = (int32_t)song.master_q12;
for (i = 0; i < 2u * n; i++)
out[i] = (out[i] * m) >> 12; /* (|out| <= 32767 after the soft clip: fits) */
}
static volatile uint8_t fx_lowcut; /* settings: 1 LOWCUT 12 dB/oct ~110 Hz, 2 BASS+ (the small speaker):
* 12 dB/oct ~220 Hz plus the harmonics of the bass (spk_bass) */
static int32_t lc_l1, lc_l2, lc_r1, lc_r2, dc_l, dc_r, dce_l, dce_r;
/* DC blocker (~2 Hz), always on: a leaky integrator of the input (Q6 state) subtracted from it.
* The >> 12 step keeps its remainder (error feedback, 0..4095) and adds it to the next one, so no
* part of the step is lost: the state follows the input exactly, down to 0 after the sound stops.
* (A rounded step of (x - dc) / 4096 would stop moving at |x - dc| < 2048 and leave an offset of up
* to +-31 at the output after silence.) */
static inline int32_t dc_block(int32_t x, int32_t *dc, int32_t *err)
{
int32_t e = (x << 6) - *dc + *err, d = e >> 12;
*err = e - (d << 12);
*dc += d;
return x - ((*dc + 32) >> 6);
}
static int32_t lce[4];
static inline int32_t lowcut1(int32_t x, int32_t *lc, int32_t *err, uint32_t sh) /* x minus its one-pole low-pass */
{
int32_t e = x - *lc + *err, d = e >> sh;
*err = e - (d << sh);
*lc += d;
return x - *lc;
}
/* BASS+: what the speaker cannot play, heard through its harmonics. The bass below ~150 Hz is clipped at its
* own envelope (a level-following trapezoid: odd harmonics), then band-passed ~220 Hz..1 kHz and added.
* The low-pass has 4 poles (#42: with 2, the trapezoid rebuilt the 300 .. 600 Hz of the mix itself, late,
* and cancelled up to 6 dB of it; now under 0.5 dB) */
static int32_t sb_lp1, sb_lp2, sb_lp3, sb_lp4, sb_env, sb_h1, sb_h2, sb_hl;
static inline int32_t spk_bass(int32_t m)
{
int32_t a, t, u;
sb_lp1 += ((m - sb_lp1) * 692) >> 15;
sb_lp2 += ((sb_lp1 - sb_lp2) * 692) >> 15;
sb_lp3 += ((sb_lp2 - sb_lp3) * 692) >> 15;
sb_lp4 += ((sb_lp3 - sb_lp4) * 692) >> 15;
a = sb_lp4 < 0 ? -sb_lp4 : sb_lp4;
if (a > sb_env)
sb_env += (a - sb_env) >> 2;
else if (sb_env > 0)
sb_env -= (sb_env >> 11) + 1;
t = clamp(sb_lp4 * 8, -sb_env, sb_env);
sb_h1 += (t - sb_h1) >> 5;
u = t - sb_h1;
sb_h2 += (u - sb_h2) >> 5;
u -= sb_h2;
sb_hl += (u - sb_hl) >> 3;
return sb_hl * 3;
}
static inline void master_out(int32_t *l, int32_t *r)
{
int32_t al, ar, a;
*l = dc_block(*l, &dc_l, &dce_l);
*r = dc_block(*r, &dc_r, &dce_r);
if (fx_lowcut) { /* two one-pole high-passes, error feedback as dc_block (the */
uint32_t sh = fx_lowcut == 2u ? 5u : 6u; /* rounded step stopped at |x - lc| < 32: an offset) */
int32_t b = fx_lowcut == 2u ? spk_bass((*l + *r) >> 1) : 0;
*l = lowcut1(*l, &lc_l1, &lce[0], sh);
*l = lowcut1(*l, &lc_l2, &lce[1], sh) + b;
*r = lowcut1(*r, &lc_r1, &lce[2], sh);
*r = lowcut1(*r, &lc_r2, &lce[3], sh) + b;
}
al = *l < 0 ? -*l : *l;
ar = *r < 0 ? -*r : *r;
a = al > ar ? al : ar;
if (a > lim_env)
lim_env += (a - lim_env) >> 2;
else if (lim_env > LIM_T)
lim_env -= ((lim_env - LIM_T) >> 12) + 1;
if (lim_env > LIM_T) {
int32_t g = (int32_t)(((uint32_t)LIM_T << 15) / (uint32_t)lim_env); /* < 32768 */
*l = ((*l >> 4) * g) >> 11; /* >> 4 first: |l| may be far above Q15 */
*r = ((*r >> 4) * g) >> 11;
}
*l = softclip(*l);
*r = softclip(*r);
}
/* length of one division (N_DIV order) in samples at the song tempo */
static const uint8_t DIV_DEN[6] = {1, 2, 4, 8, 3, 6}; /* original IDs stay fixed; slow rates append */
static uint32_t midi_beat_samples; /* zero until an external clock has a measured tempo */
static uint32_t beat_samples(void)
{
return song.g[G_CLOCK] && midi_beat_samples ? midi_beat_samples : (uint32_t)FS * 60u / (uint32_t)song.g[G_BPM];
}
static uint32_t div_samples(uint32_t div)
{
uint32_t quarter = beat_samples();
return div < 6u ? quarter / DIV_DEN[div] : div < 10u ? quarter << (div - 5u) : quarter / DIV_DEN[div % 6u];
}
#include "perform.c" /* the FX hold layer's effects (the master) */
#include "click.c" /* the metronome's click (after the master: audio.c) */
#include "duck.c" /* (Skiff) ducking: a track under another's hits (mix_part) */
#include "throw.c" /* (Skiff) throws: the PERFORM layer's KNOB 1..4 (mix_block) */
static uint32_t delay_samples(void)
{
uint32_t s = div_samples((uint32_t)song.g[G_DTIME]);
return s < 16u ? 16u : s >= DLY_LEN ? DLY_LEN - 1u : s;
}
/* ROOM (G_RTYPE 0): 4 damped combs + 2 allpasses (Freeverb-like, mono), added to out */
static __attribute__((noinline)) void rev_room(const int32_t *rev_in, int32_t *out, uint32_t n)
{
uint32_t i, k;
int32_t size = 25000 + song.g[G_RSIZE] * 50, damp = 32767 - song.g[G_RDAMP] * 200;
for (i = 0; i < n; i++) {
int32_t a = 0;
int16_t *c = rev_comb;
int32_t in = mulq15(rev_in[i], 2580); /* 1/8 at -4 dB: level as before the allpass fix */
for (k = 0; k < 4u; k++) {
int32_t o = c[fx.comb_i[k]];
fx.comb_lp[k] = o + mulq15(fx.comb_lp[k] - o, 32767 - damp);
c[fx.comb_i[k]] = (int16_t)clamp(in + mulq15(fx.comb_lp[k], size), -32768, 32767);
if (++fx.comb_i[k] >= REV_COMB[k])
fx.comb_i[k] = 0;
a += o;
c += REV_COMB[k];
}
c = rev_ap;
for (k = 0; k < 2u; k++) {
int32_t o = c[fx.ap_i[k]];
int32_t v = a + (o >> 1);
c[fx.ap_i[k]] = (int16_t)clamp(v, -32768, 32767);
a = o - a; /* Freeverb: out = buf - in (o - v is a notch comb) */
if (++fx.ap_i[k] >= REV_AP[k])
fx.ap_i[k] = 0;
c += REV_AP[k];
}
out[i] += a;
}
}
/* (Skiff phase 9) HALL (G_RTYPE 2): ROOM's combs and allpasses at half the rate, in ROOM's own buffers: every other
* sample, the pair's mean in, and from the last output to this one a straight line out. Twice the lengths (combs of
* 50 .. 61 ms): a bigger, darker space (nothing above ~11 kHz), each SIZE a longer tail; about half ROOM's work */
static __attribute__((noinline)) void rev_hall(const int32_t *rev_in, int32_t *out, uint32_t n)
{
uint32_t i, k;
int32_t size = 25000 + song.g[G_RSIZE] * 50, damp = 32767 - song.g[G_RDAMP] * 200;
for (i = 0; i + 1u < n; i += 2u) {
int32_t a = 0;
int16_t *c = rev_comb;
int32_t in = mulq15((rev_in[i] + rev_in[i + 1u]) >> 1, 2580);
for (k = 0; k < 4u; k++) {
int32_t o = c[fx.comb_i[k]];
fx.comb_lp[k] = o + mulq15(fx.comb_lp[k] - o, 32767 - damp);
c[fx.comb_i[k]] = (int16_t)clamp(in + mulq15(fx.comb_lp[k], size), -32768, 32767);
if (++fx.comb_i[k] >= REV_COMB[k])
fx.comb_i[k] = 0;
a += o;
c += REV_COMB[k];
}
c = rev_ap;
for (k = 0; k < 2u; k++) {
int32_t o = c[fx.ap_i[k]];
int32_t v = a + (o >> 1);
c[fx.ap_i[k]] = (int16_t)clamp(v, -32768, 32767);
a = o - a;
if (++fx.ap_i[k] >= REV_AP[k])
fx.ap_i[k] = 0;
c += REV_AP[k];
}
out[i] += (fx.hall_y + a) >> 1;
out[i + 1u] += a;
fx.hall_y = a;
}
}
/* SPRING (see the top), added to out */
static __attribute__((noinline)) void rev_spring(const int32_t *rev_in, int32_t *out, uint32_t n)
{
uint32_t i, k, s = (uint32_t)song.g[G_RSIZE];
int32_t g = 19661 + (int32_t)s * 85; /* the loop's gain: 0.6 .. 0.93 */
int32_t kl = 26000 - song.g[G_RDAMP] * 160; /* its low-pass: ~9 kHz .. ~1.3 kHz */
int32_t len = (int32_t)(1323u + ((s * 1323u) >> 7)) << 8, L, L2, L3, f, w;
int16_t *ln = rev_comb;
int32_t *ap = rev_u.sp;
if (!fx.sp_size)
fx.sp_size = len;
fx.sp_size += clamp(len - fx.sp_size, -256, 256); /* SIZE glides (a sample a block at most) */
L = fx.sp_size >> 8;
fx.sp_ph += 2u * LFO_INC[24]; /* the wobble: a slow sine, 1.5 samples deep */
w = (fx.sp_size >> 1) + ((osc_sine(fx.sp_ph) * 3) >> 8); /* the far end, Q8 */
L2 = w >> 8;
f = w & 255;
L3 = (L * 3) >> 2;
for (i = 0; i < n; i++) {
uint32_t wp = fx.sp_w, j = (wp & 3u) * (SP_N + 1u);
int32_t x = mulq15(rev_in[i], 2580), r = ln[(wp - (uint32_t)L) & SP_MASK], p, o;
int32_t t0 = ln[(wp - (uint32_t)L2) & SP_MASK], t1 = ln[(wp - (uint32_t)L2 - 1u) & SP_MASK];
fx.sp_lp += mulq15(r - fx.sp_lp, kl);
o = fx.sp_lp * g;
x += (o + ((o >> 31) & 32767)) >> 15; /* towards 0: a loop of floors would hold an offset */
o = x - fx.sp_hp + fx.sp_he; /* the low cut, its step's remainder kept (as */
fx.sp_he = o & 63; /* dc_block): no dead band to hold an offset in the loop */
fx.sp_hp += o >> 6;
x -= fx.sp_hp;
p = ap[j]; /* the chain: ap[j + k], stage k's output 4 samples ago */
ap[j] = x;
for (k = 1; k <= SP_N; k++) { /* (lossless: bounded by the loop's input, no clamp) */
int32_t v = (x - ap[j + k]) * SP_A; /* towards 0, as the loop's gain: floors would feed */
o = ap[j + k]; /* the loop a little offset and noise for ever */
x = ((v + ((v >> 31) & 4095)) >> 12) + p;
p = o;
ap[j + k] = x;
}
ln[wp & SP_MASK] = (int16_t)clamp(x, -32768, 32767);
fx.sp_w = (uint16_t)(wp + 1u);
out[i] += (t0 + (((t1 - t0) * f) >> 8)) * 4 + ln[(wp - (uint32_t)L3) & SP_MASK] * 2;
}
}
/* the reverb's buffers and states to silence (the model changed) */
static void rev_clear(void)
{
uint32_t i;
for (i = 0; i < sizeof rev_comb / 2u; i++)
rev_comb[i] = 0;
for (i = 0; i < sizeof rev_u.ap / 2u; i++) /* (int16: 997 of them, an odd count the int32 view misses one of) */
rev_u.ap[i] = 0;
for (i = 0; i < 4u; i++)
fx.comb_lp[i] = 0;
fx.sp_lp = fx.sp_hp = fx.sp_he = 0;
fx.hall_y = 0;
}
static void rev_model(uint32_t m, const int32_t *rev_in, int32_t *out, uint32_t n) /* G_RTYPE m, added to out */
{
if (m == 1u)
rev_spring(rev_in, out, n);
else if (m == 2u)
rev_hall(rev_in, out, n);
else
rev_room(rev_in, out, n);
}
/* (Skiff phase 9) the CHORUS bus in its MODE (G_CMODE) with FEEDBACK (G_CFDBK, 0 .. 127: up to 0.93 of its output fed
* back in), wet into wet (its part: the delay's is added after). CHORUS: the modulated delay of 5 .. 15 ms as before.
* FLANGER: the same line swept 1 .. 7 ms, its comb against the dry tracks the jet. PHASER: four first-order allpasses on
* the send, swept together by the chorus LFO once a block (RATE; DEPTH the sweep), notches against the dry. CHORUS
* without FEEDBACK is the loop in fx_buses, bit for bit */
static __attribute__((noinline)) void cho_mode(const int32_t *cho_in, const int32_t *dly_in, int32_t *wet, uint32_t n,
uint32_t cinc)
{
uint32_t i, k, mode = (uint32_t)song.g[G_CMODE], dl = delay_samples();
int32_t fb = song.g[G_CFDBK] * 240, depth = song.g[G_CDEPTH];
int32_t dfb = song.g[G_DFDBK] * 230, col = 2000 + song.g[G_DCOLOR] * 240, dmix = song.g[G_DMIX] * 258;
perf_dub_dly(&dfb, &dmix); /* ((phase 9) DUB's) */
if (mode == 2u) { /* PHASER: a = -0.95 .. -0.15 by the LFO (Q15) */
int32_t lfo = (osc_sine(fx.cho_ph) + 32768) >> 1, a; /* 0 .. 32767 */
fx.cho_ph += cinc * n;
a = -31130 + mulq15(lfo, 3000 + depth * 205);
for (i = 0; i < n; i++) {
int32_t x = (cho_in[i] >> 1) + mulq15(fx.cho_y, fb);
for (k = 0; k < 4u; k++) { /* (a + z^-1) / (1 + a z^-1): w = x - a w1, y = a w + w1 */
int32_t w = clamp(x - mulq15(fx.ph_s[k], a), -262144, 262144);
x = mulq15(w, a) + fx.ph_s[k];
fx.ph_s[k] = w;
}
fx.cho_y = clamp(x, -65536, 65536);
wet[i] = fx.cho_y << 1;
}
} else
for (i = 0; i < n; i++) { /* CHORUS (with FEEDBACK), FLANGER */
int32_t r, f, c0, c1;
uint32_t ri;
cho_buf[fx.cho_w & (CHO_LEN - 1u)] = (int16_t)clamp((cho_in[i] >> 1) + mulq15(fx.cho_y, fb), -32768, 32767);
fx.cho_ph += cinc;
r = mode == 1u ? (44 << 8) + ((osc_sine(fx.cho_ph) + 32768) * depth >> 7) /* 1 .. 7 ms, Q8 */
: (400 << 8) + ((osc_sine(fx.cho_ph) + 32768) * depth * 6 >> 8);
ri = (uint32_t)r >> 8;
f = r & 255;
c0 = cho_buf[(fx.cho_w - ri) & (CHO_LEN - 1u)];
c1 = cho_buf[(fx.cho_w - ri - 1u) & (CHO_LEN - 1u)];
fx.cho_y = c0 + (((c1 - c0) * f) >> 8);
fx.cho_w++;
wet[i] = fx.cho_y << 1;
}
for (i = 0; i < n; i++) { /* the delay as in fx_buses */
int32_t x = dly_buf[(fx.dly_w - dl) & (DLY_LEN - 1u)];
fx.dly_lp += mulq15(x - fx.dly_lp, col);
dly_buf[fx.dly_w & (DLY_LEN - 1u)] = (int16_t)clamp((dly_in[i] >> 1) + mulq15(fx.dly_lp, dfb), -32768, 32767);
fx.dly_w++;
wet[i] += mulq15(x << 1, dmix);
}
}
static int32_t part_buf[CTL]; /* a part's block (mix_part); the fade of a model change */
/* process the three buses for one block; sends in, wet stereo-equal out */
static void fx_buses(const int32_t *cho_in, const int32_t *dly_in, const int32_t *rev_in, int32_t *wet,
uint32_t n)
{
uint32_t i, dl = delay_samples();
int32_t fb = song.g[G_DFDBK] * 230, col = 2000 + song.g[G_DCOLOR] * 240;
int32_t dmix = song.g[G_DMIX] * 258;
int32_t cdepth = song.g[G_CDEPTH] * 6, rt;
uint32_t cinc = LFO_INC[song.g[G_CRATE] & 127] / CTL;
perf_dub_dly(&fb, &dmix); /* ((phase 9) DUB's) */
if (song.g[G_CMODE] || song.g[G_CFDBK]) /* (Skiff phase 9) another MODE, FEEDBACK: cho_mode, the delay */
cho_mode(cho_in, dly_in, wet, n, cinc); /* too */
else
for (i = 0; i < n; i++) {
int32_t y = 0, x, r;
/* chorus: modulated short delay, 5..15 ms */
cho_buf[fx.cho_w & (CHO_LEN - 1u)] = (int16_t)clamp(cho_in[i] >> 1, -32768, 32767);
fx.cho_ph += cinc;
r = (400 << 8) + ((osc_sine(fx.cho_ph) + 32768) * cdepth >> 8); /* Q8 delay: read between samples */
{
uint32_t ri = (uint32_t)r >> 8;
int32_t f = r & 255, c0 = cho_buf[(fx.cho_w - ri) & (CHO_LEN - 1u)];
int32_t c1 = cho_buf[(fx.cho_w - ri - 1u) & (CHO_LEN - 1u)];
y += (c0 + (((c1 - c0) * f) >> 8)) << 1;
}
fx.cho_w++;
/* delay with a low-passed feedback */
x = dly_buf[(fx.dly_w - dl) & (DLY_LEN - 1u)];
fx.dly_lp += mulq15(x - fx.dly_lp, col);
dly_buf[fx.dly_w & (DLY_LEN - 1u)] =
(int16_t)clamp((dly_in[i] >> 1) + mulq15(fx.dly_lp, fb), -32768, 32767);
fx.dly_w++;
y += mulq15(x << 1, dmix);
wet[i] = y;
}
rt = song.g[G_RTYPE] == 1 || song.g[G_RTYPE] == 2 ? song.g[G_RTYPE] : 0; /* (phase 9) HALL) */
if (rt != fx.rtype) { /* the model changed: the old one's block fades out, */
int32_t *t = part_buf, g = 65536, d = 65536 / (int32_t)n; /* its buffers are cleared, the new */
for (i = 0; i < n; i++) /* one starts from silence */
t[i] = 0;
rev_model(fx.rtype, rev_in, t, n);
for (i = 0; i < n; i++, g -= d)
wet[i] += mulq16(t[i], (uint32_t)g);
rev_clear();
fx.rtype = (uint8_t)rt;
return;
}
rev_model((uint32_t)rt, rev_in, wet, n);
}
/* one block of the whole mix (shared with hostsim.c): events -> each part (with its modulation matrix)
* -> dist -> SLICER -> level / pan / sends -> buses -> master; out: stereo Q15 */
static void events_block(uint32_t n); /* seq.c */
static int32_t send_c[CTL], send_d[CTL], send_r[CTL], wet[CTL], mix_l[CTL], mix_r[CTL];
/* one synth part into the dry mix and the sends; a part with no voice sounding costs
* the LFO tick and a cleared buffer only (after the DIST tail has run out) */
static void mix_part(track_t *t, uint32_t n)
{
int32_t *b = part_buf;
uint32_t i;
mod_begin(t); /* the matrix's per-block values into t->p (mod.c) */
if (track_render(t, b, n))
t->tail = 16; /* blocks of DIST state to run out after the last voice */
else if ((!t->tail || !t->p[P_DIST] || !--t->tail) && !slicer_busy(t)) {
slicer_track(t, 0, n); /* (the SLICER's step clock runs on) */
if (mod.on)
mod_end(t);
return;
}
{
int32_t lvl = LEVEL_Q12[t->p[P_LEVEL] & 127], pan = t->p[P_PAN];
int32_t gl = 4096 - (pan > 0 ? pan * 64 : 0), gr = 4096 + (pan < 0 ? pan * 64 : 0);
int32_t c = t->p[P_CHOR] * 258, d = t->p[P_DLY] * 258, r = t->p[P_REV] * 258, pk = t->peak;
int32_t xmax = c > d ? c : d;
xmax = 0x7FFFFFFF / ((xmax > r ? xmax : r) | 1); /* sends: loud chords at a high LEVEL */
track_dist(t, b, n);
slicer_track(t, b, n); /* slicer.c: before the level, pan and sends */
if ((pf.mute >> (t - trk)) & 1u)
AUDIO_FAR(perf_mute)((uint32_t)(t - trk), b, n); /* perform.c: a black key in the FX layer (RAM) */
if ((duck_on >> (t - trk)) & 1u)
duck_apply((uint32_t)(t - trk), b, n); /* (Skiff) duck.c: under its source's hits */
for (i = 0; i < n; i++) {
int32_t x = ((b[i] >> 2) * lvl) >> 10, a = x < 0 ? -x : x; /* pre-shift: 8 loud voices */
int32_t xs = clamp(x, -xmax, xmax); /* sends: mulq15 would overflow */
if (a > pk)
pk = a;
if (c)
send_c[i] += mulq15(xs, c);
if (d)
send_d[i] += mulq15(xs, d);
if (r)
send_r[i] += mulq15(xs, r);
mix_l[i] += (x * gl) >> 12;
mix_r[i] += (x * gr) >> 12;
}
t->peak = pk;
}
if (mod.on)
mod_end(t); /* the stored values back */
}
/* the master with the FX layer's effects between its level and master_out (perform.c) */
static __attribute__((noinline)) AUDIO_RAM void perf_master(int32_t *out, uint32_t n)
{
uint32_t i;
int32_t mg = fx_usb_fixed ? MASTER_FULL : (int32_t)song.master_q12; /* (USB LEVEL FIXED: MASTER after) */
void (*mo)(int32_t *, int32_t *) = AUDIO_FAR(master_out); /* (Skiff: XIP, from RAM) */
for (i = 0; i < n; i++) {
mix_l[i] = (((mix_l[i] + wet[i]) >> 2) * mg) >> 10;
mix_r[i] = (((mix_r[i] + wet[i]) >> 2) * mg) >> 10;
}
perf_block(mix_l, mix_r, n);
for (i = 0; i < n; i++) {
int32_t l = mix_l[i], r = mix_r[i];
mo(&l, &r);
out[2u * i] = l;
out[2u * i + 1u] = r;
}
}
static void mix_block(int32_t *out, uint32_t n)
{
uint32_t i;
int perf;
int32_t mg = fx_usb_fixed ? MASTER_FULL : (int32_t)song.master_q12; /* (USB LEVEL FIXED: MASTER after) */
for (i = 0; i < n; i++)
send_c[i] = send_d[i] = send_r[i] = mix_l[i] = mix_r[i] = 0;
if (thr_out)
throw_on(n); /* (Skiff) the throws on for the block, its events too */
events_block(n);
duck_block(n); /* (Skiff) the ducking's gains (duck.c) */
perf = AUDIO_FAR(perf_begin)(n); /* the FX hold layer at work (perform.c; its code in RAM) */
for (i = 0; i < NPART; i++)
mix_part(&trk[i], n);
if (thr_on)
throw_off(); /* (Skiff) .. and off: nothing else sees them */
if (perf)
AUDIO_FAR(perf_pre)(mix_l, mix_r, send_d, send_r, n);
fx_buses(send_c, send_d, send_r, wet, n);
if (perf) {
AUDIO_FAR(perf_master)(out, n);
return;
}
for (i = 0; i < n; i++) {
int32_t l = (((mix_l[i] + wet[i]) >> 2) * mg) >> 10;
int32_t r = (((mix_r[i] + wet[i]) >> 2) * mg) >> 10;
master_out(&l, &r);
out[2u * i] = l;
out[2u * i + 1u] = r;
}
}