[alsa-devel] Retrieving the number of input channels on a sound card
Takashi Iwai
tiwai at suse.de
Thu Jun 9 08:27:58 CEST 2011
At Wed, 8 Jun 2011 16:20:10 -0300,
André Prado wrote:
>
> I Found it out.. hope it helps someone.
>
> snd_pcm_t *capture_handle;
> snd_pcm_hw_params_t *hw_params;
>
> snd_pcm_open( &capture_handle, str, SND_PCM_STREAM_PLAYBACK, 0 );
> snd_pcm_hw_params_malloc (&hw_params);
>
> snd_pcm_hw_params_any(capture_handle, hw_params);
>
> unsigned int num_canais = 0;
> int ret;
>
> ret = snd_pcm_hw_params_get_channels(hw_params, &num_canais);
>
> printf("Number of channels: %i\n",num_canais);
Well, this isn't always correct, unfortunately.
If the device supports multiple channels (many do it indeed), the
situation becomes complicated.
You can get the minimum and maximum values in the configuration space
even before determining to the single one, via
snd_pcm_hw_params_get_channels_{min|max}(). Or check whether the
given channels are supported via
snd_pcm_hw_params_test_channels().
Takashi
>
> 2011/6/8 André Prado <andreprado88 at gmail.com>:
> > Hello. I am new to Alsa programming and i want to get the number of
> > input channels on an audio device.
> > I am reading a lot of tutorials and documentations but i am struggling
> > to do this.
> >
> > Here is a code i've saw to Lists the hardware names of MIDI output
> > device/sub-devices. I've understood it. But i want to get the number
> > of input channels.
> >
> > Any help is appreciated. THanks
> >
> > // listmidi.c
> > // Lists the hardware names of MIDI output device/sub-devices
> > // upon each ALSA sound card in the system.
> > //
> > // Compile as:
> > // gcc -o listmidi listmidi.c -lasound
> >
> > #include <stdio.h>
> > #include <string.h>
> > #include <alsa/asoundlib.h>
> >
> > int main(int argc, char **argv)
> > {
> > register int err;
> > int cardNum;
> >
> > // Start with first card
> > cardNum = -1;
> >
> > for (;;)
> > {
> > snd_ctl_t *cardHandle;
> >
> > // Get next sound card's card number. When "cardNum" == -1, then ALSA
> > // fetches the first card
> > if ((err = snd_card_next(&cardNum)) < 0)
> > {
> > printf("Can't get the next card number: %s\n", snd_strerror(err));
> > break;
> > }
> >
> > // No more cards? ALSA sets "cardNum" to -1 if so
> > if (cardNum < 0) break;
> >
> > // Open this card's control interface. We specify only the card
> > number -- not
> > // any device nor sub-device too
> > {
> > char str[64];
> >
> > sprintf(str, "hw:%i", cardNum);
> > if ((err = snd_ctl_open(&cardHandle, str, 0)) < 0)
> > {
> > printf("Can't open card %i: %s\n", cardNum, snd_strerror(err));
> > continue;
> > }
> > }
> >
> > {
> > int devNum;
> >
> > // Start with the first MIDI device on this card
> > devNum = -1;
> >
> > for (;;)
> > {
> > snd_rawmidi_info_t *rawMidiInfo;
> > register int subDevCount, i;
> >
> > // Get the number of the next MIDI device on this card
> > if ((err = snd_ctl_rawmidi_next_device(cardHandle, &devNum)) < 0)
> > {
> > printf("Can't get next MIDI device number: %s\n",
> > snd_strerror(err));
> > break;
> > }
> >
> > // No more MIDI devices on this card? ALSA sets "devNum" to -1 if so.
> > // NOTE: It's possible that this sound card may have no MIDI
> > devices on it
> > // at all, for example if it's only a digital audio card
> > if (devNum < 0) break;
> >
> > // To get some info about the subdevices of this MIDI device
> > (on the card), we need a
> > // snd_rawmidi_info_t, so let's allocate one on the stack
> > snd_rawmidi_info_alloca(&rawMidiInfo);
> > memset(rawMidiInfo, 0, snd_rawmidi_info_sizeof());
> >
> > // Tell ALSA which device (number) we want info about
> > snd_rawmidi_info_set_device(rawMidiInfo, devNum);
> >
> > // Get info on the MIDI outs of this device
> > snd_rawmidi_info_set_stream(rawMidiInfo, SND_RAWMIDI_STREAM_OUTPUT);
> >
> > i = -1;
> > subDevCount = 1;
> >
> > // More subdevices?
> > while (++i < subDevCount)
> > {
> > // Tell ALSA to fill in our snd_rawmidi_info_t with info
> > on this subdevice
> > snd_rawmidi_info_set_subdevice(rawMidiInfo, i);
> > if ((err = snd_ctl_rawmidi_info(cardHandle, rawMidiInfo)) < 0)
> > {
> > printf("Can't get info for MIDI output subdevice
> > hw:%i,%i,%i: %s\n", cardNum, devNum, i, snd_strerror(err));
> > continue;
> > }
> >
> > // Print out how many subdevices (once only)
> > if (!i)
> > {
> > subDevCount = snd_rawmidi_info_get_subdevices_count(rawMidiInfo);
> > printf("\nFound %i MIDI output subdevices on card
> > %i\n", subDevCount, cardNum);
> > }
> >
> > // NOTE: If there's only one subdevice, then the subdevice
> > number is immaterial,
> > // and can be omitted when you specify the hardware name
> > printf((subDevCount > 1 ? " hw:%i,%i,%i\n" : "
> > hw:%i,%i\n"), cardNum, devNum, i);
> > }
> > }
> > }
> >
> > // Close the card's control interface after we're done with it
> > snd_ctl_close(cardHandle);
> > }
> >
> > snd_config_update_free_global();
> > }
> >
> > --
> > Att
> > André
> >
>
>
>
> --
> Att
> André
> _______________________________________________
> Alsa-devel mailing list
> Alsa-devel at alsa-project.org
> http://mailman.alsa-project.org/mailman/listinfo/alsa-devel
>
More information about the Alsa-devel
mailing list