The usage of AudioFormatWriter with Flac format

Hi!

For any reason I can’t write flac properly. it keeps missing like 40ms of the audio or if the audio is shorter it’s just 0 file with some flac metadata that most software does not recognize.

The thing is I tried with wav too and it first acted like the same but then I used flush() for the writer before writing the memoryblock to a file and that seemed to work for the wav-file. flush for wav writes the metadata that was probably one thing missing.

But for Flac there’s no flush-method (it’s empty, virtual method from it’s parent) to finish the file.

I tested to evilishly delete the writer to force flac encoder to finish things and it kinda worked -metadata was written and the file worked, but the program crashes due to this after out of scope.

How am I supposed to use flac and AudioFormatWriter properly?

Thanks!

I quickly looked at some code I have for writing audio files. It does operate on wav files, but maybe there is a step in there that you are missing?

void Exporter::run()
{
	auto fileStream { std::make_unique<FileOutputStream> (exportFile) };
	fileStream->setPosition (0);
	fileStream->truncate ();

	WavAudioFormat wavAudioFormat;
	if (std::unique_ptr<AudioFormatWriter> writer { wavAudioFormat.createWriterFor (fileStream.get (), exportSampleRate, exportChannelCount, exportBitDepth, {}, 0) }; writer != nullptr)
	{
		// audioFormatWriter will delete the file stream when done
		fileStream.release ();

		AudioSampleBuffer buffer = generateAudio();
		writer->writeFromAudioSampleBuffer (buffer, 0, buffer.getNumSamples());
	}
	else
	{
		// do error handling stuff
	}
}