aboutsummaryrefslogtreecommitdiff
path: root/engines/wintermute/Base/BSoundMgr.cpp
blob: 18e674687c950379cab56ba0f8fc18abe5fe85de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* 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.
 *
 */

/*
 * This file is based on WME Lite.
 * http://dead-code.org/redir.php?target=wmelite
 * Copyright (c) 2011 Jan Nedoma
 */

#include "engines/wintermute/dcgf.h"
#include "engines/wintermute/Base/BSoundMgr.h"
#include "engines/wintermute/Base/BRegistry.h"
#include "engines/wintermute/utils/PathUtil.h"
#include "engines/wintermute/utils/StringUtil.h"
#include "engines/wintermute/Base/BGame.h"
#include "engines/wintermute/Base/BFileManager.h"
#include "engines/wintermute/Base/BSoundBuffer.h"

namespace WinterMute {

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

//IMPLEMENT_PERSISTENT(CBSoundMgr, true);

//////////////////////////////////////////////////////////////////////////
CBSoundMgr::CBSoundMgr(CBGame *inGame): CBBase(inGame) {
	_soundAvailable = false;

	_volumeSFX = _volumeSpeech = _volumeMusic = _volumeMaster = 100;
}


//////////////////////////////////////////////////////////////////////////
CBSoundMgr::~CBSoundMgr() {
	saveSettings();
	cleanup();
}


//////////////////////////////////////////////////////////////////////////
HRESULT CBSoundMgr::cleanup() {
	for (int i = 0; i < _sounds.GetSize(); i++) delete _sounds[i];
	_sounds.RemoveAll();
#if 0
	BASS_Free();
#endif
	return S_OK;
}

//////////////////////////////////////////////////////////////////////////
void CBSoundMgr::saveSettings() {
	if (_soundAvailable) {
		Game->_registry->WriteInt("Audio", "MasterVolume", _volumeMaster);

		Game->_registry->WriteInt("Audio", "SFXVolume",    _volumeSFX);
		Game->_registry->WriteInt("Audio", "SpeechVolume", _volumeSpeech);
		Game->_registry->WriteInt("Audio", "MusicVolume",  _volumeMusic);
	}
}

//////////////////////////////////////////////////////////////////////////
HRESULT CBSoundMgr::initialize() {
	_soundAvailable = false;
#if 0

#ifdef __IPHONEOS__
#define BASS_CONFIG_IOS_MIXAUDIO 34
	BASS_SetConfig(BASS_CONFIG_IOS_MIXAUDIO, 0);
#endif


	if (HIWORD(BASS_GetVersion()) != BASSVERSION) {
		Game->LOG(0, "An incorrect version of BASS was loaded");
		return E_FAIL;
	}

	if (!BASS_Init(-1, 44100, 0, 0, NULL)) {
		Game->LOG(0, "Can't initialize sound device");
		return E_FAIL;
	}
#endif

	_volumeMaster = Game->_registry->ReadInt("Audio", "MasterVolume", 100);

	_volumeSFX    = Game->_registry->ReadInt("Audio", "SFXVolume",    100);
	_volumeSpeech = Game->_registry->ReadInt("Audio", "SpeechVolume", 100);
	_volumeMusic  = Game->_registry->ReadInt("Audio", "MusicVolume",  100);

	_soundAvailable = true;
	setMasterVolumePercent(_volumeMaster);

	return S_OK;
}


//////////////////////////////////////////////////////////////////////////
HRESULT CBSoundMgr::initLoop() {
	if (!_soundAvailable) return S_OK;
#if 0

	BASS_Update(500);
#endif
	return S_OK;
}


//////////////////////////////////////////////////////////////////////////
CBSoundBuffer *CBSoundMgr::addSound(const char *Filename, TSoundType Type, bool Streamed) {
	if (!_soundAvailable) return NULL;

	CBSoundBuffer *sound;

	// try to switch WAV to OGG file (if available)
	AnsiString ext = PathUtil::getExtension(Filename);
	if (StringUtil::compareNoCase(ext, "wav")) {
		AnsiString path = PathUtil::getDirectoryName(Filename);
		AnsiString name = PathUtil::getFileNameWithoutExtension(Filename);

		AnsiString newFile = PathUtil::combine(path, name + "ogg");
		if (Game->_fileManager->hasFile(newFile)) {
			Filename = newFile.c_str();
		}
	}

	sound = new CBSoundBuffer(Game);
	if (!sound) return NULL;

	sound->setStreaming(Streamed);
	sound->setType(Type);


	HRESULT res = sound->loadFromFile(Filename);
	if (FAILED(res)) {
		Game->LOG(res, "Error loading sound '%s'", Filename);
		delete sound;
		return NULL;
	}

	// set volume appropriately
	switch (Type) {
	case SOUND_SFX:
		sound->setVolume(_volumeSFX);
		break;
	case SOUND_SPEECH:
		sound->setVolume(_volumeSpeech);
		break;
	case SOUND_MUSIC:
		sound->setVolume(_volumeMusic);
		break;
	}

	// register sound
	_sounds.Add(sound);

	return sound;

	return NULL;
}

//////////////////////////////////////////////////////////////////////////
HRESULT CBSoundMgr::addSound(CBSoundBuffer *Sound, TSoundType Type) {
	if (!Sound) return E_FAIL;

	// set volume appropriately
	switch (Type) {
	case SOUND_SFX:
		Sound->setVolume(_volumeSFX);
		break;
	case SOUND_SPEECH:
		Sound->setVolume(_volumeSpeech);
		break;
	case SOUND_MUSIC:
		Sound->setVolume(_volumeMusic);
		break;
	}

	// register sound
	_sounds.Add(Sound);

	return S_OK;
}

//////////////////////////////////////////////////////////////////////////
HRESULT CBSoundMgr::removeSound(CBSoundBuffer *Sound) {

	for (int i = 0; i < _sounds.GetSize(); i++) {
		if (_sounds[i] == Sound) {
			delete _sounds[i];
			_sounds.RemoveAt(i);
			return S_OK;
		}
	}

	return E_FAIL;
}


//////////////////////////////////////////////////////////////////////////
HRESULT CBSoundMgr::setVolume(TSoundType Type, int Volume) {
	if (!_soundAvailable) return S_OK;

	switch (Type) {
	case SOUND_SFX:
		_volumeSFX = Volume;
		break;
	case SOUND_SPEECH:
		_volumeSpeech = Volume;
		break;
	case SOUND_MUSIC:
		_volumeMusic  = Volume;
		break;
	}

	for (int i = 0; i < _sounds.GetSize(); i++) {
		if (_sounds[i]->_type == Type) _sounds[i]->setVolume(Volume);
	}

	return S_OK;
}

//////////////////////////////////////////////////////////////////////////
HRESULT CBSoundMgr::setVolumePercent(TSoundType Type, byte Percent) {
	return setVolume(Type, Percent);
}


//////////////////////////////////////////////////////////////////////////
byte CBSoundMgr::getVolumePercent(TSoundType Type) {
	int Volume = 0;
	switch (Type) {
	case SOUND_SFX:
		Volume = _volumeSFX;
		break;
	case SOUND_SPEECH:
		Volume = _volumeSpeech;
		break;
	case SOUND_MUSIC:
		Volume = _volumeMusic;
		break;
	default:
		error("Sound-type not set");
		break;
	}

	return (byte)Volume;
}


//////////////////////////////////////////////////////////////////////////
HRESULT CBSoundMgr::setMasterVolumePercent(byte  Percent) {
	_volumeMaster = Percent;
#if 0
	BASS_SetConfig(BASS_CONFIG_GVOL_STREAM, (uint32)(10000.0f / 100.0f * (float)Percent));
#endif
	return S_OK;
}


//////////////////////////////////////////////////////////////////////////
byte CBSoundMgr::getMasterVolumePercent() {
#if 0
	uint32 val = BASS_GetConfig(BASS_CONFIG_GVOL_STREAM);
	return (float)val / 10000.0f * 100.0f;
#endif
	return 0;
}



//////////////////////////////////////////////////////////////////////////
HRESULT CBSoundMgr::pauseAll(bool IncludingMusic) {

	for (int i = 0; i < _sounds.GetSize(); i++) {
		if (_sounds[i]->isPlaying() && (_sounds[i]->_type != SOUND_MUSIC || IncludingMusic)) {
			_sounds[i]->pause();
			_sounds[i]->_freezePaused = true;
		}
	}

	return S_OK;
}


//////////////////////////////////////////////////////////////////////////
HRESULT CBSoundMgr::resumeAll() {

	for (int i = 0; i < _sounds.GetSize(); i++) {
		if (_sounds[i]->_freezePaused) {
			_sounds[i]->resume();
			_sounds[i]->_freezePaused = false;
		}
	}

	return S_OK;
}


//////////////////////////////////////////////////////////////////////////
float CBSoundMgr::posToPan(int X, int Y) {
	float relPos = (float)X / ((float)Game->_renderer->_width);

	float minPan = -0.7f;
	float maxPan = 0.7f;

	return minPan + relPos * (maxPan - minPan);
}

} // end of namespace WinterMute