aboutsummaryrefslogtreecommitdiff
path: root/engines/wintermute/Base/BSoundBuffer.cpp
blob: a8c41cd0226f09c745b6a182a28214bba45c17e7 (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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* 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/file/BFile.h"
#include "engines/wintermute/Base/BGame.h"
#include "engines/wintermute/Base/BSoundMgr.h"
#include "engines/wintermute/Base/BSoundBuffer.h"
#include "engines/wintermute/Base/BFileManager.h"
#include "engines/wintermute/utils/utils.h"
#include "audio/audiostream.h"
#include "audio/mixer.h"
#include "audio/decoders/vorbis.h"
#include "audio/decoders/wave.h"
#include "common/system.h"

namespace WinterMute {

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

#define MAX_NONSTREAMED_FILE_SIZE 1024*1024

//////////////////////////////////////////////////////////////////////////
CBSoundBuffer::CBSoundBuffer(CBGame *inGame): CBBase(inGame) {
	_stream = NULL;
	_handle = new Audio::SoundHandle;
//	_sync = NULL;

	_streamed = false;
	_filename = NULL;
	_file = NULL;
	_privateVolume = 100;

	_looping = false;
	_loopStart = 0;

	_type = SOUND_SFX;

	_freezePaused = false;
}


//////////////////////////////////////////////////////////////////////////
CBSoundBuffer::~CBSoundBuffer() {
	Stop();

	if (_handle) {
		delete _handle;
		_handle = NULL;
	}

	if (_file) {
		Game->_fileManager->CloseFile(_file);
		_file = NULL;
	}

	delete[] _filename;
	_filename = NULL;
}


//////////////////////////////////////////////////////////////////////////
void CBSoundBuffer::SetStreaming(bool Streamed, uint32 NumBlocks, uint32 BlockSize) {
	_streamed = Streamed;
}


//////////////////////////////////////////////////////////////////////////
HRESULT CBSoundBuffer::LoadFromFile(const char *Filename, bool ForceReload) {
	warning("BSoundBuffer::LoadFromFile(%s,%d)", Filename, ForceReload);
#if 0
	if (_stream) {
		BASS_StreamFree(_stream);
		_stream = NULL;
	}
#endif
	delete _stream;
	_stream = NULL;

	if (_file) Game->_fileManager->CloseFile(_file);

	_file = Game->_fileManager->OpenFile(Filename);
	if (!_file) {
		Game->LOG(0, "Error opening sound file '%s'", Filename);
		return E_FAIL;
	}
	Common::String strFilename(Filename);
	if (strFilename.hasSuffix(".ogg")) {
		_stream = Audio::makeVorbisStream(_file, DisposeAfterUse::NO);
	} else if (strFilename.hasSuffix(".wav")) {
		warning("BSoundBuffer::LoadFromFile - WAVE not supported yet for %s", Filename);
		//_stream = Audio::makeWAVStream(_file, DisposeAfterUse::NO);
	} else {
		warning("BSoundBuffer::LoadFromFile - Unknown filetype for %s", Filename);
	}
	if (!_stream) {
		return E_FAIL;
	}
	CBUtils::SetString(&_filename, Filename);

	return S_OK;
#if 0
	BASS_FILEPROCS fileProc;
	fileProc.close = CBSoundBuffer::FileCloseProc;
	fileProc.read = CBSoundBuffer::FileReadProc;
	fileProc.seek = CBSoundBuffer::FileSeekProc;
	fileProc.length = CBSoundBuffer::FileLenProc;

	_stream = BASS_StreamCreateFileUser(STREAMFILE_NOBUFFER, 0, &fileProc, (void *)_file);
	if (!_stream) {
		Game->LOG(0, "BASS error: %d while loading '%s'", BASS_ErrorGetCode(), Filename);
		return E_FAIL;
	}

	CBUtils::SetString(&_filename, Filename);

	/*
	HRESULT res;
	bool NewlyCreated = false;

	if(!_soundBuffer || ForceReload || _streamed){
	    if(!_file) _file = Game->_fileManager->OpenFile(Filename);
	    if(!_file){
	        Game->LOG(0, "Error opening sound file '%s'", Filename);
	        return E_FAIL;
	    }
	    // switch to streamed for big files
	    if(!_streamed && (_file->GetSize() > MAX_NONSTREAMED_FILE_SIZE && !Game->_forceNonStreamedSounds)) SetStreaming(true);
	}

	// create buffer
	if(!_soundBuffer){
	    NewlyCreated = true;

	    res = InitializeBuffer(_file);
	    if(FAILED(res)){
	        Game->LOG(res, "Error creating sound buffer for file '%s'", Filename);
	        return res;
	    }
	}



	// store filename
	if(!_filename){
	    _filename = new char[strlen(Filename)+1];
	    strcpy(_filename, Filename);
	}

	// close file (if not streaming)
	if(!_streamed && _file){
	    Game->_fileManager->CloseFile(_file);
	    _file = NULL;
	}
	*/

	return S_OK;
#endif
}


//////////////////////////////////////////////////////////////////////////
HRESULT CBSoundBuffer::Play(bool looping, uint32 startSample) {
	if (startSample != 0) {
		warning("BSoundBuffer::Play - Should start playback at %d, but currently we don't", startSample);
	}
	if (_stream) {
		if (looping) {
			Audio::AudioStream *loopStream = new Audio::LoopingAudioStream(_stream, 0, DisposeAfterUse::NO);
			g_system->getMixer()->playStream(Audio::Mixer::kPlainSoundType, _handle, loopStream, -1, Audio::Mixer::kMaxChannelVolume, 0, DisposeAfterUse::YES);
		} else {
			g_system->getMixer()->playStream(Audio::Mixer::kPlainSoundType, _handle, _stream, -1, Audio::Mixer::kMaxChannelVolume, 0, DisposeAfterUse::NO);
		}
	}
	return S_OK;
}

//////////////////////////////////////////////////////////////////////////
void CBSoundBuffer::SetLooping(bool looping) {
	warning("BSoundBuffer::SetLooping(%d) - not implemented yet", looping);
#if 0
	_looping = looping;

	if (_stream) {
		BASS_ChannelFlags(_stream, looping ? BASS_SAMPLE_LOOP : 0, BASS_SAMPLE_LOOP);
	}
#endif
}

//////////////////////////////////////////////////////////////////////////
HRESULT CBSoundBuffer::Resume() {
	if (_stream && _handle) {
		g_system->getMixer()->pauseHandle(*_handle, false);
	}
	return S_OK;
}


//////////////////////////////////////////////////////////////////////////
HRESULT CBSoundBuffer::Stop() {
	if (_stream && _handle) {
		g_system->getMixer()->stopHandle(*_handle);
	}
	return S_OK;
}


//////////////////////////////////////////////////////////////////////////
HRESULT CBSoundBuffer::Pause() {
	if (_stream && _handle) {
		g_system->getMixer()->pauseHandle(*_handle, true);
	}
	return S_OK;

}

//////////////////////////////////////////////////////////////////////////
uint32 CBSoundBuffer::GetLength() {
	if (_stream) {
		uint32 len = _stream->getLength().msecs();
		return len * 1000;
	}
	return 0;
}


//////////////////////////////////////////////////////////////////////////
void CBSoundBuffer::SetType(TSoundType Type) {
	_type = Type;
}


//////////////////////////////////////////////////////////////////////////
HRESULT CBSoundBuffer::SetVolume(int Volume) {
	if (_stream && _handle) {
		byte vol = Volume / 100.f * Audio::Mixer::kMaxChannelVolume;
		g_system->getMixer()->setChannelVolume(*_handle, vol);
	}
	return S_OK;
}


//////////////////////////////////////////////////////////////////////////
HRESULT CBSoundBuffer::SetPrivateVolume(int Volume) {
#if 0
	_privateVolume = Volume;

	switch (_type) {
	case SOUND_SFX:
		Volume = Game->_soundMgr->_volumeSFX;
		break;
	case SOUND_SPEECH:
		Volume = Game->_soundMgr->_volumeSpeech;
		break;
	case SOUND_MUSIC:
		Volume = Game->_soundMgr->_volumeMusic;
		break;
	}
#endif
	return SetVolume(Volume);
}


//////////////////////////////////////////////////////////////////////////
bool CBSoundBuffer::IsPlaying() {
	if (_stream && _handle) {
		return _freezePaused || g_system->getMixer()->isSoundHandleActive(*_handle);
	} else {
		return false;
	}
}


//////////////////////////////////////////////////////////////////////////
uint32 CBSoundBuffer::GetPosition() {
	if (_stream && _handle) {
		uint32 pos = g_system->getMixer()->getSoundElapsedTime(*_handle);
		return pos;
	}
	return 0;
}


//////////////////////////////////////////////////////////////////////////
HRESULT CBSoundBuffer::SetPosition(uint32 Pos) {
	warning("CBSoundBuffer::SetPosition - not implemented yet");
#if 0
	if (_stream) {
		QWORD pos = BASS_ChannelSeconds2Bytes(_stream, (float)Pos / 1000.0f);
		BASS_ChannelSetPosition(_stream, pos, BASS_POS_BYTE);
	}
#endif
	return S_OK;
}

//////////////////////////////////////////////////////////////////////////
HRESULT CBSoundBuffer::SetLoopStart(uint32 Pos) {
	_loopStart = Pos;
#if 0
	if (_stream) {
		if (_sync) {
			BASS_ChannelRemoveSync(_stream, _sync);
			_sync = NULL;
		}
		if (_loopStart > 0) {
			QWORD len = BASS_ChannelGetLength(_stream, BASS_POS_BYTE);
			_sync = BASS_ChannelSetSync(_stream, BASS_SYNC_POS | BASS_SYNC_MIXTIME, len, CBSoundBuffer::LoopSyncProc, (void *)this);
		}
	}
#endif
	return S_OK;
}
#if 0
//////////////////////////////////////////////////////////////////////////
void CBSoundBuffer::LoopSyncProc(HSYNC handle, uint32 channel, uint32 data, void *user) {
	CBSoundBuffer *soundBuf = static_cast<CBSoundBuffer *>(user);
	QWORD pos = BASS_ChannelSeconds2Bytes(channel, (float)soundBuf->GetLoopStart() / 1000.0f);

	if (!BASS_ChannelSetPosition(channel, pos, BASS_POS_BYTE))
		BASS_ChannelSetPosition(channel, 0, BASS_POS_BYTE);
}
#endif
//////////////////////////////////////////////////////////////////////////
HRESULT CBSoundBuffer::SetPan(float pan) {
	if (_stream) {
		g_system->getMixer()->setChannelBalance(*_handle, pan * 127);
	}
	return S_OK;
}

//////////////////////////////////////////////////////////////////////////
HRESULT CBSoundBuffer::ApplyFX(TSFXType Type, float Param1, float Param2, float Param3, float Param4) {
	warning("CBSoundBuffer::ApplyFX - not implemented yet");
#if 0
	switch (Type) {
	case SFX_ECHO:
		break;

	case SFX_REVERB:
		break;

	default:
		break;
	}
#endif
	return S_OK;
}

} // end of namespace WinterMute