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
99 changes: 2 additions & 97 deletions src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -337,19 +290,15 @@ 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<Self, Error> {
pub(crate) fn new(events: &'a [TimedEvent], frames: usize) -> Result<Self, Error> {
let mut previous = 0;
for (index, event) in events.iter().enumerate() {
let problem = if event.offset >= frames {
Some(EventProblem::OffsetOutsideBlock)
} 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 })
Expand Down Expand Up @@ -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(()),
}
}
2 changes: 1 addition & 1 deletion src/formant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 2 additions & 6 deletions src/harmony.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand All @@ -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)?;
Expand Down
2 changes: 1 addition & 1 deletion src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ impl Processor {
events: &'a [TimedEvent],
) -> Result<ValidatedEvents<'a>, Error> {
self.check_block(frames)?;
ValidatedEvents::new(events, frames, self.config)
ValidatedEvents::new(events, frames)
}

/// Enforces the prepared block limit for each call.
Expand Down
14 changes: 3 additions & 11 deletions src/resynthesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
///
Expand Down Expand Up @@ -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 {
Expand All @@ -1361,7 +1353,7 @@ impl Resynthesis {
}
}
if target <= Q6::ZERO {
self.reset_dwell();
self.dwell = Q14::ZERO;
return None;
}
self.scale_initialized = true;
Expand All @@ -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 {
Expand Down
27 changes: 7 additions & 20 deletions src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1044,7 +1032,6 @@ fn decode_events(
buffer: &mut Vec<TimedEvent>,
events: Ts<BlockEvents>,
frames: usize,
config: ProcessorConfig,
) -> Result<&[TimedEvent], JsError> {
let value = JsValue::from(events);
let Some(array) = value.dyn_ref::<Array>() else {
Expand All @@ -1061,7 +1048,7 @@ fn decode_events(
Ok(())
})?;
}
ValidatedEvents::new(buffer, frames, config)?;
ValidatedEvents::new(buffer, frames)?;
Ok(buffer)
}

Expand Down
Loading