File: sound.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (280 lines) | stat: -rw-r--r-- 8,031 bytes parent folder | download | duplicates (2)
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
/* 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

#include "engines/stark/resources/sound.h"

#include "audio/decoders/vorbis.h"

#include "common/system.h"

#include "engines/stark/formats/iss.h"
#include "engines/stark/formats/xrc.h"
#include "engines/stark/resources/location.h"
#include "engines/stark/services/archiveloader.h"
#include "engines/stark/services/global.h"
#include "engines/stark/services/services.h"
#include "engines/stark/services/stateprovider.h"

namespace Stark {
namespace Resources {

Sound::~Sound() {
}

Sound::Sound(Object *parent, byte subType, uint16 index, const Common::String &name) :
		Object(parent, subType, index, name),
		_enabled(0),
		_looping(false),
		_field_64(0),
		_loopIndefinitely(false),
		_loadFromFile(true),
		_maxDuration(0),
		_stockSoundType(0),
		_field_6C(0),
		_soundType(0),
		_pan(0),
		_volume(0),
		_fadeDurationRemaining(0),
		_fadeTargetVolume(0.0),
		_fadeTargetPan(0.0),
		_shouldStopOnDestroy(true) {
	_type = TYPE;
}

Audio::RewindableAudioStream *Sound::makeAudioStream() {
	Common::SeekableReadStream *stream = nullptr;
	Audio::RewindableAudioStream *audioStream = nullptr;

	// First try the .iss / isn files
	if (_loadFromFile) {
		stream = StarkArchiveLoader->getExternalFile(_filename, _archiveName);
	} else {
		stream = StarkArchiveLoader->getFile(_filename, _archiveName);
	}

	if (stream) {
		audioStream = Formats::makeISSStream(stream, DisposeAfterUse::YES);
	}

	if (!audioStream) {
		// The 2 CD version uses Ogg Vorbis
		Common::Path filename = _filename;
		Common::String baseName(filename.baseName());
		if (baseName.hasSuffix(".iss") || baseName.hasSuffix(".isn") || baseName.hasSuffix(".ssn")) {
			baseName = Common::String(baseName.c_str(), baseName.size() - 4) + ".ovs";
			filename = _filename.getParent().appendComponent(baseName);
		}

		stream = StarkArchiveLoader->getExternalFile(filename, _archiveName);
		if (stream) {
#ifdef USE_VORBIS
			audioStream = Audio::makeVorbisStream(stream, DisposeAfterUse::YES);
#else
			warning("Cannot decode sound '%s', Vorbis support is not compiled in", filename.toString().c_str());
			delete stream;
#endif
		}
	}

	if (!audioStream) {
		warning("Unable to load sound '%s'", _filename.toString().c_str());
	}

	return audioStream;
}

Audio::Mixer::SoundType Sound::getMixerSoundType() {
	switch (_soundType) {
	case kSoundTypeVoice:
		return Audio::Mixer::kSpeechSoundType;
	case kSoundTypeEffect:
		return Audio::Mixer::kSFXSoundType;
	case kSoundTypeMusic:
		return Audio::Mixer::kMusicSoundType;
	default:
		error("Unknown sound type '%d'", _soundType);
	}
}

void Sound::play() {
	if (isPlaying()) {
		return;
	}

	Audio::RewindableAudioStream *rewindableStream = makeAudioStream();

	if (!rewindableStream) {
		return;
	}

	Audio::AudioStream *playStream;
	if (_looping) {
		playStream = Audio::makeLoopingAudioStream(rewindableStream, 0);
	} else {
		playStream = rewindableStream;
	}

	g_system->getMixer()->playStream(getMixerSoundType(), &_handle, playStream, -1,
	                                 _volume * Audio::Mixer::kMaxChannelVolume, _pan * 127);
}

bool Sound::isPlaying() {
	return g_system->getMixer()->isSoundHandleActive(_handle);
}

void Sound::stop() {
	g_system->getMixer()->stopHandle(_handle);
	_handle = Audio::SoundHandle();
}

void Sound::onPreDestroy() {
	Object::onPreDestroy();

	if (_shouldStopOnDestroy) {
		stop();
	}
}

void Sound::readData(Formats::XRCReadStream *stream) {
	_filename = stream->readString();
	_enabled = stream->readUint32LE();
	_looping = stream->readBool();
	_field_64 = stream->readUint32LE();
	_loopIndefinitely = stream->readBool();
	_maxDuration = stream->readUint32LE();
	_loadFromFile = stream->readBool(); // Used only in the 4CD version
	_stockSoundType = stream->readUint32LE();
	_soundName = stream->readString();
	_field_6C = stream->readUint32LE();
	_soundType = stream->readUint32LE();
	_pan = stream->readFloatLE();
	_volume = stream->readFloatLE();
	_archiveName = stream->getArchiveName();
}

void Sound::printData() {
	debug("filename: %s", _filename.toString().c_str());
	debug("enabled: %d", _enabled);
	debug("looping: %d", _looping);
	debug("field_64: %d", _field_64);
	debug("loopIndefinitely: %d", _loopIndefinitely);
	debug("maxDuration: %d", _maxDuration);
	debug("loadFromFile: %d", _loadFromFile);
	debug("stockSoundType: %d", _stockSoundType);
	debug("soundName: %s", _soundName.c_str());
	debug("field_6C: %d", _field_6C);
	debug("soundType: %d", _soundType);
	debug("pan: %f", _pan);
	debug("volume: %f", _volume);
}

void Sound::onGameLoop() {
	Object::onGameLoop();

	if (_subType == kSoundBackground && !isPlaying()) {
		Location *location = StarkGlobal->getCurrent()->getLocation();
		if (location->getName() != "Amongst Stalls" || StarkGlobal->getCurrentChapter() < 100) {
			play();
		}
	}

	if (_looping && !_loopIndefinitely) {
		// Automatically stop after the maximum run time has been reached
		uint32 elapsedTime = g_system->getMixer()->getSoundElapsedTime(_handle);
		if (elapsedTime > _maxDuration) {
			stop();
		}
	}

	if (_fadeDurationRemaining > 0 && isPlaying()) {
		_volume += (_fadeTargetVolume - _volume) * StarkGlobal->getMillisecondsPerGameloop() / (float) _fadeDurationRemaining;
		_pan += (_fadeTargetPan - _pan) * StarkGlobal->getMillisecondsPerGameloop() / (float) _fadeDurationRemaining;

		_fadeDurationRemaining -= StarkGlobal->getMillisecondsPerGameloop();

		if (_fadeDurationRemaining <= 0) {
			_fadeDurationRemaining = 0;

			_volume = _fadeTargetVolume;
			_pan = _fadeTargetPan;
		}

		g_system->getMixer()->setChannelVolume(_handle, _volume * Audio::Mixer::kMaxChannelVolume);
		g_system->getMixer()->setChannelBalance(_handle, _pan * 127);
	}
}

uint32 Sound::getStockSoundType() const {
	return _stockSoundType;
}

void Sound::changeVolumePan(int32 volume, int32 pan, int32 duration) {
	if (isPlaying()) {
		_fadeDurationRemaining = duration;

		if (_fadeDurationRemaining > 0) {
			_fadeTargetVolume = volume / 100.0f;
			_fadeTargetPan = pan / 100.0f;
		} else {
			_volume = volume / 100.0f;
			_pan = pan / 100.0f;

			g_system->getMixer()->setChannelVolume(_handle, _volume * Audio::Mixer::kMaxChannelVolume);
			g_system->getMixer()->setChannelBalance(_handle, _pan * 127);
		}
	} else {
		if (_fadeDurationRemaining == 0) {
			_volume = volume / 100.0f;
			_pan = pan / 100.0f;
		}
	}
}

void Sound::saveLoadCurrent(ResourceSerializer *serializer) {
	bool playing = isPlaying();
	serializer->syncAsUint32LE(playing);

	if (_subType != kSoundBackground && playing) {
		uint32 elapsed = g_system->getMixer()->getSoundElapsedTime(_handle);
		serializer->syncAsUint32LE(elapsed);
		serializer->syncAsFloat(_volume);
		serializer->syncAsFloat(_pan);
		serializer->syncAsUint32LE(_fadeDurationRemaining);
		serializer->syncAsFloat(_fadeTargetVolume);
		serializer->syncAsFloat(_fadeTargetPan);

		if (serializer->isLoading()) {
			play();
			// TODO: Seek to the "elapsed" position
		}
	}
}

void Sound::onEnginePause(bool pause) {
	g_system->getMixer()->pauseHandle(_handle, pause);
}

void Sound::setStopOnDestroy(bool stopOnDestroy) {
	_shouldStopOnDestroy = stopOnDestroy;
}

} // End of namespace Resources
} // End of namespace Stark