From 96e9aaeab4fbd86d2040ed4a8156ee61ea6e042d 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:38:15 +0000 Subject: [PATCH] refactor(processor): merge duplicate paths into private helpers - Add private slice helpers for the six process methods. - Add drain_slice and drain_planar_slices for the six drain methods. - Pass PitchEstimate to Resynthesis::target_note. - Reuse Processor::feedback in Processor::telemetry. - Add Telemetry::active_voice_slice and Processor::apply_stage. --- src/processor.rs | 246 ++++++++++++++++++++++++++------------------- src/resynthesis.rs | 10 +- 2 files changed, 148 insertions(+), 108 deletions(-) diff --git a/src/processor.rs b/src/processor.rs index 9f8ae9b..0cd242b 100644 --- a/src/processor.rs +++ b/src/processor.rs @@ -173,10 +173,13 @@ impl Processor { output: &mut [[i16; 2]], events: &[TimedEvent], ) -> Result<(), Error> { - Error::check_length(input.len(), output.len())?; - self.process_into(input.len(), events, |processor, index| { - output[index] = processor.next(input[index], OutputLayout::Stereo); - }) + self.process_stereo_slice( + input, + output, + events, + std::convert::identity, + std::convert::identity, + ) } /// Processes mono PCM into separate left and right output slices. @@ -199,13 +202,14 @@ impl Processor { right: &mut [i16], events: &[TimedEvent], ) -> Result<(), Error> { - Error::check_length(input.len(), left.len())?; - Error::check_length(input.len(), right.len())?; - self.process_into(input.len(), events, |processor, index| { - let frame = processor.next(input[index], OutputLayout::Stereo); - left[index] = frame[0]; - right[index] = frame[1]; - }) + self.process_planar_slices( + input, + left, + right, + events, + std::convert::identity, + std::convert::identity, + ) } /// Replaces mono PCM with the stereo result after the downmix. @@ -227,10 +231,12 @@ impl Processor { audio: &mut [i16], events: &[TimedEvent], ) -> Result<(), Error> { - self.process_into(audio.len(), events, |processor, index| { - let output = processor.next(audio[index], OutputLayout::Mono); - audio[index] = output[0]; - }) + self.process_mono_slice( + audio, + events, + std::convert::identity, + std::convert::identity, + ) } /// Processes mono `f32` audio into `[left, right]` stereo frames. @@ -256,12 +262,7 @@ impl Processor { output: &mut [[f32; 2]], events: &[TimedEvent], ) -> Result<(), Error> { - Error::check_length(input.len(), output.len())?; - self.process_into(input.len(), events, |processor, index| { - output[index] = processor - .next(pcm_from_f32(input[index]), OutputLayout::Stereo) - .map(pcm_to_f32); - }) + self.process_stereo_slice(input, output, events, pcm_from_f32, pcm_to_f32) } /// Processes mono `f32` audio into separate left and right `f32` slices. @@ -285,13 +286,7 @@ impl Processor { right: &mut [f32], events: &[TimedEvent], ) -> Result<(), Error> { - Error::check_length(input.len(), left.len())?; - Error::check_length(input.len(), right.len())?; - self.process_into(input.len(), events, |processor, index| { - let frame = processor.next(pcm_from_f32(input[index]), OutputLayout::Stereo); - left[index] = pcm_to_f32(frame[0]); - right[index] = pcm_to_f32(frame[1]); - }) + self.process_planar_slices(input, left, right, events, pcm_from_f32, pcm_to_f32) } /// Replaces mono `f32` audio with the stereo result after the downmix. @@ -314,10 +309,7 @@ impl Processor { audio: &mut [f32], events: &[TimedEvent], ) -> Result<(), Error> { - self.process_into(audio.len(), events, |processor, index| { - let output = processor.next(pcm_from_f32(audio[index]), OutputLayout::Mono); - audio[index] = pcm_to_f32(output[0]); - }) + self.process_mono_slice(audio, events, pcm_from_f32, pcm_to_f32) } /// Starts a tail with the current [`tail_frames`](Self::tail_frames) @@ -352,11 +344,7 @@ impl Processor { /// If the output is larger than the prepared block limit, the method /// returns [`Error::BlockTooLarge`] and does not change state or output. pub fn drain(&mut self, output: &mut [[i16; 2]]) -> Result { - let count = self.drain_into(output.len(), OutputLayout::Stereo, |index, frame| { - output[index] = frame - })?; - output[count..].fill([0; 2]); - Ok(count) + self.drain_slice(output, OutputLayout::Stereo, std::convert::identity) } /// Renders the tail into separate output channels of equal size. @@ -371,14 +359,7 @@ impl Processor { /// [`Error::BlockTooLarge`]. If either error occurs, the two channels and /// the drain state do not change. pub fn drain_planar(&mut self, left: &mut [i16], right: &mut [i16]) -> Result { - Error::check_length(left.len(), right.len())?; - let count = self.drain_into(left.len(), OutputLayout::Stereo, |index, frame| { - left[index] = frame[0]; - right[index] = frame[1]; - })?; - left[count..].fill(0); - right[count..].fill(0); - Ok(count) + self.drain_planar_slices(left, right, std::convert::identity) } /// Renders the tail as mono PCM and returns the number of rendered frames. @@ -395,11 +376,7 @@ impl Processor { /// If the output is larger than the prepared block limit, the method /// returns [`Error::BlockTooLarge`] before it changes output or state. pub fn drain_mono(&mut self, output: &mut [i16]) -> Result { - let count = self.drain_into(output.len(), OutputLayout::Mono, |index, frame| { - output[index] = frame[0]; - })?; - output[count..].fill(0); - Ok(count) + self.drain_slice(output, OutputLayout::Mono, |frame| frame[0]) } /// Renders the next stereo tail frames as `f32` pairs and returns their @@ -415,11 +392,7 @@ impl Processor { /// If the output is larger than the prepared block limit, the method /// returns [`Error::BlockTooLarge`] and does not change state or output. pub fn drain_f32(&mut self, output: &mut [[f32; 2]]) -> Result { - let count = self.drain_into(output.len(), OutputLayout::Stereo, |index, frame| { - output[index] = frame.map(pcm_to_f32); - })?; - output[count..].fill([0.0; 2]); - Ok(count) + self.drain_slice(output, OutputLayout::Stereo, |frame| frame.map(pcm_to_f32)) } /// Renders the tail into separate `f32` output channels of equal size. @@ -438,14 +411,7 @@ impl Processor { left: &mut [f32], right: &mut [f32], ) -> Result { - Error::check_length(left.len(), right.len())?; - let count = self.drain_into(left.len(), OutputLayout::Stereo, |index, frame| { - left[index] = pcm_to_f32(frame[0]); - right[index] = pcm_to_f32(frame[1]); - })?; - left[count..].fill(0.0); - right[count..].fill(0.0); - Ok(count) + self.drain_planar_slices(left, right, pcm_to_f32) } /// Renders the tail as mono `f32` audio and returns the number of rendered @@ -462,11 +428,7 @@ impl Processor { /// If the output is larger than the prepared block limit, the method /// returns [`Error::BlockTooLarge`] before it changes output or state. pub fn drain_mono_f32(&mut self, output: &mut [f32]) -> Result { - let count = self.drain_into(output.len(), OutputLayout::Mono, |index, frame| { - output[index] = pcm_to_f32(frame[0]); - })?; - output[count..].fill(0.0); - Ok(count) + self.drain_slice(output, OutputLayout::Mono, |frame| pcm_to_f32(frame[0])) } /// Returns the finite tail budget for the current configuration. @@ -528,7 +490,7 @@ impl Processor { resample_ratio: self.last_resample_ratio, period_samples: estimate.period_samples, frequency_hz: estimate.frequency_hz, - target_midi_note: self.resynthesis.target_note(), + target_midi_note: self.resynthesis.target_note(estimate), voiced: estimate.voiced, peak_level: self.peak as f32 / 32_768.0, processed_frames: self.frames, @@ -551,21 +513,21 @@ impl Processor { /// [`take_telemetry`](Self::take_telemetry). The peak windows of the /// telemetry are independent of the peak window of [`Feedback`]. pub fn telemetry(&self) -> Telemetry { - let estimate = self.resynthesis.estimate(); + let feedback = self.feedback(); let mut voice_ratios = [0.0; 16]; let mut voice_gains = [0.0; 16]; let voice_count = self.resynthesis.voices(&mut voice_ratios, &mut voice_gains); let (alien_loop, alien_phase) = self.alien.loop_state(); Telemetry { - processed_frames: self.frames, + processed_frames: feedback.processed_frames, input_peak: self.input_peak as f32 / 32_768.0, output_peak: self.output_peaks.map(|peak| peak as f32 / 32_768.0), reverb_wet_peak: self.reverb.wet_peak(), - resample_ratio: self.last_resample_ratio, - period_samples: estimate.period_samples, - frequency_hz: estimate.frequency_hz, - target_midi_note: self.resynthesis.target_note(), - voiced: estimate.voiced, + resample_ratio: feedback.resample_ratio, + period_samples: feedback.period_samples, + frequency_hz: feedback.frequency_hz, + target_midi_note: feedback.target_midi_note, + voiced: feedback.voiced, clarity: self.resynthesis.clarity(), tracking: self.resynthesis.is_tracking(), voice_count, @@ -810,30 +772,12 @@ impl Processor { match event.into_inner() { ControlEvent::Configure(value) => self.apply_config(value), ControlEvent::Resynthesis(value) => { - let mut config = self.config; - config.resynthesis = value; - self.apply_config(config); - } - ControlEvent::Punch(value) => { - let mut config = self.config; - config.punch = value; - self.apply_config(config); - } - ControlEvent::Warm(value) => { - let mut config = self.config; - config.warm = value; - self.apply_config(config); - } - ControlEvent::Alien(value) => { - let mut config = self.config; - config.alien = value; - self.apply_config(config); - } - ControlEvent::Reverb(value) => { - let mut config = self.config; - config.reverb = value; - self.apply_config(config); + self.apply_stage(|config| config.resynthesis = value); } + ControlEvent::Punch(value) => self.apply_stage(|config| config.punch = value), + ControlEvent::Warm(value) => self.apply_stage(|config| config.warm = value), + ControlEvent::Alien(value) => self.apply_stage(|config| config.alien = value), + ControlEvent::Reverb(value) => self.apply_stage(|config| config.reverb = value), ControlEvent::OutputGain(value) => { self.config.output_gain = value; self.drain_remaining = None; @@ -845,6 +789,14 @@ impl Processor { } } + /// Copies the active configuration, updates one stage and applies the + /// result. + fn apply_stage(&mut self, update: impl FnOnce(&mut ProcessorConfig)) { + let mut config = self.config; + update(&mut config); + self.apply_config(config); + } + /// Updates one validated scalar parameter, cancels an active drain and /// calls only the setter for that field. fn apply_parameter(&mut self, parameter: ParameterControl) { @@ -1005,6 +957,57 @@ impl Processor { result } + /// Checks the slice lengths and renders mono input into stereo frames. + fn process_stereo_slice( + &mut self, + input: &[T], + output: &mut [[T; 2]], + events: &[TimedEvent], + to_pcm: impl Fn(T) -> i16, + from_pcm: impl Fn(i16) -> T + Copy, + ) -> Result<(), Error> { + Error::check_length(input.len(), output.len())?; + self.process_into(input.len(), events, |processor, index| { + output[index] = processor + .next(to_pcm(input[index]), OutputLayout::Stereo) + .map(from_pcm); + }) + } + + /// Checks the channel lengths and renders mono input into left and right + /// slices. + fn process_planar_slices( + &mut self, + input: &[T], + left: &mut [T], + right: &mut [T], + events: &[TimedEvent], + to_pcm: impl Fn(T) -> i16, + from_pcm: impl Fn(i16) -> T, + ) -> Result<(), Error> { + Error::check_length(input.len(), left.len())?; + Error::check_length(input.len(), right.len())?; + self.process_into(input.len(), events, |processor, index| { + let frame = processor.next(to_pcm(input[index]), OutputLayout::Stereo); + left[index] = from_pcm(frame[0]); + right[index] = from_pcm(frame[1]); + }) + } + + /// Renders mono audio in place with the stereo downmix. + fn process_mono_slice( + &mut self, + audio: &mut [T], + events: &[TimedEvent], + to_pcm: impl Fn(T) -> i16, + from_pcm: impl Fn(i16) -> T, + ) -> Result<(), Error> { + self.process_into(audio.len(), events, |processor, index| { + let output = processor.next(to_pcm(audio[index]), OutputLayout::Mono); + audio[index] = from_pcm(output[0]); + }) + } + /// Checks the block, applies all controls at each offset and renders each /// frame. /// @@ -1027,6 +1030,39 @@ impl Processor { Ok(()) } + /// Renders tail frames into one slice and writes zeros in the unused + /// frames. + fn drain_slice( + &mut self, + output: &mut [T], + layout: OutputLayout, + from_frame: impl Fn([i16; 2]) -> T, + ) -> Result { + let count = self.drain_into(output.len(), layout, |index, frame| { + output[index] = from_frame(frame); + })?; + output[count..].fill(T::default()); + Ok(count) + } + + /// Checks the channel lengths, renders stereo tail frames and writes zeros + /// in the unused samples. + fn drain_planar_slices( + &mut self, + left: &mut [T], + right: &mut [T], + from_pcm: impl Fn(i16) -> T, + ) -> Result { + Error::check_length(left.len(), right.len())?; + let count = self.drain_into(left.len(), OutputLayout::Stereo, |index, frame| { + left[index] = from_pcm(frame[0]); + right[index] = from_pcm(frame[1]); + })?; + left[count..].fill(T::default()); + right[count..].fill(T::default()); + Ok(count) + } + /// Renders only the budget that is left. The caller writes zeros in the /// unused frames. /// @@ -1318,8 +1354,7 @@ impl Telemetry { /// If `voice_count` is more than 16, this method limits the slice length /// to 16. pub fn active_voice_ratios(&self) -> &[f32] { - let count = usize::from(self.voice_count).min(self.voice_ratios.len()); - &self.voice_ratios[..count] + self.active_voice_slice(&self.voice_ratios) } /// Returns the linear gains of the active voices, from `0` to @@ -1328,7 +1363,12 @@ impl Telemetry { /// If `voice_count` is more than 16, this method limits the slice length /// to 16. pub fn active_voice_gains(&self) -> &[f32] { - let count = usize::from(self.voice_count).min(self.voice_gains.len()); - &self.voice_gains[..count] + self.active_voice_slice(&self.voice_gains) + } + + /// Limits a 16-voice array to [`voice_count`](Self::voice_count) entries. + fn active_voice_slice<'a>(&self, values: &'a [f32; 16]) -> &'a [f32] { + let count = usize::from(self.voice_count).min(values.len()); + &values[..count] } } diff --git a/src/resynthesis.rs b/src/resynthesis.rs index f0e844f..dd11998 100644 --- a/src/resynthesis.rs +++ b/src/resynthesis.rs @@ -1206,13 +1206,13 @@ impl Resynthesis { (self.correction.to_bits() as f64 / 16384.0) as f32 } /// Returns the selected correction pitch class in the octave that is - /// nearest to the reported input. + /// nearest to `estimate`. /// /// If correction is disabled, the active table is empty or no target is - /// selected, returns the fractional MIDI note of the input. If the input - /// is not reported as voiced, returns `None`. - pub(crate) fn target_note(&self) -> Option { - let midi = self.estimate().midi_note()?; + /// selected, returns the fractional MIDI note of `estimate`. If `estimate` + /// is not voiced, returns `None`. + pub(crate) fn target_note(&self, estimate: PitchEstimate) -> Option { + let midi = estimate.midi_note()?; if self.config.correction.enabled && self.scale_initialized && self.scale.count != 0 { let class = self.scale.notes[self.scale_index] as f32; Some(class + ((midi - class) / 12.0).round() * 12.0)