I am trying to convert Linear PCM audio file (.wav) to MP3 using LAME for my iOS app. I am successfully able to do it except for one issue , the created MP3 file turns out to be smaller than the orignal .wav file. For 30 seconds wav file , created MP3 file was of 27 seconds and audio of last 3 seconds was chopped off. For 5 mins audio about 30 seconds of audio was chopped off in resulting MP3.
I am not really sure what is causing the issue , i have tried changing around few things but nothing worked. Can somebody help me out and push me towards the right direction?
Here is the code I am using for this.
int read, write;
FILE *pcm = fopen([mergeFile cStringUsingEncoding:1], "rb"); //source
fseek(pcm, 4*1024, SEEK_CUR); //skip file header
FILE *mp3 = fopen([mp3FilePath cStringUsingEncoding:1], "wb"); //output
const int PCM_SIZE = 8192;
const int MP3_SIZE = 8192;
short int pcm_buffer[PCM_SIZE*2];
unsigned char mp3_buffer[MP3_SIZE];
lame_t lame = lame_init();
lame_set_in_samplerate(lame, 44100);
lame_set_VBR(lame, vbr_default);
lame_init_params(lame);
do {
read = fread(pcm_buffer, 2*sizeof(short int), PCM_SIZE, pcm);
NSLog(@"");
if (read == 0)
write = lame_encode_flush(lame, mp3_buffer, MP3_SIZE);
else
write = lame_encode_buffer_interleaved(lame, pcm_buffer, read, mp3_buffer, MP3_SIZE);
} while (read != 0);
lame_close(lame);
fclose(mp3);
fclose(pcm);
I did same task and It's working fine for me.. The only differance I am seeing in above code is You have to replace lame_set_VBR(lame, vbr_default);
this statement with lame_set_VBR(lame, vbr_off);
This should work fine. Please check with this change.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With