firewire-dice-protocols 0.1.1

Implementation of protocols defined by TC Applied Technologies for ASICs of Digital Interface Communication Engine (DICE) as well as hardware vendors.
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright (c) 2020 Takashi Sakamoto

//! Protocol defined by TC Electronic for Konnekt 24d, Konnekt 8, Konnekt Live, and Impact Twin.
//!
//! The module includes structure, enumeration, and trait and its implementation for protocol
//! defined by TC Electronic for Konnekt 24d, Konnekt 8, Konnekt Live, and Impact Twin.

pub mod itwin;
pub mod k24d;
pub mod k8;
pub mod klive;

use super::{ch_strip::*, fw_led::*, midi_send::*, prog::*, reverb::*, standalone::*, *};

const SHELL_KNOB_NOTIFY_FLAG: u32 = 0x00010000;
const SHELL_CONFIG_NOTIFY_FLAG: u32 = 0x00020000;
const SHELL_MIXER_NOTIFY_FLAG: u32 = 0x00040000;
const SHELL_REVERB_NOTIFY_FLAG: u32 = 0x00080000;
const SHELL_CH_STRIP_NOTIFY_FLAG: u32 = 0x00100000;
// NOTE: 0x00200000 is for tuner.
// NOTE: 0x00400000 is unidentified.
const SHELL_HW_STATE_NOTIFY_FLAG: u32 = 0x01000000;

const SHELL_CH_STRIP_COUNT: usize = 2;

/// State of jack sense for analog input.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum ShellAnalogJackState {
    FrontSelected,
    FrontInserted,
    FrontInsertedAttenuated,
    RearSelected,
    RearInserted,
}

impl Default for ShellAnalogJackState {
    fn default() -> Self {
        Self::FrontSelected
    }
}

impl ShellAnalogJackState {
    const FRONT_SELECTED: u32 = 0x00;
    const FRONT_INSERTED: u32 = 0x05;
    const FRONT_INSERTED_ATTENUATED: u32 = 0x06;
    const REAR_SELECTED: u32 = 0x07;
    const REAR_INSERTED: u32 = 0x08;
}

impl From<u32> for ShellAnalogJackState {
    fn from(val: u32) -> Self {
        match val & 0xff {
            Self::FRONT_INSERTED => Self::FrontInserted,
            Self::FRONT_INSERTED_ATTENUATED => Self::FrontInsertedAttenuated,
            Self::REAR_SELECTED => Self::RearSelected,
            Self::REAR_INSERTED => Self::RearInserted,
            _ => Self::FrontSelected,
        }
    }
}

impl From<ShellAnalogJackState> for u32 {
    fn from(state: ShellAnalogJackState) -> Self {
        match state {
            ShellAnalogJackState::FrontSelected => ShellAnalogJackState::FRONT_SELECTED,
            ShellAnalogJackState::FrontInserted => ShellAnalogJackState::FRONT_INSERTED,
            ShellAnalogJackState::FrontInsertedAttenuated => {
                ShellAnalogJackState::FRONT_INSERTED_ATTENUATED
            }
            ShellAnalogJackState::RearSelected => ShellAnalogJackState::REAR_SELECTED,
            ShellAnalogJackState::RearInserted => ShellAnalogJackState::REAR_INSERTED,
        }
    }
}

/// The number of analog inputs which has jack sense.
pub const SHELL_ANALOG_JACK_STATE_COUNT: usize = 2;

/// Hardware state.
#[derive(Default, Debug, Clone, Eq, PartialEq)]
pub struct ShellHwState {
    pub analog_jack_states: [ShellAnalogJackState; SHELL_ANALOG_JACK_STATE_COUNT],
    pub firewire_led: FireWireLedState,
}

impl ShellHwState {
    pub const SIZE: usize = 28;

    pub fn build(&self, raw: &mut [u8]) {
        assert_eq!(raw.len(), Self::SIZE, "Programming error...");

        self.analog_jack_states.build_quadlet_block(&mut raw[..8]);
        self.firewire_led.build_quadlet(&mut raw[20..24]);
    }

    pub fn parse(&mut self, raw: &[u8]) {
        assert_eq!(raw.len(), Self::SIZE, "Programming error...");

        self.analog_jack_states.parse_quadlet_block(&raw[..8]);
        self.firewire_led.parse_quadlet(&raw[20..24]);
    }
}

