Concatenate multiple WAV files


#1

I am completely new to JUCE framework. I want to concatenate multiple wav files. I am using the following code -

    String fileName = "E:/output.wav";
    File outputFile = File(fileName);
    if (outputFile.exists())
    	outputFile.deleteFile();
    outputFile.create();
    WavAudioFormat wav;
    for each (String filePath in arrFiles)
    {
    	File inputFile = File(filePath);
    	ScopedPointer <AudioFormatReader> reader(wav.createReaderFor(inputFile.createInputStream(), true));
    	if (reader != nullptr)
    	{
    		ScopedPointer <OutputStream> outStream(outputFile.createOutputStream());
    		if (outStream != nullptr)
    		{
    			StringPairArray metaData = WavAudioFormat::createBWAVMetadata("", "", "", Time::getCurrentTime(), 0, "");
    			ScopedPointer <AudioFormatWriter> writer(wav.createWriterFor(outStream, reader->sampleRate, reader->numChannels,
    				(int)reader->bitsPerSample, metaData, 0));
    			if (writer != nullptr)
    			{
    				outStream.release();
    				bool ok = writer->writeFromAudioReader(*reader, 0, -1);
    				writer = nullptr;
    				reader = nullptr;
    			}
    		}
    	}
    }

However, this doesn’t seem to work properly as it is playing sound of only one file instead of multiples. Am I doing anything wrong or would I have to do it another way?


#2

Hi Arka, welcome to the forum.

Is there a reason why you’re re-creating your writer on each interation? Why not move the writer variable out of the loop and use the same writer for each input file?

Do you have to deal with different channel counts and sample rates or are all your input file in the same format?

Best,
Ben


#3

Thanks a lot. It worked.