SFX engine (wauvio/sfx)

Recipes, trajectories and modulators

See it in action: Build a sound from scratch

Common utilities (sfx_common.hpp)

  • sfx::Error — exception type (std::runtime_error) used for every validation failure.
  • Math: clampd, lerp, midi_to_hz, hz_to_midi, db_to_lin, lin_to_db, semis_to_ratio, is_finite.
  • Hashing / randomness: hash64, mix_seed(a, b), hash01, value_noise(x, seed), and the Rng class (next, uniform, bipolar, range, chance, index, gaussian).
  • Music tables: scale_names(), scale_degrees(i), scale_step(deg, k), chord_names(), chord_intervals(i). Scales: chromatic, major, natural_minor, harmonic_minor, pentatonic_major, pentatonic_minor, whole_tone, dorian, phrygian, lydian, mixolydian. Chords: major, minor, sus2, sus4, major7, minor7, dominant7, diminished, augmented, add9, cluster.

Traj — parameter trajectory

A piecewise function of normalised time u ∈ [0, 1] (the position inside a layer). Construct from a constant (Traj(5.0) converts implicitly) or add keys in time order with to(t, value, Interp); the interpolation of a key describes how the segment arriving at it is shaped. Interp = Linear, Exp, Smooth, Step (Exp requires both end values positive, otherwise it falls back to linear). Non-finite numbers throw.

Factory / methodMeaning
Traj::linear(a, b), exp(a, b), smooth(a, b)Two-key trajectories
Traj::pitch_hz(a_hz, b_hz, interp)Sweep given in Hz but stored as MIDI notes
Traj::ar(attack, peak, floor)Smooth attack over the first fraction, then exponential decay to floor
Traj::decay(floor)Exponential decay from 1
Traj::stairs(a, b, n)Staircase of n steps
at(u)Evaluate (sanitises its input)
scaled(k), shifted(d)Return modified copies
min_value(), max_value(), key_count()Inspection

Mod — stateless LFO

Mod::Shape = Sine, Triangle, Square, Saw, Smooth, Steps (the last two are seeded random). Fields: rate_hz, beats (if > 0 one cycle per that many beats, tempo-synced), depth (output is ±depth), phase (cycles). active(), eval(t_sec, bpm, seed). Because it is evaluated from time alone, layers can render in any order.

Helpers (sfx_helpers.hpp)

  • random_stairs(rng, n, lo, hi, snap), env_bell(center, width, keys, floor), env_swell(peak_at, start, end), env_hit(attack, floor), lfo(shape, rate_hz, depth, phase), lfo_beats(shape, beats_per_cycle, depth, phase), hashed_pattern(seed, n, density).

Recipe

Recipe is the plain-data description of an effect: name, duration (nominal seconds), tail (extra seconds), bpm (120), reverse_all, fade_in_ms (1), fade_out_ms (6) and layers. add(name) appends and returns a LayerSpec&; total_seconds().

LayerSpec

One layer = one source → envelope → processor chain → post automation → level/pan. Every builder returns *this so calls chain. Fields env, post, level, start, length (<0 = to the end), tail, pan, width (0 mono-panned … 1 independently rendered L/R), fade_in_ms, fade_out_ms, seed_offset.

Sources (choose one per layer)

BuilderSource kindNotes
osc(Wave, pitch)OscWave: Sine, Triangle, Saw, Square, Pulse. Options: unison(voices, cents), fm(index, ratio), sub(g), stack(ratios), pw(width)
noise(NoiseColor, density)NoiseColors: White, Pink, Brown, Blue, Violet, Dust (density = events/s for Dust)
additive(pitch, notes, harmonics, rolloff)AdditivePer-note partial stacks; inharmonic(B), spread(cents)
modal(modes, pitch, hits)ModalResonant bodies struck at normalised times; hit_notes, hit_jitter, strike(ms), decay_scale. Mode tables: modes_bell/glass/metal/gong/wood(), modes_inharmonic(seed, n, t60, spread)
formant(vowel, pitch, breath)FormantVowel 0=a 1=e 2=i 3=o 4=u (interpolated); shift(semis), whisper(breath)
grain(GrainSource, density, size_ms, pitch)GrainSources: Noise, Tone, Chord, Bell, Vowel; grain_pos, grain_pitch, scatter(pitch, position)
pluck(pitch, hits, damping)PluckString-style decay; damping 0 bright … 1 dull
chaos(ChaosKind, rate, pitch)ChaosLorenz, Logistic or Henon attractors; chaos_depth(semis, amp), chaos_out(direct)

