quiver-dsp 0.1.0

A modular audio synthesis library using Arrow-style combinators and graph-based patching
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
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
//! SIMD Vectorization and Block Processing
//!
//! This module provides SIMD-accelerated DSP operations and block-oriented
//! processing utilities for improved performance.
//!
//! # Features
//!
//! - [`AudioBlock`] - audio buffer for block/vectorized operations
//! - Block processing utilities
//! - Lazy evaluation framework
//! - SIMD-accelerated block operations (when the `simd` feature is enabled),
//!   backed by the no_std-compatible `wide` crate (`f64x4`)
//!
//! # Alignment
//!
//! Buffers are backed by a plain `Vec<f64>`, which the global allocator aligns
//! to `align_of::<f64>()` = 8 bytes. The `simd` path uses unaligned lane loads
//! (`f64x4::new`) over `chunks_exact(4)` with a scalar remainder, so no
//! over-alignment of the backing storage is required or guaranteed.

use crate::port::{BlockPortValues, GraphModule, PortValues};
use alloc::vec;
use alloc::vec::Vec;
use core::f64::consts::PI;
use libm::Libm;

#[cfg(feature = "simd")]
use wide::f64x4;

/// Block size for SIMD operations (typically 4 or 8 for SSE/AVX)
pub const SIMD_BLOCK_SIZE: usize = 4;

/// Default processing block size
pub const DEFAULT_BLOCK_SIZE: usize = 64;

/// Audio buffer for block/vectorized operations.
///
/// Provides efficient storage and operations for audio blocks. The backing
/// store is a plain `Vec<f64>` (8-byte aligned); the `simd` feature accelerates
/// the block ops with `wide::f64x4` unaligned loads and therefore imposes no
/// over-alignment requirement on the storage.
#[derive(Clone)]
pub struct AudioBlock {
    /// Sample data
    samples: Vec<f64>,
    /// Block size (number of samples)
    size: usize,
}

impl AudioBlock {
    /// Create a new audio block with the given size
    pub fn new(size: usize) -> Self {
        Self {
            samples: vec![0.0; size],
            size,
        }
    }

    /// Create a block filled with a constant value
    pub fn constant(size: usize, value: f64) -> Self {
        Self {
            samples: vec![value; size],
            size,
        }
    }

    /// Create a block from existing samples
    pub fn from_samples(samples: Vec<f64>) -> Self {
        let size = samples.len();
        Self { samples, size }
    }

    /// Get the block size
    #[inline]
    pub fn len(&self) -> usize {
        self.size
    }

