Skip to content

Commit b76d532

Browse files
Merge pull request #70 from mitchmindtree/publish
Move signal `map` and `zip_map` functions to `Signal` trait methods. Publish 0.7.
2 parents 49e3670 + 42c5d5c commit b76d532

File tree

2 files changed

+73
-75
lines changed

2 files changed

+73
-75
lines changed

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
[package]
2-
32
name = "sample"
43
description = "A crate providing the fundamentals for working with audio PCM DSP."
5-
version = "0.6.2"
4+
version = "0.7.0"
65
authors = ["mitchmindtree <[email protected]>"]
76
readme = "README.md"
87
keywords = ["dsp", "bit-depth", "rate", "pcm", "audio"]

src/signal.rs

Lines changed: 72 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,67 @@ pub trait Signal {
5252
/// ```
5353
fn next(&mut self) -> Self::Frame;
5454

55+
/// A signal that maps one set of frames to another
56+
///
57+
/// # Example
58+
///
59+
/// ```rust
60+
/// extern crate sample;
61+
///
62+
/// use sample::{signal, Signal};
63+
///
64+
/// fn main() {
65+
/// let frames = signal::gen(|| [0.5]);
66+
/// let mut mapper = frames.map(|f| [f[0], 0.25]);
67+
/// assert_eq!(mapper.next(), [0.5, 0.25]);
68+
/// assert_eq!(mapper.next(), [0.5, 0.25]);
69+
/// assert_eq!(mapper.next(), [0.5, 0.25]);
70+
/// }
71+
/// ```
72+
fn map<M, F>(self, map: M) -> Map<Self, M, F>
73+
where Self: Sized,
74+
M: FnMut(Self::Frame) -> F,
75+
F: Frame,
76+
{
77+
Map {
78+
signal: self,
79+
map: map,
80+
frame: core::marker::PhantomData,
81+
}
82+
}
83+
84+
/// A signal that maps one set of frames to another
85+
///
86+
/// # Example
87+
///
88+
/// ```rust
89+
/// extern crate sample;
90+
///
91+
/// use sample::{signal, Signal};
92+
///
93+
/// fn main() {
94+
/// let frames = signal::gen(|| [0.5]);
95+
/// let more_frames = signal::gen(|| [0.25]);
96+
/// let mut mapper = frames.zip_map(more_frames, |f, o| [f[0], o[0]]);
97+
/// assert_eq!(mapper.next(), [0.5, 0.25]);
98+
/// assert_eq!(mapper.next(), [0.5, 0.25]);
99+
/// assert_eq!(mapper.next(), [0.5, 0.25]);
100+
/// }
101+
/// ```
102+
fn zip_map<O, M, F>(self, other: O, map: M) -> ZipMap<Self, O, M, F>
103+
where Self: Sized,
104+
M: FnMut(Self::Frame, O::Frame) -> F,
105+
O: Signal,
106+
F: Frame,
107+
{
108+
ZipMap {
109+
this: self,
110+
map: map,
111+
other: other,
112+
frame: core::marker::PhantomData,
113+
}
114+
}
115+
55116
/// Provides an iterator that yields the sum of the frames yielded by both `other` and `self`
56117
/// in lock-step.
57118
///
@@ -558,18 +619,18 @@ pub struct GenMut<G, F> {
558619

559620
/// A signal that maps from one signal to another
560621
#[derive(Clone)]
561-
pub struct Map<M, S, F> {
562-
map: M,
622+
pub struct Map<S, M, F> {
563623
signal: S,
564-
frames: core::marker::PhantomData<F>,
624+
map: M,
625+
frame: core::marker::PhantomData<F>,
565626
}
566627

567628
/// A signal that iterates two signals in parallel and combines them with a function
568629
#[derive(Clone)]
569-
pub struct ZipMap<M, S, O, F> {
570-
map: M,
630+
pub struct ZipMap<S, O, M, F> {
571631
this: S,
572632
other: O,
633+
map: M,
573634
frame: core::marker::PhantomData<F>
574635
}
575636

@@ -882,68 +943,6 @@ pub fn gen_mut<G, F>(gen_mut: G) -> GenMut<G, F>
882943
}
883944

884945

885-
/// A signal that maps one set of frames to another
886-
///
887-
/// # Example
888-
///
889-
/// ```rust
890-
/// extern crate sample;
891-
///
892-
/// use sample::{signal, Signal};
893-
///
894-
/// fn main() {
895-
/// let frames = signal::gen(|| [0.5]);
896-
/// let mut mapper = signal::map(frames, |f| [f[0], 0.25]);
897-
/// assert_eq!(mapper.next(), [0.5, 0.25]);
898-
/// assert_eq!(mapper.next(), [0.5, 0.25]);
899-
/// assert_eq!(mapper.next(), [0.5, 0.25]);
900-
/// }
901-
/// ```
902-
pub fn map<M, S, F>(signal: S, map: M) -> Map<M, S, F>
903-
where M: FnMut(S::Frame) -> F,
904-
S: Signal,
905-
F: Frame,
906-
{
907-
Map {
908-
map: map,
909-
signal: signal,
910-
frames: core::marker::PhantomData,
911-
}
912-
}
913-
914-
915-
/// A signal that maps one set of frames to another
916-
///
917-
/// # Example
918-
///
919-
/// ```rust
920-
/// extern crate sample;
921-
///
922-
/// use sample::{signal, Signal};
923-
///
924-
/// fn main() {
925-
/// let frames = signal::gen(|| [0.5]);
926-
/// let more_frames = signal::gen(|| [0.25]);
927-
/// let mut mapper = signal::zip_map(frames, more_frames, |f, o| [f[0], o[0]]);
928-
/// assert_eq!(mapper.next(), [0.5, 0.25]);
929-
/// assert_eq!(mapper.next(), [0.5, 0.25]);
930-
/// assert_eq!(mapper.next(), [0.5, 0.25]);
931-
/// }
932-
/// ```
933-
pub fn zip_map<M, S, O, F>(this: S, other: O, map: M) -> ZipMap<M, S, O, F>
934-
where M: FnMut(S::Frame, O::Frame) -> F,
935-
S: Signal,
936-
O: Signal,
937-
F: Frame,
938-
{
939-
ZipMap {
940-
map: map,
941-
this: this,
942-
other: other,
943-
frame: core::marker::PhantomData,
944-
}
945-
}
946-
947946

948947
/// Create a new `Signal` from the given `Frame`-yielding `Iterator`.
949948
///
@@ -1239,9 +1238,9 @@ impl<G, F> Signal for GenMut<G, F>
12391238
}
12401239

12411240

1242-
impl<M, S, F> Signal for Map<M, S, F>
1243-
where M: FnMut(S::Frame) -> F,
1244-
S: Signal,
1241+
impl<S, M, F> Signal for Map<S, M, F>
1242+
where S: Signal,
1243+
M: FnMut(S::Frame) -> F,
12451244
F: Frame,
12461245
{
12471246
type Frame = F;
@@ -1252,10 +1251,10 @@ impl<M, S, F> Signal for Map<M, S, F>
12521251
}
12531252

12541253

1255-
impl<M, S, O, F> Signal for ZipMap<M, S, O, F>
1256-
where M: FnMut(S::Frame, O::Frame) -> F,
1257-
S: Signal,
1254+
impl<S, O, M, F> Signal for ZipMap<S, O, M, F>
1255+
where S: Signal,
12581256
O: Signal,
1257+
M: FnMut(S::Frame, O::Frame) -> F,
12591258
F: Frame,
12601259
{
12611260
type Frame = F;

0 commit comments

Comments
 (0)