Fixes a bug where vorbis can't handle large buffers.

This commit is contained in:
Lukas Dürrenberger 2016-03-06 13:22:01 +01:00
parent 9f6f02f988
commit 2c7b58f406

View File

@ -130,21 +130,31 @@ bool SoundFileWriterOgg::open(const std::string& filename, unsigned int sampleRa
////////////////////////////////////////////////////////////
void SoundFileWriterOgg::write(const Int16* samples, Uint64 count)
{
// Prepare a buffer to hold our samples
// Vorbis has issues with buffers that are too large, so we ask for 64K
static const int bufferSize = 65536;
// A frame contains a sample from each channel
int frameCount = static_cast<int>(count / m_channelCount);
float** buffer = vorbis_analysis_buffer(&m_state, frameCount);
while (frameCount > 0)
{
// Prepare a buffer to hold our samples
float** buffer = vorbis_analysis_buffer(&m_state, bufferSize);
assert(buffer);
// Write the samples to the buffer, converted to float
for (int i = 0; i < frameCount; ++i)
for (int i = 0; i < std::min(frameCount, bufferSize); ++i)
for (unsigned int j = 0; j < m_channelCount; ++j)
buffer[j][i] = *samples++ / 32767.0f;
// Tell the library how many samples we've written
vorbis_analysis_wrote(&m_state, frameCount);
vorbis_analysis_wrote(&m_state, std::min(frameCount, bufferSize));
frameCount -= bufferSize;
// Flush any produced block
flushBlocks();
}
}