audio-blocks 0.7.0

Traits to handle all audio data layouts in real-time processes
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
use rtsan_standalone::nonblocking;

use crate::{
    AudioBlock, AudioBlockMut, BlockLayout, Sample,
    mono::{MonoView, MonoViewMut},
};

pub trait AudioBlockOps<S: Sample> {
    /// Mix all channels to mono by averaging them.
    /// Only processes `min(src_frames, dst_frames)` frames.
    /// Returns `None` if all frames were processed (exact match),
    /// or `Some(frames_processed)` if a partial mix occurred.
    fn mix_to_mono(&self, dest: &mut MonoViewMut<S>) -> Option<usize>
    where
        S: std::ops::AddAssign + std::ops::Div<Output = S> + From<u16>;
    /// Mix all channels to mono by averaging them.
    /// Panics if source and destination don't have the same number of frames.
    fn mix_to_mono_exact(&self, dest: &mut MonoViewMut<S>)
    where
        S: std::ops::AddAssign + std::ops::Div<Output = S> + From<u16>;
    /// Copy a specific channel to a mono buffer.
    /// Only copies `min(src_frames, dst_frames)` frames.
    /// Returns `None` if all frames were copied (exact match),
    /// or `Some(frames_copied)` if a partial copy occurred.
    fn copy_channel_to_mono(&self, dest: &mut MonoViewMut<S>, channel: u16) -> Option<usize>;
    /// Copy a specific channel to a mono buffer.
    /// Panics if source and destination don't have the same number of frames.
    fn copy_channel_to_mono_exact(&self, dest: &mut MonoViewMut<S>, channel: u16);
}

pub trait AudioBlockOpsMut<S: Sample> {
    /// Copy samples from source block into destination.
    /// Only copies `min(src, dst)` channels and frames.
    /// Returns `None` if the entire block was copied (exact match),
    /// or `Some((channels_copied, frames_copied))` if a partial copy occurred.
    /// Never panics - safely handles mismatched block sizes.
    fn copy_from_block(&mut self, block: &impl AudioBlock<S>) -> Option<(u16, usize)>;
    /// Copy samples from source block, requiring exact size match.
    /// Panics if source and destination don't have identical channels and frames.
    fn copy_from_block_exact(&mut self, block: &impl AudioBlock<S>);
    /// Copy a mono block to all channels of this block.
    /// Only copies `min(src_frames, dst_frames)` frames.
    /// Returns `None` if all frames were copied (exact match),
    /// or `Some(frames_copied)` if a partial copy occurred.
    fn copy_mono_to_all_channels(&mut self, mono: &MonoView<S>) -> Option<usize>;
    /// Copy a mono block to all channels of this block.
    /// Panics if blocks don't have the same number of frames.
    fn copy_mono_to_all_channels_exact(&mut self, mono: &MonoView<S>);
    /// Gives access to all samples in the block.
    fn for_each(&mut self, f: impl FnMut(&mut S));
    /// Gives access to all samples in the block while supplying the information
    /// about which channel and frame number the sample is stored in.
    fn enumerate(&mut self, f: impl FnMut(u16, usize, &mut S));
    /// Iterate over all allocated samples using fast linear buffer iteration.
    ///
    /// This iterates over `num_frames_allocated()` samples, not just `num_frames()`.
    /// It uses cache-friendly linear access over the underlying storage, which is
    /// significantly faster than [`for_each`](AudioBlockOpsMut::for_each) for large buffers when the visible
    /// window is close to the allocated size.
    ///
    /// # Performance Note
    ///
    /// Only use this when `num_frames()` is close to `num_frames_allocated()`.
    /// If the buffer has been resized dramatically (e.g., `set_visible()` to half
    /// the allocation), [`for_each`](AudioBlockOpsMut::for_each) will be faster as it respects the visible window.
    fn for_each_allocated(&mut self, f: impl FnMut(&mut S));
    /// Iterate over all allocated samples with indices using fast linear buffer iteration.
    ///
    /// Like [`for_each_allocated`](AudioBlockOpsMut::for_each_allocated), this iterates over the entire allocated buffer
    /// for cache-friendly linear access. Only faster than [`enumerate`](AudioBlockOpsMut::enumerate) when the
    /// visible window is close to the allocated size.
    fn enumerate_allocated(&mut self, f: impl FnMut(u16, usize, &mut S));
    /// Sets all samples in the block to the specified value.
    /// Iterates over the entire allocated buffer for efficiency.
    fn fill_with(&mut self, sample: S);
    /// Sets all samples in the block to the default value (zero for numeric types).
    /// Iterates over the entire allocated buffer for efficiency.
    fn clear(&mut self)
    where
        S: Default;
    /// Applies gain to all samples by multiplying each sample.
    /// Iterates over the entire allocated buffer for efficiency.
    fn gain(&mut self, gain: S)
    where
        S: std::ops::Mul<Output = S> + Copy;
}

