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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// Copyright (c) 2021-2022 Weird Constructor <weirdconstructor@gmail.com>
// This file is a part of synfx-dsp. Released under GPL-3.0-or-later.
// See README.md and COPYING for details.

// This file contains a reverb implementation that is based
// on Jon Dattorro's 1997 reverb algorithm. It's also largely
// based on the C++ implementation from ValleyAudio / ValleyRackFree
//
// ValleyRackFree Copyright (C) 2020, Valley Audio Soft, Dale Johnson
// Adapted under the GPL-3.0-or-later License.
//
// See also: https://github.com/ValleyAudio/ValleyRackFree/blob/v1.0/src/Plateau/Dattorro.cpp
//      and: https://github.com/ValleyAudio/ValleyRackFree/blob/v1.0/src/Plateau/Dattorro.hpp
//
// And: https://ccrma.stanford.edu/~dattorro/music.html
// And: https://ccrma.stanford.edu/~dattorro/EffectDesignPart1.pdf

//! Contains the implementation of the Dattorro plate reverb.

use crate::crossfade;
use crate::{AllPass, DCBlockFilter, DelayBuffer, OnePoleHPF, OnePoleLPF, TriSawLFO};

const DAT_SAMPLE_RATE: f64 = 29761.0;
const DAT_SAMPLES_PER_MS: f64 = DAT_SAMPLE_RATE / 1000.0;

const DAT_INPUT_APF_TIMES_MS: [f64; 4] = [
    141.0 / DAT_SAMPLES_PER_MS,
    107.0 / DAT_SAMPLES_PER_MS,
    379.0 / DAT_SAMPLES_PER_MS,
    277.0 / DAT_SAMPLES_PER_MS,
];

const DAT_LEFT_APF1_TIME_MS: f64 = 672.0 / DAT_SAMPLES_PER_MS;
const DAT_LEFT_APF2_TIME_MS: f64 = 1800.0 / DAT_SAMPLES_PER_MS;

const DAT_RIGHT_APF1_TIME_MS: f64 = 908.0 / DAT_SAMPLES_PER_MS;
const DAT_RIGHT_APF2_TIME_MS: f64 = 2656.0 / DAT_SAMPLES_PER_MS;

const DAT_LEFT_DELAY1_TIME_MS: f64 = 4453.0 / DAT_SAMPLES_PER_MS;
const DAT_LEFT_DELAY2_TIME_MS: f64 = 3720.0 / DAT_SAMPLES_PER_MS;

const DAT_RIGHT_DELAY1_TIME_MS: f64 = 4217.0 / DAT_SAMPLES_PER_MS;
const DAT_RIGHT_DELAY2_TIME_MS: f64 = 3163.0 / DAT_SAMPLES_PER_MS;

const DAT_LEFT_TAPS_TIME_MS: [f64; 7] = [
    266.0 / DAT_SAMPLES_PER_MS,
    2974.0 / DAT_SAMPLES_PER_MS,
    1913.0 / DAT_SAMPLES_PER_MS,
    1996.0 / DAT_SAMPLES_PER_MS,
    1990.0 / DAT_SAMPLES_PER_MS,
    187.0 / DAT_SAMPLES_PER_MS,
    1066.0 / DAT_SAMPLES_PER_MS,
];

const DAT_RIGHT_TAPS_TIME_MS: [f64; 7] = [
    353.0 / DAT_SAMPLES_PER_MS,
    3627.0 / DAT_SAMPLES_PER_MS,
    1228.0 / DAT_SAMPLES_PER_MS,
    2673.0 / DAT_SAMPLES_PER_MS,
    2111.0 / DAT_SAMPLES_PER_MS,
    335.0 / DAT_SAMPLES_PER_MS,
    121.0 / DAT_SAMPLES_PER_MS,
];

const DAT_LFO_FREQS_HZ: [f64; 4] = [0.1, 0.15, 0.12, 0.18];

const DAT_INPUT_DIFFUSION1: f64 = 0.75;
const DAT_INPUT_DIFFUSION2: f64 = 0.625;
const DAT_PLATE_DIFFUSION1: f64 = 0.7;
const DAT_PLATE_DIFFUSION2: f64 = 0.5;

const DAT_LFO_EXCURSION_MS: f64 = 16.0 / DAT_SAMPLES_PER_MS;
const DAT_LFO_EXCURSION_MOD_MAX: f64 = 16.0;

/// Dattorro plate reverb implementation.
#[derive(Debug, Clone)]
pub struct DattorroReverb {
    last_scale: f64,

