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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
extern crate libsoundio_sys as raw;

use super::channels::*;
use super::util::*;

use std::cmp::min;
use std::os::raw::c_int;
use std::ptr;

/// A `ChannelLayout` specifies a number of channels, and the `ChannelId` of each channel.
/// A `ChannelLayout` also has a name, though it is really only for display purposes and does
/// not affect execution at any point.
///
/// For example, the built-in stereo layout that is returned by `ChannelLayout::get_builtin(`ChannelLayoutId::Stereo)` is equal to:
///
/// ```
/// soundio::ChannelLayout {
/// 	name: "Stereo".to_string(),
/// 	channels: vec![soundio::ChannelId::FrontLeft, soundio::ChannelId::FrontRight],
/// };
/// ```
#[derive(Debug, Clone)]
pub struct ChannelLayout {
    /// The name of the layout. This is mostly useful when enumerating built-in layouts.
    pub name: String,
    /// A list of channels. Order is significant.
    pub channels: Vec<ChannelId>,
}

impl From<raw::SoundIoChannelLayout> for ChannelLayout {
    fn from(layout: raw::SoundIoChannelLayout) -> ChannelLayout {
        ChannelLayout {
            name: latin1_to_string(layout.name),
            channels: layout
                .channels
                .iter()
                .take(layout.channel_count as usize)
                .map(|&x| x.into())
                .collect(),
        }
    }
}

impl From<ChannelLayout> for raw::SoundIoChannelLayout {
    fn from(layout: ChannelLayout) -> raw::SoundIoChannelLayout {
        raw::SoundIoChannelLayout {
            // As far as I can tell there is no need to be able to set the name,
            // and doing so would be rather complicated.
            name: ptr::null(),

            // Channel counts are silently truncated to SOUNDIO_MAX_CHANNELS.
            // TODO: Limit the number of possible channels in layout (e.g. using arrayvec).
            channel_count: min(layout.channels.len(), raw::SOUNDIO_MAX_CHANNELS) as c_int,
            channels: {
                let mut c =
                    [raw::SoundIoChannelId::SoundIoChannelIdInvalid; raw::SOUNDIO_MAX_CHANNELS];
                for (dst, &src) in c[..layout.channels.len()].iter_mut().zip(&layout.channels) {
                    *dst = src.into();
                }
                c
            },
        }
    }
}

impl ChannelLayout {
    /// Get all of the built-in layouts.
    ///
    /// # Examples
    ///
    /// ```
    /// let builtins = soundio::ChannelLayout::get_all_builtin();
    /// println!("{:?}", builtins);
    /// ```
    pub fn get_all_builtin() -> Vec<ChannelLayout> {
        let count = unsafe { raw::soundio_channel_layout_builtin_count() };
        let mut layouts = Vec::new();
        for i in 0..count {
            layouts.push(unsafe { (*raw::soundio_channel_layout_get_builtin(i)).into() });
        }
        layouts
    }

    /// Get a specific built-in layout. See `ChannelLayoutId` for a list
    /// of built-in layouts.
    ///
    /// # Examples
    ///
    /// ```
    /// let stereo_layout = soundio::ChannelLayout::get_builtin(soundio::ChannelLayoutId::Stereo);
    /// assert_eq!(stereo_layout.channels.len(), 2);
    /// ```
    pub fn get_builtin(id: ChannelLayoutId) -> ChannelLayout {
        unsafe {
            (*raw::soundio_channel_layout_get_builtin(raw::SoundIoChannelLayoutId::from(id) as _))
                .into()
        }
    }

    /// Get the default layout for the given number of channels.
    ///
    /// # Examples
    ///
    /// ```
    /// let default_stereo = soundio::ChannelLayout::get_default(2);
    /// assert_eq!(default_stereo.name, "Stereo".to_string());
    /// ```
    pub fn get_default(channel_count: i32) -> ChannelLayout {
        unsafe { (*raw::soundio_channel_layout_get_default(channel_count as c_int)).into() }
    }

    /// Iterates over preferred_layouts. Returns the first channel layout in
    /// preferred_layouts which matches (using ==) one of the channel layouts in
    /// available_layouts. Returns None if none matches.
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// let my_device: Device = ...;
    /// let preferred_layouts = vec![ChannelLayout::get_builtin(ChannelLayoutId::Stereo),
    ///                              ChannelLayout::get_builtin(ChannelLayoutId::Mono)];
    ///
    /// let available_layouts = my_device.layouts();
    ///
    /// let best_layout = ChannelLayout::best_matching_channel_layout(preferred_layouts, available_layouts);
    ///
    /// if best_layout == None {
    ///     panic!("Stereo and mono not available! What *is* this device??");
    /// }
    /// let Some(best_layout) = best_layout;
    /// ```
    pub fn best_matching_channel_layout(
        preferred_layouts: &[ChannelLayout],
        available_layouts: &[ChannelLayout],
    ) -> Option<ChannelLayout> {
        for preferred_layout in preferred_layouts {
            if available_layouts.contains(preferred_layout) {
                return Some(preferred_layout.clone());
            }
        }
        None
    }

