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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
|
/* 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.
*
*/
#include "lastexpress/sound/entry.h"
#include "lastexpress/game/logic.h"
#include "lastexpress/game/savepoint.h"
#include "lastexpress/game/state.h"
#include "lastexpress/sound/queue.h"
#include "lastexpress/sound/sound.h"
#include "lastexpress/graphics.h"
#include "lastexpress/lastexpress.h"
#include "lastexpress/resource.h"
namespace LastExpress {
#define SOUNDCACHE_ENTRY_SIZE 92160
#define FILTER_BUFFER_SIZE 2940
//////////////////////////////////////////////////////////////////////////
// SoundEntry
//////////////////////////////////////////////////////////////////////////
SoundEntry::SoundEntry(LastExpressEngine *engine) : _engine(engine) {
_type = kSoundTypeNone;
_currentDataPtr = NULL;
_blockCount = 0;
_time = 0;
_stream = NULL;
_field_34 = 0;
_field_38 = 0;
_field_3C = 0;
_variant = 0;
_entity = kEntityPlayer;
_field_48 = 0;
_priority = 0;
_subtitle = NULL;
_soundStream = NULL;
_queued = false;
}
SoundEntry::~SoundEntry() {
// Entries that have been queued will have their streamed disposed automatically
if (!_soundStream)
SAFE_DELETE(_stream);
SAFE_DELETE(_soundStream);
free(_currentDataPtr);
_subtitle = NULL;
_stream = NULL;
// Zero passed pointers
_engine = NULL;
}
void SoundEntry::open(Common::String name, SoundFlag flag, int priority) {
_priority = priority;
setType(flag);
setupStatus(flag);
loadStream(name);
}
void SoundEntry::close() {
_status.status |= kSoundStatusClosed;
// Loop until ready
//while (!(_status.status1 & 4) && !(getSoundQueue()->getFlag() & 8) && (getSoundQueue()->getFlag() & 1))
// ; // empty loop body
// The original game remove the entry from the cache here,
// but since we are called from within an iterator loop
// we will remove the entry there
// removeFromCache(entry);
if (_subtitle) {
_subtitle->draw();
SAFE_DELETE(_subtitle);
}
if (_entity) {
if (_entity == kEntitySteam)
getSound()->playLoopingSound(2);
else if (_entity != kEntityTrain)
getSavePoints()->push(kEntityPlayer, _entity, kActionEndSound);
}
}
void SoundEntry::play() {
if (!_stream)
error("[SoundEntry::play] stream has been disposed");
// Prepare sound stream
if (!_soundStream)
_soundStream = new StreamedSound();
// Compute current filter id
int32 filterId = _status.status & kSoundStatusFilter;
// TODO adjust status (based on stepIndex)
if (_queued) {
_soundStream->setFilterId(filterId);
} else {
_stream->seek(0);
// Load the stream and start playing
_soundStream->load(_stream, filterId);
_queued = true;
}
}
bool SoundEntry::isFinished() {
if (!_stream)
return true;
if (!_soundStream || !_queued)
return false;
// TODO check that all data has been queued
return _soundStream->isFinished();
}
void SoundEntry::setType(SoundFlag flag) {
switch (flag & kFlagType9) {
default:
case kFlagNone:
_type = getSoundQueue()->getCurrentType();
getSoundQueue()->setCurrentType((SoundType)(_type + 1));
break;
case kFlagType1_2: {
SoundEntry *previous2 = getSoundQueue()->getEntry(kSoundType2);
if (previous2)
previous2->update(0);
SoundEntry *previous = getSoundQueue()->getEntry(kSoundType1);
if (previous) {
previous->setType(kSoundType2);
previous->update(0);
}
_type = kSoundType1;
}
break;
case kFlagType3: {
SoundEntry *previous = getSoundQueue()->getEntry(kSoundType3);
if (previous) {
previous->setType(kSoundType4);
previous->update(0);
}
_type = kSoundType11;
}
break;
case kFlagType7: {
SoundEntry *previous = getSoundQueue()->getEntry(kSoundType7);
if (previous)
previous->setType(kSoundType8);
_type = kSoundType7;
}
break;
case kFlagType9: {
SoundEntry *previous = getSoundQueue()->getEntry(kSoundType9);
if (previous)
previous->setType(kSoundType10);
_type = kSoundType9;
}
break;
case kFlagType11: {
SoundEntry *previous = getSoundQueue()->getEntry(kSoundType11);
if (previous)
previous->setType(kSoundType14);
_type = kSoundType11;
}
break;
case kFlagType13: {
SoundEntry *previous = getSoundQueue()->getEntry(kSoundType13);
if (previous)
previous->setType(kSoundType14);
_type = kSoundType13;
}
break;
}
}
void SoundEntry::setupStatus(SoundFlag flag) {
SoundStatus statusFlag = (SoundStatus)flag;
if (!((statusFlag & 0xFF) & kSoundStatusFilter))
statusFlag = (SoundStatus)(statusFlag | kSoundStatusCached);
if (((statusFlag & 0xFF00) >> 8) & kSoundStatusClear0)
_status.status = (uint32)statusFlag;
else
_status.status = (statusFlag | kSoundStatusClear4);
}
void SoundEntry::loadStream(Common::String name) {
_name2 = name;
// Load sound data
_stream = getArchive(name);
if (!_stream)
_stream = getArchive("DEFAULT.SND");
if (!_stream)
_status.status = kSoundStatusClosed;
}
void SoundEntry::update(uint val) {
if (!(_status.status3 & 64)) {
int value2 = val;
_status.status |= kSoundStatus_100000;
if (val) {
if (getSoundQueue()->getFlag() & 32) {
_variant = val;
value2 = val * 2 + 1;
}
_field_3C = value2;
} else {
_field_3C = 0;
_status.status |= kSoundStatus_40000000;
}
}
}
bool SoundEntry::updateSound() {
bool result;
char sub[16];
if (_status.status2 & 4) {
result = false;
} else {
if (_status.status2 & 0x80) {
if (_field_48 <= getSound()->getData2()) {
_status.status |= 0x20;
_status.status &= ~0x8000;
strcpy(sub, _name2.c_str());
int l = strlen(sub) + 1;
if (l - 1 > 4)
sub[l - (1 + 4)] = 0;
showSubtitle(sub);
}
} else {
if (!(getSoundQueue()->getFlag() & 0x20)) {
if (!(_status.status3 & 8)) {
if (_entity) {
if (_entity < 0x80) {
updateEntryFlag(getSound()->getSoundFlag(_entity));
}
}
}
}
//if (status.status2 & 0x40 && !((uint32)_status.status & 0x180) && v1->soundBuffer)
// Sound_FillSoundBuffer(v1);
}
result = true;
}
return result;
}
void SoundEntry::updateEntryFlag(SoundFlag flag) {
if (flag) {
if (getSoundQueue()->getFlag() & 0x20 && _type != kSoundType9 && _type != kSoundType7)
update(flag);
else
_status.status = flag + (_status.status & ~0x1F);
} else {
_variant = 0;
_status.status |= 0x80u;
_status.status &= ~0x10001F;
}
}
void SoundEntry::updateState() {
if (getSoundQueue()->getFlag() & 32) {
if (_type != kSoundType9 && _type != kSoundType7 && _type != kSoundType5) {
uint32 variant = _status.status & kSoundStatusFilter;
_status.status &= kSoundStatusClearAll;
_variant = variant;
_status.status |= variant * 2 + 1;
}
}
_status.status |= kSoundStatus_20;
}
void SoundEntry::reset() {
_status.status |= kSoundStatusClosed;
_entity = kEntityPlayer;
if (_stream) {
if (!_soundStream) {
SAFE_DELETE(_stream);
} else {
// the original stream will be disposed
_soundStream->stop();
SAFE_DELETE(_soundStream);
}
_stream = NULL;
}
}
void SoundEntry::showSubtitle(Common::String filename) {
_subtitle = new SubtitleEntry(_engine);
_subtitle->load(filename, this);
if (_subtitle->getStatus().status2 & 4) {
_subtitle->draw();
SAFE_DELETE(_subtitle);
} else {
_status.status |= kSoundStatus_20000;
}
}
void SoundEntry::saveLoadWithSerializer(Common::Serializer &s) {
if (_name2.matchString("NISSND?") && (_status.status & kFlagType7) != kFlag3) {
s.syncAsUint32LE(_status.status);
s.syncAsUint32LE(_type);
s.syncAsUint32LE(_blockCount); // field_8;
s.syncAsUint32LE(_time);
s.syncAsUint32LE(_field_34); // field_10;
s.syncAsUint32LE(_field_38); // field_14;
s.syncAsUint32LE(_entity);
uint32 delta = (uint32)_field_48 - getSound()->getData2();
if (delta > kFlag8)
delta = 0;
s.syncAsUint32LE(delta);
s.syncAsUint32LE(_priority);
char name1[16];
strcpy((char *)&name1, _name1.c_str());
s.syncBytes((byte *)&name1, 16);
char name2[16];
strcpy((char *)&name2, _name2.c_str());
s.syncBytes((byte *)&name2, 16);
}
}
//////////////////////////////////////////////////////////////////////////
// SubtitleEntry
//////////////////////////////////////////////////////////////////////////
SubtitleEntry::SubtitleEntry(LastExpressEngine *engine) : _engine(engine) {
_sound = NULL;
_data = NULL;
}
SubtitleEntry::~SubtitleEntry() {
SAFE_DELETE(_data);
// Zero-out passed pointers
_sound = NULL;
_engine = NULL;
}
void SubtitleEntry::load(Common::String filename, SoundEntry *soundEntry) {
// Add ourselves to the list of active subtitles
getSoundQueue()->addSubtitle(this);
// Set sound entry and filename
_filename = filename + ".SBE";
_sound = soundEntry;
// Load subtitle data
if (_engine->getResourceManager()->hasFile(_filename)) {
if (getSoundQueue()->getSubtitleFlag() & 2)
return;
loadData();
} else {
_status.status = kSoundStatus_400;
}
}
void SubtitleEntry::loadData() {
_data = new SubtitleManager(_engine->getFont());
_data->load(getArchive(_filename));
getSoundQueue()->setSubtitleFlag(getSoundQueue()->getSubtitleFlag() | 2);
getSoundQueue()->setCurrentSubtitle(this);
}
void SubtitleEntry::setupAndDraw() {
if (!_sound)
error("[SubtitleEntry::setupAndDraw] Sound entry not initialized");
if (!_data) {
_data = new SubtitleManager(_engine->getFont());
_data->load(getArchive(_filename));
}
if (_data->getMaxTime() > _sound->getTime()) {
_status.status = kSoundStatus_400;
} else {
_data->setTime((uint16)_sound->getTime());
if (getSoundQueue()->getSubtitleFlag() & 1)
drawOnScreen();
}
getSoundQueue()->setCurrentSubtitle(this);
// TODO Missing code
}
void SubtitleEntry::draw() {
// Remove ourselves from the queue
getSoundQueue()->removeSubtitle(this);
if (this == getSoundQueue()->getCurrentSubtitle()) {
drawOnScreen();
getSoundQueue()->setCurrentSubtitle(NULL);
getSoundQueue()->setSubtitleFlag(0);
}
}
void SubtitleEntry::drawOnScreen() {
if (_data == NULL)
return;
getSoundQueue()->setSubtitleFlag(getSoundQueue()->getSubtitleFlag() & -2);
_engine->getGraphicsManager()->draw(_data, GraphicsManager::kBackgroundOverlay);
}
} // End of namespace LastExpress
|