rill-digital-effects 0.5.0

Digital signal effects for Rill - Delay, Distortion, Chorus, Reverb, etc.
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
use float_cmp::approx_eq;
use rill_core::traits::{Node, ParamValue, ParameterId};
use rill_digital_effects::{Delay, Distortion, DistortionType, Limiter};

const BUF_SIZE: usize = 1024;

type TestDelay = Delay<f32, BUF_SIZE>;
type TestDistortion = Distortion<f32, BUF_SIZE>;
type TestLimiter = Limiter<f32, BUF_SIZE>;

///--------------------------------------------------------------------------------------------------------------------
///  Delay tests
/// -------------------------------------------------------------------------------------------------------------------

#[test]
fn test_delay_basic() {
    let mut delay = TestDelay::with_params(44100.0, 0.1, 0.5, 0.5);
    delay.init(44100.0);

    let input = vec![1.0; 100];
    let mut output = vec![0.0; 100];

    // Process sample by sample
    for i in 0..100 {
        output[i] = delay.process_sample(input[i]);
    }

    // First sample: dry only (no delayed signal yet)
    assert!(approx_eq!(f32, output[0], 0.5, epsilon = 0.001));

    // After delay time, should see wet signal
    let delay_samples = (0.1 * 44100.0) as usize;
    if delay_samples < 100 {
        assert!(output[delay_samples] > 0.5);
    }
}

#[test]
fn test_delay_parameters() {
    let mut delay = TestDelay::with_params(44100.0, 0.2, 0.3, 0.7);

    // Note: get_parameter returns Option<ParamValue>; we'll just check that it returns something
    // but we cannot directly compare because parameter names differ.
    // Since the test expects specific parameters, we need to map.
    // Let's skip parameter checks for now, as the API changed.
    // We'll just test that setting works.
    let delay_time_id = ParameterId::new("delay_time").unwrap();
    let feedback_id = ParameterId::new("feedback").unwrap();
    let mix_id = ParameterId::new("mix").unwrap();

    assert_eq!(
        Node::get_parameter(&delay, &delay_time_id),
        Some(ParamValue::Float(0.2))
    );
    assert_eq!(
        Node::get_parameter(&delay, &feedback_id),
        Some(ParamValue::Float(0.3))
    );
    assert_eq!(
        Node::get_parameter(&delay, &mix_id),
        Some(ParamValue::Float(0.7))
    );

    Node::set_parameter(&mut delay, &delay_time_id, ParamValue::Float(0.5)).unwrap();
    Node::set_parameter(&mut delay, &feedback_id, ParamValue::Float(0.8)).unwrap();
    Node::set_parameter(&mut delay, &mix_id, ParamValue::Float(0.4)).unwrap();

    assert_eq!(
        delay.get_parameter(&delay_time_id),
        Some(ParamValue::Float(0.5))
    );
    assert_eq!(
        Node::get_parameter(&delay, &feedback_id),
        Some(ParamValue::Float(0.8))
    );
    assert_eq!(
        Node::get_parameter(&delay, &mix_id),
        Some(ParamValue::Float(0.4))
    );
}

///--------------------------------------------------------------------------------------------------------------------
///  Distortion tests
/// -------------------------------------------------------------------------------------------------------------------

#[test]
fn test_distortion_hard_clip() {
    let dist = TestDistortion::with_params(44100.0, DistortionType::HardClip, 10.0, 1.0);

    assert_eq!(dist.process_sample(0.1), 1.0); // driven to 1.0, clipped to 1.0
    assert_eq!(dist.process_sample(-0.05), -0.5); // driven to -0.5, no clip
}

#[test]
fn test_distortion_soft_clip() {
    let dist = TestDistortion::with_params(44100.0, DistortionType::SoftClip, 5.0, 1.0);

    let out = dist.process_sample(1.0);
    assert!(out < 1.0 && out > 0.9); // tanh(5) ~ 0.9999
}

#[test]
fn test_distortion_parameters() {
    let mut dist = TestDistortion::with_params(44100.0, DistortionType::SoftClip, 2.0, 0.8);

    let drive_id = ParameterId::new("drive").unwrap();
    let output_gain_id = ParameterId::new("output_gain").unwrap();
    let type_id = ParameterId::new("type").unwrap();

    assert_eq!(
        Node::get_parameter(&dist, &drive_id),
        Some(ParamValue::Float(2.0))
    );
    assert_eq!(
        Node::get_parameter(&dist, &output_gain_id),
        Some(ParamValue::Float(0.8))
    );

    Node::set_parameter(&mut dist, &drive_id, ParamValue::Float(5.0)).unwrap();
    Node::set_parameter(&mut dist, &output_gain_id, ParamValue::Float(1.2)).unwrap();
    Node::set_parameter(
        &mut dist,
        &type_id,
        ParamValue::Choice("hard_clip".to_string()),
    )
    .unwrap();

    assert_eq!(
        Node::get_parameter(&dist, &drive_id),
        Some(ParamValue::Float(5.0))
    );
    assert_eq!(
        Node::get_parameter(&dist, &output_gain_id),
        Some(ParamValue::Float(1.2))
    );
}

