-
Notifications
You must be signed in to change notification settings - Fork 5
/
apogee.rs
1416 lines (1246 loc) · 46.9 KB
/
apogee.rs
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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright (c) 2021 Takashi Sakamoto
//! Protocol defined by Apogee Electronics for Duet FireWire.
//!
//! The module includes protocol implementation defined by Apogee Electronics for Duet FireWire.
//!
//! ## Diagram of internal signal flow for Apogee Duet FireWire
//!
//! ```text
//!
//! xlr-input-1 ----> or ------> analog-input-1 --+-----+---------------> stream-output-1/2
//! ^ | |
//! xlr-input-2 ------|-> or --> analog-input-2 --|--+--+
//! | ^ | |
//! phone-input-1 --- + | | |
//! | v v
//! phone-input-2 --------+ ++=========++
//! || mixer ||
//! stream-input-1/2 -----------------------> || || ------------> analog-output-1/2
//! || 4 x 2 ||
//! ++=========++
//! ```
use super::*;
/// Protocol implementation for Duet FireWire.
#[derive(Default, Debug)]
pub struct DuetFwProtocol;
impl OxfordOperation for DuetFwProtocol {}
impl OxfwStreamFormatOperation<OxfwAvc> for DuetFwProtocol {}
/// Serializer and deserializer for parameters.
pub trait DuetFwParamsSerdes<T> {
/// Build commands for AV/C status operation.
fn default_cmds_for_params(cmds: &mut Vec<VendorCmd>);
/// Deserialize parameters for AV/C status operation.
fn cmds_to_params(params: &mut T, cmds: &[VendorCmd]);
/// Serialize parameters for AV/C control operation.
fn cmds_from_params(params: &T, cmds: &mut Vec<VendorCmd>);
}
impl<T> OxfwFcpParamsOperation<OxfwAvc, T> for DuetFwProtocol
where
DuetFwProtocol: DuetFwParamsSerdes<T>,
{
fn cache(avc: &mut OxfwAvc, params: &mut T, timeout_ms: u32) -> Result<(), Error> {
let mut cmds = Vec::new();
Self::default_cmds_for_params(&mut cmds);
let mut states = Vec::new();
cmds.into_iter().try_for_each(|cmd| {
let mut op = ApogeeCmd::new(cmd);
avc.status(&AvcAddr::Unit, &mut op, timeout_ms)
.map(|_| states.push(op.cmd))
})?;
Self::cmds_to_params(params, &states);
Ok(())
}
}
impl<T> OxfwFcpMutableParamsOperation<OxfwAvc, T> for DuetFwProtocol
where
T: Copy,
DuetFwProtocol: DuetFwParamsSerdes<T>,
{
fn update(avc: &mut OxfwAvc, params: &T, prev: &mut T, timeout_ms: u32) -> Result<(), Error> {
let mut new = Vec::new();
Self::cmds_from_params(params, &mut new);
let mut old = Vec::new();
Self::cmds_from_params(prev, &mut old);
new.iter()
.zip(&old)
.filter(|(n, o)| !n.eq(o))
.try_for_each(|(cmd, _)| {
let mut op = ApogeeCmd::new(*cmd);
avc.control(&AvcAddr::Unit, &mut op, timeout_ms)
})
.map(|_| *prev = *params)
}
}
/// Specification of meter.
pub trait DuetFwMeterSpecification<T> {
/// Offset for raw meter data.
const OFFSET: usize;
/// Size for raw meter data.
const SIZE: usize;
/// Deserialize for meter.
fn deserialize_meter(params: &mut T, raw: &[u8]);
}
/// Operation for meter.
pub trait DuetFwMeterOperation<T>: DuetFwMeterSpecification<T> {
fn cache_meter(
req: &FwReq,
node: &FwNode,
params: &mut T,
timeout_ms: u32,
) -> Result<(), Error> {
let mut raw = vec![0u8; Self::SIZE];
req.transaction(
node,
FwTcode::ReadBlockRequest,
(METER_OFFSET_BASE + Self::OFFSET) as u64,
raw.len(),
&mut raw,
timeout_ms,
)
.map(|_| Self::deserialize_meter(params, &raw))
}
}
impl<O, T> DuetFwMeterOperation<T> for O where O: DuetFwMeterSpecification<T> {}
const APOGEE_OUI: [u8; 3] = [0x00, 0x03, 0xdb];
const METER_OFFSET_BASE: usize = 0xfffff0080000;
const METER_INPUT_OFFSET: usize = 0x0004;
const METER_MIXER_OFFSET: usize = 0x0404;
/// The state of meter for analog input.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct DuetFwInputMeterState(pub [i32; 2]);
impl DuetFwMeterSpecification<DuetFwInputMeterState> for DuetFwProtocol {
const OFFSET: usize = METER_INPUT_OFFSET;
const SIZE: usize = 8;
fn deserialize_meter(state: &mut DuetFwInputMeterState, raw: &[u8]) {
let mut quadlet = [0; 4];
state.0.iter_mut().enumerate().for_each(|(i, level)| {
let pos = i * 4;
quadlet.copy_from_slice(&raw[pos..(pos + 4)]);
*level = i32::from_be_bytes(quadlet);
});
}
}
impl DuetFwProtocol {
/// The minimum value of detected level in meter state.
pub const METER_LEVEL_MIN: i32 = 0;
/// The maximum value of detected level in meter state.
pub const METER_LEVEL_MAX: i32 = i32::MAX;
/// The step value of detected level in meter state.
pub const METER_LEVEL_STEP: i32 = 0x100;
}
/// The state of meter for mixer source/output.
#[derive(Default, Debug)]
pub struct DuetFwMixerMeterState {
/// Detected level of stream inputs.
pub stream_inputs: [i32; 2],
/// Detected level of mixer outputs.
pub mixer_outputs: [i32; 2],
}
impl DuetFwMeterSpecification<DuetFwMixerMeterState> for DuetFwProtocol {
const OFFSET: usize = METER_MIXER_OFFSET;
const SIZE: usize = 16;
fn deserialize_meter(state: &mut DuetFwMixerMeterState, raw: &[u8]) {
let mut quadlet = [0; 4];
state
.stream_inputs
.iter_mut()
.chain(&mut state.mixer_outputs)
.enumerate()
.for_each(|(i, meter)| {
let pos = i * 4;
quadlet.copy_from_slice(&raw[pos..(pos + 4)]);
*meter = i32::from_be_bytes(quadlet);
});
}
}
/// Target of knob.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum DuetFwKnobTarget {
/// The pair of outputs.
OutputPair0,
/// The 1st channel (left) of input pair.
InputPair0,
/// The 2nd channel (right) of input pair.
InputPair1,
}
impl Default for DuetFwKnobTarget {
fn default() -> Self {
Self::OutputPair0
}
}
/// The state of rotary knob.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct DuetFwKnobState {
/// Whether to mute pair of outputs.
pub output_mute: bool,
/// Target to control by knob.
pub target: DuetFwKnobTarget,
/// The value of output volume.
pub output_volume: u8,
/// The value of input gains.
pub input_gains: [u8; 2],
}
fn default_cmds_for_knob_params(cmds: &mut Vec<VendorCmd>) {
cmds.push(VendorCmd::HwState(Default::default()));
}
fn cmds_from_knob_params(params: &DuetFwKnobState, cmds: &mut Vec<VendorCmd>) {
let mut raw = [0; 11];
raw[0] = params.output_mute as u8;
raw[1] = match params.target {
DuetFwKnobTarget::OutputPair0 => 0,
DuetFwKnobTarget::InputPair0 => 1,
DuetFwKnobTarget::InputPair1 => 2,
};
raw[3] = DuetFwProtocol::KNOB_OUTPUT_VALUE_MAX - params.output_volume;
raw[4] = params.input_gains[0];
raw[5] = params.input_gains[1];
cmds.push(VendorCmd::HwState(raw));
}
fn cmds_to_knob_params(params: &mut DuetFwKnobState, cmds: &[VendorCmd]) {
cmds.iter().for_each(|cmd| match cmd {
VendorCmd::HwState(raw) => {
params.output_mute = raw[0] > 0;
params.target = match raw[1] {
2 => DuetFwKnobTarget::InputPair1,
1 => DuetFwKnobTarget::InputPair0,
_ => DuetFwKnobTarget::OutputPair0,
};
params.output_volume = DuetFwProtocol::KNOB_OUTPUT_VALUE_MAX - raw[3];
params.input_gains[0] = raw[4];
params.input_gains[1] = raw[5];
}
_ => (),
});
}
impl DuetFwParamsSerdes<DuetFwKnobState> for DuetFwProtocol {
fn default_cmds_for_params(cmds: &mut Vec<VendorCmd>) {
default_cmds_for_knob_params(cmds);
}
fn cmds_to_params(params: &mut DuetFwKnobState, cmds: &[VendorCmd]) {
cmds_to_knob_params(params, cmds);
}
fn cmds_from_params(params: &DuetFwKnobState, cmds: &mut Vec<VendorCmd>) {
cmds_from_knob_params(params, cmds);
}
}
impl DuetFwProtocol {
/// The minimum value of output in knob parameters.
pub const KNOB_OUTPUT_VALUE_MIN: u8 = 0;
/// The maximum value of output in knob parameters.
pub const KNOB_OUTPUT_VALUE_MAX: u8 = 64;
/// The minimum value of input in knob parameters.
pub const KNOB_INPUT_VALUE_MIN: u8 = 10;
/// The maximum value of input in knob parameters.
pub const KNOB_INPUT_VALUE_MAX: u8 = 75;
}
/// Source of output.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum DuetFwOutputSource {
/// The pair of stream inputs.
StreamInputPair0,
/// The pair of mixer outputs.
MixerOutputPair0,
}
impl Default for DuetFwOutputSource {
fn default() -> Self {
Self::StreamInputPair0
}
}
/// Level of output.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum DuetFwOutputNominalLevel {
/// Fixed level for external amplifier.
Instrument,
/// -10 dBV, adjustable between 0 to 64 (-64 to 0 dB).
Consumer,
}
impl Default for DuetFwOutputNominalLevel {
fn default() -> Self {
Self::Instrument
}
}
/// Mode of relation between mute and knob.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum DuetFwOutputMuteMode {
/// Never.
Never,
/// Muted at knob pushed, unmuted at knob released.
Normal,
/// Muted at knob released, unmuted at knob pushed.
Swapped,
}
impl Default for DuetFwOutputMuteMode {
fn default() -> Self {
Self::Never
}
}
/// Parameters for output.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct DuetFwOutputParams {
/// Whether to mute.
pub mute: bool,
/// Volume.
pub volume: u8,
/// Source of signal.
pub source: DuetFwOutputSource,
/// Nominal level.
pub nominal_level: DuetFwOutputNominalLevel,
/// Mode of mute for line output.
pub line_mute_mode: DuetFwOutputMuteMode,
/// Mode of mute for headphone output.
pub hp_mute_mode: DuetFwOutputMuteMode,
}
fn parse_mute_mode(mute: bool, unmute: bool) -> DuetFwOutputMuteMode {
match (mute, unmute) {
(true, true) => DuetFwOutputMuteMode::Never,
(true, false) => DuetFwOutputMuteMode::Swapped,
(false, true) => DuetFwOutputMuteMode::Normal,
(false, false) => DuetFwOutputMuteMode::Never,
}
}
fn build_mute_mode(mode: &DuetFwOutputMuteMode, mute: &mut bool, unmute: &mut bool) {
match mode {
DuetFwOutputMuteMode::Never => {
*mute = true;
*unmute = true;
}
DuetFwOutputMuteMode::Normal => {
*mute = false;
*unmute = true;
}
DuetFwOutputMuteMode::Swapped => {
*mute = true;
*unmute = false;
}
}
}
fn default_cmds_for_output_params(cmds: &mut Vec<VendorCmd>) {
cmds.push(VendorCmd::OutMute(Default::default()));
cmds.push(VendorCmd::OutVolume(Default::default()));
cmds.push(VendorCmd::OutSourceIsMixer(Default::default()));
cmds.push(VendorCmd::OutIsConsumerLevel(Default::default()));
cmds.push(VendorCmd::MuteForLineOut(Default::default()));
cmds.push(VendorCmd::UnmuteForLineOut(Default::default()));
cmds.push(VendorCmd::MuteForHpOut(Default::default()));
cmds.push(VendorCmd::UnmuteForHpOut(Default::default()));
}
fn cmds_to_output_params(params: &mut DuetFwOutputParams, cmds: &[VendorCmd]) {
let mut line_out_mute = false;
let mut line_out_unmute = false;
let mut hp_out_mute = false;
let mut hp_out_unmute = false;
cmds.iter().for_each(|&cmd| match cmd {
VendorCmd::OutMute(muted) => params.mute = muted,
VendorCmd::OutVolume(vol) => params.volume = vol,
VendorCmd::OutSourceIsMixer(is_mixer) => {
params.source = if is_mixer {
DuetFwOutputSource::MixerOutputPair0
} else {
DuetFwOutputSource::StreamInputPair0
};
}
VendorCmd::OutIsConsumerLevel(is_consumer_level) => {
params.nominal_level = if is_consumer_level {
DuetFwOutputNominalLevel::Consumer
} else {
DuetFwOutputNominalLevel::Instrument
};
}
VendorCmd::MuteForLineOut(muted) => line_out_mute = muted,
VendorCmd::UnmuteForLineOut(unmuted) => line_out_unmute = unmuted,
VendorCmd::MuteForHpOut(muted) => hp_out_mute = muted,
VendorCmd::UnmuteForHpOut(unmuted) => hp_out_unmute = unmuted,
_ => (),
});
params.line_mute_mode = parse_mute_mode(line_out_mute, line_out_unmute);
params.hp_mute_mode = parse_mute_mode(hp_out_mute, hp_out_unmute);
}
fn cmds_from_output_params(params: &DuetFwOutputParams, cmds: &mut Vec<VendorCmd>) {
let mut line_out_mute = false;
let mut line_out_unmute = false;
let mut hp_out_mute = false;
let mut hp_out_unmute = false;
build_mute_mode(
¶ms.line_mute_mode,
&mut line_out_mute,
&mut line_out_unmute,
);
build_mute_mode(¶ms.hp_mute_mode, &mut hp_out_mute, &mut hp_out_unmute);
let is_mixer = params.source == DuetFwOutputSource::MixerOutputPair0;
let is_consumer_level = params.nominal_level == DuetFwOutputNominalLevel::Consumer;
cmds.push(VendorCmd::OutMute(params.mute));
cmds.push(VendorCmd::OutVolume(params.volume));
cmds.push(VendorCmd::OutSourceIsMixer(is_mixer));
cmds.push(VendorCmd::OutIsConsumerLevel(is_consumer_level));
cmds.push(VendorCmd::MuteForLineOut(line_out_mute));
cmds.push(VendorCmd::UnmuteForLineOut(line_out_unmute));
cmds.push(VendorCmd::MuteForHpOut(hp_out_mute));
cmds.push(VendorCmd::UnmuteForHpOut(hp_out_unmute));
}
impl DuetFwParamsSerdes<DuetFwOutputParams> for DuetFwProtocol {
fn default_cmds_for_params(cmds: &mut Vec<VendorCmd>) {
default_cmds_for_output_params(cmds);
}
fn cmds_to_params(params: &mut DuetFwOutputParams, cmds: &[VendorCmd]) {
cmds_to_output_params(params, cmds);
}
fn cmds_from_params(params: &DuetFwOutputParams, cmds: &mut Vec<VendorCmd>) {
cmds_from_output_params(params, cmds);
}
}
impl DuetFwProtocol {
/// The minimum value of volume in output parameters.
pub const OUTPUT_VOLUME_MIN: u8 = 0;
/// The maximum value of volume in output parameters.
pub const OUTPUT_VOLUME_MAX: u8 = 64;
}
/// Source of input.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum DuetFwInputSource {
/// From XLR plug.
Xlr,
/// From Phone plug. The gain is adjustable between 0 and 65 dB.
Phone,
}
impl Default for DuetFwInputSource {
fn default() -> Self {
Self::Xlr
}
}
/// Nominal level of input.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum DuetFwInputXlrNominalLevel {
/// Adjustable between 10 and 75 dB.
Microphone,
/// +4 dBu, with fixed gain.
Professional,
/// -10 dBV, with fixed gain.
Consumer,
}
impl Default for DuetFwInputXlrNominalLevel {
fn default() -> Self {
Self::Microphone
}
}
/// Input parameters.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct DuetFwInputParameters {
/// Gains of inputs.
pub gains: [u8; 2],
/// Polarity of XLR inputs.
pub polarities: [bool; 2],
/// Nominal level of XLR inputs.
pub xlr_nominal_levels: [DuetFwInputXlrNominalLevel; 2],
/// Whether to enable phantom powering for XLR inputs.
pub phantom_powerings: [bool; 2],
/// Source of analog inputs.
pub srcs: [DuetFwInputSource; 2],
/// Disable click sound for microphone amplifier.
pub clickless: bool,
}
fn default_cmds_for_input_params(cmds: &mut Vec<VendorCmd>) {
cmds.push(VendorCmd::InGain(0, Default::default()));
cmds.push(VendorCmd::InGain(1, Default::default()));
cmds.push(VendorCmd::MicPolarity(0, Default::default()));
cmds.push(VendorCmd::MicPolarity(1, Default::default()));
cmds.push(VendorCmd::XlrIsMicLevel(0, Default::default()));
cmds.push(VendorCmd::XlrIsMicLevel(1, Default::default()));
cmds.push(VendorCmd::XlrIsConsumerLevel(0, Default::default()));
cmds.push(VendorCmd::XlrIsConsumerLevel(1, Default::default()));
cmds.push(VendorCmd::MicPhantom(0, Default::default()));
cmds.push(VendorCmd::MicPhantom(1, Default::default()));
cmds.push(VendorCmd::InputSourceIsPhone(0, Default::default()));
cmds.push(VendorCmd::InputSourceIsPhone(1, Default::default()));
cmds.push(VendorCmd::InClickless(Default::default()));
}
fn cmds_to_input_params(params: &mut DuetFwInputParameters, cmds: &[VendorCmd]) {
let mut is_mic_levels = [false; 2];
let mut is_consumer_levels = [false; 2];
cmds.iter().for_each(|&cmd| match cmd {
VendorCmd::InGain(i, gain) => {
if i < params.gains.len() {
params.gains[i] = gain;
}
}
VendorCmd::MicPolarity(i, polarity) => {
if i < params.polarities.len() {
params.polarities[i] = polarity;
}
}
VendorCmd::XlrIsMicLevel(i, is_mic_level) => {
if i < is_mic_levels.len() {
is_mic_levels[i] = is_mic_level;
}
}
VendorCmd::XlrIsConsumerLevel(i, is_consumer_level) => {
if i < is_consumer_levels.len() {
is_consumer_levels[i] = is_consumer_level;
}
}
VendorCmd::MicPhantom(i, enabled) => {
if i < params.phantom_powerings.len() {
params.phantom_powerings[i] = enabled;
}
}
VendorCmd::InputSourceIsPhone(i, is_phone) => {
if i < params.srcs.len() {
params.srcs[i] = if is_phone {
DuetFwInputSource::Phone
} else {
DuetFwInputSource::Xlr
};
}
}
VendorCmd::InClickless(enabled) => {
params.clickless = enabled;
}
_ => (),
});
params
.xlr_nominal_levels
.iter_mut()
.enumerate()
.for_each(|(i, xlr_nominal_level)| {
*xlr_nominal_level = if is_mic_levels[i] {
DuetFwInputXlrNominalLevel::Microphone
} else if is_consumer_levels[i] {
DuetFwInputXlrNominalLevel::Consumer
} else {
DuetFwInputXlrNominalLevel::Professional
};
});
}
fn cmds_from_input_params(params: &DuetFwInputParameters, cmds: &mut Vec<VendorCmd>) {
params.gains.iter().enumerate().for_each(|(i, &gain)| {
cmds.push(VendorCmd::InGain(i, gain));
});
params
.polarities
.iter()
.enumerate()
.for_each(|(i, &polarity)| {
cmds.push(VendorCmd::MicPolarity(i, polarity));
});
params
.phantom_powerings
.iter()
.enumerate()
.for_each(|(i, &enabled)| {
cmds.push(VendorCmd::MicPhantom(i, enabled));
});
params.srcs.iter().enumerate().for_each(|(i, &src)| {
let enabled = src == DuetFwInputSource::Phone;
cmds.push(VendorCmd::InputSourceIsPhone(i, enabled));
});
cmds.push(VendorCmd::InClickless(params.clickless));
let mut is_mic_levels = [false; 2];
let mut is_consumer_levels = [false; 2];
params
.xlr_nominal_levels
.iter()
.enumerate()
.for_each(|(i, xlr_nominal_level)| match xlr_nominal_level {
DuetFwInputXlrNominalLevel::Microphone => is_mic_levels[i] = true,
DuetFwInputXlrNominalLevel::Consumer => is_consumer_levels[i] = true,
_ => (),
});
is_mic_levels.iter().enumerate().for_each(|(i, &enabled)| {
cmds.push(VendorCmd::XlrIsMicLevel(i, enabled));
});
is_consumer_levels
.iter()
.enumerate()
.for_each(|(i, &enabled)| {
cmds.push(VendorCmd::XlrIsConsumerLevel(i, enabled));
});
}
impl DuetFwParamsSerdes<DuetFwInputParameters> for DuetFwProtocol {
fn default_cmds_for_params(cmds: &mut Vec<VendorCmd>) {
default_cmds_for_input_params(cmds);
}
fn cmds_to_params(params: &mut DuetFwInputParameters, cmds: &[VendorCmd]) {
cmds_to_input_params(params, cmds);
}
fn cmds_from_params(params: &DuetFwInputParameters, cmds: &mut Vec<VendorCmd>) {
cmds_from_input_params(params, cmds);
}
}
impl DuetFwProtocol {
/// The minimum value of gain in input parameters.
pub const INPUT_GAIN_MIN: u8 = 10;
/// The minimum value of gain in input parameters.
pub const INPUT_GAIN_MAX: u8 = 75;
}
/// Parameters of mixer coefficients.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct DuetFwMixerCoefficients {
/// Coefficients of analog inputs.
pub analog_inputs: [u16; 2],
/// Coefficients of stream inputs.
pub stream_inputs: [u16; 2],
}
/// Parameters of stereo mixer.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct DuetFwMixerParams(pub [DuetFwMixerCoefficients; 2]);
fn default_cmds_for_mixer_params(cmds: &mut Vec<VendorCmd>) {
cmds.push(VendorCmd::MixerSrc(0, 0, Default::default()));
cmds.push(VendorCmd::MixerSrc(1, 0, Default::default()));
cmds.push(VendorCmd::MixerSrc(2, 0, Default::default()));
cmds.push(VendorCmd::MixerSrc(3, 0, Default::default()));
cmds.push(VendorCmd::MixerSrc(0, 1, Default::default()));
cmds.push(VendorCmd::MixerSrc(1, 1, Default::default()));
cmds.push(VendorCmd::MixerSrc(2, 1, Default::default()));
cmds.push(VendorCmd::MixerSrc(3, 1, Default::default()));
}
fn cmds_to_mixer_params(params: &mut DuetFwMixerParams, cmds: &[VendorCmd]) {
cmds.iter().for_each(|&cmd| match cmd {
VendorCmd::MixerSrc(src, dst, coef) => {
if src < 2 {
params.0[dst].analog_inputs[src] = coef;
} else if src < 4 {
params.0[dst].stream_inputs[src - 2] = coef;
}
}
_ => (),
});
}
fn cmds_from_mixer_params(params: &DuetFwMixerParams, cmds: &mut Vec<VendorCmd>) {
params.0.iter().enumerate().for_each(|(dst, coefs)| {
coefs
.analog_inputs
.iter()
.chain(&coefs.stream_inputs)
.enumerate()
.for_each(|(src, &coef)| {
cmds.push(VendorCmd::MixerSrc(src, dst, coef));
});
})
}
impl DuetFwParamsSerdes<DuetFwMixerParams> for DuetFwProtocol {
fn default_cmds_for_params(cmds: &mut Vec<VendorCmd>) {
default_cmds_for_mixer_params(cmds);
}
fn cmds_to_params(params: &mut DuetFwMixerParams, cmds: &[VendorCmd]) {
cmds_to_mixer_params(params, cmds);
}
fn cmds_from_params(params: &DuetFwMixerParams, cmds: &mut Vec<VendorCmd>) {
cmds_from_mixer_params(params, cmds);
}
}
impl DuetFwProtocol {
/// The minimum value of source gain in mixer parameters.
pub const MIXER_SOURCE_GAIN_MIN: u16 = 0;
/// The maximum value of source gain in mixer parameters.
pub const MIXER_SOURCE_GAIN_MAX: u16 = 0x3fff;
}
/// Target of display.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum DuetFwDisplayTarget {
/// For output.
Output,
/// For input.
Input,
}
impl Default for DuetFwDisplayTarget {
fn default() -> Self {
Self::Output
}
}
/// Mode of display.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum DuetFwDisplayMode {
/// Independent.
Independent,
/// Following to knob target.
FollowingToKnobTarget,
}
impl Default for DuetFwDisplayMode {
fn default() -> Self {
Self::Independent
}
}
/// Overhold of display.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum DuetFwDisplayOverhold {
/// Infinite.
Infinite,
/// Keep during two seconds.
TwoSeconds,
}
impl Default for DuetFwDisplayOverhold {
fn default() -> Self {
Self::Infinite
}
}
/// Parameters of LED display.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct DuetFwDisplayParams {
/// Target to display.
pub target: DuetFwDisplayTarget,
/// Mode to display.
pub mode: DuetFwDisplayMode,
/// Mode of overhold.
pub overhold: DuetFwDisplayOverhold,
}
fn default_cmds_for_display_params(cmds: &mut Vec<VendorCmd>) {
cmds.push(VendorCmd::DisplayIsInput(Default::default()));
cmds.push(VendorCmd::DisplayFollowToKnob(Default::default()));
cmds.push(VendorCmd::DisplayOverholdTwoSec(Default::default()));
}
fn cmds_to_display_params(params: &mut DuetFwDisplayParams, cmds: &[VendorCmd]) {
cmds.iter().for_each(|&cmd| match cmd {
VendorCmd::DisplayIsInput(enabled) => {
params.target = if enabled {
DuetFwDisplayTarget::Input
} else {
DuetFwDisplayTarget::Output
};
}
VendorCmd::DisplayFollowToKnob(enabled) => {
params.mode = if enabled {
DuetFwDisplayMode::FollowingToKnobTarget
} else {
DuetFwDisplayMode::Independent
};
}
VendorCmd::DisplayOverholdTwoSec(enabled) => {
params.overhold = if enabled {
DuetFwDisplayOverhold::TwoSeconds
} else {
DuetFwDisplayOverhold::Infinite
};
}
_ => (),
})
}
fn cmds_from_display_params(params: &DuetFwDisplayParams, cmds: &mut Vec<VendorCmd>) {
let enabled = params.target == DuetFwDisplayTarget::Input;
cmds.push(VendorCmd::DisplayIsInput(enabled));
let enabled = params.mode == DuetFwDisplayMode::FollowingToKnobTarget;
cmds.push(VendorCmd::DisplayFollowToKnob(enabled));
let enabled = params.overhold == DuetFwDisplayOverhold::TwoSeconds;
cmds.push(VendorCmd::DisplayOverholdTwoSec(enabled));
}
impl DuetFwParamsSerdes<DuetFwDisplayParams> for DuetFwProtocol {
fn default_cmds_for_params(cmds: &mut Vec<VendorCmd>) {
default_cmds_for_display_params(cmds);
}
fn cmds_to_params(params: &mut DuetFwDisplayParams, cmds: &[VendorCmd]) {
cmds_to_display_params(params, cmds);
}
fn cmds_from_params(params: &DuetFwDisplayParams, cmds: &mut Vec<VendorCmd>) {
cmds_from_display_params(params, cmds);
}
}
/// Type of command for Apogee Duet FireWire.
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum VendorCmd {
MicPolarity(usize, bool),
XlrIsMicLevel(usize, bool),
XlrIsConsumerLevel(usize, bool),
MicPhantom(usize, bool),
OutIsConsumerLevel(bool),
InGain(usize, u8),
HwState([u8; 11]),
OutMute(bool),
InputSourceIsPhone(usize, bool),
MixerSrc(usize, usize, u16),
OutSourceIsMixer(bool),
DisplayOverholdTwoSec(bool),
DisplayClear,
OutVolume(u8),
MuteForLineOut(bool),
MuteForHpOut(bool),
UnmuteForLineOut(bool),
UnmuteForHpOut(bool),
DisplayIsInput(bool),
InClickless(bool),
DisplayFollowToKnob(bool),
}
impl VendorCmd {
const APOGEE_PREFIX: [u8; 3] = [0x50, 0x43, 0x4d]; // 'P', 'C', 'M'
const MIC_POLARITY: u8 = 0x00;
const XLR_IS_MIC_LEVEL: u8 = 0x01;
const XLR_IS_CONSUMER_LEVEL: u8 = 0x02;
const MIC_PHANTOM: u8 = 0x03;
const OUT_IS_CONSUMER_LEVEL: u8 = 0x04;
const IN_GAIN: u8 = 0x05;
const HW_STATE: u8 = 0x07;
const OUT_MUTE: u8 = 0x09;
const INPUT_SOURCE_IS_PHONE: u8 = 0x0c;
const MIXER_SRC: u8 = 0x10;
const OUT_SOURCE_IS_MIXER: u8 = 0x11;
const DISPLAY_OVERHOLD_TWO_SEC: u8 = 0x13;
const DISPLAY_CLEAR: u8 = 0x14;
const OUT_VOLUME: u8 = 0x15;
const MUTE_FOR_LINE_OUT: u8 = 0x16;
const MUTE_FOR_HP_OUT: u8 = 0x17;
const UNMUTE_FOR_LINE_OUT: u8 = 0x18;
const UNMUTE_FOR_HP_OUT: u8 = 0x19;
const DISPLAY_IS_INPUT: u8 = 0x1b;
const IN_CLICKLESS: u8 = 0x1e;
const DISPLAY_FOLLOW_TO_KNOB: u8 = 0x22;
const ON: u8 = 0x70;
const OFF: u8 = 0x60;
fn build_args(&self) -> Vec<u8> {
let mut args = Vec::with_capacity(6);
args.extend_from_slice(&Self::APOGEE_PREFIX);
args.extend_from_slice(&[0xff; 3]);
match self {
Self::MicPolarity(ch, _) => {
args[3] = Self::MIC_POLARITY;
args[4] = 0x80;
args[5] = *ch as u8;
}
Self::XlrIsMicLevel(ch, _) => {
args[3] = Self::XLR_IS_MIC_LEVEL;
args[4] = 0x80;
args[5] = *ch as u8;
}
Self::XlrIsConsumerLevel(ch, _) => {
args[3] = Self::XLR_IS_CONSUMER_LEVEL;
args[4] = 0x80;
args[5] = *ch as u8;
}
Self::MicPhantom(ch, _) => {
args[3] = Self::MIC_PHANTOM;
args[4] = 0x80;
args[5] = *ch as u8;
}
Self::OutIsConsumerLevel(_) => {
args[3] = Self::OUT_IS_CONSUMER_LEVEL;
args[4] = 0x80;
}
Self::InGain(ch, _) => {
args[3] = Self::IN_GAIN;
args[4] = 0x80;
args[5] = *ch as u8;
}
Self::HwState(_) => args[3] = Self::HW_STATE,
Self::OutMute(_) => {
args[3] = Self::OUT_MUTE;
args[4] = 0x80;
}
Self::InputSourceIsPhone(ch, _) => {
args[3] = Self::INPUT_SOURCE_IS_PHONE;
args[4] = 0x80;
args[5] = *ch as u8;
}
Self::MixerSrc(src, dst, _) => {
args[3] = Self::MIXER_SRC;
args[4] = (((*src / 2) << 4) | (*src % 2)) as u8;
args[5] = *dst as u8;
}
Self::OutSourceIsMixer(_) => args[3] = Self::OUT_SOURCE_IS_MIXER,
Self::DisplayOverholdTwoSec(_) => args[3] = Self::DISPLAY_OVERHOLD_TWO_SEC,
Self::DisplayClear => args[3] = Self::DISPLAY_CLEAR,
Self::OutVolume(_) => {
args[3] = Self::OUT_VOLUME;
args[4] = 0x80;
}
Self::MuteForLineOut(_) => {
args[3] = Self::MUTE_FOR_LINE_OUT;
args[4] = 0x80;
}
Self::MuteForHpOut(_) => {
args[3] = Self::MUTE_FOR_HP_OUT;
args[4] = 0x80;
}
Self::UnmuteForLineOut(_) => {
args[3] = Self::UNMUTE_FOR_LINE_OUT;
args[4] = 0x80;
}
Self::UnmuteForHpOut(_) => {
args[3] = Self::UNMUTE_FOR_HP_OUT;
args[4] = 0x80;
}
Self::DisplayIsInput(_) => args[3] = Self::DISPLAY_IS_INPUT,
Self::InClickless(_) => args[3] = Self::IN_CLICKLESS,
Self::DisplayFollowToKnob(_) => args[3] = Self::DISPLAY_FOLLOW_TO_KNOB,
}
args
}
fn append_bool(data: &mut Vec<u8>, val: bool) {
data.push(if val { Self::ON } else { Self::OFF });
}
fn append_variable(&self, data: &mut Vec<u8>) {
match self {
Self::MicPolarity(_, enabled) => Self::append_bool(data, *enabled),
Self::XlrIsMicLevel(_, enabled) => Self::append_bool(data, *enabled),
Self::XlrIsConsumerLevel(_, enabled) => Self::append_bool(data, *enabled),
Self::MicPhantom(_, enabled) => Self::append_bool(data, *enabled),
Self::OutIsConsumerLevel(enabled) => Self::append_bool(data, *enabled),
Self::InGain(_, gain) => data.push(*gain),
Self::OutMute(enabled) => Self::append_bool(data, *enabled),
Self::InputSourceIsPhone(_, enabled) => Self::append_bool(data, *enabled),
Self::MixerSrc(_, _, gain) => data.extend_from_slice(&gain.to_be_bytes()),
Self::OutSourceIsMixer(enabled) => Self::append_bool(data, *enabled),
Self::DisplayOverholdTwoSec(enabled) => Self::append_bool(data, *enabled),
Self::OutVolume(vol) => data.push(*vol),
Self::MuteForLineOut(enabled) => Self::append_bool(data, *enabled),
Self::MuteForHpOut(enabled) => Self::append_bool(data, *enabled),
Self::UnmuteForLineOut(enabled) => Self::append_bool(data, *enabled),
Self::UnmuteForHpOut(enabled) => Self::append_bool(data, *enabled),