Trait sampara::sample::Sample[][src]

pub trait Sample: Copy + Clone + PartialOrd + PartialEq {
    type Signed: SignedSample + Duplex<Self>;
    type Float: FloatSample + Duplex<Self>;

    const EQUILIBRIUM: Self;
    fn into_sample<S>(self) -> S
    where
        Self: ConvertInto<S>,
        S: Sample
, { ... }
fn from_sample<S>(s: S) -> Self
    where
        Self: ConvertFrom<S>,
        S: Sample
, { ... }
fn into_signed_sample(self) -> Self::Signed { ... }
fn into_float_sample(self) -> Self::Float { ... }
fn add_amp(self, amp: Self::Signed) -> Self { ... }
fn mul_amp(self, amp: Self::Float) -> Self { ... } }

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 Samples 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]

When multiplying two Samples together, it is necessary to convert both temporarily into some mutual float format. This associated type represents the Sample type to convert to for optimal/lossless multiplication.

Loading content...

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);
}
Loading content...

Provided methods

fn into_sample<S>(self) -> S where
    Self: ConvertInto<S>,
    S: Sample
[src]

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]

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);
}
Loading content...

Implementations on Foreign Types

impl Sample for i8[src]

type Signed = i8

type Float = f32

impl Sample for i16[src]

type Signed = i16

type Float = f32

impl Sample for i32[src]

type Signed = i32

type Float = f32

impl Sample for i64[src]

type Signed = i64

type Float = f64

impl Sample for u8[src]

type Signed = i8

type Float = f32

impl Sample for u16[src]

type Signed = i16

type Float = f32

impl Sample for u32[src]

type Signed = i32

type Float = f32

impl Sample for u64[src]

type Signed = i64

type Float = f64

impl Sample for f32[src]

type Signed = f32

type Float = f32

impl Sample for f64[src]

type Signed = f64

type Float = f64

Loading content...

Implementors

Loading content...