1use ffi;
7use {ChannelLayout, InputProcessingParams, SampleFormat, StreamParams, StreamPrefs};
8
9#[derive(Debug)]
10pub struct StreamParamsBuilder(ffi::cubeb_stream_params);
11
12impl Default for StreamParamsBuilder {
13 fn default() -> Self {
14 StreamParamsBuilder(ffi::cubeb_stream_params {
15 format: ffi::CUBEB_SAMPLE_S16NE,
16 ..Default::default()
17 })
18 }
19}
20
21impl StreamParamsBuilder {
22 pub fn new() -> Self {
23 Default::default()
24 }
25
26 pub fn format(mut self, format: SampleFormat) -> Self {
27 self.0.format = format.into();
28 self
29 }
30
31 pub fn rate(mut self, rate: u32) -> Self {
32 self.0.rate = rate;
33 self
34 }
35
36 pub fn channels(mut self, channels: u32) -> Self {
37 self.0.channels = channels;
38 self
39 }
40
41 pub fn layout(mut self, layout: ChannelLayout) -> Self {
42 self.0.layout = layout.into();
43 self
44 }
45
46 pub fn prefs(mut self, prefs: StreamPrefs) -> Self {
47 self.0.prefs = prefs.bits();
48 self
49 }
50
51 pub fn input_params(mut self, input_params: InputProcessingParams) -> Self {
52 self.0.input_params = input_params.bits();
53 self
54 }
55
56 pub fn take(&self) -> StreamParams {
57 StreamParams::from(self.0)
58 }
59}
60
61#[cfg(test)]
62mod tests {
63 use SampleFormat;
64 use {ffi, InputProcessingParams, StreamParamsBuilder, StreamPrefs};
65
66 #[test]
67 fn stream_params_builder_channels() {
68 let params = StreamParamsBuilder::new().channels(2).take();
69 assert_eq!(params.channels(), 2);
70 }
71
72 #[test]
73 fn stream_params_builder_format() {
74 macro_rules! check(
75 ($($real:ident),*) => (
76 $(let params = StreamParamsBuilder::new()
77 .format(super::SampleFormat::$real)
78 .take();
79 assert_eq!(params.format(), super::SampleFormat::$real);
80 )*
81 ) );
82
83 check!(S16LE, S16BE, Float32LE, Float32BE);
84 }
85
86 #[test]
87 fn stream_params_builder_format_native_endian() {
88 let params = StreamParamsBuilder::new()
89 .format(SampleFormat::S16NE)
90 .take();
91 assert_eq!(
92 params.format(),
93 if cfg!(target_endian = "little") {
94 super::SampleFormat::S16LE
95 } else {
96 super::SampleFormat::S16BE
97 }
98 );
99
100 let params = StreamParamsBuilder::new()
101 .format(SampleFormat::Float32NE)
102 .take();
103 assert_eq!(
104 params.format(),
105 if cfg!(target_endian = "little") {
106 SampleFormat::Float32LE
107 } else {
108 SampleFormat::Float32BE
109 }
110 );
111 }
112
113 #[test]
114 fn stream_params_builder_layout() {
115 macro_rules! check(
116 ($($real:ident),*) => (
117 $(let params = StreamParamsBuilder::new()
118 .layout(super::ChannelLayout::$real)
119 .take();
120 assert_eq!(params.layout(), super::ChannelLayout::$real);
121 )*
122 ) );
123
124 check!(
125 UNDEFINED,
126 MONO,
127 MONO_LFE,
128 STEREO,
129 STEREO_LFE,
130 _3F,
131 _3F_LFE,
132 _2F1,
133 _2F1_LFE,
134 _3F1,
135 _3F1_LFE,
136 _2F2,
137 _2F2_LFE,
138 QUAD,
139 QUAD_LFE,
140 _3F2,
141 _3F2_LFE,
142 _3F2_BACK,
143 _3F2_LFE_BACK,
144 _3F3R_LFE,
145 _3F4_LFE
146 );
147 }
148
149 #[test]
150 fn stream_params_builder_rate() {
151 let params = StreamParamsBuilder::new().rate(44100).take();
152 assert_eq!(params.rate(), 44100);
153 }
154
155 #[test]
156 fn stream_params_builder_to_raw_channels() {
157 let params = StreamParamsBuilder::new().channels(2).take();
158 let raw = unsafe { &*params.as_ptr() };
159 assert_eq!(raw.channels, 2);
160 }
161
162 #[test]
163 fn stream_params_builder_to_raw_format() {
164 macro_rules! check(
165 ($($real:ident => $raw:ident),*) => (
166 $(let params = super::StreamParamsBuilder::new()
167 .format(SampleFormat::$real)
168 .take();
169 let raw = unsafe { &*params.as_ptr() };
170 assert_eq!(raw.format, ffi::$raw);
171 )*
172 ) );
173
174 check!(S16LE => CUBEB_SAMPLE_S16LE,
175 S16BE => CUBEB_SAMPLE_S16BE,
176 Float32LE => CUBEB_SAMPLE_FLOAT32LE,
177 Float32BE => CUBEB_SAMPLE_FLOAT32BE);
178 }
179
180 #[test]
181 fn stream_params_builder_format_to_raw_native_endian() {
182 let params = StreamParamsBuilder::new()
183 .format(SampleFormat::S16NE)
184 .take();
185 let raw = unsafe { &*params.as_ptr() };
186 assert_eq!(
187 raw.format,
188 if cfg!(target_endian = "little") {
189 ffi::CUBEB_SAMPLE_S16LE
190 } else {
191 ffi::CUBEB_SAMPLE_S16BE
192 }
193 );
194
195 let params = StreamParamsBuilder::new()
196 .format(SampleFormat::Float32NE)
197 .take();
198 let raw = unsafe { &*params.as_ptr() };
199 assert_eq!(
200 raw.format,
201 if cfg!(target_endian = "little") {
202 ffi::CUBEB_SAMPLE_FLOAT32LE
203 } else {
204 ffi::CUBEB_SAMPLE_FLOAT32BE
205 }
206 );
207 }
208
209 #[test]
210 fn stream_params_builder_to_raw_layout() {
211 macro_rules! check(
212 ($($real:ident => $raw:ident),*) => (
213 $(let params = super::StreamParamsBuilder::new()
214 .layout(super::ChannelLayout::$real)
215 .take();
216 let raw = unsafe { &*params.as_ptr() };
217 assert_eq!(raw.layout, ffi::$raw);
218 )*
219 ) );
220
221 check!(UNDEFINED => CUBEB_LAYOUT_UNDEFINED,
222 MONO => CUBEB_LAYOUT_MONO,
223 MONO_LFE => CUBEB_LAYOUT_MONO_LFE,
224 STEREO => CUBEB_LAYOUT_STEREO,
225 STEREO_LFE => CUBEB_LAYOUT_STEREO_LFE,
226 _3F => CUBEB_LAYOUT_3F,
227 _3F_LFE => CUBEB_LAYOUT_3F_LFE,
228 _2F1 => CUBEB_LAYOUT_2F1,
229 _2F1_LFE=> CUBEB_LAYOUT_2F1_LFE,
230 _3F1 => CUBEB_LAYOUT_3F1,
231 _3F1_LFE => CUBEB_LAYOUT_3F1_LFE,
232 _2F2 => CUBEB_LAYOUT_2F2,
233 _2F2_LFE => CUBEB_LAYOUT_2F2_LFE,
234 QUAD => CUBEB_LAYOUT_QUAD,
235 QUAD_LFE => CUBEB_LAYOUT_QUAD_LFE,
236 _3F2 => CUBEB_LAYOUT_3F2,
237 _3F2_LFE => CUBEB_LAYOUT_3F2_LFE,
238 _3F2_BACK => CUBEB_LAYOUT_3F2_BACK,
239 _3F2_LFE_BACK => CUBEB_LAYOUT_3F2_LFE_BACK,
240 _3F3R_LFE => CUBEB_LAYOUT_3F3R_LFE,
241 _3F4_LFE => CUBEB_LAYOUT_3F4_LFE);
242 }
243
244 #[test]
245 fn stream_params_builder_to_raw_rate() {
246 let params = StreamParamsBuilder::new().rate(44100).take();
247 let raw = unsafe { &*params.as_ptr() };
248 assert_eq!(raw.rate, 44100);
249 }
250
251 #[test]
252 fn stream_params_builder_prefs_default() {
253 let params = StreamParamsBuilder::new().take();
254 assert_eq!(params.prefs(), StreamPrefs::NONE);
255 }
256
257 #[test]
258 fn stream_params_builder_prefs() {
259 let params = StreamParamsBuilder::new()
260 .prefs(StreamPrefs::LOOPBACK)
261 .take();
262 assert_eq!(params.prefs(), StreamPrefs::LOOPBACK);
263 }
264
265 #[test]
266 fn stream_params_builder_input_params() {
267 let params = StreamParamsBuilder::new()
268 .input_params(InputProcessingParams::NOISE_SUPPRESSION)
269 .take();
270 assert_eq!(
271 params.input_params(),
272 InputProcessingParams::NOISE_SUPPRESSION
273 );
274 }
275}