Convertire un file WAV in formato MP3 con le funzioni esterne del API di Lame

Da Gambas-it.org - Wikipedia.

LAME (LAME Ain't an Mp3 Encoder) è un codificatore MPEG Audio Layer III (mp3) di alta qualità rilasciato sotto licenza LGPL.

Con le risorse dell'API di LAME è possibile realizzare facilmente un semplice applicativo capace di convertire un file audio .wav in un file audio mp3.

Per poter fruire delle risorse di LAME in Gambas, è necessario avere installata e richiamare la libreria condivisa: "libmp3lame.so.0.0.0 ".

Mostriamo un esempio per la conversione di un file wav a 44100 hertz, 16bit, 2 canali, nel quale si utilizzerà anche la libreria libc.so.6:

Library "libmp3lame:0.0.0"

Private Const PCM_SIZE As Short = 8192
Private Const MP3_SIZE As Short = 8192
Private Const vbr_default As Byte = 4
 
' lame_global_flags lame_init()
' Initialize the encoder. sets default for all encoder parameters
Private Extern lame_init() As Pointer

' int lame_set_in_samplerate(lame_global_flags flags, int rate)
' Input sample rate in Hz
Private Extern lame_set_in_samplerate(lame As Pointer, rate As Integer) As Integer

' int lame_set_brate(lame_global_flags flags, int brate)
' Set one of bit-rate compression ratio.
Private Extern lame_set_brate(lame As Pointer, brate As Integer) As Integer

' int lame_set_VBR(lame_global_flags flags, int vbr_mode)
' Types of VBR.
Private Extern lame_set_VBR(lame As Pointer, vbr As Integer) As Integer

' int lame_init_params(lame_global_flags flags)
' Sets more internal configuration based On data provided above.returns - 1 If something failed.
Private Extern lame_init_params(lame As Pointer) As Integer
 
' int lame_encode_buffer_interleaved(lame_global_flags gfp, byte[] pcm, int num_samples, byte[] mp3buf, int mp3buf_size)
' This is a convenience method using byte[] array instead of short[], the byte[] buffer used must be 2x the length of the analogous short[] version.
' Input has L & R channel data interleaved. NOTE: num_samples = number of samples in the L (or R) channel, not the total number of samples in pcm[]
Private Extern lame_encode_buffer_interleaved(lame As Pointer, pcm As Pointer, num_samples As Integer, mp3buf As Pointer, mp3buf_size As Integer) As Integer

' int lame_encode_flush(lame_global_flags gfp, unsigned char* mp3buf, Int size)
' lame_encode_flush will flush the intenal PCM buffers.
' "mp3buf" should be at least 7200 bytes long to hold all possible emitted data.
Private Extern lame_encode_flush(lame As Pointer, mp3buf As Pointer, size As Integer) As Integer
 
' int lame_close(lame_global_flags flags)
' Final call to fFree all remaining buffers.
Private Extern lame_close(lame As Pointer) As Integer


Library "libc:6"

' FILE *fopen (const char *__restrict __filename, const char *__restrict __modes)
' Open a file and create a new stream for it.
Private Extern fopen(filename As String, modes As String) As Pointer

' size_t fread (void *__restrict __ptr, size_t __size, size_t __n, FILE *__restrict __stream)
' Read chunks of generic data from STREAM.
Private Extern fread(ptr As Pointer, size As Long, n As Long, sFl As Pointer) As Long

' size_t fwrite (const void *__restrict __ptr, size_t __size, size_t __n, FILE *__restrict __s)
' Write chunks of generic data to STREAM.
Private Extern fwrite(ptr As Pointer, size As Long, n As Long, sFl As Pointer) As Long

' int fclose (FILE *__stream)
' Close STREAM.
Private Extern fclose(sFl As Pointer) As Integer


Public Sub Main()

 Dim letto, scritto As Long
 Dim pcm, mp3, handle, pcm_buffer, mp3_buffer As Pointer

 pcm_buffer = Alloc(PCM_SIZE, 4)
 mp3_buffer = Alloc(MP3_SIZE)

 pcm = fopen("/percorso/del/file.wav", "rb")
 mp3 = fopen("/percorso/del/file.mp3", "wb")

' La libreria va innanzitutto inizializzata:
 handle = lame_init()

' L'inizializzazione imposta i parametri con valori predefiniti.
' E' possibile modificare tali valori con apposite funzioni del tipo "lame_set_*()":
 lame_set_in_samplerate(handle, 44100)
 lame_set_brate(handle, 128)
 lame_set_VBR(handle, vbr_default)
   
' Dopo aver impostato i parametri, si controlla che tutto sia a posto mediante la funzione:
 lame_init_params(handle)
 
 Do
' Si legge dal file .wav:
   letto = fread(pcm_buffer, 2 * SizeOf(gb.Short), PCM_SIZE, pcm)
   If letto = 0 Then
     scritto = lame_encode_flush(handle, mp3_buffer, MP3_SIZE)
   Else
     scritto = lame_encode_buffer_interleaved(handle, pcm_buffer, letto, mp3_buffer, MP3_SIZE)
' Si scrivono i dati codificati in mpeg nel file di destinazione:
     fwrite(mp3_buffer, scritto, 1, mp3)
   Endif      
 Loop While letto <> 0

' Libera le due aree allocate; chiude l'handle della libreria e i due file:
 Free(pcm_buffer)
 Free(mp3_buffer)
 lame_close(handle)
 fclose(mp3)
 fclose(pcm)

End


Nello stesso esempio potremo ridurre le funzioni a quelle sole strettamente appartenenti all'API di LAME:

Library "libmp3lame:0.0.0"

Private Const vbr_default As Byte = 4

' lame_global_flags lame_init()
' Initialize the encoder. sets default for all encoder parameters
Private Extern lame_init() As Pointer

' int lame_set_in_samplerate(lame_global_flags flags, int rate)
' Input sample rate in Hz
Private Extern lame_set_in_samplerate(lame As Pointer, rate As Integer) As Integer

' int lame_set_brate(lame_global_flags flags, int brate)
' Set one of bit-rate compression ratio.
Private Extern lame_set_brate(lame As Pointer, brate As Integer) As Integer

' int lame_set_VBR(lame_global_flags flags, int vbr_mode)
' Types of VBR.
Private Extern lame_set_VBR(lame As Pointer, vbr As Integer) As Integer

' int lame_init_params(lame_global_flags flags)
' Sets more internal configuration based On data provided above.returns - 1 If something failed.
Private Extern lame_init_params(lame As Pointer) As Integer

' int lame_encode_buffer_interleaved(lame_global_flags gfp, byte[] pcm, int num_samples, byte[] mp3buf, int mp3buf_size)
' This is a convenience method using byte[] array instead of short[], the byte[] buffer used must be 2x the length of the analogous short[] version.
' Input has L & R channel data interleaved. NOTE: num_samples = number of samples in the L (or R) channel, not the total number of samples in pcm[]
Private Extern lame_encode_buffer_interleaved(lame As Pointer, pcm As Pointer, num_samples As Integer, mp3buf As Pointer, mp3buf_size As Integer) As Integer

' int lame_encode_flush(lame_global_flags gfp, unsigned char* mp3buf, Int size)
' lame_encode_flush will flush the intenal PCM buffers.
' "mp3buf" should be at least 7200 bytes long to hold all possible emitted data.
Private Extern lame_encode_flush(lame As Pointer, mp3buf As Pointer, size As Integer) As Integer

' int lame_close(lame_global_flags flags)
' Final call to fFree all remaining buffers.
Private Extern lame_close(lame As Pointer) As Integer
  

Public Sub Main()

 Dim handle As Pointer
 Dim pcm, mp3 As File
 Dim percorsoFile As String
 Dim scritto, campioni, dimensione As Integer
 Dim pcm_buffer, mp3_buffer As Byte[]
 
 percorsoFile = "/percorso/del/file.wav"
  
 pcm = Open percorsoFile For Read
 mp3 = Open "/percorso/del/nuovo/file.mp3" For Create

 dimensione = CInt(Stat(percorsoFile).Size)

 mp3_buffer = New Byte[dimensione]

 campioni = dimensione \ 4

' Il decodificatore necessita di essere inizializzato:
 handle = lame_init()

' L'inizializzazione imposta i parametri con valori predefiniti.
' E' possibile modificare tali valori con apposite funzioni del tipo "lame_set_*()":
 lame_set_in_samplerate(handle, 44100)
 lame_set_brate(handle, 128)
 lame_set_VBR(handle, vbr_default)

' Dopo aver impostato i parametri, si controlla che tutto sia a posto mediante la funzione:
 lame_init_params(handle)

 With pcm_buffer = New Byte[](Lof(pcm))
   .Read(pcm, 0, Lof(pcm))
 End With

 scritto = lame_encode_buffer_interleaved(handle, pcm_buffer.Data, campioni, mp3_buffer.Data, dimensione)

 mp3_buffer.Write(mp3, 0, scritto)

 scritto = lame_encode_flush(handle, mp3_buffer.Data, dimensione)

' Chiude l'handle della libreria e i due file:
 lame_close(handle)
 mp3.Close
 pcm.Close

End


Riferimenti