Convertire un file WAV in formato OggVorbis con le funzioni esterne del API di libsndfile
Da Gambas-it.org - Wikipedia.
Versione del 13 gen 2024 alle 17:40 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.37 ".
Mostriamo un semplice esempio, nel quale si convertirà un file WAV in un file OGG-Vorbis:
Library "libsndfile:1.0.37" 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 SF_FALSE As Integer = 0 Private Const SFM_READ As Integer = &10 Private Const SFM_WRITE As Integer = &20 Private Const SF_FORMAT_VORBIS As Integer = &60 Private Const SF_FORMAT_OGG As Integer = &200000 ' 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 int sf_format_check (const SF_INFO *info) ' Return TRUE if fields of the SF_INFO struct are a valid combination of values. Private Extern sf_format_check(info As SF_INFO) As Integer ' sf_count_t sf_read_float (SNDFILE *sndfile, float *ptr, sf_count_t items) ' Function for reading the data chunk in terms of items. Private Extern sf_read_float(sndfile As Pointer, ptr As Pointer, items As Long) As Long ' sf_count_t sf_write_float (SNDFILE *sndfile, float *ptr, sf_count_t items) ' Function for writing the data chunk in terms of items. Private Extern sf_write_float(sndfile As Pointer, ptr As Pointer, items As Long) As Long ' 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 buffer As New Single[4096] Dim infile, outfile As Pointer Dim sfinfo As New SF_INFO Dim fileorigine As String fileorigine = "/percorso/del/file.wav" infile = sf_open(fileorigine, SFM_READ, sfinfo) If infile == 0 Then Error.Raise("Impossibile aprire il file audio in entrata !") ' Imposta le caratteristiche del file OGG:" With sfinfo .samplerate = 44100 .channels = 2 .format_ = SF_FORMAT_VORBIS Or SF_FORMAT_OGG End With If sf_format_check(sfinfo) == SF_FALSE Then sf_close(infile) Error.Raise("Errore: codifica non valida !") Endif outfile = sf_open("/tmp/file.ogg", SFM_WRITE, sfinfo) If outfile == 0 Then Error.Raise("Impossibile aprire il file audio in uscita !") Write "\e[5mAttendere...\e[0m" Flush While sf_read_float(infile, buffer.Data, buffer.Count) > 0 ' Scrive i dati audio nel file OGG-Vorbis: sf_write_float(outfile, buffer.Data, buffer.Count) Wend Write "\rConversione terminata !" sf_close(infile) sf_close(outfile) End