From 8c4f7ca52d040ed9b6bdf966f399a75258f0923d Mon Sep 17 00:00:00 2001 From: "google-labs-stitch[bot]" <315546542+google-labs-stitch[bot]@users.noreply.github.com> Date: Mon, 30 Nov 2026 06:35:54 +0000 Subject: [PATCH] refactor: remove dead event simulation and tighten helper visibility - Remove ParameterControl::apply_to and validate_in_sequence. - Remove the ProcessorConfig argument from ValidatedEvents::new. - Remove the ProcessorConfig argument from Wasm decode_events. - Remove Resynthesis::reset_dwell and the dead dwell resets. - Make the harmony interval validators and remove_glottal private. --- src/events.rs | 99 +--------------------------------------------- src/formant.rs | 2 +- src/harmony.rs | 8 +--- src/processor.rs | 2 +- src/resynthesis.rs | 14 ++----- src/wasm.rs | 27 ++++--------- 6 files changed, 16 insertions(+), 136 deletions(-) diff --git a/src/events.rs b/src/events.rs index ef07dee..dab847b 100644 --- a/src/events.rs +++ b/src/events.rs @@ -148,53 +148,6 @@ impl ParameterControl { Self::ReverbBrightnessHz(value) => reverb::validate_brightness_hz(value), } } - - /// Validates the scalar parameter and writes it into `config`. - pub(crate) fn apply_to(self, config: &mut ProcessorConfig) -> Result<(), Error> { - self.validate()?; - match self { - Self::PunchGainDb(value) => config.punch.gain_db = value, - Self::PunchImpact(value) => config.punch.impact = value, - Self::PunchCeilingDb(value) => config.punch.ceiling_db = value, - Self::PunchAttackMs(value) => config.punch.attack_ms = value, - Self::WarmDriveDb(value) => config.warm.drive_db = value, - Self::PitchThreshold(value) => config.resynthesis.pitch.threshold = value, - Self::RetuneMs(value) => config.resynthesis.correction.retune_ms = value, - Self::Humanize(value) => config.resynthesis.correction.humanize = value, - Self::FlexTune(value) => config.resynthesis.correction.flex_tune = value, - Self::PitchRatio(value) => config.resynthesis.pitch_ratio = value, - Self::FormantWidth(value) => config.resynthesis.formant.width = value, - Self::FormantStretch(value) => config.resynthesis.formant.stretch = value, - Self::FormantAspirationMix(value) => config.resynthesis.formant.aspiration_mix = value, - Self::FormantAspirationCutoffHz(value) => { - config.resynthesis.formant.aspiration_cutoff_hz = value; - } - Self::ChoirVoices(value) => config.resynthesis.choir_voices = value, - Self::ChoirDetune(value) => config.resynthesis.choir_detune = value, - Self::ChoirDelay(value) => config.resynthesis.choir_delay = value, - Self::ChoirSpreadPercent(value) => config.resynthesis.choir_spread_percent = value, - Self::HarmonyRegisterPercent(value) => { - config.resynthesis.harmony.register_percent = value; - } - Self::HarmonyVoicingSpreadPercent(value) => { - config.resynthesis.harmony.voicing_spread_percent = value; - } - Self::HarmonyPortamentoMs(value) => { - config.resynthesis.harmony.portamento_ms = value; - } - Self::HarmonyVoices(value) => config.resynthesis.harmony.voices = value, - Self::HarmonyGain(value) => config.resynthesis.harmony_gain = value, - Self::HarmonicModulation(value) => { - config.resynthesis.harmonic_modulation = value; - } - Self::DryMix(value) => config.resynthesis.dry_mix = value, - Self::ReverbMix(value) => config.reverb.mix = value, - Self::ReverbDiffusion(value) => config.reverb.diffusion = value, - Self::ReverbDecay(value) => config.reverb.decay = value, - Self::ReverbBrightnessHz(value) => config.reverb.brightness_hz = value, - } - Ok(()) - } } /// A configuration or MIDI control that applies before a frame in the block. @@ -337,11 +290,7 @@ impl<'a> ValidatedEvents<'a> { /// first invalid event. The offset bounds have priority over the order. /// The order has priority over the payload validation. The function gives /// no tokens unless the full slice passes. - pub(crate) fn new( - events: &'a [TimedEvent], - frames: usize, - mut config: ProcessorConfig, - ) -> Result { + pub(crate) fn new(events: &'a [TimedEvent], frames: usize) -> Result { let mut previous = 0; for (index, event) in events.iter().enumerate() { let problem = if event.offset >= frames { @@ -349,7 +298,7 @@ impl<'a> ValidatedEvents<'a> { } else if event.offset < previous { Some(EventProblem::OffsetDecreasing) } else { - match validate_in_sequence(event.event, &mut config) { + match event.event.validate() { Ok(()) => None, Err(Error::InvalidConfig { field, problem }) => { Some(EventProblem::Config { field, problem }) @@ -380,47 +329,3 @@ impl<'a> ValidatedEvents<'a> { Some(ValidatedEvent(event.event)) } } - -/// Validates one event and advances the stack configuration copy. -fn validate_in_sequence(event: ControlEvent, config: &mut ProcessorConfig) -> Result<(), Error> { - match event { - ControlEvent::Configure(value) => { - value.validate()?; - *config = value; - Ok(()) - } - ControlEvent::Resynthesis(value) => { - value.validate()?; - config.resynthesis = value; - Ok(()) - } - ControlEvent::Punch(value) => { - value.validate()?; - config.punch = value; - Ok(()) - } - ControlEvent::Warm(value) => { - value.validate()?; - config.warm = value; - Ok(()) - } - ControlEvent::Alien(value) => { - value.validate()?; - config.alien = value; - Ok(()) - } - ControlEvent::Reverb(value) => { - value.validate()?; - config.reverb = value; - Ok(()) - } - ControlEvent::OutputGain(gain) => { - validate_output_gain(gain)?; - config.output_gain = gain; - Ok(()) - } - ControlEvent::Parameter(parameter) => parameter.apply_to(config), - ControlEvent::Midi(midi) | ControlEvent::CorrectionMidi(midi) => midi.validate(), - ControlEvent::Reset => Ok(()), - } -} diff --git a/src/formant.rs b/src/formant.rs index b4ea383..4060a68 100644 --- a/src/formant.rs +++ b/src/formant.rs @@ -533,7 +533,7 @@ impl Formant { /// The function subtracts the product of the previous sample and the /// glottal coefficient. It uses the common signed rounding rule. #[inline] - pub(crate) fn remove_glottal(&self, sample: i32, previous: i32) -> i32 { + fn remove_glottal(&self, sample: i32, previous: i32) -> i32 { sample.saturating_sub(round_q14(i64::from(previous) * i64::from(self.glottal))) } /// Puts the glottal envelope back from a Q14 excitation and the previous diff --git a/src/harmony.rs b/src/harmony.rs index f2adfe3..734460c 100644 --- a/src/harmony.rs +++ b/src/harmony.rs @@ -387,9 +387,7 @@ impl HarmonyConfig { } /// Checks the four fixed semitone offsets of harmony. -pub(crate) fn validate_chromatic_intervals( - chromatic_intervals: [i8; HARMONY_VOICES], -) -> Result<(), Error> { +fn validate_chromatic_intervals(chromatic_intervals: [i8; HARMONY_VOICES]) -> Result<(), Error> { for interval in chromatic_intervals { let field = "resynthesis.harmony.chromatic_intervals"; check_count(field, i64::from(interval), -24, 24)?; @@ -398,9 +396,7 @@ pub(crate) fn validate_chromatic_intervals( } /// Checks the four offsets in scale steps of harmony. -pub(crate) fn validate_diatonic_intervals( - diatonic_intervals: [i8; HARMONY_VOICES], -) -> Result<(), Error> { +fn validate_diatonic_intervals(diatonic_intervals: [i8; HARMONY_VOICES]) -> Result<(), Error> { for interval in diatonic_intervals { let field = "resynthesis.harmony.diatonic_intervals"; check_count(field, i64::from(interval), -14, 14)?; diff --git a/src/processor.rs b/src/processor.rs index 9f8ae9b..787b17d 100644 --- a/src/processor.rs +++ b/src/processor.rs @@ -786,7 +786,7 @@ impl Processor { events: &'a [TimedEvent], ) -> Result, Error> { self.check_block(frames)?; - ValidatedEvents::new(events, frames, self.config) + ValidatedEvents::new(events, frames) } /// Enforces the prepared block limit for each call. diff --git a/src/resynthesis.rs b/src/resynthesis.rs index f0e844f..f84fcb5 100644 --- a/src/resynthesis.rs +++ b/src/resynthesis.rs @@ -1142,8 +1142,6 @@ impl Resynthesis { self.correction = Q14::ONE; self.dwell = Q14::ZERO; self.scale_initialized = false; - self.previous_scale_index = 0; - self.previous_octave = 0; } /// Applies a MIDI event that the API or the block code checked before. @@ -1295,12 +1293,6 @@ impl Resynthesis { self.formant .envelope(self.formant_position, max_frequency_hz, input, output) } - /// Clears the dwell progress and the previous target bin. - fn reset_dwell(&mut self) { - self.dwell = Q14::ZERO; - self.previous_scale_index = 0; - self.previous_octave = 0; - } /// Maps the current dwell progress to a Q14 half-life decay coefficient. /// @@ -1350,7 +1342,7 @@ impl Resynthesis { self.octave = 0; target = self.shift.mul(period); if target <= Q6::ZERO { - self.reset_dwell(); + self.dwell = Q14::ZERO; return None; } while table_period(1) > target { @@ -1361,7 +1353,7 @@ impl Resynthesis { } } if target <= Q6::ZERO { - self.reset_dwell(); + self.dwell = Q14::ZERO; return None; } self.scale_initialized = true; @@ -1373,7 +1365,7 @@ impl Resynthesis { }); if target <= Q6::ZERO { self.scale_initialized = false; - self.reset_dwell(); + self.dwell = Q14::ZERO; return None; } while table_period(self.scale_index + 1) < target { diff --git a/src/wasm.rs b/src/wasm.rs index e868d16..1150471 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -324,9 +324,7 @@ impl WasmProcessor { Error::check_length(frames, right.length() as usize)?; self.processor.check_block(frames)?; let events = match events { - Some(events) => { - decode_events(&mut self.events, events, frames, self.processor.config())? - } + Some(events) => decode_events(&mut self.events, events, frames)?, None => &[], }; input.copy_to(&mut self.input[..frames]); @@ -360,9 +358,7 @@ impl WasmProcessor { let frames = audio.length() as usize; self.processor.check_block(frames)?; let events = match events { - Some(events) => { - decode_events(&mut self.events, events, frames, self.processor.config())? - } + Some(events) => decode_events(&mut self.events, events, frames)?, None => &[], }; audio.copy_to(&mut self.input[..frames]); @@ -401,9 +397,7 @@ impl WasmProcessor { Error::check_length(frames, right.length() as usize)?; self.processor.check_block(frames)?; let events = match events { - Some(events) => { - decode_events(&mut self.events, events, frames, self.processor.config())? - } + Some(events) => decode_events(&mut self.events, events, frames)?, None => &[], }; input.copy_to(&mut self.input_f32[..frames]); @@ -438,9 +432,7 @@ impl WasmProcessor { let frames = audio.length() as usize; self.processor.check_block(frames)?; let events = match events { - Some(events) => { - decode_events(&mut self.events, events, frames, self.processor.config())? - } + Some(events) => decode_events(&mut self.events, events, frames)?, None => &[], }; audio.copy_to(&mut self.input_f32[..frames]); @@ -471,9 +463,7 @@ impl WasmProcessor { ) -> Result<(), JsError> { let frames = self.check_frames(frames)?; let events = match events { - Some(events) => { - decode_events(&mut self.events, events, frames, self.processor.config())? - } + Some(events) => decode_events(&mut self.events, events, frames)?, None => &[], }; self.processor.process_planar_f32( @@ -505,9 +495,7 @@ impl WasmProcessor { ) -> Result<(), JsError> { let frames = self.check_frames(frames)?; let events = match events { - Some(events) => { - decode_events(&mut self.events, events, frames, self.processor.config())? - } + Some(events) => decode_events(&mut self.events, events, frames)?, None => &[], }; self.processor @@ -1044,7 +1032,6 @@ fn decode_events( buffer: &mut Vec, events: Ts, frames: usize, - config: ProcessorConfig, ) -> Result<&[TimedEvent], JsError> { let value = JsValue::from(events); let Some(array) = value.dyn_ref::() else { @@ -1061,7 +1048,7 @@ fn decode_events( Ok(()) })?; } - ValidatedEvents::new(buffer, frames, config)?; + ValidatedEvents::new(buffer, frames)?; Ok(buffer) }