Skip to content

Commit

Permalink
Import and apply rustfmt config from whoami (#15)
Browse files Browse the repository at this point in the history
* Import and apply rustfmt config from whoami

* Manual format changes 1

* Manual format changes 2

* Manual format changes 3

* Fix errors
  • Loading branch information
AldaronLau authored Oct 14, 2024
1 parent ed72e7e commit e9c099e
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 74 deletions.
8 changes: 8 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion examples/mix.rs
Original file line number Diff line number Diff line change
@@ -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> {
Expand Down
3 changes: 1 addition & 2 deletions examples/resample.rs
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
8 changes: 5 additions & 3 deletions examples/sawtooth.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
28 changes: 16 additions & 12 deletions src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
24 changes: 13 additions & 11 deletions src/chan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 7 additions & 5 deletions src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
11 changes: 6 additions & 5 deletions src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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));
}
Expand Down
4 changes: 2 additions & 2 deletions src/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
6 changes: 2 additions & 4 deletions src/sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Chan: Channel, const CH: usize>: Debug {
Expand Down
11 changes: 6 additions & 5 deletions src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
47 changes: 23 additions & 24 deletions src/stream/speex.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit e9c099e

Please sign in to comment.