impl<S: Sample, B: AudioBlock<S>> AudioBlockOps<S> for B {
    #[nonblocking]
    fn mix_to_mono(&self, dest: &mut MonoViewMut<S>) -> Option<usize>
    where
        S: std::ops::AddAssign + std::ops::Div<Output = S> + From<u16>,
    {
        let frames = self.num_frames().min(dest.num_frames());
        let num_channels = S::from(self.num_channels());

        for frame in 0..frames {
            let mut sum = *self.frame_iter(frame).next().unwrap();
            for sample in self.frame_iter(frame).skip(1) {
                sum += *sample;
            }
            *dest.sample_mut(frame) = sum / num_channels;
        }

        if frames == self.num_frames() {
            None
        } else {
            Some(frames)
        }
    }

    #[nonblocking]
    fn mix_to_mono_exact(&self, dest: &mut MonoViewMut<S>)
    where
        S: std::ops::AddAssign + std::ops::Div<Output = S> + From<u16>,
    {
        assert_eq!(self.num_frames(), dest.num_frames());

        let num_channels = S::from(self.num_channels());

        for frame in 0..self.num_frames() {
            let mut sum = *self.frame_iter(frame).next().unwrap();
            for sample in self.frame_iter(frame).skip(1) {
                sum += *sample;
            }
            *dest.sample_mut(frame) = sum / num_channels;
        }
    }

    #[nonblocking]
    fn copy_channel_to_mono(&self, dest: &mut MonoViewMut<S>, channel: u16) -> Option<usize> {
        let frames = self.num_frames().min(dest.num_frames());
        for (o, i) in dest
            .raw_data_mut()
            .iter_mut()
            .take(frames)
            .zip(self.channel_iter(channel).take(frames))
        {
            *o = *i;
        }

        if frames == self.num_frames() {
            None
        } else {
            Some(frames)
        }
    }

    #[nonblocking]
    fn copy_channel_to_mono_exact(&self, dest: &mut MonoViewMut<S>, channel: u16) {
        assert_eq!(self.num_frames(), dest.num_frames());
        for (o, i) in dest
            .raw_data_mut()
            .iter_mut()
            .zip(self.channel_iter(channel))
        {
            *o = *i;
        }
    }
}

impl<S: Sample, B: AudioBlockMut<S>> AudioBlockOpsMut<S> for B {
    #[nonblocking]
    fn copy_from_block(&mut self, block: &impl AudioBlock<S>) -> Option<(u16, usize)> {
        let channels = self.num_channels().min(block.num_channels());
        let frames = self.num_frames().min(block.num_frames());

        for (this_channel, other_channel) in self.channels_iter_mut().zip(block.channels_iter()) {
            for (sample_mut, sample) in this_channel.zip(other_channel) {
                *sample_mut = *sample;
            }
        }

        if channels == block.num_channels() && frames == block.num_frames() {
            None
        } else {
            Some((channels, frames))
        }
    }

    #[nonblocking]
    fn copy_from_block_exact(&mut self, block: &impl AudioBlock<S>) {
        assert_eq!(block.num_channels(), self.num_channels());
        assert_eq!(block.num_frames(), self.num_frames());
        for ch in 0..self.num_channels() {
            for (sample_mut, sample) in self.channel_iter_mut(ch).zip(block.channel_iter(ch)) {
                *sample_mut = *sample;
            }
        }
    }

