Skip to content

Commit

Permalink
capture-test: Add buffer size control & verbose logging
Browse files Browse the repository at this point in the history
Recent SOF drivers have started populating the buffer size of ALSA
hw_params structs with values that produce an -EINVAL if you try to
set them (unclear to me if this is a bug or not).

Regardless, the capture test wants control over that, so add it.  Also
hook the ALSA hw_params dump code (for debugging issues like this) and
hide it under a "--verbose" flag.

Signed-off-by: Andy Ross <[email protected]>
  • Loading branch information
andyross committed Jun 20, 2024
1 parent 6571dcf commit dc50179
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions tools/capture-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,17 @@
"""

def parse_opts():
global opts
global opts # pylint: disable=global-statement
ap = argparse.ArgumentParser(description=HELP_TEXT,
formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("-v", "--verbose", action="store_true", help="Verbose output")
ap.add_argument("--disable-rtnr", action="store_true", help="Disable RTNR noise reduction")
ap.add_argument("-c", "--card", type=int, default=0, help="ALSA card index")
ap.add_argument("--pcm", type=int, default=16, help="Output ALSA PCM index")
ap.add_argument("--cap", type=int, default=18, help="Capture ALSA PCM index")
ap.add_argument("--rate", type=int, default=48000, help="Sample rate")
ap.add_argument("--chan", type=int, default=2, help="Output channel count")
ap.add_argument("--bufsz", type=int, default=2048, help="Buffer size in frames")
ap.add_argument("--capchan", type=int,
help="Capture channel count (if different from output)")
ap.add_argument("--capbits", type=int, default=16, help="Capture sample bits (16 or 32)")
Expand Down Expand Up @@ -103,6 +105,7 @@ def err_wrap(ret):
return ret
def alloc(self, typ):
return (C.c_byte * getattr(self.lib, f"snd_{typ}_sizeof")())()
# pylint: disable=too-few-public-methods
class pcm_channel_area_t(C.Structure):
_fields_ = [("addr", C.c_ulong), ("first", C.c_int), ("step", C.c_int)]

Expand All @@ -111,8 +114,18 @@ def pcm_init_stream(pcm, rate, chans, fmt, access):
alsa.snd_pcm_hw_params_any(pcm, hwp)
alsa.snd_pcm_hw_params_set_format(pcm, hwp, fmt)
alsa.snd_pcm_hw_params_set_channels(pcm, hwp, chans)
alsa.snd_pcm_hw_params_set_rate(pcm, hwp, rate, alsa.PCM_STREAM_PLAYBACK)
alsa.snd_pcm_hw_params_set_rate(pcm, hwp, rate, 0)
alsa.snd_pcm_hw_params_set_access(pcm, hwp, access)
alsa.snd_pcm_hw_params_set_buffer_size(pcm, hwp, opts.bufsz)
if opts.verbose:
print(f"Set hw_params:")
out = C.c_ulong(0)
alsa.snd_output_buffer_open(C.byref(out))
alsa.snd_pcm_hw_params_dump(hwp, out)
buf = C.c_ulong(0)
alsa.snd_output_buffer_string(out, C.byref(buf))
print(C.string_at(buf.value).decode("ascii"))

alsa.snd_pcm_hw_params(pcm, hwp)

def ctl_disable_rtnr():
Expand Down Expand Up @@ -352,7 +365,7 @@ def echo_test():
# Just slurps in the wav file and chops off the header, assuming
# the user got the format and sampling rate correct.
WAV_HDR_LEN = 44
buf = open(opts.noise, "rb").read()[WAV_HDR_LEN:]
buf = open(opts.noise, "rb").read()[WAV_HDR_LEN:] # pylint: disable=consider-using-with

(rfd, wfd) = os.pipe()
pid = os.fork()
Expand Down

0 comments on commit dc50179

Please sign in to comment.