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.13 2002/07/30 05:24:38 relnev
21 * Revision 1.12 2002/07/28 05:19:44 relnev
24 * Revision 1.11 2002/06/16 01:43:23 relnev
25 * fixed demo dogfight multiplayer mission
29 * Revision 1.10 2002/06/09 04:41:26 relnev
30 * added copyright header
32 * Revision 1.9 2002/06/05 08:05:29 relnev
33 * stub/warning removal.
35 * reworked the sound code.
37 * Revision 1.8 2002/06/05 04:03:33 relnev
38 * finished cfilesystem.
40 * removed some old code.
42 * fixed mouse save off-by-one.
46 * Revision 1.7 2002/06/02 22:31:37 cemason
49 * Revision 1.6 2002/06/02 21:11:12 cemason
52 * Revision 1.5 2002/06/02 09:50:42 relnev
55 * Revision 1.4 2002/06/02 07:17:44 cemason
56 * Added OpenAL support.
58 * Revision 1.3 2002/05/28 17:03:29 theoddone33
59 * fs2 gets to the main game loop now
61 * Revision 1.2 2002/05/27 21:35:50 theoddone33
62 * Stub out dsound backend
64 * Revision 1.1.1.1 2002/05/03 03:28:10 root
68 * 18 10/25/99 5:56p Jefff
69 * increase num software channels to the number the users hardware can
70 * handle. not less than 16, tho.
72 * 17 9/08/99 3:22p Dave
73 * Updated builtin mission list.
75 * 16 8/27/99 6:38p Alanl
76 * crush the blasted repeating messages bug
78 * 15 8/23/99 11:16p Danw
81 * 14 8/22/99 11:06p Alanl
82 * fix small bug in ds_close_channel
84 * 13 8/19/99 11:25a Alanl
85 * change format of secondary buffer from 44100 to 22050
87 * 12 8/17/99 4:11p Danw
88 * AL: temp fix for solving A3D crash
90 * 11 8/06/99 2:20p Jasonh
91 * AL: free 3D portion of buffer first
93 * 10 8/04/99 9:48p Alanl
94 * fix bug with setting 3D properties on a 2D sound buffer
96 * 9 8/04/99 11:42a Danw
97 * tone down EAX reverb
99 * 8 8/01/99 2:06p Alanl
100 * increase the rolloff for A3D
102 * 7 7/20/99 5:28p Dave
103 * Fixed debug build error.
105 * 6 7/20/99 1:49p Dave
106 * Peter Drake build. Fixed some release build warnings.
108 * 5 7/14/99 11:32a Danw
109 * AL: add some debug code to catch nefarious A3D problem
111 * 4 5/23/99 8:11p Alanl
112 * Added support for EAX
114 * 3 10/08/98 4:29p Dave
115 * Removed reference to osdefs.h
117 * 2 10/07/98 10:54a Dave
120 * 1 10/07/98 10:51a Dave
122 * 72 6/28/98 6:34p Lawrance
123 * add sanity check in while() loop for releasing channels
125 * 71 6/13/98 1:45p Sandeep
127 * 70 6/10/98 2:29p Lawrance
128 * don't use COM for initializing DirectSound... appears some machines
131 * 69 5/26/98 2:10a Lawrance
132 * make sure DirectSound pointer gets freed if Aureal resource manager
135 * 68 5/21/98 9:14p Lawrance
136 * remove obsolete registry setting
138 * 67 5/20/98 4:28p Allender
139 * upped sound buffers as per alan's request
141 * 66 5/15/98 3:36p John
142 * Fixed bug with new graphics window code and standalone server. Made
143 * hwndApp not be a global anymore.
145 * 65 5/06/98 3:37p Lawrance
146 * allow panned sounds geesh
148 * 64 5/05/98 4:49p Lawrance
149 * Put in code to authenticate A3D, improve A3D support
151 * 63 4/20/98 11:17p Lawrance
152 * fix bug with releasing channels
154 * 62 4/20/98 7:34p Lawrance
155 * take out obsolete directsound3d debug command
157 * 61 4/20/98 11:10a Lawrance
158 * put correct flags when creating sound buffer
160 * 60 4/20/98 12:03a Lawrance
161 * Allow prioritizing of CTRL3D buffers
163 * 59 4/19/98 9:31p Lawrance
164 * Use Aureal_enabled flag
166 * 58 4/19/98 9:39a Lawrance
167 * use DYNAMIC_LOOPERS for Aureal resource manager
169 * 57 4/19/98 4:13a Lawrance
170 * Improve how dsound is initialized
172 * 56 4/18/98 9:13p Lawrance
173 * Added Aureal support.
175 * 55 4/13/98 5:04p Lawrance
176 * Write functions to determine how many milliseconds are left in a sound
178 * 54 4/09/98 5:53p Lawrance
179 * Make DirectSound init more robust
181 * 53 4/01/98 9:21p John
182 * Made NDEBUG, optimized build with no warnings or errors.
184 * 52 3/31/98 5:19p John
185 * Removed demo/save/restore. Made NDEBUG defined compile. Removed a
186 * bunch of debug stuff out of player file. Made model code be able to
187 * unload models and malloc out only however many models are needed.
190 * 51 3/29/98 12:56a Lawrance
191 * preload the warp in and explosions sounds before a mission.
193 * 50 3/25/98 6:10p Lawrance
194 * Work on DirectSound3D
196 * 49 3/24/98 4:28p Lawrance
197 * Make DirectSound3D support more robust
199 * 48 3/24/98 11:49a Dave
200 * AL: Change way buffer gets locked.
202 * 47 3/24/98 11:27a Lawrance
203 * Use buffer_size for memcpy when locking buffer
205 * 46 3/23/98 10:32a Lawrance
206 * Add functions for extracting raw sound data
208 * 45 3/19/98 5:36p Lawrance
209 * Add some sound debug functions to see how many sounds are playing, and
210 * to start/stop random looping sounds.
212 * 44 3/07/98 3:35p Dave
213 * AL: check for ds being initialized in ds_create_buffer()
215 * 43 2/18/98 5:49p Lawrance
216 * Even if the ADPCM codec is unavailable, allow game to continue.
218 * 42 2/16/98 7:31p Lawrance
219 * get compression/decompression of voice working
221 * 41 2/15/98 11:10p Lawrance
222 * more work on real-time voice system
224 * 40 2/15/98 4:43p Lawrance
225 * work on real-time voice
227 * 39 2/06/98 7:30p John
228 * Added code to monitor the number of channels of sound actually playing.
230 * 38 2/06/98 8:56a Allender
231 * fixed calling convention problem with DLL handles
233 * 37 2/04/98 6:08p Lawrance
234 * Read function pointers from dsound.dll, further work on
235 * DirectSoundCapture.
237 * 36 2/03/98 11:53p Lawrance
238 * Adding support for DirectSoundCapture
240 * 35 1/31/98 5:48p Lawrance
241 * Start on real-time voice recording
243 * 34 1/10/98 1:14p John
244 * Added explanation to debug console commands
246 * 33 12/21/97 4:33p John
247 * Made debug console functions a class that registers itself
248 * automatically, so you don't need to add the function to
249 * debugfunctions.cpp.
251 * 32 12/08/97 12:24a Lawrance
252 * Allow duplicate sounds to be stopped if less than OR equal to new sound
255 * 31 12/05/97 5:19p Lawrance
256 * re-do sound priorities to make more general and extensible
258 * 30 11/28/97 2:09p Lawrance
259 * Overhaul how ADPCM conversion works... use much less memory... safer
262 * 29 11/22/97 11:32p Lawrance
263 * decompress ADPCM data into 8 bit (not 16bit) for regular sounds (ie not
266 * 28 11/20/97 5:36p Dave
267 * Hooked in a bunch of main hall changes (including sound). Made it
268 * possible to reposition (rewind/ffwd)
269 * sound buffer pointers. Fixed animation direction change framerate
272 * 27 10/13/97 7:41p Lawrance
273 * store duration of sound
275 * 26 10/11/97 6:39p Lawrance
276 * start playing primary buffer, to reduce latency on sounds starting
278 * 25 10/08/97 5:09p Lawrance
279 * limit player impact sounds so only one plays at a time
281 * 24 9/26/97 5:43p Lawrance
282 * fix a bug that was freeing memory early when playing compressed sound
285 * 23 9/09/97 3:39p Sandeep
286 * warning level 4 bugs
288 * 22 8/16/97 4:05p Lawrance
289 * don't load sounds into hardware if running Lean_and_mean
291 * 21 8/05/97 1:39p Lawrance
292 * support compressed stereo playback
294 * 20 7/31/97 10:38a Lawrance
295 * return old debug function for toggling DirectSound3D
297 * 19 7/29/97 3:27p Lawrance
298 * make console toggle for directsound3d work right
300 * 18 7/28/97 11:39a Lawrance
301 * allow individual volume scaling on 3D buffers
303 * 17 7/18/97 8:18p Lawrance
304 * fix bug in ds_get_free_channel() that caused sounds to not play when
307 * 16 7/17/97 8:04p Lawrance
308 * allow priority sounds to play if free channel, otherwise stop lowest
309 * volume priority sound of same type
311 * 15 7/17/97 5:57p John
312 * made directsound3d config value work
314 * 14 7/17/97 5:43p John
315 * added new config stuff
317 * 13 7/17/97 4:25p John
318 * First, broken, stage of changing config stuff
320 * 12 7/15/97 12:13p Lawrance
321 * don't stop sounds that have highest priority
323 * 11 7/15/97 11:15a Lawrance
324 * limit the max instances of simultaneous sound effects, implement
325 * priorities to force critical sounds
327 * 10 6/09/97 11:50p Lawrance
328 * integrating DirectSound3D
330 * 9 6/08/97 5:59p Lawrance
331 * integrate DirectSound3D into sound system
333 * 8 6/04/97 1:19p Lawrance
334 * made hardware mixing robust
336 * 7 6/03/97 1:56p Hoffoss
337 * Return correct error code when direct sound init fails.
339 * 6 6/03/97 12:07p Lawrance
340 * don't enable 3D sounds in Primary buffer
342 * 5 6/02/97 3:45p Dan
343 * temp disable of hardware mixing until problem solved with
344 * CreateBuffer() failing
346 * 4 6/02/97 1:45p Lawrance
347 * implementing hardware mixing
349 * 3 5/29/97 4:01p Lawrance
350 * let snd_init() have final say on initialization
352 * 2 5/29/97 12:04p Lawrance
353 * creation of file to hold DirectSound specific portions
372 #include <initguid.h>
377 #include <SDL/SDL_audio.h>
381 // Pointers to functions contained in DSOUND.dll
382 HRESULT (__stdcall *pfn_DirectSoundCreate)(LPGUID lpGuid, LPDIRECTSOUND *ppDS, IUnknown FAR *pUnkOuter) = NULL;
383 HRESULT (__stdcall *pfn_DirectSoundCaptureCreate)(LPGUID lpGUID, LPDIRECTSOUNDCAPTURE *lplpDSC, LPUNKNOWN pUnkOuter) = NULL;
385 HINSTANCE Ds_dll_handle=NULL;
387 LPDIRECTSOUND pDirectSound = NULL;
388 LPDIRECTSOUNDBUFFER pPrimaryBuffer = NULL;
389 LPIA3D2 pIA3d2 = NULL;
391 static LPKSPROPERTYSET pPropertySet; // pointer to sound card property set
392 static LPDIRECTSOUNDBUFFER Ds_property_set_pdsb = NULL;
393 static LPDIRECTSOUND3DBUFFER Ds_property_set_pds3db = NULL;
395 static int Ds_must_call_couninitialize = 0;
397 channel* Channels; //[MAX_CHANNELS];
398 static int channel_next_sig = 1;
400 #define MAX_DS_SOFTWARE_BUFFERS 256
401 typedef struct ds_sound_buffer
403 LPDIRECTSOUNDBUFFER pdsb;
409 ds_sound_buffer ds_software_buffers[MAX_DS_SOFTWARE_BUFFERS];
411 #define MAX_DS_HARDWARE_BUFFERS 32
412 ds_sound_buffer ds_hardware_buffers[MAX_DS_HARDWARE_BUFFERS];
414 static DSCAPS Soundcard_caps; // current soundcard capabilities
416 extern int Snd_sram; // mem (in bytes) used up by storing sounds in system memory
417 extern int Snd_hram; // mem (in bytes) used up by storing sounds in soundcard memory
419 static int Ds_use_ds3d = 0;
420 static int Ds_use_a3d = 0;
421 static int Ds_use_eax = 0;
423 GUID IID_IA3d2_Def = {0xfb80d1e0, 0x98d3, 0x11d1, {0x90, 0xfb, 0x00, 0x60, 0x08, 0xa1, 0xf4, 0x41}};
424 GUID CLSID_A3d_Def = {0xd8f1eee0, 0xf634, 0x11cf, {0x87, 0x0, 0x0, 0xa0, 0x24, 0x5d, 0x91, 0x8b}};
426 static bool Stop_logging_sounds = false;
429 ///////////////////////////
433 ///////////////////////////
436 //#define EAX_PRESET_GENERIC EAX_ENVIRONMENT_GENERIC,0.5F,1.493F,0.5F
437 #define EAX_PRESET_GENERIC EAX_ENVIRONMENT_GENERIC,0.2F,0.2F,1.0F
438 #define EAX_PRESET_PADDEDCELL EAX_ENVIRONMENT_PADDEDCELL,0.25F,0.1F,0.0F
439 #define EAX_PRESET_ROOM EAX_ENVIRONMENT_ROOM,0.417F,0.4F,0.666F
440 #define EAX_PRESET_BATHROOM EAX_ENVIRONMENT_BATHROOM,0.653F,1.499F,0.166F
441 #define EAX_PRESET_LIVINGROOM EAX_ENVIRONMENT_LIVINGROOM,0.208F,0.478F,0.0F
442 #define EAX_PRESET_STONEROOM EAX_ENVIRONMENT_STONEROOM,0.5F,2.309F,0.888F
443 #define EAX_PRESET_AUDITORIUM EAX_ENVIRONMENT_AUDITORIUM,0.403F,4.279F,0.5F
444 #define EAX_PRESET_CONCERTHALL EAX_ENVIRONMENT_CONCERTHALL,0.5F,3.961F,0.5F
445 #define EAX_PRESET_CAVE EAX_ENVIRONMENT_CAVE,0.5F,2.886F,1.304F
446 #define EAX_PRESET_ARENA EAX_ENVIRONMENT_ARENA,0.361F,7.284F,0.332F
447 #define EAX_PRESET_HANGAR EAX_ENVIRONMENT_HANGAR,0.5F,10.0F,0.3F
448 #define EAX_PRESET_CARPETEDHALLWAY EAX_ENVIRONMENT_CARPETEDHALLWAY,0.153F,0.259F,2.0F
449 #define EAX_PRESET_HALLWAY EAX_ENVIRONMENT_HALLWAY,0.361F,1.493F,0.0F
450 #define EAX_PRESET_STONECORRIDOR EAX_ENVIRONMENT_STONECORRIDOR,0.444F,2.697F,0.638F
451 #define EAX_PRESET_ALLEY EAX_ENVIRONMENT_ALLEY,0.25F,1.752F,0.776F
452 #define EAX_PRESET_FOREST EAX_ENVIRONMENT_FOREST,0.111F,3.145F,0.472F
453 #define EAX_PRESET_CITY EAX_ENVIRONMENT_CITY,0.111F,2.767F,0.224F
454 #define EAX_PRESET_MOUNTAINS EAX_ENVIRONMENT_MOUNTAINS,0.194F,7.841F,0.472F
455 #define EAX_PRESET_QUARRY EAX_ENVIRONMENT_QUARRY,1.0F,1.499F,0.5F
456 #define EAX_PRESET_PLAIN EAX_ENVIRONMENT_PLAIN,0.097F,2.767F,0.224F
457 #define EAX_PRESET_PARKINGLOT EAX_ENVIRONMENT_PARKINGLOT,0.208F,1.652F,1.5F
458 #define EAX_PRESET_SEWERPIPE EAX_ENVIRONMENT_SEWERPIPE,0.652F,2.886F,0.25F
459 #define EAX_PRESET_UNDERWATER EAX_ENVIRONMENT_UNDERWATER,1.0F,1.499F,0.0F
460 #define EAX_PRESET_DRUGGED EAX_ENVIRONMENT_DRUGGED,0.875F,8.392F,1.388F
461 #define EAX_PRESET_DIZZY EAX_ENVIRONMENT_DIZZY,0.139F,17.234F,0.666F
462 #define EAX_PRESET_PSYCHOTIC EAX_ENVIRONMENT_PSYCHOTIC,0.486F,7.563F,0.806F
464 static LPKSPROPERTYSET Ds_eax_reverb = NULL;
466 static int Ds_eax_inited = 0;
468 EAX_REVERBPROPERTIES Ds_eax_presets[] =
470 {EAX_PRESET_GENERIC},
471 {EAX_PRESET_PADDEDCELL},
473 {EAX_PRESET_BATHROOM},
474 {EAX_PRESET_LIVINGROOM},
475 {EAX_PRESET_STONEROOM},
476 {EAX_PRESET_AUDITORIUM},
477 {EAX_PRESET_CONCERTHALL},
481 {EAX_PRESET_CARPETEDHALLWAY},
482 {EAX_PRESET_HALLWAY},
483 {EAX_PRESET_STONECORRIDOR},
487 {EAX_PRESET_MOUNTAINS},
490 {EAX_PRESET_PARKINGLOT},
491 {EAX_PRESET_SEWERPIPE},
492 {EAX_PRESET_UNDERWATER},
493 {EAX_PRESET_DRUGGED},
495 {EAX_PRESET_PSYCHOTIC},
498 GUID DSPROPSETID_EAX_ReverbProperties_Def = {0x4a4e6fc1, 0xc341, 0x11d1, {0xb7, 0x3a, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}};
499 GUID DSPROPSETID_EAXBUFFER_ReverbProperties_Def = {0x4a4e6fc0, 0xc341, 0x11d1, {0xb7, 0x3a, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}};
501 //----------------------------------------------------------------
503 void ds_get_soundcard_caps(DSCAPS *dscaps);
506 typedef struct channel
508 int sig; // uniquely identifies the sound playing on the channel
509 int snd_id; // identifies which kind of sound is playing
510 ALuint source_id; // OpenAL source id
511 int buf_id; // currently bound buffer index (-1 if none)
512 int looping; // flag to indicate that the sound is looping
514 int priority; // implementation dependant priority
519 typedef struct sound_buffer
521 ALuint buf_id; // OpenAL buffer id
522 int source_id; // source index this buffer is currently bound to
531 #define MAX_DS_SOFTWARE_BUFFERS 256
533 static int MAX_CHANNELS = 1000; // initialized properly in ds_init_channels()
535 static int channel_next_sig = 1;
537 sound_buffer sound_buffers[MAX_DS_SOFTWARE_BUFFERS];
539 static int Ds_use_ds3d = 0;
540 static int Ds_use_a3d = 0;
541 static int Ds_use_eax = 0;
543 ALCdevice *ds_sound_device;
544 void *ds_sound_context = (void *)0;
547 #define OpenAL_ErrorCheck() do { \
548 int i = alGetError(); \
549 if (i != AL_NO_ERROR) { \
550 while(i != AL_NO_ERROR) { \
551 nprintf(("Warning", "%s/%s:%d - OpenAL error %s\n", __FUNCTION__, __FILE__, __LINE__, alGetString(i))); \
558 #define OpenAL_ErrorCheck()
563 int ds_vol_lookup[101]; // lookup table for direct sound volumes
564 int ds_initialized = FALSE;
567 //--------------------------------------------------------------------------
570 // Determine if a secondary buffer is a 3d secondary buffer.
573 int ds_is_3d_buffer(LPDIRECTSOUNDBUFFER pdsb)
578 dsbc.dwSize = sizeof(dsbc);
579 hr = pdsb->GetCaps(&dsbc);
580 if ( hr == DS_OK && dsbc.dwFlags & DSBCAPS_CTRL3D ) {
589 //--------------------------------------------------------------------------
592 // Determine if a secondary buffer is a 3d secondary buffer.
594 int ds_is_3d_buffer(int sid)
598 return ds_is_3d_buffer(ds_software_buffers[sid].pdsb);
605 //--------------------------------------------------------------------------
606 // ds_build_vol_lookup()
608 // Fills up the ds_vol_lookup[] tables that converts from a volume in the form
609 // 0.0 -> 1.0 to -10000 -> 0 (this is the DirectSound method, where units are
610 // hundredths of decibls)
612 void ds_build_vol_lookup()
617 ds_vol_lookup[0] = -10000;
618 for ( i = 1; i <= 100; i++ ) {
620 ds_vol_lookup[i] = fl2i( (log(vol) / log(2.0f)) * 1000.0f);
625 //--------------------------------------------------------------------------
626 // ds_convert_volume()
628 // Takes volume between 0.0f and 1.0f and converts into
629 // DirectSound style volumes between -10000 and 0.
630 int ds_convert_volume(float volume)
634 index = fl2i(volume * 100.0f);
640 return ds_vol_lookup[index];
643 //--------------------------------------------------------------------------
644 // ds_get_percentage_vol()
646 // Converts -10000 -> 0 range volume to 0 -> 1
647 float ds_get_percentage_vol(int ds_vol)
650 vol = pow(2.0, ds_vol/1000.0);
654 // ---------------------------------------------------------------------------------------
657 // Parse a wave file.
659 // parameters: filename => file of sound to parse
660 // dest => address of pointer of where to store raw sound data (output parm)
661 // dest_size => number of bytes of sound data stored (output parm)
662 // header => address of pointer to a WAVEFORMATEX struct (output parm)
664 // returns: 0 => wave file successfully parsed
667 // NOTE: memory is malloced for the header and dest in this function. It is the responsibility
668 // of the caller to free this memory later.
670 int ds_parse_wave(char *filename, ubyte **dest, uint *dest_size, WAVEFORMATEX **header)
673 PCMWAVEFORMAT PCM_header;
675 unsigned int tag, size, next_chunk;
677 fp = cfopen( filename, "rb" );
679 nprintf(("Error", "Couldn't open '%s'\n", filename ));
683 // Skip the "RIFF" tag and file size (8 bytes)
684 // Skip the "WAVE" tag (4 bytes)
685 cfseek( fp, 12, CF_SEEK_SET );
687 // Now read RIFF tags until the end of file
690 if ( cfread( &tag, sizeof(uint), 1, fp ) != 1 )
693 if ( cfread( &size, sizeof(uint), 1, fp ) != 1 )
696 next_chunk = cftell(fp) + size;
699 case 0x20746d66: // The 'fmt ' tag
700 //nprintf(("Sound", "SOUND => size of fmt block: %d\n", size));
701 cfread( &PCM_header, sizeof(PCMWAVEFORMAT), 1, fp );
702 if ( PCM_header.wf.wFormatTag != WAVE_FORMAT_PCM ) {
703 cbExtra = cfread_short(fp);
706 // Allocate memory for WAVEFORMATEX structure + extra bytes
707 if ( (*header = (WAVEFORMATEX *) malloc ( sizeof(WAVEFORMATEX)+cbExtra )) != NULL ){
708 // Copy bytes from temporary format structure
709 memcpy (*header, &PCM_header, sizeof(PCM_header));
710 (*header)->cbSize = (unsigned short)cbExtra;
712 // Read those extra bytes, append to WAVEFORMATEX structure
714 cfread( ((ubyte *)(*header) + sizeof(WAVEFORMATEX)), cbExtra, 1, fp);
718 Assert(0); // malloc failed
722 case 0x61746164: // the 'data' tag
724 (*dest) = (ubyte *)malloc(size);
725 Assert( *dest != NULL );
726 cfread( *dest, size, 1, fp );
728 default: // unknown, skip it
731 cfseek( fp, next_chunk, CF_SEEK_SET );
738 // ---------------------------------------------------------------------------------------
747 for ( i = 0; i < MAX_DS_SOFTWARE_BUFFERS; i++ ) {
748 if ( sound_buffers[i].buf_id == 0 )
752 if ( i == MAX_DS_SOFTWARE_BUFFERS ) {
760 for ( i = 0; i < MAX_DS_SOFTWARE_BUFFERS; i++ ) {
761 if ( ds_software_buffers[i].pdsb == NULL )
765 if ( i == MAX_DS_SOFTWARE_BUFFERS ) {
773 // ---------------------------------------------------------------------------------------
784 for ( i = 0; i < MAX_DS_HARDWARE_BUFFERS; i++ ) {
785 if ( ds_hardware_buffers[i].pdsb == NULL )
789 if ( i == MAX_DS_HARDWARE_BUFFERS ) {
797 // ---------------------------------------------------------------------------------------
798 // Load a DirectSound secondary buffer with sound data. The sounds data for
799 // game sounds are stored in the DirectSound secondary buffers, and are
800 // duplicated as needed and placed in the Channels[] array to be played.
804 // sid => pointer to software id for sound ( output parm)
805 // hid => pointer to hardware id for sound ( output parm)
806 // final_size => pointer to storage to receive uncompressed sound size (output parm)
807 // header => pointer to a WAVEFORMATEX structure
808 // si => sound_info structure, contains details on the sound format
809 // flags => buffer properties ( DS_HARDWARE , DS_3D )
811 // returns: -1 => sound effect could not loaded into a secondary buffer
812 // 0 => sound effect successfully loaded into a secondary buffer
815 // NOTE: this function is slow, especially when sounds are loaded into hardware. Don't call this
816 // function from within gameplay.
819 int ds_load_buffer(int *sid, int *hid, int *final_size, void *header, sound_info *si, int flags)
822 Assert( final_size != NULL );
823 Assert( header != NULL );
824 Assert( si != NULL );
825 Assert( si->data != NULL );
827 // All sounds are required to have a software buffer
831 nprintf(("Sound","SOUND ==> No more sound buffers available\n"));
836 alGenBuffers (1, &pi);
845 switch (si->format) {
846 case WAVE_FORMAT_PCM:
855 /* format is now in pcm */
856 frequency = si->sample_rate;
858 if (si->bits == 16) {
859 if (si->n_channels == 2) {
860 format = AL_FORMAT_STEREO16;
861 } else if (si->n_channels == 1) {
862 format = AL_FORMAT_MONO16;
866 } else if (si->bits == 8) {
867 if (si->n_channels == 2) {
868 format = AL_FORMAT_STEREO8;
869 } else if (si->n_channels == 1) {
870 format = AL_FORMAT_MONO8;
880 alBufferData (pi, format, data, size, frequency);
882 sound_buffers[*sid].buf_id = pi;
883 sound_buffers[*sid].source_id = -1;
884 sound_buffers[*sid].frequency = frequency;
885 sound_buffers[*sid].bits_per_sample = si->bits;
886 sound_buffers[*sid].nchannels = si->n_channels;
887 sound_buffers[*sid].nseconds = si->size / si->avg_bytes_per_sec;
888 sound_buffers[*sid].nbytes = si->size;
895 Assert( final_size != NULL );
896 Assert( header != NULL );
897 Assert( si != NULL );
898 Assert( si->data != NULL );
899 Assert( si->size > 0 );
900 Assert( si->sample_rate > 0);
901 Assert( si->bits > 0 );
902 Assert( si->n_channels > 0 );
903 Assert( si->n_block_align >= 0 );
904 Assert( si->avg_bytes_per_sec > 0 );
906 WAVEFORMATEX *pwfx = (WAVEFORMATEX *)header;
907 DSBUFFERDESC BufferDesc;
908 WAVEFORMATEX WaveFormat;
910 int rc, final_sound_size, DSOUND_load_buffer_result = 0;
911 BYTE *pData, *pData2;
912 DWORD DataSize, DataSize2;
914 // the below two covnert_ variables are only used when the wav format is not
915 // PCM. DirectSound only takes PCM sound data, so we must convert to PCM if required
916 ubyte *convert_buffer = NULL; // storage for converted wav file
917 int convert_len; // num bytes of converted wav file
918 uint src_bytes_used; // number of source bytes actually converted (should always be equal to original size)
920 // Ensure DirectSound initialized
921 if (!ds_initialized) {
922 DSOUND_load_buffer_result = -1;
923 goto DSOUND_load_buffer_done;
926 // Set up buffer information
927 WaveFormat.wFormatTag = (unsigned short)si->format;
928 WaveFormat.nChannels = (unsigned short)si->n_channels;
929 WaveFormat.nSamplesPerSec = si->sample_rate;
930 WaveFormat.wBitsPerSample = (unsigned short)si->bits;
931 WaveFormat.cbSize = 0;
932 WaveFormat.nBlockAlign = (unsigned short)si->n_block_align;
933 WaveFormat.nAvgBytesPerSec = si->avg_bytes_per_sec;
935 final_sound_size = si->size; // assume this format will be used, may be over-ridded by convert_len
937 // Assert(WaveFormat.nChannels == 1);
939 switch ( si->format ) {
940 case WAVE_FORMAT_PCM:
943 case WAVE_FORMAT_ADPCM:
945 nprintf(( "Sound", "SOUND ==> converting sound from ADPCM to PCM\n" ));
946 rc = ACM_convert_ADPCM_to_PCM(pwfx, si->data, si->size, &convert_buffer, 0, &convert_len, &src_bytes_used, 8);
948 DSOUND_load_buffer_result = -1;
949 goto DSOUND_load_buffer_done;
952 if (src_bytes_used != si->size) {
953 Int3(); // ACM conversion failed?
954 DSOUND_load_buffer_result = -1;
955 goto DSOUND_load_buffer_done;
958 final_sound_size = convert_len;
960 // Set up the WAVEFORMATEX structure to have the right PCM characteristics
961 WaveFormat.wFormatTag = WAVE_FORMAT_PCM;
962 WaveFormat.nChannels = (unsigned short)si->n_channels;
963 WaveFormat.nSamplesPerSec = si->sample_rate;
964 WaveFormat.wBitsPerSample = 8;
965 WaveFormat.cbSize = 0;
966 WaveFormat.nBlockAlign = (unsigned short)(( WaveFormat.nChannels * WaveFormat.wBitsPerSample ) / 8);
967 WaveFormat.nAvgBytesPerSec = WaveFormat.nBlockAlign * WaveFormat.nSamplesPerSec;
969 nprintf(( "Sound", "SOUND ==> Coverted sound from ADPCM to PCM successfully\n" ));
973 nprintf(( "Sound", "Unsupported sound encoding\n" ));
974 DSOUND_load_buffer_result = -1;
975 goto DSOUND_load_buffer_done;
979 WaveFormat.wFormatTag = WAVE_FORMAT_PCM; // DirectSound only used PCM wave files
981 // Set up a DirectSound buffer
982 ZeroMemory(&BufferDesc, sizeof(BufferDesc));
983 BufferDesc.dwSize = sizeof(BufferDesc);
984 BufferDesc.dwBufferBytes = final_sound_size;
985 BufferDesc.lpwfxFormat = &WaveFormat;
987 // check if DirectSound3D is enabled and the sound is flagged for 3D
988 if ((ds_using_ds3d()) && (flags & DS_USE_DS3D)) {
989 // if (ds_using_ds3d()) {
990 BufferDesc.dwFlags = DSBCAPS_STATIC | DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_CTRLVOLUME | DSBCAPS_CTRL3D | DSBCAPS_MUTE3DATMAXDISTANCE;
992 BufferDesc.dwFlags = DSBCAPS_STATIC | DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_CTRLVOLUME | DSBCAPS_LOCSOFTWARE;
995 // Create a new software buffer using the settings for this wave
996 // All sounds are required to have a software buffer
999 nprintf(("Sound","SOUND ==> No more software secondary buffers available\n"));
1002 DSReturn = pDirectSound->CreateSoundBuffer(&BufferDesc, &ds_software_buffers[*sid].pdsb, NULL );
1004 if ( DSReturn == DS_OK && ds_software_buffers[*sid].pdsb != NULL ) {
1006 ds_software_buffers[*sid].desc = BufferDesc;
1007 ds_software_buffers[*sid].wfx = *BufferDesc.lpwfxFormat;
1009 // Lock the buffer and copy in the data
1010 if ((ds_software_buffers[*sid].pdsb)->Lock(0, final_sound_size, (void**)(&pData), &DataSize, (void**)(&pData2), &DataSize2, 0) == DS_OK) {
1012 if ( convert_buffer )
1013 memcpy(pData, convert_buffer, final_sound_size); // use converted data (PCM format)
1015 memcpy(pData, si->data, final_sound_size);
1017 (ds_software_buffers[*sid].pdsb)->Unlock(pData, DataSize, 0, 0);
1019 DSOUND_load_buffer_result = 0;
1021 // update ram used for sound
1022 Snd_sram += final_sound_size;
1023 *final_size = final_sound_size;
1026 nprintf(("Sound","SOUND => fatal error in DSOUND_load_buffer\n"));
1028 DSOUND_load_buffer_result = -1;
1031 DSOUND_load_buffer_done:
1032 if ( convert_buffer )
1033 free( convert_buffer );
1034 return DSOUND_load_buffer_result;
1038 // ---------------------------------------------------------------------------------------
1039 // ds_init_channels()
1041 // init the Channels[] array
1043 void ds_init_channels()
1050 Channels = (channel*) malloc(sizeof(channel) * MAX_CHANNELS);
1051 if (Channels == NULL) {
1052 Error(LOCATION, "Unable to allocate %d bytes for %d audio channels.", sizeof(channel) * MAX_CHANNELS, MAX_CHANNELS);
1055 // init the channels
1056 for ( i = 0; i < MAX_CHANNELS; i++ ) {
1057 alGenSources(1, &Channels[i].source_id);
1058 Channels[i].buf_id = -1;
1059 Channels[i].vol = 0;
1064 // detect how many channels we can support
1066 ds_get_soundcard_caps(&caps);
1068 // caps.dwSize = sizeof(DSCAPS);
1069 // pDirectSound->GetCaps(&caps);
1071 // minimum 16 channels
1072 MAX_CHANNELS = caps.dwMaxHwMixingStaticBuffers;
1073 int dbg_channels = MAX_CHANNELS;
1074 if (MAX_CHANNELS < 16) {
1078 // allocate the channels array
1079 Channels = (channel*) malloc(sizeof(channel) * MAX_CHANNELS);
1080 if (Channels == NULL) {
1081 Error(LOCATION, "Unable to allocate %d bytes for %d audio channels.", sizeof(channel) * MAX_CHANNELS, MAX_CHANNELS);
1084 // init the channels
1085 for ( i = 0; i < MAX_CHANNELS; i++ ) {
1086 Channels[i].pdsb = NULL;
1087 Channels[i].pds3db = NULL;
1088 Channels[i].vol = 0;
1091 mprintf(("** MAX_CHANNELS set to %d. DS reported %d.\n", MAX_CHANNELS, dbg_channels));
1095 // ---------------------------------------------------------------------------------------
1096 // ds_init_software_buffers()
1098 // init the software buffers
1100 void ds_init_software_buffers()
1105 for ( i = 0; i < MAX_DS_SOFTWARE_BUFFERS; i++ ) {
1106 sound_buffers[i].buf_id = 0;
1107 sound_buffers[i].source_id = -1;
1112 for ( i = 0; i < MAX_DS_SOFTWARE_BUFFERS; i++ ) {
1113 ds_software_buffers[i].pdsb = NULL;
1118 // ---------------------------------------------------------------------------------------
1119 // ds_init_hardware_buffers()
1121 // init the hardware buffers
1123 void ds_init_hardware_buffers()
1126 // STUB_FUNCTION; // not needed with openal (CM)
1131 for ( i = 0; i < MAX_DS_HARDWARE_BUFFERS; i++ ) {
1132 ds_hardware_buffers[i].pdsb = NULL;
1137 // ---------------------------------------------------------------------------------------
1138 // ds_init_buffers()
1140 // init the both the software and hardware buffers
1142 void ds_init_buffers()
1144 ds_init_software_buffers();
1145 ds_init_hardware_buffers();
1148 // Get the current soundcard capabilities
1150 void ds_get_soundcard_caps(DSCAPS *dscaps)
1153 int n_hbuffers, hram;
1155 dscaps->dwSize = sizeof(DSCAPS);
1157 hr = pDirectSound->GetCaps(dscaps);
1159 nprintf(("Sound","SOUND ==> DirectSound GetCaps() failed with code %s\n.",get_DSERR_text(hr) ));
1163 n_hbuffers = dscaps->dwMaxHwMixingStaticBuffers;
1164 hram = dscaps->dwTotalHwMemBytes;
1166 if ( !(dscaps->dwFlags & DSCAPS_CERTIFIED) ) {
1167 nprintf(("Sound","SOUND ==> Warning: audio driver is not Microsoft certified.\n"));
1171 // ---------------------------------------------------------------------------------------
1174 // init the both the software and hardware buffers
1176 void ds_show_caps(DSCAPS *dscaps)
1178 nprintf(("Sound", "SOUND => Soundcard Capabilities:\n"));
1179 nprintf(("Sound", "================================\n"));
1180 nprintf(("Sound", "Number of primary buffers: %d\n", dscaps->dwPrimaryBuffers ));
1181 nprintf(("Sound", "Number of total hw mixing buffers: %d\n", dscaps->dwMaxHwMixingAllBuffers ));
1182 nprintf(("Sound", "Number of total hw mixing static buffers: %d\n", dscaps->dwMaxHwMixingStaticBuffers ));
1183 nprintf(("Sound", "Number of total hw mixing streaming buffers: %d\n", dscaps->dwMaxHwMixingStreamingBuffers ));
1184 nprintf(("Sound", "Number of free hw mixing buffers: %d\n", dscaps->dwFreeHwMixingAllBuffers ));
1185 nprintf(("Sound", "Number of free hw mixing static buffers: %d\n", dscaps->dwFreeHwMixingStaticBuffers ));
1186 nprintf(("Sound", "Number of free hw mixing streaming buffers: %d\n", dscaps->dwFreeHwMixingStreamingBuffers ));
1187 nprintf(("Sound", "Number of hw 3D buffers: %d\n", dscaps->dwMaxHw3DAllBuffers ));
1188 nprintf(("Sound", "Number of hw 3D static buffers: %d\n", dscaps->dwMaxHw3DStaticBuffers ));
1189 nprintf(("Sound", "Number of hw 3D streaming buffers: %d\n", dscaps->dwMaxHw3DStreamingBuffers ));
1190 nprintf(("Sound", "Number of free hw 3D buffers: %d\n", dscaps->dwFreeHw3DAllBuffers ));
1191 nprintf(("Sound", "Number of free hw static 3D buffers: %d\n", dscaps->dwFreeHw3DStaticBuffers ));
1192 nprintf(("Sound", "Number of free hw streaming 3D buffers: %d\n", dscaps->dwFreeHw3DStreamingBuffers ));
1193 nprintf(("Sound", "Number of total hw bytes: %d\n", dscaps->dwTotalHwMemBytes ));
1194 nprintf(("Sound", "Number of free hw bytes: %d\n", dscaps->dwFreeHwMemBytes ));
1195 nprintf(("Sound", "================================\n"));
1200 // Fill in the waveformat struct with the primary buffer characteristics.
1201 void ds_get_primary_format(WAVEFORMATEX *wfx)
1203 // Set 16 bit / 22KHz / mono
1204 wfx->wFormatTag = WAVE_FORMAT_PCM;
1206 wfx->nSamplesPerSec = 22050;
1207 wfx->wBitsPerSample = 16;
1209 wfx->nBlockAlign = (unsigned short)(wfx->nChannels * (wfx->wBitsPerSample / 8));
1210 wfx->nAvgBytesPerSec = wfx->nBlockAlign * wfx->nSamplesPerSec;
1214 // obtain the function pointers from the dsound.dll
1215 void ds_dll_get_functions()
1217 pfn_DirectSoundCreate = (HRESULT(__stdcall *)(LPGUID lpGuid, LPDIRECTSOUND *ppDS, IUnknown FAR *pUnkOuter))GetProcAddress(Ds_dll_handle,"DirectSoundCreate");
1218 pfn_DirectSoundCaptureCreate = (HRESULT(__stdcall *)(LPGUID lpGuid, LPDIRECTSOUNDCAPTURE *lplpDSC, IUnknown FAR *pUnkOuter))GetProcAddress(Ds_dll_handle,"DirectSoundCaptureCreate");
1222 // Load the dsound.dll, and get funtion pointers
1223 // exit: 0 -> dll loaded successfully
1224 // !0 -> dll could not be loaded
1230 if ( !Ds_dll_loaded ) {
1231 Ds_dll_handle = LoadLibrary("dsound.dll");
1232 if ( !Ds_dll_handle ) {
1235 ds_dll_get_functions();
1248 HINSTANCE a3d_handle;
1251 a3d_handle = LoadLibrary("a3d.dll");
1255 FreeLibrary(a3d_handle);
1259 Ds_must_call_couninitialize = 1;
1261 hr = CoCreateInstance(CLSID_A3d_Def, NULL, CLSCTX_INPROC_SERVER, IID_IDirectSound, (void**)&pDirectSound);
1266 Assert(pDirectSound != NULL);
1267 hr = pDirectSound->QueryInterface(IID_IA3d2_Def, (void**)&pIA3d2);
1272 A3DCAPS_SOFTWARE swCaps;
1274 // Get Dll Software CAP to get DLL version number
1275 ZeroMemory(&swCaps,sizeof(swCaps));
1277 swCaps.dwSize = sizeof(swCaps);
1278 pIA3d2->GetSoftwareCaps(&swCaps);
1280 // Compare version from a3d.dll to header version only return A3D_OK if dll version >= to header version
1281 if (swCaps.dwVersion < A3D_CURRENT_VERSION) {
1282 pDirectSound->Release();
1283 pDirectSound = NULL;
1288 // verify this is authentic A3D
1289 int aureal_verified;
1290 aureal_verified = VerifyAurealA3D();
1292 if (aureal_verified == FALSE) {
1293 // This is fake A3D!!! Ignore
1294 pDirectSound->Release();
1295 pDirectSound = NULL;
1299 // Register our version for backwards compatibility with newer A3d.dll
1300 pIA3d2->RegisterVersion(A3D_CURRENT_VERSION);
1302 hr = pDirectSound->Initialize(NULL);
1304 pDirectSound->Release();
1305 pDirectSound = NULL;
1309 pIA3d2->SetResourceManagerMode(A3D_RESOURCE_MODE_DYNAMIC_LOOPERS);
1315 // Initialize the property set interface.
1317 // returns: 0 if successful, otherwise -1. If successful, the global pPropertySet will
1318 // set to a non-NULL value.
1320 int ds_init_property_set()
1327 // Create the secondary buffer required for EAX initialization
1329 wf.wFormatTag = WAVE_FORMAT_PCM;
1331 wf.nSamplesPerSec = 22050;
1332 wf.wBitsPerSample = 16;
1334 wf.nBlockAlign = (unsigned short)(wf.nChannels * (wf.wBitsPerSample / 8));
1335 wf.nAvgBytesPerSec = wf.nBlockAlign * wf.nSamplesPerSec;
1338 ZeroMemory(&dsbd, sizeof(dsbd));
1339 dsbd.dwSize = sizeof(dsbd);
1340 dsbd.dwFlags = DSBCAPS_CTRLDEFAULT | DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_STATIC | DSBCAPS_CTRL3D | DSBCAPS_MUTE3DATMAXDISTANCE;
1341 dsbd.dwBufferBytes = 3 * wf.nAvgBytesPerSec;
1342 dsbd.lpwfxFormat = &wf;
1344 // Create a new buffer using the settings for this wave
1345 hr = pDirectSound->CreateSoundBuffer(&dsbd, &Ds_property_set_pdsb, NULL);
1347 pPropertySet = NULL;
1351 // Get the 3D interface from the secondary buffer, which is used to query the EAX interface
1352 hr = Ds_property_set_pdsb->QueryInterface(IID_IDirectSound3DBuffer, (void**)&Ds_property_set_pds3db);
1354 Ds_property_set_pds3db = NULL;
1358 Assert(Ds_property_set_pds3db != NULL);
1359 hr = Ds_property_set_pds3db->QueryInterface(IID_IKsPropertySet, (void**)&pPropertySet);
1360 if ((FAILED(hr)) || (pPropertySet == NULL)) {
1368 // ---------------------------------------------------------------------------------------
1371 // returns: -1 => init failed
1372 // 0 => init success
1373 int ds_init(int use_a3d, int use_eax)
1376 // NOTE: A3D and EAX are unused in OpenAL
1377 const ALubyte *initStr = (const ALubyte *)"\'( (sampling-rate 22050 ))";
1378 int attr[] = { ALC_FREQUENCY, 22050, ALC_SYNC, AL_FALSE, 0 };
1384 nprintf(( "Sound", "SOUND ==> Initializing OpenAL...\n" ));
1387 ds_sound_device = alcOpenDevice (initStr);
1389 // Create Sound Device
1390 ds_sound_context = alcCreateContext (ds_sound_device, attr);
1391 alcMakeContextCurrent (ds_sound_context);
1393 if (alcGetError(ds_sound_device) != ALC_NO_ERROR) {
1394 nprintf(("Sound", "SOUND ==> Couldn't initialize OpenAL\n"));
1398 OpenAL_ErrorCheck();
1400 // Initialize DirectSound3D. Since software performance of DirectSound3D is unacceptably
1401 // slow, we require the voice manger (a DirectSound extension) to be present. The
1402 // exception is when A3D is being used, since A3D has a resource manager built in.
1403 // if (Ds_use_ds3d && ds3d_init(0) != 0)
1406 ds_build_vol_lookup();
1412 WAVEFORMATEX wave_format;
1413 DSBUFFERDESC BufferDesc;
1415 nprintf(( "Sound", "SOUND ==> Initializing DirectSound...\n" ));
1417 hwnd = (HWND)os_get_window();
1418 if ( hwnd == NULL ) {
1419 nprintf(( "Sound", "SOUND ==> No window handle, so no sound...\n" ));
1423 if ( ds_dll_load() == -1 ) {
1427 pDirectSound = NULL;
1429 Ds_use_a3d = use_a3d;
1430 Ds_use_eax = use_eax;
1432 if (Ds_use_a3d || Ds_use_eax) {
1436 if (Ds_use_a3d && Ds_use_eax) {
1441 // If we want A3D, ensure a3d.dll exists
1442 if (Ds_use_a3d == 1) {
1443 if (ds_init_a3d() != 0) {
1450 if (Ds_use_a3d == 0) {
1451 if (!pfn_DirectSoundCreate) {
1452 nprintf(( "Sound", "SOUND ==> Could not get DirectSoundCreate function pointer\n" ));
1456 hr = pfn_DirectSoundCreate(NULL, &pDirectSound, NULL);
1462 // Set up DirectSound for exclusive mode, so we can change the primary buffer if we want to.
1463 hr = pDirectSound->SetCooperativeLevel(hwnd, DSSCL_EXCLUSIVE);
1465 nprintf(("Sound","SOUND ==> DirectSound pDirectSound->SetCooperativeLevel failed with code %s\n.",get_DSERR_text(hr) ));
1466 pDirectSound = NULL;
1470 // Create the primary buffer
1471 ZeroMemory(&BufferDesc, sizeof(BufferDesc));
1472 BufferDesc.dwSize = sizeof(BufferDesc);
1474 ds_get_soundcard_caps(&Soundcard_caps);
1477 BufferDesc.dwFlags = DSBCAPS_PRIMARYBUFFER | DSBCAPS_CTRL3D;
1479 hr = pDirectSound->CreateSoundBuffer(&BufferDesc, &pPrimaryBuffer, 0);
1481 nprintf(("Sound","SOUND ==> Primary Buffer create failed with DSBCAPS_CTRL3D property... disabling DirectSound3D\n"));
1486 nprintf(("Sound","SOUND ==> Primary Buffer created with DirectSound3D enabled\n"));
1490 // If not using DirectSound3D, then create a normal primary buffer
1491 if (Ds_use_ds3d == 0) {
1492 BufferDesc.dwFlags = DSBCAPS_PRIMARYBUFFER;
1493 hr = pDirectSound->CreateSoundBuffer(&BufferDesc, &pPrimaryBuffer, 0);
1495 nprintf(("Sound","SOUND ==> Primary Buffer create failed with error: %s\n",get_DSERR_text(hr) ));
1496 pDirectSound = NULL;
1500 nprintf(("Sound","SOUND ==> Primary Buffer created with without DirectSound3D enabled\n"));
1504 // Get the primary buffer format
1505 ds_get_primary_format(&wave_format);
1507 hr = pPrimaryBuffer->SetFormat(&wave_format);
1509 nprintf(("Sound","SOUND ==> pPrimaryBuffer->SetFormat() failed with code %s\n",get_DSERR_text(hr) ));
1512 pPrimaryBuffer->GetFormat(&wave_format, sizeof(wave_format), NULL);
1513 nprintf(("Sound","SOUND ==> Primary Buffer forced to: rate: %d Hz bits: %d n_channels: %d\n",
1514 wave_format.nSamplesPerSec, wave_format.wBitsPerSample, wave_format.nChannels));
1516 // start the primary buffer playing. This will reduce sound latency when playing a sound
1517 // if no other sounds are playing.
1518 hr = pPrimaryBuffer->Play(0, 0, DSBPLAY_LOOPING);
1520 nprintf(("Sound","SOUND ==> pPrimaryBuffer->Play() failed with code %s\n",get_DSERR_text(hr) ));
1523 // Initialize DirectSound3D. Since software performance of DirectSound3D is unacceptably
1524 // slow, we require the voice manger (a DirectSound extension) to be present. The
1525 // exception is when A3D is being used, since A3D has a resource manager built in.
1527 int vm_required = 1; // voice manager
1528 if (Ds_use_a3d == 1) {
1532 if (ds3d_init(vm_required) != 0) {
1538 if (Ds_use_eax == 1) {
1539 ds_init_property_set();
1540 if (ds_eax_init() != 0) {
1545 ds_build_vol_lookup();
1549 ds_show_caps(&Soundcard_caps);
1555 // ---------------------------------------------------------------------------------------
1558 // returns the text equivalent for the a DirectSound DSERR_ code
1560 char *get_DSERR_text(int DSResult)
1565 static char buf[20];
1566 snprintf(buf, 19, "unknown %d", DSResult);
1569 switch( DSResult ) {
1575 case DSERR_ALLOCATED:
1576 return "DSERR_ALLOCATED";
1579 case DSERR_ALREADYINITIALIZED:
1580 return "DSERR_ALREADYINITIALIZED";
1583 case DSERR_BADFORMAT:
1584 return "DSERR_BADFORMAT";
1587 case DSERR_BUFFERLOST:
1588 return "DSERR_BUFFERLOST";
1591 case DSERR_CONTROLUNAVAIL:
1592 return "DSERR_CONTROLUNAVAIL";
1596 return "DSERR_GENERIC";
1599 case DSERR_INVALIDCALL:
1600 return "DSERR_INVALIDCALL";
1603 case DSERR_INVALIDPARAM:
1604 return "DSERR_INVALIDPARAM";
1607 case DSERR_NOAGGREGATION:
1608 return "DSERR_NOAGGREGATION";
1611 case DSERR_NODRIVER:
1612 return "DSERR_NODRIVER";
1615 case DSERR_OUTOFMEMORY:
1616 return "DSERR_OUTOFMEMORY";
1619 case DSERR_OTHERAPPHASPRIO:
1620 return "DSERR_OTHERAPPHASPRIO";
1623 case DSERR_PRIOLEVELNEEDED:
1624 return "DSERR_PRIOLEVELNEEDED";
1627 case DSERR_UNINITIALIZED:
1628 return "DSERR_UNINITIALIZED";
1631 case DSERR_UNSUPPORTED:
1632 return "DSERR_UNSUPPORTED";
1643 // ---------------------------------------------------------------------------------------
1644 // ds_close_channel()
1646 // Free a single channel
1648 void ds_close_channel(int i)
1651 if(Channels[i].source_id != 0 && alIsSource (Channels[i].source_id)) {
1652 alSourceStop (Channels[i].source_id);
1653 alDeleteSources(1, &Channels[i].source_id);
1655 Channels[i].source_id = 0;
1662 // If a 3D interface exists, free it
1663 if ( Channels[i].pds3db != NULL ) {
1666 Channels[i].pds3db = NULL;
1669 while(++attempts < 10) {
1670 hr = Channels[i].pds3db->Release();
1671 if ( hr == DS_OK ) {
1674 // nprintf(("Sound", "SOUND ==> Channels[channel].pds3db->Release() failed with return value %s\n", get_DSERR_text(second_hr) ));
1678 Channels[i].pds3db = NULL;
1682 if ( Channels[i].pdsb != NULL ) {
1683 // If a 2D interface exists, free it
1684 if ( Channels[i].pdsb != NULL ) {
1686 while(++attempts < 10) {
1687 hr = Channels[i].pdsb->Release();
1688 if ( hr == DS_OK ) {
1691 nprintf(("Sound", "SOUND ==> Channels[channel].pdsb->Release() failed with return value %s\n", get_DSERR_text(hr) ));
1696 Channels[i].pdsb = NULL;
1703 // ---------------------------------------------------------------------------------------
1704 // ds_close_all_channels()
1706 // Free all the channel buffers
1708 void ds_close_all_channels()
1712 for (i = 0; i < MAX_CHANNELS; i++) {
1713 ds_close_channel(i);
1717 // ---------------------------------------------------------------------------------------
1718 // ds_unload_buffer()
1721 void ds_unload_buffer(int sid, int hid)
1725 ALuint buf_id = sound_buffers[sid].buf_id;
1727 if (buf_id != 0 && alIsBuffer(buf_id)) {
1728 alDeleteBuffers(1, &buf_id);
1731 sound_buffers[sid].buf_id = 0;
1741 if ( ds_software_buffers[sid].pdsb != NULL ) {
1742 hr = ds_software_buffers[sid].pdsb->Release();
1743 if ( hr != DS_OK ) {
1745 nprintf(("Sound", "SOUND ==> ds_software_buffers[sid]->Release() failed with return value %s\n", get_DSERR_text(hr) ));
1747 ds_software_buffers[sid].pdsb = NULL;
1752 if ( ds_hardware_buffers[hid].pdsb != NULL ) {
1753 hr = ds_hardware_buffers[hid].pdsb->Release();
1754 if ( hr != DS_OK ) {
1756 nprintf(("Sound", "SOUND ==> ds_hardware_buffers[hid]->Release() failed with return value %s\n", get_DSERR_text(hr) ));
1758 ds_hardware_buffers[hid].pdsb = NULL;
1764 // ---------------------------------------------------------------------------------------
1765 // ds_close_software_buffers()
1768 void ds_close_software_buffers()
1773 for (i = 0; i < MAX_DS_SOFTWARE_BUFFERS; i++) {
1774 ALuint buf_id = sound_buffers[i].buf_id;
1776 if (buf_id != 0 && alIsBuffer(buf_id)) {
1777 alDeleteBuffers(1, &buf_id);
1780 sound_buffers[i].buf_id = 0;
1786 for (i = 0; i < MAX_DS_SOFTWARE_BUFFERS; i++) {
1787 if ( ds_software_buffers[i].pdsb != NULL ) {
1788 hr = ds_software_buffers[i].pdsb->Release();
1789 if ( hr != DS_OK ) {
1791 nprintf(("Sound", "SOUND ==> ds_software_buffers[i]->Release() failed with return value %s\n", get_DSERR_text(hr) ));
1793 ds_software_buffers[i].pdsb = NULL;
1799 // ---------------------------------------------------------------------------------------
1800 // ds_close_hardware_buffers()
1803 void ds_close_hardware_buffers()
1811 for (i = 0; i < MAX_DS_HARDWARE_BUFFERS; i++) {
1812 if ( ds_hardware_buffers[i].pdsb != NULL ) {
1813 hr = ds_hardware_buffers[i].pdsb->Release();
1814 if ( hr != DS_OK ) {
1816 nprintf(("Sound", "SOUND ==> ds_hardware_buffers[i]->Release() failed with return value %s\n", get_DSERR_text(hr) ));
1818 ds_hardware_buffers[i].pdsb = NULL;
1824 // ---------------------------------------------------------------------------------------
1825 // ds_close_buffers()
1827 // Free the channel buffers
1829 void ds_close_buffers()
1831 ds_close_software_buffers();
1832 ds_close_hardware_buffers();
1835 // ---------------------------------------------------------------------------------------
1838 // Close the DirectSound system
1842 ds_close_all_channels();
1846 if (pPropertySet != NULL) {
1847 pPropertySet->Release();
1848 pPropertySet = NULL;
1851 if (Ds_property_set_pdsb != NULL) {
1852 Ds_property_set_pdsb->Release();
1853 Ds_property_set_pdsb = NULL;
1856 if (Ds_property_set_pds3db != NULL) {
1857 Ds_property_set_pds3db->Release();
1858 Ds_property_set_pds3db = NULL;
1861 if (pPrimaryBuffer) {
1862 pPrimaryBuffer->Release();
1863 pPrimaryBuffer = NULL;
1872 pDirectSound->Release();
1873 pDirectSound = NULL;
1876 if ( Ds_dll_loaded ) {
1877 FreeLibrary(Ds_dll_handle);
1881 if (Ds_must_call_couninitialize == 1) {
1886 // free the Channels[] array, since it was dynamically allocated
1891 // ---------------------------------------------------------------------------------------
1892 // ds_get_3d_interface()
1894 // Get the 3d interface for a secondary buffer.
1896 // If the secondary buffer wasn't created with a DSBCAPS_CTRL3D flag, then no 3d interface
1900 void ds_get_3d_interface(LPDIRECTSOUNDBUFFER pdsb, LPDIRECTSOUND3DBUFFER *ppds3db)
1905 dsbc.dwSize = sizeof(dsbc);
1906 DSResult = pdsb->GetCaps(&dsbc);
1907 if ( DSResult == DS_OK && dsbc.dwFlags & DSBCAPS_CTRL3D ) {
1908 DSResult = pdsb->QueryInterface( IID_IDirectSound3DBuffer, (void**)ppds3db );
1909 if ( DSResult != DS_OK ) {
1910 nprintf(("SOUND","Could not obtain 3D interface for hardware buffer: %s\n", get_DSERR_text(DSResult) ));
1917 // ---------------------------------------------------------------------------------------
1918 // ds_get_free_channel()
1920 // Find a free channel to play a sound on. If no free channels exists, free up one based
1921 // on volume levels.
1923 // input: new_volume => volume in DS units for sound to play at
1924 // snd_id => which kind of sound to play
1925 // priority => DS_MUST_PLAY
1930 // returns: channel number to play sound on
1931 // -1 if no channel could be found
1933 // NOTE: snd_id is needed since we limit the number of concurrent samples
1937 int ds_get_free_channel(int new_volume, int snd_id, int priority)
1940 int i, first_free_channel, limit;
1941 int lowest_vol = 0, lowest_vol_index = -1;
1942 int instance_count; // number of instances of sound already playing
1943 int lowest_instance_vol, lowest_instance_vol_index;
1948 lowest_instance_vol = 99;
1949 lowest_instance_vol_index = -1;
1950 first_free_channel = -1;
1952 // Look for a channel to use to play this sample
1953 for ( i = 0; i < MAX_CHANNELS; i++ ) {
1955 if ( chp->source_id == 0 ) {
1956 if ( first_free_channel == -1 )
1957 first_free_channel = i;
1961 alGetSourceiv(chp->source_id, AL_SOURCE_STATE, &status);
1963 OpenAL_ErrorCheck();
1965 if ( status != AL_PLAYING ) {
1966 if ( first_free_channel == -1 )
1967 first_free_channel = i;
1971 if ( chp->snd_id == snd_id ) {
1973 if ( chp->vol < lowest_instance_vol && chp->looping == FALSE ) {
1974 lowest_instance_vol = chp->vol;
1975 lowest_instance_vol_index = i;
1979 if ( chp->vol < lowest_vol && chp->looping == FALSE ) {
1980 lowest_vol_index = i;
1981 lowest_vol = chp->vol;
1986 // determine the limit of concurrent instances of this sound
1997 case DS_LIMIT_THREE:
2007 // If we've exceeded the limit, then maybe stop the duplicate if it is lower volume
2008 if ( instance_count >= limit ) {
2009 // If there is a lower volume duplicate, stop it.... otherwise, don't play the sound
2010 if ( lowest_instance_vol_index >= 0 && (Channels[lowest_instance_vol_index].vol <= new_volume) ) {
2011 first_free_channel = lowest_instance_vol_index;
2013 first_free_channel = -1;
2016 // there is no limit barrier to play the sound, so see if we've ran out of channels
2017 if ( first_free_channel == -1 ) {
2018 // stop the lowest volume instance to play our sound if priority demands it
2019 if ( lowest_vol_index != -1 && priority == DS_MUST_PLAY ) {
2020 // Check if the lowest volume playing is less than the volume of the requested sound.
2021 // If so, then we are going to trash the lowest volume sound.
2022 if ( Channels[lowest_vol_index].vol <= new_volume ) {
2023 first_free_channel = lowest_vol_index;
2029 return first_free_channel;
2031 int i, first_free_channel, limit;
2032 int lowest_vol = 0, lowest_vol_index = -1;
2033 int instance_count; // number of instances of sound already playing
2034 int lowest_instance_vol, lowest_instance_vol_index;
2035 unsigned long status;
2040 lowest_instance_vol = 99;
2041 lowest_instance_vol_index = -1;
2042 first_free_channel = -1;
2044 // Look for a channel to use to play this sample
2045 for ( i = 0; i < MAX_CHANNELS; i++ ) {
2047 if ( chp->pdsb == NULL ) {
2048 if ( first_free_channel == -1 )
2049 first_free_channel = i;
2053 hr = chp->pdsb->GetStatus(&status);
2054 if ( hr != DS_OK ) {
2055 nprintf(("Sound", "SOUND ==> GetStatus failed with return value %s\n", get_DSERR_text(hr) ));
2058 if ( !(status & DSBSTATUS_PLAYING) ) {
2059 if ( first_free_channel == -1 )
2060 first_free_channel = i;
2061 ds_close_channel(i);
2065 if ( chp->snd_id == snd_id ) {
2067 if ( chp->vol < lowest_instance_vol && chp->looping == FALSE ) {
2068 lowest_instance_vol = chp->vol;
2069 lowest_instance_vol_index = i;
2073 if ( chp->vol < lowest_vol && chp->looping == FALSE ) {
2074 lowest_vol_index = i;
2075 lowest_vol = chp->vol;
2080 // determine the limit of concurrent instances of this sound
2091 case DS_LIMIT_THREE:
2101 // If we've exceeded the limit, then maybe stop the duplicate if it is lower volume
2102 if ( instance_count >= limit ) {
2103 // If there is a lower volume duplicate, stop it.... otherwise, don't play the sound
2104 if ( lowest_instance_vol_index >= 0 && (Channels[lowest_instance_vol_index].vol <= new_volume) ) {
2105 ds_close_channel(lowest_instance_vol_index);
2106 first_free_channel = lowest_instance_vol_index;
2108 first_free_channel = -1;
2111 // there is no limit barrier to play the sound, so see if we've ran out of channels
2112 if ( first_free_channel == -1 ) {
2113 // stop the lowest volume instance to play our sound if priority demands it
2114 if ( lowest_vol_index != -1 && priority == DS_MUST_PLAY ) {
2115 // Check if the lowest volume playing is less than the volume of the requested sound.
2116 // If so, then we are going to trash the lowest volume sound.
2117 if ( Channels[lowest_vol_index].vol <= new_volume ) {
2118 ds_close_channel(lowest_vol_index);
2119 first_free_channel = lowest_vol_index;
2125 return first_free_channel;
2130 // ---------------------------------------------------------------------------------------
2133 // Find a free channel to play a sound on. If no free channels exists, free up one based
2134 // on volume levels.
2136 // returns: 0 => dup was successful
2137 // -1 => dup failed (Channels[channel].pdsb will be NULL)
2140 int ds_channel_dup(LPDIRECTSOUNDBUFFER pdsb, int channel, int use_ds3d)
2144 // Duplicate the master buffer into a channel buffer.
2145 DSResult = pDirectSound->DuplicateSoundBuffer(pdsb, &Channels[channel].pdsb );
2146 if ( DSResult != DS_OK ) {
2147 nprintf(("Sound", "SOUND ==> DuplicateSoundBuffer failed with return value %s\n", get_DSERR_text(DSResult) ));
2148 Channels[channel].pdsb = NULL;
2152 // get the 3d interface for the buffer if it exists
2154 if (Channels[channel].pds3db == NULL) {
2155 ds_get_3d_interface(Channels[channel].pdsb, &Channels[channel].pds3db);
2163 // ---------------------------------------------------------------------------------------
2164 // ds_restore_buffer()
2167 void ds_restore_buffer(LPDIRECTSOUNDBUFFER pdsb)
2171 Int3(); // get Alan, he wants to see this
2172 hr = pdsb->Restore();
2173 if ( hr != DS_OK ) {
2174 nprintf(("Sound", "Sound ==> Lost a buffer, tried restoring but got %s\n", get_DSERR_text(hr) ));
2179 // Create a direct sound buffer in software, without locking any data in
2180 int ds_create_buffer(int frequency, int bits_per_sample, int nchannels, int nseconds)
2186 if (!ds_initialized) {
2192 nprintf(("Sound","SOUND ==> No more OpenAL buffers available\n"));
2196 alGenBuffers (1, &i);
2198 sound_buffers[sid].buf_id = i;
2199 sound_buffers[sid].source_id = -1;
2200 sound_buffers[sid].frequency = frequency;
2201 sound_buffers[sid].bits_per_sample = bits_per_sample;
2202 sound_buffers[sid].nchannels = nchannels;
2203 sound_buffers[sid].nseconds = nseconds;
2204 sound_buffers[sid].nbytes = nseconds * (bits_per_sample / 8) * nchannels * frequency;
2213 if (!ds_initialized) {
2219 nprintf(("Sound","SOUND ==> No more software secondary buffers available\n"));
2223 // Set up buffer format
2224 wfx.wFormatTag = WAVE_FORMAT_PCM;
2225 wfx.nChannels = (unsigned short)nchannels;
2226 wfx.nSamplesPerSec = frequency;
2227 wfx.wBitsPerSample = (unsigned short)bits_per_sample;
2229 wfx.nBlockAlign = (unsigned short)(wfx.nChannels * (wfx.wBitsPerSample / 8));
2230 wfx.nAvgBytesPerSec = wfx.nBlockAlign * wfx.nSamplesPerSec;
2232 memset(&dsbd, 0, sizeof(DSBUFFERDESC));
2233 dsbd.dwSize = sizeof(DSBUFFERDESC);
2234 dsbd.dwBufferBytes = wfx.nAvgBytesPerSec * nseconds;
2235 dsbd.lpwfxFormat = &wfx;
2236 dsbd.dwFlags = DSBCAPS_STATIC | DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_CTRLDEFAULT | DSBCAPS_LOCSOFTWARE;
2238 dsrval = pDirectSound->CreateSoundBuffer(&dsbd, &ds_software_buffers[sid].pdsb, NULL);
2239 if ( dsrval != DS_OK ) {
2243 ds_software_buffers[sid].desc = dsbd;
2248 // Lock data into an existing buffer
2249 int ds_lock_data(int sid, unsigned char *data, int size)
2254 ALuint buf_id = sound_buffers[sid].buf_id;
2257 if (sound_buffers[sid].bits_per_sample == 16) {
2258 if (sound_buffers[sid].nchannels == 2) {
2259 format = AL_FORMAT_STEREO16;
2260 } else if (sound_buffers[sid].nchannels == 1) {
2261 format = AL_FORMAT_MONO16;
2265 } else if (sound_buffers[sid].bits_per_sample == 8) {
2266 if (sound_buffers[sid].nchannels == 2) {
2267 format = AL_FORMAT_STEREO8;
2268 } else if (sound_buffers[sid].nchannels == 1) {
2269 format = AL_FORMAT_MONO8;
2277 sound_buffers[sid].nbytes = size;
2279 alBufferData(buf_id, format, data, size, sound_buffers[sid].frequency);
2281 OpenAL_ErrorCheck();
2286 LPDIRECTSOUNDBUFFER pdsb;
2288 void *buffer_data, *buffer_data2;
2289 DWORD buffer_size, buffer_size2;
2292 pdsb = ds_software_buffers[sid].pdsb;
2294 memset(&caps, 0, sizeof(DSBCAPS));
2295 caps.dwSize = sizeof(DSBCAPS);
2296 dsrval = pdsb->GetCaps(&caps);
2297 if ( dsrval != DS_OK ) {
2301 pdsb->SetCurrentPosition(0);
2303 // lock the entire buffer
2304 dsrval = pdsb->Lock(0, caps.dwBufferBytes, &buffer_data, &buffer_size, &buffer_data2, &buffer_size2, 0 );
2305 if ( dsrval != DS_OK ) {
2309 // first clear it out with silence
2310 memset(buffer_data, 0x80, buffer_size);
2311 memcpy(buffer_data, data, size);
2313 dsrval = pdsb->Unlock(buffer_data, buffer_size, 0, 0);
2314 if ( dsrval != DS_OK ) {
2322 // Stop a buffer from playing directly
2323 void ds_stop_easy(int sid)
2328 int cid = sound_buffers[sid].source_id;
2331 ALuint source_id = Channels[cid].source_id;
2333 alSourceStop(source_id);
2337 LPDIRECTSOUNDBUFFER pdsb;
2340 pdsb = ds_software_buffers[sid].pdsb;
2341 dsrval = pdsb->Stop();
2345 // Play a sound without the usual baggage (used for playing back real-time voice)
2348 // sid => software id of sound
2349 // volume => volume of sound effect in DirectSound units
2350 int ds_play_easy(int sid, int volume)
2353 if (!ds_initialized)
2356 int channel = ds_get_free_channel(volume, -1, DS_MUST_PLAY);
2359 ALuint source_id = Channels[channel].source_id;
2361 alSourceStop(source_id);
2363 if (Channels[channel].buf_id != sid) {
2364 ALuint buffer_id = sound_buffers[sid].buf_id;
2366 alSourcei(source_id, AL_BUFFER, buffer_id);
2368 OpenAL_ErrorCheck();
2371 Channels[channel].buf_id = sid;
2373 ALfloat alvol = (volume != -10000) ? pow(10.0, (float)volume / (-600.0 / log10(.5))): 0.0;
2375 alSourcef(source_id, AL_GAIN, alvol);
2377 alSourcei(source_id, AL_LOOPING, AL_FALSE);
2378 alSourcePlay(source_id);
2380 OpenAL_ErrorCheck();
2388 LPDIRECTSOUNDBUFFER pdsb;
2391 pdsb = ds_software_buffers[sid].pdsb;
2393 pdsb->SetVolume(volume);
2394 dsrval=pdsb->Play(0, 0, 0);
2395 if ( dsrval != DS_OK ) {
2403 // ---------------------------------------------------------------------------------------
2404 // Play a DirectSound secondary buffer.
2408 // sid => software id of sound
2409 // hid => hardware id of sound ( -1 if not in hardware )
2410 // snd_id => what kind of sound this is
2411 // priority => DS_MUST_PLAY
2415 // volume => volume of sound effect in DirectSound units
2416 // pan => pan of sound in DirectSound units
2417 // looping => whether the sound effect is looping or not
2419 // returns: -1 => sound effect could not be started
2420 // >=0 => sig for sound effect successfully started
2422 int ds_play(int sid, int hid, int snd_id, int priority, int volume, int pan, int looping, bool is_voice_msg)
2427 if (!ds_initialized)
2430 channel = ds_get_free_channel(volume, snd_id, priority);
2433 if ( Channels[channel].source_id == 0 ) {
2437 if ( ds_using_ds3d() ) {
2441 Channels[channel].vol = volume;
2442 Channels[channel].looping = looping;
2443 Channels[channel].priority = priority;
2446 // Channels[channel].pdsb->SetPan(pan);
2448 ALfloat alvol = (volume != -10000) ? pow(10.0, (float)volume / (-600.0 / log10(.5))): 0.0;
2449 alSourcef(Channels[channel].source_id, AL_GAIN, alvol);
2451 Channels[channel].is_voice_msg = is_voice_msg;
2453 OpenAL_ErrorCheck();
2456 alGetSourceiv(Channels[channel].source_id, AL_SOURCE_STATE, &status);
2458 OpenAL_ErrorCheck();
2460 if (status == AL_PLAYING)
2461 alSourceStop(Channels[channel].source_id);
2463 OpenAL_ErrorCheck();
2465 alSourcei (Channels[channel].source_id, AL_BUFFER, sound_buffers[sid].buf_id);
2467 OpenAL_ErrorCheck();
2469 alSourcei (Channels[channel].source_id, AL_LOOPING, (looping) ? AL_TRUE : AL_FALSE);
2471 OpenAL_ErrorCheck();
2473 alSourcePlay(Channels[channel].source_id);
2475 OpenAL_ErrorCheck();
2477 sound_buffers[sid].source_id = channel;
2478 Channels[channel].buf_id = sid;
2481 // nprintf(( "Sound", "SOUND ==> Not playing sound requested at volume %.2f\n", ds_get_percentage_vol(volume) ));
2485 Channels[channel].snd_id = snd_id;
2486 Channels[channel].sig = channel_next_sig++;
2487 if (channel_next_sig < 0 ) {
2488 channel_next_sig = 1;
2491 Channels[channel].last_position = 0;
2493 // make sure there aren't any looping voice messages
2494 for (int i=0; i<MAX_CHANNELS; i++) {
2495 if (Channels[i].is_voice_msg == true) {
2496 if (Channels[i].source_id == 0) {
2500 #ifndef PLAT_UNIX /* TODO: play position still needs some work */
2501 DWORD current_position = ds_get_play_position(i);
2502 if (current_position != 0) {
2503 if (current_position < Channels[i].last_position) {
2506 Channels[i].last_position = current_position;
2513 return Channels[channel].sig;
2518 if (!ds_initialized)
2521 channel = ds_get_free_channel(volume, snd_id, priority);
2524 if ( Channels[channel].pdsb != NULL ) {
2528 // First check if the sound is in hardware, and try to duplicate from there
2531 if ( ds_channel_dup(ds_hardware_buffers[hid].pdsb, channel, 0) == 0 ) {
2532 // nprintf(("Sound", "SOUND ==> Played sound in hardware..\n"));
2536 // Channel will be NULL if hardware dup failed, or there was no hardware dup attempted
2537 if ( Channels[channel].pdsb == NULL ) {
2538 if ( ds_channel_dup(ds_software_buffers[sid].pdsb, channel, 0) == 0 ) {
2539 // nprintf(("Sound", "SOUND ==> Played sound in software..\n"));
2543 if ( Channels[channel].pdsb == NULL ) {
2547 if ( ds_using_ds3d() ) {
2548 if ( ds_is_3d_buffer(Channels[channel].pdsb) ) {
2549 if (Channels[channel].pds3db == NULL) {
2550 ds_get_3d_interface(Channels[channel].pdsb, &Channels[channel].pds3db);
2552 if ( Channels[channel].pds3db ) {
2553 Channels[channel].pds3db->SetMode(DS3DMODE_DISABLE,DS3D_IMMEDIATE);
2559 Channels[channel].vol = volume;
2560 Channels[channel].looping = looping;
2561 Channels[channel].priority = priority;
2562 Channels[channel].pdsb->SetPan(pan);
2563 Channels[channel].pdsb->SetVolume(volume);
2564 Channels[channel].is_voice_msg = is_voice_msg;
2568 ds_flags |= DSBPLAY_LOOPING;
2570 DSResult = Channels[channel].pdsb->Play(0, 0, ds_flags );
2573 if (Stop_logging_sounds == false) {
2575 sprintf(buf, "channel %d, address: %x, ds_flags: %d", channel, Channels[channel].pdsb, ds_flags);
2576 HUD_add_to_scrollback(buf, 3);
2580 if ( DSResult == DSERR_BUFFERLOST ) {
2581 ds_restore_buffer(Channels[channel].pdsb);
2582 DSResult = Channels[channel].pdsb->Play(0, 0, ds_flags );
2585 if ( DSResult != DS_OK ) {
2586 nprintf(("Sound", "Sound ==> Play failed with return value %s\n", get_DSERR_text(DSResult) ));
2591 // nprintf(( "Sound", "SOUND ==> Not playing sound requested at volume %.2f\n", ds_get_percentage_vol(volume) ));
2595 Channels[channel].snd_id = snd_id;
2596 Channels[channel].sig = channel_next_sig++;
2597 if (channel_next_sig < 0 ) {
2598 channel_next_sig = 1;
2602 if (Stop_logging_sounds == false) {
2605 sprintf(buf, "VOICE sig: %d, sid: %d, snd_id: %d, ch: %d", Channels[channel].sig, sid, snd_id, channel);
2606 HUD_add_to_scrollback(buf, 3);
2611 Channels[channel].last_position = 0;
2613 // make sure there aren't any looping voice messages
2614 for (int i=0; i<MAX_CHANNELS; i++) {
2615 if (Channels[i].is_voice_msg == true) {
2616 if (Channels[i].pdsb == NULL) {
2620 #ifndef PLAT_UNIX /* TODO: play position still needs some work */
2621 DWORD current_position = ds_get_play_position(i);
2622 if (current_position != 0) {
2623 if (current_position < Channels[i].last_position) {
2624 ds_close_channel(i);
2626 Channels[i].last_position = current_position;
2633 return Channels[channel].sig;
2638 // ---------------------------------------------------------------------------------------
2641 // Return the channel number that is playing the sound identified by sig. If that sound is
2642 // not playing, return -1.
2644 int ds_get_channel(int sig)
2649 for ( i = 0; i < MAX_CHANNELS; i++ ) {
2650 if ( Channels[i].source_id && Channels[i].sig == sig ) {
2651 if ( ds_is_channel_playing(i) == TRUE ) {
2661 for ( i = 0; i < MAX_CHANNELS; i++ ) {
2662 if ( Channels[i].pdsb && Channels[i].sig == sig ) {
2663 if ( ds_is_channel_playing(i) == TRUE ) {
2672 // ---------------------------------------------------------------------------------------
2673 // ds_is_channel_playing()
2676 int ds_is_channel_playing(int channel)
2679 if ( Channels[channel].source_id != 0 ) {
2682 alGetSourceiv(Channels[channel].source_id, AL_SOURCE_STATE, &status);
2683 OpenAL_ErrorCheck();
2685 return (status == AL_PLAYING);
2691 unsigned long status;
2693 if ( !Channels[channel].pdsb ) {
2697 hr = Channels[channel].pdsb->GetStatus(&status);
2698 if ( hr != DS_OK ) {
2699 nprintf(("Sound", "SOUND ==> GetStatus failed with return value %s\n", get_DSERR_text(hr) ));
2703 if ( status & DSBSTATUS_PLAYING )
2710 // ---------------------------------------------------------------------------------------
2711 // ds_stop_channel()
2714 void ds_stop_channel(int channel)
2717 if ( Channels[channel].source_id != 0 ) {
2718 alSourceStop(Channels[channel].source_id);
2721 ds_close_channel(channel);
2725 // ---------------------------------------------------------------------------------------
2726 // ds_stop_channel_all()
2729 void ds_stop_channel_all()
2734 for ( i=0; i<MAX_CHANNELS; i++ ) {
2735 if ( Channels[i].source_id != 0 ) {
2736 alSourceStop(Channels[i].source_id);
2742 for ( i=0; i<MAX_CHANNELS; i++ ) {
2743 if ( Channels[i].pdsb != NULL ) {
2750 // ---------------------------------------------------------------------------------------
2753 // Set the volume for a channel. The volume is expected to be in DirectSound units
2755 // If the sound is a 3D sound buffer, this is like re-establishing the maximum
2758 void ds_set_volume( int channel, int vol )
2761 ALuint source_id = Channels[channel].source_id;
2763 if (source_id != 0) {
2764 ALfloat alvol = (vol != -10000) ? pow(10.0, (float)vol / (-600.0 / log10(.5))): 0.0;
2766 alSourcef(source_id, AL_GAIN, alvol);
2770 unsigned long status;
2772 hr = Channels[channel].pdsb->GetStatus(&status);
2773 if ( hr != DS_OK ) {
2774 nprintf(("Sound", "SOUND ==> GetStatus failed with return value %s\n", get_DSERR_text(hr) ));
2778 if ( status & DSBSTATUS_PLAYING ) {
2779 Channels[channel].pdsb->SetVolume(vol);
2784 // ---------------------------------------------------------------------------------------
2787 // Set the pan for a channel. The pan is expected to be in DirectSound units
2789 void ds_set_pan( int channel, int pan )
2795 unsigned long status;
2797 hr = Channels[channel].pdsb->GetStatus(&status);
2798 if ( hr != DS_OK ) {
2799 nprintf(("Sound", "SOUND ==> GetStatus failed with return value %s\n", get_DSERR_text(hr) ));
2803 if ( status & DSBSTATUS_PLAYING ) {
2804 Channels[channel].pdsb->SetPan(pan);
2809 // ---------------------------------------------------------------------------------------
2812 // Get the pitch of a channel
2814 int ds_get_pitch(int channel)
2821 unsigned long status, pitch = 0;
2824 hr = Channels[channel].pdsb->GetStatus(&status);
2826 if ( hr != DS_OK ) {
2827 nprintf(("Sound", "SOUND ==> GetStatus failed with return value %s\n", get_DSERR_text(hr) ));
2831 if ( status & DSBSTATUS_PLAYING ) {
2832 hr = Channels[channel].pdsb->GetFrequency(&pitch);
2833 if ( hr != DS_OK ) {
2834 nprintf(("Sound", "SOUND ==> GetFrequency failed with return value %s\n", get_DSERR_text(hr) ));
2843 // ---------------------------------------------------------------------------------------
2846 // Set the pitch of a channel
2848 void ds_set_pitch(int channel, int pitch)
2853 unsigned long status;
2856 hr = Channels[channel].pdsb->GetStatus(&status);
2857 if ( hr != DS_OK ) {
2858 nprintf(("Sound", "SOUND ==> GetStatus failed with return value %s\n", get_DSERR_text(hr) ));
2862 if ( pitch < MIN_PITCH )
2865 if ( pitch > MAX_PITCH )
2868 if ( status & DSBSTATUS_PLAYING ) {
2869 Channels[channel].pdsb->SetFrequency((unsigned long)pitch);
2874 // ---------------------------------------------------------------------------------------
2875 // ds_chg_loop_status()
2878 void ds_chg_loop_status(int channel, int loop)
2881 ALuint source_id = Channels[channel].source_id;
2883 alSourcei(source_id, AL_LOOPING, loop ? AL_TRUE : AL_FALSE);
2885 unsigned long status;
2888 hr = Channels[channel].pdsb->GetStatus(&status);
2889 if ( hr != DS_OK ) {
2890 nprintf(("Sound", "SOUND ==> GetStatus failed with return value %s\n", get_DSERR_text(hr) ));
2894 if ( !(status & DSBSTATUS_PLAYING) )
2895 return; // sound is not playing anymore
2897 if ( status & DSBSTATUS_LOOPING ) {
2899 return; // we are already looping
2901 // stop the sound from looping
2902 hr = Channels[channel].pdsb->Play(0,0,0);
2907 return; // the sound is already not looping
2909 // start the sound looping
2910 hr = Channels[channel].pdsb->Play(0,0,DSBPLAY_LOOPING);
2916 // ---------------------------------------------------------------------------------------
2919 // Starts a ds3d sound playing
2923 // sid => software id for sound to play
2924 // hid => hardware id for sound to play (-1 if not in hardware)
2925 // snd_id => identifies what type of sound is playing
2926 // pos => world pos of sound
2927 // vel => velocity of object emitting sound
2928 // min => distance at which sound doesn't get any louder
2929 // max => distance at which sound becomes inaudible
2930 // looping => boolean, whether to loop the sound or not
2931 // max_volume => volume (-10000 to 0) for 3d sound at maximum
2932 // estimated_vol => manual estimated volume
2933 // priority => DS_MUST_PLAY
2938 // returns: 0 => sound started successfully
2939 // -1 => sound could not be played
2941 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 )
2951 if (!ds_initialized)
2954 channel = ds_get_free_channel(estimated_vol, snd_id, priority);
2957 Assert(Channels[channel].pdsb == NULL);
2959 // First check if the sound is in hardware, and try to duplicate from there
2962 if ( ds_is_3d_buffer(ds_hardware_buffers[hid].pdsb) == FALSE ) {
2963 nprintf(("Sound", "SOUND ==> Tried to play non-3d buffer in ds3d_play()..\n"));
2967 if ( ds_channel_dup(ds_hardware_buffers[hid].pdsb, channel, 1) == 0 ) {
2968 nprintf(("Sound", "SOUND ==> Played sound using DirectSound3D in hardware..\n"));
2972 // Channel will be NULL if hardware dup failed, or there was no hardware dup attempted
2973 if ( Channels[channel].pdsb == NULL ) {
2976 if ( ds_is_3d_buffer(ds_software_buffers[sid].pdsb) == FALSE ) {
2977 nprintf(("Sound", "SOUND ==> Tried to play non-3d buffer in ds3d_play()..\n"));
2982 if ( ds_channel_dup(ds_software_buffers[sid].pdsb, channel, 1) == 0 ) {
2983 // nprintf(("Sound", "SOUND ==> Played sound using DirectSound3D \n"));
2987 if ( Channels[channel].pdsb == NULL ) {
2992 desc = ds_software_buffers[sid].desc;
2993 desc.lpwfxFormat = &ds_software_buffers[sid].wfx;
2995 // duplicate buffer failed, so call CreateBuffer instead
2997 hr = pDirectSound->CreateSoundBuffer(&desc, &Channels[channel].pdsb, NULL );
2999 if ( (hr == DS_OK) && (Channels[channel].pdsb) ) {
3000 BYTE *pdest, *pdest2;
3002 DWORD src_ds_size, dest_ds_size, not_used;
3005 if ( ds_get_size(sid, &src_size) != 0 ) {
3007 Channels[channel].pdsb->Release();
3011 // lock the src buffer
3012 hr = ds_software_buffers[sid].pdsb->Lock(0, src_size, (void**)&psrc, &src_ds_size, (void**)&psrc2, ¬_used, 0);
3013 if ( hr != DS_OK ) {
3014 mprintf(("err: %s\n", get_DSERR_text(hr)));
3016 Channels[channel].pdsb->Release();
3020 if ( Channels[channel].pdsb->Lock(0, src_ds_size, (void**)(&pdest), &dest_ds_size, (void**)&pdest2, ¬_used, 0) == DS_OK) {
3021 memcpy(pdest, psrc, src_ds_size);
3022 Channels[channel].pdsb->Unlock(pdest, dest_ds_size, 0, 0);
3023 ds_get_3d_interface(Channels[channel].pdsb, &Channels[channel].pds3db);
3025 Channels[channel].pdsb->Release();
3032 Assert(Channels[channel].pds3db );
3033 Channels[channel].pds3db->SetMode(DS3DMODE_NORMAL,DS3D_IMMEDIATE);
3035 // set up 3D sound data here
3036 ds3d_update_buffer(channel, i2fl(min), i2fl(max), pos, vel);
3038 Channels[channel].vol = estimated_vol;
3039 Channels[channel].looping = looping;
3041 // sets the maximum "inner cone" volume
3042 Channels[channel].pdsb->SetVolume(max_volume);
3046 ds_flags |= DSBPLAY_LOOPING;
3049 hr = Channels[channel].pdsb->Play(0, 0, ds_flags );
3051 if ( hr == DSERR_BUFFERLOST ) {
3052 ds_restore_buffer(Channels[channel].pdsb);
3053 hr = Channels[channel].pdsb->Play(0, 0, ds_flags );
3056 if ( hr != DS_OK ) {
3057 nprintf(("Sound", "Sound ==> Play failed with return value %s\n", get_DSERR_text(hr) ));
3058 if ( Channels[channel].pdsb ) {
3060 while(++attempts < 10) {
3061 hr = Channels[channel].pdsb->Release();
3062 if ( hr == DS_OK ) {
3065 nprintf(("Sound","SOUND ==> DirectSound Release() failed with code %s\n.",get_DSERR_text(hr) ));
3069 Channels[channel].pdsb = NULL;
3075 nprintf(( "Sound", "SOUND ==> Not playing requested 3D sound\n"));
3079 Channels[channel].snd_id = snd_id;
3080 Channels[channel].sig = channel_next_sig++;
3081 if (channel_next_sig < 0 ) {
3082 channel_next_sig = 1;
3084 return Channels[channel].sig;
3088 void ds_set_position(int channel, DWORD offset)
3093 // set the position of the sound buffer
3094 Channels[channel].pdsb->SetCurrentPosition(offset);
3098 DWORD ds_get_play_position(int channel)
3103 /* TODO: does this work ? */
3104 alGetSourceiv(Channels[channel].source_id, AL_BYTE_LOKI, &pos);
3111 if ( Channels[channel].pdsb ) {
3112 Channels[channel].pdsb->GetCurrentPosition((LPDWORD)&play,(LPDWORD)&write);
3121 DWORD ds_get_write_position(int channel)
3129 if ( Channels[channel].pdsb ) {
3130 Channels[channel].pdsb->GetCurrentPosition((LPDWORD)&play,(LPDWORD)&write);
3139 int ds_get_channel_size(int channel)
3142 int buf_id = Channels[channel].buf_id;
3145 return sound_buffers[buf_id].nbytes;
3154 if ( Channels[channel].pdsb ) {
3155 memset(&caps, 0, sizeof(DSBCAPS));
3156 caps.dwSize = sizeof(DSBCAPS);
3157 dsrval = Channels[channel].pdsb->GetCaps(&caps);
3158 if ( dsrval != DS_OK ) {
3161 size = caps.dwBufferBytes;
3170 // Returns the number of channels that are actually playing
3171 int ds_get_number_channels()
3177 for ( i = 0; i < MAX_CHANNELS; i++ ) {
3178 if ( Channels[i].source_id ) {
3179 if ( ds_is_channel_playing(i) == TRUE ) {
3190 for ( i = 0; i < MAX_CHANNELS; i++ ) {
3191 if ( Channels[i].pdsb ) {
3192 if ( ds_is_channel_playing(i) == TRUE ) {
3202 // retreive raw data from a sound buffer
3203 int ds_get_data(int sid, char *data)
3211 LPDIRECTSOUNDBUFFER pdsb;
3217 pdsb = ds_software_buffers[sid].pdsb;
3219 memset(&caps, 0, sizeof(DSBCAPS));
3220 caps.dwSize = sizeof(DSBCAPS);
3221 dsrval = pdsb->GetCaps(&caps);
3222 if ( dsrval != DS_OK ) {
3226 // lock the entire buffer
3227 dsrval = pdsb->Lock(0, caps.dwBufferBytes, &buffer_data, &buffer_size, 0, 0, 0);
3228 if ( dsrval != DS_OK ) {
3232 memcpy(data, buffer_data, buffer_size);
3234 dsrval = pdsb->Unlock(buffer_data, buffer_size, 0, 0);
3235 if ( dsrval != DS_OK ) {
3243 // return the size of the raw sound data
3244 int ds_get_size(int sid, int *size)
3254 LPDIRECTSOUNDBUFFER pdsb;
3258 pdsb = ds_software_buffers[sid].pdsb;
3260 memset(&caps, 0, sizeof(DSBCAPS));
3261 caps.dwSize = sizeof(DSBCAPS);
3262 dsrval = pdsb->GetCaps(&caps);
3263 if ( dsrval != DS_OK ) {
3267 *size = caps.dwBufferBytes;
3276 // Return the primary buffer interface. Note that we cast to a uint to avoid
3277 // having to include dsound.h (and thus windows.h) in ds.h.
3279 uint ds_get_primary_buffer_interface()
3285 return (uint)pPrimaryBuffer;
3289 // Return the DirectSound Interface.
3291 uint ds_get_dsound_interface()
3297 return (uint)pDirectSound;
3301 uint ds_get_property_set_interface()
3306 return (uint)pPropertySet;
3310 // --------------------
3312 // EAX Functions below
3314 // --------------------
3316 // Set the master volume for the reverb added to all sound sources.
3318 // volume: volume, range from 0 to 1.0
3320 // returns: 0 if the volume is set successfully, otherwise return -1
3322 int ds_eax_set_volume(float volume)
3329 if (Ds_eax_inited == 0) {
3333 Assert(Ds_eax_reverb);
3335 CAP(volume, 0.0f, 1.0f);
3337 hr = Ds_eax_reverb->Set(DSPROPSETID_EAX_ReverbProperties_Def, DSPROPERTY_EAX_VOLUME, NULL, 0, &volume, sizeof(float));
3338 if (SUCCEEDED(hr)) {
3346 // Set the decay time for the EAX environment (ie all sound sources)
3348 // seconds: decay time in seconds
3350 // returns: 0 if decay time is successfully set, otherwise return -1
3352 int ds_eax_set_decay_time(float seconds)
3359 if (Ds_eax_inited == 0) {
3363 Assert(Ds_eax_reverb);
3365 CAP(seconds, 0.1f, 20.0f);
3367 hr = Ds_eax_reverb->Set(DSPROPSETID_EAX_ReverbProperties_Def, DSPROPERTY_EAX_DECAYTIME, NULL, 0, &seconds, sizeof(float));
3368 if (SUCCEEDED(hr)) {
3376 // Set the damping value for the EAX environment (ie all sound sources)
3378 // damp: damp value from 0 to 2.0
3380 // returns: 0 if the damp value is successfully set, otherwise return -1
3382 int ds_eax_set_damping(float damp)
3389 if (Ds_eax_inited == 0) {
3393 Assert(Ds_eax_reverb);
3395 CAP(damp, 0.0f, 2.0f);
3397 hr = Ds_eax_reverb->Set(DSPROPSETID_EAX_ReverbProperties_Def, DSPROPERTY_EAX_DAMPING, NULL, 0, &damp, sizeof(float));
3398 if (SUCCEEDED(hr)) {
3406 // Set up the environment type for all sound sources.
3408 // envid: value from the EAX_ENVIRONMENT_* enumeration in ds_eax.h
3410 // returns: 0 if the environment is set successfully, otherwise return -1
3412 int ds_eax_set_environment(unsigned long envid)
3419 if (Ds_eax_inited == 0) {
3423 Assert(Ds_eax_reverb);
3425 hr = Ds_eax_reverb->Set(DSPROPSETID_EAX_ReverbProperties_Def, DSPROPERTY_EAX_ENVIRONMENT, NULL, 0, &envid, sizeof(unsigned long));
3426 if (SUCCEEDED(hr)) {
3434 // Set up a predefined environment for EAX
3436 // envid: value from teh EAX_ENVIRONMENT_* enumeration
3438 // returns: 0 if successful, otherwise return -1
3440 int ds_eax_set_preset(unsigned long envid)
3447 if (Ds_eax_inited == 0) {
3451 Assert(Ds_eax_reverb);
3452 Assert(envid < EAX_ENVIRONMENT_COUNT);
3454 hr = Ds_eax_reverb->Set(DSPROPSETID_EAX_ReverbProperties_Def, DSPROPERTY_EAX_ALL, NULL, 0, &Ds_eax_presets[envid], sizeof(EAX_REVERBPROPERTIES));
3455 if (SUCCEEDED(hr)) {
3464 // Set up all the parameters for an environment
3466 // id: value from teh EAX_ENVIRONMENT_* enumeration
3467 // volume: volume for the environment (0 to 1.0)
3468 // damping: damp value for the environment (0 to 2.0)
3469 // decay: decay time in seconds (0.1 to 20.0)
3471 // returns: 0 if successful, otherwise return -1
3473 int ds_eax_set_all(unsigned long id, float vol, float damping, float decay)
3480 if (Ds_eax_inited == 0) {
3484 Assert(Ds_eax_reverb);
3485 Assert(id < EAX_ENVIRONMENT_COUNT);
3487 EAX_REVERBPROPERTIES er;
3489 er.environment = id;
3491 er.fDecayTime_sec = decay;
3492 er.fDamping = damping;
3494 hr = Ds_eax_reverb->Set(DSPROPSETID_EAX_ReverbProperties_Def, DSPROPERTY_EAX_ALL, NULL, 0, &er, sizeof(EAX_REVERBPROPERTIES));
3495 if (SUCCEEDED(hr)) {
3503 // Get up the parameters for the current environment
3505 // er: (output) hold environment parameters
3507 // returns: 0 if successful, otherwise return -1
3509 int ds_eax_get_all(EAX_REVERBPROPERTIES *er)
3515 unsigned long outsize;
3517 if (Ds_eax_inited == 0) {
3521 Assert(Ds_eax_reverb);
3523 hr = Ds_eax_reverb->Get(DSPROPSETID_EAX_ReverbProperties_Def, DSPROPERTY_EAX_ALL, NULL, 0, er, sizeof(EAX_REVERBPROPERTIES), &outsize);
3524 if (SUCCEEDED(hr)) {
3532 // Close down EAX, freeing any allocated resources
3537 if (Ds_eax_inited == 0) {
3547 // returns: 0 if initialization is successful, otherwise return -1
3553 unsigned long driver_support = 0;
3555 if (Ds_eax_inited) {
3559 Assert(Ds_eax_reverb == NULL);
3561 Ds_eax_reverb = (LPKSPROPERTYSET)ds_get_property_set_interface();
3562 if (Ds_eax_reverb == NULL) {
3566 // check if the listener property is supported by the audio driver
3567 hr = Ds_eax_reverb->QuerySupport(DSPROPSETID_EAX_ReverbProperties_Def, DSPROPERTY_EAX_ALL, &driver_support);
3569 nprintf(("Sound", "QuerySupport for the EAX Listener property set failed.. disabling EAX\n"));
3570 goto ds_eax_init_failed;
3573 if ((driver_support & (KSPROPERTY_SUPPORT_GET | KSPROPERTY_SUPPORT_SET)) != (KSPROPERTY_SUPPORT_GET | KSPROPERTY_SUPPORT_SET)) {
3574 goto ds_eax_init_failed;
3577 ds_eax_set_all(EAX_ENVIRONMENT_GENERIC, 0.0f, 0.0f, 0.0f);
3583 if (Ds_eax_reverb != NULL) {
3584 Ds_eax_reverb->Release();
3585 Ds_eax_reverb = NULL;
3594 int ds_eax_is_inited()
3599 return Ds_eax_inited;
3608 if (Ds_use_a3d == 0) {
3616 // Called once per game frame to make sure voice messages aren't looping
3622 if (!ds_initialized) {
3626 for (int i=0; i<MAX_CHANNELS; i++) {
3628 if (cp->is_voice_msg) {
3629 if (cp->source_id == 0) {
3633 #ifndef PLAT_UNIX /* TODO: get play position needs some work */
3634 int current_position = ds_get_play_position(i);
3635 if (current_position != 0) {
3636 if (current_position < cp->last_position) {
3640 ds_close_channel(i);
3643 cp->last_position = current_position;
3657 int ds3d_update_buffer(int channel, float min, float max, vector *pos, vector *vel)
3664 int ds3d_update_listener(vector *pos, vector *vel, matrix *orient)
3669 ALfloat posv[] = { pos->x, pos->y, pos->z };
3670 ALfloat velv[] = { vel->x, vel->y, vel->z };
3671 ALfloat oriv[] = { orient->a1d[0],
3672 orient->a1d[1], orient->a1d[2],
3673 orient->a1d[3], orient->a1d[4],
3675 alListenerfv(AL_POSITION, posv);
3676 alListenerfv(AL_VELOCITY, velv);
3677 alListenerfv(AL_ORIENTATION, oriv);
3683 int ds3d_init (int unused)
3688 ALfloat pos[] = { 0.0, 0.0, 0.0 },
3689 vel[] = { 0.0, 0.0, 0.0 },
3690 ori[] = { 0.0, 0.0, 1.0, 0.0, -1.0, 0.0 };
3692 alListenerfv (AL_POSITION, pos);
3693 alListenerfv (AL_VELOCITY, vel);
3694 alListenerfv (AL_ORIENTATION, ori);
3696 if(alGetError() != AL_NO_ERROR)
3710 int dscap_create_buffer(int freq, int bits_per_sample, int nchannels, int nseconds)
3717 int dscap_get_raw_data(unsigned char *outbuf, unsigned int max_size)
3724 int dscap_max_buffersize()
3731 void dscap_release_buffer()
3736 int dscap_start_record()
3743 int dscap_stop_record()
3750 int dscap_supported()