oximedia-effects 0.1.5

Professional audio effects suite for OxiMedia
Documentation
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
//! Basic delay effect with feedback and filtering.
//!
//! Includes analog-style feedback saturation modeling for warm, characterful
//! repeats. The saturation stage is applied to the feedback path only, so the
//! dry signal remains uncolored.
//!
//! ## Saturation modes
//!
//! | Mode | Character |
//! |------|-----------|
//! | `None` | Clean digital delay (no coloring) |
//! | `Tape` | Soft asymmetric tanh saturation (tape head squash) |
//! | `Tube` | Asymmetric triode-style warmth (even harmonics) |
//! | `Diode` | Hard-knee diode clipping (bright edge) |

use crate::{
    utils::{DelayLine, ParameterSmoother},
    AudioEffect,
};

// ── Saturation helpers ────────────────────────────────────────────────────────

/// Feedback saturation mode for analog delay emulation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FeedbackSaturationMode {
    /// No saturation (clean digital delay).
    None,
    /// Tape-style soft saturation using hyperbolic tangent shaping.
    Tape,
    /// Tube/triode-style even-harmonic saturation.
    Tube,
    /// Diode-clipping hard-knee distortion for aggressive character.
    Diode,
}

impl Default for FeedbackSaturationMode {
    fn default() -> Self {
        Self::None
    }
}

impl FeedbackSaturationMode {
    /// Apply the saturation nonlinearity to the feedback signal.
    ///
    /// `drive` controls the input level to the nonlinearity (1.0 = unity).
    /// Output is post-normalized to stay roughly within `[-1, 1]`.
    #[inline]
    pub fn apply(self, x: f32, drive: f32) -> f32 {
        match self {
            Self::None => x,
            Self::Tape => {
                // Soft tanh saturation — classic tape head squash.
                let driven = x * drive;
                driven.tanh()
            }
            Self::Tube => {
                // Asymmetric triode-style: boost positive half slightly.
                let driven = x * drive;
                let pos = (driven + 0.1).tanh();
                let neg = (driven - 0.1).tanh();
                // Even-harmonic mix: slightly more positive than negative.
                0.5 * (pos + neg) + 0.05 * (pos - neg)
            }
            Self::Diode => {
                // Hard-knee diode clipping: linear until knee, then hard clip.
                let driven = x * drive;
                let knee = 0.7_f32;
                if driven.abs() <= knee {
                    driven
                } else {
                    driven.signum() * (knee + (driven.abs() - knee).tanh() * (1.0 - knee))
                }
            }
        }
    }
}

// ── DelayConfig ───────────────────────────────────────────────────────────────

/// Configuration for delay effect.
#[derive(Debug, Clone)]
pub struct DelayConfig {
    /// Delay time in milliseconds.
    pub delay_ms: f32,
    /// Feedback amount (0.0 - 1.0).
    pub feedback: f32,
    /// Wet signal level (0.0 - 1.0).
    pub wet: f32,
    /// Dry signal level (0.0 - 1.0).
    pub dry: f32,
    /// Low-pass filter cutoff for feedback (0.0 = no filtering, 1.0 = maximum filtering).
    pub tone: f32,
    /// Feedback saturation mode (default: `None` = clean digital).
    pub saturation: FeedbackSaturationMode,
    /// Saturation drive level (1.0 = unity, higher = more harmonic content).
    pub saturation_drive: f32,
}

impl Default for DelayConfig {
    fn default() -> Self {
        Self {
            delay_ms: 500.0,
            feedback: 0.4,
            wet: 0.5,
            dry: 0.5,
            tone: 0.0,
            saturation: FeedbackSaturationMode::None,
            saturation_drive: 1.0,
        }
    }
}

impl DelayConfig {
    /// Create a new delay configuration.
    #[must_use]
    pub fn new(delay_ms: f32, feedback: f32, wet: f32) -> Self {
        Self {
            delay_ms: delay_ms.max(0.0),
            feedback: feedback.clamp(0.0, 0.99),
            wet: wet.clamp(0.0, 1.0),
            dry: (1.0 - wet).clamp(0.0, 1.0),
            tone: 0.0,
            saturation: FeedbackSaturationMode::None,
            saturation_drive: 1.0,
        }
    }

    /// Slapback delay preset (short, single echo).
    #[must_use]
    pub fn slapback() -> Self {
        Self::new(100.0, 0.0, 0.3)
    }

    /// Dotted eighth note delay preset (at 120 BPM).
    #[must_use]
    pub fn dotted_eighth() -> Self {
        Self::new(375.0, 0.35, 0.4)
    }

