Debian Codegraph @

hinawa-yamaha-terratec-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.phase_go_unit import PhaseGoUnit
7
8
9 def handle_current_status(unit, args):
10 print('Current status:')
11 print(' Packet Streaming:')
12 print(' started: {0}'.format(unit.get_property('is-locked')))
13 rate = unit.protocol.get_sampling_rate()
14 print(' sampling-rate: {0}'.format(rate))
15 source = unit.protocol.get_clock_source()
16 print(' clock-source: {0}'.format(source))
17
18 print(' Routing:')
19 for target in unit.protocol.get_output_labels():
20 source = unit.protocol.get_output_source(target)
21 print(' {0}: {1}'.format(target, source))
22
23 print('Mixer inputs:')
24 for target in unit.protocol.get_mixer_input_labels():
25 print(' {0}:'.format(target))
26 for i in range(1, 3):
27 gain = unit.protocol.get_mixer_input_volume(target, i)
28 mute = unit.protocol.get_mixer_input_mute(target, i)
29 print(' gain: {0}, mute: {1}'.format(gain, mute))
30
31 print('Mixer output:')
32 for i in range(1, 3):
33 volume = unit.protocol.get_mixer_output_volume(i)
34 mute = unit.protocol.get_mixer_output_mute(i)
35 print(' volume: {0}, mute: {1}'.format(gain, mute))
36
37 if hasattr(unit.protocol, 'get_analog_input_level_labels'):
38 print('Analog input settings:')
39 level = unit.protocol.get_analog_input_level()
40 print(' level: {0}'.format(level))
41
42 if hasattr(unit.protocol, 'get_analog_output_labels'):
43 print('Analog output settings:')
44 for target in unit.protocol.get_analog_output_labels():
45 print(' {0}'.format(target))
46 for i in range(1, 3):
47 volume = unit.protocol.get_analog_output_volume(target, i)
48 mute = unit.protocol.get_analog_output_mute(target, i)
49 print(' volume: {0}, mute: {1}'.format(volume, mute))
50
51 return True
52
53
54 def handle_firmware_info(unit, args):
55 print('BeBoB firmware information:')
56 for category, values in unit.firmware_info.items():
57 if isinstance(values, dict):
58 print(' {0}:'.format(category))
59 for name, value in values.items():
60 print(' {0}: {1}'.format(name, value))
61 else:
62 print(' {0}: {1}'.format(category, values))
63 return True
64
65
66 def handle_output_source(unit, args):
67 ops = ('set', 'get')
68 targets = unit.protocol.get_output_labels()
69 sources = unit.protocol.get_output_source_labels()
70 if len(args) >= 1 and args[0] in targets:
71 target = args[0]
72 if len(args) >= 2 and args[1] in ops:
73 op = args[1]
74 if len(args) >= 3 and op == 'set' and args[2] in sources:
75 source = args[2]
76 unit.protocol.set_output_source(target, source)
77 return True
78 elif op == 'get':
79 print(unit.protocol.get_output_source(target))
80 return True
81 print('Arguments for output-source command:')
82 print(' output-source TARGET OP [SOURCE]')
83 print(' TARGET: [{0}]'.format('|'.join(targets)))
84 print(' OP: [{0}]'.format('|'.join(ops)))
85 print(' SOURCE: [{0}]'.format('|'.join(sources)))
86 return False
87
88
89 def _handle_target_request(unit, args, cmd, items, targets):
90 chs = ('1', '2')
91 ops = ('set', 'get')
92 if len(args) >= 1 and args[0] in items:
93 item = args[0]
94 set_func = items[item][0]
95 get_func = items[item][1]
96 if len(args) >= 2 and args[1] in targets:
97 target = args[1]
98 if len(args) >= 3 and args[2] in chs:
99 ch = int(args[2])
100 if len(args) >= 4 and args[3] in ops:
101 op = args[3]
102 if len(args) >= 5 and op == 'set':
103 if item == 'volume':
104 val = float(args[4])
105 else:
106 val = bool(int(args[4]))
107 set_func(target, ch, val)
108 return True
109 elif op == 'get':
110 print(get_func(target, ch))
111 return True
112 print('Arguments for {0} command:'.format(cmd))
113 print(' {0} ITEM TARGET CH OP [dB|ENABLE]'.format(cmd))
114 print(' ITEM: [{0}]'.format('|'.join(items)))
115 print(' TARGET: [{0}]'.format('|'.join(targets)))
116 print(' CH: [{0}]'.format('|'.join(chs)))
117 print(' OP: [{0}]'.format('|'.join(ops)))
118 print(' dB: [-128.0..128.0] if ITEM=volume and OP=set')
119 print(' ENABLE: [0|1] if ITEM=mute and OP=set')
120 return False
121
122
123 def handle_mixer_input(unit, args):
124 items = {
125 'volume': (unit.protocol.set_mixer_input_volume,
126 unit.protocol.get_mixer_input_volume),
127 'mute': (unit.protocol.set_mixer_input_mute,
128 unit.protocol.get_mixer_input_mute),
129 }
130 return _handle_target_request(unit, args, 'mixer-input', items,
131 unit.protocol.get_mixer_input_labels())
132
133
134 def handle_mixer_output(unit, args):
135 items = {
136 'volume': (unit.protocol.set_mixer_output_volume,
137 unit.protocol.get_mixer_output_volume),
138 'mute': (unit.protocol.set_mixer_output_mute,
139 unit.protocol.get_mixer_output_mute),
140 }
141 ops = ('set', 'get')
142 chs = ('1', '2')
143 if len(args) >= 1 and args[0] in items:
144 item = args[0]
145 set_func = items[item][0]
146 get_func = items[item][1]
147 if len(args) >= 2 and args[1] in chs:
148 ch = int(args[1])
149 if len(args) >= 3 and args[2] in ops:
150 op = args[2]
151 if len(args) >= 4 and op == 'set':
152 if item == 'volume':
153 val = float(args[3])
154 else:
155 val = bool(int(args[3]))
156 set_func(ch, val)
157 return True
158 elif op == 'get':
159 print(get_func(ch))
160 return True
161 print('Arguments for mixer-output command:')
162 print(' mixer-output ITEM OP CH [dB|ENABLE]')
163 print(' ITEM: [{0}]'.format('|'.join(items)))
164 print(' CH: [{0}]'.format('|'.join(chs)))
165 print(' OP: [{0}]'.format('|'.join(ops)))
166 print(' dB: [-128.0..128.0] if ITEM=volume and OP=set')
167 print(' ENABLE: [0|1] if ITEM=mute and OP=set')
168 return False
169
170
171 def handle_sampling_rate(unit, args):
172 ops = ('set', 'get')
173 rates = unit.protocol.get_sampling_rate_labels()
174 if len(args) >= 1 and args[0] in ops:
175 op = args[0]
176 if len(args) >= 2 and op == 'set' and int(args[1]) in rates:
177 rate = int(args[1])
178 if unit.get_property('is-locked'):
179 print('Packet is-locked started.')
180 return False
181 unit.protocol.set_sampling_rate(rate)
182 return True
183 elif op == 'get':
184 print(unit.protocol.get_sampling_rate())
185 return True
186 print('Arguments for sampling-rate command:')
187 print(' sampling-rate OP [RATE]')
188 print(' OP: {0}'.format('|'.join(ops)))
189 print(' RATE: {0}'.format('|'.join(map(str, rates))))
190 return False
191
192
193 def handle_clock_source(unit, args):
194 ops = ('set', 'get')
195 sources = unit.protocol.get_clock_source_labels()
196 if len(args) >= 1 and args[0] in ops:
197 op = args[0]
198 if len(args) >= 2 and op == 'set' and args[1] in sources:
199 if unit.get_property('is-locked'):
200 print('Packet is-locked started.')
201 return False
202 source = args[1]
203 unit.protocol.set_clock_source(source)
204 return True
205 elif op == 'get':
206 print(unit.protocol.get_clock_source())
207 return True
208 print('Arguments for clock-source command:')
209 print(' clock-source OPERATION [SOURCE]')
210 print(' OPERATION: [{0}]'.format('|'.join(ops)))
211 print(' SOURCE: [{0}]'.format('|'.join(sources)))
212 return False
213
214
215 def handle_analog_input_level(unit, args):
216 ops = ('set', 'get')
217 levels = unit.protocol.get_analog_input_level_labels()
218 if len(args) >= 1 and args[0] in ops:
219 op = args[0]
220 if len(args) >= 2 and op == 'set':
221 unit.protocol.set_analog_input_level(args[1])
222 return True
223 elif op == 'get':
224 print(unit.protocol.get_analog_input_level())
225 return True
226 print('Arguments for analog-input-level command:')
227 print(' analog-input-level OP [LEVEL]')
228 print(' OP: [{0}]'.format('|'.join(ops)))
229 print(' LEVEL: [{0}] if OP=set'.format('|'.join(levels)))
230 return False
231
232
233 def handle_analog_output(unit, args):
234 items = {
235 'volume': (unit.protocol.set_analog_output_volume,
236 unit.protocol.get_analog_output_volume),
237 'mute': (unit.protocol.set_analog_output_mute,
238 unit.protocol.get_analog_output_mute),
239 }
240 return _handle_target_request(unit, args, 'analog-output', items,
241 unit.protocol.get_analog_output_labels())
242
243
244 cmds = {
245 'current-status': handle_current_status,
246 'firmware-info': handle_firmware_info,
247 'output-source': handle_output_source,
248 'mixer-input': handle_mixer_input,
249 'mixer-output': handle_mixer_output,
250 'sampling-rate': handle_sampling_rate,
251 'clock-source': handle_clock_source,
252 }
253
254 fullpath = CliKit.seek_snd_unit_path()
255 if fullpath:
256 with PhaseGoUnit(fullpath) as unit:
257 if hasattr(unit.protocol, 'get_analog_input_level_labels'):
258 cmds['input-level'] = handle_analog_input_level
259 if hasattr(unit.protocol, 'get_analog_output_labels'):
260 cmds['analog-output'] = handle_analog_output
261 CliKit.dispatch_command(unit, cmds)