Convertire un file WAV in formato OggVorbis con le funzioni esterne del API di libsndfile
Da Gambas-it.org - Wikipedia.
Versione del 2 dic 2021 alle 18:12 di Vuott (Discussione | contributi)
La conversione dei formati audio mediante le sole risorse di Sndfile è abbastanza semplice.
E' necessario avere installata nel proprio sistema e richiamare in Gambas la libreria condivisa: "libsndfile.so.1.0.28 ".
Mostriamo un semplice esempio, nel quale si convertirà un file WAV in un file OGG-Vorbis:
Private Const LUN_BUFF As Long = 4096 Library "libsndfile:1.0.28" Public Struct SF_INFO frames As Long samplerate As Integer channels As Integer format_ As Integer sections As Integer seekable As Integer End Struct Private Const SFM_READ As Integer = &10 Private Const SFM_WRITE As Integer = &20 Private Const SF_FORMAT_OGG As Integer = &200000 Private Const SF_FORMAT_VORBIS As Integer = &60 ' SNDFILE * sf_open (const char *path, int mode, SF_INFO *sfinfo) ' Open the specified file for read, write or both. Private Extern sf_open(path As String, mode As Integer, sfinfo As SF_INFO) As Pointer ' sf_count_t sf_readf_short (SNDFILE *sndfile, short *ptr, sf_count_t frames) ' Function for reading the data chunk in terms of frames. Private Extern sf_readf_short(sndfile As Pointer, ptr As Pointer, frames As Long) As Long ' sf_count_t sf_writef_short (SNDFILE *sndfile, const short *ptr, sf_count_t frames) ' Function for writing the data chunk in terms of frames. Private Extern sf_writef_short(sndfile As Pointer, ptr As Pointer, frames As Long) As Long ' const char* sf_strerror (SNDFILE *sndfile) ' Returns to the caller a pointer to the current error message for the given SNDFILE. Private Extern sf_strerror(sndfile As Pointer) As String ' int sf_close (SNDFILE *sndfile) ' Close the SNDFILE and clean up all memory allocations associated with this file. Private Extern sf_close(sndfile As Pointer) As Integer Public Sub Main() Dim sfiW, sfiO As SF_INFO ' Si creano due distinti oggetti struttura "SF_INFO" per il file WAV e il file OGG-Vorbis Dim fen, fex As Pointer Dim buffer As Short[] Dim frm As Long sfiW = New SF_INFO fen = sf_open("/percorso/del/file.wav", SFM_READ, sfiW) If fen = 0 Then Error.Raise("Errore: " & sf_strerror(fen)) ' Si impostano le informazioni necessarie ed essenziali per il nuovo file audio OGG-Vorbis nella Struttura "SF_INFO": With sfiO = New SF_INFO .samplerate = 44100 .channels = 2 .format_ = SF_FORMAT_OGG Or SF_FORMAT_VORBIS End With ' Crea e apre in scrittura il nuovo file OGG-Vorbis: fex = sf_open("/percorso/del/file.ogg", SFM_WRITE, sfiO) If fex = 0 Then Error.Raise("Errore: " & sf_strerror(fex)) buffer = New Short[sfiW.frames] frm = sf_readf_short(fen, buffer.Data, LUN_BUFF) If frm > LUN_BUFF Then Error.Raise("Errore: " & sf_strerror(fen)) While frm > 0 ' Scrive i dati audio nel file OGG-Vorbis: frm = sf_writef_short(fex, buffer.Data, frm) If frm > LUN_BUFF Then Error.Raise("Errore: " & sf_strerror(fex)) frm = sf_readf_short(fen, buffer.Data, LUN_BUFF) If frm > LUN_BUFF Then Error.Raise("Errore: " & sf_strerror(fen)) Wend sf_close(fex) sf_close(fen) End