use crate::synth::env::EnvSpec;
use crate::synth::filter::FilterMode;
use crate::synth::osc::Wave;
pub use crate::synth::choir::ChoirSpec;
use crate::synth::choir::{Vowel, vf};
pub use crate::synth::fx::FxKind;
pub use crate::synth::leslie::LeslieSpec;
pub use crate::synth::reed::FreeReedSpec;
#[derive(Clone, Copy, Debug)]
pub struct OscSpec {
pub wave: Wave,
pub detune_cents: f32,
pub mix: f32,
pub octave: i32,
pub decay: f32,
}
impl OscSpec {
pub const fn new(wave: Wave, detune_cents: f32, mix: f32) -> Self {
Self {
wave,
detune_cents,
mix,
octave: 0,
decay: 0.0,
}
}
pub const fn at(
wave: Wave,
detune_cents: f32,
mix: f32,
octave: i32,
) -> Self {
Self {
wave,
detune_cents,
mix,
octave,
decay: 0.0,
}
}
pub const fn perc(
wave: Wave,
detune_cents: f32,
mix: f32,
octave: i32,
decay: f32,
) -> Self {
Self {
wave,
detune_cents,
mix,
octave,
decay,
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct FilterSpec {
pub mode: FilterMode,
pub cutoff: f32,
pub res: f32,
pub key_track: f32, pub env_amount: f32, pub vel_track: f32,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LfoTarget {
Pitch,
Filter,
Amp,
}
#[derive(Clone, Copy, Debug)]
pub struct LfoSpec {
pub rate: f32, pub depth: f32,
pub target: LfoTarget,
pub delay: f32, }
#[derive(Clone, Copy, Debug)]
pub struct StringSpec {
pub detune_cents: f32,
pub octave: i32,
pub mix: f32,
}
#[derive(Clone, Copy, Debug)]
pub struct PluckSpec {
pub strings: &'static [StringSpec],
pub decay: f32, pub damp: f32, pub hf_decay: f32,
pub pluck_pos: f32, pub damper: f32, pub noise: f32, pub magnetic: bool,
pub buzz: f32,
}
#[derive(Clone, Copy, Debug)]
pub struct PianoSpec {
pub decay: f32, pub brightness: f32, pub unison_cents: f32, pub strike_pos: f32, pub hammer: f32, pub damper: f32, pub body: f32, }
#[derive(Clone, Copy, Debug)]
pub struct Mode {
pub ratio: f32, pub level: f32, pub decay: f32, }
const fn mode(ratio: f32, level: f32, decay: f32) -> Mode {
Mode {
ratio,
level,
decay,
}
}
#[derive(Clone, Copy, Debug)]
pub struct ModalSpec {
pub modes: &'static [Mode],
pub decay: f32, pub key_decay: f32, pub corner: f32, pub hardness: f32, pub click: f32, pub onset: f32, pub spread_cents: f32, pub pickup: f32, pub damper: f32, pub tremolo_hz: f32,
pub tremolo_depth: f32, }
#[derive(Clone, Copy, Debug)]
pub struct BowSpec {
pub players: &'static [StringSpec],
pub bow_pos: f32, pub pressure: f32, pub brightness: f32, pub ring: f32, pub body: f32, pub body_follows_key: bool,
pub bow_noise: f32, pub attack: f32, pub release: f32, pub vibrato_hz: f32,
pub vibrato_cents: f32,
pub vibrato_delay: f32,
pub tremolo_hz: f32, }
#[derive(Clone, Copy, Debug)]
pub enum Exciter {
Reed { slope: f32 },
Cone { slope: f32, pos: f32 },
Lips,
Jet { stopped: bool },
}
#[derive(Clone, Copy, Debug)]
pub struct WindSpec {
pub exciter: Exciter,
pub players: &'static [StringSpec],
pub breath: f32, pub dynamics: f32, pub cutoff: f32, pub loss: f32, pub noise: f32, pub attack: f32, pub release: f32, pub vibrato_hz: f32,
pub vibrato_cents: f32,
pub vibrato_delay: f32,
pub level: f32, pub brassy: f32,
pub formants: &'static [Formant],
pub radiation: f32,
}
#[derive(Clone, Copy, Debug)]
pub struct Formant {
pub hz: f32,
pub q: f32,
pub db: f32,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct FmSpec {
pub ratio_x10: u32,
pub index_x10: u32,
}
impl FmSpec {
pub const fn new(ratio_x10: u32, index_x10: u32) -> Self {
Self {
ratio_x10,
index_x10,
}
}
pub fn parts(self) -> (f32, f32) {
(self.ratio_x10 as f32 / 10.0, self.index_x10 as f32 / 10.0)
}
}
#[derive(Clone, Copy, Debug)]
pub struct Patch {
pub oscs: &'static [OscSpec],
pub env: EnvSpec,
pub filter: Option<FilterSpec>,
pub lfo: Option<LfoSpec>,
pub fm: Option<FmSpec>,
pub gain: f32,
pub pluck: Option<PluckSpec>,
pub piano: Option<PianoSpec>,
pub modal: Option<ModalSpec>,
pub bowed: Option<BowSpec>,
pub wind: Option<WindSpec>,
pub choir: Option<ChoirSpec>,
pub reed: Option<FreeReedSpec>,
pub fx: Option<FxKind>,
pub leslie: Option<LeslieSpec>,
pub drive: f32,
}
impl Patch {
const fn with_filter(self, filter: Option<FilterSpec>) -> Self {
Self { filter, ..self }
}
const fn with_drive(self, drive: f32) -> Self {
Self { drive, ..self }
}
const fn with_leslie(self, leslie: LeslieSpec) -> Self {
Self {
leslie: Some(leslie),
..self
}
}
}
const BASE: Patch = Patch {
oscs: SINE,
env: EnvSpec::adsr(0.001, 1.0, 0.0, 0.2),
filter: None,
lfo: None,
fm: None,
gain: 1.0,
pluck: None,
piano: None,
modal: None,
bowed: None,
wind: None,
choir: None,
reed: None,
fx: None,
leslie: None,
drive: 0.0,
};
const fn p(
oscs: &'static [OscSpec],
env: EnvSpec,
filter: Option<FilterSpec>,
lfo: Option<LfoSpec>,
fm: Option<FmSpec>,
gain: f32,
) -> Patch {
Patch {
oscs,
env,
filter,
lfo,
fm,
gain,
..BASE
}
}
const fn plucked(
oscs: &'static [OscSpec],
gain: f32,
pluck: PluckSpec,
) -> Patch {
Patch {
oscs,
env: EnvSpec::adsr(0.001, 0.5, 0.0, 0.1),
gain,
pluck: Some(pluck),
..BASE
}
}
const fn piano(gain: f32, spec: PianoSpec) -> Patch {
Patch {
oscs: PIANO_OSC,
env: EnvSpec::adsr(0.001, 1.0, 0.0, 0.2),
gain,
piano: Some(spec),
..BASE
}
}
const fn modal(gain: f32, spec: ModalSpec) -> Patch {
Patch {
gain,
modal: Some(spec),
..BASE
}
}
const fn bowed(gain: f32, spec: BowSpec) -> Patch {
Patch {
oscs: SAW,
env: EnvSpec::adsr(0.05, 0.1, 1.0, 0.2),
gain,
bowed: Some(spec),
..BASE
}
}
const fn wind(gain: f32, spec: WindSpec) -> Patch {
Patch {
oscs: SAW,
env: EnvSpec::adsr(0.05, 0.1, 1.0, 0.2),
gain,
wind: Some(spec),
..BASE
}
}
const fn choir(gain: f32, spec: ChoirSpec) -> Patch {
Patch {
oscs: SAW,
env: EnvSpec::adsr(0.1, 0.1, 1.0, 0.3),
gain,
choir: Some(spec),
..BASE
}
}
const fn reed(gain: f32, spec: FreeReedSpec) -> Patch {
Patch {
oscs: SAW,
env: EnvSpec::adsr(0.03, 0.1, 1.0, 0.1),
gain,
reed: Some(spec),
..BASE
}
}
const fn fx(gain: f32, kind: FxKind) -> Patch {
Patch {
oscs: NOISE_OSC,
env: EnvSpec::adsr(0.001, 0.5, 0.0, 0.1),
gain,
fx: Some(kind),
..BASE
}
}
const fn lp(
cutoff: f32,
res: f32,
key_track: f32,
env_amount: f32,
) -> Option<FilterSpec> {
Some(FilterSpec {
mode: FilterMode::Low,
cutoff,
res,
key_track,
env_amount,
vel_track: 0.0,
})
}
const fn lpv(
cutoff: f32,
res: f32,
key_track: f32,
env_amount: f32,
vel_track: f32,
) -> Option<FilterSpec> {
Some(FilterSpec {
mode: FilterMode::Low,
cutoff,
res,
key_track,
env_amount,
vel_track,
})
}
const fn vibrato(rate: f32, depth: f32, delay: f32) -> Option<LfoSpec> {
Some(LfoSpec {
rate,
depth,
target: LfoTarget::Pitch,
delay,
})
}
const SINE: &[OscSpec] = &[OscSpec::new(Wave::Sine, 0.0, 1.0)];
const TRI: &[OscSpec] = &[OscSpec::new(Wave::Triangle, 0.0, 1.0)];
const SAW: &[OscSpec] = &[OscSpec::new(Wave::Saw, 0.0, 1.0)];
const SQR: &[OscSpec] = &[OscSpec::new(Wave::Square, 0.0, 1.0)];
const PIANO_OSC: &[OscSpec] = &[OscSpec::new(Wave::Sine, 0.0, 1.0)];
const PIANO_GRAND: PianoSpec = PianoSpec {
decay: 2.6,
brightness: 1.0,
unison_cents: 0.7,
strike_pos: 0.119,
hammer: 1.0,
damper: 0.36,
body: 0.7,
};
const PIANO_BRIGHT: PianoSpec = PianoSpec {
brightness: 1.5,
hammer: 1.2,
..PIANO_GRAND
};
const PIANO_ELECTRIC_GRAND: PianoSpec = PianoSpec {
decay: 1.6,
brightness: 1.3,
unison_cents: 2.5,
strike_pos: 0.1,
hammer: 0.5,
damper: 0.3,
body: 0.2, };
const PIANO_HONKY_TONK: PianoSpec = PianoSpec {
decay: 2.0,
brightness: 1.1,
unison_cents: 14.0,
strike_pos: 0.125,
hammer: 1.3,
damper: 0.4,
body: 1.3, };
const ENS_OSC: &[OscSpec] = &[
OscSpec::new(Wave::Saw, -10.0, 0.5),
OscSpec::new(Wave::Saw, 0.0, 0.5),
OscSpec::new(Wave::Saw, 10.0, 0.5),
];
const PAD_OSC: &[OscSpec] = &[
OscSpec::new(Wave::Saw, -12.0, 0.45),
OscSpec::new(Wave::Saw, 12.0, 0.45),
OscSpec::at(Wave::Sine, 0.0, 0.3, 1),
];
const NOISE_OSC: &[OscSpec] = &[OscSpec::new(Wave::Noise, 0.0, 1.0)];
const HARPSI_OSC: &[OscSpec] = &[OscSpec::new(Wave::Saw, 0.0, 1.0)];
const HARPSI_STRINGS: &[StringSpec] = &[
StringSpec {
detune_cents: 0.0,
octave: 0,
mix: 1.0,
}, StringSpec {
detune_cents: 4.0,
octave: 1,
mix: 0.5,
}, ];
const PLUCK_HARPSI: PluckSpec = PluckSpec {
strings: HARPSI_STRINGS,
decay: 1.6,
damp: 0.03,
pluck_pos: 0.15,
hf_decay: 0.0,
damper: 0.08,
noise: 0.85,
magnetic: false,
buzz: 0.0,
};
const FIFTHS_OSC: &[OscSpec] = &[
OscSpec::new(Wave::Saw, 0.0, 0.6),
OscSpec::at(Wave::Saw, 700.0, 0.5, 0),
];
const CRYSTAL_OSC: &[OscSpec] = &[
OscSpec::new(Wave::Sine, 0.0, 0.6),
OscSpec::at(Wave::Sine, 700.0, 0.4, 0),
];
const EP_RHODES: ModalSpec = ModalSpec {
modes: &[
mode(1.0, 1.0, 1.0),
mode(2.0, 0.06, 0.5), mode(7.2, 0.10, 0.04), ],
decay: 5.0,
key_decay: 0.5,
corner: 3_000.0,
hardness: 0.6,
click: 0.02,
onset: 0.0015,
spread_cents: 0.0,
pickup: 1.2,
damper: 0.15,
tremolo_hz: 0.0,
tremolo_depth: 0.0,
};
const EP_FM: ModalSpec = ModalSpec {
modes: &[
mode(1.0, 1.0, 1.0),
mode(2.0, 0.45, 0.5),
mode(3.0, 0.2, 0.3),
mode(4.0, 0.1, 0.2),
mode(5.0, 0.05, 0.12),
mode(14.0, 0.25, 0.03),
],
decay: 4.0,
key_decay: 0.5,
corner: 5_000.0,
hardness: 0.8,
click: 0.0,
onset: 0.001,
spread_cents: 7.0,
pickup: 0.0,
damper: 0.2,
tremolo_hz: 0.0,
tremolo_depth: 0.0,
};
const CELESTA: ModalSpec = ModalSpec {
modes: &[
mode(1.0, 1.0, 1.0),
mode(2.76, 0.07, 0.3),
mode(5.40, 0.02, 0.12),
],
decay: 2.0,
key_decay: 0.5,
corner: 2_500.0,
hardness: 0.5,
click: 0.03,
onset: 0.002,
spread_cents: 0.0,
pickup: 0.0,
damper: 0.25,
tremolo_hz: 0.0,
tremolo_depth: 0.0,
};
const GLOCKENSPIEL: ModalSpec = ModalSpec {
modes: &[
mode(1.0, 1.0, 1.0),
mode(2.76, 0.35, 0.4),
mode(5.40, 0.12, 0.18),
mode(8.93, 0.04, 0.08),
],
decay: 4.0,
key_decay: 0.4,
corner: 5_000.0,
hardness: 0.6,
click: 0.05,
onset: 0.0008,
spread_cents: 0.0,
pickup: 0.0,
damper: 0.0,
tremolo_hz: 0.0,
tremolo_depth: 0.0,
};
const VIBRAPHONE: ModalSpec = ModalSpec {
modes: &[
mode(1.0, 1.0, 1.0),
mode(4.0, 0.10, 0.2),
mode(10.0, 0.02, 0.06),
],
decay: 5.0,
key_decay: 0.5,
corner: 2_000.0,
hardness: 0.6,
click: 0.03,
onset: 0.003,
spread_cents: 0.0,
pickup: 0.0,
damper: 0.5,
tremolo_hz: 5.5,
tremolo_depth: 0.3,
};
const MARIMBA: ModalSpec = ModalSpec {
modes: &[
mode(1.0, 1.0, 1.0),
mode(3.93, 0.30, 0.18),
mode(9.2, 0.08, 0.07),
],
decay: 1.4,
key_decay: 0.8,
corner: 1_800.0,
hardness: 0.8,
click: 0.1,
onset: 0.0015,
spread_cents: 0.0,
pickup: 0.0,
damper: 0.0,
tremolo_hz: 0.0,
tremolo_depth: 0.0,
};
const XYLOPHONE: ModalSpec = ModalSpec {
modes: &[
mode(1.0, 1.0, 1.0),
mode(3.0, 0.40, 0.4),
mode(6.1, 0.12, 0.2),
mode(10.4, 0.04, 0.1),
],
decay: 0.8,
key_decay: 0.6,
corner: 4_000.0,
hardness: 1.0,
click: 0.25,
onset: 0.0005,
spread_cents: 0.0,
pickup: 0.0,
damper: 0.0,
tremolo_hz: 0.0,
tremolo_depth: 0.0,
};
const TUBULAR_BELLS: ModalSpec = ModalSpec {
modes: &[
mode(0.617, 0.20, 1.3),
mode(1.210, 0.45, 1.15),
mode(2.000, 1.00, 1.0),
mode(2.988, 0.80, 0.8),
mode(4.173, 0.55, 0.6),
mode(5.556, 0.30, 0.45),
mode(7.136, 0.15, 0.3),
],
decay: 7.0,
key_decay: 0.4,
corner: 5_000.0,
hardness: 0.6,
click: 0.12,
onset: 0.0008,
spread_cents: 0.5,
pickup: 0.0,
damper: 0.0,
tremolo_hz: 0.0,
tremolo_depth: 0.0,
};
const ONE_STRING: &[StringSpec] = &[StringSpec {
detune_cents: 0.0,
octave: 0,
mix: 1.0,
}];
const PLUCK_CLAV: PluckSpec = PluckSpec {
strings: ONE_STRING,
decay: 1.4,
damp: 0.02,
pluck_pos: 0.06,
hf_decay: 0.0,
damper: 0.02,
noise: 0.85,
magnetic: false,
buzz: 0.0,
};
const DULCIMER_STRINGS: &[StringSpec] = &[
StringSpec {
detune_cents: 0.0,
octave: 0,
mix: 1.0,
},
StringSpec {
detune_cents: 3.0,
octave: 0,
mix: 0.9,
},
StringSpec {
detune_cents: -2.5,
octave: 0,
mix: 0.8,
},
];
const PLUCK_DULCIMER: PluckSpec = PluckSpec {
strings: DULCIMER_STRINGS,
decay: 4.0,
damp: 0.1,
pluck_pos: 0.1,
hf_decay: 0.0,
damper: 1.5,
noise: 0.85,
magnetic: false,
buzz: 0.0,
};
const PLUCK_NYLON: PluckSpec = PluckSpec {
strings: ONE_STRING,
decay: 2.5,
damp: 0.3,
pluck_pos: 0.18,
hf_decay: 0.0,
damper: 0.12,
noise: 0.85,
magnetic: false,
buzz: 0.0,
};
const PLUCK_STEEL: PluckSpec = PluckSpec {
strings: ONE_STRING,
decay: 3.5,
damp: 0.15,
pluck_pos: 0.13,
hf_decay: 0.0,
damper: 0.12,
noise: 0.85,
magnetic: false,
buzz: 0.0,
};
const PLUCK_JAZZ: PluckSpec = PluckSpec {
strings: ONE_STRING,
decay: 3.0,
damp: 0.15,
pluck_pos: 0.25, hf_decay: 0.0,
damper: 0.1,
noise: 0.85,
magnetic: false,
buzz: 0.0,
};
const CLEAN_STRINGS: &[StringSpec] = &[
StringSpec {
detune_cents: 0.0,
octave: 0,
mix: 1.0,
},
StringSpec {
detune_cents: 6.0,
octave: 0,
mix: 0.35,
},
];
const PLUCK_CLEAN: PluckSpec = PluckSpec {
strings: CLEAN_STRINGS,
decay: 3.5,
damp: 0.04,
pluck_pos: 0.12,
hf_decay: 0.0,
damper: 0.1,
noise: 0.85,
magnetic: false,
buzz: 0.0,
};
const PLUCK_MUTED: PluckSpec = PluckSpec {
strings: ONE_STRING,
decay: 0.3,
damp: 0.3,
pluck_pos: 0.15,
hf_decay: 0.0,
damper: 0.05,
noise: 0.85,
magnetic: false,
buzz: 0.0,
};
const PLUCK_DRIVEN: PluckSpec = PluckSpec {
strings: ONE_STRING,
decay: 6.0,
damp: 0.05,
pluck_pos: 0.14,
hf_decay: 0.0,
damper: 0.08,
noise: 0.85,
magnetic: false,
buzz: 0.0,
};
const HARMONIC_STRINGS: &[StringSpec] = &[StringSpec {
detune_cents: 0.0,
octave: 1,
mix: 1.0,
}];
const PLUCK_HARMONICS: PluckSpec = PluckSpec {
strings: HARMONIC_STRINGS,
decay: 3.0,
damp: 0.12,
pluck_pos: 0.25,
hf_decay: 0.0,
damper: 0.15,
noise: 0.85,
magnetic: false,
buzz: 0.0,
};
const PLUCK_UPRIGHT: PluckSpec = PluckSpec {
strings: ONE_STRING,
decay: 3.0,
damp: 0.0,
hf_decay: 0.25,
pluck_pos: 0.3,
damper: 0.12,
noise: 0.1,
magnetic: false,
buzz: 0.0,
};
const PLUCK_FINGER: PluckSpec = PluckSpec {
strings: ONE_STRING,
decay: 3.0,
damp: 0.0,
hf_decay: 0.8,
pluck_pos: 0.2,
damper: 0.1,
noise: 0.2,
magnetic: true,
buzz: 0.0,
};
const PLUCK_PICK: PluckSpec = PluckSpec {
strings: ONE_STRING,
decay: 2.4,
damp: 0.0,
hf_decay: 0.7,
pluck_pos: 0.09,
damper: 0.06,
noise: 0.85,
magnetic: true,
buzz: 0.0,
};
const FRETLESS_STRINGS: &[StringSpec] = &[
StringSpec {
detune_cents: 0.0,
octave: 0,
mix: 1.0,
},
StringSpec {
detune_cents: 4.0,
octave: 0,
mix: 0.25,
},
];
const PLUCK_FRETLESS: PluckSpec = PluckSpec {
strings: FRETLESS_STRINGS,
decay: 3.5,
damp: 0.0,
hf_decay: 1.5,
pluck_pos: 0.12,
damper: 0.1,
noise: 0.1,
magnetic: true,
buzz: 0.0,
};
const PLUCK_SLAP: PluckSpec = PluckSpec {
strings: ONE_STRING,
decay: 1.8,
damp: 0.0,
hf_decay: 0.5,
pluck_pos: 0.05,
damper: 0.05,
noise: 1.0,
magnetic: true,
buzz: 0.0,
};
const PLUCK_POP: PluckSpec = PluckSpec {
strings: ONE_STRING,
decay: 1.8,
damp: 0.0,
hf_decay: 0.8,
pluck_pos: 0.035,
damper: 0.05,
noise: 1.0,
magnetic: true,
buzz: 0.0,
};
const VIOLIN: BowSpec = BowSpec {
players: ONE_STRING,
bow_pos: 0.12,
pressure: 0.75,
brightness: 12.0,
ring: 1.0,
body: 1.0,
body_follows_key: false,
bow_noise: 0.04,
attack: 0.08,
release: 0.12,
vibrato_hz: 5.6,
vibrato_cents: 14.0,
vibrato_delay: 0.3,
tremolo_hz: 0.0,
};
const VIOLA: BowSpec = BowSpec {
bow_pos: 0.13,
body: 0.78,
vibrato_hz: 5.4,
..VIOLIN
};
const CELLO: BowSpec = BowSpec {
bow_pos: 0.13,
body: 0.45,
ring: 1.5,
attack: 0.1,
vibrato_hz: 5.2,
vibrato_cents: 12.0,
..VIOLIN
};
const CONTRABASS: BowSpec = BowSpec {
bow_pos: 0.14,
body: 0.3,
ring: 2.0,
attack: 0.12,
release: 0.15,
vibrato_hz: 4.8,
vibrato_cents: 10.0,
..VIOLIN
};
const SECTION: &[StringSpec] = &[
StringSpec {
detune_cents: 0.0,
octave: 0,
mix: 1.0,
},
StringSpec {
detune_cents: 6.0,
octave: 0,
mix: 0.9,
},
StringSpec {
detune_cents: -5.0,
octave: 0,
mix: 0.9,
},
];
const TREMOLO_STRINGS: BowSpec = BowSpec {
players: SECTION,
body_follows_key: true,
attack: 0.03,
vibrato_cents: 6.0,
tremolo_hz: 6.5,
..VIOLIN
};
const STRING_ENSEMBLE: BowSpec = BowSpec {
players: SECTION,
body_follows_key: true,
attack: 0.15,
release: 0.3,
vibrato_cents: 10.0,
vibrato_delay: 0.2,
..VIOLIN
};
const SLOW_STRINGS: BowSpec = BowSpec {
attack: 0.45,
release: 0.5,
pressure: 0.65,
..STRING_ENSEMBLE
};
const FIDDLE: BowSpec = BowSpec {
bow_pos: 0.09,
pressure: 0.9,
bow_noise: 0.07,
attack: 0.04,
vibrato_cents: 7.0,
vibrato_delay: 0.25,
..VIOLIN
};
const ONE_PLAYER: &[StringSpec] = ONE_STRING;
const BRASS_PLAYERS: &[StringSpec] = &[
StringSpec {
detune_cents: 0.0,
octave: 0,
mix: 1.0,
},
StringSpec {
detune_cents: 5.0,
octave: 0,
mix: 0.85,
},
StringSpec {
detune_cents: -4.0,
octave: 0,
mix: 0.85,
},
];
const TRUMPET: WindSpec = WindSpec {
exciter: Exciter::Lips,
players: ONE_PLAYER,
breath: 0.8,
dynamics: 0.5,
cutoff: 5_000.0,
loss: 0.95,
noise: 0.03,
attack: 0.03,
release: 0.05,
vibrato_hz: 5.0,
vibrato_cents: 8.0,
vibrato_delay: 0.35,
level: 1.0,
brassy: 2.5,
formants: &[],
radiation: 0.0,
};
const TROMBONE: WindSpec = WindSpec {
cutoff: 3_000.0,
brassy: 3.0,
attack: 0.04,
vibrato_cents: 6.0,
..TRUMPET
};
const TUBA: WindSpec = WindSpec {
cutoff: 1_500.0,
attack: 0.05,
vibrato_cents: 4.0,
..TRUMPET
};
const FRENCH_HORN: WindSpec = WindSpec {
cutoff: 1_200.0,
brassy: 1.0,
attack: 0.06,
release: 0.08,
vibrato_cents: 3.0,
..TRUMPET
};
const BRASS_SECTION: WindSpec = WindSpec {
players: BRASS_PLAYERS,
cutoff: 3_500.0,
attack: 0.04,
vibrato_cents: 6.0,
..TRUMPET
};
const CLARINET: WindSpec = WindSpec {
exciter: Exciter::Reed { slope: 0.3 },
players: ONE_PLAYER,
breath: 0.7,
dynamics: 0.4,
cutoff: 3_000.0,
loss: 0.95,
noise: 0.1,
attack: 0.025,
release: 0.05,
vibrato_hz: 5.0,
vibrato_cents: 3.0,
vibrato_delay: 0.4,
level: 1.0,
brassy: 0.0,
formants: &[],
radiation: 0.0,
};
const ALTO_SAX: WindSpec = WindSpec {
exciter: Exciter::Cone {
slope: 0.3,
pos: 0.4,
},
players: ONE_PLAYER,
breath: 0.9,
dynamics: 0.3,
cutoff: 4_500.0,
loss: 0.95,
noise: 0.05,
attack: 0.015, release: 0.05,
vibrato_hz: 5.0,
vibrato_cents: 15.0,
vibrato_delay: 0.3,
level: 1.0,
brassy: 0.0,
formants: ALTO_FORMANTS,
radiation: 0.0,
};
const ALTO_FORMANTS: &[Formant] = &[
Formant {
hz: 800.0,
q: 1.2,
db: 7.0,
},
Formant {
hz: 2_400.0,
q: 2.0,
db: 7.0,
},
];
const SOPRANO_SAX: WindSpec = WindSpec {
cutoff: 6_000.0,
noise: 0.04,
vibrato_hz: 5.6,
vibrato_cents: 12.0,
formants: &[
Formant {
hz: 1_200.0,
q: 1.5,
db: 8.0,
},
Formant {
hz: 3_600.0,
q: 2.0,
db: 8.0,
},
],
..ALTO_SAX
};
const TENOR_SAX: WindSpec = WindSpec {
cutoff: 3_500.0,
noise: 0.07,
vibrato_hz: 4.8,
vibrato_cents: 18.0,
formants: &[
Formant {
hz: 600.0,
q: 1.2,
db: 7.0,
},
Formant {
hz: 1_800.0,
q: 1.8,
db: 8.0,
},
],
..ALTO_SAX
};
const BARITONE_SAX: WindSpec = WindSpec {
cutoff: 3_000.0,
noise: 0.06,
vibrato_hz: 4.5,
vibrato_cents: 10.0,
formants: &[
Formant {
hz: 400.0,
q: 1.0,
db: 8.0,
},
Formant {
hz: 1_200.0,
q: 1.6,
db: 9.0,
},
],
..ALTO_SAX
};
const OBOE: WindSpec = WindSpec {
exciter: Exciter::Cone {
slope: 0.3,
pos: 0.38,
},
cutoff: 6_000.0,
noise: 0.02,
vibrato_hz: 5.5,
vibrato_cents: 10.0,
formants: &[
Formant {
hz: 1_200.0,
q: 1.5,
db: 9.0,
},
Formant {
hz: 2_900.0,
q: 2.0,
db: 6.0,
},
],
radiation: 800.0,
..ALTO_SAX
};
const ENGLISH_HORN: WindSpec = WindSpec {
cutoff: 3_500.0,
formants: &[
Formant {
hz: 900.0,
q: 1.4,
db: 9.0,
},
Formant {
hz: 2_300.0,
q: 2.0,
db: 4.0,
},
],
radiation: 550.0,
..OBOE
};
const BASSOON: WindSpec = WindSpec {
exciter: Exciter::Cone {
slope: 0.35,
pos: 0.4,
},
cutoff: 1_500.0,
breath: 0.8,
vibrato_cents: 8.0,
formants: &[],
radiation: 0.0,
..OBOE
};
const SHANAI: WindSpec = WindSpec {
exciter: Exciter::Cone {
slope: 0.3,
pos: 0.4,
},
cutoff: 8_000.0,
vibrato_hz: 6.0,
vibrato_cents: 20.0,
formants: &[],
..ALTO_SAX
};
const BAGPIPE: WindSpec = WindSpec {
exciter: Exciter::Cone {
slope: 0.25,
pos: 0.42,
},
cutoff: 7_000.0,
dynamics: 0.0,
attack: 0.015,
release: 0.02,
vibrato_cents: 0.0,
formants: &[],
..ALTO_SAX
};
const FLUTE: WindSpec = WindSpec {
exciter: Exciter::Jet { stopped: false },
players: ONE_PLAYER,
breath: 1.0,
dynamics: 0.4,
cutoff: 4_000.0,
loss: 0.98,
noise: 0.1,
attack: 0.05,
release: 0.06,
vibrato_hz: 5.5,
vibrato_cents: 12.0,
vibrato_delay: 0.3,
level: 1.0,
brassy: 0.0,
formants: &[],
radiation: 0.0,
};
const PICCOLO: WindSpec = WindSpec {
cutoff: 6_000.0,
noise: 0.08,
..FLUTE
};
const RECORDER: WindSpec = WindSpec {
cutoff: 3_000.0,
noise: 0.04,
attack: 0.03,
vibrato_cents: 3.0,
..FLUTE
};
const PAN_FLUTE: WindSpec = WindSpec {
exciter: Exciter::Jet { stopped: true },
cutoff: 2_500.0,
noise: 0.25,
vibrato_cents: 8.0,
..FLUTE
};
const BLOWN_BOTTLE: WindSpec = WindSpec {
exciter: Exciter::Jet { stopped: true },
cutoff: 800.0,
noise: 0.35,
attack: 0.08,
vibrato_cents: 0.0,
..FLUTE
};
const SHAKUHACHI: WindSpec = WindSpec {
cutoff: 2_500.0,
noise: 0.35,
attack: 0.08,
vibrato_hz: 4.5,
vibrato_cents: 20.0,
vibrato_delay: 0.45,
..FLUTE
};
const WHISTLE: WindSpec = WindSpec {
cutoff: 6_000.0,
noise: 0.05,
attack: 0.02,
vibrato_cents: 10.0,
..FLUTE
};
const OCARINA: WindSpec = WindSpec {
exciter: Exciter::Jet { stopped: true },
cutoff: 1_000.0,
noise: 0.06,
vibrato_cents: 6.0,
..FLUTE
};
const PLUCK_PIZZICATO: PluckSpec = PluckSpec {
strings: SECTION,
decay: 0.7,
damp: 0.0,
hf_decay: 0.12,
pluck_pos: 0.3,
damper: 0.25,
noise: 0.15,
magnetic: false,
buzz: 0.0,
};
const PLUCK_HARP: PluckSpec = PluckSpec {
strings: ONE_STRING,
decay: 4.0,
damp: 0.0,
hf_decay: 0.6,
pluck_pos: 0.42,
damper: 2.5,
noise: 0.1,
magnetic: false,
buzz: 0.0,
};
const PLUCK_SITAR: PluckSpec = PluckSpec {
strings: ONE_STRING,
decay: 3.5,
damp: 0.0,
hf_decay: 1.2,
pluck_pos: 0.1,
damper: 0.6,
noise: 0.5,
magnetic: false,
buzz: 0.03,
};
const PLUCK_BANJO: PluckSpec = PluckSpec {
strings: ONE_STRING,
decay: 1.1,
damp: 0.04,
hf_decay: 0.0,
pluck_pos: 0.08,
damper: 0.15,
noise: 0.9,
magnetic: false,
buzz: 0.0,
};
const PLUCK_SHAMISEN: PluckSpec = PluckSpec {
strings: ONE_STRING,
decay: 1.2,
damp: 0.0,
hf_decay: 0.5,
pluck_pos: 0.07,
damper: 0.2,
noise: 1.0,
magnetic: false,
buzz: 0.02,
};
const PLUCK_KOTO: PluckSpec = PluckSpec {
strings: ONE_STRING,
decay: 2.8,
damp: 0.0,
hf_decay: 0.45,
pluck_pos: 0.11,
damper: 0.6,
noise: 0.6,
magnetic: false,
buzz: 0.0,
};
const TIMPANI: ModalSpec = ModalSpec {
modes: &[
mode(0.86, 0.5, 0.08), mode(1.0, 1.0, 1.0), mode(1.50, 0.55, 0.8), mode(1.98, 0.35, 0.65), mode(2.44, 0.20, 0.5), mode(2.94, 0.10, 0.4), mode(3.42, 0.05, 0.3),
],
decay: 3.5,
key_decay: 0.6,
corner: 900.0,
hardness: 1.0,
click: 0.15,
onset: 0.002,
spread_cents: 0.0,
pickup: 0.0,
damper: 0.0,
tremolo_hz: 0.0,
tremolo_depth: 0.0,
};
const MUSIC_BOX: ModalSpec = ModalSpec {
modes: &[
mode(1.0, 1.0, 1.0),
mode(6.27, 0.25, 0.3),
mode(17.55, 0.06, 0.1),
],
decay: 3.5,
key_decay: 0.5,
corner: 6_000.0,
hardness: 0.4,
click: 0.06,
onset: 0.0005,
spread_cents: 0.0,
pickup: 0.0,
damper: 0.0,
tremolo_hz: 0.0,
tremolo_depth: 0.0,
};
const KALIMBA: ModalSpec = ModalSpec {
modes: &[
mode(1.0, 1.0, 1.0),
mode(5.9, 0.18, 0.12),
mode(15.6, 0.04, 0.05),
],
decay: 1.6,
key_decay: 0.5,
corner: 2_500.0,
hardness: 0.5,
click: 0.12,
onset: 0.001,
spread_cents: 0.0,
pickup: 0.0,
damper: 0.0,
tremolo_hz: 0.0,
tremolo_depth: 0.0,
};
const TINKLE_BELL: ModalSpec = ModalSpec {
modes: &[
mode(1.0, 1.0, 1.0),
mode(2.32, 0.7, 0.7),
mode(4.25, 0.45, 0.45),
mode(6.63, 0.25, 0.3),
mode(9.38, 0.12, 0.2),
],
decay: 3.0,
key_decay: 0.4,
corner: 6_000.0,
hardness: 0.5,
click: 0.08,
onset: 0.0005,
spread_cents: 3.0,
pickup: 0.0,
damper: 0.0,
tremolo_hz: 0.0,
tremolo_depth: 0.0,
};
const AGOGO: ModalSpec = ModalSpec {
modes: &[
mode(1.0, 1.0, 1.0),
mode(2.57, 0.6, 0.6),
mode(4.09, 0.35, 0.4),
mode(5.95, 0.2, 0.3),
],
decay: 0.9,
key_decay: 0.3,
corner: 5_000.0,
hardness: 0.6,
click: 0.2,
onset: 0.0004,
spread_cents: 4.0,
pickup: 0.0,
damper: 0.0,
tremolo_hz: 0.0,
tremolo_depth: 0.0,
};
const STEEL_DRUM: ModalSpec = ModalSpec {
modes: &[
mode(1.0, 1.0, 1.0),
mode(2.0, 0.65, 0.8),
mode(3.0, 0.3, 0.6),
mode(4.02, 0.12, 0.4),
mode(5.1, 0.05, 0.3),
],
decay: 1.4,
key_decay: 0.4,
corner: 2_800.0,
hardness: 0.7,
click: 0.1,
onset: 0.001,
spread_cents: 2.5,
pickup: 0.0,
damper: 0.0,
tremolo_hz: 0.0,
tremolo_depth: 0.0,
};
const WOODBLOCK: ModalSpec = ModalSpec {
modes: &[
mode(1.0, 1.0, 1.0),
mode(2.43, 0.4, 0.6),
mode(3.87, 0.15, 0.4),
],
decay: 0.22,
key_decay: 0.3,
corner: 4_000.0,
hardness: 0.5,
click: 0.35,
onset: 0.0003,
spread_cents: 0.0,
pickup: 0.0,
damper: 0.0,
tremolo_hz: 0.0,
tremolo_depth: 0.0,
};
const TAIKO: ModalSpec = ModalSpec {
modes: &[
mode(0.5, 1.0, 1.0),
mode(0.797, 0.6, 0.6),
mode(1.068, 0.4, 0.45),
mode(1.150, 0.3, 0.4),
mode(1.327, 0.2, 0.3),
mode(1.459, 0.12, 0.25),
],
decay: 1.0,
key_decay: 0.3,
corner: 900.0,
hardness: 0.8,
click: 0.3,
onset: 0.001,
spread_cents: 0.0,
pickup: 0.0,
damper: 0.0,
tremolo_hz: 0.0,
tremolo_depth: 0.0,
};
const MELODIC_TOM: ModalSpec = ModalSpec {
modes: &[
mode(1.0, 1.0, 1.0),
mode(1.59, 0.55, 0.6),
mode(2.14, 0.35, 0.45),
mode(2.30, 0.25, 0.4),
mode(2.65, 0.15, 0.3),
],
decay: 0.6,
key_decay: 0.3,
corner: 1_800.0,
hardness: 0.8,
click: 0.25,
onset: 0.0008,
spread_cents: 0.0,
pickup: 0.0,
damper: 0.0,
tremolo_hz: 0.0,
tremolo_depth: 0.0,
};
const fn click(mix: f32) -> OscSpec {
OscSpec::perc(Wave::Noise, 0.0, mix, 0, 0.012)
}
const DRAWBAR_OSC: &[OscSpec] = &[
OscSpec::at(Wave::Sine, 0.0, 1.0, -1),
OscSpec::at(Wave::Sine, 702.0, 1.0, 0),
OscSpec::at(Wave::Sine, 0.0, 1.0, 0),
OscSpec::at(Wave::Sine, 0.0, 0.25, 1),
click(0.08),
];
const PERC_ORGAN_OSC: &[OscSpec] = &[
OscSpec::at(Wave::Sine, 0.0, 0.35, -1),
OscSpec::at(Wave::Sine, 0.0, 1.0, 0),
OscSpec::at(Wave::Sine, 0.0, 0.5, 1),
OscSpec::perc(Wave::Sine, 702.0, 1.6, 1, 0.45),
click(0.12),
];
const ROCK_ORGAN_OSC: &[OscSpec] = &[
OscSpec::at(Wave::Sine, 0.0, 1.0, -1),
OscSpec::at(Wave::Sine, 702.0, 1.0, 0),
OscSpec::at(Wave::Sine, 0.0, 1.0, 0),
OscSpec::at(Wave::Sine, 0.0, 0.8, 1),
OscSpec::at(Wave::Sine, 702.0, 0.6, 1),
OscSpec::at(Wave::Sine, 0.0, 0.6, 2),
OscSpec::at(Wave::Sine, 386.0, 0.35, 2),
OscSpec::at(Wave::Sine, 702.0, 0.35, 2),
OscSpec::at(Wave::Sine, 0.0, 0.35, 3),
click(0.1),
];
const CHURCH_ORGAN_OSC: &[OscSpec] = &[
OscSpec::at(Wave::Sine, 0.0, 0.45, -1),
OscSpec::at(Wave::Saw, 0.0, 0.7, 0),
OscSpec::at(Wave::Triangle, 1.5, 0.6, 0),
OscSpec::at(Wave::Triangle, -1.2, 0.5, 1),
OscSpec::at(Wave::Triangle, 703.0, 0.3, 1),
OscSpec::at(Wave::Triangle, 0.8, 0.3, 2),
OscSpec::at(Wave::Sine, 701.0, 0.15, 2),
OscSpec::perc(Wave::Noise, 0.0, 0.08, 0, 0.06),
];
const fn rank(detune_cents: f32, octave: i32, mix: f32) -> StringSpec {
StringSpec {
detune_cents,
octave,
mix,
}
}
const AH_LOW: Vowel = [
vf(625.0, 70.0, 0.0),
vf(1060.0, 80.0, -6.5),
vf(2450.0, 115.0, -8.0),
vf(2675.0, 125.0, -8.5),
vf(3000.0, 135.0, -21.0),
];
const AH_HIGH: Vowel = [
vf(800.0, 80.0, 0.0),
vf(1150.0, 90.0, -5.0),
vf(2850.0, 120.0, -26.0),
vf(3700.0, 130.0, -28.0),
vf(4950.0, 140.0, -55.0),
];
const OO_LOW: Vowel = [
vf(350.0, 40.0, 0.0),
vf(600.0, 70.0, -20.0),
vf(2550.0, 100.0, -24.0),
vf(2790.0, 120.0, -21.0),
vf(3125.0, 120.0, -31.0),
];
const OO_HIGH: Vowel = [
vf(340.0, 50.0, 0.0),
vf(650.0, 60.0, -16.0),
vf(2600.0, 170.0, -24.0),
vf(3200.0, 180.0, -27.0),
vf(4100.0, 200.0, -45.0),
];
const OH_LOW: Vowel = [
vf(400.0, 40.0, 0.0),
vf(775.0, 80.0, -10.5),
vf(2500.0, 100.0, -16.5),
vf(2700.0, 120.0, -16.0),
vf(2950.0, 120.0, -33.0),
];
const OH_HIGH: Vowel = [
vf(450.0, 70.0, 0.0),
vf(800.0, 80.0, -10.0),
vf(2830.0, 100.0, -19.0),
vf(3650.0, 130.0, -25.0),
vf(4950.0, 135.0, -52.0),
];
const CHOIR_SECTION: &[StringSpec] = &[
rank(0.0, 0, 1.0),
rank(9.0, 0, 0.9),
rank(-8.0, 0, 0.9),
rank(4.0, 0, 0.8),
rank(-13.0, 0, 0.8),
rank(15.0, 0, 0.7),
];
const SMALL_SECTION: &[StringSpec] = &[
rank(0.0, 0, 1.0),
rank(7.0, 0, 0.9),
rank(-6.0, 0, 0.9),
rank(12.0, 0, 0.7),
];
const CHOIR_AAHS: ChoirSpec = ChoirSpec {
singers: CHOIR_SECTION,
low: AH_LOW,
high: AH_HIGH,
split: 60.0,
brightness: 2_500.0,
breath: 0.15,
drift_cents: 6.0,
stagger: 0.06,
attack: 0.18,
release: 0.4,
vibrato_hz: 5.2,
vibrato_cents: 18.0,
vibrato_delay: 0.25,
};
const VOICE_OOHS: ChoirSpec = ChoirSpec {
singers: SMALL_SECTION,
low: OO_LOW,
high: OO_HIGH,
brightness: 1_500.0,
breath: 0.2,
drift_cents: 5.0,
stagger: 0.04,
attack: 0.12,
release: 0.35,
vibrato_hz: 5.0,
vibrato_cents: 10.0,
vibrato_delay: 0.3,
..CHOIR_AAHS
};
const SYNTH_VOICE: ChoirSpec = ChoirSpec {
singers: &[rank(-7.0, 0, 1.0), rank(7.0, 0, 1.0)],
low: OH_LOW,
high: OH_HIGH,
brightness: 4_000.0,
breath: 0.0,
drift_cents: 0.0,
stagger: 0.0,
attack: 0.08,
release: 0.3,
vibrato_hz: 5.5,
vibrato_cents: 8.0,
vibrato_delay: 0.4,
..CHOIR_AAHS
};
const VOICE_LEAD: ChoirSpec = ChoirSpec {
singers: &[rank(0.0, 0, 1.0)],
brightness: 5_000.0,
breath: 0.05,
drift_cents: 0.0,
stagger: 0.0,
attack: 0.02,
release: 0.15,
vibrato_hz: 5.5,
vibrato_cents: 12.0,
vibrato_delay: 0.2,
..CHOIR_AAHS
};
const CHOIR_PAD: ChoirSpec = ChoirSpec {
low: OH_LOW,
high: OH_HIGH,
brightness: 1_800.0,
breath: 0.1,
stagger: 0.2,
attack: 0.8,
release: 1.2,
vibrato_cents: 6.0,
vibrato_delay: 0.5,
..CHOIR_AAHS
};
const HALO_PAD: ChoirSpec = ChoirSpec {
singers: &[
rank(0.0, 0, 1.0),
rank(8.0, 0, 0.8),
rank(-6.0, 1, 0.5),
rank(5.0, 1, 0.4),
],
low: OO_LOW,
high: OO_HIGH,
brightness: 3_500.0,
breath: 0.25,
stagger: 0.15,
attack: 0.6,
release: 1.5,
vibrato_cents: 4.0,
vibrato_delay: 0.6,
..CHOIR_AAHS
};
const ACCORDION: FreeReedSpec = FreeReedSpec {
reeds: &[rank(-14.0, 0, 0.8), rank(0.0, 0, 1.0), rank(13.0, 0, 0.8)],
slot: 0.45,
asym: 0.5,
dynamics: 0.6,
attack: 0.04,
release: 0.06,
noise: 0.02,
tone: 5_500.0,
formants: &[
Formant {
hz: 1_200.0,
q: 1.0,
db: 4.0,
},
Formant {
hz: 3_000.0,
q: 2.0,
db: 3.0,
},
],
vibrato_hz: 0.0,
vibrato_cents: 0.0,
tremolo: 0.0,
level: 1.0,
};
const HARMONICA: FreeReedSpec = FreeReedSpec {
reeds: &[rank(0.0, 0, 1.0)],
slot: 0.55,
asym: 0.25,
dynamics: 0.8,
attack: 0.03,
release: 0.05,
noise: 0.06,
tone: 4_500.0,
formants: &[
Formant {
hz: 900.0,
q: 1.5,
db: 6.0,
},
Formant {
hz: 2_200.0,
q: 2.0,
db: 4.0,
},
],
vibrato_hz: 5.0,
vibrato_cents: 6.0,
tremolo: 0.15,
level: 1.0,
};
const BANDONEON: FreeReedSpec = FreeReedSpec {
reeds: &[rank(0.0, 0, 1.0), rank(3.0, 1, 0.55)],
slot: 0.35,
asym: 0.6,
dynamics: 0.7,
attack: 0.06,
release: 0.08,
noise: 0.03,
tone: 3_500.0,
formants: &[Formant {
hz: 700.0,
q: 1.0,
db: 4.0,
}],
..ACCORDION
};
const REED_ORGAN: FreeReedSpec = FreeReedSpec {
reeds: &[rank(0.0, 0, 1.0), rank(4.0, 0, 0.6)],
slot: 0.3,
asym: 0.7,
dynamics: 0.3,
attack: 0.08,
release: 0.12,
noise: 0.015,
tone: 2_500.0,
formants: &[Formant {
hz: 500.0,
q: 0.8,
db: 3.0,
}],
..ACCORDION
};
const SYNTH_BASS2_OSC: &[OscSpec] = &[
OscSpec::new(Wave::Square, 0.0, 0.6),
OscSpec::at(Wave::Sine, 0.0, 0.7, -1),
];
const SYNTH_STRINGS2_OSC: &[OscSpec] = &[
OscSpec::new(Wave::Square, -14.0, 0.5),
OscSpec::new(Wave::Square, 14.0, 0.5),
OscSpec::at(Wave::Saw, 0.0, 0.3, -1),
];
const ORCH_HIT_OSC: &[OscSpec] = &[
OscSpec::new(Wave::Saw, -8.0, 0.6),
OscSpec::new(Wave::Saw, 8.0, 0.6),
OscSpec::at(Wave::Saw, 0.0, 0.5, -1),
OscSpec::at(Wave::Saw, 702.0, 0.35, 0),
OscSpec::perc(Wave::Square, 3.0, 0.4, 1, 0.6),
OscSpec::perc(Wave::Sine, 0.0, 0.8, -2, 0.3),
OscSpec::perc(Wave::Noise, 0.0, 0.5, 0, 0.1),
];
const SYNTH_BRASS_OSC: &[OscSpec] = &[
OscSpec::new(Wave::Saw, -6.0, 0.5),
OscSpec::new(Wave::Saw, 6.0, 0.5),
];
const SYNTH_BRASS2_OSC: &[OscSpec] = &[
OscSpec::new(Wave::Square, -4.0, 0.5),
OscSpec::new(Wave::Saw, 4.0, 0.5),
];
const CALLIOPE_OSC: &[OscSpec] = &[
OscSpec::new(Wave::Triangle, 0.0, 1.0),
OscSpec::at(Wave::Sine, 0.0, 0.3, 1),
OscSpec::new(Wave::Noise, 0.0, 0.12),
];
const CHIFF_OSC: &[OscSpec] = &[
OscSpec::new(Wave::Square, 0.0, 0.7),
OscSpec::at(Wave::Triangle, 0.0, 0.3, 1),
OscSpec::perc(Wave::Noise, 0.0, 0.6, 0, 0.05),
];
const CHARANG_OSC: &[OscSpec] = &[
OscSpec::new(Wave::Saw, 0.0, 1.0),
OscSpec::at(Wave::Saw, 5.0, 0.3, 1),
];
const BASS_LEAD_OSC: &[OscSpec] = &[
OscSpec::new(Wave::Saw, 0.0, 1.0),
OscSpec::at(Wave::Square, 0.0, 0.8, -1),
];
const WARM_OSC: &[OscSpec] = &[
OscSpec::new(Wave::Saw, -7.0, 0.5),
OscSpec::new(Wave::Saw, 7.0, 0.5),
OscSpec::at(Wave::Triangle, 0.0, 0.4, -1),
];
const POLY_OSC: &[OscSpec] = &[
OscSpec::new(Wave::Saw, -5.0, 0.5),
OscSpec::new(Wave::Square, 5.0, 0.5),
];
const BRIGHT_OSC: &[OscSpec] = &[
OscSpec::new(Wave::Saw, -6.0, 0.5),
OscSpec::new(Wave::Saw, 6.0, 0.5),
OscSpec::at(Wave::Saw, 0.0, 0.4, 1),
];
const BOWED_PAD: BowSpec = BowSpec {
attack: 0.7,
release: 1.0,
pressure: 0.65,
brightness: 8.0,
vibrato_cents: 5.0,
vibrato_delay: 0.6,
..STRING_ENSEMBLE
};
const fn filter_lfo(rate: f32, depth: f32) -> Option<LfoSpec> {
Some(LfoSpec {
rate,
depth,
target: LfoTarget::Filter,
delay: 0.0,
})
}
const fn amp_lfo(rate: f32, depth: f32) -> Option<LfoSpec> {
Some(LfoSpec {
rate,
depth,
target: LfoTarget::Amp,
delay: 0.0,
})
}
const LESLIE_SLOW: LeslieSpec = LeslieSpec { fast: false };
const LESLIE_FAST: LeslieSpec = LeslieSpec { fast: true };
pub fn gm_patch(program: u8) -> Patch {
let prog = program.min(127);
PATCHES[prog as usize]
}
pub fn gm_program_name(program: u8) -> &'static str {
GM_NAMES[program.min(127) as usize]
}
#[rustfmt::skip]
static GM_NAMES: [&str; 128] = [
"Acoustic Grand Piano", "Bright Acoustic Piano", "Electric Grand Piano",
"Honky-tonk Piano", "Electric Piano 1", "Electric Piano 2", "Harpsichord",
"Clavinet",
"Celesta", "Glockenspiel", "Music Box", "Vibraphone", "Marimba", "Xylophone",
"Tubular Bells", "Dulcimer",
"Drawbar Organ", "Percussive Organ", "Rock Organ", "Church Organ",
"Reed Organ", "Accordion", "Harmonica", "Tango Accordion",
"Acoustic Guitar (nylon)", "Acoustic Guitar (steel)", "Electric Guitar (jazz)",
"Electric Guitar (clean)", "Electric Guitar (muted)", "Overdriven Guitar",
"Distortion Guitar", "Guitar Harmonics",
"Acoustic Bass", "Electric Bass (finger)", "Electric Bass (pick)",
"Fretless Bass", "Slap Bass 1", "Slap Bass 2", "Synth Bass 1", "Synth Bass 2",
"Violin", "Viola", "Cello", "Contrabass", "Tremolo Strings",
"Pizzicato Strings", "Orchestral Harp", "Timpani",
"String Ensemble 1", "String Ensemble 2", "Synth Strings 1",
"Synth Strings 2", "Choir Aahs", "Voice Oohs", "Synth Voice", "Orchestra Hit",
"Trumpet", "Trombone", "Tuba", "Muted Trumpet", "French Horn", "Brass Section",
"Synth Brass 1", "Synth Brass 2",
"Soprano Sax", "Alto Sax", "Tenor Sax", "Baritone Sax", "Oboe", "English Horn",
"Bassoon", "Clarinet",
"Piccolo", "Flute", "Recorder", "Pan Flute", "Blown Bottle", "Shakuhachi",
"Whistle", "Ocarina",
"Lead 1 (square)", "Lead 2 (sawtooth)", "Lead 3 (calliope)", "Lead 4 (chiff)",
"Lead 5 (charang)", "Lead 6 (voice)", "Lead 7 (fifths)", "Lead 8 (bass + lead)",
"Pad 1 (new age)", "Pad 2 (warm)", "Pad 3 (polysynth)", "Pad 4 (choir)",
"Pad 5 (bowed)", "Pad 6 (metallic)", "Pad 7 (halo)", "Pad 8 (sweep)",
"FX 1 (rain)", "FX 2 (soundtrack)", "FX 3 (crystal)", "FX 4 (atmosphere)",
"FX 5 (brightness)", "FX 6 (goblins)", "FX 7 (echoes)", "FX 8 (sci-fi)",
"Sitar", "Banjo", "Shamisen", "Koto", "Kalimba", "Bag pipe", "Fiddle",
"Shanai",
"Tinkle Bell", "Agogo", "Steel Drums", "Woodblock", "Taiko Drum",
"Melodic Tom", "Synth Drum", "Reverse Cymbal",
"Guitar Fret Noise", "Breath Noise", "Seashore", "Bird Tweet",
"Telephone Ring", "Helicopter", "Applause", "Gunshot",
];
static PATCHES: [Patch; 128] = build_patches();
const fn build_patches() -> [Patch; 128] {
let mut out = [DEFAULT_PATCH; 128];
let mut i = 0;
while i < 128 {
out[i] = patch_for(i as u8);
i += 1;
}
out
}
const DEFAULT_PATCH: Patch = p(
TRI,
EnvSpec::adsr(0.005, 0.4, 0.3, 0.3),
lp(4_000.0, 0.7, 0.5, 0.0),
None,
None,
0.7,
);
const fn patch_for(prog: u8) -> Patch {
match prog {
0 => piano(0.29, PIANO_GRAND),
1 => piano(0.29, PIANO_BRIGHT),
2 => piano(0.40, PIANO_ELECTRIC_GRAND),
3 => piano(0.46, PIANO_HONKY_TONK),
4 => modal(0.45, EP_RHODES),
5 => modal(0.75, EP_FM),
6 => plucked(
HARPSI_OSC,
0.9,
PLUCK_HARPSI,
),
7 => plucked(SAW, 2.0, PLUCK_CLAV)
.with_filter(Some(FilterSpec {
mode: FilterMode::Band,
cutoff: 1_100.0,
res: 0.6,
key_track: 0.5,
env_amount: 0.0,
vel_track: 0.0,
}))
.with_drive(1.5),
8 => modal(0.7, CELESTA),
9 => modal(0.6, GLOCKENSPIEL),
10 => modal(0.78, MUSIC_BOX),
11 => modal(0.7, VIBRAPHONE),
12 => modal(1.2, MARIMBA),
13 => modal(1.2, XYLOPHONE),
14 => modal(0.75, TUBULAR_BELLS),
15 => plucked(TRI, 1.3, PLUCK_DULCIMER),
16 => p(
DRAWBAR_OSC,
EnvSpec::adsr(0.005, 0.05, 1.0, 0.03),
None,
None,
None,
0.48,
)
.with_leslie(LESLIE_SLOW),
17 => p(
PERC_ORGAN_OSC,
EnvSpec::adsr(0.003, 0.05, 1.0, 0.02),
None,
None,
None,
0.6,
)
.with_leslie(LESLIE_SLOW),
18 => p(
ROCK_ORGAN_OSC,
EnvSpec::adsr(0.005, 0.05, 1.0, 0.03),
lp(5_000.0, 0.7, 0.0, 0.0), None,
None,
0.33,
)
.with_drive(3.0)
.with_leslie(LESLIE_FAST),
19 => p(
CHURCH_ORGAN_OSC,
EnvSpec::adsr(0.06, 0.1, 1.0, 0.25),
lp(4_500.0, 0.7, 0.3, 0.0),
None,
None,
0.8,
),
20 => reed(0.45, REED_ORGAN),
21 => reed(0.4, ACCORDION),
22 => reed(0.45, HARMONICA),
23 => reed(0.45, BANDONEON),
24 => {
plucked(TRI, 1.0, PLUCK_NYLON).with_filter(lp(3_500.0, 0.7, 0.5, 0.0))
}
25 => {
plucked(TRI, 0.9, PLUCK_STEEL).with_filter(lp(6_000.0, 0.7, 0.5, 0.0))
}
26 => plucked(SAW, 0.8, PLUCK_JAZZ)
.with_filter(lp(2_200.0, 0.9, 0.0, 0.0))
.with_drive(1.2),
27 => {
plucked(SAW, 0.9, PLUCK_CLEAN).with_filter(lp(4_500.0, 1.4, 0.0, 0.0))
}
28 => plucked(SAW, 1.4, PLUCK_MUTED)
.with_filter(lp(2_500.0, 0.9, 0.0, 0.0))
.with_drive(1.5),
29 => plucked(SAW, 0.45, PLUCK_DRIVEN)
.with_filter(lp(4_000.0, 1.0, 0.0, 0.0))
.with_drive(6.0),
30 => plucked(SAW, 0.35, PLUCK_DRIVEN)
.with_filter(lp(3_500.0, 1.2, 0.0, 0.0))
.with_drive(18.0),
31 => {
plucked(SAW, 0.9, PLUCK_HARMONICS).with_filter(lp(5_000.0, 0.8, 0.0, 0.0))
}
32 => {
plucked(TRI, 0.85, PLUCK_UPRIGHT).with_filter(lp(1_100.0, 0.9, 0.0, 0.0))
}
33 => {
plucked(TRI, 0.9, PLUCK_FINGER).with_filter(lp(1_400.0, 1.6, 0.0, 0.0))
}
34 => plucked(SAW, 1.8, PLUCK_PICK)
.with_filter(lp(3_500.0, 1.0, 0.0, 0.0))
.with_drive(1.2),
35 => {
plucked(TRI, 1.0, PLUCK_FRETLESS).with_filter(lp(1_000.0, 2.4, 0.0, 0.0))
}
36 => plucked(SAW, 2.0, PLUCK_SLAP)
.with_filter(lp(5_000.0, 0.8, 0.0, 0.0))
.with_drive(1.5),
37 => plucked(SAW, 1.6, PLUCK_POP)
.with_filter(lp(6_000.0, 1.2, 0.0, 0.0))
.with_drive(2.0),
38 => p(
SAW,
EnvSpec::adsr(0.003, 0.3, 0.5, 0.08),
lpv(300.0, 3.0, 0.3, 0.9, 1.0),
None,
None,
0.7,
),
39 => p(
SYNTH_BASS2_OSC,
EnvSpec::adsr(0.005, 0.5, 0.6, 0.1),
lpv(900.0, 1.0, 0.3, 0.3, 0.8),
None,
None,
0.75,
),
40 => bowed(0.74, VIOLIN),
41 => bowed(1.27, VIOLA),
42 => bowed(1.35, CELLO),
43 => bowed(1.35, CONTRABASS),
44 => bowed(1.4, TREMOLO_STRINGS),
45 => plucked(TRI, 0.77, PLUCK_PIZZICATO)
.with_filter(lp(2_500.0, 0.8, 0.5, 0.0)),
46 => plucked(TRI, 0.67, PLUCK_HARP),
47 => modal(1.06, TIMPANI),
48 => bowed(0.63, STRING_ENSEMBLE),
49 => bowed(0.82, SLOW_STRINGS),
50 => p(
ENS_OSC,
EnvSpec::adsr(0.08, 0.5, 0.85, 0.4),
lpv(4_000.0, 0.8, 0.5, 0.0, 0.6),
vibrato(5.5, 0.01, 0.3),
None,
0.55,
),
51 => p(
SYNTH_STRINGS2_OSC,
EnvSpec::adsr(0.35, 0.8, 0.8, 0.8),
lpv(2_000.0, 0.9, 0.5, 0.2, 0.5),
vibrato(4.5, 0.008, 0.5),
None,
0.55,
),
52 => choir(0.7, CHOIR_AAHS),
53 => choir(0.7, VOICE_OOHS),
54 => choir(0.7, SYNTH_VOICE),
55 => p(
ORCH_HIT_OSC,
EnvSpec::adsr(0.002, 1.6, 0.0, 1.2),
lpv(3_000.0, 0.8, 0.3, 1.0, 0.6),
None,
None,
0.8,
),
56 => wind(1.02, TRUMPET),
57 => wind(1.06, TROMBONE),
58 => wind(1.06, TUBA),
59 => wind(1.36, TRUMPET).with_filter(Some(FilterSpec {
mode: FilterMode::Band,
cutoff: 1_800.0,
res: 1.5,
key_track: 0.3,
env_amount: 0.0,
vel_track: 0.0,
})),
60 => wind(0.46, FRENCH_HORN),
61 => wind(1.07, BRASS_SECTION),
62 => p(
SYNTH_BRASS_OSC,
EnvSpec::adsr(0.03, 0.3, 0.75, 0.15),
lpv(900.0, 1.3, 0.5, 0.8, 1.0),
None,
None,
0.6,
),
63 => p(
SYNTH_BRASS2_OSC,
EnvSpec::adsr(0.08, 0.4, 0.8, 0.25),
lpv(1_400.0, 0.9, 0.5, 0.4, 0.7),
None,
None,
0.55,
),
64 => wind(0.63, SOPRANO_SAX),
65 => wind(0.65, ALTO_SAX),
66 => wind(0.73, TENOR_SAX),
67 => wind(0.83, BARITONE_SAX),
68 => wind(0.99, OBOE),
69 => wind(0.77, ENGLISH_HORN),
70 => wind(1.43, BASSOON),
71 => wind(1.02, CLARINET),
72 => wind(0.98, PICCOLO),
73 => wind(0.98, FLUTE),
74 => wind(0.98, RECORDER),
75 => wind(0.99, PAN_FLUTE),
76 => wind(1.23, BLOWN_BOTTLE),
77 => wind(1.17, SHAKUHACHI),
78 => wind(1.11, WHISTLE),
79 => wind(1.2, OCARINA),
80 => p(
SQR,
EnvSpec::adsr(0.005, 0.1, 0.9, 0.1),
lpv(3_000.0, 0.9, 0.5, 0.0, 0.8),
None,
None,
0.6,
),
81 => p(
SAW,
EnvSpec::adsr(0.005, 0.1, 0.9, 0.1),
lpv(3_500.0, 0.9, 0.5, 0.0, 0.8),
None,
None,
0.6,
),
82 => p(
CALLIOPE_OSC,
EnvSpec::adsr(0.02, 0.1, 0.9, 0.15),
lp(3_500.0, 0.7, 0.5, 0.0),
vibrato(6.0, 0.012, 0.1),
None,
0.55,
),
83 => p(
CHIFF_OSC,
EnvSpec::adsr(0.005, 0.2, 0.8, 0.15),
lpv(2_500.0, 1.0, 0.5, 0.3, 0.8),
vibrato(5.5, 0.01, 0.4),
None,
0.55,
),
84 => p(
CHARANG_OSC,
EnvSpec::adsr(0.003, 0.3, 0.8, 0.2),
lpv(3_000.0, 1.2, 0.3, 0.3, 0.8),
None,
None,
0.45,
)
.with_drive(4.0),
85 => choir(0.75, VOICE_LEAD),
86 => p(
FIFTHS_OSC,
EnvSpec::adsr(0.01, 0.2, 0.85, 0.15),
lpv(2_500.0, 1.0, 0.5, 0.0, 0.8),
None,
None,
0.55,
),
87 => p(
BASS_LEAD_OSC,
EnvSpec::adsr(0.005, 0.3, 0.7, 0.15),
lpv(1_500.0, 2.0, 0.4, 0.6, 1.0),
None,
None,
0.55,
),
88 => p(
SINE,
EnvSpec::adsr(0.05, 2.0, 0.6, 1.5),
lp(5_000.0, 0.7, 0.3, 0.0),
vibrato(4.0, 0.003, 0.5),
Some(FmSpec::new(35, 20)),
0.5,
),
89 => p(
WARM_OSC,
EnvSpec::adsr(0.5, 1.0, 0.85, 1.2),
lpv(1_100.0, 0.8, 0.4, 0.2, 0.5),
vibrato(4.0, 0.004, 0.5),
None,
0.55,
),
90 => p(
POLY_OSC,
EnvSpec::adsr(0.01, 0.6, 0.6, 0.4),
lpv(1_200.0, 1.5, 0.4, 0.7, 1.0),
None,
None,
0.5,
),
91 => choir(0.7, CHOIR_PAD),
92 => bowed(0.75, BOWED_PAD),
93 => p(
SINE,
EnvSpec::adsr(0.3, 1.5, 0.7, 1.5),
lp(4_000.0, 0.8, 0.3, 0.0),
None,
Some(FmSpec::new(14, 30)),
0.5,
),
94 => choir(0.7, HALO_PAD),
95 => p(
PAD_OSC,
EnvSpec::adsr(0.6, 1.0, 0.8, 1.0),
lpv(1_200.0, 2.5, 0.3, 0.0, 0.3),
filter_lfo(0.15, 1_500.0),
None,
0.5,
),
96 => p(
SINE,
EnvSpec::adsr(0.005, 0.8, 0.3, 1.0),
lpv(6_000.0, 0.8, 0.3, 0.0, 0.5),
amp_lfo(7.0, 0.3),
Some(FmSpec::new(70, 15)),
0.5,
),
97 => p(
PAD_OSC,
EnvSpec::adsr(1.2, 2.0, 0.75, 2.0),
lp(1_400.0, 1.0, 0.4, 0.3),
filter_lfo(0.2, 250.0),
None,
0.5,
),
98 => p(
CRYSTAL_OSC,
EnvSpec::adsr(0.005, 1.5, 0.4, 1.0),
lp(3_000.0, 1.0, 0.5, 0.0),
None,
Some(FmSpec::new(30, 30)),
0.55,
),
99 => p(
SINE,
EnvSpec::adsr(0.01, 2.5, 0.5, 1.5),
lp(3_000.0, 0.8, 0.4, 0.3),
vibrato(4.5, 0.003, 0.4),
Some(FmSpec::new(20, 15)),
0.55,
),
100 => p(
BRIGHT_OSC,
EnvSpec::adsr(0.3, 1.0, 0.8, 1.0),
lpv(6_000.0, 1.0, 0.5, 0.0, 0.5),
vibrato(5.0, 0.003, 0.3),
None,
0.45,
),
101 => p(
PAD_OSC,
EnvSpec::adsr(1.5, 1.0, 0.8, 1.5),
lp(900.0, 4.0, 0.0, 0.0),
filter_lfo(0.4, 600.0),
None,
0.5,
),
102 => p(
PAD_OSC,
EnvSpec::adsr(0.01, 1.5, 0.5, 2.0),
lp(2_500.0, 1.0, 0.5, 0.3),
amp_lfo(2.5, 0.7),
None,
0.5,
),
103 => p(
SINE,
EnvSpec::adsr(0.3, 1.0, 0.7, 1.5),
lp(3_500.0, 1.0, 0.3, 0.0),
Some(LfoSpec {
rate: 0.5,
depth: 0.1,
target: LfoTarget::Pitch,
delay: 0.0,
}),
Some(FmSpec::new(15, 40)),
0.45,
),
104 => {
plucked(SAW, 0.89, PLUCK_SITAR).with_filter(lp(5_000.0, 0.8, 0.0, 0.0))
}
105 => {
plucked(SAW, 0.89, PLUCK_BANJO).with_filter(lp(4_500.0, 1.8, 0.0, 0.0))
}
106 => {
plucked(SAW, 1.38, PLUCK_SHAMISEN).with_filter(lp(4_000.0, 1.2, 0.0, 0.0))
}
107 => {
plucked(TRI, 0.93, PLUCK_KOTO).with_filter(lp(4_000.0, 0.9, 0.0, 0.0))
}
108 => modal(0.75, KALIMBA),
109 => wind(0.34, BAGPIPE),
110 => bowed(1.5, FIDDLE),
111 => wind(1.05, SHANAI),
112 => modal(1.5, TINKLE_BELL),
113 => modal(1.29, AGOGO),
114 => modal(1.18, STEEL_DRUM),
115 => modal(1.08, WOODBLOCK),
116 => modal(0.88, TAIKO),
117 => modal(1.17, MELODIC_TOM),
118 => fx(1.0, FxKind::SynthDrum),
119 => fx(0.6, FxKind::ReverseCymbal),
120 => fx(0.9, FxKind::FretNoise),
121 => fx(0.5, FxKind::BreathNoise),
122 => fx(1.8, FxKind::Seashore),
123 => fx(0.8, FxKind::BirdTweet),
124 => fx(1.0, FxKind::Telephone),
125 => fx(0.8, FxKind::Helicopter),
126 => fx(0.8, FxKind::Applause),
_ => fx(1.0, FxKind::Gunshot),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_program_has_a_patch() {
for prog in 0u8..128 {
let pa = gm_patch(prog);
assert!(!pa.oscs.is_empty(), "program {prog} has no oscillators");
assert!(pa.gain > 0.0, "program {prog} has zero gain");
}
}
}
#[cfg(test)]
mod sound_tests {
use crate::synth::Synth;
const SR: f32 = 44_100.0;
fn render(
program: u8,
note: u8,
velocity: u8,
hold: f32,
len: f32,
) -> Vec<f32> {
let mut s = Synth::new(SR);
s.channels[0].reverb = 0.0;
s.program_change(0, program);
s.note_on(0, note, velocity);
let mut out = Vec::new();
let block = 512;
let mut buf = vec![0.0f32; block * 2];
let total = (len * SR) as usize;
let off = (hold * SR) as usize;
let mut n = 0;
while n < total {
if n <= off && off < n + block {
s.note_off(0, note);
}
s.render(&mut buf);
out.extend(buf.chunks(2).map(|f| 0.5 * (f[0] + f[1])));
n += block;
}
out
}
fn rms(x: &[f32]) -> f32 {
(x.iter().map(|v| v * v).sum::<f32>() / x.len().max(1) as f32).sqrt()
}
fn mean_freq(x: &[f32]) -> f32 {
let d: Vec<f32> = x.windows(2).map(|w| w[1] - w[0]).collect();
let r = (rms(&d) / rms(x).max(1e-9) / 2.0).min(1.0);
r.asin() * SR / std::f32::consts::PI
}
fn harmonic_profile(program: u8, note: u8) -> Vec<f32> {
let x = render(program, note, 100, 1.0, 0.6);
let seg = &x[(0.1 * SR) as usize..(0.5 * SR) as usize];
let f0 = 440.0 * 2f32.powf((note as f32 - 69.0) / 12.0);
let mags: Vec<f32> = (1..=16)
.map(|k| {
let w = std::f32::consts::TAU * f0 * k as f32 / 2.0 / SR;
let (mut re, mut im) = (0.0f32, 0.0f32);
for (i, v) in seg.iter().enumerate() {
let ph = w * i as f32;
re += v * ph.cos();
im += v * ph.sin();
}
(re * re + im * im).sqrt()
})
.collect();
let max = mags.iter().cloned().fold(1e-12, f32::max);
mags
.iter()
.map(|m| (20.0 * (m / max).log10()).max(-60.0))
.collect()
}
fn profile_distance(a: u8, b: u8) -> f32 {
let (pa, pb) = (harmonic_profile(a, 60), harmonic_profile(b, 60));
pa.iter().zip(&pb).map(|(x, y)| (x - y).abs()).sum::<f32>()
/ pa.len() as f32
}
#[test]
fn mallets_are_not_shrill() {
for prog in [8u8, 9, 11, 12, 13] {
let x = render(prog, 84, 127, 0.5, 0.5);
let f = mean_freq(&x);
assert!(f < 1_500.0, "program {prog}: mean frequency {f:.0} Hz");
}
}
#[test]
fn electric_pianos_differ() {
let d = profile_distance(4, 5);
assert!(
d > 4.0,
"EP1 and EP2 harmonic profiles differ by only {d:.1} dB"
);
}
#[test]
fn organs_differ() {
for a in 16u8..=20 {
for b in a + 1..=20 {
let d = profile_distance(a, b);
assert!(d > 4.0, "organs {a} and {b} differ by only {d:.1} dB");
}
}
}
#[test]
fn saxophones_differ() {
for a in 64u8..=67 {
for b in a + 1..=67 {
let d = profile_distance(a, b);
assert!(d > 3.0, "saxophones {a} and {b} differ by only {d:.1} dB");
}
}
}
#[test]
fn oboe_is_reedy_and_english_horn_darker() {
let x = render(68, 62, 90, 1.0, 1.0);
let seg = &x[(0.35 * SR) as usize..(0.95 * SR) as usize];
let f = 440.0 * 2f32.powf((62.0 - 69.0) / 12.0);
let h = harmonics_db(seg, f, 4);
assert!(
h[0] < h[1].max(h[2]).max(h[3]) - 3.0,
"oboe D4 fundamental dominates: {h:?}"
);
let bright = |prog| {
let x = render(prog, 64, 90, 1.0, 1.0);
mean_freq(&x[(0.35 * SR) as usize..(0.95 * SR) as usize])
};
let (oboe, horn) = (bright(68), bright(69));
assert!(
horn < 0.85 * oboe,
"English horn {horn:.0} Hz vs oboe {oboe:.0} Hz"
);
}
#[test]
fn tubular_bell_strike_tone_partials() {
let p = harmonic_profile(14, 72);
let (h2, h4, h6, h8) = (p[1], p[3], p[5], p[7]);
assert!(h4 > h2 + 6.0, "2·f0 {h4:.1} dB vs f0 {h2:.1} dB");
assert!(h6 > h2, "≈3·f0 {h6:.1} dB vs f0 {h2:.1} dB");
assert!(
h8 < h4,
"4·f0 sits at 4.17×, not on the harmonic: {h8:.1} dB"
);
}
#[test]
fn guitars_ring_and_stay_bright() {
for prog in [24u8, 25, 27] {
let x = render(prog, 52, 100, 2.0, 1.5);
let early = rms(&x[..(0.2 * SR) as usize]);
let late = rms(&x[(1.0 * SR) as usize..(1.2 * SR) as usize]);
assert!(late > early * 0.05, "program {prog} died: {early} → {late}");
let f = mean_freq(&x[..(0.3 * SR) as usize]);
assert!(f > 500.0, "program {prog} is muffled: {f:.0} Hz");
}
}
fn pitch_near(x: &[f32], f: f32) -> f32 {
let p = SR / f;
let (lo, hi) = ((p * 0.8) as usize, (p * 1.25) as usize + 2);
let n = x.len() - hi;
let ac: Vec<f32> = (lo..=hi)
.map(|lag| (0..n).map(|i| x[i] * x[i + lag]).sum())
.collect();
let mut b = 1;
for i in 1..ac.len() - 1 {
if ac[i] > ac[b] {
b = i;
}
}
let (y0, y1, y2) = (ac[b - 1], ac[b], ac[b + 1]);
let off = 0.5 * (y0 - y2) / (y0 - 2.0 * y1 + y2);
SR / ((lo + b) as f32 + off)
}
fn harmonics_db(x: &[f32], f: f32, k: usize) -> Vec<f32> {
let mags: Vec<f32> = (1..=k)
.map(|h| {
let w = std::f32::consts::TAU * f * h as f32 / SR;
let (mut re, mut im) = (0.0f32, 0.0f32);
for (i, v) in x.iter().enumerate() {
let win = 0.5
- 0.5 * (std::f32::consts::TAU * i as f32 / x.len() as f32).cos();
re += v * win * (w * i as f32).cos();
im += v * win * (w * i as f32).sin();
}
(re * re + im * im).sqrt()
})
.collect();
let max = mags.iter().cloned().fold(1e-12, f32::max);
mags.iter().map(|m| 20.0 * (m / max).log10()).collect()
}
#[test]
fn self_oscillating_instruments_speak_in_tune() {
let ranges: &[(u8, &[u8])] = &[
(40, &[55, 74, 93]), (41, &[48, 66, 84]), (42, &[36, 55, 76]), (43, &[28, 43, 60]), (48, &[36, 62, 91]), (92, &[48, 72]), (110, &[55, 91]), (56, &[55, 82]), (57, &[40, 72]), (58, &[29, 58]), (60, &[36, 77]), (61, &[45, 79]), (64, &[57, 87]), (65, &[49, 81]), (66, &[44, 76]), (67, &[36, 69]), (68, &[58, 91]), (69, &[52, 81]), (70, &[34, 75]), (71, &[50, 91]), (109, &[55, 74]), (111, &[60, 84]), (72, &[74, 100]), (73, &[60, 96]), (74, &[65, 98]), (75, &[60, 96]), (76, &[60, 84]), (77, &[55, 88]), (78, &[74, 100]), (79, &[60, 89]), ];
for &(prog, notes) in ranges {
for ¬e in notes {
for vel in [35u8, 120] {
let x = render(prog, note, vel, 1.0, 1.0);
let seg = &x[(0.35 * SR) as usize..(0.95 * SR) as usize];
let f = 440.0 * 2f32.powf((note as f32 - 69.0) / 12.0);
let tag = format!("program {prog} note {note} vel {vel}");
assert!(rms(seg) > 1e-4, "{tag}: silent ({})", rms(seg));
let cents = 1200.0 * (pitch_near(seg, f) / f).log2();
assert!(cents.abs() < 12.0, "{tag}: {cents:+.1} cents");
let h = harmonics_db(seg, f, 6);
let odd = h[0].max(h[2]).max(h[4]);
assert!(odd > -15.0, "{tag}: octave up ({h:?})");
assert!(h[0].max(h[1]) > -15.0, "{tag}: twelfth up ({h:?})");
}
}
}
}
#[test]
fn former_duplicates_differ() {
let mut pairs = vec![
(21u8, 22u8),
(21, 23),
(20, 21),
(38, 39),
(50, 51),
(52, 53),
(52, 54),
(82, 83),
(84, 85),
(86, 87),
(96, 97),
(98, 99),
(100, 101),
(102, 103),
];
for a in 88u8..=95 {
for b in a + 1..=95 {
pairs.push((a, b));
}
}
for (a, b) in pairs {
let d = profile_distance(a, b);
assert!(d > 3.0, "programs {a} and {b} differ by only {d:.1} dB");
}
}
#[test]
fn choirs_and_free_reeds_are_in_tune() {
for prog in [20u8, 21, 22, 23, 52, 53, 54, 85] {
for note in [48u8, 69] {
let x = render(prog, note, 90, 1.0, 1.0);
let seg = &x[(0.4 * SR) as usize..(0.95 * SR) as usize];
let f = 440.0 * 2f32.powf((note as f32 - 69.0) / 12.0);
let cents = 1200.0 * (pitch_near(seg, f) / f).log2();
assert!(rms(seg) > 1e-3, "program {prog} note {note}: silent");
assert!(
cents.abs() < 15.0,
"program {prog} note {note}: {cents:+.1} cents"
);
}
}
}
#[test]
fn aahs_are_brighter_than_oohs() {
let ah = render(52, 57, 90, 1.0, 1.0);
let oo = render(53, 57, 90, 1.0, 1.0);
let w = |x: &[f32]| mean_freq(&x[(0.4 * SR) as usize..]);
assert!(
w(&ah) > 1.3 * w(&oo),
"ah {} Hz vs oo {} Hz",
w(&ah),
w(&oo)
);
}
#[test]
fn synth_filters_follow_velocity() {
for prog in [38u8, 62, 81, 90] {
let soft = render(prog, 48, 40, 0.6, 0.5);
let hard = render(prog, 48, 120, 0.6, 0.5);
let (fs, fh) = (mean_freq(&soft[2_000..]), mean_freq(&hard[2_000..]));
assert!(
fh > 1.3 * fs,
"program {prog}: soft {fs:.0} Hz, hard {fh:.0} Hz"
);
}
}
#[test]
fn accordion_brightens_with_pressure() {
let soft = render(21, 57, 35, 1.0, 1.0);
let hard = render(21, 57, 120, 1.0, 1.0);
let (fs, fh) = (
mean_freq(&soft[(0.3 * SR) as usize..]),
mean_freq(&hard[(0.3 * SR) as usize..]),
);
assert!(fh > 1.1 * fs, "soft {fs:.0} Hz, hard {fh:.0} Hz");
}
#[test]
fn saxophones_speak_promptly() {
for (prog, notes) in [
(64u8, [57u8, 72]),
(65, [50, 65]),
(66, [45, 60]),
(67, [38, 53]),
] {
for note in notes {
for vel in [35u8, 100] {
let x = render(prog, note, vel, 1.0, 1.0);
let steady = rms(&x[(0.5 * SR) as usize..(0.9 * SR) as usize]);
let early = rms(&x[(0.06 * SR) as usize..(0.08 * SR) as usize]);
let db = 20.0 * (early / steady).log10();
assert!(
db > -20.0,
"program {prog} note {note} vel {vel}: {db:.1} dB at 60–80 ms"
);
}
}
}
}
#[test]
fn self_oscillating_instruments_stop() {
for prog in [40u8, 43, 48, 56, 58, 61, 65, 68, 71, 73, 75, 109] {
let x = render(prog, 52, 100, 0.5, 4.0);
let held = rms(&x[(0.3 * SR) as usize..(0.5 * SR) as usize]);
let tail = rms(&x[(3.5 * SR) as usize..]);
assert!(tail < held * 1e-3, "program {prog}: {held} → {tail}");
}
}
#[test]
fn bowed_strings_make_helmholtz_motion() {
for prog in [40u8, 42, 110] {
for note in [62u8, 74] {
let x = render(prog, note, 90, 1.0, 1.0);
let f = 440.0 * 2f32.powf((note as f32 - 69.0) / 12.0);
let h =
harmonics_db(&x[(0.4 * SR) as usize..(0.9 * SR) as usize], f, 4);
assert!(h[0] > -1.0, "program {prog} note {note}: {h:?}");
let upper = h[1].max(h[2]).max(h[3]);
assert!(upper > -20.0, "program {prog} note {note}: {h:?}");
}
}
}
#[test]
fn brass_brightens_with_dynamics() {
for prog in [56u8, 57, 60] {
let soft =
mean_freq(&render(prog, 60, 35, 1.0, 0.8)[(0.3 * SR) as usize..]);
let loud =
mean_freq(&render(prog, 60, 127, 1.0, 0.8)[(0.3 * SR) as usize..]);
assert!(
loud > 1.8 * soft,
"program {prog}: pp {soft:.0} Hz, ff {loud:.0} Hz"
);
}
}
#[test]
fn clarinet_is_odd_and_saxophone_is_not() {
let f = 440.0 * 2f32.powf((60.0 - 69.0) / 12.0);
let seg = |p| render(p, 60, 100, 1.0, 1.0)[(0.4 * SR) as usize..].to_vec();
let cl = harmonics_db(&seg(71), f, 4);
let sx = harmonics_db(&seg(65), f, 4);
assert!(
cl[1] < cl[2] - 15.0,
"clarinet h2 {:.1} vs h3 {:.1}",
cl[1],
cl[2]
);
assert!(sx[1] > -15.0, "saxophone h2 {:.1}", sx[1]);
}
#[test]
fn basses_are_plucked_strings() {
for prog in 32u8..=37 {
for note in [28u8, 40] {
let x = render(prog, note, 100, 2.0, 1.5);
let seg = |a: f32, b: f32| &x[(a * SR) as usize..(b * SR) as usize];
let (attack, body) =
(mean_freq(seg(0.0, 0.05)), mean_freq(seg(0.3, 0.5)));
let contrast = if prog == 32 { 1.2 } else { 1.5 };
assert!(
attack > contrast * body,
"program {prog} note {note}: attack {attack:.0} Hz vs body {body:.0} Hz"
);
let (early, late) = (rms(seg(0.0, 0.3)), rms(seg(1.0, 1.3)));
assert!(
late < early * 0.5,
"program {prog} note {note} does not decay: {early} → {late}"
);
}
}
for prog in 33u8..=37 {
let p = harmonic_profile(prog, 40);
let (h2, h3) = (p[3], p[5]); assert!(
h2 > -12.0 && h3 > -20.0,
"program {prog}: h2 {h2:.1}, h3 {h3:.1} dB"
);
}
}
}