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
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,11 @@ sets the configuration cancels the drain. `reset()` also cancels the drain.

### Reset

`reset()` keeps the configuration, the random stream and some controller and
filter state. If you need a fully new state, construct a new processor.
`reset()` restores the initial state for the current configuration without
memory allocation. It clears the audio histories, the controllers, the held
MIDI notes, the feedback and the frame count. It loads the configured
correction notes again. It also starts the random stream from `random_seed`.
The configuration and `formant_generation` in the telemetry do not change.

## Feedback and telemetry

Expand Down
9 changes: 4 additions & 5 deletions src/effects/punch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ const TWO_OVER_PI: Q14 = Q14::from_bits(10430);
/// its envelope and window state do not change.
///
/// A gain change updates the cached drive at the next boundary of the peak
/// window. A reset calculates that drive again immediately from the
/// configured gain.
/// window. A reset restores the default base drive until that boundary.
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
#[cfg_attr(
Expand Down Expand Up @@ -160,14 +159,14 @@ impl Punch {
self.gain = Q8::from_f64(db_to_gain(config.gain_db as f64));
self.ceiling_gain = Q14::from_f64(db_to_gain(config.ceiling_db as f64));
}
/// Clears both envelopes and the current window, then calculates the drive
/// again from the base drive and the input gain.
/// Clears both envelopes and the current window, and restores the base
/// drive.
pub(crate) fn reset(&mut self) {
self.slow = Q14::default();
self.fast = Q14::default();
self.counter = 0;
self.peak = Q14::default();
self.drive = BASE_DRIVE.mul(self.gain);
self.drive = BASE_DRIVE;
}

