2016-03-09 03:34:51 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "titanic/sound/sound.h"
|
2016-03-19 11:48:23 +00:00
|
|
|
#include "titanic/game_manager.h"
|
2016-07-03 23:07:38 +00:00
|
|
|
#include "titanic/titanic.h"
|
2016-03-09 03:34:51 +00:00
|
|
|
|
|
|
|
namespace Titanic {
|
|
|
|
|
2016-10-09 12:59:58 +00:00
|
|
|
CSound::CSound(CGameManager *owner, Audio::Mixer *mixer) :
|
2016-08-05 17:11:12 +00:00
|
|
|
_gameManager(owner), _soundManager(mixer) {
|
2016-07-03 23:07:38 +00:00
|
|
|
g_vm->_movieManager.setSoundManager(&_soundManager);
|
2016-03-09 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 23:44:18 +00:00
|
|
|
void CSound::save(SimpleFile *file) const {
|
2016-03-12 17:35:41 +00:00
|
|
|
_soundManager.save(file);
|
2016-03-10 23:44:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSound::load(SimpleFile *file) {
|
2016-03-12 17:35:41 +00:00
|
|
|
_soundManager.load(file);
|
2016-03-10 23:44:18 +00:00
|
|
|
}
|
|
|
|
|
2016-03-19 11:48:23 +00:00
|
|
|
void CSound::preLoad() {
|
|
|
|
_soundManager.preLoad();
|
|
|
|
|
|
|
|
if (_gameManager)
|
2016-08-07 23:04:28 +00:00
|
|
|
_gameManager->_musicRoom.destroyMusicHandler();
|
2016-03-19 11:48:23 +00:00
|
|
|
}
|
|
|
|
|
2016-03-20 02:43:02 +00:00
|
|
|
void CSound::preEnterView(CViewItem *newView, bool isNewRoom) {
|
2016-08-07 23:04:28 +00:00
|
|
|
CNodeItem *node = newView->findNode();
|
|
|
|
double xp, yp, zp;
|
|
|
|
node->getPosition(xp, yp, zp);
|
|
|
|
|
|
|
|
double cosVal = cos(newView->_angle);
|
|
|
|
double sinVal = -sin(newView->_angle);
|
|
|
|
|
|
|
|
_soundManager.setListenerPosition(xp, yp, zp, cosVal, sinVal, 0, isNewRoom);
|
2016-03-20 00:56:29 +00:00
|
|
|
}
|
|
|
|
|
2016-10-30 01:48:10 +00:00
|
|
|
bool CSound::isActive(int handle) {
|
2016-08-06 16:14:02 +00:00
|
|
|
if (handle != 0 && handle != -1)
|
|
|
|
return _soundManager.isActive(handle);
|
2016-03-25 01:02:25 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-07 01:55:19 +00:00
|
|
|
void CSound::setVolume(uint handle, uint volume, uint seconds) {
|
|
|
|
_soundManager.setVolume(handle, volume, seconds);
|
2016-03-25 01:02:25 +00:00
|
|
|
}
|
|
|
|
|
2016-09-03 03:40:25 +00:00
|
|
|
void CSound::activateSound(CWaveFile *waveFile, DisposeAfterUse::Flag disposeAfterUse) {
|
2016-08-12 11:46:35 +00:00
|
|
|
for (CSoundItemList::iterator i = _sounds.begin(); i != _sounds.end(); ++i) {
|
|
|
|
CSoundItem *sound = *i;
|
|
|
|
if (sound->_waveFile == waveFile) {
|
|
|
|
sound->_active = true;
|
2016-09-03 03:40:25 +00:00
|
|
|
sound->_disposeAfterUse = disposeAfterUse;
|
2016-08-12 11:46:35 +00:00
|
|
|
|
2016-09-03 03:40:25 +00:00
|
|
|
// Anything bigger than 50Kb is automatically flagged to be free when finished
|
|
|
|
if (waveFile->size() > (50 * 1024))
|
|
|
|
sound->_disposeAfterUse = DisposeAfterUse::YES;
|
2016-08-12 11:46:35 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-08-04 01:47:11 +00:00
|
|
|
}
|
|
|
|
|
2016-08-07 01:55:19 +00:00
|
|
|
void CSound::stopChannel(int channel) {
|
|
|
|
_soundManager.stopChannel(channel);
|
|
|
|
}
|
|
|
|
|
2016-08-04 01:47:11 +00:00
|
|
|
void CSound::checkSounds() {
|
2016-08-31 23:37:51 +00:00
|
|
|
for (CSoundItemList::iterator i = _sounds.begin(); i != _sounds.end(); ) {
|
2016-08-04 01:47:11 +00:00
|
|
|
CSoundItem *soundItem = *i;
|
2016-08-31 23:37:51 +00:00
|
|
|
|
2016-09-03 03:40:25 +00:00
|
|
|
if (soundItem->_active && soundItem->_disposeAfterUse == DisposeAfterUse::YES) {
|
|
|
|
if (!_soundManager.isActive(soundItem->_waveFile)) {
|
2016-08-31 23:37:51 +00:00
|
|
|
i = _sounds.erase(i);
|
2016-08-04 01:47:11 +00:00
|
|
|
delete soundItem;
|
2016-09-01 22:49:53 +00:00
|
|
|
continue;
|
2016-08-04 01:47:11 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-01 22:49:53 +00:00
|
|
|
|
|
|
|
++i;
|
2016-08-04 01:47:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSound::removeOldest() {
|
|
|
|
for (CSoundItemList::iterator i = _sounds.reverse_begin();
|
|
|
|
i != _sounds.end(); --i) {
|
|
|
|
CSoundItem *soundItem = *i;
|
2016-08-12 11:46:35 +00:00
|
|
|
if (soundItem->_active && !_soundManager.isActive(soundItem->_waveFile)) {
|
2016-08-04 01:47:11 +00:00
|
|
|
_sounds.remove(soundItem);
|
|
|
|
delete soundItem;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-05 01:21:34 +00:00
|
|
|
CWaveFile *CSound::getTrueTalkSound(CDialogueFile *dialogueFile, int index) {
|
2016-08-04 02:25:47 +00:00
|
|
|
return loadSpeech(dialogueFile, index);
|
2016-05-28 22:13:09 +00:00
|
|
|
}
|
|
|
|
|
2016-08-05 01:21:34 +00:00
|
|
|
CWaveFile *CSound::loadSound(const CString &name) {
|
2016-05-05 00:30:52 +00:00
|
|
|
checkSounds();
|
|
|
|
|
|
|
|
// Check whether an entry for the given name is already active
|
|
|
|
for (CSoundItemList::iterator i = _sounds.begin(); i != _sounds.end(); ++i) {
|
|
|
|
CSoundItem *soundItem = *i;
|
|
|
|
if (soundItem->_name == name) {
|
|
|
|
// Found it, so move it to the front of the list and return
|
|
|
|
_sounds.remove(soundItem);
|
|
|
|
_sounds.push_front(soundItem);
|
2016-11-05 23:12:39 +00:00
|
|
|
soundItem->_waveFile->reset();
|
2016-08-04 02:25:47 +00:00
|
|
|
return soundItem->_waveFile;
|
2016-05-05 00:30:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create new sound item
|
|
|
|
CSoundItem *soundItem = new CSoundItem(name);
|
2016-08-04 02:25:47 +00:00
|
|
|
soundItem->_waveFile = _soundManager.loadSound(name);
|
2016-05-05 00:30:52 +00:00
|
|
|
|
2016-08-04 02:25:47 +00:00
|
|
|
if (!soundItem->_waveFile) {
|
2016-08-04 01:47:11 +00:00
|
|
|
// Couldn't load sound, so destroy new item and return
|
2016-05-05 00:30:52 +00:00
|
|
|
delete soundItem;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the item to the list of sounds
|
|
|
|
_sounds.push_front(soundItem);
|
|
|
|
|
|
|
|
// If there are more than 10 sounds loaded, remove the last one,
|
|
|
|
// which is the least recently used of all of them
|
|
|
|
if (_sounds.size() > 10)
|
|
|
|
removeOldest();
|
|
|
|
|
2016-08-04 02:25:47 +00:00
|
|
|
return soundItem->_waveFile;
|
2016-05-05 00:30:52 +00:00
|
|
|
}
|
|
|
|
|
2016-08-04 01:47:11 +00:00
|
|
|
int CSound::playSound(const CString &name, CProximity &prox) {
|
2016-08-05 01:21:34 +00:00
|
|
|
CWaveFile *waveFile = loadSound(name);
|
2016-08-04 02:25:47 +00:00
|
|
|
if (!waveFile)
|
2016-08-04 01:47:11 +00:00
|
|
|
return -1;
|
|
|
|
|
2016-10-24 00:11:30 +00:00
|
|
|
prox._soundDuration = waveFile->getDurationTicks();
|
2016-09-01 00:20:10 +00:00
|
|
|
if (prox._soundType != Audio::Mixer::kPlainSoundType)
|
|
|
|
waveFile->_soundType = prox._soundType;
|
|
|
|
|
2016-09-03 14:49:03 +00:00
|
|
|
activateSound(waveFile, prox._disposeAfterUse);
|
2016-08-04 01:47:11 +00:00
|
|
|
|
2016-08-04 02:25:47 +00:00
|
|
|
return _soundManager.playSound(*waveFile, prox);
|
2016-05-05 00:30:52 +00:00
|
|
|
}
|
|
|
|
|
2016-08-05 01:21:34 +00:00
|
|
|
CWaveFile *CSound::loadSpeech(CDialogueFile *dialogueFile, int speechId) {
|
2016-08-04 01:47:11 +00:00
|
|
|
checkSounds();
|
|
|
|
|
|
|
|
// Check whether an entry for the given name is already active
|
|
|
|
for (CSoundItemList::iterator i = _sounds.begin(); i != _sounds.end(); ++i) {
|
2016-05-05 00:30:52 +00:00
|
|
|
CSoundItem *soundItem = *i;
|
2016-08-04 01:47:11 +00:00
|
|
|
if (soundItem->_dialogueFileHandle == dialogueFile->getFile()
|
|
|
|
&& soundItem->_speechId == speechId) {
|
|
|
|
// Found it, so move it to the front of the list and return
|
2016-05-05 00:30:52 +00:00
|
|
|
_sounds.remove(soundItem);
|
2016-08-04 01:47:11 +00:00
|
|
|
_sounds.push_front(soundItem);
|
2016-08-04 02:25:47 +00:00
|
|
|
return soundItem->_waveFile;
|
2016-05-05 00:30:52 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-04 01:47:11 +00:00
|
|
|
|
|
|
|
// Create new sound item
|
|
|
|
CSoundItem *soundItem = new CSoundItem(dialogueFile->getFile(), speechId);
|
2016-08-04 02:25:47 +00:00
|
|
|
soundItem->_waveFile = _soundManager.loadSpeech(dialogueFile, speechId);
|
2016-08-04 01:47:11 +00:00
|
|
|
|
2016-08-04 02:25:47 +00:00
|
|
|
if (!soundItem->_waveFile) {
|
2016-08-04 01:47:11 +00:00
|
|
|
// Couldn't load speech, so destroy new item and return
|
|
|
|
delete soundItem;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the item to the list of sounds
|
|
|
|
_sounds.push_front(soundItem);
|
|
|
|
|
|
|
|
// If there are more than 10 sounds loaded, remove the last one,
|
|
|
|
// which is the least recently used of all of them
|
|
|
|
if (_sounds.size() > 10)
|
|
|
|
removeOldest();
|
|
|
|
|
2016-08-04 02:25:47 +00:00
|
|
|
return soundItem->_waveFile;
|
2016-05-05 00:30:52 +00:00
|
|
|
}
|
|
|
|
|
2016-08-04 01:47:11 +00:00
|
|
|
int CSound::playSpeech(CDialogueFile *dialogueFile, int speechId, CProximity &prox) {
|
2016-08-05 01:21:34 +00:00
|
|
|
CWaveFile *waveFile = loadSpeech(dialogueFile, speechId);
|
2016-08-04 02:25:47 +00:00
|
|
|
if (!waveFile)
|
2016-08-04 01:47:11 +00:00
|
|
|
return -1;
|
|
|
|
|
2016-10-24 00:11:30 +00:00
|
|
|
prox._soundDuration = waveFile->getDurationTicks();
|
2016-09-03 14:49:03 +00:00
|
|
|
activateSound(waveFile, prox._disposeAfterUse);
|
2016-08-04 01:47:11 +00:00
|
|
|
|
2016-08-04 02:25:47 +00:00
|
|
|
return _soundManager.playSound(*waveFile, prox);
|
2016-05-07 20:50:39 +00:00
|
|
|
}
|
|
|
|
|
2016-08-07 01:55:19 +00:00
|
|
|
void CSound::stopSound(uint handle) {
|
|
|
|
_soundManager.stopSound(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSound::setCanFree(int handle) {
|
|
|
|
if (handle != 0 && handle != -1)
|
|
|
|
_soundManager.setCanFree(handle);
|
|
|
|
}
|
|
|
|
|
2016-08-07 13:49:10 +00:00
|
|
|
void CSound::updateMixer() {
|
|
|
|
_soundManager.waveMixPump();
|
|
|
|
}
|
|
|
|
|
2016-08-04 01:47:11 +00:00
|
|
|
} // End of namespace Titanic
|