wavecraft-dsp 0.12.49

Pure DSP algorithms for Wavecraft - no plugin framework dependencies
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
//! Chain combinator for serial processor composition.

use crate::traits::{ParamSpec, Processor, ProcessorParams, Transport};

/// Processor wrapper that adds a standard per-instance bypass parameter.
pub struct Bypassed<P> {
    pub processor: P,
    source_bypassed: bool,
    target_bypassed: bool,
    transition_phase: BypassTransitionPhase,
    transition_samples: u32,
}

#[derive(Debug, Clone, Copy)]
enum BypassTransitionPhase {
    Stable,
    FadeOut { remaining: u32 },
    FadeIn { remaining: u32 },
}

const DEFAULT_BYPASS_TRANSITION_SAMPLES: u32 = 64;
const MIN_BYPASS_TRANSITION_SAMPLES: u32 = 16;
const MAX_BYPASS_TRANSITION_SAMPLES: u32 = 256;
const BYPASS_TRANSITION_SECONDS: f32 = 0.002;

impl<P> Bypassed<P> {
    /// Creates a bypass wrapper around a processor instance.
    pub fn new(processor: P) -> Self {
        Self {
            processor,
            source_bypassed: false,
            target_bypassed: false,
            transition_phase: BypassTransitionPhase::Stable,
            transition_samples: DEFAULT_BYPASS_TRANSITION_SAMPLES,
        }
    }

    #[inline]
    fn transition_samples_for_rate(sample_rate: f32) -> u32 {
        if sample_rate <= 0.0 {
            return DEFAULT_BYPASS_TRANSITION_SAMPLES;
        }

        ((sample_rate * BYPASS_TRANSITION_SECONDS).round() as u32)
            .clamp(MIN_BYPASS_TRANSITION_SAMPLES, MAX_BYPASS_TRANSITION_SAMPLES)
    }

    #[inline]
    fn apply_gain_ramp(buffer: &mut [&mut [f32]], start_gain: f32, end_gain: f32) {
        let samples = buffer
            .iter()
            .map(|channel| channel.len())
            .min()
            .unwrap_or(0);
        if samples == 0 {
            return;
        }

        let denominator = samples.saturating_sub(1) as f32;

        for channel in buffer.iter_mut() {
            for (idx, sample) in channel.iter_mut().take(samples).enumerate() {
                let t = if denominator <= 0.0 {
                    1.0
                } else {
                    idx as f32 / denominator
                };
                let gain = start_gain + (end_gain - start_gain) * t;
                *sample *= gain;
            }
        }
    }
}

impl<P> Default for Bypassed<P>
where
    P: Default,
{
    fn default() -> Self {
        Self::new(P::default())
    }
}

impl<P> From<P> for Bypassed<P> {
    fn from(processor: P) -> Self {
        Self::new(processor)
    }
}

/// Parameters for [`Bypassed`].
///
/// Includes wrapped processor parameters plus one boolean bypass flag.
pub struct BypassedParams<PP> {
    pub inner: PP,
    pub bypassed: bool,
}

impl<PP> Default for BypassedParams<PP>
where
    PP: Default,
{
    fn default() -> Self {
        Self {
            inner: PP::default(),
            bypassed: false,
        }
    }
}

impl<PP> ProcessorParams for BypassedParams<PP>
where
    PP: ProcessorParams,
{
    fn param_specs() -> &'static [ParamSpec] {
        fn extend_specs(target: &mut Vec<ParamSpec>, source: &[ParamSpec]) {
            for spec in source {
                target.push(ParamSpec {
                    name: spec.name,
                    id_suffix: spec.id_suffix,
                    range: spec.range.clone(),
                    default: spec.default,
                    unit: spec.unit,
                    group: spec.group,
                });
            }
        }

        let inner_specs = PP::param_specs();
        let mut merged = Vec::with_capacity(inner_specs.len() + 1);

        extend_specs(&mut merged, inner_specs);
        merged.push(ParamSpec {
            name: "Bypass",
            id_suffix: "bypass",
            range: crate::ParamRange::Stepped { min: 0, max: 1 },
            default: 0.0,
            unit: "",
            group: None,
        });

        // See comment in ChainParams::param_specs for rationale.
        Box::leak(merged.into_boxed_slice())
    }

    fn from_param_defaults() -> Self {
        Self {
            inner: PP::from_param_defaults(),
            bypassed: false,
        }
    }

    fn plain_value_count() -> usize {
        PP::plain_value_count() + 1
    }

    fn apply_plain_values(&mut self, values: &[f32]) {
        let inner_count = PP::plain_value_count();
        let split_at = inner_count.min(values.len());
        let (inner_values, bypass_values) = values.split_at(split_at);

        self.inner.apply_plain_values(inner_values);

        if let Some(bypass_value) = bypass_values.first() {
            self.bypassed = *bypass_value >= 0.5;
        }
    }
}