Shaping and placement

shape(Traj) (envelope before the chain), after(Traj) (automation after the chain), gain(level), at(sec), len(sec), ring(sec), place(pan, width), fade(in_ms, out_ms), variation(seed_offset).

Processors (applied in call order)

BuilderProcessorDescription
lp / hp / bp(cutoff, q, poles), notch(cutoff, q)FilterPTrajectory-driven filters; filter_lfo(Mod) adds an LFO (depth in octaves) to the latest filter
drive(amount, kind, mix)DrivePKinds: Tanh, Hard, Fold, Asym, Rectify
crush(bits, down)CrushPBit/sample-rate reduction
gate(steps, step_beats, depth, smooth_ms, swing), gate_sec(...)GatePStep-sequenced gate, tempo-synced or in seconds
trem(rate, depth)TremPTremolo
pulses(rate, decay_ms, depth, jitter), pulses_at(times, ...)PulsesPAmplitude pulse trains at a rate or explicit times
stutter(seg_sec, repeats, prob, from, to)StutterPProbabilistic segment repeats
freeze(at_frac, window_sec)FreezePSustain a captured window
dropout(rate_per_sec, min_ms, max_ms, depth)DropoutPRandom signal dropouts
comb(freq, feedback, mix)CombPComb filter
ringmod(freq, mix)RingPRing modulation
reverb(t60, damping, mix, predelay_ms)ReverbPSmall synthetic Schroeder tail (not convolution)
echo(time_sec, feedback, mix, damping)EchoPFeedback echo
reverse()ReversePReverse the layer
spectral(mode, amount, at_frac)SpectralPFFT effects: Disintegrate, Scramble, TiltFade, Freeze

The processors are stored in a std::variant named Proc.

Full API listing

wauvio/sfx/core/sfx_common.hpp

wauvio::sfx::class Error

class Error : public std::runtime_error
public:
    explicit Error(const std::string& what) : std::runtime_error(what) {}
inline bool   is_finite(double v) noexcept { return std::isfinite(v); }
inline double clampd(double v, double lo, double hi) noexcept { return v < lo ? lo : (v > hi ? hi : v); }
inline double lerp(double a, double b, double t) noexcept { return a + (b - a) * t; }
inline double midi_to_hz(double m) noexcept { return 440.0 * std::pow(2.0, (m - 69.0) / 12.0); }
inline double hz_to_midi(double f) noexcept { return 69.0 + 12.0 * std::log2(std::max(f, 1e-6) / 440.0); }
inline double db_to_lin(double db) noexcept { return std::pow(10.0, db / 20.0); }
inline double lin_to_db(double v) noexcept { return 20.0 * std::log10(std::max(v, 1e-12)); }
inline double semis_to_ratio(double s) noexcept { return std::pow(2.0, s / 12.0); }
inline uint64_t hash64(uint64_t x) noexcept
inline uint64_t mix_seed(uint64_t a, uint64_t b) noexcept { return hash64(a ^ (hash64(b) + 0x9E3779B97F4A7C15ull + (a << 6) + (a >> 2))); }
inline double hash01(uint64_t x) noexcept { return static_cast<double>(hash64(x) >> 11) * (1.0 / 9007199254740992.0); }

wauvio::sfx::class Rng

