audio_processor_traits/audio_buffer/util.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
24use crate::AudioBuffer;
25
26/// Set all samples of an AudioBuffer to a constant
27pub fn set_all<SampleType>(buf: &mut AudioBuffer<SampleType>, value: SampleType)
28where
29 SampleType: Clone,
30{
31 for sample in buf.slice_mut() {
32 *sample = value.clone();
33 }
34}
35
36/// Set all samples of an AudioBuffer to Zero::zero
37pub fn clear<SampleType>(buf: &mut AudioBuffer<SampleType>)
38where
39 SampleType: num::Zero + Copy,
40{
41 for sample in buf.slice_mut() {
42 *sample = SampleType::zero();
43 }
44}
45
46#[cfg(test)]
47mod test {
48 use super::*;
49
50 #[test]
51 fn test_set_all() {
52 let mut buffer = AudioBuffer::from_interleaved(1, &[1.0, 2.0, 3.0, 4.0]);
53 set_all(&mut buffer, 4.0);
54 assert_eq!(buffer.channel(0), &[4.0, 4.0, 4.0, 4.0]);
55 }
56
57 #[test]
58 fn test_clear() {
59 let mut buffer = AudioBuffer::from_interleaved(1, &[1.0, 2.0, 3.0, 4.0]);
60 clear(&mut buffer);
61 assert_eq!(buffer.channel(0), &[0.0, 0.0, 0.0, 0.0]);
62 }
63}