firewire-dice-protocols 0.3.0

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

//! Protocol defined by TC Electronic for Desktop Konnekt 6.
//!
//! The module includes structure, enumeration, and trait and its implementation for protocol
//! defined by TC Electronic for Desktop Konnekt 6.
//!
//! ## Diagram of internal signal flow
//!
//! ```text
//!                  +-------+
//! XLR input -----> |       |
//!                  | input | -> analog input-1/2 -----------> stream-output-1/2
//! Phone input 1 -> | scene |          |          (unused) --> stream-output-3/4
//! Phone input 2 -> |       |          |          (unused) --> stream-output-5/6
//!                  +-------+          v
//!                                ++=======++
//!                                || 4 x 2 ||
//!                         +----> ||       || -----+---------> analog-output-1/2 (main)
//!                         |      || mixer ||      |
//!                         |      ++=======++      |
//! stream-input-1/2 -------+                       |
//! stream-input-3/4 -------------------------------or--------> analog-output-3/4 (headphone)
//! stream-input-5/6 --> (unused)
//! ```
//!
//! Reverb effect is not implemented in hardware, while control items are in hardware surface.

use super::tcelectronic::*;

const DESKTOP_HW_STATE_NOTIFY_FLAG: u32 = 0x00010000;
const DESKTOP_CONFIG_NOTIFY_FLAG: u32 = 0x00020000;
const DESKTOP_MIXER_STATE_NOTIFY_FLAG: u32 = 0x00040000;
const DESKTOP_PANEL_NOTIFY_FLAG: u32 = 0x00080000;

/// Protocol implementation of Desktop Konnekt 6.
#[derive(Default, Debug)]
pub struct Desktopk6Protocol;

impl TcatOperation for Desktopk6Protocol {}

impl TcatGlobalSectionSpecification for Desktopk6Protocol {}

/// Segment for panel. 0x0008..0x0097 (36 quads).
pub type Desktopk6HwStateSegment = TcKonnektSegment<DesktopHwState>;

/// Segment for configuration. 0x0098..0x00b7 (8 quads).
pub type Desktopk6ConfigSegment = TcKonnektSegment<DesktopConfig>;

/// Segment for state of mixer. 0x00b8..0x0367 (172 quads).
pub type Desktopk6MixerStateSegment = TcKonnektSegment<DesktopMixerState>;

/// Segment for panel. 0x2008..0x2047 (15 quads).
pub type Desktopk6PanelSegment = TcKonnektSegment<DesktopPanel>;

/// Segment for meter. 0x20e4..0x213f (23 quads).
pub type Desktopk6MeterSegment = TcKonnektSegment<DesktopMeter>;

macro_rules! segment_default {
    ($p:ty, $t:ty) => {
        impl Default for TcKonnektSegment<$t> {
            fn default() -> Self {
                Self {
                    data: <$t>::default(),
                    raw: vec![0; <$p as TcKonnektSegmentSerdes<$t>>::SIZE],
                }
            }
        }
    };
}

segment_default!(Desktopk6Protocol, DesktopHwState);
segment_default!(Desktopk6Protocol, DesktopConfig);
segment_default!(Desktopk6Protocol, DesktopMixerState);
segment_default!(Desktopk6Protocol, DesktopPanel);
segment_default!(Desktopk6Protocol, DesktopMeter);

/// Target of meter.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum MeterTarget {
    /// Analog input 1/2.
    Input,
    /// Mixer output 1/2 before volume adjustment.
    Pre,
    /// Mixer output 1/2 after volume adjustment.
    Post,
}

impl Default for MeterTarget {
    fn default() -> Self {
        Self::Input
    }
}

const METER_TARGETS: &[MeterTarget] = &[MeterTarget::Input, MeterTarget::Pre, MeterTarget::Post];

const METER_TARGET_LABEL: &str = "meter-target";

fn serialize_meter_target(target: &MeterTarget, raw: &mut [u8]) -> Result<(), String> {
    serialize_position(METER_TARGETS, target, raw, METER_TARGET_LABEL)
}

fn deserialize_meter_target(target: &mut MeterTarget, raw: &[u8]) -> Result<(), String> {
    deserialize_position(METER_TARGETS, target, raw, METER_TARGET_LABEL)
}

