views:

444

answers:

2

Hi

Is it possible to combine two wav files into one as if they've been played simultaneously? I need to do this programatically.

The only way I found till now is to play both simultaneously and record the output using AVAudioRecorder. But this won't work if the user's using headphones.

Can someone please point me to the right direction.

Thanks.

A: 

There might be a better way, but if both wavs have same encoding/bitrate, etc, you can just strip the wav header (first 44 bytes according to wav format spec) of the second file and concatenate the rest of 2nd wav to first.

ttvd
But wouldn't that just put the files so they play one after the other? I want to combine them so playing the resultant file feels like they are being played simultaneously.
lostInTransit
I am not sure; depending on your wav files there might be some detectable "transition" with this method. You could probably "fade" it somehow, by interpolating the data at end of wav1 and start of wav2. But that of course would require analyzing the encoding/content of your files.
ttvd
+2  A: 

You'll need to read the sample data of each file, average each sample value, and write the result to a new file. You can use ExtAudioFile to read and write files, including the headers and the sample data.

You also need to update the header with the correct length of the file, because there are fields whose value depends on the length of the sample data.

lucius