ALSA is composed of a Linux driver framework and a user-space API that together provide access to audio hardware. Some systems have multiple audio devices, so ALSA assigns each device a unique set of numbers: card, device and sub-device. Sometimes the set of available audio devices might change at run-time, such is when a USB audio device is plugged in or unplugged. On such systems, the device numbers are not guaranteed to be consistent. If an application would like to use a specific audio device, it requires a way to find out what numbers have been assigned to that device.
One way to find the numbers for a device is to know the device name. ALSA provides API functions for enumerating sound devices, and reading the name of each one. An application using this method can look at each device on the system, get the device name, and compare that name to the one it’s looking for. When the name is found, it knows what numbers to use in opening the device. ALSA cards can be enumerated using the snd_card_next function. Then the card is opened (snd_ctl_open) and its info is read (snd_ctl_card_info). For each card, the device numbers can be enumerated using the snd_ctl_pcm_next_device function. For each device the pcm info can be read (snd_ctl_pcm_info) and the name can be read from the info (snd_pcm_info_get_name). The sub-device number is almost always zero.