ios - How to convert wav to mp3 for samplerate 16000? -
i'm trying make conversion wav file mp3 using samplerate 16000. after converting quality of audio not , speed seems increased. worked if audio conversion sample rate set 8000. have created method wav 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); { 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 { }
for converting wave mp3 use can use extaudiofileconverter class : https://github.com/lixing123/extaudiofileconverter
for converting mp3 sample rate 16000:
extaudioconverter* converter = [[extaudioconverter alloc] init]; converter.outputsamplerate = 16000; converter.outputnumberchannels = 2; converter.outputbitdepth = bitdepth_16; converter.outputformatid = kaudioformatlinearpcm; converter.outputfiletype = kaudiofilemp3type; converter.inputfile = @"desktop/input.wav"; converter.outputfile = @"desktop/output.mp3"; [converter convert];
Comments
Post a Comment