/// Parameter of monitor source.
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct MonitorSrcParam {
    ///  ch 1 gain to mixer ch 1/2 (0xfffffc18..0x00000000, -90.0..0.00 dB)
    pub gain_to_mixer: i32,
    ///  ch 1 pan to mixer ch 1/2 (0xffffffce..0x00000032, -50.0..+50.0 dB)
    pub pan_to_mixer: i32,
    ///  ch 1 gain to send ch 1/2 (0xfffffc18..0x00000000, -90.0..0.00 dB)
    pub gain_to_send: i32,
}

impl MonitorSrcParam {
    const SIZE: usize = 12;

    pub fn build(&self, raw: &mut [u8]) {
        assert_eq!(
            raw.len(),
            Self::SIZE,
            "Programming error for the length of monitor source parameter."
        );

        self.gain_to_mixer.build_quadlet(&mut raw[..4]);
        self.pan_to_mixer.build_quadlet(&mut raw[4..8]);
        self.gain_to_send.build_quadlet(&mut raw[8..12]);
    }

    pub fn parse(&mut self, raw: &[u8]) {
        assert_eq!(
            raw.len(),
            Self::SIZE,
            "Programming error for the length of monitor source parameter."
        );

        self.gain_to_mixer.parse_quadlet(&raw[..4]);
        self.pan_to_mixer.parse_quadlet(&raw[4..8]);
        self.gain_to_send.parse_quadlet(&raw[8..12]);
    }
}

/// Monitor source.
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct ShellMonitorSrcPair {
    ///  ch 1/2 stereo link (0 or 1)
    pub stereo_link: bool,
    /// Left channel.
    pub left: MonitorSrcParam,
    /// Right channel.
    pub right: MonitorSrcParam,
}

impl ShellMonitorSrcPair {
    const SIZE: usize = 28;

    pub fn build(&self, raw: &mut [u8]) {
        self.stereo_link.build_quadlet(&mut raw[..4]);
        self.left.build(&mut raw[4..16]);
        self.right.build(&mut raw[16..28]);
    }

    pub fn parse(&mut self, raw: &[u8]) {
        self.stereo_link.parse_quadlet(&raw[..4]);
        self.left.parse(&raw[4..16]);
        self.right.parse(&raw[16..28]);
    }
}

/// Mute state for monitor sources.
#[derive(Debug)]
pub struct ShellMonitorSrcMute {
    pub stream: bool,
    pub analog: Vec<bool>,
    pub digital: Vec<bool>,
}

/// State of mixer.
#[derive(Debug)]
pub struct ShellMixerState {
    pub stream: ShellMonitorSrcPair,
    pub analog: Vec<ShellMonitorSrcPair>,
    pub digital: Vec<ShellMonitorSrcPair>,
    pub mutes: ShellMonitorSrcMute,
    /// The level of output volume.
    pub output_volume: i32,
    /// Whether to dim level of output volume
    pub output_dim_enable: bool,
    /// The level of output volume at dimmed.
    pub output_dim_volume: i32,
}

const SHELL_MIXER_MONITOR_SRC_COUNT: usize = 10;

impl ShellMixerState {
    pub const SIZE: usize = ShellMonitorSrcPair::SIZE * SHELL_MIXER_MONITOR_SRC_COUNT + 36;
}

/// The type of monitor source.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum ShellMixerMonitorSrcType {
    Stream,
    Analog,
    Spdif,
    Adat,
    AdatSpdif,
}

pub trait ShellMixerStateConvert {
    const MONITOR_SRC_MAP: [Option<ShellMixerMonitorSrcType>; SHELL_MIXER_MONITOR_SRC_COUNT];

    fn state(&self) -> &ShellMixerState;
    fn state_mut(&mut self) -> &mut ShellMixerState;