    /// Check if empty
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.size == 0
    }

    /// Get a sample at the given index
    #[inline]
    pub fn get(&self, index: usize) -> f64 {
        self.samples.get(index).copied().unwrap_or(0.0)
    }

    /// Set a sample at the given index
    #[inline]
    pub fn set(&mut self, index: usize, value: f64) {
        if index < self.size {
            self.samples[index] = value;
        }
    }

    /// Get a slice of all samples
    #[inline]
    pub fn as_slice(&self) -> &[f64] {
        &self.samples
    }

    /// Get a mutable slice of all samples
    #[inline]
    pub fn as_mut_slice(&mut self) -> &mut [f64] {
        &mut self.samples
    }

    /// Fill the block with a constant value
    pub fn fill(&mut self, value: f64) {
        self.samples.fill(value);
    }

    /// Clear the block (fill with zeros)
    pub fn clear(&mut self) {
        self.fill(0.0);
    }

    /// Add a constant to all samples
    #[cfg(not(feature = "simd"))]
    pub fn add_scalar(&mut self, value: f64) {
        for sample in &mut self.samples {
            *sample += value;
        }
    }

    /// Multiply all samples by a constant
    #[cfg(not(feature = "simd"))]
    pub fn mul_scalar(&mut self, value: f64) {
        for sample in &mut self.samples {
            *sample *= value;
        }
    }

    /// Add another block element-wise
    #[cfg(not(feature = "simd"))]
    pub fn add_block(&mut self, other: &AudioBlock) {
        let len = self.size.min(other.size);
        for i in 0..len {
            self.samples[i] += other.samples[i];
        }
    }

    /// Multiply by another block element-wise
    #[cfg(not(feature = "simd"))]
    pub fn mul_block(&mut self, other: &AudioBlock) {
        let len = self.size.min(other.size);
        for i in 0..len {
            self.samples[i] *= other.samples[i];
        }
    }

    /// SIMD-accelerated scalar addition (when the `simd` feature is enabled).
    ///
    /// Processes four lanes at a time with `wide::f64x4` over
    /// `chunks_exact(4)`, with a scalar remainder. Because IEEE-754 addition is
    /// correctly rounded per lane, the result is bit-for-bit identical to the
    /// scalar path.
    #[cfg(feature = "simd")]
    pub fn add_scalar(&mut self, value: f64) {
        let mut chunks = self.samples.chunks_exact_mut(SIMD_BLOCK_SIZE);
        for c in &mut chunks {
            let r = (f64x4::new([c[0], c[1], c[2], c[3]]) + value).to_array();
            c.copy_from_slice(&r);
        }
        for x in chunks.into_remainder() {
            *x += value;
        }
    }

    /// SIMD-accelerated scalar multiplication (when the `simd` feature is
    /// enabled). Bit-for-bit identical to the scalar path.
    #[cfg(feature = "simd")]
    pub fn mul_scalar(&mut self, value: f64) {
        let mut chunks = self.samples.chunks_exact_mut(SIMD_BLOCK_SIZE);
        for c in &mut chunks {
            let r = (f64x4::new([c[0], c[1], c[2], c[3]]) * value).to_array();
            c.copy_from_slice(&r);
        }
        for x in chunks.into_remainder() {
            *x *= value;
        }
    }

    /// SIMD-accelerated element-wise block addition (when the `simd` feature is
    /// enabled). Bit-for-bit identical to the scalar path.
    #[cfg(feature = "simd")]
    pub fn add_block(&mut self, other: &AudioBlock) {
        let len = self.size.min(other.size);
        let a = &mut self.samples[..len];
        let b = &other.samples[..len];
        let mut a_chunks = a.chunks_exact_mut(SIMD_BLOCK_SIZE);
        let mut b_chunks = b.chunks_exact(SIMD_BLOCK_SIZE);
        for (ca, cb) in a_chunks.by_ref().zip(b_chunks.by_ref()) {
            let va = f64x4::new([ca[0], ca[1], ca[2], ca[3]]);
            let vb = f64x4::new([cb[0], cb[1], cb[2], cb[3]]);
            ca.copy_from_slice(&(va + vb).to_array());
        }
        for (x, y) in a_chunks
            .into_remainder()
            .iter_mut()
            .zip(b_chunks.remainder())
        {
            *x += *y;
        }
    }

    /// SIMD-accelerated element-wise block multiplication (when the `simd`
    /// feature is enabled). Bit-for-bit identical to the scalar path.
    #[cfg(feature = "simd")]
    pub fn mul_block(&mut self, other: &AudioBlock) {
        let len = self.size.min(other.size);
        let a = &mut self.samples[..len];
        let b = &other.samples[..len];
        let mut a_chunks = a.chunks_exact_mut(SIMD_BLOCK_SIZE);
        let mut b_chunks = b.chunks_exact(SIMD_BLOCK_SIZE);
        for (ca, cb) in a_chunks.by_ref().zip(b_chunks.by_ref()) {
            let va = f64x4::new([ca[0], ca[1], ca[2], ca[3]]);
            let vb = f64x4::new([cb[0], cb[1], cb[2], cb[3]]);
            ca.copy_from_slice(&(va * vb).to_array());
        }
        for (x, y) in a_chunks
            .into_remainder()
            .iter_mut()
            .zip(b_chunks.remainder())
        {
            *x *= *y;
        }
    }

    /// Apply a function to all samples
    pub fn map<F: Fn(f64) -> f64>(&mut self, f: F) {
        for sample in &mut self.samples {
            *sample = f(*sample);
        }
    }

    /// Apply soft clipping (tanh saturation)
    pub fn soft_clip(&mut self, drive: f64) {
        for sample in &mut self.samples {
            *sample = Libm::<f64>::tanh(*sample * drive) / Libm::<f64>::tanh(drive).max(0.001);
        }
    }

    /// Apply hard clipping
    pub fn hard_clip(&mut self, threshold: f64) {
        for sample in &mut self.samples {
            *sample = sample.clamp(-threshold, threshold);
        }
    }

    /// Get the peak (maximum absolute value)
    pub fn peak(&self) -> f64 {
        self.samples.iter().map(|s| s.abs()).fold(0.0, f64::max)
    }

    /// Get the RMS (root mean square) value
    pub fn rms(&self) -> f64 {
        if self.samples.is_empty() {
            return 0.0;
        }
        let sum_sq: f64 = self.samples.iter().map(|s| s * s).sum();
        Libm::<f64>::sqrt(sum_sq / self.size as f64)
    }

    /// Copy from another block
    pub fn copy_from(&mut self, other: &AudioBlock) {
        let len = self.size.min(other.size);
        self.samples[..len].copy_from_slice(&other.samples[..len]);
    }
}

