0

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.)

JellicleCat
  • 28,480
  • 24
  • 109
  • 162
  • 1
    Obviously `pcm_open` failed. For a reason, print `dev->error`. – user58697 Oct 24 '16 at 22:01
  • Thanks @user58697 . The message is `cannot set hw params: Invalid argument`. If I were using regular alsa, I'd use the 'plugin' layer, but that appears not to be an option here, so I still don't know what I need to change. – JellicleCat Oct 25 '16 at 01:04

2 Answers2

3

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

tay10r
  • 4,234
  • 2
  • 24
  • 44
  • Thanks. I wish I recalled what was going on here. I do have tinyalsa working now. – JellicleCat Jan 17 '17 at 17:02
  • 1
    @JellicleCat, the problem in your code I saw from pastebin is your period size and period count. If I recall correctly, we have a program called tinypcminfo that helps determine what the valid parameters are. Usually 1024 it's okay for period size, and 4 is good enough for period count. But that depends on what you're doing with the audio. – tay10r Jul 12 '17 at 23:36
  • is there any mechanism tinyalsa provide to dump the raw data into a file? like alsa-lib provides file plugin to dump the data into a file. – Vishal Gupta May 29 '20 at 12:09
  • @VishalGupta, yes. You can use `tinycap` and use the `--` option to record raw samples to the standard output. Then just redirect the standard output to the file you want to put the samples into. For example, `tinycap -t 3 -- >audio.raw`. – tay10r May 30 '20 at 02:51
  • @tay10r sorry my question might be not clear. I looking for something similar like "file plugin" of alsa. at the time when I am doing tinyplay then tinyplay will dump the data into a file as well when it writes the data to PCM. one way I found by modifying the pcm.c file. but with alsa lib we can do this by creating .asoundrc and creating a file plugin so when we do aplay it also dumps the data into file when it writes to PCM device. – Vishal Gupta Jun 02 '20 at 16:46
  • @VishalGupta currently that kind of plugin system isn't supported. There's only so much you can add to a library and still consider it minimalistic. If you have any other questions or want to continue the discussion, please create an issue at https://github.com/tinyalsa/tinyalsa – tay10r Jun 03 '20 at 15:03
0

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.

user58697
  • 7,808
  • 1
  • 14
  • 28