    fn create_mixer_state() -> ShellMixerState {
        let analog_input_pair_count = Self::MONITOR_SRC_MAP
            .iter()
            .filter(|&&m| m == Some(ShellMixerMonitorSrcType::Analog))
            .count();
        let digital_input_pair_count = Self::MONITOR_SRC_MAP
            .iter()
            .filter(|&&m| {
                m != Some(ShellMixerMonitorSrcType::Analog)
                    && m != Some(ShellMixerMonitorSrcType::Stream)
                    && m.is_some()
            })
            .count();

        ShellMixerState {
            stream: Default::default(),
            analog: vec![Default::default(); analog_input_pair_count],
            digital: vec![Default::default(); digital_input_pair_count],
            mutes: ShellMonitorSrcMute {
                stream: Default::default(),
                analog: vec![Default::default(); analog_input_pair_count * 2],
                digital: vec![Default::default(); digital_input_pair_count * 2],
            },
            output_volume: Default::default(),
            output_dim_enable: Default::default(),
            output_dim_volume: Default::default(),
        }
    }

    fn build(&self, raw: &mut [u8]) {
        let state = self.state();

        state.stream.build(&mut raw[..ShellMonitorSrcPair::SIZE]);

        // For analog inputs.
        Self::MONITOR_SRC_MAP
            .iter()
            .enumerate()
            .filter(|(_, &m)| m == Some(ShellMixerMonitorSrcType::Analog))
            .zip(&state.analog)
            .for_each(|((i, _), src)| {
                let pos = i * ShellMonitorSrcPair::SIZE;
                src.build(&mut raw[pos..(pos + ShellMonitorSrcPair::SIZE)]);
            });

        // For digital inputs.
        Self::MONITOR_SRC_MAP
            .iter()
            .enumerate()
            .filter(|(_, &m)| {
                m.is_some()
                    && m != Some(ShellMixerMonitorSrcType::Analog)
                    && m != Some(ShellMixerMonitorSrcType::Stream)
            })
            .zip(&state.digital)
            .for_each(|((i, _), src)| {
                let pos = i * ShellMonitorSrcPair::SIZE;
                src.build(&mut raw[pos..(pos + ShellMonitorSrcPair::SIZE)]);
            });

        // For mixer output.
        state.output_dim_enable.build_quadlet(&mut raw[280..284]);
        state.output_volume.build_quadlet(&mut raw[284..288]);
        state.output_dim_volume.build_quadlet(&mut raw[296..300]);

        // For mute of sources.
        let mut mutes = 0u32;
        if state.mutes.stream {
            mutes |= 0x00000001;
        }
        state
            .mutes
            .analog
            .iter()
            .chain(&state.mutes.digital)
            .enumerate()
            .filter(|(_, &muted)| muted)
            .for_each(|(i, _)| {
                mutes |= 1 << (8 + i);
            });
        mutes.build_quadlet(&mut raw[308..312]);
    }

    fn parse(&mut self, raw: &[u8]) {
        let state = self.state_mut();

        state.stream.parse(&raw[..ShellMonitorSrcPair::SIZE]);

        // For analog inputs.
        Self::MONITOR_SRC_MAP
            .iter()
            .enumerate()
            .filter(|(_, &m)| m == Some(ShellMixerMonitorSrcType::Analog))
            .zip(&mut state.analog)
            .for_each(|((i, _), src)| {
                let pos = i * ShellMonitorSrcPair::SIZE;
                src.parse(&raw[pos..(pos + ShellMonitorSrcPair::SIZE)]);
            });

        // For digital inputs.
        Self::MONITOR_SRC_MAP
            .iter()
            .enumerate()
            .filter(|(_, &m)| m.is_some() && m != Some(ShellMixerMonitorSrcType::Analog))
            .zip(&mut state.digital)
            .for_each(|((i, _), src)| {
                let pos = i * ShellMonitorSrcPair::SIZE;
                src.parse(&raw[pos..(pos + ShellMonitorSrcPair::SIZE)]);
            });

        // For mixer output.
        state.output_dim_enable.parse_quadlet(&raw[280..284]);
        state.output_volume.parse_quadlet(&raw[284..288]);
        state.output_dim_volume.parse_quadlet(&raw[296..300]);

        // For mute of sources.
        let mut mutes = 0u32;
        mutes.parse_quadlet(&raw[308..312]);
        state.mutes.stream = mutes & 0x00000001 > 0;
        state
            .mutes
            .analog
            .iter_mut()
            .chain(&mut state.mutes.digital)
            .enumerate()
            .for_each(|(i, muted)| {
                *muted = mutes & (1 << (8 + i)) > 0;
            });
    }
}

