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
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright (c) 2020 Takashi Sakamoto

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

use super::*;

/// Protocol implementation of Konnekt Live.
#[derive(Default)]
pub struct KliveProtocol;

/// Segment for knob. 0x0000..0x0027 (36 quads).
pub type KliveKnobSegment = TcKonnektSegment<KliveKnob>;
impl SegmentOperation<KliveKnob> for KliveProtocol {}

/// Segment for configuration. 0x0028..0x00ab (33 quads).
pub type KliveConfigSegment = TcKonnektSegment<KliveConfig>;
impl SegmentOperation<KliveConfig> for KliveProtocol {}

/// Segment for state of mixer. 0x00ac..0x0217 (91 quads).
pub type KliveMixerStateSegment = TcKonnektSegment<KliveMixerState>;
impl SegmentOperation<KliveMixerState> for KliveProtocol {}

/// Segment for state of reverb effect. 0x0218..0x025b. (17 quads)
pub type KliveReverbStateSegment = TcKonnektSegment<KliveReverbState>;
impl SegmentOperation<KliveReverbState> for KliveProtocol {}

/// Segment for states of channel strip effect. 0x0260..0x037f (72 quads).
pub type KliveChStripStatesSegment = TcKonnektSegment<KliveChStripStates>;
impl SegmentOperation<KliveChStripStates> for KliveProtocol {}

// NOTE: Segment for tuner. 0x0384..0x039c (8 quads).

/// Segment for mixer meter. 0x1068..0x10c3 (23 quads).
pub type KliveMixerMeterSegment = TcKonnektSegment<KliveMixerMeter>;
impl SegmentOperation<KliveMixerMeter> for KliveProtocol {}

/// Segment for state of hardware. 0x1008..0x1023 (7 quads).
pub type KliveHwStateSegment = TcKonnektSegment<KliveHwState>;
impl SegmentOperation<KliveHwState> for KliveProtocol {}

/// Segment for meter of reverb effect. 0x10c4..0x010db (6 quads).
pub type KliveReverbMeterSegment = TcKonnektSegment<KliveReverbMeter>;
impl SegmentOperation<KliveReverbMeter> for KliveProtocol {}

/// Segment for meters of channel strip effect. 0x10dc..0x1117 (15 quads).
pub type KliveChStripMetersSegment = TcKonnektSegment<KliveChStripMeters>;
impl SegmentOperation<KliveChStripMeters> for KliveProtocol {}

/// State of knob.
#[derive(Default, Debug)]
pub struct KliveKnob {
    pub target: ShellKnobTarget,
    pub knob2_target: ShellKnob2Target,
    pub prog: TcKonnektLoadedProgram,
    pub out_impedance: [OutputImpedance; 2],
}

impl TcKonnektSegmentData for KliveKnob {
    fn build(&self, raw: &mut [u8]) {
        self.target.0.build_quadlet(&mut raw[..4]);
        self.knob2_target.0.build_quadlet(&mut raw[4..8]);
        self.prog.build(&mut raw[8..12]);
        self.out_impedance.build_quadlet_block(&mut raw[12..20]);
    }

    fn parse(&mut self, raw: &[u8]) {
        self.target.0.parse_quadlet(&raw[..4]);
        self.knob2_target.0.parse_quadlet(&raw[4..8]);
        self.prog.parse(&raw[8..12]);
        self.out_impedance.parse_quadlet_block(&raw[12..20]);
    }
}

impl ShellKnobTargetSpec for KliveKnob {
    const HAS_SPDIF: bool = false;
    const HAS_EFFECTS: bool = false;
}

impl ShellKnob2TargetSpec for KliveKnob {
    const KNOB2_TARGET_COUNT: usize = 9;
}

impl TcKonnektSegmentSpec for TcKonnektSegment<KliveKnob> {
    const OFFSET: usize = 0x0004;
    const SIZE: usize = SHELL_KNOB_SIZE;
}

impl TcKonnektNotifiedSegmentSpec for TcKonnektSegment<KliveKnob> {
    const NOTIFY_FLAG: u32 = SHELL_KNOB_NOTIFY_FLAG;
}

/// Configuration.
#[derive(Default, Debug)]
pub struct KliveConfig {
    pub opt: ShellOptIfaceConfig,
    pub coax_out_src: ShellCoaxOutPairSrc,
    pub out_01_src: ShellPhysOutSrc,
    pub out_23_src: ShellPhysOutSrc,
    pub mixer_stream_src_pair: ShellMixerStreamSrcPair,
    pub standalone_src: ShellStandaloneClkSrc,
    pub standalone_rate: TcKonnektStandaloneClkRate,
    pub midi_sender: TcKonnektMidiSender,
}