/// Current scene for analog input 1/2.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum InputScene {
    /// Microphone and instrument.
    MicInst,
    /// Two instruments.
    DualInst,
    /// Two line inputs for stereo.
    StereoIn,
}

impl Default for InputScene {
    fn default() -> Self {
        Self::MicInst
    }
}

const INPUT_SCENES: &[InputScene] = &[
    InputScene::MicInst,
    InputScene::DualInst,
    InputScene::StereoIn,
];

const INPUT_SCENE_LABEL: &str = "inputscene";

fn serialize_input_scene(scene: &InputScene, raw: &mut [u8]) -> Result<(), String> {
    serialize_position(INPUT_SCENES, scene, raw, INPUT_SCENE_LABEL)
}

fn deserialize_input_scene(scene: &mut InputScene, raw: &[u8]) -> Result<(), String> {
    deserialize_position(INPUT_SCENES, scene, raw, INPUT_SCENE_LABEL)
}

/// General state of hardware.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct DesktopHwState {
    /// The target of meter in surface.
    pub meter_target: MeterTarget,
    /// Use mixer output as monaural.
    pub mixer_output_monaural: bool,
    /// Whether to adjust volume of headphone output by main knob.
    pub knob_assign_to_hp: bool,
    /// Whether to dim main output.
    pub mixer_output_dim_enabled: bool,
    /// The volume of main output if dimmed between -1000..-60. (-94.0..-6.0 dB)
    pub mixer_output_dim_volume: i32,
    /// The use case of analog input 1/2.
    pub input_scene: InputScene,
    /// Multiplex reverb signal to stream input 1/2 in advance.
    pub reverb_to_master: bool,
    /// Multiplex reverb signal to stream input 3/4 in advance.
    pub reverb_to_hp: bool,
    /// Turn on backlight in master knob.
    pub master_knob_backlight: bool,
    /// Phantom powering in microphone 1.
    pub mic_0_phantom: bool,
    /// Signal boost in microphone 1 by 12 dB.
    pub mic_0_boost: bool,
}

impl DesktopHwState {
    const REVERB_TO_MAIN_MASK: u32 = 0x00000001;
    const REVERB_TO_HP_MASK: u32 = 0x00000002;
}

impl TcKonnektSegmentSerdes<DesktopHwState> for Desktopk6Protocol {
    const NAME: &'static str = "hardware-state";
    const OFFSET: usize = 0x0008;
    const SIZE: usize = 144;

    fn serialize(params: &DesktopHwState, raw: &mut [u8]) -> Result<(), String> {
        serialize_meter_target(&params.meter_target, &mut raw[..4])?;
        serialize_bool(&params.mixer_output_monaural, &mut raw[4..8]);
        serialize_bool(&params.knob_assign_to_hp, &mut raw[8..12]);
        serialize_bool(&params.mixer_output_dim_enabled, &mut raw[12..16]);
        serialize_i32(&params.mixer_output_dim_volume, &mut raw[16..20]);
        serialize_input_scene(&params.input_scene, &mut raw[20..24])?;

        let mut val = 0;
        if params.reverb_to_master {
            val |= DesktopHwState::REVERB_TO_MAIN_MASK;
        }
        if params.reverb_to_hp {
            val |= DesktopHwState::REVERB_TO_HP_MASK;
        }
        serialize_u32(&val, &mut raw[28..32]);

        serialize_bool(&params.master_knob_backlight, &mut raw[32..36]);
        serialize_bool(&params.mic_0_phantom, &mut raw[52..56]);
        serialize_bool(&params.mic_0_boost, &mut raw[56..60]);

        Ok(())
    }

    fn deserialize(params: &mut DesktopHwState, raw: &[u8]) -> Result<(), String> {
        deserialize_meter_target(&mut params.meter_target, &raw[..4])?;
        deserialize_bool(&mut params.mixer_output_monaural, &raw[4..8]);
        deserialize_bool(&mut params.knob_assign_to_hp, &raw[8..12]);
        deserialize_bool(&mut params.mixer_output_dim_enabled, &raw[12..16]);
        deserialize_i32(&mut params.mixer_output_dim_volume, &raw[16..20]);
        deserialize_input_scene(&mut params.input_scene, &raw[20..24])?;

        let mut val = 0;
        deserialize_u32(&mut val, &raw[28..32]);
        params.reverb_to_master = val & DesktopHwState::REVERB_TO_MAIN_MASK > 0;
        params.reverb_to_hp = val & DesktopHwState::REVERB_TO_HP_MASK > 0;

        deserialize_bool(&mut params.master_knob_backlight, &raw[32..36]);
        deserialize_bool(&mut params.mic_0_phantom, &raw[52..56]);
        deserialize_bool(&mut params.mic_0_boost, &raw[56..60]);

        Ok(())
    }
}

