Sample

Trait Sample 

Source
pub trait Sample:
    Copy
    + Default
    + Send
    + Sync
    + 'static
    + Add<Output = Self>
    + Sub<Output = Self>
    + Mul<Output = Self>
    + Div<Output = Self>
    + PartialOrd {
    const ZERO: Self;
    const ONE: Self;

    // Required methods
    fn from_f32(value: f32) -> Self;
    fn to_f32(self) -> f32;
    fn from_f64(value: f64) -> Self;
    fn to_f64(self) -> f64;
    fn abs(self) -> Self;
    fn sqrt(self) -> Self;
    fn sin(self) -> Self;
    fn cos(self) -> Self;
    fn min(self, other: Self) -> Self;
    fn max(self, other: Self) -> Self;

    // Provided method
    fn clamp(self, min: Self, max: Self) -> Self { ... }
}
Expand description

Trait for audio sample types (f32, f64).

Designed for zero-cost abstraction - all methods inline for monomorphization. Only includes operations commonly needed in audio DSP inner loops.

§Design Philosophy

  • Minimal but complete: Only essential operations
  • Inline everything: Trust LLVM to optimize
  • Includes Div: For cleaner RMS/average calculations

§Example: Generic Gain Plugin

fn process_generic<S: Sample>(&mut self, buffer: &mut Buffer<S>) {
    let gain = S::from_f32(self.params.gain_linear());
    for (input, output) in buffer.zip_channels() {
        for (i, o) in input.iter().zip(output.iter_mut()) {
            *o = *i * gain;
        }
    }
}

Required Associated Constants§

Source

const ZERO: Self

Zero value (0.0).

Source

const ONE: Self

Unit value (1.0).

Required Methods§

Source

fn from_f32(value: f32) -> Self

Convert from f32.

Source

fn to_f32(self) -> f32

Convert to f32.

Source

fn from_f64(value: f64) -> Self

Convert from f64.

Source

fn to_f64(self) -> f64

Convert to f64.

Source

fn abs(self) -> Self

Absolute value.

Source

fn sqrt(self) -> Self

Square root.

Source

fn sin(self) -> Self

Sine.

Source

fn cos(self) -> Self

Cosine.

Source

fn min(self, other: Self) -> Self

Minimum of two values.

Source

fn max(self, other: Self) -> Self

Maximum of two values.

Provided Methods§

Source

fn clamp(self, min: Self, max: Self) -> Self

Clamp value between min and max.

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.

Implementations on Foreign Types§

Source§

impl Sample for f32

Source§

const ZERO: Self = 0f32

Source§

const ONE: Self = 1f32

Source§

fn from_f32(value: f32) -> Self

Source§

fn to_f32(self) -> f32

Source§

fn from_f64(value: f64) -> Self

Source§

fn to_f64(self) -> f64

Source§

fn abs(self) -> Self

Source§

fn sqrt(self) -> Self

Source§

fn sin(self) -> Self

Source§

fn cos(self) -> Self

Source§

fn min(self, other: Self) -> Self

Source§

fn max(self, other: Self) -> Self

Source§

fn clamp(self, min: Self, max: Self) -> Self

Source§

impl Sample for f64

Source§

const ZERO: Self = 0f64

Source§

const ONE: Self = 1f64

Source§

fn from_f32(value: f32) -> Self

Source§

fn to_f32(self) -> f32

Source§

fn from_f64(value: f64) -> Self

Source§

fn to_f64(self) -> f64

Source§

fn abs(self) -> Self

Source§

fn sqrt(self) -> Self

Source§

fn sin(self) -> Self

Source§

fn cos(self) -> Self

Source§

fn min(self, other: Self) -> Self

Source§

fn max(self, other: Self) -> Self

Source§

fn clamp(self, min: Self, max: Self) -> Self

Implementors§