# Writing an audio file

**URL:** <https://forum.juce.com/t/writing-an-audio-file/9473>\
**Category:** General JUCE discussion\
**Created:** [October 31, 2012, 12:43pm UTC](https://forum.juce.com/t/writing-an-audio-file/9473 "2012-10-31T12:43:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shlomi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/shlomi/32/99_2.png) [@Shlomi](https://forum.juce.com/u/Shlomi)\
**Post date:** [October 31, 2012, 12:43pm UTC](https://forum.juce.com/t/writing-an-audio-file/9473/1 "2012-10-31T12:43:25Z")

</div>

Hi

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

So I did this:

```auto

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

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

		FileOutputStream fos(newFile);

		MemoryBlock mb;

		originalFile.loadFileAsData(mb);
		WavAudioFormat waveAudioFormat;

		MemoryInputStream mis(mb,true);
		AudioFormatManager formatManager;
		formatManager.registerBasicFormats();

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

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

		writer->writeFromAudioReader(*reader,0,reader->lengthInSamples);

```

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.

---

<div class="post-metadata">

**Author:** ![masshacker](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/masshacker/32/650_2.png) [@masshacker](https://forum.juce.com/u/masshacker)\
**Post date:** [October 31, 2012, 2:57pm UTC](https://forum.juce.com/t/writing-an-audio-file/9473/2 "2012-10-31T14:57:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Shlomi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/shlomi/32/99_2.png) [@Shlomi](https://forum.juce.com/u/Shlomi)\
**Post date:** [October 31, 2012, 3:13pm UTC](https://forum.juce.com/t/writing-an-audio-file/9473/3 "2012-10-31T15:13:32Z")

</div>

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:

```auto
    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)
                {
                    outStream.release();

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

                    return ok && tempFile.overwriteTargetFileWithTemporary();
                }
            }
        }

        return false;
    }
}
```
