File: digi.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-- 7,310 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 "audio/audiostream.h"
#include "audio/mixer.h"
#include "audio/decoders/raw.h"
#include "m4/platform/sound/digi.h"
#include "m4/adv_r/adv_file.h"
#include "m4/core/imath.h"
#include "m4/fileio/extensions.h"
#include "m4/vars.h"

namespace M4 {
namespace Sound {

Digi::~Digi() {
	unload_sounds();
}

void Digi::loadFootstepSounds(const char **names) {
	if (!_sounds.empty())
		unload_sounds();

	if (names) {
		for (; *names; ++names)
			preload(*names, true, NOWHERE);
	}
}

void Digi::unload_sounds() {
	_mixer->stopAll();

	for (auto it = _sounds.begin(); it != _sounds.end(); ++it) {
		rtoss(it->_value._filename);
		free(it->_value._data);
	}

	_sounds.clear();
}

bool Digi::preload(const Common::String &name, bool isFootsteps, int roomNum) {
	MemHandle workHandle;
	int32 assetSize;

	if (_sounds.contains(name))
		return true;

	// Load in the sound
	Common::String fileName = expand_name_2_RAW(name, roomNum);
	if ((workHandle = rget(fileName, &assetSize)) == nullptr)
		error("Could not find sound - %s", fileName.c_str());

	HLock(workHandle);
	const byte *pSrc = (byte *)*workHandle;
	byte *pDest = (byte *)malloc(assetSize);
	Common::copy(pSrc, pSrc + assetSize, pDest);

	HUnLock(workHandle);

	_sounds[name] = DigiEntry(fileName, pDest, assetSize);
	_sounds[name]._isFootsteps = isFootsteps;
	return false;
}

void Digi::unload(const Common::String &name) {
	if (_sounds.contains(name)) {
		// Stop it if it's playing
		for (int channel = 0; channel < MAX_CHANNELS; ++channel) {
			if (_channels[channel]._name == name)
				stop(channel, true);
		}

		// Remove the underlying resource
		if (!_sounds[name]._filename.empty() && !_sounds[name]._isFootsteps) {
			rtoss(_sounds[name]._filename);
			_sounds[name]._filename.clear();

			// Delete the sound entry
			free(_sounds[name]._data);
			_sounds.erase(name);
		}
	}
}

void Digi::task() {
	// No implementation
}

int32 Digi::play(const Common::String &name, uint channel, int32 vol, int32 trigger, int32 room_num) {
	return play(name, channel, vol, trigger, room_num, false);
}

int32 Digi::play_loop(const Common::String &name, uint channel, int32 vol, int32 trigger, int32 room_num) {
	return play(name, channel, vol, trigger, room_num, true);
}

int32 Digi::play(const Common::String &name, uint channel, int32 vol, int32 trigger, int32 room_num, bool loop) {
	assert(channel < 4);

	// Assure no prior sound for the channel is playing
	stop(channel);

	// Load in the new sound
	preload(name, false, room_num);
	DigiEntry &entry = _sounds[name];
	Channel &c = _channels[channel];

	// Create new audio stream
	Audio::AudioStream *stream = Audio::makeLoopingAudioStream(
			Audio::makeRawStream(entry._data, entry._size, 11025, Audio::FLAG_UNSIGNED,
				DisposeAfterUse::NO),
			loop ? 0 : 1);
	_mixer->playStream(Audio::Mixer::kSFXSoundType, &c._soundHandle, stream,
		-1, vol);

	if (trigger < 0 || trigger > 32767)
		trigger = -1;
	c._trigger = kernel_trigger_create(trigger);
	c._name = name;

	return 0;
}

void Digi::playFootsteps() {
	// Get a list of the walking sounds
	Common::Array<Common::String> names;

	for (auto it = _sounds.begin(); it != _sounds.end(); ++it) {
		if (it->_value._isFootsteps)
			names.push_back(it->_key);
	}

	if (!names.empty()) {
		play(names[imath_ranged_rand(0, (int)names.size() - 1)].c_str(),
			1, 100, NO_TRIGGER, GLOBAL_SCENE);
	}
}

void Digi::stop(uint channel, bool calledFromUnload) {
	assert(channel < 4);

	Channel &c = _channels[channel];
	if (!c._name.empty()) {
		Common::String name = c._name;

		_mixer->stopHandle(c._soundHandle);
		c._trigger = -1;
		c._name.clear();

		if (!calledFromUnload) {
			digi_unload(name);
		}
	}
}

void Digi::flush_mem() {
	unload_sounds();
}

void Digi::read_another_chunk() {
	// For ScummVM, the audio data is completely loaded for each sound. But we still
	// need to check whether a sound has finished so it's trigger can be dispatched
	for (int channel = 0; channel < MAX_CHANNELS; ++channel) {
		Channel &c = _channels[channel];

		// Check if the channel has a sound playing that finished
		if (c._trigger != -1 && !_mixer->isSoundHandleActive(c._soundHandle)) {
			int trigger = c._trigger;
			c._trigger = -1;
			stop(channel);

			// Dispatch the trigger
			kernel_trigger_dispatchx(trigger);
		}
	}
}

bool Digi::play_state(int channel) const {
	return _mixer->isSoundHandleActive(_channels[channel]._soundHandle);
}

void Digi::change_volume(int channel, int vol) {
	_mixer->setChannelVolume(_channels[channel]._soundHandle, vol);
}

void Digi::set_overall_volume(int vol) {
	_mixer->setVolumeForSoundType(Audio::Mixer::kPlainSoundType, vol);
}

int Digi::get_overall_volume() {
	return _mixer->getVolumeForSoundType(Audio::Mixer::kPlainSoundType);
}

int32 Digi::ticks_to_play(const char *name, int roomNum) {
	// Get the file and retrieve it's size
	Common::String filename = expand_name_2_RAW(name, roomNum);
	SysFile sf(filename);
	double size = sf.size();
	sf.close();

	term_message("  digi_ticks_to_play");
	term_message("  %s", filename.c_str());
	term_message("  size = %f, room = %d", size, roomNum);

	return (int32)floor(size * 0.000090702946 * 60.0);
}

void Digi::change_panning(int val1, int val2) {
	warning("TODO: digi_change_panning");
}

} // namespace Sound

bool digi_preload(const Common::String &name, int roomNum) {
	return _G(digi).preload(name, false, roomNum);
}

void digi_unload(const Common::String &name) {
	_G(digi).unload(name);
}

int32 digi_play(const char *name, uint channel, int32 vol, int32 trigger, int32 room_num) {
	return _G(digi).play(name, channel, vol, trigger, room_num);
}

int32 digi_play_loop(const char *name, uint channel, int32 vol, int32 trigger, int32 room_num) {
	return _G(digi).play_loop(name, channel, vol, trigger, room_num);
}

void digi_read_another_chunk() {
	return _G(digi).read_another_chunk();
}

void digi_stop(int slot) {
	_G(digi).stop(slot);
}

bool digi_play_state(int channel) {
	return _G(digi).play_state(channel);
}

void digi_change_volume(int channel, int vol) {
	_G(digi).change_volume(channel, vol);
}

void digi_set_overall_volume(int vol) {
	_G(digi).set_overall_volume(vol);
}

int digi_get_overall_volume() {
	return _G(digi).get_overall_volume();
}

int32 digi_ticks_to_play(const char *name, int roomNum) {
	return _G(digi).ticks_to_play(name, roomNum);
}

void digi_change_panning(int val1, int val2) {
	_G(digi).change_panning(val1, val2);
}

} // namespace M4