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

Da Gambas-it.org - Wikipedia.
 
(3 versioni intermedie di uno stesso utente non sono mostrate)
Riga 1: Riga 1:
 
Alcune funzioni esterne del API di ALSA consentono di sapere quante e quali sono le schede audio presenti nel proprio computer.
 
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:
Il seguente applicativo elenca semplicemente i nomi di tutte le schede audio installate:
 
 
  Library "libasound:2.0.0"
 
  Library "libasound:2.0.0"
 
   
 
   
Riga 38: Riga 37:
 
   
 
   
 
   
 
   
  '''Public''' Sub Main()
+
  Public Sub Main()
 
   
 
   
 
   Dim err As Integer
 
   Dim err As Integer
Riga 45: Riga 44:
 
   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  
      
+
      Chiusura(schedHandle)
    stringa = "hw:" & numSchede
+
      Error.Raise("Impossibile aprire la scheda " & numSchede & ":" & snd_strerror(err))
      
+
     Endif
    err = snd_ctl_open(VarPtr(schedHandle), stringa, 0)
+
    snd_ctl_card_info_malloc(VarPtr(schedInfo))
    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))
+
      Chiusura(schedHandle)
    Else
+
      Error.Raise("Impossibile ottenere informazioni per la scheda audio: " & numSchede & " " & snd_strerror(err))
      Print "Scheda audio "; numSchede; " = "; snd_ctl_card_info_get_name(schedInfo)
+
    Else
    Endif
+
      Print "\e[0mScheda audio "; numSchede; " = \e[1m"; snd_ctl_card_info_get_name(schedInfo)
      
+
    Endif
<FONT color=gray>' ''Al termine chiude l'interfaccia di controllo della scheda audio:''</font>
+
     err = snd_card_next(VarPtr(numSchede))
    snd_ctl_close(schedHandle)
+
    If err < 0 Then
   
+
      Chiusura(schedHandle)
  Wend
+
      Error.Raise("Impossibile ottenere il numero della scheda successiva: ", snd_strerror(err))
 +
    Endif
 +
  Until numSchede < 0
 +
 
 +
  Chiusura(schedHandle)
 
    
 
    
  <FONT color=gray>' ''Va in chiusura liberando la memoria precedentemente allocata:''</font>
+
  End
  snd_config_update_free_global()
+
 
   
 
   
  '''End'''
+
  Private Procedure Chiusura(han as Pointer)
 +
 
 +
<FONT color=gray>' ''Al termine chiude l'interfaccia di controllo della scheda audio e libera la memoria precedentemente allocata:''</font>
 +
  snd_ctl_close(han)
 +
  snd_config_update_free_global()
 +
 
 +
End

Versione attuale delle 12:25, 13 gen 2024

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