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

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.

Related

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.

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.

iphone game caf sound effects

I am using Caf file for sound effects. But some times some of the files just have no volume. Is it common for caf? After restarting, it's fine again. Instrument shows I've only a couple ofk's of memory leaks. I use soundbooth to edit and save wav as aiff then rename to caf
Caf is an uncompressed format developed by Apple. Try using mp3 files, they are accepted by iphone, they weight less and work better in general.

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.

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?