audionimbus 0.16.0

A safe wrapper around Steam Audio that provides spatial audio capabilities with realistic occlusion, reverb, and HRTF effects, accounting for physical attributes and scene geometry.
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
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
//! Types and utilities for working with audio buffers.

use crate::context::Context;
use crate::effect::ambisonics::AmbisonicsType;
use crate::ffi_wrapper::FFIWrapper;
use smallvec::SmallVec;
use std::marker::PhantomData;

/// Number of channel pointers stored without a heap allocation.
const INLINE_CHANNEL_CAPACITY: usize = 16;

/// Audio sample type.
pub type Sample = f32;

/// Channel pointers
type ChannelPointers = SmallVec<[*mut Sample; INLINE_CHANNEL_CAPACITY]>;

mod sealed {
    use super::Sample;

    /// Supplies channel pointers to the public read interface.
    pub trait ChannelPointers {
        /// Returns the channel pointers.
        fn channel_ptrs(&self) -> &[*mut Sample];
    }
}

/// Read access to borrowed audio samples.
pub trait AudioBuffer: crate::sealed::Sealed + sealed::ChannelPointers {
    /// Returns the number of channels.
    fn num_channels(&self) -> usize {
        self.channel_ptrs().len()
    }

    /// Returns the number of samples per channel.
    fn num_samples(&self) -> usize;

    /// Returns a channel by index.
    fn channel(&self, index: usize) -> Option<&[Sample]> {
        self.channel_ptrs().get(index).map(|pointer| {
            // SAFETY: Implementations guarantee that every pointer is valid for `num_samples`.
            unsafe { std::slice::from_raw_parts(*pointer, AudioBuffer::num_samples(self)) }
        })
    }

    /// Returns an iterator over channels.
    fn channels(&self) -> impl ExactSizeIterator<Item = &[Sample]> + '_ {
        let num_samples = AudioBuffer::num_samples(self);
        self.channel_ptrs().iter().map(move |pointer| {
            // SAFETY: Implementations guarantee that every pointer is valid for `num_samples`.
            unsafe { std::slice::from_raw_parts(*pointer, num_samples) }
        })
    }

    /// Interleaves the channel data into `dst`.
    ///
    /// # Errors
    ///
    /// - [`AudioBufferOperationError::InterleaveLengthOverflow`] if the total sample count exceeds
    ///   the native indexing range.
    /// - [`AudioBufferOperationError::InterleaveLengthMismatch`] if `dst` does not match the total
    ///   sample count.
    fn interleave(
        &self,
        context: &Context,
        dst: &mut [Sample],
    ) -> Result<(), AudioBufferOperationError>
    where
        Self: Sized,
    {
        let expected_len = self
            .num_channels()
            .checked_mul(self.num_samples())
            .filter(|expected_len| i32::try_from(*expected_len).is_ok())
            .ok_or(AudioBufferOperationError::InterleaveLengthOverflow {
                num_channels: self.num_channels(),
                num_samples: self.num_samples(),
            })?;
        if dst.len() != expected_len {
            return Err(AudioBufferOperationError::InterleaveLengthMismatch {
                dst_len: dst.len(),
                expected_len,
            });
        }

        let mut input = view_as_ffi(self, self.channel_ptrs(), self.num_samples());
        unsafe {
            audionimbus_sys::iplAudioBufferInterleave(
                context.raw_ptr(),
                &raw mut *input,
                dst.as_mut_ptr(),
            );
        }

        Ok(())
    }

    /// Converts Ambisonic samples from `in_type` into `out_type`.
    ///
    /// Steam Audio processes N3D natively, so conversion is best kept at integration boundaries.
    ///
    /// # Errors
    ///
    /// - [`AudioBufferOperationError::ChannelCountMismatch`] if the channel counts differ.
    /// - [`AudioBufferOperationError::SampleCountMismatch`] if the per-channel sample counts differ.
    fn convert_ambisonics_into(
        &self,
        context: &Context,
        in_type: AmbisonicsType,
        out_type: AmbisonicsType,
        out: &mut AudioBufferMut<'_>,
    ) -> Result<(), AudioBufferOperationError>
    where
        Self: Sized,
    {
        validate_matching_shape(self, out)?;

        let mut input = view_as_ffi(self, self.channel_ptrs(), self.num_samples());
        let mut output = out.as_ffi_mut();
        unsafe {
            audionimbus_sys::iplAudioBufferConvertAmbisonics(
                context.raw_ptr(),
                in_type.into(),
                out_type.into(),
                &raw mut *input,
                &raw mut *output,
            );
        }

        Ok(())
    }
}

