-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathXAPX00.py
1992 lines (1823 loc) · 89.5 KB
/
XAPX00.py
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
# pylint: disable=E221
"""
Modified by Loafdude
XAPX00 Control.
Adapted to python and XAP 800
Jay Love ([email protected])
(C) 2017
Based on the javascript library ap800js.lib:
Gentner/ClearOne AP800 Automatic Mixer Control Library
Copyright (C) 2005 Kade Consulting, LLC. All rights reserved.
Author: Dan Rudman (dantelope)
This script may be freely copied and modified so long as it carries a
reference to the above copyright. Any modifications to this script
must use this license and, thus, must itself be freely copyable and
modifiable under the same terms.
Issues:
Matrix Routing:
Wildcard is not accepted for channels, why not?
Add ability to turn off all matrix entries
Is matrix retained after poweroff? Add ability to clear by default?
"""
__version__ = '0.2.3'
import serial
import logging
import math
import time
import warnings
import time
import string
from functools import wraps
testing = 0
_LOGGER = logging.getLogger(__name__)
if 0:
import sys
out_hdlr = logging.StreamHandler(sys.stdout)
out_hdlr.setFormatter(logging.Formatter('%(asctime)s %(message)s'))
out_hdlr.setLevel(logging.DEBUG)
_LOGGER.addHandler(out_hdlr)
_LOGGER.setLevel(logging.DEBUG)
# Global Constants
XAP800_CMD = "#5"
XAP400_CMD = "#7"
XAP800Type = "XAP800"
XAP400Type = "XAP400"
XAP800_LOCATION_PREFIX = "XAP800"
XAP800_UNIT_TYPE = 1
EOM = "\r"
DEVICE_MAXMICS = "Max Number of Microphones"
matrixGeo = {'XAP800': [{"c": 1, "og": "O", "ig": "I"},
{"c": 2, "og": "O", "ig": "I"},
{"c": 3, "og": "O", "ig": "I"},
{"c": 4, "og": "O", "ig": "I"},
{"c": 5, "og": "O", "ig": "I"},
{"c": 6, "og": "O", "ig": "I"},
{"c": 7, "og": "O", "ig": "I"},
{"c": 8, "og": "O", "ig": "I"},
{"c": 9, "og": "O", "ig": "I"},
{"c": 10, "og": "O", "ig": "I"},
{"c": 11, "og": "O", "ig": "I"},
{"c": 12, "og": "O", "ig": "I"},
{"c": "O", "og": "E", "ig": "E"},
{"c": "P", "og": "E", "ig": "E"},
{"c": "Q", "og": "E", "ig": "E"},
{"c": "R", "og": "E", "ig": "E"},
{"c": "S", "og": "E", "ig": "E"},
{"c": "T", "og": "E", "ig": "E"},
{"c": "U", "og": "E", "ig": "E"},
{"c": "V", "og": "E", "ig": "E"},
{"c": "W", "og": "E", "ig": "E"},
{"c": "X", "og": "E", "ig": "E"},
{"c": "Y", "og": "E", "ig": "E"},
{"c": "Z", "og": "E", "ig": "E"},
{"c": "A", "og": "P", "ig": "P"},
{"c": "B", "og": "P", "ig": "P"},
{"c": "C", "og": "P", "ig": "P"},
{"c": "D", "og": "P", "ig": "P"},
{"c": "E", "og": "P", "ig": "P"},
{"c": "F", "og": "P", "ig": "P"},
{"c": "G", "og": "P", "ig": "P"},
{"c": "H", "og": "P", "ig": "P"}],
'XAP400': [{"c": 1, "og": "O", "ig": "I"},
{"c": 2, "og": "O", "ig": "I"},
{"c": 3, "og": "O", "ig": "I"},
{"c": 4, "og": "O", "ig": "I"},
{"c": 5, "og": "O", "ig": "I"},
{"c": 6, "og": "O", "ig": "I"},
{"c": 7, "og": "O", "ig": "I"},
{"c": 8, "og": "O", "ig": "I"},
{"c": "O", "og": "E", "ig": "E"},
{"c": "P", "og": "E", "ig": "E"},
{"c": "Q", "og": "E", "ig": "E"},
{"c": "R", "og": "E", "ig": "E"},
{"c": "S", "og": "E", "ig": "E"},
{"c": "T", "og": "E", "ig": "E"},
{"c": "U", "og": "E", "ig": "E"},
{"c": "V", "og": "E", "ig": "E"},
{"c": "W", "og": "E", "ig": "E"},
{"c": "X", "og": "E", "ig": "E"},
{"c": "Y", "og": "E", "ig": "E"},
{"c": "Z", "og": "E", "ig": "E"},
{"c": "A", "og": "P", "ig": "P"},
{"c": "B", "og": "P", "ig": "P"},
{"c": "C", "og": "P", "ig": "P"},
{"c": "D", "og": "P", "ig": "P"}]}
nogainGroups = ['E']
unit_types = {5: "XAP800",
7: "XAP400",
4: "PSR1212",
6: "XAPTH2"}
def db2linear(db, maxref=0):
"""Convert a db level to a linear level of 0-1.
If maxref is provided, the return value is a proportion of maxref
"""
return (10.0 ** ((float(db) + 0.0000001 - maxref) / 20.0))
def linear2db(gain, maxref=0):
"""Convert a linear volume level of 0-1 to db.
if maxref is provided, then gain parameter provided is used
as a proportion of maxref. This is to allow the use of the
maxgain level.
"""
dbdiff = 20.0 * math.log10(float(gain) + 0.000001)
return min(max(maxref + dbdiff, -99), 99)
class XAPX00(object):
"""XAPX000 Module."""
def __init__(self, comPort="/dev/ttyUSB0", baudRate=38400,
XAPType=XAP800Type, rtscts=True, object=None):
"""init: no parameters required."""
_LOGGER.debug("XAPX00 version: {}".format(__version__))
self.comPort = comPort
self.baudRate = baudRate
self.byteLength = 8
self.object = object
self.write_to_object = False
self.stopBits = 1
self.parity = "N"
self.timeout = 1
self.mqtt_command_queue = []
self.connected_unit_id = None
self.available_units = []
self.rtscts = rtscts
self.XAPType = XAPType
self.input_groups = ['I', "M", "P", "L"]
self.unit_attribute = "units"
self.input_attribute = "input_channels"
self.output_attribute = "output_channels"
self.gating_attribute = "gating_groups"
self.output_groups = ['O']
self.XAPCMD = XAP800_CMD if XAPType == XAP800Type else XAP400_CMD
self.connected = 0
self.convertDb = 1 # translate levels between linear(0-1) and db
self._lastcall = time.time()
self._maxtime = 60 * 60 * 1 # After 1hr reset serial before attempting commands
self._maxrespdelay = 5
self._sleeptime = 0.25
self._waiting_response = 0
self.ExpansionChannels = string.ascii_uppercase[string.ascii_uppercase.find('O'):]
self.ProcessingChannels = string.ascii_uppercase[:string.ascii_uppercase.find('H')]
def connect(self):
"""Open serial port and check connection."""
_LOGGER.info("Connecting to XAPX00 at " + str(self.baudRate) +
" baud...")
self.serial = serial.Serial(self.comPort, self.baudRate,
timeout=self.timeout, rtscts=self.rtscts)
# Ensure connectivity by requesting the UID of the first unit
units = []
self.serial.reset_input_buffer()
self.serial.readlines(5000) # clear response
self.serial.write("#5* PRGSTRING 7\r".encode())
units = units + self.getSerialData("PRGSTRING")
self.serial.write("#7* PRGSTRING 7\r".encode())
units = units + self.getSerialData("PRGSTRING")
for u in units:
self.serial.write(("#" + u[0] + u[1] + " PRGSTRING 7 **IMATTACHED**" + u[0] + u[1] + "\r").encode())
self.serial.readlines(5000) # Clear Buffer
self.serial.write("#5* STRING 7\r".encode())
self.serial.write("#7* STRING 7\r".encode())
data = self.serial.readlines(5000)
attached_unit = None
for d in data:
try:
if "**IMATTACHED**" in str(d):
attached_unit = str(d)[str(d).index("**IMATTACHED**")+15]
except:
for u in units:
self.serial.write(("#" + u[0] + u[1] + " PRGSTRING 7 " + u[4].strip() + "\r").encode())
raise Exception("COULD NOT DETERMINE ATTACHED UNIT")
for u in units:
if u[4].strip() == "":
u[4] = "CLEAR"
self.serial.write(("#" + u[0] + u[1] + " PRGSTRING 7 " + u[4].strip() + "\r").encode())
if attached_unit is None:
raise Exception("Could Not Determine Attached Unit")
self.connected_unit_id = attached_unit
for u in units:
self.available_units.append({"device_id": int(u[1]),
"device_type": unit_types[int(u[0])]})
self.connected = 1
print(" Serially Attached to Unit ID " + str(self.connected_unit_id))
print(" Total Units Found: " + str(len(self.available_units)))
def getSerialData(self, command):
result = []
for serdata in self.serial.readlines(5000):
resp = serdata.decode('ascii').strip().split('#')
if len(resp) is 2:
data = resp[1].split()
data.append('')
if len(data) > 3:
type, did, cmd, value1, value2 = data[0][0:1], data[0][1:2], data[1], data[2], data[3]
if str(cmd) == str(command):
result.append([type, did, cmd, value1, value2])
return result
def disconnect(self):
"""Disconnect from serial port"""
self.serial.close()
self.connected = 0
def send(self, data):
"""Send the specified data string to the XAP800
Returns:
number of bytes sent
"""
starttime = time.time()
while 1:
if self._waiting_response==1:
if time.time() - starttime > self._maxrespdelay:
break
_LOGGER.debug("Send going to sleep\n")
time.sleep(self._sleeptime)
else:
break
currtime = time.time()
if currtime - self._lastcall > self._maxtime:
self.reset()
self._lastcall = currtime
_LOGGER.debug("Sending: %s", data)
if not testing:
self.serial.reset_input_buffer()
bytessent = self.serial.write(data.encode())
return bytessent
else:
self._waiting_response = 1
return len(data)
def XAPCommand(self, command, *args, **kwargs):
unitCode=kwargs.get('unitCode',0)
rtnCount = kwargs.get('rtnCount',1)
args = [str(x) for x in args]
xapstr = "%s%s %s %s %s" % ( self.XAPCMD, unitCode, command, " ".join(args), EOM)
_LOGGER.debug("Sending %s" % xapstr)
#print("" + xapstr)
starttime = time.time()
while 1:
if self._waiting_response==1:
if time.time() - starttime > self._maxrespdelay:
break
_LOGGER.debug("Send going to sleep\n")
time.sleep(self._sleeptime)
else:
break
currtime = time.time()
if currtime - self._lastcall > self._maxtime:
self.reset()
self._lastcall = currtime
_LOGGER.debug("Sending: %s", xapstr)
self.serial.reset_input_buffer()
self.serial.write(xapstr.encode())
self._waiting_response = 1
while 1:
res, cmd = self.readResponseCommand()
if res == None:
return None
elif cmd == command:
return self.decodeResponse(res)
else: # Got a response but not the right command.
othercmd = self.decodeResponse(res)
def mqtt_process_cmds(self):
if len(self.mqtt_command_queue) > 0:
queue = self.mqtt_command_queue
self.mqtt_command_queue = []
for item in queue:
if len(item['args']) == 0:
item['cmd']()
else:
item['cmd'](*item['args'])
time.sleep(0.1)
def listen(self):
while 1:
res, cmd = self.readResponseCommand()
if res == None:
return None
else: # Got a response but not the right command.
return self.decodeResponse(res)
def decodeResponse(self, res):
try:
res.append("")
command = res[1]
unit = int(res[0][1:2])
except:
print("Mangled Command:" + str(res))
command = None
value = None
if command == "GATE":
value = format(int('0x' + str(res[2]), 16), "08b")
if self.write_to_object:
try:
if self.object.units[unit].device_type is "XAP400":
gates = format(int('0x' + str(res[2]), 16), "04b")[::-1]
else:
gates = format(int('0x' + str(res[2]), 16), "08b")[::-1]
except (TypeError, KeyError):
gates = format(int('0x' + str(res[2]), 16), "08b")[::-1]
channel = 1
for bit in gates:
try:
gate = getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel].gate_open
if gate is None:
gate = False
getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[
channel].__setattr__("gate_open", False)
if bool(int(bit)) is not gate:
getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[
channel].__setattr__("gate_open", bool(int(bit)))
except (TypeError, KeyError):
break
channel += 1
elif command == "DECAY":
channel, value = int(res[2]), int(res[3])
strings = {1: "Slow",
2: "Medium",
3: "Fast"}
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'gate_decay', value)
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'gate_decay_string', strings[value])
elif command == "AEC":
channel, value = convertToInt(res[2]), bool(int(res[3]))
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'AEC', value)
elif command == "GREPORT":
value = bool(int(res[2]))
elif command == "MAX":
channel, group, value = convertToInt(res[2]), convertToInt(res[3]), str(res[4])
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], (self.output_attribute if group
in self.output_groups else self.input_attribute))[channel], 'gain_max', float(value))
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], (self.output_attribute if group
in self.output_groups else self.input_attribute))[channel], 'gain_max_string', value)
elif command == "MIN":
channel, group, value = convertToInt(res[2]), convertToInt(res[3]), str(res[4])
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], (self.output_attribute if group
in self.output_groups else self.input_attribute))[channel], 'gain_min', float(value))
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], (self.output_attribute if group
in self.output_groups else self.input_attribute))[channel], 'gain_min_string', value)
elif command == "GAIN":
channel, group, value = convertToInt(res[2]), convertToInt(res[3]), str(res[4])
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], (self.output_attribute if group
in self.output_groups else self.input_attribute))[channel], 'gain', float(value))
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], (self.output_attribute if group
in self.output_groups else self.input_attribute))[channel], 'gain_string', value)
elif command == "LVL": # No Auto Update
channel, group, meter_position, value = convertToInt(res[2]), convertToInt(res[3]), str(res[4]), float(res[5])
elif command == "LABEL": # No Auto Update
value = ''
if res[3] == "E":
channel, group, inout = convertToInt(res[2]), convertToInt(res[3]), str(res[4])
for i, v in enumerate(res):
if i > 4:
value = value + ' ' + v
else:
channel, group = convertToInt(res[2]), convertToInt(res[3])
for i, v in enumerate(res):
if i > 3:
value = value + ' ' + v
value = value.strip()
elif command == "MTRX":
src_channel, src_group, dst_channel, dst_group, value = convertToInt(res[2]), convertToInt(res[3]), convertToInt(res[4]), convertToInt(res[5]), str(res[6])
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], "matrix")[src_channel][dst_channel], 'state', value)
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], "matrix")[src_channel][dst_channel], 'enabled', bool(int(value)))
getattr(getattr(self.object, self.unit_attribute)[unit], "matrix")[src_channel][dst_channel].getType()
elif command == "MTRXLVL":
src_channel, src_group, dst_channel, dst_group, value = convertToInt(res[2]), convertToInt(res[3]), convertToInt(res[4]), convertToInt(res[5]), str(res[6])
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], "matrix")[src_channel][dst_channel], 'attenuation', float(value))
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], "matrix")[src_channel][dst_channel], 'attenuation_string', value)
elif command == "MUTE":
print(res)
channel, group, value = convertToInt(res[2]), convertToInt(res[3]), bool(int(res[4]))
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], (self.output_attribute if group
in self.output_groups else self.input_attribute))[channel], 'MUTE', int(value))
elif command == "RAMP":
channel, group, rate, value = convertToInt(res[2]), convertToInt(res[3]), str(res[4]), float(res[5])
elif command == "AAMB":
channel, value = convertToInt(res[2]), bool(int(res[3]))
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'adaptive_ambient', value)
elif command == "AGC":
channel, group, value = convertToInt(res[2]), convertToInt(res[3]), bool(int(res[4]))
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], (self.output_attribute if group
in self.output_groups else self.input_attribute))[channel], 'AGC', value)
elif command == "AMBLVL":
channel, value = convertToInt(res[2]), str(res[3])
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'ambient_level', float(value))
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'ambient_level_string', value)
elif command == "BAUD":
value = str(res[2])
if self.write_to_object:
setattr(getattr(self.object, self.unit_attribute)[unit], 'baudrate', value)
elif command == "MASTER":
value = str(res[2])
strings = {1: "Master",
2: "Slave"}
if self.write_to_object:
setattr(getattr(self.object, self.unit_attribute)[unit], 'master_mode', value)
setattr(getattr(self.object, self.unit_attribute)[unit], 'master_mode_string', strings[value])
elif command == "FLOW":
value = bool(int(res[2]))
if self.write_to_object:
setattr(getattr(self.object, self.unit_attribute)[unit], 'flowcontrol', value)
elif command == "MDMODE":
value = bool(int(res[2]))
if self.write_to_object:
setattr(getattr(self.object, self.unit_attribute)[unit], 'modem_mode', value)
elif command == "MINIT":
value = str(res[2])
if self.write_to_object:
setattr(getattr(self.object, self.unit_attribute)[unit], 'modem_init_string', value)
elif command == "MPASS":
value = str(res[2])
if self.write_to_object:
setattr(getattr(self.object, self.unit_attribute)[unit], 'modem_pass', value)
elif command == "VER":
value = str(res[2])
if self.write_to_object:
setattr(getattr(self.object, self.unit_attribute)[unit], 'FW_version', value)
elif command == "DSPVER":
value = str(res[2])
if self.write_to_object:
setattr(getattr(self.object, self.unit_attribute)[unit], 'DSP_version', value)
elif command == "SFTYMUTE":
value = bool(int(res[2]))
if self.write_to_object:
setattr(getattr(self.object, self.unit_attribute)[unit], 'safety_mute', value)
elif command == "UID":
value = str(res[2])
if self.write_to_object:
setattr(getattr(self.object, self.unit_attribute)[unit], 'serial_number', value)
elif command == "DID":
value = str(res[2])
if self.write_to_object:
setattr(getattr(self.object, self.unit_attribute)[unit], 'device_id', value)
elif command == "TOUT":
value = int(res[2])
if value is 0:
value = False
if self.write_to_object:
setattr(getattr(self.object, self.unit_attribute)[unit], 'panel_timeout', value)
elif command == "LFP":
value = int(res[2])
strings = {0: "Panel Unlocked",
1: "Panel Locked",
3: "Lock once timed out"}
if self.write_to_object:
setattr(getattr(self.object, self.unit_attribute)[unit], 'panel_lockout', value)
setattr(getattr(self.object, self.unit_attribute)[unit], 'panel_lockout_string', strings[value])
elif command == "PRESET":
print("ERROR: Could not parse serial command " + ' '.join(res))
channel, value = convertToInt(res[2]), convertToInt(res[3])
# Not Implemented
elif command == "DFLTM":
channel, group, value = convertToInt(res[2]), convertToInt(res[3]), str(res[4])
# Convert Position and Group into string
elif command == "FPP":
value = str(res[2])
if self.write_to_object:
setattr(getattr(self.object, self.unit_attribute)[unit], 'panel_passcode', value)
elif command == "CHAIRO":
channel, value = convertToInt(res[2]), bool(int(res[3]))
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'gate_chairman', value)
elif command == "GMODE":
channel, value = convertToInt(res[2]), int(res[3])
strings = {1: "Auto",
2: "Manual On",
3: "Manual Off"}
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'gating', value)
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'gating_string', strings[value])
elif command == "MLINE":
channel, value = convertToInt(res[2]), int(res[3])
strings = {0: "0dB",
2: "25dB",
1: "55dB"}
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'gain_coarse', value)
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'gain_coarse_string', strings[value])
elif command == "NLP":
channel, value = convertToInt(res[2]), int(res[3])
strings = {0: "Off",
1: "Soft",
2: "Medium",
3: "Aggressive"}
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'NLP', value)
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'NLP_string', strings[value])
elif command == "GRPSEL":
channel, value = convertToInt(res[2]), convertToInt(res[3])
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'gate_group', value)
elif command == "GOVER":
channel, value = convertToInt(res[2]), bool(int(res[3]))
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'gate_override', value)
elif command == "PAA":
channel, value = convertToInt(res[2]), bool(int(res[3]))
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'PA_adaptive', value)
elif command == "PP":
channel, value = convertToInt(res[2]), bool(int(res[3]))
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'phantom_power', value)
elif command == "NCSEL":
channel, group, value = convertToInt(res[2]), convertToInt(res[3]), bool(int(res[4]))
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'NC', value)
elif command == "NCD":
channel, group, value = convertToInt(res[2]), convertToInt(res[3]), int(res[4])
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'NC_depth', value)
elif command == "GHOLD":
channel, value = convertToInt(res[2]), str(res[3])
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'gate_holdtime', float(value))
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'gate_holdtime_string', value)
elif command == "AGCSET":
channel, group, threshold, target, attack, gain = convertToInt(res[2]), convertToInt(res[3]), str(res[4]), str(res[5]), str(res[6]), str(res[7])
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'AGC_threshold', float(threshold))
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'AGC_threshold_string', threshold)
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'AGC_target', float(target))
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'AGC_target_string', target)
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'AGC_attack', float(attack))
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'AGC_attack_string', attack)
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'AGC_gain', float(gain))
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'AGC_gain_string', gain)
value = [threshold, target, attack, gain]
elif command == "FILTER":
channel, group, node, ftype = convertToInt(res[2]), convertToInt(res[3]), int(res[4]), int(res[5])
freq, gain, bandwidth = None, None, None
strings = {0: "Not Configured",
1: "All Pass",
2: "Low Pass",
3: "High Pass",
4: "Low Shelving",
5: "High Shelving",
6: "Parametric EQ",
7: "CD Horn",
8: "Bessel Crossover",
9: "Butterworth Crossover",
10: "Linkwitz-Riley Crossover",
11: "Notch"}
if ftype is not 0:
freq = str(res[6])
if ftype is 4 or ftype is 5:
gain = str(res[7])
elif ftype is 6 or 6 < ftype < 11:
gain = str(res[7])
bandwidth = str(res[8])
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel].filters[node],
'type', int(ftype))
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel].filters[node],
'type_string', strings[ftype])
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel].filters[node],
'frequency', float(freq))
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel].filters[node],
'frequency_string', freq)
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel].filters[node],
'gain', float(gain))
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel].filters[node],
'gain_string', gain)
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel].filters[node],
'bandwidth', float(bandwidth))
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel].filters[node],
'bandwidth_string', bandwidth)
value = [channel, group, node, ftype, freq, gain, bandwidth]
elif command == "FILTSEL":
channel, group, node, value = convertToInt(res[2]), convertToInt(res[3]), int(res[4]), bool(int(res[5]))
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel].filters[node],
'enabled', value)
elif command == "COMPSEL":
channel, value = convertToInt(res[2]), bool(int(res[3]))
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'compressor', value)
elif command == "CGROUP":
channel, value = convertToInt(res[2]), int(res[3])
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'compressor_group', value)
elif command == "COMPRESS":
channel, threshold, ratio, attack, release, gain = convertToInt(res[2]), str(res[3]), str(res[4]), str(res[5]), str(res[6]), str(res[7])
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'threshold', float(threshold))
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'threshold_string', threshold)
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'ratio', float(ratio))
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'ratio_string', ratio)
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'attack', float(attack))
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'attack_string', attack)
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'release', float(release))
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'release_string', release)
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'gain', float(gain))
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'gain_string', gain)
value = [channel, threshold, ratio, attack, release, gain]
elif command == "DELAYSEL":
channel, value = convertToInt(res[2]), bool(int(res[3]))
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'delay', value)
elif command == "DELAY":
channel, value = convertToInt(res[2]), str(res[3])
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'delay_time', float(value))
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'delay_time_string', value)
elif command == "GRATIO":
channel, value = convertToInt(res[2]), int(res[3])
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'gate_ratio', float(value))
elif command == "REFSEL":
channel, ref_group, ref_channel = convertToInt(res[2]), str(res[3]), convertToInt(res[4])
if self.write_to_object:
refchan = getattr(getattr(self.object, self.unit_attribute)[unit], self.output_attribute)[ref_channel]
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'AEC_PA_reference', refchan)
else:
refchan = ref_channel
value = refchan
elif command == "OFFA":
channel, value = convertToInt(res[2]), str(res[3])
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'gate_attenuation', float(value))
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.input_attribute)[channel],
'gate_attenuation_string', value)
elif command == "FMP":
channel, value = convertToInt(res[2]), bool(int(res[3]))
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.gating_attribute)[channel],
'first_mic_priority', value)
elif command == "LMO":
channel, value = convertToInt(res[2]), str(res[3])
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.gating_attribute)[channel],
'last_mic', value)
elif command == "MMAX":
channel, value = convertToInt(res[2]), int(res[3])
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.gating_attribute)[channel],
'max_mics', value)
elif command == "NOM":
channel, value = convertToInt(res[2]), bool(int(res[3]))
if self.write_to_object:
setattr(getattr(getattr(self.object, self.unit_attribute)[unit], self.output_attribute)[channel],
'number_of_mic_attenuation', value)
elif command == "PRGSTRING":
pass # Not implemented
elif command == "STRING":
pass # Not implemented
else:
print("ERROR: Could not parse serial command " + ' '.join(res))
return value
def readResponseCommand(self):
"""Get response from unit.
Args:
numElements: How many response components to return,
starts from end of resposne
Returns:
response string from unit
"""
while 1:
resp = self.serial.readline().decode() #Get the line
#print("Resp:" + resp)
if len(resp) > 5 and resp[0:5] == "ERROR": #If Error
self._waiting_response = 0
raise Exception(resp)
_LOGGER.debug("Response %s" % resp)
if resp.find('#') > -1:
self._waiting_response = 0
break
if resp == '':
# nothing coming, have read too many lines
return None, None
# print("Resp:" + resp)
respitems = resp.split("#", maxsplit=1)[1].split()
try:
command = respitems[1]
except:
command = "ERROR"
return respitems, command
def readResponse(self, numElements=1):
"""Get response from unit.
Args:
numElements: How many response components to return,
starts from end of resposne
Returns:
response string from unit
"""
while 1:
resp = self.serial.readline().decode()
if len(resp) > 5 and resp[0:5] == "ERROR":
self._waiting_response = 0
raise Exception(resp)
_LOGGER.debug("Response %s" % resp)
if resp.find('#') > -1:
self._waiting_response = 0
break
if resp == '':
# nothing coming, have read too many lines
return None
respitems = resp.split("#",maxsplit=1)[1].split()
if numElements == 1:
return respitems[-1]
else:
return respitems[-numElements:]
def getUniqueId(self, unitCode=0):
"""Requests the unique ID of the target XAP800.
This is a hex value preprogrammed at the factory.
unitCode - the unit code of the target XAP800
"""
res = self.XAPCommand("UID", unitCode=unitCode)
return res
def reset(self):
"""Reset connection."""
warnings.warn("Clearing Serial Connection")
self.serial.reset_input_buffer()
def setDecayRate(self, channel, decayRate, unitCode=0):
"""Modify the decay rate for the specified XAP800.
unitCode: the unit code of the target XAP800
channel: 1-8
decayRate: the rate of decay
1 = slow, 2 = medium, 3 = fast
"""
# Ensure compliance with level boundary conditions
if decayRate < 1:
decayRate = 1
elif decayRate > 3:
decayRate = 3
res = self.XAPCommand("DECAY", channel, decayRate, unitCode=unitCode)
return int(res)
def getDecayRate(self, channel, unitCode=0):
"""Request the decay rate for the specified XAP800.
channel: 1-8
unitCode - the unit code of the target XAP800
"""
res = self.XAPCommand("DECAY", channel, unitCode=unitCode)
return res
def setEchoCanceller(self, channel, isEnabled, unitCode=0):
"""Enable/Disable the echo canceller.
unitCode - the unit code of the target XAP800
channel - the target channel (1-8, or * for all)
isEnabled - true to enable the channel, false to disable
"""
res = self.XAPCommand("AEC", channel, (1 if isEnabled else 0), unitCode=unitCode)
return res
def getEchoCanceller(self, channel, unitCode=0):
"""Enable or disable the echo canceller for the channel-unit.
unitCode - the unit code of the target XAP800
channel - the target channel (1-8, or * for all)
isEnabled - true to enable the channel, false to disable
"""
res = self.XAPCommand("AEC", channel, unitCode=unitCode)
return res
def getMaxGain(self, channel, group="I", unitCode=0):
"""Get max gain setting for a channel."""
if group in nogainGroups: #E is expansion, GAIN is set on source unit, so returns max
raise Exception('Gain not available on Expansion Bus')
resp = self.XAPCommand("MAX", channel, group, unitCode=unitCode)
return float(resp)
def setMaxGain(self, channel, gain, group="I", unitCode=0):
"""Set max gain setting for a channel."""
if group in nogainGroups: #E is expansion, GAIN is set on source unit, so returns max
raise Exception('Gain not available on Expansion Bus')
resp = self.XAPCommand("MAX", channel, group, gain, unitCode=unitCode)
return resp
def getMinGain(self, channel, group="I", unitCode=0):
"""Get min gain setting for a channel."""
if group in nogainGroups: # E is expansion, GAIN is set on source unit, so returns mix
raise Exception('Gain not available on Expansion Bus')
resp = self.XAPCommand("MIN", channel, group, unitCode=unitCode)
return float(resp)
def setMinGain(self, channel, gain, group="I", unitCode=0):
"""Set min gain setting for a channel."""
if group in nogainGroups: # E is expansion, GAIN is set on source unit, so returns mix
raise Exception('Gain not available on Expansion Bus')
resp = self.XAPCommand("MIN", channel, group, gain, unitCode=unitCode)
return resp
def getPropGain(self, channel, group="I", unitCode=0):
"""Get gain level for a channel relative to that channel's maxgain setting
Returned as linear scale of 0-1, with 1=maxgain
"""
if group in nogainGroups: #E is expansion, GAIN is set on source unit, so return max
raise Exception('Gain not available on Expansion Bus')
maxdb = self.getMaxGain(channel, group=group, unitCode=unitCode)
resp = self.XAPCommand("GAIN", channel, group, unitCode=unitCode, rtnCount=2)
resp = db2linear(resp, maxdb)
return resp
def setPropGain(self, channel, gain, isAbsolute=1, group="I", unitCode=0):
"""Set gain level for a channel relative to that channel's maxgain setting.
Gain input is on a linear scale of 0-1, with 1=maxgain
"""
if group in nogainGroups: #E is expansion, GAIN is set on source unit, so return max
raise Exception('Gain not available on Expansion Bus')
maxdb = self.getMaxGain(channel, group, unitCode)
dbgain = linear2db(gain, maxdb) # if self.convertDb else gain
_LOGGER.debug("setPropGain: linear:{}, max:{}, db:{}".format( gain, maxdb, dbgain))
resp = self.XAPCommand("GAIN", channel, group, dbgain, "A" if isAbsolute == 1 else "R",
unitCode=unitCode, rtnCount=2)[0]
return db2linear(resp, maxdb) # if self.convertDb else resp
def getGain(self, channel, group="I", unitCode=0):
"""Get gain level for a channel/group, 0 - 1"""
if group == 'E': #E is expansion, GAIN is set on source unit, so return max
raise Exception('Gain not available on Expansion Bus')
else:
resp = self.XAPCommand("GAIN", channel, group, unitCode = unitCode,
rtnCount=2)[0]
return db2linear(resp) if self.convertDb else resp
def setGain(self, channel, gain, group="I", isAbsolute=True, unitCode=0):
"""Sets the gain on the specified channel for the specified XAP800.
unitCode - the unit code of the target XAP800
channel - the target channel (1-8, A-D, 1-2, or * for all)
channelType - the type of channel (I for input, O for output,
S for subbus)
gain - the amount of gain to apply, 0 - 1.
isAbsolute - true if the level specified is an absolute setting,
false if it's a relative movement.
"""
if str(group) in nogainGroups: #E is expansion, GAIN is set on source unit, so return max
raise Exception('Gain not available on Expansion Bus')
gain = linear2db(gain) if self.convertDb else gain
resp = self.XAPCommand("GAIN", channel, gain, group, "A" if isAbsolute else "R",
unitCode=unitCode, rtnCount=2)[0]
return db2linear(resp) if self.convertDb else resp
def getLevel(self, channel, group="I", stage="I", unitCode=0):
"""Requests the db level of the target channel on the specified XAP800.
read-only
channel - the target channel (1-8, A-D)
unitCode - the unit code of the target XAP800
group - the target channel type
stage - See documentation
"""
self.send(XAP800_CMD + str(unitCode) + " LVL " + str(channel) + " " +
group + " " + stage + " " + EOM)
return float(self.readResponse())
def getLabel(self, channel, group, inout=None, unitCode=0):
"""Retrieve the text label assigned to an inpout or ouput
inout - 0 = Output, 1 = Input. For expansion bus only
"""
if inout != None and group == "E":
resp = self.XAPCommand("LABEL", channel, group, inout, unitCode=unitCode)
else:
resp = self.XAPCommand("LABEL", channel, group, unitCode=unitCode)
return resp
def setLabel(self, channel, group, label, inout=None, unitCode=0):
"""Retrieve the text label assigned to an inpout or ouput
inout - 0 = Output, 1 = Input. For expansion bus only
label - String, up to 20 characters. Use CLEAR to clear the field
"""
if inout != None and group == "E":
resp = self.XAPCommand("LABEL", channel, group, inout, label, unitCode=unitCode)
else:
resp = self.XAPCommand("LABEL", channel, group, label, unitCode=unitCode)
return resp
def setGateReport(self, toggle, unitCode=0):
"""Set gate status autoreports on/off
"""
if toggle:
toggle = "1"
else:
toggle = "0"
resp = self.XAPCommand("GREPORT", toggle, unitCode=unitCode)
return resp
def setMatrixRouting(self, inChannel, outChannel, state=1, inGroup="I",
outGroup="O", unitCode=0):
"""Set the routing matrix for the target channel.
unitCode - the unit code of the target XAP800
inChannel - the target input channel or group (1-25 see
page 64 of the XAP800 manual for
details)
outChannel - the target output channel or group (1-25 see
page 64 of the XAP800 manual for
details)
inGroup - input group (I-inputs, M=mics, L=line, ...
outGroup - output group (O=all Outputs 1-12, P=processing A-H, ...
state - 0=off, 1=on (line inputs only), 2=toggle (line only),
3=Non-Gated (mic only), 4=Gated (mic only), Null=currrent mode