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
#[deny(missing_copy_implementations)]
#[deny(missing_docs)]
extern crate envelope_detector;
extern crate time_calc as time;
use envelope_detector::{EnvelopeDetector, Frame, Sample};
use std::marker::PhantomData;
use time::Ms;
pub mod detector;
pub mod even_gain_fn;
#[cfg(feature = "dsp-chain")]
pub mod dsp_node;
pub use detector::{Detector, PeakEnvelopeDetector, RmsEnvelopeDetector};
pub use even_gain_fn::{EvenGainFunction, Average, Minimum};
#[derive(Clone, Debug)]
pub struct Compressor<F, D, EGF> {
envelope_detector: D,
attack_ms: Ms,
release_ms: Ms,
pub threshold: f32,
slope: f32,
even_gain_fn: PhantomData<EGF>,
frame: PhantomData<F>,
}
pub type PeakCompressor<F, EGF> = Compressor<F, PeakEnvelopeDetector<F>, EGF>;
pub type PeakAvgCompressor<F> = PeakCompressor<F, Average>;
pub type PeakMinCompressor<F> = PeakCompressor<F, Minimum>;
pub type RmsCompressor<F, EGF> = Compressor<F, RmsEnvelopeDetector<F>, EGF>;
pub type RmsAvgCompressor<F> = RmsCompressor<F, Average>;
pub type RmsMinCompressor<F> = RmsCompressor<F, Minimum>;
fn calc_slope(ratio: f32) -> f32 {
1.0 - (1.0 / ratio)
}
impl<F, D, EGF> Compressor<F, D, EGF>
where F: Frame,
D: Detector<F>,
EGF: EvenGainFunction,
{
fn new(detector: D, attack_ms: Ms, release_ms: Ms, threshold: f32, ratio: f32) -> Self {
let slope = calc_slope(ratio);
Compressor {
envelope_detector: detector,
attack_ms: attack_ms,
release_ms: release_ms,
threshold: threshold,
slope: slope,
even_gain_fn: std::marker::PhantomData,
frame: std::marker::PhantomData,
}
}
pub fn set_attack_ms<M: Into<Ms>>(&mut self, ms: M, sample_hz: f64) {
let ms: Ms = ms.into();
self.attack_ms = ms;
self.update_attack_to_sample_hz(sample_hz);
}
pub fn set_release_ms<M: Into<Ms>>(&mut self, ms: M, sample_hz: f64) {
let ms: Ms = ms.into();
self.release_ms = ms;
self.update_release_to_sample_hz(sample_hz);
}
pub fn update_attack_to_sample_hz(&mut self, sample_hz: f64) {
let frames = self.attack_ms.samples(sample_hz) as f32;
self.envelope_detector.detector().set_attack_frames(frames);
}
pub fn update_release_to_sample_hz(&mut self, sample_hz: f64) {
let frames = self.release_ms.samples(sample_hz) as f32;
self.envelope_detector.detector().set_release_frames(frames);
}
pub fn next_gain_per_channel(&mut self, next_frame: F) -> F::Float {
let threshold = self.threshold.to_sample();
let slope = self.slope.to_sample();
let identity = <F::Sample as Sample>::identity();
let env_frame = self.envelope_detector.detector().next(next_frame).to_float_frame();
env_frame.map(|s| {
let s = if s > identity { identity } else { s };
if s > threshold { identity - (s - threshold) * slope } else { identity }
})
}
#[inline]
pub fn next_gain(&mut self, next_frame: F) -> <F::Sample as Sample>::Float {
EGF::next_gain(self, next_frame)
}
#[inline]
pub fn next_frame(&mut self, next_frame: F) -> F {
let gain = self.next_gain(next_frame);
next_frame.scale_amp(gain)
}
}
impl<F, EGF> PeakCompressor<F, EGF>
where F: Frame,
EGF: EvenGainFunction,
{
pub fn peak<A, R>(attack_ms: A,
release_ms: R,
sample_hz: f64,
threshold: f32,
ratio: f32) -> Self
where A: Into<Ms>,
R: Into<Ms>,
{
let attack_ms: Ms = attack_ms.into();
let release_ms: Ms = release_ms.into();
let attack_frames = attack_ms.samples(sample_hz) as f32;
let release_frames = release_ms.samples(sample_hz) as f32;
let envelope_detector = EnvelopeDetector::peak(attack_frames, release_frames);
Compressor::new(envelope_detector, attack_ms, release_ms, threshold, ratio)
}
}
impl<F> PeakAvgCompressor<F>
where F: Frame,
{
pub fn peak_avg<A, R>(attack_ms: A,
release_ms: R,
sample_hz: f64,
threshold: f32,
ratio: f32) -> Self
where A: Into<Ms>,
R: Into<Ms>,
{
Self::peak(attack_ms, release_ms, sample_hz, threshold, ratio)
}
}
impl<F> PeakMinCompressor<F>
where F: Frame,
{
pub fn peak_min<A, R>(attack_ms: A,
release_ms: R,
sample_hz: f64,
threshold: f32,
ratio: f32) -> Self
where A: Into<Ms>,
R: Into<Ms>,
{
Self::peak(attack_ms, release_ms, sample_hz, threshold, ratio)
}
}
impl<F, EGF> RmsCompressor<F, EGF>
where F: Frame,
EGF: EvenGainFunction,
{
pub fn rms<W, A, R>(window_ms: W,
attack_ms: A,
release_ms: R,
sample_hz: f64,
threshold: f32,
ratio: f32) -> Self
where W: Into<Ms>,
A: Into<Ms>,
R: Into<Ms>,
{
let window_ms: Ms = window_ms.into();
let attack_ms: Ms = attack_ms.into();
let release_ms: Ms = release_ms.into();
let window_frames = window_ms.samples(sample_hz) as usize;
let attack_frames = attack_ms.samples(sample_hz) as f32;
let release_frames = release_ms.samples(sample_hz) as f32;
let envelope_detector = EnvelopeDetector::rms(window_frames, attack_frames, release_frames);
let rms_envelope_detector = RmsEnvelopeDetector {
rms: envelope_detector,
window_ms: window_ms,
};
Compressor::new(rms_envelope_detector, attack_ms, release_ms, threshold, ratio)
}
pub fn set_window_ms<M: Into<Ms>>(&mut self, ms: M, sample_hz: f64) {
let ms: Ms = ms.into();
self.envelope_detector.window_ms = ms;
self.update_window_to_sample_hz(sample_hz);
}
pub fn update_window_to_sample_hz(&mut self, sample_hz: f64) {
let frames = self.envelope_detector.window_ms.samples(sample_hz) as usize;
self.envelope_detector.rms.set_window_frames(frames);
}
}
impl<F> RmsAvgCompressor<F>
where F: Frame,
{
pub fn rms_avg<W, A, R>(window_ms: W,
attack_ms: A,
release_ms: R,
sample_hz: f64,
threshold: f32,
ratio: f32) -> Self
where W: Into<Ms>,
A: Into<Ms>,
R: Into<Ms>,
{
Self::rms(window_ms, attack_ms, release_ms, sample_hz, threshold, ratio)
}
}
impl<F> RmsMinCompressor<F>
where F: Frame,
{
pub fn rms_min<W, A, R>(window_ms: W,
attack_ms: A,
release_ms: R,
sample_hz: f64,
threshold: f32,
ratio: f32) -> Self
where W: Into<Ms>,
A: Into<Ms>,
R: Into<Ms>,
{
Self::rms(window_ms, attack_ms, release_ms, sample_hz, threshold, ratio)
}
}