impl Default for AudioBlock {
    fn default() -> Self {
        Self::new(DEFAULT_BLOCK_SIZE)
    }
}

/// Block processor for efficient batch processing
pub struct BlockProcessor {
    /// Processing block size
    block_size: usize,
    /// Sample rate
    sample_rate: f64,
}

impl BlockProcessor {
    /// Create a new block processor
    pub fn new(block_size: usize, sample_rate: f64) -> Self {
        Self {
            block_size,
            sample_rate,
        }
    }

    /// Get the block size
    pub fn block_size(&self) -> usize {
        self.block_size
    }

    /// Get the sample rate
    pub fn sample_rate(&self) -> f64 {
        self.sample_rate
    }

    /// Process a module for one block
    pub fn process_block(
        &self,
        module: &mut dyn GraphModule,
        inputs: &BlockPortValues,
        outputs: &mut BlockPortValues,
    ) {
        module.process_block(inputs, outputs, self.block_size);
    }

    /// Process a module sample-by-sample fallback
    pub fn process_samples(
        &self,
        module: &mut dyn GraphModule,
        inputs: &BlockPortValues,
        outputs: &mut BlockPortValues,
    ) {
        for i in 0..self.block_size {
            let in_frame = inputs.frame(i);
            let mut out_frame = PortValues::new();
            module.tick(&in_frame, &mut out_frame);
            outputs.set_frame(i, out_frame);
        }
    }
}

/// Lazy signal node for deferred evaluation
///
/// Signals are only computed when their value is actually needed,
/// avoiding unnecessary computation.
pub struct LazySignal<F: Fn() -> f64> {
    /// Computation function
    compute: F,
    /// Cached value (if computed)
    cached: Option<f64>,
    /// Whether the cache is valid
    valid: bool,
}

impl<F: Fn() -> f64> LazySignal<F> {
    /// Create a new lazy signal
    pub fn new(compute: F) -> Self {
        Self {
            compute,
            cached: None,
            valid: false,
        }
    }

    /// Get the signal value (computing if necessary)
    pub fn get(&mut self) -> f64 {
        if !self.valid {
            self.cached = Some((self.compute)());
            self.valid = true;
        }
        self.cached.unwrap_or(0.0)
    }

    /// Invalidate the cache (force recomputation on next get)
    pub fn invalidate(&mut self) {
        self.valid = false;
    }

    /// Check if the value has been computed
    pub fn is_computed(&self) -> bool {
        self.valid
    }
}

/// Lazy block signal for deferred block evaluation
pub struct LazyBlock {
    /// Cached block
    block: AudioBlock,
    /// Whether the cache is valid
    valid: bool,
}

impl LazyBlock {
    /// Create a new lazy block with the given size
    pub fn new(size: usize) -> Self {
        Self {
            block: AudioBlock::new(size),
            valid: false,
        }
    }

    /// Get the block, computing if necessary
    pub fn get<F: FnOnce(&mut AudioBlock)>(&mut self, compute: F) -> &AudioBlock {
        if !self.valid {
            compute(&mut self.block);
            self.valid = true;
        }
        &self.block
    }

    /// Get mutable access to the block (marks as valid)
    pub fn get_mut(&mut self) -> &mut AudioBlock {
        self.valid = true;
        &mut self.block
    }

    /// Invalidate the cache
    pub fn invalidate(&mut self) {
        self.valid = false;
    }

    /// Check if computed
    pub fn is_computed(&self) -> bool {
        self.valid
    }
}

/// Stereo audio block pair
#[derive(Clone)]
pub struct StereoBlock {
    /// Left channel
    pub left: AudioBlock,
    /// Right channel
    pub right: AudioBlock,
}

