usbdac-core 0.1.7

Direct USB DAC playback for Rust audio applications on Android and Linux, with PCM, DoP, native DSD and bit-perfect reporting.
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
//! Explicit Pioneer DJM routing choices, independent of the USB backend.
//!
//! Values and option order follow Linux ALSA `sound/usb/mixer_quirks.c`
//! (`snd_djm_opts_*` and `snd_djm_get_label_*`). These tables describe writes;
//! they do not report mixer state or select defaults on connection.

/// DJM models with both source-proven routing requests and PCM transport support.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DjmModel {
    Djm250Mk2,
    Djm450,
    Djm750,
    Djm850,
    Djm900Nxs2,
    Djm750Mk2,
    DjmV10,
}

/// A routing control. Pair numbers are one-based USB stereo pairs.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DjmControlKey {
    CaptureLevel,
    CaptureSource(u8),
    PlaybackDestination(u8),
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DjmOption {
    /// Zero-based option ordinal, scoped to this control and model.
    pub id: u8,
    pub label: &'static str,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DjmControl {
    pub key: DjmControlKey,
    pub options: Vec<DjmOption>,
}

/// Available choices for a proven model in an active USB configuration.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DjmRoutingProfile {
    pub model: DjmModel,
    pub configuration: u8,
    pub controls: Vec<DjmControl>,
}

// The upper byte is the USB stereo pair. Share only identical lower-byte
// sequences; their ordering is the ALSA enumeration ordering.
const TWO_CHANNEL: &[u8] = &[0x03, 0x00, 0x06, 0x07, 0x08, 0x09, 0x0d, 0x0a];
const TWO_MASTER: &[u8] = &[0x0a, 0x11, 0x12, 0x07, 0x08, 0x09, 0x0d];
const CD_PHONO: &[u8] = &[0x01, 0x03, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0f];
const LINE_CD: &[u8] = &[0x00, 0x01, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0f];
const LINE_PHONO: &[u8] = &[0x00, 0x03, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0f];
const DIGITAL_CHANNEL: &[u8] = &[0x00, 0x02, 0x03, 0x06, 0x07, 0x08, 0x09, 0x0a];
const FOUR_MASTER: &[u8] = &[0x07, 0x08, 0x09, 0x0a, 0x11, 0x12, 0x13, 0x14];
const V10_PHONO: &[u8] = &[
    0x03, 0x00, 0x02, 0x06, 0x10, 0x07, 0x08, 0x09, 0x0a, 0x21, 0x22,
];
const V10_LINE: &[u8] = &[0x00, 0x02, 0x06, 0x10, 0x07, 0x08, 0x09, 0x0a, 0x21, 0x22];
const PLAYBACK: &[u8] = &[0x00, 0x01, 0x04];
const LEVEL_COMMON: &[&str] = &["-19dB", "-15dB", "-10dB", "-5dB"];
const LEVEL_V10: &[&str] = &["+15dB", "+12dB", "+9dB", "+6dB", "+3dB", "0dB"];

