Trait sampara::sample::Sample [−][src]
A trait for working generically across different sample format types, both in terms of representation (integral versus floating-point) and bitsize.
Associated Types
type Signed: SignedSample + Duplex<Self>
[src]
When adding two Sample
s together, it is necessary to convert
both temporarily into some mutual signed format. This associated type
represents the Sample
type to convert to for optimal/lossless
addition.
type Float: FloatSample + Duplex<Self>
[src]
Associated Constants
const EQUILIBRIUM: Self
[src]
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, i.e. the “zero amplitude” value.
use sampara::Sample; fn main() { assert_eq!(0.0, f32::EQUILIBRIUM); assert_eq!(0, i32::EQUILIBRIUM); assert_eq!(128, u8::EQUILIBRIUM); assert_eq!(32_768_u16, Sample::EQUILIBRIUM); }
Provided methods
fn into_sample<S>(self) -> S where
Self: ConvertInto<S>,
S: Sample,
[src]
Self: ConvertInto<S>,
S: Sample,
Converts this Sample
into another Sample
type.
use sampara::Sample; fn main() { assert_eq!(0.0.into_sample::<i32>(), 0); assert_eq!(0.0.into_sample::<u8>(), 128); assert_eq!((-1.0).into_sample::<u8>(), 0); }
fn from_sample<S>(s: S) -> Self where
Self: ConvertFrom<S>,
S: Sample,
[src]
Self: ConvertFrom<S>,
S: Sample,
Creates an instance of this Sample
from another Sample
type.
use sampara::Sample; fn main() { assert_eq!(f32::from_sample(128u8), 0.0); assert_eq!(i8::from_sample(-1.0), -128); assert_eq!(u16::from_sample(0.5), 49152); }
fn into_signed_sample(self) -> Self::Signed
[src]
Converts this Sample
into its corresponding Self::Signed
type.
This is a simple wrapper around Sample::into_sample
to provide
extra type inference convenience in some cases.
use sampara::Sample; fn main() { assert_eq!(128_u8.into_signed_sample(), 0_i8); assert_eq!(128_u16.into_signed_sample(), -32640_i16); assert_eq!((-128_i8).into_signed_sample(), -128_i8); }
fn into_float_sample(self) -> Self::Float
[src]
Converts this Sample
into its corresponding Self::Float
type.
This is a simple wrapper around Sample::into_sample
to provide
extra type inference convenience in some cases.
use sampara::Sample; fn main() { assert_eq!(128_u8.into_float_sample(), 0.0_f32); assert_eq!(128_u16.into_float_sample(), -0.99609375_f32); assert_eq!((-128_i8).into_float_sample(), -1.0_f32); }
fn add_amp(self, amp: Self::Signed) -> Self
[src]
Adds/offsets the amplitude of this Sample
by a signed amplitude.
This value will be converted into Self::Signed
, then added. The
result will then be converted back into Self
. This double conversion
is to correctly handle the addition of unsigned signal formats.
use sampara::Sample; fn main() { assert_eq!(0.25.add_amp(0.5), 0.75); assert_eq!(192u8.add_amp(-128), 64); }
fn mul_amp(self, amp: Self::Float) -> Self
[src]
Multiplies/scales the amplitude of this Sample
by a float amplitude.
This value will be converted into Self::Float
, then multiplied. The
result will then be converted back into Self
. This double conversion
is to correctly handle the multiplication of integer signal formats.
use sampara::Sample; fn main() { assert_eq!(64_i8.mul_amp(0.5), 32); assert_eq!(0.5.mul_amp(-2.0), -1.0); assert_eq!(64_u8.mul_amp(0.0), 128); }