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/ds.cpp $
15 * C file for interface to DirectSound
18 * Revision 1.16 2003/08/03 16:03:53 taylor
19 * working play position; 2D pan; pitch; cleanup
21 * Revision 1.15 2003/03/15 05:12:56 theoddone33
22 * Fix OpenAL cleanup (Taylor)
24 * Revision 1.14 2002/08/01 04:55:45 relnev
25 * experimenting with texture state
27 * Revision 1.13 2002/07/30 05:24:38 relnev
30 * Revision 1.12 2002/07/28 05:19:44 relnev
33 * Revision 1.11 2002/06/16 01:43:23 relnev
34 * fixed demo dogfight multiplayer mission
38 * Revision 1.10 2002/06/09 04:41:26 relnev
39 * added copyright header
41 * Revision 1.9 2002/06/05 08:05:29 relnev
42 * stub/warning removal.
44 * reworked the sound code.
46 * Revision 1.8 2002/06/05 04:03:33 relnev
47 * finished cfilesystem.
49 * removed some old code.
51 * fixed mouse save off-by-one.
55 * Revision 1.7 2002/06/02 22:31:37 cemason
58 * Revision 1.6 2002/06/02 21:11:12 cemason
61 * Revision 1.5 2002/06/02 09:50:42 relnev
64 * Revision 1.4 2002/06/02 07:17:44 cemason
65 * Added OpenAL support.
67 * Revision 1.3 2002/05/28 17:03:29 theoddone33
68 * fs2 gets to the main game loop now
70 * Revision 1.2 2002/05/27 21:35:50 theoddone33
71 * Stub out dsound backend
73 * Revision 1.1.1.1 2002/05/03 03:28:10 root
77 * 18 10/25/99 5:56p Jefff
78 * increase num software channels to the number the users hardware can
79 * handle. not less than 16, tho.
81 * 17 9/08/99 3:22p Dave
82 * Updated builtin mission list.
84 * 16 8/27/99 6:38p Alanl
85 * crush the blasted repeating messages bug
87 * 15 8/23/99 11:16p Danw
90 * 14 8/22/99 11:06p Alanl
91 * fix small bug in ds_close_channel
93 * 13 8/19/99 11:25a Alanl
94 * change format of secondary buffer from 44100 to 22050
96 * 12 8/17/99 4:11p Danw
97 * AL: temp fix for solving A3D crash
99 * 11 8/06/99 2:20p Jasonh
100 * AL: free 3D portion of buffer first
102 * 10 8/04/99 9:48p Alanl
103 * fix bug with setting 3D properties on a 2D sound buffer
105 * 9 8/04/99 11:42a Danw
106 * tone down EAX reverb
108 * 8 8/01/99 2:06p Alanl
109 * increase the rolloff for A3D
111 * 7 7/20/99 5:28p Dave
112 * Fixed debug build error.
114 * 6 7/20/99 1:49p Dave
115 * Peter Drake build. Fixed some release build warnings.
117 * 5 7/14/99 11:32a Danw
118 * AL: add some debug code to catch nefarious A3D problem
120 * 4 5/23/99 8:11p Alanl
121 * Added support for EAX
123 * 3 10/08/98 4:29p Dave
124 * Removed reference to osdefs.h
126 * 2 10/07/98 10:54a Dave
129 * 1 10/07/98 10:51a Dave
131 * 72 6/28/98 6:34p Lawrance
132 * add sanity check in while() loop for releasing channels
134 * 71 6/13/98 1:45p Sandeep
136 * 70 6/10/98 2:29p Lawrance
137 * don't use COM for initializing DirectSound... appears some machines
140 * 69 5/26/98 2:10a Lawrance
141 * make sure DirectSound pointer gets freed if Aureal resource manager
144 * 68 5/21/98 9:14p Lawrance
145 * remove obsolete registry setting
147 * 67 5/20/98 4:28p Allender
148 * upped sound buffers as per alan's request
150 * 66 5/15/98 3:36p John
151 * Fixed bug with new graphics window code and standalone server. Made
152 * hwndApp not be a global anymore.
154 * 65 5/06/98 3:37p Lawrance
155 * allow panned sounds geesh
157 * 64 5/05/98 4:49p Lawrance
158 * Put in code to authenticate A3D, improve A3D support
160 * 63 4/20/98 11:17p Lawrance
161 * fix bug with releasing channels
163 * 62 4/20/98 7:34p Lawrance
164 * take out obsolete directsound3d debug command
166 * 61 4/20/98 11:10a Lawrance
167 * put correct flags when creating sound buffer
169 * 60 4/20/98 12:03a Lawrance
170 * Allow prioritizing of CTRL3D buffers
172 * 59 4/19/98 9:31p Lawrance
173 * Use Aureal_enabled flag
175 * 58 4/19/98 9:39a Lawrance
176 * use DYNAMIC_LOOPERS for Aureal resource manager
178 * 57 4/19/98 4:13a Lawrance
179 * Improve how dsound is initialized
181 * 56 4/18/98 9:13p Lawrance
182 * Added Aureal support.
184 * 55 4/13/98 5:04p Lawrance
185 * Write functions to determine how many milliseconds are left in a sound
187 * 54 4/09/98 5:53p Lawrance
188 * Make DirectSound init more robust
190 * 53 4/01/98 9:21p John
191 * Made NDEBUG, optimized build with no warnings or errors.
193 * 52 3/31/98 5:19p John
194 * Removed demo/save/restore. Made NDEBUG defined compile. Removed a
195 * bunch of debug stuff out of player file. Made model code be able to
196 * unload models and malloc out only however many models are needed.
199 * 51 3/29/98 12:56a Lawrance
200 * preload the warp in and explosions sounds before a mission.
202 * 50 3/25/98 6:10p Lawrance
203 * Work on DirectSound3D
205 * 49 3/24/98 4:28p Lawrance
206 * Make DirectSound3D support more robust
208 * 48 3/24/98 11:49a Dave
209 * AL: Change way buffer gets locked.
211 * 47 3/24/98 11:27a Lawrance
212 * Use buffer_size for memcpy when locking buffer
214 * 46 3/23/98 10:32a Lawrance
215 * Add functions for extracting raw sound data
217 * 45 3/19/98 5:36p Lawrance
218 * Add some sound debug functions to see how many sounds are playing, and
219 * to start/stop random looping sounds.
221 * 44 3/07/98 3:35p Dave
222 * AL: check for ds being initialized in ds_create_buffer()
224 * 43 2/18/98 5:49p Lawrance
225 * Even if the ADPCM codec is unavailable, allow game to continue.
227 * 42 2/16/98 7:31p Lawrance
228 * get compression/decompression of voice working
230 * 41 2/15/98 11:10p Lawrance
231 * more work on real-time voice system
233 * 40 2/15/98 4:43p Lawrance
234 * work on real-time voice
236 * 39 2/06/98 7:30p John
237 * Added code to monitor the number of channels of sound actually playing.
239 * 38 2/06/98 8:56a Allender
240 * fixed calling convention problem with DLL handles
242 * 37 2/04/98 6:08p Lawrance
243 * Read function pointers from dsound.dll, further work on
244 * DirectSoundCapture.
246 * 36 2/03/98 11:53p Lawrance
247 * Adding support for DirectSoundCapture
249 * 35 1/31/98 5:48p Lawrance
250 * Start on real-time voice recording
252 * 34 1/10/98 1:14p John
253 * Added explanation to debug console commands
255 * 33 12/21/97 4:33p John
256 * Made debug console functions a class that registers itself
257 * automatically, so you don't need to add the function to
258 * debugfunctions.cpp.
260 * 32 12/08/97 12:24a Lawrance
261 * Allow duplicate sounds to be stopped if less than OR equal to new sound
264 * 31 12/05/97 5:19p Lawrance
265 * re-do sound priorities to make more general and extensible
267 * 30 11/28/97 2:09p Lawrance
268 * Overhaul how ADPCM conversion works... use much less memory... safer
271 * 29 11/22/97 11:32p Lawrance
272 * decompress ADPCM data into 8 bit (not 16bit) for regular sounds (ie not
275 * 28 11/20/97 5:36p Dave
276 * Hooked in a bunch of main hall changes (including sound). Made it
277 * possible to reposition (rewind/ffwd)
278 * sound buffer pointers. Fixed animation direction change framerate
281 * 27 10/13/97 7:41p Lawrance
282 * store duration of sound
284 * 26 10/11/97 6:39p Lawrance
285 * start playing primary buffer, to reduce latency on sounds starting
287 * 25 10/08/97 5:09p Lawrance
288 * limit player impact sounds so only one plays at a time
290 * 24 9/26/97 5:43p Lawrance
291 * fix a bug that was freeing memory early when playing compressed sound
294 * 23 9/09/97 3:39p Sandeep
295 * warning level 4 bugs
297 * 22 8/16/97 4:05p Lawrance
298 * don't load sounds into hardware if running Lean_and_mean
300 * 21 8/05/97 1:39p Lawrance
301 * support compressed stereo playback
303 * 20 7/31/97 10:38a Lawrance
304 * return old debug function for toggling DirectSound3D
306 * 19 7/29/97 3:27p Lawrance
307 * make console toggle for directsound3d work right
309 * 18 7/28/97 11:39a Lawrance
310 * allow individual volume scaling on 3D buffers
312 * 17 7/18/97 8:18p Lawrance
313 * fix bug in ds_get_free_channel() that caused sounds to not play when
316 * 16 7/17/97 8:04p Lawrance
317 * allow priority sounds to play if free channel, otherwise stop lowest
318 * volume priority sound of same type
320 * 15 7/17/97 5:57p John
321 * made directsound3d config value work
323 * 14 7/17/97 5:43p John
324 * added new config stuff
326 * 13 7/17/97 4:25p John
327 * First, broken, stage of changing config stuff
329 * 12 7/15/97 12:13p Lawrance
330 * don't stop sounds that have highest priority
332 * 11 7/15/97 11:15a Lawrance
333 * limit the max instances of simultaneous sound effects, implement
334 * priorities to force critical sounds
336 * 10 6/09/97 11:50p Lawrance
337 * integrating DirectSound3D
339 * 9 6/08/97 5:59p Lawrance
340 * integrate DirectSound3D into sound system
342 * 8 6/04/97 1:19p Lawrance
343 * made hardware mixing robust
345 * 7 6/03/97 1:56p Hoffoss
346 * Return correct error code when direct sound init fails.
348 * 6 6/03/97 12:07p Lawrance
349 * don't enable 3D sounds in Primary buffer
351 * 5 6/02/97 3:45p Dan
352 * temp disable of hardware mixing until problem solved with
353 * CreateBuffer() failing
355 * 4 6/02/97 1:45p Lawrance
356 * implementing hardware mixing
358 * 3 5/29/97 4:01p Lawrance
359 * let snd_init() have final say on initialization
361 * 2 5/29/97 12:04p Lawrance
362 * creation of file to hold DirectSound specific portions
381 #include <initguid.h>
386 #include <SDL/SDL_audio.h>
390 // Pointers to functions contained in DSOUND.dll
391 HRESULT (__stdcall *pfn_DirectSoundCreate)(LPGUID lpGuid, LPDIRECTSOUND *ppDS, IUnknown FAR *pUnkOuter) = NULL;
392 HRESULT (__stdcall *pfn_DirectSoundCaptureCreate)(LPGUID lpGUID, LPDIRECTSOUNDCAPTURE *lplpDSC, LPUNKNOWN pUnkOuter) = NULL;
394 HINSTANCE Ds_dll_handle=NULL;
396 LPDIRECTSOUND pDirectSound = NULL;
397 LPDIRECTSOUNDBUFFER pPrimaryBuffer = NULL;
398 LPIA3D2 pIA3d2 = NULL;
400 static LPKSPROPERTYSET pPropertySet; // pointer to sound card property set
401 static LPDIRECTSOUNDBUFFER Ds_property_set_pdsb = NULL;
402 static LPDIRECTSOUND3DBUFFER Ds_property_set_pds3db = NULL;
404 static int Ds_must_call_couninitialize = 0;
406 channel* Channels; //[MAX_CHANNELS];
407 static int channel_next_sig = 1;
409 #define MAX_DS_SOFTWARE_BUFFERS 256
410 typedef struct ds_sound_buffer
412 LPDIRECTSOUNDBUFFER pdsb;
418 ds_sound_buffer ds_software_buffers[MAX_DS_SOFTWARE_BUFFERS];
420 #define MAX_DS_HARDWARE_BUFFERS 32
421 ds_sound_buffer ds_hardware_buffers[MAX_DS_HARDWARE_BUFFERS];
423 static DSCAPS Soundcard_caps; // current soundcard capabilities
425 extern int Snd_sram; // mem (in bytes) used up by storing sounds in system memory
426 extern int Snd_hram; // mem (in bytes) used up by storing sounds in soundcard memory
428 static int Ds_use_ds3d = 0;
429 static int Ds_use_a3d = 0;
430 static int Ds_use_eax = 0;
432 GUID IID_IA3d2_Def = {0xfb80d1e0, 0x98d3, 0x11d1, {0x90, 0xfb, 0x00, 0x60, 0x08, 0xa1, 0xf4, 0x41}};
433 GUID CLSID_A3d_Def = {0xd8f1eee0, 0xf634, 0x11cf, {0x87, 0x0, 0x0, 0xa0, 0x24, 0x5d, 0x91, 0x8b}};
435 static bool Stop_logging_sounds = false;
438 ///////////////////////////
442 ///////////////////////////
445 //#define EAX_PRESET_GENERIC EAX_ENVIRONMENT_GENERIC,0.5F,1.493F,0.5F
446 #define EAX_PRESET_GENERIC EAX_ENVIRONMENT_GENERIC,0.2F,0.2F,1.0F
447 #define EAX_PRESET_PADDEDCELL EAX_ENVIRONMENT_PADDEDCELL,0.25F,0.1F,0.0F
448 #define EAX_PRESET_ROOM EAX_ENVIRONMENT_ROOM,0.417F,0.4F,0.666F
449 #define EAX_PRESET_BATHROOM EAX_ENVIRONMENT_BATHROOM,0.653F,1.499F,0.166F
450 #define EAX_PRESET_LIVINGROOM EAX_ENVIRONMENT_LIVINGROOM,0.208F,0.478F,0.0F
451 #define EAX_PRESET_STONEROOM EAX_ENVIRONMENT_STONEROOM,0.5F,2.309F,0.888F
452 #define EAX_PRESET_AUDITORIUM EAX_ENVIRONMENT_AUDITORIUM,0.403F,4.279F,0.5F
453 #define EAX_PRESET_CONCERTHALL EAX_ENVIRONMENT_CONCERTHALL,0.5F,3.961F,0.5F
454 #define EAX_PRESET_CAVE EAX_ENVIRONMENT_CAVE,0.5F,2.886F,1.304F
455 #define EAX_PRESET_ARENA EAX_ENVIRONMENT_ARENA,0.361F,7.284F,0.332F
456 #define EAX_PRESET_HANGAR EAX_ENVIRONMENT_HANGAR,0.5F,10.0F,0.3F
457 #define EAX_PRESET_CARPETEDHALLWAY EAX_ENVIRONMENT_CARPETEDHALLWAY,0.153F,0.259F,2.0F
458 #define EAX_PRESET_HALLWAY EAX_ENVIRONMENT_HALLWAY,0.361F,1.493F,0.0F
459 #define EAX_PRESET_STONECORRIDOR EAX_ENVIRONMENT_STONECORRIDOR,0.444F,2.697F,0.638F
460 #define EAX_PRESET_ALLEY EAX_ENVIRONMENT_ALLEY,0.25F,1.752F,0.776F
461 #define EAX_PRESET_FOREST EAX_ENVIRONMENT_FOREST,0.111F,3.145F,0.472F
462 #define EAX_PRESET_CITY EAX_ENVIRONMENT_CITY,0.111F,2.767F,0.224F
463 #define EAX_PRESET_MOUNTAINS EAX_ENVIRONMENT_MOUNTAINS,0.194F,7.841F,0.472F
464 #define EAX_PRESET_QUARRY EAX_ENVIRONMENT_QUARRY,1.0F,1.499F,0.5F
465 #define EAX_PRESET_PLAIN EAX_ENVIRONMENT_PLAIN,0.097F,2.767F,0.224F
466 #define EAX_PRESET_PARKINGLOT EAX_ENVIRONMENT_PARKINGLOT,0.208F,1.652F,1.5F
467 #define EAX_PRESET_SEWERPIPE EAX_ENVIRONMENT_SEWERPIPE,0.652F,2.886F,0.25F
468 #define EAX_PRESET_UNDERWATER EAX_ENVIRONMENT_UNDERWATER,1.0F,1.499F,0.0F
469 #define EAX_PRESET_DRUGGED EAX_ENVIRONMENT_DRUGGED,0.875F,8.392F,1.388F
470 #define EAX_PRESET_DIZZY EAX_ENVIRONMENT_DIZZY,0.139F,17.234F,0.666F
471 #define EAX_PRESET_PSYCHOTIC EAX_ENVIRONMENT_PSYCHOTIC,0.486F,7.563F,0.806F
473 static LPKSPROPERTYSET Ds_eax_reverb = NULL;
475 static int Ds_eax_inited = 0;
477 EAX_REVERBPROPERTIES Ds_eax_presets[] =
479 {EAX_PRESET_GENERIC},
480 {EAX_PRESET_PADDEDCELL},
482 {EAX_PRESET_BATHROOM},
483 {EAX_PRESET_LIVINGROOM},
484 {EAX_PRESET_STONEROOM},
485 {EAX_PRESET_AUDITORIUM},
486 {EAX_PRESET_CONCERTHALL},
490 {EAX_PRESET_CARPETEDHALLWAY},
491 {EAX_PRESET_HALLWAY},
492 {EAX_PRESET_STONECORRIDOR},
496 {EAX_PRESET_MOUNTAINS},
499 {EAX_PRESET_PARKINGLOT},
500 {EAX_PRESET_SEWERPIPE},
501 {EAX_PRESET_UNDERWATER},
502 {EAX_PRESET_DRUGGED},
504 {EAX_PRESET_PSYCHOTIC},
507 GUID DSPROPSETID_EAX_ReverbProperties_Def = {0x4a4e6fc1, 0xc341, 0x11d1, {0xb7, 0x3a, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}};
508 GUID DSPROPSETID_EAXBUFFER_ReverbProperties_Def = {0x4a4e6fc0, 0xc341, 0x11d1, {0xb7, 0x3a, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}};
510 //----------------------------------------------------------------
512 void ds_get_soundcard_caps(DSCAPS *dscaps);
515 typedef struct channel
517 int sig; // uniquely identifies the sound playing on the channel
518 int snd_id; // identifies which kind of sound is playing
519 ALuint source_id; // OpenAL source id
520 int buf_id; // currently bound buffer index (-1 if none)
521 int looping; // flag to indicate that the sound is looping
523 int priority; // implementation dependant priority
528 typedef struct sound_buffer
530 ALuint buf_id; // OpenAL buffer id
531 int source_id; // source index this buffer is currently bound to
540 #define MAX_DS_SOFTWARE_BUFFERS 256
542 static int MAX_CHANNELS = 1000; // initialized properly in ds_init_channels()
544 static int channel_next_sig = 1;
546 sound_buffer sound_buffers[MAX_DS_SOFTWARE_BUFFERS];
548 static int Ds_use_ds3d = 0;
549 static int Ds_use_a3d = 0;
550 static int Ds_use_eax = 0;
552 static int AL_play_position = 0;
555 // in case it's not defined by older/other drivers
556 #define AL_BYTE_LOKI 0x100C
559 ALCdevice *ds_sound_device;
560 void *ds_sound_context = (void *)0;
563 #define OpenAL_ErrorCheck() do { \
564 int i = alGetError(); \
565 if (i != AL_NO_ERROR) { \
566 while(i != AL_NO_ERROR) { \
567 nprintf(("Warning", "%s/%s:%d - OpenAL error %s\n", __FUNCTION__, __FILE__, __LINE__, alGetString(i))); \
574 #define OpenAL_ErrorCheck()
579 int ds_vol_lookup[101]; // lookup table for direct sound volumes
580 int ds_initialized = FALSE;
583 //--------------------------------------------------------------------------
586 // Determine if a secondary buffer is a 3d secondary buffer.
589 int ds_is_3d_buffer(LPDIRECTSOUNDBUFFER pdsb)
594 dsbc.dwSize = sizeof(dsbc);
595 hr = pdsb->GetCaps(&dsbc);
596 if ( hr == DS_OK && dsbc.dwFlags & DSBCAPS_CTRL3D ) {
605 //--------------------------------------------------------------------------
608 // Determine if a secondary buffer is a 3d secondary buffer.
610 int ds_is_3d_buffer(int sid)
614 return ds_is_3d_buffer(ds_software_buffers[sid].pdsb);
626 //--------------------------------------------------------------------------
627 // ds_build_vol_lookup()
629 // Fills up the ds_vol_lookup[] tables that converts from a volume in the form
630 // 0.0 -> 1.0 to -10000 -> 0 (this is the DirectSound method, where units are
631 // hundredths of decibls)
633 void ds_build_vol_lookup()
638 ds_vol_lookup[0] = -10000;
639 for ( i = 1; i <= 100; i++ ) {
641 ds_vol_lookup[i] = fl2i( (log(vol) / log(2.0f)) * 1000.0f);
646 //--------------------------------------------------------------------------
647 // ds_convert_volume()
649 // Takes volume between 0.0f and 1.0f and converts into
650 // DirectSound style volumes between -10000 and 0.
651 int ds_convert_volume(float volume)
655 index = fl2i(volume * 100.0f);
661 return ds_vol_lookup[index];
664 //--------------------------------------------------------------------------
665 // ds_get_percentage_vol()
667 // Converts -10000 -> 0 range volume to 0 -> 1
668 float ds_get_percentage_vol(int ds_vol)
671 vol = pow(2.0, ds_vol/1000.0);
675 // ---------------------------------------------------------------------------------------
678 // Parse a wave file.
680 // parameters: filename => file of sound to parse
681 // dest => address of pointer of where to store raw sound data (output parm)
682 // dest_size => number of bytes of sound data stored (output parm)
683 // header => address of pointer to a WAVEFORMATEX struct (output parm)
685 // returns: 0 => wave file successfully parsed
688 // NOTE: memory is malloced for the header and dest in this function. It is the responsibility
689 // of the caller to free this memory later.
691 int ds_parse_wave(char *filename, ubyte **dest, uint *dest_size, WAVEFORMATEX **header)
694 PCMWAVEFORMAT PCM_header;
696 unsigned int tag, size, next_chunk;
698 fp = cfopen( filename, "rb" );
700 nprintf(("Error", "Couldn't open '%s'\n", filename ));
704 // Skip the "RIFF" tag and file size (8 bytes)
705 // Skip the "WAVE" tag (4 bytes)
706 cfseek( fp, 12, CF_SEEK_SET );
708 // Now read RIFF tags until the end of file
711 if ( cfread( &tag, sizeof(uint), 1, fp ) != 1 )
714 if ( cfread( &size, sizeof(uint), 1, fp ) != 1 )
717 next_chunk = cftell(fp) + size;
720 case 0x20746d66: // The 'fmt ' tag
721 //nprintf(("Sound", "SOUND => size of fmt block: %d\n", size));
722 cfread( &PCM_header, sizeof(PCMWAVEFORMAT), 1, fp );
723 if ( PCM_header.wf.wFormatTag != WAVE_FORMAT_PCM ) {
724 cbExtra = cfread_short(fp);
727 // Allocate memory for WAVEFORMATEX structure + extra bytes
728 if ( (*header = (WAVEFORMATEX *) malloc ( sizeof(WAVEFORMATEX)+cbExtra )) != NULL ){
729 // Copy bytes from temporary format structure
730 memcpy (*header, &PCM_header, sizeof(PCM_header));
731 (*header)->cbSize = (unsigned short)cbExtra;
733 // Read those extra bytes, append to WAVEFORMATEX structure
735 cfread( ((ubyte *)(*header) + sizeof(WAVEFORMATEX)), cbExtra, 1, fp);
739 Assert(0); // malloc failed
743 case 0x61746164: // the 'data' tag
745 (*dest) = (ubyte *)malloc(size);
746 Assert( *dest != NULL );
747 cfread( *dest, size, 1, fp );
749 default: // unknown, skip it
752 cfseek( fp, next_chunk, CF_SEEK_SET );
759 // ---------------------------------------------------------------------------------------
768 for ( i = 0; i < MAX_DS_SOFTWARE_BUFFERS; i++ ) {
769 if ( sound_buffers[i].buf_id == 0 )
773 if ( i == MAX_DS_SOFTWARE_BUFFERS ) {
781 for ( i = 0; i < MAX_DS_SOFTWARE_BUFFERS; i++ ) {
782 if ( ds_software_buffers[i].pdsb == NULL )
786 if ( i == MAX_DS_SOFTWARE_BUFFERS ) {
794 // ---------------------------------------------------------------------------------------
805 for ( i = 0; i < MAX_DS_HARDWARE_BUFFERS; i++ ) {
806 if ( ds_hardware_buffers[i].pdsb == NULL )
810 if ( i == MAX_DS_HARDWARE_BUFFERS ) {
818 // ---------------------------------------------------------------------------------------
819 // Load a DirectSound secondary buffer with sound data. The sounds data for
820 // game sounds are stored in the DirectSound secondary buffers, and are
821 // duplicated as needed and placed in the Channels[] array to be played.
825 // sid => pointer to software id for sound ( output parm)
826 // hid => pointer to hardware id for sound ( output parm)
827 // final_size => pointer to storage to receive uncompressed sound size (output parm)
828 // header => pointer to a WAVEFORMATEX structure
829 // si => sound_info structure, contains details on the sound format
830 // flags => buffer properties ( DS_HARDWARE , DS_3D )
832 // returns: -1 => sound effect could not loaded into a secondary buffer
833 // 0 => sound effect successfully loaded into a secondary buffer
836 // NOTE: this function is slow, especially when sounds are loaded into hardware. Don't call this
837 // function from within gameplay.
840 int ds_load_buffer(int *sid, int *hid, int *final_size, void *header, sound_info *si, int flags)
843 Assert( final_size != NULL );
844 Assert( header != NULL );
845 Assert( si != NULL );
846 Assert( si->data != NULL );
848 // All sounds are required to have a software buffer
852 nprintf(("Sound","SOUND ==> No more sound buffers available\n"));
857 alGenBuffers (1, &pi);
866 switch (si->format) {
867 case WAVE_FORMAT_PCM:
876 /* format is now in pcm */
877 frequency = si->sample_rate;
879 if (si->bits == 16) {
880 if (si->n_channels == 2) {
881 format = AL_FORMAT_STEREO16;
882 } else if (si->n_channels == 1) {
883 format = AL_FORMAT_MONO16;
887 } else if (si->bits == 8) {
888 if (si->n_channels == 2) {
889 format = AL_FORMAT_STEREO8;
890 } else if (si->n_channels == 1) {
891 format = AL_FORMAT_MONO8;
901 alBufferData (pi, format, data, size, frequency);
903 sound_buffers[*sid].buf_id = pi;
904 sound_buffers[*sid].source_id = -1;
905 sound_buffers[*sid].frequency = frequency;
906 sound_buffers[*sid].bits_per_sample = si->bits;
907 sound_buffers[*sid].nchannels = si->n_channels;
908 sound_buffers[*sid].nseconds = si->size / si->avg_bytes_per_sec;
909 sound_buffers[*sid].nbytes = si->size;
916 Assert( final_size != NULL );
917 Assert( header != NULL );
918 Assert( si != NULL );
919 Assert( si->data != NULL );
920 Assert( si->size > 0 );
921 Assert( si->sample_rate > 0);
922 Assert( si->bits > 0 );
923 Assert( si->n_channels > 0 );
924 Assert( si->n_block_align >= 0 );
925 Assert( si->avg_bytes_per_sec > 0 );
927 WAVEFORMATEX *pwfx = (WAVEFORMATEX *)header;
928 DSBUFFERDESC BufferDesc;
929 WAVEFORMATEX WaveFormat;
931 int rc, final_sound_size, DSOUND_load_buffer_result = 0;
932 BYTE *pData, *pData2;
933 DWORD DataSize, DataSize2;
935 // the below two covnert_ variables are only used when the wav format is not
936 // PCM. DirectSound only takes PCM sound data, so we must convert to PCM if required
937 ubyte *convert_buffer = NULL; // storage for converted wav file
938 int convert_len; // num bytes of converted wav file
939 uint src_bytes_used; // number of source bytes actually converted (should always be equal to original size)
941 // Ensure DirectSound initialized
942 if (!ds_initialized) {
943 DSOUND_load_buffer_result = -1;
944 goto DSOUND_load_buffer_done;
947 // Set up buffer information
948 WaveFormat.wFormatTag = (unsigned short)si->format;
949 WaveFormat.nChannels = (unsigned short)si->n_channels;
950 WaveFormat.nSamplesPerSec = si->sample_rate;
951 WaveFormat.wBitsPerSample = (unsigned short)si->bits;
952 WaveFormat.cbSize = 0;
953 WaveFormat.nBlockAlign = (unsigned short)si->n_block_align;
954 WaveFormat.nAvgBytesPerSec = si->avg_bytes_per_sec;
956 final_sound_size = si->size; // assume this format will be used, may be over-ridded by convert_len
958 // Assert(WaveFormat.nChannels == 1);
960 switch ( si->format ) {
961 case WAVE_FORMAT_PCM:
964 case WAVE_FORMAT_ADPCM:
966 nprintf(( "Sound", "SOUND ==> converting sound from ADPCM to PCM\n" ));
967 rc = ACM_convert_ADPCM_to_PCM(pwfx, si->data, si->size, &convert_buffer, 0, &convert_len, &src_bytes_used, 8);
969 DSOUND_load_buffer_result = -1;
970 goto DSOUND_load_buffer_done;
973 if (src_bytes_used != si->size) {
974 Int3(); // ACM conversion failed?
975 DSOUND_load_buffer_result = -1;
976 goto DSOUND_load_buffer_done;
979 final_sound_size = convert_len;
981 // Set up the WAVEFORMATEX structure to have the right PCM characteristics
982 WaveFormat.wFormatTag = WAVE_FORMAT_PCM;
983 WaveFormat.nChannels = (unsigned short)si->n_channels;
984 WaveFormat.nSamplesPerSec = si->sample_rate;
985 WaveFormat.wBitsPerSample = 8;
986 WaveFormat.cbSize = 0;
987 WaveFormat.nBlockAlign = (unsigned short)(( WaveFormat.nChannels * WaveFormat.wBitsPerSample ) / 8);
988 WaveFormat.nAvgBytesPerSec = WaveFormat.nBlockAlign * WaveFormat.nSamplesPerSec;
990 nprintf(( "Sound", "SOUND ==> Coverted sound from ADPCM to PCM successfully\n" ));
994 nprintf(( "Sound", "Unsupported sound encoding\n" ));
995 DSOUND_load_buffer_result = -1;
996 goto DSOUND_load_buffer_done;
1000 WaveFormat.wFormatTag = WAVE_FORMAT_PCM; // DirectSound only used PCM wave files
1002 // Set up a DirectSound buffer
1003 ZeroMemory(&BufferDesc, sizeof(BufferDesc));
1004 BufferDesc.dwSize = sizeof(BufferDesc);
1005 BufferDesc.dwBufferBytes = final_sound_size;
1006 BufferDesc.lpwfxFormat = &WaveFormat;
1008 // check if DirectSound3D is enabled and the sound is flagged for 3D
1009 if ((ds_using_ds3d()) && (flags & DS_USE_DS3D)) {
1010 // if (ds_using_ds3d()) {
1011 BufferDesc.dwFlags = DSBCAPS_STATIC | DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_CTRLVOLUME | DSBCAPS_CTRL3D | DSBCAPS_MUTE3DATMAXDISTANCE;
1013 BufferDesc.dwFlags = DSBCAPS_STATIC | DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_CTRLVOLUME | DSBCAPS_LOCSOFTWARE;
1016 // Create a new software buffer using the settings for this wave
1017 // All sounds are required to have a software buffer
1018 *sid = ds_get_sid();
1020 nprintf(("Sound","SOUND ==> No more software secondary buffers available\n"));
1023 DSReturn = pDirectSound->CreateSoundBuffer(&BufferDesc, &ds_software_buffers[*sid].pdsb, NULL );
1025 if ( DSReturn == DS_OK && ds_software_buffers[*sid].pdsb != NULL ) {
1027 ds_software_buffers[*sid].desc = BufferDesc;
1028 ds_software_buffers[*sid].wfx = *BufferDesc.lpwfxFormat;
1030 // Lock the buffer and copy in the data
1031 if ((ds_software_buffers[*sid].pdsb)->Lock(0, final_sound_size, (void**)(&pData), &DataSize, (void**)(&pData2), &DataSize2, 0) == DS_OK) {
1033 if ( convert_buffer )
1034 memcpy(pData, convert_buffer, final_sound_size); // use converted data (PCM format)
1036 memcpy(pData, si->data, final_sound_size);
1038 (ds_software_buffers[*sid].pdsb)->Unlock(pData, DataSize, 0, 0);
1040 DSOUND_load_buffer_result = 0;
1042 // update ram used for sound
1043 Snd_sram += final_sound_size;
1044 *final_size = final_sound_size;
1047 nprintf(("Sound","SOUND => fatal error in DSOUND_load_buffer\n"));
1049 DSOUND_load_buffer_result = -1;
1052 DSOUND_load_buffer_done:
1053 if ( convert_buffer )
1054 free( convert_buffer );
1055 return DSOUND_load_buffer_result;
1059 // ---------------------------------------------------------------------------------------
1060 // ds_init_channels()
1062 // init the Channels[] array
1064 void ds_init_channels()
1071 Channels = (channel*) malloc(sizeof(channel) * MAX_CHANNELS);
1072 if (Channels == NULL) {
1073 Error(LOCATION, "Unable to allocate %d bytes for %d audio channels.", sizeof(channel) * MAX_CHANNELS, MAX_CHANNELS);
1076 // init the channels
1077 for ( i = 0; i < MAX_CHANNELS; i++ ) {
1078 alGenSources(1, &Channels[i].source_id);
1079 Channels[i].buf_id = -1;
1080 Channels[i].vol = 0;
1085 // detect how many channels we can support
1087 ds_get_soundcard_caps(&caps);
1089 // caps.dwSize = sizeof(DSCAPS);
1090 // pDirectSound->GetCaps(&caps);
1092 // minimum 16 channels
1093 MAX_CHANNELS = caps.dwMaxHwMixingStaticBuffers;
1094 int dbg_channels = MAX_CHANNELS;
1095 if (MAX_CHANNELS < 16) {
1099 // allocate the channels array
1100 Channels = (channel*) malloc(sizeof(channel) * MAX_CHANNELS);
1101 if (Channels == NULL) {
1102 Error(LOCATION, "Unable to allocate %d bytes for %d audio channels.", sizeof(channel) * MAX_CHANNELS, MAX_CHANNELS);
1105 // init the channels
1106 for ( i = 0; i < MAX_CHANNELS; i++ ) {
1107 Channels[i].pdsb = NULL;
1108 Channels[i].pds3db = NULL;
1109 Channels[i].vol = 0;
1112 mprintf(("** MAX_CHANNELS set to %d. DS reported %d.\n", MAX_CHANNELS, dbg_channels));
1116 // ---------------------------------------------------------------------------------------
1117 // ds_init_software_buffers()
1119 // init the software buffers
1121 void ds_init_software_buffers()
1126 for ( i = 0; i < MAX_DS_SOFTWARE_BUFFERS; i++ ) {
1127 sound_buffers[i].buf_id = 0;
1128 sound_buffers[i].source_id = -1;
1133 for ( i = 0; i < MAX_DS_SOFTWARE_BUFFERS; i++ ) {
1134 ds_software_buffers[i].pdsb = NULL;
1139 // ---------------------------------------------------------------------------------------
1140 // ds_init_hardware_buffers()
1142 // init the hardware buffers
1144 void ds_init_hardware_buffers()
1147 // STUB_FUNCTION; // not needed with openal (CM)
1152 for ( i = 0; i < MAX_DS_HARDWARE_BUFFERS; i++ ) {
1153 ds_hardware_buffers[i].pdsb = NULL;
1158 // ---------------------------------------------------------------------------------------
1159 // ds_init_buffers()
1161 // init the both the software and hardware buffers
1163 void ds_init_buffers()
1165 ds_init_software_buffers();
1166 ds_init_hardware_buffers();
1169 // Get the current soundcard capabilities
1171 void ds_get_soundcard_caps(DSCAPS *dscaps)
1174 int n_hbuffers, hram;
1176 dscaps->dwSize = sizeof(DSCAPS);
1178 hr = pDirectSound->GetCaps(dscaps);
1180 nprintf(("Sound","SOUND ==> DirectSound GetCaps() failed with code %s\n.",get_DSERR_text(hr) ));
1184 n_hbuffers = dscaps->dwMaxHwMixingStaticBuffers;
1185 hram = dscaps->dwTotalHwMemBytes;
1187 if ( !(dscaps->dwFlags & DSCAPS_CERTIFIED) ) {
1188 nprintf(("Sound","SOUND ==> Warning: audio driver is not Microsoft certified.\n"));
1192 // ---------------------------------------------------------------------------------------
1195 // init the both the software and hardware buffers
1197 void ds_show_caps(DSCAPS *dscaps)
1199 nprintf(("Sound", "SOUND => Soundcard Capabilities:\n"));
1200 nprintf(("Sound", "================================\n"));
1201 nprintf(("Sound", "Number of primary buffers: %d\n", dscaps->dwPrimaryBuffers ));
1202 nprintf(("Sound", "Number of total hw mixing buffers: %d\n", dscaps->dwMaxHwMixingAllBuffers ));
1203 nprintf(("Sound", "Number of total hw mixing static buffers: %d\n", dscaps->dwMaxHwMixingStaticBuffers ));
1204 nprintf(("Sound", "Number of total hw mixing streaming buffers: %d\n", dscaps->dwMaxHwMixingStreamingBuffers ));
1205 nprintf(("Sound", "Number of free hw mixing buffers: %d\n", dscaps->dwFreeHwMixingAllBuffers ));
1206 nprintf(("Sound", "Number of free hw mixing static buffers: %d\n", dscaps->dwFreeHwMixingStaticBuffers ));
1207 nprintf(("Sound", "Number of free hw mixing streaming buffers: %d\n", dscaps->dwFreeHwMixingStreamingBuffers ));
1208 nprintf(("Sound", "Number of hw 3D buffers: %d\n", dscaps->dwMaxHw3DAllBuffers ));
1209 nprintf(("Sound", "Number of hw 3D static buffers: %d\n", dscaps->dwMaxHw3DStaticBuffers ));
1210 nprintf(("Sound", "Number of hw 3D streaming buffers: %d\n", dscaps->dwMaxHw3DStreamingBuffers ));
1211 nprintf(("Sound", "Number of free hw 3D buffers: %d\n", dscaps->dwFreeHw3DAllBuffers ));
1212 nprintf(("Sound", "Number of free hw static 3D buffers: %d\n", dscaps->dwFreeHw3DStaticBuffers ));
1213 nprintf(("Sound", "Number of free hw streaming 3D buffers: %d\n", dscaps->dwFreeHw3DStreamingBuffers ));
1214 nprintf(("Sound", "Number of total hw bytes: %d\n", dscaps->dwTotalHwMemBytes ));
1215 nprintf(("Sound", "Number of free hw bytes: %d\n", dscaps->dwFreeHwMemBytes ));
1216 nprintf(("Sound", "================================\n"));
1221 // Fill in the waveformat struct with the primary buffer characteristics.
1222 void ds_get_primary_format(WAVEFORMATEX *wfx)
1224 // Set 16 bit / 22KHz / mono
1225 wfx->wFormatTag = WAVE_FORMAT_PCM;
1227 wfx->nSamplesPerSec = 22050;
1228 wfx->wBitsPerSample = 16;
1230 wfx->nBlockAlign = (unsigned short)(wfx->nChannels * (wfx->wBitsPerSample / 8));
1231 wfx->nAvgBytesPerSec = wfx->nBlockAlign * wfx->nSamplesPerSec;
1235 // obtain the function pointers from the dsound.dll
1236 void ds_dll_get_functions()
1238 pfn_DirectSoundCreate = (HRESULT(__stdcall *)(LPGUID lpGuid, LPDIRECTSOUND *ppDS, IUnknown FAR *pUnkOuter))GetProcAddress(Ds_dll_handle,"DirectSoundCreate");
1239 pfn_DirectSoundCaptureCreate = (HRESULT(__stdcall *)(LPGUID lpGuid, LPDIRECTSOUNDCAPTURE *lplpDSC, IUnknown FAR *pUnkOuter))GetProcAddress(Ds_dll_handle,"DirectSoundCaptureCreate");
1243 // Load the dsound.dll, and get funtion pointers
1244 // exit: 0 -> dll loaded successfully
1245 // !0 -> dll could not be loaded
1251 if ( !Ds_dll_loaded ) {
1252 Ds_dll_handle = LoadLibrary("dsound.dll");
1253 if ( !Ds_dll_handle ) {
1256 ds_dll_get_functions();
1269 HINSTANCE a3d_handle;
1272 a3d_handle = LoadLibrary("a3d.dll");
1276 FreeLibrary(a3d_handle);
1280 Ds_must_call_couninitialize = 1;
1282 hr = CoCreateInstance(CLSID_A3d_Def, NULL, CLSCTX_INPROC_SERVER, IID_IDirectSound, (void**)&pDirectSound);
1287 Assert(pDirectSound != NULL);
1288 hr = pDirectSound->QueryInterface(IID_IA3d2_Def, (void**)&pIA3d2);
1293 A3DCAPS_SOFTWARE swCaps;
1295 // Get Dll Software CAP to get DLL version number
1296 ZeroMemory(&swCaps,sizeof(swCaps));
1298 swCaps.dwSize = sizeof(swCaps);
1299 pIA3d2->GetSoftwareCaps(&swCaps);
1301 // Compare version from a3d.dll to header version only return A3D_OK if dll version >= to header version
1302 if (swCaps.dwVersion < A3D_CURRENT_VERSION) {
1303 pDirectSound->Release();
1304 pDirectSound = NULL;
1309 // verify this is authentic A3D
1310 int aureal_verified;
1311 aureal_verified = VerifyAurealA3D();
1313 if (aureal_verified == FALSE) {
1314 // This is fake A3D!!! Ignore
1315 pDirectSound->Release();
1316 pDirectSound = NULL;
1320 // Register our version for backwards compatibility with newer A3d.dll
1321 pIA3d2->RegisterVersion(A3D_CURRENT_VERSION);
1323 hr = pDirectSound->Initialize(NULL);
1325 pDirectSound->Release();
1326 pDirectSound = NULL;
1330 pIA3d2->SetResourceManagerMode(A3D_RESOURCE_MODE_DYNAMIC_LOOPERS);
1336 // Initialize the property set interface.
1338 // returns: 0 if successful, otherwise -1. If successful, the global pPropertySet will
1339 // set to a non-NULL value.
1341 int ds_init_property_set()
1348 // Create the secondary buffer required for EAX initialization
1350 wf.wFormatTag = WAVE_FORMAT_PCM;
1352 wf.nSamplesPerSec = 22050;
1353 wf.wBitsPerSample = 16;
1355 wf.nBlockAlign = (unsigned short)(wf.nChannels * (wf.wBitsPerSample / 8));
1356 wf.nAvgBytesPerSec = wf.nBlockAlign * wf.nSamplesPerSec;
1359 ZeroMemory(&dsbd, sizeof(dsbd));
1360 dsbd.dwSize = sizeof(dsbd);
1361 dsbd.dwFlags = DSBCAPS_CTRLDEFAULT | DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_STATIC | DSBCAPS_CTRL3D | DSBCAPS_MUTE3DATMAXDISTANCE;
1362 dsbd.dwBufferBytes = 3 * wf.nAvgBytesPerSec;
1363 dsbd.lpwfxFormat = &wf;
1365 // Create a new buffer using the settings for this wave
1366 hr = pDirectSound->CreateSoundBuffer(&dsbd, &Ds_property_set_pdsb, NULL);
1368 pPropertySet = NULL;
1372 // Get the 3D interface from the secondary buffer, which is used to query the EAX interface
1373 hr = Ds_property_set_pdsb->QueryInterface(IID_IDirectSound3DBuffer, (void**)&Ds_property_set_pds3db);
1375 Ds_property_set_pds3db = NULL;
1379 Assert(Ds_property_set_pds3db != NULL);
1380 hr = Ds_property_set_pds3db->QueryInterface(IID_IKsPropertySet, (void**)&pPropertySet);
1381 if ((FAILED(hr)) || (pPropertySet == NULL)) {
1389 // ---------------------------------------------------------------------------------------
1392 // returns: -1 => init failed
1393 // 0 => init success
1394 int ds_init(int use_a3d, int use_eax)
1397 // NOTE: A3D and EAX are unused in OpenAL
1398 const ALubyte *initStr = (const ALubyte *)"\'( (sampling-rate 22050 ))";
1399 int attr[] = { ALC_FREQUENCY, 22050, ALC_SYNC, AL_FALSE, 0 };
1405 nprintf(( "Sound", "SOUND ==> Initializing OpenAL...\n" ));
1408 ds_sound_device = alcOpenDevice (initStr);
1410 // Create Sound Device
1411 ds_sound_context = alcCreateContext (ds_sound_device, attr);
1412 alcMakeContextCurrent (ds_sound_context);
1414 if (alcGetError(ds_sound_device) != ALC_NO_ERROR) {
1415 nprintf(("Sound", "SOUND ==> Couldn't initialize OpenAL\n"));
1419 OpenAL_ErrorCheck();
1421 // make sure we can actually use AL_BYTE_LOKI (Mac OpenAL doesn't have it)
1422 AL_play_position = alIsExtensionPresent( (ALubyte*)"AL_LOKI_play_position" );
1424 // Initialize DirectSound3D. Since software performance of DirectSound3D is unacceptably
1425 // slow, we require the voice manger (a DirectSound extension) to be present. The
1426 // exception is when A3D is being used, since A3D has a resource manager built in.
1427 // if (Ds_use_ds3d && ds3d_init(0) != 0)
1430 // setup default listener position/orientation
1431 // this is needed for 2D pan
1432 alListener3f(AL_POSITION, 0.0, 0.0, 0.0);
1434 ALfloat list_orien[] = { 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f };
1435 alListenerfv(AL_ORIENTATION, list_orien);
1437 ds_build_vol_lookup();
1443 WAVEFORMATEX wave_format;
1444 DSBUFFERDESC BufferDesc;
1446 nprintf(( "Sound", "SOUND ==> Initializing DirectSound...\n" ));
1448 hwnd = (HWND)os_get_window();
1449 if ( hwnd == NULL ) {
1450 nprintf(( "Sound", "SOUND ==> No window handle, so no sound...\n" ));
1454 if ( ds_dll_load() == -1 ) {
1458 pDirectSound = NULL;
1460 Ds_use_a3d = use_a3d;
1461 Ds_use_eax = use_eax;
1463 if (Ds_use_a3d || Ds_use_eax) {
1467 if (Ds_use_a3d && Ds_use_eax) {
1472 // If we want A3D, ensure a3d.dll exists
1473 if (Ds_use_a3d == 1) {
1474 if (ds_init_a3d() != 0) {
1481 if (Ds_use_a3d == 0) {
1482 if (!pfn_DirectSoundCreate) {
1483 nprintf(( "Sound", "SOUND ==> Could not get DirectSoundCreate function pointer\n" ));
1487 hr = pfn_DirectSoundCreate(NULL, &pDirectSound, NULL);
1493 // Set up DirectSound for exclusive mode, so we can change the primary buffer if we want to.
1494 hr = pDirectSound->SetCooperativeLevel(hwnd, DSSCL_EXCLUSIVE);
1496 nprintf(("Sound","SOUND ==> DirectSound pDirectSound->SetCooperativeLevel failed with code %s\n.",get_DSERR_text(hr) ));
1497 pDirectSound = NULL;
1501 // Create the primary buffer
1502 ZeroMemory(&BufferDesc, sizeof(BufferDesc));
1503 BufferDesc.dwSize = sizeof(BufferDesc);
1505 ds_get_soundcard_caps(&Soundcard_caps);
1508 BufferDesc.dwFlags = DSBCAPS_PRIMARYBUFFER | DSBCAPS_CTRL3D;
1510 hr = pDirectSound->CreateSoundBuffer(&BufferDesc, &pPrimaryBuffer, 0);
1512 nprintf(("Sound","SOUND ==> Primary Buffer create failed with DSBCAPS_CTRL3D property... disabling DirectSound3D\n"));
1517 nprintf(("Sound","SOUND ==> Primary Buffer created with DirectSound3D enabled\n"));
1521 // If not using DirectSound3D, then create a normal primary buffer
1522 if (Ds_use_ds3d == 0) {
1523 BufferDesc.dwFlags = DSBCAPS_PRIMARYBUFFER;
1524 hr = pDirectSound->CreateSoundBuffer(&BufferDesc, &pPrimaryBuffer, 0);
1526 nprintf(("Sound","SOUND ==> Primary Buffer create failed with error: %s\n",get_DSERR_text(hr) ));
1527 pDirectSound = NULL;
1531 nprintf(("Sound","SOUND ==> Primary Buffer created with without DirectSound3D enabled\n"));
1535 // Get the primary buffer format
1536 ds_get_primary_format(&wave_format);
1538 hr = pPrimaryBuffer->SetFormat(&wave_format);
1540 nprintf(("Sound","SOUND ==> pPrimaryBuffer->SetFormat() failed with code %s\n",get_DSERR_text(hr) ));
1543 pPrimaryBuffer->GetFormat(&wave_format, sizeof(wave_format), NULL);
1544 nprintf(("Sound","SOUND ==> Primary Buffer forced to: rate: %d Hz bits: %d n_channels: %d\n",
1545 wave_format.nSamplesPerSec, wave_format.wBitsPerSample, wave_format.nChannels));
1547 // start the primary buffer playing. This will reduce sound latency when playing a sound
1548 // if no other sounds are playing.
1549 hr = pPrimaryBuffer->Play(0, 0, DSBPLAY_LOOPING);
1551 nprintf(("Sound","SOUND ==> pPrimaryBuffer->Play() failed with code %s\n",get_DSERR_text(hr) ));
1554 // Initialize DirectSound3D. Since software performance of DirectSound3D is unacceptably
1555 // slow, we require the voice manger (a DirectSound extension) to be present. The
1556 // exception is when A3D is being used, since A3D has a resource manager built in.
1558 int vm_required = 1; // voice manager
1559 if (Ds_use_a3d == 1) {
1563 if (ds3d_init(vm_required) != 0) {
1569 if (Ds_use_eax == 1) {
1570 ds_init_property_set();
1571 if (ds_eax_init() != 0) {
1576 ds_build_vol_lookup();
1580 ds_show_caps(&Soundcard_caps);
1586 // ---------------------------------------------------------------------------------------
1589 // returns the text equivalent for the a DirectSound DSERR_ code
1591 char *get_DSERR_text(int DSResult)
1596 static char buf[20];
1597 snprintf(buf, 19, "unknown %d", DSResult);
1600 switch( DSResult ) {
1606 case DSERR_ALLOCATED:
1607 return "DSERR_ALLOCATED";
1610 case DSERR_ALREADYINITIALIZED:
1611 return "DSERR_ALREADYINITIALIZED";
1614 case DSERR_BADFORMAT:
1615 return "DSERR_BADFORMAT";
1618 case DSERR_BUFFERLOST:
1619 return "DSERR_BUFFERLOST";
1622 case DSERR_CONTROLUNAVAIL:
1623 return "DSERR_CONTROLUNAVAIL";
1627 return "DSERR_GENERIC";
1630 case DSERR_INVALIDCALL:
1631 return "DSERR_INVALIDCALL";
1634 case DSERR_INVALIDPARAM:
1635 return "DSERR_INVALIDPARAM";
1638 case DSERR_NOAGGREGATION:
1639 return "DSERR_NOAGGREGATION";
1642 case DSERR_NODRIVER:
1643 return "DSERR_NODRIVER";
1646 case DSERR_OUTOFMEMORY:
1647 return "DSERR_OUTOFMEMORY";
1650 case DSERR_OTHERAPPHASPRIO:
1651 return "DSERR_OTHERAPPHASPRIO";
1654 case DSERR_PRIOLEVELNEEDED:
1655 return "DSERR_PRIOLEVELNEEDED";
1658 case DSERR_UNINITIALIZED:
1659 return "DSERR_UNINITIALIZED";
1662 case DSERR_UNSUPPORTED:
1663 return "DSERR_UNSUPPORTED";
1674 // ---------------------------------------------------------------------------------------
1675 // ds_close_channel()
1677 // Free a single channel
1679 void ds_close_channel(int i)
1682 if(Channels[i].source_id != 0 && alIsSource (Channels[i].source_id)) {
1683 alSourceStop (Channels[i].source_id);
1684 alDeleteSources(1, &Channels[i].source_id);
1686 Channels[i].source_id = 0;
1693 // If a 3D interface exists, free it
1694 if ( Channels[i].pds3db != NULL ) {
1697 Channels[i].pds3db = NULL;
1700 while(++attempts < 10) {
1701 hr = Channels[i].pds3db->Release();
1702 if ( hr == DS_OK ) {
1705 // nprintf(("Sound", "SOUND ==> Channels[channel].pds3db->Release() failed with return value %s\n", get_DSERR_text(second_hr) ));
1709 Channels[i].pds3db = NULL;
1713 if ( Channels[i].pdsb != NULL ) {
1714 // If a 2D interface exists, free it
1715 if ( Channels[i].pdsb != NULL ) {
1717 while(++attempts < 10) {
1718 hr = Channels[i].pdsb->Release();
1719 if ( hr == DS_OK ) {
1722 nprintf(("Sound", "SOUND ==> Channels[channel].pdsb->Release() failed with return value %s\n", get_DSERR_text(hr) ));
1727 Channels[i].pdsb = NULL;
1734 // ---------------------------------------------------------------------------------------
1735 // ds_close_all_channels()
1737 // Free all the channel buffers
1739 void ds_close_all_channels()
1743 for (i = 0; i < MAX_CHANNELS; i++) {
1744 ds_close_channel(i);
1748 // ---------------------------------------------------------------------------------------
1749 // ds_unload_buffer()
1752 void ds_unload_buffer(int sid, int hid)
1756 ALuint buf_id = sound_buffers[sid].buf_id;
1758 if (buf_id != 0 && alIsBuffer(buf_id)) {
1759 alDeleteBuffers(1, &buf_id);
1762 sound_buffers[sid].buf_id = 0;
1772 if ( ds_software_buffers[sid].pdsb != NULL ) {
1773 hr = ds_software_buffers[sid].pdsb->Release();
1774 if ( hr != DS_OK ) {
1776 nprintf(("Sound", "SOUND ==> ds_software_buffers[sid]->Release() failed with return value %s\n", get_DSERR_text(hr) ));
1778 ds_software_buffers[sid].pdsb = NULL;
1783 if ( ds_hardware_buffers[hid].pdsb != NULL ) {
1784 hr = ds_hardware_buffers[hid].pdsb->Release();
1785 if ( hr != DS_OK ) {
1787 nprintf(("Sound", "SOUND ==> ds_hardware_buffers[hid]->Release() failed with return value %s\n", get_DSERR_text(hr) ));
1789 ds_hardware_buffers[hid].pdsb = NULL;
1795 // ---------------------------------------------------------------------------------------
1796 // ds_close_software_buffers()
1799 void ds_close_software_buffers()
1804 for (i = 0; i < MAX_DS_SOFTWARE_BUFFERS; i++) {
1805 ALuint buf_id = sound_buffers[i].buf_id;
1807 if (buf_id != 0 && alIsBuffer(buf_id)) {
1808 alDeleteBuffers(1, &buf_id);
1811 sound_buffers[i].buf_id = 0;
1817 for (i = 0; i < MAX_DS_SOFTWARE_BUFFERS; i++) {
1818 if ( ds_software_buffers[i].pdsb != NULL ) {
1819 hr = ds_software_buffers[i].pdsb->Release();
1820 if ( hr != DS_OK ) {
1822 nprintf(("Sound", "SOUND ==> ds_software_buffers[i]->Release() failed with return value %s\n", get_DSERR_text(hr) ));
1824 ds_software_buffers[i].pdsb = NULL;
1830 // ---------------------------------------------------------------------------------------
1831 // ds_close_hardware_buffers()
1834 void ds_close_hardware_buffers()
1842 for (i = 0; i < MAX_DS_HARDWARE_BUFFERS; i++) {
1843 if ( ds_hardware_buffers[i].pdsb != NULL ) {
1844 hr = ds_hardware_buffers[i].pdsb->Release();
1845 if ( hr != DS_OK ) {
1847 nprintf(("Sound", "SOUND ==> ds_hardware_buffers[i]->Release() failed with return value %s\n", get_DSERR_text(hr) ));
1849 ds_hardware_buffers[i].pdsb = NULL;
1855 // ---------------------------------------------------------------------------------------
1856 // ds_close_buffers()
1858 // Free the channel buffers
1860 void ds_close_buffers()
1862 ds_close_software_buffers();
1863 ds_close_hardware_buffers();
1866 // ---------------------------------------------------------------------------------------
1869 // Close the DirectSound system
1873 ds_close_all_channels();
1877 if (pPropertySet != NULL) {
1878 pPropertySet->Release();
1879 pPropertySet = NULL;
1882 if (Ds_property_set_pdsb != NULL) {
1883 Ds_property_set_pdsb->Release();
1884 Ds_property_set_pdsb = NULL;
1887 if (Ds_property_set_pds3db != NULL) {
1888 Ds_property_set_pds3db->Release();
1889 Ds_property_set_pds3db = NULL;
1892 if (pPrimaryBuffer) {
1893 pPrimaryBuffer->Release();
1894 pPrimaryBuffer = NULL;
1903 pDirectSound->Release();
1904 pDirectSound = NULL;
1907 if ( Ds_dll_loaded ) {
1908 FreeLibrary(Ds_dll_handle);
1912 if (Ds_must_call_couninitialize == 1) {
1917 // free the Channels[] array, since it was dynamically allocated
1922 ds_sound_context = alcGetCurrentContext();
1923 ds_sound_device = alcGetContextsDevice(ds_sound_context);
1924 alcDestroyContext(ds_sound_context);
1925 alcCloseDevice(ds_sound_device);
1929 // ---------------------------------------------------------------------------------------
1930 // ds_get_3d_interface()
1932 // Get the 3d interface for a secondary buffer.
1934 // If the secondary buffer wasn't created with a DSBCAPS_CTRL3D flag, then no 3d interface
1938 void ds_get_3d_interface(LPDIRECTSOUNDBUFFER pdsb, LPDIRECTSOUND3DBUFFER *ppds3db)
1943 dsbc.dwSize = sizeof(dsbc);
1944 DSResult = pdsb->GetCaps(&dsbc);
1945 if ( DSResult == DS_OK && dsbc.dwFlags & DSBCAPS_CTRL3D ) {
1946 DSResult = pdsb->QueryInterface( IID_IDirectSound3DBuffer, (void**)ppds3db );
1947 if ( DSResult != DS_OK ) {
1948 nprintf(("SOUND","Could not obtain 3D interface for hardware buffer: %s\n", get_DSERR_text(DSResult) ));
1955 // ---------------------------------------------------------------------------------------
1956 // ds_get_free_channel()
1958 // Find a free channel to play a sound on. If no free channels exists, free up one based
1959 // on volume levels.
1961 // input: new_volume => volume in DS units for sound to play at
1962 // snd_id => which kind of sound to play
1963 // priority => DS_MUST_PLAY
1968 // returns: channel number to play sound on
1969 // -1 if no channel could be found
1971 // NOTE: snd_id is needed since we limit the number of concurrent samples
1975 int ds_get_free_channel(int new_volume, int snd_id, int priority)
1978 int i, first_free_channel, limit;
1979 int lowest_vol = 0, lowest_vol_index = -1;
1980 int instance_count; // number of instances of sound already playing
1981 int lowest_instance_vol, lowest_instance_vol_index;
1986 lowest_instance_vol = 99;
1987 lowest_instance_vol_index = -1;
1988 first_free_channel = -1;
1990 // Look for a channel to use to play this sample
1991 for ( i = 0; i < MAX_CHANNELS; i++ ) {
1993 if ( chp->source_id == 0 ) {
1994 if ( first_free_channel == -1 )
1995 first_free_channel = i;
1999 alGetSourcei(chp->source_id, AL_SOURCE_STATE, &status);
2001 OpenAL_ErrorCheck();
2003 if ( status != AL_PLAYING ) {
2004 if ( first_free_channel == -1 )
2005 first_free_channel = i;
2009 if ( chp->snd_id == snd_id ) {
2011 if ( chp->vol < lowest_instance_vol && chp->looping == FALSE ) {
2012 lowest_instance_vol = chp->vol;
2013 lowest_instance_vol_index = i;
2017 if ( chp->vol < lowest_vol && chp->looping == FALSE ) {
2018 lowest_vol_index = i;
2019 lowest_vol = chp->vol;
2024 // determine the limit of concurrent instances of this sound
2035 case DS_LIMIT_THREE:
2045 // If we've exceeded the limit, then maybe stop the duplicate if it is lower volume
2046 if ( instance_count >= limit ) {
2047 // If there is a lower volume duplicate, stop it.... otherwise, don't play the sound
2048 if ( lowest_instance_vol_index >= 0 && (Channels[lowest_instance_vol_index].vol <= new_volume) ) {
2049 first_free_channel = lowest_instance_vol_index;
2051 first_free_channel = -1;
2054 // there is no limit barrier to play the sound, so see if we've ran out of channels
2055 if ( first_free_channel == -1 ) {
2056 // stop the lowest volume instance to play our sound if priority demands it
2057 if ( lowest_vol_index != -1 && priority == DS_MUST_PLAY ) {
2058 // Check if the lowest volume playing is less than the volume of the requested sound.
2059 // If so, then we are going to trash the lowest volume sound.
2060 if ( Channels[lowest_vol_index].vol <= new_volume ) {
2061 first_free_channel = lowest_vol_index;
2067 return first_free_channel;
2069 int i, first_free_channel, limit;
2070 int lowest_vol = 0, lowest_vol_index = -1;
2071 int instance_count; // number of instances of sound already playing
2072 int lowest_instance_vol, lowest_instance_vol_index;
2073 unsigned long status;
2078 lowest_instance_vol = 99;
2079 lowest_instance_vol_index = -1;
2080 first_free_channel = -1;
2082 // Look for a channel to use to play this sample
2083 for ( i = 0; i < MAX_CHANNELS; i++ ) {
2085 if ( chp->pdsb == NULL ) {
2086 if ( first_free_channel == -1 )
2087 first_free_channel = i;
2091 hr = chp->pdsb->GetStatus(&status);
2092 if ( hr != DS_OK ) {
2093 nprintf(("Sound", "SOUND ==> GetStatus failed with return value %s\n", get_DSERR_text(hr) ));
2096 if ( !(status & DSBSTATUS_PLAYING) ) {
2097 if ( first_free_channel == -1 )
2098 first_free_channel = i;
2099 ds_close_channel(i);
2103 if ( chp->snd_id == snd_id ) {
2105 if ( chp->vol < lowest_instance_vol && chp->looping == FALSE ) {
2106 lowest_instance_vol = chp->vol;
2107 lowest_instance_vol_index = i;
2111 if ( chp->vol < lowest_vol && chp->looping == FALSE ) {
2112 lowest_vol_index = i;
2113 lowest_vol = chp->vol;
2118 // determine the limit of concurrent instances of this sound
2129 case DS_LIMIT_THREE:
2139 // If we've exceeded the limit, then maybe stop the duplicate if it is lower volume
2140 if ( instance_count >= limit ) {
2141 // If there is a lower volume duplicate, stop it.... otherwise, don't play the sound
2142 if ( lowest_instance_vol_index >= 0 && (Channels[lowest_instance_vol_index].vol <= new_volume) ) {
2143 ds_close_channel(lowest_instance_vol_index);
2144 first_free_channel = lowest_instance_vol_index;
2146 first_free_channel = -1;
2149 // there is no limit barrier to play the sound, so see if we've ran out of channels
2150 if ( first_free_channel == -1 ) {
2151 // stop the lowest volume instance to play our sound if priority demands it
2152 if ( lowest_vol_index != -1 && priority == DS_MUST_PLAY ) {
2153 // Check if the lowest volume playing is less than the volume of the requested sound.
2154 // If so, then we are going to trash the lowest volume sound.
2155 if ( Channels[lowest_vol_index].vol <= new_volume ) {
2156 ds_close_channel(lowest_vol_index);
2157 first_free_channel = lowest_vol_index;
2163 return first_free_channel;
2168 // ---------------------------------------------------------------------------------------
2171 // Find a free channel to play a sound on. If no free channels exists, free up one based
2172 // on volume levels.
2174 // returns: 0 => dup was successful
2175 // -1 => dup failed (Channels[channel].pdsb will be NULL)
2178 int ds_channel_dup(LPDIRECTSOUNDBUFFER pdsb, int channel, int use_ds3d)
2182 // Duplicate the master buffer into a channel buffer.
2183 DSResult = pDirectSound->DuplicateSoundBuffer(pdsb, &Channels[channel].pdsb );
2184 if ( DSResult != DS_OK ) {
2185 nprintf(("Sound", "SOUND ==> DuplicateSoundBuffer failed with return value %s\n", get_DSERR_text(DSResult) ));
2186 Channels[channel].pdsb = NULL;
2190 // get the 3d interface for the buffer if it exists
2192 if (Channels[channel].pds3db == NULL) {
2193 ds_get_3d_interface(Channels[channel].pdsb, &Channels[channel].pds3db);
2201 // ---------------------------------------------------------------------------------------
2202 // ds_restore_buffer()
2205 void ds_restore_buffer(LPDIRECTSOUNDBUFFER pdsb)
2209 Int3(); // get Alan, he wants to see this
2210 hr = pdsb->Restore();
2211 if ( hr != DS_OK ) {
2212 nprintf(("Sound", "Sound ==> Lost a buffer, tried restoring but got %s\n", get_DSERR_text(hr) ));
2217 // Create a direct sound buffer in software, without locking any data in
2218 int ds_create_buffer(int frequency, int bits_per_sample, int nchannels, int nseconds)
2224 if (!ds_initialized) {
2230 nprintf(("Sound","SOUND ==> No more OpenAL buffers available\n"));
2234 alGenBuffers (1, &i);
2236 sound_buffers[sid].buf_id = i;
2237 sound_buffers[sid].source_id = -1;
2238 sound_buffers[sid].frequency = frequency;
2239 sound_buffers[sid].bits_per_sample = bits_per_sample;
2240 sound_buffers[sid].nchannels = nchannels;
2241 sound_buffers[sid].nseconds = nseconds;
2242 sound_buffers[sid].nbytes = nseconds * (bits_per_sample / 8) * nchannels * frequency;
2251 if (!ds_initialized) {
2257 nprintf(("Sound","SOUND ==> No more software secondary buffers available\n"));
2261 // Set up buffer format
2262 wfx.wFormatTag = WAVE_FORMAT_PCM;
2263 wfx.nChannels = (unsigned short)nchannels;
2264 wfx.nSamplesPerSec = frequency;
2265 wfx.wBitsPerSample = (unsigned short)bits_per_sample;
2267 wfx.nBlockAlign = (unsigned short)(wfx.nChannels * (wfx.wBitsPerSample / 8));
2268 wfx.nAvgBytesPerSec = wfx.nBlockAlign * wfx.nSamplesPerSec;
2270 memset(&dsbd, 0, sizeof(DSBUFFERDESC));
2271 dsbd.dwSize = sizeof(DSBUFFERDESC);
2272 dsbd.dwBufferBytes = wfx.nAvgBytesPerSec * nseconds;
2273 dsbd.lpwfxFormat = &wfx;
2274 dsbd.dwFlags = DSBCAPS_STATIC | DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_CTRLDEFAULT | DSBCAPS_LOCSOFTWARE;
2276 dsrval = pDirectSound->CreateSoundBuffer(&dsbd, &ds_software_buffers[sid].pdsb, NULL);
2277 if ( dsrval != DS_OK ) {
2281 ds_software_buffers[sid].desc = dsbd;
2286 // Lock data into an existing buffer
2287 int ds_lock_data(int sid, unsigned char *data, int size)
2292 ALuint buf_id = sound_buffers[sid].buf_id;
2295 if (sound_buffers[sid].bits_per_sample == 16) {
2296 if (sound_buffers[sid].nchannels == 2) {
2297 format = AL_FORMAT_STEREO16;
2298 } else if (sound_buffers[sid].nchannels == 1) {
2299 format = AL_FORMAT_MONO16;
2303 } else if (sound_buffers[sid].bits_per_sample == 8) {
2304 if (sound_buffers[sid].nchannels == 2) {
2305 format = AL_FORMAT_STEREO8;
2306 } else if (sound_buffers[sid].nchannels == 1) {
2307 format = AL_FORMAT_MONO8;
2315 sound_buffers[sid].nbytes = size;
2317 alBufferData(buf_id, format, data, size, sound_buffers[sid].frequency);
2319 OpenAL_ErrorCheck();
2324 LPDIRECTSOUNDBUFFER pdsb;
2326 void *buffer_data, *buffer_data2;
2327 DWORD buffer_size, buffer_size2;
2330 pdsb = ds_software_buffers[sid].pdsb;
2332 memset(&caps, 0, sizeof(DSBCAPS));
2333 caps.dwSize = sizeof(DSBCAPS);
2334 dsrval = pdsb->GetCaps(&caps);
2335 if ( dsrval != DS_OK ) {
2339 pdsb->SetCurrentPosition(0);
2341 // lock the entire buffer
2342 dsrval = pdsb->Lock(0, caps.dwBufferBytes, &buffer_data, &buffer_size, &buffer_data2, &buffer_size2, 0 );
2343 if ( dsrval != DS_OK ) {
2347 // first clear it out with silence
2348 memset(buffer_data, 0x80, buffer_size);
2349 memcpy(buffer_data, data, size);
2351 dsrval = pdsb->Unlock(buffer_data, buffer_size, 0, 0);
2352 if ( dsrval != DS_OK ) {
2360 // Stop a buffer from playing directly
2361 void ds_stop_easy(int sid)
2366 int cid = sound_buffers[sid].source_id;
2369 ALuint source_id = Channels[cid].source_id;
2371 alSourceStop(source_id);
2375 LPDIRECTSOUNDBUFFER pdsb;
2378 pdsb = ds_software_buffers[sid].pdsb;
2379 dsrval = pdsb->Stop();
2383 // Play a sound without the usual baggage (used for playing back real-time voice)
2386 // sid => software id of sound
2387 // volume => volume of sound effect in DirectSound units
2388 int ds_play_easy(int sid, int volume)
2391 if (!ds_initialized)
2394 int channel = ds_get_free_channel(volume, -1, DS_MUST_PLAY);
2397 ALuint source_id = Channels[channel].source_id;
2399 alSourceStop(source_id);
2401 if (Channels[channel].buf_id != sid) {
2402 ALuint buffer_id = sound_buffers[sid].buf_id;
2404 alSourcei(source_id, AL_BUFFER, buffer_id);
2406 OpenAL_ErrorCheck();
2409 Channels[channel].buf_id = sid;
2411 ALfloat alvol = (volume != -10000) ? pow(10.0, (float)volume / (-600.0 / log10(.5))): 0.0;
2413 alSourcef(source_id, AL_GAIN, alvol);
2415 alSourcei(source_id, AL_LOOPING, AL_FALSE);
2416 alSourcePlay(source_id);
2418 OpenAL_ErrorCheck();
2426 LPDIRECTSOUNDBUFFER pdsb;
2429 pdsb = ds_software_buffers[sid].pdsb;
2431 pdsb->SetVolume(volume);
2432 dsrval=pdsb->Play(0, 0, 0);
2433 if ( dsrval != DS_OK ) {
2441 // ---------------------------------------------------------------------------------------
2442 // Play a DirectSound secondary buffer.
2446 // sid => software id of sound
2447 // hid => hardware id of sound ( -1 if not in hardware )
2448 // snd_id => what kind of sound this is
2449 // priority => DS_MUST_PLAY
2453 // volume => volume of sound effect in DirectSound units
2454 // pan => pan of sound in DirectSound units
2455 // looping => whether the sound effect is looping or not
2457 // returns: -1 => sound effect could not be started
2458 // >=0 => sig for sound effect successfully started
2460 int ds_play(int sid, int hid, int snd_id, int priority, int volume, int pan, int looping, bool is_voice_msg)
2465 if (!ds_initialized)
2468 channel = ds_get_free_channel(volume, snd_id, priority);
2471 if ( Channels[channel].source_id == 0 ) {
2475 if ( ds_using_ds3d() ) {
2479 Channels[channel].vol = volume;
2480 Channels[channel].looping = looping;
2481 Channels[channel].priority = priority;
2483 // set new position for pan or zero out if none
2484 ALfloat alpan = (float)pan / MAX_PAN;
2487 alSource3f(Channels[channel].source_id, AL_POSITION, alpan, 0.0, 1.0);
2489 alSource3f(Channels[channel].source_id, AL_POSITION, 0.0, 0.0, 0.0);
2492 OpenAL_ErrorCheck();
2494 alSource3f(Channels[channel].source_id, AL_VELOCITY, 0.0, 0.0, 0.0);
2496 OpenAL_ErrorCheck();
2498 alSourcef(Channels[channel].source_id, AL_PITCH, 1.0);
2500 OpenAL_ErrorCheck();
2502 ALfloat alvol = (volume != -10000) ? pow(10.0, (float)volume / (-600.0 / log10(.5))): 0.0;
2503 alSourcef(Channels[channel].source_id, AL_GAIN, alvol);
2505 Channels[channel].is_voice_msg = is_voice_msg;
2507 OpenAL_ErrorCheck();
2510 alGetSourcei(Channels[channel].source_id, AL_SOURCE_STATE, &status);
2512 OpenAL_ErrorCheck();
2514 if (status == AL_PLAYING)
2515 alSourceStop(Channels[channel].source_id);
2517 OpenAL_ErrorCheck();
2519 alSourcei (Channels[channel].source_id, AL_BUFFER, sound_buffers[sid].buf_id);
2521 OpenAL_ErrorCheck();
2523 alSourcei (Channels[channel].source_id, AL_LOOPING, (looping) ? AL_TRUE : AL_FALSE);
2525 OpenAL_ErrorCheck();
2527 alSourcePlay(Channels[channel].source_id);
2529 OpenAL_ErrorCheck();
2531 sound_buffers[sid].source_id = channel;
2532 Channels[channel].buf_id = sid;
2535 // nprintf(( "Sound", "SOUND ==> Not playing sound requested at volume %.2f\n", ds_get_percentage_vol(volume) ));
2539 Channels[channel].snd_id = snd_id;
2540 Channels[channel].sig = channel_next_sig++;
2541 if (channel_next_sig < 0 ) {
2542 channel_next_sig = 1;
2545 Channels[channel].last_position = 0;
2547 // make sure there aren't any looping voice messages
2548 for (int i=0; i<MAX_CHANNELS; i++) {
2549 if (Channels[i].is_voice_msg == true) {
2550 if (Channels[i].source_id == 0) {
2554 DWORD current_position = ds_get_play_position(i);
2555 if (current_position != 0) {
2556 if (current_position < Channels[i].last_position) {
2559 Channels[i].last_position = current_position;
2565 return Channels[channel].sig;
2570 if (!ds_initialized)
2573 channel = ds_get_free_channel(volume, snd_id, priority);
2576 if ( Channels[channel].pdsb != NULL ) {
2580 // First check if the sound is in hardware, and try to duplicate from there
2583 if ( ds_channel_dup(ds_hardware_buffers[hid].pdsb, channel, 0) == 0 ) {
2584 // nprintf(("Sound", "SOUND ==> Played sound in hardware..\n"));
2588 // Channel will be NULL if hardware dup failed, or there was no hardware dup attempted
2589 if ( Channels[channel].pdsb == NULL ) {
2590 if ( ds_channel_dup(ds_software_buffers[sid].pdsb, channel, 0) == 0 ) {
2591 // nprintf(("Sound", "SOUND ==> Played sound in software..\n"));
2595 if ( Channels[channel].pdsb == NULL ) {
2599 if ( ds_using_ds3d() ) {
2600 if ( ds_is_3d_buffer(Channels[channel].pdsb) ) {
2601 if (Channels[channel].pds3db == NULL) {
2602 ds_get_3d_interface(Channels[channel].pdsb, &Channels[channel].pds3db);
2604 if ( Channels[channel].pds3db ) {
2605 Channels[channel].pds3db->SetMode(DS3DMODE_DISABLE,DS3D_IMMEDIATE);
2611 Channels[channel].vol = volume;
2612 Channels[channel].looping = looping;
2613 Channels[channel].priority = priority;
2614 Channels[channel].pdsb->SetPan(pan);
2615 Channels[channel].pdsb->SetVolume(volume);
2616 Channels[channel].is_voice_msg = is_voice_msg;
2620 ds_flags |= DSBPLAY_LOOPING;
2622 DSResult = Channels[channel].pdsb->Play(0, 0, ds_flags );
2625 if (Stop_logging_sounds == false) {
2627 sprintf(buf, "channel %d, address: %x, ds_flags: %d", channel, Channels[channel].pdsb, ds_flags);
2628 HUD_add_to_scrollback(buf, 3);
2632 if ( DSResult == DSERR_BUFFERLOST ) {
2633 ds_restore_buffer(Channels[channel].pdsb);
2634 DSResult = Channels[channel].pdsb->Play(0, 0, ds_flags );
2637 if ( DSResult != DS_OK ) {
2638 nprintf(("Sound", "Sound ==> Play failed with return value %s\n", get_DSERR_text(DSResult) ));
2643 // nprintf(( "Sound", "SOUND ==> Not playing sound requested at volume %.2f\n", ds_get_percentage_vol(volume) ));
2647 Channels[channel].snd_id = snd_id;
2648 Channels[channel].sig = channel_next_sig++;
2649 if (channel_next_sig < 0 ) {
2650 channel_next_sig = 1;
2654 if (Stop_logging_sounds == false) {
2657 sprintf(buf, "VOICE sig: %d, sid: %d, snd_id: %d, ch: %d", Channels[channel].sig, sid, snd_id, channel);
2658 HUD_add_to_scrollback(buf, 3);
2663 Channels[channel].last_position = 0;
2665 // make sure there aren't any looping voice messages
2666 for (int i=0; i<MAX_CHANNELS; i++) {
2667 if (Channels[i].is_voice_msg == true) {
2668 if (Channels[i].pdsb == NULL) {
2672 DWORD current_position = ds_get_play_position(i);
2673 if (current_position != 0) {
2674 if (current_position < Channels[i].last_position) {
2675 ds_close_channel(i);
2677 Channels[i].last_position = current_position;
2683 return Channels[channel].sig;
2688 // ---------------------------------------------------------------------------------------
2691 // Return the channel number that is playing the sound identified by sig. If that sound is
2692 // not playing, return -1.
2694 int ds_get_channel(int sig)
2699 for ( i = 0; i < MAX_CHANNELS; i++ ) {
2700 if ( Channels[i].source_id && Channels[i].sig == sig ) {
2701 if ( ds_is_channel_playing(i) == TRUE ) {
2711 for ( i = 0; i < MAX_CHANNELS; i++ ) {
2712 if ( Channels[i].pdsb && Channels[i].sig == sig ) {
2713 if ( ds_is_channel_playing(i) == TRUE ) {
2722 // ---------------------------------------------------------------------------------------
2723 // ds_is_channel_playing()
2726 int ds_is_channel_playing(int channel)
2729 if ( Channels[channel].source_id != 0 ) {
2732 alGetSourcei(Channels[channel].source_id, AL_SOURCE_STATE, &status);
2733 OpenAL_ErrorCheck();
2735 return (status == AL_PLAYING);
2741 unsigned long status;
2743 if ( !Channels[channel].pdsb ) {
2747 hr = Channels[channel].pdsb->GetStatus(&status);
2748 if ( hr != DS_OK ) {
2749 nprintf(("Sound", "SOUND ==> GetStatus failed with return value %s\n", get_DSERR_text(hr) ));
2753 if ( status & DSBSTATUS_PLAYING )
2760 // ---------------------------------------------------------------------------------------
2761 // ds_stop_channel()
2764 void ds_stop_channel(int channel)
2767 if ( Channels[channel].source_id != 0 ) {
2768 alSourceStop(Channels[channel].source_id);
2771 ds_close_channel(channel);
2775 // ---------------------------------------------------------------------------------------
2776 // ds_stop_channel_all()
2779 void ds_stop_channel_all()
2784 for ( i=0; i<MAX_CHANNELS; i++ ) {
2785 if ( Channels[i].source_id != 0 ) {
2786 alSourceStop(Channels[i].source_id);
2792 for ( i=0; i<MAX_CHANNELS; i++ ) {
2793 if ( Channels[i].pdsb != NULL ) {
2800 // ---------------------------------------------------------------------------------------
2803 // Set the volume for a channel. The volume is expected to be in DirectSound units
2805 // If the sound is a 3D sound buffer, this is like re-establishing the maximum
2808 void ds_set_volume( int channel, int vol )
2811 ALuint source_id = Channels[channel].source_id;
2813 if (source_id != 0) {
2814 ALfloat alvol = (vol != -10000) ? pow(10.0, (float)vol / (-600.0 / log10(.5))): 0.0;
2816 alSourcef(source_id, AL_GAIN, alvol);
2820 unsigned long status;
2822 hr = Channels[channel].pdsb->GetStatus(&status);
2823 if ( hr != DS_OK ) {
2824 nprintf(("Sound", "SOUND ==> GetStatus failed with return value %s\n", get_DSERR_text(hr) ));
2828 if ( status & DSBSTATUS_PLAYING ) {
2829 Channels[channel].pdsb->SetVolume(vol);
2834 // ---------------------------------------------------------------------------------------
2837 // Set the pan for a channel. The pan is expected to be in DirectSound units
2839 void ds_set_pan( int channel, int pan )
2844 alGetSourcei(Channels[channel].source_id, AL_SOURCE_STATE, &state);
2846 if (state == AL_PLAYING) {
2847 ALfloat alpan = (pan != 0) ? ((float)pan / MAX_PAN) : 0.0;
2848 alSource3f(Channels[channel].source_id, AL_POSITION, alpan, 0.0, 1.0);
2852 unsigned long status;
2854 hr = Channels[channel].pdsb->GetStatus(&status);
2855 if ( hr != DS_OK ) {
2856 nprintf(("Sound", "SOUND ==> GetStatus failed with return value %s\n", get_DSERR_text(hr) ));
2860 if ( status & DSBSTATUS_PLAYING ) {
2861 Channels[channel].pdsb->SetPan(pan);
2866 // ---------------------------------------------------------------------------------------
2869 // Get the pitch of a channel
2871 int ds_get_pitch(int channel)
2875 ALfloat alpitch = 0;
2878 alGetSourcei(Channels[channel].source_id, AL_SOURCE_STATE, &status);
2880 if (status == AL_PLAYING)
2881 alGetSourcef(Channels[channel].source_id, AL_PITCH, &alpitch);
2883 // convert OpenAL values to DirectSound values and return
2884 pitch = fl2i( pow(10.0, (alpitch + 2.0)) );
2888 unsigned long status, pitch = 0;
2891 hr = Channels[channel].pdsb->GetStatus(&status);
2893 if ( hr != DS_OK ) {
2894 nprintf(("Sound", "SOUND ==> GetStatus failed with return value %s\n", get_DSERR_text(hr) ));
2898 if ( status & DSBSTATUS_PLAYING ) {
2899 hr = Channels[channel].pdsb->GetFrequency(&pitch);
2900 if ( hr != DS_OK ) {
2901 nprintf(("Sound", "SOUND ==> GetFrequency failed with return value %s\n", get_DSERR_text(hr) ));
2910 // ---------------------------------------------------------------------------------------
2913 // Set the pitch of a channel
2915 void ds_set_pitch(int channel, int pitch)
2920 if ( pitch < MIN_PITCH )
2923 if ( pitch > MAX_PITCH )
2926 alGetSourcei(Channels[channel].source_id, AL_SOURCE_STATE, &status);
2928 if (status == AL_PLAYING) {
2929 ALfloat alpitch = log10(pitch) - 2.0;
2930 alSourcef(Channels[channel].source_id, AL_PITCH, alpitch);
2933 unsigned long status;
2936 hr = Channels[channel].pdsb->GetStatus(&status);
2937 if ( hr != DS_OK ) {
2938 nprintf(("Sound", "SOUND ==> GetStatus failed with return value %s\n", get_DSERR_text(hr) ));
2942 if ( pitch < MIN_PITCH )
2945 if ( pitch > MAX_PITCH )
2948 if ( status & DSBSTATUS_PLAYING ) {
2949 Channels[channel].pdsb->SetFrequency((unsigned long)pitch);
2954 // ---------------------------------------------------------------------------------------
2955 // ds_chg_loop_status()
2958 void ds_chg_loop_status(int channel, int loop)
2961 ALuint source_id = Channels[channel].source_id;
2963 alSourcei(source_id, AL_LOOPING, loop ? AL_TRUE : AL_FALSE);
2965 unsigned long status;
2968 hr = Channels[channel].pdsb->GetStatus(&status);
2969 if ( hr != DS_OK ) {
2970 nprintf(("Sound", "SOUND ==> GetStatus failed with return value %s\n", get_DSERR_text(hr) ));
2974 if ( !(status & DSBSTATUS_PLAYING) )
2975 return; // sound is not playing anymore
2977 if ( status & DSBSTATUS_LOOPING ) {
2979 return; // we are already looping
2981 // stop the sound from looping
2982 hr = Channels[channel].pdsb->Play(0,0,0);
2987 return; // the sound is already not looping
2989 // start the sound looping
2990 hr = Channels[channel].pdsb->Play(0,0,DSBPLAY_LOOPING);
2996 // ---------------------------------------------------------------------------------------
2999 // Starts a ds3d sound playing
3003 // sid => software id for sound to play
3004 // hid => hardware id for sound to play (-1 if not in hardware)
3005 // snd_id => identifies what type of sound is playing
3006 // pos => world pos of sound
3007 // vel => velocity of object emitting sound
3008 // min => distance at which sound doesn't get any louder
3009 // max => distance at which sound becomes inaudible
3010 // looping => boolean, whether to loop the sound or not
3011 // max_volume => volume (-10000 to 0) for 3d sound at maximum
3012 // estimated_vol => manual estimated volume
3013 // priority => DS_MUST_PLAY
3018 // returns: 0 => sound started successfully
3019 // -1 => sound could not be played
3021 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 )
3031 if (!ds_initialized)
3034 channel = ds_get_free_channel(estimated_vol, snd_id, priority);
3037 Assert(Channels[channel].pdsb == NULL);
3039 // First check if the sound is in hardware, and try to duplicate from there
3042 if ( ds_is_3d_buffer(ds_hardware_buffers[hid].pdsb) == FALSE ) {
3043 nprintf(("Sound", "SOUND ==> Tried to play non-3d buffer in ds3d_play()..\n"));
3047 if ( ds_channel_dup(ds_hardware_buffers[hid].pdsb, channel, 1) == 0 ) {
3048 nprintf(("Sound", "SOUND ==> Played sound using DirectSound3D in hardware..\n"));
3052 // Channel will be NULL if hardware dup failed, or there was no hardware dup attempted
3053 if ( Channels[channel].pdsb == NULL ) {
3056 if ( ds_is_3d_buffer(ds_software_buffers[sid].pdsb) == FALSE ) {
3057 nprintf(("Sound", "SOUND ==> Tried to play non-3d buffer in ds3d_play()..\n"));
3062 if ( ds_channel_dup(ds_software_buffers[sid].pdsb, channel, 1) == 0 ) {
3063 // nprintf(("Sound", "SOUND ==> Played sound using DirectSound3D \n"));
3067 if ( Channels[channel].pdsb == NULL ) {
3072 desc = ds_software_buffers[sid].desc;
3073 desc.lpwfxFormat = &ds_software_buffers[sid].wfx;
3075 // duplicate buffer failed, so call CreateBuffer instead
3077 hr = pDirectSound->CreateSoundBuffer(&desc, &Channels[channel].pdsb, NULL );
3079 if ( (hr == DS_OK) && (Channels[channel].pdsb) ) {
3080 BYTE *pdest, *pdest2;
3082 DWORD src_ds_size, dest_ds_size, not_used;
3085 if ( ds_get_size(sid, &src_size) != 0 ) {
3087 Channels[channel].pdsb->Release();
3091 // lock the src buffer
3092 hr = ds_software_buffers[sid].pdsb->Lock(0, src_size, (void**)&psrc, &src_ds_size, (void**)&psrc2, ¬_used, 0);
3093 if ( hr != DS_OK ) {
3094 mprintf(("err: %s\n", get_DSERR_text(hr)));
3096 Channels[channel].pdsb->Release();
3100 if ( Channels[channel].pdsb->Lock(0, src_ds_size, (void**)(&pdest), &dest_ds_size, (void**)&pdest2, ¬_used, 0) == DS_OK) {
3101 memcpy(pdest, psrc, src_ds_size);
3102 Channels[channel].pdsb->Unlock(pdest, dest_ds_size, 0, 0);
3103 ds_get_3d_interface(Channels[channel].pdsb, &Channels[channel].pds3db);
3105 Channels[channel].pdsb->Release();
3112 Assert(Channels[channel].pds3db );
3113 Channels[channel].pds3db->SetMode(DS3DMODE_NORMAL,DS3D_IMMEDIATE);
3115 // set up 3D sound data here
3116 ds3d_update_buffer(channel, i2fl(min), i2fl(max), pos, vel);
3118 Channels[channel].vol = estimated_vol;
3119 Channels[channel].looping = looping;
3121 // sets the maximum "inner cone" volume
3122 Channels[channel].pdsb->SetVolume(max_volume);
3126 ds_flags |= DSBPLAY_LOOPING;
3129 hr = Channels[channel].pdsb->Play(0, 0, ds_flags );
3131 if ( hr == DSERR_BUFFERLOST ) {
3132 ds_restore_buffer(Channels[channel].pdsb);
3133 hr = Channels[channel].pdsb->Play(0, 0, ds_flags );
3136 if ( hr != DS_OK ) {
3137 nprintf(("Sound", "Sound ==> Play failed with return value %s\n", get_DSERR_text(hr) ));
3138 if ( Channels[channel].pdsb ) {
3140 while(++attempts < 10) {
3141 hr = Channels[channel].pdsb->Release();
3142 if ( hr == DS_OK ) {
3145 nprintf(("Sound","SOUND ==> DirectSound Release() failed with code %s\n.",get_DSERR_text(hr) ));
3149 Channels[channel].pdsb = NULL;
3155 nprintf(( "Sound", "SOUND ==> Not playing requested 3D sound\n"));
3159 Channels[channel].snd_id = snd_id;
3160 Channels[channel].sig = channel_next_sig++;
3161 if (channel_next_sig < 0 ) {
3162 channel_next_sig = 1;
3164 return Channels[channel].sig;
3168 void ds_set_position(int channel, DWORD offset)
3173 // set the position of the sound buffer
3174 Channels[channel].pdsb->SetCurrentPosition(offset);
3178 DWORD ds_get_play_position(int channel)
3183 if (!AL_play_position)
3186 alGetSourcei(Channels[channel].source_id, AL_BYTE_LOKI, &pos);
3194 if ( Channels[channel].pdsb ) {
3195 Channels[channel].pdsb->GetCurrentPosition((LPDWORD)&play,(LPDWORD)&write);
3204 DWORD ds_get_write_position(int channel)
3212 if ( Channels[channel].pdsb ) {
3213 Channels[channel].pdsb->GetCurrentPosition((LPDWORD)&play,(LPDWORD)&write);
3222 int ds_get_channel_size(int channel)
3225 int buf_id = Channels[channel].buf_id;
3228 return sound_buffers[buf_id].nbytes;
3237 if ( Channels[channel].pdsb ) {
3238 memset(&caps, 0, sizeof(DSBCAPS));
3239 caps.dwSize = sizeof(DSBCAPS);
3240 dsrval = Channels[channel].pdsb->GetCaps(&caps);
3241 if ( dsrval != DS_OK ) {
3244 size = caps.dwBufferBytes;
3253 // Returns the number of channels that are actually playing
3254 int ds_get_number_channels()
3259 if (!ds_initialized) {
3264 for ( i = 0; i < MAX_CHANNELS; i++ ) {
3265 if ( Channels[i].source_id ) {
3266 if ( ds_is_channel_playing(i) == TRUE ) {
3277 for ( i = 0; i < MAX_CHANNELS; i++ ) {
3278 if ( Channels[i].pdsb ) {
3279 if ( ds_is_channel_playing(i) == TRUE ) {
3289 // retreive raw data from a sound buffer
3290 int ds_get_data(int sid, char *data)
3298 LPDIRECTSOUNDBUFFER pdsb;
3304 pdsb = ds_software_buffers[sid].pdsb;
3306 memset(&caps, 0, sizeof(DSBCAPS));
3307 caps.dwSize = sizeof(DSBCAPS);
3308 dsrval = pdsb->GetCaps(&caps);
3309 if ( dsrval != DS_OK ) {
3313 // lock the entire buffer
3314 dsrval = pdsb->Lock(0, caps.dwBufferBytes, &buffer_data, &buffer_size, 0, 0, 0);
3315 if ( dsrval != DS_OK ) {
3319 memcpy(data, buffer_data, buffer_size);
3321 dsrval = pdsb->Unlock(buffer_data, buffer_size, 0, 0);
3322 if ( dsrval != DS_OK ) {
3330 // return the size of the raw sound data
3331 int ds_get_size(int sid, int *size)
3341 LPDIRECTSOUNDBUFFER pdsb;
3345 pdsb = ds_software_buffers[sid].pdsb;
3347 memset(&caps, 0, sizeof(DSBCAPS));
3348 caps.dwSize = sizeof(DSBCAPS);
3349 dsrval = pdsb->GetCaps(&caps);
3350 if ( dsrval != DS_OK ) {
3354 *size = caps.dwBufferBytes;
3363 // Return the primary buffer interface. Note that we cast to a uint to avoid
3364 // having to include dsound.h (and thus windows.h) in ds.h.
3366 uint ds_get_primary_buffer_interface()
3372 return (uint)pPrimaryBuffer;
3376 // Return the DirectSound Interface.
3378 uint ds_get_dsound_interface()
3384 return (uint)pDirectSound;
3388 uint ds_get_property_set_interface()
3393 return (uint)pPropertySet;
3397 // --------------------
3399 // EAX Functions below
3401 // --------------------
3403 // Set the master volume for the reverb added to all sound sources.
3405 // volume: volume, range from 0 to 1.0
3407 // returns: 0 if the volume is set successfully, otherwise return -1
3409 int ds_eax_set_volume(float volume)
3416 if (Ds_eax_inited == 0) {
3420 Assert(Ds_eax_reverb);
3422 CAP(volume, 0.0f, 1.0f);
3424 hr = Ds_eax_reverb->Set(DSPROPSETID_EAX_ReverbProperties_Def, DSPROPERTY_EAX_VOLUME, NULL, 0, &volume, sizeof(float));
3425 if (SUCCEEDED(hr)) {
3433 // Set the decay time for the EAX environment (ie all sound sources)
3435 // seconds: decay time in seconds
3437 // returns: 0 if decay time is successfully set, otherwise return -1
3439 int ds_eax_set_decay_time(float seconds)
3446 if (Ds_eax_inited == 0) {
3450 Assert(Ds_eax_reverb);
3452 CAP(seconds, 0.1f, 20.0f);
3454 hr = Ds_eax_reverb->Set(DSPROPSETID_EAX_ReverbProperties_Def, DSPROPERTY_EAX_DECAYTIME, NULL, 0, &seconds, sizeof(float));
3455 if (SUCCEEDED(hr)) {
3463 // Set the damping value for the EAX environment (ie all sound sources)
3465 // damp: damp value from 0 to 2.0
3467 // returns: 0 if the damp value is successfully set, otherwise return -1
3469 int ds_eax_set_damping(float damp)
3476 if (Ds_eax_inited == 0) {
3480 Assert(Ds_eax_reverb);
3482 CAP(damp, 0.0f, 2.0f);
3484 hr = Ds_eax_reverb->Set(DSPROPSETID_EAX_ReverbProperties_Def, DSPROPERTY_EAX_DAMPING, NULL, 0, &damp, sizeof(float));
3485 if (SUCCEEDED(hr)) {
3493 // Set up the environment type for all sound sources.
3495 // envid: value from the EAX_ENVIRONMENT_* enumeration in ds_eax.h
3497 // returns: 0 if the environment is set successfully, otherwise return -1
3499 int ds_eax_set_environment(unsigned long envid)
3506 if (Ds_eax_inited == 0) {
3510 Assert(Ds_eax_reverb);
3512 hr = Ds_eax_reverb->Set(DSPROPSETID_EAX_ReverbProperties_Def, DSPROPERTY_EAX_ENVIRONMENT, NULL, 0, &envid, sizeof(unsigned long));
3513 if (SUCCEEDED(hr)) {
3521 // Set up a predefined environment for EAX
3523 // envid: value from teh EAX_ENVIRONMENT_* enumeration
3525 // returns: 0 if successful, otherwise return -1
3527 int ds_eax_set_preset(unsigned long envid)
3534 if (Ds_eax_inited == 0) {
3538 Assert(Ds_eax_reverb);
3539 Assert(envid < EAX_ENVIRONMENT_COUNT);
3541 hr = Ds_eax_reverb->Set(DSPROPSETID_EAX_ReverbProperties_Def, DSPROPERTY_EAX_ALL, NULL, 0, &Ds_eax_presets[envid], sizeof(EAX_REVERBPROPERTIES));
3542 if (SUCCEEDED(hr)) {
3551 // Set up all the parameters for an environment
3553 // id: value from teh EAX_ENVIRONMENT_* enumeration
3554 // volume: volume for the environment (0 to 1.0)
3555 // damping: damp value for the environment (0 to 2.0)
3556 // decay: decay time in seconds (0.1 to 20.0)
3558 // returns: 0 if successful, otherwise return -1
3560 int ds_eax_set_all(unsigned long id, float vol, float damping, float decay)
3567 if (Ds_eax_inited == 0) {
3571 Assert(Ds_eax_reverb);
3572 Assert(id < EAX_ENVIRONMENT_COUNT);
3574 EAX_REVERBPROPERTIES er;
3576 er.environment = id;
3578 er.fDecayTime_sec = decay;
3579 er.fDamping = damping;
3581 hr = Ds_eax_reverb->Set(DSPROPSETID_EAX_ReverbProperties_Def, DSPROPERTY_EAX_ALL, NULL, 0, &er, sizeof(EAX_REVERBPROPERTIES));
3582 if (SUCCEEDED(hr)) {
3590 // Get up the parameters for the current environment
3592 // er: (output) hold environment parameters
3594 // returns: 0 if successful, otherwise return -1
3596 int ds_eax_get_all(EAX_REVERBPROPERTIES *er)
3602 unsigned long outsize;
3604 if (Ds_eax_inited == 0) {
3608 Assert(Ds_eax_reverb);
3610 hr = Ds_eax_reverb->Get(DSPROPSETID_EAX_ReverbProperties_Def, DSPROPERTY_EAX_ALL, NULL, 0, er, sizeof(EAX_REVERBPROPERTIES), &outsize);
3611 if (SUCCEEDED(hr)) {
3619 // Close down EAX, freeing any allocated resources
3624 if (Ds_eax_inited == 0) {
3634 // returns: 0 if initialization is successful, otherwise return -1
3640 unsigned long driver_support = 0;
3642 if (Ds_eax_inited) {
3646 Assert(Ds_eax_reverb == NULL);
3648 Ds_eax_reverb = (LPKSPROPERTYSET)ds_get_property_set_interface();
3649 if (Ds_eax_reverb == NULL) {
3653 // check if the listener property is supported by the audio driver
3654 hr = Ds_eax_reverb->QuerySupport(DSPROPSETID_EAX_ReverbProperties_Def, DSPROPERTY_EAX_ALL, &driver_support);
3656 nprintf(("Sound", "QuerySupport for the EAX Listener property set failed.. disabling EAX\n"));
3657 goto ds_eax_init_failed;
3660 if ((driver_support & (KSPROPERTY_SUPPORT_GET | KSPROPERTY_SUPPORT_SET)) != (KSPROPERTY_SUPPORT_GET | KSPROPERTY_SUPPORT_SET)) {
3661 goto ds_eax_init_failed;
3664 ds_eax_set_all(EAX_ENVIRONMENT_GENERIC, 0.0f, 0.0f, 0.0f);
3670 if (Ds_eax_reverb != NULL) {
3671 Ds_eax_reverb->Release();
3672 Ds_eax_reverb = NULL;
3681 int ds_eax_is_inited()
3686 return Ds_eax_inited;
3695 if (Ds_use_a3d == 0) {
3703 // Called once per game frame to make sure voice messages aren't looping
3709 if (!ds_initialized) {
3713 for (int i=0; i<MAX_CHANNELS; i++) {
3715 if (cp->is_voice_msg == true) {
3716 if (cp->source_id == 0) {
3720 int current_position = ds_get_play_position(i);
3721 if (current_position != 0) {
3722 if (current_position < cp->last_position) {
3726 ds_close_channel(i);
3729 cp->last_position = current_position;
3742 int ds3d_update_buffer(int channel, float min, float max, vector *pos, vector *vel)
3749 int ds3d_update_listener(vector *pos, vector *vel, matrix *orient)
3754 ALfloat posv[] = { pos->x, pos->y, pos->z };
3755 ALfloat velv[] = { vel->x, vel->y, vel->z };
3756 ALfloat oriv[] = { orient->a1d[0],
3757 orient->a1d[1], orient->a1d[2],
3758 orient->a1d[3], orient->a1d[4],
3760 alListenerfv(AL_POSITION, posv);
3761 alListenerfv(AL_VELOCITY, velv);
3762 alListenerfv(AL_ORIENTATION, oriv);
3768 int ds3d_init (int unused)
3773 ALfloat pos[] = { 0.0, 0.0, 0.0 },
3774 vel[] = { 0.0, 0.0, 0.0 },
3775 ori[] = { 0.0, 0.0, 1.0, 0.0, -1.0, 0.0 };
3777 alListenerfv (AL_POSITION, pos);
3778 alListenerfv (AL_VELOCITY, vel);
3779 alListenerfv (AL_ORIENTATION, ori);
3781 if(alGetError() != AL_NO_ERROR)
3795 int dscap_create_buffer(int freq, int bits_per_sample, int nchannels, int nseconds)
3802 int dscap_get_raw_data(unsigned char *outbuf, unsigned int max_size)
3809 int dscap_max_buffersize()
3816 void dscap_release_buffer()
3821 int dscap_start_record()
3828 int dscap_stop_record()
3835 int dscap_supported()