audio_processor_time/chorus/
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.
23use audio_processor_traits::simple_processor::MonoAudioProcessor;
24use audio_processor_traits::{AudioBuffer, AudioContext, AudioProcessor};
25use augmented_oscillator::Oscillator;
26
27use crate::MonoDelayProcessor;
28
29pub struct ChorusProcessor {
30    mono_delay_processor: Vec<MonoDelayProcessor<f32>>,
31    oscillator: Oscillator<f32>,
32}
33
34impl Default for ChorusProcessor {
35    fn default() -> Self {
36        Self {
37            mono_delay_processor: vec![],
38            oscillator: Oscillator::sine(44100.0),
39        }
40    }
41}
42
43impl AudioProcessor for ChorusProcessor {
44    type SampleType = f32;
45
46    fn prepare(&mut self, context: &mut AudioContext) {
47        self.mono_delay_processor.resize_with(
48            context.settings.output_channels(),
49            MonoDelayProcessor::default,
50        );
51        for processor in &mut self.mono_delay_processor {
52            processor.m_prepare(context);
53            processor.handle().set_feedback(0.0);
54            processor.handle().set_delay_time_secs(0.01);
55        }
56
57        self.oscillator
58            .set_sample_rate(context.settings.sample_rate());
59        self.oscillator.set_frequency(3.0);
60    }
61
62    fn process(&mut self, context: &mut AudioContext, data: &mut AudioBuffer<Self::SampleType>) {
63        for frame_num in 0..data.num_samples() {
64            let time = self.oscillator.next_sample();
65
66            for (channel_num, delay) in self.mono_delay_processor.iter_mut().enumerate() {
67                let sample = &mut data.channels_mut()[channel_num][frame_num];
68                delay.handle().set_delay_time_secs(0.02 + time * 0.001);
69                *sample = *sample + 0.4 * delay.m_process(context, *sample)
70            }
71        }
72    }
73}