finally a perfect ToSample trait!
This commit is contained in:
parent
41bbd3d371
commit
0194f60ca3
6 changed files with 38 additions and 127 deletions
|
@ -10,4 +10,5 @@ keywords.workspace = true
|
|||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
[dependencies]
|
||||
fundsp = { workspace = true }
|
||||
dasp_sample = { workspace = true }
|
||||
|
|
|
@ -1,61 +1,35 @@
|
|||
use std::{
|
||||
array, mem,
|
||||
ops::{BitOr, Shl},
|
||||
};
|
||||
|
||||
use dasp_sample::{FromSample, Sample, U24};
|
||||
mod media;
|
||||
use fundsp::Real;
|
||||
|
||||
pub trait AsMedia<M, T> {
|
||||
fn as_media(self) -> T;
|
||||
}
|
||||
pub type Byte = u8;
|
||||
|
||||
pub trait AsSample<S>
|
||||
pub trait BytesToSample<Ir, It, Ii>
|
||||
where
|
||||
S: Sample,
|
||||
Self: IntoIterator<Item = It, IntoIter = Ii> + Sized,
|
||||
Ii: Iterator<Item = It>,
|
||||
Ir: FromSample<It>
|
||||
+ Sample
|
||||
+ Default
|
||||
+ From<u8>
|
||||
+ Shl<Ir, Output = Ir>
|
||||
+ BitOr<Ir, Output = Ir>,
|
||||
It: Sample,
|
||||
{
|
||||
fn as_sample(self) -> S;
|
||||
}
|
||||
|
||||
impl<S> AsSample<S> for u8
|
||||
where
|
||||
S: Sample + FromSample<u8>,
|
||||
{
|
||||
fn as_sample(self) -> S {
|
||||
self.to_sample()
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> AsSample<S> for (u8, u8)
|
||||
where
|
||||
S: Sample + FromSample<u16>,
|
||||
{
|
||||
fn as_sample(self) -> S {
|
||||
(((self.0 as u16) << 8) | self.1 as u16).to_sample()
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> AsSample<S> for (u8, u8, u8)
|
||||
where
|
||||
S: Sample + FromSample<U24>,
|
||||
{
|
||||
fn as_sample(self) -> S {
|
||||
(((U24::from(self.0)) << 16.into()) | (U24::from(self.1) << 8.into()) | U24::from(self.2))
|
||||
fn to_sample<S>(self) -> S
|
||||
where
|
||||
S: Real + Sample + FromSample<Ir>,
|
||||
{
|
||||
self.into_iter()
|
||||
.map(|it| Ir::from_sample(it))
|
||||
.reduce(|acc, ir| (acc << Ir::from(mem::size_of::<It>() as u8 * 8u8)) | ir)
|
||||
.unwrap()
|
||||
.to_sample()
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> AsSample<S> for (u8, u8, u8, u8)
|
||||
where
|
||||
S: Sample + FromSample<u32>,
|
||||
{
|
||||
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<T, S> AsMedia<media::Sample, S> for T
|
||||
where
|
||||
T: AsSample<S>,
|
||||
S: Sample,
|
||||
{
|
||||
fn as_media(self) -> S {
|
||||
self.as_sample()
|
||||
}
|
||||
}
|
||||
impl BytesToSample<U24, Byte, array::IntoIter<Byte, 3>> for [Byte; 3] {}
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
pub struct Sample;
|
Loading…
Add table
Add a link
Reference in a new issue