Added end gain clamping behavior.

This commit is contained in:
Jamie Hardt 2024-07-14 10:41:16 -07:00
parent 84db58ba27
commit 54cc52d44d

View file

@ -9,6 +9,7 @@ pub fn linear_gain_ramp<I>(
duration: Duration, duration: Duration,
start_gain: f32, start_gain: f32,
end_gain: f32, end_gain: f32,
clamp_end: bool
) -> LinearGainRamp<I> ) -> LinearGainRamp<I>
where where
I: Source, I: Source,
@ -22,6 +23,7 @@ where
total_ns: duration as f32, total_ns: duration as f32,
start_gain, start_gain,
end_gain, end_gain,
clamp_end
} }
} }
@ -33,6 +35,7 @@ pub struct LinearGainRamp<I> {
total_ns: f32, total_ns: f32,
start_gain: f32, start_gain: f32,
end_gain: f32, end_gain: f32,
clamp_end: bool,
} }
impl<I> LinearGainRamp<I> impl<I> LinearGainRamp<I>
@ -68,16 +71,22 @@ where
#[inline] #[inline]
fn next(&mut self) -> Option<I::Item> { fn next(&mut self) -> Option<I::Item> {
if self.remaining_ns <= 0.0 { let factor: f32;
return self.input.next();
}
let factor: f32 = f32::lerp( if self.remaining_ns <= 0.0 {
self.start_gain, if self.clamp_end {
self.end_gain, factor = self.end_gain;
self.remaining_ns as u32, } else {
self.total_ns as u32, factor = 1.0f32;
); }
} else {
factor = f32::lerp(
self.start_gain,
self.end_gain,
self.remaining_ns as u32,
self.total_ns as u32,
);
}
self.remaining_ns -= self.remaining_ns -=
1000000000.0 / (self.input.sample_rate() as f32 * self.channels() as f32); 1000000000.0 / (self.input.sample_rate() as f32 * self.channels() as f32);