/// An immutable view over borrowed audio samples.
///
/// The view stores channel pointers, but never owns the samples they reference.
#[derive(Clone, Debug)]
pub struct AudioBufferRef<'a> {
    /// Number of samples in each channel.
    num_samples: usize,
    /// Pointer to the first sample in each channel.
    channel_ptrs: ChannelPointers,
    /// Ties the view to the borrowed samples.
    _samples: PhantomData<&'a [Sample]>,
}

impl<'a> AudioBufferRef<'a> {
    /// Constructs a view over contiguous `samples`.
    ///
    /// Each channel occupies one contiguous region of equal length.
    ///
    /// # Errors
    ///
    /// - [`AudioBufferError::NoChannels`] if `num_channels` is zero.
    /// - [`AudioBufferError::NoSamples`] if `samples` is empty.
    /// - [`AudioBufferError::DataLengthMismatch`] if the data cannot be divided evenly.
    /// - [`AudioBufferError::TooManyChannels`] if the channel count exceeds the native limit.
    /// - [`AudioBufferError::TooManySamples`] if the per-channel count exceeds the native limit.
    pub fn try_new(samples: &'a [Sample], num_channels: usize) -> Result<Self, AudioBufferError> {
        let num_samples = validate_contiguous_layout(samples.len(), num_channels)?;
        let channel_ptrs = samples
            .chunks_exact(num_samples)
            .map(|channel| channel.as_ptr().cast_mut())
            .collect();

        Ok(Self::from_validated_parts(channel_ptrs, num_samples))
    }

    /// Constructs a view over separate channels.
    ///
    /// # Errors
    ///
    /// - [`AudioBufferError::NoChannels`] if no channels are provided.
    /// - [`AudioBufferError::NoSamples`] if the channels are empty.
    /// - [`AudioBufferError::ChannelLengthMismatch`] if channel lengths differ.
    /// - [`AudioBufferError::TooManyChannels`] if the channel count exceeds the native limit.
    /// - [`AudioBufferError::TooManySamples`] if the per-channel count exceeds the native limit.
    pub fn try_from_channels<I>(channels: I) -> Result<Self, AudioBufferError>
    where
        I: IntoIterator<Item = &'a [Sample]>,
    {
        let mut channels = channels.into_iter();
        let first = channels.next().ok_or(AudioBufferError::NoChannels)?;
        let num_samples = first.len();
        let mut channel_ptrs = ChannelPointers::new();
        channel_ptrs.push(first.as_ptr().cast_mut());

        for (channel, samples) in channels.enumerate() {
            let channel = channel + 1;
            if samples.len() != num_samples {
                return Err(AudioBufferError::ChannelLengthMismatch {
                    channel,
                    expected: num_samples,
                    actual: samples.len(),
                });
            }
            channel_ptrs.push(samples.as_ptr().cast_mut());
        }

        validate_view_dimensions(channel_ptrs.len(), num_samples)?;
        Ok(Self::from_validated_parts(channel_ptrs, num_samples))
    }

    /// Returns the number of channels.
    pub fn num_channels(&self) -> usize {
        AudioBuffer::num_channels(self)
    }

    /// Returns the number of samples per channel.
    pub fn num_samples(&self) -> usize {
        AudioBuffer::num_samples(self)
    }

    /// Returns a channel by index.
    pub fn channel(&self, index: usize) -> Option<&[Sample]> {
        AudioBuffer::channel(self, index)
    }

    /// Returns an iterator over channels.
    pub fn channels(&self) -> impl ExactSizeIterator<Item = &[Sample]> + '_ {
        AudioBuffer::channels(self)
    }

    /// Constructs a view from raw channel pointers.
    ///
    /// # Errors
    ///
    /// - [`AudioBufferError::NoChannels`] if no pointers are provided.
    /// - [`AudioBufferError::NoSamples`] if `num_samples` is zero.
    /// - [`AudioBufferError::NullChannelPointer`] if a pointer is null.
    /// - [`AudioBufferError::TooManyChannels`] if the channel count exceeds the native limit.
    /// - [`AudioBufferError::TooManySamples`] if the sample count exceeds the native limit.
    ///
    /// # Safety
    ///
    /// Every pointer must be aligned, initialized, and valid to read `num_samples` consecutive
    /// samples for the returned view's lifetime.
    /// The referenced memory must not be mutated for that lifetime except through interior
    /// mutability that upholds Rust's aliasing rules.
    pub unsafe fn try_from_raw_parts(
        channel_ptrs: &[*const Sample],
        num_samples: usize,
    ) -> Result<Self, AudioBufferError> {
        validate_view_dimensions(channel_ptrs.len(), num_samples)?;
        let channel_ptrs = channel_ptrs
            .iter()
            .enumerate()
            .map(|(channel, pointer)| {
                if pointer.is_null() {
                    Err(AudioBufferError::NullChannelPointer { channel })
                } else {
                    Ok(pointer.cast_mut())
                }
            })
            .collect::<Result<_, _>>()?;

        Ok(Self::from_validated_parts(channel_ptrs, num_samples))
    }

    fn from_validated_parts(channel_ptrs: ChannelPointers, num_samples: usize) -> Self {
        Self {
            num_samples,
            channel_ptrs,
            _samples: PhantomData,
        }
    }

    #[allow(dead_code)]
    pub(crate) fn as_ffi(&self) -> FFIWrapper<'_, audionimbus_sys::IPLAudioBuffer, Self> {
        view_as_ffi(self, &self.channel_ptrs, self.num_samples)
    }
}

