Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
AldaronLau committed Oct 14, 2024
1 parent cea643c commit b73be49
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 140 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
16 changes: 6 additions & 10 deletions examples/mix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Samp, N>
for Mixer<'a, Samp, N>
{
impl<'a, Samp: Sample, const N: usize> Sink<Samp, N> for Mixer<'a, Samp, N> {
#[inline(always)]
fn sample_rate(&self) -> NonZeroU32 {
self.audio.sample_rate()
Expand All @@ -45,9 +43,7 @@ impl<'a, Samp: Sample, const N: usize> Sink<Samp, N>
}
}

impl<Samp: Sample, const N: usize> Sink<Samp, N>
for &mut Mixer<'_, Samp, N>
{
impl<Samp: Sample, const N: usize> Sink<Samp, N> for &mut Mixer<'_, Samp, N> {
#[inline(always)]
fn sample_rate(&self) -> NonZeroU32 {
self.audio.sample_rate()
Expand All @@ -62,10 +58,10 @@ impl<Samp: Sample, const N: usize> Sink<Samp, N>
fn sink_with(&mut self, iter: &mut dyn Iterator<Item = Frame<Samp, N>>) {
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;
Expand All @@ -90,8 +86,8 @@ fn save_file(name: &str, audio: &Audio<Samp32, 2>) -> std::io::Result<()> {
// Convert audio to byte buffer
let mut samples = Vec::<u8>::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
Expand Down
2 changes: 1 addition & 1 deletion examples/sawtooth.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use fon::{
samp::{Samp16, Samp32},
pos::Mono,
samp::{Samp16, Samp32},
Audio,
};

Expand Down
18 changes: 13 additions & 5 deletions src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -61,7 +61,8 @@ impl<Samp: Sample, const COUNT: usize> Audio<Samp, COUNT> {
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
Expand Down Expand Up @@ -162,7 +163,10 @@ impl<'a, Samp: Sample, const COUNT: usize> Sink<Samp, COUNT>
}

#[inline(always)]
fn sink_with(&mut self, iter: &mut dyn Iterator<Item = Frame<Samp, COUNT>>) {
fn sink_with(
&mut self,
iter: &mut dyn Iterator<Item = Frame<Samp, COUNT>>,
) {
let mut this = self;
Sink::<Samp, COUNT>::sink_with(&mut this, iter)
}
Expand All @@ -182,7 +186,10 @@ impl<Samp: Sample, const COUNT: usize> Sink<Samp, COUNT>
}

#[inline(always)]
fn sink_with(&mut self, iter: &mut dyn Iterator<Item = Frame<Samp, COUNT>>) {
fn sink_with(
&mut self,
iter: &mut dyn Iterator<Item = Frame<Samp, COUNT>>,
) {
for frame in self.audio.iter_mut().skip(self.index) {
*frame = if let Some(frame) = iter.next() {
frame
Expand Down Expand Up @@ -326,7 +333,8 @@ impl<const COUNT: usize> Audio<Samp64, COUNT> {
}
}

impl<Samp, const COUNT: usize> From<Audio<Samp, COUNT>> for Vec<Frame<Samp, COUNT>>
impl<Samp, const COUNT: usize> From<Audio<Samp, COUNT>>
for Vec<Frame<Samp, COUNT>>
where
Samp: Sample,
{
Expand Down
Loading

0 comments on commit b73be49

Please sign in to comment.