maudio 0.1.3

Rust bindings to the miniaudio library
Documentation
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
//! Channel configuration and channel-related audio utilities.
use maudio_sys::ffi as sys;

use crate::{ErrorKinds, MaudioError};

/// Channel mixing strategy used by the channel converter when a direct 1:1 channel-position mapping
/// is not possible (or when channel counts differ).
///
/// In miniaudio, if the input and output channel counts are the same *and* the channel maps contain
/// the same channel positions (just in a different order), channels are simply shuffled.
/// If there is no 1:1 mapping of channel positions, or the channel counts differ, channels are mixed
/// according to a `ChannelMixMode` configured via `ma_channel_converter_config`.
///
/// Notes from miniaudio’s channel mapping rules:
/// - **Mono → multi-channel**: the mono channel is copied to each output channel.
/// - **Multi-channel → mono**: all channels are averaged and copied to the mono channel.
/// - For more complex conversions, one of the mix modes below is used.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(C)]
pub enum ChannelMixMode {
    /// Rectangular mixing.
    ///
    /// Miniaudio comment: “Simple averaging based on the plane(s) the channel is sitting on.”
    ///
    /// This mode uses spatial locality based on a rectangle to compute a simple distribution
    /// between input and output channel positions. Conceptually, imagine sitting in the middle
    /// of a room with speakers on the walls representing channel positions.
    Rectangular,
    /// Simple mixing.
    ///
    /// Miniaudio comment: “Drop excess channels; zeroed out extra channels.”
    ///
    /// Excess input channels are dropped, and any extra output channels are filled with silence.
    /// Example:
    /// - 4 → 2: channels 3 and 4 are dropped
    /// - 2 → 4: channels 3 and 4 are set to silence
    Simple,
    /// Custom weights mixing.
    ///
    /// Miniaudio comment: “Use custom weights specified in ma_channel_converter_config.”
    ///
    /// This mode applies user-defined weights configured on the channel converter config.
    CustomWeights,
    /// Default mixing mode.
    ///
    /// In miniaudio this maps to `Rectangular`.
    Default,
}

impl From<ChannelMixMode> for sys::ma_channel_mix_mode {
    fn from(value: ChannelMixMode) -> Self {
        match value {
            ChannelMixMode::Rectangular => sys::ma_channel_mix_mode_ma_channel_mix_mode_rectangular,
            ChannelMixMode::Simple => sys::ma_channel_mix_mode_ma_channel_mix_mode_simple,
            ChannelMixMode::CustomWeights => {
                sys::ma_channel_mix_mode_ma_channel_mix_mode_custom_weights
            }
            ChannelMixMode::Default => sys::ma_channel_mix_mode_ma_channel_mix_mode_default,
        }
    }
}

impl TryFrom<sys::ma_channel_mix_mode> for ChannelMixMode {
    type Error = MaudioError;

    fn try_from(value: sys::ma_channel_mix_mode) -> Result<Self, Self::Error> {
        match value {
            sys::ma_channel_mix_mode_ma_channel_mix_mode_rectangular => {
                Ok(ChannelMixMode::Rectangular)
            }
            sys::ma_channel_mix_mode_ma_channel_mix_mode_simple => Ok(ChannelMixMode::Simple),
            sys::ma_channel_mix_mode_ma_channel_mix_mode_custom_weights => {
                Ok(ChannelMixMode::CustomWeights)
            }
            other => Err(MaudioError::new_ma_error(ErrorKinds::unknown_enum::<
                ChannelMixMode,
            >(other as i64))),
        }
    }
}

/// Standard channel ordering conventions.
///
/// This enum specifies how audio channels are ordered in memory for
/// multi-channel audio streams. Different platforms, file formats, and
/// APIs use different canonical layouts.
///
/// These variants directly correspond to miniaudio’s
/// `ma_standard_channel_map`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(C)]
pub enum ChannelMap {
    /// Microsoft channel ordering.
    ///
    /// This is the default channel layout used by Windows audio APIs
    /// (e.g. WASAPI) and is the default channel map in miniaudio.
    ///
    /// Typical ordering:
    /// - Mono: `M`
    /// - Stereo: `L, R`
    /// - Surround: `L, R, C, LFE, SL, SR`
    Microsoft,

