Sample size mismatch when converting from wav to flac

Hello, how are you my friends!
I have problem: number of samples not correct when convert wav file to flac.
Please see test code and comments.
Lot of help wanted.

Thanks you so much

	// DEBUG
	// The file. Sample rate : 44100 for me
	const File file("C:\\<your path>\\<your file>.wav");

	AudioFormatManager AFMgr;
	AFMgr.registerBasicFormats();

	// Read the wav file from disk.
	std::unique_ptr<AudioFormatReader> reader1(AFMgr.createReaderFor(file));

	// Number of samples are correct
	// Output -> Float Sample Size : 1114105 (for my file)
	DBG("Float Sample Size : " << reader1->lengthInSamples);

	// Convert reader to flac.
	FlacAudioFormat Format1;
	MemoryOutputStream* StreamFlacOut = new MemoryOutputStream();
	std::unique_ptr<AudioFormatWriter> afw(
		Format1.createWriterFor(
			StreamFlacOut,
			reader1->sampleRate, // 44100 for me
			reader1->numChannels, // 2
			24,
			StringPairArray(),
			0));
	afw->writeFromAudioReader(*reader1, 0, reader1->lengthInSamples);
	StreamFlacOut->flush();

	// Read the flac output stream.
	
	// Method 1
	FlacAudioFormat Format2;
	MemoryInputStream* StreamFlacIn = new MemoryInputStream(StreamFlacOut->getData(), StreamFlacOut->getDataSize(), false);
	std::unique_ptr<AudioFormatReader> reader2(Format2.createReaderFor(StreamFlacIn, false));

	// Method 2
	//	std::unique_ptr<AudioFormatReader> reader2(
	//		AFMgr.createReaderFor(
	//			std::make_unique<MemoryInputStream>(
	//				StreamFlacOut->getData(), 
	//				StreamFlacOut->getDataSize(), 
	//				false)));

	// NUMBER OF SAMPLES SHOULD BE THE SAME BUT ARE NOT CORRECT
	// Output -> Flac Sample Size : 1113984 (for my file) ***** Should be 1114105 (for my file) ******
	DBG("Flac Sample Size : " << reader2->lengthInSamples);

// FAILS
jassert(reader2->lengthInSamples == reader1->lengthInSamples);

	// DEBUG
2 Likes

The flac decoder seems to be incrementing the lengthInSamples by chunks of 1152 and it is omitting the tail end of the samples (1113984/1152 = 967 - fits perfectly, 1114105/1152 = 967.10 - doesn’t fit)

2 Likes