diff --git a/Cargo.toml b/Cargo.toml index 2851580..e938185 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "fon" -version = "0.6.0" +version = "0.7.0" edition = "2021" rust-version = "1.70" license = "Apache-2.0 OR BSL-1.0 OR MIT" diff --git a/examples/mix.rs b/examples/mix.rs index c5e1137..6490ffb 100644 --- a/examples/mix.rs +++ b/examples/mix.rs @@ -25,9 +25,7 @@ impl<'a, Samp: Sample, const N: usize> Mixer<'a, Samp, N> { // Using '_ results in reserved lifetime error. #[allow(single_use_lifetimes)] -impl<'a, Samp: Sample, const N: usize> Sink - for Mixer<'a, Samp, N> -{ +impl<'a, Samp: Sample, const N: usize> Sink for Mixer<'a, Samp, N> { #[inline(always)] fn sample_rate(&self) -> NonZeroU32 { self.audio.sample_rate() @@ -45,9 +43,7 @@ impl<'a, Samp: Sample, const N: usize> Sink } } -impl Sink - for &mut Mixer<'_, Samp, N> -{ +impl Sink for &mut Mixer<'_, Samp, N> { #[inline(always)] fn sample_rate(&self) -> NonZeroU32 { self.audio.sample_rate() @@ -62,10 +58,10 @@ impl Sink 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 + for (sample, samp) in frame.samples_mut().iter_mut().zip(other.samples()) { - *channel += *chan; + *sample += *samp; } } else { break; @@ -90,8 +86,8 @@ fn save_file(name: &str, audio: &Audio) -> std::io::Result<()> { // Convert audio to byte buffer let mut samples = Vec::::new(); for frame in audio.iter() { - for channel in frame.samples() { - samples.extend(channel.to_f32().to_le_bytes()); + for sample in frame.samples() { + samples.extend(sample.to_f32().to_le_bytes()); } } // Save byte buffer diff --git a/examples/sawtooth.rs b/examples/sawtooth.rs index eb3f4fa..79c0b8a 100644 --- a/examples/sawtooth.rs +++ b/examples/sawtooth.rs @@ -1,6 +1,6 @@ use fon::{ - samp::{Samp16, Samp32}, pos::Mono, + samp::{Samp16, Samp32}, Audio, }; diff --git a/src/audio.rs b/src/audio.rs index ecaf85a..749d8fb 100644 --- a/src/audio.rs +++ b/src/audio.rs @@ -12,8 +12,8 @@ use core::{ #[cfg(not(test))] use crate::math::Libm; use crate::{ - samp::{Samp16, Samp24, Samp32, Samp64, Sample}, frame::Frame, + samp::{Samp16, Samp24, Samp32, Samp64, Sample}, Sink, Stream, }; @@ -61,7 +61,8 @@ 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, _, COUNT, 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 @@ -162,7 +163,10 @@ impl<'a, Samp: Sample, const COUNT: 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) } @@ -182,7 +186,10 @@ 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 @@ -326,7 +333,8 @@ impl Audio { } } -impl From> for Vec> +impl From> + for Vec> where Samp: Sample, { diff --git a/src/frame.rs b/src/frame.rs index f7c1861..7a14db8 100644 --- a/src/frame.rs +++ b/src/frame.rs @@ -6,9 +6,9 @@ use core::{ ops::{Add, Mul, Neg, Sub}, }; -use crate::samp::Sample; #[cfg(not(test))] use crate::math::Libm; +use crate::samp::Sample; /// Frame - A number of interleaved [`Sample`]s #[repr(transparent)] @@ -87,30 +87,30 @@ impl Frame { } #[inline(always)] - fn pan_1(mut self, chan: Samp, _x: f32) -> Self { + fn pan_1(mut self, samp: Samp, _x: f32) -> Self { const MONO: usize = 0; - self.0[MONO] += chan; + self.0[MONO] += samp; self } #[inline(always)] - fn pan_2(mut self, chan: Samp, x: f32) -> Self { + fn pan_2(mut self, samp: Samp, x: f32) -> Self { const LEFT: usize = 0; const RIGHT: usize = 1; // Convert to radians, left is now at 0. let x = (x + 0.25) * core::f32::consts::PI; // Pan distance - self.0[LEFT] += chan * x.cos().into(); - self.0[RIGHT] += chan * x.sin().into(); + self.0[LEFT] += samp * x.cos().into(); + self.0[RIGHT] += samp * x.sin().into(); self } #[inline(always)] - fn pan_3(mut self, chan: Samp, x: f32) -> Self { + fn pan_3(mut self, samp: Samp, x: f32) -> Self { const LEFT: usize = 0; const RIGHT: usize = 1; const CENTER: usize = 2; @@ -120,26 +120,26 @@ impl Frame { // Center-Right Speakers x if x < 0.25 => { let x = 4.0 * x * FRAC_PI_2; - self.0[CENTER] += chan * x.cos().into(); - self.0[RIGHT] += chan * x.sin().into(); + self.0[CENTER] += samp * x.cos().into(); + self.0[RIGHT] += samp * x.sin().into(); } // Right-Center Speakers x if x < 0.5 => { let x = 4.0 * (x - 0.25) * FRAC_PI_2; - self.0[RIGHT] += chan * x.cos().into(); - self.0[CENTER] += chan * x.sin().into(); + self.0[RIGHT] += samp * x.cos().into(); + self.0[CENTER] += samp * x.sin().into(); } // Center-Left Speakers x if x < 0.75 => { let x = 4.0 * (x - 0.50) * FRAC_PI_2; - self.0[CENTER] += chan * x.cos().into(); - self.0[LEFT] += chan * x.sin().into(); + self.0[CENTER] += samp * x.cos().into(); + self.0[LEFT] += samp * x.sin().into(); } // Left-Center Speakers x => { let x = 4.0 * (x - 0.75) * FRAC_PI_2; - self.0[LEFT] += chan * x.cos().into(); - self.0[CENTER] += chan * x.sin().into(); + self.0[LEFT] += samp * x.cos().into(); + self.0[CENTER] += samp * x.sin().into(); } } @@ -147,7 +147,7 @@ impl Frame { } #[inline(always)] - fn pan_4(mut self, chan: Samp, x: f32) -> Self { + fn pan_4(mut self, samp: Samp, x: f32) -> Self { const FRONT_L: usize = 0; const FRONT_R: usize = 1; const SURROUND_L: usize = 2; @@ -158,26 +158,26 @@ impl Frame { // Front Left - Front Right Speakers (60° slice) x if x < 60.0 / 360.0 => { let x = (360.0 / 60.0) * x * FRAC_PI_2; - self.0[FRONT_L] += chan * x.cos().into(); - self.0[FRONT_R] += chan * x.sin().into(); + self.0[FRONT_L] += samp * x.cos().into(); + self.0[FRONT_R] += samp * x.sin().into(); } // Front Right - Back Right Speakers (80° slice) x if x < 140.0 / 360.0 => { let x = (360.0 / 80.0) * (x - 60.0 / 360.0) * FRAC_PI_2; - self.0[FRONT_R] += chan * x.cos().into(); - self.0[SURROUND_R] += chan * x.sin().into(); + self.0[FRONT_R] += samp * x.cos().into(); + self.0[SURROUND_R] += samp * x.sin().into(); } // Back Right - Back Left Speakers (140° slice) x if x < 280.0 / 360.0 => { let x = (360.0 / 140.0) * (x - 140.0 / 360.0) * FRAC_PI_2; - self.0[SURROUND_R] += chan * x.cos().into(); - self.0[SURROUND_L] += chan * x.sin().into(); + self.0[SURROUND_R] += samp * x.cos().into(); + self.0[SURROUND_L] += samp * x.sin().into(); } // Back Left - Front Left Speakers (80° slice) x => { let x = (360.0 / 80.0) * (x - 280.0 / 360.0) * FRAC_PI_2; - self.0[SURROUND_L] += chan * x.cos().into(); - self.0[FRONT_L] += chan * x.sin().into(); + self.0[SURROUND_L] += samp * x.cos().into(); + self.0[FRONT_L] += samp * x.sin().into(); } } @@ -185,7 +185,7 @@ impl Frame { } #[inline(always)] - fn pan_5(mut self, chan: Samp, x: f32) -> Self { + fn pan_5(mut self, samp: Samp, x: f32) -> Self { const FRONT_L: usize = 0; const FRONT_R: usize = 1; const FRONT: usize = 2; @@ -196,32 +196,32 @@ impl Frame { // Front Center - Front Right Speakers (30° slice) x if x < 30.0 / 360.0 => { let x = (360.0 / 30.0) * x * FRAC_PI_2; - self.0[FRONT] += chan * x.cos().into(); - self.0[FRONT_R] += chan * x.sin().into(); + self.0[FRONT] += samp * x.cos().into(); + self.0[FRONT_R] += samp * x.sin().into(); } // Front Right - Back Right Speakers (80° slice) x if x < 110.0 / 360.0 => { let x = (360.0 / 80.0) * (x - 30.0 / 360.0) * FRAC_PI_2; - self.0[FRONT_R] += chan * x.cos().into(); - self.0[SURROUND_R] += chan * x.sin().into(); + self.0[FRONT_R] += samp * x.cos().into(); + self.0[SURROUND_R] += samp * x.sin().into(); } // Back Right - Back Left Speakers (140° slice) x if x < 250.0 / 360.0 => { let x = (360.0 / 140.0) * (x - 110.0 / 360.0) * FRAC_PI_2; - self.0[SURROUND_R] += chan * x.cos().into(); - self.0[SURROUND_L] += chan * x.sin().into(); + self.0[SURROUND_R] += samp * x.cos().into(); + self.0[SURROUND_L] += samp * x.sin().into(); } // Back Left - Front Left Speakers (80° slice) x if x < 330.0 / 360.0 => { let x = (360.0 / 80.0) * (x - 250.0 / 360.0) * FRAC_PI_2; - self.0[SURROUND_L] += chan * x.cos().into(); - self.0[FRONT_L] += chan * x.sin().into(); + self.0[SURROUND_L] += samp * x.cos().into(); + self.0[FRONT_L] += samp * x.sin().into(); } // Front Left - Center Speakers (30° slice) x => { let x = (360.0 / 30.0) * (x - 330.0 / 360.0) * FRAC_PI_2; - self.0[FRONT_L] += chan * x.cos().into(); - self.0[FRONT] += chan * x.sin().into(); + self.0[FRONT_L] += samp * x.cos().into(); + self.0[FRONT] += samp * x.sin().into(); } } @@ -229,7 +229,7 @@ impl Frame { } #[inline(always)] - fn pan_6(mut self, chan: Samp, x: f32) -> Self { + fn pan_6(mut self, samp: Samp, x: f32) -> Self { const FRONT_L: usize = 0; const FRONT_R: usize = 1; const FRONT: usize = 2; @@ -241,32 +241,32 @@ impl Frame { // Front Center - Front Right Speakers (30° slice) x if x < 30.0 / 360.0 => { let x = (360.0 / 30.0) * x * FRAC_PI_2; - self.0[FRONT] += chan * x.cos().into(); - self.0[FRONT_R] += chan * x.sin().into(); + self.0[FRONT] += samp * x.cos().into(); + self.0[FRONT_R] += samp * x.sin().into(); } // Front Right - Back Right Speakers (80° slice) x if x < 110.0 / 360.0 => { let x = (360.0 / 80.0) * (x - 30.0 / 360.0) * FRAC_PI_2; - self.0[FRONT_R] += chan * x.cos().into(); - self.0[SURROUND_R] += chan * x.sin().into(); + self.0[FRONT_R] += samp * x.cos().into(); + self.0[SURROUND_R] += samp * x.sin().into(); } // Back Right - Back Left Speakers (140° slice) x if x < 250.0 / 360.0 => { let x = (360.0 / 140.0) * (x - 110.0 / 360.0) * FRAC_PI_2; - self.0[SURROUND_R] += chan * x.cos().into(); - self.0[SURROUND_L] += chan * x.sin().into(); + self.0[SURROUND_R] += samp * x.cos().into(); + self.0[SURROUND_L] += samp * x.sin().into(); } // Back Left - Front Left Speakers (80° slice) x if x < 330.0 / 360.0 => { let x = (360.0 / 80.0) * (x - 250.0 / 360.0) * FRAC_PI_2; - self.0[SURROUND_L] += chan * x.cos().into(); - self.0[FRONT_L] += chan * x.sin().into(); + self.0[SURROUND_L] += samp * x.cos().into(); + self.0[FRONT_L] += samp * x.sin().into(); } // Front Left - Center Speakers (30° slice) x => { let x = (360.0 / 30.0) * (x - 330.0 / 360.0) * FRAC_PI_2; - self.0[FRONT_L] += chan * x.cos().into(); - self.0[FRONT] += chan * x.sin().into(); + self.0[FRONT_L] += samp * x.cos().into(); + self.0[FRONT] += samp * x.sin().into(); } } @@ -274,7 +274,7 @@ impl Frame { } #[inline(always)] - fn pan_7(mut self, chan: Samp, x: f32) -> Self { + fn pan_7(mut self, samp: Samp, x: f32) -> Self { const FRONT_L: usize = 0; const FRONT_R: usize = 1; const FRONT: usize = 2; @@ -287,38 +287,38 @@ impl Frame { // Front Center - Front Right Speakers (30° slice) x if x < 30.0 / 360.0 => { let x = (360.0 / 30.0) * x * FRAC_PI_2; - self.0[FRONT] += chan * x.cos().into(); - self.0[FRONT_R] += chan * x.sin().into(); + self.0[FRONT] += samp * x.cos().into(); + self.0[FRONT_R] += samp * x.sin().into(); } // Front Right - Side Right Speakers (60° slice) x if x < 90.0 / 360.0 => { let x = (360.0 / 60.0) * (x - 30.0 / 360.0) * FRAC_PI_2; - self.0[FRONT_R] += chan * x.cos().into(); - self.0[RIGHT] += chan * x.sin().into(); + self.0[FRONT_R] += samp * x.cos().into(); + self.0[RIGHT] += samp * x.sin().into(); } // Side Right - Back Speakers (90° slice) x if x < 180.0 / 360.0 => { let x = (360.0 / 90.0) * (x - 90.0 / 360.0) * FRAC_PI_2; - self.0[RIGHT] += chan * x.cos().into(); - self.0[BACK] += chan * x.sin().into(); + self.0[RIGHT] += samp * x.cos().into(); + self.0[BACK] += samp * x.sin().into(); } // Back - Side Left Speakers (90° slice) x if x < 270.0 / 360.0 => { let x = (360.0 / 90.0) * (x - 180.0 / 360.0) * FRAC_PI_2; - self.0[BACK] += chan * x.cos().into(); - self.0[LEFT] += chan * x.sin().into(); + self.0[BACK] += samp * x.cos().into(); + self.0[LEFT] += samp * x.sin().into(); } // Side Left - Front Left Speakers (60° slice) x if x < 330.0 / 360.0 => { let x = (360.0 / 60.0) * (x - 270.0 / 360.0) * FRAC_PI_2; - self.0[LEFT] += chan * x.cos().into(); - self.0[FRONT_L] += chan * x.sin().into(); + self.0[LEFT] += samp * x.cos().into(); + self.0[FRONT_L] += samp * x.sin().into(); } // Front Left - Center Speakers (30° slice) x => { let x = (360.0 / 30.0) * (x - 330.0 / 360.0) * FRAC_PI_2; - self.0[FRONT_L] += chan * x.cos().into(); - self.0[FRONT] += chan * x.sin().into(); + self.0[FRONT_L] += samp * x.cos().into(); + self.0[FRONT] += samp * x.sin().into(); } } @@ -326,7 +326,7 @@ impl Frame { } #[inline(always)] - fn pan_8(mut self, chan: Samp, x: f32) -> Self { + fn pan_8(mut self, samp: Samp, x: f32) -> Self { const FRONT_L: usize = 0; const FRONT_R: usize = 1; const FRONT: usize = 2; @@ -340,44 +340,44 @@ impl Frame { // Front Center - Front Right Speakers (30° slice) x if x < 30.0 / 360.0 => { let x = (360.0 / 30.0) * x * FRAC_PI_2; - self.0[FRONT] += chan * x.cos().into(); - self.0[FRONT_R] += chan * x.sin().into(); + self.0[FRONT] += samp * x.cos().into(); + self.0[FRONT_R] += samp * x.sin().into(); } // Front Right - Side Right Speakers (60° slice) x if x < 90.0 / 360.0 => { let x = (360.0 / 60.0) * (x - 30.0 / 360.0) * FRAC_PI_2; - self.0[FRONT_R] += chan * x.cos().into(); - self.0[RIGHT] += chan * x.sin().into(); + self.0[FRONT_R] += samp * x.cos().into(); + self.0[RIGHT] += samp * x.sin().into(); } // Side Right - Back Right Speakers (60° slice) x if x < 150.0 / 360.0 => { let x = (360.0 / 60.0) * (x - 90.0 / 360.0) * FRAC_PI_2; - self.0[RIGHT] += chan * x.cos().into(); - self.0[BACK_R] += chan * x.sin().into(); + self.0[RIGHT] += samp * x.cos().into(); + self.0[BACK_R] += samp * x.sin().into(); } // Back Right - Back Left Speakers (60° slice) x if x < 210.0 / 360.0 => { let x = (360.0 / 60.0) * (x - 150.0 / 360.0) * FRAC_PI_2; - self.0[BACK_R] += chan * x.cos().into(); - self.0[BACK_L] += chan * x.sin().into(); + self.0[BACK_R] += samp * x.cos().into(); + self.0[BACK_L] += samp * x.sin().into(); } // Back Left - Side Left Speakers (60° slice) x if x < 270.0 / 360.0 => { let x = (360.0 / 60.0) * (x - 210.0 / 360.0) * FRAC_PI_2; - self.0[BACK_L] += chan * x.cos().into(); - self.0[LEFT] += chan * x.sin().into(); + self.0[BACK_L] += samp * x.cos().into(); + self.0[LEFT] += samp * x.sin().into(); } // Side Left - Front Left Speakers (60° slice) x if x < 330.0 / 360.0 => { let x = (360.0 / 60.0) * (x - 270.0 / 360.0) * FRAC_PI_2; - self.0[LEFT] += chan * x.cos().into(); - self.0[FRONT_L] += chan * x.sin().into(); + self.0[LEFT] += samp * x.cos().into(); + self.0[FRONT_L] += samp * x.sin().into(); } // Front Left - Center Speakers (30° slice) x => { let x = (360.0 / 30.0) * (x - 330.0 / 360.0) * FRAC_PI_2; - self.0[FRONT_L] += chan * x.cos().into(); - self.0[FRONT] += chan * x.sin().into(); + self.0[FRONT_L] += samp * x.cos().into(); + self.0[FRONT] += samp * x.sin().into(); } } @@ -612,7 +612,7 @@ impl Frame { } impl Frame { - /// Create a new mono interleaved audio frame from channel(s). + /// Create a new mono interleaved audio frame from sample(s). #[inline(always)] pub fn new(mono: Samp) -> Self { Self([mono]) @@ -620,7 +620,7 @@ impl Frame { } impl Frame { - /// Create a new stereo interleaved audio frame from channel(s). + /// Create a new stereo interleaved audio frame from sample(s). #[inline(always)] pub fn new(left: Samp, right: Samp) -> Self { Self([left, right]) @@ -628,7 +628,7 @@ impl Frame { } impl Frame { - /// Create a new surround 3.0 interleaved audio frame from channel(s). + /// Create a new surround 3.0 interleaved audio frame from sample(s). #[inline(always)] pub fn new(left: Samp, right: Samp, center: Samp) -> Self { Self([left, right, center]) @@ -636,7 +636,7 @@ impl Frame { } impl Frame { - /// Create a new surround 4.0 interleaved audio frame from channel(s). + /// Create a new surround 4.0 interleaved audio frame from sample(s). #[inline(always)] pub fn new( left: Samp, @@ -649,7 +649,7 @@ impl Frame { } impl Frame { - /// Create a new surround 5.0 interleaved audio frame from channel(s). + /// Create a new surround 5.0 interleaved audio frame from sample(s). #[inline(always)] pub fn new( left: Samp, @@ -663,7 +663,7 @@ impl Frame { } impl Frame { - /// Create a new surround 5.1 interleaved audio frame from channel(s). + /// Create a new surround 5.1 interleaved audio frame from sample(s). #[inline(always)] pub fn new( left: Samp, @@ -678,7 +678,7 @@ impl Frame { } impl Frame { - /// Create a new surround 6.1 interleaved audio frame from channel(s). + /// Create a new surround 6.1 interleaved audio frame from sample(s). #[inline(always)] pub fn new( left: Samp, @@ -694,7 +694,7 @@ impl Frame { } impl Frame { - /// Create a new surround 7.1 interleaved audio frame from channel(s). + /// Create a new surround 7.1 interleaved audio frame from sample(s). #[inline(always)] #[allow(clippy::too_many_arguments)] pub fn new( @@ -761,8 +761,8 @@ impl Neg for Frame { #[inline(always)] fn neg(mut self) -> Self { - for chan in self.0.iter_mut() { - *chan = -*chan; + for samp in self.0.iter_mut() { + *samp = -*samp; } self } diff --git a/src/lib.rs b/src/lib.rs index dac3e91..529fdc4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -103,8 +103,8 @@ mod private; mod sink; mod stream; -pub mod samp; pub mod pos; +pub mod samp; pub use audio::{Audio, AudioSink}; pub use frame::Frame; diff --git a/src/pos.rs b/src/pos.rs index 963ece5..409d617 100644 --- a/src/pos.rs +++ b/src/pos.rs @@ -2,7 +2,7 @@ use core::ops::{Index, IndexMut}; -use crate::{samp::Sample, frame::Frame}; +use crate::{frame::Frame, samp::Sample}; /// All directions /// - Mono diff --git a/src/samp.rs b/src/samp.rs index b331aba..edb2620 100644 --- a/src/samp.rs +++ b/src/samp.rs @@ -1,6 +1,7 @@ -//! Audio channels (left, right, etc.). Each channel contains a single sample. +//! Audio samples //! -//! An audio [`Frame`](crate::frame::Frame) is used to group multiple channels. +//! An audio [`Frame`](crate::frame::Frame) is used to group multiple channels +//! with a positions configuration. use core::{ fmt::Debug, @@ -635,10 +636,22 @@ mod tests { // Test multiplication assert_eq!(Samp16::new(0), Samp16::new(0) * Samp16::new(32767)); assert_eq!(Samp16::new(32767), Samp16::new(32767) * Samp16::new(32767)); - assert_eq!(Samp16::new(-32768), Samp16::new(32767) * Samp16::new(-32768)); - assert_eq!(Samp16::new(-32768), Samp16::new(-32768) * Samp16::new(32767)); - assert_eq!(Samp16::new(32767), Samp16::new(-32768) * Samp16::new(-32768)); - assert_eq!(Samp16::new(-16384), Samp16::new(32767) * Samp16::new(-16384)); + assert_eq!( + Samp16::new(-32768), + Samp16::new(32767) * Samp16::new(-32768) + ); + assert_eq!( + Samp16::new(-32768), + Samp16::new(-32768) * Samp16::new(32767) + ); + assert_eq!( + Samp16::new(32767), + Samp16::new(-32768) * Samp16::new(-32768) + ); + assert_eq!( + Samp16::new(-16384), + Samp16::new(32767) * Samp16::new(-16384) + ); // Test negation assert_eq!(Samp16::MIN, -Samp16::MAX); assert_eq!(Samp16::MAX, -Samp16::MIN); @@ -649,7 +662,10 @@ mod tests { #[test] fn ch24_arith() { // Test addition - assert_eq!(Samp24::new(-1), Samp24::new(-8388608) + Samp24::new(8388607)); + assert_eq!( + Samp24::new(-1), + Samp24::new(-8388608) + Samp24::new(8388607) + ); assert_eq!( Samp24::new(2097152), Samp24::new(-2097152) + Samp24::new(4194304) @@ -657,12 +673,21 @@ mod tests { assert_eq!(Samp24::MAX, Samp24::MID + Samp24::MAX); assert_eq!(Samp24::MIN, Samp24::new(-4194304) + Samp24::new(-4194304)); // Test subtraction - assert_eq!(Samp24::new(0), Samp24::new(-8388608) - Samp24::new(-8388608)); + assert_eq!( + Samp24::new(0), + Samp24::new(-8388608) - Samp24::new(-8388608) + ); assert_eq!(Samp24::new(0), Samp24::new(8388607) - Samp24::new(8388607)); - assert_eq!(Samp24::new(-8388607), Samp24::new(0) - Samp24::new(8388607)); + assert_eq!( + Samp24::new(-8388607), + Samp24::new(0) - Samp24::new(8388607) + ); // Test multiplication assert_eq!(Samp24::new(0), Samp24::new(0) * Samp24::new(8388607)); - assert_eq!(Samp24::new(8388607), Samp24::new(8388607) * Samp24::new(8388607)); + assert_eq!( + Samp24::new(8388607), + Samp24::new(8388607) * Samp24::new(8388607) + ); assert_eq!( Samp24::new(-8388608), Samp24::new(8388607) * Samp24::new(-8388608) diff --git a/src/sink.rs b/src/sink.rs index b576c9d..6103cd3 100644 --- a/src/sink.rs +++ b/src/sink.rs @@ -38,7 +38,8 @@ where _phantom: core::marker::PhantomData (Samp, S)>, } -impl SinkTo +impl + SinkTo where Samp: Sample + From, S: Sample, diff --git a/src/stream.rs b/src/stream.rs index 2e03dba..909f85a 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -2,8 +2,8 @@ use alloc::vec::Vec; use core::{mem, num::NonZeroU32}; use crate::{ - samp::{Samp32, Sample}, frame::Frame, + samp::{Samp32, Sample}, Audio, Sink, }; @@ -95,12 +95,12 @@ impl Stream { } // Generate silence. - for chan in 0..N { - self.samples[chan].input.clear(); + for samp in 0..N { + self.samples[samp].input.clear(); } for _ in 0..self.input_latency { - for chan in 0..N { - self.samples[chan].input.push(0.0); + for samp in 0..N { + self.samples[samp].input.push(0.0); } } @@ -146,14 +146,14 @@ impl Stream { .map(|frame| frame.to()) .collect::>(), ); - for chan in 0..N { - self.samples[chan].input.clear(); + for samp in 0..N { + self.samples[samp].input.clear(); } for frame in converted.iter() { - for chan in 0..N { - self.samples[chan] + for samp in 0..N { + self.samples[samp] .input - .push(frame.samples()[chan].to_f32()); + .push(frame.samples()[samp].to_f32()); } } @@ -174,14 +174,14 @@ impl Stream { let mut out = u32::MAX; // Allocate space for output samples and resample - for chan in 0..N { - self.samples[chan].output.resize(sink.len(), 0.0); + for samp in 0..N { + self.samples[samp].output.resize(sink.len(), 0.0); // FIXME: Remove length parameters, return number of output samples. - self.samples[chan].state.process_float( - self.samples[chan].input.as_slice(), - &mut (self.samples[chan].input.len() as u32), - self.samples[chan].output.as_mut_slice(), + self.samples[samp].state.process_float( + self.samples[samp].input.as_slice(), + &mut (self.samples[samp].input.len() as u32), + self.samples[samp].output.as_mut_slice(), &mut out, self.ratio.1, ); @@ -190,9 +190,9 @@ 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..N { - out_frame.samples_mut()[chan] = - S::from(self.samples[chan].output[i]); + for samp in 0..N { + out_frame.samples_mut()[samp] = + S::from(self.samples[samp].output[i]); } out_frame })); diff --git a/src/stream/speex.rs b/src/stream/speex.rs index bf6f75e..63e4b18 100644 --- a/src/stream/speex.rs +++ b/src/stream/speex.rs @@ -615,13 +615,7 @@ fn speex_resampler_magic( ) -> u32 { let mut tmp_in_len = st.magic_samples; let mem_idx = st.filt_len as usize; - speex_resampler_process_native( - st, - &mut tmp_in_len, - out, - &mut out_len, - den, - ); + speex_resampler_process_native(st, &mut tmp_in_len, out, &mut out_len, den); st.magic_samples -= tmp_in_len; if st.magic_samples != 0 { let mem = &st.mem[mem_idx - 1 + tmp_in_len as usize..].to_vec();