Skip to content

Commit

Permalink
sof-tplgreader: parse rates from topology file
Browse files Browse the repository at this point in the history
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 <fred.oh@linux.intel.com>
  • Loading branch information
fredoh9 committed Apr 15, 2024
1 parent 74bcaa5 commit a7f9990
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ apl
<br> Dumps info from tplg binary file.

* tplgtool.py
<br> Dumps info from tplg binary file. (deprecated)
<br> 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
Expand Down
14 changes: 13 additions & 1 deletion tools/sof-tplgreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions tools/tplgtool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down

0 comments on commit a7f9990

Please sign in to comment.