File: sound.h

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 (339 lines) | stat: -rw-r--r-- 7,989 bytes parent folder | download | duplicates (3)
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
/* 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/>.
 *
 */

#ifndef ASYLUM_SYSTEM_SOUND_H
#define ASYLUM_SYSTEM_SOUND_H

#include "common/array.h"
#include "common/rect.h"

#include "audio/mixer.h"

#include "asylum/system/config.h"

#include "asylum/shared.h"

namespace Asylum {

class AsylumEngine;

struct ResourceEntry;

struct SoundItem {
	ResourceId resourceId;
	int32 field_4;
	int32 field_8;
	int32 field_C;

	SoundItem() {
		resourceId = kResourceNone;
		field_4 = 0;
		field_8 = 0;
		field_C = 0;
	}
};

struct FrameSoundItem {
	ResourceId resourceId;
	uint32 frameIndex;
	int32 index;
	int32 field_C;
	int32 field_10;
	int32 field_14;

	FrameSoundItem() {
		resourceId = kResourceNone;
		frameIndex = 0;
		index = 0;
		field_C = 0;
		field_10 = 0;
		field_14 = 0;
	}
};

struct AmbientSoundItem {
	int32 field_0;
	int32 flags;
	ResourceId resourceId;
	int32 delta;
	int32 attenuation;
	int32 nextTick;
	int32 flagNum[6];
	Common::Point point;

	AmbientSoundItem() {
		field_0 = 0;
		flags = 0;
		resourceId = kResourceNone;
		delta = 0;
		attenuation = 0;
		nextTick = 0;
		memset(&flagNum, 0, sizeof(flagNum));
	}
};

struct SoundQueueItem {
	ResourceId resourceId;
	Audio::SoundHandle handle;

	SoundQueueItem() {
		resourceId = kResourceNone;
	}
};

class Sound {
public:
	Sound(AsylumEngine *engine, Audio::Mixer *mixer);
	~Sound();

	//////////////////////////////////////////////////////////////////////////
	// Playing sounds & music
	//////////////////////////////////////////////////////////////////////////

	/**
	 * Play sound
	 *
	 * @param resourceId Identifier for the resource.
	 * @param looping    true to looping.
	 * @param volume 	 The volume.
	 * @param panning    The panning.
	 */
	void playSound(ResourceId resourceId, bool looping = false, int32 volume = Config.sfxVolume, int32 panning = 0);

	/**
	 * Play music
	 *
	 * @param resourceId Identifier for the resource.
	 * @param volume 	 The volume.
	 */
	void playMusic(ResourceId resourceId, int32 volume = Config.musicVolume);

	/**
	 * Change music
	 *
	 * @param index 		 Zero-based index of the music
	 * @param musicStatusExt The music status.
	 */
	void changeMusic(int32 index, int32 musicStatusExt);

	/**
	 * Query if a sound with the resource id is playing.
	 *
	 * @param resourceId Identifier for the resource.
	 *
	 * @return true if playing, false if not.
	 */
	bool isPlaying(ResourceId resourceId);

	//////////////////////////////////////////////////////////////////////////
	// Volume & panning
	//////////////////////////////////////////////////////////////////////////

	/**
	 * Sets the volume for a buffered sound resource
	 *
	 * @param resourceId Identifier for the resource.
	 * @param volume 	 The volume.
	 */
	void setVolume(ResourceId resourceId, int32 volume);

	/**
	 * Sets the music volume.
	 *
	 * @param volume The volume.
	 */
	void setMusicVolume(int32 volume);

	/**
	 * Gets the music volume.
	 *
	 * @return The music volume.
	 */
	int32 getMusicVolume() { return _musicVolume; }

	/**
	 * Sets the panning for a buffered sound resource
	 *
	 * @param resourceId Identifier for the resource.
	 * @param panning    The panning.
	 */
	void setPanning(ResourceId resourceId, int32 panning);