impl<'a> TryFrom<&'a [Sample]> for AudioBufferRef<'a> {
    type Error = AudioBufferError;

    fn try_from(samples: &'a [Sample]) -> Result<Self, Self::Error> {
        Self::try_new(samples, 1)
    }
}

impl crate::sealed::Sealed for AudioBufferRef<'_> {}

impl sealed::ChannelPointers for AudioBufferRef<'_> {
    fn channel_ptrs(&self) -> &[*mut Sample] {
        &self.channel_ptrs
    }
}

impl AudioBuffer for AudioBufferRef<'_> {
    fn num_samples(&self) -> usize {
        self.num_samples
    }
}

// SAFETY: The view has the same read-only access semantics as `&[Sample]`.
unsafe impl Send for AudioBufferRef<'_> {}
// SAFETY: The view has the same read-only access semantics as `&[Sample]`.
unsafe impl Sync for AudioBufferRef<'_> {}

/// A mutable view over exclusively borrowed audio samples.
///
/// The view stores channel pointers, but never owns the samples they reference.
#[derive(Debug)]
pub struct AudioBufferMut<'a> {
    /// Number of samples in each channel.
    num_samples: usize,
    /// Pointer to the first sample in each channel.
    channel_ptrs: ChannelPointers,
    /// Ties the view to the exclusively borrowed samples.
    _samples: PhantomData<&'a mut [Sample]>,
}

impl<'a> AudioBufferMut<'a> {
    /// Constructs a mutable view over contiguous `samples`.
    ///
    /// # Errors
    ///
    /// - [`AudioBufferError::NoChannels`] if `num_channels` is zero.
    /// - [`AudioBufferError::NoSamples`] if `samples` is empty.
    /// - [`AudioBufferError::DataLengthMismatch`] if the data cannot be divided evenly.
    /// - [`AudioBufferError::TooManyChannels`] if the channel count exceeds the native limit.
    /// - [`AudioBufferError::TooManySamples`] if the per-channel count exceeds the native limit.
    pub fn try_new(
        samples: &'a mut [Sample],
        num_channels: usize,
    ) -> Result<Self, AudioBufferError> {
        let num_samples = validate_contiguous_layout(samples.len(), num_channels)?;
        let channel_ptrs = samples
            .chunks_exact_mut(num_samples)
            .map(<[Sample]>::as_mut_ptr)
            .collect();

        Ok(Self::from_validated_parts(channel_ptrs, num_samples))
    }

    /// Constructs a mutable view over separate channels.
    ///
    /// Safe Rust guarantees that the mutable channel slices do not overlap.
    ///
    /// # Errors
    ///
    /// - [`AudioBufferError::NoChannels`] if no channels are provided.
    /// - [`AudioBufferError::NoSamples`] if the channels are empty.
    /// - [`AudioBufferError::ChannelLengthMismatch`] if channel lengths differ.
    /// - [`AudioBufferError::TooManyChannels`] if the channel count exceeds the native limit.
    /// - [`AudioBufferError::TooManySamples`] if the per-channel count exceeds the native limit.
    pub fn try_from_channels<I>(channels: I) -> Result<Self, AudioBufferError>
    where
        I: IntoIterator<Item = &'a mut [Sample]>,
    {
        let mut channels = channels.into_iter();
        let first = channels.next().ok_or(AudioBufferError::NoChannels)?;
        let num_samples = first.len();
        let mut channel_ptrs = ChannelPointers::new();
        channel_ptrs.push(first.as_mut_ptr());

        for (channel, samples) in channels.enumerate() {
            let channel = channel + 1;
            if samples.len() != num_samples {
                return Err(AudioBufferError::ChannelLengthMismatch {
                    channel,
                    expected: num_samples,
                    actual: samples.len(),
                });
            }
            channel_ptrs.push(samples.as_mut_ptr());
        }

        validate_view_dimensions(channel_ptrs.len(), num_samples)?;
        Ok(Self::from_validated_parts(channel_ptrs, num_samples))
    }

    /// Returns the number of channels.
    pub fn num_channels(&self) -> usize {
        AudioBuffer::num_channels(self)
    }

