Sapere con le funzioni esterne di ALSA quali schede audio sono installate nel computer

Da Gambas-it.org - Wikipedia.

Alcune funzioni esterne del API di ALSA consentono di sapere quante e quali sono le schede audio presenti nel proprio computer.

Il seguente codice elenca semplicemente i nomi di tutte le schede audio installate:

Library "libasound:2.0.0"

' int snd_card_next(int *card)
' Try to determine the next card.
Private Extern snd_card_next(cardP As Pointer) As Integer

' const char* snd_strerror(int errnum)
' Returns the message for an error code.
Private Extern snd_strerror(errnum As Integer) As String

' int snd_ctl_open(snd_ctl_t **ctl, const char *name, int mode)
' Opens a CTL.
Private Extern snd_ctl_open(ctl As Pointer, name As String, mode As Integer) As Integer

' snd_ctl_card_info_malloc(ptr)
' Allocate an invalid snd_ctl_card_info_t using standard malloc.
Private Extern snd_ctl_card_info_malloc(ptr As Pointer)

' int snd_ctl_card_info(snd_ctl_t * ctl, snd_ctl_card_info_t * info)
' Get card related information.
Private Extern snd_ctl_card_info(ctl As Pointer, info As Pointer) As Integer

' const char* snd_ctl_card_info_get_name(const snd_ctl_card_info_t * obj)
' Get card name from a CTL card info.
Private Extern snd_ctl_card_info_get_name(obj As Pointer) As String

' int snd_ctl_close(snd_ctl_t * ctl)
' Close CTL handle.
Private Extern snd_ctl_close(ctl As Pointer) As Integer

' int snd_config_update_free_global(void)
' Frees the global configuration tree in snd_config.
Private Extern snd_config_update_free_global() As Integer


Public Sub Main()

 Dim err As Integer
 Dim numSchede As Short = -1
 Dim stringa As String
 Dim schedHandle, schedInfo As Pointer
 
' Ottiene il numero della scheda audio successiva.
' Se "numSchede" == 0, allora ALSA recupera la prima scheda:
 err = snd_card_next(VarPtr(numSchede))
 If err < 0 Then Error.Raise("Impossibile ottenere il numero della scheda successiva: ", snd_strerror(err))
  
 Repeat  
   stringa = "hw:" & numSchede
   err = snd_ctl_open(VarPtr(schedHandle), stringa, 0)
   If err < 0 Then 
     Chiusura(schedHandle)
     Error.Raise("Impossibile aprire la scheda " & numSchede & ":" & snd_strerror(err))
   Endif
   snd_ctl_card_info_malloc(VarPtr(schedInfo))
   
' Dice ad ALSA di compilare "snd_ctl_card_info_t" con informazioni sulla corrente scheda audio individuata:
   err = snd_ctl_card_info(schedHandle, schedInfo)
   If err < 0 Then
     Chiusura(schedHandle)
     Error.Raise("Impossibile ottenere informazioni per la scheda audio: " & numSchede & " " & snd_strerror(err))
   Else
     Print "\e[0mScheda audio "; numSchede; " = \e[1m"; snd_ctl_card_info_get_name(schedInfo)
   Endif
   err = snd_card_next(VarPtr(numSchede))
   If err < 0 Then
     Chiusura(schedHandle)
     Error.Raise("Impossibile ottenere il numero della scheda successiva: ", snd_strerror(err))
   Endif
 Until numSchede < 0
 
 Chiusura(schedHandle)
 
End


Private Procedure Chiusura(han as Pointer)
 
' Al termine chiude l'interfaccia di controllo della scheda audio e libera la memoria precedentemente allocata:
 snd_ctl_close(han)
 snd_config_update_free_global()
 
End