How can I Access/extract raw(headless) audio data from a caf file on iPhone? - iphone

I am working on a project that requires me to send raw headerless ulaw or linear pcm data to a server.
I am using the AVAudioRecorder and I can save out audio files to the phone quite easily, but the problem is that it no matter what I try and do, they get wrapped in a caf file or stamped with some kind of header.
It's my understanding that a caf file is just a wrapper file for raw audio data and that this audio data can be in various formats including linear pcm and ulaw.
So my questions are, how can I generate a raw audio file without the caf wrapper to begin with (ie just the headerless raw audio stuffed into a file) or if I record a caf that contains the raw Ulaw or linear pcm audio data, how can I easily go in and extract just the raw audio data from the caf file and leave everything else behind.

If the audio data in the CAF is already in the format you need it in, I'd use the AudioFile API to grab the caf file's audio data and write them to a headerless/RAW file before uploading to my server.
If you need to convert the CAF's data, use the ExtAudioFile API instead. Either way, pretty straightforward.
http://developer.apple.com/library/ios/#documentation/MusicAudio/Reference/AudioFileConvertRef/Reference/reference.html
http://developer.apple.com/library/ios/#documentation/MusicAudio/Reference/ExtendedAudioFileServicesReference/Reference/reference.html

If you use the Audio Queue API for recording, instead of AVAudioRecorder, you will have access to raw PCM sample buffers, and can write those out to a flat file for later sending to your server.

Related

Storing web audio processed data from a custom worklet to turn into wav file

I was wondering if there was a way to store the data in a custom audio worklet for further processing on the client side, ie turning it into a WAV file? I've seen that it's possible to output an audio stream to a MediaRecorder, but that results in the creation of lossy audio via the ogg codec. If possible, I would like access to the raw PCM data from the worklet processor so I can encode it as WAV or another lossless format.
My hunch is that this can be accomplished by attaching something to the global audio scope and retrieving it from the audio context, but I'm not sure. Help would be appreciated!
Answering own question, I see that there is now the possibility to use PCM as a codec, ie in https://github.com/muaz-khan/RecordRTC/. This is unfortunately undocumented in most major web audio documentation, but as it exists in several modern browsers, it is good enough for my needs!

iOS: Record audio in other format than caf

I am working on audio recording. I am able to record my audio in caf (Core audio format). Now I want to record sound in .mp3 or .amr.
I tried a lot of google but didn't found anything. I am not sure if my steps following work:
Write that Audio with .caf extention to document dir
Read data from that file
write the file again with .mp3 or .amr extention
I have 100% doubt that this will not going to work as this will just re save file with some other extension. This won't change audio codec.
I thought that for converting any format whether it is audio or video we need to change its codecs and data sequence.
Please also suggest me any player which don't play .caf files not anything else. So I can make sure that my files are converted successfully.
UPDATE Please also let me know that the way I am re-writing data with different extension is correct in the way I am needed?
You might be interested in the iPhoneExtAudioFileConvertTest example.
The MP3 format is however not available due to patents, maybe you can use LAME.
From the Document The Basics: Audio Codecs, Supported Audio Formats, and Audio Sessions

How to convert a wav or caf audio file on iPhone

I am working on a iPhone IM app, which support audio message. I've tried caf and wav format follow "Speak Here", but the files are too large to be send through the internet. So I'm wondering if I can convert any of them to some small format, like mp3 or amr.
Thank you for you help in advance.
CoreAudio has som great built-in datatypes for just this Core Audio Data Types
Use iLBC, short for Internet Low Bitrate Codec, to record your audio. Limit your sample rate to 8kHz.
If you insist on mp3, use LAME - the open source MP3 encoder library to convert your audio.
After you've recorded the audio file, the iOS AVFoundation AVAsset Reader/Writer APIs can be used to convert the file by, say, reading a .wav and writing AAC or other compressed file type.

Which one of these is better for short audio input in iPhone- .caf or .wav?

I am making a simple application for iPhone, and I want to enter a short audio file on an object click. Which of .caf and .wav would be better?
I am building a simple application in Cocos2d in which balloons produce a pop sound when clicked. What are the memory issues with both sound versions?
If you do not need specific Core Audio Format features, then WAV has more universal support (and it would be my default choice for that reason).
Core Audio Format basically functions as a container for other audio file formats, including WAV. Core Audio Format has many great features, but it's not evident from the description that you need any of these.
In response to a deleted comment, which was moved to the question:
I can't speak for Cocos2d specifically, so I will write about the file formats in general: WAV does not use data compression. CAF may. If it is a short sound file, you probably don't want data compression (because it requires a good amount of processing to convert to LPCM for playback). If you play the pop often, then you will want to hold onto an uncompressed version of the audio data for easy processing. 1 second will require 44100 * 2 bytes at CD quality in memory (per channel).
For a short sound file such as a balloon pop, a 16 bit WAV file sounds ideal. In that sense, the memory difference should not be a deciding factor. If you have a lot of audio files, or long audio files to load into memory, then the situation changes. For now, I don't consider memory to be a problem in your case. Since CAF is a container, its uncompressed representation will be nearly identical (the difference will be a little more header data in the CAF).
A CAF file is a basically Core Audio Format. So it is well suited for the Apple frameworks. The best advantage of CAF over WAV is while recording when you can have files more than 4 GB and also in CAF you don't need to update the WAV header after each packet recording.
Anyway, I assume you don't need these features related to CAF. And as Justin said, I do believe that WAV will be the better option as you can have more support for WAV than the CAF format.

Recording sound as WAV on iphone

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
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
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?