    /// Returns the number of samples per channel.
    pub fn num_samples(&self) -> usize {
        AudioBuffer::num_samples(self)
    }

    /// Returns a channel by index.
    pub fn channel(&self, index: usize) -> Option<&[Sample]> {
        AudioBuffer::channel(self, index)
    }

    /// Returns an iterator over channels.
    pub fn channels(&self) -> impl ExactSizeIterator<Item = &[Sample]> + '_ {
        AudioBuffer::channels(self)
    }

    /// Returns a mutable channel by index.
    pub fn channel_mut(&mut self, index: usize) -> Option<&mut [Sample]> {
        let pointer = self.channel_ptrs.get_mut(index)?;
        // SAFETY: Construction guarantees exclusive, disjoint channel regions.
        Some(unsafe { std::slice::from_raw_parts_mut(*pointer, self.num_samples) })
    }

    /// Deinterleaves `src` into the channel data.
    ///
    /// # Errors
    ///
    /// Returns [`AudioBufferOperationError::DeinterleaveLengthMismatch`] if `src` does not match
    /// the total sample count.
    pub fn deinterleave(
        &mut self,
        context: &Context,
        src: &[Sample],
    ) -> Result<(), AudioBufferOperationError> {
        let expected_len = self.num_channels() * self.num_samples();
        if src.len() != expected_len {
            return Err(AudioBufferOperationError::DeinterleaveLengthMismatch {
                src_len: src.len(),
                expected_len,
            });
        }

        let mut output = self.as_ffi_mut();
        unsafe {
            audionimbus_sys::iplAudioBufferDeinterleave(
                context.raw_ptr(),
                src.as_ptr().cast_mut(),
                &raw mut *output,
            );
        }

        Ok(())
    }

    /// Mixes `source` into this buffer.
    ///
    /// # Errors
    ///
    /// - [`AudioBufferOperationError::ChannelCountMismatch`] if the channel counts differ.
    /// - [`AudioBufferOperationError::SampleCountMismatch`] if the per-channel sample counts differ.
    pub fn mix(
        &mut self,
        context: &Context,
        source: &impl AudioBuffer,
    ) -> Result<(), AudioBufferOperationError> {
        validate_matching_shape(self, source)?;

        let mut input = view_as_ffi(source, source.channel_ptrs(), source.num_samples());
        let mut output = self.as_ffi_mut();
        unsafe {
            audionimbus_sys::iplAudioBufferMix(
                context.raw_ptr(),
                &raw mut *input,
                &raw mut *output,
            );
        }

        Ok(())
    }

    /// Downmixes `source` into this mono buffer.
    ///
    /// Steam Audio writes the arithmetic mean of the source channels. Mix manually when the
    /// channels require different weights.
    ///
    /// # Errors
    ///
    /// - [`AudioBufferOperationError::DownmixDestinationNotMono`] if this buffer is not mono.
    /// - [`AudioBufferOperationError::SampleCountMismatch`] if the per-channel sample counts differ.
    pub fn downmix(
        &mut self,
        context: &Context,
        source: &impl AudioBuffer,
    ) -> Result<(), AudioBufferOperationError> {
        if self.num_channels() != 1 {
            return Err(AudioBufferOperationError::DownmixDestinationNotMono {
                num_channels: self.num_channels(),
            });
        }
        if self.num_samples() != source.num_samples() {
            return Err(AudioBufferOperationError::SampleCountMismatch {
                self_num_samples: self.num_samples(),
                other_num_samples: source.num_samples(),
            });
        }

        let mut input = view_as_ffi(source, source.channel_ptrs(), source.num_samples());
        let mut output = self.as_ffi_mut();
        unsafe {
            audionimbus_sys::iplAudioBufferDownmix(
                context.raw_ptr(),
                &raw mut *input,
                &raw mut *output,
            );
        }

        Ok(())
    }

    /// Converts Ambisonic samples from `in_type` into `out_type` in place.
    ///
    /// Steam Audio processes N3D natively, so conversion is best kept at integration boundaries.
    pub fn convert_ambisonics(
        &mut self,
        context: &Context,
        in_type: AmbisonicsType,
        out_type: AmbisonicsType,
    ) {
        let mut buffer = self.as_ffi_mut();
        unsafe {
            audionimbus_sys::iplAudioBufferConvertAmbisonics(
                context.raw_ptr(),
                in_type.into(),
                out_type.into(),
                &raw mut *buffer,
                &raw mut *buffer,
            );
        }
    }

    /// Constructs a mutable view from raw channel pointers.
    ///
    /// # Errors
    ///
    /// - [`AudioBufferError::NoChannels`] if no pointers are provided.
    /// - [`AudioBufferError::NoSamples`] if `num_samples` is zero.
    /// - [`AudioBufferError::NullChannelPointer`] if a pointer is null.
    /// - [`AudioBufferError::TooManyChannels`] if the channel count exceeds the native limit.
    /// - [`AudioBufferError::TooManySamples`] if the sample count exceeds the native limit.
    ///
    /// # Safety
    ///
    /// Every pointer must be aligned, initialized, and valid to read and write `num_samples`
    /// consecutive samples for the returned view's lifetime.
    /// Channel sample regions must be pairwise disjoint, and no other pointer may access them
    /// during that lifetime.
    pub unsafe fn try_from_raw_parts(
        channel_ptrs: &[*mut Sample],
        num_samples: usize,
    ) -> Result<Self, AudioBufferError> {
        validate_view_dimensions(channel_ptrs.len(), num_samples)?;
        let channel_ptrs = channel_ptrs
            .iter()
            .copied()
            .enumerate()
            .map(|(channel, pointer)| {
                if pointer.is_null() {
                    Err(AudioBufferError::NullChannelPointer { channel })
                } else {
                    Ok(pointer)
                }
            })
            .collect::<Result<_, _>>()?;

        Ok(Self::from_validated_parts(channel_ptrs, num_samples))
    }

    /// Returns an iterator over mutable channels.
    pub fn channels_mut(&mut self) -> impl ExactSizeIterator<Item = &mut [Sample]> + '_ {
        let num_samples = self.num_samples;
        self.channel_ptrs.iter_mut().map(move |pointer| {
            // SAFETY: Construction guarantees exclusive, disjoint channel regions.
            unsafe { std::slice::from_raw_parts_mut(*pointer, num_samples) }
        })
    }

    fn from_validated_parts(channel_ptrs: ChannelPointers, num_samples: usize) -> Self {
        Self {
            num_samples,
            channel_ptrs,
            _samples: PhantomData,
        }
    }

    #[allow(dead_code)]
    pub(crate) fn as_ffi(&self) -> FFIWrapper<'_, audionimbus_sys::IPLAudioBuffer, Self> {
        view_as_ffi(self, &self.channel_ptrs, self.num_samples)
    }

    #[allow(dead_code)]
    pub(crate) fn as_ffi_mut(&mut self) -> FFIWrapper<'_, audionimbus_sys::IPLAudioBuffer, Self> {
        view_as_ffi(self, &self.channel_ptrs, self.num_samples)
    }
}

