ALSA: open a PCM device in shared mode

12,362

Solution 1

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.

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

Solution 3

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.

Solution 4

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

Share:
12,362
MD Sayem Ahmed
Author by

MD Sayem Ahmed

I have a Bachelor degree in Computer Science and Engineering. I usually work with Java, Spring, and Distributed Systems in my day-to-day work. There are a few things that I like, including, but not limited to, Java, Spring, Domain Driven Design, Distributed Systems, TDD, and CI/CD. I am currently working as a Senior Software Engineer at eBay Kleinanzeigen. You can check out my blog or LinkedIn if you want to connect.

Updated on July 20, 2022

Comments

  • MD Sayem Ahmed
    MD Sayem Ahmed almost 2 years

    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?

  • MD Sayem Ahmed
    MD Sayem Ahmed over 12 years
    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.
  • 16851556
    16851556 almost 3 years
    Thanks, switching from ALSA to PulseAudio in my media player settings worked!