ALSA: open a PCM device in shared mode
Asked Answered
C

6

10

I want to playback some audio data using ALSA to a PCM device. As an example I have downloaded this sample example and run it in my PC. It works fine when no other process is currently using the sound card. But it doesn't play anything when some other process uses the audio device (i.e., media player playing songs) and shows following error -

Playback open error: Device or resource busy

Looking at the source code of this example I could say that the snd_pcm_open function at line 882 is throwing this error. It finds the device busy since another process is currently using it.

I also tried the reverse way - first started the example then tried to start a song. In this case the media player stays idle, showing "idle" just beside the progress bar (I am using Banshee). I am assuming that snd_pcm_open gains exclusive right of the device resource so that no other process can use it.

But I don't want that. I want to play sound to a audio device without requiring any exclusive rights so that other processes in the PC can share the same device for outputting audio data.

How can I do that? How can I open a PCM device so that other processes can also share the same device?

Corticate answered 17/11, 2011 at 14:25 Comment(0)
C
3

I couldn't find a way to share a device between multiple processes. I tried to use the dmix plugin to combine multiple playback streams using the .asoundrc configuration file but that didn't work for some reason. I tried to use the default device of my sound card but that didn't work too. According to a recommendation in ALSA mailing list I tried to open the default device using default:CARD=x as the device name parameter to the snd_pcm_open function which results in a device not found error in my system. Apparently there is no way to do this (if someone can find a way, please update this answer).

Rather than using ALSA, I used PulseAudio next which solved my problem.

Corticate answered 8/12, 2011 at 20:26 Comment(1)
Thanks, switching from ALSA to PulseAudio in my media player settings worked!Herschel
P
2

The trouble with ALSA back then was the absence of a mixer plugin inserted in default output. In recent versions of ALSA, this does not happen anymore as dmix plugin is now included in the output path by default.

(You can recreate the shared soundcard trouble for yourself if you mess with ~/.asoundrc, or if you bypass the mixer by telling application to use directly the hardware plug. Note that some applications, such as JACK will try to use the hardware directly if not told to do otherwise.)

With PulseAudio, you still use ALSA as backend, you just added a layer with a mixer (and a ton of other things you probably do not even want to know about). JACK would do exactly the same job for you.

Nowadays, ALSA's dmix plugin will do it for you, so you can get rid of middleware such as PulseAudios and JACKs until you really need the non-basic sound system features they provide.

For reference, look up some .asoundrc documentation.

Pharyngology answered 1/1, 2018 at 20:55 Comment(0)
G
1

Check the file /dev/sndstat (if you have the OSS compatibility layer enabled). It should list audio devices which correspond to the ALSA devices on your system. And also you can check /proc/asound/devices to get features of each device.

Try access to different playback devices (in your example it's "plughw:0,0" opened) by choosing different names for snd_pcm_open(): This is from here:

plughw:card,device. Both have as parameters the card (ID string or numerical index), device and optionally subdevice of the hardware to be accessed.

Godard answered 17/11, 2011 at 14:52 Comment(1)
No, I don't have OSS compatibility layer enabled, and I don't really want to rely on it. I also checked /proc/asound/devices and saw the device list there (I can fetch that list programmatically though). I can also access different playback devices. But none of these are really issue here. I wanted to know if I can open a single device in shared mode.Corticate
G
1

Maybe you could use Jack: http://jackaudio.org/

Galipot answered 17/11, 2011 at 14:56 Comment(0)
K
0

I had this problem recently with Audacious, and I managed to fix it with Audacious, so I thought I would share what I did in case it sheds any light on other people's problems.

The symptoms:

If I have firefox open with a youtube tab open (not even running), then when I try and play music in Audacious, I cannot. I get this error:

alsa snd_pcm_open device busy

I also got a number of other errors to do with setting PCM bitrates etc.

Audacious was configured to use:

PCM Device: hw:0,0 - Generic Analog 
Mixer Device: default - Default mixer dev 
Mixer Element: Master

I changed this to:

PCM Device: sysdefault: CARD=Generic - HD-Audio Generic, Generic Analog Default Audio Device
Mixer Device: hw: CARD=Generic - HD-Audio Generic Direct control device
Mixer Element: Master

And the errors disappeared. So I think that some of the sound card devices and access routes are exclusive to applications and some are not, so experiment with different options.

Kunming answered 17/5, 2021 at 8:42 Comment(0)
I
0

I came to this old question because these days Pulseaudio has the audio device. Solutions in some circumstances may be

  • padsp - a wrapper command to give the wrapped program a fake OSS device. This may help with some applications, or a sweep built with the OSS build option.
  • pasuspend - make Pulseaudio let go of the audio device for a while, so you can lend it to something else. This allows me to run the sweep sound editor using
    pasuspender sweep &
    
    Note that other applications will be blocked from using the sound device, but volume controls still work. Without this, sweep would start but fail to play or record with Error opening ALSA device plughw:0,0: Device or resource busy
Immunogenic answered 23/8, 2023 at 19:11 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Cathicathie

© 2022 - 2024 — McMap. All rights reserved.