impl StereoBlock {
    /// Create a new stereo block with the given size
    pub fn new(size: usize) -> Self {
        Self {
            left: AudioBlock::new(size),
            right: AudioBlock::new(size),
        }
    }

    /// Get the block size
    pub fn len(&self) -> usize {
        self.left.len()
    }

    /// Check if empty
    pub fn is_empty(&self) -> bool {
        self.left.is_empty()
    }

    /// Clear both channels
    pub fn clear(&mut self) {
        self.left.clear();
        self.right.clear();
    }

    /// Apply gain to both channels
    pub fn apply_gain(&mut self, gain: f64) {
        self.left.mul_scalar(gain);
        self.right.mul_scalar(gain);
    }

    /// Apply stereo panning
    /// pan: -1.0 (full left) to 1.0 (full right)
    pub fn apply_pan(&mut self, pan: f64) {
        let pan_angle = (pan + 1.0) * PI / 4.0;
        let left_gain = Libm::<f64>::cos(pan_angle);
        let right_gain = Libm::<f64>::sin(pan_angle);

        self.left.mul_scalar(left_gain);
        self.right.mul_scalar(right_gain);
    }

    /// Mix another stereo block into this one
    pub fn mix(&mut self, other: &StereoBlock) {
        self.left.add_block(&other.left);
        self.right.add_block(&other.right);
    }

    /// Get the peak level (max of both channels)
    pub fn peak(&self) -> f64 {
        self.left.peak().max(self.right.peak())
    }

    /// Get a stereo sample at the given index
    pub fn get_sample(&self, index: usize) -> (f64, f64) {
        (self.left.get(index), self.right.get(index))
    }

    /// Set a stereo sample at the given index
    pub fn set_sample(&mut self, index: usize, left: f64, right: f64) {
        self.left.set(index, left);
        self.right.set(index, right);
    }
}

impl Default for StereoBlock {
    fn default() -> Self {
        Self::new(DEFAULT_BLOCK_SIZE)
    }
}

/// Ring buffer for delay lines and lookahead.
///
/// # Capacity semantics
///
/// `capacity` is the requested capacity and the maximum valid delay tap:
/// [`read`](Self::read) returns `0.0` for any `delay >= capacity`, so taps
/// beyond the requested capacity stay bounded even though the internal storage
/// may be larger. Internally the backing storage is rounded up to a power of
/// two so the per-sample wrap is a bitmask (`& (len - 1)`) rather than a modulo
/// — the slowest integer op — in the real-time audio path. A requested
/// capacity of `0` is clamped to a single-slot internal buffer so
/// [`write`](Self::write)/[`read`](Self::read) never index an empty `Vec` or
/// divide by zero; `len`/`is_empty` still report the requested capacity.
pub struct RingBuffer {
    buffer: Vec<f64>,
    write_pos: usize,
    /// Requested capacity (public delay-tap bound).
    capacity: usize,
    /// `buffer.len() - 1`; `buffer.len()` is a power of two, so this is a mask.
    mask: usize,
}

impl RingBuffer {
    /// Create a new ring buffer with the given capacity.
    ///
    /// The internal storage is rounded up to a power of two (and to at least
    /// one slot when `capacity == 0`) so wrapping uses a bitmask; the public
    /// capacity reported by [`len`](Self::len) is the value requested here.
    pub fn new(capacity: usize) -> Self {
        // Clamp internal storage to at least one slot so a zero-capacity buffer
        // (nonsensical but constructible via the public API) cannot panic on
        // write()/read(); round up to a power of two for masked wrapping.
        let internal = capacity.max(1).next_power_of_two();
        Self {
            buffer: vec![0.0; internal],
            write_pos: 0,
            capacity,
            mask: internal - 1,
        }
    }

    /// Get the requested buffer capacity (maximum valid delay tap).
    pub fn len(&self) -> usize {
        self.capacity
    }

    /// Check if empty (requested capacity is zero).
    pub fn is_empty(&self) -> bool {
        self.capacity == 0
    }

    /// Write a sample and return the oldest sample.
    pub fn write(&mut self, sample: f64) -> f64 {
        let old = self.buffer[self.write_pos];
        self.buffer[self.write_pos] = sample;
        // Power-of-two mask wrap instead of `% size`.
        self.write_pos = (self.write_pos + 1) & self.mask;
        old
    }