impl ShellMixerStreamSrcPairSpec for KliveConfig {
    const MAXIMUM_STREAM_SRC_PAIR_COUNT: usize = 6;
}

impl ShellStandaloneClkSpec for KliveConfig {
    const STANDALONE_CLOCK_SOURCES: &'static [ShellStandaloneClkSrc] = &[
        ShellStandaloneClkSrc::Optical,
        ShellStandaloneClkSrc::Coaxial,
        ShellStandaloneClkSrc::Internal,
    ];
}

impl TcKonnektSegmentData for KliveConfig {
    fn build(&self, raw: &mut [u8]) {
        self.opt.build(&mut raw[..12]);
        self.coax_out_src.0.build_quadlet(&mut raw[12..16]);
        self.out_01_src.build_quadlet(&mut raw[16..20]);
        self.out_23_src.build_quadlet(&mut raw[20..24]);
        self.mixer_stream_src_pair.build_quadlet(&mut raw[24..28]);
        self.standalone_src.build_quadlet(&mut raw[28..32]);
        self.standalone_rate.build_quadlet(&mut raw[32..36]);
        self.midi_sender.build(&mut raw[84..120]);
    }

    fn parse(&mut self, raw: &[u8]) {
        self.opt.parse(&raw[..12]);
        self.coax_out_src.0.parse_quadlet(&raw[12..16]);
        self.out_01_src.parse_quadlet(&raw[16..20]);
        self.out_23_src.parse_quadlet(&raw[20..24]);
        self.mixer_stream_src_pair.parse_quadlet(&raw[24..28]);
        self.standalone_src.parse_quadlet(&raw[28..32]);
        self.standalone_rate.parse_quadlet(&raw[32..36]);
        self.midi_sender.parse(&raw[84..120]);
    }
}

impl TcKonnektSegmentSpec for TcKonnektSegment<KliveConfig> {
    const OFFSET: usize = 0x0028;
    const SIZE: usize = 132;
}

impl TcKonnektNotifiedSegmentSpec for TcKonnektSegment<KliveConfig> {
    const NOTIFY_FLAG: u32 = SHELL_CONFIG_NOTIFY_FLAG;
}

/// The source of channel strip effect.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum ChStripSrc {
    Stream01,
    Analog01,
    Analog23,
    Digital01,
    Digital23,
    Digital45,
    Digital67,
    MixerOutput,
    None,
}

impl Default for ChStripSrc {
    fn default() -> Self {
        ChStripSrc::None
    }
}

impl From<u32> for ChStripSrc {
    fn from(val: u32) -> Self {
        match val {
            0 => Self::Stream01,
            4 => Self::Analog01,
            5 => Self::Analog23,
            6 => Self::Digital01,
            7 => Self::Digital23,
            8 => Self::Digital45,
            9 => Self::Digital67,
            10 => Self::MixerOutput,
            _ => Self::None,
        }
    }
}

impl From<ChStripSrc> for u32 {
    fn from(src: ChStripSrc) -> Self {
        match src {
            ChStripSrc::Stream01 => 0,
            ChStripSrc::Analog01 => 4,
            ChStripSrc::Analog23 => 5,
            ChStripSrc::Digital01 => 6,
            ChStripSrc::Digital23 => 7,
            ChStripSrc::Digital45 => 8,
            ChStripSrc::Digital67 => 9,
            ChStripSrc::MixerOutput => 10,
            ChStripSrc::None => 11,
        }
    }
}

/// The type of channel strip effect.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum ChStripMode {
    FabrikC,
    RIAA1964,
    RIAA1987,
}

impl Default for ChStripMode {
    fn default() -> Self {
        ChStripMode::FabrikC
    }
}

impl From<u32> for ChStripMode {
    fn from(val: u32) -> Self {
        match val {
            2 => Self::RIAA1987,
            1 => Self::RIAA1964,
            _ => Self::FabrikC,
        }
    }
}

impl From<ChStripMode> for u32 {
    fn from(effect: ChStripMode) -> Self {
        match effect {
            ChStripMode::FabrikC => 0,
            ChStripMode::RIAA1964 => 1,
            ChStripMode::RIAA1987 => 2,
        }
    }
}

