Skip to contentSkip to conversation

How to get CardId for pseudo soundcards like dmix and dsnoop

DISCUSSION Gregor Jasny 2 participants 2 messages Jun 10, 2005

lore
Share & tools
lore
#0 lore raw

Hi,

In my application the user can specify the pcm devices as a string (i.e. hw:0,0, plughw:1, dmix, default).

Now I want to open the corresponding mixer device. At the moment I fetch the cardid via snd_pcm_info_get_card. Then I build a string hw:<cardid> and attach to that string. This works fine for hw and plughw devices. Doing this for dmix or dsnoop devices fails because the cardid is -1.

Another way I tried is to attach to the user supplied pcm string directly. But this does not work for dmix and dsnoop.

How do I get the right mixer for a opened pcm device?

Thank you for answering.

Gregor

PS: I've attached a small test case

#include <stdio.h>
#include <stdlib.h>

#include <alsa/asoundlib.h>

int main (int argc, char *argv[])

{
	snd_pcm_t *pcm;
	snd_mixer_t *mixer;
	int err;
	int incoming = 1;
	snd_pcm_info_t *pcm_info;
	int cardid;
	char dev[64];

	if (argc < 2)
		exit (EXIT_FAILURE);
	
	if ((err = snd_pcm_open (&pcm,
	                         argv[1],
	                         (incoming?SND_PCM_STREAM_CAPTURE:SND_PCM_STREAM_PLAYBACK),
	                         SND_PCM_NONBLOCK)) < 0) {
		fprintf (stderr, "Can not determine card id: %s (%s)\n", argv[1], snd_strerror (err));
		return EXIT_FAILURE;
	}

	snd_pcm_info_alloca(&pcm_info);
	if ((err = snd_pcm_info( pcm, pcm_info )) < 0) {
        	fprintf (stderr, "Can not get pcm info: %s (%s)\n", argv[1], snd_strerror (err));
		snd_pcm_close(pcm);
		return EXIT_FAILURE;
	}

	cardid = snd_pcm_info_get_card( pcm_info );

	printf ("CardId: %i\n", cardid);

	sprintf(dev, "hw:%i", cardid); /* Option 1 */

// strcpy (dev, argv[1]); /* Option 2 */

	if ((err = snd_mixer_open(&mixer, 0)) < 0) {
		fprintf (stderr, "snd_open_mixer(): %s\n", snd_strerror(-err));
		return EXIT_FAILURE;
	}
	if ((err = snd_mixer_attach(mixer, dev)) < 0) {
		fprintf (stderr, "snd_mixer_attach(%s): %s\n", dev, snd_strerror(-err));
		return EXIT_FAILURE;
	}
	printf("Attached to mixer: %s\n", dev);
	
	return EXIT_SUCCESS;
}
#1 lore raw
Quoted 4 lines · Gregor Jasny

In my application the user can specify the pcm devices as a string (i.e. hw:0,0, plughw:1, dmix, default).

Now I want to open the corresponding mixer device.

What do you mean with "corresponding" when there is no, or more than one, mixer device?

Quoted 4 lines

At the moment I fetch the cardid via snd_pcm_info_get_card. Then I build a string hw:<cardid> and attach to that string. This works fine for hw and plughw devices. Doing this for dmix or dsnoop devices fails because the cardid is -1.

Or for virtual software-only devices, or devices composed of several hardware devices.

Quoted 2 lines

Another way I tried is to attach to the user supplied pcm string directly.

This is what Jack does, but it may fail if there isn't a control device with the same name.

Quoted 1 line

How do I get the right mixer for a opened pcm device?

Hope that the user has a ctl.right definition in his .asoundrc. ;-)

Using the card id is a good heuristic, but your program shouldn't complain if that fails. It may be better to optionally allow the user to specify a control device name, too.

HTH
Clemens

-------------------------------------------------------
This SF.Net email is sponsored by: NEC IT Guy Games. How far can you shotput a projector? How fast can you ride your desk chair down the office luge track? If you want to score the big prize, get to know the little guy. Play to win an NEC 61" plasma display: http://www.necitguy.com/?r=20