| 1 |
#!/usr/bin/env python3 |
| 2 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 3 |
# Copyright (C) 2018 Takashi Sakamoto |
| 4 |
|
| 5 |
import sys |
| 6 |
import time |
| 7 |
import signal |
| 8 |
|
| 9 |
from hinawa_utils.misc.cli_kit import CliKit |
| 10 |
from hinawa_utils.efw.efw_unit import EfwUnit |
| 11 |
|
| 12 |
|
| 13 |
def handle_hardware_info(unit, args): |
| 14 |
print('Mode: {0}'.format(unit.info['model'])) |
| 15 |
print('Supported features:') |
| 16 |
for key, value in unit.info['features'].items(): |
| 17 |
if value: |
| 18 |
print(' ', key) |
| 19 |
print('Supported signals for clock source:') |
| 20 |
for key, value in unit.info['clock-sources'].items(): |
| 21 |
if value: |
| 22 |
print(' ', key) |
| 23 |
print('Supported sampling rates:') |
| 24 |
for key, value in unit.info['sampling-rates'].items(): |
| 25 |
if value: |
| 26 |
print(' ', key) |
| 27 |
print('List of physical ports:') |
| 28 |
print(' Inputs:') |
| 29 |
for i, ch in enumerate(unit.info['phys-inputs']): |
| 30 |
print(' {0:02d}: {1}'.format(i, ch)) |
| 31 |
print(' Outputs:') |
| 32 |
for i, ch in enumerate(unit.info['phys-outputs']): |
| 33 |
print(' {0:02d}: {1}'.format(i, ch)) |
| 34 |
print('The number of channels for mixer:') |
| 35 |
print(' Capture: ', unit.info['capture-channels']) |
| 36 |
print(' Playback:', unit.info['playback-channels']) |
| 37 |
print('The number of PCM channels in a stream:') |
| 38 |
rates = ['44.1/48.0', '88.2/96.0'] |
| 39 |
if unit.info['sampling-rates'][176400]: |
| 40 |
rates.append('176.4/192.0') |
| 41 |
print(' tx:') |
| 42 |
for index, rate in enumerate(rates): |
| 43 |
print('{0:>16}: {1}'.format(rate, |
| 44 |
unit.info['tx-stream-channels'][index])) |
| 45 |
print(' tx:') |
| 46 |
for index, rate in enumerate(rates): |
| 47 |
print('{0:>16}: {1}'.format(rate, |
| 48 |
unit.info['rx-stream-channels'][index])) |
| 49 |
print('Firmware versions:') |
| 50 |
for key, value in unit.info['firmware-versions'].items(): |
| 51 |
print('{0:>8}: {1}'.format(key, value)) |
| 52 |
|
| 53 |
return True |
| 54 |
|
| 55 |
|
| 56 |
def handle_clock_source(unit, args): |
| 57 |
ops = ('set', 'get') |
| 58 |
sources = [] |
| 59 |
for key, value in unit.info['clock-sources'].items(): |
| 60 |
if value: |
| 61 |
sources.append(key) |
| 62 |
if len(args) >= 1 and args[0] in ops: |
| 63 |
op = args[0] |
| 64 |
rate, source = unit.get_clock_state() |
| 65 |
if len(args) >= 2 and op == 'set' and args[1] in sources: |
| 66 |
source = args[1] |
| 67 |
unit.set_clock_state(rate, source) |
| 68 |
return True |
| 69 |
elif op == 'get': |
| 70 |
print(source) |
| 71 |
return True |
| 72 |
print('Arguments for clock-source command:') |
| 73 |
print(' clock-source OP[SOURCE]') |
| 74 |
print(' OP: [{0}]'.format('|'.join(ops))) |
| 75 |
print(' SOURCE: [{0}]'.format('|'.join(sources))) |
| 76 |
return False |
| 77 |
|
| 78 |
|
| 79 |
def handle_sampling_rate(unit, args): |
| 80 |
ops = ('set', 'get') |
| 81 |
rates = [] |
| 82 |
for key, value in unit.info['sampling-rates'].items(): |
| 83 |
if value: |
| 84 |
rates.append(key) |
| 85 |
if len(args) >= 1 and args[0] in ops: |
| 86 |
op = args[0] |
| 87 |
rate, source = unit.get_clock_state() |
| 88 |
if len(args) >= 2 and op == 'set' and int(args[1]) in rates: |
| 89 |
rate = int(args[1]) |
| 90 |
unit.set_clock_state(rate, source) |
| 91 |
return True |
| 92 |
elif op == 'get': |
| 93 |
print(rate) |
| 94 |
return True |
| 95 |
print('Arguments for sampling-rate command:') |
| 96 |
print(' clock-source OP [SOURCE]') |
| 97 |
print(' OP: [{0}]'.format('|'.join(ops))) |
| 98 |
print(' SOURCE: [{0}]'.format('|'.join(map(str, rates)))) |
| 99 |
return False |
| 100 |
|
| 101 |
|
| 102 |
def handle_control_room(unit, args): |
| 103 |
ops = ('set', 'get') |
| 104 |
sources = unit.get_control_room_source_labels() |
| 105 |
if len(args) >= 1 and args[0] in ops: |
| 106 |
op = args[0] |
| 107 |
if len(args) >= 2 and op == 'set' and args[1] in sources: |
| 108 |
source = args[1] |
| 109 |
unit.set_control_room_mirroring(source) |
| 110 |
return True |
| 111 |
elif op == 'get': |
| 112 |
print(unit.get_control_room_mirroring()) |
| 113 |
return True |
| 114 |
print('Arguments for control-room command:') |
| 115 |
print(' control-room OP [SOURCE]') |
| 116 |
print(' OP: [{0}]'.format('|'.join(ops))) |
| 117 |
print(' SOURCE: [{0}]'.format('|'.join(sources))) |
| 118 |
return False |
| 119 |
|
| 120 |
|
| 121 |
def handle_digital_input(unit, args): |
| 122 |
ops = ('set', 'get') |
| 123 |
modes = unit.get_digital_input_mode_labels() |
| 124 |
if len(args) >= 1 and args[0] in ops: |
| 125 |
op = args[0] |
| 126 |
if len(args) >= 2 and op == 'set' and args[1] in modes: |
| 127 |
mode = args[1] |
| 128 |
unit.set_digital_input_mode(args[1]) |
| 129 |
return True |
| 130 |
elif op == 'get': |
| 131 |
print(unit.get_digital_input_mode()) |
| 132 |
return True |
| 133 |
print('Arguments for digital-input command:') |
| 134 |
print(' digital-input OP [MODE]') |
| 135 |
print(' OP: [{0}]'.format('|'.join(ops))) |
| 136 |
print(' MODE: [{0}] if OP=set'.format('|'.join(modes))) |
| 137 |
return False |
| 138 |
|
| 139 |
|
| 140 |
def handle_phantom_powering(unit, args): |
| 141 |
ops = ('set', 'get') |
| 142 |
if len(args) >= 1 and args[0] in ops: |
| 143 |
op = args[0] |
| 144 |
if len(args) >= 2 and op == ops[0] and int(args[1]) < 2: |
| 145 |
mode = int(args[1]) |
| 146 |
unit.set_phantom_powering(mode) |
| 147 |
return True |
| 148 |
else: |
| 149 |
print(unit.get_phantom_powering()) |
| 150 |
return True |
| 151 |
print('Arguments for phantom-powering command:') |
| 152 |
print(' phantom-powering OP [ENABLE]') |
| 153 |
print(' OP: [{0}]'.format('|'.join(ops))) |
| 154 |
print(' ENABLE: [0|1]') |
| 155 |
return False |
| 156 |
|
| 157 |
|
| 158 |
def handle_box_state(unit, args): |
| 159 |
states = unit.get_box_states() |
| 160 |
ops = ('set', 'get') |
| 161 |
if len(args) >= 2: |
| 162 |
name, op = args[0:2] |
| 163 |
if name in states and op in ops: |
| 164 |
labels = unit.get_box_state_labels(name) |
| 165 |
if op == 'set' and len(args) >= 3 and args[2] in labels: |
| 166 |
label = args[2] |
| 167 |
unit.set_box_states(name, label) |
| 168 |
return True |
| 169 |
elif op == 'get': |
| 170 |
print(states[name]) |
| 171 |
return True |
| 172 |
print('Arguments for box-state command:') |
| 173 |
print(' box-state NAME OP [VALUE]') |
| 174 |
print(' NAME: [{0}]'.format('|'.join(states))) |
| 175 |
print(' OP: [{0}]'.format('|'.join(ops))) |
| 176 |
for state in states: |
| 177 |
labels = unit.get_box_state_labels(state) |
| 178 |
print(' VALUE: [{0}] if NAME={1}'.format('|'.join(labels), state)) |
| 179 |
return False |
| 180 |
|
| 181 |
|
| 182 |
def handle_rx_stream_mapping(unit, args): |
| 183 |
ops = ('set', 'get') |
| 184 |
# Generate stereo pair for outputs |
| 185 |
targets = [] |
| 186 |
prev = '' |
| 187 |
num = 0 |
| 188 |
for index, name in enumerate(unit.info['phys-outputs']): |
| 189 |
if index % 2 == 0: |
| 190 |
if prev != name: |
| 191 |
num = 0 |
| 192 |
targets.append('{0}-{1}/{2}'.format(name, |
| 193 |
num * 2 + 1, num * 2 + 2)) |
| 194 |
prev = name |
| 195 |
num += 1 |
| 196 |
# Generate stereo pair for sources |
| 197 |
sources = [] |
| 198 |
for ch in range(unit.info['rx-stream-channels'][0]): |
| 199 |
if ch % 2 == 0: |
| 200 |
sources.append('stream-{0}/{1}'.format(ch + 1, ch + 2)) |
| 201 |
maps = unit.get_stream_mapping() |
| 202 |
if len(args) >= 2: |
| 203 |
target, op = args[0:2] |
| 204 |
if target in targets and op in ops: |
| 205 |
target = targets.index(target) |
| 206 |
if op == 'set' and len(args) >= 3 and args[2] in sources: |
| 207 |
maps['rx-map'][target] = sources.index(args[2]) |
| 208 |
unit.set_stream_mapping(maps['rx-map'], maps['tx-map']) |
| 209 |
return True |
| 210 |
elif op == 'get': |
| 211 |
print(sources[maps['rx-map'][target] // 2]) |
| 212 |
return True |
| 213 |
print('Arguments for rx-stream-mapping command:') |
| 214 |
print(' rx-stream-mapping TARGETS OP [SOURCE]') |
| 215 |
print(' TARGET: [{0}]'.format('|'.join(targets))) |
| 216 |
print(' OP: [{0}]'.format('|'.join(ops))) |
| 217 |
print(' SOURCE: [{0}]'.format('|'.join(sources))) |
| 218 |
return False |
| 219 |
|
| 220 |
|
| 221 |
def handle_tx_stream_mapping(unit, args): |
| 222 |
ops = ('set', 'get') |
| 223 |
# Generate stereo pair for inputs |
| 224 |
inputs = [] |
| 225 |
prev = '' |
| 226 |
num = 0 |
| 227 |
for index, name in enumerate(unit.info['phys-inputs']): |
| 228 |
if index % 2 == 0: |
| 229 |
if prev != name: |
| 230 |
num = 0 |
| 231 |
inputs.append('{0}-{1}/{2}'.format(name, num * 2 + 1, num * 2 + 2)) |
| 232 |
prev = name |
| 233 |
num += 1 |
| 234 |
# Generate stereo pair for sinks |
| 235 |
sinks = [] |
| 236 |
for ch in range(unit.info['tx-stream-channels'][0]): |
| 237 |
if ch % 2 == 0: |
| 238 |
sinks.append('stream-{0}/{1}'.format(ch + 1, ch + 2)) |
| 239 |
maps = unit.get_stream_mapping() |
| 240 |
if len(args) >= 2: |
| 241 |
op, input = args[0:2] |
| 242 |
if op in ops and input in inputs: |
| 243 |
input = inputs.index(input) |
| 244 |
if op == 'set' and len(args) >= 3 and args[2] in sinks: |
| 245 |
sink = sinks.index(args[2]) * 2 |
| 246 |
maps['tx-map'][input] = sink |
| 247 |
unit.set_stream_mapping(maps['tx-map'], maps['tx-map']) |
| 248 |
return True |
| 249 |
else: |
| 250 |
print(sinks[maps['tx-map'][input] // 2]) |
| 251 |
return True |
| 252 |
print('Arguments for tx-stream-mapping command:') |
| 253 |
print(' tx-stream-mapping OP INPUT [SINK]') |
| 254 |
print(' OP: [{0}]'.format('|'.join(ops))) |
| 255 |
print(' INPUT: [{0}]'.format('|'.join(inputs))) |
| 256 |
print(' SINK: [{0}]'.format('|'.join(sinks))) |
| 257 |
return False |
| 258 |
|
| 259 |
|
| 260 |
def handle_output(unit, args): |
| 261 |
items = { |
| 262 |
'gain': (unit.set_phys_out_gain, unit.get_phys_out_gain), |
| 263 |
'mute': (unit.set_phys_out_mute, unit.get_phys_out_mute), |
| 264 |
} |
| 265 |
ops = ('set', 'get') |
| 266 |
if unit.info['features']['nominal-output']: |
| 267 |
items['nominal'] = ( |
| 268 |
unit.set_phys_out_nominal, |
| 269 |
unit.get_phys_out_nominal, |
| 270 |
) |
| 271 |
if len(args) >= 3: |
| 272 |
item, ch, op = args[0:3] |
| 273 |
ch = int(ch) |
| 274 |
if item in items and ch < len(unit.info['phys-outputs']) and op in ops: |
| 275 |
set_func = items[item][0] |
| 276 |
get_func = items[item][1] |
| 277 |
if op == 'set' and len(args) >= 4: |
| 278 |
if item == 'gain': |
| 279 |
val = float(args[3]) |
| 280 |
else: |
| 281 |
val = int(args[3]) |
| 282 |
set_func(ch, val) |
| 283 |
return True |
| 284 |
elif op == 'get': |
| 285 |
print(get_func(ch)) |
| 286 |
return True |
| 287 |
print('Arguments for output command:') |
| 288 |
print(' output ITEM CH OP [dB|ENABLE]') |
| 289 |
print(' ITEM: [{0}]'.format('|'.join(items))) |
| 290 |
print(' CH: [0-{0}]'.format(len(unit.info['phys-outputs']) - 1)) |
| 291 |
print(' OP: [{0}]'.format('|'.join(ops))) |
| 292 |
print(' dB: [-144.0..0.0] if OP=get ITEM=gain') |
| 293 |
print(' ENABLE: [0|1] if OP=get ITEM=mute') |
| 294 |
if 'nominal' in items: |
| 295 |
print(' ENABLE: [0|1] if OP=get ITEM=nominal') |
| 296 |
return False |
| 297 |
|
| 298 |
|
| 299 |
def handle_input(unit, args): |
| 300 |
items = { |
| 301 |
'nominal': (unit.set_phys_in_nominal, unit.get_phys_in_nominal), |
| 302 |
} |
| 303 |
ops = ('set', 'get') |
| 304 |
if len(args) >= 3: |
| 305 |
item, ch, op = args[0:3] |
| 306 |
ch = int(ch) |
| 307 |
if item in items and ch < len(unit.info['phys-inputs']) and op in ops: |
| 308 |
set_func = items[item][0] |
| 309 |
get_func = items[item][1] |
| 310 |
if len(args) >= 4 and op == 'set': |
| 311 |
val = int(args[3]) |
| 312 |
set_func(ch, val) |
| 313 |
return True |
| 314 |
elif op == 'get': |
| 315 |
print(get_func(ch)) |
| 316 |
return True |
| 317 |
print('Arguments for input command:') |
| 318 |
print(' input ITEM CH OP [ENABLE]') |
| 319 |
print(' ITEM: [{0}]'.format('|'.join(items))) |
| 320 |
print(' CH: [0-{0}]'.format(len(unit.info['phys-inputs']) - 1)) |
| 321 |
print(' OP: [{0}]'.format('|'.join(ops))) |
| 322 |
print(' ENABLE: [0|1] if OP=set') |
| 323 |
return False |
| 324 |
|
| 325 |
|
| 326 |
def handle_playback(unit, args): |
| 327 |
items = { |
| 328 |
# key: (set_func, get_func, min_val, max_val) |
| 329 |
'gain': (unit.set_playback_gain, unit.get_playback_gain), |
| 330 |
'mute': (unit.set_playback_mute, unit.get_playback_mute), |
| 331 |
'solo': (unit.set_playback_solo, unit.get_playback_solo), |
| 332 |
} |
| 333 |
ops = ('set', 'get') |
| 334 |
if len(args) >= 3: |
| 335 |
item, ch, op = args[0:3] |
| 336 |
ch = int(ch) |
| 337 |
if item in items and ch < unit.info['playback-channels'] and op in ops: |
| 338 |
set_func = items[item][0] |
| 339 |
get_func = items[item][1] |
| 340 |
if op == 'set' and len(args) >= 4: |
| 341 |
if item == 'gain': |
| 342 |
val = float(args[3]) |
| 343 |
else: |
| 344 |
val = int(args[3]) |
| 345 |
set_func(ch, val) |
| 346 |
return True |
| 347 |
elif op == 'get': |
| 348 |
print(get_func(ch)) |
| 349 |
return True |
| 350 |
print('Arguments for playback command:') |
| 351 |
print(' playback ITEM CH OP [dB|ENABLE]') |
| 352 |
print(' ITEM: [{0}]'.format('|'.join(items))) |
| 353 |
print(' CH: [0-{0}]'.format(unit.info['playback-channels'] - 1)) |
| 354 |
print(' OP: [{0}]'.format('|'.join(ops))) |
| 355 |
print(' dB: [-144.0..0] if OP=set and ITEM=gain') |
| 356 |
print(' ENABLE: [0|1] if OP=set and ITEM=mute') |
| 357 |
print(' ENABLE: [0|1] if OP=set and ITEM=solo') |
| 358 |
return False |
| 359 |
|
| 360 |
|
| 361 |
def handle_monitor(unit, args): |
| 362 |
items = { |
| 363 |
'gain': (unit.set_monitor_gain, unit.get_monitor_gain), |
| 364 |
'mute': (unit.set_monitor_mute, unit.get_monitor_mute), |
| 365 |
'solo': (unit.set_monitor_solo, unit.get_monitor_solo), |
| 366 |
'pan': (unit.set_monitor_pan, unit.get_monitor_pan,) |
| 367 |
} |
| 368 |
ops = ('set', 'get') |
| 369 |
if len(args) >= 4: |
| 370 |
item, input, output, op = args[0:4] |
| 371 |
input = int(input) |
| 372 |
output = int(output) |
| 373 |
if (item in items and input < unit.info['capture-channels'] and |
| 374 |
output < unit.info['playback-channels'] and op in ops): |
| 375 |
set_func = items[item][0] |
| 376 |
get_func = items[item][1] |
| 377 |
if op == 'set' and len(args) >= 5: |
| 378 |
if item == 'gain': |
| 379 |
val = float(args[4]) |
| 380 |
else: |
| 381 |
val = int(args[4]) |
| 382 |
set_func(input, output, val) |
| 383 |
return True |
| 384 |
elif op == 'get': |
| 385 |
print(get_func(input, output)) |
| 386 |
return True |
| 387 |
print('Arguments for monitor command:') |
| 388 |
print(' monitor ITEM INPUT OUTPUT OP [dB|BALANCE|ENABLE]') |
| 389 |
print(' ITEM: [{0}]'.format('|'.join(items))) |
| 390 |
print(' INPUT: [0-{0}]'.format(unit.info['capture-channels'] - 1)) |
| 391 |
print(' OUTPUT: [0-{0}]'.format(unit.info['playback-channels'] - 1)) |
| 392 |
print(' OP: [{0}]'.format('|'.join(ops))) |
| 393 |
print(' dB: [-144.0..0.0] if OP=set and ITEM=gain') |
| 394 |
print(' ENABLE: [0|1] if OP=set and ITEM=mute') |
| 395 |
print(' ENABLE: [0|1] if OP=set and ITEM=solo') |
| 396 |
print(' BALANCE:[0..255] (left-right) if OP=set and ITEM=pan') |
| 397 |
return False |
| 398 |
|
| 399 |
|
| 400 |
def handle_listen_metering(unit, args): |
| 401 |
# This is handled by another context. |
| 402 |
def handle_unix_signal(signum, frame): |
| 403 |
sys.exit() |
| 404 |
signal.signal(signal.SIGINT, handle_unix_signal) |
| 405 |
while 1: |
| 406 |
meters = unit.get_metering() |
| 407 |
for name in sorted(meters): |
| 408 |
print(name, meters[name]) |
| 409 |
print('') |
| 410 |
time.sleep(0.1) |
| 411 |
return True |
| 412 |
|
| 413 |
|
| 414 |
def get_available_commands(features): |
| 415 |
cmds = { |
| 416 |
'hardware-info': handle_hardware_info, |
| 417 |
'clock-source': handle_clock_source, |
| 418 |
'sampling-rate': handle_sampling_rate, |
| 419 |
'box-state': handle_box_state, |
| 420 |
'output': handle_output, |
| 421 |
'playback': handle_playback, |
| 422 |
'monitor': handle_monitor, |
| 423 |
'listen-metering': handle_listen_metering, |
| 424 |
} |
| 425 |
if features['control-room-mirroring']: |
| 426 |
cmds['control-room'] = handle_control_room |
| 427 |
if features['rx-mapping']: |
| 428 |
cmds['rx-stream-mapping'] = handle_rx_stream_mapping |
| 429 |
if features['tx-mapping']: |
| 430 |
cmds['tx-stream-mapping'] = handle_tx_stream_mapping |
| 431 |
if features['phantom-powering']: |
| 432 |
cmds['phantom-powering'] = handle_phantom_powering |
| 433 |
if features['nominal-input']: |
| 434 |
cmds['input'] = handle_input |
| 435 |
|
| 436 |
count = 0 |
| 437 |
for iface in ('spdif-coax', 'spdif-opt', 'aesebu-xlr', 'adat-opt'): |
| 438 |
if features[iface]: |
| 439 |
count += 1 |
| 440 |
if count > 1: |
| 441 |
cmds['digital-input'] = handle_digital_input |
| 442 |
|
| 443 |
return cmds |
| 444 |
|
| 445 |
|
| 446 |
fullpath = CliKit.seek_snd_unit_path() |
| 447 |
if fullpath: |
| 448 |
with EfwUnit(fullpath) as unit: |
| 449 |
cmds = get_available_commands(unit.info['features']) |
| 450 |
CliKit.dispatch_command(unit, cmds) |