aboutsummaryrefslogtreecommitdiff
path: root/engines/sword25/sfx/soundengine.h
blob: 153caaf44514894df2e14b118fcbedfc373b0bb3 (plain)
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
/* 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.
 *
 * $URL$
 * $Id$
 *
 */

/*
 * This code is based on Broken Sword 2.5 engine
 *
 * Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
 *
 * Licensed under GNU GPL v2
 *
 */

/*
 * BS_SoundEngine
 * -------------
 * This is the sound engine interface that contains all the methods a sound
 * engine must implement.
 * Implementations of the sound engine have to be derived from this class.
 * It should be noted that a sound engine must maintain a list of all the
 * samples it uses, and that these samples should be released when the
 * destructor is called.
 *
 * Autor: Malte Thiesen
 */

#ifndef SWORD25_SOUNDENGINE_H
#define SWORD25_SOUNDENGINE_H

#include "sword25/kernel/common.h"
#include "sword25/kernel/resservice.h"
#include "sword25/kernel/persistable.h"

#include "sound/audiostream.h"
#include "sound/mixer.h"

namespace Sword25 {

#define SOUND_HANDLES 32

enum sndHandleType {
	kFreeHandle,
	kEffectHandle,
	kVoiceHandle
};

struct SndHandle {
	Audio::SoundHandle handle;
	sndHandleType type;
};


class SoundEngine : public ResourceService, public Persistable {
public:
	// -----------------------------------------------------------------------------
	// Enums and Types
	// -----------------------------------------------------------------------------

	enum SOUND_TYPES {
		MUSIC = 0,
		SPEECH = 1,
		SFX = 2
	};

	/**
	 * The callback function of PlayDynamicSoundEx
	 * @param UserData      User-specified pointer
	 * @param Data          Pointer to the data buffer
	 * @param DataLength    Length of the data to be written in bytes
	*/
	typedef void (*DynamicSoundReadCallback)(void *UserData, void *Data, uint DataLength);

	// -----------------------------------------------------------------------------
	// Constructor / destructor
	// -----------------------------------------------------------------------------

	SoundEngine(Kernel *pKernel);
	~SoundEngine() {};

	// --------------------------------------------------------------
	// THIS METHOD MUST BE IMPLEMENTED BY THE SOUND ENGINE
	// --------------------------------------------------------------

	/**
	 * Initialises the sound engine
	 * @param SampleRate    Specifies the sample rate to use.
	 * @param Channels      The maximum number of channels. The default is 32.
	 * @return              Returns true on success, otherwise false.
	 * @remark              Calls to other methods may take place only if this
	 * method was called successfully.
	 */
	bool Init(uint SampleRate, uint Channels = 32);

	/**
	 * Performs a "tick" of the sound engine
	 *
	 * This method should be called once per frame. It can be used by implementations
	 * of the sound engine that are not running in their own thread, or to perform
	 * additional administrative tasks that are needed.
	 */
	void Update();

	/**
	 * Sets the default volume for the different sound types
	 * @param Volume        The default volume level (0 = off, 1 = full volume)
	 * @param Type          The SoundType whose volume is to be changed
	 */
	void SetVolume(float Volume, SOUND_TYPES Type);

	/**
	 * Specifies the default volume of different sound types
	 * @param Type          The SoundType
	 * @return              Returns the standard sound volume for the given type
	 * (0 = off, 1 = full volume).
	*/
	float GetVolume(SOUND_TYPES Type);

	/**
	 * Pauses all the sounds that are playing.
	 */
	void PauseAll();

	/**
	 * Resumes all currently stopped sounds
	 */
	void ResumeAll();

	/**
	 * Pauses all sounds of a given layer.
	 * @param Layer         The Sound Layer
	*/
	void PauseLayer(uint Layer);

	/**
	 * Resumes all the sounds in a layer that was previously stopped with PauseLayer()
	 * @param Layer         The Sound Layer
	*/
	void ResumeLayer(uint Layer);


	/**
	 * Plays a sound
	 * @param FileName      The filename of the sound to be played
	 * @param Type          The type of sound
	 * @param Volume        The volume of the sound (0 = off, 1 = full volume)
	 * @param Pan           Panning (-1 = full left, 1 = right)
	 * @param Loop          Indicates whether the sound should be looped
	 * @param LoopStart     Indicates the starting loop point. If a value less than 0 is passed, the start
	 * of the sound is used.
	 * @param LoopEnd       Indicates the ending loop point. If a avlue is passed less than 0, the end of
	 * the sound is used.
	 * @param Layer         The sound layer
	 * @return              Returns true if the playback of the sound was started successfully.
	 * @remark              If more control is needed over the playing, eg. changing the sound parameters
	 * for Volume and Panning, then PlaySoundEx should be used.
	*/
	bool PlaySound(const Common::String &FileName, SOUND_TYPES Type, float Volume = 1.0f, float Pan = 0.0f, bool Loop = false, int LoopStart = -1, int LoopEnd = -1, uint Layer = 0);

