1#[derive(Clone, Copy, Debug, PartialEq, Eq)]
12#[non_exhaustive]
13pub enum DjmModel {
14 Djm250Mk2,
15 Djm450,
16 Djm750,
17 Djm850,
18 Djm900Nxs2,
19 Djm750Mk2,
20 DjmV10,
21 DjmS11,
23}
24
25#[derive(Clone, Copy, Debug, PartialEq, Eq)]
27pub enum DjmControlKey {
28 CaptureLevel,
29 CaptureSource(u8),
30 PlaybackDestination(u8),
31}
32
33#[derive(Clone, Debug, PartialEq, Eq)]
34pub struct DjmOption {
35 pub id: u8,
37 pub label: &'static str,
38}
39
40#[derive(Clone, Debug, PartialEq, Eq)]
41pub struct DjmControl {
42 pub key: DjmControlKey,
43 pub options: Vec<DjmOption>,
44}
45
46#[derive(Clone, Debug, PartialEq, Eq)]
48pub struct DjmRoutingProfile {
49 pub model: DjmModel,
50 pub configuration: u8,
51 pub controls: Vec<DjmControl>,
52}
53
54const TWO_CHANNEL: &[u8] = &[0x03, 0x00, 0x06, 0x07, 0x08, 0x09, 0x0d, 0x0a];
57const TWO_MASTER: &[u8] = &[0x0a, 0x11, 0x12, 0x07, 0x08, 0x09, 0x0d];
58const CD_PHONO: &[u8] = &[0x01, 0x03, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0f];
59const LINE_CD: &[u8] = &[0x00, 0x01, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0f];
60const LINE_PHONO: &[u8] = &[0x00, 0x03, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0f];
61const DIGITAL_CHANNEL: &[u8] = &[0x00, 0x02, 0x03, 0x06, 0x07, 0x08, 0x09, 0x0a];
62const FOUR_MASTER: &[u8] = &[0x07, 0x08, 0x09, 0x0a, 0x11, 0x12, 0x13, 0x14];
63const V10_PHONO: &[u8] = &[
64 0x03, 0x00, 0x02, 0x06, 0x10, 0x07, 0x08, 0x09, 0x0a, 0x21, 0x22,
65];
66const V10_LINE: &[u8] = &[0x00, 0x02, 0x06, 0x10, 0x07, 0x08, 0x09, 0x0a, 0x21, 0x22];
67const S11_CHANNEL: &[u8] = &[0x00, 0x03, 0x06, 0x07, 0x08, 0x09, 0x0d];
69const S11_MASTER: &[u8] = &[0x07, 0x08, 0x09, 0x0a, 0x0d, 0x11, 0x12];
70const PLAYBACK: &[u8] = &[0x00, 0x01, 0x04];
71const LEVEL_COMMON: &[&str] = &["-19dB", "-15dB", "-10dB", "-5dB"];
72const LEVEL_V10: &[&str] = &["+15dB", "+12dB", "+9dB", "+6dB", "+3dB", "0dB"];
73
74impl DjmModel {
75 pub fn key(self) -> &'static str {
76 match self {
77 Self::Djm250Mk2 => "djm-250mk2",
78 Self::Djm450 => "djm-450",
79 Self::Djm750 => "djm-750",
80 Self::Djm850 => "djm-850",
81 Self::Djm900Nxs2 => "djm-900nxs2",
82 Self::Djm750Mk2 => "djm-750mk2",
83 Self::DjmV10 => "djm-v10",
84 Self::DjmS11 => "djm-s11",
85 }
86 }
87
88 pub fn from_key(key: &str) -> Option<Self> {
89 match key {
90 "djm-250mk2" => Some(Self::Djm250Mk2),
91 "djm-450" => Some(Self::Djm450),
92 "djm-750" => Some(Self::Djm750),
93 "djm-850" => Some(Self::Djm850),
94 "djm-900nxs2" => Some(Self::Djm900Nxs2),
95 "djm-750mk2" => Some(Self::Djm750Mk2),
96 "djm-v10" => Some(Self::DjmV10),
97 "djm-s11" => Some(Self::DjmS11),
98 _ => None,
99 }
100 }
101
102 pub(crate) fn from_usb_id(vendor: u16, product: u16) -> Option<Self> {
103 match (vendor, product) {
104 (0x2b73, 0x0017) => Some(Self::Djm250Mk2),
105 (0x2b73, 0x0013) => Some(Self::Djm450),
106 (0x08e4, 0x017f) => Some(Self::Djm750),
107 (0x08e4, 0x0163) => Some(Self::Djm850),
108 (0x2b73, 0x000a) => Some(Self::Djm900Nxs2),
109 (0x2b73, 0x001b) => Some(Self::Djm750Mk2),
110 (0x2b73, 0x0034) => Some(Self::DjmV10),
111 (0x2b73, 0x0037) => Some(Self::DjmS11),
112 _ => None,
113 }
114 }
115
116 fn levels(self) -> &'static [&'static str] {
117 if self == Self::DjmV10 {
118 LEVEL_V10
119 } else {
120 LEVEL_COMMON
121 }
122 }
123
124 fn capture_sources(self, pair: u8) -> Option<&'static [u8]> {
125 match (self, pair) {
126 (Self::Djm250Mk2 | Self::Djm450, 1..=2) => Some(TWO_CHANNEL),
127 (Self::Djm250Mk2 | Self::Djm450, 3) => Some(TWO_MASTER),
128 (Self::Djm750, 1 | 4) => Some(CD_PHONO),
129 (Self::Djm750 | Self::Djm850, 2..=3) => Some(LINE_CD),
130 (Self::Djm850, 1 | 4) => Some(LINE_PHONO),
131 (Self::Djm900Nxs2 | Self::Djm750Mk2, 1..=4) => Some(DIGITAL_CHANNEL),
132 (Self::Djm900Nxs2 | Self::Djm750Mk2, 5) => Some(FOUR_MASTER),
133 (Self::DjmV10, 1 | 3 | 4 | 6) => Some(V10_PHONO),
134 (Self::DjmV10, 2 | 5) => Some(V10_LINE),
135 (Self::DjmS11, 1..=2) => Some(S11_CHANNEL),
136 (Self::DjmS11, 3) => Some(S11_MASTER),
137 _ => None,
138 }
139 }
140
141 fn has_playback(self) -> bool {
142 matches!(self, Self::Djm250Mk2 | Self::Djm450 | Self::Djm750Mk2)
143 }
144
145 pub fn controls(self) -> Vec<DjmControl> {
146 let mut controls = vec![DjmControl {
147 key: DjmControlKey::CaptureLevel,
148 options: self
149 .levels()
150 .iter()
151 .enumerate()
152 .map(|(id, &label)| DjmOption {
153 id: id as u8,
154 label,
155 })
156 .collect(),
157 }];
158 for pair in 1..=6 {
159 if let Some(sources) = self.capture_sources(pair) {
160 controls.push(DjmControl {
161 key: DjmControlKey::CaptureSource(pair),
162 options: sources
163 .iter()
164 .enumerate()
165 .map(|(id, &value)| DjmOption {
166 id: id as u8,
167 label: self.capture_label(value),
168 })
169 .collect(),
170 });
171 }
172 }
173 if self.has_playback() {
174 for pair in 1..=3 {
175 controls.push(DjmControl {
176 key: DjmControlKey::PlaybackDestination(pair),
177 options: ["Ch1", "Ch2", "Aux"]
178 .into_iter()
179 .enumerate()
180 .map(|(id, label)| DjmOption {
181 id: id as u8,
182 label,
183 })
184 .collect(),
185 });
186 }
187 }
188 controls
189 }
190
191 pub(crate) fn request(self, key: DjmControlKey, option: u8) -> Option<(u16, u16)> {
193 match key {
194 DjmControlKey::CaptureLevel => self
195 .levels()
196 .get(usize::from(option))
197 .map(|_| (u16::from(option) << 8, 0x8003)),
198 DjmControlKey::CaptureSource(pair) => self
199 .capture_sources(pair)?
200 .get(usize::from(option))
201 .map(|&value| ((u16::from(pair) << 8) | u16::from(value), 0x8002)),
202 DjmControlKey::PlaybackDestination(pair)
203 if self.has_playback() && (1..=3).contains(&pair) =>
204 {
205 PLAYBACK
206 .get(usize::from(option))
207 .map(|&value| ((u16::from(pair) << 8) | u16::from(value), 0x8016))
208 }
209 _ => None,
210 }
211 }
212
213 fn capture_label(self, value: u8) -> &'static str {
214 match (self, value) {
215 (Self::Djm850, 0x00) => "Control Tone CD/LINE",
216 (Self::Djm850, 0x01) => "Control Tone LINE",
217 (_, 0x00) => "Control Tone LINE",
218 (_, 0x01) => "Control Tone CD/LINE",
219 (_, 0x02) => "Control Tone DIGITAL",
220 (_, 0x03) => "Control Tone PHONO",
221 (_, 0x06) => "Post Fader",
222 (_, 0x07) => "Cross Fader A",
223 (_, 0x08) => "Cross Fader B",
224 (_, 0x09) => "Mic",
225 (_, 0x0a) => "Rec Out",
226 (_, 0x0d) => "Aux",
227 (_, 0x0f) => "None",
228 (_, 0x10) => "FX SEND",
229 (_, 0x11) => "Post Fader Ch1",
230 (_, 0x12) => "Post Fader Ch2",
231 (_, 0x13) => "Post Fader Ch3",
232 (_, 0x14) => "Post Fader Ch4",
233 (_, 0x21) => "EXT1 SEND",
234 (_, 0x22) => "EXT2 SEND",
235 _ => unreachable!("capture option table has no ALSA label"),
236 }
237 }
238}
239
240#[cfg(test)]
241mod tests {
242 use super::*;
243
244 #[test]
245 fn model_identities_and_control_order() {
246 let cases = [
247 (DjmModel::Djm250Mk2, "djm-250mk2", 0x2b73, 0x0017, 3, 3),
248 (DjmModel::Djm450, "djm-450", 0x2b73, 0x0013, 3, 3),
249 (DjmModel::Djm750, "djm-750", 0x08e4, 0x017f, 4, 0),
250 (DjmModel::Djm850, "djm-850", 0x08e4, 0x0163, 4, 0),
251 (DjmModel::Djm900Nxs2, "djm-900nxs2", 0x2b73, 0x000a, 5, 0),
252 (DjmModel::Djm750Mk2, "djm-750mk2", 0x2b73, 0x001b, 5, 3),
253 (DjmModel::DjmV10, "djm-v10", 0x2b73, 0x0034, 6, 0),
254 (DjmModel::DjmS11, "djm-s11", 0x2b73, 0x0037, 3, 0),
255 ];
256 for (model, key, vendor, product, inputs, outputs) in cases {
257 assert_eq!(model.key(), key);
258 assert_eq!(DjmModel::from_key(key), Some(model));
259 assert_eq!(DjmModel::from_usb_id(vendor, product), Some(model));
260 assert_eq!(DjmModel::from_usb_id(0xffff, product), None);
261 let controls = model.controls();
262 let expected: Vec<_> = std::iter::once(DjmControlKey::CaptureLevel)
263 .chain((1..=inputs).map(DjmControlKey::CaptureSource))
264 .chain((1..=outputs).map(DjmControlKey::PlaybackDestination))
265 .collect();
266 assert_eq!(controls.iter().map(|c| c.key).collect::<Vec<_>>(), expected);
267 for control in controls {
268 for (ordinal, option) in control.options.iter().enumerate() {
269 assert_eq!(option.id, ordinal as u8);
270 assert!(model.request(control.key, option.id).is_some());
271 assert!(!option.label.is_empty());
272 }
273 assert_eq!(
274 model.request(control.key, control.options.len() as u8),
275 None
276 );
277 }
278 assert_eq!(model.request(DjmControlKey::CaptureSource(0), 0), None);
279 assert_eq!(
280 model.request(DjmControlKey::CaptureSource(inputs + 1), 0),
281 None
282 );
283 assert_eq!(
284 model.request(DjmControlKey::PlaybackDestination(0), 0),
285 None
286 );
287 assert_eq!(
288 model.request(DjmControlKey::PlaybackDestination(outputs + 1), 0),
289 None
290 );
291 }
292 assert_eq!(DjmModel::from_key("djm-a9"), None);
293 assert_eq!(DjmModel::from_usb_id(0x2b73, 0x003c), None);
294 }
295
296 #[test]
297 fn alsa_capture_request_sequences() {
298 use DjmModel::*;
299 let cases: &[(DjmModel, &[&[u8]])] = &[
300 (
301 Djm250Mk2,
302 &[
303 &[3, 0, 6, 7, 8, 9, 13, 10],
304 &[3, 0, 6, 7, 8, 9, 13, 10],
305 &[10, 17, 18, 7, 8, 9, 13],
306 ],
307 ),
308 (
309 Djm450,
310 &[
311 &[3, 0, 6, 7, 8, 9, 13, 10],
312 &[3, 0, 6, 7, 8, 9, 13, 10],
313 &[10, 17, 18, 7, 8, 9, 13],
314 ],
315 ),
316 (
317 Djm750,
318 &[
319 &[1, 3, 6, 7, 8, 9, 10, 15],
320 &[0, 1, 6, 7, 8, 9, 10, 15],
321 &[0, 1, 6, 7, 8, 9, 10, 15],
322 &[1, 3, 6, 7, 8, 9, 10, 15],
323 ],
324 ),
325 (
326 Djm850,
327 &[
328 &[0, 3, 6, 7, 8, 9, 10, 15],
329 &[0, 1, 6, 7, 8, 9, 10, 15],
330 &[0, 1, 6, 7, 8, 9, 10, 15],
331 &[0, 3, 6, 7, 8, 9, 10, 15],
332 ],
333 ),
334 (
335 Djm900Nxs2,
336 &[
337 &[0, 2, 3, 6, 7, 8, 9, 10],
338 &[0, 2, 3, 6, 7, 8, 9, 10],
339 &[0, 2, 3, 6, 7, 8, 9, 10],
340 &[0, 2, 3, 6, 7, 8, 9, 10],
341 &[7, 8, 9, 10, 17, 18, 19, 20],
342 ],
343 ),
344 (
345 Djm750Mk2,
346 &[
347 &[0, 2, 3, 6, 7, 8, 9, 10],
348 &[0, 2, 3, 6, 7, 8, 9, 10],
349 &[0, 2, 3, 6, 7, 8, 9, 10],
350 &[0, 2, 3, 6, 7, 8, 9, 10],
351 &[7, 8, 9, 10, 17, 18, 19, 20],
352 ],
353 ),
354 (
355 DjmV10,
356 &[
357 &[3, 0, 2, 6, 16, 7, 8, 9, 10, 33, 34],
358 &[0, 2, 6, 16, 7, 8, 9, 10, 33, 34],
359 &[3, 0, 2, 6, 16, 7, 8, 9, 10, 33, 34],
360 &[3, 0, 2, 6, 16, 7, 8, 9, 10, 33, 34],
361 &[0, 2, 6, 16, 7, 8, 9, 10, 33, 34],
362 &[3, 0, 2, 6, 16, 7, 8, 9, 10, 33, 34],
363 ],
364 ),
365 (
366 DjmS11,
367 &[
368 &[0, 3, 6, 7, 8, 9, 13],
369 &[0, 3, 6, 7, 8, 9, 13],
370 &[7, 8, 9, 10, 13, 17, 18],
371 ],
372 ),
373 ];
374 for &(model, pairs) in cases {
375 for (pair, values) in pairs.iter().enumerate() {
376 let pair = (pair + 1) as u8;
377 let control = model
378 .controls()
379 .into_iter()
380 .find(|c| c.key == DjmControlKey::CaptureSource(pair))
381 .unwrap();
382 assert_eq!(control.options.len(), values.len());
383 for (option, &value) in values.iter().enumerate() {
384 assert_eq!(
385 model.request(control.key, option as u8),
386 Some(((u16::from(pair) << 8) | u16::from(value), 0x8002))
387 );
388 }
389 }
390 }
391 }
392
393 #[test]
394 fn labels_levels_and_playback_match_alsa() {
395 let controls = DjmModel::Djm850.controls();
396 assert_eq!(controls[1].options[0].label, "Control Tone CD/LINE");
397 assert_eq!(controls[2].options[1].label, "Control Tone LINE");
398 for model in [
399 DjmModel::Djm250Mk2,
400 DjmModel::Djm450,
401 DjmModel::Djm750,
402 DjmModel::Djm850,
403 DjmModel::Djm900Nxs2,
404 DjmModel::Djm750Mk2,
405 DjmModel::DjmV10,
406 DjmModel::DjmS11,
407 ] {
408 let controls = model.controls();
409 let expected = if model == DjmModel::DjmV10 {
410 &["+15dB", "+12dB", "+9dB", "+6dB", "+3dB", "0dB"][..]
411 } else {
412 &["-19dB", "-15dB", "-10dB", "-5dB"][..]
413 };
414 assert_eq!(
415 controls[0]
416 .options
417 .iter()
418 .map(|o| o.label)
419 .collect::<Vec<_>>(),
420 expected
421 );
422 for option in &controls[0].options {
423 assert_eq!(
424 model.request(DjmControlKey::CaptureLevel, option.id),
425 Some((u16::from(option.id) << 8, 0x8003))
426 );
427 }
428 for control in controls
429 .iter()
430 .filter(|c| matches!(c.key, DjmControlKey::PlaybackDestination(_)))
431 {
432 let DjmControlKey::PlaybackDestination(pair) = control.key else {
433 unreachable!()
434 };
435 assert_eq!(
436 control.options.iter().map(|o| o.label).collect::<Vec<_>>(),
437 ["Ch1", "Ch2", "Aux"]
438 );
439 for (id, low) in [0u16, 1, 4].into_iter().enumerate() {
440 assert_eq!(
441 model.request(control.key, id as u8),
442 Some(((u16::from(pair) << 8) | low, 0x8016))
443 );
444 }
445 }
446 }
447 }
448
449 #[test]
453 fn djm_s11_routing_matches_the_pinned_alsa_table() {
454 let model = DjmModel::DjmS11;
455 let controls = model.controls();
456 let labels = |pair: u8| {
457 controls
458 .iter()
459 .find(|c| c.key == DjmControlKey::CaptureSource(pair))
460 .unwrap()
461 .options
462 .iter()
463 .map(|o| o.label)
464 .collect::<Vec<_>>()
465 };
466 let channel = [
467 "Control Tone LINE",
468 "Control Tone PHONO",
469 "Post Fader",
470 "Cross Fader A",
471 "Cross Fader B",
472 "Mic",
473 "Aux",
474 ];
475 assert_eq!(labels(1), channel);
476 assert_eq!(labels(2), channel);
477 assert_eq!(
478 labels(3),
479 [
480 "Cross Fader A",
481 "Cross Fader B",
482 "Mic",
483 "Rec Out",
484 "Aux",
485 "Post Fader Ch1",
486 "Post Fader Ch2",
487 ]
488 );
489 assert_eq!(
490 controls[0]
491 .options
492 .iter()
493 .map(|o| o.label)
494 .collect::<Vec<_>>(),
495 ["-19dB", "-15dB", "-10dB", "-5dB"]
496 );
497 assert_eq!(
498 model.request(DjmControlKey::CaptureLevel, 3),
499 Some((0x0300, 0x8003))
500 );
501 assert_eq!(model.request(DjmControlKey::CaptureLevel, 4), None);
502 assert_eq!(
504 model.request(DjmControlKey::CaptureSource(1), 1),
505 Some((0x0103, 0x8002))
506 );
507 assert_eq!(
508 model.request(DjmControlKey::CaptureSource(2), 1),
509 Some((0x0203, 0x8002))
510 );
511 assert_eq!(
512 model.request(DjmControlKey::CaptureSource(3), 3),
513 Some((0x030a, 0x8002))
514 );
515 assert_eq!(model.request(DjmControlKey::CaptureSource(4), 0), None);
516 for pair in 1..=3 {
517 assert_eq!(
518 model.request(DjmControlKey::PlaybackDestination(pair), 0),
519 None
520 );
521 }
522 assert_eq!(DjmModel::from_usb_id(0x08e4, 0x0037), None);
523 assert_eq!(DjmModel::from_usb_id(0x2b73, 0x0038), None);
524 }
525}