    /// ALSA (Advanced Linux Sound Architecture) channel ordering.
    ///
    /// Used by ALSA on Linux systems. The ordering differs slightly from
    /// Microsoft layouts for certain surround configurations.
    Alsa,

    /// RFC 3551 channel ordering.
    ///
    /// Based on AIFF channel conventions and defined in RFC 3551
    /// (RTP payload format for audio and video conferences).
    Rfc3551,

    /// FLAC channel ordering.
    ///
    /// Used by the FLAC audio format. Channels follow the ordering defined
    /// by the FLAC specification.
    Flac,

    /// Vorbis channel ordering.
    ///
    /// Used by the Vorbis audio format. This ordering is also commonly used
    /// by other container formats that adopt Vorbis conventions.
    Vorbis,

    /// FreeBSD `sound(4)` channel ordering.
    ///
    /// Used by FreeBSD’s legacy audio subsystem.
    Sound4,

    /// sndio channel ordering.
    ///
    /// Used by the sndio audio system.
    /// See: <https://www.sndio.org/tips.html>
    Sndio,

    /// Web Audio API channel ordering.
    ///
    /// Defined by the Web Audio API specification:
    /// <https://webaudio.github.io/web-audio-api/#ChannelOrdering>
    ///
    /// Only 1, 2, 4, and 6 channel layouts are explicitly defined by the
    /// specification, but additional layouts can be inferred using
    /// logical assumptions.
    ///
    /// In miniaudio, this maps to the same ordering as `Flac`.
    Webaudio,

    /// Default channel ordering.
    ///
    /// This maps to `Microsoft`, which is the default channel map used
    /// throughout miniaudio when no explicit channel map is specified.
    Default,
}

impl From<ChannelMap> for sys::ma_standard_channel_map {
    fn from(value: ChannelMap) -> Self {
        match value {
            ChannelMap::Microsoft => sys::ma_standard_channel_map_ma_standard_channel_map_microsoft,
            ChannelMap::Alsa => sys::ma_standard_channel_map_ma_standard_channel_map_alsa,
            ChannelMap::Rfc3551 => sys::ma_standard_channel_map_ma_standard_channel_map_rfc3551,
            ChannelMap::Flac => sys::ma_standard_channel_map_ma_standard_channel_map_flac,
            ChannelMap::Vorbis => sys::ma_standard_channel_map_ma_standard_channel_map_vorbis,
            ChannelMap::Sound4 => sys::ma_standard_channel_map_ma_standard_channel_map_sound4,
            ChannelMap::Sndio => sys::ma_standard_channel_map_ma_standard_channel_map_sndio,
            ChannelMap::Webaudio => sys::ma_standard_channel_map_ma_standard_channel_map_webaudio,
            ChannelMap::Default => sys::ma_standard_channel_map_ma_standard_channel_map_default,
        }
    }
}

impl TryFrom<sys::ma_standard_channel_map> for ChannelMap {
    type Error = MaudioError;

    fn try_from(value: sys::ma_standard_channel_map) -> Result<Self, Self::Error> {
        match value {
            sys::ma_standard_channel_map_ma_standard_channel_map_microsoft => {
                Ok(ChannelMap::Microsoft)
            }
            sys::ma_standard_channel_map_ma_standard_channel_map_alsa => Ok(ChannelMap::Alsa),
            sys::ma_standard_channel_map_ma_standard_channel_map_rfc3551 => Ok(ChannelMap::Rfc3551),
            sys::ma_standard_channel_map_ma_standard_channel_map_flac => Ok(ChannelMap::Flac),
            sys::ma_standard_channel_map_ma_standard_channel_map_vorbis => Ok(ChannelMap::Vorbis),
            sys::ma_standard_channel_map_ma_standard_channel_map_sound4 => Ok(ChannelMap::Sound4),
            sys::ma_standard_channel_map_ma_standard_channel_map_sndio => Ok(ChannelMap::Sndio),
            other => Err(MaudioError::new_ma_error(ErrorKinds::unknown_enum::<
                ChannelMap,
            >(other as i64))),
        }
    }
}

#[repr(transparent)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Channel(pub sys::ma_channel);

