mirror of
https://github.com/RustAudio/rodio
synced 2025-03-04 06:57:16 +00:00
fix crossfade 2018 & cargo fmt
This commit is contained in:
parent
d7c77aa52e
commit
7ca3b1d635
3 changed files with 33 additions and 19 deletions
|
@ -9,7 +9,8 @@ fn main() {
|
|||
[-10.0, 0.0, 0.0],
|
||||
[1.0, 0.0, 0.0],
|
||||
[-1.0, 0.0, 0.0],
|
||||
).unwrap();
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let file = std::fs::File::open("examples/music.ogg").unwrap();
|
||||
let source = rodio::Decoder::new(BufReader::new(file)).unwrap();
|
||||
|
|
|
@ -1,15 +1,17 @@
|
|||
use std::time::Duration;
|
||||
|
||||
use Source;
|
||||
use Sample;
|
||||
use source::Mix;
|
||||
use source::TakeDuration;
|
||||
use source::FadeIn;
|
||||
use crate::source::{FadeIn, Mix, TakeDuration};
|
||||
use crate::Sample;
|
||||
use crate::Source;
|
||||
|
||||
/// Mixes one sound fading out with another sound fading in for the given duration.
|
||||
///
|
||||
/// Only the crossfaded portion (beginning of fadeout, beginning of fadein) is returned.
|
||||
pub fn crossfade<I1,I2>(input_fadeout: I1, input_fadein: I2, duration: Duration) -> Crossfade<I1,I2>
|
||||
pub fn crossfade<I1, I2>(
|
||||
input_fadeout: I1,
|
||||
input_fadein: I2,
|
||||
duration: Duration,
|
||||
) -> Crossfade<I1, I2>
|
||||
where
|
||||
I1: Source,
|
||||
I2: Source,
|
||||
|
@ -22,15 +24,14 @@ where
|
|||
input_fadeout.mix(input_fadein)
|
||||
}
|
||||
|
||||
pub type Crossfade<I1,I2> = Mix<TakeDuration<I1>,FadeIn<TakeDuration<I2>>>;
|
||||
|
||||
pub type Crossfade<I1, I2> = Mix<TakeDuration<I1>, FadeIn<TakeDuration<I2>>>;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use buffer::SamplesBuffer;
|
||||
use crate::buffer::SamplesBuffer;
|
||||
fn dummysource(length: u8) -> SamplesBuffer<f32> {
|
||||
let data: Vec<f32> = (1 ..= length).map(f32::from).collect();
|
||||
let data: Vec<f32> = (1..=length).map(f32::from).collect();
|
||||
let source = SamplesBuffer::new(1, 1, data);
|
||||
source
|
||||
}
|
||||
|
@ -39,7 +40,11 @@ mod tests {
|
|||
fn test_crossfade() {
|
||||
let source1 = dummysource(10);
|
||||
let source2 = dummysource(10);
|
||||
let mut mixed = crossfade(source1, source2, Duration::from_secs(5) + Duration::from_nanos(1));
|
||||
let mut mixed = crossfade(
|
||||
source1,
|
||||
source2,
|
||||
Duration::from_secs(5) + Duration::from_nanos(1),
|
||||
);
|
||||
assert_eq!(mixed.next(), Some(1.0));
|
||||
assert_eq!(mixed.next(), Some(2.0));
|
||||
assert_eq!(mixed.next(), Some(3.0));
|
||||
|
@ -49,7 +54,11 @@ mod tests {
|
|||
|
||||
let source1 = dummysource(10);
|
||||
let source2 = dummysource(10).amplify(0.0);
|
||||
let mut mixed = crossfade(source1, source2, Duration::from_secs(5) + Duration::from_nanos(1));
|
||||
let mut mixed = crossfade(
|
||||
source1,
|
||||
source2,
|
||||
Duration::from_secs(5) + Duration::from_nanos(1),
|
||||
);
|
||||
assert_eq!(mixed.next(), Some(1.0 * 1.0));
|
||||
assert_eq!(mixed.next(), Some(2.0 * 0.8));
|
||||
assert_eq!(mixed.next(), Some(3.0 * 0.6));
|
||||
|
|
|
@ -20,13 +20,16 @@ where
|
|||
}
|
||||
|
||||
/// A filter that can be applied to a `TakeDuration`.
|
||||
#[derive(Clone,Debug)]
|
||||
#[derive(Clone, Debug)]
|
||||
enum DurationFilter {
|
||||
FadeOut,
|
||||
}
|
||||
impl DurationFilter
|
||||
{
|
||||
fn apply<I: Iterator>(&self, sample: <I as Iterator>::Item, parent: &TakeDuration<I>) -> <I as Iterator>::Item
|
||||
impl DurationFilter {
|
||||
fn apply<I: Iterator>(
|
||||
&self,
|
||||
sample: <I as Iterator>::Item,
|
||||
parent: &TakeDuration<I>,
|
||||
) -> <I as Iterator>::Item
|
||||
where
|
||||
I::Item: Sample,
|
||||
{
|
||||
|
@ -36,7 +39,7 @@ impl DurationFilter
|
|||
let remaining = parent.remaining_duration.as_millis() as f32;
|
||||
let total = parent.requested_duration.as_millis() as f32;
|
||||
sample.amplify(remaining / total)
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -148,7 +151,8 @@ where
|
|||
+ self.duration_per_sample.subsec_nanos() as u64;
|
||||
let remaining_samples = (remaining_nanos / nanos_per_sample) as usize;
|
||||
|
||||
self.input.current_frame_len()
|
||||
self.input
|
||||
.current_frame_len()
|
||||
.filter(|value| *value < remaining_samples)
|
||||
.or(Some(remaining_samples))
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue