pub trait Frame:
Copy
+ Clone
+ PartialEq {
type Sample: Sample;
type NumChannels: NumChannels;
type Channels: Iterator<Item = Self::Sample>;
type Signed: Frame<Sample = <Self::Sample as Sample>::Signed, NumChannels = Self::NumChannels>;
type Float: Frame<Sample = <Self::Sample as Sample>::Float, NumChannels = Self::NumChannels>;
const EQUILIBRIUM: Self;
const CHANNELS: usize;
Show 13 methods
// Required methods
fn from_fn<F>(from: F) -> Self
where F: FnMut(usize) -> Self::Sample;
fn from_samples<I>(samples: &mut I) -> Option<Self>
where I: Iterator<Item = Self::Sample>;
fn channels(self) -> Self::Channels;
fn channel(&self, idx: usize) -> Option<&Self::Sample>;
unsafe fn channel_unchecked(&self, idx: usize) -> &Self::Sample;
fn map<F, M>(self, map: M) -> F
where F: Frame<NumChannels = Self::NumChannels>,
M: FnMut(Self::Sample) -> F::Sample;
fn zip_map<O, F, M>(self, other: O, zip_map: M) -> F
where O: Frame<NumChannels = Self::NumChannels>,
F: Frame<NumChannels = Self::NumChannels>,
M: FnMut(Self::Sample, O::Sample) -> F::Sample;
fn to_signed_frame(self) -> Self::Signed;
fn to_float_frame(self) -> Self::Float;
// Provided methods
fn offset_amp(self, offset: <Self::Sample as Sample>::Signed) -> Self { ... }
fn scale_amp(self, amp: <Self::Sample as Sample>::Float) -> Self { ... }
fn add_amp<F>(self, other: F) -> Self
where F: Frame<Sample = <Self::Sample as Sample>::Signed, NumChannels = Self::NumChannels> { ... }
fn mul_amp<F>(self, other: F) -> Self
where F: Frame<Sample = <Self::Sample as Sample>::Float, NumChannels = Self::NumChannels> { ... }
}
Expand description
Represents one sample from each channel at a single discrete instance in time within a PCM signal.
Implementations are provided for:
- All fixed-size arrays up to a length of 32 elements.
- All primitive types that implement
Sample
. These implementations assumeCHANNELS = 1
.
Required Associated Constants§
Sourceconst EQUILIBRIUM: Self
const EQUILIBRIUM: Self
The equilibrium value for the wave that this Sample
type represents. This is normally the
value that is equal distance from both the min and max ranges of the sample.
§Examples
use dasp_frame::{Frame, Mono, Stereo};
fn main() {
assert_eq!(Mono::<f32>::EQUILIBRIUM, [0.0]);
assert_eq!(Stereo::<f32>::EQUILIBRIUM, [0.0, 0.0]);
assert_eq!(<[f32; 3]>::EQUILIBRIUM, [0.0, 0.0, 0.0]);
assert_eq!(<[u8; 2]>::EQUILIBRIUM, [128u8, 128]);
}
Required Associated Types§
Sourcetype NumChannels: NumChannels
type NumChannels: NumChannels
A typified version of a number of channels in the Frame
, used for safely mapping frames
of the same length to other Frame
s, perhaps with a different Sample
associated type.
Sourcetype Channels: Iterator<Item = Self::Sample>
type Channels: Iterator<Item = Self::Sample>
An iterator yielding the sample in each channel, starting from left (channel 0) and ending at the right (channel NumChannels-1).
Required Methods§
Sourcefn from_fn<F>(from: F) -> Self
fn from_fn<F>(from: F) -> Self
Create a new Frame
where the Sample
for each channel is produced by the given function.
The given function should map each channel index to its respective sample.
Sourcefn from_samples<I>(samples: &mut I) -> Option<Self>
fn from_samples<I>(samples: &mut I) -> Option<Self>
Create a new Frame
from a borrowed Iterator
yielding samples for each channel.
Returns None
if the given Iterator
does not yield enough Sample
s.
This is necessary for the signal::FromSamples
Iterator
, that converts some Iterator
yielding Sample
s to an Iterator
yielding Frame
s.
Sourcefn channels(self) -> Self::Channels
fn channels(self) -> Self::Channels
Converts the frame into an iterator yielding the sample for each channel in the frame.
Sourcefn channel(&self, idx: usize) -> Option<&Self::Sample>
fn channel(&self, idx: usize) -> Option<&Self::Sample>
Yields a reference to the Sample
of the channel at the given index if there is one.
Sourceunsafe fn channel_unchecked(&self, idx: usize) -> &Self::Sample
unsafe fn channel_unchecked(&self, idx: usize) -> &Self::Sample
Returns a pointer to the sample of the channel at the given index, without doing bounds checking.
Note: This is primarily a necessity for efficient Frame::map
and Frame::zip_map
methods, as for those methods we can guarantee lengths of different Frame
s to be the same
at compile-time.
Sourcefn map<F, M>(self, map: M) -> F
fn map<F, M>(self, map: M) -> F
Applies the given function to each sample in the Frame
in channel order and returns the
result as a new Frame
.
§Example
use dasp_frame::Frame;
use dasp_sample::Sample;
fn main() {
let foo = [0i16, 0];
let bar: [u8; 2] = foo.map(Sample::to_sample);
assert_eq!(bar, [128u8, 128]);
}
Sourcefn zip_map<O, F, M>(self, other: O, zip_map: M) -> Fwhere
O: Frame<NumChannels = Self::NumChannels>,
F: Frame<NumChannels = Self::NumChannels>,
M: FnMut(Self::Sample, O::Sample) -> F::Sample,
fn zip_map<O, F, M>(self, other: O, zip_map: M) -> Fwhere
O: Frame<NumChannels = Self::NumChannels>,
F: Frame<NumChannels = Self::NumChannels>,
M: FnMut(Self::Sample, O::Sample) -> F::Sample,
Calls the given function with the pair of elements at every index and returns the resulting Frame.
On a Vec
this would be akin to .into_iter().zip(other).map(|(a, b)| ...).collect()
, though
much quicker and tailored to fixed-size arrays of samples.
Sourcefn to_signed_frame(self) -> Self::Signed
fn to_signed_frame(self) -> Self::Signed
Converts the frame type to the equivalent signal in its associated Float
ing point format.
§Example
use dasp_frame::Frame;
fn main() {
let foo = [128u8; 2];
let signed = foo.to_signed_frame();
assert_eq!(signed, [0i8; 2]);
}
Sourcefn to_float_frame(self) -> Self::Float
fn to_float_frame(self) -> Self::Float
Converts the frame type to the equivalent signal in its associated Signed
format.
§Example
use dasp_frame::Frame;
fn main() {
let foo = [128u8; 2];
let float = foo.to_float_frame();
assert_eq!(float, [0.0; 2]);
}
Provided Methods§
Sourcefn offset_amp(self, offset: <Self::Sample as Sample>::Signed) -> Self
fn offset_amp(self, offset: <Self::Sample as Sample>::Signed) -> Self
Offsets the amplitude of every channel in the frame by the given offset
and yields the
resulting frame.
§Example
use dasp_frame::Frame;
fn main() {
assert_eq!([0.25, -0.5].offset_amp(0.5), [0.75, 0.0]);
assert_eq!([0.5, -0.25].offset_amp(-0.25), [0.25, -0.5]);
assert_eq!([128u8, 192].offset_amp(-64), [64, 128]);
}
Sourcefn scale_amp(self, amp: <Self::Sample as Sample>::Float) -> Self
fn scale_amp(self, amp: <Self::Sample as Sample>::Float) -> Self
Multiplies each Sample
in the Frame
by the given amplitude and returns the resulting
Frame
.
- A > 1.0 amplifies the sample.
- A < 1.0 attenuates the sample.
- A == 1.0 yields the same sample.
- A == 0.0 yields the
Sample::equilibrium
.
§Example
use dasp_frame::Frame;
fn main() {
assert_eq!([0.1, 0.2, -0.1, -0.2].scale_amp(2.0), [0.2, 0.4, -0.2, -0.4]);
}
Sourcefn add_amp<F>(self, other: F) -> Self
fn add_amp<F>(self, other: F) -> Self
Sums each channel in other
with each channel in self
and returns the resulting Frame
.
§Example
use dasp_frame::Frame;
fn main() {
let foo = [0.25, 0.5].add_amp([-0.75, 0.25]);
assert_eq!(foo, [-0.5, 0.75]);
}
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.