pub mod bowed;
pub mod channel;
pub mod choir;
pub mod cymbal;
pub mod drums;
pub mod env;
pub mod filter;
pub mod fx;
pub mod leslie;
pub mod modal;
pub mod osc;
pub mod patches;
pub mod perc;
pub mod piano;
pub mod reed;
pub mod reverb;
pub mod voice;
pub mod waveguide;
pub mod wind;
use channel::Channel;
use leslie::Leslie;
use reverb::Reverb;
use voice::Voice;
const MAX_VOICES: usize = 64;
pub struct Synth {
pub sr: f32,
pub channels: [Channel; 16],
voices: Vec<Voice>,
sample_clock: u64,
pub master_gain: f32,
reverb: Reverb,
channel_buf: Vec<f32>,
send_buf: Vec<f32>,
leslies: [Option<Leslie>; 16],
}
impl Synth {
pub fn new(sr: f32) -> Self {
let channels = std::array::from_fn(|i| Channel::new(i as u8));
Self {
sr,
channels,
voices: Vec::with_capacity(MAX_VOICES),
sample_clock: 0,
master_gain: 0.8,
reverb: Reverb::new(sr),
channel_buf: Vec::new(),
send_buf: Vec::new(),
leslies: std::array::from_fn(|_| None),
}
}
pub fn note_on(&mut self, channel: u8, note: u8, velocity: u8) {
if velocity == 0 {
self.note_off(channel, note);
return;
}
let ch_idx = channel as usize;
let ch = &self.channels[ch_idx];
let vel = velocity_curve(velocity);
let voice = if ch.is_drums {
let dp = drums::drum_patch(note);
if matches!(dp.sound, drums::DrumSound::Cymbal(_)) && dp.group == 0 {
let clock = self.sample_clock;
if self
.voices
.iter_mut()
.any(|v| v.restrike(channel, note, vel, clock))
{
return;
}
}
if dp.group != 0 {
for v in self.voices.iter_mut() {
if v.channel == channel && v.drum_group() == dp.group {
v.note_off(false);
}
}
}
Voice::new_drum(self.sr, channel, note, vel, &dp, self.sample_clock)
} else {
let p = patches::gm_patch(ch.program);
if p.piano.is_some() {
Voice::new_piano(
self.sr,
channel,
note,
vel,
&p,
ch.bend_semitones(),
self.sample_clock,
)
} else if p.bowed.is_some()
|| p.wind.is_some()
|| p.choir.is_some()
|| p.reed.is_some()
|| p.fx.is_some()
{
Voice::new_waveguide(self.sr, channel, note, vel, &p, self.sample_clock)
} else if p.modal.is_some() {
Voice::new_modal(
self.sr,
channel,
note,
vel,
&p,
ch.bend_semitones(),
self.sample_clock,
)
} else if p.pluck.is_some() {
Voice::new_plucked(
self.sr,
channel,
note,
vel,
&p,
ch.bend_semitones(),
self.sample_clock,
)
} else {
Voice::new_tonal(
self.sr,
channel,
note,
vel,
&p,
ch.bend_semitones(),
self.sample_clock,
)
}
};
self.add_voice(voice);
}
pub fn note_off(&mut self, channel: u8, note: u8) {
let sustain = self.channels[channel as usize].sustain;
for v in self.voices.iter_mut() {
if v.channel == channel
&& v.note == note
&& !v.released
&& !v.held_by_sustain
{
v.note_off(sustain);
}
}
}
pub fn all_notes_off(&mut self, channel: u8) {
for v in self.voices.iter_mut() {
if v.channel == channel {
v.note_off(false);
}
}
}
pub fn all_sound_off(&mut self, channel: u8) {
self.voices.retain(|v| v.channel != channel);
}
pub fn reset_controllers(&mut self, channel: u8) {
let ch = &mut self.channels[channel as usize];
ch.expression = 1.0;
ch.mod_wheel = 0.0;
ch.sustain = false;
ch.pitch_bend = 0.0;
}
pub fn program_change(&mut self, channel: u8, program: u8) {
self.channels[channel as usize].program = program;
}
pub fn pitch_bend(&mut self, channel: u8, bend: f32) {
self.channels[channel as usize].pitch_bend = bend.clamp(-1.0, 1.0);
}
pub fn control_change(&mut self, channel: u8, cc: u8, value: u8) {
let v = value as f32 / 127.0;
let mut released_sustain = false;
{
let ch = &mut self.channels[channel as usize];
match cc {
1 => ch.mod_wheel = v,
6 if ch.rpn_msb == 0 && ch.rpn_lsb == 0 => {
ch.bend_range = value as f32;
}
7 => ch.volume = v,
10 => ch.pan = v * 2.0 - 1.0,
91 => ch.reverb = v,
11 => ch.expression = v,
64 => {
let on = value >= 64;
ch.sustain = on;
if !on {
released_sustain = true;
}
}
100 => ch.rpn_lsb = value,
101 => ch.rpn_msb = value,
_ => {}
}
}
if released_sustain {
for v in self.voices.iter_mut() {
if v.channel == channel {
v.release_if_sustained();
}
}
return;
}
match cc {
120 => self.all_sound_off(channel),
121 => self.reset_controllers(channel),
123 => self.all_notes_off(channel),
_ => {}
}
}
pub fn render(&mut self, out: &mut [f32]) {
out.fill(0.0);
self.send_buf.clear();
self.send_buf.resize(out.len(), 0.0);
self.channel_buf.resize(out.len(), 0.0);
let mg = self.master_gain;
for (c, ch) in self.channels.iter().enumerate() {
if !self.voices.iter().any(|v| v.channel as usize == c) {
continue;
}
let buf = &mut self.channel_buf;
buf.fill(0.0);
for v in self.voices.iter_mut().filter(|v| v.channel as usize == c) {
v.render_add(buf, ch);
}
if let Some(spec) = patches::gm_patch(ch.program).leslie {
let (gl, gr) = voice::equal_power_pan(ch.pan);
let fast = spec.fast || ch.mod_wheel >= 0.5;
self.leslies[c]
.get_or_insert_with(|| Leslie::new(spec, self.sr))
.process(buf, fast, gl, gr);
}
let send = ch.reverb;
for ((o, s), x) in out.iter_mut().zip(&mut self.send_buf).zip(buf.iter())
{
*o += *x;
*s += *x * send;
}
}
self.reverb.process(&self.send_buf, out);
for s in out.iter_mut() {
*s *= mg;
*s = soft_clip(*s);
}
let frames = out.len() / 2;
self.sample_clock = self.sample_clock.wrapping_add(frames as u64);
self.voices.retain(|v| !v.is_idle());
}
fn add_voice(&mut self, v: Voice) {
if self.voices.len() >= MAX_VOICES {
self.steal_voice();
}
self.voices.push(v);
}
fn steal_voice(&mut self) {
let mut best: Option<usize> = None;
let mut best_released = false;
let mut best_age = u64::MAX;
for (i, v) in self.voices.iter().enumerate() {
if v.released && !best_released {
best = Some(i);
best_released = true;
best_age = v.started_at;
} else if v.released == best_released && v.started_at < best_age {
best = Some(i);
best_age = v.started_at;
}
}
if let Some(i) = best {
self.voices.swap_remove(i);
}
}
}
fn velocity_curve(velocity: u8) -> f32 {
let v = velocity as f32 / 127.0;
v * v
}
#[inline]
fn soft_clip(x: f32) -> f32 {
x.tanh()
}