/// State of mixer.
#[derive(Debug)]
pub struct KliveMixerState {
    /// The common structure for state of mixer.
    pub mixer: ShellMixerState,
    /// The parameter of return from reverb effect.
    pub reverb_return: ShellReverbReturn,
    /// Whether to use channel strip effect as plugin. It results in output of channel strip effect
    /// on tx stream.
    pub use_ch_strip_as_plugin: bool,
    /// The source of channel strip effect.
    pub ch_strip_src: ChStripSrc,
    /// The type of channel effect. Fabrik-C, RIAA-1964, and RIAA-1987.
    pub ch_strip_mode: ChStripMode,
    /// Whether to use channel strip effect at middle sampling rate (88.2/96.0 kHz).
    pub use_reverb_at_mid_rate: bool,
    /// Whether to use mixer function.
    pub enabled: bool,
}

impl Default for KliveMixerState {
    fn default() -> Self {
        KliveMixerState {
            mixer: Self::create_mixer_state(),
            reverb_return: Default::default(),
            use_ch_strip_as_plugin: Default::default(),
            ch_strip_src: Default::default(),
            ch_strip_mode: Default::default(),
            use_reverb_at_mid_rate: Default::default(),
            enabled: Default::default(),
        }
    }
}

impl ShellMixerStateConvert for KliveMixerState {
    const MONITOR_SRC_MAP: [Option<ShellMixerMonitorSrcType>; SHELL_MIXER_MONITOR_SRC_COUNT] = [
        Some(ShellMixerMonitorSrcType::Stream),
        None,
        None,
        Some(ShellMixerMonitorSrcType::Spdif),
        Some(ShellMixerMonitorSrcType::Analog),
        Some(ShellMixerMonitorSrcType::Analog),
        Some(ShellMixerMonitorSrcType::AdatSpdif),
        Some(ShellMixerMonitorSrcType::Adat),
        Some(ShellMixerMonitorSrcType::Adat),
        Some(ShellMixerMonitorSrcType::Adat),
    ];

    fn state(&self) -> &ShellMixerState {
        &self.mixer
    }

    fn state_mut(&mut self) -> &mut ShellMixerState {
        &mut self.mixer
    }
}

impl TcKonnektSegmentData for KliveMixerState {
    fn build(&self, raw: &mut [u8]) {
        ShellMixerStateConvert::build(self, raw);

        self.reverb_return.build(&mut raw[316..328]);
        self.use_ch_strip_as_plugin
            .build_quadlet(&mut raw[328..332]);
        self.ch_strip_src.build_quadlet(&mut raw[332..336]);
        self.ch_strip_mode.build_quadlet(&mut raw[336..340]);
        self.use_reverb_at_mid_rate
            .build_quadlet(&mut raw[340..344]);
        self.enabled.build_quadlet(&mut raw[344..348]);
    }

    fn parse(&mut self, raw: &[u8]) {
        ShellMixerStateConvert::parse(self, raw);

        self.reverb_return.parse(&raw[316..328]);
        self.use_ch_strip_as_plugin.parse_quadlet(&raw[328..332]);
        self.ch_strip_src.parse_quadlet(&raw[332..336]);
        self.ch_strip_mode.parse_quadlet(&raw[336..340]);
        self.use_reverb_at_mid_rate.parse_quadlet(&raw[340..344]);
        self.enabled.parse_quadlet(&raw[344..348]);
    }
}

impl TcKonnektSegmentSpec for TcKonnektSegment<KliveMixerState> {
    const OFFSET: usize = 0x00ac;
    const SIZE: usize = ShellMixerState::SIZE + 48;
}

impl TcKonnektNotifiedSegmentSpec for TcKonnektSegment<KliveMixerState> {
    const NOTIFY_FLAG: u32 = SHELL_MIXER_NOTIFY_FLAG;
}

#[derive(Default, Debug)]
pub struct KliveReverbState(pub ReverbState);

impl TcKonnektSegmentData for KliveReverbState {
    fn build(&self, raw: &mut [u8]) {
        self.0.build(raw)
    }

    fn parse(&mut self, raw: &[u8]) {
        self.0.parse(raw)
    }
}

impl TcKonnektSegmentSpec for TcKonnektSegment<KliveReverbState> {
    const OFFSET: usize = 0x0218;
    const SIZE: usize = ReverbState::SIZE;
}

impl TcKonnektNotifiedSegmentSpec for TcKonnektSegment<KliveReverbState> {
    const NOTIFY_FLAG: u32 = SHELL_REVERB_NOTIFY_FLAG;
}

#[derive(Default, Debug)]
pub struct KliveChStripStates(pub [ChStripState; SHELL_CH_STRIP_COUNT]);

impl TcKonnektSegmentData for KliveChStripStates {
    fn build(&self, raw: &mut [u8]) {
        self.0.build(raw)
    }

