From 50572b3d8847b3167283dd183c2cc9fbcb83578c Mon Sep 17 00:00:00 2001 From: p6nj Date: Sat, 29 Jun 2024 14:08:50 +0200 Subject: [PATCH] start lib --- Cargo.toml | 3 +++ bingus/Cargo.toml | 2 +- bingus/src/lib.rs | 65 ++++++++++++++++++++++++++++++++++++++------- bingus/src/media.rs | 1 + bong/Cargo.toml | 1 + 5 files changed, 62 insertions(+), 10 deletions(-) create mode 100644 bingus/src/media.rs diff --git a/Cargo.toml b/Cargo.toml index 872e07e..2fb99f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,6 @@ package.description = "databending made easy" package.authors = ["Breval Ferrari "] package.repository = "https://codeberg.org/p6nj/bent" package.keywords = ["databending", "data-bending", "bending", "bend", "art"] + +[workspace.dependencies] +dasp_sample = "0.11.0" diff --git a/bingus/Cargo.toml b/bingus/Cargo.toml index e696687..8027b64 100644 --- a/bingus/Cargo.toml +++ b/bingus/Cargo.toml @@ -9,5 +9,5 @@ repository.workspace = true keywords.workspace = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] +dasp_sample = { workspace = true } diff --git a/bingus/src/lib.rs b/bingus/src/lib.rs index 7d12d9a..a717ba8 100644 --- a/bingus/src/lib.rs +++ b/bingus/src/lib.rs @@ -1,14 +1,61 @@ -pub fn add(left: usize, right: usize) -> usize { - left + right +use dasp_sample::{FromSample, Sample, U24}; +mod media; + +pub trait AsMedia { + fn as_media(self) -> T; } -#[cfg(test)] -mod tests { - use super::*; +pub trait AsSample +where + S: Sample, +{ + fn as_sample(self) -> S; +} - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); +impl AsSample for u8 +where + S: Sample + FromSample, +{ + fn as_sample(self) -> S { + self.to_sample() + } +} + +impl AsSample for (u8, u8) +where + S: Sample + FromSample, +{ + fn as_sample(self) -> S { + (((self.0 as u16) << 8) | self.1 as u16).to_sample() + } +} + +impl AsSample for (u8, u8, u8) +where + S: Sample + FromSample, +{ + fn as_sample(self) -> S { + (((U24::from(self.0)) << 16.into()) | (U24::from(self.1) << 8.into()) | U24::from(self.2)) + .to_sample() + } +} + +impl AsSample for (u8, u8, u8, u8) +where + S: Sample + FromSample, +{ + fn as_sample(self) -> S { + (((self.0 as u32) << 24) | ((self.1 as u32) << 16) | ((self.2 as u32) << 8) | self.3 as u32) + .to_sample() + } +} + +impl AsMedia for T +where + T: AsSample, + S: Sample, +{ + fn as_media(self) -> S { + self.as_sample() } } diff --git a/bingus/src/media.rs b/bingus/src/media.rs new file mode 100644 index 0000000..43abd1f --- /dev/null +++ b/bingus/src/media.rs @@ -0,0 +1 @@ +pub struct Sample; diff --git a/bong/Cargo.toml b/bong/Cargo.toml index 72e02ed..c739bb9 100644 --- a/bong/Cargo.toml +++ b/bong/Cargo.toml @@ -7,6 +7,7 @@ description.workspace = true authors.workspace = true repository.workspace = true keywords.workspace = true +dependencies.workspace = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html