impl<P> Processor for Bypassed<P>
where
    P: Processor,
{
    type Params = BypassedParams<P::Params>;

    fn process(&mut self, buffer: &mut [&mut [f32]], transport: &Transport, params: &Self::Params) {
        if params.bypassed != self.target_bypassed {
            self.target_bypassed = params.bypassed;
            if self.source_bypassed != self.target_bypassed {
                self.transition_phase = BypassTransitionPhase::FadeOut {
                    remaining: self.transition_samples,
                };
            }
        }

        if !self.source_bypassed {
            self.processor.process(buffer, transport, &params.inner);
        }

        let samples = buffer
            .iter()
            .map(|channel| channel.len())
            .min()
            .unwrap_or(0) as u32;
        if samples == 0 {
            return;
        }

        match self.transition_phase {
            BypassTransitionPhase::Stable => {}
            BypassTransitionPhase::FadeOut { remaining } => {
                let used = remaining.min(samples);
                let total = self.transition_samples.max(1) as f32;
                let start_gain = remaining as f32 / total;
                let end_gain = remaining.saturating_sub(used) as f32 / total;

                Self::apply_gain_ramp(buffer, start_gain, end_gain);

                let new_remaining = remaining.saturating_sub(used);
                if new_remaining == 0 {
                    self.source_bypassed = self.target_bypassed;
                    self.transition_phase = BypassTransitionPhase::FadeIn {
                        remaining: self.transition_samples,
                    };
                } else {
                    self.transition_phase = BypassTransitionPhase::FadeOut {
                        remaining: new_remaining,
                    };
                }
            }
            BypassTransitionPhase::FadeIn { remaining } => {
                let used = remaining.min(samples);
                let total = self.transition_samples.max(1) as f32;
                let start_gain = 1.0 - (remaining as f32 / total);
                let end_gain = 1.0 - (remaining.saturating_sub(used) as f32 / total);

                Self::apply_gain_ramp(buffer, start_gain, end_gain);

                let new_remaining = remaining.saturating_sub(used);
                if new_remaining == 0 {
                    self.transition_phase = BypassTransitionPhase::Stable;
                } else {
                    self.transition_phase = BypassTransitionPhase::FadeIn {
                        remaining: new_remaining,
                    };
                }
            }
        }
    }

    fn set_sample_rate(&mut self, sample_rate: f32) {
        self.transition_samples = Self::transition_samples_for_rate(sample_rate);
        self.processor.set_sample_rate(sample_rate);
    }

    fn reset(&mut self) {
        self.source_bypassed = self.target_bypassed;
        self.transition_phase = BypassTransitionPhase::Stable;
        self.processor.reset();
    }
}

/// Combines two processors in series: A → B.
///
/// Audio flows through processor A, then through processor B.
/// Parameters from both processors are merged.
pub struct Chain<A, B> {
    pub first: A,
    pub second: B,
}

impl<A, B> Default for Chain<A, B>
where
    A: Default,
    B: Default,
{
    fn default() -> Self {
        Self {
            first: A::default(),
            second: B::default(),
        }
    }
}

/// Combined parameters for chained processors.
///
/// Merges parameter specs from both processors.
pub struct ChainParams<PA, PB> {
    pub first: PA,
    pub second: PB,
}

impl<PA, PB> Default for ChainParams<PA, PB>
where
    PA: Default,
    PB: Default,
{
    fn default() -> Self {
        Self {
            first: PA::default(),
            second: PB::default(),
        }
    }
}

