Signal

Trait Signal 

Source
pub trait Signal<const N: usize> {
    type Frame: Frame<N>;

Show 15 methods // Required method fn next(&mut self) -> Option<Self::Frame>; // Provided methods fn sig_next(&mut self) -> Self::Frame { ... } fn by_ref(&mut self) -> &mut Self where Self: Sized { ... } fn map<F, M, const NF: usize>(self, func: M) -> Map<Self, F, M, N, NF> where Self: Sized, F: Frame<NF>, M: FnMut(Self::Frame) -> F { ... } fn zip_map<O, F, M, const NO: usize, const NF: usize>( self, other: O, func: M, ) -> ZipMap<Self, O, F, M, N, NO, NF> where Self: Sized, O: Signal<NO>, F: Frame<NF>, M: FnMut(Self::Frame, O::Frame) -> F { ... } fn add_signal<B>(self, other: B) -> AddSignal<Self, B, N> where Self: Sized, B: Signal<N>, Self::Frame: Frame<N, Signed = <B::Frame as Frame<N>>::Signed> { ... } fn mul_signal<B>(self, other: B) -> MulSignal<Self, B, N> where Self: Sized, B: Signal<N>, Self::Frame: Frame<N, Float = <B::Frame as Frame<N>>::Float> { ... } fn add_frame<F>(self, frame: F) -> AddFrame<Self, F, N> where Self: Sized, Self::Frame: Frame<N, Signed = F>, F: Frame<N> { ... } fn mul_frame<F>(self, frame: F) -> MulFrame<Self, F, N> where Self: Sized, Self::Frame: Frame<N, Float = F>, F: Frame<N> { ... } fn add_amp<X>(self, amp: X) -> AddAmp<Self, X, N> where Self: Sized, Self::Frame: Frame<N>, <Self::Frame as Frame<N>>::Sample: Sample<Signed = X>, X: Sample { ... } fn mul_amp<X>(self, amp: X) -> MulAmp<Self, X, N> where Self: Sized, Self::Frame: Frame<N>, <Self::Frame as Frame<N>>::Sample: Sample<Float = X>, X: Sample { ... } fn delay(self, n_frames: usize) -> Delay<Self, N> where Self: Sized { ... } fn inspect<F>(self, func: F) -> Inspect<Self, F, N> where Self: Sized, F: FnMut(&Self::Frame) { ... } fn take(self, n: usize) -> Take<Self, N> where Self: Sized { ... } fn into_iter(self) -> IntoIter<Self, N> where Self: Sized { ... }
}
Expand description

Types that yield a sequence of Frames, representing an audio signal.

This trait is inspired by the Iterator trait and has similar methods and adaptors, but with a DSP-related focus.

Required Associated Types§

Source

type Frame: Frame<N>

The Frame type returned by this Signal.

Required Methods§

Source

fn next(&mut self) -> Option<Self::Frame>

Advances Self and returns the next Frame, or None if there are no more to yield.

Provided Methods§

Source

fn sig_next(&mut self) -> Self::Frame

Similar to [next], but will always yield a Frame. Yields Frame::EQUILIBRIUM if there are no more actual Frames to yield.

Source

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Borrows this Signal rather than consuming it.

This is useful for applying adaptors while still retaining ownership of the original Signal.

use sampara::{signal, Signal};

fn main() {
    let mut signal = signal::from_frames(vec![0, 1, 2, 3]);
    assert_eq!(signal.next(), Some(0));
    assert_eq!(signal.by_ref().add_amp(10).next(), Some(11));
    assert_eq!(signal.by_ref().mul_amp(2.5_f32).next(), Some(5));
    assert_eq!(signal.next(), Some(3));
}
Source

fn map<F, M, const NF: usize>(self, func: M) -> Map<Self, F, M, N, NF>
where Self: Sized, F: Frame<NF>, M: FnMut(Self::Frame) -> F,

Creates a new Signal that applies a function to each Frame of Self.

Source

fn zip_map<O, F, M, const NO: usize, const NF: usize>( self, other: O, func: M, ) -> ZipMap<Self, O, F, M, N, NO, NF>
where Self: Sized, O: Signal<NO>, F: Frame<NF>, M: FnMut(Self::Frame, O::Frame) -> F,

