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::source::Zero;
37
38    fn dummy_source(length: u8) -> SamplesBuffer {
39        let data: Vec<f32> = (1..=length).map(f32::from).collect();
40        SamplesBuffer::new(1, 1, data)
41    }
42
43    #[test]
44    fn test_crossfade_with_self() {
45        let source1 = dummy_source(10);
46        let source2 = dummy_source(10);
47        let mut mixed = crossfade(
48            source1,
49            source2,
50            Duration::from_secs(5) + Duration::from_nanos(1),
51        );
52        assert_eq!(mixed.next(), Some(1.0));
53        assert_eq!(mixed.next(), Some(2.0));
54        assert_eq!(mixed.next(), Some(3.0));
55        assert_eq!(mixed.next(), Some(4.0));
56        assert_eq!(mixed.next(), Some(5.0));
57        assert_eq!(mixed.next(), None);
58    }
59
60    #[test]
61    fn test_crossfade() {
62        let source1 = dummy_source(10);
63        let source2 = Zero::new(1, 1);
64        let mixed = crossfade(
65            source1,
66            source2,
67            Duration::from_secs(5) + Duration::from_nanos(1),
68        );
69        let result = mixed.collect::<Vec<_>>();
70        assert_eq!(result.len(), 5);
71        assert!(result
72            .iter()
73            .zip(vec![1.0, 2.0 * 0.8, 3.0 * 0.6, 4.0 * 0.4, 5.0 * 0.2])
74            .all(|(a, b)| (a - b).abs() < 1e-6));
75    }
76}