impl Channel {
    #[inline]
    pub const fn as_raw(self) -> sys::ma_channel {
        self.0
    }

    #[inline]
    pub const fn from_raw(v: sys::ma_channel) -> Self {
        Self(v)
    }
}

#[repr(u8)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum ChannelPosition {
    None,
    Mono,
    FrontLeft,
    FrontRight,
    FrontCenter,
    Lfe,
    BackLeft,
    BackRight,
    FrontLeftCenter,
    FrontRightCenter,
    BackCenter,
    SideLeft,
    SideRight,
    TopCenter,
    TopFrontLeft,
    TopFrontCenter,
    TopFrontRight,
    TopBackLeft,
    TopBackCenter,
    TopBackRight,
    Aux0,
    Aux1,
    Aux2,
    Aux3,
    Aux4,
    Aux5,
    Aux6,
    Aux7,
    Aux8,
    Aux9,
    Aux10,
    Aux11,
    Aux12,
    Aux13,
    Aux14,
    Aux15,
    Aux16,
    Aux17,
    Aux18,
    Aux19,
    Aux20,
    Aux21,
    Aux22,
    Aux23,
    Aux24,
    Aux25,
    Aux26,
    Aux27,
    Aux28,
    Aux29,
    Aux30,
    Aux31,
}

impl TryFrom<sys::ma_channel> for ChannelPosition {
    type Error = MaudioError;

    fn try_from(v: sys::ma_channel) -> Result<Self, Self::Error> {
        match v {
            0 => Ok(Self::None),
            1 => Ok(Self::Mono),
            2 => Ok(Self::FrontLeft),
            3 => Ok(Self::FrontRight),
            4 => Ok(Self::FrontCenter),
            5 => Ok(Self::Lfe),
            6 => Ok(Self::BackLeft),
            7 => Ok(Self::BackRight),
            8 => Ok(Self::FrontLeftCenter),
            9 => Ok(Self::FrontRightCenter),
            10 => Ok(Self::BackCenter),
            11 => Ok(Self::SideLeft),
            12 => Ok(Self::SideRight),
            13 => Ok(Self::TopCenter),
            14 => Ok(Self::TopFrontLeft),
            15 => Ok(Self::TopFrontCenter),
            16 => Ok(Self::TopFrontRight),
            17 => Ok(Self::TopBackLeft),
            18 => Ok(Self::TopBackCenter),
            19 => Ok(Self::TopBackRight),
            20 => Ok(Self::Aux0),
            21 => Ok(Self::Aux1),
            22 => Ok(Self::Aux2),
            23 => Ok(Self::Aux3),
            24 => Ok(Self::Aux4),
            25 => Ok(Self::Aux5),
            26 => Ok(Self::Aux6),
            27 => Ok(Self::Aux7),
            28 => Ok(Self::Aux8),
            29 => Ok(Self::Aux9),
            30 => Ok(Self::Aux10),
            31 => Ok(Self::Aux11),
            32 => Ok(Self::Aux12),
            33 => Ok(Self::Aux13),
            34 => Ok(Self::Aux14),
            35 => Ok(Self::Aux15),
            36 => Ok(Self::Aux16),
            37 => Ok(Self::Aux17),
            38 => Ok(Self::Aux18),
            39 => Ok(Self::Aux19),
            40 => Ok(Self::Aux20),
            41 => Ok(Self::Aux21),
            42 => Ok(Self::Aux22),
            43 => Ok(Self::Aux23),
            44 => Ok(Self::Aux24),
            45 => Ok(Self::Aux25),
            46 => Ok(Self::Aux26),
            47 => Ok(Self::Aux27),
            48 => Ok(Self::Aux28),
            49 => Ok(Self::Aux29),
            50 => Ok(Self::Aux30),
            51 => Ok(Self::Aux31),
            _ => Err(MaudioError::new_ma_error(ErrorKinds::unknown_enum::<
                ChannelPosition,
            >(v as i64))),
        }
    }
}

impl TryFrom<Channel> for ChannelPosition {
    type Error = MaudioError;

    #[inline]
    fn try_from(c: Channel) -> Result<Self, Self::Error> {
        ChannelPosition::try_from(c.0)
    }
}

