|
|
Riga 1: |
Riga 1: |
− | '''LAME''' (''LAME Ain't an Mp3 Encoder'') è un codificatore MPEG Audio Layer III (mp3) di alta qualità rilasciato sotto licenza LGPL.
| |
| | | |
− | | + | #REDIRECT [[Convertire_un_file_WAV_in_formato_MP3_con_le_funzioni_esterne_del_API_di_Lame]] |
− | =Convertire un file .wav in file mp3=
| |
− | Con le risorse dell'[http://it.wikipedia.org/wiki/Application_programming_interface 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 dinamica 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
| |
− |
| |
− | <FONT color=gray>' ''lame_global_flags lame_init()''
| |
− | ' ''Initialize the encoder. sets default for all encoder parameters''</font>
| |
− | Private Extern lame_init() As Pointer
| |
− |
| |
− | <FONT color=gray>' ''int lame_set_in_samplerate(lame_global_flags flags, int rate)''
| |
− | ' ''Input sample rate in Hz''</font>
| |
− | Private Extern lame_set_in_samplerate(lame As Pointer, rate As Integer) As Integer
| |
− |
| |
− | <FONT color=gray>' ''int lame_set_brate(lame_global_flags flags, int brate)''
| |
− | ' ''Set one of bit-rate compression ratio.''</font>
| |
− | Private Extern lame_set_brate(lame As Pointer, brate As Integer) As Integer
| |
− |
| |
− | <FONT color=gray>' ''int lame_set_VBR(lame_global_flags flags, int vbr_mode)''
| |
− | ' ''Types of VBR.''</font>
| |
− | Private Extern lame_set_VBR(lame As Pointer, vbr As Integer) As Integer
| |
− |
| |
− | <FONT color=gray>' ''int lame_init_params(lame_global_flags flags)''
| |
− | ' ''Sets more internal configuration based On data provided above.returns - 1 If something failed.''</font>
| |
− | Private Extern lame_init_params(lame As Pointer) As Integer
| |
− |
| |
− | <FONT color=gray>' ''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[]''</font>
| |
− | 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
| |
− |
| |
− | <FONT color=gray>' ''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.''</font>
| |
− | Private Extern lame_encode_flush(lame As Pointer, mp3buf As Pointer, size As Integer) As Integer
| |
− |
| |
− | <FONT color=gray>' ''int lame_close(lame_global_flags flags)''
| |
− | ' ''Final call to fFree all remaining buffers.''</font>
| |
− | Private Extern lame_close(lame As Pointer) As Integer
| |
− |
| |
− |
| |
− | Library "libc:6"
| |
− |
| |
− | <FONT color=gray>' ''FILE *fopen (const char *__restrict __filename, const char *__restrict __modes)''
| |
− | ' ''Open a file and create a new stream for it.''</font>
| |
− | Private Extern fopen(filename As String, modes As String) As Pointer
| |
− |
| |
− | <FONT color=gray>' ''size_t fread (void *__restrict __ptr, size_t __size, size_t __n, FILE *__restrict __stream)''
| |
− | ' ''Read chunks of generic data from STREAM.''</font>
| |
− | Private Extern fread(ptr As Pointer, size As Long, n As Long, sFl As Pointer) As Long
| |
− |
| |
− | <FONT color=gray>' ''size_t fwrite (const void *__restrict __ptr, size_t __size, size_t __n, FILE *__restrict __s)''
| |
− | ' ''Write chunks of generic data to STREAM.''</font>
| |
− | Private Extern fwrite(ptr As Pointer, size As Long, n As Long, sFl As Pointer) As Long
| |
− |
| |
− | <FONT color=gray>' ''int fclose (FILE *__stream)''
| |
− | ' ''Close STREAM.''</font>
| |
− | 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("<FONT color=gray>''/percorso/del/file.wav''</font>", "rb")
| |
− | mp3 = fopen("<FONT color=gray>''/percorso/del/file.mp3''</font>", "wb")
| |
− |
| |
− | <FONT color=gray>' ''La libreria va innanzitutto inizializzata:''</font>
| |
− | handle = lame_init()
| |
− |
| |
− | <FONT color=gray>' ''L'inizializzazione imposta i parametri con valori predefiniti.''
| |
− | ' ''E' possibile modificare tali valori con apposite funzioni del tipo "lame_set_*()":''</font>
| |
− | lame_set_in_samplerate(handle, 44100)
| |
− | lame_set_brate(handle, 128)
| |
− | lame_set_VBR(handle, vbr_default)
| |
− |
| |
− | <FONT color=gray>' ''Dopo aver impostato i parametri, si controlla che tutto sia a posto mediante la funzione:''</font>
| |
− | lame_init_params(handle)
| |
− |
| |
− | Do
| |
− |
| |
− | <FONT color=gray>' ''Si legge dal file .wav:''</font>
| |
− | 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)
| |
− | <FONT color=gray>' ''Si scrivono i dati codificati in mpeg nel file di destinazione:''</font>
| |
− | fwrite(mp3_buffer, scritto, 1, mp3)
| |
− | Endif
| |
− |
| |
− | Loop While letto <> 0
| |
− |
| |
− | <FONT color=gray>' ''Libera le due aree allocate; chiude l'handle della libreria e i due file:''</font>
| |
− | 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
| |
− |
| |
− | <FONT color=gray>' ''lame_global_flags lame_init()''
| |
− | ' ''Initialize the encoder. sets default for all encoder parameters''</font>
| |
− | Private Extern lame_init() As Pointer
| |
− |
| |
− | <FONT color=gray>' ''int lame_set_in_samplerate(lame_global_flags flags, int rate)''
| |
− | ' ''Input sample rate in Hz''</font>
| |
− | Private Extern lame_set_in_samplerate(lame As Pointer, rate As Integer) As Integer
| |
− |
| |
− | <FONT color=gray>' ''int lame_set_brate(lame_global_flags flags, int brate)''
| |
− | ' ''Set one of bit-rate compression ratio.''</font>
| |
− | Private Extern lame_set_brate(lame As Pointer, brate As Integer) As Integer
| |
− |
| |
− | <FONT color=gray>' ''int lame_set_VBR(lame_global_flags flags, int vbr_mode)''
| |
− | ' ''Types of VBR.''</font>
| |
− | Private Extern lame_set_VBR(lame As Pointer, vbr As Integer) As Integer
| |
− |
| |
− | <FONT color=gray>' ''int lame_init_params(lame_global_flags flags)''
| |
− | ' ''Sets more internal configuration based On data provided above.returns - 1 If something failed.''</font>
| |
− | Private Extern lame_init_params(lame As Pointer) As Integer
| |
− |
| |
− | <FONT color=gray>' ''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[]''</font>
| |
− | 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
| |
− |
| |
− | <FONT color=gray>' ''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.''</font>
| |
− | Private Extern lame_encode_flush(lame As Pointer, mp3buf As Pointer, size As Integer) As Integer
| |
− |
| |
− | <FONT color=gray>' ''int lame_close(lame_global_flags flags)''
| |
− | ' ''Final call to fFree all remaining buffers.''</font>
| |
− | 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 = "<FONT color=gray>''/percorso/del/file.wav''</font>"
| |
− |
| |
− | pcm = Open percorsoFile For Read
| |
− | mp3 = Open "<FONT color=gray>''/percorso/del/nuovo/file.mp3''</font>" For Create
| |
− |
| |
− | dimensione = CInt(Stat(percorsoFile).Size)
| |
− |
| |
− | mp3_buffer = New Byte[dimensione]
| |
− |
| |
− | campioni = dimensione \ 4
| |
− |
| |
− | <FONT color=gray>' ''Il decodificatore necessita di essere inizializzato:''</font>
| |
− | handle = lame_init()
| |
− |
| |
− | <FONT color=gray>' ''L'inizializzazione imposta i parametri con valori predefiniti.''
| |
− | ' ''E' possibile modificare tali valori con apposite funzioni del tipo "lame_set_*()":''</font>
| |
− | lame_set_in_samplerate(handle, 44100)
| |
− | lame_set_brate(handle, 128)
| |
− | lame_set_VBR(handle, vbr_default)
| |
− |
| |
− | <FONT color=gray>' ''Dopo aver impostato i parametri, si controlla che tutto sia a posto mediante la funzione:''</font>
| |
− | 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, campioni, mp3_buffer, dimensione)
| |
− |
| |
− | mp3_buffer.Write(mp3, 0, scritto)
| |
− |
| |
− | scritto = lame_encode_flush(handle, mp3_buffer, dimensione)
| |
− |
| |
− | <FONT color=gray>' ''Chiude l'handle della libreria e i due file:''</font>
| |
− | lame_close(handle)
| |
− | mp3.Close
| |
− | pcm.Close
| |
− |
| |
− | '''End'''
| |
− | | |
− | | |
− | | |
− | =Riferimenti=
| |
− | * http://lame.sourceforge.net/
| |
− | * http://openinnowhere.sourceforge.net/lameonj/javadoc/lame/std/Lame.html#Lame%28%29
| |