    inp_dc_block: [DCBlockFilter<f64>; 2],
    out_dc_block: [DCBlockFilter<f64>; 2],

    lfos: [TriSawLFO<f64>; 4],

    input_hpf: OnePoleHPF<f64>,
    input_lpf: OnePoleLPF<f64>,

    pre_delay: DelayBuffer<f64>,
    input_apfs: [(AllPass<f64>, f64, f64); 4],

    apf1: [(AllPass<f64>, f64, f64); 2],
    hpf: [OnePoleHPF<f64>; 2],
    lpf: [OnePoleLPF<f64>; 2],
    apf2: [(AllPass<f64>, f64, f64); 2],
    delay1: [(DelayBuffer<f64>, f64); 2],
    delay2: [(DelayBuffer<f64>, f64); 2],

    left_sum: f64,
    right_sum: f64,

    dbg_count: usize,
}

pub trait DattorroReverbParams {
    /// Time for the pre-delay of the reverb. Any sensible `ms` that fits
    /// into a delay buffer of 5 seconds.
    fn pre_delay_time_ms(&self) -> f64;
    /// The size of the reverb, values go from 0.0 to 1.0.
    fn time_scale(&self) -> f64;
    /// High-pass input filter cutoff freq in Hz, range: 0.0 to 22000.0
    fn input_high_cutoff_hz(&self) -> f64;
    /// Low-pass input filter cutoff freq in Hz, range: 0.0 to 22000.0
    fn input_low_cutoff_hz(&self) -> f64;
    /// High-pass reverb filter cutoff freq in Hz, range: 0.0 to 22000.0
    fn reverb_high_cutoff_hz(&self) -> f64;
    /// Low-pass reverb filter cutoff freq in Hz, range: 0.0 to 22000.0
    fn reverb_low_cutoff_hz(&self) -> f64;
    /// Modulation speed factor, range: 0.0 to 1.0
    fn mod_speed(&self) -> f64;
    /// Modulation depth from the LFOs, range: 0.0 to 1.0
    fn mod_depth(&self) -> f64;
    /// Modulation shape (from saw to tri to saw), range: 0.0 to 1.0
    fn mod_shape(&self) -> f64;
    /// The mix between output from the pre-delay and the input diffusion.
    /// range: 0.0 to 1.0. Default should be 1.0
    fn input_diffusion_mix(&self) -> f64;
    /// The amount of plate diffusion going on, range: 0.0 to 1.0
    fn diffusion(&self) -> f64;
    /// Internal tank decay time, range: 0.0 to 1.0
    fn decay(&self) -> f64;
}

impl DattorroReverb {
    pub fn new() -> Self {
        let mut this = Self {
            last_scale: 1.0,

            inp_dc_block: [DCBlockFilter::new(); 2],
            out_dc_block: [DCBlockFilter::new(); 2],

            lfos: [TriSawLFO::new(); 4],

            input_hpf: OnePoleHPF::new(),
            input_lpf: OnePoleLPF::new(),

            pre_delay: DelayBuffer::new(),
            input_apfs: Default::default(),

            apf1: Default::default(),
            hpf: [OnePoleHPF::new(); 2],
            lpf: [OnePoleLPF::new(); 2],
            apf2: Default::default(),
            delay1: Default::default(),
            delay2: Default::default(),

            left_sum: 0.0,
            right_sum: 0.0,

            dbg_count: 0,
        };

        this.reset();

        this
    }

