cpal/src/samples_formats.rs

52 lines
1.1 KiB
Rust
Raw Normal View History

2014-12-17 07:47:19 +00:00
use std::mem;
/// Format that each sample has.
2015-02-22 09:31:25 +00:00
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
2014-12-17 07:47:19 +00:00
pub enum SampleFormat {
/// The value 0 corresponds to 0.
I16,
/// The value 0 corresponds to 32768.
U16,
2014-12-17 08:13:58 +00:00
/// The boundaries are (-1.0, 1.0).
2014-12-17 07:47:19 +00:00
F32,
}
impl SampleFormat {
2014-12-17 08:13:58 +00:00
/// Returns the size in bytes of a sample of this format.
#[inline]
2015-01-09 20:25:51 +00:00
pub fn get_sample_size(&self) -> usize {
2014-12-17 07:47:19 +00:00
match self {
&SampleFormat::I16 => mem::size_of::<i16>(),
&SampleFormat::U16 => mem::size_of::<u16>(),
&SampleFormat::F32 => mem::size_of::<f32>(),
}
}
}
/// Trait for containers that contain PCM data.
2015-08-20 12:38:25 +00:00
pub unsafe trait Sample: Copy + Clone {
/// Returns the `SampleFormat` corresponding to this data type.
fn get_format() -> SampleFormat;
2014-12-17 07:47:19 +00:00
}
2015-08-20 12:38:25 +00:00
unsafe impl Sample for u16 {
#[inline]
2015-08-20 12:38:25 +00:00
fn get_format() -> SampleFormat {
2014-12-17 07:47:19 +00:00
SampleFormat::U16
}
}
2015-08-20 12:38:25 +00:00
unsafe impl Sample for i16 {
#[inline]
2015-08-20 12:38:25 +00:00
fn get_format() -> SampleFormat {
2014-12-17 07:47:19 +00:00
SampleFormat::I16
}
}
2015-08-20 12:38:25 +00:00
unsafe impl Sample for f32 {
#[inline]
2015-08-20 12:38:25 +00:00
fn get_format() -> SampleFormat {
2014-12-17 07:47:19 +00:00
SampleFormat::F32
}
}