impl TcKonnektMutableSegmentOperation<DesktopHwState> for Desktopk6Protocol {}

impl TcKonnektNotifiedSegmentOperation<DesktopHwState> for Desktopk6Protocol {
    const NOTIFY_FLAG: u32 = DESKTOP_HW_STATE_NOTIFY_FLAG;
}

/// Configuration.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct DesktopConfig {
    /// Sampling rate at standalone mode.
    pub standalone_rate: TcKonnektStandaloneClockRate,
}

impl TcKonnektSegmentSerdes<DesktopConfig> for Desktopk6Protocol {
    const NAME: &'static str = "configuration";
    const OFFSET: usize = 0x0098;
    const SIZE: usize = 32;

    fn serialize(params: &DesktopConfig, raw: &mut [u8]) -> Result<(), String> {
        serialize_standalone_clock_rate(&params.standalone_rate, &mut raw[4..8])
    }

    fn deserialize(params: &mut DesktopConfig, raw: &[u8]) -> Result<(), String> {
        deserialize_standalone_clock_rate(&mut params.standalone_rate, &raw[4..8])
    }
}

impl TcKonnektMutableSegmentOperation<DesktopConfig> for Desktopk6Protocol {}

impl TcKonnektNotifiedSegmentOperation<DesktopConfig> for Desktopk6Protocol {
    const NOTIFY_FLAG: u32 = DESKTOP_CONFIG_NOTIFY_FLAG;
}

impl AsRef<TcKonnektStandaloneClockRate> for DesktopConfig {
    fn as_ref(&self) -> &TcKonnektStandaloneClockRate {
        &self.standalone_rate
    }
}

impl AsMut<TcKonnektStandaloneClockRate> for DesktopConfig {
    fn as_mut(&mut self) -> &mut TcKonnektStandaloneClockRate {
        &mut self.standalone_rate
    }
}

/// Source of headphone.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum DesktopHpSrc {
    /// Stream input 3/4.
    Stream23,
    /// Mixer output 1/2.
    Mixer01,
}

impl Default for DesktopHpSrc {
    fn default() -> Self {
        Self::Stream23
    }
}

fn serialize_hp_src(src: &DesktopHpSrc, raw: &mut [u8]) -> Result<(), String> {
    assert!(raw.len() >= 4);

    let val = match src {
        DesktopHpSrc::Stream23 => 0x05,
        DesktopHpSrc::Mixer01 => 0x0b,
    };

    serialize_u32(&val, raw);

    Ok(())
}

fn deserialize_hp_src(src: &mut DesktopHpSrc, raw: &[u8]) -> Result<(), String> {
    assert!(raw.len() >= 4);

    let mut val = 0u32;
    deserialize_u32(&mut val, raw);

    *src = match val {
        0x05 => DesktopHpSrc::Stream23,
        _ => DesktopHpSrc::Mixer01,
    };

    Ok(())
}

/// State of mixer.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct DesktopMixerState {
    /// The input level of microphone 1 and phone 1 for instrument, between -1000 and 0 (-94.0 and
    /// 0.0 dB)
    pub mic_inst_level: [i32; 2],
    /// The LR balance of microphone 1 and phone 1 for instrument, between -50 and 50.
    pub mic_inst_pan: [i32; 2],
    /// The level to send from microphone 1 and phone 1 for instrument, between -1000 and 0 (-94.0
    /// and 0.0 dB)
    pub mic_inst_send: [i32; 2],
    /// The input level of both phone 1 and 2 for instrument, between -1000 and 0 (-94.0 and
    /// 0.0 dB)
    pub dual_inst_level: [i32; 2],
    /// The LR balance  of both phone 1 and 2 for instrument, between -50 and 50.
    pub dual_inst_pan: [i32; 2],
    /// The level to send from both phone 1 and 2 for instrument, between -1000 and 0 (-94.0 and
    /// 0.0 dB)
    pub dual_inst_send: [i32; 2],
    /// The input level of both phone 1 and 2 for line, between -1000 and 0 (-94.0 and 0.0 dB)
    pub stereo_in_level: i32,
    /// The LR balance of both phone 1 and 2 for line, between -50 and 50.
    pub stereo_in_pan: i32,
    /// The level to send of both phone 1 and 2 for line, between -1000 and 0 (-94.0 and 0.0 dB)
    pub stereo_in_send: i32,
    /// The source of headphone output.
    pub hp_src: DesktopHpSrc,
}