class Rng
public:
    explicit Rng(uint64_t seed = 1) noexcept { reseed(seed); }
    void reseed(uint64_t seed) noexcept { uint64_t z = seed; for (auto& s : s_) { z += 0x9E3779B97F4A7C15ull; s = hash64(z); } }
    uint64_t next() noexcept
    double uniform() noexcept { return static_cast<double>(next() >> 11) * (1.0 / 9007199254740992.0); }   // [0,1)
    double bipolar() noexcept { return uniform() * 2.0 - 1.0; }                                              // [-1,1)
    double range(double a, double b) noexcept { return a + (b - a) * uniform(); }
    bool   chance(double p) noexcept { return uniform() < p; }
    int    index(int n) noexcept { return n <= 1 ? 0 : static_cast<int>(uniform() * n) % n; }
    double gaussian() noexcept { const double u1 = std::max(uniform(), 1e-12), u2 = uniform(); return std::sqrt(-2.0 * std::log(u1)) * std::cos(TWO_PI * u2); }
inline double value_noise(double x, uint64_t seed) noexcept
inline const std::vector<std::string>& scale_names()
inline std::vector<int> scale_degrees(int index)
inline int scale_step(const std::vector<int>& deg, int k)
inline const std::vector<std::string>& chord_names()
inline std::vector<double> chord_intervals(int index)

wauvio/sfx/core/sfx_traj.hpp

wauvio::sfx::enum class Interp

enum class Interp { Linear, Exp, Smooth, Step };

wauvio::sfx::class Traj

/// A parameter trajectory over normalised time u in [0, 1] (u = position inside the layer).
/// Keys are added in time order; each key records how the segment *arriving* at it is
/// interpolated. Exp interpolation needs both end values to be positive and falls back to
/// linear otherwise. Non-finite numbers are rejected when building, and at() sanitises its input.
///
///   Traj::exp(200, 12000)                                           // exponential sweep (musically even)
///   Traj(0).to(0.1, 1, Interp::Smooth).to(1, 0.001, Interp::Exp)    // attack then decay
class Traj
public:
    Traj(double constant = 0.0) { check(0.0, constant); k_.push_back({0.0, constant, Interp::Linear}); }   // NOLINT implicit by design
    static Traj linear(double a, double b) { return Traj(a).to(1.0, b, Interp::Linear); }
    static Traj exp(double a, double b)    { return Traj(a).to(1.0, b, Interp::Exp); }
    static Traj smooth(double a, double b) { return Traj(a).to(1.0, b, Interp::Smooth); }
    /// Frequency sweep given in Hz, stored as MIDI notes (so a linear trajectory is an exponential sweep in Hz).
    static Traj pitch_hz(double a_hz, double b_hz, Interp i = Interp::Linear) { return Traj(hz_to_midi(a_hz)).to(1.0, hz_to_midi(b_hz), i); }
    /// Attack to `peak` over the first `attack` fraction, then exponential decay to `floor` at the end.
    static Traj ar(double attack, double peak = 1.0, double floor = 0.001) { attack = clampd(attack, 0.0, 1.0); return Traj(0.0).to(attack, peak, Interp::Smooth).to(1.0, floor, Interp::Exp); }
    /// Exponential decay from 1 to `floor`.
    static Traj decay(double floor = 0.001) { return Traj(1.0).to(1.0, floor, Interp::Exp); }
    /// `n` equal steps from a to b (a staircase), including both end values.
    static Traj stairs(double a, double b, int n)
    /// Add a key at normalised time `t` (must not be earlier than the previous key).
    Traj& to(double t, double v, Interp how = Interp::Linear)
    double at(double u) const noexcept
    Traj scaled(double k) const { Traj t = *this; for (auto& key : t.k_) key.v *= k; return t; }
    Traj shifted(double d) const { Traj t = *this; for (auto& key : t.k_) key.v += d; return t; }
    double min_value() const { double m = k_[0].v; for (auto& k : k_) m = std::min(m, k.v); return m; }
    double max_value() const { double m = k_[0].v; for (auto& k : k_) m = std::max(m, k.v); return m; }
    size_t key_count() const noexcept { return k_.size(); }

wauvio::sfx::struct Mod

/// Low-frequency modulator, evaluated statelessly from time (so any layer can be rendered in any order).
struct Mod
    enum class Shape { Sine, Triangle, Square, Saw, Smooth, Steps };   // Smooth/Steps = random (seeded)
    Shape  shape = Shape::Sine;
    double rate_hz = 0.0;   ///< cycles per second (ignored if `beats` > 0)
    double beats   = 0.0;   ///< if > 0: one cycle per this many beats (tempo-synced)
    double depth   = 0.0;   ///< output range is [-depth, +depth]
    double phase   = 0.0;   ///< cycles
    bool active() const noexcept { return depth != 0.0 && (rate_hz > 0.0 || beats > 0.0); }
    double eval(double t_sec, double bpm, uint64_t seed) const noexcept

