1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use super::audio_format::{self, linear_pcm_flags};


/// Dynamic representation of audio data sample format.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum SampleFormat {
    F32,
    I32,
    I16,
    I8,
}

impl SampleFormat {

    pub fn does_match_flags(&self, flags: audio_format::LinearPCMFlags) -> bool {
        let is_float = flags.contains(linear_pcm_flags::IS_FLOAT);
        let is_signed_integer = flags.contains(linear_pcm_flags::IS_SIGNED_INTEGER);
        match *self {
            SampleFormat::F32 => is_float && !is_signed_integer,
            SampleFormat::I32 |
            SampleFormat::I16 |
            SampleFormat::I8 => is_signed_integer && !is_float,
        }
    }

    pub fn from_flags_and_bytes_per_frame(flags: audio_format::LinearPCMFlags,
                                          bytes_per_frame: u32) -> Option<Self>
    {
        Some(if flags.contains(linear_pcm_flags::IS_FLOAT) {
            SampleFormat::F32
        } else {
            // TODO: Check whether or not we need to consider unsigned ints and `IS_PACKED`.
            match bytes_per_frame {
                1 => SampleFormat::I8,
                2 => SampleFormat::I16,
                4 => SampleFormat::I32,
                _ => return None,
            }
        })
    }

    pub fn size_in_bytes(&self) -> usize {
        use std::mem::size_of;
        match *self {
            SampleFormat::F32 => size_of::<f32>(),
            SampleFormat::I32 => size_of::<i32>(),
            SampleFormat::I16 => size_of::<i16>(),
            SampleFormat::I8 => size_of::<i8>(),
        }
    }

}

/// Audio data sample types.
pub trait Sample {
    /// Dynamic representation of audio data sample format.
    fn sample_format() -> SampleFormat;
}

/// Simplified implementation of the `Sample` trait for sample types.
macro_rules! impl_sample {
    ($($T:ident $format:ident),* $(,)*) => {
        $(
            impl Sample for $T {
                fn sample_format() -> SampleFormat {
                    SampleFormat::$format
                }
            }
        )*
    }
}

impl_sample!(f32 F32, i32 I32, i16 I16, i8 I8);