I am trying to record some audio and convert them to other sound formats. I am using AVAudioRecorder class to record and these are the recording settings I used..
NSDictionary *recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];//kAudioFormatMicrosoftGSM,kAudioFormatLinearPCM
[recordSetting setValue:[NSNumber numberWithFloat:8000] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
[recordSetting setValue :[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
[recordSetting setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];
Recording is working beautifully. Now I want to convert this sound file to mp3 format. Can I do this with AudioToolBox framework. I tried this
AudioStreamBasicDescription sourceFormat,destinationFormat;
//Setting up source Format setting..here wav
sourceFormat.mSampleRate = 8000.0;
sourceFormat.mFormatID = kAudioFormatLinearPCM;
sourceFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger ;
sourceFormat.mBytesPerPacket = 4;
sourceFormat.mFramesPerPacket = 1;
sourceFormat.mBytesPerFrame = 4;
sourceFormat.mChannelsPerFrame = 2;
sourceFormat.mBitsPerChannel = 16;
destinationFormat.mSampleRate = 8000.0;
destinationFormat.mFormatID = kAudioFormatMPEGLayer3;
destinationFormat.mFormatFlags = 0;
destinationFormat.mBytesPerPacket = 4;
destinationFormat.mFramesPerPacket = 1;
destinationFormat.mBytesPerFrame = 4;
destinationFormat.mChannelsPerFrame = 2;
destinationFormat.mBitsPerChannel = 16;
OSStatus returnCode = AudioConverterNew(&sourceFormat,&destinationFormat,&converter);
if (returnCode) {
NSLog(@"error getting converter : %d",returnCode);
return;
}
The function AudioConverterNew() is giving me error kAudioConverterErr_FormatNotSupported ('fmt'). I tried with different mFormatID and mFormatFlag combinations. But whenever one of the operant (source or destination) is mp3 I am getting this error. Now please help me with these questions.
Can we use AudioToolbox framework and functions to convert sounds between compressed and non compressed format (At present I want to convert between .wav and .mp3). In the AudioConverterNew documentation they are saying "Encoding and decoding between linear PCM and compressed formats is supported". But they are not specifically saying which compressed formats.
If the answer to question 1 is 'no' then which framework do I need to use to convert the sound between above mentioned formats?
Some answers
Yes, you can use the AudioToolbox framework to convert sounds except in the case of .mp3 format. Due to copyright issues, mp3 is not available as a target format within the framework.
If you want to convert to mp3, you'll likely need to use the LAME libraries: http://lame.cvs.sourceforge.net/viewvc/lame/lame/USAGE
Here's one for the .mp3 spec: http://mpgedit.org/mpgedit/mpeg_format/MP3Format.html The other formats you can find pretty easily via web search
Hope that helps
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