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) 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); int frameCount = static_cast<int>(count / m_channelCount);
float** buffer = vorbis_analysis_buffer(&m_state, frameCount);
assert(buffer);
// Write the samples to the buffer, converted to float while (frameCount > 0)
for (int i = 0; i < frameCount; ++i) {
for (unsigned int j = 0; j < m_channelCount; ++j) // Prepare a buffer to hold our samples
buffer[j][i] = *samples++ / 32767.0f; float** buffer = vorbis_analysis_buffer(&m_state, bufferSize);
assert(buffer);
// Tell the library how many samples we've written // Write the samples to the buffer, converted to float
vorbis_analysis_wrote(&m_state, frameCount); 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;
// Flush any produced block // Tell the library how many samples we've written
flushBlocks(); vorbis_analysis_wrote(&m_state, std::min(frameCount, bufferSize));
frameCount -= bufferSize;
// Flush any produced block
flushBlocks();
}
} }