    pub fn reset(&mut self) {
        self.input_lpf.reset();
        self.input_hpf.reset();

        self.input_lpf.set_freq(22000.0);
        self.input_hpf.set_freq(0.0);

        self.input_apfs[0] = (AllPass::new(), DAT_INPUT_APF_TIMES_MS[0], DAT_INPUT_DIFFUSION1);
        self.input_apfs[1] = (AllPass::new(), DAT_INPUT_APF_TIMES_MS[1], DAT_INPUT_DIFFUSION1);
        self.input_apfs[2] = (AllPass::new(), DAT_INPUT_APF_TIMES_MS[2], DAT_INPUT_DIFFUSION2);
        self.input_apfs[3] = (AllPass::new(), DAT_INPUT_APF_TIMES_MS[3], DAT_INPUT_DIFFUSION2);

        self.apf1[0] = (AllPass::new(), DAT_LEFT_APF1_TIME_MS, -DAT_PLATE_DIFFUSION1);
        self.apf1[1] = (AllPass::new(), DAT_RIGHT_APF1_TIME_MS, -DAT_PLATE_DIFFUSION1);
        self.apf2[0] = (AllPass::new(), DAT_LEFT_APF2_TIME_MS, -DAT_PLATE_DIFFUSION2);
        self.apf2[1] = (AllPass::new(), DAT_RIGHT_APF2_TIME_MS, -DAT_PLATE_DIFFUSION2);

        self.delay1[0] = (DelayBuffer::new(), DAT_LEFT_DELAY1_TIME_MS);
        self.delay1[1] = (DelayBuffer::new(), DAT_RIGHT_DELAY1_TIME_MS);
        self.delay2[0] = (DelayBuffer::new(), DAT_LEFT_DELAY2_TIME_MS);
        self.delay2[1] = (DelayBuffer::new(), DAT_RIGHT_DELAY2_TIME_MS);

        self.lpf[0].reset();
        self.lpf[1].reset();
        self.lpf[0].set_freq(10000.0);
        self.lpf[1].set_freq(10000.0);

        self.hpf[0].reset();
        self.hpf[1].reset();
        self.hpf[0].set_freq(0.0);
        self.hpf[1].set_freq(0.0);

        self.lfos[0].set(DAT_LFO_FREQS_HZ[0], 0.5);
        self.lfos[0].set_phase_offs(0.0);
        self.lfos[0].reset();
        self.lfos[1].set(DAT_LFO_FREQS_HZ[1], 0.5);
        self.lfos[1].set_phase_offs(0.25);
        self.lfos[1].reset();
        self.lfos[2].set(DAT_LFO_FREQS_HZ[2], 0.5);
        self.lfos[2].set_phase_offs(0.5);
        self.lfos[2].reset();
        self.lfos[3].set(DAT_LFO_FREQS_HZ[3], 0.5);
        self.lfos[3].set_phase_offs(0.75);
        self.lfos[3].reset();

        self.inp_dc_block[0].reset();
        self.inp_dc_block[1].reset();
        self.out_dc_block[0].reset();
        self.out_dc_block[1].reset();

        self.pre_delay.reset();

        self.left_sum = 0.0;
        self.right_sum = 0.0;

        self.set_time_scale(1.0);
    }

    #[inline]
    pub fn set_time_scale(&mut self, scale: f64) {
        if (self.last_scale - scale).abs() > std::f64::EPSILON {
            let scale = scale.max(0.1);
            self.last_scale = scale;

            self.apf1[0].1 = DAT_LEFT_APF1_TIME_MS * scale;
            self.apf1[1].1 = DAT_RIGHT_APF1_TIME_MS * scale;
            self.apf2[0].1 = DAT_LEFT_APF2_TIME_MS * scale;
            self.apf2[1].1 = DAT_RIGHT_APF2_TIME_MS * scale;

            self.delay1[0].1 = DAT_LEFT_DELAY1_TIME_MS * scale;
            self.delay1[1].1 = DAT_RIGHT_DELAY1_TIME_MS * scale;
            self.delay2[0].1 = DAT_LEFT_DELAY2_TIME_MS * scale;
            self.delay2[1].1 = DAT_RIGHT_DELAY2_TIME_MS * scale;
        }
    }

    pub fn set_sample_rate(&mut self, srate: f64) {
        self.inp_dc_block[0].set_sample_rate(srate);
        self.inp_dc_block[1].set_sample_rate(srate);
        self.out_dc_block[0].set_sample_rate(srate);
        self.out_dc_block[1].set_sample_rate(srate);

        self.lfos[0].set_sample_rate(srate);
        self.lfos[1].set_sample_rate(srate);
        self.lfos[2].set_sample_rate(srate);
        self.lfos[3].set_sample_rate(srate);

        self.input_hpf.set_sample_rate(srate);
        self.input_lpf.set_sample_rate(srate);

        self.pre_delay.set_sample_rate(srate);

        self.input_apfs[0].0.set_sample_rate(srate);
        self.input_apfs[1].0.set_sample_rate(srate);
        self.input_apfs[2].0.set_sample_rate(srate);
        self.input_apfs[3].0.set_sample_rate(srate);

        self.apf1[0].0.set_sample_rate(srate);
        self.apf1[1].0.set_sample_rate(srate);
        self.apf2[0].0.set_sample_rate(srate);
        self.apf2[1].0.set_sample_rate(srate);

        self.hpf[0].set_sample_rate(srate);
        self.hpf[1].set_sample_rate(srate);
        self.lpf[0].set_sample_rate(srate);
        self.lpf[1].set_sample_rate(srate);

        self.delay1[0].0.set_sample_rate(srate);
        self.delay1[1].0.set_sample_rate(srate);
        self.delay2[0].0.set_sample_rate(srate);
        self.delay2[1].0.set_sample_rate(srate);
    }

