views:

47

answers:

2

I record audio in .caf format and later need to convert it to .wav in order to send the file as a email attachment. How can I convert the file to wav format in iOS 3.1.x?

The format of the audio:

[NSNumber numberWithInt:kAudioFormatLinearPCM], AVFormatIDKey,
[NSNumber numberWithFloat:11025.0f],            AVSampleRateKey,
[NSNumber numberWithInt:1],                     AVNumberOfChannelsKey,
[NSNumber numberWithInt:16],                    AVLinearPCMBitDepthKey,
[NSNumber numberWithBool:YES],                  AVLinearPCMIsBigEndianKey,
[NSNumber numberWithBool:NO],                   AVLinearPCMIsFloatKey,

I am not tied to this format, I just need to be able to record and playback on the device and email a wav version file.

+1  A: 

The Bad:

I don't think there is any "automatic" way to do this wit the iPhone, meaning you have to do it yourself - manually.

The Good:

It is very very simple to do. A WAV file - assuming your just talking about using raw, uncompressed audio - is just a header you need to stick on the beginning of the audio data. The format is very simple. There are numerous references on the web:

http://www.sonicspot.com/guide/wavefiles.html

The Ugly:

I am not farmiliar with the source format you are talking about. If it is uncompressed, great. If not, you may need to uncompress it. WAV files also have methods to store compressed data, so if your data source is compressed, then you might be able to store it as such in the WAV by generating new headers and indicating it compressed as such.

Also - by "compressed" - I mean in any codec other than standard X-bit Y-Channel PCM.

Brad
It is PCM, see updated question.
zaph
So very easy then. You only have a single audio channel so there's no question of how the multiple channels get packed in. You should just be able to fillout a WAV header structure with the pertinent information and then append your raw audio data. As you know all the info up front - they're all *constants* - the only variable would be the size of the audio clip. Use a "Compression Code" of "1" (PCM/Uncompressed). There are a *billion* references for this on the web - in addition to the one I posted. Use it, or Google "WAV file format" or "WAV file header" if you want another.
Brad
It seems that since Apple has created a .caf container there should be an API to create standard formats such as .wav and. aif without having to munge the raw data.
zaph
A: 

The answer is simple, just init with a .wav extension.

The answer was provided by Paul Bruneau on the Apple Developer forum:

From the class reference docs, the recorder infers the file type from the extension that you choose. So try .aiff or .aif or .wav

initWithURL:settings:error: Initializes and returns an audio recorder.

- (id)initWithURL:(NSURL *)url
settings:(NSDictionary *)settings
error:(NSError **)outError

Parameters: url

The file system location to record to. The file type to record to is inferred from the file extension included in this parameter’s value.

zaph