Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 48 additions & 16 deletions src/effects/punch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ impl PunchConfig {
pub(crate) struct Punch {
/// The prepared rate, which converts the envelope times to sample counts.
rate: SampleRate,
/// The validated controls for this stage.
config: PunchConfig,
/// The number of frames in the 6 ms peak-hold window at `rate`.
hold_frames: usize,
/// The write offset into the leaf half of `hold_tree`.
Expand Down Expand Up @@ -134,23 +136,27 @@ impl Punch {
/// settings.
pub(crate) fn new(rate: SampleRate, config: PunchConfig) -> Self {
let hold_frames = (rate.as_u32() as usize * HOLD_MS).div_ceil(1000);
let drive_decay = Q30::from_f64(envelope_decay(DRIVE_SMOOTH_MS, rate.hz() as f64, 1));
let drive_decay = Self::decay(rate, DRIVE_SMOOTH_MS);
let mut result = Self {
rate,
config,
hold_frames,
hold_cursor: 0,
hold_tree: [Q28::ZERO; MAX_HOLD_FRAMES * 2],
slow: Q28::ZERO,
fast: Q28::ZERO,
slow_decay: Q30::ZERO,
fast_decay: Q30::ZERO,
fast_decay: Self::decay(rate, FAST_RELEASE_MS),
attack_decay: Q30::ZERO,
drive_decay,
gain: Q28::ONE,
ceiling_gain: Q28::ONE,
drive: BASE_DRIVE,
};
result.set_config(config);
result.prepare_impact();
result.prepare_attack();
result.prepare_gain();
result.prepare_ceiling();
result.reset();
result
}
Expand All @@ -171,21 +177,47 @@ impl Punch {
let out = ceiling * two_over_pi * (drive * env / ceiling).atan();
(out / env) as f32
}
/// Calculates the quantized controls again, and keeps the envelopes and
/// the current drive.
/// The processor controls the bypass. This stage never reads an enabled
/// flag.
/// Calculates the quantized controls that changed, and keeps the envelopes
/// and the current drive.
/// The processor controls the bypass. The audio path of this stage does
/// not read `enabled`.
pub(crate) fn set_config(&mut self, config: PunchConfig) {
let impact = coefficient_to_exp_value(1.0, 250.0, 5000.0, 1.0 - config.impact);
let decay = |milliseconds: f64| {
Q30::from_f64(envelope_decay(milliseconds, self.rate.hz() as f64, 1))
};
self.slow_decay = decay(impact as f64);
self.fast_decay = decay(FAST_RELEASE_MS);
self.attack_decay = decay(config.attack_ms as f64);
self.gain = Q28::from_f64(db_to_gain(config.gain_db as f64));
let old = self.config;
self.config = config;
if old.impact != config.impact {
self.prepare_impact();
}
if old.attack_ms != config.attack_ms {
self.prepare_attack();
}
if old.gain_db != config.gain_db {
self.prepare_gain();
}
if old.ceiling_db != config.ceiling_db {
self.prepare_ceiling();
}
}
/// Converts a half-life in milliseconds to a per-sample Q30 coefficient.
fn decay(rate: SampleRate, milliseconds: f64) -> Q30 {
Q30::from_f64(envelope_decay(milliseconds, rate.hz() as f64, 1))
}
/// Calculates the release coefficient of the adaptive follower.
fn prepare_impact(&mut self) {
let impact = coefficient_to_exp_value(1.0, 250.0, 5000.0, 1.0 - self.config.impact);
self.slow_decay = Self::decay(self.rate, impact as f64);
}
/// Calculates the shared attack coefficient of the two followers.
fn prepare_attack(&mut self) {
self.attack_decay = Self::decay(self.rate, self.config.attack_ms as f64);
}
/// Quantizes the linear input gain to Q28.
fn prepare_gain(&mut self) {
self.gain = Q28::from_f64(db_to_gain(self.config.gain_db as f64));
}
/// Quantizes the linear output ceiling to Q28, with a minimum of one LSB.
fn prepare_ceiling(&mut self) {
self.ceiling_gain =
Q28::from_f64(db_to_gain(config.ceiling_db as f64)).max(Q28::from_bits(1));
Q28::from_f64(db_to_gain(self.config.ceiling_db as f64)).max(Q28::from_bits(1));
}
/// Clears the peak hold and both envelopes, then sets the drive from the
/// base drive and the input gain.
Expand Down
44 changes: 38 additions & 6 deletions src/effects/reverb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,20 +195,52 @@ impl Reverb {
wet_peak: 0,
dirty: false,
};
result.set_config(config);
result.prepare_mix();
result.prepare_diffusion();
result.prepare_decay();
result.prepare_brightness();
result
}
/// Updates the coefficients and does not clear the delay or filter state.
/// Updates the coefficients that changed and does not clear the delay or
/// filter state.
/// The f32 multiplication by 100 occurs before the f64 division and the
/// quantization. If you combine these expressions, the quantized
/// coefficients can change.
pub(crate) fn set_config(&mut self, config: ReverbConfig) {
let old = self.config;
self.config = config;
self.mix = Q14::from_f64((config.mix * 100.0) as f64 / 100.0);
self.diffuse = Q14::from_f64((config.diffusion * 100.0) as f64 / 100.0);
self.decay = Q14::from_f64((config.decay * 100.0) as f64 / 105.0);
if old.mix != config.mix {
self.prepare_mix();
}
if old.diffusion != config.diffusion {
self.prepare_diffusion();
}
if old.decay != config.decay {
self.prepare_decay();
}
if old.brightness_hz != config.brightness_hz {
self.prepare_brightness();
}
}
/// Quantizes the wet fraction to Q14.
fn prepare_mix(&mut self) {
self.mix = Q14::from_f64((self.config.mix * 100.0) as f64 / 100.0);
}
/// Quantizes the diffusion control and the allpass coefficients of
/// MediumHall and LargeHall to Q14.
fn prepare_diffusion(&mut self) {
self.diffuse = Q14::from_f64((self.config.diffusion * 100.0) as f64 / 100.0);
self.hall_diffuse = [0.4, 0.5, 0.6, 0.8, 0.9, 0.95].map(|g| mul(self.diffuse, q14(g)));
let brightness = config
}
/// Quantizes the feedback coefficient to Q14.
fn prepare_decay(&mut self) {
self.decay = Q14::from_f64((self.config.decay * 100.0) as f64 / 105.0);
}
/// Updates the coefficients of the two low-pass filters and keeps their
/// histories.
fn prepare_brightness(&mut self) {
let brightness = self
.config
.brightness_hz
.min((self.rate.hz() as f64 * 0.49) as f32);
let design = Design::low_pass_one_pole(self.rate.hz(), brightness);
Expand Down
73 changes: 57 additions & 16 deletions src/formant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ pub(crate) struct Formant {
/// The Q14 compensation, from the aspiration cutoff divided by the
/// sample rate.
noise_amplitude: i32,
/// If `true`, `add_noise` returns the input and does not advance the
/// random stream or the aspiration filter.
aspiration_muted: bool,
/// The wrapping count of model designs that the stage tried. Only the
/// telemetry reads it.
generation: u32,
Expand Down Expand Up @@ -354,32 +357,70 @@ impl Formant {
noise_filter: Biquad::new(),
noise_gain: 0_i32,
noise_amplitude: 0_i32,
aspiration_muted: false,
generation: 0,
bank_epoch: 0,
};
result.set_config(config);
result.prepare_width();
result.prepare_stretch();
result.prepare_aspiration_mix();
result.prepare_aspiration_cutoff();
result
}
/// Quantizes the validated controls, prepares the Q7 map for the area
/// interpolation and updates the coefficients of the aspiration filter.
/// Quantizes the validated controls that changed, updates the Q7 area map
/// and updates the aspiration filter.
///
/// The histories do not change. A valid stretch gives a model with 25 to
/// 99 entries. Thus, the last point of the map and the denominators of
/// the interpolation are always defined.
/// The histories do not change.
pub(crate) fn set_config(&mut self, config: FormantConfig) {
let old = self.config;
self.config = config;
self.width = (config.width as f64 * 16384.0) as i32;
self.width_squared = (config.width as f64 * config.width as f64 * 16384.0) as i32;
self.stretch = (config.stretch as f64 * 16384.0) as i32;
if old.width != config.width {
self.prepare_width();
}
if old.stretch != config.stretch {
self.prepare_stretch();
}
if old.aspiration_mix != config.aspiration_mix {
self.prepare_aspiration_mix();
}
if old.aspiration_cutoff_hz != config.aspiration_cutoff_hz {
self.prepare_aspiration_cutoff();
}
}
/// Mutes or restores the random aspiration and keeps the configured
/// controls.
pub(crate) fn set_aspiration_muted(&mut self, muted: bool) {
self.aspiration_muted = muted;
}
/// Quantizes the width and its square to Q14.
fn prepare_width(&mut self) {
self.width = (self.config.width as f64 * 16384.0) as i32;
self.width_squared = (self.config.width as f64 * self.config.width as f64 * 16384.0) as i32;
}
/// Quantizes the stretch and prepares the Q7 map for the area
/// interpolation.
///
/// A valid stretch gives a model with 25 to 99 entries. Thus, the last
/// point of the map and the denominators of the interpolation are always
/// defined.
fn prepare_stretch(&mut self) {
self.stretch = (self.config.stretch as f64 * 16384.0) as i32;
self.stretch_count =
(((ORDER - 1) as f64 * config.stretch as f64 + 1.001) as usize).min(MAX_ORDER);
(((ORDER - 1) as f64 * self.config.stretch as f64 + 1.001) as usize).min(MAX_ORDER);
for i in 0..self.stretch_count - 1 {
let fraction = i as f64 / (self.stretch_count - 1) as f64;
self.stretch_map[i] = ((ORDER - 1) as f64 * fraction * 128.0) as i32;
}
self.stretch_map[self.stretch_count - 1] = ((ORDER - 1) * 128) as i32;
self.noise_gain = (config.aspiration_mix as f64 * 16384.0) as i32;
let frequency = (config.aspiration_cutoff_hz as i32)
}
/// Quantizes the aspiration level to Q14.
fn prepare_aspiration_mix(&mut self) {
self.noise_gain = (self.config.aspiration_mix as f64 * 16384.0) as i32;
}
/// Updates the compensation and the high-pass coefficients of the
/// aspiration filter, and keeps the filter history.
fn prepare_aspiration_cutoff(&mut self) {
let frequency = (self.config.aspiration_cutoff_hz as i32)
.min((self.rate.hz() as f64 * 0.48) as i32)
.max(500);
self.noise_amplitude = (frequency as f64 * 0.8 / self.rate.hz() as f64 * 16384.0) as i32;
Expand Down Expand Up @@ -739,11 +780,11 @@ impl Formant {
/// Blends random aspiration into a Q14 sample word. The signal modulates
/// the aspiration, and a high-pass filter changes it.
///
/// If the level is 0.01 or less, the function returns immediately. The
/// random stream and the noise filter then do not advance. If not, the
/// function reads one random value.
/// If aspiration is muted or the level is 0.01 or less, the function
/// returns immediately. The random stream and the noise filter then do not
/// advance. If not, the function reads one random value.
fn add_noise(&mut self, sample: i32, context: &mut NumericContext) -> i32 {
if self.config.aspiration_mix <= 0.01 {
if self.aspiration_muted || self.config.aspiration_mix <= 0.01 {
return sample;
}
let random = context.random.next_u31() >> 17;
Expand Down
18 changes: 5 additions & 13 deletions src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -884,9 +884,9 @@ impl Processor {
/// unused frames.
///
/// The method mutes aspiration temporarily, but does not change the
/// settings that the host sees. It restores the configuration before
/// completion resets the state. `Some(0)` records completion, so later
/// drain calls continue to return zero.
/// settings that the host sees. It restores aspiration before completion
/// resets the state. `Some(0)` records completion, so later drain calls
/// continue to return zero.
fn drain_into(
&mut self,
frames: usize,
Expand All @@ -895,19 +895,11 @@ impl Processor {
) -> Result<usize, Error> {
self.check_block(frames)?;
let count = self.drain_remaining.unwrap_or(0).min(frames);
let configured = self.config.resynthesis;
if configured.formant.aspiration_mix != 0.0 {
let mut silent = configured;
silent.formant.aspiration_mix = 0.0;
// Zero aspiration is valid in all validated configurations.
self.resynthesis.apply_config(silent);
}
self.resynthesis.set_aspiration_muted(true);
for index in 0..count {
write(index, self.next(0, layout));
}
if configured.formant.aspiration_mix != 0.0 {
self.resynthesis.apply_config(configured);
}
self.resynthesis.set_aspiration_muted(false);
if let Some(remaining) = self.drain_remaining {
let remaining = remaining - count;
if remaining == 0 {
Expand Down
Loading
Loading