Debian Codegraph @

hinawa-tascam-fw-rack-cli

raw symbols SCIP
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: GPL-3.0-or-later
3 # Copyright (C) 2018 Takashi Sakamoto
4
5 from hinawa_utils.misc.cli_kit import CliKit
6 from hinawa_utils.tscm.tscm_rack_unit import TscmRackUnit
7
8
9 def handle_clock_source(unit, args):
10 ops = ('set', 'get')
11 if len(args) > 0 and args[0] in ops:
12 op = args[0]
13 if op == ops[0] and len(args) == 2:
14 if unit.get_property('is-locked'):
15 print('Packet is-locked started.')
16 return False
17 source = args[1]
18 unit.set_clock_source(source)
19 else:
20 print(unit.get_clock_source())
21 return True
22 print('Arguments for clock-source command:')
23 print(' clock-source OP [SRC]')
24 print(' OP: [{0}]'.format('|'.join(ops)))
25 print(' SRC: [{0}]'.format('|'.join(unit.supported_clock_sources)))
26 return False
27
28
29 def handle_sampling_rate(unit, args):
30 ops = ('set', 'get')
31 if len(args) > 0 and args[0] in ops:
32 op = args[0]
33 if op == ops[0] and len(args) == 2:
34 rate = int(args[1])
35 unit.set_sampling_rate(rate)
36 else:
37 print(unit.get_sampling_rate())
38 return True
39 print('Arguments for sampling-rate command:')
40 print(' sampling-rate OP [RATE]')
41 print(' OP: [{0}]'.format('|'.join(ops)))
42 rates = [str(r) for r in unit.supported_sampling_rates]
43 print(' RATE: [{0}]'.format('|'.join(rates)))
44 return False
45
46
47 def handle_firmware_versions(unit, args):
48 info = unit.get_firmware_versions()
49 for name, value in info.items():
50 print('{0}: {1}'.format(name, value))
51 return True
52
53
54 def handle_routing(unit, args, cmd, labels, set_func, get_func):
55 ops = ('set', 'get')
56 if len(args) > 0 and args[0] in ops:
57 op = args[0]
58 if op == ops[0] and len(args) == 2 and args[1] in labels:
59 src = args[1]
60 set_func(src)
61 return True
62 elif op == 'get':
63 print(get_func())
64 return True
65 print('Arguments for {0} command:'.format(cmd))
66 print(' {0} OP [SRC]'.format(cmd))
67 print(' OP: [{0}]'.format('|'.join(ops)))
68 print(' SRC: [{0}]'.format('|'.join(labels)))
69 return False
70
71
72 def handle_coax_out_src(unit, args):
73 return handle_routing(unit, args, 'coaxial-out-source',
74 unit.get_coax_out_src_labels(),
75 unit.set_coax_out_src,
76 unit.get_coax_out_src)
77
78
79 def handle_opt_out_src(unit, args):
80 return handle_routing(unit, args, 'optical-out-source',
81 unit.get_opt_out_src_labels(),
82 unit.set_opt_out_src,
83 unit.get_opt_out_src)
84
85
86 def handle_stream_spdif_in_src(unit, args):
87 return handle_routing(unit, args, 'stream-spdif-in-source',
88 unit.get_stream_spdif_in_src_labels(),
89 unit.set_stream_spdif_in_src,
90 unit.get_stream_spdif_in_src)
91
92
93 def handle_input_threshold(unit, args):
94 ops = ('set', 'get')
95
96 if len(args) >= 1 and args[0] in ops:
97 op = args[0]
98 if op == 'set' and len(args) >= 2:
99 level = float(args[1])
100 unit.set_input_threshold(level)
101 return True
102 elif op == 'get':
103 level = unit.get_input_threshold()
104 print('{:.3f}'.format(level))
105 return True
106 print('Arguments for input-threshold command:')
107 print(' input-threshold OP [LEVEL]')
108 print(' OP: [{0}]'.format('|'.join(ops)))
109 print(' LEVEL: [-inf, -90..0]')
110 return True
111
112
113 def handle_monitor_input(unit, args):
114 ops = ('set', 'get')
115 items = {
116 'gain': (unit.set_gain, unit.get_gain),
117 'mute': (unit.set_mute, unit.get_mute),
118 'balance': (unit.set_balance, unit.get_balance),
119 }
120 labels = unit.get_channel_labels()
121
122 if len(args) >= 1 and args[0] in labels:
123 ch = args[0]
124 if len(args) >= 2 and args[1] in items:
125 item = args[1]
126 set_func, get_func = items[item]
127 if len(args) >= 3 and args[2] in ops:
128 op = args[2]
129 if len(args) >= 4 and op == 'set':
130 val = None
131 if item == 'gain':
132 val = float(args[3])
133 elif item == 'mute':
134 val = bool(int(args[3]))
135 elif item == 'balance':
136 val = int(args[3])
137 if val != None:
138 set_func(ch, val)
139 return True
140 elif op == 'get':
141 print(get_func(ch))
142 return True
143 print('Arguments for monitor-input command:')
144 print(' monitor-input CH ITEM OP [dB|MUTE|BALANCE]')
145 print(' CH: [{0}]'.format('|'.join(labels)))
146 print(' ITEM: [{0}]'.format('|'.join(items)))
147 print(' OP: [{0}]'.format('|'.join(ops)))
148 print(' dB: [0-99] (percentage) if OP=set')
149 print(' MUTE: [0|1] if OP=set')
150 print(' BALANCE:[0-99] (left-to-right, percentage) if OP=set')
151 return False
152
153
154 cmds = {
155 'clock-source': handle_clock_source,
156 'sampling-rate': handle_sampling_rate,
157 'firmware-versions': handle_firmware_versions,
158 'coaxial-out-source': handle_coax_out_src,
159 'optical-out-source': handle_opt_out_src,
160 'stream-spdif-in-source': handle_stream_spdif_in_src,
161 'input-threshold': handle_input_threshold,
162
163 'monitor-input': handle_monitor_input,
164 }
165
166 fullpath = CliKit.seek_snd_unit_path()
167 if fullpath:
168 with TscmRackUnit(fullpath) as unit:
169 CliKit.dispatch_command(unit, cmds)