From a7f99905736f495774265079fa45e1af90fe4b5f Mon Sep 17 00:00:00 2001 From: Fred Oh Date: Wed, 10 Apr 2024 15:23:53 -0700 Subject: [PATCH] sof-tplgreader: parse rates from topology file Related to sampling rate in caps, there are rates, rate_min and rate_max. Added support for the rates list from the topology file and use first in the list as supported rate. The sampling rate 48000 is intentionally placed first in the dictionary to ensure it appears first in the list. Signed-off-by: Fred Oh --- README.md | 3 ++- tools/sof-tplgreader.py | 14 +++++++++++++- tools/tplgtool.py | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f368d76a..f7818b7a 100644 --- a/README.md +++ b/README.md @@ -148,7 +148,8 @@ apl
Dumps info from tplg binary file. * tplgtool.py -
Dumps info from tplg binary file. (deprecated) +
Dumps info from tplg binary file. sof-tplgreader.py still use this but new features + will go to tplgtool2.py. Will be deprecated. ## System configuration tips diff --git a/tools/sof-tplgreader.py b/tools/sof-tplgreader.py index cb45cd73..4d919d14 100755 --- a/tools/sof-tplgreader.py +++ b/tools/sof-tplgreader.py @@ -63,11 +63,13 @@ def loadFile(self, filename, sofcard=0): # supported formats of playback pipeline in formats[0] # supported formats of capture pipeline in formats[1] formats = TplgFormatter.get_pcm_fmt(pcm) + rates = TplgFormatter.get_pcm_rate_list(pcm) # if capture is present, pcm['capture'] = 1, otherwise, pcm['capture'] = 0, # same thing for pcm['playback'] pipeline_dict['fmts'] = " ".join(formats[pcm['capture']]) # use the first supported format for test pipeline_dict['fmt'] = pipeline_dict['fmts'].split(' ')[0] + pipeline_dict['rates'] = " ".join(rates[pcm['capture']]) pipeline_dict['rate_min'], pipeline_dict['rate_max'] = self._key2str(cap, 'rate') pipeline_dict['ch_min'], pipeline_dict['ch_max'] = self._key2str(cap, 'channels') # for pcm with both playback and capture capabilities, we can extract two pipelines. @@ -95,7 +97,17 @@ def loadFile(self, filename, sofcard=0): # format pipeline, this change for script direct access 'rate' 'channel' 'dev' 'snd' for pipeline in self._pipeline_lst: #pipeline['fmt']=pipeline['fmt'].upper().replace('LE', '_LE') - pipeline['rate'] = pipeline['rate_min'] if int(pipeline['rate_min']) != 0 else pipeline['rate_max'] + # If rates is available get first sampling rate from the list. + # If rates list doesn't have any, then get it from rate_min or rate_max. + # If none of them is available, then set rate to 0 + if rates[0] != 0: + pipeline['rate'] = pipeline['rates'].split(' ')[0] + elif int(pipeline['rate_min']) != 0: + pipeline['rate'] = pipeline['rate_min'] + elif int(pipeline['rate_max']) != 0: + pipeline['rate'] = pipeline['rate_max'] + else: + pipeline['rate'] = 0 pipeline['channel'] = pipeline['ch_min'] pipeline['dev'] = "hw:" + str(sofcard) + ',' + pipeline['id'] # the character devices for PCM under /dev/snd take the form of diff --git a/tools/tplgtool.py b/tools/tplgtool.py index 2e7873ef..25ed687f 100755 --- a/tools/tplgtool.py +++ b/tools/tplgtool.py @@ -607,6 +607,31 @@ def _to_fmt_string(fmt): fmts.append("FLOAT") return fmts + + # transform number denoted pipeline sampling rates to string + # TODO: remove repeated if conditions + @staticmethod + def _to_rate_string(rate): + rates = [] + # Note: intentionally put 48000 first to make first in the list + if rate & (1 << 7) != 0: + rates.append("48000") + if rate & (1 << 1) != 0: + rates.append("8000") + if rate & (1 << 3) != 0: + rates.append("16000") + if rate & (1 << 4) != 0: + rates.append("22050") + if rate & (1 << 5) != 0: + rates.append("32000") + if rate & (1 << 6) != 0: + rates.append("44100") + if rate & (1 << 8) != 0: + rates.append("64000") + if rate & (1 << 9) != 0: + rates.append("96000") + return rates + # always return a list, playback stream fmt in fmt_list[0] # capture stream fmt in fmt_list[1], the format of absense # stream is UNKNOWN @@ -618,6 +643,17 @@ def get_pcm_fmt(pcm): fmt_list.append(TplgFormatter._to_fmt_string(cap["formats"])) return fmt_list + # always return a list, pcm rates in the list in ascending order + @staticmethod + def get_pcm_rate_list(pcm): + #print("Fred: get_pcm_rates") + rate_list = [] + caps = pcm["caps"] + #print(caps) + for cap in caps: + rate_list.append(TplgFormatter._to_rate_string(cap["rates"])) + return rate_list + @staticmethod def get_pcm_type(item): if item["playback"] == 1 and item["capture"] == 1: