|
|
(12 versioni intermedie di uno stesso utente non sono mostrate) |
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.
| |
− | | |
− | Innanzitutto in Gambas dovremo dichiarare la [http://it.wikipedia.org/wiki/Dynamic-link_library libreria condivisa] .so, contenente le funzioni di ''LAME'', che attualmente è "''libmp3lame.so.0.0.0''":
| |
− | Library "libmp3lame:0.0.0"
| |
− | | |
− | | |
− | Ne mostriamo di seguito un esempio:
| |
− | '''Private''' Const PCM_SIZE As Short = 8192
| |
− | '''Private''' Const MP3_SIZE As Short = 8192
| |
− | '''Private''' Const vbr_default As Byte = 4
| |
− |
| |
− |
| |
− | Library "libmp3lame:0.0.0"
| |
− |
| |
− | <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>' ''size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) --> legge da filStr nmemb elementi, ciascuno di dimensione size.''
| |
− | ' ''Gli elementi letti vengono immagazzinati nel buffer puntato da ptr che deve essere di dimensioni adeguate.''
| |
− | ' ''La funzione fread() ritorna il numero di elementi letti.''</font>
| |
− | '''Private''' Extern fread(ptr As Pointer, size As Integer, nmemb As Integer, filStr As Pointer) As Integer In "libc:6"
| |
− |
| |
− | <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_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>' ''size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) --> La funzione fwrite() scrive su stream nmemb elementi, ciascuno di dimensione size.''
| |
− | ' ''Il primo elemento e' individuato dal puntatore ptr. La funzione fwrite() ritorna il numero di elementi scritti.''</font>
| |
− | '''Private''' Extern fwrite(ptr As Pointer, size As Integer, nmemb As Integer, filStr As Pointer) As Integer In "libc:6"
| |
− |
| |
− | <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
| |
− |
| |
− | <FONT color=gray>' ''FILE *fopen(const char *path, const char *mode) --> La funzione fopen() apre il file path associandolo ad uno stream.''</font>
| |
− | '''Private''' Extern fopen(path As String, mode As String) As Pointer In "libc:6"
| |
− |
| |
− | <FONT color=gray>' ''int fclose(FILE *stream) --> La funzione fclose() chiude il file associato a "stream" durante l'apertura con "fopen()".''</font>
| |
− | '''Private''' Extern fclose(filStr As Pointer) As Integer In "libc:6"
| |
− |
| |
− |
| |
− | '''Public''' Sub Button1_Click()
| |
− |
| |
− | Dim letto, scritto As Integer
| |
− | 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")
| |
− |
| |
− | <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>' ''Chiude l'handle alla libreria e i due file:''</font>
| |
− | lame_close(handle)
| |
− | fclose(mp3)
| |
− | fclose(pcm)
| |
− |
| |
− | '''End'''
| |
− | | |
− | | |
− | | |
− | | |
− | =Riferimenti=
| |
− | * Il sito dell'[http://openinnowhere.sourceforge.net/lameonj/javadoc/lame/std/Lame.html#Lame%28%29 API di LAME]
| |