Trouble with AudioFormatWriter

Hello! I’m having issues writing a mono audio file from a single-channel AudioBuffer. Here’s the code:

[code]//create audio buffer from sample data
AudioSampleBuffer* outputBuffer = new AudioSampleBuffer(1, outIndx);
outputBuffer->clear();
outputBuffer->copyFrom(0, 0, final_output, outIndx);

//initialize wav format data
WavAudioFormat wavFormat;

//create stream to output file
ScopedPointer<FileOutputStream> outputStream (outputWavFile.createOutputStream());
AudioFormatWriter* wavWriter = wavFormat.createWriterFor(outputStream, 44100, 1, 16, StringPairArray(), 0);

//write file
if (wavWriter != 0) {
    outputStream.release();
    wavWriter->writeFromAudioSampleBuffer(*outputBuffer, 0, outIndx);
}

wavWriter = 0;[/code]

It’s creating the file on the desktop, and the file is the correct size (I’m porting code that originally used libsndfile). The file itself is full of data. However, all of my audio file players see the file as having a duration of 0.0, leading me to think that the header is being written wrong. Any ideas on things I should try to fix?

Maybe try actually actually deleting your wave writer object?

Please forgive my ignorance on the topic, as I’ve only been using C++ for about a year now.

If I call delete wavWriter; at the last line, I end up with the same behavior. From my (little) experience with explicit destructor calls, this should delete it. Is there a different call I should be making?

Edit: I should add, just in case, that I’ve checked the data contained within the AudioBuffer. Every sample has a value between -1 and 1, and there are no nan samples. I’m also using the latest git code, not the website release.

If you set your wavWriter pointer to 0 before deleting it you are just calling delete on a null pointer which will do nothing.

Better still use a ScopedPointer for your wavWriter, it will then always be deleted, when it goes out of scope or you assign a new pointer (eg. 0) to it.

Your problem does just sound like you’re leaking the writer object - if its destructor is never called, then it’ll never get chance to write the correct header length and close the file.

Thanks, guys! Dave’s advice to remove the “wavWriter = 0;” line worked perfectly. I’ll look into replacing it with a scoped pointer.

I’m in a digital arts program, so a lot of the memory managing aspects of C++ were glossed over in our classes (unfortunately). I appreciate the help.