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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright © 2017-2018 Mozilla Foundation
//
// This program is made available under an ISC-style license.  See the
// accompanying file LICENSE for details.

use ffi;

bitflags! {
    /// Some common layout definitions
    pub struct ChannelLayout: ffi::cubeb_channel_layout {
        ///
        const FRONT_LEFT = ffi::CHANNEL_FRONT_LEFT;
        const FRONT_RIGHT = ffi::CHANNEL_FRONT_RIGHT;
        const FRONT_CENTER = ffi::CHANNEL_FRONT_CENTER;
        const LOW_FREQUENCY = ffi::CHANNEL_LOW_FREQUENCY;
        const BACK_LEFT = ffi::CHANNEL_BACK_LEFT;
        const BACK_RIGHT = ffi::CHANNEL_BACK_RIGHT;
        const FRONT_LEFT_OF_CENTER = ffi::CHANNEL_FRONT_LEFT_OF_CENTER;
        const FRONT_RIGHT_OF_CENTER = ffi::CHANNEL_FRONT_RIGHT_OF_CENTER;
        const BACK_CENTER = ffi::CHANNEL_BACK_CENTER;
        const SIDE_LEFT = ffi::CHANNEL_SIDE_LEFT;
        const SIDE_RIGHT = ffi::CHANNEL_SIDE_RIGHT;
        const TOP_CENTER = ffi::CHANNEL_TOP_CENTER;
        const TOP_FRONT_LEFT = ffi::CHANNEL_TOP_FRONT_LEFT;
        const TOP_FRONT_CENTER = ffi::CHANNEL_TOP_FRONT_CENTER;
        const TOP_FRONT_RIGHT = ffi::CHANNEL_TOP_FRONT_RIGHT;
        const TOP_BACK_LEFT = ffi::CHANNEL_TOP_BACK_LEFT;
        const TOP_BACK_CENTER = ffi::CHANNEL_TOP_BACK_CENTER;
        const TOP_BACK_RIGHT = ffi::CHANNEL_TOP_BACK_RIGHT;
    }
}

macro_rules! bits {
    ($($x:ident => $y:ident),*) => {
        $(pub const $x: ChannelLayout = ChannelLayout::from_bits_truncate(ffi::$y);)*
    }
}

impl ChannelLayout {
    bits!(UNDEFINED => CUBEB_LAYOUT_UNDEFINED,
          MONO => CUBEB_LAYOUT_MONO,
          MONO_LFE => CUBEB_LAYOUT_MONO_LFE,
          STEREO => CUBEB_LAYOUT_STEREO,
          STEREO_LFE => CUBEB_LAYOUT_STEREO_LFE,
          _3F => CUBEB_LAYOUT_3F,
          _3F_LFE => CUBEB_LAYOUT_3F_LFE,
          _2F1 => CUBEB_LAYOUT_2F1,
          _2F1_LFE => CUBEB_LAYOUT_2F1_LFE,
          _3F1 => CUBEB_LAYOUT_3F1,
          _3F1_LFE => CUBEB_LAYOUT_3F1_LFE,
          _2F2 => CUBEB_LAYOUT_2F2,
          _2F2_LFE => CUBEB_LAYOUT_2F2_LFE,
          QUAD => CUBEB_LAYOUT_QUAD,
          QUAD_LFE => CUBEB_LAYOUT_QUAD_LFE,
          _3F2 => CUBEB_LAYOUT_3F2,
          _3F2_LFE => CUBEB_LAYOUT_3F2_LFE,
          _3F2_BACK => CUBEB_LAYOUT_3F2_BACK,
          _3F2_LFE_BACK => CUBEB_LAYOUT_3F2_LFE_BACK,
          _3F3R_LFE => CUBEB_LAYOUT_3F3R_LFE,
          _3F4_LFE => CUBEB_LAYOUT_3F4_LFE
    );
}

impl From<ffi::cubeb_channel> for ChannelLayout {
    fn from(x: ffi::cubeb_channel) -> Self {
        ChannelLayout::from_bits_truncate(x)
    }
}