/// Return configuration of reverb effect.
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct ShellReverbReturn {
    /// Whether to use reverb effect as plugin. When enabled, return of reverb effect is delivered
    /// by rx stream.
    pub plugin_mode: bool,
    /// The gain to return reverb effect to mixer output.
    pub return_gain: i32,
    /// Whether to mute return reverb effect to mixer output.
    pub return_mute: bool,
}

impl ShellReverbReturn {
    pub const SIZE: usize = 12;

    pub fn build(&self, raw: &mut [u8]) {
        assert_eq!(raw.len(), Self::SIZE, "Programming error");

        self.plugin_mode.build_quadlet(&mut raw[..4]);
        self.return_gain.build_quadlet(&mut raw[4..8]);
        self.return_mute.build_quadlet(&mut raw[8..12]);
    }

    pub fn parse(&mut self, raw: &[u8]) {
        assert_eq!(raw.len(), Self::SIZE, "Programming error");

        self.plugin_mode.parse_quadlet(&raw[..4]);
        self.return_gain.parse_quadlet(&raw[4..8]);
        self.return_mute.parse_quadlet(&raw[8..12]);
    }
}

/// Meter information. -1000..0 (-94.0..0 dB).
#[derive(Default, Debug)]
pub struct ShellMixerMeter {
    pub stream_inputs: [i32; Self::STREAM_INPUT_COUNT],
    pub analog_inputs: Vec<i32>,
    pub digital_inputs: Vec<i32>,
    pub main_outputs: [i32; Self::MAIN_OUTPUT_COUNT],
}

impl ShellMixerMeter {
    pub const SIZE: usize = 0x5c;

    const STREAM_INPUT_COUNT: usize = 2;
    const MAIN_OUTPUT_COUNT: usize = 2;
    const MAX_STREAM_INPUT_COUNT: usize = 8;
    const MAX_ANALOG_INPUT_COUNT: usize = 4;
    const MAX_DIGITAL_INPUT_COUNT: usize = 8;
}

pub trait ShellMixerMeterConvert {
    const ANALOG_INPUT_COUNT: usize;
    const DIGITAL_INPUT_COUNT: usize;

    fn meter(&self) -> &ShellMixerMeter;
    fn meter_mut(&mut self) -> &mut ShellMixerMeter;

    fn create_meter_state() -> ShellMixerMeter {
        ShellMixerMeter {
            stream_inputs: [Default::default(); ShellMixerMeter::STREAM_INPUT_COUNT],
            analog_inputs: vec![Default::default(); Self::ANALOG_INPUT_COUNT],
            digital_inputs: vec![Default::default(); Self::DIGITAL_INPUT_COUNT],
            main_outputs: [Default::default(); ShellMixerMeter::MAIN_OUTPUT_COUNT],
        }
    }

    fn build(&self, raw: &mut [u8]) {
        assert_eq!(raw.len(), ShellMixerMeter::SIZE, "Programming error...");

        let state = self.meter();

        let mut offset = 0;
        state.stream_inputs.iter().enumerate().for_each(|(i, m)| {
            let pos = offset + i * 4;
            m.build_quadlet(&mut raw[pos..(pos + 4)]);
        });

        offset += ShellMixerMeter::MAX_STREAM_INPUT_COUNT * 4;
        state
            .analog_inputs
            .iter()
            .take(Self::ANALOG_INPUT_COUNT)
            .take(ShellMixerMeter::MAX_ANALOG_INPUT_COUNT)
            .enumerate()
            .for_each(|(i, m)| {
                let pos = offset + i * 4;
                m.build_quadlet(&mut raw[pos..(pos + 4)]);
            });

        offset += ShellMixerMeter::MAX_ANALOG_INPUT_COUNT * 4;
        state
            .digital_inputs
            .iter()
            .take(Self::DIGITAL_INPUT_COUNT)
            .take(ShellMixerMeter::MAX_DIGITAL_INPUT_COUNT)
            .enumerate()
            .for_each(|(i, m)| {
                let pos = offset + i * 4;
                m.build_quadlet(&mut raw[pos..(pos + 4)]);
            });

        offset += ShellMixerMeter::MAX_DIGITAL_INPUT_COUNT * 4;
        state.main_outputs.iter().enumerate().for_each(|(i, m)| {
            let pos = offset + i * 4;
            m.build_quadlet(&mut raw[pos..(pos + 4)]);
        });
    }