impl From<ChannelPosition> for Channel {
    #[inline]
    fn from(p: ChannelPosition) -> Self {
        Channel(p as sys::ma_channel)
    }
}

impl From<ChannelPosition> for sys::ma_channel {
    #[inline]
    fn from(p: ChannelPosition) -> Self {
        p as sys::ma_channel
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{sys, MaError};

    #[test]
    fn test_channel_map_from_rust_to_sys_variants() {
        // Exact-name variants.
        assert_eq!(
            sys::ma_standard_channel_map::from(ChannelMap::Microsoft),
            sys::ma_standard_channel_map_ma_standard_channel_map_microsoft
        );
        assert_eq!(
            sys::ma_standard_channel_map::from(ChannelMap::Alsa),
            sys::ma_standard_channel_map_ma_standard_channel_map_alsa
        );
        assert_eq!(
            sys::ma_standard_channel_map::from(ChannelMap::Rfc3551),
            sys::ma_standard_channel_map_ma_standard_channel_map_rfc3551
        );
        assert_eq!(
            sys::ma_standard_channel_map::from(ChannelMap::Flac),
            sys::ma_standard_channel_map_ma_standard_channel_map_flac
        );
        assert_eq!(
            sys::ma_standard_channel_map::from(ChannelMap::Vorbis),
            sys::ma_standard_channel_map_ma_standard_channel_map_vorbis
        );
        assert_eq!(
            sys::ma_standard_channel_map::from(ChannelMap::Sound4),
            sys::ma_standard_channel_map_ma_standard_channel_map_sound4
        );
        assert_eq!(
            sys::ma_standard_channel_map::from(ChannelMap::Sndio),
            sys::ma_standard_channel_map_ma_standard_channel_map_sndio
        );

        // Aliases from miniaudio:
        // - webaudio = flac
        // - default = microsoft
        assert_eq!(
            sys::ma_standard_channel_map::from(ChannelMap::Webaudio),
            sys::ma_standard_channel_map_ma_standard_channel_map_webaudio
        );
        assert_eq!(
            sys::ma_standard_channel_map_ma_standard_channel_map_webaudio,
            sys::ma_standard_channel_map_ma_standard_channel_map_flac
        );

        assert_eq!(
            sys::ma_standard_channel_map::from(ChannelMap::Default),
            sys::ma_standard_channel_map_ma_standard_channel_map_default
        );
        assert_eq!(
            sys::ma_standard_channel_map_ma_standard_channel_map_default,
            sys::ma_standard_channel_map_ma_standard_channel_map_microsoft
        );
    }

    #[test]
    fn test_channel_map_try_from_sys_to_rust_variants() {
        // Most should round-trip directly.
        assert_eq!(
            ChannelMap::try_from(sys::ma_standard_channel_map_ma_standard_channel_map_microsoft)
                .unwrap(),
            ChannelMap::Microsoft
        );
        assert_eq!(
            ChannelMap::try_from(sys::ma_standard_channel_map_ma_standard_channel_map_alsa)
                .unwrap(),
            ChannelMap::Alsa
        );
        assert_eq!(
            ChannelMap::try_from(sys::ma_standard_channel_map_ma_standard_channel_map_rfc3551)
                .unwrap(),
            ChannelMap::Rfc3551
        );
        assert_eq!(
            ChannelMap::try_from(sys::ma_standard_channel_map_ma_standard_channel_map_flac)
                .unwrap(),
            ChannelMap::Flac
        );
        assert_eq!(
            ChannelMap::try_from(sys::ma_standard_channel_map_ma_standard_channel_map_vorbis)
                .unwrap(),
            ChannelMap::Vorbis
        );
        assert_eq!(
            ChannelMap::try_from(sys::ma_standard_channel_map_ma_standard_channel_map_sound4)
                .unwrap(),
            ChannelMap::Sound4
        );
        assert_eq!(
            ChannelMap::try_from(sys::ma_standard_channel_map_ma_standard_channel_map_sndio)
                .unwrap(),
            ChannelMap::Sndio
        );

        // Alias semantics:
        // miniaudio defines:
        //   ma_standard_channel_map_webaudio = ma_standard_channel_map_flac
        //
        // That means the *sys value* for "webaudio" is numerically identical to FLAC.
        // In a TryFrom mapping, you can only pick one Rust variant for that number.
        //
        // The usual choice is to map that numeric value back to `ChannelMap::Flac`.
        // If your TryFrom intentionally maps it to `Webaudio` instead, change this assertion.
        let from_webaudio =
            ChannelMap::try_from(sys::ma_standard_channel_map_ma_standard_channel_map_webaudio)
                .unwrap();
        assert!(
            matches!(from_webaudio, ChannelMap::Flac | ChannelMap::Webaudio),
            "Expected FLAC/WEBAUDIO alias to map to Flac or Webaudio; got {from_webaudio:?}"
        );

        // Default is also an alias:
        //   ma_standard_channel_map_default = ma_standard_channel_map_microsoft
        //
        // Same ambiguity as above: the numeric value is identical. Accept either.
        let from_default =
            ChannelMap::try_from(sys::ma_standard_channel_map_ma_standard_channel_map_default)
                .unwrap();
        assert!(
            matches!(from_default, ChannelMap::Microsoft | ChannelMap::Default),
            "Expected DEFAULT/MICROSOFT alias to map to Microsoft or Default; got {from_default:?}"
        );
    }

    #[test]
    fn test_channel_map_try_from_invalid_returns_error() {
        let invalid: sys::ma_standard_channel_map = 0x7FFF as sys::ma_standard_channel_map;

        let err = ChannelMap::try_from(invalid).unwrap_err();
        assert_eq!(err, MaError(sys::ma_result_MA_ERROR));
    }

    #[test]
    fn test_channel_mix_mode_from_rust_to_sys_variants() {
        assert_eq!(
            sys::ma_channel_mix_mode::from(ChannelMixMode::Rectangular),
            sys::ma_channel_mix_mode_ma_channel_mix_mode_rectangular
        );
        assert_eq!(
            sys::ma_channel_mix_mode::from(ChannelMixMode::Simple),
            sys::ma_channel_mix_mode_ma_channel_mix_mode_simple
        );
        assert_eq!(
            sys::ma_channel_mix_mode::from(ChannelMixMode::CustomWeights),
            sys::ma_channel_mix_mode_ma_channel_mix_mode_custom_weights
        );

        // Alias from miniaudio:
        // - default = rectangular
        assert_eq!(
            sys::ma_channel_mix_mode::from(ChannelMixMode::Default),
            sys::ma_channel_mix_mode_ma_channel_mix_mode_default
        );
        assert_eq!(
            sys::ma_channel_mix_mode_ma_channel_mix_mode_default,
            sys::ma_channel_mix_mode_ma_channel_mix_mode_rectangular
        );
    }

    #[test]
    fn test_channel_mix_mode_try_from_sys_to_rust_variants() {
        assert_eq!(
            ChannelMixMode::try_from(sys::ma_channel_mix_mode_ma_channel_mix_mode_rectangular)
                .unwrap(),
            ChannelMixMode::Rectangular
        );
        assert_eq!(
            ChannelMixMode::try_from(sys::ma_channel_mix_mode_ma_channel_mix_mode_simple).unwrap(),
            ChannelMixMode::Simple
        );
        assert_eq!(
            ChannelMixMode::try_from(sys::ma_channel_mix_mode_ma_channel_mix_mode_custom_weights)
                .unwrap(),
            ChannelMixMode::CustomWeights
        );

        // Default is an alias for rectangular in miniaudio; accept either mapping choice.
        let from_default =
            ChannelMixMode::try_from(sys::ma_channel_mix_mode_ma_channel_mix_mode_default).unwrap();
        assert!(
            matches!(
                from_default,
                ChannelMixMode::Rectangular | ChannelMixMode::Default
            ),
            "Expected DEFAULT/RECTANGULAR alias to map to Rectangular or Default; got {from_default:?}"
        );
    }

    #[test]
    fn test_channel_mix_mode_try_from_invalid_returns_error() {
        let invalid: sys::ma_standard_channel_map = 0x7FFF as sys::ma_standard_channel_map;

        let err = ChannelMixMode::try_from(invalid).unwrap_err();
        assert_eq!(err, MaError(sys::ma_result_MA_ERROR));
    }
}