impl TcKonnektSegmentSerdes<DesktopMixerState> for Desktopk6Protocol {
    const NAME: &'static str = "mixer-state";
    const OFFSET: usize = 0x00b8;
    const SIZE: usize = 688;

    fn serialize(params: &DesktopMixerState, raw: &mut [u8]) -> Result<(), String> {
        serialize_i32(&params.mic_inst_level[0], &mut raw[12..16]);
        serialize_i32(&params.mic_inst_pan[0], &mut raw[16..20]);
        serialize_i32(&params.mic_inst_send[0], &mut raw[20..24]);
        serialize_i32(&params.mic_inst_level[1], &mut raw[28..32]);
        serialize_i32(&params.mic_inst_pan[1], &mut raw[32..36]);
        serialize_i32(&params.mic_inst_send[1], &mut raw[40..44]);

        serialize_i32(&params.dual_inst_level[0], &mut raw[228..232]);
        serialize_i32(&params.dual_inst_pan[0], &mut raw[232..236]);
        serialize_i32(&params.dual_inst_send[0], &mut raw[240..244]);
        serialize_i32(&params.dual_inst_level[1], &mut raw[248..252]);
        serialize_i32(&params.dual_inst_pan[1], &mut raw[252..256]);
        serialize_i32(&params.dual_inst_send[1], &mut raw[260..264]);

        serialize_i32(&params.stereo_in_level, &mut raw[444..448]);
        serialize_i32(&params.stereo_in_pan, &mut raw[448..452]);
        serialize_i32(&params.stereo_in_send, &mut raw[452..456]);

        serialize_hp_src(&params.hp_src, &mut raw[648..652])
    }

    fn deserialize(params: &mut DesktopMixerState, raw: &[u8]) -> Result<(), String> {
        deserialize_i32(&mut params.mic_inst_level[0], &raw[12..16]);
        deserialize_i32(&mut params.mic_inst_pan[0], &raw[16..20]);
        deserialize_i32(&mut params.mic_inst_send[0], &raw[20..24]);
        deserialize_i32(&mut params.mic_inst_level[1], &raw[28..32]);
        deserialize_i32(&mut params.mic_inst_pan[1], &raw[32..36]);
        deserialize_i32(&mut params.mic_inst_send[1], &raw[40..44]);

        deserialize_i32(&mut params.dual_inst_level[0], &raw[228..232]);
        deserialize_i32(&mut params.dual_inst_pan[0], &raw[232..236]);
        deserialize_i32(&mut params.dual_inst_send[0], &raw[240..244]);
        deserialize_i32(&mut params.dual_inst_level[1], &raw[248..252]);
        deserialize_i32(&mut params.dual_inst_pan[1], &raw[252..256]);
        deserialize_i32(&mut params.dual_inst_send[1], &raw[260..264]);

        deserialize_i32(&mut params.stereo_in_level, &raw[444..448]);
        deserialize_i32(&mut params.stereo_in_pan, &raw[448..452]);
        deserialize_i32(&mut params.stereo_in_send, &raw[452..456]);

        deserialize_hp_src(&mut params.hp_src, &raw[648..652])
    }
}

impl TcKonnektMutableSegmentOperation<DesktopMixerState> for Desktopk6Protocol {}

impl TcKonnektNotifiedSegmentOperation<DesktopMixerState> for Desktopk6Protocol {
    const NOTIFY_FLAG: u32 = DESKTOP_MIXER_STATE_NOTIFY_FLAG;
}

