Skip to content

Commit

Permalink
Rename channel to sample
Browse files Browse the repository at this point in the history
  • Loading branch information
AldaronLau committed Oct 14, 2024
1 parent f564bf2 commit ab34575
Show file tree
Hide file tree
Showing 12 changed files with 1,193 additions and 1,194 deletions.
32 changes: 16 additions & 16 deletions examples/mix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
use std::num::NonZeroU32;

use fon::{
chan::{Ch32, Channel},
samp::{Samp32, Sample},
Audio, Frame, Sink, Stream,
};

#[derive(Debug)]
pub struct Mixer<'a, Chan: Channel, const CH: usize> {
pub struct Mixer<'a, Samp: Sample, const CH: usize> {
index: usize,
audio: &'a mut Audio<Chan, CH>,
audio: &'a mut Audio<Samp, CH>,
}

#[allow(single_use_lifetimes)]
impl<'a, Chan: Channel, const CH: usize> Mixer<'a, Chan, CH> {
impl<'a, Samp: Sample, const CH: usize> Mixer<'a, Samp, CH> {
#[inline(always)]
fn new(audio: &'a mut Audio<Chan, CH>) -> Self {
fn new(audio: &'a mut Audio<Samp, CH>) -> Self {
let index = 0;

Mixer { index, audio }
Expand All @@ -25,8 +25,8 @@ impl<'a, Chan: Channel, const CH: usize> Mixer<'a, Chan, CH> {

// Using '_ results in reserved lifetime error.
#[allow(single_use_lifetimes)]
impl<'a, Chan: Channel, const CH: usize> Sink<Chan, CH>
for Mixer<'a, Chan, CH>
impl<'a, Samp: Sample, const CH: usize> Sink<Samp, CH>
for Mixer<'a, Samp, CH>
{
#[inline(always)]
fn sample_rate(&self) -> NonZeroU32 {
Expand All @@ -39,14 +39,14 @@ impl<'a, Chan: Channel, const CH: usize> Sink<Chan, CH>
}

#[inline(always)]
fn sink_with(&mut self, iter: &mut dyn Iterator<Item = Frame<Chan, CH>>) {
fn sink_with(&mut self, iter: &mut dyn Iterator<Item = Frame<Samp, CH>>) {
let mut this = self;
Sink::<Chan, CH>::sink_with(&mut this, iter)
Sink::<Samp, CH>::sink_with(&mut this, iter)
}
}

impl<Chan: Channel, const CH: usize> Sink<Chan, CH>
for &mut Mixer<'_, Chan, CH>
impl<Samp: Sample, const CH: usize> Sink<Samp, CH>
for &mut Mixer<'_, Samp, CH>
{
#[inline(always)]
fn sample_rate(&self) -> NonZeroU32 {
Expand All @@ -59,11 +59,11 @@ impl<Chan: Channel, const CH: usize> Sink<Chan, CH>
}

#[inline(always)]
fn sink_with(&mut self, iter: &mut dyn Iterator<Item = Frame<Chan, CH>>) {
fn sink_with(&mut self, iter: &mut dyn Iterator<Item = Frame<Samp, CH>>) {
for frame in self.audio.iter_mut().skip(self.index) {
if let Some(other) = iter.next() {
for (channel, chan) in
frame.channels_mut().iter_mut().zip(other.channels())
frame.samples_mut().iter_mut().zip(other.samples())
{
*channel += *chan;
}
Expand All @@ -75,7 +75,7 @@ impl<Chan: Channel, const CH: usize> Sink<Chan, CH>
}
}

fn load_file(in_hz: u32, in_file: &str) -> Audio<Ch32, 2> {
fn load_file(in_hz: u32, in_file: &str) -> Audio<Samp32, 2> {
// Load file as f32 buffer.
let rawfile = std::fs::read(in_file).unwrap();
let mut audio = Vec::new();
Expand All @@ -86,11 +86,11 @@ fn load_file(in_hz: u32, in_file: &str) -> Audio<Ch32, 2> {
Audio::with_f32_buffer(in_hz, audio)
}

fn save_file(name: &str, audio: &Audio<Ch32, 2>) -> std::io::Result<()> {
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.channels() {
for channel in frame.samples() {
samples.extend(channel.to_f32().to_le_bytes());
}
}
Expand Down
6 changes: 3 additions & 3 deletions examples/resample.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::convert::TryInto;

use fon::{chan::Ch32, Audio};
use fon::{samp::Samp32, 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 All @@ -11,9 +11,9 @@ fn resample(in_hz: u32, in_file: &str, out_hz: u32, out_file: &str) {
audio.push(f32::from_le_bytes(sample.try_into().unwrap()));
}
// Create type-safe audio type from f32 buffer.
let audio = Audio::<Ch32, 2>::with_f32_buffer(in_hz, audio);
let audio = Audio::<Samp32, 2>::with_f32_buffer(in_hz, audio);
// Stream resampler into new audio type.
let mut audio = Audio::<Ch32, 2>::with_audio(out_hz, &audio);
let mut audio = Audio::<Samp32, 2>::with_audio(out_hz, &audio);
// Write file as f32 buffer.
let mut bytes = Vec::new();
for sample in audio.as_f32_slice() {
Expand Down
6 changes: 3 additions & 3 deletions examples/sawtooth.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use fon::{
chan::{Ch16, Ch32},
samp::{Samp16, Samp32},
pos::Mono,
Audio,
};

fn main() {
// Create mono 32-bit floating point audio buffer.
let mut a = Audio::<Ch32, 1>::with_silence(48_000, 256);
let mut a = Audio::<Samp32, 1>::with_silence(48_000, 256);
let mut counter = 0.0;
for f in a.iter_mut() {
f[Mono] = counter.into();
Expand All @@ -15,7 +15,7 @@ fn main() {
}

// Convert to 16-Bit audio format
let mut audio = Audio::<Ch16, 1>::with_audio(48_000, &a);
let mut audio = Audio::<Samp16, 1>::with_audio(48_000, &a);

// Print out converted wave.
for (sample, other) in
Expand Down
Loading

0 comments on commit ab34575

Please sign in to comment.