audio_processor_utility/noise.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 std::marker::PhantomData;
24
25use rand::{Rng, SeedableRng};
26
27use audio_processor_traits::simple_processor::MonoAudioProcessor;
28use audio_processor_traits::{AudioContext, Float};
29
30/// White-noise generator, returns random numbers between -1 and 1 using [`rand::rngs::SmallRng`].
31///
32/// Float type for samples is generic, requires implementations of [`num::Float`] and
33/// [`rand::distributions::uniform::SampleUniform`]
34///
35/// # Example
36///
37/// Generating sample by sample see [`audio_processor_traits::SimpleAudioProcessor`]:
38///
39/// ```
40/// use audio_processor_traits::{AudioContext, AudioProcessorSettings};
41/// use audio_processor_traits::simple_processor::MonoAudioProcessor;
42/// use audio_processor_utility::noise::WhiteNoiseProcessor;
43///
44/// let mut context = AudioContext::from(AudioProcessorSettings::default());
45/// let mut processor = WhiteNoiseProcessor::<f32>::default();
46/// let noise1 = processor.m_process(&mut context, 0.0);
47/// let noise2 = processor.m_process(&mut context, 0.0);
48/// let noise3 = processor.m_process(&mut context, 0.0);
49/// ```
50///
51/// Generating buffer by buffer see [`audio_processor_traits::simple_processor::process_buffer`]:
52///
53/// ```
54/// use audio_processor_traits::simple_processor::process_buffer;
55/// use audio_processor_traits::{AudioContext, AudioProcessorSettings, AudioBuffer};
56/// use audio_processor_utility::noise::WhiteNoiseProcessor;
57///
58/// let mut context = AudioContext::from(AudioProcessorSettings::default());
59/// let mut buffer = AudioBuffer::empty();
60/// buffer.resize(2, 1000);
61/// let mut processor = WhiteNoiseProcessor::<f32>::default();
62/// process_buffer(&mut context, &mut processor, &mut buffer);
63/// ```
64///
65/// Using as a standalone processor see [`audio_processor_standalone`]:
66///
67/// ```
68/// use audio_processor_standalone::StandaloneAudioOnlyProcessor;
69/// use audio_processor_traits::simple_processor::MonoCopyProcessor;
70/// use audio_processor_utility::noise::WhiteNoiseProcessor;
71///
72/// let processor = MonoCopyProcessor::new(WhiteNoiseProcessor::<f32>::default());
73/// let _standalone = StandaloneAudioOnlyProcessor::new(processor, Default::default());
74/// // now call standalone_start
75/// ```
76pub struct WhiteNoiseProcessor<SampleType> {
77 rng: rand::rngs::SmallRng,
78 phantom: PhantomData<SampleType>,
79}
80
81impl<SampleType> Default for WhiteNoiseProcessor<SampleType> {
82 fn default() -> Self {
83 Self {
84 rng: rand::rngs::SmallRng::from_entropy(),
85 phantom: PhantomData::default(),
86 }
87 }
88}
89
90impl<SampleType: Float + rand::distributions::uniform::SampleUniform> MonoAudioProcessor
91 for WhiteNoiseProcessor<SampleType>
92{
93 type SampleType = SampleType;
94
95 fn m_process(
96 &mut self,
97 _context: &mut AudioContext,
98 _sample: Self::SampleType,
99 ) -> Self::SampleType {
100 self.rng.gen_range(-SampleType::one()..SampleType::one())
101 }
102}
103
104#[cfg(test)]
105mod test {
106 use super::*;
107
108 #[test]
109 fn test_no_alloc() {
110 let mut context = AudioContext::default();
111 let mut processor = WhiteNoiseProcessor::default();
112 assert_no_alloc::assert_no_alloc(|| {
113 for i in 0..10 {
114 processor.m_process(&mut context, i as f32);
115 }
116 })
117 }
118}