    #[nonblocking]
    fn copy_mono_to_all_channels(&mut self, mono: &MonoView<S>) -> Option<usize> {
        let frames = mono.num_frames().min(self.num_frames());
        for channel in self.channels_iter_mut() {
            for (sample_mut, sample) in channel.take(frames).zip(mono.samples().iter().take(frames))
            {
                *sample_mut = *sample;
            }
        }

        if frames == mono.num_frames() {
            None
        } else {
            Some(frames)
        }
    }

    #[nonblocking]
    fn copy_mono_to_all_channels_exact(&mut self, mono: &MonoView<S>) {
        assert_eq!(mono.num_frames(), self.num_frames());
        for channel in self.channels_iter_mut() {
            for (sample_mut, sample) in channel.zip(mono.samples()) {
                *sample_mut = *sample;
            }
        }
    }

    #[nonblocking]
    fn for_each(&mut self, mut f: impl FnMut(&mut S)) {
        // below 8 channels it is faster to always go per channel
        if self.num_channels() < 8 {
            for channel in self.channels_iter_mut() {
                channel.for_each(&mut f);
            }
        } else {
            match self.layout() {
                BlockLayout::Sequential | BlockLayout::Planar => {
                    for channel in self.channels_iter_mut() {
                        channel.for_each(&mut f);
                    }
                }
                BlockLayout::Interleaved => {
                    for frame in 0..self.num_frames() {
                        self.frame_iter_mut(frame).for_each(&mut f);
                    }
                }
            }
        }
    }

    #[nonblocking]
    fn enumerate(&mut self, mut f: impl FnMut(u16, usize, &mut S)) {
        // below 8 channels it is faster to always go per channel
        if self.num_channels() < 8 {
            for (ch, channel) in self.channels_iter_mut().enumerate() {
                for (fr, sample) in channel.enumerate() {
                    f(ch as u16, fr, sample)
                }
            }
        } else {
            match self.layout() {
                BlockLayout::Interleaved => {
                    for (fr, frame) in self.frames_iter_mut().enumerate() {
                        for (ch, sample) in frame.enumerate() {
                            f(ch as u16, fr, sample)
                        }
                    }
                }
                BlockLayout::Planar | BlockLayout::Sequential => {
                    for (ch, channel) in self.channels_iter_mut().enumerate() {
                        for (fr, sample) in channel.enumerate() {
                            f(ch as u16, fr, sample)
                        }
                    }
                }
            }
        }
    }

    #[nonblocking]
    fn for_each_allocated(&mut self, mut f: impl FnMut(&mut S)) {
        match self.layout() {
            BlockLayout::Interleaved => self
                .as_interleaved_view_mut()
                .expect("Layout is interleaved")
                .raw_data_mut()
                .iter_mut()
                .for_each(&mut f),
            BlockLayout::Planar => self
                .as_planar_view_mut()
                .expect("Layout is planar")
                .raw_data_mut()
                .iter_mut()
                .for_each(|c| c.as_mut().iter_mut().for_each(&mut f)),
            BlockLayout::Sequential => self
                .as_sequential_view_mut()
                .expect("Layout is sequential")
                .raw_data_mut()
                .iter_mut()
                .for_each(&mut f),
        }
    }

    #[nonblocking]
    fn enumerate_allocated(&mut self, mut f: impl FnMut(u16, usize, &mut S)) {
        match self.layout() {
            BlockLayout::Interleaved => {
                let num_frames = self.num_frames_allocated();
                self.as_interleaved_view_mut()
                    .expect("Layout is interleaved")
                    .raw_data_mut()
                    .iter_mut()
                    .enumerate()
                    .for_each(|(i, sample)| {
                        let channel = i % num_frames;
                        let frame = i / num_frames;
                        f(channel as u16, frame, sample)
                    });
            }
            BlockLayout::Planar => self
                .as_planar_view_mut()
                .expect("Layout is planar")
                .raw_data_mut()
                .iter_mut()
                .enumerate()
                .for_each(|(ch, v)| {
                    v.as_mut()
                        .iter_mut()
                        .enumerate()
                        .for_each(|(frame, sample)| f(ch as u16, frame, sample))
                }),
            BlockLayout::Sequential => {
                let num_frames = self.num_frames_allocated();
                self.as_sequential_view_mut()
                    .expect("Layout is sequential")
                    .raw_data_mut()
                    .iter_mut()
                    .enumerate()
                    .for_each(|(i, sample)| {
                        let channel = i / num_frames;
                        let frame = i % num_frames;
                        f(channel as u16, frame, sample)
                    });
            }
        }
    }

