Get the current channel mapping for a PCM stream


#include <sys/asoundlib.h>

int snd_pcm_query_channel_map( snd_pcm_t *pcm,
                               snd_pcm_chmap_t *chmap );


The handle for the PCM device, which you must have opened by calling snd_pcm_open_name(), snd_pcm_open(), or snd_pcm_open_preferred().
A pointer to a snd_pcm_chmap_t structure where the function can store the mapping. The map contains playback or capture channels. You must allocate this structure before calling this function.


Use the -l asound option to qcc to link against this library.


This function retrieves a map of speakers or microphones based on whether it's a playback or capture channel. When this function successfully returns, the map array contains a map of speakers. If you don't specify an array that's large enough to store the number of channels in map.pos, the function returns with ENOMEM. When the function returns with ENOMEM, map.channels is updated with the minimum size of array that is required for map.pos.

If you want to determine the correct size for the array, you can call this function the first time, to determine the array size to allocate for map.pos. Then, call the function a second time to retrieve the PCM channels.


EOK on success, an errno upon failure. The errno values are available in the errno.h file.


The value of pcm or chmap is NULL.
Not enough memory was available.


snd_pcm_chmap_t *chmap;
int i;

/* Size the chmap to hold up to 32 channels */

printf("Get channel map using snd_pcm_query_channel_map()...\n");
chmap = calloc(1, sizeof(snd_pcm_chmap_t) + (sizeof(int) * 32));
chmap->channels = 32;

if ((rtn = snd_pcm_query_channel_map(pcm_handle, chmap)) != EOK)
    fprintf(stderr, "snd_pcm_query_channel_map failed: %s\n", snd_strerror(rtn));
    return -1;

printf("Number of channels = %d\n", chmap->channels);
for(i = 0; i < chmap->channels; i++)
    printf("\tchmap.pos[%d] = %d\n", i, chmap->pos[i]);

free (chmap);


QNX Neutrino

Cancellation point No
Interrupt handler No
Signal handler Yes
Thread Read the Caveats


This function isn't thread-safe if pcm (snd_pcm_t) is used across multiple threads.