wauvio/sfx/core/sfx_helpers.hpp

wauvio::sfx::Free functions, constants and aliases

inline Traj random_stairs(Rng& r, int n, double lo, double hi, bool snap = true)
inline Traj env_bell(double center = 0.5, double width = 0.22, int keys = 24, double floor = 0.0008)
inline Traj env_swell(double peak_at = 0.85, double start = 0.02, double end = 0.0008) { return Traj(start).to(peak_at, 1.0, Interp::Exp).to(1.0, end, Interp::Exp); }
inline Traj env_hit(double a = 0.004, double floor = 0.0008) { return Traj::ar(a, 1.0, floor); }
inline Mod lfo(Mod::Shape s, double rate_hz, double depth, double phase = 0.0) { Mod m; m.shape = s; m.rate_hz = rate_hz; m.depth = depth; m.phase = phase; return m; }
inline Mod lfo_beats(Mod::Shape s, double beats_per_cycle, double depth, double phase = 0.0) { Mod m; m.shape = s; m.beats = beats_per_cycle; m.depth = depth; m.phase = phase; return m; }
inline std::vector<double> hashed_pattern(uint64_t seed, int n, double density)

wauvio/sfx/core/sfx_recipe.hpp

wauvio::sfx::enum class Wave

enum class Wave { Sine, Triangle, Saw, Square, Pulse };

wauvio::sfx::enum class NoiseColor

enum class NoiseColor { White, Pink, Brown, Blue, Violet, Dust };

wauvio::sfx::enum class ChaosKind

enum class ChaosKind { Lorenz, Logistic, Henon };

wauvio::sfx::enum class GrainSource

enum class GrainSource { Noise, Tone, Chord, Bell, Vowel };

wauvio::sfx::struct Mode

struct Mode { double ratio = 1.0; double t60 = 1.0; double amp = 1.0; };   ///< one resonant mode of a modal source
/// Mode tables for metallic / resonant bodies (frequency ratios are relative to the root).
inline std::vector<Mode> modes_bell()  { return { {1.0,3.0,1.0},{2.0,2.2,0.6},{2.4,1.8,0.5},{3.0,1.4,0.4},{4.5,1.0,0.3},{5.33,0.8,0.25},{6.8,0.6,0.15} }; }
inline std::vector<Mode> modes_glass() { return { {1.0,2.6,1.0},{2.32,2.0,0.55},{4.25,1.5,0.45},{6.63,1.1,0.3},{9.38,0.8,0.2} }; }
inline std::vector<Mode> modes_metal() { return { {1.0,1.6,1.0},{1.47,1.3,0.8},{1.98,1.1,0.7},{2.42,1.0,0.55},{2.89,0.8,0.5},{3.51,0.7,0.4},{4.12,0.5,0.35},{5.6,0.4,0.25} }; }
inline std::vector<Mode> modes_gong()  { return { {1.0,5.0,1.0},{1.58,4.0,0.8},{2.12,3.4,0.7},{2.76,2.6,0.55},{3.3,2.0,0.45},{4.37,1.4,0.3},{5.8,1.0,0.2} }; }
inline std::vector<Mode> modes_wood()  { return { {1.0,0.35,1.0},{2.57,0.22,0.6},{4.2,0.14,0.35},{6.1,0.1,0.2} }; }
inline std::vector<Mode> modes_inharmonic(uint64_t seed, int n, double t60, double spread = 1.0)

wauvio::sfx::enum class SourceKind

enum class SourceKind { None, Osc, Noise, Additive, Modal, Formant, Grain, Pluck, Chaos };

wauvio::sfx::struct Source

