audio_processor_traits/
settings.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
24/// Options provided to the audio-processor before calling `process`.
25#[derive(Clone, PartialEq, Debug, Copy)]
26pub struct AudioProcessorSettings {
27    /// The sample rate of the signal
28    pub sample_rate: f32,
29    /// The number of input channels to the signal
30    pub input_channels: usize,
31    /// The number of output channels to the signal
32    pub output_channels: usize,
33    /// Buffer size of this processing loop
34    pub block_size: usize,
35}
36
37impl Default for AudioProcessorSettings {
38    fn default() -> Self {
39        Self::new(44100.0, 2, 2, 512)
40    }
41}
42
43impl AudioProcessorSettings {
44    /// Create audio processor settings with the given options
45    pub fn new(
46        sample_rate: f32,
47        input_channels: usize,
48        output_channels: usize,
49        block_size: usize,
50    ) -> Self {
51        AudioProcessorSettings {
52            sample_rate,
53            input_channels,
54            output_channels,
55            block_size,
56        }
57    }
58
59    /// The sample rate in samples/second as a floating point number
60    pub fn sample_rate(&self) -> f32 {
61        self.sample_rate
62    }
63
64    /// The number of input channels
65    pub fn input_channels(&self) -> usize {
66        self.input_channels
67    }
68
69    /// The number of output channels
70    pub fn output_channels(&self) -> usize {
71        self.output_channels
72    }
73
74    /// The number of samples which will be provided on each `process` call
75    pub fn block_size(&self) -> usize {
76        self.block_size
77    }
78
79    /// Set the sample rate of this settings object
80    pub fn set_sample_rate(&mut self, sample_rate: f32) {
81        self.sample_rate = sample_rate;
82    }
83
84    /// Set the number of input channels of this settings object
85    pub fn set_input_channels(&mut self, input_channels: usize) {
86        self.input_channels = input_channels;
87    }
88
89    /// Set the number of output channels of this settings object
90    pub fn set_output_channels(&mut self, output_channels: usize) {
91        self.output_channels = output_channels;
92    }
93
94    /// Set the buffer size of this settings object
95    pub fn set_block_size(&mut self, block_size: usize) {
96        self.block_size = block_size;
97    }
98}
99
100#[cfg(test)]
101mod test {
102    use super::*;
103
104    #[test]
105    fn test_create_settings() {
106        let settings = AudioProcessorSettings::default();
107        assert_eq!(settings.sample_rate(), 44100.0);
108    }
109
110    #[test]
111    fn test_new() {
112        let settings = AudioProcessorSettings::new(22050.0, 1, 2, 256);
113        assert_eq!(settings.sample_rate(), 22050.0);
114        assert_eq!(settings.input_channels(), 1);
115        assert_eq!(settings.output_channels(), 2);
116        assert_eq!(settings.block_size(), 256);
117    }
118
119    #[test]
120    fn test_set_sample_rate() {
121        let mut settings = AudioProcessorSettings::new(22050.0, 1, 2, 256);
122        settings.set_sample_rate(44100.0);
123        assert_eq!(settings.sample_rate(), 44100.0);
124    }
125
126    #[test]
127    fn test_set_input_channels() {
128        let mut settings = AudioProcessorSettings::new(22050.0, 1, 2, 256);
129        settings.set_input_channels(10);
130        assert_eq!(settings.input_channels(), 10);
131    }
132
133    #[test]
134    fn test_set_output_channels() {
135        let mut settings = AudioProcessorSettings::new(22050.0, 1, 2, 256);
136        settings.set_output_channels(10);
137        assert_eq!(settings.output_channels(), 10);
138    }
139
140    #[test]
141    fn test_set_block_size() {
142        let mut settings = AudioProcessorSettings::new(22050.0, 1, 2, 256);
143        settings.set_block_size(10);
144        assert_eq!(settings.block_size(), 10);
145    }
146}