Convertire la frequenza di campionamento di un file WAV con le funzioni esterne del API di SDL2

Da Gambas-it.org - Wikipedia.

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


Mostriamo di seguito un esempio di codice per convertire la frequenza di campionamento di un file WAV. In tal caso sarà necessario richiamare le seguenti librerie dell'API di SDL2:

  • libSDL2-2.0.so
  • libSDL2_mixer-2.0.so

Mostriamo di seguito un esempio di codice per convertire la frequenza di campionamento di un file WAV al valore 22050 hertz:

Library "libSDL2-2.0"

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

Public Struct SDL_AudioCVT
  needed As Integer
  src_format As Short
  dst_format As Short
  rate_incr As Float
  buf As Pointer
  len As Integer
  len_cvt As Integer
  len_mult As Integer
  len_ratio As Float
  filters[10] As Pointer
  filter_index As Integer
End Struct

Private Const SDL_INIT_AUDIO As Integer = 16
Private Const SDL_AUDIO_MASK_BITSIZE As Integer = 127

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

' 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(SDL_RWops As Pointer, freesrc As Integer, SDL_spec As SDL_AudioSpec, audio_buf As Pointer, audio_len As Pointer) As SDL_AudioSpec

' int SDL_BuildAudioCVT(SDL_AudioCVT * cvt, SDL_AudioFormat src_format, Uint8 src_channels, int src_rate, SDL_AudioFormat dst_format, Uint8 dst_channels, int dst_rate)
' Initialize an SDL_AudioCVT structure for conversion.
Private Extern SDL_BuildAudioCVT(SDL_cvt As SDL_AudioCVT, src_format As Integer, src_channels As Byte, src_rate As Integer, dst_format As Integer, dst_channels As Byte, dst_rate As Integer) As Integer

' int SDL_ConvertAudio(SDL_AudioCVT* cvt)
' Converts audio data to a desired audio format.
Private Extern SDL_ConvertAudio(SDL_cvt As SDL_AudioCVT) As Integer

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


' void *memcpy(void *str1, const void *str2, size_t n)
' Copies n characters from memory area str2 to memory area str1.
Private Extern memcpy(str1 As Pointer, str2 As Pointer, n As Integer) In "libc:6"


Public Sub Main()

 Dim filename As String = "/percorso/del/file.wav"
 Dim convertito As String = "/percorso/del/file.wav/convertito"
 Dim data, pbb As Pointer
 Dim lun, cvtfreq, bitsize, blockalign, avgbytes As Integer
 Dim spec As New SDL_AudioSpec
 Dim cvt As New SDL_AudioCVT
 Dim fl As File
 Dim bb As Byte[]
 Dim st As Stream
 
' Imposta la nuova frequenza di campionamento, nella quale il file wav sarà convertito:
 cvtfreq = 22050
 
' Inizializza la libreria SDL2:
 If SDL_Init(SDL_INIT_AUDIO) < 0 Then Error.Raise("Impossibile inizializzare la libreria SDL2 !")
  
 If IsNull(SDL_LoadWAV_RW(SDL_RWFromFile(filename, "rb"), 1, spec, VarPtr(data), VarPtr(lun))) Then 
   Error.Raise("Impossibile caricare il file audio: " & filename & " !")
 Endif
  
 If SDL_BuildAudioCVT(cvt, spec.format, spec.channels, spec.freq, spec.format, spec.channels, cvtfreq) = -1 Then
   Error.Raise("Impossibile realizzare il CVT !")
 Endif
 
 cvt.len = lun
 cvt.buf = Alloc(lun * cvt.len_mult)
 
 memcpy(cvt.buf, data, lun)
  
 If SDL_ConvertAudio(cvt) = -1 Then Error.Raise("Conversione fallita !")
   
 bitsize = spec.format And SDL_AUDIO_MASK_BITSIZE
 blockalign = (bitsize / 8) * spec.channels
 avgbytes = cvtfreq * blockalign
   
' Scrive l'header del file WAV in uscita:
 fl = Open convertito For Create
 Write #fl, &46464952 As Integer   ' RIFF
 Write #fl, &45564157 As Integer   ' WAVE
 Write #fl, &20746D66 As Integer   ' fmt
 Write #fl, 16 As Integer   ' chunk size
 Write #fl, 1 As Short   ' uncompressed
 Write #fl, spec.channels As Short   ' channels
 Write #fl, cvtfreq As Integer   ' sample rate
 Write #fl, avgbytes As Integer   ' average bytes per second
 Write #fl, blockalign As Short   ' block align
 Write #fl, bitsize As Short   ' significant bits per sample
 Write #fl, &61746164 As Integer   ' data
 Write #fl, cvt.len_cvt As Integer   ' size
 
 bb = New Byte[cvt.len_cvt]
' Accede alla "Struttura" interna del vettore "bb":
 pbb = Object.Address(bb)
' Vengono usati i "Memory Stream" per scrivere nella "Struttura" interna del vettore "bb":
 st = Memory pbb + 32 For Write
' Scrive l'indirizzo di memoria di "cvt.buf" nella "Struttura" interna del vettore "bb".
' In tal modo il vettore "bb" assume i valori contenuti dall'area di memoria puntata da "cvt.buf":
 Write #st, cvt.buf As Pointer
 
' Scrive i valori contenuti dal vettore "bb" nel file precedentemente aperto:
 bb.Write(fl, 0, bb.Count)
  
' Va in chiusura:
 st.Close
 Free(cvt.buf + SizeOf(gb.Pointer))
 fl.Close
 SDL_Quit()
 
End



Riferimenti