Debian Codegraph @

hinawa-maudio-bebob-cli

raw
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: GPL-3.0-or-later
3 # Copyright (C) 2018 Takashi Sakamoto
4
5 import time
6 import signal
7 import sys
8
9 from hinawa_utils.misc.cli_kit import CliKit
10
11 from hinawa_utils.bebob.maudio_unit import MaudioUnit
12
13
14 def _handle_target_volume(unit, args, cmd, targets_func, set_func, get_func):
15 chs = ('0', '1')
16 ops = ('set', 'get')
17 targets = targets_func()
18 if len(args) >= 1 and args[0] in targets:
19 target = args[0]
20 if len(args) >= 2 and args[1] in chs:
21 ch = int(args[1])
22 if len(args) >= 3 and args[2] in ops:
23 op = args[2]
24 if op == 'set' and len(args) >= 4:
25 db = float(args[3])
26 set_func(target, ch, db)
27 return True
28 elif op == 'get':
29 print(get_func(target, ch))
30 return True
31 print('Arguments for {0} command:'.format(cmd))
32 print(' {0} TARGET CH OP [dB]'.format(cmd))
33 print(' TARGET: [{0}]'.format('|'.join(targets)))
34 print(' CH: [{0}]'.format('|'.join(chs)))
35 print(' OP: [{0}]'.format('|'.join(ops)))
36 print(' dB: [-128.00..128.00] if OP=set')
37 return False
38
39
40 def handle_input_gain(unit, args):
41 return _handle_target_volume(unit, args, 'input-gain',
42 unit.protocol.get_input_labels,
43 unit.protocol.set_input_gain,
44 unit.protocol.get_input_gain)
45
46
47 def handle_aux_input(unit, args):
48 return _handle_target_volume(unit, args, 'aux-input',
49 unit.protocol.get_aux_input_labels,
50 unit.protocol.set_aux_input,
51 unit.protocol.get_aux_input)
52
53
54 def handle_output_volume(unit, args):
55 return _handle_target_volume(unit, args, 'output-volume',
56 unit.protocol.get_output_labels,
57 unit.protocol.set_output_volume,
58 unit.protocol.get_output_volume)
59
60
61 def handle_headphone_volume(unit, args):
62 return _handle_target_volume(unit, args, 'headphone-volume',
63 unit.protocol.get_headphone_labels,
64 unit.protocol.set_headphone_volume,
65 unit.protocol.get_headphone_volume)
66
67
68 def handle_input_balance(unit, args):
69 chs = ('0', '1')
70 ops = ('set', 'get')
71 targets = unit.protocol.get_input_balance_labels()
72 if len(args) >= 1 and args[0] in targets:
73 target = args[0]
74 if len(args) >= 2 and args[1] in chs:
75 ch = int(args[1])
76 if len(args) >= 3 and args[2] in ops:
77 op = args[2]
78 if len(args) >= 4 and op == 'set':
79 balance = float(args[3])
80 unit.protocol.set_input_balance(target, ch, balance)
81 return True
82 elif op == 'get':
83 print(unit.protocol.get_input_balance(target, ch))
84 return True
85 print('Arguments for input-balance command')
86 print(' input-balance TARGET CH OP [BALANCE]')
87 print(' TARGET: [{0}]'.format('|'.join(targets)))
88 print(' CH: [{0}]'.format('|'.join(chs)))
89 print(' OP: [{0}]'.format('|'.join(ops)))
90 print(' BALANCE:[-128.0..128.0] (left-right) if OP=set')
91 return False
92
93
94 def handle_aux_balance(unit, args):
95 chs = ('0', '1')
96 ops = ('set', 'get')
97 if len(args) >= 1 and args[0] in chs:
98 ch = int(args[0])
99 if len(args) >= 2 and args[1] in ops:
100 op = args[1]
101 if len(args) >= 3 and op == 'set':
102 balance = float(args[2])
103 unit.protocol.set_aux_balance(ch, balance)
104 return True
105 else:
106 print(unit.protocol.get_aux_balance(ch))
107 return True
108 print('Arguments for aux-balance command:')
109 print(' aux-balance CH OP [BALANCE]')
110 print(' CH: [{0}]'.format('|'.join(chs)))
111 print(' OP: [{0}]'.format('|'.join(ops)))
112 print(' BALACE: [-128.0..128.0] (left-right) if OP=set]')
113 return False
114
115
116 def handle_aux_volume(unit, args):
117 chs = ('0', '1')
118 ops = ('set', 'get')
119 if len(args) > 0 and int(args[0]) in chs:
120 ch = int(args[0])
121 if len(args) > 1 and args[1] in ops:
122 op = args[1]
123 if len(args) > 2 and op == 'set':
124 db = float(args[2])
125 unit.protocol.set_aux_volume(ch, db)
126 return True
127 elif op == 'get':
128 print(unit.protocol.get_aux_volume(ch))
129 return True
130 print('Arguments for aux-volume command:')
131 print(' aux-volume CH OP [dB]')
132 print(' CH: [{0}]'.format('|'.join(chs)))
133 print(' OP: [{0}]'.format('|'.join(ops)))
134 print(' dB: [-128.0..128.0] if OP=set')
135 return False
136
137
138 def handle_mixer_routing(unit, args):
139 ops = ('set', 'get')
140 targets = unit.protocol.get_mixer_labels()
141 sources = unit.protocol.get_mixer_source_labels()
142 if len(args) >= 1 and args[0] in targets:
143 target = args[0]
144 if len(args) >= 2 and args[1] in sources:
145 source = args[1]
146 if len(args) >= 3 and args[2] in ops:
147 op = args[2]
148 if len(args) >= 4 and op == 'set':
149 enable = bool(int(args[3]))
150 unit.protocol.set_mixer_routing(target, source, enable)
151 return True
152 elif op == 'get':
153 print(unit.protocol.get_mixer_routing(target, source))
154 return True
155 print('Arguments for mixer-routing command:')
156 print(' mixer-routing TARGET SOURCE [ENABLE]')
157 print(' TARGET: [{0}]'.format('|'.join(targets)))
158 print(' SOURCE: [{0}]'.format('|'.join(sources)))
159 print(' OP: [{0}]'.format('|'.join(ops)))
160 print(' ENABLE: [0|1]')
161 return False
162
163
164 def _handle_target_source(unit, args, cmd, targets_func, sources_func,
165 set_func, get_func):
166 ops = ('set', 'get')
167 targets = targets_func()
168 if len(args) >= 1 and args[0] in targets:
169 target = args[0]
170 if len(args) >= 2 and args[1] in ops:
171 op = args[1]
172 sources = sources_func(target)
173 if len(args) >= 3 and op == 'set' and args[2] in sources:
174 source = args[2]
175 set_func(target, source)
176 return True
177 elif op == 'get':
178 print(get_func(target))
179 return True
180 print('Arguments for {0} command:'.format(cmd))
181 print(' {0} TARGET OP [SRC]'.format(cmd))
182 print(' TARGET: [{0}]'.format('|'.join(targets)))
183 print(' OP: [{0}]'.format('|'.join(ops)))
184 for target in targets:
185 sources = sources_func(target)
186 print(' SRC: [{0}] if TARGET={1} and OP=set'.format(
187 '|'.join(sources), target))
188 return False
189
190
191 def handle_headphone_source(unit, args):
192 return _handle_target_source(unit, args, 'headphone-source',
193 unit.protocol.get_headphone_labels,
194 unit.protocol.get_headphone_source_labels,
195 unit.protocol.set_headphone_source,
196 unit.protocol.get_headphone_source)
197
198
199 def handle_output_source(unit, args):
200 return _handle_target_source(unit, args, 'output-source',
201 unit.protocol.get_output_labels,
202 unit.protocol.get_output_source_labels,
203 unit.protocol.set_output_source,
204 unit.protocol.get_output_source)
205
206
207 def handle_clock_source(unit, args):
208 ops = ('set', 'get')
209 sources = unit.protocol.get_clock_source_labels()
210 if len(args) > 0 and args[0] in ops:
211 op = args[0]
212 if len(args) > 1 and op == 'set' and args[1] in sources:
213 source = args[1]
214 unit.protocol.set_clock_source(source)
215 return True
216 elif op == 'get':
217 print(unit.protocol.get_clock_source())
218 return True
219 print('Arguments for clock-source command:')
220 print(' clock-source OP [SRC]')
221 print(' OP: {0}'.format('|'.join(ops)))
222 print(' SRC: {0} if OP=set'.format('|'.join(sources)))
223 print(' Packet streaming should be stopped.')
224 return False
225
226
227 def handle_listen_metering(unit, args):
228 # This is handled by another context.
229 def handle_unix_signal(signum, frame):
230 sys.exit()
231 signal.signal(signal.SIGINT, handle_unix_signal)
232 while 1:
233 # At higher sampling rate, this causes timeout frequently.
234 try:
235 meters = unit.protocol.get_meters()
236 for name in sorted(meters):
237 print('{0}: {1:08x}'.format(name, meters[name]))
238 except Exception as e:
239 pass
240 print('')
241 time.sleep(0.1)
242 return True
243
244
245 def handle_sampling_rate(unit, args):
246 ops = ('set', 'get')
247 rates = unit.protocol.get_sampling_rate_labels()
248 if len(args) >= 1 and args[0] in ops:
249 op = args[0]
250 if len(args) >= 2 and op == 'set':
251 rate = int(args[1])
252 unit.protocol.set_sampling_rate(rate)
253 return True
254 elif op == 'get':
255 print(unit.protocol.get_sampling_rate())
256 return True
257 print('Arguments for sampling-rate command:')
258 print(' sampling-rate OP [RATE]')
259 print(' OP: {0}'.format('|'.join(ops)))
260 print(' RATE: {0}'.format('|'.join(map(str, rates))))
261 return False
262
263
264 cmds = {
265 'input-gain': handle_input_gain,
266 'input-balance': handle_input_balance,
267 'mixer-routing': handle_mixer_routing,
268 'listen-metering': handle_listen_metering,
269 'clock-source': handle_clock_source,
270 'sampling-rate': handle_sampling_rate,
271 }
272
273 fullpath = CliKit.seek_snd_unit_path()
274 if fullpath:
275 with MaudioUnit(fullpath) as unit:
276 if len(unit.protocol.get_aux_input_labels()) > 0:
277 cmds['aux-input'] = handle_aux_input
278 cmds['aux-volume'] = handle_aux_volume
279 if (hasattr(unit.protocol, 'set_aux_balance') and
280 hasattr(unit.protocol, 'get_aux_balance')):
281 cmds['aux-balance'] = handle_aux_balance
282 if len(unit.protocol.get_output_labels()) > 0:
283 cmds['output-volume'] = handle_output_volume
284 cmds['output-source'] = handle_output_source
285 if len(unit.protocol.get_headphone_labels()) > 0:
286 cmds['headphone-volume'] = handle_headphone_volume
287 cmds['headphone-source'] = handle_headphone_source
288 CliKit.dispatch_command(unit, cmds)