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.
 
(12 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.28''"
+
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:
Private Const LUN_BUFF As Long = 4096
+
  Library "libsndfile:1.0.37"
 
  Library "libsndfile"
 
 
   
 
   
 
  Public Struct SF_INFO
 
  Public Struct SF_INFO
Riga 12: Riga 10:
 
   samplerate As Integer
 
   samplerate As Integer
 
   channels As Integer
 
   channels As Integer
   _format As Integer
+
   format_ As Integer
 
   sections As Integer
 
   sections As Integer
 
   seekable As Integer
 
   seekable As Integer
 
  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
Private Const SF_FORMAT_VORBIS As Integer = &60
 
 
   
 
   
 
  <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 sf_readf_short (SNDFILE *sndfile, short *ptr, sf_count_t frames)''
+
  <FONT Color=gray>' ''sf_count_t int sf_format_check (const SF_INFO *info)''
  ' ''Function for reading the data chunk in terms of frames.''</font>
+
  ' ''Return TRUE if fields of the SF_INFO struct are a valid combination of values.''</font>
  Private Extern sf_readf_short(sndfile As Pointer, ptr As Pointer, frames As Long) As Long
+
  Private Extern sf_format_check(info As SF_INFO) As Integer
 
   
 
   
  <FONT Color=gray>' ''sf_count_t sf_writef_short (SNDFILE *sndfile, const short *ptr, sf_count_t frames)''
+
  <FONT Color=gray>' ''sf_count_t sf_read_float (SNDFILE *sndfile, float *ptr, sf_count_t items)''
  ' ''Function for writing the data chunk in terms of frames.''</font>
+
  ' ''Function for reading the data chunk in terms of items.''</font>
  Private Extern sf_writef_short(sndfile As Pointer, ptr As Pointer, frames As Long) As Long
+
  Private Extern sf_read_float(sndfile As Pointer, ptr As Pointer, items As Long) As Long
 
   
 
   
  <FONT Color=gray>' ''const char* sf_strerror (SNDFILE *sndfile)''
+
  <FONT Color=gray>' ''sf_count_t sf_write_float (SNDFILE *sndfile, float *ptr, sf_count_t items)''
  ' ''Returns to the caller a pointer to the current error message for the given SNDFILE.''</font>
+
  ' ''Function for writing the data chunk in terms of items.''</font>
  Private Extern sf_strerror(sndfile As Pointer) As String
+
  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()
+
  Public Sub Main()
 
+
   Dim sfiW, sfiO As SF_INFO  <FONT Color=gray>' ''Si creano due distinti oggetti struttura "SF_INFO" per il file WAV e il file OGG-Vorbis''</font>
+
   Dim buffer As New Single[4096]
   Dim fen, fex As Pointer
+
   Dim infile, outfile As Pointer
   Dim buffer As Short[]
+
   Dim sfinfo As New SF_INFO
   Dim frm As Long
+
   Dim fileorigine As String
 
+
   sfiW = New SF_INFO
+
   fileorigine = "<FONT Color=darkgreen>''/percorso/del/file.wav''</font>"
  fen = sf_open("<FONT Color=gray>''/percorso/del/file.wav''</font>", SFM_READ, sfiW)
+
   If fen = 0 Then Error.Raise("Errore: " & sf_strerror(fen))
+
  infile = sf_open(fileorigine, SFM_READ, sfinfo)
 
+
   If infile == 0 Then Error.Raise("Impossibile aprire il file audio in entrata !")
  <FONT Color=gray>' ''Si determinano le impostazioni necessarie ed essenziali''
+
' ''per il nuovo file audio OGG-Vorbis nella Struttura "SF_INFO":''</font>
+
  <FONT Color=gray>' ''Imposta le caratteristiche del file OGG:''</font>"
   With sfiO = New SF_INFO
+
   With sfinfo
 
     .samplerate = 44100
 
     .samplerate = 44100
 
     .channels = 2
 
     .channels = 2
     ._format = SF_FORMAT_OGG Or SF_FORMAT_VORBIS
+
     .format_ = SF_FORMAT_VORBIS Or SF_FORMAT_OGG
 
   End With
 
   End With
    
+
  <FONT Color=gray>' ''Si crea e si apre in scrittura il nuovo file OGG-Vorbis:''</font>
+
   If sf_format_check(sfinfo) == SF_FALSE Then
   fex = sf_open("<FONT Color=gray>''/percorso/del/file.ogg''</font>", SFM_WRITE, sfiO)
+
    sf_close(infile)
   If fex = 0 Then Error.Raise("Errore: " & sf_strerror(fex))
+
    Error.Raise("Errore: codifica non valida !")
 
+
  Endif
   buffer = New Short[sfiW.frames]
+
   
    
+
   outfile = sf_open("<FONT Color=darkgreen>/tmp/file.ogg</font>", SFM_WRITE, sfinfo)
   frm = sf_readf_short(fen, buffer.Data, LUN_BUFF)
+
   If outfile == 0 Then Error.Raise("Impossibile aprire il file audio in uscita !")
  If frm > LUN_BUFF Then Error.Raise("Errore: " & sf_strerror(fen))
+
 
+
   Write "\e[5mAttendere...\e[0m"
  While frm > 0
+
   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>
     frm = sf_writef_short(fex, buffer.Data, frm)
+
     sf_write_float(outfile, buffer.Data, buffer.Count)
    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
 
   Wend
    
+
   sf_close(fex)
+
   Write "\rConversione terminata !"
   sf_close(fen)
+
 
+
   sf_close(infile)
  '''End'''
+
   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