extern crate libsoundio_sys as raw;
use std::ffi::CStr;
use std::fmt;
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum Format {
Invalid,
S8,
U8,
S16LE,
S16BE,
U16LE,
U16BE,
S24LE,
S24BE,
U24LE,
U24BE,
S32LE,
S32BE,
U32LE,
U32BE,
Float32LE,
Float32BE,
Float64LE,
Float64BE,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum Endian {
Big,
Little,
}
pub fn endianness(f: Format) -> Endian {
match f {
Format::Invalid
| Format::U8
| Format::S8
| Format::U16LE
| Format::S16LE
| Format::U24LE
| Format::S24LE
| Format::U32LE
| Format::S32LE
| Format::Float32LE
| Format::Float64LE => Endian::Little,
Format::U16BE
| Format::S16BE
| Format::U24BE
| Format::S24BE
| Format::U32BE
| Format::S32BE
| Format::Float32BE
| Format::Float64BE => Endian::Big,
}
}
#[cfg(target_endian = "big")]
#[allow(non_upper_case_globals)]
pub mod native {
use super::Format;
pub const S16FE: Format = Format::S16LE;
pub const S16NE: Format = Format::S16BE;
pub const U16FE: Format = Format::U16LE;
pub const U16NE: Format = Format::U16BE;
pub const S24FE: Format = Format::S24LE;
pub const S24NE: Format = Format::S24BE;
pub const U24FE: Format = Format::U24LE;
pub const U24NE: Format = Format::U24BE;
pub const S32FE: Format = Format::S32LE;
pub const S32NE: Format = Format::S32BE;
pub const U32FE: Format = Format::U32LE;
pub const U32NE: Format = Format::U32BE;
pub const Float32FE: Format = Format::Float32LE;
pub const Float32NE: Format = Format::Float32BE;
pub const Float64FE: Format = Format::Float64LE;
pub const Float64NE: Format = Format::Float64BE;
}
#[cfg(target_endian = "little")]
#[allow(non_upper_case_globals)]
pub mod native {
use super::Format;
pub const S16NE: Format = Format::S16LE;
pub const S16FE: Format = Format::S16BE;
pub const U16NE: Format = Format::U16LE;
pub const U16FE: Format = Format::U16BE;
pub const S24NE: Format = Format::S24LE;
pub const S24FE: Format = Format::S24BE;
pub const U24NE: Format = Format::U24LE;
pub const U24FE: Format = Format::U24BE;
pub const S32NE: Format = Format::S32LE;
pub const S32FE: Format = Format::S32BE;
pub const U32NE: Format = Format::U32LE;
pub const U32FE: Format = Format::U32BE;
pub const Float32NE: Format = Format::Float32LE;
pub const Float32FE: Format = Format::Float32BE;
pub const Float64NE: Format = Format::Float64LE;
pub const Float64FE: Format = Format::Float64BE;
}
impl From<raw::SoundIoFormat> for Format {
fn from(format: raw::SoundIoFormat) -> Format {
match format {
raw::SoundIoFormat::SoundIoFormatS8 => Format::S8,
raw::SoundIoFormat::SoundIoFormatU8 => Format::U8,
raw::SoundIoFormat::SoundIoFormatS16LE => Format::S16LE,
raw::SoundIoFormat::SoundIoFormatS16BE => Format::S16BE,
raw::SoundIoFormat::SoundIoFormatU16LE => Format::U16LE,
raw::SoundIoFormat::SoundIoFormatU16BE => Format::U16BE,
raw::SoundIoFormat::SoundIoFormatS24LE => Format::S24LE,
raw::SoundIoFormat::SoundIoFormatS24BE => Format::S24BE,
raw::SoundIoFormat::SoundIoFormatU24LE => Format::U24LE,
raw::SoundIoFormat::SoundIoFormatU24BE => Format::U24BE,
raw::SoundIoFormat::SoundIoFormatS32LE => Format::S32LE,
raw::SoundIoFormat::SoundIoFormatS32BE => Format::S32BE,
raw::SoundIoFormat::SoundIoFormatU32LE => Format::U32LE,
raw::SoundIoFormat::SoundIoFormatU32BE => Format::U32BE,
raw::SoundIoFormat::SoundIoFormatFloat32LE => Format::Float32LE,
raw::SoundIoFormat::SoundIoFormatFloat32BE => Format::Float32BE,
raw::SoundIoFormat::SoundIoFormatFloat64LE => Format::Float64LE,
raw::SoundIoFormat::SoundIoFormatFloat64BE => Format::Float64BE,
_ => Format::Invalid,
}
}
}
impl From<Format> for raw::SoundIoFormat {
fn from(format: Format) -> raw::SoundIoFormat {
match format {
Format::S8 => raw::SoundIoFormat::SoundIoFormatS8,
Format::U8 => raw::SoundIoFormat::SoundIoFormatU8,
Format::S16LE => raw::SoundIoFormat::SoundIoFormatS16LE,
Format::S16BE => raw::SoundIoFormat::SoundIoFormatS16BE,
Format::U16LE => raw::SoundIoFormat::SoundIoFormatU16LE,
Format::U16BE => raw::SoundIoFormat::SoundIoFormatU16BE,
Format::S24LE => raw::SoundIoFormat::SoundIoFormatS24LE,
Format::S24BE => raw::SoundIoFormat::SoundIoFormatS24BE,
Format::U24LE => raw::SoundIoFormat::SoundIoFormatU24LE,
Format::U24BE => raw::SoundIoFormat::SoundIoFormatU24BE,
Format::S32LE => raw::SoundIoFormat::SoundIoFormatS32LE,
Format::S32BE => raw::SoundIoFormat::SoundIoFormatS32BE,
Format::U32LE => raw::SoundIoFormat::SoundIoFormatU32LE,
Format::U32BE => raw::SoundIoFormat::SoundIoFormatU32BE,
Format::Float32LE => raw::SoundIoFormat::SoundIoFormatFloat32LE,
Format::Float32BE => raw::SoundIoFormat::SoundIoFormatFloat32BE,
Format::Float64LE => raw::SoundIoFormat::SoundIoFormatFloat64LE,
Format::Float64BE => raw::SoundIoFormat::SoundIoFormatFloat64BE,
_ => raw::SoundIoFormat::SoundIoFormatInvalid,
}
}
}
impl Format {
pub fn bytes_per_sample(self) -> usize {
unsafe { raw::soundio_get_bytes_per_sample(self.into()) as usize }
}
pub fn bytes_per_frame(self, channel_count: usize) -> usize {
self.bytes_per_sample() * channel_count
}
pub fn bytes_per_second(self, channel_count: usize, sample_rate: usize) -> usize {
self.bytes_per_sample() * channel_count * sample_rate
}
}
impl fmt::Display for Format {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let c_str: &CStr = unsafe { CStr::from_ptr(raw::soundio_format_string((*self).into())) };
f.write_str(c_str.to_str().unwrap())
}
}