tinyalsa: pcm_start yields bad file number error

1.3k Views Asked by At

I'm attempting a barebones program to use tinyalsa, but pcm_start always fails, returning -1 and setting errno to 9 (EBADF, i.e. bad file number). The call to pcm_open before this returns a non-null pointer, but it sets errno to 22.

There appears to be no documentation for tinyalsa, so I'm having trouble understanding what I'm supposed to do. I based my program on an example from alsa (not tinyalsa), and I've read the header files for tinyalsa. Can anyone provide any guidance?

pcm * dev = pcm_open(1, 0, PCM_OUT, &config);
if (err = pcm_start(dev)) printf("err: %d\t errno: %d\n", err, errno);

(Full code available on pastebin.)

I infer the values for the first two arguments of pcm_open from aplay --list-devices, which outputs:

**** List of PLAYBACK Hardware Devices ****
card 1: PCH [HDA Intel PCH], device 0: ALC3232 Analog [ALC3232 Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

(I am compiling and running this on my workstation, not on an Android.)

2

There are 2 best solutions below

6
On

I'm one of the maintainers for the TinyALSA project.

You should error check that code of yours.

This is how you would correctly check for a failure with the PCM structure:

dev = pcm_open(1, 0, PCM_OUT, &config);
if (dev == NULL) {
    /* memory allocation failure */
} else if (!pcm_is_ready(pcm)){
    printf("error: pcm_open: %s\n", pcm_get_error(pcm));
}

And that should tell you why TinyALSA could not open your device.

If that doesn't help debug your problem, submit an issue on GitHub and include a link to the code, the error message after pcm_open, and a list of the directory contents in /dev/snd.

Also, the documentation on the API is definitely a work in progress. I included the bit about error checking pcm_open in the master branch. If you need further clarification on something, please create an issue for it!

Thanks

0
On

Now looking at pcm.c, the cannot set hw params message comes from line 865,

    if (ioctl(pcm->fd, SNDRV_PCM_IOCTL_HW_PARAMS, &params)) {

params are initialized from config. At this place I have no say. My best recommendation is to step pcm_open in a debugger and see what it thinks about params.