    fn parse(&mut self, raw: &[u8]) {
        assert_eq!(raw.len(), ShellMixerMeter::SIZE, "Programming error...");

        let state = self.meter_mut();

        let mut offset = 0;
        state
            .stream_inputs
            .iter_mut()
            .enumerate()
            .for_each(|(i, m)| {
                let pos = offset + i * 4;
                m.parse_quadlet(&raw[pos..(pos + 4)]);
            });

        offset += ShellMixerMeter::MAX_STREAM_INPUT_COUNT * 4;
        state
            .analog_inputs
            .iter_mut()
            .take(Self::ANALOG_INPUT_COUNT)
            .take(ShellMixerMeter::MAX_ANALOG_INPUT_COUNT)
            .enumerate()
            .for_each(|(i, m)| {
                let pos = offset + i * 4;
                m.parse_quadlet(&raw[pos..(pos + 4)]);
            });

        offset += ShellMixerMeter::MAX_ANALOG_INPUT_COUNT * 4;
        state
            .digital_inputs
            .iter_mut()
            .take(Self::DIGITAL_INPUT_COUNT)
            .take(ShellMixerMeter::MAX_DIGITAL_INPUT_COUNT)
            .enumerate()
            .for_each(|(i, m)| {
                let pos = offset + i * 4;
                m.parse_quadlet(&raw[pos..(pos + 4)]);
            });

        offset += ShellMixerMeter::MAX_DIGITAL_INPUT_COUNT * 4;
        state
            .main_outputs
            .iter_mut()
            .enumerate()
            .for_each(|(i, m)| {
                let pos = offset + i * 4;
                m.parse_quadlet(&raw[pos..(pos + 4)]);
            });
    }
}

/// Available source for sampling clock.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum ShellPhysOutSrc {
    Stream,
    Analog01,
    MixerOut01,
    MixerSend01,
}

impl Default for ShellPhysOutSrc {
    fn default() -> Self {
        Self::Stream
    }
}

impl From<u32> for ShellPhysOutSrc {
    fn from(val: u32) -> Self {
        match val {
            3 => Self::MixerSend01,
            2 => Self::MixerOut01,
            1 => Self::Analog01,
            _ => Self::Stream,
        }
    }
}

impl From<ShellPhysOutSrc> for u32 {
    fn from(src: ShellPhysOutSrc) -> Self {
        match src {
            ShellPhysOutSrc::MixerSend01 => 3,
            ShellPhysOutSrc::MixerOut01 => 2,
            ShellPhysOutSrc::Analog01 => 1,
            ShellPhysOutSrc::Stream => 0,
        }
    }
}

/// Format of optical input interface.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum ShellOptInputIfaceFormat {
    Adat0to7,
    Adat0to5Spdif01,
    Toslink01Spdif01,
}

impl Default for ShellOptInputIfaceFormat {
    fn default() -> Self {
        ShellOptInputIfaceFormat::Adat0to7
    }
}

impl From<u32> for ShellOptInputIfaceFormat {
    fn from(val: u32) -> Self {
        match val {
            2 => Self::Toslink01Spdif01,
            1 => Self::Adat0to5Spdif01,
            _ => Self::Adat0to7,
        }
    }
}

impl From<ShellOptInputIfaceFormat> for u32 {
    fn from(fmt: ShellOptInputIfaceFormat) -> Self {
        match fmt {
            ShellOptInputIfaceFormat::Toslink01Spdif01 => 2,
            ShellOptInputIfaceFormat::Adat0to5Spdif01 => 1,
            ShellOptInputIfaceFormat::Adat0to7 => 0,
        }
    }
}

/// Format of optical output interface.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum ShellOptOutputIfaceFormat {
    Adat,
    Spdif,
}

impl Default for ShellOptOutputIfaceFormat {
    fn default() -> Self {
        Self::Adat
    }
}

impl From<u32> for ShellOptOutputIfaceFormat {
    fn from(val: u32) -> Self {
        match val {
            1 => Self::Spdif,
            _ => Self::Adat,
        }
    }
}

impl From<ShellOptOutputIfaceFormat> for u32 {
    fn from(fmt: ShellOptOutputIfaceFormat) -> Self {
        match fmt {
            ShellOptOutputIfaceFormat::Spdif => 1,
            ShellOptOutputIfaceFormat::Adat => 0,
        }
    }
}

/// Source for optical output interface.
#[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
pub struct ShellOptOutputSrc(pub ShellPhysOutSrc);

