1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use audio_garbage_collector::{make_shared, Shared};
use audio_processor_traits::parameters::{
make_handle_ref, AudioProcessorHandleProvider, AudioProcessorHandleRef,
};
use audio_processor_traits::{AtomicF32, AudioBuffer, AudioProcessor, AudioProcessorSettings};
pub use generic_handle::BitCrusherHandleRef;
mod generic_handle;
pub struct BitCrusherHandle {
sample_rate: AtomicF32,
bit_rate: AtomicF32,
}
impl BitCrusherHandle {
pub fn sample_rate(&self) -> f32 {
self.sample_rate.get()
}
pub fn bit_rate(&self) -> f32 {
self.bit_rate.get()
}
pub fn set_sample_rate(&self, sample_rate: f32) {
self.sample_rate.set(sample_rate);
}
pub fn set_bit_rate(&self, bit_rate: f32) {
self.bit_rate.set(bit_rate);
}
}
impl Default for BitCrusherHandle {
fn default() -> Self {
Self {
sample_rate: AtomicF32::new(44100.0),
bit_rate: AtomicF32::new(44100.0),
}
}
}
pub struct BitCrusherProcessor {
handle: Shared<BitCrusherHandle>,
}
impl AudioProcessorHandleProvider for BitCrusherProcessor {
fn generic_handle(&self) -> AudioProcessorHandleRef {
make_handle_ref(BitCrusherHandleRef::new(self.handle.clone()))
}
}
impl BitCrusherProcessor {
pub fn new(handle: Shared<BitCrusherHandle>) -> Self {
BitCrusherProcessor { handle }
}
pub fn handle(&self) -> &Shared<BitCrusherHandle> {
&self.handle
}
fn step_size(&self) -> usize {
(self.handle.sample_rate() / self.handle.bit_rate()) as usize
}
}
impl Default for BitCrusherProcessor {
fn default() -> Self {
Self::new(make_shared(BitCrusherHandle::default()))
}
}
impl AudioProcessor for BitCrusherProcessor {
type SampleType = f32;
fn prepare(&mut self, settings: AudioProcessorSettings) {
self.handle.set_sample_rate(settings.sample_rate());
if (self.handle.sample_rate() - self.handle.bit_rate()).abs() < f32::EPSILON {
self.handle.set_bit_rate(settings.sample_rate());
}
}
fn process<BufferType: AudioBuffer<SampleType = Self::SampleType>>(
&mut self,
data: &mut BufferType,
) {
let step_size = self.step_size();
let mut sample_index = 0;
let buffer_size = data.num_samples();
while sample_index < buffer_size {
let first_index = sample_index;
let limit_index = (sample_index + step_size).min(buffer_size);
while sample_index < limit_index {
for channel_index in 0..data.num_channels() {
let value = *data.get(channel_index, first_index);
data.set(channel_index, sample_index, value);
}
sample_index += 1;
}
}
}
}
#[cfg(test)]
mod test {
use std::time::Duration;
use audio_processor_testing_helpers::sine_buffer;
use audio_processor_traits::VecAudioBuffer;
use super::*;
#[test]
fn test_construct_bitcrusher() {
let _processor = BitCrusherProcessor::default();
}
#[test]
fn test_step_size_is_1_on_passthrough() {
let settings = AudioProcessorSettings::default();
let mut processor = BitCrusherProcessor::default();
processor.prepare(settings);
assert_eq!(processor.step_size(), 1);
}
#[test]
fn test_step_size_is_2_on_lower_bitrate() {
let settings = AudioProcessorSettings::default();
let mut processor = BitCrusherProcessor::default();
processor.prepare(settings);
processor
.handle()
.set_bit_rate(settings.sample_rate() / 2.0);
assert_eq!(processor.step_size(), 2);
}
#[test]
fn test_passthrough_bitcrusher() {
let settings = AudioProcessorSettings::default();
let mut processor = BitCrusherProcessor::default();
processor.prepare(settings);
let input_buffer = VecAudioBuffer::from(sine_buffer(
settings.sample_rate(),
440.0,
Duration::from_millis(10),
));
let mut output_buffer = input_buffer.clone();
processor.process(&mut output_buffer);
assert_eq!(input_buffer.slice(), output_buffer.slice());
}
}