My question has to do with the libasound function named "snd_pcm_hw_params" in connection with code to play a sound file. I am new to ALSA programming. Using a coding example I found on the internet, I wrote a small program to play a 7 second .wav file to the default sound card. When I run this code several times in a row, occasionally (but not always) the requisite call to snd_pcm_hw_params to write the previously filled in snd_hw_params_t struct to the driver, I get back an error code of -2 (ENOENT). I have no idea what this means, nor how to handle nor prevent it. My code just emits an error message and bails. Usually, if I run it again, the code runs fine. Its fine for my use, but eventually, this code is supposed to be given to a non-programmer to use, and I'd like to either prevent the error, or resolve it internally without involving said non-programming user. I note hear that the user is supposed to be able to cause an early abort of the program by clicking a button, and when this happens, my code calls snd_pcm_drop, followed by snd_pcm_close. If the program runs to completion, and plays all 7 seconds of the wav file, then it finishes up by calling snd_pcm_drain, followed by snd_pcm_close. Any help or suggestions would be greatly appreciated. :)
Asked
Active
Viewed 260 times
0
-
as an additional comment -- I have discovered that, after the -ENOENT error occurs, if I rerun the program, even if it is just a fraction of a second later, that the same code completes without error. I hypothesize that the snd_pcm_stop() function somehow takes the default soundcard device "offline' or "inaccessible" for a short time interval. I am trying to find some way to prevent or recover from this. Does anyone have a clear idea of what snd_pcm_reset() actually does? – Dennis Newbold Feb 02 '20 at 17:51
-
Also, FYI - I changes the code so that if it gets -ENOENT, it will sleep for about 10 msec. and then issue the same call again. This second call return an "invalid argument" error. I don't know why. Thx. – Dennis Newbold Feb 02 '20 at 17:51
-
To all: I still do not understand the cause of the error. I fixed it by simulating it observed behavior thus: for (int i = 0 ; i < 10 ; i++ ) { open default sound device ; configure sound parameters; rc = snd_pcm_hw_params( ... ) ; if ( rc == -ENOENT) { close default sound device ; sleep for 25 msec. } else break; } and only taking an error exit if the problem persisted after ten attempts. FYI, in case it matters, the default sound card is: Intel [hda INTEL], device 0: 92HD71B7X Analog (per aplay --list-devices) – Dennis Newbold Feb 07 '20 at 01:12
-
We can't help you to fix your code if we don't see it. – alpereira7 Oct 09 '20 at 07:55