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