#[test]
fn test_distortion_types() {
    let test_inputs = vec![0.1, 0.5, 1.0, -0.3, -0.8];

    for &dist_type in &[
        DistortionType::HardClip,
        DistortionType::SoftClip,
        DistortionType::Tube,
        DistortionType::Fuzz,
    ] {
        let dist = TestDistortion::with_params(44100.0, dist_type, 2.0, 1.0);

        for &input in &test_inputs {
            let output = dist.process_sample(input);
            assert!(!output.is_nan(), "Output should not be NaN");
            assert!(
                (-2.0..=2.0).contains(&output),
                "Output out of range: {}",
                output
            );
        }
    }
}

///--------------------------------------------------------------------------------------------------------------------
///  Limiter tests
/// -------------------------------------------------------------------------------------------------------------------
#[test]
fn test_limiter_basic() {
    println!("\n=== Test: Limiter Basic ===");

    let mut limiter = TestLimiter::new(44100.0, -6.0, 0.005, 0.1, 1.0);
    limiter.init(44100.0);

    let lookahead_samples = limiter.lookahead_samples();
    println!("Lookahead samples: {}, initializing...", lookahead_samples);

    // Fill buffer and wait for initialization
    for i in 0..lookahead_samples {
        let out = limiter.process_sample(0.1);
        if i < 10 {
            println!("Init sample {}: output = {:.3}", i, out);
        }
    }

    // Now test with low signal
    let test_input = 0.2;
    let test_output = limiter.process_sample(test_input);
    println!(
        "Low signal - input: {}, output: {}",
        test_input, test_output
    );

    // Should pass through unchanged (gain ~1.0)
    assert!(
        (test_output - test_input).abs() < 0.1,
        "Low signal should pass through, got {}",
        test_output
    );

    // Check high signal limiting
    let high_input = 1.5;
    let mut outputs = Vec::new();

    println!("\nProcessing high-level signal ({}):", high_input);
    for i in 0..2000 {
        let out = limiter.process_sample(high_input);
        outputs.push(out);
        if i % 200 == 0 {
            println!(
                "Sample {}: output = {:.3}, current_gain = {:.3}",
                i,
                out,
                limiter.current_gain()
            );
        }
    }

    // Last samples should be stable and limited
    let last_few = &outputs[1800..];
    let avg = last_few.iter().sum::<f32>() / last_few.len() as f32;
    let max_val = last_few.iter().fold(0.0f32, |a: f32, &b| a.max(b));
    let min_val = last_few.iter().fold(0.0f32, |a: f32, &b| a.min(b));

    println!("\nLast 200 samples statistics:");
    println!("  Average: {:.3}", avg);
    println!("  Max: {:.3}", max_val);
    println!("  Min: {:.3}", min_val);

    // Checks
    let threshold_linear = 10.0_f32.powf(-6.0 / 20.0); // -6dB ≈ 0.5

    assert!(
        avg > 0.0,
        "Average output should be positive, got {:.3}",
        avg
    );
    assert!(max_val < high_input, "Max output should be less than input");
    assert!(max_val > 0.0, "Max output should be positive");
    assert!(
        max_val < threshold_linear * 1.2,
        "Max output should be near threshold, got {:.3}",
        max_val
    );

    println!("\n✅ Limiter basic test passed");
}

#[test]
fn test_limiter_envelope() {
    println!("\n=== Test: Limiter Envelope ===");

    let mut limiter = TestLimiter::new(44100.0, -6.0, 0.01, 0.1, 1.0);
    limiter.set_lookahead(0.01);
    limiter.init(44100.0);

    let lookahead_samples = limiter.lookahead_samples();
    println!("Lookahead samples: {}, initializing...", lookahead_samples);

    // Initialization
    for _ in 0..lookahead_samples {
        let _ = limiter.process_sample(0.1);
    }

    // Generate signal with peak
    let total_samples = 4000;
    let peak_start = 1000;
    let peak_end = 1100;
    let mut outputs = Vec::with_capacity(total_samples);

    println!(
        "\nGenerating signal with peak at samples {}-{}",
        peak_start, peak_end
    );

    for i in 0..total_samples {
        let input = if i >= peak_start && i < peak_end {
            2.0
        } else {
            0.1
        };
        let output = limiter.process_sample(input);
        outputs.push(output);
    }

    // Find peak output level
    let max_output = outputs.iter().fold(0.0f32, |a: f32, &b| a.max(b));
    let max_idx = outputs
        .iter()
        .enumerate()
        .max_by(|(_, a): &(usize, &f32), (_, b): &(usize, &f32)| a.partial_cmp(b).unwrap())
        .map(|(i, _)| i)
        .unwrap_or(0);

    println!("\nPeak output: {:.3} at sample {}", max_output, max_idx);

    // Verify peak is limited according to threshold
    let threshold_linear = 10.0_f32.powf(-6.0 / 20.0); // -6dB ≈ 0.5
    let expected_max = 2.0 * threshold_linear; // ≈ 1.0

    println!("Threshold linear: {:.3}", threshold_linear);
    println!("Expected max output: {:.3}", expected_max);

    // Peak should be approximately expected_max (accounting for smoothing)
    assert!(
        max_output > 0.8 && max_output < 1.2,
        "Peak should be around {:.3}, got {:.3}",
        expected_max,
        max_output
    );

    // Verify signal recovers after peak
    let after_peak = peak_end + 500; // Allow time for recovery
    if after_peak < outputs.len() {
        let recovery = outputs[after_peak];
        println!("Recovery at sample {}: {:.3}", after_peak, recovery);
        assert!(
            (recovery - 0.1).abs() < 0.15,
            "After peak, output should return near 0.1, got {:.3}",
            recovery
        );
    }

    println!("\n✅ Limiter envelope test passed");
}

