Skip to main content

rodio/source/
crossfade.rs

1use crate::source::{FadeIn, Mix, TakeDuration};
2use crate::Source;
3use std::time::Duration;
4
5/// Mixes one sound fading out with another sound fading in for the given
6/// duration.
7///
8/// Only the crossfaded portion (beginning of fadeout, beginning of fadein) is
9/// returned.
10pub fn crossfade<I1, I2>(
11    input_fadeout: I1,
12    input_fadein: I2,
13    duration: Duration,
14) -> Crossfade<I1, I2>
15where
16    I1: Source,
17    I2: Source,
18{
19    let mut input_fadeout = input_fadeout.take_duration(duration);
20    input_fadeout.set_filter_fadeout();
21    let input_fadein = input_fadein.take_duration(duration).fade_in(duration);
22    input_fadeout.mix(input_fadein)
23}
24
25/// Mixes one sound fading out with another sound fading in for the given
26/// duration.
27///
28/// Only the crossfaded portion (beginning of fadeout, beginning of fadein) is
29/// covered.
30pub type Crossfade<I1, I2> = Mix<TakeDuration<I1>, FadeIn<TakeDuration<I2>>>;
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35    use crate::buffer::SamplesBuffer;
36    use crate::math::nz;
37    use crate::source::Zero;
38    use crate::Sample;
39
40    fn dummy_source(length: u8) -> SamplesBuffer {
41        let data: Vec<Sample> = (1..=length).map(Sample::from).collect();
42        SamplesBuffer::new(nz!(1), nz!(1), data)
43    }
44
45    #[test]
46    fn test_crossfade_with_self() {
47        let source1 = dummy_source(10);
48        let source2 = dummy_source(10);
49        let mut mixed = crossfade(
50            source1,
51            source2,
52            Duration::from_secs(5) + Duration::from_nanos(1),
53        );
54
55        // Use approximate equality for floating-point comparisons
56        let eps = 1e-6;
57        assert!((mixed.next().unwrap() - 1.0).abs() < eps);
58        assert!((mixed.next().unwrap() - 2.0).abs() < eps);
59        assert!((mixed.next().unwrap() - 3.0).abs() < eps);
60        assert!((mixed.next().unwrap() - 4.0).abs() < eps);
61        assert!((mixed.next().unwrap() - 5.0).abs() < eps);
62        assert_eq!(mixed.next(), None);
63    }
64
65    #[test]
66    fn test_crossfade() {
67        let source1 = dummy_source(10);
68        let source2 = Zero::new(nz!(1), nz!(1));
69        let mixed = crossfade(
70            source1,
71            source2,
72            Duration::from_secs(5) + Duration::from_nanos(1),
73        );
74        let result = mixed.collect::<Vec<_>>();
75        assert_eq!(result.len(), 5);
76        assert!(result
77            .iter()
78            .zip(vec![1.0, 2.0 * 0.8, 3.0 * 0.6, 4.0 * 0.4, 5.0 * 0.2])
79            .all(|(a, b)| (a - b).abs() < 1e-6));
80    }
81}