    /// Find the given channel in a layout and return its index, or `None` if it wasn't found.
    ///
    /// # Examples
    ///
    /// ```
    /// let layout = soundio::ChannelLayout::get_builtin(soundio::ChannelLayoutId::Stereo);
    /// let left_idx = layout.find_channel(soundio::ChannelId::FrontLeft);
    /// let center_idx = layout.find_channel(soundio::ChannelId::FrontCenter);
    ///
    /// assert_eq!(left_idx, Some(0));
    /// assert_eq!(center_idx, None);
    /// ```
    pub fn find_channel(&self, channel: ChannelId) -> Option<usize> {
        // There is a C function for this but it seems simpler and safer to do it in Rust.
        self.channels.iter().position(|&c| c == channel)
    }

    /// Populate the name field with the built-in name if this layout matches one of the built-in layouts.
    /// Returns `true` if it did.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut layout = soundio::ChannelLayout {
    ///     name: "".to_string(),
    ///     channels: vec![soundio::ChannelId::FrontLeft, soundio::ChannelId::FrontRight],
    /// };
    ///
    /// assert_eq!(layout.detect_builtin(), true);
    /// assert_eq!(layout.name, "Stereo".to_string());
    /// ```
    pub fn detect_builtin(&mut self) -> bool {
        let mut raw_layout = raw::SoundIoChannelLayout::from(self.clone());

        if unsafe { raw::soundio_channel_layout_detect_builtin(&mut raw_layout) } != 0 {
            self.name = latin1_to_string(raw_layout.name);
            return true;
        }
        false
    }

    /// Sort a set of `ChannelLayouts` by channel count, descending. The content of the channels
    /// and the layout name are ignored; only the number of channels is significant.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut layouts = soundio::ChannelLayout::get_all_builtin();
    ///
    /// soundio::ChannelLayout::sort(&mut layouts);
    ///
    /// for i in 0..layouts.len()-1 {
    ///     assert!(layouts[i+1].channels.len() >= layouts[i].channels.len());
    /// }
    /// ```
    pub fn sort(layouts: &mut [ChannelLayout]) {
        // This is easier to do in Rust. It literally sorts by channel count.
        layouts.sort_by(|a, b| a.channels.len().cmp(&b.channels.len()));
    }
}

/// Equality testing for layouts. The channels must be the same
/// IDs and in the same order. The layout name is ignored.
///
/// # Examples
///
/// ```
/// let layout_a = soundio::ChannelLayout {
///     name: "unimportant".to_string(),
///     channels: vec![soundio::ChannelId::FrontLeft, soundio::ChannelId::FrontRight],
/// };
/// let layout_b = soundio::ChannelLayout {
///     name: "doesn't matter".to_string(),
///     channels: vec![soundio::ChannelId::FrontLeft, soundio::ChannelId::FrontRight],
/// };
///
/// assert_eq!(layout_a, layout_b);
/// ```
impl PartialEq for ChannelLayout {
    fn eq(&self, other: &ChannelLayout) -> bool {
        self.channels == other.channels
    }
}
impl Eq for ChannelLayout {}

/// Built-in channel layouts for convenience.
/// These can be used with `ChannelLayout::get_builtin()`.
///
/// Some values are prepended with `C` where they started with a digit. For example
/// `C2Point1` means 2.1 and so on.
///
/// # Examples
///
/// ```
/// println!("Stereo Layout: {:?}", soundio::ChannelLayout::get_builtin(soundio::ChannelLayoutId::Stereo));
/// ```
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum ChannelLayoutId {
    Mono,
    Stereo,
    C2Point1, // Ignore the 'C'. It's just there because it can't start with a number.
    C3Point0,
    C3Point0Back,
    C3Point1,
    C4Point0,
    Quad,
    QuadSide,
    C4Point1,
    C5Point0Back,
    C5Point0Side,
    C5Point1,
    C5Point1Back,
    C6Point0Side,
    C6Point0Front,
    Hexagonal,
    C6Point1,
    C6Point1Back,
    C6Point1Front,
    C7Point0,
    C7Point0Front,
    C7Point1,
    C7Point1Wide,
    C7Point1WideBack,
    Octagonal,
}