struct Source
    SourceKind kind = SourceKind::None;
    Traj pitch = 60.0;                       ///< MIDI note (fractional allowed); used by every pitched kind
    // Osc
    Wave wave = Wave::Saw; int voices = 1; Traj detune_cents = 0.0; Traj pulse_width = 0.5;
    double sub = 0.0;                        ///< sine one octave below, relative level
    std::vector<double> ratios;              ///< non-empty: a stack of oscillators at pitch * ratio (e.g. metallic square stacks)
    Traj fm_index = 0.0; double fm_ratio = 1.0;   ///< phase modulation of the carrier by a sine at pitch * fm_ratio
    // Noise
    NoiseColor color = NoiseColor::White; Traj density = 200.0;   ///< density = events per second (Dust only)
    // Additive: each note (semitone offset from `pitch`) gets `harmonics` partials
    std::vector<double> notes { 0.0 }; int harmonics = 1; Traj rolloff = 1.0;   ///< partial n has amplitude n^-rolloff
    double inharmonicity = 0.0;              ///< partial n sits at n*sqrt(1 + B*n^2)
    Traj partial_spread = 0.0;               ///< random per-partial detune in cents (smooth, seeded)
    // Modal / Pluck: events happen at normalised times `hits` (+/- jitter); hit k plays pitch + hit_notes[k % size]
    std::vector<Mode> modes; std::vector<double> hits; std::vector<double> hit_notes; double hit_jitter = 0.0; double velocity_var = 0.3;
    double strike_ms = 1.5;                  ///< noise burst at each hit (the "click" of the attack)
    double t60_scale = 1.0;                  ///< multiplies every mode / string decay time
    double damping = 0.5;                    ///< Pluck: 0 bright & ringing .. 1 dull & short
    // Formant
    Traj vowel = 0.0;                        ///< 0=a 1=e 2=i 3=o 4=u, interpolated
    Traj formant_shift = 0.0;                ///< semitones, moves all formants
    double breath = 0.0;                     ///< 0 voiced .. 1 whispered (noise excitation)
    bool   noise_excited = false;
    // Grain
    GrainSource grain_source = GrainSource::Noise; Traj grain_ms = 60.0; Traj grain_pitch = 0.0;   ///< semitones
    double pitch_scatter = 0.0; Traj position = 0.0; double position_scatter = 0.2; double grain_chord = 0.0;
    // Chaos
    ChaosKind chaos = ChaosKind::Lorenz; Traj chaos_rate = 40.0;   ///< attractor evolution speed
    Traj chaos_depth = 12.0;                 ///< semitones of pitch modulation (carrier mode)
    double chaos_amp = 0.0;                  ///< amount the attractor also modulates the level
    bool   chaos_direct = false;             ///< output the attractor itself instead of an oscillator
    Traj logistic_r = 3.9;

wauvio::sfx::struct FilterP

struct FilterP  { enum Type { LowPass, HighPass, BandPass, Notch } type = LowPass; Traj cutoff = 1000.0; Traj q = 0.707; int poles = 2; Mod lfo; };   // lfo depth in octaves

wauvio::sfx::struct DriveP

struct DriveP   { enum Kind { Tanh, Hard, Fold, Asym, Rectify } kind = Tanh; Traj amount = 2.0; double mix = 1.0; };

wauvio::sfx::struct CrushP

struct CrushP   { Traj bits = 8.0; Traj down = 1.0; };

wauvio::sfx::struct GateP

struct GateP    { std::vector<double> steps { 1.0, 0.0 }; double step_beats = 0.25; double step_sec = 0.0; double depth = 1.0; double smooth_ms = 3.0; double swing = 0.0; };

wauvio::sfx::struct TremP

struct TremP    { Traj rate = 5.0; Traj depth = 0.5; };

wauvio::sfx::struct PulsesP

struct PulsesP  { Traj rate = 8.0; std::vector<double> times; double decay_ms = 40.0; double attack_ms = 0.5; double depth = 1.0; double jitter = 0.0; };

wauvio::sfx::struct StutterP

struct StutterP { double seg_sec = 0.1; int repeats = 3; double prob = 0.5; double from = 0.0; double to = 1.0; };

wauvio::sfx::struct FreezeP

struct FreezeP  { double at = 0.5; double window_sec = 0.06; };

wauvio::sfx::struct DropoutP