	/**
	 * Plays a sound
	 * @param Type          The type of sound
	 * @param Volume        The volume of the sound (0 = off, 1 = full volume)
	 * @param Pan           Panning (-1 = full left, 1 = right)
	 * @param Loop          Indicates whether the sound should be looped
	 * @param LoopStart     Indicates the starting loop point. If a value less than 0 is passed, the start
	 * of the sound is used.
	 * @param LoopEnd       Indicates the ending loop point. If a avlue is passed less than 0, the end of
	 * the sound is used.
	 * @param Layer         The sound layer
	 * @return              Returns a handle to the sound. With this handle, the sound can be manipulated during playback.
	 * @remark              If more control is needed over the playing, eg. changing the sound parameters
	 * for Volume and Panning, then PlaySoundEx should be used.
	 */
	uint PlaySoundEx(const Common::String &FileName, SOUND_TYPES Type, float Volume = 1.0f, float Pan = 0.0f, bool Loop = false, int LoopStart = -1, int LoopEnd = -1, uint Layer = 0);

	/**
	 * Plays a sound generated at runtime
	 * @param ReadCallback  A pointer to a callback function that is called when sound data is needed.
	 * See the documentation for DynamicSoundReadCallback for more information.
	 * @param UserData      A pointer to the data. These are passed to the callback function each time.
	 * If no such data is needed, this parameter can be set to NULL.
	 * @param Type          The type of sound
	 * @param SampleRate    The sample rate for the sound
	 * @param BitsPerSample The size of the sample in bits. This statement is independant of the number of
	 * channels. Allowed values are 8, 16, 24, and 32.
	 * @param Channels      The number of channels. Allowed values are 1 and 2.
	 * @param Volume        The volume of the sound (0 = off, 1 = full volume)
	 * @param Pan           Panning (-1 = full left, 1 = right)
	 * @param Layer         The sound layer
	 * @return              Returns a handle to the sound. With this handle, the sound can be manipulated during playback.
	 * @remark              Dynamic sounds cannot be persisted.
	 */
	uint PlayDynamicSoundEx(DynamicSoundReadCallback ReadCallback, void *UserData, SOUND_TYPES Type, uint SampleRate, uint BitsPerSample, uint Channels, float Volume = 1.0f, float Pan = 0.0f, uint Layer = 0);

	/**
	 * Sets the volume of a playing sound
	 * @param Handle        The sound handle
	 * @param Volume        The volume of the sound (0 = off, 1 = full volume)
	 */
	void SetSoundVolume(uint Handle, float Volume);

	/**
	 * Sets the panning of a playing sound
	 * @param Handle        The sound handle
	 * @param Pan           Panning (-1 = full left, 1 = right)
	 */
	void SetSoundPanning(uint Handle, float Pan);

	/**
	 * Pauses a playing sound
	 * @param Handle        The sound handle
	 */
	void PauseSound(uint Handle);

	/**
	 * Resumes a paused sound
	 * @param Handle        The sound handle
	 */
	void ResumeSound(uint Handle);

	/**
	 * Stops a playing sound
	 * @param Handle        The sound handle
	 * @remark              Calling this method invalidates the passed handle; it can no longer be used.
	 */
	void StopSound(uint Handle);

	/**
	 * Returns whether a sound is paused
	 * @param Handle        The sound handle
	 * @return              Returns true if the sound is paused, false otherwise.
	 */
	bool IsSoundPaused(uint Handle);

	/**
	 * Returns whether a sound is still playing.
	 * @param Handle        The sound handle
	 * @return              Returns true if the sound is playing, false otherwise.
	*/
	bool IsSoundPlaying(uint Handle);

	/**
	 * Returns the volume of a playing sound (0 = off, 1 = full volume)
	 */
	float GetSoundVolume(uint Handle);

	/**
	 * Returns the panning of a playing sound (-1 = full left, 1 = right)
	 */
	float GetSoundPanning(uint Handle);

	/**
	 * Returns the position within a playing sound in seconds
	 */
	float GetSoundTime(uint Handle);

	Resource    *LoadResource(const Common::String &FileName);
	bool            CanLoadResource(const Common::String &FileName);

	bool persist(OutputPersistenceBlock &writer);
	bool unpersist(InputPersistenceBlock &reader);

private:
	bool _RegisterScriptBindings();
	SndHandle *getHandle(uint *id);

private:
	Audio::Mixer *_mixer;
	SndHandle _handles[SOUND_HANDLES];
};

} // End of namespace Sword25

#endif