Writing an audio file


I tried to load a wav file and write it again as a new file (just to test the writing)

So I did this:

		File originalFile("c:/tmp/originalFile.wav");

		File newFile("c:/tmp/newFile.wav");

		FileOutputStream fos(newFile);

		MemoryBlock mb;

		WavAudioFormat waveAudioFormat;

		MemoryInputStream mis(mb,true);
		AudioFormatManager formatManager;

		AudioFormatReader* reader = formatManager.createReaderFor (&mis);

		StringPairArray metaData = WavAudioFormat::createBWAVMetadata("","","",Time::getCurrentTime(),0,"");
		writer = waveAudioFormat.createWriterFor(&fos,reader->sampleRate,reader->numChannels,reader->bitsPerSample,
			metaData ,0);


When I run it I get the newFile with the size of the original file, but It won’t play.
I tried comparing the binaries and checking the WAV spec but I still don’t know why it doesn’t play.

I also tried playing with the metadata parameters and I tried to use the metadata from the original file (empty) and I also tried the whole thing with an AIFF format.

Where did you declare “writer” ?
Where do you destroy it?
I’m thinking that maybe your file isn’t getting closed properly.

Thanks, You are right about forgetting to close the file and I also had more issue there,

Found Juce’s helper fun. for WAV coping, it’s working great:

    static bool slowCopyWavFileWithNewMetadata (const File& file, const StringPairArray& metadata)
        TemporaryFile tempFile (file);

        WavAudioFormat wav;
        ScopedPointer <AudioFormatReader> reader (wav.createReaderFor (file.createInputStream(), true));

        if (reader != nullptr)
            ScopedPointer <OutputStream> outStream (tempFile.getFile().createOutputStream());

            if (outStream != nullptr)
                ScopedPointer <AudioFormatWriter> writer (wav.createWriterFor (outStream, reader->sampleRate,
                                                                               reader->numChannels, (int) reader->bitsPerSample,
                                                                               metadata, 0));

                if (writer != nullptr)

                    bool ok = writer->writeFromAudioReader (*reader, 0, -1);
                    writer = nullptr;
                    reader = nullptr;

                    return ok && tempFile.overwriteTargetFileWithTemporary();

        return false;