raw sound samples

This commit is contained in:
Breval Ferrari 2025-03-10 00:31:20 -04:00
parent 68b90af755
commit 76ced9bb9d
No known key found for this signature in database
GPG key ID: F71E304D6400AB8E
3 changed files with 84 additions and 0 deletions

View file

@ -17,3 +17,5 @@ infer = "0.16"
thiserror = "2.0"
derive-new = "0.7"
strum = { version = "0.26", features = ["derive"] }
dasp_sample = "0.11.0"
derive_wrapper = "0.1"

View file

@ -0,0 +1,3 @@
pub use dasp_sample::Sample;
mod raw;
pub use raw::RawSamples;

79
bingus/src/snd/raw.rs Normal file
View file

@ -0,0 +1,79 @@
use super::Sample;
use derive_wrapper::{AsRef, From};
use num::{
traits::{FromBytes, ToBytes},
Zero,
};
use crate::{Bendable, IntoDataBytes, TryFromDataBytes};
#[derive(From, AsRef)]
pub struct RawSamples<T>(Vec<T>)
where
T: Sample;
impl<T> IntoDataBytes for RawSamples<T>
where
T: Sample + ToBytes,
{
fn into_bytes(self) -> crate::Bytes {
self.as_ref()
.iter()
.flat_map(|subpixel| subpixel.to_ne_bytes().as_ref().to_vec())
.collect()
}
}
impl<T> TryFromDataBytes for RawSamples<T>
where
T: Sample + FromBytes + ToBytes + Zero,
<T as FromBytes>::Bytes: Sized + for<'a> TryFrom<&'a [u8]>,
{
type Error = ();
type Format = ();
fn try_from_bytes(
bytes: crate::Bytes,
_format: Self::Format,
crop: crate::Crop,
) -> Result<Self, Self::Error>
where
Self: Sized,
{
Ok(match crop {
crate::Crop::End => bytes
.chunks_exact(T::zero().to_ne_bytes().as_ref().len())
.map(|p| {
T::from_ne_bytes(&match <T as FromBytes>::Bytes::try_from(p) {
Ok(v) => v,
Err(_) => unreachable!("you messed up chunk size!"),
})
})
.collect::<Vec<T>>(),
crate::Crop::Start => bytes
.rchunks_exact(T::zero().to_ne_bytes().as_ref().len())
.map(|p| {
T::from_ne_bytes(&match <T as FromBytes>::Bytes::try_from(p) {
Ok(v) => v,
Err(_) => unreachable!("you messed up chunk size!"),
})
})
.collect::<Vec<T>>(),
}
.into())
}
}
impl<T> Bendable for RawSamples<T>
where
T: Sample + FromBytes + ToBytes + Zero,
<T as FromBytes>::Bytes: Sized + for<'a> TryFrom<&'a [u8]>,
{
type Unit = T;
fn map<F: Fn(&Self::Unit) -> Self::Unit + Sync>(mut self, f: F) -> Self {
self.0.iter_mut().for_each(|e| *e = f(e));
self
}
fn format() -> crate::dynamic::Format {
crate::Format::Sound
}
}