Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Recorded sound in iphone from one format to another say wav to mp3

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.

  1. 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.

  2. If the answer to question 1 is 'no' then which framework do I need to use to convert the sound between above mentioned formats?

  3. Not related to above 2, but can anybody give me a link to any websites where information about different sound formats (wav,mp3,aac etc ) and their digital representations (cpm,lpcm etc) so that I can understand which uses what.
like image 689
xProgrammer Avatar asked Nov 15 '22 07:11

xProgrammer


1 Answers

Some answers

  1. 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.

  2. 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

  3. 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

like image 131
sean808080 Avatar answered Dec 18 '22 12:12

sean808080