I'm trying to make conversion from wav file to mp3 using Samplerate 16000. After converting the quality of the audio is not good and its speed seems like an increased. Where as it worked well if the audio conversion sample rate set to 8000. I have created the method for Wav to mp3:
+(void)convertFromWavToMp3:(NSString *)filePath FileName:(NSString *)kFileName SampleRate:(int)kSampleRate failAction:(WavToMp3CompletionBlock)failBlock successAction:(WavToMp3CompletionBlock)successBlock
{
NSLog(@"%@", [filePath stringByDeletingLastPathComponent]);
NSString *mp3FileName = kFileName;
mp3FileName = [mp3FileName stringByAppendingString:@".mp3"];
NSString *mp3FilePath = [[filePath stringByDeletingLastPathComponent] stringByAppendingPathComponent:mp3FileName];
NSLog(@"%@", mp3FilePath);
@try {
int read, write;
NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];
char const *path_cstr = [filemgr fileSystemRepresentationWithPath:filePath];
FILE *pcm = fopen(path_cstr, "rb"); //source
fseek(pcm, 4*1024, SEEK_CUR); //skip file header
FILE *mp3 = fopen([mp3FilePath cStringUsingEncoding:1], "wb"); //output
const int PCM_SIZE = 16384;
const int MP3_SIZE = 16384;
short int pcm_buffer[PCM_SIZE*2];
unsigned char mp3_buffer[MP3_SIZE];
lame_t lame = lame_init();
lame_set_in_samplerate(lame, 8000);
lame_set_num_channels(lame, 1);
lame_set_VBR(lame, vbr_default);
// lame_set_brate(lame, 128);
lame_set_quality(lame, 0);
lame_set_brate(lame, 128);
lame_set_num_samples(lame, 8000);
lame_set_out_samplerate(lame, 8000);
lame_set_mode(lame, 3);
lame_init_params(lame);
lame_print_config(lame);
do {
read = fread(pcm_buffer, 2*sizeof(short int), PCM_SIZE, pcm);
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);
fwrite(mp3_buffer, write, 1, mp3);
} while (read != 0);
lame_close(lame);
fclose(mp3);
fclose(pcm);
successBlock(true);
}
@catch (NSException *exception) {
NSLog(@"%@",[exception description]);
failBlock(false);
}
@finally {
}