|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +"""Parses the .tplg file argument and returns a list of volume |
| 4 | +kcontrols, one per line. |
| 5 | +
|
| 6 | +Pro tip: try using these commands _interactively_ with ipython3 |
| 7 | +""" |
| 8 | + |
| 9 | +# Keep this script short and simple. If you want to get something else |
| 10 | +# from .tplg files, create another script. |
| 11 | + |
| 12 | +import sys |
| 13 | +from tplgtool2 import TplgBinaryFormat, TplgType, DapmType, SofVendorToken |
| 14 | + |
| 15 | +TPLG_FORMAT = TplgBinaryFormat() |
| 16 | + |
| 17 | + |
| 18 | +def main(): |
| 19 | + "Main" |
| 20 | + |
| 21 | + parsed_tplg = TPLG_FORMAT.parse_file(sys.argv[1]) |
| 22 | + |
| 23 | + # pylint: disable=invalid-name |
| 24 | + DAPMs = [ |
| 25 | + item for item in parsed_tplg if item.header.type == TplgType.DAPM_WIDGET.name |
| 26 | + ] |
| 27 | + |
| 28 | + for dapm in DAPMs: |
| 29 | + gain_blocks = [b for b in dapm.blocks if b.widget.id == DapmType.PGA.name] |
| 30 | + |
| 31 | + for gb in gain_blocks: |
| 32 | + # debug |
| 33 | + # print(f"{gb.widget.id}: {gb.widget.name}") |
| 34 | + print_volume_kcontrols(gb) |
| 35 | + |
| 36 | + |
| 37 | +def print_volume_kcontrols(gain_block): |
| 38 | + "Print volume kcontrols" |
| 39 | + |
| 40 | + # Either 1 volume kcontrol, or 1 volume + 1 switch |
| 41 | + assert gain_block.widget.num_kcontrols in (1, 2) |
| 42 | + |
| 43 | + # A switch is either a DapmType.SWITCH, or DapmType.MIXER |
| 44 | + # with a max "volume" = 1. Don't include switches here. |
| 45 | + volume_kcontrols = [ |
| 46 | + kc |
| 47 | + for kc in gain_block.kcontrols |
| 48 | + if kc.hdr.type == DapmType.MIXER.name and kc.body.max != 1 |
| 49 | + ] |
| 50 | + |
| 51 | + assert len(volume_kcontrols) == 1 |
| 52 | + |
| 53 | + wname_prefix = ( |
| 54 | + f"{gain_block.widget.name} " if has_wname_prefix(gain_block.widget) else "" |
| 55 | + ) |
| 56 | + |
| 57 | + for vkc in volume_kcontrols: |
| 58 | + print(wname_prefix + vkc.hdr.name) |
| 59 | + |
| 60 | + |
| 61 | +# This could probably be moved to tplgtool2.py? |
| 62 | +def has_wname_prefix(widget): |
| 63 | + """Is the kcontrol name prefixed with the widget name? ("PGAxx" or "Dmicxx") |
| 64 | + Check SOF_TKN_COMP_NO_WNAME_IN_KCONTROL_NAME""" |
| 65 | + |
| 66 | + wname_elems = [ |
| 67 | + prv.elems |
| 68 | + for prv in widget.priv |
| 69 | + if prv.elems[0].token |
| 70 | + == SofVendorToken.SOF_TKN_COMP_NO_WNAME_IN_KCONTROL_NAME.name |
| 71 | + ] |
| 72 | + |
| 73 | + if len(wname_elems) == 0: # typically: topo v1 |
| 74 | + no_wname_prefix = 0 |
| 75 | + elif len(wname_elems) == 1: # typically: topo v2 |
| 76 | + assert len(wname_elems[0]) == 1 |
| 77 | + no_wname_prefix = wname_elems[0][0].value |
| 78 | + else: |
| 79 | + assert False, f"Unexpected len of wname_elems={wname_elems}" |
| 80 | + |
| 81 | + assert no_wname_prefix in (0, 1) |
| 82 | + |
| 83 | + # Double-negation: "no_wname false" => prefix |
| 84 | + return not no_wname_prefix |
| 85 | + |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + main() |
0 commit comments