Trait sample::signal::Signal
[−]
[src]
pub trait Signal { type Frame: Frame; fn next(&mut self) -> Self::Frame; fn map<M, F>(self, map: M) -> Map<Self, M, F>
where
Self: Sized,
M: FnMut(Self::Frame) -> F,
F: Frame, { ... } fn zip_map<O, M, F>(self, other: O, map: M) -> ZipMap<Self, O, M, F>
where
Self: Sized,
M: FnMut(Self::Frame, O::Frame) -> F,
O: Signal,
F: Frame, { ... } fn add_amp<S>(self, other: S) -> AddAmp<Self, S>
where
Self: Sized,
S: Signal,
S::Frame: Frame<Sample = <<Self::Frame as Frame>::Sample as Sample>::Signed, NumChannels = <Self::Frame as Frame>::NumChannels>, { ... } fn mul_amp<S>(self, other: S) -> MulAmp<Self, S>
where
Self: Sized,
S: Signal,
S::Frame: Frame<Sample = <<Self::Frame as Frame>::Sample as Sample>::Float, NumChannels = <Self::Frame as Frame>::NumChannels>, { ... } fn offset_amp(
self,
offset: <<Self::Frame as Frame>::Sample as Sample>::Signed
) -> OffsetAmp<Self>
where
Self: Sized, { ... } fn scale_amp(
self,
amp: <<Self::Frame as Frame>::Sample as Sample>::Float
) -> ScaleAmp<Self>
where
Self: Sized, { ... } fn offset_amp_per_channel<F>(
self,
amp_frame: F
) -> OffsetAmpPerChannel<Self, F>
where
Self: Sized,
F: Frame<Sample = <<Self::Frame as Frame>::Sample as Sample>::Signed, NumChannels = <Self::Frame as Frame>::NumChannels>, { ... } fn scale_amp_per_channel<F>(
self,
amp_frame: F
) -> ScaleAmpPerChannel<Self, F>
where
Self: Sized,
F: Frame<Sample = <<Self::Frame as Frame>::Sample as Sample>::Float, NumChannels = <Self::Frame as Frame>::NumChannels>, { ... } fn mul_hz<M, I>(
self,
interpolator: I,
mul_per_frame: M
) -> MulHz<Self, M, I>
where
Self: Sized,
M: Signal<Frame = [f64; 1]>,
I: Interpolator, { ... } fn from_hz_to_hz<I>(
self,
interpolator: I,
source_hz: f64,
target_hz: f64
) -> Converter<Self, I>
where
Self: Sized,
I: Interpolator, { ... } fn scale_hz<I>(self, interpolator: I, multi: f64) -> Converter<Self, I>
where
Self: Sized,
I: Interpolator, { ... } fn delay(self, n_frames: usize) -> Delay<Self>
where
Self: Sized, { ... } fn into_interleaved_samples(self) -> IntoInterleavedSamples<Self>
where
Self: Sized, { ... } fn clip_amp(
self,
thresh: <<Self::Frame as Frame>::Sample as Sample>::Signed
) -> ClipAmp<Self>
where
Self: Sized, { ... } fn inspect<F>(self, inspect: F) -> Inspect<Self, F>
where
Self: Sized,
F: FnMut(&Self::Frame), { ... } fn bus(self) -> Bus<Self>
where
Self: Sized, { ... } fn take(self, n: usize) -> Take<Self>
where
Self: Sized, { ... } fn by_ref(&mut self) -> &mut Self
where
Self: Sized, { ... } }
Types that yield Frame
s as a multi-channel PCM signal.
For example, Signal
allows us to add two signals, modulate a signal's amplitude by another
signal, scale a signals amplitude and much more.
Associated Types
Required Methods
fn next(&mut self) -> Self::Frame
Yield the next Frame
in the Signal
.
Example
extern crate sample; use sample::{signal, Signal}; fn main() { let frames = [[0.2], [-0.6], [0.4]]; let mut signal = signal::from_iter(frames.iter().cloned()); assert_eq!(signal.next(), [0.2]); assert_eq!(signal.next(), [-0.6]); assert_eq!(signal.next(), [0.4]); }
Provided Methods
fn map<M, F>(self, map: M) -> Map<Self, M, F> where
Self: Sized,
M: FnMut(Self::Frame) -> F,
F: Frame,
Self: Sized,
M: FnMut(Self::Frame) -> F,
F: Frame,
A signal that maps one set of frames to another
Example
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 zip_map<O, M, F>(self, other: O, map: M) -> ZipMap<Self, O, M, F> where
Self: Sized,
M: FnMut(Self::Frame, O::Frame) -> F,
O: Signal,
F: Frame,
Self: Sized,
M: FnMut(Self::Frame, O::Frame) -> F,
O: Signal,
F: Frame,
A signal that maps one set of frames to another
Example
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 add_amp<S>(self, other: S) -> AddAmp<Self, S> where
Self: Sized,
S: Signal,
S::Frame: Frame<Sample = <<Self::Frame as Frame>::Sample as Sample>::Signed, NumChannels = <Self::Frame as Frame>::NumChannels>,
Self: Sized,
S: Signal,
S::Frame: Frame<Sample = <<Self::Frame as Frame>::Sample as Sample>::Signed, NumChannels = <Self::Frame as Frame>::NumChannels>,
Provides an iterator that yields the sum of the frames yielded by both other
and self
in lock-step.
Example
extern crate sample; use sample::{signal, Signal}; fn main() { let a = [[0.2], [-0.6], [0.4]]; let b = [[0.2], [0.1], [-0.8]]; let a_signal = signal::from_iter(a.iter().cloned()); let b_signal = signal::from_iter(b.iter().cloned()); let added: Vec<_> = a_signal.add_amp(b_signal).take(3).collect(); assert_eq!(added, vec![[0.4], [-0.5], [-0.4]]); }
fn mul_amp<S>(self, other: S) -> MulAmp<Self, S> where
Self: Sized,
S: Signal,
S::Frame: Frame<Sample = <<Self::Frame as Frame>::Sample as Sample>::Float, NumChannels = <Self::Frame as Frame>::NumChannels>,
Self: Sized,
S: Signal,
S::Frame: Frame<Sample = <<Self::Frame as Frame>::Sample as Sample>::Float, NumChannels = <Self::Frame as Frame>::NumChannels>,
Provides an iterator that yields the product of the frames yielded by both other
and
self
in lock-step.
Example
extern crate sample; use sample::{signal, Signal}; fn main() { let a = [[0.25], [-0.8], [-0.5]]; let b = [[0.2], [0.5], [0.8]]; let a_signal = signal::from_iter(a.iter().cloned()); let b_signal = signal::from_iter(b.iter().cloned()); let added: Vec<_> = a_signal.mul_amp(b_signal).take(3).collect(); assert_eq!(added, vec![[0.05], [-0.4], [-0.4]]); }
fn offset_amp(
self,
offset: <<Self::Frame as Frame>::Sample as Sample>::Signed
) -> OffsetAmp<Self> where
Self: Sized,
self,
offset: <<Self::Frame as Frame>::Sample as Sample>::Signed
) -> OffsetAmp<Self> where
Self: Sized,
Provides an iterator that offsets the amplitude of every channel in each frame of the signal by some sample value and yields the resulting frames.
Example
extern crate sample; use sample::{signal, Signal}; fn main() { let frames = [[0.25, 0.4], [-0.2, -0.5]]; let signal = signal::from_iter(frames.iter().cloned()); let offset: Vec<_> = signal.offset_amp(0.5).take(2).collect(); assert_eq!(offset, vec![[0.75, 0.9], [0.3, 0.0]]); }
fn scale_amp(
self,
amp: <<Self::Frame as Frame>::Sample as Sample>::Float
) -> ScaleAmp<Self> where
Self: Sized,
self,
amp: <<Self::Frame as Frame>::Sample as Sample>::Float
) -> ScaleAmp<Self> where
Self: Sized,
Produces an Iterator
that scales the amplitude of the sample of each channel in every
Frame
yielded by self
by the given amplitude.
Example
extern crate sample; use sample::{signal, Signal}; fn main() { let frames = [[0.2], [-0.5], [-0.4], [0.3]]; let signal = signal::from_iter(frames.iter().cloned()); let scaled: Vec<_> = signal.scale_amp(2.0).take(4).collect(); assert_eq!(scaled, vec![[0.4], [-1.0], [-0.8], [0.6]]); }
fn offset_amp_per_channel<F>(self, amp_frame: F) -> OffsetAmpPerChannel<Self, F> where
Self: Sized,
F: Frame<Sample = <<Self::Frame as Frame>::Sample as Sample>::Signed, NumChannels = <Self::Frame as Frame>::NumChannels>,
Self: Sized,
F: Frame<Sample = <<Self::Frame as Frame>::Sample as Sample>::Signed, NumChannels = <Self::Frame as Frame>::NumChannels>,
Produces a new Signal
that offsets the amplitude of every Frame
in self
by the
respective amplitudes in each channel of the given amp_frame
.
Example
extern crate sample; use sample::{signal, Signal}; fn main() { let frames = [[0.5, 0.3], [-0.25, 0.9]]; let signal = signal::from_iter(frames.iter().cloned()); let offset: Vec<_> = signal.offset_amp_per_channel([0.25, -0.5]).take(2).collect(); assert_eq!(offset, vec![[0.75, -0.2], [0.0, 0.4]]); }
fn scale_amp_per_channel<F>(self, amp_frame: F) -> ScaleAmpPerChannel<Self, F> where
Self: Sized,
F: Frame<Sample = <<Self::Frame as Frame>::Sample as Sample>::Float, NumChannels = <Self::Frame as Frame>::NumChannels>,
Self: Sized,
F: Frame<Sample = <<Self::Frame as Frame>::Sample as Sample>::Float, NumChannels = <Self::Frame as Frame>::NumChannels>,
Produces a new Signal
that scales the amplitude of every Frame
in self
by the
respective amplitudes in each channel of the given amp_frame
.
Example
extern crate sample; use sample::{signal, Signal}; fn main() { let frames = [[0.2, -0.5], [-0.4, 0.3]]; let signal = signal::from_iter(frames.iter().cloned()); let scaled: Vec<_> = signal.scale_amp_per_channel([0.5, 2.0]).take(2).collect(); assert_eq!(scaled, vec![[0.1, -1.0], [-0.2, 0.6]]); }
fn mul_hz<M, I>(self, interpolator: I, mul_per_frame: M) -> MulHz<Self, M, I> where
Self: Sized,
M: Signal<Frame = [f64; 1]>,
I: Interpolator,
Self: Sized,
M: Signal<Frame = [f64; 1]>,
I: Interpolator,
Multiplies the rate at which frames of self
are yielded by the given signal
.
This happens by wrapping self
in a rate::Converter
and calling set_playback_hz_scale
with each value yielded by signal
Example
extern crate sample; use sample::{signal, Signal}; use sample::interpolate::Linear; fn main() { let foo = [[0.0], [1.0], [0.0], [-1.0]]; let mul = [[1.0], [1.0], [0.5], [0.5], [0.5], [0.5]]; let mut source = signal::from_iter(foo.iter().cloned()); let interp = Linear::from_source(&mut source); let hz_signal = signal::from_iter(mul.iter().cloned()); let frames: Vec<_> = source.mul_hz(interp, hz_signal).take(6).collect(); assert_eq!(&frames[..], &[[0.0], [1.0], [0.0], [-0.5], [-1.0], [-0.5]][..]); }
fn from_hz_to_hz<I>(
self,
interpolator: I,
source_hz: f64,
target_hz: f64
) -> Converter<Self, I> where
Self: Sized,
I: Interpolator,
self,
interpolator: I,
source_hz: f64,
target_hz: f64
) -> Converter<Self, I> where
Self: Sized,
I: Interpolator,
Converts the rate at which frames of the Signal
are yielded using interpolation.
Example
extern crate sample; use sample::{signal, Signal}; use sample::interpolate::Linear; fn main() { let foo = [[0.0], [1.0], [0.0], [-1.0]]; let mut source = signal::from_iter(foo.iter().cloned()); let interp = Linear::from_source(&mut source); let frames: Vec<_> = source.from_hz_to_hz(interp, 1.0, 2.0).take(8).collect(); assert_eq!(&frames[..], &[[0.0], [0.5], [1.0], [0.5], [0.0], [-0.5], [-1.0], [-0.5]][..]); }
fn scale_hz<I>(self, interpolator: I, multi: f64) -> Converter<Self, I> where
Self: Sized,
I: Interpolator,
Self: Sized,
I: Interpolator,
Multiplies the rate at which frames of the Signal
are yielded by the given value.
Example
extern crate sample; use sample::{signal, Signal}; use sample::interpolate::Linear; fn main() { let foo = [[0.0], [1.0], [0.0], [-1.0]]; let mut source = signal::from_iter(foo.iter().cloned()); let interp = Linear::from_source(&mut source); let frames: Vec<_> = source.scale_hz(interp, 0.5).take(8).collect(); assert_eq!(&frames[..], &[[0.0], [0.5], [1.0], [0.5], [0.0], [-0.5], [-1.0], [-0.5]][..]); }
fn delay(self, n_frames: usize) -> Delay<Self> where
Self: Sized,
Self: Sized,
Delays the Signal
by the given number of frames.
The delay is performed by yielding Frame::equilibrium()
n_frames
times before
continuing to yield frames from signal
.
Example
extern crate sample; use sample::{signal, Signal}; fn main() { let frames = [[0.2], [0.4]]; let signal = signal::from_iter(frames.iter().cloned()); let delayed: Vec<_> = signal.delay(2).take(4).collect(); assert_eq!(delayed, vec![[0.0], [0.0], [0.2], [0.4]]); }
fn into_interleaved_samples(self) -> IntoInterleavedSamples<Self> where
Self: Sized,
Self: Sized,
Converts a Signal
into a type that yields the interleaved Sample
s.
Example
extern crate sample; use sample::{signal, Signal}; fn main() { let frames = [[0.1, 0.2], [0.3, 0.4]]; let signal = signal::from_iter(frames.iter().cloned()); let samples = signal.into_interleaved_samples(); let samples: Vec<_> = samples.into_iter().take(4).collect(); assert_eq!(samples, vec![0.1, 0.2, 0.3, 0.4]); }
fn clip_amp(
self,
thresh: <<Self::Frame as Frame>::Sample as Sample>::Signed
) -> ClipAmp<Self> where
Self: Sized,
self,
thresh: <<Self::Frame as Frame>::Sample as Sample>::Signed
) -> ClipAmp<Self> where
Self: Sized,
Clips the amplitude of each channel in each Frame
yielded by self
to the given
threshold amplitude.
Example
extern crate sample; use sample::{signal, Signal}; fn main() { let frames = [[1.2, 0.8], [-0.7, -1.4]]; let signal = signal::from_iter(frames.iter().cloned()); let clipped: Vec<_> = signal.clip_amp(0.9).take(2).collect(); assert_eq!(clipped, vec![[0.9, 0.8], [-0.7, -0.9]]); }
fn inspect<F>(self, inspect: F) -> Inspect<Self, F> where
Self: Sized,
F: FnMut(&Self::Frame),
Self: Sized,
F: FnMut(&Self::Frame),
Create a new Signal
that calls the enclosing function on each iteration.
Example
extern crate sample; use sample::{signal, Signal}; fn main() { let mut f = [0.0]; let mut signal = signal::gen_mut(move || { f[0] += 0.1; f }); let func = |x: &[f64; 1]| { assert_eq!(*x, [0.1]); }; let mut inspected = signal.inspect(func); let out = inspected.next(); assert_eq!(out, [0.1]); }
fn bus(self) -> Bus<Self> where
Self: Sized,
Self: Sized,
Moves the Signal
into a Bus
from which its output may be divided into multiple other
Signal
s in the form of Output
s.
This method allows to create more complex directed acyclic graph structures that incorporate concepts like sends, side-chaining, etc, rather than being restricted to tree structures where signals can only ever be joined but never divided.
Note: When using multiple Output
s in this fashion, you will need to be sure to pull the
frames from each Output
in sync (whether per frame or per buffer). This is because when
output A requests Frame
s before output B, those frames must remain available for output
B and in turn must be stored in an intermediary ring buffer.
Example
extern crate sample; use sample::{signal, Signal}; fn main() { let frames = [[0.1], [0.2], [0.3], [0.4], [0.5], [0.6]]; let signal = signal::from_iter(frames.iter().cloned()); let bus = signal.bus(); let mut a = bus.send(); let mut b = bus.send(); assert_eq!(a.by_ref().take(3).collect::<Vec<_>>(), vec![[0.1], [0.2], [0.3]]); assert_eq!(b.by_ref().take(3).collect::<Vec<_>>(), vec![[0.1], [0.2], [0.3]]); let c = bus.send(); assert_eq!(c.take(3).collect::<Vec<_>>(), vec![[0.4], [0.5], [0.6]]); assert_eq!(b.take(3).collect::<Vec<_>>(), vec![[0.4], [0.5], [0.6]]); assert_eq!(a.take(3).collect::<Vec<_>>(), vec![[0.4], [0.5], [0.6]]); }
fn take(self, n: usize) -> Take<Self> where
Self: Sized,
Self: Sized,
Converts the Signal
into an Iterator
that will yield the given number for Frame
s
before returning None
.
Example
extern crate sample; use sample::{signal, Signal}; fn main() { let frames = [[0.1], [0.2], [0.3], [0.4]]; let mut signal = signal::from_iter(frames.iter().cloned()).take(2); assert_eq!(signal.next(), Some([0.1])); assert_eq!(signal.next(), Some([0.2])); assert_eq!(signal.next(), None); }
fn by_ref(&mut self) -> &mut Self where
Self: Sized,
Self: Sized,
Borrows a Signal rather than consuming it.
This is useful to allow applying signal adaptors while still retaining ownership of the original signal.
Example
extern crate sample; use sample::{signal, Signal}; fn main() { let frames = [[0], [1], [2], [3], [4]]; let mut signal = signal::from_iter(frames.iter().cloned()); assert_eq!(signal.next(), [0]); assert_eq!(signal.by_ref().take(2).collect::<Vec<_>>(), vec![[1], [2]]); assert_eq!(signal.next(), [3]); assert_eq!(signal.next(), [4]); }
Implementors
impl<'a, S> Signal for &'a mut S where
S: Signal, type Frame = S::Frame;impl<F> Signal for Box<Signal<Frame = F>> where
F: Frame, type Frame = F;impl<I> Signal for FromIterator<I> where
I: Iterator,
I::Item: Frame, type Frame = I::Item;impl<I, F> Signal for FromInterleavedSamplesIterator<I, F> where
I: Iterator,
I::Item: Sample,
F: Frame<Sample = I::Item>, type Frame = F;impl<F> Signal for Equilibrium<F> where
F: Frame, type Frame = F;impl<G, F> Signal for Gen<G, F> where
G: Fn() -> F,
F: Frame, type Frame = F;impl<G, F> Signal for GenMut<G, F> where
G: FnMut() -> F,
F: Frame, type Frame = F;impl<S, M, F> Signal for Map<S, M, F> where
S: Signal,
M: FnMut(S::Frame) -> F,
F: Frame, type Frame = F;impl<S, O, M, F> Signal for ZipMap<S, O, M, F> where
S: Signal,
O: Signal,
M: FnMut(S::Frame, O::Frame) -> F,
F: Frame, type Frame = F;impl<S> Signal for Hz<S> where
S: Signal<Frame = [f64; 1]>, type Frame = [f64; 1];impl Signal for ConstHz type Frame = [f64; 1];
impl<S> Signal for Phase<S> where
S: Step, type Frame = [f64; 1];impl<S> Signal for Sine<S> where
S: Step, type Frame = [f64; 1];impl<S> Signal for Saw<S> where
S: Step, type Frame = [f64; 1];impl<S> Signal for Square<S> where
S: Step, type Frame = [f64; 1];impl Signal for Noise type Frame = [f64; 1];
impl<S> Signal for NoiseSimplex<S> where
S: Step, type Frame = [f64; 1];impl<A, B> Signal for AddAmp<A, B> where
A: Signal,
B: Signal,
B::Frame: Frame<Sample = <<A::Frame as Frame>::Sample as Sample>::Signed, NumChannels = <A::Frame as Frame>::NumChannels>, type Frame = A::Frame;impl<A, B> Signal for MulAmp<A, B> where
A: Signal,
B: Signal,
B::Frame: Frame<Sample = <<A::Frame as Frame>::Sample as Sample>::Float, NumChannels = <A::Frame as Frame>::NumChannels>, type Frame = A::Frame;impl<S> Signal for ScaleAmp<S> where
S: Signal, type Frame = S::Frame;impl<S, F> Signal for ScaleAmpPerChannel<S, F> where
S: Signal,
F: Frame<Sample = <<S::Frame as Frame>::Sample as Sample>::Float, NumChannels = <S::Frame as Frame>::NumChannels>, type Frame = S::Frame;impl<S> Signal for OffsetAmp<S> where
S: Signal, type Frame = S::Frame;impl<S, F> Signal for OffsetAmpPerChannel<S, F> where
S: Signal,
F: Frame<Sample = <<S::Frame as Frame>::Sample as Sample>::Signed, NumChannels = <S::Frame as Frame>::NumChannels>, type Frame = S::Frame;impl<S, M, I> Signal for MulHz<S, M, I> where
S: Signal,
<S::Frame as Frame>::Sample: Duplex<f64>,
M: Signal<Frame = [f64; 1]>,
I: Interpolator<Frame = S::Frame>, type Frame = S::Frame;impl<S> Signal for Delay<S> where
S: Signal, type Frame = S::Frame;impl<S, F> Signal for Inspect<S, F> where
S: Signal,
F: FnMut(&S::Frame), type Frame = S::Frame;impl<S> Signal for ClipAmp<S> where
S: Signal, type Frame = S::Frame;impl<S> Signal for Output<S> where
S: Signal, type Frame = S::Frame;impl<S, I> Signal for Converter<S, I> where
S: Signal,
I: Interpolator<Frame = S::Frame>, type Frame = S::Frame;