]> icculus.org git repositories - taylor/freespace2.git/blob - include/ds.h
use new audiostr code rather than old windows/*nix version
[taylor/freespace2.git] / include / ds.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/ds.h $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Header file for interface to DirectSound
16  *
17  * $Log$
18  * Revision 1.4  2005/08/12 08:47:24  taylor
19  * use new audiostr code rather than old windows/*nix version
20  * update all OpenAL commands with new error checking macros
21  * fix play_position to properly account for real position, fixes the talking heads and message text cutting out early
22  * movies will now use better filtering when scaled
23  *
24  * Revision 1.3  2002/06/09 04:41:12  relnev
25  * added copyright header
26  *
27  * Revision 1.2  2002/05/27 01:06:01  theoddone33
28  * sound.cpp works
29  *
30  * Revision 1.1.1.1  2002/05/03 03:28:12  root
31  * Initial import.
32  *
33  * 
34  * 5     8/27/99 6:38p Alanl
35  * crush the blasted repeating messages bug
36  * 
37  * 4     8/01/99 2:06p Alanl
38  * increase the rolloff for A3D
39  * 
40  * 3     5/23/99 8:11p Alanl
41  * Added support for EAX
42  * 
43  * 2     10/07/98 10:54a Dave
44  * Initial checkin.
45  * 
46  * 1     10/07/98 10:51a Dave
47  * 
48  * 21    5/05/98 4:49p Lawrance
49  * Put in code to authenticate A3D, improve A3D support
50  * 
51  * 20    4/20/98 12:03a Lawrance
52  * Allow prioritizing of CTRL3D buffers
53  * 
54  * 19    4/18/98 9:12p Lawrance
55  * Added Aureal support.
56  * 
57  * 18    4/13/98 5:04p Lawrance
58  * Write functions to determine how many milliseconds are left in a sound
59  * 
60  * 17    3/23/98 10:32a Lawrance
61  * Add functions for extracting raw sound data
62  * 
63  * 16    2/15/98 11:10p Lawrance
64  * more work on real-time voice system
65  * 
66  * 15    2/15/98 4:43p Lawrance
67  * work on real-time voice
68  * 
69  * 14    2/06/98 7:30p John
70  * Added code to monitor the number of channels of sound actually playing.
71  * 
72  * 13    2/06/98 8:56a Allender
73  * fixed calling convention problem with DLL handles
74  * 
75  * 12    2/04/98 6:08p Lawrance
76  * Read function pointers from dsound.dll, further work on
77  * DirectSoundCapture.
78  * 
79  * 11    1/31/98 5:48p Lawrance
80  * Start on real-time voice recording
81  * 
82  * 10    12/05/97 5:19p Lawrance
83  * re-do sound priorities to make more general and extensible
84  * 
85  * 9     11/20/97 5:36p Dave
86  * Hooked in a bunch of main hall changes (including sound). Made it
87  * possible to reposition (rewind/ffwd) 
88  * sound buffer pointers. Fixed animation direction change framerate
89  * problem.
90  * 
91  * 8     10/13/97 7:41p Lawrance
92  * store duration of sound
93  * 
94  * 7     7/28/97 11:39a Lawrance
95  * allow individual volume scaling on 3D buffers
96  * 
97  * 6     7/17/97 9:32a John
98  * made all directX header files name start with a v
99  * 
100  * 5     7/15/97 11:15a Lawrance
101  * limit the max instances of simultaneous sound effects, implement
102  * priorities to force critical sounds
103  * 
104  * 4     6/09/97 11:50p Lawrance
105  * integrating DirectSound3D
106  * 
107  * 3     6/02/97 1:45p Lawrance
108  * implementing hardware mixing
109  * 
110  * 2     5/29/97 12:04p Lawrance
111  * creation of file to hold DirectSound specific portions
112  *
113  * $NoKeywords: $
114  */
115
116 #ifndef __DS_H__
117 #define __DS_H__
118
119 #ifndef PLAT_UNIX
120 #include <windows.h>
121 #include <mmreg.h>
122 #include "vdsound.h"
123 #endif
124 #include "pstypes.h"
125
126 #define DS_HARDWARE     (1<<0)
127 #define DS_3D                   (1<<1)
128 #define DS_USE_DS3D     (1<<2)
129
130 // Constants that DirectSound should assign, but doesn't
131 #define MAX_PAN         1500.0f
132 #define MIN_PITCH               100
133 #define MAX_PITCH               100000
134
135
136 // limits placed on how many concurrent sounds of the same type can play simultaneously
137 #define DS_MUST_PLAY                            0
138 #define DS_LIMIT_ONE                            1
139 #define DS_LIMIT_TWO                            2
140 #define DS_LIMIT_THREE                  3
141
142 #define DS_DUP_FAILED                   -99
143
144 typedef struct sound_info {
145         int     format;         // WAVE_FORMAT_* defines from mmreg.h
146         uint    size;
147         int     sample_rate;
148         int     avg_bytes_per_sec;
149         int     n_block_align;
150         int     bits;
151         int     n_channels;
152         int     duration;       // time in ms for duration of sound
153         ubyte   *data;
154 } sound_info;
155
156 extern int                                                      ds_initialized;
157
158
159 #ifdef PLAT_UNIX
160 extern const char* openal_error_string();
161
162 // if an error occurs after executing 'x' then do 'y'
163 #define OpenAL_ErrorCheck( x, y )       do {    \
164         x;      \
165         const char *error_text = openal_error_string(); \
166         if ( error_text != NULL ) {     \
167                 while ( error_text != NULL ) {  \
168                         nprintf(("Warning", "SOUND: %s:%d - OpenAL error = '%s'\n", __FILE__, __LINE__, error_text));   \
169                         error_text = openal_error_string();     \
170                 }       \
171                 y;      \
172         }       \
173 } while (0);
174
175 // like OpenAL_ErrorCheck() except that it gives the error message from x but does nothing about it
176 #define OpenAL_ErrorPrint( x )  do {    \
177         x;      \
178         const char *error_text = openal_error_string(); \
179         if ( error_text != NULL ) {     \
180                 while ( error_text != NULL ) {  \
181                         nprintf(("Sound", "OpenAL ERROR: \"%s\" in %s, line %i\n", error_text, __FILE__, __LINE__));    \
182                         error_text = openal_error_string();     \
183                 }       \
184         }       \
185 } while (0);
186
187 #else
188
189 extern LPDIRECTSOUNDBUFFER              pPrimaryBuffer;
190 extern LPDIRECTSOUND                            pDirectSound;
191
192 extern HRESULT (__stdcall *pfn_DirectSoundCaptureCreate)(LPGUID lpGUID, LPDIRECTSOUNDCAPTURE *lplpDSC, LPUNKNOWN pUnkOuter);
193 #endif
194
195 int     ds_init(int use_a3d, int use_eax);
196 void    ds_close();
197 void    ds_get_primary_format(WAVEFORMATEX *wfx);
198 int     ds_parse_wave(char *filename, ubyte **dest, uint *dest_size, WAVEFORMATEX **header);
199 int     ds_load_buffer(int *sid, int *hid, int *final_size, void *header, sound_info *si, int flags);
200 void    ds_unload_buffer(int sid, int hid);
201 int     ds_play(int sid, int hid, int snd_id, int priority, int volume, int pan, int looping, bool is_voice_msg = false);
202 int     ds_convert_volume(float volume);                // Convert a volume from 0.0f->1.0f to -10000 -> 0
203 float ds_get_percentage_vol(int ds_vol);        // Convert a volume from -10000 -> 0 to 0.0f->1.0f
204 int     ds_get_channel(int sig);
205 int     ds_is_channel_playing(int channel);
206 void    ds_stop_channel(int channel);
207 void    ds_stop_channel_all();
208 void    ds_set_volume( int channel, int vol );
209 void    ds_set_pan( int channel, int pan );
210 int     ds_get_pitch(int channel);
211 void    ds_set_pitch(int channel, int pitch);
212 void    ds_chg_loop_status(int channel, int loop);
213 void  ds_set_position(int channel, DWORD offset);
214 DWORD ds_get_play_position(int channel);
215 DWORD ds_get_write_position(int channel);
216 int     ds_get_data(int sid, char *data);
217 int     ds_get_size(int sid, int *size);
218
219 int     ds_create_buffer(int frequency, int bits_per_sample, int nchannels, int nseconds);
220 int     ds_lock_data(int sid, unsigned char *data, int size);
221 int     ds_play_easy(int sid, int volume);
222 void    ds_stop_easy(int sid);
223 int     ds_get_channel_size(int channel);
224 int     ds_is_3d_buffer(int sid);
225 int     ds_using_ds3d();
226 bool    ds_using_a3d();
227
228 unsigned int ds_get_primary_buffer_interface();
229 unsigned int ds_get_dsound_interface();
230 unsigned int ds_get_property_set_interface();
231
232 // Returns the number of channels that are actually playing
233 int ds_get_number_channels();
234
235 int ds3d_play( int sid, int hid, int snd_id, vector *pos, vector *vel, int min, int max, int looping, int max_volume, int estimated_vol, int priority=DS_MUST_PLAY );
236
237 // Get a character string for the error code
238 char    *get_DSERR_text(int DSResult);
239
240 void ds_do_frame();
241
242 // --------------------
243 //
244 // Creative eax.h
245 //
246 // --------------------
247
248 // EAX (listener) reverb property set {4a4e6fc1-c341-11d1-b73a-444553540000}
249 #ifndef PLAT_UNIX
250 DEFINE_GUID(DSPROPSETID_EAX_ReverbProperties, 
251     0x4a4e6fc1,
252     0xc341,
253     0x11d1,
254     0xb7, 0x3a, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00);
255 #endif
256
257 typedef enum 
258 {
259     DSPROPERTY_EAX_ALL,                // all reverb properties
260     DSPROPERTY_EAX_ENVIRONMENT,        // standard environment no.
261     DSPROPERTY_EAX_VOLUME,             // loudness of the reverb
262     DSPROPERTY_EAX_DECAYTIME,          // how long the reverb lasts
263     DSPROPERTY_EAX_DAMPING             // the high frequencies decay faster
264 } DSPROPERTY_EAX_REVERBPROPERTY;
265
266 #define EAX_NUM_STANDARD_PROPERTIES (DSPROPERTY_EAX_DAMPING + 1)
267
268 // use this structure for get/set all properties...
269 typedef struct 
270 {
271     unsigned long environment;          // 0 to EAX_ENVIRONMENT_COUNT-1
272     float fVolume;                      // 0 to 1
273     float fDecayTime_sec;               // seconds, 0.1 to 100
274     float fDamping;                     // 0 to 1
275 } EAX_REVERBPROPERTIES;
276
277
278 enum
279 {
280     EAX_ENVIRONMENT_GENERIC,
281     EAX_ENVIRONMENT_PADDEDCELL,
282     EAX_ENVIRONMENT_ROOM,
283     EAX_ENVIRONMENT_BATHROOM,
284     EAX_ENVIRONMENT_LIVINGROOM,
285     EAX_ENVIRONMENT_STONEROOM,
286     EAX_ENVIRONMENT_AUDITORIUM,
287     EAX_ENVIRONMENT_CONCERTHALL,
288     EAX_ENVIRONMENT_CAVE,
289     EAX_ENVIRONMENT_ARENA,
290     EAX_ENVIRONMENT_HANGAR,
291     EAX_ENVIRONMENT_CARPETEDHALLWAY,
292     EAX_ENVIRONMENT_HALLWAY,
293     EAX_ENVIRONMENT_STONECORRIDOR,
294     EAX_ENVIRONMENT_ALLEY,
295     EAX_ENVIRONMENT_FOREST,
296     EAX_ENVIRONMENT_CITY,
297     EAX_ENVIRONMENT_MOUNTAINS,
298     EAX_ENVIRONMENT_QUARRY,
299     EAX_ENVIRONMENT_PLAIN,
300     EAX_ENVIRONMENT_PARKINGLOT,
301     EAX_ENVIRONMENT_SEWERPIPE,
302     EAX_ENVIRONMENT_UNDERWATER,
303     EAX_ENVIRONMENT_DRUGGED,
304     EAX_ENVIRONMENT_DIZZY,
305     EAX_ENVIRONMENT_PSYCHOTIC,
306
307     EAX_ENVIRONMENT_COUNT           // total number of environments
308 };
309
310 #define EAX_MAX_ENVIRONMENT (EAX_ENVIRONMENT_COUNT - 1)
311
312 // EAX buffer reverb property set {4a4e6fc0-c341-11d1-b73a-444553540000}
313 #ifndef PLAT_UNIX
314 DEFINE_GUID(DSPROPSETID_EAXBUFFER_ReverbProperties, 
315     0x4a4e6fc0,
316     0xc341,
317     0x11d1,
318     0xb7, 0x3a, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00);
319 #endif
320
321 typedef enum 
322 {
323     DSPROPERTY_EAXBUFFER_ALL,           // all reverb buffer properties
324     DSPROPERTY_EAXBUFFER_REVERBMIX      // the wet source amount
325 } DSPROPERTY_EAXBUFFER_REVERBPROPERTY;
326
327 // use this structure for get/set all properties...
328 typedef struct 
329 {
330     float fMix;                          // linear factor, 0.0F to 1.0F
331 } EAXBUFFER_REVERBPROPERTIES;
332
333 #define EAX_REVERBMIX_USEDISTANCE -1.0F // out of normal range
334                                          // signifies the reverb engine should
335                                          // calculate it's own reverb mix value
336                                          // based on distance
337
338 // prototypes
339
340 int ds_eax_init();
341 void ds_eax_close();
342
343 int ds_eax_set_preset(unsigned long envid);
344
345 int ds_eax_set_volume(float volume);
346 int ds_eax_set_decay_time(float seconds);
347 int ds_eax_set_damping(float damp);
348 int ds_eax_set_environment(unsigned long envid);
349 int ds_eax_set_all(unsigned long id, float volume, float damping, float decay);
350 int ds_eax_get_all(EAX_REVERBPROPERTIES *er);
351 int ds_eax_is_inited();
352
353 #endif /* __DS_H__ */
354