audio_processor_utility/
stereo.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;
25
26/// An `AudioProcessor` which will use a "source channel" as the output for all channels.
27/// Does not perform bounds checking.
28pub struct MonoToStereoProcessor<SampleType> {
29    source_channel: usize,
30    phantom: PhantomData<SampleType>,
31}
32
33impl<SampleType> Default for MonoToStereoProcessor<SampleType> {
34    /// Use channel `0` as the source
35    fn default() -> Self {
36        MonoToStereoProcessor::new(0)
37    }
38}
39
40impl<SampleType> MonoToStereoProcessor<SampleType> {
41    /// Use channel `source_channel` as the source for all output channels
42    pub fn new(source_channel: usize) -> Self {
43        MonoToStereoProcessor {
44            source_channel,
45            phantom: Default::default(),
46        }
47    }
48
49    /// Set the `source_channel` to use as the source for output
50    pub fn set_source_channel(&mut self, source_channel: usize) {
51        self.source_channel = source_channel;
52    }
53
54    /// Get the `source_channel` to use as the source for output
55    pub fn source_channel(&self) -> usize {
56        self.source_channel
57    }
58}
59
60impl<SampleType> AudioProcessor for MonoToStereoProcessor<SampleType>
61where
62    SampleType: Float + Sync + Send,
63{
64    type SampleType = SampleType;
65
66    fn process(&mut self, _context: &mut AudioContext, buffer: &mut AudioBuffer<SampleType>) {
67        for sample_num in 0..buffer.num_samples() {
68            let source_sample = *buffer.get(self.source_channel, sample_num);
69
70            for channel_num in 0..buffer.num_channels() {
71                buffer.set(channel_num, sample_num, source_sample);
72            }
73        }
74    }
75}
76
77#[cfg(test)]
78mod test {
79    use audio_processor_testing_helpers::assert_f_eq;
80    use audio_processor_traits::AudioBuffer;
81
82    use super::*;
83
84    #[test]
85    fn test_mono_to_stereo_handles_mono_input() {
86        let mut mono = MonoToStereoProcessor::new(1);
87        let samples = [1., 0.1, 1., 0.1, 1., 0.1, 1., 0.1, 1., 0.1, 1., 0.1];
88        let mut input = AudioBuffer::from_interleaved(2, &samples);
89        let mut context = AudioContext::default();
90
91        mono.process(&mut context, &mut input);
92
93        for sample_index in 0..input.num_samples() {
94            for channel_index in 0..input.num_channels() {
95                let sample = *input.get(channel_index, sample_index);
96                assert_f_eq!(sample, 0.1);
97            }
98        }
99    }
100
101    #[test]
102    fn test_mono_to_stereo_can_have_the_source_changed() {
103        let mut mono = MonoToStereoProcessor::new(1);
104        mono.set_source_channel(0);
105        let samples = [1., 0.1, 1., 0.1, 1., 0.1, 1., 0.1, 1., 0.1, 1., 0.1];
106        let mut input = AudioBuffer::from_interleaved(2, &samples);
107        let mut context = AudioContext::default();
108
109        mono.process(&mut context, &mut input);
110
111        for sample_index in 0..input.num_samples() {
112            for channel_index in 0..input.num_channels() {
113                let sample = *input.get(channel_index, sample_index);
114                assert_f_eq!(sample, 1.);
115            }
116        }
117    }
118}