Debian Codegraph @

hinawa-apogee-ensemble-cli

raw
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.bebob.apogee_ensemble_unit import ApogeeEnsembleUnit
7
8
9 def handle_clock_src(unit, args):
10 ops = ('set', 'get')
11 srcs = unit.get_clock_src_labels()
12 if len(args) >= 1 and args[0] in ops:
13 op = args[0]
14 if op == 'set' and len(args) >= 2 and args[1] in srcs:
15 src = args[1]
16 unit.set_clock_src(src)
17 return True
18 elif op == 'get':
19 print(unit.get_clock_src())
20 return True
21 print('Arguments for clock-source command:')
22 print(' clock-source OP [SRC]')
23 print(' OP: [{0}]'.format('|'.join(ops)))
24 print(' SRC: [{0}] if OP=set'.format('|'.join(srcs)))
25 print(' Packet streaming should be stopped.')
26 return False
27
28
29 def handle_knob_states(unit, args):
30 states = unit.get_knob_states()
31 for key, val in states.items():
32 print('{0}: {1}'.format(key, val))
33 return True
34
35
36 def handle_knob_volume(unit, args):
37 ops = ('set', 'get')
38 targets = unit.get_knob_out_labels()
39 if len(args) >= 2 and args[0] in targets and args[1] in ops:
40 target = args[0]
41 op = args[1]
42 if op == 'set' and len(args) >= 3:
43 db = float(args[2])
44 unit.set_knob_out_volume(target, db)
45 return True
46 elif op == 'get':
47 states = unit.get_knob_states()
48 print(states[target])
49 return True
50 print('Arguments for out-volume command:')
51 print(' out-volume TARGET OP [dB]')
52 print(' TARGET: [{0}]'.format('|'.join(targets)))
53 print(' OP: [{0}]'.format('|'.join(ops)))
54 print(' dB: [-127.00..0.00] if OP=set')
55 return False
56
57
58 def handle_stream_mode(unit, args):
59 ops = ('set', 'get')
60 modes = unit.get_stream_mode_labels()
61 if len(args) >= 1:
62 op = args[0]
63 if op in ops:
64 if op == 'set' and len(args) >= 2 and args[1] in modes:
65 mode = args[1]
66 unit.set_stream_mode(mode)
67 return True
68 elif op == 'get':
69 print(unit.get_stream_mode())
70 return True
71 print('Arguments for stream-mode command:')
72 print(' stream-mode OP [MODE]')
73 print(' OP: [{0}]'.format('|'.join(ops)))
74 print(' MODE: [{0}] if OP=set'.format('|'.join(modes)))
75 print(' This operation corresponds bus-reset.')
76 return False
77
78
79 def handle_display_mode(unit, args):
80 ops = ('set', 'get')
81 if len(args) >= 1 and args[0] in ops:
82 op = args[0]
83 if op == 'set' and len(args) >= 2 and args[1] in ('0', '1'):
84 enable = bool(int(args[1]))
85 unit.set_display_mode(enable)
86 return True
87 elif op == 'get':
88 print(unit.get_display_mode())
89 return True
90 print('Arguments for display-mode command:')
91 print(' display-mode OP [ENABLE]')
92 print(' OP: [{0}]'.format('|'.join(ops)))
93 print(' ENABLE: [0|1] if OP=set')
94 return False
95
96
97 def handle_display_target(unit, args):
98 ops = ('set', 'get')
99 targets = unit.get_display_target_labels()
100 if len(args) >= 1 and args[0] in ops:
101 op = args[0]
102 if op == 'set' and len(args) >= 2 and args[1] in targets:
103 target = args[1]
104 unit.set_display_target(target)
105 return True
106 elif op == 'get':
107 print(unit.get_display_target())
108 return True
109 print('Arguments for display-target command:')
110 print(' display-target OP [TARGET]')
111 print(' OP: [{0}]'.format('|'.join(ops)))
112 print(' TARGET: [{0}] if OP=set'.format('|'.join(targets)))
113 return False
114
115
116 def handle_display_illuminate(unit, args):
117 ops = ('set', 'get')
118 if len(args) >= 1 and args[0] in ops:
119 op = args[0]
120 if op == 'set' and len(args) >= 2 and args[1] in ('0', '1'):
121 enable = bool(int(args[1]))
122 unit.set_display_illuminate(enable)
123 return True
124 elif op == 'get':
125 print(unit.get_display_illuminate())
126 return True
127 print('Arguments for display-illuminate command:')
128 print(' display-illuminate OP [ENABLE]')
129 print(' OP: [{0}]'.format('|'.join(ops)))
130 print(' ENABLE: [0|1]')
131 return False
132
133
134 def handle_display_overhold(unit, args):
135 ops = ('set', 'get')
136 if len(args) >= 1 and args[0] in ops:
137 op = args[0]
138 if op == 'set' and len(args) >= 2 and args[1] in ('0', '1'):
139 enable = bool(int(args[1]))
140 unit.set_display_overhold(enable)
141 return True
142 elif op == 'get':
143 print(unit.get_display_overhold())
144 return True
145 print('Arguments for display-overhold command:')
146 print(' display-overhold OP [ENABLE]')
147 print(' OP: [{0}]'.format('|'.join(ops)))
148 print(' ENABLE: [0|1]')
149 return False
150
151
152 def handle_opt_iface_mode(unit, args):
153 ops = ('set', 'get')
154 targets = unit.get_opt_iface_target_labels()
155 modes = unit.get_opt_iface_mode_labels()
156 if len(args) >= 2 and args[0] in targets and args[1] in ops:
157 target = args[0]
158 op = args[1]
159 if op == 'set' and len(args) >= 3 and args[2] in modes:
160 mode = args[2]
161 unit.set_opt_iface_mode(target, mode)
162 return True
163 elif op == 'get':
164 print(unit.get_opt_iface_mode(target))
165 return True
166 print('Arguments for opt-iface-mode command:')
167 print(' opt-iface-mode TARGET OP [MODE]')
168 print(' TARGET: [{0}]'.format('|'.join(targets)))
169 print(' OP: [{0}]'.format('|'.join(ops)))
170 print(' MODE: [{0}]'.format('|'.join(modes)))
171 return False
172
173
174 def handle_cd_mode(unit, args):
175 ops = ('set', 'get')
176 if len(args) >= 1 and args[0] in ops:
177 op = args[0]
178 if op == 'set' and len(args) >= 2 and args[1] in ('0', '1'):
179 enable = bool(int(args[1]))
180 unit.set_cd_mode(enable)
181 return True
182 elif op == 'get':
183 print(unit.get_cd_mode())
184 return True
185 print('Arguments for cd-mode command:')
186 print(' cd-mode OP [ENABLE]')
187 print(' OP: [{0}]'.format('|'.join(ops)))
188 print(' ENABLE: [0|1]')
189 return False
190
191
192 def handle_in_attenuate(unit, args):
193 ops = ('set', 'get')
194 targets = unit.get_line_in_labels()
195 attrs = unit.get_in_attr_labels()
196 if len(args) >= 2 and args[0] in targets and args[1] in ops:
197 target = args[0]
198 op = args[1]
199 if op == 'set' and len(args) >= 3 and args[2] in attrs:
200 attr = args[2]
201 unit.set_in_attr(target, attr)
202 return True
203 elif op == 'get':
204 print(unit.get_in_attr(target))
205 return True
206 print('Arguments for in-attenuate command:')
207 print(' in-attenuate TARGET OP [ATTR]')
208 print(' TARGET: [{0}]'.format('|'.join(targets)))
209 print(' OP: [{0}]'.format('|'.join(ops)))
210 print(' ATTR: [{0}] if OP=set'.format('|'.join(attrs)))
211 return False
212
213
214 def handle_in_limit(unit, args):
215 ops = ('set', 'get')
216 targets = unit.get_line_in_labels()
217 if len(args) >= 2 and args[0] in targets and args[1] in ops:
218 target = args[0]
219 op = args[1]
220 if op == 'set' and len(args) >= 3 and args[2] in ('0', '1'):
221 enable = bool(int(args[2]))
222 unit.set_soft_limit(target, enable)
223 return True
224 elif op == 'get':
225 print(unit.get_soft_limit(target))
226 return True
227 print('Arguments for in-limit command:')
228 print(' in-limit TARGET OP [ENABLE]')
229 print(' TARGET: [{0}]'.format('|'.join(targets)))
230 print(' OP: [{0}]'.format('|'.join(ops)))
231 print(' ENABLE: [0|1] if OP=set')
232 return False
233
234
235 def handle_mic_polarity(unit, args):
236 ops = ('set', 'get')
237 targets = unit.get_mic_labels()
238 if len(args) >= 2 and args[0] in targets and args[1] in ops:
239 target = args[0]
240 op = args[1]
241 if op == 'set' and len(args) >= 3 and args[2] in ('0', '1'):
242 invert = bool(int(args[2]))
243 unit.set_polarity(target, invert)
244 return True
245 elif op == 'get':
246 print(unit.get_polarity(target))
247 return True
248 print('Arguments for mic-polarity command:')
249 print(' mic-polarity TARGET OP [INVERT]')
250 print(' TARGET: [{0}]'.format('|'.join(targets)))
251 print(' OP: [{0}]'.format('|'.join(ops)))
252 print(' INVERT: [0|1] if OP=set')
253 return False
254
255
256 def handle_mic_power(unit, args):
257 ops = ('set', 'get')
258 targets = unit.get_mic_labels()
259 if len(args) >= 2 and args[0] in targets and args[1] in ops:
260 target = args[0]
261 op = args[1]
262 if op == 'set' and len(args) >= 3 and args[2] in ('0', '1'):
263 enable = bool(int(args[2]))
264 unit.set_phantom_power(target, enable)
265 return True
266 elif op == 'get':
267 print(unit.get_phantom_power(target))
268 return True
269 print('Arguments for mic-power command:')
270 print(' mic-power TARGET OP [ENABLE]')
271 print(' TARGET: [{0}]'.format('|'.join(targets)))
272 print(' OP: [{0}]'.format('|'.join(ops)))
273 print(' ENABLE: [0|1] if OP=set')
274 return False
275
276
277 def handle_out_attenuate(unit, args):
278 ops = ('set', 'get')
279 targets = unit.get_line_out_labels()
280 attrs = unit.get_out_attr_labels()
281 if len(args) >= 2 and args[0] in targets and args[1] in ops:
282 target = args[0]
283 op = args[1]
284 if op == 'set' and len(args) >= 3 and args[2] in attrs:
285 attr = args[2]
286 unit.set_out_attr(target, attr)
287 return True
288 elif op == 'get':
289 print(unit.get_out_attr(target))
290 return True
291 print('Arguments for out-attenuate command:')
292 print(' out-attenuate TARGET OP [ATTR]')
293 print(' TARGET: [{0}]'.format('|'.join(targets)))
294 print(' OP: [{0}]'.format('|'.join(ops)))
295 print(' ATTR: [{0}] if OP=set'.format('|'.join(attrs)))
296 return False
297
298
299 def handle_cap_src(unit, args):
300 ops = ('set', 'get')
301 targets = unit.get_cap_labels()
302 srcs = unit.get_cap_src_labels()
303 if len(args) >= 2 and args[0] in targets and args[1] in ops:
304 target = args[0]
305 op = args[1]
306 if op == 'set' and len(args) >= 3 and args[2] in srcs:
307 dst = args[2]
308 unit.set_cap_src(target, dst)
309 return True
310 elif op == 'get':
311 print(unit.get_cap_src(target))
312 return True
313 print('Arguments for cap-src command:')
314 print(' cap-src TARGET OP [DST]')
315 print(' TARGET: [{0}]'.format('|'.join(targets)))
316 print(' OP: [{0}]'.format('|'.join(ops)))
317 print(' SRC: [{0}] if OP=set'.format('|'.join(srcs)))
318 return False
319
320
321 def handle_out_src(unit, args):
322 ops = ('set', 'get')
323 targets = unit.get_out_labels()
324 srcs = unit.get_out_src_labels()
325 if len(args) >= 2 and args[0] in targets and args[1] in ops:
326 target = args[0]
327 op = args[1]
328 if op == 'set' and len(args) >= 3 and args[2] in srcs:
329 src = args[2]
330 unit.set_out_src(target, src)
331 return True
332 elif op == 'get':
333 print(unit.get_out_src(target))
334 return True
335 print('Arguments for out-src command:')
336 print(' out-src TARGET OP [SRC]')
337 print(' TARGET: [{0}]'.format('|'.join(targets)))
338 print(' OP: [{0}]'.format('|'.join(ops)))
339 print(' SRC: [{0}] if OP=set'.format('|'.join(srcs)))
340 return False
341
342
343 def handle_hp_src(unit, args):
344 ops = ('set', 'get')
345 targets = unit.get_hp_labels()
346 srcs = unit.get_hp_src_labels()
347 if len(args) >= 2 and args[0] in targets and args[1] in ops:
348 target = args[0]
349 op = args[1]
350 if op == 'set' and len(args) >= 3 and args[2] in srcs:
351 src = args[2]
352 unit.set_hp_src(target, src)
353 return True
354 elif op == 'get':
355 print(unit.get_hp_src(target))
356 return True
357 print('Arguments for hp-src command:')
358 print(' hp-src TARGET OP [SRC]')
359 print(' TARGET: [{0}]'.format('|'.join(targets)))
360 print(' OP: [{0}]'.format('|'.join(ops)))
361 print(' SRC: [{0}] if OP=set'.format('|'.join(srcs)))
362 return False
363
364
365 def handle_mixer_src(unit, args):
366 ops = ('set', 'get')
367 targets = unit.get_mixer_labels()
368 srcs = unit.get_mixer_src_labels()
369 if (len(args) >= 3 and args[0] in targets and args[1] in srcs
370 and args[2] in ops):
371 target = args[0]
372 src = args[1]
373 op = args[2]
374 if op == 'set' and len(args) >= 5:
375 db = float(args[3])
376 balance = float(args[4])
377 unit.set_mixer_src(target, src, db, balance)
378 return True
379 elif op == 'get':
380 print(unit.get_mixer_src(target, src))
381 return True
382 print('Arguments for mixer-src command:')
383 print(' mixer-src TARGET SRC OP [dB BALANCE]')
384 print(' TARGET: [{0}]'.format('|'.join(targets)))
385 print(' SRC: [{0}]'.format('|'.join(srcs)))
386 print(' OP: [{0}]'.format('|'.join(ops)))
387 print(' dB: [-48.0..0.0] if OP=set')
388 print(' BALANCE:[0.0..100.0] if OP=set')
389 return False
390
391
392 def handle_reset_meters(unit, args):
393 unit.reset_meters()
394 return True
395
396
397 def handle_16bit_mode(unit, args):
398 ops = ('set', 'get')
399 targets = unit.get_16bit_mode_labels()
400 if len(args) >= 1 and args[0] in ops:
401 op = args[0]
402 if op == 'set' and len(args) >= 2 and args[1] in targets:
403 target = args[1]
404 unit.set_16bit_mode(target)
405 return True
406 elif op == 'get':
407 print(unit.get_16bit_mode())
408 return True
409 print('Arguments for 16bit-mode command:')
410 print(' 16bit-mode OP [TARGET]')
411 print(' OP: [{0}]'.format('|'.join(ops)))
412 print(' TARGET: [{0}] if OP=set'.format('|'.join(targets)))
413 return False
414
415
416 def handle_spdif_resample(unit, args):
417 ops = ('set', 'get')
418 ifaces = unit.get_spdif_resample_iface_labels()
419 directions = unit.get_spdif_resample_direction_labels()
420 rates = unit.get_spdif_resample_rate_labels()
421 if len(args) >= 1 and args[0] in ops:
422 op = args[0]
423 if (op == 'set' and len(args) >= 5 and args[1] in ('0', '1')
424 and args[2] in ifaces and args[3] in directions
425 and int(args[4]) in rates):
426 enable = bool(int(args[1]))
427 iface = args[2]
428 direction = args[3]
429 rate = int(args[4])
430 unit.set_spdif_resample(enable, iface, direction, rate)
431 return True
432 elif op == 'get':
433 print(unit.get_spdif_resample())
434 return True
435 print('Arguments for spdif-resample command:')
436 print(' spdif-resample OP [ENABLE IFACE DIRECTION RATE]')
437 print(' OP: [{0}]'.format('|'.join(ops)))
438 print(' ENABLE: [0|1] if OP=set')
439 print(' IFACE: [{0}] if OP=set'.format('|'.join(ifaces)))
440 print(' DIRECTION: [{0}] if OP=set'.format('|'.join(directions)))
441 print(' RATE: [{0}] if OP=set'.format('|'.join(map(str, rates))))
442 return False
443
444
445 cmds = {
446 'clock-source': handle_clock_src,
447 'knob-states': handle_knob_states,
448 'knob-volume': handle_knob_volume,
449 'stream-mode': handle_stream_mode,
450 'display-mode': handle_display_mode,
451 'display-target': handle_display_target,
452 'display-illuminate': handle_display_illuminate,
453 'display-overhold': handle_display_overhold,
454 'opt-iface-mode': handle_opt_iface_mode,
455 'cd-mode': handle_cd_mode,
456 'in-attenuate': handle_in_attenuate,
457 'in-limit': handle_in_limit,
458 'mic-polarity': handle_mic_polarity,
459 'mic-power': handle_mic_power,
460 'out-attenuate': handle_out_attenuate,
461 'cap-src': handle_cap_src,
462 'out-src': handle_out_src,
463 'hp-src': handle_hp_src,
464 'mixer-src': handle_mixer_src,
465 'reset-meters': handle_reset_meters,
466 '16bit-mode': handle_16bit_mode,
467 'spdif-resample': handle_spdif_resample,
468 }
469
470 fullpath = CliKit.seek_snd_unit_path()
471 if fullpath:
472 with ApogeeEnsembleUnit(fullpath) as unit:
473 CliKit.dispatch_command(unit, cmds)