use crate::synth::patches::{Exciter, WindSpec};
use crate::synth::waveguide::{
Biquad, DcBlock, Delay, Noise, OnePole, Ramp, Vibrato, note_hz,
};
use std::f32::consts::{PI, TAU};
const MIN_BEND: f32 = 0.25;
const SILENT: f32 = 2e-5;
const MIN_CUTOFF: f32 = 3.0;
const REED_NOISE_HZ: f32 = 1_500.0;
const LIP_TUNE: f32 = 0.9;
const LIP_Q: f32 = 20.0;
const LIP_GAP: f32 = 0.1;
const LIP_Z: f32 = 1.0;
const LIP_PULL: f32 = 1.0038;
const STEEP_DELAY: f32 = 6.0;
const JET_RATIO: f32 = 0.45;
const JET_LP: f32 = 2.0;
const JET_GAIN: f32 = 3.6;
const JET_OFFSET: f32 = 0.5;
const JET_FEEDBACK: f32 = 0.5;
const JET_REFLECT: f32 = 0.5;
#[derive(Clone, Debug)]
struct Bore {
main: Delay,
aux: Delay,
lp: OnePole,
loss: f32,
dc: DcBlock,
lip: Biquad,
jet_lp: f32,
steep: Delay,
bell: OnePole,
freq: f32,
filter_delay: f32,
vib: Vibrato,
mix: f32,
}
#[derive(Clone, Debug)]
pub struct Wind {
bores: Vec<Bore>,
exciter: Exciter,
breath: Ramp,
noise: Noise,
noise_amt: f32,
turb: OnePole,
release: f32,
out_dc: DcBlock,
norm: f32,
energy: f32,
released: bool,
brassy: f32,
breath_ref: f32,
tone: Option<Biquad>,
body: Vec<Biquad>,
sr: f32,
}
#[inline]
fn reed(x: f32, slope: f32) -> f32 {
(0.7 + slope * x).clamp(-1.0, 1.0)
}
impl Wind {
pub fn new(spec: &WindSpec, key: u8, velocity: f32, sr: f32) -> Self {
let k = key as f32;
let players = spec.players.len().max(1) as f32;
let bores = spec
.players
.iter()
.enumerate()
.map(|(i, p)| {
let freq = note_hz(k + p.detune_cents / 100.0) * 2f32.powi(p.octave);
let lp = OnePole::corner(spec.cutoff.max(MIN_CUTOFF * freq), sr);
let loss = (spec.loss / lp.gain(freq, sr)).min(0.999);
let cap = 2.0 * sr / (freq * MIN_BEND) + 8.0;
let r = i as f32;
Bore {
main: Delay::new(cap),
aux: Delay::new(cap),
lp,
loss,
dc: DcBlock::new(5.0, sr),
lip: Biquad::reson(freq, 0.99, sr),
jet_lp: 0.0,
steep: Delay::new(2.0 * STEEP_DELAY),
bell: lp,
freq,
filter_delay: lp.delay(freq, sr),
vib: Vibrato::new(
spec.vibrato_hz * (1.0 + 0.07 * r),
spec.vibrato_cents,
spec.vibrato_delay * (1.0 + 0.2 * r),
(0.41 * r).fract(),
sr,
),
mix: p.mix,
}
})
.collect();
let v = velocity.clamp(0.0, 1.0).sqrt();
let target = spec.breath * (1.0 + spec.dynamics * (v - 0.6)).max(0.9);
let mut breath = Ramp::new();
breath.go(target, spec.attack, sr);
let model_gain = match spec.exciter {
Exciter::Reed { .. } => 9.5,
Exciter::Cone { .. } => 9.0,
Exciter::Lips => 5.7,
Exciter::Jet { .. } => 0.15 / spec.breath.max(0.1),
};
Self {
bores,
exciter: spec.exciter,
breath,
noise: Noise::new(0x6C07_8965 ^ (key as u32).wrapping_mul(0x9E37_79B9)),
noise_amt: spec.noise,
turb: OnePole::corner(REED_NOISE_HZ, sr),
release: spec.release,
out_dc: DcBlock::new(30.0, sr),
norm: model_gain * spec.level / players.sqrt(),
energy: 1.0,
released: false,
brassy: spec.brassy * velocity.clamp(0.0, 1.0),
breath_ref: spec.breath.max(0.1),
tone: matches!(spec.exciter, Exciter::Lips).then(|| {
let corner =
spec.cutoff * 2f32.powf(3.0 * (velocity.clamp(0.0, 1.0) - 0.5));
Biquad::lowpass(corner, 0.6, sr)
}),
body: (spec.radiation > 0.0)
.then(|| Biquad::highpass(spec.radiation, 0.7, sr))
.into_iter()
.chain(
spec
.formants
.iter()
.map(|f| Biquad::peak(f.hz, f.q, f.db, sr)),
)
.collect(),
sr,
}
}
pub fn release(&mut self) {
if !self.released {
self.released = true;
self.breath.go(0.0, self.release, self.sr);
}
}
pub fn is_silent(&self) -> bool {
self.released && self.energy < SILENT && self.breath.value < 1e-3
}
#[inline]
pub fn tick(&mut self, bend: f32, wheel: f32) -> f32 {
let p = self.breath.tick();
let n = self.noise.tick();
let n_reed = 2.5 * self.turb.tick(n);
let sr = self.sr;
let mut s = 0.0;
for b in self.bores.iter_mut() {
let f = b.freq * bend * (1.0 + b.vib.tick(wheel));
let period = sr / f;
let y = match self.exciter {
Exciter::Reed { slope } => {
let mouth = p * (1.0 + self.noise_amt * n_reed);
let d = (0.5 * period - b.filter_delay).max(2.0);
let o = b.main.read(d);
let refl = -b.loss * b.lp.tick(o);
let diff = refl - mouth;
b.main.write(mouth + diff * reed(diff, -slope));
o + refl
}
Exciter::Cone { slope, pos } => {
let mouth = p * (1.0 + self.noise_amt * n_reed);
let total = (period - b.filter_delay).max(4.0);
let d1 = (pos * total).max(2.0);
let d0 = (total - d1).max(2.0);
let o0 = b.main.read(d0);
let o1 = b.aux.read(d1);
let bell = -b.loss * b.lp.tick(o0);
let out = bell - o1;
let diff = mouth - out;
b.aux.write(bell);
b.main.write(mouth - diff * reed(diff, slope) - bell);
o0 + bell
}
Exciter::Lips => {
let mouth = p * (1.0 + self.noise_amt * n);
let d = (2.0 * period * LIP_PULL - b.filter_delay).max(2.0);
let o = b.main.read(d);
let back = b.loss * b.lp.tick(o);
let r = (-PI * f * LIP_TUNE / (LIP_Q * sr)).exp();
b.lip.set_lip(f * LIP_TUNE, r, sr);
let h = (LIP_GAP + b.lip.state()).max(0.0);
let a = mouth - 2.0 * back;
let zh = LIP_Z * h;
let u = if a >= 0.0 {
h * 0.5 * (-zh + (zh * zh + 4.0 * a).sqrt())
} else {
-h * 0.5 * (-zh + (zh * zh - 4.0 * a).sqrt())
};
b.lip.tick(a - LIP_Z * u);
b.main.write(b.dc.tick(back + LIP_Z * u));
b.steep.write(o);
let shift = self.brassy * (p / self.breath_ref) * o;
let o = b.steep.read(STEEP_DELAY - shift.clamp(-3.0, 3.0));
o - b.loss * b.bell.tick(o)
}
Exciter::Jet { stopped } => {
let (len, sign) = if stopped {
(0.5 * period, -1.0)
} else {
(period, 1.0)
};
let d = (len - b.filter_delay).max(2.0);
let o = b.main.read(d);
let back = b.dc.tick(sign * b.loss * b.lp.tick(o));
let jetted = b.aux.read((JET_RATIO * period).max(2.0));
b.aux.write(-JET_FEEDBACK * back + self.noise_amt * n);
let k = 1.0 - (-TAU * JET_LP * f / sr).exp();
b.jet_lp += k * (jetted - b.jet_lp);
let deflect =
(JET_GAIN * b.jet_lp + JET_OFFSET).tanh() - JET_OFFSET.tanh();
b.main.write(p * deflect + JET_REFLECT * back);
o
}
};
s += b.mix * y;
}
if let Some(t) = self.tone.as_mut() {
s = t.tick(s);
}
for b in self.body.iter_mut() {
s = b.tick(s);
}
let out = self.out_dc.tick(s) * self.norm;
self.energy = (self.energy * 0.9995).max(out.abs());
out
}
}