    /// Read a sample with the given delay (in samples).
    ///
    /// Returns `0.0` for `delay >= capacity` (the requested capacity), so taps
    /// beyond the requested capacity are bounded.
    pub fn read(&self, delay: usize) -> f64 {
        if delay >= self.capacity {
            return 0.0;
        }
        // buffer.len() == mask + 1 is a power of two, so masking equals modulo.
        let read_pos = (self.write_pos + self.buffer.len() - delay - 1) & self.mask;
        self.buffer[read_pos]
    }

    /// Read with fractional delay using linear interpolation.
    ///
    /// The interpolated result is passed through
    /// `flush_denorm`: this is the
    /// read side of feedback delay lines, where a decaying loop asymptotes to
    /// subnormals that incur large CPU penalties. Flushing sub-threshold values
    /// to exactly `0.0` keeps the feedback path real-time-safe.
    pub fn read_interp(&self, delay: f64) -> f64 {
        let delay_floor = Libm::<f64>::floor(delay);
        let delay_int = delay_floor as usize;
        let frac = delay - delay_floor;

        let s1 = self.read(delay_int);
        let s2 = self.read(delay_int + 1);

        crate::modules::common::flush_denorm(s1 + frac * (s2 - s1))
    }

    /// Clear the buffer
    pub fn clear(&mut self) {
        self.buffer.fill(0.0);
        self.write_pos = 0;
    }
}

/// Processing context for block-oriented operations
pub struct ProcessContext {
    /// Sample rate
    pub sample_rate: f64,
    /// Block size
    pub block_size: usize,
    /// Current sample position (absolute)
    pub sample_position: u64,
    /// Tempo (BPM) if known
    pub tempo: Option<f64>,
    /// Time signature (numerator, denominator) if known
    pub time_signature: Option<(u32, u32)>,
}

impl ProcessContext {
    /// Create a new processing context
    pub fn new(sample_rate: f64, block_size: usize) -> Self {
        Self {
            sample_rate,
            block_size,
            sample_position: 0,
            tempo: None,
            time_signature: None,
        }
    }

    /// Get the current time in seconds
    pub fn time_seconds(&self) -> f64 {
        self.sample_position as f64 / self.sample_rate
    }

    /// Advance the position by one block
    pub fn advance(&mut self) {
        self.sample_position += self.block_size as u64;
    }

