fix memory leak in sound stream

This commit is contained in:
Eduardo Bart 2012-04-14 16:52:00 -03:00
parent 341898f1d0
commit fe33614c3a
2 changed files with 21 additions and 20 deletions

View File

@ -61,7 +61,7 @@ void SoundManager::terminate()
m_musicEnabled = false; m_musicEnabled = false;
m_soundEnabled = false; m_soundEnabled = false;
alcMakeContextCurrent(NULL); alcMakeContextCurrent(nullptr);
if(m_context) { if(m_context) {
alcDestroyContext(m_context); alcDestroyContext(m_context);

View File

@ -108,37 +108,38 @@ void StreamSoundSource::update()
bool StreamSoundSource::fillBufferAndQueue(ALuint buffer) bool StreamSoundSource::fillBufferAndQueue(ALuint buffer)
{ {
// fill buffer // fill buffer
static DataBuffer<char> bufferData(STREAM_FRAGMENT_SIZE); static DataBuffer<char> bufferData(2*STREAM_FRAGMENT_SIZE);
ALenum format = m_soundFile->getSampleFormat(); ALenum format = m_soundFile->getSampleFormat();
int maxRead = STREAM_FRAGMENT_SIZE;
if(m_downMix != NoDownMix)
maxRead *= 2;
int bytesRead = 0; int bytesRead = 0;
do { do {
bytesRead += m_soundFile->read(&bufferData[bytesRead], STREAM_FRAGMENT_SIZE - bytesRead); bytesRead += m_soundFile->read(&bufferData[bytesRead], maxRead - bytesRead);
// end of sound file // end of sound file
if(bytesRead < STREAM_FRAGMENT_SIZE) { if(bytesRead < maxRead) {
if(m_looping) if(m_looping)
m_soundFile->reset(); m_soundFile->reset();
else else
break; break;
} }
} while(bytesRead < STREAM_FRAGMENT_SIZE); } while(bytesRead < maxRead);
bool done = bytesRead >= STREAM_FRAGMENT_SIZE;
if(m_downMix != NoDownMix) {
if(format == AL_FORMAT_STEREO16) {
if(bytesRead > 0) {
uint16_t *data = (uint16_t*)bufferData.data();
bytesRead /= 2;
for(int i=0;i<bytesRead;i++)
data[i] = data[2*i + (m_downMix == DownMixLeft ? 0 : 1)];
}
format = AL_FORMAT_MONO16;
}
}
if(bytesRead > 0) { if(bytesRead > 0) {
if(m_downMix != NoDownMix) {
if(format == AL_FORMAT_STEREO16) {
assert(bytesRead % 2 == 0);
bytesRead /= 2;
uint16_t *data = (uint16_t*)bufferData.data();
for(int i=0;i<bytesRead/2;i++)
data[i] = data[2*i + (m_downMix == DownMixLeft ? 0 : 1)];
format = AL_FORMAT_MONO16;
}
}
alBufferData(buffer, format, &bufferData[0], bytesRead, m_soundFile->getRate()); alBufferData(buffer, format, &bufferData[0], bytesRead, m_soundFile->getRate());
ALenum err = alGetError(); ALenum err = alGetError();
if(err != AL_NO_ERROR) if(err != AL_NO_ERROR)
@ -151,7 +152,7 @@ bool StreamSoundSource::fillBufferAndQueue(ALuint buffer)
} }
// return false if there aren't more buffers to fill // return false if there aren't more buffers to fill
return done; return bytesRead >= STREAM_FRAGMENT_SIZE;
} }
void StreamSoundSource::downMix(StreamSoundSource::DownMix downMix) void StreamSoundSource::downMix(StreamSoundSource::DownMix downMix)