impl DjmModel {
    pub fn key(self) -> &'static str {
        match self {
            Self::Djm250Mk2 => "djm-250mk2",
            Self::Djm450 => "djm-450",
            Self::Djm750 => "djm-750",
            Self::Djm850 => "djm-850",
            Self::Djm900Nxs2 => "djm-900nxs2",
            Self::Djm750Mk2 => "djm-750mk2",
            Self::DjmV10 => "djm-v10",
        }
    }

    pub fn from_key(key: &str) -> Option<Self> {
        match key {
            "djm-250mk2" => Some(Self::Djm250Mk2),
            "djm-450" => Some(Self::Djm450),
            "djm-750" => Some(Self::Djm750),
            "djm-850" => Some(Self::Djm850),
            "djm-900nxs2" => Some(Self::Djm900Nxs2),
            "djm-750mk2" => Some(Self::Djm750Mk2),
            "djm-v10" => Some(Self::DjmV10),
            _ => None,
        }
    }

    pub(crate) fn from_usb_id(vendor: u16, product: u16) -> Option<Self> {
        match (vendor, product) {
            (0x2b73, 0x0017) => Some(Self::Djm250Mk2),
            (0x2b73, 0x0013) => Some(Self::Djm450),
            (0x08e4, 0x017f) => Some(Self::Djm750),
            (0x08e4, 0x0163) => Some(Self::Djm850),
            (0x2b73, 0x000a) => Some(Self::Djm900Nxs2),
            (0x2b73, 0x001b) => Some(Self::Djm750Mk2),
            (0x2b73, 0x0034) => Some(Self::DjmV10),
            _ => None,
        }
    }

    fn levels(self) -> &'static [&'static str] {
        if self == Self::DjmV10 {
            LEVEL_V10
        } else {
            LEVEL_COMMON
        }
    }

    fn capture_sources(self, pair: u8) -> Option<&'static [u8]> {
        match (self, pair) {
            (Self::Djm250Mk2 | Self::Djm450, 1..=2) => Some(TWO_CHANNEL),
            (Self::Djm250Mk2 | Self::Djm450, 3) => Some(TWO_MASTER),
            (Self::Djm750, 1 | 4) => Some(CD_PHONO),
            (Self::Djm750 | Self::Djm850, 2..=3) => Some(LINE_CD),
            (Self::Djm850, 1 | 4) => Some(LINE_PHONO),
            (Self::Djm900Nxs2 | Self::Djm750Mk2, 1..=4) => Some(DIGITAL_CHANNEL),
            (Self::Djm900Nxs2 | Self::Djm750Mk2, 5) => Some(FOUR_MASTER),
            (Self::DjmV10, 1 | 3 | 4 | 6) => Some(V10_PHONO),
            (Self::DjmV10, 2 | 5) => Some(V10_LINE),
            _ => None,
        }
    }

    fn has_playback(self) -> bool {
        matches!(self, Self::Djm250Mk2 | Self::Djm450 | Self::Djm750Mk2)
    }

    pub fn controls(self) -> Vec<DjmControl> {
        let mut controls = vec![DjmControl {
            key: DjmControlKey::CaptureLevel,
            options: self
                .levels()
                .iter()
                .enumerate()
                .map(|(id, &label)| DjmOption {
                    id: id as u8,
                    label,
                })
                .collect(),
        }];
        for pair in 1..=6 {
            if let Some(sources) = self.capture_sources(pair) {
                controls.push(DjmControl {
                    key: DjmControlKey::CaptureSource(pair),
                    options: sources
                        .iter()
                        .enumerate()
                        .map(|(id, &value)| DjmOption {
                            id: id as u8,
                            label: self.capture_label(value),
                        })
                        .collect(),
                });
            }
        }
        if self.has_playback() {
            for pair in 1..=3 {
                controls.push(DjmControl {
                    key: DjmControlKey::PlaybackDestination(pair),
                    options: ["Ch1", "Ch2", "Aux"]
                        .into_iter()
                        .enumerate()
                        .map(|(id, label)| DjmOption {
                            id: id as u8,
                            label,
                        })
                        .collect(),
                });
            }
        }
        controls
    }

    /// Resolve an explicitly selected choice to `(wValue, wIndex)`.
    pub(crate) fn request(self, key: DjmControlKey, option: u8) -> Option<(u16, u16)> {
        match key {
            DjmControlKey::CaptureLevel => self
                .levels()
                .get(usize::from(option))
                .map(|_| (u16::from(option) << 8, 0x8003)),
            DjmControlKey::CaptureSource(pair) => self
                .capture_sources(pair)?
                .get(usize::from(option))
                .map(|&value| ((u16::from(pair) << 8) | u16::from(value), 0x8002)),
            DjmControlKey::PlaybackDestination(pair)
                if self.has_playback() && (1..=3).contains(&pair) =>
            {
                PLAYBACK
                    .get(usize::from(option))
                    .map(|&value| ((u16::from(pair) << 8) | u16::from(value), 0x8016))
            }
            _ => None,
        }
    }

    fn capture_label(self, value: u8) -> &'static str {
        match (self, value) {
            (Self::Djm850, 0x00) => "Control Tone CD/LINE",
            (Self::Djm850, 0x01) => "Control Tone LINE",
            (_, 0x00) => "Control Tone LINE",
            (_, 0x01) => "Control Tone CD/LINE",
            (_, 0x02) => "Control Tone DIGITAL",
            (_, 0x03) => "Control Tone PHONO",
            (_, 0x06) => "Post Fader",
            (_, 0x07) => "Cross Fader A",
            (_, 0x08) => "Cross Fader B",
            (_, 0x09) => "Mic",
            (_, 0x0a) => "Rec Out",
            (_, 0x0d) => "Aux",
            (_, 0x0f) => "None",
            (_, 0x10) => "FX SEND",
            (_, 0x11) => "Post Fader Ch1",
            (_, 0x12) => "Post Fader Ch2",
            (_, 0x13) => "Post Fader Ch3",
            (_, 0x14) => "Post Fader Ch4",
            (_, 0x21) => "EXT1 SEND",
            (_, 0x22) => "EXT2 SEND",
            _ => unreachable!("capture option table has no ALSA label"),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn model_identities_and_control_order() {
        let cases = [
            (DjmModel::Djm250Mk2, "djm-250mk2", 0x2b73, 0x0017, 3, 3),
            (DjmModel::Djm450, "djm-450", 0x2b73, 0x0013, 3, 3),
            (DjmModel::Djm750, "djm-750", 0x08e4, 0x017f, 4, 0),
            (DjmModel::Djm850, "djm-850", 0x08e4, 0x0163, 4, 0),
            (DjmModel::Djm900Nxs2, "djm-900nxs2", 0x2b73, 0x000a, 5, 0),
            (DjmModel::Djm750Mk2, "djm-750mk2", 0x2b73, 0x001b, 5, 3),
            (DjmModel::DjmV10, "djm-v10", 0x2b73, 0x0034, 6, 0),
        ];
        for (model, key, vendor, product, inputs, outputs) in cases {
            assert_eq!(model.key(), key);
            assert_eq!(DjmModel::from_key(key), Some(model));
            assert_eq!(DjmModel::from_usb_id(vendor, product), Some(model));
            assert_eq!(DjmModel::from_usb_id(0xffff, product), None);
            let controls = model.controls();
            let expected: Vec<_> = std::iter::once(DjmControlKey::CaptureLevel)
                .chain((1..=inputs).map(DjmControlKey::CaptureSource))
                .chain((1..=outputs).map(DjmControlKey::PlaybackDestination))
                .collect();
            assert_eq!(controls.iter().map(|c| c.key).collect::<Vec<_>>(), expected);
            for control in controls {
                for (ordinal, option) in control.options.iter().enumerate() {
                    assert_eq!(option.id, ordinal as u8);
                    assert!(model.request(control.key, option.id).is_some());
                    assert!(!option.label.is_empty());
                }
                assert_eq!(
                    model.request(control.key, control.options.len() as u8),
                    None
                );
            }
            assert_eq!(model.request(DjmControlKey::CaptureSource(0), 0), None);
            assert_eq!(
                model.request(DjmControlKey::CaptureSource(inputs + 1), 0),
                None
            );
            assert_eq!(
                model.request(DjmControlKey::PlaybackDestination(0), 0),
                None
            );
            assert_eq!(
                model.request(DjmControlKey::PlaybackDestination(outputs + 1), 0),
                None
            );
        }
        assert_eq!(DjmModel::from_key("djm-a9"), None);
        assert_eq!(DjmModel::from_usb_id(0x2b73, 0x003c), None);
    }

    #[test]
    fn alsa_capture_request_sequences() {
        use DjmModel::*;
        let cases: &[(DjmModel, &[&[u8]])] = &[
            (
                Djm250Mk2,
                &[
                    &[3, 0, 6, 7, 8, 9, 13, 10],
                    &[3, 0, 6, 7, 8, 9, 13, 10],
                    &[10, 17, 18, 7, 8, 9, 13],
                ],
            ),
            (
                Djm450,
                &[
                    &[3, 0, 6, 7, 8, 9, 13, 10],
                    &[3, 0, 6, 7, 8, 9, 13, 10],
                    &[10, 17, 18, 7, 8, 9, 13],
                ],
            ),
            (
                Djm750,
                &[
                    &[1, 3, 6, 7, 8, 9, 10, 15],
                    &[0, 1, 6, 7, 8, 9, 10, 15],
                    &[0, 1, 6, 7, 8, 9, 10, 15],
                    &[1, 3, 6, 7, 8, 9, 10, 15],
                ],
            ),
            (
                Djm850,
                &[
                    &[0, 3, 6, 7, 8, 9, 10, 15],
                    &[0, 1, 6, 7, 8, 9, 10, 15],
                    &[0, 1, 6, 7, 8, 9, 10, 15],
                    &[0, 3, 6, 7, 8, 9, 10, 15],
                ],
            ),
            (
                Djm900Nxs2,
                &[
                    &[0, 2, 3, 6, 7, 8, 9, 10],
                    &[0, 2, 3, 6, 7, 8, 9, 10],
                    &[0, 2, 3, 6, 7, 8, 9, 10],
                    &[0, 2, 3, 6, 7, 8, 9, 10],
                    &[7, 8, 9, 10, 17, 18, 19, 20],
                ],
            ),
            (
                Djm750Mk2,
                &[
                    &[0, 2, 3, 6, 7, 8, 9, 10],
                    &[0, 2, 3, 6, 7, 8, 9, 10],
                    &[0, 2, 3, 6, 7, 8, 9, 10],
                    &[0, 2, 3, 6, 7, 8, 9, 10],
                    &[7, 8, 9, 10, 17, 18, 19, 20],
                ],
            ),
            (
                DjmV10,
                &[
                    &[3, 0, 2, 6, 16, 7, 8, 9, 10, 33, 34],
                    &[0, 2, 6, 16, 7, 8, 9, 10, 33, 34],
                    &[3, 0, 2, 6, 16, 7, 8, 9, 10, 33, 34],
                    &[3, 0, 2, 6, 16, 7, 8, 9, 10, 33, 34],
                    &[0, 2, 6, 16, 7, 8, 9, 10, 33, 34],
                    &[3, 0, 2, 6, 16, 7, 8, 9, 10, 33, 34],
                ],
            ),
        ];
        for &(model, pairs) in cases {
            for (pair, values) in pairs.iter().enumerate() {
                let pair = (pair + 1) as u8;
                let control = model
                    .controls()
                    .into_iter()
                    .find(|c| c.key == DjmControlKey::CaptureSource(pair))
                    .unwrap();
                assert_eq!(control.options.len(), values.len());
                for (option, &value) in values.iter().enumerate() {
                    assert_eq!(
                        model.request(control.key, option as u8),
                        Some(((u16::from(pair) << 8) | u16::from(value), 0x8002))
                    );
                }
            }
        }
    }

    #[test]
    fn labels_levels_and_playback_match_alsa() {
        let controls = DjmModel::Djm850.controls();
        assert_eq!(controls[1].options[0].label, "Control Tone CD/LINE");
        assert_eq!(controls[2].options[1].label, "Control Tone LINE");
        for model in [
            DjmModel::Djm250Mk2,
            DjmModel::Djm450,
            DjmModel::Djm750,
            DjmModel::Djm850,
            DjmModel::Djm900Nxs2,
            DjmModel::Djm750Mk2,
            DjmModel::DjmV10,
        ] {
            let controls = model.controls();
            let expected = if model == DjmModel::DjmV10 {
                &["+15dB", "+12dB", "+9dB", "+6dB", "+3dB", "0dB"][..]
            } else {
                &["-19dB", "-15dB", "-10dB", "-5dB"][..]
            };
            assert_eq!(
                controls[0]
                    .options
                    .iter()
                    .map(|o| o.label)
                    .collect::<Vec<_>>(),
                expected
            );
            for option in &controls[0].options {
                assert_eq!(
                    model.request(DjmControlKey::CaptureLevel, option.id),
                    Some((u16::from(option.id) << 8, 0x8003))
                );
            }
            for control in controls
                .iter()
                .filter(|c| matches!(c.key, DjmControlKey::PlaybackDestination(_)))
            {
                let DjmControlKey::PlaybackDestination(pair) = control.key else {
                    unreachable!()
                };
                assert_eq!(
                    control.options.iter().map(|o| o.label).collect::<Vec<_>>(),
                    ["Ch1", "Ch2", "Aux"]
                );
                for (id, low) in [0u16, 1, 4].into_iter().enumerate() {
                    assert_eq!(
                        model.request(control.key, id as u8),
                        Some(((u16::from(pair) << 8) | low, 0x8016))
                    );
                }
            }
        }
    }
}