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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
use audio_garbage_collector::{make_shared, Shared};
use audio_processor_traits::{AudioBuffer, AudioProcessor, AudioProcessorSettings};
use augmented_audio_volume::db_to_amplitude;
use handle::CompressorHandle;
type FloatT = augmented_audio_volume::Float;
mod handle {
#[cfg(not(feature = "f64"))]
use audio_processor_traits::AtomicF32 as AtomicFloat;
#[cfg(feature = "f64")]
use audio_processor_traits::AtomicF64 as AtomicFloat;
use super::FloatT;
pub fn calculate_multiplier(sample_rate: FloatT, duration_ms: FloatT) -> FloatT {
let attack_secs = duration_ms * 0.001;
let attack_samples = sample_rate * attack_secs;
FloatT::exp2(-1.0 / attack_samples)
}
pub struct CompressorHandle {
make_up_gain_db: AtomicFloat,
knee_width_db: AtomicFloat,
threshold_db: AtomicFloat,
ratio: AtomicFloat,
attack_ms: AtomicFloat,
release_ms: AtomicFloat,
sample_rate: AtomicFloat,
}
impl Default for CompressorHandle {
fn default() -> Self {
Self {
make_up_gain_db: AtomicFloat::new(0.0),
knee_width_db: AtomicFloat::new(0.1),
threshold_db: AtomicFloat::new(-10.0),
ratio: AtomicFloat::new(2.0),
attack_ms: AtomicFloat::new(3.0),
release_ms: AtomicFloat::new(10.0),
sample_rate: AtomicFloat::new(44100.0),
}
}
}
impl CompressorHandle {
pub fn attack_mult(&self) -> FloatT {
calculate_multiplier(self.sample_rate.get(), self.attack_ms.get())
}
pub fn release_mult(&self) -> FloatT {
calculate_multiplier(self.sample_rate.get(), self.release_ms.get())
}
pub fn set_attack_ms(&self, value: FloatT) {
self.attack_ms.set(value);
}
pub fn set_make_up_gain(&self, value: FloatT) {
self.make_up_gain_db.set(value);
}
pub fn set_release_ms(&self, value: FloatT) {
self.release_ms.set(value);
}
pub fn set_sample_rate(&self, sample_rate: FloatT) {
self.sample_rate.set(sample_rate);
}
pub fn set_threshold(&self, threshold: FloatT) {
self.threshold_db.set(threshold)
}
pub fn set_knee_width(&self, width: FloatT) {
self.knee_width_db.set(width)
}
pub fn set_ratio(&self, ratio: FloatT) {
self.ratio.set(ratio)
}
pub fn ratio(&self) -> FloatT {
self.ratio.get()
}
pub fn make_up_gain(&self) -> FloatT {
self.make_up_gain_db.get()
}
pub fn threshold(&self) -> FloatT {
self.threshold_db.get()
}
pub fn knee_width(&self) -> FloatT {
self.knee_width_db.get()
}
}
}
pub struct CompressorProcessor {
peak_detector_state: PeakDetector,
handle: Shared<CompressorHandle>,
}
impl Default for CompressorProcessor {
fn default() -> Self {
Self::new()
}
}
impl CompressorProcessor {
pub fn new() -> Self {
Self {
peak_detector_state: PeakDetector::default(),
handle: make_shared(CompressorHandle::default()),
}
}
pub fn handle(&self) -> &Shared<CompressorHandle> {
&self.handle
}
}
impl AudioProcessor for CompressorProcessor {
type SampleType = FloatT;
fn prepare(&mut self, settings: AudioProcessorSettings) {
self.handle.set_sample_rate(settings.sample_rate());
}
fn process<BufferType: AudioBuffer<SampleType = Self::SampleType>>(
&mut self,
data: &mut BufferType,
) {
for frame in data.frames_mut() {
self.peak_detector_state.accept_frame(
self.handle.attack_mult(),
self.handle.release_mult(),
frame,
);
self.apply_gain(frame);
}
}
}
impl CompressorProcessor {
fn apply_gain(&mut self, frame: &mut [FloatT]) {
let gain = self.compute_gain();
for sample in frame {
*sample *= gain;
}
}
fn compute_gain(&self) -> FloatT {
let level = self.peak_detector_state.value;
let ratio = self.handle.ratio();
let make_up_gain = db_to_amplitude(self.handle.make_up_gain(), 1.0);
let threshold = db_to_amplitude(self.handle.threshold(), 1.0);
let width = db_to_amplitude(self.handle.knee_width(), 1.0);
let delta = level - threshold;
let output = if (2.0 * delta) < -width {
1.0
} else if (2.0 * delta.abs()) <= width {
1.0 + (1.0 / ratio - 1.0) * (delta + width / 2.0).powf(2.0) / 2.0 * width
} else {
1.0 + delta * (1.0 / ratio - 1.0)
};
make_up_gain + output
}
}
struct PeakDetector {
value: FloatT,
}
impl Default for PeakDetector {
fn default() -> Self {
Self { value: 0.0 }
}
}
impl PeakDetector {
fn accept_frame(&mut self, attack_mult: FloatT, release_mult: FloatT, frame: &[FloatT]) {
let frame_len = frame.len() as FloatT;
let new: FloatT = frame.iter().map(|f| FloatT::abs(*f)).sum::<FloatT>() / frame_len;
let curr_slope = if self.value > new {
release_mult
} else {
attack_mult
};
self.value = (self.value * curr_slope) + ((1.0 - curr_slope) * new);
}
}
#[cfg(test)]
mod test {
use audio_processor_testing_helpers::charts::{
draw_multi_vec_charts, draw_vec_chart, BLUE, RED,
};
use audio_processor_testing_helpers::relative_path;
use audio_processor_file::AudioFileProcessor;
use audio_processor_traits::{
audio_buffer, InterleavedAudioBuffer, OwnedAudioBuffer, VecAudioBuffer,
};
use augmented_audio_volume::amplitude_to_db;
use super::*;
#[test]
fn test_peak_detector() {
let mut peak = PeakDetector::default();
peak.accept_frame(0.01, 0.02, &[1.0, 1.0]);
assert!(peak.value > 0.0);
}
#[test]
fn test_create_compressor() {
let _ = CompressorProcessor::new();
}
#[test]
fn test_knee_widths() {
let amp = db_to_amplitude(0.1, 1.0);
assert!(amp > 0.0);
assert!(amp < 2.0);
}
#[test]
fn test_peak_detector_output() {
let output_path = relative_path!("src/peak-detector");
let settings = AudioProcessorSettings::default();
let mut input = setup_input_processor(settings);
let mut processor = PeakDetector::default();
let attack_multi = handle::calculate_multiplier(settings.sample_rate(), 1.0);
let release_mult = handle::calculate_multiplier(settings.sample_rate(), 5.0);
let mut input_vec = vec![];
let mut output_vec = vec![];
{
let mut buffer = VecAudioBuffer::new();
buffer.resize(2, settings.block_size(), 0.0);
let num_chunks = (input.num_samples() / 8) / settings.block_size();
for _chunk in 0..num_chunks {
audio_buffer::clear(&mut buffer);
input.process(&mut buffer);
for frame in buffer.frames() {
input_vec.push(average(frame));
processor.accept_frame(attack_multi, release_mult, frame);
output_vec.push(processor.value * 2.0);
}
}
}
draw_multi_vec_charts(
&output_path,
"Peak Detector",
vec![(RED, input_vec), (BLUE, output_vec)],
);
}
#[test]
fn test_compress_synth_loop() {
let output_path = relative_path!("src/compressor");
let settings = AudioProcessorSettings::default();
let mut input = setup_input_processor(settings);
let mut processor = CompressorProcessor::new();
processor.prepare(settings);
processor.handle.set_ratio(30.0);
processor.handle.set_threshold(-10.0);
processor.handle.set_attack_ms(1.0);
processor.handle.set_release_ms(5.0);
processor.handle.set_knee_width(-1.0);
processor
.handle
.set_make_up_gain(amplitude_to_db(0.25, 1.0));
let mut input_vec = vec![];
let mut output_vec = vec![];
let mut gain_vec = vec![];
{
let mut buffer = VecAudioBuffer::new();
buffer.resize(1, settings.block_size(), 0.0);
let num_chunks = (input.num_samples() / 8) / settings.block_size();
for _chunk in 0..num_chunks {
audio_buffer::clear(&mut buffer);
input.process(&mut buffer);
for frame in buffer.frames() {
input_vec.push(average(frame))
}
for sample in buffer.slice_mut() {
let mut buf = [*sample];
let mut one_sample = InterleavedAudioBuffer::new(1, &mut buf);
processor.process(&mut one_sample);
*sample = buf[0];
output_vec.push(*sample);
gain_vec.push(processor.compute_gain());
}
}
}
draw_vec_chart(&output_path, "Input", input_vec);
draw_vec_chart(&output_path, "Gain", gain_vec);
draw_vec_chart(&output_path, "Output", output_vec);
}
fn average(frame: &[FloatT]) -> FloatT {
let num_samples = frame.len() as FloatT;
frame.iter().copied().sum::<FloatT>() / num_samples
}
fn setup_input_processor(settings: AudioProcessorSettings) -> AudioFileProcessor {
let input_file_path = relative_path!("../../../../input-files/C3-loop.mp3");
let mut input = AudioFileProcessor::from_path(
audio_garbage_collector::handle(),
settings,
&input_file_path,
)
.unwrap();
input.prepare(settings);
input
}
}