Mixing two wav files

Hello,

What is the best way to read two wav files from the hard drive, mix them together and save the result file to disk?

Thank you

I have the same question as you so I’m ‘reviving’ this thread.

In my personal use case I have the two .wav files loaded into two different AudioFormatReaderSource and i’m mixing them using MixerAudioSource

I don’t have a solution for this. Just some additional information.

It’s not as simple as it looks. In the easiest case, you read the audio data of the two files and add them together. After that, you save the result as a wav file. You will find code samples for reading and writing audio files here in the forum or in some tutorials.

There are some special cases you have to resolve to make it usable for all kind of audio data:

  • The audio files can have different sample rates.
    Up or downsampling of one file maybe is required. You will see that there are a lot of different ways of doing this. Juce also offers some utilities for this.

  • The length of the audio files is most likely different.

  • You need to adjust the volume of the result file to avoid clipping.

  • Some files are mono and some stereo.
    A simple solution is to convert all files to stereo when loading them.

Nice, thanks for the advices!!!

Could you or someone else please help with this in terms of code ? I’m not exactly sure on how to begin or what juce functions to use. I’ve read through juce::File and juce::AndroidDocument but found nothing related to this.

At least for my use case here are some particularities:

First and the most important of them all: This app is for Android.

• The audio files can have different sample rates:

All of the audio files have the same sample rate (I hope so)

• The length of the audio files is most likely different:

The length of the audio files will yes be different but they will always start at the same time. The user chooses a backing track and then records on top of it, so the end of the recording will probably be different.

• You need to adjust the volume of the result file to avoid clipping.

Yes and this is an issue I’m facing right now: I’m using two different AudioFormatReaderSource mixed into a MixerAudioSource and I actually lost the volume control that I had when using just a single one to play a single sound. In my app the user should be able to change the volume of both the files independently before saving it into the final file.

•Some files are mono and some stereo.

All the files will be stereo, but if there’s mono I’ll probably implement something to convert to stereo.