    /// Reset to the beginning
    pub fn reset(&mut self) {
        self.sample_position = 0;
    }
}

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

    #[test]
    fn test_audio_block_basic() {
        let mut block = AudioBlock::new(64);
        assert_eq!(block.len(), 64);

        block.set(0, 1.0);
        block.set(63, -1.0);
        assert_eq!(block.get(0), 1.0);
        assert_eq!(block.get(63), -1.0);
        assert_eq!(block.get(100), 0.0); // Out of bounds
    }

    #[test]
    fn test_audio_block_operations() {
        let mut block = AudioBlock::constant(4, 2.0);

        block.add_scalar(1.0);
        assert_eq!(block.get(0), 3.0);

        block.mul_scalar(2.0);
        assert_eq!(block.get(0), 6.0);
    }

    #[test]
    fn test_audio_block_block_ops() {
        let mut a = AudioBlock::constant(4, 2.0);
        let b = AudioBlock::constant(4, 3.0);

        a.add_block(&b);
        assert_eq!(a.get(0), 5.0);

        a.mul_block(&b);
        assert_eq!(a.get(0), 15.0);
    }

    #[test]
    fn test_audio_block_stats() {
        let block = AudioBlock::from_samples(vec![1.0, -2.0, 1.5, -1.5]);

        assert_eq!(block.peak(), 2.0);
        assert!((block.rms() - 1.541).abs() < 0.01);
    }

    #[test]
    fn test_stereo_block() {
        let mut stereo = StereoBlock::new(4);

        stereo.set_sample(0, 1.0, 0.5);
        let (l, r) = stereo.get_sample(0);
        assert_eq!(l, 1.0);
        assert_eq!(r, 0.5);

        stereo.apply_gain(2.0);
        let (l, r) = stereo.get_sample(0);
        assert_eq!(l, 2.0);
        assert_eq!(r, 1.0);
    }

    #[test]
    fn test_ring_buffer() {
        let mut ring = RingBuffer::new(4);

        // Write and read
        ring.write(1.0);
        ring.write(2.0);
        ring.write(3.0);

        assert_eq!(ring.read(0), 3.0); // Most recent
        assert_eq!(ring.read(1), 2.0);
        assert_eq!(ring.read(2), 1.0);
    }

    #[test]
    fn test_ring_buffer_interp() {
        let mut ring = RingBuffer::new(4);

        ring.write(0.0);
        ring.write(1.0);
        ring.write(2.0);

        // Interpolated read
        let interp = ring.read_interp(0.5);
        assert!((interp - 1.5).abs() < 0.001);
    }

    #[test]
    fn test_lazy_signal() {
        let mut lazy = LazySignal::new(|| 42.0);

        // First get should compute
        assert_eq!(lazy.get(), 42.0);
        assert!(lazy.is_computed());

        // Second get should use cache
        assert_eq!(lazy.get(), 42.0);

        // Invalidate and recompute
        lazy.invalidate();
        assert!(!lazy.is_computed());
    }

    #[test]
    fn test_lazy_block() {
        let mut lazy = LazyBlock::new(4);

        let block = lazy.get(|b| {
            b.fill(5.0);
        });
        assert_eq!(block.get(0), 5.0);
        assert!(lazy.is_computed());

        lazy.invalidate();
        assert!(!lazy.is_computed());
    }

    #[test]
    fn test_process_context() {
        let mut ctx = ProcessContext::new(44100.0, 64);

        assert_eq!(ctx.sample_position, 0);
        assert_eq!(ctx.time_seconds(), 0.0);

        ctx.advance();
        assert_eq!(ctx.sample_position, 64);
        assert!((ctx.time_seconds() - 64.0 / 44100.0).abs() < 0.0001);
    }

    #[test]
    fn test_audio_block_constant() {
        let block = AudioBlock::constant(8, 5.0);
        assert_eq!(block.get(0), 5.0);
        assert_eq!(block.get(7), 5.0);
    }

    #[test]
    fn test_audio_block_from_samples() {
        let samples = vec![1.0, 2.0, 3.0, 4.0];
        let block = AudioBlock::from_samples(samples);
        assert_eq!(block.len(), 4);
        assert_eq!(block.get(0), 1.0);
        assert_eq!(block.get(3), 4.0);
    }

    #[test]
    fn test_audio_block_is_empty() {
        let empty = AudioBlock::new(0);
        assert!(empty.is_empty());

        let non_empty = AudioBlock::new(4);
        assert!(!non_empty.is_empty());
    }

    #[test]
    fn test_audio_block_as_slice() {
        let mut block = AudioBlock::new(4);
        block.fill(2.0);

        let slice = block.as_slice();
        assert_eq!(slice.len(), 4);
        assert_eq!(slice[0], 2.0);

        let mut_slice = block.as_mut_slice();
        mut_slice[0] = 99.0;
        assert_eq!(block.get(0), 99.0);
    }

    #[test]
    fn test_audio_block_add_scalar() {
        let mut block = AudioBlock::from_samples(vec![1.0, 2.0, 3.0, 4.0]);
        block.add_scalar(10.0);
        assert_eq!(block.get(0), 11.0);
        assert_eq!(block.get(3), 14.0);
    }

    #[test]
    fn test_audio_block_add_block() {
        let mut block1 = AudioBlock::from_samples(vec![1.0, 2.0, 3.0, 4.0]);
        let block2 = AudioBlock::from_samples(vec![10.0, 20.0, 30.0, 40.0]);
        block1.add_block(&block2);
        assert_eq!(block1.get(0), 11.0);
        assert_eq!(block1.get(3), 44.0);
    }

    #[test]
    fn test_audio_block_mul_block() {
        let mut block1 = AudioBlock::from_samples(vec![1.0, 2.0, 3.0, 4.0]);
        let block2 = AudioBlock::from_samples(vec![2.0, 2.0, 2.0, 2.0]);
        block1.mul_block(&block2);
        assert_eq!(block1.get(0), 2.0);
        assert_eq!(block1.get(3), 8.0);
    }

    #[test]
    fn test_audio_block_map() {
        let mut block = AudioBlock::from_samples(vec![1.0, 2.0, 3.0, 4.0]);
        block.map(|x| x * 2.0);
        assert_eq!(block.get(0), 2.0);
        assert_eq!(block.get(3), 8.0);
    }

    #[test]
    fn test_audio_block_hard_clip() {
        let mut block = AudioBlock::from_samples(vec![-10.0, -1.0, 0.0, 1.0, 10.0]);
        block.hard_clip(5.0);
        assert_eq!(block.get(0), -5.0);
        assert_eq!(block.get(4), 5.0);
    }

    #[test]
    fn test_audio_block_copy_from() {
        let source = AudioBlock::from_samples(vec![1.0, 2.0, 3.0, 4.0]);
        let mut dest = AudioBlock::new(4);
        dest.copy_from(&source);
        assert_eq!(dest.get(0), 1.0);
        assert_eq!(dest.get(3), 4.0);
    }

    #[test]
    fn test_stereo_block_default() {
        let stereo = StereoBlock::default();
        assert_eq!(stereo.len(), DEFAULT_BLOCK_SIZE);
    }

    #[test]
    fn test_stereo_block_get_set_sample() {
        let mut stereo = StereoBlock::new(4);
        stereo.set_sample(0, 1.0, 2.0);
        let (l, r) = stereo.get_sample(0);
        assert_eq!(l, 1.0);
        assert_eq!(r, 2.0);
    }

    #[test]
    fn test_stereo_block_apply_pan() {
        let mut stereo = StereoBlock::new(4);
        stereo.left.fill(1.0);
        stereo.right.fill(1.0);

        stereo.apply_pan(-1.0); // Full left
        assert!(stereo.left.peak() > stereo.right.peak());
    }

    #[test]
    fn test_stereo_block_mix() {
        let mut stereo1 = StereoBlock::new(4);
        stereo1.left.fill(1.0);
        stereo1.right.fill(1.0);

        let mut stereo2 = StereoBlock::new(4);
        stereo2.left.fill(2.0);
        stereo2.right.fill(2.0);

        stereo1.mix(&stereo2);
        assert_eq!(stereo1.left.get(0), 3.0);
        assert_eq!(stereo1.right.get(0), 3.0);
    }

    #[test]
    fn test_ring_buffer_is_empty() {
        let buf = RingBuffer::new(4);
        assert!(!buf.is_empty());

        let empty_buf = RingBuffer::new(0);
        assert!(empty_buf.is_empty());
    }

    #[test]
    fn test_ring_buffer_read_interp() {
        let mut buf = RingBuffer::new(4);
        buf.write(1.0);
        buf.write(3.0);

        // Interpolated read at 0.5 should be between 1.0 and 3.0
        let interp = buf.read_interp(0.5);
        assert!(interp > 1.0 && interp < 3.0);
    }

    #[test]
    fn test_ring_buffer_clear() {
        let mut buf = RingBuffer::new(4);
        buf.write(10.0);
        buf.write(20.0);

        buf.clear();
        assert_eq!(buf.read(0), 0.0);
        assert_eq!(buf.read(1), 0.0);
    }

    #[test]
    fn test_block_processor_process_samples() {
        use crate::modules::Vco;
        use crate::port::BlockPortValues;

        let processor = BlockProcessor::new(64, 44100.0);

        let mut vco = Vco::new(44100.0);
        let inputs = BlockPortValues::new(64);
        let mut outputs = BlockPortValues::new(64);

        processor.process_samples(&mut vco, &inputs, &mut outputs);
        assert_eq!(processor.block_size(), 64);
    }

    #[test]
    fn test_lazy_block_get_mut() {
        let mut lazy = LazyBlock::new(4);
        let block = lazy.get_mut();
        block.fill(42.0);
        assert!(lazy.is_computed());
        assert_eq!(lazy.get(|_| {}).get(0), 42.0);
    }

    #[test]
    fn test_process_context_reset() {
        let mut ctx = ProcessContext::new(44100.0, 64);
        ctx.advance();
        ctx.advance();

        ctx.reset();
        assert_eq!(ctx.sample_position, 0);
    }

    // Q061: a zero-capacity ring buffer must not panic on write/read.
    #[test]
    fn test_ring_buffer_zero_capacity_no_panic() {
        let mut ring = RingBuffer::new(0);
        assert!(ring.is_empty());
        assert_eq!(ring.len(), 0);
        // Previously: OOB index into an empty Vec + modulo-by-zero.
        let old = ring.write(1.0);
        assert_eq!(old, 0.0);
        assert_eq!(ring.read(0), 0.0); // delay >= capacity(0) -> bounded to 0.0
        assert_eq!(ring.read_interp(0.0), 0.0);
    }

    // Q060: power-of-two internal storage preserves read/write index semantics
    // and bounds delay taps to the requested capacity.
    #[test]
    fn test_ring_buffer_power_of_two_wrap_semantics() {
        // Requested capacity 3 rounds internal storage up to 4.
        let mut ring = RingBuffer::new(3);
        assert_eq!(ring.len(), 3);
        for v in [1.0, 2.0, 3.0, 4.0, 5.0] {
            ring.write(v);
        }
        // Most-recent-first within the requested capacity.
        assert_eq!(ring.read(0), 5.0);
        assert_eq!(ring.read(1), 4.0);
        assert_eq!(ring.read(2), 3.0);
        // Delay at or beyond the requested capacity stays bounded to 0.0.
        assert_eq!(ring.read(3), 0.0);
        assert_eq!(ring.read(100), 0.0);
    }

    // Q057: the feedback read path flushes sub-threshold values to exactly 0.0.
    #[test]
    fn test_ring_buffer_read_interp_flushes_denormals() {
        let mut ring = RingBuffer::new(8);

        // A single sub-threshold value read back must flush to exactly 0.0.
        ring.write(1e-300); // far below the 1e-20 flush threshold
        ring.write(1e-300);
        assert_eq!(ring.read_interp(0.5), 0.0);

        // A decaying feedback loop must settle at exactly 0.0 rather than
        // lingering at a tiny (denormal-prone) value. Without flushing, `out`
        // would be ~0.5^200 (a nonzero normal number), not 0.0.
        ring.clear();
        ring.write(1.0);
        let feedback = 0.5;
        let mut out = 0.0;
        for _ in 0..200 {
            let delayed = ring.read_interp(0.0);
            out = delayed * feedback;
            ring.write(out);
        }
        assert_eq!(out, 0.0, "decaying feedback loop must reach exactly 0.0");
    }

    // Q062: the SIMD block ops must agree bit-for-bit with a scalar reference
    // over random-length, non-multiple-of-4 blocks (exercising the vectorized
    // chunks and the scalar remainder). Gated on `simd` so the AudioBlock
    // methods here are the vectorized `wide::f64x4` path.
    #[cfg(feature = "simd")]
    #[test]
    fn test_simd_scalar_equivalence_nonmultiple_of_four() {
        use crate::rng::Rng;

        let mut rng = Rng::from_seed(0xC0FFEE);
        for &len in &[1usize, 2, 3, 5, 6, 7, 9, 13, 17, 31, 63, 100, 127] {
            let data: Vec<f64> = (0..len).map(|_| rng.next_f64_bipolar()).collect();
            let other: Vec<f64> = (0..len).map(|_| rng.next_f64_bipolar()).collect();
            let s = rng.next_f64_bipolar();

            // add_scalar
            let mut blk = AudioBlock::from_samples(data.clone());
            blk.add_scalar(s);
            let expect: Vec<f64> = data.iter().map(|x| x + s).collect();
            assert_eq!(blk.as_slice(), expect.as_slice(), "add_scalar len={len}");

            // mul_scalar
            let mut blk = AudioBlock::from_samples(data.clone());
            blk.mul_scalar(s);
            let expect: Vec<f64> = data.iter().map(|x| x * s).collect();
            assert_eq!(blk.as_slice(), expect.as_slice(), "mul_scalar len={len}");

            // add_block
            let mut blk = AudioBlock::from_samples(data.clone());
            blk.add_block(&AudioBlock::from_samples(other.clone()));
            let expect: Vec<f64> = data.iter().zip(&other).map(|(a, b)| a + b).collect();
            assert_eq!(blk.as_slice(), expect.as_slice(), "add_block len={len}");

            // mul_block
            let mut blk = AudioBlock::from_samples(data.clone());
            blk.mul_block(&AudioBlock::from_samples(other.clone()));
            let expect: Vec<f64> = data.iter().zip(&other).map(|(a, b)| a * b).collect();
            assert_eq!(blk.as_slice(), expect.as_slice(), "mul_block len={len}");
        }
    }
}