Clipping of samples in ogg encoder

Hi all,
I have a strange bug. I m trying to convert wav file into ogg file. I changed the JUCE OGG format writer code to accept VBR mode. And also to accept various constant bitrate modes from -1 to 10. ( it was “Low Quality” , “Medium Quality” and “High Quality” )

[code]//The Changes is done for providing option for variable bitrates
int iReturnValue = 1;

	if ( qualityIndex <= 10 )
		iReturnValue = vorbis_encode_init_vbr (&vi, numChannels, (int) sampleRate, jlimit (-0.1f, 1.0f, qualityIndex / 10.0f)) ;
		iReturnValue = vorbis_encode_init(&vi , numChannels, (int) sampleRate, qualityIndex, qualityIndex, qualityIndex);
	if ( iReturnValue == 0 )


Now for Constant bitrate of 256kbps ( qualityIndex = 256000 ). I found that some of the samples are missing from the ogg file.

Can anyone tell me wats causing the problem here. Help is Really appreciated.

Thanks and Regards,

Maybe if you use vbr mode you’re supposed to use different functions for writing the data?.. Or if samples are missing at the end, perhaps there’s an extra flush required when it’s finished?

but i m using CBR mode for encoding… Wait i will revert back and test with the original code u had…

And i Also checked with the flush before…
if we use the flush before deleting the filestream then the sample count is improved but not exact…
for example if i had 10000 samples in wav file, i get 5728 samples without using flush and around 7000 odd samples if i use flush in the destructor…

Sorry u have written code only for VBR i guess…
I searched in the net and most of the libraries use thre modes VBR , CBR and ABR
and all the libraries use more or less the same code as u used for writing. But the difference i found out was only in loops start and end.

I tried reverting back my code… But the problem is still same…
I used wave file of 441000 samples to convert to ogg and i found in the oggreader that it has only 438404 samples in it.
Here is the screen shot my app which shows sample missing at the end.

i used the code

[code] pFormat = new juce::OggVorbisAudioFormat ();
pWriter = pFormat->createWriterFor(pOutputStream, _dSampleRate, _iChannels, _iBitDepth, 0, 1.0f );
if ( pWriter != 0L )
juce::AudioFormatReaderSource* pReaderSrc = new juce::AudioFormatReaderSource(pReader, false);
juce::int64 iTotalSamplesToWrite = (int) (pReader->lengthInSamples;

					while (iTotalSamplesToWrite > 0)
						juce::int64 iSamplesToRead = juce::jmin( iTotalSamplesToWrite , (juce::int64)BLOCK_SIZE );
						juce::AudioSampleBuffer tempBuffer(pReader->numChannels, iSamplesToRead);
						juce::AudioSourceChannelInfo info;
						info.buffer				= &tempBuffer;
						info.startSample		= 0;
						info.numSamples			= iSamplesToRead;

						pReaderSrc->getNextAudioBlock (info);
						tempBuffer.writeToAudioWriter( pWriter , 0 , info.numSamples );

						iTotalSamplesToWrite -= iSamplesToRead;
				SAFE_DELETE ( pReaderSrc );
			SAFE_DELETE ( pFormat );
			SAFE_DELETE ( pWriter );
			SAFE_DELETE ( pReader );


Thanks and regards,

Well, that’s not necessarily a bug - it’s a block-based format so will probably round things to the nearest block, and if the last few samples are empty, there’s no reason why it’d bother storing them. Try a file that doesn’t have any silence at the end.

I will check that out…
Meanwhile i exported the same file using the quicktime app… But it was showing all the samples.

Well I used one wave file with dense samples at the end but result is still same…
I used file of 161920 samples, resultant ogg file has 160896 samples in it here is the screen shot.

This is with the current tip code, right? Not your hacked version?

It definitely flushes correctly, so I can’t really think what else could be wrong with it…

I m using the original juce library for this one…
And i have found that it was logged earlier too in this forum…

Yeah u fixed it in Juce 1.50 …
I m using juce 1.46… Thanks to rotemb who fixed it.