/// Configuration for optical interface.
#[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
pub struct ShellOptIfaceConfig {
    pub input_format: ShellOptInputIfaceFormat,
    pub output_format: ShellOptOutputIfaceFormat,
    pub output_source: ShellOptOutputSrc,
}

impl ShellOptIfaceConfig {
    const SIZE: usize = 12;

    pub fn build(&self, raw: &mut [u8]) {
        assert_eq!(raw.len(), Self::SIZE, "Programming error...");

        self.input_format.build_quadlet(&mut raw[..4]);
        self.output_format.build_quadlet(&mut raw[4..8]);
        self.output_source.0.build_quadlet(&mut raw[8..]);
    }

    pub fn parse(&mut self, raw: &[u8]) {
        assert_eq!(raw.len(), Self::SIZE, "Programming error...");

        self.input_format.parse_quadlet(&raw[..4]);
        self.output_format.parse_quadlet(&raw[4..8]);
        self.output_source.0.parse_quadlet(&raw[8..]);
    }
}

/// Source of coaxial output interface.
#[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
pub struct ShellCoaxOutPairSrc(pub ShellPhysOutSrc);

/// Available source for sampling clock.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum ShellStandaloneClkSrc {
    Optical,
    Coaxial,
    Internal,
}

impl Default for ShellStandaloneClkSrc {
    fn default() -> Self {
        Self::Internal
    }
}

impl From<ShellStandaloneClkSrc> for u32 {
    fn from(src: ShellStandaloneClkSrc) -> Self {
        match src {
            ShellStandaloneClkSrc::Optical => 0,
            ShellStandaloneClkSrc::Coaxial => 1,
            ShellStandaloneClkSrc::Internal => 2,
        }
    }
}

impl From<u32> for ShellStandaloneClkSrc {
    fn from(val: u32) -> Self {
        match val {
            2 => ShellStandaloneClkSrc::Internal,
            1 => ShellStandaloneClkSrc::Coaxial,
            _ => ShellStandaloneClkSrc::Optical,
        }
    }
}

pub trait ShellStandaloneClkSpec: TcKonnektSegmentData {
    const STANDALONE_CLOCK_SOURCES: &'static [ShellStandaloneClkSrc];
}

/// Source pair of stream to mixer.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum ShellMixerStreamSrcPair {
    Stream01,
    Stream23,
    Stream45,
    Stream67,
    Stream89,
    Stream1011,
    Stream1213,
}

impl Default for ShellMixerStreamSrcPair {
    fn default() -> Self {
        ShellMixerStreamSrcPair::Stream01
    }
}

impl From<u32> for ShellMixerStreamSrcPair {
    fn from(val: u32) -> Self {
        match val {
            6 => Self::Stream1213,
            5 => Self::Stream1011,
            4 => Self::Stream89,
            3 => Self::Stream67,
            2 => Self::Stream45,
            1 => Self::Stream23,
            _ => Self::Stream01,
        }
    }
}

impl From<ShellMixerStreamSrcPair> for u32 {
    fn from(pair: ShellMixerStreamSrcPair) -> Self {
        match pair {
            ShellMixerStreamSrcPair::Stream01 => 0,
            ShellMixerStreamSrcPair::Stream23 => 1,
            ShellMixerStreamSrcPair::Stream45 => 2,
            ShellMixerStreamSrcPair::Stream67 => 3,
            ShellMixerStreamSrcPair::Stream89 => 4,
            ShellMixerStreamSrcPair::Stream1011 => 5,
            ShellMixerStreamSrcPair::Stream1213 => 6,
        }
    }
}

/// Specification for source pair of stream to mixer.
pub trait ShellMixerStreamSrcPairSpec {
    const MAXIMUM_STREAM_SRC_PAIR_COUNT: usize;
}

/// Target of knob.
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct ShellKnobTarget(pub u32);

/// Maximum value of knob.
pub trait ShellKnobTargetSpec {
    const HAS_SPDIF: bool;
    const HAS_EFFECTS: bool;
}

/// Target of knob 2.
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct ShellKnob2Target(pub u32);

/// Maximum value of knob 2.
pub trait ShellKnob2TargetSpec {
    const KNOB2_TARGET_COUNT: usize;
}

pub const SHELL_KNOB_SIZE: usize = 36;