14

I am making an iPhone recording app that needs to submit the sound file as a .wav to an external server.

Starting from the SpeakHere example, I am able to record sound as a file, but only as .caf

Does anyone know how to record it as a wav instead? Or how to convert from .caf to .wav on the iphone? (The conversion must happen on the phone)

EDIT:

I'm wondering if anything can be done with using kAudioFileWAVEType instead of kAudioFileCAFType in AudioFileCreateWithURL

Eric Brotto
  • 53,471
  • 32
  • 129
  • 174
matt
  • 1,895
  • 5
  • 19
  • 26

2 Answers2

20

Yup, the key is kAudioFileWAVEType

AudioFileCreateWithURL (
            audioFileURL,
            kAudioFileWAVEType,
            &audioFormat,
            kAudioFileFlags_EraseFile,
            &audioFileID
        );

Changing to WAVE from CAF in the SpeakHere example causes the recording to be as wav. I also edit the same thing in the playback class, and changed the filename to be Recording.wav

matt
  • 1,895
  • 5
  • 19
  • 26
  • Be carefully. AVAudioFileTypeKey with kAudioFileWAVEType is only available on iOS 11.0 or newer – CFIFok Mar 05 '18 at 06:30
2

Have a look at libsndfile. It is a well used (including by Audacity) C library for working with lots of file formats. It supports read and write of a variety of CAF and WAV formats.

The CAF File structure and WAV format are fairly similar. If the worst comes to the worst, converting shouldn't be too hard.

It would involve taking the Audio data chunk, and copying as is into the WAV file, and using the information in the Audio Description Chunk to add an equivalent fmt subchunk for the WAV file. It is fairly simple byte copying.

However, be aware (as Eric pointed out) there are licensing issues, see: Can the Libsndfile library be used on the iPhone iOS?

Community
  • 1
  • 1
Nick Fortescue
  • 43,045
  • 26
  • 106
  • 134
  • 1
    Thanks. I used the WAV format link to confirm that the recording was working. I was having a problem sending data to my server. – matt Feb 23 '09 at 21:51
  • 1
    I voted this down because the libsndfile can't be used on the iPhone: http://stackoverflow.com/questions/4939268/can-the-libsndfile-library-be-used-on-the-iphone-ios – Eric Brotto Feb 10 '11 at 14:40
  • Thanks for the comment. You can still use it by linking statically into your code (or at least the answer you mention says so), but that is definitely worth mentioning in the answer here. I'll add it – Nick Fortescue Feb 14 '11 at 18:20
  • 1
    Actually, you can't statically link it either, unless you provide object files or source code to end-users. – damian May 20 '11 at 16:36