aboutsummaryrefslogtreecommitdiff
path: root/engines/tony/sound.h
blob: 8f4a509930a53526d52e558a3c991fa182618693 (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
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/* 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.
 *
 */

/*
 * This code is based on original Tony Tough source code
 *
 * Copyright (c) 1997-2003 Nayma Software
 */

#ifndef TONY_SOUND_H
#define TONY_SOUND_H

#include "audio/mixer.h"
#include "common/file.h"
#include "tony/gfxcore.h"
#include "tony/loc.h"
#include "tony/utils.h"

namespace Audio {
class RewindableAudioStream;	
}

namespace Tony {

class FPSTREAM;
class FPSFX;

enum CODECS {
	FPCODEC_RAW,
	FPCODEC_ADPCM
};


/****************************************************************************\
*****************************************************************************
*       class FPSound
*       -------------
* Description: Sound driver per Falling Pumpkins
*****************************************************************************
\****************************************************************************/

class FPSOUND {

private:

	bool bSoundSupported;

	/****************************************************************************\
	*       Methods
	\****************************************************************************/

public:

	/****************************************************************************\
	*
	* Function:     FPSOUND::FPSOUND();
	*
	* Description:  Default constructor. Initializes the attributes
	*
	\****************************************************************************/

	FPSOUND();

	/****************************************************************************\
	*
	* Function:     FPSOUND::~FPSOUND();
	*
	* Description:  Deinitialize the object, free memory
	*
	\****************************************************************************/

	~FPSOUND();

	/****************************************************************************\
	*
	* Function:     bool FPSOUND::Init();
	*
	* Description:  Initializes the objects, and prepare everything required to 
	*               create streams and sound effects.
	*
	* Return:       True if everything is OK, False otherwise.
	*
	\****************************************************************************/

	bool Init();

	/****************************************************************************\
	*
	* Function:     bool CreateStream(FPSTREAM** lplpStream);
	*
	* Description:  Allocates an object of type FPSTREAM, and return its 
	*               pointer after it has been initialized.
	*
	* Input:        FPSTREAM** lplpStream   Will contain the pointer of the 
	*                                       object
	*
	* Return:       True is everything i OK, False otherwise
	*
	* Note:         The use of functions like CreateStream () and CreateSfx () 
	*               are due to the fact that the class constructors and 
	*               FPSTREAM FPSFX require that DirectSound is already initialized. 
	*               In this way, you avoid the bugs that would be created if an 
	*               object type is declared FPSTREAM FPSFX or global 
	*               (or anyway before initializing DirectSound).
	\****************************************************************************/

	bool CreateStream(FPSTREAM **lplpStream);

	/****************************************************************************\
	*
	* Function:     bool CreateSfx(FPSFX** lplpSfx);
	*
	* Description:  Allocates an object of type FPSFX and returns a pointer 
	*               pointing to it
	*
	* Input:        FPSFX** lplpSfx         Will contain the pointer of the 
	*                                       object
	*
	* Return:       True is everything i OK, False otherwise
	*
	* Note:         See notes about CreateStream()
	*
	\****************************************************************************/

	bool CreateSfx(FPSFX **lplpSfx);

	/****************************************************************************\
	*
	* Function:     void SetMasterVolume(int dwVolume);
	*
	* Description:  Set main volume
	*
	* Input:        int dwVolume          Volume to be set (0-63)
	*
	\****************************************************************************/

	void SetMasterVolume(int dwVolume);

	/****************************************************************************\
	*
	* Function:     void GetMasterVolume(LPINT lpdwVolume);
	*
	* Description:  Get main volume
	*
	* Input:        LPINT lpdwVolume        This variable will contain the 
	*                                       current volume (0-63)
	*
	\****************************************************************************/

	void GetMasterVolume(int *lpdwVolume);
};

class FPSFX {

	/****************************************************************************\
	*       Attributes
	\****************************************************************************/

private:
	bool bSoundSupported;                 // True if the sound is active
	bool bFileLoaded;                     // True is a file is opened
	bool bLoop;                           // True is sound effect should loop
	int lastVolume;

	bool bIsVoice;
	bool bPaused;

	Audio::AudioStream *_stream;
	Audio::RewindableAudioStream *_rewindableStream;
	Audio::SoundHandle _handle;

public:
	uint32 hEndOfBuffer;

private:


	/****************************************************************************\
	*       Methods
	\****************************************************************************/

public:
	/**
	 * Check process for whether sounds have finished playing
	 */
	static void soundCheckProcess(CORO_PARAM, const void *param);

	/****************************************************************************\
	*
	* Function:     FPSFX(bool bSoundOn);
	*
	* Description:  Default constructor. *DO NOT* declare the object directly,
	*               create it though FPSOUND::CreateSfx() instead
	*
	\****************************************************************************/

	FPSFX(bool bSoundOn);

	/****************************************************************************\
	*
	* Function:     ~FPSFX();
	*
	* Description:  Default destructor. It also stops the sound effect that 
	*               may be running, and free the memory used.
	*
	\****************************************************************************/

	~FPSFX();

	/****************************************************************************\
	*
	* Function:     Release();
	*
	* Description:  Releases the memory object. Must be called when the object 
	*               is no longer useful and **ONLY** when the object was created 
	*               with the FPSOUND :: CreateStream ().
	*
	* Note:         Any object pointers are no longer valid after this call.
	*
	\****************************************************************************/

	void Release();

	/****************************************************************************\
	*
	* Function:     bool LoadFile(char *lpszFileName, uint32 dwCodec=FPCODEC_RAW);
	*
	* Description:  Opens a file and load sound effect
	*
	* Input:        char *lpszFile          SFX filename
	*               uint32 dwCodec          CODEC to be used to decompress
	*                                       the sound samples
	*
	* Return:       True if everything is OK, False otherwise
	*
	\****************************************************************************/

	bool LoadFile(const char *lpszFileName, uint32 dwCodec = FPCODEC_RAW);
	bool loadWave(Common::SeekableReadStream *stream);
	bool LoadVoiceFromVDB(Common::File &vdbFP);

	/****************************************************************************\
	*
	* Function:     bool Play();
	*
	* Description:  Play the loaded FX.
	*
	* Return:       True if everything is OK, False otherwise
	*
	\****************************************************************************/

	bool Play();

	/****************************************************************************\
	*
	* Function:     bool Stop();
	*
	* Description:  Stop a FX
	*
	* Return:       True if everything is OK, False otherwise
	*
	\****************************************************************************/

	bool Stop();

	/****************************************************************************\
	*
	* Function:     void Pause(bool bPause);
	*
	* Description:  Pause a FX
	*
	\****************************************************************************/

	void Pause(bool bPause);

	/****************************************************************************\
	*
	* Function:     bool SetLoop(bool bLoop);
	*
	* Description:  Enables or disables SFX loop
	*
	* Input:        bool bLoop              True to activate the loop, else False
	*
	* Note:         The loop must be activated before the SFX starts to play, 
	*               else the effect will only be noticable next time the SFX is 
	*               played
	*
	\****************************************************************************/

	void SetLoop(bool bLoop);

	/****************************************************************************\
	*
	* Function:     void SetVolume(int dwVolume);
	*
	* Description:  Set SFX Volume
	*
	* Input:        int dwVolume            Volume to set (0-63)
	*
	\****************************************************************************/

	void SetVolume(int dwVolume);

	/****************************************************************************\
	*
	* Function:     void GetVolume(int * lpdwVolume);
	*
	* Description:  Get SFX volume
	*
	* Input:        int * lpdwVolume        Will contain the current volume
	*
	\****************************************************************************/

	void GetVolume(int *lpdwVolume);

	/**
	 * Returns true if the sound has finished playing
	 */
	bool endOfBuffer() const;
};

class FPSTREAM {

	/****************************************************************************\
	*       Attributes
	\****************************************************************************/

private:

	/*
	    HWND hwnd;
	    LPDIRECTSOUND lpDS;
	    LPDIRECTSOUNDBUFFER lpDSBuffer;    // DirectSound circular buffer
	    LPDIRECTSOUNDNOTIFY lpDSNotify;    // Notify hotspots in the buffer
	*/
	byte *lpTempBuffer;                    // Temporary buffer use for decompression

	uint32 dwBufferSize;                   // Buffer size (bytes)
	uint32 dwSize;                         // Stream size (bytes)
	uint32 dwCodec;                        // CODEC used

	HANDLE hThreadEnd;                     // Event used to close thread
	Common::File _file;                    // File handle used for the stream
	HANDLE hPlayThread;                    // Handle of the Play thread
	HANDLE hHot1, hHot2, hHot3;            // Events set by DirectSoundNotify
	HANDLE hPlayThread_PlayFast;
	HANDLE hPlayThread_PlayNormal;

	bool bSoundSupported;                  // True if the sound is active
	bool bFileLoaded;                      // True if the file is open 
	bool bLoop;                            // True if the stream should loop
	bool bDoFadeOut;                       // True if fade out is required
	bool bSyncExit;
	bool bPaused;
	int lastVolume;
	FPSTREAM *SyncToPlay;
//	DSBPOSITIONNOTIFY dspnHot[3];

	bool CreateBuffer(int nBufSize);

public:
	bool bIsPlaying;                      // True if the stream is playing

private:

	static void PlayThread(FPSTREAM *This);

	/****************************************************************************\
	*       Methods
	\****************************************************************************/

public:

	/****************************************************************************\
	*
	* Function:     FPSTREAM(bool bSoundOn);
	*
	* Description:  Default contractor. *DO NOT* declare the object directly: use
	*                                   FPSOUND::CreateStream() indtead
	*
	\****************************************************************************/

	FPSTREAM(bool bSoundOn);

	/****************************************************************************\
	*
	* Function:     ~FPSTREAM();
	*
	* Description:  Destructor by default. Stops the playing stream (if any) and
	*                                      frees the memory used by them
	*
	\****************************************************************************/

	~FPSTREAM();

	/****************************************************************************\
	*
	* Function:     Release();
	*
	* Description:  Releases memory used by object. Must be used when the object 
	*               is no longer used. *ONLY*�for objects created by 
	*               FPSOUND::CreateStream().
	*
	* Note:         Object pointers are no longer valid after this call.
	*
	\****************************************************************************/

	void Release();

	/****************************************************************************\
	*
	* Function:     bool LoadFile(char *lpszFileName, uint32 dwCodec=FPCODEC_RAW);
	*
	* Description:  Open a file for a stream.
	*
	* Input:        char *lpszFile          Filename to be opened
	*               uint32 dwCodec          CODEC to be used to decompress
	*                                       sound samples
	*
	* Return:       True if everything is OK, False otherwise
	*
	\****************************************************************************/

	bool LoadFile(const char *lpszFileName, uint32 dwCodec = FPCODEC_RAW, int nSync = 2000);

	/****************************************************************************\
	*
	* Function:     UnloadFile();
	*
	* Description:  Close a file stream (if opened). This function must be
	*               called to free the memory used by the stream
	*
	* Return:       Just to be sure, the destructor of this class calls
	*               UnloadFile() if it has not been mentioned explicitly
	*
	\****************************************************************************/

	bool UnloadFile();

	/****************************************************************************\
	*
	* Function:     bool Play();
	*
	* Description:  Play the loaded stream.
	*
	* Return:       True if everything is OK, False otherwise
	*
	\****************************************************************************/

	bool Play();
	void PlayFast(void);
	void Prefetch(void);

	/****************************************************************************\
	*
	* Function:     bool Stop();
	*
	* Description:  Stops the play of the stream.
	*
	* Return:       True if everything is OK, False otherwise
	*
	\****************************************************************************/

	bool Stop(bool bSync = false);
	void WaitForSync(FPSTREAM *toplay);

	/****************************************************************************\
	*
	* Function:     void Pause(bool bPause);
	*
	* Description:  Pause sound effect
	*
	\****************************************************************************/

	void Pause(bool bPause);

	/****************************************************************************\
	*
	* Function:     bool SetLoop(bool bLoop);
	*
	* Description:  Enable of disable stream loop
	*
	* Input:        bool bLoop              True to enable loop, false otherwise
	*
	* Note:         The loop must be activated BEFORE you play back the stream. 
	*               Any changes made during play will not have 'effect until
	*               the stream is not stopped, and then comes back into play.
	*
	\****************************************************************************/

	void SetLoop(bool bLoop);

	/****************************************************************************\
	*
	* Function:     void SetVolume(int dwVolume);
	*
	* Description:  Change stream colume
	*
	* Input:        int dwVolume            Volume to be set (0-63)
	*
	\****************************************************************************/

	void SetVolume(int dwVolume);



	/****************************************************************************\
	*
	* Function:     void GetVolume(LPINT lpdwVolume);
	*
	* Description:  Get stream volume
	*
	* Input:        LPINT lpdwVolume        Will contain the current stream volume
	*
	\****************************************************************************/

	void GetVolume(int *lpdwVolume);
};


} // End of namespace Tony

#endif