impl<PA, PB> ProcessorParams for ChainParams<PA, PB>
where
    PA: ProcessorParams,
    PB: ProcessorParams,
{
    fn param_specs() -> &'static [ParamSpec] {
        fn extend_specs(target: &mut Vec<ParamSpec>, source: &[ParamSpec]) {
            for spec in source {
                target.push(ParamSpec {
                    name: spec.name,
                    id_suffix: spec.id_suffix,
                    range: spec.range.clone(),
                    default: spec.default,
                    unit: spec.unit,
                    group: spec.group,
                });
            }
        }

        // WORKAROUND FOR HOT-RELOAD HANG:
        //
        // Do NOT use OnceLock or any locking primitive here. On macOS, when the
        // subprocess calls dlopen() → wavecraft_get_params_json() → param_specs(),
        // initialization of OnceLock statics can hang indefinitely (30s timeout).
        //
        // Instead, we allocate and leak the merged specs on EVERY call. This is
        // acceptable because:
        // 1. param_specs() is called at most once per plugin load (startup only)
        // 2. The leak is ~hundreds of bytes (not per-sample, not per-frame)
        // 3. Plugin lifetime = process lifetime (no meaningful leak)
        // 4. Hot-reload works correctly (no 30s hang)
        //
        // This is a pragmatic trade-off: small memory leak vs. broken hot-reload.
        // Future work: investigate root cause of OnceLock hang on macOS dlopen.

        let first_specs = PA::param_specs();
        let second_specs = PB::param_specs();

        let mut merged = Vec::with_capacity(first_specs.len() + second_specs.len());

        extend_specs(&mut merged, first_specs);
        extend_specs(&mut merged, second_specs);

        // Leak to get 'static reference (intentional - see comment above)
        Box::leak(merged.into_boxed_slice())
    }

    fn from_param_defaults() -> Self {
        Self {
            first: PA::from_param_defaults(),
            second: PB::from_param_defaults(),
        }
    }

    fn plain_value_count() -> usize {
        PA::plain_value_count() + PB::plain_value_count()
    }

    fn apply_plain_values(&mut self, values: &[f32]) {
        let first_count = PA::plain_value_count();
        let split_at = first_count.min(values.len());
        let (first_values, second_values) = values.split_at(split_at);

        self.first.apply_plain_values(first_values);
        self.second.apply_plain_values(second_values);
    }
}

