audio_processor_traits/noop_processors/
mod.rs

1// Augmented Audio: Audio libraries and applications
2// Copyright (c) 2022 Pedro Tacla Yamada
3//
4// The MIT License (MIT)
5//
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to deal
8// in the Software without restriction, including without limitation the rights
9// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10// copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12//
13// The above copyright notice and this permission notice shall be included in
14// all copies or substantial portions of the Software.
15//
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22// THE SOFTWARE.
23
24use std::marker::PhantomData;
25
26use num::Float;
27
28use crate::{AudioBuffer, AudioContext, AudioProcessor};
29
30/// An audio-processor which doesn't do any work.
31pub struct NoopAudioProcessor<SampleType>(PhantomData<SampleType>);
32
33impl<SampleType> Default for NoopAudioProcessor<SampleType> {
34    fn default() -> Self {
35        Self::new()
36    }
37}
38
39impl<SampleType> NoopAudioProcessor<SampleType> {
40    pub fn new() -> Self {
41        NoopAudioProcessor(PhantomData::default())
42    }
43}
44
45impl<SampleType: Send + Copy> AudioProcessor for NoopAudioProcessor<SampleType> {
46    type SampleType = SampleType;
47    fn process(&mut self, _context: &mut AudioContext, _frame: &mut AudioBuffer<Self::SampleType>) {
48    }
49}
50
51/// An audio-processor which mutes all channels.
52pub struct SilenceAudioProcessor<SampleType>(PhantomData<SampleType>);
53
54impl<SampleType> SilenceAudioProcessor<SampleType> {
55    pub fn new() -> Self {
56        SilenceAudioProcessor(PhantomData)
57    }
58}
59
60impl<SampleType> Default for SilenceAudioProcessor<SampleType> {
61    fn default() -> Self {
62        Self::new()
63    }
64}
65
66impl<SampleType: Float + Send + Sized> AudioProcessor for SilenceAudioProcessor<SampleType> {
67    type SampleType = SampleType;
68
69    fn process(&mut self, _context: &mut AudioContext, output: &mut AudioBuffer<SampleType>) {
70        for channel in output.channels_mut() {
71            for sample in channel {
72                *sample = SampleType::zero();
73            }
74        }
75    }
76}
77
78#[cfg(test)]
79mod test {
80    use crate::AudioProcessor;
81
82    use super::*;
83
84    #[test]
85    fn test_noop_processor() {
86        let mut output = NoopAudioProcessor::<f32>::default();
87        let mut ctx = AudioContext::default();
88        output.prepare(&mut ctx);
89
90        let buffer = [[1.0, 1.0, 1.0], [2.0, 2.0, 2.0]];
91        let mut buffer = AudioBuffer::from(buffer.iter().cloned());
92        output.process(&mut ctx, &mut buffer);
93        assert_eq!(
94            buffer.channels(),
95            &vec![vec![1.0, 1.0, 1.0], vec![2.0, 2.0, 2.0],]
96        );
97    }
98
99    #[test]
100    fn test_silence_processor() {
101        let mut output = SilenceAudioProcessor::<f32>::default();
102        let mut ctx = AudioContext::default();
103        output.prepare(&mut ctx);
104        let mut buffer = AudioBuffer::from([[1.0, 1.0, 1.0], [2.0, 2.0, 2.0]].iter().cloned());
105
106        output.process(&mut ctx, &mut buffer);
107        assert_eq!(
108            buffer.channels(),
109            &vec![vec![0.0, 0.0, 0.0], vec![0.0, 0.0, 0.0],]
110        );
111    }
112}