impl<'a> TryFrom<&'a mut [Sample]> for AudioBufferMut<'a> {
    type Error = AudioBufferError;

    fn try_from(samples: &'a mut [Sample]) -> Result<Self, Self::Error> {
        Self::try_new(samples, 1)
    }
}

impl crate::sealed::Sealed for AudioBufferMut<'_> {}

impl sealed::ChannelPointers for AudioBufferMut<'_> {
    fn channel_ptrs(&self) -> &[*mut Sample] {
        &self.channel_ptrs
    }
}

impl AudioBuffer for AudioBufferMut<'_> {
    fn num_samples(&self) -> usize {
        self.num_samples
    }
}

// SAFETY: The view has the same exclusive access semantics as `&mut [Sample]`.
unsafe impl Send for AudioBufferMut<'_> {}
// SAFETY: Shared access cannot mutate samples; writable operations require `&mut self`.
unsafe impl Sync for AudioBufferMut<'_> {}

/// Validates contiguous channel data and returns the sample count per channel.
///
/// # Errors
///
/// - [`AudioBufferError::NoChannels`] if `num_channels` is zero.
/// - [`AudioBufferError::NoSamples`] if `len` is zero.
/// - [`AudioBufferError::DataLengthMismatch`] if the data cannot be divided evenly.
/// - [`AudioBufferError::TooManyChannels`] if the channel count exceeds the native limit.
/// - [`AudioBufferError::TooManySamples`] if the per-channel count exceeds the native limit.
fn validate_contiguous_layout(len: usize, num_channels: usize) -> Result<usize, AudioBufferError> {
    if num_channels == 0 {
        return Err(AudioBufferError::NoChannels);
    }
    if len == 0 {
        return Err(AudioBufferError::NoSamples);
    }
    if !len.is_multiple_of(num_channels) {
        return Err(AudioBufferError::DataLengthMismatch { len, num_channels });
    }

    let num_samples = len / num_channels;
    validate_view_dimensions(num_channels, num_samples)?;
    Ok(num_samples)
}