impl<A, B> Processor for Chain<A, B>
where
    A: Processor,
    B: Processor,
{
    type Params = ChainParams<A::Params, B::Params>;

    fn process(&mut self, buffer: &mut [&mut [f32]], transport: &Transport, params: &Self::Params) {
        // Process first, then second (serial chain)
        self.first.process(buffer, transport, &params.first);
        self.second.process(buffer, transport, &params.second);
    }

    fn set_sample_rate(&mut self, sample_rate: f32) {
        self.first.set_sample_rate(sample_rate);
        self.second.set_sample_rate(sample_rate);
    }

    fn reset(&mut self) {
        self.first.reset();
        self.second.reset();
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::{
        Arc,
        atomic::{AtomicBool, AtomicU32, Ordering},
    };

    #[derive(Clone)]
    struct TestGainParams {
        level: f32,
    }

    impl Default for TestGainParams {
        fn default() -> Self {
            Self { level: 1.0 }
        }
    }

    impl ProcessorParams for TestGainParams {
        fn param_specs() -> &'static [ParamSpec] {
            static SPECS: [ParamSpec; 1] = [ParamSpec {
                name: "Level",
                id_suffix: "level",
                range: crate::ParamRange::Linear { min: 0.0, max: 2.0 },
                default: 1.0,
                unit: "x",
                group: None,
            }];
            &SPECS
        }

        fn from_param_defaults() -> Self {
            Self { level: 1.0 }
        }

        fn apply_plain_values(&mut self, values: &[f32]) {
            if let Some(level) = values.first() {
                self.level = *level;
            }
        }
    }

    #[derive(Default)]
    struct TestGainDsp;

    impl Processor for TestGainDsp {
        type Params = TestGainParams;

        fn process(
            &mut self,
            buffer: &mut [&mut [f32]],
            _transport: &Transport,
            params: &Self::Params,
        ) {
            for channel in buffer.iter_mut() {
                for sample in channel.iter_mut() {
                    *sample *= params.level;
                }
            }
        }
    }

    #[derive(Clone, Default)]
    struct TestPassthroughParams;

    impl ProcessorParams for TestPassthroughParams {
        fn param_specs() -> &'static [ParamSpec] {
            &[]
        }
    }

    #[derive(Default)]
    struct TestPassthroughDsp;

    impl Processor for TestPassthroughDsp {
        type Params = TestPassthroughParams;

        fn process(
            &mut self,
            _buffer: &mut [&mut [f32]],
            _transport: &Transport,
            _params: &Self::Params,
        ) {
        }
    }

    #[derive(Clone)]
    struct TestParams;

    impl Default for TestParams {
        fn default() -> Self {
            Self
        }
    }

    impl ProcessorParams for TestParams {
        fn param_specs() -> &'static [ParamSpec] {
            &[]
        }
    }

    #[derive(Clone, Default)]
    struct PanicParamSpecsParams {
        value: f32,
    }

    impl ProcessorParams for PanicParamSpecsParams {
        fn param_specs() -> &'static [ParamSpec] {
            panic!("param_specs must not be called from runtime split path")
        }

        fn plain_value_count() -> usize {
            1
        }

        fn apply_plain_values(&mut self, values: &[f32]) {
            if let Some(value) = values.first() {
                self.value = *value;
            }
        }
    }

    struct LifecycleProbe {
        set_sample_rate_calls: Arc<AtomicU32>,
        reset_calls: Arc<AtomicU32>,
        last_sample_rate_bits: Arc<AtomicU32>,
        touched_process: Arc<AtomicBool>,
    }

    impl LifecycleProbe {
        fn new() -> Self {
            Self {
                set_sample_rate_calls: Arc::new(AtomicU32::new(0)),
                reset_calls: Arc::new(AtomicU32::new(0)),
                last_sample_rate_bits: Arc::new(AtomicU32::new(0)),
                touched_process: Arc::new(AtomicBool::new(false)),
            }
        }
    }

    impl Processor for LifecycleProbe {
        type Params = TestParams;

        fn process(
            &mut self,
            _buffer: &mut [&mut [f32]],
            _transport: &Transport,
            _params: &Self::Params,
        ) {
            self.touched_process.store(true, Ordering::SeqCst);
        }

        fn set_sample_rate(&mut self, sample_rate: f32) {
            self.set_sample_rate_calls.fetch_add(1, Ordering::SeqCst);
            self.last_sample_rate_bits
                .store(sample_rate.to_bits(), Ordering::SeqCst);
        }

        fn reset(&mut self) {
            self.reset_calls.fetch_add(1, Ordering::SeqCst);
        }
    }

    #[test]
    fn test_chain_processes_in_order() {
        let mut chain = Chain {
            first: TestGainDsp,
            second: TestGainDsp,
        };

        let mut left = [1.0_f32, 1.0_f32];
        let mut right = [1.0_f32, 1.0_f32];
        let mut buffer = [&mut left[..], &mut right[..]];

        let transport = Transport::default();
        let params = ChainParams {
            first: TestGainParams { level: 0.5 },
            second: TestGainParams { level: 2.0 },
        };

        chain.process(&mut buffer, &transport, &params);

        // Expected: 1.0 * 0.5 * 2.0 = 1.0
        assert!((buffer[0][0] - 1.0_f32).abs() < 1e-6);
        assert!((buffer[1][0] - 1.0_f32).abs() < 1e-6);
    }

    #[test]
    fn test_chain_with_passthrough() {
        let mut chain = Chain {
            first: TestPassthroughDsp,
            second: TestGainDsp,
        };

        let mut left = [2.0_f32, 2.0_f32];
        let mut right = [2.0_f32, 2.0_f32];
        let mut buffer = [&mut left[..], &mut right[..]];

        let transport = Transport::default();
        let params = ChainParams {
            first: TestPassthroughParams,
            second: TestGainParams { level: 0.5 },
        };

        chain.process(&mut buffer, &transport, &params);

        // Expected: 2.0 * 1.0 * 0.5 = 1.0
        assert!((buffer[0][0] - 1.0_f32).abs() < 1e-6);
    }

    #[test]
    fn test_chain_params_merge() {
        let specs = <ChainParams<TestGainParams, TestGainParams>>::param_specs();
        assert_eq!(specs.len(), 2); // Both gain params

        // Both should have "Level" name
        assert_eq!(specs[0].name, "Level");
        assert_eq!(specs[1].name, "Level");
    }

    #[test]
    fn test_chain_default() {
        let _chain: Chain<TestGainDsp, TestPassthroughDsp> = Chain::default();
        let _params: ChainParams<TestGainParams, TestPassthroughParams> = ChainParams::default();
    }

    #[test]
    fn test_chain_from_param_defaults_uses_children_spec_defaults() {
        let defaults = <ChainParams<TestGainParams, TestGainParams>>::from_param_defaults();
        assert!((defaults.first.level - 1.0).abs() < 1e-6);
        assert!((defaults.second.level - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_chain_apply_plain_values_splits_by_child_param_count() {
        let mut params = <ChainParams<TestGainParams, TestGainParams>>::from_param_defaults();
        params.apply_plain_values(&[0.25, 1.75]);

        assert!((params.first.level - 0.25).abs() < 1e-6);
        assert!((params.second.level - 1.75).abs() < 1e-6);
    }

    #[test]
    fn test_bypassed_apply_plain_values_uses_plain_value_count_without_param_specs() {
        let mut params = <BypassedParams<PanicParamSpecsParams>>::from_param_defaults();
        params.apply_plain_values(&[0.5, 1.0]);

        assert!((params.inner.value - 0.5).abs() < 1e-6);
        assert!(params.bypassed);
    }

    #[test]
    fn test_chain_apply_plain_values_uses_plain_value_count_without_param_specs() {
        let mut params =
            <ChainParams<PanicParamSpecsParams, PanicParamSpecsParams>>::from_param_defaults();
        params.apply_plain_values(&[0.25, 0.75]);

        assert!((params.first.value - 0.25).abs() < 1e-6);
        assert!((params.second.value - 0.75).abs() < 1e-6);
    }

    #[test]
    fn test_chain_propagates_set_sample_rate_to_both_processors() {
        let first = LifecycleProbe::new();
        let second = LifecycleProbe::new();

        let first_calls = Arc::clone(&first.set_sample_rate_calls);
        let second_calls = Arc::clone(&second.set_sample_rate_calls);
        let first_sr = Arc::clone(&first.last_sample_rate_bits);
        let second_sr = Arc::clone(&second.last_sample_rate_bits);

        let mut chain = Chain { first, second };
        chain.set_sample_rate(48_000.0);

        assert_eq!(first_calls.load(Ordering::SeqCst), 1);
        assert_eq!(second_calls.load(Ordering::SeqCst), 1);
        assert_eq!(f32::from_bits(first_sr.load(Ordering::SeqCst)), 48_000.0);
        assert_eq!(f32::from_bits(second_sr.load(Ordering::SeqCst)), 48_000.0);
    }

    #[test]
    fn test_chain_propagates_reset_to_both_processors() {
        let first = LifecycleProbe::new();
        let second = LifecycleProbe::new();

        let first_resets = Arc::clone(&first.reset_calls);
        let second_resets = Arc::clone(&second.reset_calls);

        let mut chain = Chain { first, second };
        chain.reset();

        assert_eq!(first_resets.load(Ordering::SeqCst), 1);
        assert_eq!(second_resets.load(Ordering::SeqCst), 1);
    }

    #[test]
    fn test_bypassed_param_specs_include_bypass_flag() {
        let specs = <BypassedParams<TestGainParams>>::param_specs();
        assert_eq!(specs.len(), 2);
        assert_eq!(specs[0].id_suffix, "level");
        assert_eq!(specs[1].id_suffix, "bypass");
    }

    #[test]
    fn test_bypassed_process_skips_child_when_bypassed_after_transition() {
        let touched = Arc::new(AtomicBool::new(false));

        struct TouchProbe {
            touched: Arc<AtomicBool>,
        }

        impl Processor for TouchProbe {
            type Params = TestParams;

            fn process(
                &mut self,
                _buffer: &mut [&mut [f32]],
                _transport: &Transport,
                _params: &Self::Params,
            ) {
                self.touched.store(true, Ordering::SeqCst);
            }
        }

        let mut wrapped = Bypassed::new(TouchProbe {
            touched: Arc::clone(&touched),
        });

        let mut left = [1.0_f32, 2.0_f32];
        let mut right = [3.0_f32, 4.0_f32];
        let mut buffer = [&mut left[..], &mut right[..]];

        // First call starts transition, subsequent calls settle bypass.
        for _ in 0..140 {
            wrapped.process(
                &mut buffer,
                &Transport::default(),
                &BypassedParams {
                    inner: TestParams,
                    bypassed: true,
                },
            );
        }

        touched.store(false, Ordering::SeqCst);

        wrapped.process(
            &mut buffer,
            &Transport::default(),
            &BypassedParams {
                inner: TestParams,
                bypassed: true,
            },
        );

        let mut verify_left = [1.0_f32, 2.0_f32];
        let mut verify_right = [3.0_f32, 4.0_f32];
        let original_left = verify_left;
        let original_right = verify_right;
        let mut verify_buffer = [&mut verify_left[..], &mut verify_right[..]];

        wrapped.process(
            &mut verify_buffer,
            &Transport::default(),
            &BypassedParams {
                inner: TestParams,
                bypassed: true,
            },
        );

        assert!(!touched.load(Ordering::SeqCst));
        assert_eq!(verify_left, original_left);
        assert_eq!(verify_right, original_right);
    }

    #[test]
    fn test_bypassed_process_runs_child_when_active() {
        let mut wrapped = Bypassed::new(TestGainDsp);

        let mut left = [1.0_f32, 1.0_f32];
        let mut right = [1.0_f32, 1.0_f32];
        let mut buffer = [&mut left[..], &mut right[..]];

        wrapped.process(
            &mut buffer,
            &Transport::default(),
            &BypassedParams {
                inner: TestGainParams { level: 0.5 },
                bypassed: false,
            },
        );

        assert!((left[0] - 0.5_f32).abs() < 1e-6);
        assert!((right[0] - 0.5_f32).abs() < 1e-6);
    }

    #[derive(Default)]
    struct PolarityFlip;

    impl Processor for PolarityFlip {
        type Params = TestPassthroughParams;

        fn process(
            &mut self,
            buffer: &mut [&mut [f32]],
            _transport: &Transport,
            _params: &Self::Params,
        ) {
            for channel in buffer.iter_mut() {
                for sample in channel.iter_mut() {
                    *sample = -*sample;
                }
            }
        }
    }

    #[test]
    fn test_bypassed_transition_smooths_toggle_edges() {
        let mut wrapped = Bypassed::new(PolarityFlip);

        let active = BypassedParams {
            inner: TestPassthroughParams,
            bypassed: false,
        };
        let bypassed = BypassedParams {
            inner: TestPassthroughParams,
            bypassed: true,
        };

        // Warm up active state: output should be wet (-1.0).
        let mut previous = -1.0_f32;
        for _ in 0..4 {
            let mut sample = [1.0_f32];
            let mut buffer = [&mut sample[..]];
            wrapped.process(&mut buffer, &Transport::default(), &active);
            previous = sample[0];
        }
        assert!(previous < -0.95);

        // Toggle to bypassed and ensure output does not jump directly to +1.0.
        let mut max_step = 0.0_f32;
        for _ in 0..160 {
            let mut sample = [1.0_f32];
            let mut buffer = [&mut sample[..]];
            wrapped.process(&mut buffer, &Transport::default(), &bypassed);

            let step = (sample[0] - previous).abs();
            max_step = max_step.max(step);
            previous = sample[0];
        }

        // Bounded transition: no full-scale discontinuity in one sample.
        assert!(max_step < 1.0);
        // Settles to dry signal after transition.
        assert!(previous > 0.95);
    }

    #[test]
    fn test_bypassed_transition_is_bidirectional() {
        let mut wrapped = Bypassed::new(PolarityFlip);

        let active = BypassedParams {
            inner: TestPassthroughParams,
            bypassed: false,
        };
        let bypassed = BypassedParams {
            inner: TestPassthroughParams,
            bypassed: true,
        };

        // Move into stable bypassed state.
        let mut sample_out = 0.0_f32;
        for _ in 0..160 {
            let mut sample = [1.0_f32];
            let mut buffer = [&mut sample[..]];
            wrapped.process(&mut buffer, &Transport::default(), &bypassed);
            sample_out = sample[0];
        }
        assert!(sample_out > 0.95);

        // Toggle back to active and ensure transition settles to wet (-1.0).
        let mut max_step = 0.0_f32;
        let mut previous = sample_out;
        for _ in 0..160 {
            let mut sample = [1.0_f32];
            let mut buffer = [&mut sample[..]];
            wrapped.process(&mut buffer, &Transport::default(), &active);

            let step = (sample[0] - previous).abs();
            max_step = max_step.max(step);
            previous = sample[0];
        }

        assert!(max_step < 1.0);
        assert!(previous < -0.95);
    }
}