/// Moves `current` toward `target` with a one-pole filter.
Expand Down
27 changes: 22 additions & 5 deletions src/effects/reverb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ pub enum ReverbMode {
/// The reverb is disabled by default. In bypass, the stage returns the input
/// and does not advance the delay network or the damping filters. If the
/// stage is enabled and the mix is zero, the network still operates. A reset
/// clears the delay buffer but keeps the memories of the damping filters. The
/// mix, diffusion and decay controls are quantized to Q14. The function of
/// each control changes with the network.
/// clears the delay buffer, the write position and the memories of the
/// damping filters. The mix, diffusion and decay controls are quantized to
/// Q14. The function of each control changes with the network.
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
#[cfg_attr(
Expand Down Expand Up @@ -209,11 +209,23 @@ impl Reverb {
}
/// Clears only the delay contents, and keeps the write cursor and the
/// damping memories.
pub(crate) fn reset_delay(&mut self) {
if self.dirty {
self.delay.clear();
self.dirty = false;
}
}
/// Clears the delay contents, resets the write cursor to zero and clears
/// both damping filters.
pub(crate) fn reset(&mut self) {
if self.dirty {
self.delay.reset();
self.dirty = false;
} else {
self.delay.write = 0;
}
self.lpf1.reset();
self.lpf2.reset();
}
/// Returns the largest absolute Q14 wet contribution since the last
/// clear, as f32.
Expand Down Expand Up @@ -431,7 +443,7 @@ struct DelayLine {
/// the Q14 audio scale.
buffer: Vec<Q14>,
/// The current frame position, always less than the buffer length. A
/// reset does not change it.
/// delay clear keeps it, and a full reset sets it to zero.
write: usize,
}
impl DelayLine {
Expand All @@ -444,9 +456,14 @@ impl DelayLine {
}
}
/// Clears the samples and does not move the current write position.
fn reset(&mut self) {
fn clear(&mut self) {
self.buffer.fill(Q14::default());
}
/// Clears the samples and sets the write position to zero.
fn reset(&mut self) {
self.clear();
self.write = 0;
}
/// Maps a delay to a storage index with a maximum of one wrap.
///
/// Each offset from the caller must not be more than the buffer length.
Expand Down
40 changes: 31 additions & 9 deletions src/harmony.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,8 @@ pub(crate) struct HarmonyState {
/// The last usable Q14 ratio of the input period to the smoothed target
/// period for each voice.
current_ratio: [Q14; HARMONY_VOICES],
/// The Q12 attack and release envelope. A MIDI release or a reset does
/// not clear it.
/// The Q12 attack and release envelope. A MIDI release does not clear it,
/// and a full reset sets it to zero.
smoothed_gain: [Q12; HARMONY_VOICES],
/// The last active Q6 target. It is kept while the gain of a released
/// voice decreases to zero.
Expand Down Expand Up @@ -509,8 +509,8 @@ impl HarmonyState {
/// Applies a configuration that validation already accepted.
///
/// If the source changes to an interval source or to a named or degree
/// chord source, the function calls `reset`. It prepares the tables
/// again only for the fields that changed.
/// chord source, the function calls `release_notes`. It prepares the
/// tables again only for the fields that changed.
pub(crate) fn apply_config(&mut self, config: HarmonyConfig) {
let old = self.config;
self.config = config;
Expand All @@ -523,7 +523,7 @@ impl HarmonyState {
| HarmonySource::ChordByName
)
{
self.reset();
self.release_notes();
}
if old.key != config.key
|| old.scale != config.scale
Expand Down Expand Up @@ -560,7 +560,7 @@ impl HarmonyState {
/// chord again from the released notes. Thus the chord voices decay
/// with the gain release. The other chord sources keep their chord
/// preparation.
pub(crate) fn reset(&mut self) {
pub(crate) fn release_notes(&mut self) {
self.midi_period.fill(Q6::ZERO);
self.portamento_period.fill(Q6::ZERO);
self.pending_gain.fill(Q12::ZERO);
Expand All @@ -570,6 +570,28 @@ impl HarmonyState {
self.setup_chord();
}
}
/// Restores the envelopes, targets, MIDI slots and scale caches to their
/// initial values for the current configuration.
pub(crate) fn reset(&mut self) {
self.midi_note = [0; HARMONY_VOICES];
self.midi_period = [Q6::ZERO; HARMONY_VOICES];
self.midi_gain = [Q12::ZERO; HARMONY_VOICES];
self.midi_order = [0; HARMONY_VOICES];
self.portamento_period = [Q6::ZERO; HARMONY_VOICES];
self.current_ratio = [Q14::ONE; HARMONY_VOICES];
self.smoothed_gain = [Q12::ZERO; HARMONY_VOICES];
self.period_output = [Q6::ZERO; HARMONY_VOICES];
self.target_period = [Q6::ZERO; HARMONY_VOICES];
self.chord_period = [Q6::ZERO; HARMONY_VOICES];
self.chord_gain = [Q12::from_bits(-16_384); HARMONY_VOICES];
self.current_idx = [39; HARMONY_VOICES];
self.scale_target = [Q6::ZERO; HARMONY_VOICES];
self.scale_note = [0; HARMONY_VOICES];
self.pending_gain.fill(Q12::ZERO);
self.pending_order.fill(0);
self.event_counter = 0;
self.setup_chord();
}
/// Converts cents above C4 to a Q6 sample period. C4 is 900 cents below
/// A4 at 440 Hz.
/// The intermediate period is rounded to f32 before the fixed-point
Expand Down Expand Up @@ -701,12 +723,12 @@ impl HarmonyState {
/// the omni sources. The omni sources steal the oldest voice and resume
/// the newest pending note. If two orders are equal, strict comparisons
/// keep the lowest voice index or pitch. Each Note Off clears the pending
/// entry of its pitch. All Notes Off calls `reset` and returns `false`.
/// The gain releases occur at the next control steps.
/// entry of its pitch. All Notes Off calls `release_notes` and returns
/// `false`. The gain releases occur at the next control steps.
pub(crate) fn apply_validated_midi(&mut self, event: MidiEvent) -> bool {
let (channel, note, velocity) = match event {
MidiEvent::AllNotesOff => {
self.reset();
self.release_notes();
return false;
}
MidiEvent::NoteOn {
Expand Down
7 changes: 4 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@
//! successful process call, a successful configuration call or a reset
//! cancels the drain.
//!
//! [`Processor::reset`] keeps the configuration and some controller state, for
//! example the random stream. Its documentation lists the state that it keeps.
//! If you need a fully new instance, make a new processor.
//! [`Processor::reset`] restores the initial state for the current
//! configuration without memory allocation. It clears the audio histories, the
//! controllers, the held MIDI notes, the feedback and the frame count. The
//! configuration and [`Telemetry::formant_generation`] do not change.
//!
//! # Execution model
//!
Expand Down
10 changes: 8 additions & 2 deletions src/numeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ pub(crate) fn envelope_decay(milliseconds: f64, rate: f64, window: usize) -> f64
/// The lookup cache and the pseudo-random state for all stages of one
/// processor chain.
///
/// An audio reset keeps this state.
/// A stereo change keeps this state, and a processor reset restores it.
#[derive(Debug)]
pub(crate) struct NumericContext {
/// The lookup cursor for Punch. An audio reset keeps its position.
/// The lookup cursor for Punch.
pub atan_q14: AtanQ14,
/// The random stream for choir and aspiration. The order in which stages
/// read values sets the random values that later stages get.
Expand All @@ -180,6 +180,12 @@ impl NumericContext {
random: RandomStream::new(seed),
}
}
/// Restores the initial arctangent lookup cursor and starts the random
/// stream again from `seed`.
pub(crate) fn reset(&mut self, seed: u32) {
self.atan_q14 = AtanQ14::default();
self.random = RandomStream::new(seed);
}
}
/// The capacity in frames of the history for pitch and resynthesis. The
/// value is the same at the two supported rates.
Expand Down
2 changes: 1 addition & 1 deletion src/numeric/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! stream. The order in which stages read values is part of the repeatable
//! result. If you enable a stage that reads the stream, later stages get
//! different values. This generator is for audio. It is not a cryptographic
//! random source. An audio reset does not set a new seed.
//! random source. A processor reset starts the stream again from the seed.

/// The 48-bit linear congruential state, made from a 32-bit seed.
#[derive(Debug)]
Expand Down
10 changes: 9 additions & 1 deletion src/pitch/tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl PitchTracker {
/// memories, and requests acquisition.
/// The acquisition tables clear at the next decimated call. The previous
/// usable period stays until another tracking update replaces it.
pub(crate) fn reset(&mut self) {
pub(crate) fn reset_audio(&mut self) {
let written = usize::from(self.written);
if written == HISTORY {
self.raw.fill(0);
Expand All @@ -165,6 +165,14 @@ impl PitchTracker {
}
}

/// Clears the audio state, the previous usable period and the last minimum
/// slot.
pub(crate) fn reset(&mut self) {
self.reset_audio();
self.previous_period = Q6::default();
self.best = 0;
}

/// Invalidates the current period and keeps the previous usable period.
/// The acquisition tables clear later, at the next acquisition sample.
fn restart(&mut self) {
Expand Down
59 changes: 30 additions & 29 deletions src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ impl Processor {
}
if self.config.resynthesis.stereo_output != config.resynthesis.stereo_output {
// Resynthesis resets its histories if its stereo setting changes.
self.reset_post_resynthesis();
self.reset_post_resynthesis(false);
}
if self.config.resynthesis.random_seed != config.resynthesis.random_seed {
self.numeric.random = RandomStream::new(config.resynthesis.random_seed as u32);
Expand All @@ -505,45 +505,46 @@ impl Processor {
self.drain_remaining = None;
}

/// Clears the audio histories, held notes, pitch feedback, peak and frame
/// count.
///
/// This method cancels an active drain. It also clears the correction
/// ratio, the Punch envelopes, the Warm filters, the Alien recording and
/// the reverb delays. It releases the harmony MIDI notes and, in
/// MIDI-chord mode, the chord voices. It keeps these items:
///
/// - the configuration
/// - the random stream and the arctangent lookup position of Punch
/// - the reverb low-pass memories and the reverb write position
/// - correction key 127 and the active correction note table
/// - the last usable period of the pitch tracker, which fills unvoiced
/// gaps
/// - the harmony gain envelopes and the last voice ratios of the harmony
/// controller
/// - the prepared chord targets of the named and degree chord sources
/// - the choir drift
/// - the control-step counter of Choir and Harmony
/// - the phase of the harmonic modulation.
///
/// When the method clears the held keys, it does not load the configured
/// scale again. For a fully new state, make a new processor.
/// Restores the initial state for the current configuration without memory
/// allocation.
///
/// This method cancels an active drain. It clears all audio and filter
/// histories, the pitch tracker and the formant synthesis state. It also
/// clears the choir drift, the modulation phase and the harmony envelopes
/// and voices. The peak windows, the reported correction ratio and the
/// frame count reset to zero.
///
/// The method releases all harmony MIDI notes and all 128 correction keys.
/// Then it loads the configured correction scale again, with the initial
/// sample-rate rule of [`crate::CorrectionConfig`]. It restores the
/// initial Punch drive and arctangent cursor. It also starts the random
/// stream again from the low 32 bits of `random_seed`.
///
/// The preparation, the active configuration and
/// [`Telemetry::formant_generation`] do not change.
pub fn reset(&mut self) {
self.punch.reset();
self.warm.reset();
self.resynthesis.reset();
self.reset_post_resynthesis();
self.reset_post_resynthesis(true);
self.numeric
.reset(self.config.resynthesis.random_seed as u32);
self.frames = 0;
self.drain_remaining = None;
}

/// Clears the stages, the peak windows and the ratio after resynthesis.
///
/// Both a full reset and a change of stereo output clear this state. The
/// frame count does not change here.
fn reset_post_resynthesis(&mut self) {
/// A full reset clears the reverb filters and write cursor. A change of
/// stereo output clears only the reverb delay buffer. The frame count does
/// not change here.
fn reset_post_resynthesis(&mut self, full: bool) {
self.alien.reset();
self.reverb.reset();
if full {
self.reverb.reset();
} else {
self.reverb.reset_delay();
}
self.peak = 0;
self.clear_telemetry_peaks();
self.last_resample_ratio = 0.0;
Expand Down
Loading