mirror of
https://github.com/RustAudio/rodio
synced 2024-11-10 06:04:16 +00:00
Implementing fade-in as a wrapper
...around LinearRamp
This commit is contained in:
parent
9be1df5e55
commit
8864b22e04
1 changed files with 13 additions and 26 deletions
|
@ -2,7 +2,7 @@ use std::time::Duration;
|
|||
|
||||
use crate::{Sample, Source};
|
||||
|
||||
use super::SeekError;
|
||||
use super::{linear_ramp::linear_gain_ramp, LinearGainRamp, SeekError};
|
||||
|
||||
/// Internal function that builds a `FadeIn` object.
|
||||
pub fn fadein<I>(input: I, duration: Duration) -> FadeIn<I>
|
||||
|
@ -10,21 +10,15 @@ where
|
|||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
let duration = duration.as_secs() * 1000000000 + duration.subsec_nanos() as u64;
|
||||
|
||||
FadeIn {
|
||||
input,
|
||||
remaining_ns: duration as f32,
|
||||
total_ns: duration as f32,
|
||||
ramp: linear_gain_ramp(input, duration, 0.0f32, 1.0f32, false),
|
||||
}
|
||||
}
|
||||
|
||||
/// Filter that modifies raises the volume from silence over a time period.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct FadeIn<I> {
|
||||
input: I,
|
||||
remaining_ns: f32,
|
||||
total_ns: f32,
|
||||
ramp: LinearGainRamp<I>
|
||||
}
|
||||
|
||||
impl<I> FadeIn<I>
|
||||
|
@ -35,19 +29,19 @@ where
|
|||
/// Returns a reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner(&self) -> &I {
|
||||
&self.input
|
||||
self.ramp.inner()
|
||||
}
|
||||
|
||||
/// Returns a mutable reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner_mut(&mut self) -> &mut I {
|
||||
&mut self.input
|
||||
self.ramp.inner_mut()
|
||||
}
|
||||
|
||||
/// Returns the inner source.
|
||||
#[inline]
|
||||
pub fn into_inner(self) -> I {
|
||||
self.input
|
||||
self.ramp.into_inner()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -60,19 +54,12 @@ where
|
|||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<I::Item> {
|
||||
if self.remaining_ns <= 0.0 {
|
||||
return self.input.next();
|
||||
}
|
||||
|
||||
let factor = 1.0 - self.remaining_ns / self.total_ns;
|
||||
self.remaining_ns -=
|
||||
1000000000.0 / (self.input.sample_rate() as f32 * self.channels() as f32);
|
||||
self.input.next().map(|value| value.amplify(factor))
|
||||
self.inner_mut().next()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.input.size_hint()
|
||||
self.inner().size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -90,26 +77,26 @@ where
|
|||
{
|
||||
#[inline]
|
||||
fn current_frame_len(&self) -> Option<usize> {
|
||||
self.input.current_frame_len()
|
||||
self.inner().current_frame_len()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn channels(&self) -> u16 {
|
||||
self.input.channels()
|
||||
self.inner().channels()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn sample_rate(&self) -> u32 {
|
||||
self.input.sample_rate()
|
||||
self.inner().sample_rate()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn total_duration(&self) -> Option<Duration> {
|
||||
self.input.total_duration()
|
||||
self.inner().total_duration()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> {
|
||||
self.input.try_seek(pos)
|
||||
self.inner_mut().try_seek(pos)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue