Skip to main content

usbdac_core/
pioneer.rs

1//! Explicit Pioneer DJM routing choices, independent of the USB backend.
2//!
3//! Values and option order follow Linux ALSA `sound/usb/mixer_quirks.c`
4//! (`snd_djm_opts_*` and `snd_djm_get_label_*`). These tables describe writes;
5//! they do not report mixer state or select defaults on connection.
6
7/// DJM models with both source-proven routing requests and PCM transport support.
8#[derive(Clone, Copy, Debug, PartialEq, Eq)]
9pub enum DjmModel {
10    Djm250Mk2,
11    Djm450,
12    Djm750,
13    Djm850,
14    Djm900Nxs2,
15    Djm750Mk2,
16    DjmV10,
17}
18
19/// A routing control. Pair numbers are one-based USB stereo pairs.
20#[derive(Clone, Copy, Debug, PartialEq, Eq)]
21pub enum DjmControlKey {
22    CaptureLevel,
23    CaptureSource(u8),
24    PlaybackDestination(u8),
25}
26
27#[derive(Clone, Debug, PartialEq, Eq)]
28pub struct DjmOption {
29    /// Zero-based option ordinal, scoped to this control and model.
30    pub id: u8,
31    pub label: &'static str,
32}
33
34#[derive(Clone, Debug, PartialEq, Eq)]
35pub struct DjmControl {
36    pub key: DjmControlKey,
37    pub options: Vec<DjmOption>,
38}
39
40/// Available choices for a proven model in an active USB configuration.
41#[derive(Clone, Debug, PartialEq, Eq)]
42pub struct DjmRoutingProfile {
43    pub model: DjmModel,
44    pub configuration: u8,
45    pub controls: Vec<DjmControl>,
46}
47
48// The upper byte is the USB stereo pair. Share only identical lower-byte
49// sequences; their ordering is the ALSA enumeration ordering.
50const TWO_CHANNEL: &[u8] = &[0x03, 0x00, 0x06, 0x07, 0x08, 0x09, 0x0d, 0x0a];
51const TWO_MASTER: &[u8] = &[0x0a, 0x11, 0x12, 0x07, 0x08, 0x09, 0x0d];
52const CD_PHONO: &[u8] = &[0x01, 0x03, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0f];
53const LINE_CD: &[u8] = &[0x00, 0x01, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0f];
54const LINE_PHONO: &[u8] = &[0x00, 0x03, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0f];
55const DIGITAL_CHANNEL: &[u8] = &[0x00, 0x02, 0x03, 0x06, 0x07, 0x08, 0x09, 0x0a];
56const FOUR_MASTER: &[u8] = &[0x07, 0x08, 0x09, 0x0a, 0x11, 0x12, 0x13, 0x14];
57const V10_PHONO: &[u8] = &[
58    0x03, 0x00, 0x02, 0x06, 0x10, 0x07, 0x08, 0x09, 0x0a, 0x21, 0x22,
59];
60const V10_LINE: &[u8] = &[0x00, 0x02, 0x06, 0x10, 0x07, 0x08, 0x09, 0x0a, 0x21, 0x22];
61const PLAYBACK: &[u8] = &[0x00, 0x01, 0x04];
62const LEVEL_COMMON: &[&str] = &["-19dB", "-15dB", "-10dB", "-5dB"];
63const LEVEL_V10: &[&str] = &["+15dB", "+12dB", "+9dB", "+6dB", "+3dB", "0dB"];
64
65impl DjmModel {
66    pub fn key(self) -> &'static str {
67        match self {
68            Self::Djm250Mk2 => "djm-250mk2",
69            Self::Djm450 => "djm-450",
70            Self::Djm750 => "djm-750",
71            Self::Djm850 => "djm-850",
72            Self::Djm900Nxs2 => "djm-900nxs2",
73            Self::Djm750Mk2 => "djm-750mk2",
74            Self::DjmV10 => "djm-v10",
75        }
76    }
77
78    pub fn from_key(key: &str) -> Option<Self> {
79        match key {
80            "djm-250mk2" => Some(Self::Djm250Mk2),
81            "djm-450" => Some(Self::Djm450),
82            "djm-750" => Some(Self::Djm750),
83            "djm-850" => Some(Self::Djm850),
84            "djm-900nxs2" => Some(Self::Djm900Nxs2),
85            "djm-750mk2" => Some(Self::Djm750Mk2),
86            "djm-v10" => Some(Self::DjmV10),
87            _ => None,
88        }
89    }
90
91    pub(crate) fn from_usb_id(vendor: u16, product: u16) -> Option<Self> {
92        match (vendor, product) {
93            (0x2b73, 0x0017) => Some(Self::Djm250Mk2),
94            (0x2b73, 0x0013) => Some(Self::Djm450),
95            (0x08e4, 0x017f) => Some(Self::Djm750),
96            (0x08e4, 0x0163) => Some(Self::Djm850),
97            (0x2b73, 0x000a) => Some(Self::Djm900Nxs2),
98            (0x2b73, 0x001b) => Some(Self::Djm750Mk2),
99            (0x2b73, 0x0034) => Some(Self::DjmV10),
100            _ => None,
101        }
102    }
103
104    fn levels(self) -> &'static [&'static str] {
105        if self == Self::DjmV10 {
106            LEVEL_V10
107        } else {
108            LEVEL_COMMON
109        }
110    }
111
112    fn capture_sources(self, pair: u8) -> Option<&'static [u8]> {
113        match (self, pair) {
114            (Self::Djm250Mk2 | Self::Djm450, 1..=2) => Some(TWO_CHANNEL),
115            (Self::Djm250Mk2 | Self::Djm450, 3) => Some(TWO_MASTER),
116            (Self::Djm750, 1 | 4) => Some(CD_PHONO),
117            (Self::Djm750 | Self::Djm850, 2..=3) => Some(LINE_CD),
118            (Self::Djm850, 1 | 4) => Some(LINE_PHONO),
119            (Self::Djm900Nxs2 | Self::Djm750Mk2, 1..=4) => Some(DIGITAL_CHANNEL),
120            (Self::Djm900Nxs2 | Self::Djm750Mk2, 5) => Some(FOUR_MASTER),
121            (Self::DjmV10, 1 | 3 | 4 | 6) => Some(V10_PHONO),
122            (Self::DjmV10, 2 | 5) => Some(V10_LINE),
123            _ => None,
124        }
125    }
126
127    fn has_playback(self) -> bool {
128        matches!(self, Self::Djm250Mk2 | Self::Djm450 | Self::Djm750Mk2)
129    }
130
131    pub fn controls(self) -> Vec<DjmControl> {
132        let mut controls = vec![DjmControl {
133            key: DjmControlKey::CaptureLevel,
134            options: self
135                .levels()
136                .iter()
137                .enumerate()
138                .map(|(id, &label)| DjmOption {
139                    id: id as u8,
140                    label,
141                })
142                .collect(),
143        }];
144        for pair in 1..=6 {
145            if let Some(sources) = self.capture_sources(pair) {
146                controls.push(DjmControl {
147                    key: DjmControlKey::CaptureSource(pair),
148                    options: sources
149                        .iter()
150                        .enumerate()
151                        .map(|(id, &value)| DjmOption {
152                            id: id as u8,
153                            label: self.capture_label(value),
154                        })
155                        .collect(),
156                });
157            }
158        }
159        if self.has_playback() {
160            for pair in 1..=3 {
161                controls.push(DjmControl {
162                    key: DjmControlKey::PlaybackDestination(pair),
163                    options: ["Ch1", "Ch2", "Aux"]
164                        .into_iter()
165                        .enumerate()
166                        .map(|(id, label)| DjmOption {
167                            id: id as u8,
168                            label,
169                        })
170                        .collect(),
171                });
172            }
173        }
174        controls
175    }
176
177    /// Resolve an explicitly selected choice to `(wValue, wIndex)`.
178    pub(crate) fn request(self, key: DjmControlKey, option: u8) -> Option<(u16, u16)> {
179        match key {
180            DjmControlKey::CaptureLevel => self
181                .levels()
182                .get(usize::from(option))
183                .map(|_| (u16::from(option) << 8, 0x8003)),
184            DjmControlKey::CaptureSource(pair) => self
185                .capture_sources(pair)?
186                .get(usize::from(option))
187                .map(|&value| ((u16::from(pair) << 8) | u16::from(value), 0x8002)),
188            DjmControlKey::PlaybackDestination(pair)
189                if self.has_playback() && (1..=3).contains(&pair) =>
190            {
191                PLAYBACK
192                    .get(usize::from(option))
193                    .map(|&value| ((u16::from(pair) << 8) | u16::from(value), 0x8016))
194            }
195            _ => None,
196        }
197    }
198
199    fn capture_label(self, value: u8) -> &'static str {
200        match (self, value) {
201            (Self::Djm850, 0x00) => "Control Tone CD/LINE",
202            (Self::Djm850, 0x01) => "Control Tone LINE",
203            (_, 0x00) => "Control Tone LINE",
204            (_, 0x01) => "Control Tone CD/LINE",
205            (_, 0x02) => "Control Tone DIGITAL",
206            (_, 0x03) => "Control Tone PHONO",
207            (_, 0x06) => "Post Fader",
208            (_, 0x07) => "Cross Fader A",
209            (_, 0x08) => "Cross Fader B",
210            (_, 0x09) => "Mic",
211            (_, 0x0a) => "Rec Out",
212            (_, 0x0d) => "Aux",
213            (_, 0x0f) => "None",
214            (_, 0x10) => "FX SEND",
215            (_, 0x11) => "Post Fader Ch1",
216            (_, 0x12) => "Post Fader Ch2",
217            (_, 0x13) => "Post Fader Ch3",
218            (_, 0x14) => "Post Fader Ch4",
219            (_, 0x21) => "EXT1 SEND",
220            (_, 0x22) => "EXT2 SEND",
221            _ => unreachable!("capture option table has no ALSA label"),
222        }
223    }
224}
225
226#[cfg(test)]
227mod tests {
228    use super::*;
229
230    #[test]
231    fn model_identities_and_control_order() {
232        let cases = [
233            (DjmModel::Djm250Mk2, "djm-250mk2", 0x2b73, 0x0017, 3, 3),
234            (DjmModel::Djm450, "djm-450", 0x2b73, 0x0013, 3, 3),
235            (DjmModel::Djm750, "djm-750", 0x08e4, 0x017f, 4, 0),
236            (DjmModel::Djm850, "djm-850", 0x08e4, 0x0163, 4, 0),
237            (DjmModel::Djm900Nxs2, "djm-900nxs2", 0x2b73, 0x000a, 5, 0),
238            (DjmModel::Djm750Mk2, "djm-750mk2", 0x2b73, 0x001b, 5, 3),
239            (DjmModel::DjmV10, "djm-v10", 0x2b73, 0x0034, 6, 0),
240        ];
241        for (model, key, vendor, product, inputs, outputs) in cases {
242            assert_eq!(model.key(), key);
243            assert_eq!(DjmModel::from_key(key), Some(model));
244            assert_eq!(DjmModel::from_usb_id(vendor, product), Some(model));
245            assert_eq!(DjmModel::from_usb_id(0xffff, product), None);
246            let controls = model.controls();
247            let expected: Vec<_> = std::iter::once(DjmControlKey::CaptureLevel)
248                .chain((1..=inputs).map(DjmControlKey::CaptureSource))
249                .chain((1..=outputs).map(DjmControlKey::PlaybackDestination))
250                .collect();
251            assert_eq!(controls.iter().map(|c| c.key).collect::<Vec<_>>(), expected);
252            for control in controls {
253                for (ordinal, option) in control.options.iter().enumerate() {
254                    assert_eq!(option.id, ordinal as u8);
255                    assert!(model.request(control.key, option.id).is_some());
256                    assert!(!option.label.is_empty());
257                }
258                assert_eq!(
259                    model.request(control.key, control.options.len() as u8),
260                    None
261                );
262            }
263            assert_eq!(model.request(DjmControlKey::CaptureSource(0), 0), None);
264            assert_eq!(
265                model.request(DjmControlKey::CaptureSource(inputs + 1), 0),
266                None
267            );
268            assert_eq!(
269                model.request(DjmControlKey::PlaybackDestination(0), 0),
270                None
271            );
272            assert_eq!(
273                model.request(DjmControlKey::PlaybackDestination(outputs + 1), 0),
274                None
275            );
276        }
277        assert_eq!(DjmModel::from_key("djm-a9"), None);
278        assert_eq!(DjmModel::from_usb_id(0x2b73, 0x003c), None);
279    }
280
281    #[test]
282    fn alsa_capture_request_sequences() {
283        use DjmModel::*;
284        let cases: &[(DjmModel, &[&[u8]])] = &[
285            (
286                Djm250Mk2,
287                &[
288                    &[3, 0, 6, 7, 8, 9, 13, 10],
289                    &[3, 0, 6, 7, 8, 9, 13, 10],
290                    &[10, 17, 18, 7, 8, 9, 13],
291                ],
292            ),
293            (
294                Djm450,
295                &[
296                    &[3, 0, 6, 7, 8, 9, 13, 10],
297                    &[3, 0, 6, 7, 8, 9, 13, 10],
298                    &[10, 17, 18, 7, 8, 9, 13],
299                ],
300            ),
301            (
302                Djm750,
303                &[
304                    &[1, 3, 6, 7, 8, 9, 10, 15],
305                    &[0, 1, 6, 7, 8, 9, 10, 15],
306                    &[0, 1, 6, 7, 8, 9, 10, 15],
307                    &[1, 3, 6, 7, 8, 9, 10, 15],
308                ],
309            ),
310            (
311                Djm850,
312                &[
313                    &[0, 3, 6, 7, 8, 9, 10, 15],
314                    &[0, 1, 6, 7, 8, 9, 10, 15],
315                    &[0, 1, 6, 7, 8, 9, 10, 15],
316                    &[0, 3, 6, 7, 8, 9, 10, 15],
317                ],
318            ),
319            (
320                Djm900Nxs2,
321                &[
322                    &[0, 2, 3, 6, 7, 8, 9, 10],
323                    &[0, 2, 3, 6, 7, 8, 9, 10],
324                    &[0, 2, 3, 6, 7, 8, 9, 10],
325                    &[0, 2, 3, 6, 7, 8, 9, 10],
326                    &[7, 8, 9, 10, 17, 18, 19, 20],
327                ],
328            ),
329            (
330                Djm750Mk2,
331                &[
332                    &[0, 2, 3, 6, 7, 8, 9, 10],
333                    &[0, 2, 3, 6, 7, 8, 9, 10],
334                    &[0, 2, 3, 6, 7, 8, 9, 10],
335                    &[0, 2, 3, 6, 7, 8, 9, 10],
336                    &[7, 8, 9, 10, 17, 18, 19, 20],
337                ],
338            ),
339            (
340                DjmV10,
341                &[
342                    &[3, 0, 2, 6, 16, 7, 8, 9, 10, 33, 34],
343                    &[0, 2, 6, 16, 7, 8, 9, 10, 33, 34],
344                    &[3, 0, 2, 6, 16, 7, 8, 9, 10, 33, 34],
345                    &[3, 0, 2, 6, 16, 7, 8, 9, 10, 33, 34],
346                    &[0, 2, 6, 16, 7, 8, 9, 10, 33, 34],
347                    &[3, 0, 2, 6, 16, 7, 8, 9, 10, 33, 34],
348                ],
349            ),
350        ];
351        for &(model, pairs) in cases {
352            for (pair, values) in pairs.iter().enumerate() {
353                let pair = (pair + 1) as u8;
354                let control = model
355                    .controls()
356                    .into_iter()
357                    .find(|c| c.key == DjmControlKey::CaptureSource(pair))
358                    .unwrap();
359                assert_eq!(control.options.len(), values.len());
360                for (option, &value) in values.iter().enumerate() {
361                    assert_eq!(
362                        model.request(control.key, option as u8),
363                        Some(((u16::from(pair) << 8) | u16::from(value), 0x8002))
364                    );
365                }
366            }
367        }
368    }
369
370    #[test]
371    fn labels_levels_and_playback_match_alsa() {
372        let controls = DjmModel::Djm850.controls();
373        assert_eq!(controls[1].options[0].label, "Control Tone CD/LINE");
374        assert_eq!(controls[2].options[1].label, "Control Tone LINE");
375        for model in [
376            DjmModel::Djm250Mk2,
377            DjmModel::Djm450,
378            DjmModel::Djm750,
379            DjmModel::Djm850,
380            DjmModel::Djm900Nxs2,
381            DjmModel::Djm750Mk2,
382            DjmModel::DjmV10,
383        ] {
384            let controls = model.controls();
385            let expected = if model == DjmModel::DjmV10 {
386                &["+15dB", "+12dB", "+9dB", "+6dB", "+3dB", "0dB"][..]
387            } else {
388                &["-19dB", "-15dB", "-10dB", "-5dB"][..]
389            };
390            assert_eq!(
391                controls[0]
392                    .options
393                    .iter()
394                    .map(|o| o.label)
395                    .collect::<Vec<_>>(),
396                expected
397            );
398            for option in &controls[0].options {
399                assert_eq!(
400                    model.request(DjmControlKey::CaptureLevel, option.id),
401                    Some((u16::from(option.id) << 8, 0x8003))
402                );
403            }
404            for control in controls
405                .iter()
406                .filter(|c| matches!(c.key, DjmControlKey::PlaybackDestination(_)))
407            {
408                let DjmControlKey::PlaybackDestination(pair) = control.key else {
409                    unreachable!()
410                };
411                assert_eq!(
412                    control.options.iter().map(|o| o.label).collect::<Vec<_>>(),
413                    ["Ch1", "Ch2", "Aux"]
414                );
415                for (id, low) in [0u16, 1, 4].into_iter().enumerate() {
416                    assert_eq!(
417                        model.request(control.key, id as u8),
418                        Some(((u16::from(pair) << 8) | low, 0x8016))
419                    );
420                }
421            }
422        }
423    }
424}