]> icculus.org git repositories - taylor/freespace2.git/blob - include/sound.h
fix issue with looping audio streams
[taylor/freespace2.git] / include / sound.h
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell 
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/Sound/Sound.h $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * <insert description of file here>
16  *
17  * $Log$
18  * Revision 1.4  2002/06/09 04:41:15  relnev
19  * added copyright header
20  *
21  * Revision 1.3  2002/05/31 07:24:28  relnev
22  * warning
23  *
24  * Revision 1.2  2002/05/07 03:16:43  theoddone33
25  * The Great Newline Fix
26  *
27  * Revision 1.1.1.1  2002/05/03 03:28:12  root
28  * Initial import.
29  *
30  * 
31  * 6     9/08/99 3:22p Dave
32  * Updated builtin mission list.
33  * 
34  * 5     8/27/99 6:38p Alanl
35  * crush the blasted repeating messages bug
36  * 
37  * 4     6/18/99 5:16p Dave
38  * Added real beam weapon lighting. Fixed beam weapon sounds. Added MOTD
39  * dialog to PXO screen.
40  * 
41  * 3     5/23/99 8:11p Alanl
42  * Added support for EAX
43  * 
44  * 2     10/07/98 10:54a Dave
45  * Initial checkin.
46  * 
47  * 1     10/07/98 10:51a Dave
48  * 
49  * 30    5/12/98 5:39p Lawrance
50  * Increase MAX_SOUNDS to 175.. to account for missions with lots of voice
51  * 
52  * 29    5/12/98 2:43p Lawrance
53  * Make snd_time_remaining() work for arbitrary format
54  * 
55  * 28    4/13/98 5:04p Lawrance
56  * Write functions to determine how many milliseconds are left in a sound
57  * 
58  * 27    3/29/98 12:56a Lawrance
59  * preload the warp in and explosions sounds before a mission.
60  * 
61  * 26    3/25/98 6:10p Lawrance
62  * Work on DirectSound3D
63  * 
64  * 25    3/23/98 10:34a Lawrance
65  * fix parameter in snd_size()
66  * 
67  * 24    3/23/98 10:32a Lawrance
68  * Add functions for extracting raw sound data
69  * 
70  * 23    3/21/98 3:34p Lawrance
71  * Allow 3d sounds to have their ranges modified dynamically
72  * 
73  * 22    3/19/98 5:36p Lawrance
74  * Add some sound debug functions to see how many sounds are playing, and
75  * to start/stop random looping sounds.
76  * 
77  * 21    3/17/98 5:55p Lawrance
78  * Support object-linked sounds for asteroids.
79  * 
80  * 20    2/20/98 8:32p Lawrance
81  * Add radius parm to sound_play_3d()
82  * 
83  * 19    1/07/98 11:08a Lawrance
84  * pass priority to snd_play_raw()
85  * 
86  * 18    12/05/97 5:19p Lawrance
87  * re-do sound priorities to make more general and extensible
88  * 
89  * 17    11/20/97 5:36p Dave
90  * Hooked in a bunch of main hall changes (including sound). Made it
91  * possible to reposition (rewind/ffwd) 
92  * sound buffer pointers. Fixed animation direction change framerate
93  * problem.
94  * 
95  * 16    11/20/97 1:06a Lawrance
96  * Add Master_voice_volume, make voices play back at correctly scaled
97  * volumes
98  * 
99  * 15    10/14/97 11:33p Lawrance
100  * get RSX implemented
101  * 
102  * 14    10/13/97 7:41p Lawrance
103  * store duration of sound
104  * 
105  * 13    10/01/97 5:55p Lawrance
106  * change call to snd_play_3d() to allow for arbitrary listening position
107  * 
108  * 12    7/15/97 11:15a Lawrance
109  * limit the max instances of simultaneous sound effects, implement
110  * priorities to force critical sounds
111  * 
112  * 11    6/12/97 5:16p Lawrance
113  * allow snd_play_looping to take just one parm
114  * 
115  * 10    6/09/97 11:50p Lawrance
116  * integrating DirectSound3D
117  * 
118  * 9     6/05/97 11:25a Lawrance
119  * use sound signatures to ensure correct sound is loaded
120  * 
121  * 8     6/05/97 1:36a Lawrance
122  * using a new interface to play sounds
123  * 
124  * 7     6/05/97 1:08a Lawrance
125  * new sound play interface
126  * 
127  * 6     6/02/97 1:45p Lawrance
128  * implementing hardware mixing
129  * 
130  * 5     5/29/97 3:32p Lawrance
131  * added call to snd_do_frame()
132  * 
133  * 4     5/29/97 12:04p Lawrance
134  * split off acm, direct sound, and direct sound 3d portions to separate
135  * files
136  * 
137  * 3     5/26/97 10:25a Lawrance
138  * extern ds_convert_volume() 
139  * 
140  * 2     5/06/97 9:36a Lawrance
141  * added support for min and max distances for 3d sounds
142  * 
143  * 1     4/28/97 4:45p John
144  * Initial version of ripping sound & movie out of OsAPI.
145  * 
146  * 18    4/24/97 4:14p Lawrance
147  * make ds_initialized extern
148  * 
149  * 17    4/21/97 8:58a Lawrance
150  * keep track of how much mem is used for sounds
151  * 
152  * 16    4/20/97 11:19a Lawrance
153  * sndman_ interface obsolete.  Using snd_ functions to load, play, and
154  * manage static sound fx
155  * 
156  * 15    4/18/97 2:54p Lawrance
157  * sounds now have a default volume, when playing, pass a scaling factor
158  * not the actual volume
159  * 
160  * 14    4/09/97 4:34p Lawrance
161  * allow looped sounds to be cut off after they complete the full sample
162  * duration
163  * 
164  * 13    4/03/97 4:27p Lawrance
165  * expanding functionality to support event driven music
166  * 
167  * 12    3/25/97 10:49a Lawrance
168  * ADPCM -> PCM conversion working
169  * 
170  * 11    3/24/97 5:50p Lawrance
171  * added code to convert from ADPCM -> PCM using Audio Compression Manager
172  * 
173  * 10    3/19/97 11:48a Lawrance
174  * make each sound effect have a DirectSound secondary buffer
175  * 
176  * 9     3/10/97 4:16p Allender
177  * new messaging system.  builtin5 fixed to support it.  made new sound
178  * function to determine if sound is still playing.
179  * 
180  * 8     2/25/97 11:13a Lawrance
181  * added Master_sound_volume to scale volume passed to snd_play()
182  * 
183  * 7     1/10/97 10:44a John
184  * added functions to dynamically set volume/pan
185  * 
186  * 6     12/03/96 3:50p John
187  * Added some initial support for looping sounds.
188  * 
189  * 5     11/05/96 11:02a Mike
190  * Add sound toggle to Delete-D menu.
191  * Make some global variables look global - ie, capitalize them.
192  * Fix bug in collision code to make ship:ship collision detection work.
193  * Fix setting of parent_obj in obj_create().
194  * 
195  * 4     11/04/96 3:16p John
196  * First rev of working sound.
197  * 
198  * 3     11/04/96 10:37a John
199  * Closed the DirectSound object on program termination.
200  * 
201  * 2     10/30/96 5:27p John
202  * some hacked in sound stuff so i can experiment with directsound.
203  * 
204  * 1     10/28/96 11:36a John
205  *
206  * $NoKeywords: $
207  */
208
209 #ifndef __SOUND_H__
210 #define __SOUND_H__
211
212 #define MAX_SOUNDS      256
213
214 #define GAME_SND_USE_3D                 (1<<1)
215 #define GAME_SND_VOICE                  (1<<2)
216
217 #define SND_FLAG_3D                     (1<<1)
218 #define SND_FLAG_VOICE          (1<<2)
219 #define SND_FLAG_LOOPING        (1<<3)
220 #define SND_FLAG_EXT            (1<<4)
221
222 // Priorities that can be passed to snd_play() functions to limit how many concurrent sounds of a 
223 // given type are played.
224 #define SND_PRIORITY_MUST_PLAY                          0
225 #define SND_PRIORITY_SINGLE_INSTANCE            1
226 #define SND_PRIORITY_DOUBLE_INSTANCE            2
227 #define SND_PRIORITY_TRIPLE_INSTANCE            3
228
229 typedef struct game_snd
230 {
231         int     sig;                                            // number of sound in sounds.tbl (not used)
232         char    filename[MAX_FILENAME_LEN];
233         float   default_volume;         // range: 0.0 -> 1.0
234         int     min, max;                               // min: distance at which sound will stop getting louder  max: distance at which sound is inaudible
235         int     preload;                                        // preload sound (ie read from disk before mission starts)
236         int     id;                                             // index into Sounds[], where sound data is stored
237         int     id_sig;                                 // signature of Sounds[] element
238         int     flags;
239 } game_snd;
240
241 typedef struct sound_env
242 {
243         unsigned long id;
244         float volume;
245         float damping;
246         float decay;
247 } sound_env;
248
249 extern int              Sound_enabled;
250 extern float    Master_sound_volume;            // 0 -> 1.0
251 extern float    Master_voice_volume;            // 0 -> 1.0
252 extern int              Snd_sram;                                       // System memory consumed by sound data 
253
254 //int   snd_load( char *filename, int hardware=0, int three_d=0, int *sig=NULL );
255 int     snd_load( game_snd *gs);
256
257 int     snd_unload( int sndnum );
258 void    snd_unload_all();
259
260 // Plays a sound with volume between 0 and 1.0, where 0 is the
261 // inaudible and 1.0 is the loudest sound in the game.
262 // Pan goes from -1.0 all the way left to 0.0 in center to 1.0 all the way right.
263 int snd_play( game_snd *gs, float pan=0.0f, float vol_scale=1.0f, int priority = SND_PRIORITY_SINGLE_INSTANCE, bool voice_message = false );
264
265 // Play a sound directly from index returned from snd_load().  Bypasses
266 // the sound management process of using game_snd.
267 int snd_play_raw( int soundnum, float pan, float vol_scale=1.0f, int priority = SND_PRIORITY_MUST_PLAY );
268
269 // Plays a sound with volume between 0 and 1.0, where 0 is the
270 // inaudible and 1.0 is the loudest sound in the game.  It scales
271 // the pan and volume relative to the current viewer's location.
272 int snd_play_3d(game_snd *gs, vector *source_pos, vector *listen_pos, float radius=0.0f, vector *vel = NULL, int looping = 0, float vol_scale=1.0f, int priority = SND_PRIORITY_SINGLE_INSTANCE, vector *sound_fvec = NULL, float range_factor = 1.0f, int force = 0 );
273
274 // update the given 3d sound with a new position
275 void snd_update_3d_pos(int soudnnum, game_snd *gs, vector *new_pos);
276
277 // Use these for looping sounds.
278 // Returns the handle of the sound. -1 if failed.
279 // If startloop or stoploop are not -1, then then are used.
280 int     snd_play_looping( game_snd *gs, float pan=0.0f, float vol_scale=1.0f, int priority = SND_PRIORITY_MUST_PLAY, int force = 0 );
281
282 void    snd_stop( int snd_handle );
283
284 // Sets the volume of a sound that is already playing.
285 // The volume is between 0 and 1.0, where 0 is the
286 // inaudible and 1.0 is the loudest sound in the game.
287 void snd_set_volume( int snd_handle, float volume );
288
289 // Sets the panning location of a sound that is already playing.
290 // Pan goes from -1.0 all the way left to 0.0 in center to 1.0 all the way right.
291 void snd_set_pan( int snd_handle, float pan );
292
293 // Sets the pitch (frequency) of a sound that is already playing
294 // Valid values for pitch are > 0
295 void snd_set_pitch( int snd_handle, float pitch );
296 float snd_get_pitch( int snd_handle );
297
298 // Stops all sounds from playing, even looping ones.
299 void    snd_stop_all();
300
301 // determines if the sound handle is still palying
302 int     snd_is_playing( int snd_handle );
303
304 // change the looping status of a sound that is playing
305 void    snd_chg_loop_status(int snd_handle, int loop);
306
307 // return the time in ms for the duration of the sound
308 int snd_get_duration(int snd_id);
309
310 // get a 3D vol and pan for a particular sound
311 int     snd_get_3d_vol_and_pan(game_snd *gs, vector *pos, float* vol, float *pan, float radius=0.0f);
312
313 int     snd_init();
314 void    snd_close();
315
316 // Return 1 or 0 to show that sound system is inited ok
317 int     snd_is_inited();
318
319 // Returns a pointer to the direct sound object
320 uint    sound_get_ds();
321
322 void    snd_update_listener(vector *pos, vector *vel, matrix *orient);
323
324 void    snd_update_source(int snd_handle, int min, int max, vector *pos, vector *vel);
325
326 void    snd_use_lib(int lib_id);
327
328 int snd_num_playing();
329
330 int snd_get_data(int handle, char *data);
331 int snd_size(int handle, int *size);
332 void snd_do_frame();
333
334 // repositioning of the sound buffer pointer
335 void snd_rewind(int snd_handle, game_snd *sg, float seconds);                                   // rewind N seconds from the current position
336 void snd_ffwd(int snd_handle, game_snd *sg, float seconds);                                             // fast forward N seconds from the current position
337 void snd_set_pos(int snd_handle, game_snd *sg, float val,int as_pct);           // set the position val as either a percentage (if as_pct) or as a # of seconds into the sound
338
339 void snd_get_format(int handle, int *bits_per_sample, int *frequency);
340 int snd_time_remaining(int handle, int bits_per_sample=8, int frequency=11025);
341
342 // sound environment
343
344 enum
345 {
346     SND_ENV_GENERIC,
347     SND_ENV_PADDEDCELL,
348     SND_ENV_ROOM,
349     SND_ENV_BATHROOM,
350     SND_ENV_LIVINGROOM,
351     SND_ENV_STONEROOM,
352     SND_ENV_AUDITORIUM,
353     SND_ENV_CONCERTHALL,
354     SND_ENV_CAVE,
355     SND_ENV_ARENA,
356     SND_ENV_HANGAR,
357     SND_ENV_CARPETEDHALLWAY,
358     SND_ENV_HALLWAY,
359     SND_ENV_STONECORRIDOR,
360     SND_ENV_ALLEY,
361     SND_ENV_FOREST,
362     SND_ENV_CITY,
363     SND_ENV_MOUNTAINS,
364     SND_ENV_QUARRY,
365     SND_ENV_PLAIN,
366     SND_ENV_PARKINGLOT,
367     SND_ENV_SEWERPIPE,
368     SND_ENV_UNDERWATER,
369     SND_ENV_DRUGGED,
370     SND_ENV_DIZZY,
371     SND_ENV_PSYCHOTIC
372 };
373
374 int sound_env_set(sound_env *se);
375 int sound_env_get(sound_env *se);
376 int sound_env_disable();
377 int sound_env_supported();
378
379 #endif
380