Skip to content

Commit

Permalink
TITANIC: Fix and cleanup for sound freeing
Browse files Browse the repository at this point in the history
  • Loading branch information
dreammaster committed Sep 3, 2016
1 parent 375fb60 commit d971dbe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
18 changes: 10 additions & 8 deletions engines/titanic/sound/sound.cpp
Expand Up @@ -68,15 +68,16 @@ void CSound::setVolume(uint handle, uint volume, uint seconds) {
_soundManager.setVolume(handle, volume, seconds);
}

void CSound::activateSound(CWaveFile *waveFile, bool freeFlag) {
void CSound::activateSound(CWaveFile *waveFile, DisposeAfterUse::Flag disposeAfterUse) {
for (CSoundItemList::iterator i = _sounds.begin(); i != _sounds.end(); ++i) {
CSoundItem *sound = *i;
if (sound->_waveFile == waveFile) {
sound->_active = true;
sound->_freeFlag = freeFlag;
sound->_disposeAfterUse = disposeAfterUse;

if (!freeFlag && waveFile->size() > 51200)
sound->_freeFlag = true;
// Anything bigger than 50Kb is automatically flagged to be free when finished
if (waveFile->size() > (50 * 1024))
sound->_disposeAfterUse = DisposeAfterUse::YES;
break;
}
}
Expand All @@ -90,8 +91,8 @@ void CSound::checkSounds() {
for (CSoundItemList::iterator i = _sounds.begin(); i != _sounds.end(); ) {
CSoundItem *soundItem = *i;

if (soundItem->_active && soundItem->_freeFlag) {
if (_soundManager.isActive(soundItem->_waveFile)) {
if (soundItem->_active && soundItem->_disposeAfterUse == DisposeAfterUse::YES) {
if (!_soundManager.isActive(soundItem->_waveFile)) {
i = _sounds.erase(i);
delete soundItem;
continue;
Expand Down Expand Up @@ -162,7 +163,8 @@ int CSound::playSound(const CString &name, CProximity &prox) {
if (prox._soundType != Audio::Mixer::kPlainSoundType)
waveFile->_soundType = prox._soundType;

activateSound(waveFile, prox._freeSoundFlag);
activateSound(waveFile, prox._freeSoundFlag ? DisposeAfterUse::YES :
DisposeAfterUse::NO);

return _soundManager.playSound(*waveFile, prox);
}
Expand Down Expand Up @@ -209,7 +211,7 @@ int CSound::playSpeech(CDialogueFile *dialogueFile, int speechId, CProximity &pr
return -1;

prox._soundDuration = waveFile->getDuration();
activateSound(waveFile, prox._freeSoundFlag);
activateSound(waveFile, prox._freeSoundFlag ? DisposeAfterUse::YES : DisposeAfterUse::NO);

return _soundManager.playSound(*waveFile, prox);
}
Expand Down
13 changes: 8 additions & 5 deletions engines/titanic/sound/sound.h
Expand Up @@ -41,15 +41,17 @@ class CSoundItem : public ListItem {
CWaveFile *_waveFile;
File *_dialogueFileHandle;
int _speechId;
bool _freeFlag;
DisposeAfterUse::Flag _disposeAfterUse;
bool _active;
public:
CSoundItem() : ListItem(), _waveFile(nullptr), _dialogueFileHandle(nullptr),
_speechId(0), _freeFlag(false), _active(false) {}
_speechId(0), _disposeAfterUse(DisposeAfterUse::NO), _active(false) {}
CSoundItem(const CString &name) : ListItem(), _name(name), _waveFile(nullptr),
_dialogueFileHandle(nullptr), _speechId(0), _freeFlag(false), _active(false) {}
_dialogueFileHandle(nullptr), _disposeAfterUse(DisposeAfterUse::NO),
_speechId(0), _active(false) {}
CSoundItem(File *dialogueFile, int speechId) : ListItem(), _waveFile(nullptr),
_dialogueFileHandle(dialogueFile), _speechId(speechId), _freeFlag(false), _active(false) {}
_dialogueFileHandle(dialogueFile), _speechId(speechId), _active(false),
_disposeAfterUse(DisposeAfterUse::NO) {}
};

class CSoundItemList : public List<CSoundItem> {
Expand Down Expand Up @@ -126,7 +128,8 @@ class CSound {
/**
* Flags a sound about to be played as activated
*/
void activateSound(CWaveFile *waveFile, bool freeFlag);
void activateSound(CWaveFile *waveFile,
DisposeAfterUse::Flag disposeAfterUse = DisposeAfterUse::NO);

/**
* Stops any sounds attached to a given channel
Expand Down

0 comments on commit d971dbe

Please sign in to comment.