[alsa-devel] plughw behaviour not consistent with hw
Hello list,
I'm working on an application that requires a period time of 10ms. The hardware at the other end of the bus is a ulaw codec running at 8kHz. The application runs at 16kHz S16_LE and I was hoping to use the plughw device to do sample rate conversion and ulaw encode/decode. This way the application doesn't need to change to accommodate this ulaw codec. However, it's not working and I don't know how to go about debugging. I'm hoping I can get some pointers here.
The problem is best illustrated by this output, first using plughw: alsa_read: 10 alsa_read: 20 alsa_read: 0 alsa_read: 20 alsa_read: 0 alsa_read: 20 alsa_read: 0 alsa_read: 20 alsa_read: 0 alsa_read: 20
It would appear that plughw gives the correct period time on the first call, but then subsequently buffers 2 periods before returning. Using hw with matching sample rate, sample format, and number of samples: alsa_read: 10 alsa_read: 10 alsa_read: 10 alsa_read: 10 alsa_read: 10 alsa_r ead: 10 alsa_read: 10 alsa_read: 10 alsa_read: 10 alsa_read: 10
This code snippet generated the above output:
for (i = 0; i < 10; ++i) { gettimeofday(&time1, NULL); err = snd_pcm_readi (capture_handle, buf, NSMP); if (err != NSMP) { printf ("read from audio interface failed " " (%s)\n", snd_strerror (err)); exit (1); } gettimeofday(&time2, NULL); printf("alsa_read: %d\n", getTimeDiff(time1, time2)); }
I have tried using the patch from here (without the environment variable switch):
http://mailman.alsa-project.org/pipermail/alsa-devel/2008-October/01170 3.html
to print out the configuration of plughw's plugin chain, but it generates no output. I've verified the md5sum of the library after compilation and it matches the one I compiled. And I ran ldconfig after replacing libasound.so. So I don't know what's going on there. I definitely call snd_pcm_hw_params() when setting up the capture device. This is using alsa-lib-1.0.25.
Does anyone know why plughw might be acting the way it is and how I can get it to deliver 10ms periods? Equally appreciated would be any tips on how to debug this problem.
Thanks, Stef
Hi,
On 2018/11/01 13:44, Stefano Antonelli wrote:
Does anyone know why plughw might be acting the way it is and how I can get it to deliver 10ms periods? Equally appreciated would be any tips on how to debug this problem.
The 'plughw' PCM node of alsa-lib configuration space is assigned to 'plug' PCM plugin[1]. This PCM plugin can aggregate several PCM plugins to perform resampling. For example, in a below case, two PCM plugins (rate/route conversion PCM plugins) are aggregated by 'plug' PCM plugin in front of 'hw' PCM plugin which direct accesses to ALSA PCM chracter device.
$ LC_ALL=C arecord -v -Dplughw:1,0 /dev/null -fS16_LE Recording WAVE '/dev/null' : Signed 16 bit Little Endian, Rate 8000 Hz, Mono Plug PCM: Rate conversion PCM (48000, sformat=S16_LE) Converter: libspeex (external) Protocol version: 10002 Its setup is: stream : CAPTURE access : RW_INTERLEAVED format : S16_LE ... Slave: Route conversion PCM (sformat=S16_LE) Transformation table: 0 <- 0*0.5 + 1*0.5 Its setup is: stream : CAPTURE access : MMAP_INTERLEAVED format : S16_LE ... Slave: Hardware PCM card 1 'HDA Intel PCH' device 0 subdevice 0 Its setup is: stream : CAPTURE access : MMAP_INTERLEAVED format : S16_LE ...
Well, for capture direction, the most of PCM plugins in alsa-lib/alsa-utils are programmed to do resampling in .avail_update() callback of plugin framework in alsa-lib. The number of available PCM frames is calculated in this timing. On the other hand, poll(2) system call is executed against ALSA PCM character device. Even if poll(2) returns to notify available PCM frames in the device, actual number of available frames is a summary of resampling done by the aggregated PCM plugins. As a result, you can see the behaviour because plugins possibly have buffers for count gap between in/out frames.
[1] http://git.alsa-project.org/?p=alsa-lib.git;a=blob;f=src/conf/alsa.conf;h=bb...
Regards
Takashi Sakamoto
participants (2)
-
Stefano Antonelli
-
Takashi Sakamoto