aboutsummaryrefslogtreecommitdiff
path: root/engines/wintermute/BSoundBuffer.cpp
blob: 6b689f59ca4608941dea357b990aa40588e694dc (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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/* 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 "dcgf.h"
#include "BFile.h"
#include "BGame.h"
#include "BSoundMgr.h"
#include "BSoundBuffer.h"
#include "BFileManager.h"
#include "utils.h"
#include "audio/audiostream.h"
#include "audio/mixer.h"
#include "audio/decoders/vorbis.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() {
#if 0
	Stop();

	if (_stream) {
		BASS_StreamFree(_stream);
		_stream = NULL;
	}

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

	SAFE_DELETE_ARRAY(_filename);
#endif
}


//////////////////////////////////////////////////////////////////////////
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;
	}

	_stream = Audio::makeVorbisStream(_file->getMemStream(), DisposeAfterUse::YES);
	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) {
	warning("Play: %s", _filename);
	if (_stream) {
		SetLooping(Looping);
		g_system->getMixer()->playStream(Audio::Mixer::kPlainSoundType, _handle, _stream);
		//BASS_ChannelPlay(_stream, TRUE);
	}
	return S_OK;
}

//////////////////////////////////////////////////////////////////////////
void CBSoundBuffer::SetLooping(bool looping) {
#if 0
	_looping = looping;

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

//////////////////////////////////////////////////////////////////////////
HRESULT CBSoundBuffer::Resume() {
#if 0
	if (_stream) {
		BASS_ChannelPlay(_stream, FALSE);
	}
#endif
	return S_OK;
}


//////////////////////////////////////////////////////////////////////////
HRESULT CBSoundBuffer::Stop() {
#if 0
	if (_stream) {
		BASS_ChannelStop(_stream);
	}
#endif
	return S_OK;
}


//////////////////////////////////////////////////////////////////////////
HRESULT CBSoundBuffer::Pause() {
#if 0
	if (_stream) {
		BASS_ChannelPause(_stream);
	}
#endif
	return S_OK;

}

//////////////////////////////////////////////////////////////////////////
uint32 CBSoundBuffer::GetLength() {
#if 0
	QWORD len = BASS_ChannelGetLength(_stream, BASS_POS_BYTE);
	return 1000 * BASS_ChannelBytes2Seconds(_stream, len);
#endif
	return 0;
}


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


//////////////////////////////////////////////////////////////////////////
HRESULT CBSoundBuffer::SetVolume(int Volume) {
#if 0
	if (_stream) {
		BASS_ChannelSetAttribute(_stream, BASS_ATTRIB_VOL, (float)Volume / 100.0f);
	}
#endif
	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 0
	return _freezePaused || BASS_ChannelIsActive(_stream) == BASS_ACTIVE_PLAYING;
#endif
	return false;
}


//////////////////////////////////////////////////////////////////////////
uint32 CBSoundBuffer::GetPosition() {
#if 0
	if (_stream) {
		QWORD len = BASS_ChannelGetPosition(_stream, BASS_POS_BYTE);
		return 1000 * BASS_ChannelBytes2Seconds(_stream, len);
	} else return 0;
#endif
	return 0;
}


//////////////////////////////////////////////////////////////////////////
HRESULT CBSoundBuffer::SetPosition(uint32 Pos) {
#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 0
	if (_stream) {
		BASS_ChannelSetAttribute(_stream, BASS_ATTRIB_PAN, Pan);
	}
#endif
	return S_OK;
}

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

	case SFX_REVERB:
		break;

	default:
		break;
	}
#endif
	return S_OK;
}

#if 0
//////////////////////////////////////////////////////////////////////////
void CBSoundBuffer::FileCloseProc(void *user) {
	/*
	CBFile* file = static_cast<CBFile*>(user);
	file->Game->_fileManager->CloseFile(file);
	*/
}

//////////////////////////////////////////////////////////////////////////
QWORD CBSoundBuffer::FileLenProc(void *user) {
	CBFile *file = static_cast<CBFile *>(user);
	return file->GetSize();
}

//////////////////////////////////////////////////////////////////////////
uint32 CBSoundBuffer::FileReadProc(void *buffer, uint32 length, void *user) {
	CBFile *file = static_cast<CBFile *>(user);
	uint32 oldPos = file->GetPos();
	file->Read(buffer, length);
	return file->GetPos() - oldPos;
}

//////////////////////////////////////////////////////////////////////////
BOOL CBSoundBuffer::FileSeekProc(QWORD offset, void *user) {
	CBFile *file = static_cast<CBFile *>(user);
	return SUCCEEDED(file->Seek(offset));
}
#endif
} // end of namespace WinterMute