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
|
/* 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/base/base_game.h"
#include "engines/wintermute/base/sound/base_sound_manager.h"
#include "engines/wintermute/base/sound/base_sound_buffer.h"
#include "engines/wintermute/base/base_file_manager.h"
#include "engines/wintermute/wintermute.h"
#include "audio/audiostream.h"
#include "audio/mixer.h"
#ifdef USE_VORBIS
#include "audio/decoders/vorbis.h"
#endif
#include "audio/decoders/wave.h"
#include "audio/decoders/raw.h"
#include "common/system.h"
#include "common/substream.h"
namespace Wintermute {
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
#define MAX_NONSTREAMED_FILE_SIZE 1024*1024
//////////////////////////////////////////////////////////////////////////
BaseSoundBuffer::BaseSoundBuffer(BaseGame *inGame) : BaseClass(inGame) {
_stream = nullptr;
_handle = nullptr;
// _sync = nullptr;
_streamed = false;
_filename = "";
_file = nullptr;
_privateVolume = 255;
_volume = 255;
_pan = 0;
_looping = false;
_loopStart = 0;
_startPos = 0;
_type = Audio::Mixer::kSFXSoundType;
_freezePaused = false;
}
//////////////////////////////////////////////////////////////////////////
BaseSoundBuffer::~BaseSoundBuffer() {
stop();
if (_handle) {
g_system->getMixer()->stopHandle(*_handle);
delete _handle;
_handle = nullptr;
}
delete _stream;
_stream = nullptr;
}
//////////////////////////////////////////////////////////////////////////
void BaseSoundBuffer::setStreaming(bool streamed, uint32 numBlocks, uint32 blockSize) {
_streamed = streamed;
}
//////////////////////////////////////////////////////////////////////////
bool BaseSoundBuffer::loadFromFile(const Common::String &filename, bool forceReload) {
debugC(kWintermuteDebugAudio, "BSoundBuffer::LoadFromFile(%s,%d)", filename.c_str(), forceReload);
// Load a file, but avoid having the File-manager handle the disposal of it.
_file = BaseFileManager::getEngineInstance()->openFile(filename, true, false);
if (!_file) {
_gameRef->LOG(0, "Error opening sound file '%s'", filename.c_str());
return STATUS_FAILED;
}
Common::String strFilename(filename);
strFilename.toLowercase();
if (strFilename.hasSuffix(".ogg")) {
#ifdef USE_VORBIS
_stream = Audio::makeVorbisStream(_file, DisposeAfterUse::YES);
#else
error("BSoundBuffer::LoadFromFile - Ogg Vorbis not supported by this version of ScummVM (please report as this shouldn't trigger)");
#endif
} else if (strFilename.hasSuffix(".wav")) {
int waveSize, waveRate;
byte waveFlags;
uint16 waveType;
if (Audio::loadWAVFromStream(*_file, waveSize, waveRate, waveFlags, &waveType)) {
if (waveType == 1) {
// We need to wrap the file in a substream to make sure the size is right.
_file = new Common::SeekableSubReadStream(_file, _file->pos(), waveSize + _file->pos(), DisposeAfterUse::YES);
_stream = Audio::makeRawStream(_file, waveRate, waveFlags, DisposeAfterUse::YES);
} else {
error("BSoundBuffer::LoadFromFile - WAVE not supported yet for %s with type %d", filename.c_str(), waveType);
}
}
} else {
error("BSoundBuffer::LoadFromFile - Unknown filetype for %s", filename.c_str());
}
if (!_stream) {
return STATUS_FAILED;
}
_filename = filename;
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
bool BaseSoundBuffer::play(bool looping, uint32 startSample) {
if (_handle) {
g_system->getMixer()->stopHandle(*_handle);
delete _handle;
_handle = nullptr;
}
// Store the loop-value for save-games.
setLooping(looping);
if (_stream) {
_stream->seek(startSample);
_handle = new Audio::SoundHandle;
if (_looping) {
if (_loopStart != 0) {
Audio::AudioStream *loopStream = new Audio::SubLoopingAudioStream(_stream, 0, Audio::Timestamp(_loopStart, _stream->getRate()), _stream->getLength(), DisposeAfterUse::NO);
g_system->getMixer()->playStream(_type, _handle, loopStream, -1, _volume, _pan, DisposeAfterUse::YES);
} else {
Audio::AudioStream *loopStream = new Audio::LoopingAudioStream(_stream, 0, DisposeAfterUse::NO);
g_system->getMixer()->playStream(_type, _handle, loopStream, -1, _volume, _pan, DisposeAfterUse::YES);
}
} else {
g_system->getMixer()->playStream(_type, _handle, _stream, -1, _volume, _pan, DisposeAfterUse::NO);
}
}
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
void BaseSoundBuffer::setLooping(bool looping) {
if (isPlaying()) {
// This warning is here, to see if this is ever the case.
warning("BSoundBuffer::SetLooping(%d) - won't change a playing sound", looping); // TODO
}
_looping = looping;
}
//////////////////////////////////////////////////////////////////////////
bool BaseSoundBuffer::resume() {
// If the sound was paused while active:
if (_stream && _handle) {
g_system->getMixer()->pauseHandle(*_handle, false);
} else if (_stream) { // Otherwise we come from a savegame, and thus have no handle
play(_looping, _startPos);
} else {
warning("BaseSoundBuffer::resume - Called without a handle or a stream");
}
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
bool BaseSoundBuffer::stop() {
if (_stream && _handle) {
g_system->getMixer()->stopHandle(*_handle);
}
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
bool BaseSoundBuffer::pause() {
if (_stream && _handle) {
g_system->getMixer()->pauseHandle(*_handle, true);
}
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
uint32 BaseSoundBuffer::getLength() {
if (_stream) {
uint32 len = _stream->getLength().msecs();
return len * 1000;
}
return 0;
}
//////////////////////////////////////////////////////////////////////////
void BaseSoundBuffer::setType(Audio::Mixer::SoundType type) {
_type = type;
}
//////////////////////////////////////////////////////////////////////////
void BaseSoundBuffer::updateVolume() {
setVolume(_privateVolume);
}
//////////////////////////////////////////////////////////////////////////
bool BaseSoundBuffer::setVolume(int volume) {
_volume = volume * _gameRef->_soundMgr->getMasterVolume() / 255;
if (_stream && _handle) {
byte vol = (byte)(_volume);
g_system->getMixer()->setChannelVolume(*_handle, vol);
}
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
bool BaseSoundBuffer::setPrivateVolume(int volume) {
_privateVolume = volume;
return setVolume(_privateVolume);
}
//////////////////////////////////////////////////////////////////////////
bool BaseSoundBuffer::isPlaying() {
if (_stream && _handle) {
return _freezePaused || g_system->getMixer()->isSoundHandleActive(*_handle);
} else {
return false;
}
}
//////////////////////////////////////////////////////////////////////////
uint32 BaseSoundBuffer::getPosition() {
if (_stream && _handle) {
uint32 pos = g_system->getMixer()->getSoundElapsedTime(*_handle);
return pos;
}
return 0;
}
//////////////////////////////////////////////////////////////////////////
bool BaseSoundBuffer::setPosition(uint32 pos) {
if (isPlaying()) {
warning("BaseSoundBuffer::SetPosition - not implemented for playing sounds yet.");
}
_startPos = pos;
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
bool BaseSoundBuffer::setLoopStart(uint32 pos) {
_loopStart = pos;
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
bool BaseSoundBuffer::setPan(float pan) {
pan = MAX(pan, -1.0f);
pan = MIN(pan, 1.0f);
_pan = (int8)(pan * 127);
if (_handle) {
g_system->getMixer()->setChannelBalance(*_handle, _pan);
}
return STATUS_OK;
}
//////////////////////////////////////////////////////////////////////////
bool BaseSoundBuffer::applyFX(TSFXType type, float param1, float param2, float param3, float param4) {
// This function was already stubbed out in WME Lite, and thus isn't reimplemented here either.
switch (type) {
case SFX_ECHO:
//warning("BaseSoundBuffer::ApplyFX(SFX_ECHO, %f, %f, %f, %f) - not implemented yet", param1, param2, param3, param4);
break;
case SFX_REVERB:
//warning("BaseSoundBuffer::ApplyFX(SFX_REVERB, %f, %f, %f, %f) - not implemented yet", param1, param2, param3, param4);
break;
default:
break;
}
return STATUS_OK;
}
int32 BaseSoundBuffer::getPrivateVolume() const {
return _privateVolume;
}
bool BaseSoundBuffer::isLooping() const {
return _looping;
}
bool BaseSoundBuffer::isFreezePaused() const {
return _freezePaused;
}
void BaseSoundBuffer::setFreezePaused(bool freezePaused) {
_freezePaused = freezePaused;
}
Audio::Mixer::SoundType BaseSoundBuffer::getType() const {
return _type;
}
} // End of namespace Wintermute
|