/// Validates channel and sample counts for the native descriptor.
///
/// # Errors
///
/// - [`AudioBufferError::NoChannels`] if `num_channels` is zero.
/// - [`AudioBufferError::NoSamples`] if `num_samples` is zero.
/// - [`AudioBufferError::TooManyChannels`] if the channel count exceeds the native limit.
/// - [`AudioBufferError::TooManySamples`] if the sample count exceeds the native limit.
fn validate_view_dimensions(
    num_channels: usize,
    num_samples: usize,
) -> Result<(), AudioBufferError> {
    if num_channels == 0 {
        return Err(AudioBufferError::NoChannels);
    }
    if num_samples == 0 {
        return Err(AudioBufferError::NoSamples);
    }
    if i32::try_from(num_channels).is_err() {
        return Err(AudioBufferError::TooManyChannels { num_channels });
    }
    if i32::try_from(num_samples).is_err() {
        return Err(AudioBufferError::TooManySamples { num_samples });
    }
    Ok(())
}

/// Creates a native descriptor tied to `owner`.
pub(crate) fn read_as_ffi<Buffer>(
    buffer: &Buffer,
) -> FFIWrapper<'_, audionimbus_sys::IPLAudioBuffer, Buffer>
where
    Buffer: AudioBuffer,
{
    view_as_ffi(buffer, buffer.channel_ptrs(), buffer.num_samples())
}

/// Creates a native descriptor tied to `owner`.
#[allow(dead_code)]
fn view_as_ffi<'a, Owner>(
    _owner: &'a Owner,
    channel_ptrs: &[*mut Sample],
    num_samples: usize,
) -> FFIWrapper<'a, audionimbus_sys::IPLAudioBuffer, Owner> {
    let audio_buffer = audionimbus_sys::IPLAudioBuffer {
        numChannels: channel_ptrs.len() as i32,
        numSamples: num_samples as i32,
        data: channel_ptrs.as_ptr().cast_mut(),
    };

    FFIWrapper::new(audio_buffer)
}

/// Validates that two views have the same channel layout.
fn validate_matching_shape(
    first: &impl AudioBuffer,
    second: &impl AudioBuffer,
) -> Result<(), AudioBufferOperationError> {
    if first.num_channels() != second.num_channels() {
        return Err(AudioBufferOperationError::ChannelCountMismatch {
            self_num_channels: first.num_channels(),
            other_num_channels: second.num_channels(),
        });
    }
    if first.num_samples() != second.num_samples() {
        return Err(AudioBufferOperationError::SampleCountMismatch {
            self_num_samples: first.num_samples(),
            other_num_samples: second.num_samples(),
        });
    }
    Ok(())
}

/// [`AudioBuffer`] construction errors.
#[derive(Debug, PartialEq, Eq)]
pub enum AudioBufferError {
    /// No channels were provided.
    NoChannels,

    /// No samples were provided per channel.
    NoSamples,

    /// Contiguous data cannot be divided evenly into the requested channels.
    DataLengthMismatch { len: usize, num_channels: usize },

    /// A channel has a different length than the first channel.
    ChannelLengthMismatch {
        channel: usize,
        expected: usize,
        actual: usize,
    },

    /// A raw channel pointer is null.
    NullChannelPointer { channel: usize },

    /// The channel count exceeds Steam Audio's native integer range.
    TooManyChannels { num_channels: usize },

    /// The per-channel sample count exceeds Steam Audio's native integer range.
    TooManySamples { num_samples: usize },
}

impl std::error::Error for AudioBufferError {}

impl std::fmt::Display for AudioBufferError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match &self {
            Self::NoChannels => write!(f, "audio buffer has no channels"),
            Self::NoSamples => write!(f, "audio buffer channels have no samples"),
            Self::DataLengthMismatch { len, num_channels } => write!(
                f,
                "sample data length {len} is not divisible by {num_channels} channels"
            ),
            Self::ChannelLengthMismatch {
                channel,
                expected,
                actual,
            } => write!(
                f,
                "channel {channel} has {actual} samples, expected {expected}"
            ),
            Self::NullChannelPointer { channel } => {
                write!(f, "channel {channel} has a null sample pointer")
            }
            Self::TooManyChannels { num_channels } => write!(
                f,
                "channel count {num_channels} exceeds the native integer range"
            ),
            Self::TooManySamples { num_samples } => write!(
                f,
                "sample count {num_samples} exceeds the native integer range"
            ),
        }
    }
}

/// Errors produced by audio buffer operations.
#[derive(Debug, PartialEq, Eq)]
pub enum AudioBufferOperationError {
    /// The total interleaved sample count exceeds the native indexing range.
    InterleaveLengthOverflow {
        num_channels: usize,
        num_samples: usize,
    },

    /// Destination slice length does not match audio buffer length.
    InterleaveLengthMismatch { dst_len: usize, expected_len: usize },

    /// Source slice length does not match audio buffer length.
    DeinterleaveLengthMismatch { src_len: usize, expected_len: usize },

    /// Audio buffers have mismatched number of channels.
    ChannelCountMismatch {
        self_num_channels: usize,
        other_num_channels: usize,
    },

    /// Audio buffers have mismatched number of samples.
    SampleCountMismatch {
        self_num_samples: usize,
        other_num_samples: usize,
    },