    fn parse(&mut self, raw: &[u8]) {
        self.0.parse(raw)
    }
}

impl TcKonnektSegmentSpec for TcKonnektSegment<KliveChStripStates> {
    const OFFSET: usize = 0x0260;
    const SIZE: usize = ChStripState::SIZE * SHELL_CH_STRIP_COUNT + 4;
}

impl TcKonnektNotifiedSegmentSpec for TcKonnektSegment<KliveChStripStates> {
    const NOTIFY_FLAG: u32 = SHELL_CH_STRIP_NOTIFY_FLAG;
}

#[derive(Default, Debug)]
pub struct KliveHwState(pub ShellHwState);

impl TcKonnektSegmentData for KliveHwState {
    fn build(&self, raw: &mut [u8]) {
        self.0.build(raw)
    }

    fn parse(&mut self, raw: &[u8]) {
        self.0.parse(raw)
    }
}

impl TcKonnektSegmentSpec for TcKonnektSegment<KliveHwState> {
    const OFFSET: usize = 0x1008;
    const SIZE: usize = ShellHwState::SIZE;
}

impl TcKonnektNotifiedSegmentSpec for TcKonnektSegment<KliveHwState> {
    const NOTIFY_FLAG: u32 = SHELL_HW_STATE_NOTIFY_FLAG;
}

const KLIVE_METER_ANALOG_INPUT_COUNT: usize = 4;
const KLIVE_METER_DIGITAL_INPUT_COUNT: usize = 8;

#[derive(Debug)]
pub struct KliveMixerMeter(pub ShellMixerMeter);

impl Default for KliveMixerMeter {
    fn default() -> Self {
        KliveMixerMeter(Self::create_meter_state())
    }
}

impl ShellMixerMeterConvert for KliveMixerMeter {
    const ANALOG_INPUT_COUNT: usize = KLIVE_METER_ANALOG_INPUT_COUNT;
    const DIGITAL_INPUT_COUNT: usize = KLIVE_METER_DIGITAL_INPUT_COUNT;

    fn meter(&self) -> &ShellMixerMeter {
        &self.0
    }

    fn meter_mut(&mut self) -> &mut ShellMixerMeter {
        &mut self.0
    }
}

impl TcKonnektSegmentData for KliveMixerMeter {
    fn build(&self, raw: &mut [u8]) {
        ShellMixerMeterConvert::build(self, raw)
    }

    fn parse(&mut self, raw: &[u8]) {
        ShellMixerMeterConvert::parse(self, raw)
    }
}

impl TcKonnektSegmentSpec for TcKonnektSegment<KliveMixerMeter> {
    const OFFSET: usize = 0x1068;
    const SIZE: usize = ShellMixerMeter::SIZE;
}

#[derive(Default, Debug)]
pub struct KliveReverbMeter(pub ReverbMeter);

impl TcKonnektSegmentData for KliveReverbMeter {
    fn build(&self, raw: &mut [u8]) {
        self.0.build(raw)
    }

    fn parse(&mut self, raw: &[u8]) {
        self.0.parse(raw)
    }
}

impl TcKonnektSegmentSpec for TcKonnektSegment<KliveReverbMeter> {
    const OFFSET: usize = 0x10c4;
    const SIZE: usize = ReverbMeter::SIZE;
}

#[derive(Default, Debug)]
pub struct KliveChStripMeters(pub [ChStripMeter; SHELL_CH_STRIP_COUNT]);

impl TcKonnektSegmentData for KliveChStripMeters {
    fn build(&self, raw: &mut [u8]) {
        self.0.build(raw)
    }

    fn parse(&mut self, raw: &[u8]) {
        self.0.parse(raw)
    }
}

impl TcKonnektSegmentSpec for TcKonnektSegment<KliveChStripMeters> {
    const OFFSET: usize = 0x10dc;
    const SIZE: usize = ChStripMeter::SIZE * SHELL_CH_STRIP_COUNT + 4;
}

/// Impedance of output.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum OutputImpedance {
    Unbalance,
    Balance,
}

impl Default for OutputImpedance {
    fn default() -> Self {
        Self::Unbalance
    }
}

impl From<u32> for OutputImpedance {
    fn from(val: u32) -> Self {
        match val {
            0 => Self::Unbalance,
            _ => Self::Balance,
        }
    }
}

impl From<OutputImpedance> for u32 {
    fn from(impedance: OutputImpedance) -> Self {
        match impedance {
            OutputImpedance::Unbalance => 0,
            OutputImpedance::Balance => 1,
        }
    }
}