Estrarre alcune informazioni di un file audio WAV con le funzioni esterne del API di SDL2

Da Gambas-it.org - Wikipedia.

Con alcune funzioni esterne del API del sistema SDL2 è possibile estrarre alcune informazioni da un file audio di formato WAV.

E' necessario avere installata nel sistema e richiamare nel programma Gambas la libreria condivisa: "libSDL2-2.0.so.0.3000.0 ".

Mostriamo un semplice esempio:

Library "libSDL2-2.0:0.3000.0"

Private Const SDL_INIT_AUDIO As Integer = 16

Public Struct SDL_AudioSpec
  freq As Integer
  format As Short
  channels As Byte
  silence As Byte
  samples As Short
  padding As Short
  size As Integer
  callback As Pointer
  userdata As Pointer
End Struct

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

' const char* SDL_GetError(void)
' Retrieve a message about the last error that occurred.
Private Extern SDL_GetError() As String

' SDL_RWops * SDL_RWFromFile(const char *file, const char *mode)
' Create SDL_RWops structures from various data streams.
Private Extern SDL_RWFromFile(SDLfile As String, mode As String) As Pointer

' SDL_AudioSpec * SDL_LoadWAV_RW(SDL_RWops * src, int freesrc, SDL_AudioSpec * spec, Uint8 ** audio_buf, Uint32 * audio_len)
' Loads a WAVE from the data source.
Private Extern SDL_LoadWAV_RW(src As Pointer, freesrc As Integer, spec As SDL_AudioSpec, audio_buf As Pointer, audio_len As Pointer) As SDL_AudioSpec

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


Public Sub Main()

 Dim percorso, s As String
 Dim i, buf, lun As Integer
 Dim rwops As Pointer
 Dim ausp As New SDL_AudioSpec   
  
 percorso = "/percorso/del/file.wav"
  
' Inizializza la libreria SDL2:"
 i = SDL_Init(SDL_INIT_AUDIO)
 If i < 0 Then Error.Raise("Impossibile inizializzare la libreria SDL2: " & SDL_GetError())
  
 rwops = SDL_RWFromFile(percorso, "rb")
  
 SDL_LoadWAV_RW(rwops, 1, ausp, VarPtr(buf), VarPtr(lun))
  
 Print "File audio:        "; percorso
 Print "Dati audio grezzi: "; lun; " byte"
 With ausp
   Print "Frequenza:         "; .freq; " hertz"
   Print "Canali:            "; .channels
   Print "Buffer audio:      "; .samples; " byte"
   For i = 15 To 0 Step -1
     If (Shl(1, i) And .format) > 0 Then
       Select Case 2 ^ i
         Case 8
           s &= "8-bit"
         Case 16
           s &= "16-bit"
         Case 32
           s &= "32-bit"
         Case 256
           s &= "Float "
         Case 4096
           s &= "BigEndian "
         Case 32768
           s &= "Signed "
       End Select
     Endif
   Next
   Print "Formato audio:     "; s
 End With
  
 SDL_Quit()
  
End