Skip to content

Commit dc50179

Browse files
committed
capture-test: Add buffer size control & verbose logging
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 <andyross@google.com>
1 parent 6571dcf commit dc50179

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

tools/capture-test.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,17 @@
4646
"""
4747

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

@@ -111,8 +114,18 @@ def pcm_init_stream(pcm, rate, chans, fmt, access):
111114
alsa.snd_pcm_hw_params_any(pcm, hwp)
112115
alsa.snd_pcm_hw_params_set_format(pcm, hwp, fmt)
113116
alsa.snd_pcm_hw_params_set_channels(pcm, hwp, chans)
114-
alsa.snd_pcm_hw_params_set_rate(pcm, hwp, rate, alsa.PCM_STREAM_PLAYBACK)
117+
alsa.snd_pcm_hw_params_set_rate(pcm, hwp, rate, 0)
115118
alsa.snd_pcm_hw_params_set_access(pcm, hwp, access)
119+
alsa.snd_pcm_hw_params_set_buffer_size(pcm, hwp, opts.bufsz)
120+
if opts.verbose:
121+
print(f"Set hw_params:")
122+
out = C.c_ulong(0)
123+
alsa.snd_output_buffer_open(C.byref(out))
124+
alsa.snd_pcm_hw_params_dump(hwp, out)
125+
buf = C.c_ulong(0)
126+
alsa.snd_output_buffer_string(out, C.byref(buf))
127+
print(C.string_at(buf.value).decode("ascii"))
128+
116129
alsa.snd_pcm_hw_params(pcm, hwp)
117130

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

357370
(rfd, wfd) = os.pipe()
358371
pid = os.fork()

0 commit comments

Comments
 (0)