1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "titanic/sound/sound.h"
24 #include "titanic/game_manager.h"
25 #include "titanic/titanic.h"
26 
27 namespace Titanic {
28 
~CSoundItem()29 CSoundItem::~CSoundItem() {
30 	delete _waveFile;
31 }
32 
33 /*------------------------------------------------------------------------*/
34 
CSound(CGameManager * owner,Audio::Mixer * mixer)35 CSound::CSound(CGameManager *owner, Audio::Mixer *mixer) :
36 		_gameManager(owner), _soundManager(mixer) {
37 	g_vm->_movieManager.setSoundManager(&_soundManager);
38 }
39 
~CSound()40 CSound::~CSound() {
41 	_soundManager.qsWaveMixCloseSession();
42 	_sounds.destroyContents();
43 }
44 
save(SimpleFile * file) const45 void CSound::save(SimpleFile *file) const {
46 	_soundManager.save(file);
47 }
48 
load(SimpleFile * file)49 void CSound::load(SimpleFile *file) {
50 	_soundManager.load(file);
51 }
52 
preLoad()53 void CSound::preLoad() {
54 	_soundManager.preLoad();
55 
56 	if (_gameManager)
57 		_gameManager->_musicRoom.destroyMusicHandler();
58 }
59 
preEnterView(CViewItem * newView,bool isNewRoom)60 void CSound::preEnterView(CViewItem *newView, bool isNewRoom) {
61 	CNodeItem *node = newView->findNode();
62 	double xp, yp, zp;
63 	node->getPosition(xp, yp, zp);
64 
65 	double cosVal = cos(newView->_angle);
66 	double sinVal = -sin(newView->_angle);
67 
68 	_soundManager.setListenerPosition(xp, yp, zp, cosVal, sinVal, 0, isNewRoom);
69 }
70 
isActive(int handle)71 bool CSound::isActive(int handle) {
72 	if (handle != 0 && handle != -1)
73 		return _soundManager.isActive(handle);
74 
75 	return false;
76 }
77 
setVolume(uint handle,uint volume,uint seconds)78 void CSound::setVolume(uint handle, uint volume, uint seconds) {
79 	_soundManager.setVolume(handle, volume, seconds);
80 }
81 
activateSound(CWaveFile * waveFile,DisposeAfterUse::Flag disposeAfterUse)82 void CSound::activateSound(CWaveFile *waveFile, DisposeAfterUse::Flag disposeAfterUse) {
83 	for (CSoundItemList::iterator i = _sounds.begin(); i != _sounds.end(); ++i) {
84 		CSoundItem *sound = *i;
85 		if (sound->_waveFile == waveFile) {
86 			sound->_active = true;
87 			sound->_disposeAfterUse = disposeAfterUse;
88 
89 			// Anything bigger than 50Kb is automatically flagged to be free when finished
90 			if (waveFile->size() > (50 * 1024))
91 				sound->_disposeAfterUse = DisposeAfterUse::YES;
92 			break;
93 		}
94 	}
95 }
96 
stopChannel(int channel)97 void CSound::stopChannel(int channel) {
98 	_soundManager.stopChannel(channel);
99 }
100 
checkSounds()101 void CSound::checkSounds() {
102 	for (CSoundItemList::iterator i = _sounds.begin(); i != _sounds.end(); ) {
103 		CSoundItem *soundItem = *i;
104 
105 		if (soundItem->_active && soundItem->_disposeAfterUse == DisposeAfterUse::YES) {
106 			if (!_soundManager.isActive(soundItem->_waveFile)) {
107 				i = _sounds.erase(i);
108 				delete soundItem;
109 				continue;
110 			}
111 		}
112 
113 		++i;
114 	}
115 }
116 
removeOldest()117 void CSound::removeOldest() {
118 	for (CSoundItemList::iterator i = _sounds.reverse_begin();
119 			i != _sounds.end(); --i) {
120 		CSoundItem *soundItem = *i;
121 		if (soundItem->_active && !_soundManager.isActive(soundItem->_waveFile)) {
122 			_sounds.remove(soundItem);
123 			delete soundItem;
124 			break;
125 		}
126 	}
127 }
128 
getTrueTalkSound(CDialogueFile * dialogueFile,int index)129 CWaveFile *CSound::getTrueTalkSound(CDialogueFile *dialogueFile, int index) {
130 	return loadSpeech(dialogueFile, index);
131 }
132 
loadSound(const CString & name)133 CWaveFile *CSound::loadSound(const CString &name) {
134 	checkSounds();
135 
136 	// Check whether an entry for the given name is already active
137 	for (CSoundItemList::iterator i = _sounds.begin(); i != _sounds.end(); ++i) {
138 		CSoundItem *soundItem = *i;
139 		if (soundItem->_name == name) {
140 			// Found it, so move it to the front of the list and return
141 			_sounds.remove(soundItem);
142 			_sounds.push_front(soundItem);
143 			return soundItem->_waveFile;
144 		}
145 	}
146 
147 	// Create new sound item
148 	CSoundItem *soundItem = new CSoundItem(name);
149 	soundItem->_waveFile = _soundManager.loadSound(name);
150 
151 	if (!soundItem->_waveFile) {
152 		// Couldn't load sound, so destroy new item and return
153 		delete soundItem;
154 		return 0;
155 	}
156 
157 	// Add the item to the list of sounds
158 	_sounds.push_front(soundItem);
159 
160 	// If there are more than 10 sounds loaded, remove the last one,
161 	// which is the least recently used of all of them
162 	if (_sounds.size() > 10)
163 		removeOldest();
164 
165 	return soundItem->_waveFile;
166 }
167 
playSound(const CString & name,CProximity & prox)168 int CSound::playSound(const CString &name, CProximity &prox) {
169 	CWaveFile *waveFile  = loadSound(name);
170 	if (!waveFile)
171 		return -1;
172 
173 	prox._soundDuration = waveFile->getDurationTicks();
174 	if (prox._soundType != Audio::Mixer::kPlainSoundType)
175 		waveFile->_soundType = prox._soundType;
176 
177 	activateSound(waveFile, prox._disposeAfterUse);
178 
179 	return _soundManager.playSound(*waveFile, prox);
180 }
181 
loadSpeech(CDialogueFile * dialogueFile,int speechId)182 CWaveFile *CSound::loadSpeech(CDialogueFile *dialogueFile, int speechId) {
183 	checkSounds();
184 
185 	// Check whether an entry for the given name is already active
186 	for (CSoundItemList::iterator i = _sounds.begin(); i != _sounds.end(); ++i) {
187 		CSoundItem *soundItem = *i;
188 		if (soundItem->_dialogueFileHandle == dialogueFile->getFile()
189 				&& soundItem->_speechId == speechId) {
190 			// Found it, so move it to the front of the list and return
191 			_sounds.remove(soundItem);
192 			_sounds.push_front(soundItem);
193 			return soundItem->_waveFile;
194 		}
195 	}
196 
197 	// Create new sound item
198 	CSoundItem *soundItem = new CSoundItem(dialogueFile->getFile(), speechId);
199 	soundItem->_waveFile = _soundManager.loadSpeech(dialogueFile, speechId);
200 
201 	if (!soundItem->_waveFile) {
202 		// Couldn't load speech, so destroy new item and return
203 		delete soundItem;
204 		return 0;
205 	}
206 
207 	// Add the item to the list of sounds
208 	_sounds.push_front(soundItem);
209 
210 	// If there are more than 10 sounds loaded, remove the last one,
211 	// which is the least recently used of all of them
212 	if (_sounds.size() > 10)
213 		removeOldest();
214 
215 	return soundItem->_waveFile;
216 }
217 
playSpeech(CDialogueFile * dialogueFile,int speechId,CProximity & prox)218 int CSound::playSpeech(CDialogueFile *dialogueFile, int speechId, CProximity &prox) {
219 	CWaveFile *waveFile = loadSpeech(dialogueFile, speechId);
220 	if (!waveFile)
221 		return -1;
222 
223 	prox._soundDuration = waveFile->getDurationTicks();
224 	if (prox._soundType != Audio::Mixer::kPlainSoundType)
225 		waveFile->_soundType = prox._soundType;
226 
227 	activateSound(waveFile, prox._disposeAfterUse);
228 	return _soundManager.playSound(*waveFile, prox);
229 }
230 
stopSound(uint handle)231 void CSound::stopSound(uint handle) {
232 	_soundManager.stopSound(handle);
233 }
234 
setCanFree(int handle)235 void CSound::setCanFree(int handle) {
236 	if (handle != 0 && handle != -1)
237 		_soundManager.setCanFree(handle);
238 }
239 
updateMixer()240 void CSound::updateMixer() {
241 	_soundManager.waveMixPump();
242 }
243 
244 } // End of namespace Titanic
245