/// Panel in hardware surface.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct DesktopPanel {
    /// The count of panel button to push.
    pub panel_button_count: u32,
    /// The value of main knob, between -1000 and 0.
    pub main_knob_value: i32,
    /// The value of phone knob, between -1000 and 0.
    pub phone_knob_value: i32,
    /// The value of mix knob, between 0 and 1000.
    pub mix_knob_value: u32,
    /// The state of reverb LED.
    pub reverb_led_on: bool,
    /// The value of reverb knob, between -1000 and 0.
    pub reverb_knob_value: i32,
    /// The state of FireWire LED.
    pub firewire_led: FireWireLedState,
}

impl TcKonnektSegmentSerdes<DesktopPanel> for Desktopk6Protocol {
    const NAME: &'static str = "hardware-panel";
    const OFFSET: usize = 0x2008;
    const SIZE: usize = 64;

    fn serialize(params: &DesktopPanel, raw: &mut [u8]) -> Result<(), String> {
        serialize_u32(&params.panel_button_count, &mut raw[..4]);
        serialize_i32(&params.main_knob_value, &mut raw[4..8]);
        serialize_i32(&params.phone_knob_value, &mut raw[8..12]);
        serialize_u32(&params.mix_knob_value, &mut raw[12..16]);
        serialize_bool(&params.reverb_led_on, &mut raw[16..20]);
        serialize_i32(&params.reverb_knob_value, &mut raw[24..28]);
        serialize_fw_led_state(&params.firewire_led, &mut raw[36..40])?;
        Ok(())
    }

    fn deserialize(params: &mut DesktopPanel, raw: &[u8]) -> Result<(), String> {
        deserialize_u32(&mut params.panel_button_count, &raw[..4]);
        deserialize_i32(&mut params.main_knob_value, &raw[4..8]);
        deserialize_i32(&mut params.phone_knob_value, &raw[8..12]);
        deserialize_u32(&mut params.mix_knob_value, &raw[12..16]);
        deserialize_bool(&mut params.reverb_led_on, &raw[16..20]);
        deserialize_i32(&mut params.reverb_knob_value, &raw[24..28]);
        deserialize_fw_led_state(&mut params.firewire_led, &raw[36..40])?;
        Ok(())
    }
}

impl TcKonnektMutableSegmentOperation<DesktopPanel> for Desktopk6Protocol {}

impl TcKonnektNotifiedSegmentOperation<DesktopPanel> for Desktopk6Protocol {
    const NOTIFY_FLAG: u32 = DESKTOP_PANEL_NOTIFY_FLAG;
}

impl AsRef<FireWireLedState> for DesktopPanel {
    fn as_ref(&self) -> &FireWireLedState {
        &self.firewire_led
    }
}

impl AsMut<FireWireLedState> for DesktopPanel {
    fn as_mut(&mut self) -> &mut FireWireLedState {
        &mut self.firewire_led
    }
}

/// Hardware metering.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct DesktopMeter {
    pub analog_inputs: [i32; 2],
    pub mixer_outputs: [i32; 2],
    pub stream_inputs: [i32; 2],
}

impl TcKonnektSegmentSerdes<DesktopMeter> for Desktopk6Protocol {
    const NAME: &'static str = "hardware-meter";
    const OFFSET: usize = 0x20e4;
    const SIZE: usize = 92;

    fn serialize(params: &DesktopMeter, raw: &mut [u8]) -> Result<(), String> {
        serialize_i32(&params.analog_inputs[0], &mut raw[..4]);
        serialize_i32(&params.analog_inputs[1], &mut raw[4..8]);
        serialize_i32(&params.mixer_outputs[0], &mut raw[40..44]);
        serialize_i32(&params.mixer_outputs[1], &mut raw[44..48]);
        serialize_i32(&params.stream_inputs[0], &mut raw[48..52]);
        serialize_i32(&params.stream_inputs[1], &mut raw[52..56]);
        Ok(())
    }

    fn deserialize(params: &mut DesktopMeter, raw: &[u8]) -> Result<(), String> {
        deserialize_i32(&mut params.analog_inputs[0], &raw[..4]);
        deserialize_i32(&mut params.analog_inputs[1], &raw[4..8]);
        deserialize_i32(&mut params.mixer_outputs[0], &raw[40..44]);
        deserialize_i32(&mut params.mixer_outputs[1], &raw[44..48]);
        deserialize_i32(&mut params.stream_inputs[0], &raw[48..52]);
        deserialize_i32(&mut params.stream_inputs[1], &raw[52..56]);
        Ok(())
    }
}