Поиск доступных звуковых карт в Linux программно - PullRequest
5 голосов
/ 03 сентября 2011

Есть ли способ получить список доступных звуковых карт в системе программно, используя asoundlib и C? Я хочу это с той же информацией, что и /proc/asound/cards.

1 Ответ

6 голосов
/ 06 июня 2012

Вы можете перебирать карточки, используя snd_card_next, начиная со значения -1, чтобы получить 0-ю карточку.

Вот пример кода;скомпилируйте его с помощью gcc -o countcards countcards.c -lasound:

#include <alsa/asoundlib.h>
#include <stdio.h>

int main()
{
    int totalCards = 0;   // No cards found yet
    int cardNum = -1;     // Start with first card
    int err;

    for (;;) {
        // Get next sound card's card number.
        if ((err = snd_card_next(&cardNum)) < 0) {
            fprintf(stderr, "Can't get the next card number: %s\n",
                            snd_strerror(err));
            break;
        }

        if (cardNum < 0)
            // No more cards
            break;

        ++totalCards;   // Another card found, so bump the count
    }

    printf("ALSA found %i card(s)\n", totalCards);

    // ALSA allocates some memory to load its config file when we call
    // snd_card_next. Now that we're done getting the info, tell ALSA
    // to unload the info and release the memory.
    snd_config_update_free_global();
}

Этот код сокращен с cardnames.c (который также открывает каждую карту, чтобы прочитать ее имя).

...