Avoid an extra call to get the buffer size

experimental
Chris Robinson 7 years ago
parent 5c53ee42a1
commit 41bb35655b

@ -941,7 +941,7 @@ void OpenAL_Output::setHrtf(const std::string &hrtfname, HrtfMode hrtfmode)
} }
Sound_Handle OpenAL_Output::loadSound(const std::string &fname) std::pair<Sound_Handle,size_t> OpenAL_Output::loadSound(const std::string &fname)
{ {
getALError(); getALError();
@ -966,27 +966,31 @@ Sound_Handle OpenAL_Output::loadSound(const std::string &fname)
decoder->getInfo(&srate, &chans, &type); decoder->getInfo(&srate, &chans, &type);
format = getALFormat(chans, type); format = getALFormat(chans, type);
if(!format) return nullptr; if(!format) return std::make_pair(nullptr, 0);
decoder->readAll(data); decoder->readAll(data);
decoder->close(); decoder->close();
ALint size;
ALuint buf = 0; ALuint buf = 0;
alGenBuffers(1, &buf); alGenBuffers(1, &buf);
alBufferData(buf, format, &data[0], data.size(), srate); alBufferData(buf, format, data.data(), data.size(), srate);
alGetBufferi(buf, AL_SIZE, &size);
if(getALError() != AL_NO_ERROR) if(getALError() != AL_NO_ERROR)
{ {
if(buf && alIsBuffer(buf)) if(buf && alIsBuffer(buf))
alDeleteBuffers(1, &buf); alDeleteBuffers(1, &buf);
getALError(); getALError();
return nullptr; return std::make_pair(nullptr, 0);
} }
return MAKE_PTRID(buf); return std::make_pair(MAKE_PTRID(buf), size);
} }
void OpenAL_Output::unloadSound(Sound_Handle data) size_t OpenAL_Output::unloadSound(Sound_Handle data)
{ {
ALuint buffer = GET_PTRID(data); ALuint buffer = GET_PTRID(data);
if(!buffer) return 0;
// Make sure no sources are playing this buffer before unloading it. // Make sure no sources are playing this buffer before unloading it.
SoundVec::const_iterator iter = mActiveSounds.begin(); SoundVec::const_iterator iter = mActiveSounds.begin();
for(;iter != mActiveSounds.end();++iter) for(;iter != mActiveSounds.end();++iter)
@ -1003,19 +1007,11 @@ void OpenAL_Output::unloadSound(Sound_Handle data)
alSourcei(source, AL_BUFFER, 0); alSourcei(source, AL_BUFFER, 0);
} }
} }
alDeleteBuffers(1, &buffer);
getALError();
}
size_t OpenAL_Output::getSoundDataSize(Sound_Handle data) const
{
ALuint buffer = GET_PTRID(data);
ALint size = 0; ALint size = 0;
alGetBufferi(buffer, AL_SIZE, &size); alGetBufferi(buffer, AL_SIZE, &size);
alDeleteBuffers(1, &buffer);
getALError(); getALError();
return size;
return (ALuint)size;
} }

@ -66,9 +66,8 @@ namespace MWSound
virtual std::vector<std::string> enumerateHrtf(); virtual std::vector<std::string> enumerateHrtf();
virtual void setHrtf(const std::string &hrtfname, HrtfMode hrtfmode); virtual void setHrtf(const std::string &hrtfname, HrtfMode hrtfmode);
virtual Sound_Handle loadSound(const std::string &fname); virtual std::pair<Sound_Handle,size_t> loadSound(const std::string &fname);
virtual void unloadSound(Sound_Handle data); virtual size_t unloadSound(Sound_Handle data);
virtual size_t getSoundDataSize(Sound_Handle data) const;
virtual bool playSound(Sound *sound, Sound_Handle data, float offset); virtual bool playSound(Sound *sound, Sound_Handle data, float offset);
virtual bool playSound3D(Sound *sound, Sound_Handle data, float offset); virtual bool playSound3D(Sound *sound, Sound_Handle data, float offset);

@ -36,9 +36,8 @@ namespace MWSound
virtual std::vector<std::string> enumerateHrtf() = 0; virtual std::vector<std::string> enumerateHrtf() = 0;
virtual void setHrtf(const std::string &hrtfname, HrtfMode hrtfmode) = 0; virtual void setHrtf(const std::string &hrtfname, HrtfMode hrtfmode) = 0;
virtual Sound_Handle loadSound(const std::string &fname) = 0; virtual std::pair<Sound_Handle,size_t> loadSound(const std::string &fname) = 0;
virtual void unloadSound(Sound_Handle data) = 0; virtual size_t unloadSound(Sound_Handle data) = 0;
virtual size_t getSoundDataSize(Sound_Handle data) const = 0;
virtual bool playSound(Sound *sound, Sound_Handle data, float offset) = 0; virtual bool playSound(Sound *sound, Sound_Handle data, float offset) = 0;
virtual bool playSound3D(Sound *sound, Sound_Handle data, float offset) = 0; virtual bool playSound3D(Sound *sound, Sound_Handle data, float offset) = 0;

@ -215,10 +215,11 @@ namespace MWSound
if(!sfx->mHandle) if(!sfx->mHandle)
{ {
sfx->mHandle = mOutput->loadSound(sfx->mResourceName); size_t size;
std::tie(sfx->mHandle, size) = mOutput->loadSound(sfx->mResourceName);
if(!sfx->mHandle) return nullptr; if(!sfx->mHandle) return nullptr;
mBufferCacheSize += mOutput->getSoundDataSize(sfx->mHandle); mBufferCacheSize += size;
if(mBufferCacheSize > mBufferCacheMax) if(mBufferCacheSize > mBufferCacheMax)
{ {
do { do {
@ -229,8 +230,8 @@ namespace MWSound
} }
Sound_Buffer *unused = mUnusedBuffers.back(); Sound_Buffer *unused = mUnusedBuffers.back();
mBufferCacheSize -= mOutput->getSoundDataSize(unused->mHandle); size = mOutput->unloadSound(unused->mHandle);
mOutput->unloadSound(unused->mHandle); mBufferCacheSize -= size;
unused->mHandle = 0; unused->mHandle = 0;
mUnusedBuffers.pop_back(); mUnusedBuffers.pop_back();

Loading…
Cancel
Save