2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
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
10 * $Logfile: /Freespace2/code/Sound/ds3d.cpp $
15 * C file for interface to DirectSound3D
18 * Revision 1.2 2002/06/09 04:41:27 relnev
19 * added copyright header
21 * Revision 1.1.1.1 2002/05/03 03:28:10 root
25 * 12 8/05/99 4:45p Alanl
26 * the FINAL tweak to rolloffs!
28 * 11 8/05/99 4:34p Alanl
29 * change rolloff factors again
31 * 10 8/05/99 4:27p Danw
32 * would you believe we're still tweaking the EAX?? :)
34 * 9 8/05/99 4:04p Danw
35 * tweak rolloffs for EAX
36 * 8 8/05/99 2:54p Danw
37 * tweak rolloffs for A3D and EAX
38 * 7 8/05/99 10:54a Alanl
39 * change EAX rolloff to 3.0
41 * 6 8/04/99 11:51a Danw
42 * tweak rolloffs for A3D and EAX
43 * 5 8/04/99 11:42a Danw
44 * tweak rolloffs for A3D and EAX
46 * 4 8/01/99 2:06p Alanl
47 * increase the rolloff for A3D
49 * 3 5/23/99 8:11p Alanl
50 * Added support for EAX
52 * 2 10/07/98 10:54a Dave
55 * 1 10/07/98 10:51a Dave
57 * 15 5/06/98 2:16p Dan
59 * 14 5/05/98 4:49p Lawrance
60 * Put in code to authenticate A3D, improve A3D support
62 * 13 4/19/98 9:30p Lawrance
63 * Use Aureal_enabled flag
65 * 12 9/09/97 3:39p Sandeep
66 * warning level 4 bugs
68 * 11 8/01/97 10:40a Lawrance
69 * decrease rolloff for DirectSound3D sounds
71 * 10 7/29/97 2:54p Lawrance
73 * 9 7/28/97 11:39a Lawrance
74 * allow individual volume scaling on 3D buffers
76 * 8 7/17/97 9:32a John
77 * made all directX header files name start with a v
79 * 7 6/09/97 11:50p Lawrance
80 * integrating DirectSound3D
82 * 6 6/09/97 8:53a Lawrance
85 * 5 6/08/97 5:59p Lawrance
86 * integrate DirectSound3D into sound system
88 * 4 6/02/97 1:45p Lawrance
89 * implementing hardware mixing
91 * 3 5/29/97 4:02p Lawrance
92 * listener interface in place
94 * 2 5/29/97 12:03p Lawrance
95 * creation of file to hold DirectSound3D specific code
113 DSPROPERTY_VMANAGER_MODE = 0,
114 DSPROPERTY_VMANAGER_PRIORITY,
115 DSPROPERTY_VMANAGER_STATE
116 } DSPROPERTY_VMANAGER;
121 DSPROPERTY_VMANAGER_MODE_DEFAULT = 0,
122 DSPROPERTY_VMANAGER_MODE_AUTO,
123 DSPROPERTY_VMANAGER_MODE_REPORT,
124 DSPROPERTY_VMANAGER_MODE_USER
130 DSPROPERTY_VMANAGER_STATE_PLAYING3DHW = 0,
131 DSPROPERTY_VMANAGER_STATE_SILENT,
132 DSPROPERTY_VMANAGER_STATE_BUMPED,
133 DSPROPERTY_VMANAGER_STATE_PLAYFAILED
137 extern LPDIRECTSOUND pDirectSound;
139 int DS3D_inited = FALSE;
141 LPDIRECTSOUND3DLISTENER pDS3D_listener = NULL;
143 GUID DSPROPSETID_VoiceManager_Def = {0x62a69bae, 0xdf9d, 0x11d1, {0x99, 0xa6, 0x0, 0xc0, 0x4f, 0xc9, 0x9d, 0x46}};
145 // ---------------------------------------------------------------------------------------
146 // ds3d_update_buffer()
148 // parameters: channel => identifies the 3D sound to update
149 // min => the distance at which sound doesn't get any louder
150 // max => the distance at which sound doesn't attenuate any further
151 // pos => world position of sound
152 // vel => velocity of the objects producing the sound
154 // returns: 0 => success
158 int ds3d_update_buffer(int channel, float min, float max, vector *pos, vector *vel)
161 LPDIRECTSOUND3DBUFFER pds3db;
162 float max_dist, min_dist;
164 if (DS3D_inited == FALSE)
170 pds3db = Channels[channel].pds3db;
171 Assert( pds3db != NULL);
173 // set the buffer position
175 hr = pds3db->SetPosition(pos->x, pos->y, pos->z, DS3D_DEFERRED);
178 // set the buffer veclocity
180 hr = pds3db->SetVelocity(vel->x, vel->y, vel->z, DS3D_DEFERRED);
183 hr = pds3db->SetVelocity(0.0f, 0.0f, 0.0f, DS3D_DEFERRED);
186 // set the min distance
187 hr = pds3db->GetMinDistance(&min_dist);
188 hr = pds3db->SetMinDistance( min, DS3D_DEFERRED );
189 // set the max distance
190 hr = pds3db->GetMaxDistance(&max_dist);
191 // hr = pds3db->SetMaxDistance( max, DS3D_DEFERRED );
192 hr = pds3db->SetMaxDistance( 100000.0f, DS3D_DEFERRED );
198 // ---------------------------------------------------------------------------------------
199 // ds3d_update_listener()
201 // returns: 0 => success
204 int ds3d_update_listener(vector *pos, vector *vel, matrix *orient)
208 if (DS3D_inited == FALSE)
211 if ( pDS3D_listener == NULL )
214 // set the listener position
216 hr = pDS3D_listener->SetPosition(pos->x, pos->y, pos->z, DS3D_DEFERRED);
219 // set the listener veclocity
221 hr = pDS3D_listener->SetVelocity(vel->x, vel->y, vel->z, DS3D_DEFERRED);
224 if ( orient != NULL ) {
225 hr = pDS3D_listener->SetOrientation( orient->fvec.x, orient->fvec.y, orient->fvec.z,
226 orient->uvec.x, orient->uvec.y, orient->uvec.z,
230 float rolloff_factor = 1.0f;
231 if (ds_using_a3d() == true) {
232 rolloff_factor = 3.0f; // A3D rolloff
234 rolloff_factor = 3.0f; // EAX rolloff
237 hr = pDS3D_listener->SetRolloffFactor( rolloff_factor, DS3D_DEFERRED );
238 hr = pDS3D_listener->SetDopplerFactor( 1.0f, DS3D_DEFERRED );
240 hr = pDS3D_listener->CommitDeferredSettings();
242 nprintf(("SOUND","Error in pDS3D_listener->CommitDeferredSettings(): %s\n", get_DSERR_text(hr) ));
249 // ---------------------------------------------------------------------------------------
250 // ds3d_init_listener()
253 // returns: 0 => success
256 int ds3d_init_listener()
260 if ( pDS3D_listener != NULL )
263 hr = pPrimaryBuffer->QueryInterface(IID_IDirectSound3DListener, (void**)&pDS3D_listener);
265 nprintf(("Sound","SOUND => Fatal error calling pPrimaryBuffer->QueryInterface(): %s\n", get_DSERR_text(hr) ));
272 // ---------------------------------------------------------------------------------------
273 // ds3d_close_listener()
276 void ds3d_close_listener()
278 if ( pDS3D_listener != NULL ) {
279 pDS3D_listener->Release();
280 pDS3D_listener = NULL;
285 // ---------------------------------------------------------------------------------------
288 // Initialize the DirectSound3D system. Call the initalization for the pDS3D_listener
290 // returns: -1 => init failed
292 int ds3d_init(int voice_manager_required)
294 if ( DS3D_inited == TRUE )
297 if (voice_manager_required == 1) {
298 LPKSPROPERTYSET pset;
299 pset = (LPKSPROPERTYSET)ds_get_property_set_interface();
302 nprintf(("Sound", "Disabling DirectSound3D since unable to get property set interface\n"));
307 unsigned long driver_support = 0;
309 hr = pset->QuerySupport(DSPROPSETID_VoiceManager_Def, DSPROPERTY_VMANAGER_MODE, &driver_support);
311 nprintf(("Sound", "Driver does not support Voice Manager extension, so abort DirectSound3D initialization\n"));
315 if ((driver_support & KSPROPERTY_SUPPORT_SET|KSPROPERTY_SUPPORT_GET) != (KSPROPERTY_SUPPORT_SET|KSPROPERTY_SUPPORT_GET)) {
316 nprintf(("Sound", "Driver does not support Voice Manager extension, so abort DirectSound3D initialization\n"));
320 VmMode vmode = DSPROPERTY_VMANAGER_MODE_AUTO;
321 hr = pset->Set(DSPROPSETID_VoiceManager_Def, DSPROPERTY_VMANAGER_MODE, NULL, 0, &vmode, sizeof(float));
323 nprintf(("Sound", "Driver does not support Voice Manager extension, so abort DirectSound3D initialization\n"));
328 if (ds3d_init_listener() != 0) {
337 // ---------------------------------------------------------------------------------------
340 // De-initialize the DirectSound3D system
344 if ( DS3D_inited == FALSE )
347 ds3d_close_listener();