    #[nonblocking]
    fn fill_with(&mut self, sample: S) {
        self.for_each_allocated(|v| *v = sample);
    }

    #[nonblocking]
    fn clear(&mut self)
    where
        S: Default,
    {
        self.fill_with(S::default());
    }

    #[nonblocking]
    fn gain(&mut self, gain: S)
    where
        S: std::ops::Mul<Output = S> + Copy,
    {
        self.for_each_allocated(|v| *v = *v * gain);
    }
}

#[cfg(test)]
mod tests {
    use rtsan_standalone::no_sanitize_realtime;

    use crate::{
        interleaved::InterleavedViewMut,
        sequential::{SequentialView, SequentialViewMut},
    };

    use super::*;

    #[test]
    fn test_copy_from_block_dest_larger() {
        // Destination is larger than source - source is fully copied
        let mut data = [0.0; 15];
        let mut block = InterleavedViewMut::from_slice(&mut data, 3);
        let view = SequentialView::from_slice(&[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0], 2);

        // Source fits entirely, so returns None (exact match from source perspective)
        let result = block.copy_from_block(&view);
        assert_eq!(result, None);

        // Buffer size should NOT change
        assert_eq!(block.num_channels(), 3);
        assert_eq!(block.num_frames(), 5);

        // First 2 channels should have the data
        // Only check first 4 frames that were copied
        assert_eq!(
            block.channel_iter(0).take(4).copied().collect::<Vec<_>>(),
            vec![0.0, 1.0, 2.0, 3.0]
        );
        assert_eq!(
            block.channel_iter(1).take(4).copied().collect::<Vec<_>>(),
            vec![4.0, 5.0, 6.0, 7.0]
        );
    }

    #[test]
    fn test_copy_from_block_exact_match_returns_none() {
        // Source and destination are the same size - should return None
        let mut data = [0.0; 8];
        let mut block = InterleavedViewMut::from_slice(&mut data, 2);
        let view = SequentialView::from_slice(&[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0], 2);

        let result = block.copy_from_block(&view);
        assert_eq!(result, None); // Exact match returns None
    }

    #[test]
    fn test_copy_from_block_clamps_to_dest_size() {
        // Source is larger than destination - partial copy
        let mut data = [0.0; 4]; // 2 channels, 2 frames
        let mut block = SequentialViewMut::from_slice(&mut data, 2);
        let view = SequentialView::from_slice(&[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0], 2);

        let result = block.copy_from_block(&view);
        assert_eq!(result, Some((2, 2))); // Only 2 frames copied
        // Only first 2 frames copied from each channel
        assert_eq!(
            block.channel_iter(0).copied().collect::<Vec<_>>(),
            vec![0.0, 1.0]
        );
        assert_eq!(
            block.channel_iter(1).copied().collect::<Vec<_>>(),
            vec![4.0, 5.0]
        );
    }

    #[test]
    fn test_copy_from_block_source_larger_channels() {
        // Source has more channels than destination
        let mut data = [0.0; 4]; // 1 channel, 4 frames
        let mut block = SequentialViewMut::from_slice(&mut data, 1);
        let view = SequentialView::from_slice(&[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0], 2);

        let result = block.copy_from_block(&view);
        assert_eq!(result, Some((1, 4))); // Only 1 channel copied

        assert_eq!(
            block.channel_iter(0).copied().collect::<Vec<_>>(),
            vec![0.0, 1.0, 2.0, 3.0]
        );
    }

