rodio/source/
amplify.rs

1use std::time::Duration;
2
3use super::SeekError;
4use crate::{
5    common::{ChannelCount, SampleRate},
6    math, Source,
7};
8
9/// Internal function that builds a `Amplify` object.
10pub fn amplify<I>(input: I, factor: f32) -> Amplify<I>
11where
12    I: Source,
13{
14    Amplify { input, factor }
15}
16
17/// Filter that modifies each sample by a given value.
18#[derive(Clone, Debug)]
19pub struct Amplify<I> {
20    input: I,
21    factor: f32,
22}
23
24impl<I> Amplify<I> {
25    /// Modifies the amplification factor.
26    #[inline]
27    pub fn set_factor(&mut self, factor: f32) {
28        self.factor = factor;
29    }
30
31    /// Modifies the amplification factor logarithmically.
32    #[inline]
33    pub fn set_log_factor(&mut self, factor: f32) {
34        self.factor = math::db_to_linear(factor);
35    }
36
37    /// Returns a reference to the inner source.
38    #[inline]
39    pub fn inner(&self) -> &I {
40        &self.input
41    }
42
43    /// Returns a mutable reference to the inner source.
44    #[inline]
45    pub fn inner_mut(&mut self) -> &mut I {
46        &mut self.input
47    }
48
49    /// Returns the inner source.
50    #[inline]
51    pub fn into_inner(self) -> I {
52        self.input
53    }
54}
55
56impl<I> Iterator for Amplify<I>
57where
58    I: Source,
59{
60    type Item = I::Item;
61
62    #[inline]
63    fn next(&mut self) -> Option<Self::Item> {
64        self.input.next().map(|value| value * self.factor)
65    }
66
67    #[inline]
68    fn size_hint(&self) -> (usize, Option<usize>) {
69        self.input.size_hint()
70    }
71}
72
73impl<I> ExactSizeIterator for Amplify<I> where I: Source + ExactSizeIterator {}
74
75impl<I> Source for Amplify<I>
76where
77    I: Source,
78{
79    #[inline]
80    fn current_span_len(&self) -> Option<usize> {
81        self.input.current_span_len()
82    }
83
84    #[inline]
85    fn channels(&self) -> ChannelCount {
86        self.input.channels()
87    }
88
89    #[inline]
90    fn sample_rate(&self) -> SampleRate {
91        self.input.sample_rate()
92    }
93
94    #[inline]
95    fn total_duration(&self) -> Option<Duration> {
96        self.input.total_duration()
97    }
98
99    #[inline]
100    fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> {
101        self.input.try_seek(pos)
102    }
103}