audio_processor_utility/
mono.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.
23use audio_processor_traits::{AudioBuffer, AudioContext, AudioProcessor, Float};
24use std::marker::PhantomData;
25use std::ops::AddAssign;
26
27/// An `AudioProcessor` which will sum all input channels into input 0.
28///
29/// If there are no channels it'll no-op. It'll not mute the remaining channels.
30pub struct StereoToMonoProcessor<SampleType> {
31    phantom: PhantomData<SampleType>,
32}
33
34impl<SampleType> Default for StereoToMonoProcessor<SampleType> {
35    fn default() -> Self {
36        Self::new()
37    }
38}
39
40impl<SampleType> StereoToMonoProcessor<SampleType> {
41    pub fn new() -> Self {
42        StereoToMonoProcessor {
43            phantom: PhantomData::default(),
44        }
45    }
46}
47
48impl<SampleType> AudioProcessor for StereoToMonoProcessor<SampleType>
49where
50    SampleType: Float + Sync + Send + AddAssign,
51{
52    type SampleType = SampleType;
53
54    fn process(&mut self, _context: &mut AudioContext, buffer: &mut AudioBuffer<SampleType>) {
55        if buffer.is_empty() {
56            return;
57        }
58
59        for sample_num in 0..buffer.num_samples() {
60            let mut sum: SampleType = SampleType::zero();
61
62            for channel_num in 0..buffer.num_channels() {
63                sum += *buffer.get(channel_num, sample_num);
64                buffer.set(channel_num, sample_num, SampleType::zero());
65            }
66
67            buffer.set(0, sample_num, sum);
68        }
69    }
70}
71
72#[cfg(test)]
73mod test {
74    use audio_processor_testing_helpers::assert_f_eq;
75    use audio_processor_traits::AudioBuffer;
76
77    use super::*;
78
79    #[test]
80    fn test_stereo_to_mono_processor_sums_channels() {
81        let mut mono = StereoToMonoProcessor::new();
82        let samples = [1., 0.1, 1., 0.1, 1., 0.1, 1., 0.1, 1., 0.1, 1., 0.1];
83        let mut input = AudioBuffer::from_interleaved(2, &samples);
84        let mut context = AudioContext::default();
85
86        mono.process(&mut context, &mut input);
87
88        for sample_index in 0..input.num_samples() {
89            let sample = *input.get(0, sample_index);
90            assert_f_eq!(sample, 1.1);
91        }
92    }
93
94    #[test]
95    fn test_stereo_to_mono_can_handle_mono_input() {
96        let mut mono = StereoToMonoProcessor::new();
97        let samples = [1., 1., 1., 1., 1., 1.];
98        let mut input = AudioBuffer::from_interleaved(1, &samples);
99        let mut context = AudioContext::default();
100
101        mono.process(&mut context, &mut input);
102
103        for sample_index in 0..input.num_samples() {
104            let sample = *input.get(0, sample_index);
105            assert_f_eq!(sample, 1.0);
106        }
107    }
108
109    #[test]
110    fn test_stereo_to_mono_can_handle_empty_input() {
111        let mut mono = StereoToMonoProcessor::new();
112        let samples: [f32; 0] = [];
113        let mut input = AudioBuffer::from_interleaved(1, &samples);
114        let mut context = AudioContext::default();
115
116        mono.process(&mut context, &mut input);
117    }
118}