From cea643c4379d9a1c3fd027c4c53b66f70e5b005c Mon Sep 17 00:00:00 2001 From: Jeron Aldaron Lau Date: Sun, 13 Oct 2024 23:01:35 -0500 Subject: [PATCH] Some missed ones --- examples/mix.rs | 22 ++++---- src/audio.rs | 138 ++++++++++++++++++++++++------------------------ src/frame.rs | 80 ++++++++++++++-------------- src/sink.rs | 24 ++++----- src/stream.rs | 28 +++++----- 5 files changed, 146 insertions(+), 146 deletions(-) diff --git a/examples/mix.rs b/examples/mix.rs index 365fff1..c5e1137 100644 --- a/examples/mix.rs +++ b/examples/mix.rs @@ -8,15 +8,15 @@ use fon::{ }; #[derive(Debug)] -pub struct Mixer<'a, Samp: Sample, const CH: usize> { +pub struct Mixer<'a, Samp: Sample, const N: usize> { index: usize, - audio: &'a mut Audio, + audio: &'a mut Audio, } #[allow(single_use_lifetimes)] -impl<'a, Samp: Sample, const CH: usize> Mixer<'a, Samp, CH> { +impl<'a, Samp: Sample, const N: usize> Mixer<'a, Samp, N> { #[inline(always)] - fn new(audio: &'a mut Audio) -> Self { + fn new(audio: &'a mut Audio) -> Self { let index = 0; Mixer { index, audio } @@ -25,8 +25,8 @@ impl<'a, Samp: Sample, const CH: usize> Mixer<'a, Samp, CH> { // Using '_ results in reserved lifetime error. #[allow(single_use_lifetimes)] -impl<'a, Samp: Sample, const CH: usize> Sink - for Mixer<'a, Samp, CH> +impl<'a, Samp: Sample, const N: usize> Sink + for Mixer<'a, Samp, N> { #[inline(always)] fn sample_rate(&self) -> NonZeroU32 { @@ -39,14 +39,14 @@ impl<'a, Samp: Sample, const CH: usize> Sink } #[inline(always)] - fn sink_with(&mut self, iter: &mut dyn Iterator>) { + fn sink_with(&mut self, iter: &mut dyn Iterator>) { let mut this = self; - Sink::::sink_with(&mut this, iter) + Sink::::sink_with(&mut this, iter) } } -impl Sink - for &mut Mixer<'_, Samp, CH> +impl Sink + for &mut Mixer<'_, Samp, N> { #[inline(always)] fn sample_rate(&self) -> NonZeroU32 { @@ -59,7 +59,7 @@ impl Sink } #[inline(always)] - fn sink_with(&mut self, iter: &mut dyn Iterator>) { + fn sink_with(&mut self, iter: &mut dyn Iterator>) { for frame in self.audio.iter_mut().skip(self.index) { if let Some(other) = iter.next() { for (channel, chan) in diff --git a/src/audio.rs b/src/audio.rs index e663ea6..ecaf85a 100644 --- a/src/audio.rs +++ b/src/audio.rs @@ -20,18 +20,18 @@ use crate::{ /// Audio buffer (fixed-size array of audio [`Frame`](crate::frame::Frame)s at /// sample rate specified in hertz). #[derive(Debug)] -pub struct Audio { +pub struct Audio { // Sample rate of the audio in hertz. sample_rate: NonZeroU32, // Audio frames. - frames: Box<[Frame]>, + frames: Box<[Frame]>, } -impl Audio { +impl Audio { /// Construct an `Audio` buffer with all all samples set to zero. #[inline(always)] pub fn with_silence(hz: u32, len: usize) -> Self { - Self::with_frames(hz, vec![Frame::::default(); len]) + Self::with_frames(hz, vec![Frame::::default(); len]) } /// Construct an `Audio` buffer with owned sample data. You can get @@ -40,7 +40,7 @@ impl Audio { #[inline(always)] pub fn with_frames(hz: u32, frames: B) -> Self where - B: Into]>>, + B: Into]>>, { Audio { sample_rate: hz.try_into().unwrap(), @@ -61,7 +61,7 @@ impl Audio { audio.len() as f64 * hz as f64 / audio.sample_rate().get() as f64; let mut output = Self::with_silence(hz, len.ceil() as usize); let mut stream = Stream::new(hz); - let mut sink = crate::SinkTo::<_, Samp, _, CH, N>::new(output.sink()); + let mut sink = crate::SinkTo::<_, Samp, _, COUNT, N>::new(output.sink()); stream.pipe(audio, &mut sink); stream.flush(&mut sink); output @@ -69,37 +69,37 @@ impl Audio { /// Get an audio frame. #[inline(always)] - pub fn get(&self, index: usize) -> Option> { + pub fn get(&self, index: usize) -> Option> { self.frames.get(index).cloned() } /// Get a mutable reference to an audio frame. #[inline(always)] - pub fn get_mut(&mut self, index: usize) -> Option<&mut Frame> { + pub fn get_mut(&mut self, index: usize) -> Option<&mut Frame> { self.frames.get_mut(index) } /// Get a slice of all audio frames. #[inline(always)] - pub fn as_slice(&self) -> &[Frame] { + pub fn as_slice(&self) -> &[Frame] { &self.frames } /// Get a slice of all audio frames. #[inline(always)] - pub fn as_mut_slice(&mut self) -> &mut [Frame] { + pub fn as_mut_slice(&mut self) -> &mut [Frame] { &mut self.frames } /// Returns an iterator over the audio frames. #[inline(always)] - pub fn iter(&self) -> Iter<'_, Frame> { + pub fn iter(&self) -> Iter<'_, Frame> { self.frames.iter() } /// Returns an iterator that allows modifying each audio frame. #[inline(always)] - pub fn iter_mut(&mut self) -> IterMut<'_, Frame> { + pub fn iter_mut(&mut self) -> IterMut<'_, Frame> { self.frames.iter_mut() } @@ -125,13 +125,13 @@ impl Audio { #[inline(always)] pub fn silence(&mut self) { for f in self.frames.iter_mut() { - *f = Frame::::default() + *f = Frame::::default() } } /// Sink audio into this audio buffer from a `Stream`. #[inline(always)] - pub fn sink(&mut self) -> AudioSink<'_, Samp, CH> { + pub fn sink(&mut self) -> AudioSink<'_, Samp, COUNT> { AudioSink { index: 0, audio: self, @@ -141,15 +141,15 @@ impl Audio { /// Returned from [`Audio::sink()`](crate::Audio::sink). #[derive(Debug)] -pub struct AudioSink<'a, Samp: Sample, const CH: usize> { +pub struct AudioSink<'a, Samp: Sample, const COUNT: usize> { index: usize, - audio: &'a mut Audio, + audio: &'a mut Audio, } // Using '_ results in reserved lifetime error. #[allow(single_use_lifetimes)] -impl<'a, Samp: Sample, const CH: usize> Sink - for AudioSink<'a, Samp, CH> +impl<'a, Samp: Sample, const COUNT: usize> Sink + for AudioSink<'a, Samp, COUNT> { #[inline(always)] fn sample_rate(&self) -> NonZeroU32 { @@ -162,14 +162,14 @@ impl<'a, Samp: Sample, const CH: usize> Sink } #[inline(always)] - fn sink_with(&mut self, iter: &mut dyn Iterator>) { + fn sink_with(&mut self, iter: &mut dyn Iterator>) { let mut this = self; - Sink::::sink_with(&mut this, iter) + Sink::::sink_with(&mut this, iter) } } -impl Sink - for &mut AudioSink<'_, Samp, CH> +impl Sink + for &mut AudioSink<'_, Samp, COUNT> { #[inline(always)] fn sample_rate(&self) -> NonZeroU32 { @@ -182,7 +182,7 @@ impl Sink } #[inline(always)] - fn sink_with(&mut self, iter: &mut dyn Iterator>) { + fn sink_with(&mut self, iter: &mut dyn Iterator>) { for frame in self.audio.iter_mut().skip(self.index) { *frame = if let Some(frame) = iter.next() { frame @@ -194,7 +194,7 @@ impl Sink } } -impl Audio { +impl Audio { /// Construct an `Audio` buffer from an `i16` buffer. #[allow(unsafe_code)] pub fn with_i16_buffer(hz: u32, buffer: B) -> Self @@ -203,14 +203,14 @@ impl Audio { { let buffer: Box<[i16]> = buffer.into(); let bytes = buffer.len() * size_of::(); - let len = bytes / size_of::>(); - assert_eq!(0, bytes % size_of::>()); + let len = bytes / size_of::>(); + assert_eq!(0, bytes % size_of::>()); let slice = Box::<[i16]>::into_raw(buffer); - let frames: Box<[Frame]> = unsafe { - let ptr = (*slice).as_mut_ptr() as *mut Frame; + let frames: Box<[Frame]> = unsafe { + let ptr = (*slice).as_mut_ptr() as *mut Frame; Box::from_raw(from_raw_parts_mut(ptr, len)) }; - let frames: Vec> = frames.into(); + let frames: Vec> = frames.into(); Audio::with_frames(hz, frames) } @@ -227,7 +227,7 @@ impl Audio { } } -impl Audio { +impl Audio { /// Construct an `Audio` buffer from an `u8` buffer. #[allow(unsafe_code)] pub fn with_u8_buffer(hz: u32, buffer: B) -> Self @@ -236,14 +236,14 @@ impl Audio { { let buffer: Box<[u8]> = buffer.into(); let bytes = buffer.len() * size_of::(); - let len = bytes / size_of::>(); - assert_eq!(0, bytes % size_of::>()); + let len = bytes / size_of::>(); + assert_eq!(0, bytes % size_of::>()); let slice = Box::<[u8]>::into_raw(buffer); - let frames: Box<[Frame]> = unsafe { - let ptr = (*slice).as_mut_ptr() as *mut Frame; + let frames: Box<[Frame]> = unsafe { + let ptr = (*slice).as_mut_ptr() as *mut Frame; Box::from_raw(from_raw_parts_mut(ptr, len)) }; - let frames: Vec> = frames.into(); + let frames: Vec> = frames.into(); Audio::with_frames(hz, frames) } @@ -260,7 +260,7 @@ impl Audio { } } -impl Audio { +impl Audio { /// Construct an `Audio` buffer from an `f32` buffer. #[allow(unsafe_code)] pub fn with_f32_buffer(hz: u32, buffer: B) -> Self @@ -269,14 +269,14 @@ impl Audio { { let buffer: Box<[f32]> = buffer.into(); let bytes = buffer.len() * size_of::(); - let len = bytes / size_of::>(); - assert_eq!(0, bytes % size_of::>()); + let len = bytes / size_of::>(); + assert_eq!(0, bytes % size_of::>()); let slice = Box::<[f32]>::into_raw(buffer); - let frames: Box<[Frame]> = unsafe { - let ptr = (*slice).as_mut_ptr() as *mut Frame; + let frames: Box<[Frame]> = unsafe { + let ptr = (*slice).as_mut_ptr() as *mut Frame; Box::from_raw(from_raw_parts_mut(ptr, len)) }; - let frames: Vec> = frames.into(); + let frames: Vec> = frames.into(); Audio::with_frames(hz, frames) } @@ -293,7 +293,7 @@ impl Audio { } } -impl Audio { +impl Audio { /// Construct an `Audio` buffer from an `f64` buffer. #[allow(unsafe_code)] pub fn with_f64_buffer(hz: u32, buffer: B) -> Self @@ -302,14 +302,14 @@ impl Audio { { let buffer: Box<[f64]> = buffer.into(); let bytes = buffer.len() * size_of::(); - let len = bytes / size_of::>(); - assert_eq!(0, bytes % size_of::>()); + let len = bytes / size_of::>(); + assert_eq!(0, bytes % size_of::>()); let slice = Box::<[f64]>::into_raw(buffer); - let frames: Box<[Frame]> = unsafe { - let ptr = (*slice).as_mut_ptr() as *mut Frame; + let frames: Box<[Frame]> = unsafe { + let ptr = (*slice).as_mut_ptr() as *mut Frame; Box::from_raw(from_raw_parts_mut(ptr, len)) }; - let frames: Vec> = frames.into(); + let frames: Vec> = frames.into(); Audio::with_frames(hz, frames) } @@ -326,32 +326,32 @@ impl Audio { } } -impl From> for Vec> +impl From> for Vec> where Samp: Sample, { /// Get internal sample data as `Vec` of audio frames. - fn from(audio: Audio) -> Self { + fn from(audio: Audio) -> Self { audio.frames.into() } } -impl From> - for Box<[Frame]> +impl From> + for Box<[Frame]> { /// Get internal sample data as `Vec` of audio frames. - fn from(audio: Audio) -> Self { - let audio: Vec> = audio.frames.into(); + fn from(audio: Audio) -> Self { + let audio: Vec> = audio.frames.into(); audio.into() } } -impl From> for Box<[i16]> { +impl From> for Box<[i16]> { /// Get internal sample data as boxed slice of *i16*. #[allow(unsafe_code)] - fn from(audio: Audio) -> Self { - let mut frames: Vec> = audio.frames.into(); - let capacity = frames.len() * size_of::>() / 2; + fn from(audio: Audio) -> Self { + let mut frames: Vec> = audio.frames.into(); + let capacity = frames.len() * size_of::>() / 2; let buffer: Box<[i16]> = unsafe { let ptr = frames.as_mut_ptr() as *mut i16; Box::from_raw(from_raw_parts_mut(ptr, capacity)) @@ -360,12 +360,12 @@ impl From> for Box<[i16]> { } } -impl From> for Box<[u8]> { +impl From> for Box<[u8]> { /// Get internal sample data as boxed slice of *u8*. #[allow(unsafe_code)] - fn from(audio: Audio) -> Self { - let mut frames: Vec> = audio.frames.into(); - let capacity = frames.len() * size_of::>() / 3; + fn from(audio: Audio) -> Self { + let mut frames: Vec> = audio.frames.into(); + let capacity = frames.len() * size_of::>() / 3; let buffer: Box<[u8]> = unsafe { let ptr = frames.as_mut_ptr() as *mut u8; Box::from_raw(from_raw_parts_mut(ptr, capacity)) @@ -374,12 +374,12 @@ impl From> for Box<[u8]> { } } -impl From> for Box<[f32]> { +impl From> for Box<[f32]> { /// Get internal sample data as boxed slice of *f32*. #[allow(unsafe_code)] - fn from(audio: Audio) -> Self { - let mut frames: Vec> = audio.frames.into(); - let capacity = frames.len() * size_of::>() / 4; + fn from(audio: Audio) -> Self { + let mut frames: Vec> = audio.frames.into(); + let capacity = frames.len() * size_of::>() / 4; let buffer: Box<[f32]> = unsafe { let ptr = frames.as_mut_ptr() as *mut f32; Box::from_raw(from_raw_parts_mut(ptr, capacity)) @@ -388,12 +388,12 @@ impl From> for Box<[f32]> { } } -impl From> for Box<[f64]> { +impl From> for Box<[f64]> { /// Get internal sample data as boxed slice of *f64*. #[allow(unsafe_code)] - fn from(audio: Audio) -> Self { - let mut frames: Vec> = audio.frames.into(); - let capacity = frames.len() * size_of::>() / 8; + fn from(audio: Audio) -> Self { + let mut frames: Vec> = audio.frames.into(); + let capacity = frames.len() * size_of::>() / 8; let buffer: Box<[f64]> = unsafe { let ptr = frames.as_mut_ptr() as *mut f64; Box::from_raw(from_raw_parts_mut(ptr, capacity)) diff --git a/src/frame.rs b/src/frame.rs index eb46c32..f7c1861 100644 --- a/src/frame.rs +++ b/src/frame.rs @@ -13,24 +13,24 @@ use crate::math::Libm; /// Frame - A number of interleaved [`Sample`]s #[repr(transparent)] #[derive(Copy, Clone, Debug, PartialEq)] -pub struct Frame([Samp; CH]); +pub struct Frame([Samp; COUNT]); -impl Default for Frame { +impl Default for Frame { fn default() -> Self { - Frame([Samp::default(); CH]) + Frame([Samp::default(); COUNT]) } } -impl Frame { +impl Frame { /// Get a mutable slice of the samples in this frame. #[inline(always)] - pub fn samples_mut(&mut self) -> &mut [Samp; CH] { + pub fn samples_mut(&mut self) -> &mut [Samp; COUNT] { &mut self.0 } /// Get a slice of the samples in this frame. #[inline(always)] - pub fn samples(&self) -> &[Samp; CH] { + pub fn samples(&self) -> &[Samp; COUNT] { &self.0 } @@ -39,16 +39,16 @@ impl Frame { /// 1.0/0.0 is straight ahead, 0.25 is right, 0.5 is back, and 0.75 is left. /// The algorithm used is "Constant Power Panning". #[inline(always)] - pub fn pan>(self, channel: C, angle: f32) -> Self { - match CH { - 1 => self.pan_1(channel.into(), angle.rem_euclid(1.0)), - 2 => self.pan_2(channel.into(), angle.rem_euclid(1.0)), - 3 => self.pan_3(channel.into(), angle.rem_euclid(1.0)), - 4 => self.pan_4(channel.into(), angle.rem_euclid(1.0)), - 5 => self.pan_5(channel.into(), angle.rem_euclid(1.0)), - 6 => self.pan_6(channel.into(), angle.rem_euclid(1.0)), - 7 => self.pan_7(channel.into(), angle.rem_euclid(1.0)), - 8 => self.pan_8(channel.into(), angle.rem_euclid(1.0)), + pub fn pan>(self, sample: S, angle: f32) -> Self { + match COUNT { + 1 => self.pan_1(sample.into(), angle.rem_euclid(1.0)), + 2 => self.pan_2(sample.into(), angle.rem_euclid(1.0)), + 3 => self.pan_3(sample.into(), angle.rem_euclid(1.0)), + 4 => self.pan_4(sample.into(), angle.rem_euclid(1.0)), + 5 => self.pan_5(sample.into(), angle.rem_euclid(1.0)), + 6 => self.pan_6(sample.into(), angle.rem_euclid(1.0)), + 7 => self.pan_7(sample.into(), angle.rem_euclid(1.0)), + 8 => self.pan_8(sample.into(), angle.rem_euclid(1.0)), _ => unreachable!(), } } @@ -72,8 +72,8 @@ impl Frame { /// Convert an audio Frame to another format. #[inline(always)] - pub fn to, const N: usize>(self) -> Frame { - match CH { + pub fn to, const N: usize>(self) -> Frame { + match COUNT { 1 => self.to_1(), 2 => self.to_2(), 3 => self.to_3(), @@ -385,10 +385,10 @@ impl Frame { } #[inline(always)] - fn to_1, const N: usize>(self) -> Frame { + fn to_1, const N: usize>(self) -> Frame { const MONO: usize = 0; - let mut frame = Frame::::default(); + let mut frame = Frame::::default(); let mono = self.0[MONO].into(); frame.0[0] = mono; if N == 1 { @@ -400,11 +400,11 @@ impl Frame { } #[inline(always)] - fn to_2, const N: usize>(self) -> Frame { + fn to_2, const N: usize>(self) -> Frame { const LEFT: usize = 0; const RIGHT: usize = 1; - let mut frame = Frame::::default(); + let mut frame = Frame::::default(); let left = self.0[LEFT].into(); let right = self.0[RIGHT].into(); if N == 1 { @@ -419,12 +419,12 @@ impl Frame { } #[inline(always)] - fn to_3, const N: usize>(self) -> Frame { + fn to_3, const N: usize>(self) -> Frame { const LEFT: usize = 0; const RIGHT: usize = 1; const CENTER: usize = 2; - let mut frame = Frame::::default(); + let mut frame = Frame::::default(); let left = self.0[LEFT].into(); let right = self.0[RIGHT].into(); let center = self.0[CENTER].into(); @@ -458,7 +458,7 @@ impl Frame { } #[inline(always)] - fn to_4, const N: usize>(self) -> Frame { + fn to_4, const N: usize>(self) -> Frame { const FRONT_L: usize = 0; const FRONT_R: usize = 1; const SURROUND_L: usize = 2; @@ -471,7 +471,7 @@ impl Frame { let surround_r = self.0[SURROUND_R]; // Amplitude reduction. let amplitude = (N as f32 / 4.0).min(1.0); - Frame::::default() + Frame::::default() .pan(front_l * amplitude.into(), -30.0 / 360.0) .pan(front_r * amplitude.into(), 30.0 / 360.0) .pan(surround_l * amplitude.into(), -110.0 / 360.0) @@ -479,7 +479,7 @@ impl Frame { } #[inline(always)] - fn to_5, const N: usize>(self) -> Frame { + fn to_5, const N: usize>(self) -> Frame { const FRONT_L: usize = 0; const FRONT_R: usize = 1; const FRONT: usize = 2; @@ -494,7 +494,7 @@ impl Frame { let front = self.0[FRONT]; // Amplitude reduction. let amplitude = (N as f32 / 5.0).min(1.0); - Frame::::default() + Frame::::default() .pan(front_l * amplitude.into(), -30.0 / 360.0) .pan(front_r * amplitude.into(), 30.0 / 360.0) .pan(surround_l * amplitude.into(), -110.0 / 360.0) @@ -503,7 +503,7 @@ impl Frame { } #[inline(always)] - fn to_6, const N: usize>(self) -> Frame { + fn to_6, const N: usize>(self) -> Frame { const FRONT_L: usize = 0; const FRONT_R: usize = 1; const FRONT: usize = 2; @@ -520,7 +520,7 @@ impl Frame { let lfe = self.0[LFE]; // Amplitude reduction. let amplitude = (N as f32 / 5.0).min(1.0); - let mut frame = Frame::::default() + let mut frame = Frame::::default() .pan(front_l * amplitude.into(), -30.0 / 360.0) .pan(front_r * amplitude.into(), 30.0 / 360.0) .pan(surround_l * amplitude.into(), -110.0 / 360.0) @@ -536,7 +536,7 @@ impl Frame { } #[inline(always)] - fn to_7, const N: usize>(self) -> Frame { + fn to_7, const N: usize>(self) -> Frame { const FRONT_L: usize = 0; const FRONT_R: usize = 1; const FRONT: usize = 2; @@ -555,7 +555,7 @@ impl Frame { let back = self.0[BACK]; // Amplitude reduction. let amplitude = (N as f32 / 6.0).min(1.0); - let mut frame = Frame::::default() + let mut frame = Frame::::default() .pan(front_l * amplitude.into(), -30.0 / 360.0) .pan(front_r * amplitude.into(), 30.0 / 360.0) .pan(left * amplitude.into(), -90.0 / 360.0) @@ -572,7 +572,7 @@ impl Frame { } #[inline(always)] - fn to_8, const N: usize>(self) -> Frame { + fn to_8, const N: usize>(self) -> Frame { const FRONT_L: usize = 0; const FRONT_R: usize = 1; const FRONT: usize = 2; @@ -593,7 +593,7 @@ impl Frame { let back_r = self.0[BACK_R]; // Amplitude reduction. let amplitude = (N as f32 / 7.0).min(1.0); - let mut frame = Frame::::default() + let mut frame = Frame::::default() .pan(front_l * amplitude.into(), -30.0 / 360.0) .pan(front_r * amplitude.into(), 30.0 / 360.0) .pan(left * amplitude.into(), -90.0 / 360.0) @@ -714,13 +714,13 @@ impl Frame { } } -impl From for Frame { +impl From for Frame { fn from(rhs: f32) -> Self { - Frame([Samp::from(rhs); CH]) + Frame([Samp::from(rhs); COUNT]) } } -impl Add for Frame { +impl Add for Frame { type Output = Self; #[inline(always)] @@ -732,7 +732,7 @@ impl Add for Frame { } } -impl Sub for Frame { +impl Sub for Frame { type Output = Self; #[inline(always)] @@ -744,7 +744,7 @@ impl Sub for Frame { } } -impl Mul for Frame { +impl Mul for Frame { type Output = Self; #[inline(always)] @@ -756,7 +756,7 @@ impl Mul for Frame { } } -impl Neg for Frame { +impl Neg for Frame { type Output = Self; #[inline(always)] diff --git a/src/sink.rs b/src/sink.rs index 954b77f..b576c9d 100644 --- a/src/sink.rs +++ b/src/sink.rs @@ -3,7 +3,7 @@ use core::{fmt::Debug, num::NonZeroU32}; use crate::{samp::Sample, Frame}; /// Audio sink - a type that consumes audio samples. -pub trait Sink: Debug { +pub trait Sink: Debug { /// Get the sample rate of the sink in hertz. fn sample_rate(&self) -> NonZeroU32; @@ -17,7 +17,7 @@ pub trait Sink: Debug { /// **Warning**: if used incorrectly, this method may introduce audio /// aliasing. To avoid that, make sure the sample rate of the frames from /// the iterator matches exactly the sample rate of the sink. - fn sink_with(&mut self, iter: &mut dyn Iterator>); + fn sink_with(&mut self, iter: &mut dyn Iterator>); /// Check if the sink is empty (length of zero). fn is_empty(&self) -> bool { @@ -28,21 +28,21 @@ pub trait Sink: Debug { /// Sink that converts to a different audio format before passing to another /// [`Sink`](crate::Sink). #[derive(Debug)] -pub struct SinkTo +pub struct SinkTo where Samp: Sample + From, S: Sample, - K: Sink, + K: Sink, { sink: K, _phantom: core::marker::PhantomData (Samp, S)>, } -impl SinkTo +impl SinkTo where Samp: Sample + From, S: Sample, - K: Sink, + K: Sink, { /// Convert an arbitrary `Sink` type to a different format. pub fn new(sink: K) -> Self { @@ -54,12 +54,12 @@ where } #[allow(single_use_lifetimes)] -impl Sink - for &mut SinkTo +impl Sink + for &mut SinkTo where Samp: Sample + From, S: Sample, - K: Sink, + K: Sink, { /// Get the sample rate of the sink in hertz. fn sample_rate(&self) -> NonZeroU32 { @@ -84,12 +84,12 @@ where } #[allow(single_use_lifetimes)] -impl Sink - for SinkTo +impl Sink + for SinkTo where Samp: Sample + From, S: Sample, - K: Sink, + K: Sink, { /// Get the sample rate of the sink in hertz. fn sample_rate(&self) -> NonZeroU32 { diff --git a/src/stream.rs b/src/stream.rs index 4ed8fd0..2e03dba 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -23,7 +23,7 @@ const WINDOW_FN_OVERSAMPLE: usize = 32; /// Stream resampler. #[derive(Debug)] -pub struct Stream { +pub struct Stream { /// Target sample rate (constant). output_sample_rate: u32, /// Source sample rate (changeable) @@ -36,7 +36,7 @@ pub struct Stream { input_latency: u32, } -impl Stream { +impl Stream { /// Create a new stream at target sample rate. pub fn new(target_hz: u32) -> Self { assert_ne!(target_hz, 0); @@ -88,18 +88,18 @@ impl Stream { pub fn flush(mut self, sink: K) where S: Sample, - K: Sink, + K: Sink, { if self.samples[0].state.started == 0 { return; } // Generate silence. - for chan in 0..CH { + for chan in 0..N { self.samples[chan].input.clear(); } for _ in 0..self.input_latency { - for chan in 0..CH { + for chan in 0..N { self.samples[chan].input.push(0.0); } } @@ -113,11 +113,11 @@ impl Stream { /// If the sink gets full, then no more audio will be written. If there is /// not enough audio then the sink chooses whether or not to fill the rest /// of it's buffer with silence. - pub fn pipe(&mut self, audio: &Audio, mut sink: K) + pub fn pipe(&mut self, audio: &Audio, mut sink: K) where Samp: Sample, S: Sample + From, - K: Sink, + K: Sink, Samp32: From, { // Make sure that the sample rates match. @@ -138,7 +138,7 @@ impl Stream { } // First, de-interleave input audio data into f32 buffer. - let converted = Audio::::with_frames( + let converted = Audio::::with_frames( audio.sample_rate().get(), audio .as_slice() @@ -146,11 +146,11 @@ impl Stream { .map(|frame| frame.to()) .collect::>(), ); - for chan in 0..CH { + for chan in 0..N { self.samples[chan].input.clear(); } for frame in converted.iter() { - for chan in 0..CH { + for chan in 0..N { self.samples[chan] .input .push(frame.samples()[chan].to_f32()); @@ -164,7 +164,7 @@ impl Stream { fn resample_audio(&mut self, mut sink: K) where S: Sample, - K: Sink, + K: Sink, { // If no input samples, skip doing the work. if self.samples[0].input.is_empty() { @@ -174,7 +174,7 @@ impl Stream { let mut out = u32::MAX; // Allocate space for output samples and resample - for chan in 0..CH { + for chan in 0..N { self.samples[chan].output.resize(sink.len(), 0.0); // FIXME: Remove length parameters, return number of output samples. @@ -189,8 +189,8 @@ impl Stream { // Then, re-interleave the samples back. sink.sink_with(&mut (0..out as usize).map(|i| { - let mut out_frame = Frame::::default(); - for chan in 0..CH { + let mut out_frame = Frame::::default(); + for chan in 0..N { out_frame.samples_mut()[chan] = S::from(self.samples[chan].output[i]); }