Skip to main content

Module sample

Module sample 

Source
Expand description

Float and Sample - the precision-routing traits that let plugin code stay in one float type without per-call-site casts.

Plugin authors don’t usually name these traits directly. They pick a precision via the prelude (truce::prelude / truce::prelude32 for f32, truce::prelude64 for f64); the prelude’s type Sample alias resolves the bound at the call sites. The traits surface only when DSP code wants to convert between precisions per value:

use truce_params::sample::Float;
let v_f32: f32 = 0.5;
let v_f64: f64 = v_f32.to_f64();   // widen
let back:  f32 = f32::from_f64(v_f64); // narrow

Both traits are sealed at f32 and f64. Downstream code can’t add new impls; numeric types beyond these two have never been worth the complexity for audio.

§Two traits, why

  • Float is the broad math bound. Use it for utilities like db_to_linear, midi_note_to_freq - values that happen to be f32 or f64 but aren’t audio samples. The bound carries the precision-routing methods (from_f32/from_f64/to_f32/to_f64) plus a handful of math primitives (exp, log10, powf). Float::from_f64’s NaN debug-assert is the same as Sample’s, because anywhere a NaN narrowing slips through is a bug regardless of whether the value is a sample or a gain coefficient.
  • Sample is Float plus the marker bounds that buffer code needs (Default + Send + Sync + 'static) so the wrapper can default-construct scratch buffers and pass them across threads. This is the bound that goes on AudioBuffer<S>, Plugin::Sample, and the FloatParamRead<S> extension trait.

Traits§

Float
Broad numeric trait for code that operates on f32 or f64 but isn’t necessarily handling audio samples. Use this for math utilities (gain conversions, frequency math, filter coefficients). For audio-sample-typed surfaces (AudioBuffer<S>, smoother reads), use Sample instead, which extends Float with the marker bounds buffer code needs.
Sample
Audio-sample subtype of Float. Adds the Default + Send + Sync + 'static marker bounds that buffer code, scratch allocators, and the param-read extension trait need.