| 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.focusrite_saffirepro_io import FocusriteSaffireproIoUnit |
| 7 |
|
| 8 |
|
| 9 |
def handle_mixer_input(unit, args): |
| 10 |
ITEMS = { |
| 11 |
'balance': (unit.set_mixer_input_balance, |
| 12 |
unit.get_mixer_input_balance), |
| 13 |
'gain': (unit.set_mixer_input_gain, |
| 14 |
unit.get_mixer_input_gain), |
| 15 |
} |
| 16 |
CHS = (1, 2) |
| 17 |
OPS = ('set', 'get') |
| 18 |
targets = unit.get_mixer_input_labels() |
| 19 |
if len(args) >= 1 and args[0] in targets: |
| 20 |
target = args[0] |
| 21 |
if len(args) >= 2 and args[1] in ITEMS: |
| 22 |
set_func, get_func = ITEMS[args[1]] |
| 23 |
if len(args) >= 3 and int(args[2]) in CHS: |
| 24 |
ch = int(args[2]) |
| 25 |
if len(args) >= 4 and args[3] in OPS: |
| 26 |
op = args[3] |
| 27 |
if len(args) >= 5 and op == 'set': |
| 28 |
val = float(args[4]) |
| 29 |
set_func(target, ch, val) |
| 30 |
return True |
| 31 |
elif op == 'get': |
| 32 |
print(get_func(target, ch)) |
| 33 |
return True |
| 34 |
print('Arguments for mixer-input command:') |
| 35 |
print(' mixer-input TARGET ITEM CH OP [dB|BALANCE]') |
| 36 |
print(' TARGET: [{0}]'.format('|'.join(targets))) |
| 37 |
print(' ITEM: [{0}]'.format('|'.join(ITEMS))) |
| 38 |
print(' CH: [{0}] (0=both)'.format('|'.join(map(str, CHS)))) |
| 39 |
print(' OP: [{0}]'.format('|'.join(OPS))) |
| 40 |
print(' dB: [-128.0..128.0] if ITEM=gain and OP=set') |
| 41 |
print(' BALANCE:[0..99] (left-to-right) if ITEM=balance and OP=set') |
| 42 |
return False |
| 43 |
|
| 44 |
|
| 45 |
def handle_output_params(unit, args): |
| 46 |
ITEMS = { |
| 47 |
'volume': (unit.set_output_volume, unit.get_output_volume), |
| 48 |
'mute': (unit.set_output_mute, unit.get_output_mute), |
| 49 |
'dim': (unit.set_output_dim, unit.get_output_dim), |
| 50 |
'hwctl': (unit.set_output_hwctl, unit.get_output_hwctl), |
| 51 |
'pad': (unit.set_output_pad, unit.get_output_pad), |
| 52 |
} |
| 53 |
OPS = ('set', 'get') |
| 54 |
targets = unit.get_output_labels() |
| 55 |
if len(args) >= 1 and args[0] in targets: |
| 56 |
target = args[0] |
| 57 |
if len(args) >= 2 and args[1] in ITEMS: |
| 58 |
item = args[1] |
| 59 |
set_func, get_func = ITEMS[item] |
| 60 |
if len(args) >= 3 and args[2] in OPS: |
| 61 |
op = args[2] |
| 62 |
if len(args) >= 4 and op == 'set': |
| 63 |
db = float(args[3]) |
| 64 |
set_func(target, db) |
| 65 |
return True |
| 66 |
elif op == 'get': |
| 67 |
print(get_func(target)) |
| 68 |
return True |
| 69 |
print('Arguments for output-params command:') |
| 70 |
print(' output-params TARGET ITEM OP [dB|MUTE|DIM|HECTL|PAD]') |
| 71 |
print(' TARGET: [{0}]'.format('|'.join(targets))) |
| 72 |
print(' ITEM: [{0}]'.format('|'.join(ITEMS))) |
| 73 |
print(' OP: [{0}]'.format('|'.join(OPS))) |
| 74 |
print(' dB: [-128.00..128.00] if ITEM=volume and OP=set') |
| 75 |
print(' MUTE: [0|1] if ITEM=mute and OP=set') |
| 76 |
print(' DIM: [0|1] if ITEM=dim and OP=set') |
| 77 |
print(' HWCTL: [0|1] if ITEM=hwctl and OP=set') |
| 78 |
print(' PAD: [0|1] if ITEM=pad and OP=set') |
| 79 |
return False |
| 80 |
|
| 81 |
|
| 82 |
def handle_output_source(unit, args): |
| 83 |
OPS = ('set', 'get') |
| 84 |
targets = unit.get_output_destination_labels() |
| 85 |
if len(args) >= 1 and args[0] in targets: |
| 86 |
target = args[0] |
| 87 |
if len(args) >= 2 and args[1] in OPS: |
| 88 |
op = args[1] |
| 89 |
sources = unit.get_output_source_labels(target) |
| 90 |
if len(args) >= 3 and op == 'set' and args[2] in sources: |
| 91 |
source = args[2] |
| 92 |
unit.set_output_source(target, source) |
| 93 |
return True |
| 94 |
elif op == 'get': |
| 95 |
print(unit.get_output_source(target)) |
| 96 |
return True |
| 97 |
print('Arguments for output-source command:') |
| 98 |
print(' output-source TARGET OP [SRC]') |
| 99 |
print(' TARGET: [{0}]'.format('|'.join(targets))) |
| 100 |
print(' OP: [{0}]'.format('|'.join(OPS))) |
| 101 |
for target in targets: |
| 102 |
sources = unit.get_output_source_labels(target) |
| 103 |
print(' SRC: [{0}] if TARGET={1} and OP=set'.format( |
| 104 |
'|'.join(sources), target)) |
| 105 |
return False |
| 106 |
|
| 107 |
|
| 108 |
def handle_rate_mode(unit, args): |
| 109 |
OPS = ('set', 'get') |
| 110 |
modes = unit.get_supported_rate_modes() |
| 111 |
if len(args) >= 1 and args[0] in OPS: |
| 112 |
op = args[0] |
| 113 |
if len(args) >= 2 and op == 'set' and args[1] in modes: |
| 114 |
mode = args[1] |
| 115 |
unit.set_rate_mode(mode) |
| 116 |
return True |
| 117 |
elif op == 'get': |
| 118 |
print(unit.get_rate_mode()) |
| 119 |
return True |
| 120 |
print('Arguments for rate-mode command:') |
| 121 |
print(' rate-mode OP [MODE]') |
| 122 |
print(' OP: [{0}]'.format('|'.join(OPS))) |
| 123 |
print(' MODE: [{0}]'.format('|'.join(modes))) |
| 124 |
print(' Packet streaming should be stopped.') |
| 125 |
print(' The set operation corresponds to bus reset.') |
| 126 |
return False |
| 127 |
|
| 128 |
|
| 129 |
def handle_sampling_rate(unit, args): |
| 130 |
OPS = ('set', 'get') |
| 131 |
rates = unit.get_supported_sampling_rates() |
| 132 |
if len(args) >= 1 and args[0] in OPS: |
| 133 |
op = args[0] |
| 134 |
if len(args) >= 2 and op == 'set' and int(args[1]) in rates: |
| 135 |
rate = int(args[1]) |
| 136 |
unit.set_sampling_rate(rate) |
| 137 |
return True |
| 138 |
elif op == 'get': |
| 139 |
print(unit.get_sampling_rate()) |
| 140 |
return True |
| 141 |
print('Arguments for sampling-rate command:') |
| 142 |
print(' sampling-rate OP [SOURCE]') |
| 143 |
print(' OP: [{0}]'.format('|'.join(OPS))) |
| 144 |
print(' SOURCE: [{0}]'.format('|'.join(map(str, rates)))) |
| 145 |
print(' Packet streaming should be stopped.') |
| 146 |
return False |
| 147 |
|
| 148 |
|
| 149 |
def handle_clock_source(unit, args): |
| 150 |
OPS = ('set', 'get') |
| 151 |
sources = unit.get_supported_clock_sources() |
| 152 |
if len(args) > 0 and args[0] in OPS: |
| 153 |
op = args[0] |
| 154 |
if len(args) > 1 and op == 'set' and args[1] in sources: |
| 155 |
source = args[1] |
| 156 |
unit.set_clock_source(source) |
| 157 |
return True |
| 158 |
elif op == 'get': |
| 159 |
print(unit.get_clock_source()) |
| 160 |
return True |
| 161 |
print('Arguments for clock-source command:') |
| 162 |
print(' clock-source OP [SRC]') |
| 163 |
print(' OP: [{0}]'.format('|'.join(OPS))) |
| 164 |
print(' SRC: [{0}] if OP=set'.format('|'.join(sources))) |
| 165 |
print(' Packet streaming should be stopped.') |
| 166 |
return False |
| 167 |
|
| 168 |
|
| 169 |
cmds = { |
| 170 |
'mixer-input': handle_mixer_input, |
| 171 |
'output-params': handle_output_params, |
| 172 |
'output-source': handle_output_source, |
| 173 |
'rate-mode': handle_rate_mode, |
| 174 |
'sampling-rate': handle_sampling_rate, |
| 175 |
'clock-source': handle_clock_source, |
| 176 |
} |
| 177 |
|
| 178 |
fullpath = CliKit.seek_snd_unit_path() |
| 179 |
if fullpath: |
| 180 |
with FocusriteSaffireproIoUnit(fullpath) as unit: |
| 181 |
CliKit.dispatch_command(unit, cmds) |