#[test]
fn test_limiter_parameters() {
    println!("\n=== Test: Limiter Parameters ===");

    let mut limiter = TestLimiter::new(44100.0, -3.0, 0.01, 0.2, 1.5);

    let threshold_id = ParameterId::new("threshold").unwrap();
    let attack_id = ParameterId::new("attack").unwrap();
    let release_id = ParameterId::new("release").unwrap();
    let output_gain_id = ParameterId::new("output_gain").unwrap();

    assert_eq!(
        Node::get_parameter(&limiter, &threshold_id),
        Some(ParamValue::Float(-3.0))
    );
    assert_eq!(
        Node::get_parameter(&limiter, &attack_id),
        Some(ParamValue::Float(0.01))
    );
    assert_eq!(
        Node::get_parameter(&limiter, &release_id),
        Some(ParamValue::Float(0.2))
    );
    assert_eq!(
        Node::get_parameter(&limiter, &output_gain_id),
        Some(ParamValue::Float(1.5))
    );

    Node::set_parameter(&mut limiter, &threshold_id, ParamValue::Float(-10.0)).unwrap();
    Node::set_parameter(&mut limiter, &attack_id, ParamValue::Float(0.02)).unwrap();
    Node::set_parameter(&mut limiter, &release_id, ParamValue::Float(0.3)).unwrap();
    Node::set_parameter(&mut limiter, &output_gain_id, ParamValue::Float(0.8)).unwrap();

    assert_eq!(
        Node::get_parameter(&limiter, &threshold_id),
        Some(ParamValue::Float(-10.0))
    );
    assert_eq!(
        Node::get_parameter(&limiter, &attack_id),
        Some(ParamValue::Float(0.02))
    );
    assert_eq!(
        Node::get_parameter(&limiter, &release_id),
        Some(ParamValue::Float(0.3))
    );
    assert_eq!(
        Node::get_parameter(&limiter, &output_gain_id),
        Some(ParamValue::Float(0.8))
    );

    println!("✅ Limiter parameter test passed");
}

#[test]
fn test_limiter_reset() {
    println!("\n=== Test: Limiter Reset ===");

    let mut limiter = TestLimiter::new(44100.0, -6.0, 0.01, 0.1, 1.0);
    limiter.init(44100.0);

    // Initialization
    let lookahead_samples = limiter.lookahead_samples();
    for _ in 0..lookahead_samples * 2 {
        limiter.process_sample(0.1);
    }

    // Process high signal (should change gain)
    println!("Processing high signal (first pass)...");
    for i in 0..200 {
        // Increased to 200 samples
        let out = limiter.process_sample(1.5);
        if i == 0 || i == 100 || i == 199 {
            println!(
                "  Step {}: gain={:.3}, out={:.3}",
                i,
                limiter.current_gain(),
                out
            );
        }
    }

    let gain_before = limiter.current_gain();
    println!("Gain before reset: {:.3}", gain_before);

    // Verify gain has decreased
    assert!(
        gain_before < 0.8,
        "Gain should be reduced (<0.8), got {:.3}",
        gain_before
    );

    // Reset
    println!("Resetting...");
    limiter.reset();

    // After reset, gain should be 1.0
    assert_eq!(limiter.current_gain(), 1.0, "Gain should reset to 1.0");

    // Warm up again
    println!("Warming up after reset...");
    for i in 0..lookahead_samples * 2 {
        let out = limiter.process_sample(0.1);
        if i == 0 {
            println!("  First sample after reset: out={:.3}", out);
        }
    }

    // Verify limiter works again
    println!("Testing limiting after reset (200 samples)...");
    let mut max_out = 0.0f32;
    for i in 0..200 {
        // Increased to 200 samples
        let out = limiter.process_sample(1.5);
        max_out = max_out.max(out);
        if i == 0 || i == 100 || i == 199 {
            println!(
                "  Step {}: gain={:.3}, out={:.3}",
                i,
                limiter.current_gain(),
                out
            );
        }
    }

    let gain_after = limiter.current_gain();
    println!("Gain after reset and processing: {:.3}", gain_after);

    // Verify gain has decreased again (now <0.8)
    assert!(
        gain_after < 0.8,
        "Gain should be reduced again (<0.8), got {:.3}",
        gain_after
    );

    // Verify output is limited
    assert!(
        max_out < 1.0,
        "Output should be limited (<1.0), got {:.3}",
        max_out
    );

    println!("✅ Limiter reset test passed");
}