    /// Long ambient delay preset.
    #[must_use]
    pub fn ambient() -> Self {
        Self::new(750.0, 0.6, 0.5).with_tone(0.4)
    }

    /// Analog tape delay preset with tape saturation.
    #[must_use]
    pub fn tape() -> Self {
        Self {
            saturation: FeedbackSaturationMode::Tape,
            saturation_drive: 1.5,
            ..Self::new(380.0, 0.5, 0.45).with_tone(0.3)
        }
    }

    /// Vintage tube delay with triode character.
    #[must_use]
    pub fn tube() -> Self {
        Self {
            saturation: FeedbackSaturationMode::Tube,
            saturation_drive: 1.2,
            ..Self::new(420.0, 0.4, 0.4).with_tone(0.2)
        }
    }

    /// Set tone control.
    #[must_use]
    pub fn with_tone(mut self, tone: f32) -> Self {
        self.tone = tone.clamp(0.0, 1.0);
        self
    }

    /// Set saturation mode.
    #[must_use]
    pub fn with_saturation(mut self, mode: FeedbackSaturationMode, drive: f32) -> Self {
        self.saturation = mode;
        self.saturation_drive = drive.clamp(0.1, 10.0);
        self
    }
}

// ── MonoDelay ─────────────────────────────────────────────────────────────────

/// Simple mono delay effect.
///
/// The delay line is a pre-allocated circular buffer sized to accommodate
/// up to 2 seconds at the given sample rate — no allocations occur during
/// audio processing.
///
/// The feedback path optionally passes through a saturation stage before
/// being written back into the delay line, modeling the nonlinear character
/// of analog delay hardware (tape machines, bucket-brigade devices, tube circuits).
pub struct MonoDelay {
    /// Pre-allocated circular buffer (ring buffer).
    delay_line: DelayLine,
    delay_samples: usize,
    config: DelayConfig,
    /// One-pole low-pass state for tone/brightness control in the feedback path.
    tone_filter: f32,
    tone_smoother: ParameterSmoother,
    sample_rate: f32,
}

impl MonoDelay {
    /// Create a new mono delay.
    ///
    /// Allocates a circular ring buffer large enough for a 2-second delay at
    /// the given sample rate. No further allocations happen during processing.
    #[must_use]
    pub fn new(config: DelayConfig, sample_rate: f32) -> Self {
        #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
        let max_delay_samples = ((2000.0 * sample_rate) / 1000.0) as usize; // 2 second max

        #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
        let delay_samples = ((config.delay_ms * sample_rate) / 1000.0) as usize;

        Self {
            delay_line: DelayLine::new(max_delay_samples),
            delay_samples,
            config,
            tone_filter: 0.0,
            tone_smoother: ParameterSmoother::new(10.0, sample_rate),
            sample_rate,
        }
    }

    /// Set delay time in milliseconds.
    pub fn set_delay_ms(&mut self, delay_ms: f32) {
        self.config.delay_ms = delay_ms.max(0.0);
        #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
        let delay_samp = ((delay_ms * self.sample_rate) / 1000.0) as usize;
        self.delay_samples = delay_samp.min(self.delay_line.max_delay());
    }

    /// Set feedback amount.
    pub fn set_feedback(&mut self, feedback: f32) {
        self.config.feedback = feedback.clamp(0.0, 0.99);
    }

    /// Set wet level.
    pub fn set_wet(&mut self, wet: f32) {
        self.config.wet = wet.clamp(0.0, 1.0);
    }

    /// Set dry level.
    pub fn set_dry(&mut self, dry: f32) {
        self.config.dry = dry.clamp(0.0, 1.0);
    }

    /// Set tone (low-pass filter for feedback).
    pub fn set_tone(&mut self, tone: f32) {
        self.config.tone = tone.clamp(0.0, 1.0);
        self.tone_smoother.set_target(self.config.tone);
    }

    /// Set the feedback saturation mode.
    pub fn set_saturation(&mut self, mode: FeedbackSaturationMode, drive: f32) {
        self.config.saturation = mode;
        self.config.saturation_drive = drive.clamp(0.1, 10.0);
    }
}