impl From<raw::SoundIoChannelLayoutId> for ChannelLayoutId {
    fn from(channel_layout_id: raw::SoundIoChannelLayoutId) -> ChannelLayoutId {
        match channel_layout_id {
            raw::SoundIoChannelLayoutId::SoundIoChannelLayoutIdMono => ChannelLayoutId::Mono,
            raw::SoundIoChannelLayoutId::SoundIoChannelLayoutIdStereo => ChannelLayoutId::Stereo,
            raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId2Point1 => ChannelLayoutId::C2Point1,
            raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId3Point0 => ChannelLayoutId::C3Point0,
            raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId3Point0Back => {
                ChannelLayoutId::C3Point0Back
            }
            raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId3Point1 => ChannelLayoutId::C3Point1,
            raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId4Point0 => ChannelLayoutId::C4Point0,
            raw::SoundIoChannelLayoutId::SoundIoChannelLayoutIdQuad => ChannelLayoutId::Quad,
            raw::SoundIoChannelLayoutId::SoundIoChannelLayoutIdQuadSide => {
                ChannelLayoutId::QuadSide
            }
            raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId4Point1 => ChannelLayoutId::C4Point1,
            raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId5Point0Back => {
                ChannelLayoutId::C5Point0Back
            }
            raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId5Point0Side => {
                ChannelLayoutId::C5Point0Side
            }
            raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId5Point1 => ChannelLayoutId::C5Point1,
            raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId5Point1Back => {
                ChannelLayoutId::C5Point1Back
            }
            raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId6Point0Side => {
                ChannelLayoutId::C6Point0Side
            }
            raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId6Point0Front => {
                ChannelLayoutId::C6Point0Front
            }
            raw::SoundIoChannelLayoutId::SoundIoChannelLayoutIdHexagonal => {
                ChannelLayoutId::Hexagonal
            }
            raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId6Point1 => ChannelLayoutId::C6Point1,
            raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId6Point1Back => {
                ChannelLayoutId::C6Point1Back
            }
            raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId6Point1Front => {
                ChannelLayoutId::C6Point1Front
            }
            raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId7Point0 => ChannelLayoutId::C7Point0,
            raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId7Point0Front => {
                ChannelLayoutId::C7Point0Front
            }
            raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId7Point1 => ChannelLayoutId::C7Point1,
            raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId7Point1Wide => {
                ChannelLayoutId::C7Point1Wide
            }
            raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId7Point1WideBack => {
                ChannelLayoutId::C7Point1WideBack
            }
            raw::SoundIoChannelLayoutId::SoundIoChannelLayoutIdOctagonal => {
                ChannelLayoutId::Octagonal
            }
        }
    }
}

impl From<ChannelLayoutId> for raw::SoundIoChannelLayoutId {
    fn from(channel_layout_id: ChannelLayoutId) -> raw::SoundIoChannelLayoutId {
        match channel_layout_id {
            ChannelLayoutId::Mono => raw::SoundIoChannelLayoutId::SoundIoChannelLayoutIdMono,
            ChannelLayoutId::Stereo => raw::SoundIoChannelLayoutId::SoundIoChannelLayoutIdStereo,
            ChannelLayoutId::C2Point1 => raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId2Point1,
            ChannelLayoutId::C3Point0 => raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId3Point0,
            ChannelLayoutId::C3Point0Back => {
                raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId3Point0Back
            }
            ChannelLayoutId::C3Point1 => raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId3Point1,
            ChannelLayoutId::C4Point0 => raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId4Point0,
            ChannelLayoutId::Quad => raw::SoundIoChannelLayoutId::SoundIoChannelLayoutIdQuad,
            ChannelLayoutId::QuadSide => {
                raw::SoundIoChannelLayoutId::SoundIoChannelLayoutIdQuadSide
            }
            ChannelLayoutId::C4Point1 => raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId4Point1,
            ChannelLayoutId::C5Point0Back => {
                raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId5Point0Back
            }
            ChannelLayoutId::C5Point0Side => {
                raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId5Point0Side
            }
            ChannelLayoutId::C5Point1 => raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId5Point1,
            ChannelLayoutId::C5Point1Back => {
                raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId5Point1Back
            }
            ChannelLayoutId::C6Point0Side => {
                raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId6Point0Side
            }
            ChannelLayoutId::C6Point0Front => {
                raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId6Point0Front
            }
            ChannelLayoutId::Hexagonal => {
                raw::SoundIoChannelLayoutId::SoundIoChannelLayoutIdHexagonal
            }
            ChannelLayoutId::C6Point1 => raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId6Point1,
            ChannelLayoutId::C6Point1Back => {
                raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId6Point1Back
            }
            ChannelLayoutId::C6Point1Front => {
                raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId6Point1Front
            }
            ChannelLayoutId::C7Point0 => raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId7Point0,
            ChannelLayoutId::C7Point0Front => {
                raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId7Point0Front
            }
            ChannelLayoutId::C7Point1 => raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId7Point1,
            ChannelLayoutId::C7Point1Wide => {
                raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId7Point1Wide
            }
            ChannelLayoutId::C7Point1WideBack => {
                raw::SoundIoChannelLayoutId::SoundIoChannelLayoutId7Point1WideBack
            }
            ChannelLayoutId::Octagonal => {
                raw::SoundIoChannelLayoutId::SoundIoChannelLayoutIdOctagonal
            }
        }
    }
}