Creates a new Signal that applies a function to each pair of Frames in Self and another Signal.

Source

fn add_signal<B>(self, other: B) -> AddSignal<Self, B, N>
where Self: Sized, B: Signal<N>, Self::Frame: Frame<N, Signed = <B::Frame as Frame<N>>::Signed>,

Creates a new Signal that yields the sum of pairs of Frames yielded by Self and another Signal in lockstep.

Source

fn mul_signal<B>(self, other: B) -> MulSignal<Self, B, N>
where Self: Sized, B: Signal<N>, Self::Frame: Frame<N, Float = <B::Frame as Frame<N>>::Float>,

Creates a new Signal that yields the product of pairs of Frames yielded by Self and another Signal in lockstep.

Source

fn add_frame<F>(self, frame: F) -> AddFrame<Self, F, N>
where Self: Sized, Self::Frame: Frame<N, Signed = F>, F: Frame<N>,

Creates a new Signal that yields each Frame of a Signal summed with a constant Frame.

Source

fn mul_frame<F>(self, frame: F) -> MulFrame<Self, F, N>
where Self: Sized, Self::Frame: Frame<N, Float = F>, F: Frame<N>,

Creates a new Signal that yields each Frame of a Signal multiplied with a constant Frame.

Source

fn add_amp<X>(self, amp: X) -> AddAmp<Self, X, N>
where Self: Sized, Self::Frame: Frame<N>, <Self::Frame as Frame<N>>::Sample: Sample<Signed = X>, X: Sample,

Creates a new Signal that yields each Frame of a Signal with each channel summed with a constant Sample.

Source

fn mul_amp<X>(self, amp: X) -> MulAmp<Self, X, N>
where Self: Sized, Self::Frame: Frame<N>, <Self::Frame as Frame<N>>::Sample: Sample<Float = X>, X: Sample,

Creates a new Signal that yields each Frame of a Signal with each channel multiplied with a constant Sample.

Source

fn delay(self, n_frames: usize) -> Delay<Self, N>
where Self: Sized,

Delays Self by a given number of frames. The delay is performed by yielding Frame::EQUILIBRIUM that number of times before continuing to yield frames from Self.

Source

fn inspect<F>(self, func: F) -> Inspect<Self, F, N>
where Self: Sized, F: FnMut(&Self::Frame),

Calls an inspection function on each Frame yielded by this Signal, and then passes the Frame through.

use sampara::{signal, Signal};

fn main() {
    let mut max: Option<i32> = None;
    let mut signal = signal::from_frames(vec![2i32, 3, 1])
        .inspect(|&f| {
            if let Some(m) = max {
                max.replace(m.max(f));
            } else {
                max = Some(f);
            }
        });

    assert_eq!(signal.next(), Some(2));
    assert_eq!(signal.next(), Some(3));
    assert_eq!(signal.next(), Some(1));
    assert_eq!(signal.next(), None);
    assert_eq!(max, Some(3));
}
Source

fn take(self, n: usize) -> Take<Self, N>
where Self: Sized,

Returns a new Signal that yields only the first N Frames of Self.

use sampara::{signal, Signal};

fn main() {
    let mut signal = signal::from_frames(0u8..=99)
        .take(3);

    assert_eq!(signal.next(), Some(0));
    assert_eq!(signal.next(), Some(1));
    assert_eq!(signal.next(), Some(2));
    assert_eq!(signal.next(), None);
}
Source

fn into_iter(self) -> IntoIter<Self, N>
where Self: Sized,

Converts this Signal into an Iterator yielding Frames.

use sampara::{signal, Signal};

fn main() {
    let signal = signal::from_frames(vec![2i32, 3, 1]).add_amp(5);
    let iter = signal.into_iter();

    assert_eq!(iter.collect::<Vec<_>>(), vec![7, 8, 6]);
}

Implementations on Foreign Types§

Source§

impl<S, const N: usize> Signal<N> for &mut S
where S: Signal<N>,

Source§

type Frame = <S as Signal<N>>::Frame

Source§

fn next(&mut self) -> Option<Self::Frame>

Implementors§

Source§

impl<A, B, const N: usize> Signal<N> for AddSignal<A, B, N>
where A: Signal<N>, B: Signal<N>, A::Frame: Frame<N, Signed = <B::Frame as Frame<N>>::Signed>,

