Skip to content
Open
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
246 changes: 143 additions & 103 deletions src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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<usize, Error> {
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.
Expand All @@ -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<usize, Error> {
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.
Expand All @@ -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<usize, Error> {
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
Expand All @@ -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<usize, Error> {
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.
Expand All @@ -438,14 +411,7 @@ impl Processor {
left: &mut [f32],
right: &mut [f32],
) -> Result<usize, Error> {
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
Expand All @@ -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<usize, Error> {
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.
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -1005,6 +957,57 @@ impl Processor {
result
}

/// Checks the slice lengths and renders mono input into stereo frames.
fn process_stereo_slice<T: Copy>(
&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<T: Copy>(
&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<T: Copy>(
&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.
///
Expand All @@ -1027,6 +1030,39 @@ impl Processor {
Ok(())
}

/// Renders tail frames into one slice and writes zeros in the unused
/// frames.
fn drain_slice<T: Copy + Default>(
&mut self,
output: &mut [T],
layout: OutputLayout,
from_frame: impl Fn([i16; 2]) -> T,
) -> Result<usize, Error> {
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<T: Copy + Default>(
&mut self,
left: &mut [T],
right: &mut [T],
from_pcm: impl Fn(i16) -> T,
) -> Result<usize, Error> {
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.
///
Expand Down Expand Up @@ -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
Expand All @@ -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]
}
}
Loading
Loading