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

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.

Related

Can we slice an audio file(mp3 or any) into segments(parts of smaller size) programmatically in Xcode

I have a sound file which requires to be split into smaller parts. Is there any way i can do it, dividing an audio file into small slices of sound as Music Cutter does.
I also need to check that sliced parts can be played individually.
Any help will be appreciated.
Thanks
Vikas Ojha
It depends on the specific format of the file - an mp3 file, no - a wav file, yes.
For an mp3 file you would have to decompress the audio from the file first (you could use an audioqueue ), then split the raw audio data how ever you please, then re-encode each slice as mp3. Apple has sample code for decoding and encoding mp3's.
This is still not going to enable you to accept any input file format, for that you would need other external libraries, such as ffmpeg, LAME, etc. you might have to build these yourself and they may have disagreeable licensing restrictions.

Creating video file from images and audio( pre-recorded )

I have come across some sample codes where set of images are added to make a QTmovie.
I am targeting this for OS X platform without any QT frameworks.
I have ague idea of creating a file with extension and embed it with appropriate metadata and find a way to insert images and audio in required format. So when the file is created it can simply be played.
I am not sure of what format/extension is better.
pointers are much appreciated.
Without QuickTime (or an equivalent multimedia framework), what you describe is quite a lot of work. Ordinarily, you would use a video compression algorithm (such as H.264) to encode your images into video, and an audio compression algorithm (such as AAC) to encode your audio track. Then you would write these streams into a container file, such as an MPEG-4 file, which interleaves the streams for playback, contains metadata and indexes and so on. Then for playback, you parse the file, decode the video and audio data, and schedule them for playback, taking care to keep them in sync.
QuickTime does all this (and more) for you, and it would be an enormous undertaking to write it all yourself. Is there some reason why you are running on OS X but cannot use QuickTime?
Given the question is tagged with iPhone, why can't you just use QTKit?
If you had to do it from scratch, you could adopt a very simple solution whereby you store your image sequence as a set of JPEG files (but then you would require libjpeg; use raw RGB or PPM if you must), the audio track as a raw WAV data, and then have another file (a text file you define) that stored timing information, so you would simply stream out the audio, and have the frame numbers of the images stored with their corresponding timecode/sample offset. That is a very simple solution that could be made to work without too much effort.
If you give us some more idea of what you are trying to achieve, we could offer some more specific suggestions.
If you want to write a program to do this, you could use Xuggler in Java to do it. It will allow you to save your final video in a format playable by almost any media player.
Start out by gaining an understanding of how video files (e.g. MP4, Quicktime) actually represent audio and video with this Overly Simplistic Guide to Internet Video.
Then, play around with the MediaTool tutorials. You can write programs that make raw images into video files (see this sample code). Finally, to write a program that makes audio and video that are in sync, see this tutorial; it generates a set of images, and makes some audio noise that is timed to change when a ball hits the edge of a box.
Hope that helps.
Art

Which audio format would be the best?

I was using AVAudioPlayer to play multiple audio clips back to back but there was always a small silence between tracks and then i came to know of Finch, a library which uses OpenAL to play audio. with this the silence problem seems to be solved theoretically but then i found that it doesn't play m4a or any other compressed formats.
Now i am looking for an uncompressed audio format which would have relatively less file size (though uncompressed means that all of them should have almost same size) and a method to convert, i am also googling on afconvert in a mean while.
CAF files work great for this. I've built an application that loops audio files, and I was impressed with the relatively small file size.
Check out this question for more info on converting to CAF.

How can I reduce the size of .wav audio files for iPhone apps?

I have some music that loops. The .wav file size is about 8 meg. I load this and just loop it... everything was working fine until I added another 4 meg .wav file. Now the game crashes... removing the additional audio file fixed the crashing.
So how can I reduce the size of these .wav files? I thought about releasing the memory after I'm done with the 4 meg file but what I tried didn't work (and I'd rather the game itself be a smaller file size so it's easier to download).
Thanks!
If you're only playing one sound file at a time, just use mp3 or aac. That way you also get hardware decoding, for improved performance and battery life.
Unfortunately, the iPhone can only play one hardware decoded sound file at a time. So if you're looking to ever play more than one at once, you'll need to do your own decoding of the second file; IMA 4:1 is recommended, though you'll have to find or implement your own decoder, Apple doesn't give you one.
This is all based off of this blog post, which goes into a bit more detail.
Actually .WAV is a container. The contents can be compressed or uncompressed, it all depends on the WAVEFORMATEX structure contained in the first "fmt " tag in the .WAV file.
For instance, in Windows 7 all of the built-in sounds are .WAV files that contain MP3 data.
You can just author your .WAV files as MP3 files and (assuming that the iPhone correctly handles the .WAV container) they should work.
Wav in a uncompressed format will inevitably take up tons of space. 8 Megs is not really big for an uncompressed wav.
You should consider an alternate, compressed format such as mp3 or aac. You might need to link in a decoding library though.
Check out this link on including mp3 files in your iphone app:
If your WAV files are stereo, you could try making them mono. This would basically half the file size. The disadvantage would obviously be that your sound is now mono.
I wouldn't use WAV. Take a look at ffmpegx. It's free, runs on the mac and will convert your waves files to MP3 or a host of other formats.

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?