Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename channel to sample #21

Merged
merged 3 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
38 changes: 17 additions & 21 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 N: usize> {
index: usize,
audio: &'a mut Audio<Chan, CH>,
audio: &'a mut Audio<Samp, N>,
}

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

Mixer { index, audio }
Expand All @@ -25,9 +25,7 @@ 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 N: usize> Sink<Samp, N> for Mixer<'a, Samp, N> {
#[inline(always)]
fn sample_rate(&self) -> NonZeroU32 {
self.audio.sample_rate()
Expand All @@ -39,15 +37,13 @@ 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, N>>) {
let mut this = self;
Sink::<Chan, CH>::sink_with(&mut this, iter)
Sink::<Samp, N>::sink_with(&mut this, iter)
}
}

impl<Chan: Channel, const CH: usize> Sink<Chan, CH>
for &mut Mixer<'_, Chan, CH>
{
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 @@ -59,13 +55,13 @@ 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, N>>) {
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())
for (sample, samp) in
frame.samples_mut().iter_mut().zip(other.samples())
{
*channel += *chan;
*sample += *samp;
}
} else {
break;
Expand All @@ -75,7 +71,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,12 +82,12 @@ 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() {
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
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},
pos::Mono,
samp::{Samp16, Samp32},
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
Loading