Differenze tra le versioni di "Sapere con le funzioni esterne di ALSA quali schede audio sono installate nel computer"

Da Gambas-it.org - Wikipedia.
Riga 45: Riga 45:
 
   Dim schedHandle, schedInfo As Pointer
 
   Dim schedHandle, schedInfo As Pointer
 
    
 
    
 
 
  While True
 
   
 
 
  <FONT color=gray>' ''Ottiene il numero della scheda audio successiva.''
 
  <FONT color=gray>' ''Ottiene il numero della scheda audio successiva.''
  ' ''Se "numSchede" = -1, allora ALSA recupera la prima scheda:''</font>
+
  ' ''Se "numSchede" == 0, allora ALSA recupera la prima scheda:''</font>
    err = snd_card_next(VarPtr(numSchede))
+
  err = snd_card_next(VarPtr(numSchede))
    If err < 0 Then Error.Raise("Impossibile ottenere il numero della scheda successiva: ", snd_strerror(err))
+
  If err < 0 Then Error.Raise("Impossibile ottenere il numero della scheda successiva: ", snd_strerror(err))
   
+
 
    If numSchede < 0 Then Break
+
  Repeat  
   
+
     stringa = "hw:" & numSchede
<FONT color=gray>' ''Si apre l'interfaccia di controllo di questa scheda.''
+
     err = snd_ctl_open(VarPtr(schedHandle), stringa, 0)
  ' ''Viene specificato solo il numero della Scheda, ma nessun dispositivo audio né alcun sub-dispositivo:''</font>
+
    If err < 0 Then Error.Raise("Impossibile aprire la scheda " & numSchede & ":" & snd_strerror(err))
      
+
     snd_ctl_card_info_malloc(VarPtr(schedInfo))
    stringa = "hw:" & numSchede
 
      
 
    err = snd_ctl_open(VarPtr(schedHandle), stringa, 0)
 
    If err < 0 Then Error.Raise("Impossibile aprire la scheda " & numSchede & ":" & snd_strerror(err))
 
      
 
    snd_ctl_card_info_malloc(VarPtr(schedInfo))
 
 
      
 
      
 
  <FONT color=gray>' ''Dice ad ALSA di compilare "snd_ctl_card_info_t" con informazioni sulla corrente scheda audio individuata:''</font>
 
  <FONT color=gray>' ''Dice ad ALSA di compilare "snd_ctl_card_info_t" con informazioni sulla corrente scheda audio individuata:''</font>
    err = snd_ctl_card_info(schedHandle, schedInfo)
+
    err = snd_ctl_card_info(schedHandle, schedInfo)
    If err < 0 Then
+
    If err < 0 Then
      Error.Raise("Impossibile ottenere informazioni per la scheda audio: " & numSchede & " " & snd_strerror(err))
+
      Error.Raise("Impossibile ottenere informazioni per la scheda audio: " & numSchede & " " & snd_strerror(err))
    Else
+
    Else
      Print "Scheda audio "; numSchede; " = "; snd_ctl_card_info_get_name(schedInfo)
+
      Print "Scheda audio "; numSchede; " = "; snd_ctl_card_info_get_name(schedInfo)
    Endif
+
    Endif
      
+
     err = snd_card_next(VarPtr(numSchede))
<FONT color=gray>' ''Al termine chiude l'interfaccia di controllo della scheda audio:''</font>
+
    If err < 0 Then Error.Raise("Impossibile ottenere il numero della scheda successiva: ", snd_strerror(err))
    snd_ctl_close(schedHandle)
+
  Until numSchede < 0
   
 
  Wend
 
 
    
 
    
  <FONT color=gray>' ''Va in chiusura liberando la memoria precedentemente allocata:''</font>
+
  <FONT color=gray>' ''Al termine chiude l'interfaccia di controllo della scheda audio e libera la memoria precedentemente allocata:''</font>
  snd_config_update_free_global()
+
  snd_ctl_close(schedHandle)
 +
  snd_config_update_free_global()
 
   
 
   
 
  '''End'''
 
  '''End'''

Versione delle 12:38, 10 ago 2020

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


Il seguente applicativo 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 Error.Raise("Impossibile aprire la scheda " & numSchede & ":" & snd_strerror(err))
   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
     Error.Raise("Impossibile ottenere informazioni per la scheda audio: " & numSchede & " " & snd_strerror(err))
   Else
     Print "Scheda audio "; numSchede; " = "; snd_ctl_card_info_get_name(schedInfo)
   Endif
   err = snd_card_next(VarPtr(numSchede))
   If err < 0 Then Error.Raise("Impossibile ottenere il numero della scheda successiva: ", snd_strerror(err))
 Until numSchede < 0
 
' Al termine chiude l'interfaccia di controllo della scheda audio e libera la memoria precedentemente allocata:
 snd_ctl_close(schedHandle)
 snd_config_update_free_global()

End