impl From<ChannelLayout> for ffi::cubeb_channel {
    fn from(x: ChannelLayout) -> Self {
        x.bits()
    }
}

impl ChannelLayout {
    pub fn num_channels(&self) -> u32 {
        unsafe { ffi::cubeb_channel_layout_nb_channels(self.bits()) }
    }
}

#[cfg(test)]
mod test {
    use ffi;

    #[test]
    fn channel_layout_from_raw() {
        macro_rules! check(
            ($($raw:ident => $real:ident),*) => (
                $(let x = super::ChannelLayout::from(ffi::$raw);
                  assert_eq!(x, super::ChannelLayout::$real);
                )*
            ) );

        check!(CUBEB_LAYOUT_UNDEFINED => UNDEFINED,
               CUBEB_LAYOUT_MONO => MONO,
               CUBEB_LAYOUT_MONO_LFE => MONO_LFE,
               CUBEB_LAYOUT_STEREO => STEREO,
               CUBEB_LAYOUT_STEREO_LFE => STEREO_LFE,
               CUBEB_LAYOUT_3F => _3F,
               CUBEB_LAYOUT_3F_LFE => _3F_LFE,
               CUBEB_LAYOUT_2F1 => _2F1,
               CUBEB_LAYOUT_2F1_LFE => _2F1_LFE,
               CUBEB_LAYOUT_3F1 => _3F1,
               CUBEB_LAYOUT_3F1_LFE => _3F1_LFE,
               CUBEB_LAYOUT_2F2 => _2F2,
               CUBEB_LAYOUT_2F2_LFE => _2F2_LFE,
               CUBEB_LAYOUT_QUAD => QUAD,
               CUBEB_LAYOUT_QUAD_LFE => QUAD_LFE,
               CUBEB_LAYOUT_3F2 => _3F2,
               CUBEB_LAYOUT_3F2_LFE => _3F2_LFE,
               CUBEB_LAYOUT_3F2_BACK => _3F2_BACK,
               CUBEB_LAYOUT_3F2_LFE_BACK => _3F2_LFE_BACK,
               CUBEB_LAYOUT_3F3R_LFE => _3F3R_LFE,
               CUBEB_LAYOUT_3F4_LFE => _3F4_LFE);
    }

    #[test]
    fn channel_layout_into_raw() {
        macro_rules! check(
            ($($real:ident => $raw:ident),*) => (
                $(let x = super::ChannelLayout::$real;
                  let x: ffi::cubeb_channel_layout = x.into();
                  assert_eq!(x, ffi::$raw);
                )*
            ) );

        check!(UNDEFINED => CUBEB_LAYOUT_UNDEFINED,
               MONO => CUBEB_LAYOUT_MONO,
               MONO_LFE => CUBEB_LAYOUT_MONO_LFE,
               STEREO => CUBEB_LAYOUT_STEREO,
               STEREO_LFE => CUBEB_LAYOUT_STEREO_LFE,
               _3F => CUBEB_LAYOUT_3F,
               _3F_LFE => CUBEB_LAYOUT_3F_LFE,
               _2F1 => CUBEB_LAYOUT_2F1,
               _2F1_LFE=> CUBEB_LAYOUT_2F1_LFE,
               _3F1 => CUBEB_LAYOUT_3F1,
               _3F1_LFE =>  CUBEB_LAYOUT_3F1_LFE,
               _2F2 => CUBEB_LAYOUT_2F2,
               _2F2_LFE => CUBEB_LAYOUT_2F2_LFE,
               QUAD => CUBEB_LAYOUT_QUAD,
               QUAD_LFE => CUBEB_LAYOUT_QUAD_LFE,
               _3F2 => CUBEB_LAYOUT_3F2,
               _3F2_LFE => CUBEB_LAYOUT_3F2_LFE,
               _3F2_BACK => CUBEB_LAYOUT_3F2_BACK,
               _3F2_LFE_BACK => CUBEB_LAYOUT_3F2_LFE_BACK,
               _3F3R_LFE => CUBEB_LAYOUT_3F3R_LFE,
               _3F4_LFE => CUBEB_LAYOUT_3F4_LFE);
    }
}