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.
|
2015-09-01 12:26:25 +00:00
|
|
|
#[inline]
|
2017-10-11 08:39:44 +00:00
|
|
|
pub fn 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>(),
|
|
|
|
}
|
|
|
|
}
|
2017-10-11 08:39:44 +00:00
|
|
|
|
|
|
|
/// Deprecated. Use `sample_size` instead.
|
|
|
|
#[inline]
|
|
|
|
#[deprecated]
|
|
|
|
pub fn get_sample_size(&self) -> usize {
|
|
|
|
self.sample_size()
|
|
|
|
}
|
2014-12-17 07:47:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
2017-10-11 08:39:44 +00:00
|
|
|
// TODO: rename to `format()`. Requires a breaking change.
|
2015-08-20 12:38:25 +00:00
|
|
|
fn get_format() -> SampleFormat;
|
2017-10-18 18:24:05 +00:00
|
|
|
|
|
|
|
/// Turns the sample into its equivalent as a floating-point.
|
|
|
|
fn to_f32(&self) -> f32;
|
2017-10-21 07:45:02 +00:00
|
|
|
/// Converts this sample into a standard i16 sample.
|
|
|
|
fn to_i16(&self) -> i16;
|
|
|
|
/// Converts this sample into a standard u16 sample.
|
|
|
|
fn to_u16(&self) -> u16;
|
|
|
|
|
|
|
|
/// Converts any sample type to this one by calling `to_i16`, `to_u16` or `to_f32`.
|
|
|
|
fn from<S>(&S) -> Self
|
|
|
|
where S: Sample;
|
2014-12-17 07:47:19 +00:00
|
|
|
}
|
|
|
|
|
2015-08-20 12:38:25 +00:00
|
|
|
unsafe impl Sample for u16 {
|
2015-09-01 12:26:25 +00:00
|
|
|
#[inline]
|
2015-08-20 12:38:25 +00:00
|
|
|
fn get_format() -> SampleFormat {
|
2014-12-17 07:47:19 +00:00
|
|
|
SampleFormat::U16
|
|
|
|
}
|
2017-10-18 18:24:05 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn to_f32(&self) -> f32 {
|
2017-10-21 07:45:02 +00:00
|
|
|
self.to_i16().to_f32()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn to_i16(&self) -> i16 {
|
|
|
|
if *self >= 32768 {
|
|
|
|
(*self - 32768) as i16
|
|
|
|
} else {
|
|
|
|
(*self as i16) - 32767 - 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn to_u16(&self) -> u16 {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn from<S>(sample: &S) -> Self
|
|
|
|
where S: Sample
|
|
|
|
{
|
|
|
|
sample.to_u16()
|
2017-10-18 18:24:05 +00:00
|
|
|
}
|
2014-12-17 07:47:19 +00:00
|
|
|
}
|
|
|
|
|
2015-08-20 12:38:25 +00:00
|
|
|
unsafe impl Sample for i16 {
|
2015-09-01 12:26:25 +00:00
|
|
|
#[inline]
|
2015-08-20 12:38:25 +00:00
|
|
|
fn get_format() -> SampleFormat {
|
2014-12-17 07:47:19 +00:00
|
|
|
SampleFormat::I16
|
|
|
|
}
|
2017-10-18 18:24:05 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn to_f32(&self) -> f32 {
|
|
|
|
if *self < 0 {
|
|
|
|
*self as f32 / -(::std::i16::MIN as f32)
|
|
|
|
} else {
|
|
|
|
*self as f32 / ::std::i16::MAX as f32
|
|
|
|
}
|
|
|
|
}
|
2017-10-21 07:45:02 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn to_i16(&self) -> i16 {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn to_u16(&self) -> u16 {
|
|
|
|
if *self < 0 {
|
|
|
|
(*self - ::std::i16::MIN) as u16
|
|
|
|
} else {
|
|
|
|
(*self as u16) + 32768
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn from<S>(sample: &S) -> Self
|
|
|
|
where S: Sample
|
|
|
|
{
|
|
|
|
sample.to_i16()
|
|
|
|
}
|
2014-12-17 07:47:19 +00:00
|
|
|
}
|
|
|
|
|
2015-08-20 12:38:25 +00:00
|
|
|
unsafe impl Sample for f32 {
|
2015-09-01 12:26:25 +00:00
|
|
|
#[inline]
|
2015-08-20 12:38:25 +00:00
|
|
|
fn get_format() -> SampleFormat {
|
2014-12-17 07:47:19 +00:00
|
|
|
SampleFormat::F32
|
|
|
|
}
|
2017-10-18 18:24:05 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn to_f32(&self) -> f32 {
|
|
|
|
*self
|
|
|
|
}
|
2017-10-21 07:45:02 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn to_i16(&self) -> i16 {
|
|
|
|
if *self >= 0.0 {
|
|
|
|
(*self * ::std::i16::MAX as f32) as i16
|
|
|
|
} else {
|
|
|
|
(-*self * ::std::i16::MIN as f32) as i16
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn to_u16(&self) -> u16 {
|
|
|
|
(((*self + 1.0) * 0.5) * ::std::u16::MAX as f32).round() as u16
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn from<S>(sample: &S) -> Self
|
|
|
|
where S: Sample
|
|
|
|
{
|
|
|
|
sample.to_f32()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::Sample;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn i16_to_i16() {
|
|
|
|
assert_eq!(0i16.to_i16(), 0);
|
|
|
|
assert_eq!((-467i16).to_i16(), -467);
|
|
|
|
assert_eq!(32767i16.to_i16(), 32767);
|
|
|
|
assert_eq!((-32768i16).to_i16(), -32768);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn i16_to_u16() {
|
|
|
|
assert_eq!(0i16.to_u16(), 32768);
|
|
|
|
assert_eq!((-16384i16).to_u16(), 16384);
|
|
|
|
assert_eq!(32767i16.to_u16(), 65535);
|
|
|
|
assert_eq!((-32768i16).to_u16(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn i16_to_f32() {
|
|
|
|
assert_eq!(0i16.to_f32(), 0.0);
|
|
|
|
assert_eq!((-16384i16).to_f32(), -0.5);
|
|
|
|
assert_eq!(32767i16.to_f32(), 1.0);
|
|
|
|
assert_eq!((-32768i16).to_f32(), -1.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn u16_to_i16() {
|
|
|
|
assert_eq!(32768u16.to_i16(), 0);
|
|
|
|
assert_eq!(16384u16.to_i16(), -16384);
|
|
|
|
assert_eq!(65535u16.to_i16(), 32767);
|
|
|
|
assert_eq!(0u16.to_i16(), -32768);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn u16_to_u16() {
|
|
|
|
assert_eq!(0u16.to_u16(), 0);
|
|
|
|
assert_eq!(467u16.to_u16(), 467);
|
|
|
|
assert_eq!(32767u16.to_u16(), 32767);
|
|
|
|
assert_eq!(65535u16.to_u16(), 65535);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn u16_to_f32() {
|
|
|
|
assert_eq!(0u16.to_f32(), -1.0);
|
|
|
|
assert_eq!(32768u16.to_f32(), 0.0);
|
|
|
|
assert_eq!(65535u16.to_f32(), 1.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn f32_to_i16() {
|
|
|
|
assert_eq!(0.0f32.to_i16(), 0);
|
|
|
|
assert_eq!((-0.5f32).to_i16(), ::std::i16::MIN / 2);
|
|
|
|
assert_eq!(1.0f32.to_i16(), ::std::i16::MAX);
|
|
|
|
assert_eq!((-1.0f32).to_i16(), ::std::i16::MIN);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn f32_to_u16() {
|
|
|
|
assert_eq!((-1.0f32).to_u16(), 0);
|
|
|
|
assert_eq!(0.0f32.to_u16(), 32768);
|
|
|
|
assert_eq!(1.0f32.to_u16(), 65535);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn f32_to_f32() {
|
|
|
|
assert_eq!(0.1f32.to_f32(), 0.1);
|
|
|
|
assert_eq!((-0.7f32).to_f32(), -0.7);
|
|
|
|
assert_eq!(1.0f32.to_f32(), 1.0);
|
|
|
|
}
|
2014-12-17 08:08:13 +00:00
|
|
|
}
|