Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 3436

Graphics, sound and multimedia • Trouble with ALSA on Pi5/PiOS5

$
0
0
I have some long-standing code that uses the ALSA PCM API to produce sound effects.
I've just built this code on a Pi5 with PiOS5 for the first time and it doesn't work....

Well....

It does produce the sound effect, but takes about 50% load to do it ! I have worked out the problem and can reproduce it with the example code given on the alsa web site,
https://www.alsa-project.org/alsa-doc/a ... ample.html

If you build this with

Code:

gcc -o test test.c -lasound -lm
and run it with

Code:

./test -m write_and_poll -D default 
and look at the cpu load with top you will see

Code:

  Tasks: 216 total,   2 running, 214 sleeping,   0 stopped,   0 zombie%Cpu(s):  3.9 us,  7.3 sy,  0.0 ni, 88.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st MiB Mem :   8045.7 total,   2497.7 free,   3621.7 used,   2469.9 buff/cache     MiB Swap:    100.0 total,    100.0 free,      0.0 used.   4424.0 avail Mem     PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND                        8223 petero    20   0  172960   6624   4864 R  42.2   0.1   0:02.98 test                           2019 petero    20   0  703680 162944 127328 S   1.0   2.0   4:21.40 wayfire             
The problem is in this function

Code:

/* *   Transfer method - write and wait for room in buffer using poll */static int wait_for_poll(snd_pcm_t *handle, struct pollfd *ufds, unsigned int count){    unsigned short revents;     while (1) {//ufds->events = POLLERR|POLLOUT;        poll(ufds, count, -1);        snd_pcm_poll_descriptors_revents(handle, ufds, count, &revents);        if (revents & POLLERR)            return -EIO;        if (revents & POLLOUT)            return 0;    }}
poll is returning immediatley but after calling snd_pcm_poll_descriptors_revents most of the time neither POLLERR or POLLOUT are set, so it effectivley busy loops until once in a while POLLOUT is set and the function returns.

The value in ufds->events is 0X29, whichi is not what I expected at all.
If I fix that and don't use snd_pcm_poll_descriptors_revents it works as expected..

Code:

static int wait_for_poll(snd_pcm_t *handle, struct pollfd *ufds, unsigned int count){    unsigned short revents;    int ret;    while (1) {ufds->events = POLLERR | POLLOUT;        ret = poll(ufds, count, -1);        //snd_pcm_poll_descriptors_revents(handle, ufds, count, &revents);revents = ufds->revents;        if (revents & POLLERR)            return -EIO;        if (revents & POLLOUT)            return 0;    }}
With this version the CPU load is 0.3% vs the original 42% !!!!
I note that

Code:

 cat /proc/asound/version Advanced Linux Sound Architecture Driver Version k6.1.0-rpi7-rpi-2712.
so I wonder if this is a regression due to pi specific mods ?

PeterO

Statistics: Posted by PeterO — Thu Feb 01, 2024 10:29 am — Replies 8 — Views 111



Viewing all articles
Browse latest Browse all 3436

Trending Articles