diff --git a/bent-funny-zone/src/main.rs b/bent-funny-zone/src/main.rs index dfd2102..c7e6a80 100644 --- a/bent-funny-zone/src/main.rs +++ b/bent-funny-zone/src/main.rs @@ -1,5 +1,5 @@ use anyhow::Result; -use bingus::BytesToSample; +use bingus::convert::samples::BytesToSample; fn main() -> Result<()> { dbg!([1u8, 2, 3].to_sample::()); diff --git a/bingus/src/convert.rs b/bingus/src/convert.rs new file mode 100644 index 0000000..6d13bdb --- /dev/null +++ b/bingus/src/convert.rs @@ -0,0 +1,3 @@ +pub mod samples; + +pub type Byte = u8; diff --git a/bingus/src/convert/samples.rs b/bingus/src/convert/samples.rs new file mode 100644 index 0000000..bbbf6e7 --- /dev/null +++ b/bingus/src/convert/samples.rs @@ -0,0 +1,6 @@ +use super::Byte; + +mod from; +mod into; + +pub use into::BytesToSample; diff --git a/bingus/src/convert/samples/from.rs b/bingus/src/convert/samples/from.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/bingus/src/convert/samples/from.rs @@ -0,0 +1 @@ + diff --git a/bingus/src/convert/samples/into.rs b/bingus/src/convert/samples/into.rs new file mode 100644 index 0000000..0e626d6 --- /dev/null +++ b/bingus/src/convert/samples/into.rs @@ -0,0 +1,51 @@ +use std::{ + array, mem, + ops::{BitOr, Shl}, +}; + +use dasp_sample::{FromSample, Sample, U24, U48}; +use fundsp::Real; + +use super::Byte; + +pub trait BytesToSample +where + Self: IntoIterator + Sized, + Ii: Iterator, + Ir: FromSample + + Sample + + Default + + From + + Shl + + BitOr, + It: Sample, +{ + fn to_sample(self) -> S + where + S: Real + Sample + FromSample, + { + self.into_iter() + .map(|it| Ir::from_sample(it)) + .reduce(|acc, ir| (acc << Ir::from(mem::size_of::() as u8 * 8u8)) | ir) + .unwrap() + .to_sample() + } +} + +macro_rules! impl_bts_for_array_of { + ($($N:expr, $Ir:ty);*) => + { + $( + impl BytesToSample<$Ir, Byte, array::IntoIter> for [Byte; $N] {} + )* + } +} + +impl_bts_for_array_of! { + 1, u8; + 2, u16; + 3, U24; + 4, u32; + 6, U48; + 8, u64 +} diff --git a/bingus/src/lib.rs b/bingus/src/lib.rs index adb5159..b5b6721 100644 --- a/bingus/src/lib.rs +++ b/bingus/src/lib.rs @@ -1,51 +1 @@ -use std::{ - array, mem, - ops::{BitOr, Shl}, -}; - -use dasp_sample::{FromSample, Sample, U24, U48}; -use fundsp::Real; - -pub type Byte = u8; - -pub trait BytesToSample -where - Self: IntoIterator + Sized, - Ii: Iterator, - Ir: FromSample - + Sample - + Default - + From - + Shl - + BitOr, - It: Sample, -{ - fn to_sample(self) -> S - where - S: Real + Sample + FromSample, - { - self.into_iter() - .map(|it| Ir::from_sample(it)) - .reduce(|acc, ir| (acc << Ir::from(mem::size_of::() as u8 * 8u8)) | ir) - .unwrap() - .to_sample() - } -} - -macro_rules! impl_bts_for_array_of { - ($($N:expr, $Ir:ty);*) => - { - $( - impl BytesToSample<$Ir, Byte, array::IntoIter> for [Byte; $N] {} - )* - } -} - -impl_bts_for_array_of! { - 1, u8; - 2, u16; - 3, U24; - 4, u32; - 6, U48; - 8, u64 -} +pub mod convert;