    #[test]
    fn test_copy_from_block_exact() {
        let mut data = [0.0; 8];
        let mut block = InterleavedViewMut::from_slice(&mut data, 2);
        let view = SequentialView::from_slice(&[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0], 2);
        block.copy_from_block_exact(&view);

        assert_eq!(block.num_channels(), 2);
        assert_eq!(block.num_frames(), 4);

        // First 2 channels should have the data
        assert_eq!(
            block.channel_iter(0).take(4).copied().collect::<Vec<_>>(),
            vec![0.0, 1.0, 2.0, 3.0]
        );
        assert_eq!(
            block.channel_iter(1).take(4).copied().collect::<Vec<_>>(),
            vec![4.0, 5.0, 6.0, 7.0]
        );
        // Third channel should still be zeros (only visible frames matter for iterator)
        // Note: channel_iter respects num_visible, so we check allocated directly
    }

    #[test]
    #[should_panic]
    #[no_sanitize_realtime]
    fn test_copy_from_block_exact_wrong_channels() {
        let mut data = [0.0; 12];
        let mut block = InterleavedViewMut::from_slice(&mut data, 3);
        let view = SequentialView::from_slice(&[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0], 2);
        block.copy_from_block_exact(&view);
    }

    #[test]
    #[should_panic]
    #[no_sanitize_realtime]
    fn test_copy_from_block_exact_wrong_frames() {
        let mut data = [0.0; 10];
        let mut block = InterleavedViewMut::from_slice(&mut data, 2);
        let view = SequentialView::from_slice(&[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0], 2);
        block.copy_from_block_exact(&view);
    }

    #[test]
    fn test_for_each() {
        // Test that for_each processes all visible samples
        let mut data = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0];
        let mut block = SequentialViewMut::from_slice(&mut data, 2);

        let mut count = 0;
        block.for_each(|v| {
            *v *= 2.0;
            count += 1;
        });
        assert_eq!(count, 8); // All 8 samples processed
        assert_eq!(data, [0.0, 2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0]);