Source§

type Frame = <A as Signal<N>>::Frame

Source§

impl<A, B, const N: usize> Signal<N> for MulSignal<A, B, N>
where A: Signal<N>, B: Signal<N>, A::Frame: Frame<N, Float = <B::Frame as Frame<N>>::Float>,

Source§

type Frame = <A as Signal<N>>::Frame

Source§

impl<D, const N: usize> Signal<N> for Phase<D, N>
where D: Delta<N>,

Source§

type Frame = <D as Delta<N>>::Delta

Source§

impl<D, const N: usize> Signal<N> for Saw<D, N>
where D: Delta<N>,

Source§

type Frame = <D as Delta<N>>::Delta

Source§

impl<D, const N: usize> Signal<N> for Sine<D, N>
where D: Delta<N>,

Source§

type Frame = <D as Delta<N>>::Delta

Source§

impl<D, const N: usize> Signal<N> for Square<D, N>
where D: Delta<N>,

Source§

type Frame = <D as Delta<N>>::Delta

Source§

impl<F, G, const N: usize> Signal<N> for FromFn<F, G, N>
where F: Frame<N>, G: FnMut() -> Option<F>,

Source§

type Frame = F

Source§

impl<F, I, const N: usize> Signal<N> for FromSamples<F, I, N>
where F: Frame<N, Sample = I::Item>, I: Iterator, I::Item: Sample,

Source§

type Frame = F

Source§

impl<F, const N: usize> Signal<N> for Constant<F, N>
where F: Frame<N>,

Source§

type Frame = F

Source§

impl<F, const N: usize> Signal<N> for Empty<F, N>
where F: Frame<N>,

Source§

type Frame = F

Source§

impl<F, const N: usize> Signal<N> for Equilibrium<F, N>
where F: Frame<N>,

Source§

type Frame = F

Source§

impl<I, const N: usize> Signal<N> for FromFrames<I, N>
where I: Iterator, I::Item: Frame<N>,

Source§

type Frame = <I as Iterator>::Item

Source§

impl<S, F, M, const N: usize, const NF: usize> Signal<NF> for Map<S, F, M, N, NF>
where S: Signal<N>, F: Frame<NF>, M: FnMut(S::Frame) -> F,

Source§

type Frame = F

Source§

impl<S, F, const N: usize> Signal<N> for AddFrame<S, F, N>
where S: Signal<N>, S::Frame: Frame<N, Signed = F>, F: Frame<N>,

Source§

type Frame = <S as Signal<N>>::Frame

Source§

impl<S, F, const N: usize> Signal<N> for Inspect<S, F, N>
where S: Signal<N>, F: FnMut(&S::Frame),

Source§

type Frame = <S as Signal<N>>::Frame

Source§

impl<S, F, const N: usize> Signal<N> for MulFrame<S, F, N>
where S: Signal<N>, S::Frame: Frame<N, Float = F>, F: Frame<N>,

Source§

type Frame = <S as Signal<N>>::Frame

Source§

impl<S, O, M, F, const N: usize, const NO: usize, const NF: usize> Signal<NF> for ZipMap<S, O, F, M, N, NO, NF>
where S: Signal<N>, O: Signal<NO>, M: FnMut(S::Frame, O::Frame) -> F, F: Frame<NF>,

Source§

type Frame = F

Source§

impl<S, X, const N: usize> Signal<N> for AddAmp<S, X, N>
where S: Signal<N>, S::Frame: Frame<N>, <S::Frame as Frame<N>>::Sample: Sample<Signed = X>, X: Sample,

Source§

type Frame = <S as Signal<N>>::Frame

Source§

impl<S, X, const N: usize> Signal<N> for MulAmp<S, X, N>
where S: Signal<N>, S::Frame: Frame<N>, <S::Frame as Frame<N>>::Sample: Sample<Float = X>, X: Sample,

Source§

type Frame = <S as Signal<N>>::Frame

Source§

impl<S, const N: usize> Signal<N> for Delay<S, N>
where S: Signal<N>,

Source§

type Frame = <S as Signal<N>>::Frame

Source§

impl<S, const N: usize> Signal<N> for Take<S, N>
where S: Signal<N>,

Source§

type Frame = <S as Signal<N>>::Frame