struct DropoutP { double rate_per_sec = 4.0; double min_ms = 5.0; double max_ms = 40.0; double depth = 1.0; };

wauvio::sfx::struct CombP

struct CombP    { Traj freq = 220.0; double feedback = 0.8; double mix = 0.5; };

wauvio::sfx::struct RingP

struct RingP    { Traj freq = 300.0; double mix = 1.0; };

wauvio::sfx::struct ReverbP

struct ReverbP  { double t60 = 2.0; double damping = 0.4; double mix = 0.3; double predelay_ms = 0.0; };   // small synthetic Schroeder tail, not a convolution reverb

wauvio::sfx::struct EchoP

struct EchoP    { double time_sec = 0.25; double feedback = 0.4; double mix = 0.4; double damping = 0.3; };

wauvio::sfx::struct ReverseP

struct ReverseP {};

wauvio::sfx::struct SpectralP

struct SpectralP { enum Mode { Disintegrate, Scramble, TiltFade, Freeze } mode = Scramble; Traj amount = 0.5; double at = 0.5; };
using Proc = std::variant<FilterP, DriveP, CrushP, GateP, TremP, PulsesP, StutterP, FreezeP, DropoutP, CombP, RingP, ReverbP, EchoP, ReverseP, SpectralP>;

wauvio::sfx::struct LayerSpec

