Mixing multiple wav files

I’m stumped now. Can you step through inside AudioFormatWriter::writeFromAudioSource() to see what’s happening?

Will this concatenate these audio files sequentially? Or this would just mix the sounds from the sources? What would I have to do if I just want to merge / concatenate the audio files? Thanks in advance.

This should mix them… I see, your line:

totalNumSamples += reader->lengthInSamples

should be:

totalNumSamples = jmax (totalNumSamples, reader->lengthInSamples);

Where totalNumSamples is set to zero initially.

(Which should be inside the if block in case the reader is nullptr.)

If you want to concatenate you could just create an array of audio sources and write them in turn to the same AudioFormatWriter object.

…you are creating the AudioFormatReaderSource on the stack, so at the end of the block it gets destroyed. You have to allocate it on the heap using:

AudioFormatReaderSource newSource = new AudioFormatReaderSource (reader, true);
mixerSource.addInputSource (newSource, true);

the flag “true” in addInputSource transfers ownership to the mixer, so it get’s destroyed and does not leak. But it does not copy, so in this case when the object is destroyed at the end of the block luckily it is removed from the mixer.