Convertire un file di soli dati audio raw in un file di formato WAV con le funzioni esterne del API di Sndfile
Da Gambas-it.org - Wikipedia.
E' possibile convertire un file contenente soli dati audio grezzi (raw) in un file audio di formato WAV con le funzioni esterne del API di Sndfile. E' necessario avere installata nel proprio sistema e richiamare in Gambas la libreria condivisa: "libsndfile.so.1.0.31 ".
Mostriamo un esempio pratico:
Library "libsndfile:1.0.31" 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_WAV As Integer = &010000 Private Const SF_FORMAT_PCM_16 As Integer = &02 ' 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/dati/audio/grezzi" 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 WAV:" With sfinfo .samplerate = 44100 .channels = 2 .format_ = SF_FORMAT_WAV Or SF_FORMAT_PCM_16 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.wav", 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 WAV: sf_write_float(outfile, buffer.Data, buffer.Count) Wend Write "\rConversione terminata !" sf_close(infile) sf_close(outfile) End