    /// A downmix destination has more than one channel.
    DownmixDestinationNotMono { num_channels: usize },
}

impl std::error::Error for AudioBufferOperationError {}

impl std::fmt::Display for AudioBufferOperationError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Self::InterleaveLengthOverflow {
                num_channels,
                num_samples,
            } => write!(
                f,
                "interleaved length for {num_channels} channels with {num_samples} samples exceeds the native integer range"
            ),
            Self::InterleaveLengthMismatch {
                dst_len,
                expected_len,
            } => write!(
                f,
                "destination slice length {dst_len} does not match expected length {expected_len}"
            ),
            Self::DeinterleaveLengthMismatch {
                src_len,
                expected_len,
            } => write!(
                f,
                "source slice length {src_len} does not match expected length {expected_len}"
            ),
            Self::ChannelCountMismatch {
                self_num_channels,
                other_num_channels,
            } => write!(
                f,
                "channel count mismatch: buffer has {self_num_channels} channels, other has {other_num_channels}"
            ),
            Self::SampleCountMismatch {
                self_num_samples,
                other_num_samples,
            } => write!(
                f,
                "sample count mismatch: buffer has {self_num_samples} samples, other has {other_num_samples}"
            ),
            Self::DownmixDestinationNotMono { num_channels } => write!(
                f,
                "downmix destination must be mono, but has {num_channels} channels"
            ),
        }
    }
}

/// Returns the number of channels required for a given ambisonics order.
///
/// The channel count is given by:
///
/// ```text
/// (order + 1)^2
/// ```
///
/// # Examples
///
/// ```
/// # use audionimbus::*;
/// const FOA: u32 = num_ambisonics_channels(1);
/// assert_eq!(FOA, 4);
///
/// const HOA3: u32 = num_ambisonics_channels(3);
/// assert_eq!(HOA3, 16);
/// ```
pub const fn num_ambisonics_channels(order: u32) -> u32 {
    (order + 1) * (order + 1)
}

/// Describes the channel count requirement for an audio buffer.
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum ChannelRequirement {
    /// The buffer must have exactly this many channels.
    Exactly(u32),

    /// The buffer must have at least this many channels.
    AtLeast(u32),

    /// The buffer must have a channel count within the given inclusive range.
    Range { min: u32, max: u32 },
}

impl ChannelRequirement {
    /// Returns whether a number of channels satisfies this requirement.
    pub fn is_satisfied_by(&self, actual: u32) -> bool {
        match *self {
            Self::Exactly(num_channels) => actual == num_channels,
            Self::AtLeast(num_channels) => actual >= num_channels,
            Self::Range { min, max } => (min..=max).contains(&actual),
        }
    }
}

