diff --git a/Cargo.toml b/Cargo.toml index a4c7d6e4..253cdbd7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,7 @@ [package] - name = "sample" description = "A crate providing the fundamentals for working with audio PCM DSP." -version = "0.6.2" +version = "0.7.0" authors = ["mitchmindtree "] readme = "README.md" keywords = ["dsp", "bit-depth", "rate", "pcm", "audio"] diff --git a/src/signal.rs b/src/signal.rs index 764682a3..00ddd57d 100644 --- a/src/signal.rs +++ b/src/signal.rs @@ -52,6 +52,67 @@ pub trait Signal { /// ``` fn next(&mut self) -> Self::Frame; + /// A signal that maps one set of frames to another + /// + /// # Example + /// + /// ```rust + /// extern crate sample; + /// + /// use sample::{signal, Signal}; + /// + /// fn main() { + /// let frames = signal::gen(|| [0.5]); + /// let mut mapper = frames.map(|f| [f[0], 0.25]); + /// assert_eq!(mapper.next(), [0.5, 0.25]); + /// assert_eq!(mapper.next(), [0.5, 0.25]); + /// assert_eq!(mapper.next(), [0.5, 0.25]); + /// } + /// ``` + fn map(self, map: M) -> Map + where Self: Sized, + M: FnMut(Self::Frame) -> F, + F: Frame, + { + Map { + signal: self, + map: map, + frame: core::marker::PhantomData, + } + } + + /// A signal that maps one set of frames to another + /// + /// # Example + /// + /// ```rust + /// extern crate sample; + /// + /// use sample::{signal, Signal}; + /// + /// fn main() { + /// let frames = signal::gen(|| [0.5]); + /// let more_frames = signal::gen(|| [0.25]); + /// let mut mapper = frames.zip_map(more_frames, |f, o| [f[0], o[0]]); + /// assert_eq!(mapper.next(), [0.5, 0.25]); + /// assert_eq!(mapper.next(), [0.5, 0.25]); + /// assert_eq!(mapper.next(), [0.5, 0.25]); + /// } + /// ``` + fn zip_map(self, other: O, map: M) -> ZipMap + where Self: Sized, + M: FnMut(Self::Frame, O::Frame) -> F, + O: Signal, + F: Frame, + { + ZipMap { + this: self, + map: map, + other: other, + frame: core::marker::PhantomData, + } + } + /// Provides an iterator that yields the sum of the frames yielded by both `other` and `self` /// in lock-step. /// @@ -558,18 +619,18 @@ pub struct GenMut { /// A signal that maps from one signal to another #[derive(Clone)] -pub struct Map { - map: M, +pub struct Map { signal: S, - frames: core::marker::PhantomData, + map: M, + frame: core::marker::PhantomData, } /// A signal that iterates two signals in parallel and combines them with a function #[derive(Clone)] -pub struct ZipMap { - map: M, +pub struct ZipMap { this: S, other: O, + map: M, frame: core::marker::PhantomData } @@ -882,68 +943,6 @@ pub fn gen_mut(gen_mut: G) -> GenMut } -/// A signal that maps one set of frames to another -/// -/// # Example -/// -/// ```rust -/// extern crate sample; -/// -/// use sample::{signal, Signal}; -/// -/// fn main() { -/// let frames = signal::gen(|| [0.5]); -/// let mut mapper = signal::map(frames, |f| [f[0], 0.25]); -/// assert_eq!(mapper.next(), [0.5, 0.25]); -/// assert_eq!(mapper.next(), [0.5, 0.25]); -/// assert_eq!(mapper.next(), [0.5, 0.25]); -/// } -/// ``` -pub fn map(signal: S, map: M) -> Map - where M: FnMut(S::Frame) -> F, - S: Signal, - F: Frame, -{ - Map { - map: map, - signal: signal, - frames: core::marker::PhantomData, - } -} - - -/// A signal that maps one set of frames to another -/// -/// # Example -/// -/// ```rust -/// extern crate sample; -/// -/// use sample::{signal, Signal}; -/// -/// fn main() { -/// let frames = signal::gen(|| [0.5]); -/// let more_frames = signal::gen(|| [0.25]); -/// let mut mapper = signal::zip_map(frames, more_frames, |f, o| [f[0], o[0]]); -/// assert_eq!(mapper.next(), [0.5, 0.25]); -/// assert_eq!(mapper.next(), [0.5, 0.25]); -/// assert_eq!(mapper.next(), [0.5, 0.25]); -/// } -/// ``` -pub fn zip_map(this: S, other: O, map: M) -> ZipMap - where M: FnMut(S::Frame, O::Frame) -> F, - S: Signal, - O: Signal, - F: Frame, -{ - ZipMap { - map: map, - this: this, - other: other, - frame: core::marker::PhantomData, - } -} - /// Create a new `Signal` from the given `Frame`-yielding `Iterator`. /// @@ -1239,9 +1238,9 @@ impl Signal for GenMut } -impl Signal for Map - where M: FnMut(S::Frame) -> F, - S: Signal, +impl Signal for Map + where S: Signal, + M: FnMut(S::Frame) -> F, F: Frame, { type Frame = F; @@ -1252,10 +1251,10 @@ impl Signal for Map } -impl Signal for ZipMap - where M: FnMut(S::Frame, O::Frame) -> F, - S: Signal, +impl Signal for ZipMap + where S: Signal, O: Signal, + M: FnMut(S::Frame, O::Frame) -> F, F: Frame, { type Frame = F;