    #[inline]
    fn calc_apf_delay_times(
        &mut self,
        params: &mut dyn DattorroReverbParams,
    ) -> (f64, f64, f64, f64) {
        let left_apf1_delay_ms = self.apf1[0].1
            + (self.lfos[0].next_bipolar() as f64
                * DAT_LFO_EXCURSION_MS
                * DAT_LFO_EXCURSION_MOD_MAX
                * params.mod_depth());
        let right_apf1_delay_ms = self.apf1[1].1
            + (self.lfos[1].next_bipolar() as f64
                * DAT_LFO_EXCURSION_MS
                * DAT_LFO_EXCURSION_MOD_MAX
                * params.mod_depth());
        let left_apf2_delay_ms = self.apf2[0].1
            + (self.lfos[2].next_bipolar() as f64
                * DAT_LFO_EXCURSION_MS
                * DAT_LFO_EXCURSION_MOD_MAX
                * params.mod_depth());
        let right_apf2_delay_ms = self.apf2[1].1
            + (self.lfos[3].next_bipolar() as f64
                * DAT_LFO_EXCURSION_MS
                * DAT_LFO_EXCURSION_MOD_MAX
                * params.mod_depth());

        (left_apf1_delay_ms, right_apf1_delay_ms, left_apf2_delay_ms, right_apf2_delay_ms)
    }

