Search code examples
clinuxpcmalsa

ALSA configuration to enable device sharing between multiple processes


I have the following code in the .asoundrc file in my home directory -

pcm.lol
{
    type dmix
    ipc_key 1024
    ipc_key_add_uid true
    slave{
        pcm "hw:0,0"
        period_time 125000
        rate 48000
        format S32_LE
        channels 2
    }
}

When I run this sample example it works fine (I changed the device name to plug:lol). I can hear a sound playing in my headphones. The problem occurs when two or more processes try to output audio data at the same time. In this case, the later process cannot output its audio data properly.

For example, I ran a media player which was playing a song and then I tried running that example. I couldn't hear any sound from it, all I could hear was the music playing in the media player. Similar thing happens when I run this example first and then the media player, in which case I only listen audio output from my example.

How can I solve this problem? Is there any error with my pcm device configuration? Or do I have to provide any additional PCM configuration parameters in the example code ?

Also, it would have been really nice if I could create this logical device configuration programmatically rather than using a configuration file like this.


Solution

  • As far as I have searched and checked, there is apparently no concrete way to do this in ALSA. I can share the built-in default device between multiple processes, but any other configuration didn't provide this functionality. Also, I couldn't find any programmatic way to configure an ALSA device. The configuration file seems to be the only way to do this.