導(dǎo)入lame
gitbub地址:
https://github.com/hedgehogIda/caf-mp3
在錄制caf文件時咆蒿,需要使用雙通道届搁,否則在轉(zhuǎn)換為MP3格式時搔课,聲音不對。caf錄制端的設(shè)置為:
NSMutableDictionary * recordSetting = [NSMutableDictionary dictionary];
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];//
[recordSetting setValue:[NSNumber numberWithFloat:8000.0] forKey:AVSampleRateKey];//采樣率
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];//聲音通道遂铡,這里必須為雙通道
[recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityLow] forKey:AVEncoderAudioQualityKey];//音頻質(zhì)量
在轉(zhuǎn)換mp3端的代碼為:
NSString *cafFilePath = destPath; //caf文件路徑
NSString *mp3FilePath = mp3FilePath;//存儲mp3文件的路徑
@try {
int read, write;
FILE *pcm = fopen([destPath cStringUsingEncoding:1], "rb"); //source 被轉(zhuǎn)換的音頻文件位置
fseek(pcm, 4*1024, SEEK_CUR); //skip file header
FILE *mp3 = fopen([mp3FilePath cStringUsingEncoding:1], "wb"); //output 輸出生成的Mp3文件位置
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, 11025.0);
lame_set_VBR(lame, vbr_default);
lame_init_params(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);
}
@catch (NSException *exception) {
NSLog(@"%@",[exception description]);
}
@finally {
}
補充
先說明一點肮疗,默認AVAudioRecorder錄制后的格式是.caf,而大部分的播放器都是不支持這個格式的忧便,下面一段設(shè)置是可以讓錄制格式是wav的格式kAudioFormatLinearPCM],AVFormatIDKey
#錄制
NSDictionary *recordSetting = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0],AVSampleRateKey, //采樣率
[NSNumber numberWithInt: kAudioFormatLinearPCM],AVFormatIDKey,
[NSNumber numberWithInt:16],AVLinearPCMBitDepthKey,//采樣位數(shù)
默認 16
[NSNumber numberWithInt: 2], AVNumberOfChannelsKey,//通道的數(shù)目
[NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey,//大端還是小端
是內(nèi)存的組織方式
[NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,nil];//采樣信號是整數(shù)還是浮點數(shù)
_recordedFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:@"RecordedFile"]];//文件存儲位置
NSError* error;
_recorder = [[AVAudioRecorder alloc] initWithURL:_recordedFile settings:settings error:&error];//設(shè)置AVAudioRecorder
#轉(zhuǎn)為Mp3
- (void) toMp3
{
NSString *cafFilePath =[NSTemporaryDirectory() stringByAppendingString:@"RecordedFile"];
NSString *mp3FileName = @"Mp3File";
mp3FileName = [mp3FileName stringByAppendingString:@".mp3"];
NSString *mp3FilePath = [[NSHomeDirectory() stringByAppendingFormat:@"/Documents/"] stringByAppendingPathComponent:mp3FileName];
@try {
int read, write;
FILE *pcm = fopen([cafFilePath 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, _sampleRate);
lame_set_VBR(lame, vbr_default);
lame_init_params(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);
}
@catch (NSException *exception) {
NSLog(@"%@",[exception description]);
}
@finally {
[self performSelectorOnMainThread:@selector(convertMp3Finish)
withObject:nil
waitUntilDone:YES];
}
}