rodio/source/
spatial.rs

1use std::time::Duration;
2
3use super::SeekError;
4use crate::common::{ChannelCount, SampleRate};
5use crate::source::ChannelVolume;
6use crate::Source;
7
8/// A simple spatial audio source. The underlying source is transformed to Mono
9/// and then played in stereo. The left and right channel's volume are amplified
10/// differently depending on the distance of the left and right ear to the source.
11#[derive(Clone)]
12pub struct Spatial<I>
13where
14    I: Source,
15{
16    input: ChannelVolume<I>,
17}
18
19fn dist_sq(a: [f32; 3], b: [f32; 3]) -> f32 {
20    a.iter()
21        .zip(b.iter())
22        .map(|(a, b)| (a - b) * (a - b))
23        .sum::<f32>()
24}
25
26impl<I> Spatial<I>
27where
28    I: Source,
29{
30    /// Builds a new `SpatialSink`, beginning playback on a stream.
31    pub fn new(
32        input: I,
33        emitter_position: [f32; 3],
34        left_ear: [f32; 3],
35        right_ear: [f32; 3],
36    ) -> Spatial<I>
37    where
38        I: Source,
39    {
40        let mut ret = Spatial {
41            input: ChannelVolume::new(input, vec![0.0, 0.0]),
42        };
43        ret.set_positions(emitter_position, left_ear, right_ear);
44        ret
45    }
46
47    /// Sets the position of the emitter and ears in the 3D world.
48    pub fn set_positions(
49        &mut self,
50        emitter_pos: [f32; 3],
51        left_ear: [f32; 3],
52        right_ear: [f32; 3],
53    ) {
54        debug_assert!(left_ear != right_ear);
55        let left_dist_sq = dist_sq(left_ear, emitter_pos);
56        let right_dist_sq = dist_sq(right_ear, emitter_pos);
57        let max_diff = dist_sq(left_ear, right_ear).sqrt();
58        let left_dist = left_dist_sq.sqrt();
59        let right_dist = right_dist_sq.sqrt();
60        let left_diff_modifier = (((left_dist - right_dist) / max_diff + 1.0) / 4.0 + 0.5).min(1.0);
61        let right_diff_modifier =
62            (((right_dist - left_dist) / max_diff + 1.0) / 4.0 + 0.5).min(1.0);
63        let left_dist_modifier = (1.0 / left_dist_sq).min(1.0);
64        let right_dist_modifier = (1.0 / right_dist_sq).min(1.0);
65        self.input
66            .set_volume(0, left_diff_modifier * left_dist_modifier);
67        self.input
68            .set_volume(1, right_diff_modifier * right_dist_modifier);
69    }
70}
71
72impl<I> Iterator for Spatial<I>
73where
74    I: Source,
75{
76    type Item = I::Item;
77
78    #[inline]
79    fn next(&mut self) -> Option<I::Item> {
80        self.input.next()
81    }
82
83    #[inline]
84    fn size_hint(&self) -> (usize, Option<usize>) {
85        self.input.size_hint()
86    }
87}
88
89impl<I> ExactSizeIterator for Spatial<I> where I: Source + ExactSizeIterator {}
90
91impl<I> Source for Spatial<I>
92where
93    I: Source,
94{
95    #[inline]
96    fn current_span_len(&self) -> Option<usize> {
97        self.input.current_span_len()
98    }
99
100    #[inline]
101    fn channels(&self) -> ChannelCount {
102        self.input.channels()
103    }
104
105    #[inline]
106    fn sample_rate(&self) -> SampleRate {
107        self.input.sample_rate()
108    }
109
110    #[inline]
111    fn total_duration(&self) -> Option<Duration> {
112        self.input.total_duration()
113    }
114
115    #[inline]
116    fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> {
117        self.input.try_seek(pos)
118    }
119}