    pub fn process(
        &mut self,
        params: &mut dyn DattorroReverbParams,
        input_l: f64,
        input_r: f64,
    ) -> (f64, f64) {
        // Some parameter setup...
        let timescale = 0.1 + (4.0 - 0.1) * params.time_scale();
        self.set_time_scale(timescale);

        self.hpf[0].set_freq(params.reverb_high_cutoff_hz());
        self.hpf[1].set_freq(params.reverb_high_cutoff_hz());
        self.lpf[0].set_freq(params.reverb_low_cutoff_hz());
        self.lpf[1].set_freq(params.reverb_low_cutoff_hz());

        let mod_speed = params.mod_speed();
        let mod_speed = mod_speed * mod_speed;
        let mod_speed = mod_speed * 99.0 + 1.0;

        self.lfos[0].set(DAT_LFO_FREQS_HZ[0] * mod_speed, params.mod_shape());
        self.lfos[1].set(DAT_LFO_FREQS_HZ[1] * mod_speed, params.mod_shape());
        self.lfos[2].set(DAT_LFO_FREQS_HZ[2] * mod_speed, params.mod_shape());
        self.lfos[3].set(DAT_LFO_FREQS_HZ[3] * mod_speed, params.mod_shape());

        self.apf1[0].2 = -DAT_PLATE_DIFFUSION1 * params.diffusion();
        self.apf1[1].2 = -DAT_PLATE_DIFFUSION1 * params.diffusion();
        self.apf2[0].2 = DAT_PLATE_DIFFUSION2 * params.diffusion();
        self.apf2[1].2 = DAT_PLATE_DIFFUSION2 * params.diffusion();

        let (left_apf1_delay_ms, right_apf1_delay_ms, left_apf2_delay_ms, right_apf2_delay_ms) =
            self.calc_apf_delay_times(params);

        // Parameter setup done!

        // Input into their corresponding DC blockers
        let input_r = self.inp_dc_block[0].next(input_r);
        let input_l = self.inp_dc_block[1].next(input_l);

        // Sum of DC outputs => LPF => HPF
        self.input_lpf.set_freq(params.input_low_cutoff_hz());
        self.input_hpf.set_freq(params.input_high_cutoff_hz());
        let out_lpf = self.input_lpf.process(input_r + input_l);
        let out_hpf = self.input_hpf.process(out_lpf);

        // HPF => Pre-Delay
        let out_pre_delay = if params.pre_delay_time_ms() < 0.1 {
            out_hpf
        } else {
            self.pre_delay.next_cubic(params.pre_delay_time_ms(), out_hpf)
        };

        // Pre-Delay => 4 All-Pass filters
        let mut diffused = out_pre_delay;
        for (apf, time, g) in &mut self.input_apfs {
            diffused = apf.next(*time, *g, diffused);
        }

        // Mix between diffused and pre-delayed intput for further processing
        let tank_feed = crossfade(out_pre_delay, diffused, params.input_diffusion_mix());

        // First tap for the output
        self.left_sum += tank_feed;
        self.right_sum += tank_feed;

        // Calculate tank decay of the left/right signal channels.
        let decay = 1.0 - params.decay().clamp(0.1, 0.9999);
        let decay = 1.0 - (decay * decay);

        // Left Sum => APF1 => Delay1 => LPF => HPF => APF2 => Delay2
        // And then send this over to the right sum.
        let left = self.left_sum;
        let left = self.apf1[0].0.next(left_apf1_delay_ms, self.apf1[0].2, left);
        let left_apf_tap = left;
        let left = self.delay1[0].0.next_cubic(self.delay1[0].1, left);
        let left = self.lpf[0].process(left);
        let left = self.hpf[0].process(left);
        let left = left * decay;
        let left = self.apf2[0].0.next(left_apf2_delay_ms, self.apf2[0].2, left);
        let left = self.delay2[0].0.next_cubic(self.delay2[0].1, left);

        //        if self.dbg_count % 48 == 0 {
        //            println!("APFS dcy={:8.6}; {:8.6} {:8.6} {:8.6} {:8.6} | {:8.6} {:8.6} {:8.6} {:8.6}",
        //                decay,
        //                self.apf1[0].2,
        //                self.apf1[1].2,
        //                self.apf2[0].2,
        //                self.apf2[1].2,
        //                left_apf1_delay_ms, right_apf1_delay_ms,
        //                left_apf2_delay_ms, right_apf2_delay_ms);
        //            println!("DELY1/2 {:8.6} / {:8.6} | {:8.6} / {:8.6}",
        //                self.delay1[0].1,
        //                self.delay2[0].1,
        //                self.delay1[1].1,
        //                self.delay2[1].1);
        //        }

        // Right Sum => APF1 => Delay1 => LPF => HPF => APF2 => Delay2
        // And then send this over to the left sum.
        let right = self.right_sum;
        let right = self.apf1[1].0.next(right_apf1_delay_ms, self.apf1[1].2, right);
        let right_apf_tap = right;
        let right = self.delay1[1].0.next_cubic(self.delay1[1].1, right);
        let right = self.lpf[1].process(right);
        let right = self.hpf[1].process(right);
        let right = right * decay;
        let right = self.apf2[1].0.next(right_apf2_delay_ms, self.apf2[1].2, right);
        let right = self.delay2[1].0.next_cubic(self.delay2[1].1, right);

        self.right_sum = left * decay;
        self.left_sum = right * decay;

        let mut left_accum = left_apf_tap;
        left_accum += self.delay1[0].0.tap_n(DAT_LEFT_TAPS_TIME_MS[0]);
        left_accum += self.delay1[0].0.tap_n(DAT_LEFT_TAPS_TIME_MS[1]);
        left_accum -= self.apf2[0].0.delay_tap_n(DAT_LEFT_TAPS_TIME_MS[2]);
        left_accum += self.delay2[0].0.tap_n(DAT_LEFT_TAPS_TIME_MS[3]);
        left_accum -= self.delay1[1].0.tap_n(DAT_LEFT_TAPS_TIME_MS[4]);
        left_accum -= self.apf2[1].0.delay_tap_n(DAT_LEFT_TAPS_TIME_MS[5]);
        left_accum -= self.delay2[1].0.tap_n(DAT_LEFT_TAPS_TIME_MS[6]);

        let mut right_accum = right_apf_tap;
        right_accum += self.delay1[1].0.tap_n(DAT_RIGHT_TAPS_TIME_MS[0]);
        right_accum += self.delay1[1].0.tap_n(DAT_RIGHT_TAPS_TIME_MS[1]);
        right_accum -= self.apf2[1].0.delay_tap_n(DAT_RIGHT_TAPS_TIME_MS[2]);
        right_accum += self.delay2[1].0.tap_n(DAT_RIGHT_TAPS_TIME_MS[3]);
        right_accum -= self.delay1[0].0.tap_n(DAT_RIGHT_TAPS_TIME_MS[4]);
        right_accum -= self.apf2[0].0.delay_tap_n(DAT_RIGHT_TAPS_TIME_MS[5]);
        right_accum -= self.delay2[0].0.tap_n(DAT_RIGHT_TAPS_TIME_MS[6]);

        let left_out = self.out_dc_block[0].next(left_accum);
        let right_out = self.out_dc_block[1].next(right_accum);

        self.dbg_count += 1;

        (left_out * 0.5, right_out * 0.5)
    }
}