impl std::fmt::Display for ChannelRequirement {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Self::Exactly(num_channels) => {
                write!(f, "exactly {num_channels}")
            }
            Self::AtLeast(num_channels) => {
                write!(f, "at least {num_channels}")
            }
            Self::Range { min, max } => {
                write!(f, "between {min} and {max} (inclusive)")
            }
        }
    }
}

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

    mod audio_buffer_ref {
        use super::*;

        mod try_new {
            use super::*;

            #[test]
            fn immutable() {
                let samples = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0];
                let buffer = AudioBufferRef::try_new(&samples, 2).unwrap();

                assert_eq!(buffer.num_channels(), 2);
                assert_eq!(buffer.num_samples(), 3);
                assert_eq!(buffer.channel(0), Some(&samples[..3]));
                assert_eq!(buffer.channel(1), Some(&samples[3..]));
                assert_eq!(buffer.clone().channels().count(), 2);
            }

            #[test]
            fn invalid() {
                assert_eq!(
                    AudioBufferRef::try_new(&[0.0; 4], 0).unwrap_err(),
                    AudioBufferError::NoChannels
                );
                assert_eq!(
                    AudioBufferRef::try_new(&[], 1).unwrap_err(),
                    AudioBufferError::NoSamples
                );
                assert_eq!(
                    AudioBufferRef::try_new(&[0.0; 5], 2).unwrap_err(),
                    AudioBufferError::DataLengthMismatch {
                        len: 5,
                        num_channels: 2,
                    }
                );
            }

            #[test]
            fn storage() {
                let samples = [0.0; INLINE_CHANNEL_CAPACITY];
                let buffer = AudioBufferRef::try_new(&samples, INLINE_CHANNEL_CAPACITY).unwrap();

                assert!(!buffer.channel_ptrs.spilled());

                let samples = [0.0; INLINE_CHANNEL_CAPACITY + 1];
                let buffer =
                    AudioBufferRef::try_new(&samples, INLINE_CHANNEL_CAPACITY + 1).unwrap();

                assert!(buffer.channel_ptrs.spilled());
            }
        }

        mod try_from {
            use super::*;

            #[test]
            fn mono() {
                let samples = [1.0, 2.0];
                let buffer = AudioBufferRef::try_from(&samples[..]).unwrap();

                assert_eq!(buffer.num_channels(), 1);
                assert_eq!(buffer.channel(0), Some(&samples[..]));
            }
        }

        mod try_from_channels {
            use super::*;

            #[test]
            fn unequal() {
                let first = [0.0; 2];
                let second = [0.0; 3];

                assert_eq!(
                    AudioBufferRef::try_from_channels([&first[..], &second[..]]).unwrap_err(),
                    AudioBufferError::ChannelLengthMismatch {
                        channel: 1,
                        expected: 2,
                        actual: 3,
                    }
                );
            }
        }

        mod try_from_raw_parts {
            use super::*;

            #[test]
            fn invalid() {
                let pointers = [std::ptr::null()];
                let result = unsafe { AudioBufferRef::try_from_raw_parts(&pointers, 1) };

                assert_eq!(
                    result.unwrap_err(),
                    AudioBufferError::NullChannelPointer { channel: 0 }
                );

                let sample = 0.0;
                let pointers = [&raw const sample];
                let num_samples = usize::try_from(i32::MAX).unwrap() + 1;
                let result = unsafe { AudioBufferRef::try_from_raw_parts(&pointers, num_samples) };

                assert_eq!(
                    result.unwrap_err(),
                    AudioBufferError::TooManySamples { num_samples }
                );
            }
        }

        mod as_ffi {
            use super::*;

            #[test]
            fn descriptors() {
                let samples = [0.0; 8];
                let buffer = AudioBufferRef::try_new(&samples, 2).unwrap();
                let ffi = buffer.as_ffi();

                assert_eq!(ffi.numChannels, 2);
                assert_eq!(ffi.numSamples, 4);
                assert!(!ffi.data.is_null());
            }
        }
    }

    mod audio_buffer_mut {
        use super::*;

        mod try_new {
            use super::*;

            #[test]
            fn valid() {
                let mut samples = [1.0, 2.0, 3.0, 4.0];
                let mut buffer = AudioBufferMut::try_new(&mut samples, 2).unwrap();

                for channel in buffer.channels_mut() {
                    channel.fill(0.5);
                }
                drop(buffer);

                assert_eq!(samples, [0.5; 4]);
            }
        }

        mod try_from {
            use super::*;

            #[test]
            fn mono() {
                let mut samples = [1.0, 2.0];
                let mut buffer = AudioBufferMut::try_from(&mut samples[..]).unwrap();
                buffer.channels_mut().next().unwrap().fill(0.5);
                drop(buffer);

                assert_eq!(samples, [0.5; 2]);
            }
        }

        mod try_from_channels {
            use super::*;

            #[test]
            fn valid() {
                let mut samples = [0.0; 6];
                let (left, right) = samples.split_at_mut(3);
                let mut buffer = AudioBufferMut::try_from_channels([left, right]).unwrap();

                let mut channels = buffer.channels_mut();
                channels.next().unwrap().fill(1.0);
                channels.next().unwrap().fill(2.0);
                drop(channels);
                drop(buffer);

                assert_eq!(samples, [1.0, 1.0, 1.0, 2.0, 2.0, 2.0]);
            }
        }

        mod try_from_raw_parts {
            use super::*;

            #[test]
            fn null_pointer() {
                let pointers = [std::ptr::null_mut()];
                let result = unsafe { AudioBufferMut::try_from_raw_parts(&pointers, 1) };

                assert_eq!(
                    result.unwrap_err(),
                    AudioBufferError::NullChannelPointer { channel: 0 }
                );
            }
        }

        mod as_ffi {
            use super::*;

            #[test]
            fn descriptors() {
                let mut samples = [0.0; 8];
                let mut buffer = AudioBufferMut::try_new(&mut samples, 2).unwrap();

                assert_eq!(buffer.as_ffi().numChannels, 2);
                assert_eq!(buffer.as_ffi_mut().numSamples, 4);
            }
        }
    }

    mod channel_requirement {
        use super::*;

        #[test]
        fn test_is_satisfied_by() {
            assert!(ChannelRequirement::Exactly(2).is_satisfied_by(2));
            assert!(!ChannelRequirement::Exactly(2).is_satisfied_by(1));
            assert!(ChannelRequirement::AtLeast(2).is_satisfied_by(3));
            assert!(!ChannelRequirement::AtLeast(2).is_satisfied_by(1));
            assert!(ChannelRequirement::Range { min: 1, max: 4 }.is_satisfied_by(3));
            assert!(!ChannelRequirement::Range { min: 1, max: 4 }.is_satisfied_by(5));
        }
    }
}