Opening a PCM device in shared mode
Hello all,
I am a beginner in ALSA API. I am developing a sound application where I need to open a PCM device for recording/playback audio data. I want to open this device in shared mode so that other applications can use the same device. I have downloaded the example ALSA program from here
<http://www.alsa-project.org/alsa-doc/alsa-lib/_2test_2pcm_8c-example.html>
and run it on my PC. It works fine when it runs alone. But when I start a song on a media player and then run this example, it shows an error saying -
|Playback open error: Device or resource busy
|
Looking at the source code of this example I could say that thesnd_pcm_open
<http://www.alsa-project.org/alsa-doc/alsa-lib/group___p_c_m.html#gb7eaaec6f27e4ca48cada9795cd2479b>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?
P.S. : I have two sound cards on my system, one is built-in in motherboard, another one is a USB Card. I tried opening the USB (by specifying "plughw:1,0" as argument in the snd_pcm_open) card and then got this error.
Thank you in advance.
Regards,
Sayem Ahmed
A message that is not in the archive (sent off-list or not yet received): C516192DA6D2B74F8192CDC209D958FD5834F34861@dr-mail
Hi Anders,
Thanks for your reply. I have created a /*.asoundrc*/ file in my home directory as you suggested and added the following configuration there (as you can see, these are basically the exact default configuration that the dmix plugin has) -
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
}
}Now when I start my program while a media player is running, it doesn't show any /*Device or Resource busy */error. It successfully opens the device and starts to write the sine wave into it. But the problem is I can't hear its output. I should have heard two sounds simultaneously but I can only hear the song in the media player, not the output sound from the example program. Similar things happens when I start the example first and the start a media player, I can only hear sounds from the first one.
Also, when I try to run the example program on different rate (i.e., rate 8000, channel 1) it throws an error saying format doesn't match. I guess it's because of the configuration rate that I specified in the file. I tried to supply multiple values for rate using array notations ( rate [8000, 48000] ) but it threw an error telling that the /*.asoundrc */file is corrupted or old.
Actually the whole point of my exploration is this - I want to display a list of available audio cards/devices in my system to my application user who will then select the input and output audio card to record/playback audio data accordingly.
Thank you.
Regards,
Sayem Ahmed
Quoted 25 lines · Anders Gnistrup
Hi Sayem
It's because you open the device plughw:0,0.
It's actually the device hw:0,0 with a additional "plug" pluggin. The plug pluggin takes care of resampling. Devices is not mixing device by design. This is handled by plugins (the dmix plugin)What you want is a device that can mix (and possible resample). For that you need a new virtual device. I have had the same problem myself, and the solution I have used is a .asoundrc configuration.
pcm.mydmix
{ type dmix ipc_key 1024 slave { pcm "hw:0,0" period_time 0 period_size 1024 buffer_size 8192 rate 44100 format "S32_LE" } }Instead of the slave definition you could make your own. Now the device mydmix can be used as a mixing device.
Quoted 2 lines · Sayem Ahmed
Also, when I try to run the example program on different rate (i.e., rate 8000, channel 1) it throws an error saying format doesn't match.
The "default" device for your hardware should have the plug plugin on top of the dmix plugin.
Quoted 5 lines
Actually the whole point of my exploration is this - I want to display a
list of available audio cards/devices in my system to my application user who will then select the input and output audio card to record/playback audio data accordingly.
This is the job of the desktop environment.
Is there a reason why your application needs to
duplicate this?
Regards,
Clemens
Quoted 2 lines · Clemens Ladisch
Is there a reason why your application needs to
duplicate this?
Yes. Clients would like to install multiple sound cards into their system so that they can perform multiple VoIP calls at the same time. For each call a different sound card will be used. This is why I need this functionality in my application.
Quoted 7 lines · Sayem Ahmed
Quoted 2 lines · Clemens Ladisch
Is there a reason why your application needs to
duplicate this?Yes. Clients would like to install multiple sound cards into their system so that they can perform multiple VoIP calls at the same time.
Use snd_card_next() to enumerate cards.
To open the default device of a specific card, try device name "default:CARD=x", but this won't work if the user has redefined the "default" device definition.
Regards,
Clemens