mirror of
https://github.com/RustAudio/rodio
synced 2025-03-04 15:07:18 +00:00
Added a fadeout source
This commit is contained in:
parent
f2f93e6947
commit
52e7d4aaa8
3 changed files with 114 additions and 1 deletions
|
@ -30,7 +30,7 @@ fn main() {
|
|||
.skip_duration(Duration::from_secs_f32(1.0))
|
||||
.take_duration(Duration::from_secs_f32(1.0))
|
||||
.amplify(0.20)
|
||||
.linear_gain_ramp(Duration::from_secs_f32(1.0), 1.0, 0.0, true);
|
||||
.fade_out(Duration::from_secs_f32(1.0));
|
||||
|
||||
tx.append(note_body);
|
||||
tx.append(note_end);
|
||||
|
|
102
src/source/fadeout.rs
Normal file
102
src/source/fadeout.rs
Normal file
|
@ -0,0 +1,102 @@
|
|||
use std::time::Duration;
|
||||
|
||||
use crate::{Sample, Source};
|
||||
|
||||
use super::{linear_ramp::linear_gain_ramp, LinearGainRamp, SeekError};
|
||||
|
||||
/// Internal function that builds a `FadeOut` object.
|
||||
pub fn fadeout<I>(input: I, duration: Duration) -> FadeOut<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
FadeOut {
|
||||
input: linear_gain_ramp(input, duration, 1.0f32, 0.0f32, true),
|
||||
}
|
||||
}
|
||||
|
||||
/// Filter that modifies raises the volume from silence over a time period.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct FadeOut<I> {
|
||||
input: LinearGainRamp<I>,
|
||||
}
|
||||
|
||||
impl<I> FadeOut<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
/// Returns a reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner(&self) -> &I {
|
||||
self.input.inner()
|
||||
}
|
||||
|
||||
/// Returns a mutable reference to the inner source.
|
||||
#[inline]
|
||||
pub fn inner_mut(&mut self) -> &mut I {
|
||||
self.input.inner_mut()
|
||||
}
|
||||
|
||||
/// Returns the inner source.
|
||||
#[inline]
|
||||
pub fn into_inner(self) -> I {
|
||||
self.input.into_inner()
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> Iterator for FadeOut<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
type Item = I::Item;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<I::Item> {
|
||||
self.input.next()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.input.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
impl<I> ExactSizeIterator for FadeOut<I>
|
||||
where
|
||||
I: Source + ExactSizeIterator,
|
||||
I::Item: Sample,
|
||||
{
|
||||
}
|
||||
|
||||
impl<I> Source for FadeOut<I>
|
||||
where
|
||||
I: Source,
|
||||
I::Item: Sample,
|
||||
{
|
||||
#[inline]
|
||||
fn current_frame_len(&self) -> Option<usize> {
|
||||
self.inner().current_frame_len()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn channels(&self) -> u16 {
|
||||
self.inner().channels()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn sample_rate(&self) -> u32 {
|
||||
self.inner().sample_rate()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn total_duration(&self) -> Option<Duration> {
|
||||
self.inner().total_duration()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> {
|
||||
self.inner_mut().try_seek(pos)
|
||||
}
|
||||
}
|
|
@ -16,6 +16,7 @@ pub use self::done::Done;
|
|||
pub use self::empty::Empty;
|
||||
pub use self::empty_callback::EmptyCallback;
|
||||
pub use self::fadein::FadeIn;
|
||||
pub use self::fadeout::FadeOut;
|
||||
pub use self::from_factory::{from_factory, FromFactoryIter};
|
||||
pub use self::from_iter::{from_iter, FromIter};
|
||||
pub use self::linear_ramp::LinearGainRamp;
|
||||
|
@ -45,6 +46,7 @@ mod done;
|
|||
mod empty;
|
||||
mod empty_callback;
|
||||
mod fadein;
|
||||
mod fadeout;
|
||||
mod from_factory;
|
||||
mod from_iter;
|
||||
mod linear_ramp;
|
||||
|
@ -251,6 +253,15 @@ where
|
|||
fadein::fadein(self, duration)
|
||||
}
|
||||
|
||||
/// Fades out the sound.
|
||||
#[inline]
|
||||
fn fade_out(self, duration: Duration) -> FadeOut<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
fadeout::fadeout(self, duration)
|
||||
}
|
||||
|
||||
/// Applies a linear gain ramp to the sound.
|
||||
///
|
||||
/// If `clamp_end` is `true`, all samples subsequent to the end of the ramp
|
||||
|
|
Loading…
Add table
Reference in a new issue