Conoscere con le funzioni esterne di SDL2 i dispositivi audio presenti nel sistema

Da Gambas-it.org - Wikipedia.

La libreria SDL 2 è un API multi-piattaforma contenente funzioni per la gestione multimediale dell'audio e del video.

Per poter utilizzare le funzioni esterne di SDL2 in Gambas, si richiamerà la seguente libreria (con l'attuale versione): libSDL2-2.0.so


Mostriamo di seguito un esempio di codice per verificare i dispositivi audio presenti nel sistema.

Library "libSDL2-2.0"

Private Const SDL_INIT_AUDIO As Integer = 16
 
' int SDL_Init(Uint32 flags)
' Initialize the SDL library.
Private Extern SDL_Init(flags As Integer) As Integer

' int SDL_GetNumAudioDrivers(void)
' Returns the number of built in audio drivers.
Private Extern SDL_GetNumAudioDrivers() As Integer

' const char* SDL_GetAudioDriver(int index)
' Returns the name of the audio driver at the requested index, or NULL if an invalid index was specified.
Private Extern SDL_GetAudioDriver(index As Integer) As String

' const char* SDL_GetCurrentAudioDriver(void)
' Returns the name of the current audio driver or NULL if no driver has been initialized.
Private Extern SDL_GetCurrentAudioDriver() As String

' int SDL_GetNumAudioDevices(int iscapture)
' Returns the number of available devices exposed by the current driver or -1 if an explicit list of devices can't be determined.
Private Extern SDL_GetNumAudioDevices(iscapture As Integer) As Integer

' const char* SDL_GetAudioDeviceName(int index, int iscapture)
' Returns the name of the audio device at the requested index, or NULL on error.
Private Extern SDL_GetAudioDeviceName(index As Integer, iscapture As Integer) As String

' void SDL_Quit(void)
' Clean up all initialized subsystems.
Private Extern SDL_Quit()


Public Sub Main()

 Dim n, i As Integer
  
' Inizializza la libreria SDL2:
  If SDL_Init(SDL_INIT_AUDIO) < 0 Then Error.Raise("Impossibile inizializzare la libreria SDL2 !")
   
' Mostra i dispositivi audio disponibili:
  n = SDL_GetNumAudioDrivers()
  If n = 0 Then
    Print "Nessun driver audio presente nel sistema !\n\n"
  Else
    Print "Driver audio presenti nel sistema:"
    For i = 0 To n - 1
      Print "  "; SDL_GetAudioDriver(i)
    Next
  Endif
   
  Print "\nDriver audio usato: \n  "; SDL_GetCurrentAudioDriver()
   
  For i = 0 To 1
    Dispositivi(i)
  Next
   
  SDL_Quit()
     
End


Private Procedure Dispositivi(cattura As Integer)
 
 Dim ts As String
 Dim n, i As Integer
 
  ts = IIf(cattura, "cattura", "uscita")
  n = SDL_GetNumAudioDevices(cattura)
  
  Print "\nDispositivi di "; ts; ":"
  If n = -1 Then
    Print "  Il driver non può verificare specifici dispositivi "; ts; "."
  Else If n = 0 Then
    Print "  Nessun dispositivo di "; ts; " trovato."
  Else
    For i = 0 To n - 1
      Print "  "; SDL_GetAudioDeviceName(i, cattura)
    Next
  Endif
 
End



Riferimenti