impl AudioEffect for MonoDelay {
    fn process_sample(&mut self, input: f32) -> f32 {
        // Read delayed sample.
        let delayed = self.delay_line.read(self.delay_samples);

        // Apply one-pole low-pass tone control to the feedback path.
        let tone = self.tone_smoother.next();
        self.tone_filter = delayed * (1.0 - tone) + self.tone_filter * tone;

        // Apply feedback saturation nonlinearity before writing back.
        let saturated = self
            .config
            .saturation
            .apply(self.tone_filter, self.config.saturation_drive);

        // Write input + saturated feedback to the delay line.
        let feedback_signal = saturated * self.config.feedback;
        self.delay_line.write(input + feedback_signal);

        // Mix wet and dry.
        delayed * self.config.wet + input * self.config.dry
    }

    fn reset(&mut self) {
        self.delay_line.clear();
        self.tone_filter = 0.0;
        self.tone_smoother.reset(0.0);
    }

    fn latency_samples(&self) -> usize {
        0 // Zero latency (delay is part of the effect)
    }

    fn set_wet_dry(&mut self, wet: f32) {
        self.config.wet = wet.clamp(0.0, 1.0);
        self.config.dry = 1.0 - self.config.wet;
    }

    fn wet_dry(&self) -> f32 {
        self.config.wet
    }
}

// ── StereoDelay ───────────────────────────────────────────────────────────────

/// Stereo delay effect.
pub struct StereoDelay {
    left: MonoDelay,
    right: MonoDelay,
    cross_feedback: f32,
}

impl StereoDelay {
    /// Create a new stereo delay.
    #[must_use]
    pub fn new(config: DelayConfig, sample_rate: f32) -> Self {
        Self {
            left: MonoDelay::new(config.clone(), sample_rate),
            right: MonoDelay::new(config, sample_rate),
            cross_feedback: 0.0,
        }
    }

    /// Create with different delay times for left and right.
    #[must_use]
    pub fn new_dual(left_config: DelayConfig, right_config: DelayConfig, sample_rate: f32) -> Self {
        Self {
            left: MonoDelay::new(left_config, sample_rate),
            right: MonoDelay::new(right_config, sample_rate),
            cross_feedback: 0.0,
        }
    }

    /// Set cross-feedback amount (feedback from left to right and vice versa).
    pub fn set_cross_feedback(&mut self, amount: f32) {
        self.cross_feedback = amount.clamp(0.0, 0.99);
    }

    /// Set delay time for both channels.
    pub fn set_delay_ms(&mut self, delay_ms: f32) {
        self.left.set_delay_ms(delay_ms);
        self.right.set_delay_ms(delay_ms);
    }

    /// Set delay time for left channel.
    pub fn set_left_delay_ms(&mut self, delay_ms: f32) {
        self.left.set_delay_ms(delay_ms);
    }

    /// Set delay time for right channel.
    pub fn set_right_delay_ms(&mut self, delay_ms: f32) {
        self.right.set_delay_ms(delay_ms);
    }

    fn process_sample_internal(&mut self, input_l: f32, input_r: f32) -> (f32, f32) {
        let out_l = self.left.process_sample(input_l);
        let out_r = self.right.process_sample(input_r);

        // Apply cross-feedback if enabled.
        if self.cross_feedback > 0.0 {
            let cross_l = out_r * self.cross_feedback;
            let cross_r = out_l * self.cross_feedback;
            (out_l + cross_l, out_r + cross_r)
        } else {
            (out_l, out_r)
        }
    }
}

impl AudioEffect for StereoDelay {
    fn process_sample(&mut self, input: f32) -> f32 {
        let (left, _right) = self.process_sample_internal(input, input);
        left
    }

    fn process_sample_stereo(&mut self, left: f32, right: f32) -> (f32, f32) {
        self.process_sample_internal(left, right)
    }

    fn reset(&mut self) {
        self.left.reset();
        self.right.reset();
    }

    fn set_wet_dry(&mut self, wet: f32) {
        self.left.set_wet_dry(wet);
        self.right.set_wet_dry(wet);
    }