	/**
	 * Determine the amount to increase the supplied sound sample's volume based on the position.
	 *
	 * @param point       The coordinates
	 * @param attenuation The attenuation.
	 * @param delta       The delta.
	 *
	 * @return The calculated volume adjustement.
	 */
	int32 calculateVolumeAdjustement(const Common::Point &point, int32 attenuation, int32 delta);

	/**
	 * Gets an adjusted volume.
	 *
	 * @param volume The volume.
	 *
	 * @return The adjusted volume.
	 */
	int32 getAdjustedVolume(int32 volume) const;

	/**
	 * Calculates the panning at point.
	 *
	 * @param point The coordinates.
	 *
	 * @return The calculated panning at point.
	 */
	int32 calculatePanningAtPoint(const Common::Point &point);

	//////////////////////////////////////////////////////////////////////////
	// Stopping sounds
	//////////////////////////////////////////////////////////////////////////

	/**
	 * Stop the first playing sound with the ResourceId
	 *
	 * @param resourceId Identifier for the resource.
	 */
	void stop(ResourceId resourceId);

	/**
	 * Stop all sounds with the ResourceId
	 *
	 * @param resourceId Identifier for the resource.
	 */
	void stopAll(ResourceId resourceId);

	/**
	 * Stop all buffered sounds
	 */
	void stopAll();

	/**
	 * Stop music.
	 */
	void stopMusic();

	//////////////////////////////////////////////////////////////////////////
	// Conversion functions
	//////////////////////////////////////////////////////////////////////////

	/**
	 * Convert volume to a ScummVM mixer value
	 *
	 * @param [in,out] vol The volume.
	 */
	static void convertVolumeFrom(int32 &vol);

	/**
	 * Convert ScummVM mixer value to a volume
	 *
	 * @param [in,out] vol The volume.
	 */
	static void convertVolumeTo(int32 &vol);

private:
	AsylumEngine *_vm;

	Audio::Mixer       *_mixer;

	Audio::SoundHandle _musicHandle;
	int32 _musicVolume;

	Common::Array<SoundQueueItem> _soundQueue;

	//////////////////////////////////////////////////////////////////////////
	// Sound queue
	//////////////////////////////////////////////////////////////////////////

	/**
	 * Find the index within the sound queue of the sound sample with provided id.
	 *
	 * @param resourceId Identifier for the resource.
	 *
	 * @return The item.
	 */
	SoundQueueItem *getItem(ResourceId resourceId);

	/**
	 * Find the index within the sound queue of the playing sound sample with provided id.
	 *
	 * @param resourceId Identifier for the resource.
	 *
	 * @return The playing item.
	 */
	SoundQueueItem *getPlayingItem(ResourceId resourceId);

	/**
	 * Adds a sound to the sound queue.
	 *
	 * @param resourceId Identifier for the resource.
	 *
	 * @return the sound buffer item
	 */
	SoundQueueItem *addToQueue(ResourceId resourceId);

	/**
	 * Clears the sound queue from finished sounds
	 */
	void cleanupQueue();

	//////////////////////////////////////////////////////////////////////////
	// Helper functions
	//////////////////////////////////////////////////////////////////////////

	/**
	 * Checks if the sound file is valid
	 *
	 * @return true if valid, false if not.
	 */
	bool isValidSoundResource(ResourceId resourceId);

	/**
	 * Play sound data.
	 *
	 * @param type 				 The type.
	 * @param handle             The handle.
	 * @param soundData          The sound data
	 * @param soundDataLength    Length of the sound data.
	 * @param loop 				 true to loop.
	 * @param vol 				 The volume.
	 * @param pan 				 The pan.
	 */
	void playSoundData(Audio::Mixer::SoundType type, Audio::SoundHandle *handle, byte *soundData, uint32 soundDataLength, bool loop = false, int32 vol = 0, int32 pan = 0);

	//////////////////////////////////////////////////////////////////////////
	// Conversion functions
	//////////////////////////////////////////////////////////////////////////

	/**
	 * Convert pan.
	 *
	 * @param [in,out] pan The pan.
	 */
	static void convertPan(int32 &pan);
};

} // end of namespace Asylum

#endif // ASYLUM_SYSTEM_SOUND_H