struct LayerSpec
    std::string name;
    Source src;
    std::vector<Proc> chain;
    Traj env = 1.0;            ///< amplitude envelope applied to the source, before the chain (tails can ring on)
    Traj post = 1.0;           ///< amplitude automation applied after the chain
    double level = 1.0;        ///< linear peak level in the mix
    double start = 0.0;        ///< seconds from the start of the effect
    double length = -1.0;      ///< seconds of source; < 0 = until the end of the effect
    double tail = 0.0;         ///< extra seconds the chain may ring after the source ends
    double pan = 0.0;          ///< -1 left .. +1 right (equal power)
    double width = 0.0;        ///< 0 mono (just panned) .. 1 independently rendered L/R (decorrelated)
    double fade_in_ms = 0.5, fade_out_ms = 4.0;
    uint64_t seed_offset = 0;  ///< change to get a different random take of just this layer
    // sources
    LayerSpec& osc(Wave w, Traj pitch_midi) { src = Source(); src.kind = SourceKind::Osc; src.wave = w; src.pitch = pitch_midi; return *this; }
    LayerSpec& noise(NoiseColor c, Traj density = 200.0) { src = Source(); src.kind = SourceKind::Noise; src.color = c; src.density = density; return *this; }
    LayerSpec& additive(Traj pitch_midi, std::vector<double> notes, int harmonics, Traj rolloff = 1.0) { src = Source(); src.kind = SourceKind::Additive; src.pitch = pitch_midi; src.notes = std::move(notes); src.harmonics = harmonics; src.rolloff = rolloff; return *this; }
    LayerSpec& modal(std::vector<Mode> modes, Traj pitch_midi, std::vector<double> hits) { src = Source(); src.kind = SourceKind::Modal; src.modes = std::move(modes); src.pitch = pitch_midi; src.hits = std::move(hits); return *this; }
    LayerSpec& formant(Traj vowel, Traj pitch_midi, double breath = 0.0) { src = Source(); src.kind = SourceKind::Formant; src.vowel = vowel; src.pitch = pitch_midi; src.breath = breath; return *this; }
    LayerSpec& grain(GrainSource gs, Traj density, Traj size_ms, Traj pitch_midi = 60.0) { src = Source(); src.kind = SourceKind::Grain; src.grain_source = gs; src.density = density; src.grain_ms = size_ms; src.pitch = pitch_midi; return *this; }
    LayerSpec& pluck(Traj pitch_midi, std::vector<double> hits, double damping = 0.5) { src = Source(); src.kind = SourceKind::Pluck; src.pitch = pitch_midi; src.hits = std::move(hits); src.damping = damping; return *this; }
    LayerSpec& chaos(ChaosKind k, Traj rate, Traj pitch_midi = 60.0) { src = Source(); src.kind = SourceKind::Chaos; src.chaos = k; src.chaos_rate = rate; src.pitch = pitch_midi; return *this; }
    // source options
    LayerSpec& unison(int voices, Traj cents) { src.voices = voices; src.detune_cents = cents; return *this; }
    LayerSpec& fm(Traj index, double ratio) { src.fm_index = index; src.fm_ratio = ratio; return *this; }
    LayerSpec& sub(double g) { src.sub = g; return *this; }
    LayerSpec& stack(std::vector<double> ratios) { src.ratios = std::move(ratios); return *this; }
    LayerSpec& pw(Traj w) { src.pulse_width = w; return *this; }
    LayerSpec& inharmonic(double b) { src.inharmonicity = b; return *this; }
    LayerSpec& spread(Traj cents) { src.partial_spread = cents; return *this; }
    LayerSpec& hit_notes(std::vector<double> n) { src.hit_notes = std::move(n); return *this; }
    LayerSpec& hit_jitter(double j, double vel_var = 0.3) { src.hit_jitter = j; src.velocity_var = vel_var; return *this; }
    LayerSpec& strike(double ms) { src.strike_ms = ms; return *this; }
    LayerSpec& decay_scale(double s) { src.t60_scale = s; return *this; }
    LayerSpec& scatter(double pitch_semis, double position_frac) { src.pitch_scatter = pitch_semis; src.position_scatter = position_frac; return *this; }
    LayerSpec& grain_pos(Traj p) { src.position = p; return *this; }
    LayerSpec& grain_pitch(Traj semis) { src.grain_pitch = semis; return *this; }
    LayerSpec& shift(Traj semis) { src.formant_shift = semis; return *this; }
    LayerSpec& whisper(double breath) { src.breath = breath; return *this; }
    LayerSpec& chaos_depth(Traj semis, double amp = 0.0) { src.chaos_depth = semis; src.chaos_amp = amp; return *this; }
    LayerSpec& chaos_out(bool direct) { src.chaos_direct = direct; return *this; }
    // shaping and placement
    LayerSpec& shape(Traj e) { env = e; return *this; }
    LayerSpec& after(Traj p) { post = p; return *this; }
    LayerSpec& gain(double g) { level = g; return *this; }
    LayerSpec& at(double sec) { start = sec; return *this; }
    LayerSpec& len(double sec) { length = sec; return *this; }
    LayerSpec& ring(double sec) { tail = sec; return *this; }
    LayerSpec& place(double p, double w = 0.0) { pan = p; width = w; return *this; }
    LayerSpec& fade(double in_ms, double out_ms) { fade_in_ms = in_ms; fade_out_ms = out_ms; return *this; }
    LayerSpec& variation(uint64_t s) { seed_offset = s; return *this; }
    // processors
    LayerSpec& lp(Traj cutoff, Traj q = 0.707, int poles = 2) { FilterP f; f.type = FilterP::LowPass; f.cutoff = cutoff; f.q = q; f.poles = poles; chain.push_back(f); return *this; }
    LayerSpec& hp(Traj cutoff, Traj q = 0.707, int poles = 2) { FilterP f; f.type = FilterP::HighPass; f.cutoff = cutoff; f.q = q; f.poles = poles; chain.push_back(f); return *this; }
    LayerSpec& bp(Traj cutoff, Traj q = 2.0, int poles = 2) { FilterP f; f.type = FilterP::BandPass; f.cutoff = cutoff; f.q = q; f.poles = poles; chain.push_back(f); return *this; }
    LayerSpec& notch(Traj cutoff, Traj q = 2.0) { FilterP f; f.type = FilterP::Notch; f.cutoff = cutoff; f.q = q; chain.push_back(f); return *this; }
    LayerSpec& filter_lfo(Mod m) { for (size_t i = chain.size(); i-- > 0;) if (auto* f = std::get_if<FilterP>(&chain[i])) { f->lfo = m; return *this; } throw Error("filter_lfo: no filter in the chain yet"); }
    LayerSpec& drive(Traj amount, DriveP::Kind k = DriveP::Tanh, double mix = 1.0) { DriveP d; d.kind = k; d.amount = amount; d.mix = mix; chain.push_back(d); return *this; }
    LayerSpec& crush(Traj bits, Traj down = 1.0) { CrushP c; c.bits = bits; c.down = down; chain.push_back(c); return *this; }
    LayerSpec& gate(std::vector<double> steps, double step_beats, double depth = 1.0, double smooth_ms = 3.0, double swing = 0.0) { GateP g; g.steps = std::move(steps); g.step_beats = step_beats; g.depth = depth; g.smooth_ms = smooth_ms; g.swing = swing; chain.push_back(g); return *this; }
    LayerSpec& gate_sec(std::vector<double> steps, double step_sec, double depth = 1.0, double smooth_ms = 2.0) { GateP g; g.steps = std::move(steps); g.step_sec = step_sec; g.depth = depth; g.smooth_ms = smooth_ms; chain.push_back(g); return *this; }
    LayerSpec& trem(Traj rate_hz, Traj depth) { TremP t; t.rate = rate_hz; t.depth = depth; chain.push_back(t); return *this; }
    LayerSpec& pulses(Traj rate_hz, double decay_ms, double depth = 1.0, double jitter = 0.0) { PulsesP p; p.rate = rate_hz; p.decay_ms = decay_ms; p.depth = depth; p.jitter = jitter; chain.push_back(p); return *this; }
    LayerSpec& pulses_at(std::vector<double> times, double decay_ms, double attack_ms = 0.5, double depth = 1.0) { PulsesP p; p.times = std::move(times); p.decay_ms = decay_ms; p.attack_ms = attack_ms; p.depth = depth; chain.push_back(p); return *this; }
    LayerSpec& stutter(double seg_sec, int repeats, double prob, double from = 0.0, double to = 1.0) { StutterP s; s.seg_sec = seg_sec; s.repeats = repeats; s.prob = prob; s.from = from; s.to = to; chain.push_back(s); return *this; }
    LayerSpec& freeze(double at_frac, double window_sec) { FreezeP f; f.at = at_frac; f.window_sec = window_sec; chain.push_back(f); return *this; }
    LayerSpec& dropout(double rate_per_sec, double min_ms, double max_ms, double depth = 1.0) { DropoutP d; d.rate_per_sec = rate_per_sec; d.min_ms = min_ms; d.max_ms = max_ms; d.depth = depth; chain.push_back(d); return *this; }
    LayerSpec& comb(Traj freq_hz, double feedback, double mix = 0.5) { CombP c; c.freq = freq_hz; c.feedback = feedback; c.mix = mix; chain.push_back(c); return *this; }
    LayerSpec& ringmod(Traj freq_hz, double mix = 1.0) { RingP r; r.freq = freq_hz; r.mix = mix; chain.push_back(r); return *this; }
    LayerSpec& reverb(double t60, double damping = 0.4, double mix = 0.3, double predelay_ms = 0.0) { ReverbP r; r.t60 = t60; r.damping = damping; r.mix = mix; r.predelay_ms = predelay_ms; chain.push_back(r); return *this; }
    LayerSpec& echo(double time_sec, double feedback, double mix = 0.4, double damping = 0.3) { EchoP e; e.time_sec = time_sec; e.feedback = feedback; e.mix = mix; e.damping = damping; chain.push_back(e); return *this; }
    LayerSpec& reverse() { chain.push_back(ReverseP{}); return *this; }
    LayerSpec& spectral(SpectralP::Mode m, Traj amount, double at_frac = 0.5) { SpectralP s; s.mode = m; s.amount = amount; s.at = at_frac; chain.push_back(s); return *this; }

wauvio::sfx::struct Recipe

struct Recipe
    std::string name;
    double duration = 1.0;     ///< nominal length in seconds (what "duration" means to the caller)
    double tail = 0.0;         ///< extra seconds after `duration` (decay of reverb-like layers)
    double bpm = 120.0;        ///< tempo used by tempo-synced layers
    bool   reverse_all = false;///< reverse the finished mix (offline)
    double fade_in_ms = 1.0, fade_out_ms = 6.0;
    std::vector<LayerSpec> layers;
    LayerSpec& add(const std::string& layer_name) { layers.emplace_back(); layers.back().name = layer_name; return layers.back(); }
    double total_seconds() const