    fn wet_dry(&self) -> f32 {
        self.left.wet_dry()
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_delay_config() {
        let config = DelayConfig::default();
        assert_eq!(config.delay_ms, 500.0);
        assert_eq!(config.feedback, 0.4);
    }

    #[test]
    fn test_delay_presets() {
        let slapback = DelayConfig::slapback();
        assert!(slapback.delay_ms < 200.0);

        let ambient = DelayConfig::ambient();
        assert!(ambient.delay_ms > 500.0);
    }

    #[test]
    fn test_tape_preset() {
        let tape = DelayConfig::tape();
        assert_eq!(tape.saturation, FeedbackSaturationMode::Tape);
        assert!(tape.saturation_drive > 1.0);
    }

    #[test]
    fn test_tube_preset() {
        let tube = DelayConfig::tube();
        assert_eq!(tube.saturation, FeedbackSaturationMode::Tube);
    }

    #[test]
    fn test_mono_delay_clean() {
        let config = DelayConfig::new(100.0, 0.5, 0.5);
        let mut delay = MonoDelay::new(config, 48000.0);

        // Process impulse
        let out1 = delay.process_sample(1.0);
        assert!((out1 - 0.5).abs() < 0.01); // Should be mostly dry initially

        // Process silence — should get delayed echo after 100 ms.
        for _ in 0..4799 {
            delay.process_sample(0.0);
        }

        let echo = delay.process_sample(0.0);
        assert!(echo.abs() > 0.1, "Should have echo: {echo}");
    }

    #[test]
    fn test_mono_delay_tape_saturation() {
        let config = DelayConfig::tape();
        let mut delay = MonoDelay::new(config, 48000.0);

        // Process hot signal through tape delay — all outputs must stay finite.
        for _ in 0..2000 {
            let out = delay.process_sample(0.9);
            assert!(out.is_finite(), "tape delay output must be finite");
        }
    }

    #[test]
    fn test_mono_delay_tube_saturation() {
        let config = DelayConfig::tube();
        let mut delay = MonoDelay::new(config, 48000.0);

        for _ in 0..2000 {
            let out = delay.process_sample(0.8);
            assert!(out.is_finite(), "tube delay output must be finite");
        }
    }

    #[test]
    fn test_mono_delay_diode_saturation() {
        let config =
            DelayConfig::new(200.0, 0.5, 0.5).with_saturation(FeedbackSaturationMode::Diode, 2.0);
        let mut delay = MonoDelay::new(config, 48000.0);

        for _ in 0..2000 {
            let out = delay.process_sample(0.7);
            assert!(out.is_finite(), "diode delay output must be finite");
        }
    }

    #[test]
    fn test_saturation_none_is_linear() {
        let x = 0.5_f32;
        assert!((FeedbackSaturationMode::None.apply(x, 1.0) - x).abs() < 1e-6);
    }

    #[test]
    fn test_saturation_tape_bounded() {
        for &x in &[-2.0_f32, -1.0, -0.5, 0.0, 0.5, 1.0, 2.0] {
            let out = FeedbackSaturationMode::Tape.apply(x, 1.5);
            assert!(
                out.abs() <= 1.0 + 1e-5,
                "tape output {out} exceeds unity for input {x}"
            );
        }
    }

    #[test]
    fn test_saturation_tube_finite() {
        for &x in &[-2.0_f32, -1.0, 0.0, 1.0, 2.0] {
            let out = FeedbackSaturationMode::Tube.apply(x, 1.2);
            assert!(out.is_finite(), "tube output must be finite for input {x}");
        }
    }

    #[test]
    fn test_saturation_diode_finite() {
        for &x in &[-2.0_f32, -1.0, -0.7, 0.0, 0.7, 1.0, 2.0] {
            let out = FeedbackSaturationMode::Diode.apply(x, 2.0);
            assert!(out.is_finite(), "diode output must be finite for input {x}");
        }
    }

    #[test]
    fn test_stereo_delay() {
        let config = DelayConfig::default();
        let mut delay = StereoDelay::new(config, 48000.0);

        let (out_l, out_r) = delay.process_sample_stereo(1.0, 0.5);
        assert!(out_l != out_r); // Different inputs should give different outputs
    }

    #[test]
    fn test_delay_reset() {
        let config = DelayConfig::new(100.0, 0.9, 0.5);
        let mut delay = MonoDelay::new(config, 48000.0);

        // Fill delay line
        for _ in 0..1000 {
            delay.process_sample(1.0);
        }

        delay.reset();

        // After reset, delay line should be clear
        let output = delay.process_sample(0.0);
        assert!(output.abs() < 0.01);
    }

    #[test]
    fn test_wet_dry_trait() {
        let config = DelayConfig::new(100.0, 0.5, 0.4);
        let mut delay = MonoDelay::new(config, 48000.0);

        // Initial wet level from config
        assert!((delay.wet_dry() - 0.4).abs() < 1e-5);

        // Update via trait method
        delay.set_wet_dry(0.7);
        assert!((delay.wet_dry() - 0.7).abs() < 1e-5);

        // Clamping
        delay.set_wet_dry(1.5);
        assert!((delay.wet_dry() - 1.0).abs() < 1e-5);
    }
}