[SOLVED] Strange issue with writing audio buffer to .wav file

I’m seeing this strange issue when trying to write an empty, 10 second long buffer to a .wav file upon instantiation of my plugin. I’m writing the file in a function that is called from the constructor of my AudioProcessor.

void FileIoAudioProcessor::writeFile()
	File file(File::getSpecialLocation(File::SpecialLocationType::userDesktopDirectory).getChildFile("testBuffer.wav"));
	AudioBuffer<float> testBuffer(2, 480000);
	WavAudioFormat format;
	std::unique_ptr<AudioFormatWriter> writer;
	writer.reset(format.createWriterFor(new FileOutputStream(file), 48000, testBuffer.getNumChannels(), 24, {}, 0));

	if (writer != nullptr) {
		writer->writeFromAudioSampleBuffer(testBuffer, 0, testBuffer.getNumSamples());

The issue is that, upon loading my plugin in Ableton, the size of the file created seems to continuously grow for as long as my plugin is opened. For example, if I open the plugin and let it run for 1 minute, I get a file of size 100 MB. If I open the plugin and let it run for 10 minutes, I get a file of size 3 GB. In both scenarios, loading the resulting .wav file in a media player shows a 10 second long file. I also tried creating a raw pointer for the writer and manually deleting it but this did not change anything.

Another strange issue is that, if I try to open my newly generated .wav file while the plugin is open in the DAW, I get a playback error in whatever media player I use. If I want to play the file, I have to close the whole plugin down.

Thought these issues were interesting enough to post about. As it is, the current state of my file writing system is unusable. I’ve consulted a few people on the code and they claim that it checks out (the forums would confirm this as well). Does anyone know what’s going on?

Maybe a memory corruption because of something else happening in the plugin, that causes the writeFile function to run seemingly endlessly. It would be really weird, though. If the code is run from the GUI thread, you would notice your plugin’s and Live’s GUI freezing. Have you tried debugging the plugin, to see if the writeFile function is entered unexpectedly or if some state is corrupt when initially entering it?

I suspect this is something really simple, though. Maybe you have left a call to the writeFile method in the processBlock method when you only intended to test the function by calling it from the constructor? That would pretty much explain the symptoms. processBlock is called often by the host and you don’t delete the output file in your code. The result would be a quickly growing file. (Since the same file would be appended to.) You would probably also notice Live’s audio playback being completely corrupted because of all the heavy work you are doing in your writeFile method.

Try calling the writeFile function from somewhere else than from the constructor, like from prepareToPlay. (Taking care you don’t run writeFile more than once, because prepareToPlay may be called multiple times by the host.)

Have you tried another host application besides Live?

Do a full clean and rebuild of your project. That can sometimes clear up bizarre issues.

You suspected correctly… I had another call in the processBlock… sorry!