audio_processor_traits/noop_processors/
mod.rs1use std::marker::PhantomData;
25
26use num::Float;
27
28use crate::{AudioBuffer, AudioContext, AudioProcessor};
29
30pub 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
51pub 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}