1use std::borrow::Cow;
12use std::fmt;
13use std::marker::PhantomData;
14use std::mem;
15use std::vec::Vec;
16
17use arrayvec::ArrayVec;
18use bitflags::bitflags;
19
20use crate::conv::{ConvertibleSample, FromSample, IntoSample};
21use crate::errors::Result;
22use crate::sample::{i24, u24, Sample};
23use crate::units::Duration;
24
25const AUDIO_PLANES_STORAGE_STACK_LIMIT: usize = 8;
28
29bitflags! {
30 #[derive(Default)]
36 pub struct Channels: u32 {
37 const FRONT_LEFT = 0x0000_0001;
39 const FRONT_RIGHT = 0x0000_0002;
41 const FRONT_CENTRE = 0x0000_0004;
43 const LFE1 = 0x0000_0008;
45 const REAR_LEFT = 0x0000_0010;
47 const REAR_RIGHT = 0x0000_0020;
49 const FRONT_LEFT_CENTRE = 0x0000_0040;
51 const FRONT_RIGHT_CENTRE = 0x0000_0080;
53 const REAR_CENTRE = 0x0000_0100;
55 const SIDE_LEFT = 0x0000_0200;
57 const SIDE_RIGHT = 0x0000_0400;
59 const TOP_CENTRE = 0x0000_0800;
61 const TOP_FRONT_LEFT = 0x0000_1000;
63 const TOP_FRONT_CENTRE = 0x0000_2000;
65 const TOP_FRONT_RIGHT = 0x0000_4000;
67 const TOP_REAR_LEFT = 0x0000_8000;
69 const TOP_REAR_CENTRE = 0x0001_0000;
71 const TOP_REAR_RIGHT = 0x0002_0000;
73 const REAR_LEFT_CENTRE = 0x0004_0000;
75 const REAR_RIGHT_CENTRE = 0x0008_0000;
77 const FRONT_LEFT_WIDE = 0x0010_0000;
79 const FRONT_RIGHT_WIDE = 0x0020_0000;
81 const FRONT_LEFT_HIGH = 0x0040_0000;
83 const FRONT_CENTRE_HIGH = 0x0080_0000;
85 const FRONT_RIGHT_HIGH = 0x0100_0000;
87 const LFE2 = 0x0200_0000;
89 }
90}
91
92pub struct ChannelsIter {
94 channels: Channels,
95}
96
97impl Iterator for ChannelsIter {
98 type Item = Channels;
99
100 fn next(&mut self) -> Option<Self::Item> {
101 if !self.channels.is_empty() {
102 let channel = Channels::from_bits_truncate(1 << self.channels.bits.trailing_zeros());
103 self.channels ^= channel;
104 Some(channel)
105 }
106 else {
107 None
108 }
109 }
110}
111
112impl Channels {
113 pub fn count(self) -> usize {
115 self.bits.count_ones() as usize
116 }
117
118 pub fn iter(&self) -> ChannelsIter {
120 ChannelsIter { channels: *self }
121 }
122}
123
124impl fmt::Display for Channels {
125 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
126 write!(f, "{:#032b}", self.bits)
127 }
128}
129
130#[derive(Copy, Clone, Debug)]
132pub enum Layout {
133 Mono,
135 Stereo,
137 TwoPointOne,
139 FivePointOne,
141}
142
143impl Layout {
144 pub fn into_channels(self) -> Channels {
146 match self {
147 Layout::Mono => Channels::FRONT_LEFT,
148 Layout::Stereo => Channels::FRONT_LEFT | Channels::FRONT_RIGHT,
149 Layout::TwoPointOne => Channels::FRONT_LEFT | Channels::FRONT_RIGHT | Channels::LFE1,
150 Layout::FivePointOne => {
151 Channels::FRONT_LEFT
152 | Channels::FRONT_RIGHT
153 | Channels::FRONT_CENTRE
154 | Channels::REAR_LEFT
155 | Channels::REAR_RIGHT
156 | Channels::LFE1
157 }
158 }
159 }
160}
161
162#[derive(Copy, Clone, Debug, PartialEq, Eq)]
164pub struct SignalSpec {
165 pub rate: u32,
167
168 pub channels: Channels,
171}
172
173impl SignalSpec {
174 pub fn new(rate: u32, channels: Channels) -> Self {
175 SignalSpec { rate, channels }
176 }
177
178 pub fn new_with_layout(rate: u32, layout: Layout) -> Self {
179 SignalSpec { rate, channels: layout.into_channels() }
180 }
181}
182
183enum AudioPlaneStorage<'a, S, const N: usize> {
185 Stack(ArrayVec<&'a [S], N>),
186 Heap(Vec<&'a [S]>),
187}
188
189pub struct AudioPlanes<'a, S: 'a + Sample> {
191 planes: AudioPlaneStorage<'a, S, AUDIO_PLANES_STORAGE_STACK_LIMIT>,
192}
193
194impl<'a, S: Sample> AudioPlanes<'a, S> {
195 fn new(channels: Channels) -> Self {
197 let n_planes = channels.count();
198
199 if n_planes <= AUDIO_PLANES_STORAGE_STACK_LIMIT {
200 AudioPlanes { planes: AudioPlaneStorage::Stack(ArrayVec::new()) }
201 }
202 else {
203 AudioPlanes { planes: AudioPlaneStorage::Heap(Vec::with_capacity(n_planes)) }
204 }
205 }
206
207 fn push(&mut self, plane: &'a [S]) {
210 match &mut self.planes {
211 AudioPlaneStorage::Stack(planes) => {
212 debug_assert!(!planes.is_full());
213 planes.push(plane);
214 }
215 AudioPlaneStorage::Heap(planes) => {
216 planes.push(plane);
217 }
218 }
219 }
220
221 pub fn planes(&self) -> &[&'a [S]] {
223 match &self.planes {
224 AudioPlaneStorage::Stack(planes) => planes,
225 AudioPlaneStorage::Heap(planes) => planes,
226 }
227 }
228}
229
230enum AudioPlaneStorageMut<'a, S, const N: usize> {
232 Stack(ArrayVec<&'a mut [S], N>),
233 Heap(Vec<&'a mut [S]>),
234}
235
236pub struct AudioPlanesMut<'a, S: 'a + Sample> {
238 planes: AudioPlaneStorageMut<'a, S, AUDIO_PLANES_STORAGE_STACK_LIMIT>,
239}
240
241impl<'a, S: Sample> AudioPlanesMut<'a, S> {
242 fn new(channels: Channels) -> Self {
244 let n_planes = channels.count();
245
246 if n_planes <= AUDIO_PLANES_STORAGE_STACK_LIMIT {
247 AudioPlanesMut { planes: AudioPlaneStorageMut::Stack(ArrayVec::new()) }
248 }
249 else {
250 AudioPlanesMut { planes: AudioPlaneStorageMut::Heap(Vec::with_capacity(n_planes)) }
251 }
252 }
253
254 fn push(&mut self, plane: &'a mut [S]) {
257 match &mut self.planes {
258 AudioPlaneStorageMut::Stack(planes) => {
259 debug_assert!(!planes.is_full());
260 planes.push(plane);
261 }
262 AudioPlaneStorageMut::Heap(storage) => {
263 storage.push(plane);
264 }
265 }
266 }
267
268 pub fn planes(&mut self) -> &mut [&'a mut [S]] {
270 match &mut self.planes {
271 AudioPlaneStorageMut::Stack(planes) => planes,
272 AudioPlaneStorageMut::Heap(planes) => planes,
273 }
274 }
275}
276
277#[derive(Clone)]
283pub struct AudioBuffer<S: Sample> {
284 buf: Vec<S>,
285 spec: SignalSpec,
286 n_frames: usize,
287 n_capacity: usize,
288}
289
290impl<S: Sample> AudioBuffer<S> {
291 pub fn new(duration: Duration, spec: SignalSpec) -> Self {
294 assert!(duration <= u64::MAX / spec.channels.count() as u64, "duration too large");
296
297 let n_samples = duration * spec.channels.count() as u64;
299
300 assert!(n_samples <= (usize::MAX / mem::size_of::<S>()) as u64, "duration too large");
304
305 let buf = vec![S::MID; n_samples as usize];
307
308 AudioBuffer { buf, spec, n_frames: 0, n_capacity: duration as usize }
309 }
310
311 pub fn unused() -> Self {
314 AudioBuffer {
315 buf: Vec::with_capacity(0),
316 spec: SignalSpec::new(0, Channels::empty()),
317 n_frames: 0,
318 n_capacity: 0,
319 }
320 }
321
322 pub fn is_unused(&self) -> bool {
324 self.n_capacity == 0
325 }
326
327 pub fn spec(&self) -> &SignalSpec {
329 &self.spec
330 }
331
332 pub fn capacity(&self) -> usize {
335 self.n_capacity
336 }
337
338 pub fn planes(&self) -> AudioPlanes<S> {
344 let mut planes = AudioPlanes::new(self.spec.channels);
347
348 for channel in self.buf.chunks_exact(self.n_capacity) {
349 planes.push(&channel[..self.n_frames]);
350 }
351
352 planes
353 }
354
355 pub fn planes_mut(&mut self) -> AudioPlanesMut<S> {
362 let mut planes = AudioPlanesMut::new(self.spec.channels);
365
366 for channel in self.buf.chunks_exact_mut(self.n_capacity) {
367 planes.push(&mut channel[..self.n_frames]);
368 }
369
370 planes
371 }
372
373 pub fn convert<T: Sample>(&self, dest: &mut AudioBuffer<T>)
376 where
377 S: IntoSample<T>,
378 {
379 assert!(dest.n_capacity >= self.n_capacity);
380 assert!(dest.spec == self.spec);
381
382 for c in 0..self.spec.channels.count() {
383 let begin = c * self.n_capacity;
384 let end = begin + self.n_frames;
385
386 for (d, s) in dest.buf[begin..end].iter_mut().zip(&self.buf[begin..end]) {
387 *d = (*s).into_sample();
388 }
389 }
390
391 dest.n_frames = self.n_frames;
392 }
393
394 pub fn make_equivalent<E: Sample>(&self) -> AudioBuffer<E> {
396 AudioBuffer::<E>::new(self.n_capacity as Duration, self.spec)
397 }
398}
399
400macro_rules! impl_audio_buffer_ref_func {
401 ($var:expr, $buf:ident,$expr:expr) => {
402 match $var {
403 AudioBufferRef::U8($buf) => $expr,
404 AudioBufferRef::U16($buf) => $expr,
405 AudioBufferRef::U24($buf) => $expr,
406 AudioBufferRef::U32($buf) => $expr,
407 AudioBufferRef::S8($buf) => $expr,
408 AudioBufferRef::S16($buf) => $expr,
409 AudioBufferRef::S24($buf) => $expr,
410 AudioBufferRef::S32($buf) => $expr,
411 AudioBufferRef::F32($buf) => $expr,
412 AudioBufferRef::F64($buf) => $expr,
413 }
414 };
415}
416
417#[derive(Clone)]
419pub enum AudioBufferRef<'a> {
420 U8(Cow<'a, AudioBuffer<u8>>),
421 U16(Cow<'a, AudioBuffer<u16>>),
422 U24(Cow<'a, AudioBuffer<u24>>),
423 U32(Cow<'a, AudioBuffer<u32>>),
424 S8(Cow<'a, AudioBuffer<i8>>),
425 S16(Cow<'a, AudioBuffer<i16>>),
426 S24(Cow<'a, AudioBuffer<i24>>),
427 S32(Cow<'a, AudioBuffer<i32>>),
428 F32(Cow<'a, AudioBuffer<f32>>),
429 F64(Cow<'a, AudioBuffer<f64>>),
430}
431
432impl<'a> AudioBufferRef<'a> {
433 pub fn spec(&self) -> &SignalSpec {
435 impl_audio_buffer_ref_func!(self, buf, buf.spec())
436 }
437
438 pub fn capacity(&self) -> usize {
441 impl_audio_buffer_ref_func!(self, buf, buf.capacity())
442 }
443
444 pub fn frames(&self) -> usize {
446 impl_audio_buffer_ref_func!(self, buf, buf.frames())
447 }
448
449 pub fn convert<T>(&self, dest: &mut AudioBuffer<T>)
450 where
451 T: Sample
452 + FromSample<u8>
453 + FromSample<u16>
454 + FromSample<u24>
455 + FromSample<u32>
456 + FromSample<i8>
457 + FromSample<i16>
458 + FromSample<i24>
459 + FromSample<i32>
460 + FromSample<f32>
461 + FromSample<f64>,
462 {
463 impl_audio_buffer_ref_func!(self, buf, buf.convert(dest))
464 }
465
466 pub fn make_equivalent<E: Sample>(&self) -> AudioBuffer<E> {
467 impl_audio_buffer_ref_func!(self, buf, buf.make_equivalent::<E>())
468 }
469}
470
471pub trait AsAudioBufferRef {
474 fn as_audio_buffer_ref(&self) -> AudioBufferRef;
476}
477
478macro_rules! impl_as_audio_buffer_ref {
479 ($fmt:ty, $ref:path) => {
480 impl AsAudioBufferRef for AudioBuffer<$fmt> {
481 fn as_audio_buffer_ref(&self) -> AudioBufferRef {
482 $ref(Cow::Borrowed(self))
483 }
484 }
485 };
486}
487
488impl_as_audio_buffer_ref!(u8, AudioBufferRef::U8);
489impl_as_audio_buffer_ref!(u16, AudioBufferRef::U16);
490impl_as_audio_buffer_ref!(u24, AudioBufferRef::U24);
491impl_as_audio_buffer_ref!(u32, AudioBufferRef::U32);
492impl_as_audio_buffer_ref!(i8, AudioBufferRef::S8);
493impl_as_audio_buffer_ref!(i16, AudioBufferRef::S16);
494impl_as_audio_buffer_ref!(i24, AudioBufferRef::S24);
495impl_as_audio_buffer_ref!(i32, AudioBufferRef::S32);
496impl_as_audio_buffer_ref!(f32, AudioBufferRef::F32);
497impl_as_audio_buffer_ref!(f64, AudioBufferRef::F64);
498
499pub trait Signal<S: Sample> {
502 fn frames(&self) -> usize;
505
506 fn clear(&mut self);
509
510 fn chan(&self, channel: usize) -> &[S];
512
513 fn chan_mut(&mut self, channel: usize) -> &mut [S];
515
516 fn chan_pair_mut(&mut self, first: usize, second: usize) -> (&mut [S], &mut [S]);
518
519 fn render_silence(&mut self, n_frames: Option<usize>);
523
524 fn render_reserved(&mut self, n_frames: Option<usize>);
531
532 fn render<'a, F>(&'a mut self, n_frames: Option<usize>, render: F) -> Result<()>
537 where
538 F: FnMut(&mut AudioPlanesMut<'a, S>, usize) -> Result<()>;
539
540 #[inline]
544 fn fill<'a, F>(&'a mut self, fill: F) -> Result<()>
545 where
546 F: FnMut(&mut AudioPlanesMut<'a, S>, usize) -> Result<()>,
547 {
548 self.clear();
549 self.render(None, fill)
550 }
551
552 fn transform<F>(&mut self, f: F)
555 where
556 F: Fn(S) -> S;
557
558 fn truncate(&mut self, n_frames: usize);
561
562 fn shift(&mut self, shift: usize);
565
566 fn trim(&mut self, start: usize, end: usize) {
568 self.truncate(self.frames().saturating_sub(end));
571
572 self.shift(start);
574 }
575}
576
577impl<S: Sample> Signal<S> for AudioBuffer<S> {
578 fn clear(&mut self) {
579 self.n_frames = 0;
580 }
581
582 fn frames(&self) -> usize {
583 self.n_frames
584 }
585
586 fn chan(&self, channel: usize) -> &[S] {
587 let start = channel * self.n_capacity;
588
589 assert!(start + self.n_capacity <= self.buf.len(), "invalid channel index");
591
592 &self.buf[start..start + self.n_frames]
593 }
594
595 fn chan_mut(&mut self, channel: usize) -> &mut [S] {
596 let start = channel * self.n_capacity;
597
598 assert!(start + self.n_capacity <= self.buf.len(), "invalid channel index");
600
601 &mut self.buf[start..start + self.n_frames]
602 }
603
604 fn chan_pair_mut(&mut self, first: usize, second: usize) -> (&mut [S], &mut [S]) {
605 assert!(first != second, "channel indicies cannot be the same");
607
608 let first_idx = self.n_capacity * first;
609 let second_idx = self.n_capacity * second;
610
611 assert!(first_idx + self.n_capacity <= self.buf.len(), "invalid channel index");
613 assert!(second_idx + self.n_capacity <= self.buf.len(), "invalid channel index");
614
615 if first_idx < second_idx {
616 let (a, b) = self.buf.split_at_mut(second_idx);
617
618 (&mut a[first_idx..first_idx + self.n_frames], &mut b[..self.n_frames])
619 }
620 else {
621 let (a, b) = self.buf.split_at_mut(first_idx);
622
623 (&mut b[..self.n_frames], &mut a[second_idx..second_idx + self.n_frames])
624 }
625 }
626
627 fn render_silence(&mut self, n_frames: Option<usize>) {
628 let n_silent_frames = n_frames.unwrap_or(self.n_capacity - self.n_frames);
629
630 assert!(self.n_frames + n_silent_frames <= self.capacity(), "capacity will be exceeded");
632
633 for channel in self.buf.chunks_exact_mut(self.n_capacity) {
634 for sample in &mut channel[self.n_frames..self.n_frames + n_silent_frames] {
635 *sample = S::MID;
636 }
637 }
638
639 self.n_frames += n_silent_frames;
640 }
641
642 fn render_reserved(&mut self, n_frames: Option<usize>) {
643 let n_reserved_frames = n_frames.unwrap_or(self.n_capacity - self.n_frames);
644 assert!(self.n_frames + n_reserved_frames <= self.n_capacity, "capacity will be exceeded");
646 self.n_frames += n_reserved_frames;
647 }
648
649 fn render<'a, F>(&'a mut self, n_frames: Option<usize>, mut render: F) -> Result<()>
650 where
651 F: FnMut(&mut AudioPlanesMut<'a, S>, usize) -> Result<()>,
652 {
653 let n_render_frames = n_frames.unwrap_or(self.n_capacity - self.n_frames);
656
657 let end = self.n_frames + n_render_frames;
659 assert!(end <= self.n_capacity, "capacity will be exceeded");
660
661 let mut planes = AudioPlanesMut::new(self.spec.channels);
665
666 for channel in self.buf.chunks_exact_mut(self.n_capacity) {
667 planes.push(&mut channel[self.n_frames..end]);
668 }
669
670 while self.n_frames < end {
673 render(&mut planes, self.n_frames)?;
674 self.n_frames += 1;
675 }
676
677 Ok(())
678 }
679
680 fn transform<F>(&mut self, f: F)
681 where
682 F: Fn(S) -> S,
683 {
684 debug_assert!(self.n_frames <= self.n_capacity);
685
686 for plane in self.buf.chunks_mut(self.n_capacity) {
688 for sample in &mut plane[0..self.n_frames] {
689 *sample = f(*sample);
690 }
691 }
692 }
693
694 fn truncate(&mut self, n_frames: usize) {
695 if n_frames < self.n_frames {
696 self.n_frames = n_frames;
697 }
698 }
699
700 fn shift(&mut self, shift: usize) {
701 if shift >= self.n_frames {
702 self.clear();
703 }
704 else if shift > 0 {
705 for plane in self.buf.chunks_mut(self.n_capacity) {
707 plane.copy_within(shift..self.n_frames, 0);
708 }
709 self.n_frames -= shift;
710 }
711 }
712}
713
714pub struct SampleBuffer<S: Sample> {
718 buf: Box<[S]>,
719 n_written: usize,
720}
721
722impl<S: Sample> SampleBuffer<S> {
723 pub fn new(duration: Duration, spec: SignalSpec) -> SampleBuffer<S> {
726 assert!(duration <= u64::MAX / spec.channels.count() as u64, "duration too large");
728
729 let n_samples = duration * spec.channels.count() as u64;
731
732 assert!(n_samples <= (usize::MAX / mem::size_of::<S>()) as u64, "duration too large");
736
737 let buf = vec![S::MID; n_samples as usize].into_boxed_slice();
739
740 SampleBuffer { buf, n_written: 0 }
741 }
742
743 pub fn len(&self) -> usize {
745 self.n_written
746 }
747
748 pub fn is_empty(&self) -> bool {
750 self.n_written == 0
751 }
752
753 pub fn samples(&self) -> &[S] {
755 &self.buf[..self.n_written]
756 }
757
758 pub fn samples_mut(&mut self) -> &mut [S] {
760 &mut self.buf[..self.n_written]
761 }
762
763 pub fn capacity(&self) -> usize {
765 self.buf.len()
766 }
767
768 pub fn clear(&mut self) {
770 self.n_written = 0;
771 }
772
773 pub fn copy_planar_ref(&mut self, src: AudioBufferRef)
776 where
777 S: ConvertibleSample,
778 {
779 match src {
780 AudioBufferRef::U8(buf) => self.copy_planar_typed(&buf),
781 AudioBufferRef::U16(buf) => self.copy_planar_typed(&buf),
782 AudioBufferRef::U24(buf) => self.copy_planar_typed(&buf),
783 AudioBufferRef::U32(buf) => self.copy_planar_typed(&buf),
784 AudioBufferRef::S8(buf) => self.copy_planar_typed(&buf),
785 AudioBufferRef::S16(buf) => self.copy_planar_typed(&buf),
786 AudioBufferRef::S24(buf) => self.copy_planar_typed(&buf),
787 AudioBufferRef::S32(buf) => self.copy_planar_typed(&buf),
788 AudioBufferRef::F32(buf) => self.copy_planar_typed(&buf),
789 AudioBufferRef::F64(buf) => self.copy_planar_typed(&buf),
790 }
791 }
792
793 pub fn copy_planar_typed<F>(&mut self, src: &AudioBuffer<F>)
796 where
797 F: Sample + IntoSample<S>,
798 {
799 let n_frames = src.frames();
800 let n_channels = src.spec.channels.count();
801 let n_samples = n_frames * n_channels;
802
803 assert!(self.capacity() >= n_samples);
806
807 for ch in 0..n_channels {
808 let ch_slice = src.chan(ch);
809
810 for (dst, src) in self.buf[ch * n_frames..].iter_mut().zip(ch_slice) {
811 *dst = (*src).into_sample();
812 }
813 }
814
815 self.n_written = n_samples;
817 }
818
819 pub fn copy_interleaved_ref(&mut self, src: AudioBufferRef)
822 where
823 S: ConvertibleSample,
824 {
825 match src {
826 AudioBufferRef::U8(buf) => self.copy_interleaved_typed(&buf),
827 AudioBufferRef::U16(buf) => self.copy_interleaved_typed(&buf),
828 AudioBufferRef::U24(buf) => self.copy_interleaved_typed(&buf),
829 AudioBufferRef::U32(buf) => self.copy_interleaved_typed(&buf),
830 AudioBufferRef::S8(buf) => self.copy_interleaved_typed(&buf),
831 AudioBufferRef::S16(buf) => self.copy_interleaved_typed(&buf),
832 AudioBufferRef::S24(buf) => self.copy_interleaved_typed(&buf),
833 AudioBufferRef::S32(buf) => self.copy_interleaved_typed(&buf),
834 AudioBufferRef::F32(buf) => self.copy_interleaved_typed(&buf),
835 AudioBufferRef::F64(buf) => self.copy_interleaved_typed(&buf),
836 }
837 }
838
839 pub fn copy_interleaved_typed<F>(&mut self, src: &AudioBuffer<F>)
842 where
843 F: Sample + IntoSample<S>,
844 {
845 let n_channels = src.spec.channels.count();
846 let n_samples = src.frames() * n_channels;
847
848 assert!(self.capacity() >= n_samples);
851
852 for ch in 0..n_channels {
854 let ch_slice = src.chan(ch);
855
856 for (dst, src) in self.buf[ch..].iter_mut().step_by(n_channels).zip(ch_slice) {
857 *dst = (*src).into_sample();
858 }
859 }
860
861 self.n_written = n_samples;
863 }
864}
865
866mod sealed {
872 pub trait Sealed: bytemuck::Pod {}
873}
874
875impl sealed::Sealed for u8 {}
876impl sealed::Sealed for i8 {}
877impl sealed::Sealed for u16 {}
878impl sealed::Sealed for i16 {}
879impl sealed::Sealed for u32 {}
880impl sealed::Sealed for i32 {}
881impl sealed::Sealed for u64 {}
882impl sealed::Sealed for i64 {}
883impl sealed::Sealed for f32 {}
884impl sealed::Sealed for f64 {}
885impl sealed::Sealed for [u8; 1] {}
886impl sealed::Sealed for [u8; 2] {}
887impl sealed::Sealed for [u8; 3] {}
888impl sealed::Sealed for [u8; 4] {}
889impl sealed::Sealed for [u8; 5] {}
890impl sealed::Sealed for [u8; 6] {}
891impl sealed::Sealed for [u8; 7] {}
892impl sealed::Sealed for [u8; 8] {}
893
894pub trait RawSample: Sample {
897 type RawType: Copy + Default + sealed::Sealed;
900
901 fn into_raw_sample(self) -> Self::RawType;
902}
903
904impl RawSample for u8 {
905 type RawType = u8;
906
907 #[inline(always)]
908 fn into_raw_sample(self) -> Self::RawType {
909 self
910 }
911}
912
913impl RawSample for i8 {
914 type RawType = i8;
915
916 #[inline(always)]
917 fn into_raw_sample(self) -> Self::RawType {
918 self
919 }
920}
921
922impl RawSample for u16 {
923 type RawType = u16;
924
925 #[inline(always)]
926 fn into_raw_sample(self) -> Self::RawType {
927 self
928 }
929}
930
931impl RawSample for i16 {
932 type RawType = i16;
933
934 #[inline(always)]
935 fn into_raw_sample(self) -> Self::RawType {
936 self
937 }
938}
939
940impl RawSample for u24 {
941 type RawType = [u8; 3];
942
943 #[inline(always)]
944 fn into_raw_sample(self) -> Self::RawType {
945 self.to_ne_bytes()
946 }
947}
948
949impl RawSample for i24 {
950 type RawType = [u8; 3];
951
952 #[inline(always)]
953 fn into_raw_sample(self) -> Self::RawType {
954 self.to_ne_bytes()
955 }
956}
957
958impl RawSample for u32 {
959 type RawType = u32;
960
961 #[inline(always)]
962 fn into_raw_sample(self) -> Self::RawType {
963 self
964 }
965}
966
967impl RawSample for i32 {
968 type RawType = i32;
969
970 #[inline(always)]
971 fn into_raw_sample(self) -> Self::RawType {
972 self
973 }
974}
975
976impl RawSample for f32 {
977 type RawType = f32;
978
979 #[inline(always)]
980 fn into_raw_sample(self) -> Self::RawType {
981 self
982 }
983}
984
985impl RawSample for f64 {
986 type RawType = f64;
987
988 #[inline(always)]
989 fn into_raw_sample(self) -> Self::RawType {
990 self
991 }
992}
993
994pub struct RawSampleBuffer<S: Sample + RawSample> {
998 buf: Box<[S::RawType]>,
999 n_written: usize,
1000 sample_format: PhantomData<S>,
1002}
1003
1004impl<S: Sample + RawSample> RawSampleBuffer<S> {
1005 pub fn new(duration: Duration, spec: SignalSpec) -> RawSampleBuffer<S> {
1008 assert!(duration <= u64::MAX / spec.channels.count() as u64, "duration too large");
1010
1011 let n_samples = duration * spec.channels.count() as u64;
1013
1014 assert!(
1018 n_samples <= (usize::MAX / mem::size_of::<S::RawType>()) as u64,
1019 "duration too large"
1020 );
1021
1022 let buf = vec![S::MID.into_raw_sample(); n_samples as usize].into_boxed_slice();
1024
1025 RawSampleBuffer { buf, n_written: 0, sample_format: PhantomData }
1026 }
1027
1028 pub fn len(&self) -> usize {
1030 self.n_written
1031 }
1032
1033 pub fn is_empty(&self) -> bool {
1035 self.n_written == 0
1036 }
1037
1038 pub fn capacity(&self) -> usize {
1040 self.buf.len()
1041 }
1042
1043 pub fn clear(&mut self) {
1045 self.n_written = 0;
1046 }
1047
1048 pub fn as_bytes(&self) -> &[u8] {
1050 bytemuck::cast_slice(&self.buf[..self.n_written])
1054 }
1055
1056 pub fn copy_planar_ref(&mut self, src: AudioBufferRef)
1059 where
1060 S: ConvertibleSample,
1061 {
1062 match src {
1063 AudioBufferRef::U8(buf) => self.copy_planar_typed(&buf),
1064 AudioBufferRef::U16(buf) => self.copy_planar_typed(&buf),
1065 AudioBufferRef::U24(buf) => self.copy_planar_typed(&buf),
1066 AudioBufferRef::U32(buf) => self.copy_planar_typed(&buf),
1067 AudioBufferRef::S8(buf) => self.copy_planar_typed(&buf),
1068 AudioBufferRef::S16(buf) => self.copy_planar_typed(&buf),
1069 AudioBufferRef::S24(buf) => self.copy_planar_typed(&buf),
1070 AudioBufferRef::S32(buf) => self.copy_planar_typed(&buf),
1071 AudioBufferRef::F32(buf) => self.copy_planar_typed(&buf),
1072 AudioBufferRef::F64(buf) => self.copy_planar_typed(&buf),
1073 }
1074 }
1075
1076 pub fn copy_planar_typed<F>(&mut self, src: &AudioBuffer<F>)
1080 where
1081 F: Sample + IntoSample<S>,
1082 {
1083 let n_channels = src.spec.channels.count();
1084 let n_samples = n_channels * src.n_frames;
1085
1086 assert!(self.capacity() >= n_samples);
1089
1090 let dst_buf = &mut self.buf[..n_samples];
1091
1092 for (ch, dst_ch) in dst_buf.chunks_exact_mut(src.n_frames).enumerate() {
1093 let src_ch = src.chan(ch);
1094
1095 for (&s, d) in src_ch.iter().zip(dst_ch) {
1096 *d = s.into_sample().into_raw_sample();
1097 }
1098 }
1099
1100 self.n_written = n_samples;
1101 }
1102
1103 pub fn copy_planar(&mut self, src: &AudioBuffer<S>) {
1106 let n_channels = src.spec.channels.count();
1107 let n_samples = src.n_frames * n_channels;
1108
1109 assert!(self.capacity() >= n_samples);
1112
1113 let dst_buf = &mut self.buf[..n_samples];
1114
1115 for (ch, dst_ch) in dst_buf.chunks_exact_mut(src.n_frames).enumerate() {
1116 let src_ch = src.chan(ch);
1117
1118 for (&s, d) in src_ch.iter().zip(dst_ch) {
1119 *d = s.into_raw_sample();
1120 }
1121 }
1122
1123 self.n_written = n_samples;
1124 }
1125
1126 pub fn copy_interleaved_ref(&mut self, src: AudioBufferRef)
1129 where
1130 S: ConvertibleSample,
1131 {
1132 match src {
1133 AudioBufferRef::U8(buf) => self.copy_interleaved_typed(&buf),
1134 AudioBufferRef::U16(buf) => self.copy_interleaved_typed(&buf),
1135 AudioBufferRef::U24(buf) => self.copy_interleaved_typed(&buf),
1136 AudioBufferRef::U32(buf) => self.copy_interleaved_typed(&buf),
1137 AudioBufferRef::S8(buf) => self.copy_interleaved_typed(&buf),
1138 AudioBufferRef::S16(buf) => self.copy_interleaved_typed(&buf),
1139 AudioBufferRef::S24(buf) => self.copy_interleaved_typed(&buf),
1140 AudioBufferRef::S32(buf) => self.copy_interleaved_typed(&buf),
1141 AudioBufferRef::F32(buf) => self.copy_interleaved_typed(&buf),
1142 AudioBufferRef::F64(buf) => self.copy_interleaved_typed(&buf),
1143 }
1144 }
1145
1146 pub fn copy_interleaved_typed<F>(&mut self, src: &AudioBuffer<F>)
1150 where
1151 F: Sample + IntoSample<S>,
1152 {
1153 let n_frames = src.n_frames;
1154 let n_channels = src.spec.channels.count();
1155 let n_samples = n_frames * n_channels;
1156
1157 assert!(self.capacity() >= n_samples);
1160
1161 let dst_buf = &mut self.buf[..n_samples];
1163
1164 match n_channels {
1166 0 => (),
1168 1 => {
1170 for (&s, d) in src.chan(0).iter().zip(dst_buf) {
1171 *d = s.into_sample().into_raw_sample();
1172 }
1173 }
1174 2 => {
1176 let l_buf = src.chan(0);
1177 let r_buf = src.chan(1);
1178
1179 for ((&l, &r), d) in l_buf.iter().zip(r_buf).zip(dst_buf.chunks_exact_mut(2)) {
1180 d[0] = l.into_sample().into_raw_sample();
1181 d[1] = r.into_sample().into_raw_sample();
1182 }
1183 }
1184 _ => {
1186 for ch in 0..n_channels {
1187 let src_ch = src.chan(ch);
1188 let dst_ch_iter = dst_buf[ch..].iter_mut().step_by(n_channels);
1189
1190 for (&s, d) in src_ch.iter().zip(dst_ch_iter) {
1191 *d = s.into_sample().into_raw_sample();
1192 }
1193 }
1194 }
1195 }
1196
1197 self.n_written = n_samples;
1198 }
1199
1200 pub fn copy_interleaved(&mut self, src: &AudioBuffer<S>) {
1203 let n_frames = src.n_frames;
1204 let n_channels = src.spec.channels.count();
1205 let n_samples = n_frames * n_channels;
1206
1207 assert!(self.capacity() >= n_samples);
1210
1211 let dst_buf = &mut self.buf[..n_samples];
1213
1214 match n_channels {
1216 0 => (),
1218 1 => {
1220 for (&s, d) in src.chan(0).iter().zip(dst_buf) {
1221 *d = s.into_raw_sample();
1222 }
1223 }
1224 2 => {
1226 let l_buf = src.chan(0);
1227 let r_buf = src.chan(1);
1228
1229 for ((&l, &r), d) in l_buf.iter().zip(r_buf).zip(dst_buf.chunks_exact_mut(2)) {
1230 d[0] = l.into_raw_sample();
1231 d[1] = r.into_raw_sample();
1232 }
1233 }
1234 _ => {
1236 for ch in 0..n_channels {
1237 let src_ch = src.chan(ch);
1238 let dst_ch_iter = dst_buf[ch..].iter_mut().step_by(n_channels);
1239
1240 for (&s, d) in src_ch.iter().zip(dst_ch_iter) {
1241 *d = s.into_raw_sample();
1242 }
1243 }
1244 }
1245 }
1246
1247 self.n_written = n_samples;
1248 }
1249}