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);