        // Test enumerate provides valid channel/frame indices
        let mut data = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0];
        let mut block = InterleavedViewMut::from_slice(&mut data, 2);

        let mut seen = Vec::with_capacity(8);
        block.enumerate(|c, f, v| {
            seen.push((c, f, *v));
        });

        // Should have seen all 8 samples
        assert_eq!(seen.len(), 8);

        // Verify each sample appears exactly once with valid indices
        let mut values: Vec<f32> = seen.iter().map(|(_, _, v)| *v).collect();
        values.sort_by(|a, b| a.partial_cmp(b).unwrap());
        assert_eq!(values, vec![0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]);

        // Verify channel indices are valid (0 or 1 for 2-channel block)
        for (c, _, _) in &seen {
            assert!(*c == 0 || *c == 1);
        }

        // Verify frame indices are valid (0-3 for 4-frame block)
        for (_, f, _) in &seen {
            assert!(*f < 4);
        }
    }

    #[test]
    fn test_for_each_allocated() {
        // Test that for_each_allocated iterates over the entire allocated buffer
        let mut data = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0];
        let mut block = SequentialViewMut::from_slice(&mut data, 2);

        // Resize to half, but for_each_allocated should still process all
        block.set_visible(2, 2);

        let mut count = 0;
        block.for_each_allocated(|v| {
            *v *= 2.0;
            count += 1;
        });

        // Should have processed all 8 samples (4 frames * 2 channels), not just 4
        assert_eq!(count, 8);
        // All values should be doubled
        assert_eq!(data, [0.0, 2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0]);
    }

    #[test]
    fn test_enumerate_allocated() {
        // Test that enumerate_allocated provides correct indices for entire buffer
        let mut data = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0];
        let mut block = SequentialViewMut::from_slice(&mut data, 2);

        // Resize to half
        block.set_visible(2, 2);

        let mut seen = Vec::with_capacity(8);
        block.enumerate_allocated(|c, f, v| {
            seen.push((c, f, *v));
            *v = 0.0;
        });

        // Should have seen all 8 samples with correct (channel, frame) indices
        assert_eq!(seen.len(), 8);
        assert_eq!(seen[0], (0, 0, 0.0));
        assert_eq!(seen[1], (0, 1, 1.0));
        assert_eq!(seen[2], (0, 2, 2.0));
        assert_eq!(seen[3], (0, 3, 3.0));
        assert_eq!(seen[4], (1, 0, 4.0));
        assert_eq!(seen[5], (1, 1, 5.0));
        assert_eq!(seen[6], (1, 2, 6.0));
        assert_eq!(seen[7], (1, 3, 7.0));

        // All values should be zeroed
        assert_eq!(data, [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]);
    }

    #[test]
    fn test_clear() {
        let mut data = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0];
        let mut block = SequentialViewMut::from_slice(&mut data, 2);

        block.fill_with(1.0);

        assert_eq!(
            block.channel_iter(0).copied().collect::<Vec<_>>(),
            vec![1.0, 1.0, 1.0, 1.0]
        );
        assert_eq!(
            block.channel_iter(1).copied().collect::<Vec<_>>(),
            vec![1.0, 1.0, 1.0, 1.0]
        );

        block.clear();

        assert_eq!(
            block.channel_iter(0).copied().collect::<Vec<_>>(),
            vec![0.0, 0.0, 0.0, 0.0]
        );
        assert_eq!(
            block.channel_iter(1).copied().collect::<Vec<_>>(),
            vec![0.0, 0.0, 0.0, 0.0]
        );
    }

    #[test]
    fn test_mix_to_mono() {
        use crate::mono::MonoViewMut;

        let data = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
        let block = SequentialView::from_slice(&data, 2);

        let mut mono_data = [0.0; 4];
        let mut mono = MonoViewMut::from_slice(&mut mono_data);

        block.mix_to_mono(&mut mono);

        assert_eq!(mono.num_frames(), 4);
        assert_eq!(
            mono.samples().iter().copied().collect::<Vec<_>>(),
            vec![3.0, 4.0, 5.0, 6.0] // (1+5)/2, (2+6)/2, (3+7)/2, (4+8)/2
        );
    }

    #[test]
    fn test_copy_mono_to_all_channels() {
        use crate::mono::MonoView;

        let mono_data = [1.0, 2.0, 3.0, 4.0];
        let mono = MonoView::from_slice(&mono_data);

        let mut data = [0.0; 12];
        let mut block = SequentialViewMut::from_slice(&mut data, 3);

        block.copy_mono_to_all_channels(&mono);

        assert_eq!(
            block.channel_iter(0).copied().collect::<Vec<_>>(),
            vec![1.0, 2.0, 3.0, 4.0]
        );
        assert_eq!(
            block.channel_iter(1).copied().collect::<Vec<_>>(),
            vec![1.0, 2.0, 3.0, 4.0]
        );
        assert_eq!(
            block.channel_iter(2).copied().collect::<Vec<_>>(),
            vec![1.0, 2.0, 3.0, 4.0]
        );
    }

    #[test]
    fn test_mix_to_mono_flexible_dest_smaller() {
        use crate::mono::MonoViewMut;

        // Source has 4 frames, destination has 2 frames
        // Should only process 2 frames, returns Some(frames_processed)
        let data = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
        let block = SequentialView::from_slice(&data, 2);

        let mut mono_data = [0.0; 2];
        let mut mono = MonoViewMut::from_slice(&mut mono_data);

        let result = block.mix_to_mono(&mut mono);
        assert_eq!(result, Some(2)); // Partial mix

        assert_eq!(mono.num_frames(), 2);
        assert_eq!(
            mono.samples().iter().copied().collect::<Vec<_>>(),
            vec![3.0, 4.0] // (1+5)/2, (2+6)/2 - only first 2 frames
        );
    }

    #[test]
    fn test_mix_to_mono_flexible_self_smaller() {
        use crate::mono::MonoViewMut;

        // Source has 2 frames, destination has 4 frames
        // Should only process 2 frames, returns None (exact match for source)
        let data = [1.0, 2.0, 3.0, 4.0];
        let block = SequentialView::from_slice(&data, 2);

        let mut mono_data = [99.0; 4];
        let mut mono = MonoViewMut::from_slice(&mut mono_data);

        let result = block.mix_to_mono(&mut mono);
        assert_eq!(result, None); // Source was fully processed

        assert_eq!(mono.num_frames(), 4);
        // Only first 2 frames should be mixed
        assert_eq!(
            mono.samples().iter().copied().collect::<Vec<_>>(),
            vec![2.0, 3.0, 99.0, 99.0] // (1+3)/2, (2+4)/2, then unchanged
        );
    }

    #[test]
    fn test_mix_to_mono_exact() {
        use crate::mono::MonoViewMut;

        let data = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
        let block = SequentialView::from_slice(&data, 2);

        let mut mono_data = [0.0; 4];
        let mut mono = MonoViewMut::from_slice(&mut mono_data);

        block.mix_to_mono_exact(&mut mono);

        assert_eq!(mono.num_frames(), 4);
        assert_eq!(
            mono.samples().iter().copied().collect::<Vec<_>>(),
            vec![3.0, 4.0, 5.0, 6.0] // (1+5)/2, (2+6)/2, (3+7)/2, (4+8)/2
        );
    }

    #[test]
    #[should_panic]
    #[no_sanitize_realtime]
    fn test_mix_to_mono_exact_wrong_frames() {
        use crate::mono::MonoViewMut;

        let data = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
        let block = SequentialView::from_slice(&data, 2);

        let mut mono_data = [0.0; 2]; // Wrong size - only 2 frames instead of 4
        let mut mono = MonoViewMut::from_slice(&mut mono_data);

        block.mix_to_mono_exact(&mut mono);
    }

    #[test]
    fn test_copy_channel_to_mono_flexible_dest_smaller() {
        use crate::mono::MonoViewMut;

        // Source has 4 frames, destination has 2 frames
        // Should only copy 2 frames, returns Some(frames_copied)
        let data = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
        let block = SequentialView::from_slice(&data, 2);

        let mut mono_data = [0.0; 2];
        let mut mono = MonoViewMut::from_slice(&mut mono_data);

        let result = block.copy_channel_to_mono(&mut mono, 0);
        assert_eq!(result, Some(2)); // Partial copy

        assert_eq!(mono.num_frames(), 2);
        assert_eq!(
            mono.samples().iter().copied().collect::<Vec<_>>(),
            vec![1.0, 2.0] // Only first 2 frames from channel 0
        );
    }

    #[test]
    fn test_copy_channel_to_mono_flexible_self_smaller() {
        use crate::mono::MonoViewMut;

        // Source has 2 frames, destination has 4 frames allocated
        // Should only copy 2 frames, returns None (source fully copied)
        let data = [1.0, 2.0, 3.0, 4.0];
        let block = SequentialView::from_slice(&data, 2);

        let mut mono_data = [0.0; 4];
        let mut mono = MonoViewMut::from_slice(&mut mono_data);

        let result = block.copy_channel_to_mono(&mut mono, 1);
        assert_eq!(result, None); // Source fully copied

        assert_eq!(mono.num_frames(), 4);
        // Only first 2 frames should be copied from channel 1
        assert_eq!(
            mono.samples().iter().copied().collect::<Vec<_>>(),
            vec![3.0, 4.0, 0.0, 0.0]
        );
    }

    #[test]
    fn test_copy_channel_to_mono_exact_match_returns_none() {
        use crate::mono::MonoViewMut;

        // Source and destination have the same size
        // Should copy all frames and return None
        let data = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
        let block = SequentialView::from_slice(&data, 2);

        let mut mono_data = [0.0; 4];
        let mut mono = MonoViewMut::from_slice(&mut mono_data);

        let result = block.copy_channel_to_mono(&mut mono, 0);
        assert_eq!(result, None); // Exact match

        assert_eq!(
            mono.samples().iter().copied().collect::<Vec<_>>(),
            vec![1.0, 2.0, 3.0, 4.0]
        );
    }

    #[test]
    fn test_copy_channel_to_mono_exact() {
        use crate::mono::MonoViewMut;

        let data = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
        let block = SequentialView::from_slice(&data, 2);

        let mut mono_data = [0.0; 4];
        let mut mono = MonoViewMut::from_slice(&mut mono_data);

        block.copy_channel_to_mono_exact(&mut mono, 1);

        assert_eq!(
            mono.samples().iter().copied().collect::<Vec<_>>(),
            vec![5.0, 6.0, 7.0, 8.0]
        );
    }

    #[test]
    #[should_panic]
    #[no_sanitize_realtime]
    fn test_copy_channel_to_mono_exact_wrong_frames() {
        use crate::mono::MonoViewMut;

        let data = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];
        let block = SequentialView::from_slice(&data, 2);

        let mut mono_data = [0.0; 2]; // Wrong size
        let mut mono = MonoViewMut::from_slice(&mut mono_data);

        block.copy_channel_to_mono_exact(&mut mono, 0);
    }

    #[test]
    fn test_copy_mono_to_all_channels_flexible_dest_smaller() {
        use crate::mono::MonoView;

        // Mono has 4 frames, destination has only 2 frames
        // Should only copy 2 frames, returns Some(frames_copied)
        let mono_data = [1.0, 2.0, 3.0, 4.0];
        let mono = MonoView::from_slice(&mono_data);

        let mut data = [0.0; 2]; // 1 channel, 2 frames
        let mut block = SequentialViewMut::from_slice(&mut data, 1);

        let result = block.copy_mono_to_all_channels(&mono);
        assert_eq!(result, Some(2)); // Partial copy

        assert_eq!(
            block.channel_iter(0).copied().collect::<Vec<_>>(),
            vec![1.0, 2.0] // Only first 2 frames copied
        );
    }

    #[test]
    fn test_copy_mono_to_all_channels_flexible_self_smaller() {
        use crate::mono::MonoView;

        // Mono has 2 frames, destination has 4 frames
        // Should only copy 2 frames, returns None (mono fully copied)
        let mono_data = [1.0, 2.0];
        let mono = MonoView::from_slice(&mono_data);

        let mut data = [0.0; 8]; // 2 channels, 4 frames
        let mut block = SequentialViewMut::from_slice(&mut data, 2);

        let result = block.copy_mono_to_all_channels(&mono);
        assert_eq!(result, None); // Mono fully copied

        assert_eq!(
            block.channel_iter(0).copied().collect::<Vec<_>>(),
            vec![1.0, 2.0, 0.0, 0.0] // Only first 2 frames copied
        );
        assert_eq!(
            block.channel_iter(1).copied().collect::<Vec<_>>(),
            vec![1.0, 2.0, 0.0, 0.0] // Only first 2 frames copied
        );
    }

    #[test]
    fn test_copy_mono_to_all_channels_exact_match_returns_none() {
        use crate::mono::MonoView;

        // Mono and destination have the same size
        // Should copy all frames and return None
        let mono_data = [1.0, 2.0, 3.0, 4.0];
        let mono = MonoView::from_slice(&mono_data);

        let mut data = [0.0; 12]; // 3 channels, 4 frames
        let mut block = SequentialViewMut::from_slice(&mut data, 3);

        let result = block.copy_mono_to_all_channels(&mono);
        assert_eq!(result, None); // Exact match

        assert_eq!(
            block.channel_iter(0).copied().collect::<Vec<_>>(),
            vec![1.0, 2.0, 3.0, 4.0]
        );
    }

    #[test]
    fn test_copy_mono_to_all_channels_exact() {
        use crate::mono::MonoView;

        let mono_data = [1.0, 2.0, 3.0, 4.0];
        let mono = MonoView::from_slice(&mono_data);

        let mut data = [0.0; 12];
        let mut block = SequentialViewMut::from_slice(&mut data, 3);

        block.copy_mono_to_all_channels_exact(&mono);

        assert_eq!(
            block.channel_iter(0).copied().collect::<Vec<_>>(),
            vec![1.0, 2.0, 3.0, 4.0]
        );
        assert_eq!(
            block.channel_iter(1).copied().collect::<Vec<_>>(),
            vec![1.0, 2.0, 3.0, 4.0]
        );
        assert_eq!(
            block.channel_iter(2).copied().collect::<Vec<_>>(),
            vec![1.0, 2.0, 3.0, 4.0]
        );
    }

    #[test]
    #[should_panic]
    #[no_sanitize_realtime]
    fn test_copy_mono_to_all_channels_exact_wrong_frames() {
        use crate::mono::MonoView;

        let mono_data = [1.0, 2.0, 3.0, 4.0, 5.0]; // 5 frames
        let mono = MonoView::from_slice(&mono_data);

        let mut data = [0.0; 12]; // 3 channels, 4 frames - mismatch!
        let mut block = SequentialViewMut::from_slice(&mut data, 3);

        block.copy_mono_to_all_channels_exact(&mono);
    }
}