diff --git a/.rustfmt.toml b/.rustfmt.toml index df99c69..5e6866e 100644 --- a/.rustfmt.toml +++ b/.rustfmt.toml @@ -1 +1,9 @@ +# Don't deviate too much, just reduce columns and stricter enforcement +edition = "2021" +unstable_features = true max_width = 80 +reorder_impl_items = true +group_imports = "StdExternalCrate" +imports_granularity = "Crate" +normalize_doc_attributes = true +wrap_comments = true diff --git a/examples/mix.rs b/examples/mix.rs index 234f9a8..a89dd57 100644 --- a/examples/mix.rs +++ b/examples/mix.rs @@ -1,10 +1,11 @@ // Audio mixing example +use std::num::NonZeroU32; + use fon::{ chan::{Ch32, Channel}, Audio, Frame, Sink, Stream, }; -use std::num::NonZeroU32; #[derive(Debug)] pub struct Mixer<'a, Chan: Channel, const CH: usize> { diff --git a/examples/resample.rs b/examples/resample.rs index df66fbc..a09877b 100644 --- a/examples/resample.rs +++ b/examples/resample.rs @@ -1,7 +1,6 @@ use std::convert::TryInto; -use fon::chan::Ch32; -use fon::Audio; +use fon::{chan::Ch32, Audio}; // Resample an audio file from one sample rate to another. fn resample(in_hz: u32, in_file: &str, out_hz: u32, out_file: &str) { diff --git a/examples/sawtooth.rs b/examples/sawtooth.rs index cf8a216..5899265 100644 --- a/examples/sawtooth.rs +++ b/examples/sawtooth.rs @@ -1,6 +1,8 @@ -use fon::chan::{Ch16, Ch32}; -use fon::pos::Mono; -use fon::Audio; +use fon::{ + chan::{Ch16, Ch32}, + pos::Mono, + Audio, +}; fn main() { // Create mono 32-bit floating point audio buffer. diff --git a/src/audio.rs b/src/audio.rs index 338bd3b..845ecd5 100644 --- a/src/audio.rs +++ b/src/audio.rs @@ -7,20 +7,24 @@ // At your choosing (See accompanying files LICENSE_APACHE_2_0.txt, // LICENSE_MIT.txt and LICENSE_BOOST_1_0.txt). +use alloc::{ + boxed::Box, + slice::{Iter, IterMut}, + vec, + vec::Vec, +}; +use core::{ + convert::TryInto, fmt::Debug, mem::size_of, num::NonZeroU32, + slice::from_raw_parts_mut, +}; + #[cfg(not(test))] use crate::math::Libm; - -use crate::chan::{Ch16, Ch24, Ch32, Ch64, Channel}; -use crate::frame::Frame; -use crate::{Sink, Stream}; - -use alloc::boxed::Box; -use alloc::slice::{Iter, IterMut}; -use alloc::{vec, vec::Vec}; - -use core::convert::TryInto; -use core::num::NonZeroU32; -use core::{fmt::Debug, mem::size_of, slice::from_raw_parts_mut}; +use crate::{ + chan::{Ch16, Ch24, Ch32, Ch64, Channel}, + frame::Frame, + Sink, Stream, +}; /// Audio buffer (fixed-size array of audio [`Frame`](crate::frame::Frame)s at /// sample rate specified in hertz). diff --git a/src/chan.rs b/src/chan.rs index 40198f5..296cad4 100644 --- a/src/chan.rs +++ b/src/chan.rs @@ -11,12 +11,14 @@ //! //! An audio [`Frame`](crate::frame::Frame) is used to group multiple channels. +use core::{ + fmt::Debug, + ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign}, +}; + #[cfg(not(test))] use crate::math::Libm; - use crate::private::Sealed; -use core::fmt::Debug; -use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign}; /// Component of a speaker configuration, such as *front left*, *lfe*, *etc*. pub trait Channel: @@ -71,9 +73,9 @@ pub trait Channel: pub struct Ch16(i16); impl Channel for Ch16 { - const MIN: Ch16 = Ch16(-32_768); - const MID: Ch16 = Ch16(0); const MAX: Ch16 = Ch16(32_767); + const MID: Ch16 = Ch16(0); + const MIN: Ch16 = Ch16(-32_768); #[inline(always)] fn to_f32(self) -> f32 { @@ -170,9 +172,9 @@ impl Neg for Ch16 { pub struct Ch24(i16, u8); impl Channel for Ch24 { - const MIN: Ch24 = Ch24::new(-8_388_608); - const MID: Ch24 = Ch24::new(0); const MAX: Ch24 = Ch24::new(8_388_607); + const MID: Ch24 = Ch24::new(0); + const MIN: Ch24 = Ch24::new(-8_388_608); #[inline(always)] fn to_f32(self) -> f32 { @@ -276,9 +278,9 @@ impl Neg for Ch24 { pub struct Ch32(f32); impl Channel for Ch32 { - const MIN: Ch32 = Ch32(-1.0); - const MID: Ch32 = Ch32(0.0); const MAX: Ch32 = Ch32(1.0); + const MID: Ch32 = Ch32(0.0); + const MIN: Ch32 = Ch32(-1.0); #[inline(always)] fn to_f32(self) -> f32 { @@ -371,9 +373,9 @@ impl Neg for Ch32 { pub struct Ch64(f64); impl Channel for Ch64 { - const MIN: Ch64 = Ch64(-1.0); - const MID: Ch64 = Ch64(0.0); const MAX: Ch64 = Ch64(1.0); + const MID: Ch64 = Ch64(0.0); + const MIN: Ch64 = Ch64(-1.0); #[inline(always)] fn to_f32(self) -> f32 { diff --git a/src/frame.rs b/src/frame.rs index d9e1989..c4c8020 100644 --- a/src/frame.rs +++ b/src/frame.rs @@ -9,13 +9,15 @@ //! Frame (interleaved sample) types -#[cfg(not(test))] -use crate::math::Libm; +use core::{ + f32::consts::FRAC_PI_2, + fmt::Debug, + ops::{Add, Mul, Neg, Sub}, +}; use crate::chan::Channel; -use core::f32::consts::FRAC_PI_2; -use core::fmt::Debug; -use core::ops::{Add, Mul, Neg, Sub}; +#[cfg(not(test))] +use crate::math::Libm; /// Frame - A number of interleaved sample [channel]s. /// diff --git a/src/math.rs b/src/math.rs index 11ab8f7..3a8abf5 100644 --- a/src/math.rs +++ b/src/math.rs @@ -173,10 +173,9 @@ impl Libm for f64 { #[cfg(test)] mod tests { - use super::*; + use core::{f32::consts::PI as PI_F32, f64::consts::PI as PI_F64}; - use core::f32::consts::PI as PI_F32; - use core::f64::consts::PI as PI_F64; + use super::*; fn assert_approx_eq_f32(a: f32, b: f32) { if a != b { @@ -207,14 +206,16 @@ mod tests { #[test] fn powi() { for x in [0.0, 1.0, 1.5, -0.4, -1000.09301, 564.33333, PI_F64] { - //std implementation has slightly different results across platforms + // std implementation has slightly different results across + // platforms for i in -16..16 { assert_approx_eq_f64(Libm::powi(x, i), f64::powi(x, i)); } } for x in [0.0, 1.0, 1.5, -0.4, -1000.09301, 564.33333, PI_F32] { - //std implementation has slightly different results across platforms + // std implementation has slightly different results across + // platforms for i in -16..16 { assert_approx_eq_f32(Libm::powi(x, i), f32::powi(x, i)); } diff --git a/src/pos.rs b/src/pos.rs index 5f2ba5a..cfdfe0b 100644 --- a/src/pos.rs +++ b/src/pos.rs @@ -9,10 +9,10 @@ //! Speaker/channel positions within a speaker configuration. -use crate::chan::Channel; -use crate::frame::Frame; use core::ops::{Index, IndexMut}; +use crate::{chan::Channel, frame::Frame}; + /// All directions /// - Mono #[derive(Copy, Clone, Debug)] diff --git a/src/sink.rs b/src/sink.rs index 2e66879..0436bd4 100644 --- a/src/sink.rs +++ b/src/sink.rs @@ -7,11 +7,9 @@ // At your choosing (See accompanying files LICENSE_APACHE_2_0.txt, // LICENSE_MIT.txt and LICENSE_BOOST_1_0.txt). -use core::fmt::Debug; -use core::num::NonZeroU32; +use core::{fmt::Debug, num::NonZeroU32}; -use crate::chan::Channel; -use crate::Frame; +use crate::{chan::Channel, Frame}; /// Audio sink - a type that consumes audio samples. pub trait Sink: Debug { diff --git a/src/stream.rs b/src/stream.rs index 6da6b3c..4c3181a 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -8,12 +8,13 @@ // LICENSE_MIT.txt and LICENSE_BOOST_1_0.txt). use alloc::vec::Vec; -use core::mem; -use core::num::NonZeroU32; +use core::{mem, num::NonZeroU32}; -use crate::chan::{Ch32, Channel}; -use crate::frame::Frame; -use crate::{Audio, Sink}; +use crate::{ + chan::{Ch32, Channel}, + frame::Frame, + Audio, Sink, +}; mod speex; diff --git a/src/stream/speex.rs b/src/stream/speex.rs index 16163b3..e9e4bf2 100644 --- a/src/stream/speex.rs +++ b/src/stream/speex.rs @@ -1,14 +1,12 @@ // FIXME: Once remove macros, can delete #![allow(trivial_casts, trivial_numeric_casts)] +use alloc::{vec, vec::Vec}; +use core::{f64::consts::PI, mem}; + #[cfg(not(test))] use crate::math::Libm; -use alloc::vec; -use alloc::vec::Vec; -use core::f64::consts::PI; -use core::mem; - #[derive(Clone)] pub(crate) struct ResamplerState { pub(crate) filt_len: u32, @@ -138,14 +136,15 @@ macro_rules! algo { } impl ResamplerState { - /* * Resample a float array. The input and output buffers must *not* overlap. - * @param st Resampler state - * @param in Input buffer - * @param in_len number of input samples in the input buffer. Returns the - * number of samples processed - * @param out Output buffer - * @param out_len Size of the output buffer. Returns the number of samples written - */ + /// Resample a float array. The input and output buffers must *not* overlap. + /// + /// - @param st Resampler state + /// - @param in Input buffer + /// - @param in_len number of input samples in the input buffer. Returns + /// the number of samples processed + /// - @param out Output buffer + /// - @param out_len Size of the output buffer. Returns the number of + /// samples written pub(crate) fn process_float( &mut self, mut in_0: &[f32], @@ -198,22 +197,22 @@ impl ResamplerState { } } - /* * Make sure that the first samples to go out of the resamplers don't have - * leading zeros. This is only useful before starting to use a newly created - * resampler. It is recommended to use that when resampling an audio file, as - * it will generate a file with the same length. For real-time processing, - * it is probably easier not to use this call (so that the output duration - * is the same for the first frame). - * @param st Resampler state - */ + /// Make sure that the first samples to go out of the resamplers don't have + /// leading zeros. This is only useful before starting to use a newly + /// created resampler. It is recommended to use that when resampling an + /// audio file, as it will generate a file with the same length. For + /// real-time processing, it is probably easier not to use this call (so + /// that the output duration is the same for the first frame). + /// + /// - @param st Resampler state pub(crate) fn skip_zeros(&mut self) { let filt_len = self.filt_len / 2; self.last_sample = filt_len; } - /* * Reset a resampler so a new (unrelated) stream can be processed. - * @param st Resampler state - */ + /// Reset a resampler so a new (unrelated) stream can be processed. + /// + /// - @param st Resampler state #[allow(unused)] // For now. pub(crate) fn reset_mem(&mut self) { self.last_sample = 0;