Differenze tra le versioni di "Convertire un file WAV in formato OggVorbis con le funzioni esterne del API di libsndfile"
Da Gambas-it.org - Wikipedia.
Riga 17: | Riga 17: | ||
End Struct | End Struct | ||
+ | Private Const SF_FALSE As Integer = 0 | ||
Private Const SFM_READ As Integer = &10 | Private Const SFM_READ As Integer = &10 | ||
Private Const SFM_WRITE As Integer = &20 | Private Const SFM_WRITE As Integer = &20 | ||
+ | Private Const SF_FORMAT_VORBIS As Integer = &60 | ||
Private Const SF_FORMAT_OGG As Integer = &200000 | Private Const SF_FORMAT_OGG As Integer = &200000 | ||
− | |||
<FONT Color=gray>' ''SNDFILE * sf_open (const char *path, int mode, SF_INFO *sfinfo)'' | <FONT Color=gray>' ''SNDFILE * sf_open (const char *path, int mode, SF_INFO *sfinfo)'' | ||
' Open the specified file for read, write or both.''</font> | ' Open the specified file for read, write or both.''</font> | ||
Private Extern sf_open(path As String, mode As Integer, sfinfo As SF_INFO) As Pointer | Private Extern sf_open(path As String, mode As Integer, sfinfo As SF_INFO) As Pointer | ||
+ | |||
+ | <FONT Color=gray>' ''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.''</font> | ||
+ | Private Extern sf_format_check(info As SF_INFO) As Integer | ||
<FONT Color=gray>' ''sf_count_t sf_readf_short (SNDFILE *sndfile, short *ptr, sf_count_t frames)'' | <FONT Color=gray>' ''sf_count_t sf_readf_short (SNDFILE *sndfile, short *ptr, sf_count_t frames)'' | ||
Riga 33: | Riga 38: | ||
' ''Function for writing the data chunk in terms of frames.''</font> | ' ''Function for writing the data chunk in terms of frames.''</font> | ||
Private Extern sf_writef_short(sndfile As Pointer, ptr As Pointer, frames As Long) As Long | Private Extern sf_writef_short(sndfile As Pointer, ptr As Pointer, frames As Long) As Long | ||
− | |||
− | |||
− | |||
− | |||
<FONT Color=gray>' ''int sf_close (SNDFILE *sndfile)'' | <FONT Color=gray>' ''int sf_close (SNDFILE *sndfile)'' | ||
Riga 43: | Riga 44: | ||
− | + | Public Sub Main() | |
− | + | ||
− | Dim | + | Dim buffer As New Single[4096] |
− | Dim | + | Dim infile, outfile As Pointer |
− | Dim | + | Dim sfinfo As New SF_INFO |
− | Dim | + | Dim fileorigine As String |
− | + | ||
− | + | fileorigine = "<FONT Color=darkgreen>''/percorso/del/file.wav''</font>" | |
− | + | ||
− | If | + | infile = sf_open(fileorigine, SFM_READ, sfinfo) |
− | + | If infile == 0 Then Error.Raise("Impossibile aprire il file audio in entrata !") | |
− | <FONT Color=gray>' '' | + | |
− | With | + | <FONT Color=gray>' ''Imposta le caratteristiche del file OGG:''</font>" |
+ | With sfinfo | ||
.samplerate = 44100 | .samplerate = 44100 | ||
.channels = 2 | .channels = 2 | ||
− | .format_ = SF_FORMAT_OGG | + | .format_ = SF_FORMAT_VORBIS Or SF_FORMAT_OGG |
End With | 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 !") | |
− | If | ||
Write "\e[5mAttendere...\e[0m" | Write "\e[5mAttendere...\e[0m" | ||
Flush | Flush | ||
− | + | ||
− | While | + | While sf_read_float(infile, buffer, buffer.Count) > 0 |
<FONT Color=gray>' ''Scrive i dati audio nel file OGG-Vorbis:''</font> | <FONT Color=gray>' ''Scrive i dati audio nel file OGG-Vorbis:''</font> | ||
− | + | sf_write_float(outfile, buffer, buffer.Count) | |
− | |||
− | |||
− | |||
− | |||
Wend | Wend | ||
Write "\rConversione terminata !" | Write "\rConversione terminata !" | ||
− | + | ||
− | sf_close( | + | sf_close(infile) |
− | sf_close( | + | sf_close(outfile) |
− | + | ||
− | + | End |
Versione delle 16:17, 3 nov 2023
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.31 ".
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.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_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_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 ' 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.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, buffer.Count) > 0 ' Scrive i dati audio nel file OGG-Vorbis: sf_write_float(outfile, buffer, buffer.Count) Wend Write "\rConversione terminata !" sf_close(infile) sf_close(outfile) End