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.
(8 versioni intermedie di uno stesso utente non sono mostrate) | |||
Riga 1: | Riga 1: | ||
La conversione dei formati audio mediante le sole risorse di ''Sndfile'' è abbastanza semplice. | 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. | + | 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: | Mostriamo un semplice esempio, nel quale si convertirà un file WAV in un file OGG-Vorbis: | ||
− | + | Library "libsndfile:1.0.37" | |
− | |||
− | Library "libsndfile:1.0. | ||
Public Struct SF_INFO | Public Struct SF_INFO | ||
Riga 17: | Riga 15: | ||
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)'' | ||
Riga 26: | Riga 25: | ||
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 | + | <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 | + | Private Extern sf_format_check(info As SF_INFO) As Integer |
− | <FONT Color=gray>' ''sf_count_t | + | <FONT Color=gray>' ''sf_count_t sf_read_float (SNDFILE *sndfile, float *ptr, sf_count_t items)'' |
− | ' ''Function for | + | ' ''Function for reading the data chunk in terms of items.''</font> |
− | Private Extern | + | Private Extern sf_read_float(sndfile As Pointer, ptr As Pointer, items As Long) As Long |
− | <FONT Color=gray>' '' | + | <FONT Color=gray>' ''sf_count_t sf_write_float (SNDFILE *sndfile, float *ptr, sf_count_t items)'' |
− | ' '' | + | ' ''Function for writing the data chunk in terms of items.''</font> |
− | Private Extern | + | Private Extern sf_write_float(sndfile As Pointer, ptr As Pointer, items As Long) As Long |
<FONT Color=gray>' ''int sf_close (SNDFILE *sndfile)'' | <FONT Color=gray>' ''int sf_close (SNDFILE *sndfile)'' | ||
Riga 43: | Riga 42: | ||
− | + | 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) | |
− | If | + | Error.Raise("Errore: codifica non valida !") |
− | + | Endif | |
− | + | ||
− | + | outfile = sf_open("<FONT Color=darkgreen>/tmp/file.ogg</font>", 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 | ||
<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.Data, buffer.Count) | |
− | |||
− | |||
− | |||
− | |||
Wend | Wend | ||
− | + | ||
− | sf_close( | + | Write "\rConversione terminata !" |
− | sf_close( | + | |
− | + | sf_close(infile) | |
− | + | sf_close(outfile) | |
+ | |||
+ | End |
Versione attuale delle 17:40, 13 gen 2024
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