pub trait SampleType:
SizedSample
+ Send
+ 'static {
const SILENCE: Self;
// Required methods
fn format() -> SampleFormat;
fn mix(self, other: Self) -> Self;
fn to_f32(self) -> f32;
fn from_f32(sample: f32) -> Self;
}Expand description
§SampleType
Marker trait for the real Rust types a sample can be stored in, so a
function can take samples: &[S] where S: SampleType and be called with a
plain &[f32], &[i16], &[u8], … with no conversion at the call site.
It only ever bounds a type parameter — S: SampleType reads as “S is a
sample type”, so OutputClass<f32> or data_callback::<i16> names the
concrete type directly and the compiler picks the conversions for it.
Implemented (via the blanket impl below) for every type cpal can hand to a stream callback:
i8, i16, I24, i32, i64, u8, u16, U24, u32, u64, f32, f64
cpal::I24 / cpal::U24 are i32 / u32 under the hood, just clamped to
a 24-bit range — they exist as separate newtypes only so the conversion
scales by 2^23 instead of 2^31. If you already have plain i32 data, pass
&[i32]; use I24 only when the values really are 24-bit.
DSD (DsdU8/DsdU16/DsdU32) has no type of its own: it is a 1-bit
bitstream packed into u8/u16/u32, so it stays a SampleFormat-level
concept and is not part of this trait.
Required Associated Constants§
Required Methods§
Sourcefn format() -> SampleFormat
fn format() -> SampleFormat
The SampleFormat this type is laid out as on a device.
Sourcefn mix(self, other: Self) -> Self
fn mix(self, other: Self) -> Self
Sums two samples in their own format — no float round-trip, so an
i16 pipeline stays i16 from add_samples all the way to the device.
Silence is the identity: mixing with SILENCE is a
no-op, including for the unsigned formats where silence isn’t zero.
Summing has the range of the format itself, so loud sources can overflow
it (wrapping in release, panicking in debug) exactly as an integer +
would. Leave headroom, or attenuate before mixing.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".