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.10 2002/06/09 04:41:26 relnev
19 * added copyright header
21 * Revision 1.9 2002/06/05 08:05:29 relnev
22 * stub/warning removal.
24 * reworked the sound code.
26 * Revision 1.8 2002/06/05 04:03:33 relnev
27 * finished cfilesystem.
29 * removed some old code.
31 * fixed mouse save off-by-one.
35 * Revision 1.7 2002/06/02 22:31:37 cemason
38 * Revision 1.6 2002/06/02 21:11:12 cemason
41 * Revision 1.5 2002/06/02 09:50:42 relnev
44 * Revision 1.4 2002/06/02 07:17:44 cemason
45 * Added OpenAL support.
47 * Revision 1.3 2002/05/28 17:03:29 theoddone33
48 * fs2 gets to the main game loop now
50 * Revision 1.2 2002/05/27 21:35:50 theoddone33
51 * Stub out dsound backend
53 * Revision 1.1.1.1 2002/05/03 03:28:10 root
57 * 18 10/25/99 5:56p Jefff
58 * increase num software channels to the number the users hardware can
59 * handle. not less than 16, tho.
61 * 17 9/08/99 3:22p Dave
62 * Updated builtin mission list.
64 * 16 8/27/99 6:38p Alanl
65 * crush the blasted repeating messages bug
67 * 15 8/23/99 11:16p Danw
70 * 14 8/22/99 11:06p Alanl
71 * fix small bug in ds_close_channel
73 * 13 8/19/99 11:25a Alanl
74 * change format of secondary buffer from 44100 to 22050
76 * 12 8/17/99 4:11p Danw
77 * AL: temp fix for solving A3D crash
79 * 11 8/06/99 2:20p Jasonh
80 * AL: free 3D portion of buffer first
82 * 10 8/04/99 9:48p Alanl
83 * fix bug with setting 3D properties on a 2D sound buffer
85 * 9 8/04/99 11:42a Danw
86 * tone down EAX reverb
88 * 8 8/01/99 2:06p Alanl
89 * increase the rolloff for A3D
91 * 7 7/20/99 5:28p Dave
92 * Fixed debug build error.
94 * 6 7/20/99 1:49p Dave
95 * Peter Drake build. Fixed some release build warnings.
97 * 5 7/14/99 11:32a Danw
98 * AL: add some debug code to catch nefarious A3D problem
100 * 4 5/23/99 8:11p Alanl
101 * Added support for EAX
103 * 3 10/08/98 4:29p Dave
104 * Removed reference to osdefs.h
106 * 2 10/07/98 10:54a Dave
109 * 1 10/07/98 10:51a Dave
111 * 72 6/28/98 6:34p Lawrance
112 * add sanity check in while() loop for releasing channels
114 * 71 6/13/98 1:45p Sandeep
116 * 70 6/10/98 2:29p Lawrance
117 * don't use COM for initializing DirectSound... appears some machines
120 * 69 5/26/98 2:10a Lawrance
121 * make sure DirectSound pointer gets freed if Aureal resource manager
124 * 68 5/21/98 9:14p Lawrance
125 * remove obsolete registry setting
127 * 67 5/20/98 4:28p Allender
128 * upped sound buffers as per alan's request
130 * 66 5/15/98 3:36p John
131 * Fixed bug with new graphics window code and standalone server. Made
132 * hwndApp not be a global anymore.
134 * 65 5/06/98 3:37p Lawrance
135 * allow panned sounds geesh
137 * 64 5/05/98 4:49p Lawrance
138 * Put in code to authenticate A3D, improve A3D support
140 * 63 4/20/98 11:17p Lawrance
141 * fix bug with releasing channels
143 * 62 4/20/98 7:34p Lawrance
144 * take out obsolete directsound3d debug command
146 * 61 4/20/98 11:10a Lawrance
147 * put correct flags when creating sound buffer
149 * 60 4/20/98 12:03a Lawrance
150 * Allow prioritizing of CTRL3D buffers
152 * 59 4/19/98 9:31p Lawrance
153 * Use Aureal_enabled flag
155 * 58 4/19/98 9:39a Lawrance
156 * use DYNAMIC_LOOPERS for Aureal resource manager
158 * 57 4/19/98 4:13a Lawrance
159 * Improve how dsound is initialized
161 * 56 4/18/98 9:13p Lawrance
162 * Added Aureal support.
164 * 55 4/13/98 5:04p Lawrance
165 * Write functions to determine how many milliseconds are left in a sound
167 * 54 4/09/98 5:53p Lawrance
168 * Make DirectSound init more robust
170 * 53 4/01/98 9:21p John
171 * Made NDEBUG, optimized build with no warnings or errors.
173 * 52 3/31/98 5:19p John
174 * Removed demo/save/restore. Made NDEBUG defined compile. Removed a
175 * bunch of debug stuff out of player file. Made model code be able to
176 * unload models and malloc out only however many models are needed.
179 * 51 3/29/98 12:56a Lawrance
180 * preload the warp in and explosions sounds before a mission.
182 * 50 3/25/98 6:10p Lawrance
183 * Work on DirectSound3D
185 * 49 3/24/98 4:28p Lawrance
186 * Make DirectSound3D support more robust
188 * 48 3/24/98 11:49a Dave
189 * AL: Change way buffer gets locked.
191 * 47 3/24/98 11:27a Lawrance
192 * Use buffer_size for memcpy when locking buffer
194 * 46 3/23/98 10:32a Lawrance
195 * Add functions for extracting raw sound data
197 * 45 3/19/98 5:36p Lawrance
198 * Add some sound debug functions to see how many sounds are playing, and
199 * to start/stop random looping sounds.
201 * 44 3/07/98 3:35p Dave
202 * AL: check for ds being initialized in ds_create_buffer()
204 * 43 2/18/98 5:49p Lawrance
205 * Even if the ADPCM codec is unavailable, allow game to continue.
207 * 42 2/16/98 7:31p Lawrance
208 * get compression/decompression of voice working
210 * 41 2/15/98 11:10p Lawrance
211 * more work on real-time voice system
213 * 40 2/15/98 4:43p Lawrance
214 * work on real-time voice
216 * 39 2/06/98 7:30p John
217 * Added code to monitor the number of channels of sound actually playing.
219 * 38 2/06/98 8:56a Allender
220 * fixed calling convention problem with DLL handles
222 * 37 2/04/98 6:08p Lawrance
223 * Read function pointers from dsound.dll, further work on
224 * DirectSoundCapture.
226 * 36 2/03/98 11:53p Lawrance
227 * Adding support for DirectSoundCapture
229 * 35 1/31/98 5:48p Lawrance
230 * Start on real-time voice recording
232 * 34 1/10/98 1:14p John
233 * Added explanation to debug console commands
235 * 33 12/21/97 4:33p John
236 * Made debug console functions a class that registers itself
237 * automatically, so you don't need to add the function to
238 * debugfunctions.cpp.
240 * 32 12/08/97 12:24a Lawrance
241 * Allow duplicate sounds to be stopped if less than OR equal to new sound
244 * 31 12/05/97 5:19p Lawrance
245 * re-do sound priorities to make more general and extensible
247 * 30 11/28/97 2:09p Lawrance
248 * Overhaul how ADPCM conversion works... use much less memory... safer
251 * 29 11/22/97 11:32p Lawrance
252 * decompress ADPCM data into 8 bit (not 16bit) for regular sounds (ie not
255 * 28 11/20/97 5:36p Dave
256 * Hooked in a bunch of main hall changes (including sound). Made it
257 * possible to reposition (rewind/ffwd)
258 * sound buffer pointers. Fixed animation direction change framerate
261 * 27 10/13/97 7:41p Lawrance
262 * store duration of sound
264 * 26 10/11/97 6:39p Lawrance
265 * start playing primary buffer, to reduce latency on sounds starting
267 * 25 10/08/97 5:09p Lawrance
268 * limit player impact sounds so only one plays at a time
270 * 24 9/26/97 5:43p Lawrance
271 * fix a bug that was freeing memory early when playing compressed sound
274 * 23 9/09/97 3:39p Sandeep
275 * warning level 4 bugs
277 * 22 8/16/97 4:05p Lawrance
278 * don't load sounds into hardware if running Lean_and_mean
280 * 21 8/05/97 1:39p Lawrance
281 * support compressed stereo playback
283 * 20 7/31/97 10:38a Lawrance
284 * return old debug function for toggling DirectSound3D
286 * 19 7/29/97 3:27p Lawrance
287 * make console toggle for directsound3d work right
289 * 18 7/28/97 11:39a Lawrance
290 * allow individual volume scaling on 3D buffers
292 * 17 7/18/97 8:18p Lawrance
293 * fix bug in ds_get_free_channel() that caused sounds to not play when
296 * 16 7/17/97 8:04p Lawrance
297 * allow priority sounds to play if free channel, otherwise stop lowest
298 * volume priority sound of same type
300 * 15 7/17/97 5:57p John
301 * made directsound3d config value work
303 * 14 7/17/97 5:43p John
304 * added new config stuff
306 * 13 7/17/97 4:25p John
307 * First, broken, stage of changing config stuff
309 * 12 7/15/97 12:13p Lawrance
310 * don't stop sounds that have highest priority
312 * 11 7/15/97 11:15a Lawrance
313 * limit the max instances of simultaneous sound effects, implement
314 * priorities to force critical sounds
316 * 10 6/09/97 11:50p Lawrance
317 * integrating DirectSound3D
319 * 9 6/08/97 5:59p Lawrance
320 * integrate DirectSound3D into sound system
322 * 8 6/04/97 1:19p Lawrance
323 * made hardware mixing robust
325 * 7 6/03/97 1:56p Hoffoss
326 * Return correct error code when direct sound init fails.
328 * 6 6/03/97 12:07p Lawrance
329 * don't enable 3D sounds in Primary buffer
331 * 5 6/02/97 3:45p Dan
332 * temp disable of hardware mixing until problem solved with
333 * CreateBuffer() failing
335 * 4 6/02/97 1:45p Lawrance
336 * implementing hardware mixing
338 * 3 5/29/97 4:01p Lawrance
339 * let snd_init() have final say on initialization
341 * 2 5/29/97 12:04p Lawrance
342 * creation of file to hold DirectSound specific portions
361 #include <initguid.h>
363 #include "verifya3d.h"
368 #include <SDL/SDL_audio.h>
372 // Pointers to functions contained in DSOUND.dll
373 HRESULT (__stdcall *pfn_DirectSoundCreate)(LPGUID lpGuid, LPDIRECTSOUND *ppDS, IUnknown FAR *pUnkOuter) = NULL;
374 HRESULT (__stdcall *pfn_DirectSoundCaptureCreate)(LPGUID lpGUID, LPDIRECTSOUNDCAPTURE *lplpDSC, LPUNKNOWN pUnkOuter) = NULL;
376 HINSTANCE Ds_dll_handle=NULL;
378 LPDIRECTSOUND pDirectSound = NULL;
379 LPDIRECTSOUNDBUFFER pPrimaryBuffer = NULL;
380 LPIA3D2 pIA3d2 = NULL;
382 static LPKSPROPERTYSET pPropertySet; // pointer to sound card property set
383 static LPDIRECTSOUNDBUFFER Ds_property_set_pdsb = NULL;
384 static LPDIRECTSOUND3DBUFFER Ds_property_set_pds3db = NULL;
386 static int Ds_must_call_couninitialize = 0;
388 channel* Channels; //[MAX_CHANNELS];
389 static int channel_next_sig = 1;
391 #define MAX_DS_SOFTWARE_BUFFERS 256
392 typedef struct ds_sound_buffer
394 LPDIRECTSOUNDBUFFER pdsb;
400 ds_sound_buffer ds_software_buffers[MAX_DS_SOFTWARE_BUFFERS];
402 #define MAX_DS_HARDWARE_BUFFERS 32
403 ds_sound_buffer ds_hardware_buffers[MAX_DS_HARDWARE_BUFFERS];
405 static DSCAPS Soundcard_caps; // current soundcard capabilities
407 extern int Snd_sram; // mem (in bytes) used up by storing sounds in system memory
408 extern int Snd_hram; // mem (in bytes) used up by storing sounds in soundcard memory
410 static int Ds_use_ds3d = 0;
411 static int Ds_use_a3d = 0;
412 static int Ds_use_eax = 0;
414 GUID IID_IA3d2_Def = {0xfb80d1e0, 0x98d3, 0x11d1, {0x90, 0xfb, 0x00, 0x60, 0x08, 0xa1, 0xf4, 0x41}};
415 GUID CLSID_A3d_Def = {0xd8f1eee0, 0xf634, 0x11cf, {0x87, 0x0, 0x0, 0xa0, 0x24, 0x5d, 0x91, 0x8b}};
417 static bool Stop_logging_sounds = false;
420 ///////////////////////////
424 ///////////////////////////
427 //#define EAX_PRESET_GENERIC EAX_ENVIRONMENT_GENERIC,0.5F,1.493F,0.5F
428 #define EAX_PRESET_GENERIC EAX_ENVIRONMENT_GENERIC,0.2F,0.2F,1.0F
429 #define EAX_PRESET_PADDEDCELL EAX_ENVIRONMENT_PADDEDCELL,0.25F,0.1F,0.0F
430 #define EAX_PRESET_ROOM EAX_ENVIRONMENT_ROOM,0.417F,0.4F,0.666F
431 #define EAX_PRESET_BATHROOM EAX_ENVIRONMENT_BATHROOM,0.653F,1.499F,0.166F
432 #define EAX_PRESET_LIVINGROOM EAX_ENVIRONMENT_LIVINGROOM,0.208F,0.478F,0.0F
433 #define EAX_PRESET_STONEROOM EAX_ENVIRONMENT_STONEROOM,0.5F,2.309F,0.888F
434 #define EAX_PRESET_AUDITORIUM EAX_ENVIRONMENT_AUDITORIUM,0.403F,4.279F,0.5F
435 #define EAX_PRESET_CONCERTHALL EAX_ENVIRONMENT_CONCERTHALL,0.5F,3.961F,0.5F
436 #define EAX_PRESET_CAVE EAX_ENVIRONMENT_CAVE,0.5F,2.886F,1.304F
437 #define EAX_PRESET_ARENA EAX_ENVIRONMENT_ARENA,0.361F,7.284F,0.332F
438 #define EAX_PRESET_HANGAR EAX_ENVIRONMENT_HANGAR,0.5F,10.0F,0.3F
439 #define EAX_PRESET_CARPETEDHALLWAY EAX_ENVIRONMENT_CARPETEDHALLWAY,0.153F,0.259F,2.0F
440 #define EAX_PRESET_HALLWAY EAX_ENVIRONMENT_HALLWAY,0.361F,1.493F,0.0F
441 #define EAX_PRESET_STONECORRIDOR EAX_ENVIRONMENT_STONECORRIDOR,0.444F,2.697F,0.638F
442 #define EAX_PRESET_ALLEY EAX_ENVIRONMENT_ALLEY,0.25F,1.752F,0.776F
443 #define EAX_PRESET_FOREST EAX_ENVIRONMENT_FOREST,0.111F,3.145F,0.472F
444 #define EAX_PRESET_CITY EAX_ENVIRONMENT_CITY,0.111F,2.767F,0.224F
445 #define EAX_PRESET_MOUNTAINS EAX_ENVIRONMENT_MOUNTAINS,0.194F,7.841F,0.472F
446 #define EAX_PRESET_QUARRY EAX_ENVIRONMENT_QUARRY,1.0F,1.499F,0.5F
447 #define EAX_PRESET_PLAIN EAX_ENVIRONMENT_PLAIN,0.097F,2.767F,0.224F
448 #define EAX_PRESET_PARKINGLOT EAX_ENVIRONMENT_PARKINGLOT,0.208F,1.652F,1.5F
449 #define EAX_PRESET_SEWERPIPE EAX_ENVIRONMENT_SEWERPIPE,0.652F,2.886F,0.25F
450 #define EAX_PRESET_UNDERWATER EAX_ENVIRONMENT_UNDERWATER,1.0F,1.499F,0.0F
451 #define EAX_PRESET_DRUGGED EAX_ENVIRONMENT_DRUGGED,0.875F,8.392F,1.388F
452 #define EAX_PRESET_DIZZY EAX_ENVIRONMENT_DIZZY,0.139F,17.234F,0.666F
453 #define EAX_PRESET_PSYCHOTIC EAX_ENVIRONMENT_PSYCHOTIC,0.486F,7.563F,0.806F
455 static LPKSPROPERTYSET Ds_eax_reverb = NULL;
457 static int Ds_eax_inited = 0;
459 EAX_REVERBPROPERTIES Ds_eax_presets[] =
461 {EAX_PRESET_GENERIC},
462 {EAX_PRESET_PADDEDCELL},
464 {EAX_PRESET_BATHROOM},
465 {EAX_PRESET_LIVINGROOM},
466 {EAX_PRESET_STONEROOM},
467 {EAX_PRESET_AUDITORIUM},
468 {EAX_PRESET_CONCERTHALL},
472 {EAX_PRESET_CARPETEDHALLWAY},
473 {EAX_PRESET_HALLWAY},
474 {EAX_PRESET_STONECORRIDOR},
478 {EAX_PRESET_MOUNTAINS},
481 {EAX_PRESET_PARKINGLOT},
482 {EAX_PRESET_SEWERPIPE},
483 {EAX_PRESET_UNDERWATER},
484 {EAX_PRESET_DRUGGED},
486 {EAX_PRESET_PSYCHOTIC},
489 GUID DSPROPSETID_EAX_ReverbProperties_Def = {0x4a4e6fc1, 0xc341, 0x11d1, {0xb7, 0x3a, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}};
490 GUID DSPROPSETID_EAXBUFFER_ReverbProperties_Def = {0x4a4e6fc0, 0xc341, 0x11d1, {0xb7, 0x3a, 0x44, 0x45, 0x53, 0x54, 0x00, 0x00}};
492 //----------------------------------------------------------------
494 void ds_get_soundcard_caps(DSCAPS *dscaps);
497 typedef struct channel
499 int sig; // uniquely identifies the sound playing on the channel
500 int snd_id; // identifies which kind of sound is playing
501 ALuint source_id; // OpenAL source id
502 int buf_id; // currently bound buffer index (-1 if none)
503 int looping; // flag to indicate that the sound is looping
505 int priority; // implementation dependant priority
510 typedef struct sound_buffer
512 ALuint buf_id; // OpenAL buffer id
513 int source_id; // source index this buffer is currently bound to
521 #define MAX_DS_SOFTWARE_BUFFERS 256
523 static int MAX_CHANNELS = 1000; // initialized properly in ds_init_channels()
525 static int channel_next_sig = 1;
527 sound_buffer sound_buffers[MAX_DS_SOFTWARE_BUFFERS];
529 static int Ds_use_ds3d = 0;
530 static int Ds_use_a3d = 0;
531 static int Ds_use_eax = 0;
533 ALCdevice *ds_sound_device;
534 void *ds_sound_context = (void *)0;
537 #define OpenAL_ErrorCheck() do { \
538 int i = alGetError(); \
539 if (i != AL_NO_ERROR) { \
540 while(i != AL_NO_ERROR) { \
541 nprintf(("Warning", "%s/%s:%d - OpenAL error %s\n", __FUNCTION__, __FILE__, __LINE__, alGetString(i))); \
548 #define OpenAL_ErrorCheck()
553 int ds_vol_lookup[101]; // lookup table for direct sound volumes
554 int ds_initialized = FALSE;
557 //--------------------------------------------------------------------------
560 // Determine if a secondary buffer is a 3d secondary buffer.
563 int ds_is_3d_buffer(LPDIRECTSOUNDBUFFER pdsb)
568 dsbc.dwSize = sizeof(dsbc);
569 hr = pdsb->GetCaps(&dsbc);
570 if ( hr == DS_OK && dsbc.dwFlags & DSBCAPS_CTRL3D ) {
579 //--------------------------------------------------------------------------
582 // Determine if a secondary buffer is a 3d secondary buffer.
584 int ds_is_3d_buffer(int sid)
588 return ds_is_3d_buffer(ds_software_buffers[sid].pdsb);
595 //--------------------------------------------------------------------------
596 // ds_build_vol_lookup()
598 // Fills up the ds_vol_lookup[] tables that converts from a volume in the form
599 // 0.0 -> 1.0 to -10000 -> 0 (this is the DirectSound method, where units are
600 // hundredths of decibls)
602 void ds_build_vol_lookup()
607 ds_vol_lookup[0] = -10000;
608 for ( i = 1; i <= 100; i++ ) {
610 ds_vol_lookup[i] = fl2i( (log(vol) / log(2.0f)) * 1000.0f);
615 //--------------------------------------------------------------------------
616 // ds_convert_volume()
618 // Takes volume between 0.0f and 1.0f and converts into
619 // DirectSound style volumes between -10000 and 0.
620 int ds_convert_volume(float volume)
624 index = fl2i(volume * 100.0f);
630 return ds_vol_lookup[index];
633 //--------------------------------------------------------------------------
634 // ds_get_percentage_vol()
636 // Converts -10000 -> 0 range volume to 0 -> 1
637 float ds_get_percentage_vol(int ds_vol)
640 vol = pow(2.0, ds_vol/1000.0);
644 // ---------------------------------------------------------------------------------------
647 // Parse a wave file.
649 // parameters: filename => file of sound to parse
650 // dest => address of pointer of where to store raw sound data (output parm)
651 // dest_size => number of bytes of sound data stored (output parm)
652 // header => address of pointer to a WAVEFORMATEX struct (output parm)
654 // returns: 0 => wave file successfully parsed
657 // NOTE: memory is malloced for the header and dest in this function. It is the responsibility
658 // of the caller to free this memory later.
660 int ds_parse_wave(char *filename, ubyte **dest, uint *dest_size, WAVEFORMATEX **header)
663 PCMWAVEFORMAT PCM_header;
665 unsigned int tag, size, next_chunk;
667 fp = cfopen( filename, "rb" );
669 nprintf(("Error", "Couldn't open '%s'\n", filename ));
673 // Skip the "RIFF" tag and file size (8 bytes)
674 // Skip the "WAVE" tag (4 bytes)
675 cfseek( fp, 12, CF_SEEK_SET );
677 // Now read RIFF tags until the end of file
680 if ( cfread( &tag, sizeof(uint), 1, fp ) != 1 )
683 if ( cfread( &size, sizeof(uint), 1, fp ) != 1 )
686 next_chunk = cftell(fp) + size;
689 case 0x20746d66: // The 'fmt ' tag
690 //nprintf(("Sound", "SOUND => size of fmt block: %d\n", size));
691 cfread( &PCM_header, sizeof(PCMWAVEFORMAT), 1, fp );
692 if ( PCM_header.wf.wFormatTag != WAVE_FORMAT_PCM ) {
693 cbExtra = cfread_short(fp);
696 // Allocate memory for WAVEFORMATEX structure + extra bytes
697 if ( (*header = (WAVEFORMATEX *) malloc ( sizeof(WAVEFORMATEX)+cbExtra )) != NULL ){
698 // Copy bytes from temporary format structure
699 memcpy (*header, &PCM_header, sizeof(PCM_header));
700 (*header)->cbSize = (unsigned short)cbExtra;
702 // Read those extra bytes, append to WAVEFORMATEX structure
704 cfread( ((ubyte *)(*header) + sizeof(WAVEFORMATEX)), cbExtra, 1, fp);
708 Assert(0); // malloc failed
712 case 0x61746164: // the 'data' tag
714 (*dest) = (ubyte *)malloc(size);
715 Assert( *dest != NULL );
716 cfread( *dest, size, 1, fp );
718 default: // unknown, skip it
721 cfseek( fp, next_chunk, CF_SEEK_SET );
728 // ---------------------------------------------------------------------------------------
737 for ( i = 0; i < MAX_DS_SOFTWARE_BUFFERS; i++ ) {
738 if ( sound_buffers[i].buf_id == 0 )
742 if ( i == MAX_DS_SOFTWARE_BUFFERS ) {
750 for ( i = 0; i < MAX_DS_SOFTWARE_BUFFERS; i++ ) {
751 if ( ds_software_buffers[i].pdsb == NULL )
755 if ( i == MAX_DS_SOFTWARE_BUFFERS ) {
763 // ---------------------------------------------------------------------------------------
774 for ( i = 0; i < MAX_DS_HARDWARE_BUFFERS; i++ ) {
775 if ( ds_hardware_buffers[i].pdsb == NULL )
779 if ( i == MAX_DS_HARDWARE_BUFFERS ) {
787 // ---------------------------------------------------------------------------------------
788 // Load a DirectSound secondary buffer with sound data. The sounds data for
789 // game sounds are stored in the DirectSound secondary buffers, and are
790 // duplicated as needed and placed in the Channels[] array to be played.
794 // sid => pointer to software id for sound ( output parm)
795 // hid => pointer to hardware id for sound ( output parm)
796 // final_size => pointer to storage to receive uncompressed sound size (output parm)
797 // header => pointer to a WAVEFORMATEX structure
798 // si => sound_info structure, contains details on the sound format
799 // flags => buffer properties ( DS_HARDWARE , DS_3D )
801 // returns: -1 => sound effect could not loaded into a secondary buffer
802 // 0 => sound effect successfully loaded into a secondary buffer
805 // NOTE: this function is slow, especially when sounds are loaded into hardware. Don't call this
806 // function from within gameplay.
809 int ds_load_buffer(int *sid, int *hid, int *final_size, void *header, sound_info *si, int flags)
812 Assert( final_size != NULL );
813 Assert( header != NULL );
814 Assert( si != NULL );
815 Assert( si->data != NULL );
817 // All sounds are required to have a software buffer
821 nprintf(("Sound","SOUND ==> No more sound buffers available\n"));
826 alGenBuffers (1, &pi);
835 switch (si->format) {
836 case WAVE_FORMAT_PCM:
845 /* format is now in pcm */
846 frequency = si->sample_rate;
848 if (si->bits == 16) {
849 if (si->n_channels == 2) {
850 format = AL_FORMAT_STEREO16;
851 } else if (si->n_channels == 1) {
852 format = AL_FORMAT_MONO16;
856 } else if (si->bits == 8) {
857 if (si->n_channels == 2) {
858 format = AL_FORMAT_STEREO8;
859 } else if (si->n_channels == 1) {
860 format = AL_FORMAT_MONO8;
870 alBufferData (pi, format, data, size, frequency);
872 sound_buffers[*sid].buf_id = pi;
873 sound_buffers[*sid].source_id = -1;
874 sound_buffers[*sid].frequency = frequency;
875 sound_buffers[*sid].bits_per_sample = si->bits;
876 sound_buffers[*sid].nchannels = si->n_channels;
877 sound_buffers[*sid].nseconds = si->size / si->avg_bytes_per_sec;
884 Assert( final_size != NULL );
885 Assert( header != NULL );
886 Assert( si != NULL );
887 Assert( si->data != NULL );
888 Assert( si->size > 0 );
889 Assert( si->sample_rate > 0);
890 Assert( si->bits > 0 );
891 Assert( si->n_channels > 0 );
892 Assert( si->n_block_align >= 0 );
893 Assert( si->avg_bytes_per_sec > 0 );
895 WAVEFORMATEX *pwfx = (WAVEFORMATEX *)header;
896 DSBUFFERDESC BufferDesc;
897 WAVEFORMATEX WaveFormat;
899 int rc, final_sound_size, DSOUND_load_buffer_result = 0;
900 BYTE *pData, *pData2;
901 DWORD DataSize, DataSize2;
903 // the below two covnert_ variables are only used when the wav format is not
904 // PCM. DirectSound only takes PCM sound data, so we must convert to PCM if required
905 ubyte *convert_buffer = NULL; // storage for converted wav file
906 int convert_len; // num bytes of converted wav file
907 uint src_bytes_used; // number of source bytes actually converted (should always be equal to original size)
909 // Ensure DirectSound initialized
910 if (!ds_initialized) {
911 DSOUND_load_buffer_result = -1;
912 goto DSOUND_load_buffer_done;
915 // Set up buffer information
916 WaveFormat.wFormatTag = (unsigned short)si->format;
917 WaveFormat.nChannels = (unsigned short)si->n_channels;
918 WaveFormat.nSamplesPerSec = si->sample_rate;
919 WaveFormat.wBitsPerSample = (unsigned short)si->bits;
920 WaveFormat.cbSize = 0;
921 WaveFormat.nBlockAlign = (unsigned short)si->n_block_align;
922 WaveFormat.nAvgBytesPerSec = si->avg_bytes_per_sec;
924 final_sound_size = si->size; // assume this format will be used, may be over-ridded by convert_len
926 // Assert(WaveFormat.nChannels == 1);
928 switch ( si->format ) {
929 case WAVE_FORMAT_PCM:
932 case WAVE_FORMAT_ADPCM:
934 nprintf(( "Sound", "SOUND ==> converting sound from ADPCM to PCM\n" ));
935 rc = ACM_convert_ADPCM_to_PCM(pwfx, si->data, si->size, &convert_buffer, 0, &convert_len, &src_bytes_used, 8);
937 DSOUND_load_buffer_result = -1;
938 goto DSOUND_load_buffer_done;
941 if (src_bytes_used != si->size) {
942 Int3(); // ACM conversion failed?
943 DSOUND_load_buffer_result = -1;
944 goto DSOUND_load_buffer_done;
947 final_sound_size = convert_len;
949 // Set up the WAVEFORMATEX structure to have the right PCM characteristics
950 WaveFormat.wFormatTag = WAVE_FORMAT_PCM;
951 WaveFormat.nChannels = (unsigned short)si->n_channels;
952 WaveFormat.nSamplesPerSec = si->sample_rate;
953 WaveFormat.wBitsPerSample = 8;
954 WaveFormat.cbSize = 0;
955 WaveFormat.nBlockAlign = (unsigned short)(( WaveFormat.nChannels * WaveFormat.wBitsPerSample ) / 8);
956 WaveFormat.nAvgBytesPerSec = WaveFormat.nBlockAlign * WaveFormat.nSamplesPerSec;
958 nprintf(( "Sound", "SOUND ==> Coverted sound from ADPCM to PCM successfully\n" ));
962 nprintf(( "Sound", "Unsupported sound encoding\n" ));
963 DSOUND_load_buffer_result = -1;
964 goto DSOUND_load_buffer_done;
968 WaveFormat.wFormatTag = WAVE_FORMAT_PCM; // DirectSound only used PCM wave files
970 // Set up a DirectSound buffer
971 ZeroMemory(&BufferDesc, sizeof(BufferDesc));
972 BufferDesc.dwSize = sizeof(BufferDesc);
973 BufferDesc.dwBufferBytes = final_sound_size;
974 BufferDesc.lpwfxFormat = &WaveFormat;
976 // check if DirectSound3D is enabled and the sound is flagged for 3D
977 if ((ds_using_ds3d()) && (flags & DS_USE_DS3D)) {
978 // if (ds_using_ds3d()) {
979 BufferDesc.dwFlags = DSBCAPS_STATIC | DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_CTRLVOLUME | DSBCAPS_CTRL3D | DSBCAPS_MUTE3DATMAXDISTANCE;
981 BufferDesc.dwFlags = DSBCAPS_STATIC | DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_CTRLVOLUME | DSBCAPS_LOCSOFTWARE;
984 // Create a new software buffer using the settings for this wave
985 // All sounds are required to have a software buffer
988 nprintf(("Sound","SOUND ==> No more software secondary buffers available\n"));
991 DSReturn = pDirectSound->CreateSoundBuffer(&BufferDesc, &ds_software_buffers[*sid].pdsb, NULL );
993 if ( DSReturn == DS_OK && ds_software_buffers[*sid].pdsb != NULL ) {
995 ds_software_buffers[*sid].desc = BufferDesc;
996 ds_software_buffers[*sid].wfx = *BufferDesc.lpwfxFormat;
998 // Lock the buffer and copy in the data
999 if ((ds_software_buffers[*sid].pdsb)->Lock(0, final_sound_size, (void**)(&pData), &DataSize, (void**)(&pData2), &DataSize2, 0) == DS_OK) {
1001 if ( convert_buffer )
1002 memcpy(pData, convert_buffer, final_sound_size); // use converted data (PCM format)
1004 memcpy(pData, si->data, final_sound_size);
1006 (ds_software_buffers[*sid].pdsb)->Unlock(pData, DataSize, 0, 0);
1008 DSOUND_load_buffer_result = 0;
1010 // update ram used for sound
1011 Snd_sram += final_sound_size;
1012 *final_size = final_sound_size;
1015 nprintf(("Sound","SOUND => fatal error in DSOUND_load_buffer\n"));
1017 DSOUND_load_buffer_result = -1;
1020 DSOUND_load_buffer_done:
1021 if ( convert_buffer )
1022 free( convert_buffer );
1023 return DSOUND_load_buffer_result;
1027 // ---------------------------------------------------------------------------------------
1028 // ds_init_channels()
1030 // init the Channels[] array
1032 void ds_init_channels()
1039 Channels = (channel*) malloc(sizeof(channel) * MAX_CHANNELS);
1040 if (Channels == NULL) {
1041 Error(LOCATION, "Unable to allocate %d bytes for %d audio channels.", sizeof(channel) * MAX_CHANNELS, MAX_CHANNELS);
1044 // init the channels
1045 for ( i = 0; i < MAX_CHANNELS; i++ ) {
1046 alGenSources(1, &Channels[i].source_id);
1047 Channels[i].buf_id = -1;
1048 Channels[i].vol = 0;
1053 // detect how many channels we can support
1055 ds_get_soundcard_caps(&caps);
1057 // caps.dwSize = sizeof(DSCAPS);
1058 // pDirectSound->GetCaps(&caps);
1060 // minimum 16 channels
1061 MAX_CHANNELS = caps.dwMaxHwMixingStaticBuffers;
1062 int dbg_channels = MAX_CHANNELS;
1063 if (MAX_CHANNELS < 16) {
1067 // allocate the channels array
1068 Channels = (channel*) malloc(sizeof(channel) * MAX_CHANNELS);
1069 if (Channels == NULL) {
1070 Error(LOCATION, "Unable to allocate %d bytes for %d audio channels.", sizeof(channel) * MAX_CHANNELS, MAX_CHANNELS);
1073 // init the channels
1074 for ( i = 0; i < MAX_CHANNELS; i++ ) {
1075 Channels[i].pdsb = NULL;
1076 Channels[i].pds3db = NULL;
1077 Channels[i].vol = 0;
1080 mprintf(("** MAX_CHANNELS set to %d. DS reported %d.\n", MAX_CHANNELS, dbg_channels));
1084 // ---------------------------------------------------------------------------------------
1085 // ds_init_software_buffers()
1087 // init the software buffers
1089 void ds_init_software_buffers()
1094 for ( i = 0; i < MAX_DS_SOFTWARE_BUFFERS; i++ ) {
1095 sound_buffers[i].buf_id = 0;
1096 sound_buffers[i].source_id = -1;
1101 for ( i = 0; i < MAX_DS_SOFTWARE_BUFFERS; i++ ) {
1102 ds_software_buffers[i].pdsb = NULL;
1107 // ---------------------------------------------------------------------------------------
1108 // ds_init_hardware_buffers()
1110 // init the hardware buffers
1112 void ds_init_hardware_buffers()
1115 // STUB_FUNCTION; // not needed with openal (CM)
1120 for ( i = 0; i < MAX_DS_HARDWARE_BUFFERS; i++ ) {
1121 ds_hardware_buffers[i].pdsb = NULL;
1126 // ---------------------------------------------------------------------------------------
1127 // ds_init_buffers()
1129 // init the both the software and hardware buffers
1131 void ds_init_buffers()
1133 ds_init_software_buffers();
1134 ds_init_hardware_buffers();
1137 // Get the current soundcard capabilities
1139 void ds_get_soundcard_caps(DSCAPS *dscaps)
1142 int n_hbuffers, hram;
1144 dscaps->dwSize = sizeof(DSCAPS);
1146 hr = pDirectSound->GetCaps(dscaps);
1148 nprintf(("Sound","SOUND ==> DirectSound GetCaps() failed with code %s\n.",get_DSERR_text(hr) ));
1152 n_hbuffers = dscaps->dwMaxHwMixingStaticBuffers;
1153 hram = dscaps->dwTotalHwMemBytes;
1155 if ( !(dscaps->dwFlags & DSCAPS_CERTIFIED) ) {
1156 nprintf(("Sound","SOUND ==> Warning: audio driver is not Microsoft certified.\n"));
1160 // ---------------------------------------------------------------------------------------
1163 // init the both the software and hardware buffers
1165 void ds_show_caps(DSCAPS *dscaps)
1167 nprintf(("Sound", "SOUND => Soundcard Capabilities:\n"));
1168 nprintf(("Sound", "================================\n"));
1169 nprintf(("Sound", "Number of primary buffers: %d\n", dscaps->dwPrimaryBuffers ));
1170 nprintf(("Sound", "Number of total hw mixing buffers: %d\n", dscaps->dwMaxHwMixingAllBuffers ));
1171 nprintf(("Sound", "Number of total hw mixing static buffers: %d\n", dscaps->dwMaxHwMixingStaticBuffers ));
1172 nprintf(("Sound", "Number of total hw mixing streaming buffers: %d\n", dscaps->dwMaxHwMixingStreamingBuffers ));
1173 nprintf(("Sound", "Number of free hw mixing buffers: %d\n", dscaps->dwFreeHwMixingAllBuffers ));
1174 nprintf(("Sound", "Number of free hw mixing static buffers: %d\n", dscaps->dwFreeHwMixingStaticBuffers ));
1175 nprintf(("Sound", "Number of free hw mixing streaming buffers: %d\n", dscaps->dwFreeHwMixingStreamingBuffers ));
1176 nprintf(("Sound", "Number of hw 3D buffers: %d\n", dscaps->dwMaxHw3DAllBuffers ));
1177 nprintf(("Sound", "Number of hw 3D static buffers: %d\n", dscaps->dwMaxHw3DStaticBuffers ));
1178 nprintf(("Sound", "Number of hw 3D streaming buffers: %d\n", dscaps->dwMaxHw3DStreamingBuffers ));
1179 nprintf(("Sound", "Number of free hw 3D buffers: %d\n", dscaps->dwFreeHw3DAllBuffers ));
1180 nprintf(("Sound", "Number of free hw static 3D buffers: %d\n", dscaps->dwFreeHw3DStaticBuffers ));
1181 nprintf(("Sound", "Number of free hw streaming 3D buffers: %d\n", dscaps->dwFreeHw3DStreamingBuffers ));
1182 nprintf(("Sound", "Number of total hw bytes: %d\n", dscaps->dwTotalHwMemBytes ));
1183 nprintf(("Sound", "Number of free hw bytes: %d\n", dscaps->dwFreeHwMemBytes ));
1184 nprintf(("Sound", "================================\n"));
1189 // Fill in the waveformat struct with the primary buffer characteristics.
1190 void ds_get_primary_format(WAVEFORMATEX *wfx)
1192 // Set 16 bit / 22KHz / mono
1193 wfx->wFormatTag = WAVE_FORMAT_PCM;
1195 wfx->nSamplesPerSec = 22050;
1196 wfx->wBitsPerSample = 16;
1198 wfx->nBlockAlign = (unsigned short)(wfx->nChannels * (wfx->wBitsPerSample / 8));
1199 wfx->nAvgBytesPerSec = wfx->nBlockAlign * wfx->nSamplesPerSec;
1203 // obtain the function pointers from the dsound.dll
1204 void ds_dll_get_functions()
1206 pfn_DirectSoundCreate = (HRESULT(__stdcall *)(LPGUID lpGuid, LPDIRECTSOUND *ppDS, IUnknown FAR *pUnkOuter))GetProcAddress(Ds_dll_handle,"DirectSoundCreate");
1207 pfn_DirectSoundCaptureCreate = (HRESULT(__stdcall *)(LPGUID lpGuid, LPDIRECTSOUNDCAPTURE *lplpDSC, IUnknown FAR *pUnkOuter))GetProcAddress(Ds_dll_handle,"DirectSoundCaptureCreate");
1211 // Load the dsound.dll, and get funtion pointers
1212 // exit: 0 -> dll loaded successfully
1213 // !0 -> dll could not be loaded
1219 if ( !Ds_dll_loaded ) {
1220 Ds_dll_handle = LoadLibrary("dsound.dll");
1221 if ( !Ds_dll_handle ) {
1224 ds_dll_get_functions();
1237 HINSTANCE a3d_handle;
1240 a3d_handle = LoadLibrary("a3d.dll");
1244 FreeLibrary(a3d_handle);
1248 Ds_must_call_couninitialize = 1;
1250 hr = CoCreateInstance(CLSID_A3d_Def, NULL, CLSCTX_INPROC_SERVER, IID_IDirectSound, (void**)&pDirectSound);
1255 Assert(pDirectSound != NULL);
1256 hr = pDirectSound->QueryInterface(IID_IA3d2_Def, (void**)&pIA3d2);
1261 A3DCAPS_SOFTWARE swCaps;
1263 // Get Dll Software CAP to get DLL version number
1264 ZeroMemory(&swCaps,sizeof(swCaps));
1266 swCaps.dwSize = sizeof(swCaps);
1267 pIA3d2->GetSoftwareCaps(&swCaps);
1269 // Compare version from a3d.dll to header version only return A3D_OK if dll version >= to header version
1270 if (swCaps.dwVersion < A3D_CURRENT_VERSION) {
1271 pDirectSound->Release();
1272 pDirectSound = NULL;
1277 // verify this is authentic A3D
1278 int aureal_verified;
1279 aureal_verified = VerifyAurealA3D();
1281 if (aureal_verified == FALSE) {
1282 // This is fake A3D!!! Ignore
1283 pDirectSound->Release();
1284 pDirectSound = NULL;
1288 // Register our version for backwards compatibility with newer A3d.dll
1289 pIA3d2->RegisterVersion(A3D_CURRENT_VERSION);
1291 hr = pDirectSound->Initialize(NULL);
1293 pDirectSound->Release();
1294 pDirectSound = NULL;
1298 pIA3d2->SetResourceManagerMode(A3D_RESOURCE_MODE_DYNAMIC_LOOPERS);
1304 // Initialize the property set interface.
1306 // returns: 0 if successful, otherwise -1. If successful, the global pPropertySet will
1307 // set to a non-NULL value.
1309 int ds_init_property_set()
1316 // Create the secondary buffer required for EAX initialization
1318 wf.wFormatTag = WAVE_FORMAT_PCM;
1320 wf.nSamplesPerSec = 22050;
1321 wf.wBitsPerSample = 16;
1323 wf.nBlockAlign = (unsigned short)(wf.nChannels * (wf.wBitsPerSample / 8));
1324 wf.nAvgBytesPerSec = wf.nBlockAlign * wf.nSamplesPerSec;
1327 ZeroMemory(&dsbd, sizeof(dsbd));
1328 dsbd.dwSize = sizeof(dsbd);
1329 dsbd.dwFlags = DSBCAPS_CTRLDEFAULT | DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_STATIC | DSBCAPS_CTRL3D | DSBCAPS_MUTE3DATMAXDISTANCE;
1330 dsbd.dwBufferBytes = 3 * wf.nAvgBytesPerSec;
1331 dsbd.lpwfxFormat = &wf;
1333 // Create a new buffer using the settings for this wave
1334 hr = pDirectSound->CreateSoundBuffer(&dsbd, &Ds_property_set_pdsb, NULL);
1336 pPropertySet = NULL;
1340 // Get the 3D interface from the secondary buffer, which is used to query the EAX interface
1341 hr = Ds_property_set_pdsb->QueryInterface(IID_IDirectSound3DBuffer, (void**)&Ds_property_set_pds3db);
1343 Ds_property_set_pds3db = NULL;
1347 Assert(Ds_property_set_pds3db != NULL);
1348 hr = Ds_property_set_pds3db->QueryInterface(IID_IKsPropertySet, (void**)&pPropertySet);
1349 if ((FAILED(hr)) || (pPropertySet == NULL)) {
1357 // ---------------------------------------------------------------------------------------
1360 // returns: -1 => init failed
1361 // 0 => init success
1362 int ds_init(int use_a3d, int use_eax)
1365 // NOTE: A3D and EAX are unused in OpenAL
1366 const ALubyte *initStr = (const ALubyte *)"\'( (sampling-rate 22050 ))";
1367 int attr[] = { ALC_FREQUENCY, 22050, ALC_SYNC, AL_FALSE, 0 };
1373 nprintf(( "Sound", "SOUND ==> Initializing OpenAL...\n" ));
1376 ds_sound_device = alcOpenDevice (initStr);
1378 // Create Sound Device
1379 ds_sound_context = alcCreateContext (ds_sound_device, attr);
1380 alcMakeContextCurrent (ds_sound_context);
1382 if (alcGetError(ds_sound_device) != ALC_NO_ERROR) {
1383 nprintf(("Sound", "SOUND ==> Couldn't initialize OpenAL\n"));
1387 OpenAL_ErrorCheck();
1389 // Initialize DirectSound3D. Since software performance of DirectSound3D is unacceptably
1390 // slow, we require the voice manger (a DirectSound extension) to be present. The
1391 // exception is when A3D is being used, since A3D has a resource manager built in.
1392 // if (Ds_use_ds3d && ds3d_init(0) != 0)
1395 ds_build_vol_lookup();
1401 WAVEFORMATEX wave_format;
1402 DSBUFFERDESC BufferDesc;
1404 nprintf(( "Sound", "SOUND ==> Initializing DirectSound...\n" ));
1406 hwnd = (HWND)os_get_window();
1407 if ( hwnd == NULL ) {
1408 nprintf(( "Sound", "SOUND ==> No window handle, so no sound...\n" ));
1412 if ( ds_dll_load() == -1 ) {
1416 pDirectSound = NULL;
1418 Ds_use_a3d = use_a3d;
1419 Ds_use_eax = use_eax;
1421 if (Ds_use_a3d || Ds_use_eax) {
1425 if (Ds_use_a3d && Ds_use_eax) {
1430 // If we want A3D, ensure a3d.dll exists
1431 if (Ds_use_a3d == 1) {
1432 if (ds_init_a3d() != 0) {
1439 if (Ds_use_a3d == 0) {
1440 if (!pfn_DirectSoundCreate) {
1441 nprintf(( "Sound", "SOUND ==> Could not get DirectSoundCreate function pointer\n" ));
1445 hr = pfn_DirectSoundCreate(NULL, &pDirectSound, NULL);
1451 // Set up DirectSound for exclusive mode, so we can change the primary buffer if we want to.
1452 hr = pDirectSound->SetCooperativeLevel(hwnd, DSSCL_EXCLUSIVE);
1454 nprintf(("Sound","SOUND ==> DirectSound pDirectSound->SetCooperativeLevel failed with code %s\n.",get_DSERR_text(hr) ));
1455 pDirectSound = NULL;
1459 // Create the primary buffer
1460 ZeroMemory(&BufferDesc, sizeof(BufferDesc));
1461 BufferDesc.dwSize = sizeof(BufferDesc);
1463 ds_get_soundcard_caps(&Soundcard_caps);
1466 BufferDesc.dwFlags = DSBCAPS_PRIMARYBUFFER | DSBCAPS_CTRL3D;
1468 hr = pDirectSound->CreateSoundBuffer(&BufferDesc, &pPrimaryBuffer, 0);
1470 nprintf(("Sound","SOUND ==> Primary Buffer create failed with DSBCAPS_CTRL3D property... disabling DirectSound3D\n"));
1475 nprintf(("Sound","SOUND ==> Primary Buffer created with DirectSound3D enabled\n"));
1479 // If not using DirectSound3D, then create a normal primary buffer
1480 if (Ds_use_ds3d == 0) {
1481 BufferDesc.dwFlags = DSBCAPS_PRIMARYBUFFER;
1482 hr = pDirectSound->CreateSoundBuffer(&BufferDesc, &pPrimaryBuffer, 0);
1484 nprintf(("Sound","SOUND ==> Primary Buffer create failed with error: %s\n",get_DSERR_text(hr) ));
1485 pDirectSound = NULL;
1489 nprintf(("Sound","SOUND ==> Primary Buffer created with without DirectSound3D enabled\n"));
1493 // Get the primary buffer format
1494 ds_get_primary_format(&wave_format);
1496 hr = pPrimaryBuffer->SetFormat(&wave_format);
1498 nprintf(("Sound","SOUND ==> pPrimaryBuffer->SetFormat() failed with code %s\n",get_DSERR_text(hr) ));
1501 pPrimaryBuffer->GetFormat(&wave_format, sizeof(wave_format), NULL);
1502 nprintf(("Sound","SOUND ==> Primary Buffer forced to: rate: %d Hz bits: %d n_channels: %d\n",
1503 wave_format.nSamplesPerSec, wave_format.wBitsPerSample, wave_format.nChannels));
1505 // start the primary buffer playing. This will reduce sound latency when playing a sound
1506 // if no other sounds are playing.
1507 hr = pPrimaryBuffer->Play(0, 0, DSBPLAY_LOOPING);
1509 nprintf(("Sound","SOUND ==> pPrimaryBuffer->Play() failed with code %s\n",get_DSERR_text(hr) ));
1512 // Initialize DirectSound3D. Since software performance of DirectSound3D is unacceptably
1513 // slow, we require the voice manger (a DirectSound extension) to be present. The
1514 // exception is when A3D is being used, since A3D has a resource manager built in.
1516 int vm_required = 1; // voice manager
1517 if (Ds_use_a3d == 1) {
1521 if (ds3d_init(vm_required) != 0) {
1527 if (Ds_use_eax == 1) {
1528 ds_init_property_set();
1529 if (ds_eax_init() != 0) {
1534 ds_build_vol_lookup();
1538 ds_show_caps(&Soundcard_caps);
1544 // ---------------------------------------------------------------------------------------
1547 // returns the text equivalent for the a DirectSound DSERR_ code
1549 char *get_DSERR_text(int DSResult)
1554 static char buf[20];
1555 snprintf(buf, 19, "unknown %d", DSResult);
1558 switch( DSResult ) {
1564 case DSERR_ALLOCATED:
1565 return "DSERR_ALLOCATED";
1568 case DSERR_ALREADYINITIALIZED:
1569 return "DSERR_ALREADYINITIALIZED";
1572 case DSERR_BADFORMAT:
1573 return "DSERR_BADFORMAT";
1576 case DSERR_BUFFERLOST:
1577 return "DSERR_BUFFERLOST";
1580 case DSERR_CONTROLUNAVAIL:
1581 return "DSERR_CONTROLUNAVAIL";
1585 return "DSERR_GENERIC";
1588 case DSERR_INVALIDCALL:
1589 return "DSERR_INVALIDCALL";
1592 case DSERR_INVALIDPARAM:
1593 return "DSERR_INVALIDPARAM";
1596 case DSERR_NOAGGREGATION:
1597 return "DSERR_NOAGGREGATION";
1600 case DSERR_NODRIVER:
1601 return "DSERR_NODRIVER";
1604 case DSERR_OUTOFMEMORY:
1605 return "DSERR_OUTOFMEMORY";
1608 case DSERR_OTHERAPPHASPRIO:
1609 return "DSERR_OTHERAPPHASPRIO";
1612 case DSERR_PRIOLEVELNEEDED:
1613 return "DSERR_PRIOLEVELNEEDED";
1616 case DSERR_UNINITIALIZED:
1617 return "DSERR_UNINITIALIZED";
1620 case DSERR_UNSUPPORTED:
1621 return "DSERR_UNSUPPORTED";
1632 // ---------------------------------------------------------------------------------------
1633 // ds_close_channel()
1635 // Free a single channel
1637 void ds_close_channel(int i)
1640 if(Channels[i].source_id != 0 && alIsSource (Channels[i].source_id)) {
1641 alSourceStop (Channels[i].source_id);
1642 alDeleteSources(1, &Channels[i].source_id);
1644 Channels[i].source_id = 0;
1651 // If a 3D interface exists, free it
1652 if ( Channels[i].pds3db != NULL ) {
1655 Channels[i].pds3db = NULL;
1658 while(++attempts < 10) {
1659 hr = Channels[i].pds3db->Release();
1660 if ( hr == DS_OK ) {
1663 // nprintf(("Sound", "SOUND ==> Channels[channel].pds3db->Release() failed with return value %s\n", get_DSERR_text(second_hr) ));
1667 Channels[i].pds3db = NULL;
1671 if ( Channels[i].pdsb != NULL ) {
1672 // If a 2D interface exists, free it
1673 if ( Channels[i].pdsb != NULL ) {
1675 while(++attempts < 10) {
1676 hr = Channels[i].pdsb->Release();
1677 if ( hr == DS_OK ) {
1680 nprintf(("Sound", "SOUND ==> Channels[channel].pdsb->Release() failed with return value %s\n", get_DSERR_text(hr) ));
1685 Channels[i].pdsb = NULL;
1692 // ---------------------------------------------------------------------------------------
1693 // ds_close_all_channels()
1695 // Free all the channel buffers
1697 void ds_close_all_channels()
1701 for (i = 0; i < MAX_CHANNELS; i++) {
1702 ds_close_channel(i);
1706 // ---------------------------------------------------------------------------------------
1707 // ds_unload_buffer()
1710 void ds_unload_buffer(int sid, int hid)
1714 ALuint buf_id = sound_buffers[sid].buf_id;
1716 if (buf_id != 0 && alIsBuffer(buf_id)) {
1717 alDeleteBuffers(1, &buf_id);
1720 sound_buffers[sid].buf_id = 0;
1730 if ( ds_software_buffers[sid].pdsb != NULL ) {
1731 hr = ds_software_buffers[sid].pdsb->Release();
1732 if ( hr != DS_OK ) {
1734 nprintf(("Sound", "SOUND ==> ds_software_buffers[sid]->Release() failed with return value %s\n", get_DSERR_text(hr) ));
1736 ds_software_buffers[sid].pdsb = NULL;
1741 if ( ds_hardware_buffers[hid].pdsb != NULL ) {
1742 hr = ds_hardware_buffers[hid].pdsb->Release();
1743 if ( hr != DS_OK ) {
1745 nprintf(("Sound", "SOUND ==> ds_hardware_buffers[hid]->Release() failed with return value %s\n", get_DSERR_text(hr) ));
1747 ds_hardware_buffers[hid].pdsb = NULL;
1753 // ---------------------------------------------------------------------------------------
1754 // ds_close_software_buffers()
1757 void ds_close_software_buffers()
1762 for (i = 0; i < MAX_DS_SOFTWARE_BUFFERS; i++) {
1763 ALuint buf_id = sound_buffers[i].buf_id;
1765 if (buf_id != 0 && alIsBuffer(buf_id)) {
1766 alDeleteBuffers(1, &buf_id);
1769 sound_buffers[i].buf_id = 0;
1775 for (i = 0; i < MAX_DS_SOFTWARE_BUFFERS; i++) {
1776 if ( ds_software_buffers[i].pdsb != NULL ) {
1777 hr = ds_software_buffers[i].pdsb->Release();
1778 if ( hr != DS_OK ) {
1780 nprintf(("Sound", "SOUND ==> ds_software_buffers[i]->Release() failed with return value %s\n", get_DSERR_text(hr) ));
1782 ds_software_buffers[i].pdsb = NULL;
1788 // ---------------------------------------------------------------------------------------
1789 // ds_close_hardware_buffers()
1792 void ds_close_hardware_buffers()
1800 for (i = 0; i < MAX_DS_HARDWARE_BUFFERS; i++) {
1801 if ( ds_hardware_buffers[i].pdsb != NULL ) {
1802 hr = ds_hardware_buffers[i].pdsb->Release();
1803 if ( hr != DS_OK ) {
1805 nprintf(("Sound", "SOUND ==> ds_hardware_buffers[i]->Release() failed with return value %s\n", get_DSERR_text(hr) ));
1807 ds_hardware_buffers[i].pdsb = NULL;
1813 // ---------------------------------------------------------------------------------------
1814 // ds_close_buffers()
1816 // Free the channel buffers
1818 void ds_close_buffers()
1820 ds_close_software_buffers();
1821 ds_close_hardware_buffers();
1824 // ---------------------------------------------------------------------------------------
1827 // Close the DirectSound system
1831 ds_close_all_channels();
1835 if (pPropertySet != NULL) {
1836 pPropertySet->Release();
1837 pPropertySet = NULL;
1840 if (Ds_property_set_pdsb != NULL) {
1841 Ds_property_set_pdsb->Release();
1842 Ds_property_set_pdsb = NULL;
1845 if (Ds_property_set_pds3db != NULL) {
1846 Ds_property_set_pds3db->Release();
1847 Ds_property_set_pds3db = NULL;
1850 if (pPrimaryBuffer) {
1851 pPrimaryBuffer->Release();
1852 pPrimaryBuffer = NULL;
1861 pDirectSound->Release();
1862 pDirectSound = NULL;
1865 if ( Ds_dll_loaded ) {
1866 FreeLibrary(Ds_dll_handle);
1870 if (Ds_must_call_couninitialize == 1) {
1875 // free the Channels[] array, since it was dynamically allocated
1880 // ---------------------------------------------------------------------------------------
1881 // ds_get_3d_interface()
1883 // Get the 3d interface for a secondary buffer.
1885 // If the secondary buffer wasn't created with a DSBCAPS_CTRL3D flag, then no 3d interface
1889 void ds_get_3d_interface(LPDIRECTSOUNDBUFFER pdsb, LPDIRECTSOUND3DBUFFER *ppds3db)
1894 dsbc.dwSize = sizeof(dsbc);
1895 DSResult = pdsb->GetCaps(&dsbc);
1896 if ( DSResult == DS_OK && dsbc.dwFlags & DSBCAPS_CTRL3D ) {
1897 DSResult = pdsb->QueryInterface( IID_IDirectSound3DBuffer, (void**)ppds3db );
1898 if ( DSResult != DS_OK ) {
1899 nprintf(("SOUND","Could not obtain 3D interface for hardware buffer: %s\n", get_DSERR_text(DSResult) ));
1906 // ---------------------------------------------------------------------------------------
1907 // ds_get_free_channel()
1909 // Find a free channel to play a sound on. If no free channels exists, free up one based
1910 // on volume levels.
1912 // input: new_volume => volume in DS units for sound to play at
1913 // snd_id => which kind of sound to play
1914 // priority => DS_MUST_PLAY
1919 // returns: channel number to play sound on
1920 // -1 if no channel could be found
1922 // NOTE: snd_id is needed since we limit the number of concurrent samples
1926 int ds_get_free_channel(int new_volume, int snd_id, int priority)
1929 int i, first_free_channel, limit;
1930 int lowest_vol = 0, lowest_vol_index = -1;
1931 int instance_count; // number of instances of sound already playing
1932 int lowest_instance_vol, lowest_instance_vol_index;
1937 lowest_instance_vol = 99;
1938 lowest_instance_vol_index = -1;
1939 first_free_channel = -1;
1941 // Look for a channel to use to play this sample
1942 for ( i = 0; i < MAX_CHANNELS; i++ ) {
1944 if ( chp->source_id == 0 ) {
1945 if ( first_free_channel == -1 )
1946 first_free_channel = i;
1950 alGetSourceiv(chp->source_id, AL_SOURCE_STATE, &status);
1952 OpenAL_ErrorCheck();
1954 if ( status != AL_PLAYING ) {
1955 if ( first_free_channel == -1 )
1956 first_free_channel = i;
1960 if ( chp->snd_id == snd_id ) {
1962 if ( chp->vol < lowest_instance_vol && chp->looping == FALSE ) {
1963 lowest_instance_vol = chp->vol;
1964 lowest_instance_vol_index = i;
1968 if ( chp->vol < lowest_vol && chp->looping == FALSE ) {
1969 lowest_vol_index = i;
1970 lowest_vol = chp->vol;
1975 // determine the limit of concurrent instances of this sound
1986 case DS_LIMIT_THREE:
1996 // If we've exceeded the limit, then maybe stop the duplicate if it is lower volume
1997 if ( instance_count >= limit ) {
1998 // If there is a lower volume duplicate, stop it.... otherwise, don't play the sound
1999 if ( lowest_instance_vol_index >= 0 && (Channels[lowest_instance_vol_index].vol <= new_volume) ) {
2000 first_free_channel = lowest_instance_vol_index;
2002 first_free_channel = -1;
2005 // there is no limit barrier to play the sound, so see if we've ran out of channels
2006 if ( first_free_channel == -1 ) {
2007 // stop the lowest volume instance to play our sound if priority demands it
2008 if ( lowest_vol_index != -1 && priority == DS_MUST_PLAY ) {
2009 // Check if the lowest volume playing is less than the volume of the requested sound.
2010 // If so, then we are going to trash the lowest volume sound.
2011 if ( Channels[lowest_vol_index].vol <= new_volume ) {
2012 first_free_channel = lowest_vol_index;
2018 return first_free_channel;
2020 int i, first_free_channel, limit;
2021 int lowest_vol = 0, lowest_vol_index = -1;
2022 int instance_count; // number of instances of sound already playing
2023 int lowest_instance_vol, lowest_instance_vol_index;
2024 unsigned long status;
2029 lowest_instance_vol = 99;
2030 lowest_instance_vol_index = -1;
2031 first_free_channel = -1;
2033 // Look for a channel to use to play this sample
2034 for ( i = 0; i < MAX_CHANNELS; i++ ) {
2036 if ( chp->pdsb == NULL ) {
2037 if ( first_free_channel == -1 )
2038 first_free_channel = i;
2042 hr = chp->pdsb->GetStatus(&status);
2043 if ( hr != DS_OK ) {
2044 nprintf(("Sound", "SOUND ==> GetStatus failed with return value %s\n", get_DSERR_text(hr) ));
2047 if ( !(status & DSBSTATUS_PLAYING) ) {
2048 if ( first_free_channel == -1 )
2049 first_free_channel = i;
2050 ds_close_channel(i);
2054 if ( chp->snd_id == snd_id ) {
2056 if ( chp->vol < lowest_instance_vol && chp->looping == FALSE ) {
2057 lowest_instance_vol = chp->vol;
2058 lowest_instance_vol_index = i;
2062 if ( chp->vol < lowest_vol && chp->looping == FALSE ) {
2063 lowest_vol_index = i;
2064 lowest_vol = chp->vol;
2069 // determine the limit of concurrent instances of this sound
2080 case DS_LIMIT_THREE:
2090 // If we've exceeded the limit, then maybe stop the duplicate if it is lower volume
2091 if ( instance_count >= limit ) {
2092 // If there is a lower volume duplicate, stop it.... otherwise, don't play the sound
2093 if ( lowest_instance_vol_index >= 0 && (Channels[lowest_instance_vol_index].vol <= new_volume) ) {
2094 ds_close_channel(lowest_instance_vol_index);
2095 first_free_channel = lowest_instance_vol_index;
2097 first_free_channel = -1;
2100 // there is no limit barrier to play the sound, so see if we've ran out of channels
2101 if ( first_free_channel == -1 ) {
2102 // stop the lowest volume instance to play our sound if priority demands it
2103 if ( lowest_vol_index != -1 && priority == DS_MUST_PLAY ) {
2104 // Check if the lowest volume playing is less than the volume of the requested sound.
2105 // If so, then we are going to trash the lowest volume sound.
2106 if ( Channels[lowest_vol_index].vol <= new_volume ) {
2107 ds_close_channel(lowest_vol_index);
2108 first_free_channel = lowest_vol_index;
2114 return first_free_channel;
2119 // ---------------------------------------------------------------------------------------
2122 // Find a free channel to play a sound on. If no free channels exists, free up one based
2123 // on volume levels.
2125 // returns: 0 => dup was successful
2126 // -1 => dup failed (Channels[channel].pdsb will be NULL)
2129 int ds_channel_dup(LPDIRECTSOUNDBUFFER pdsb, int channel, int use_ds3d)
2133 // Duplicate the master buffer into a channel buffer.
2134 DSResult = pDirectSound->DuplicateSoundBuffer(pdsb, &Channels[channel].pdsb );
2135 if ( DSResult != DS_OK ) {
2136 nprintf(("Sound", "SOUND ==> DuplicateSoundBuffer failed with return value %s\n", get_DSERR_text(DSResult) ));
2137 Channels[channel].pdsb = NULL;
2141 // get the 3d interface for the buffer if it exists
2143 if (Channels[channel].pds3db == NULL) {
2144 ds_get_3d_interface(Channels[channel].pdsb, &Channels[channel].pds3db);
2152 // ---------------------------------------------------------------------------------------
2153 // ds_restore_buffer()
2156 void ds_restore_buffer(LPDIRECTSOUNDBUFFER pdsb)
2160 Int3(); // get Alan, he wants to see this
2161 hr = pdsb->Restore();
2162 if ( hr != DS_OK ) {
2163 nprintf(("Sound", "Sound ==> Lost a buffer, tried restoring but got %s\n", get_DSERR_text(hr) ));
2168 // Create a direct sound buffer in software, without locking any data in
2169 int ds_create_buffer(int frequency, int bits_per_sample, int nchannels, int nseconds)
2175 if (!ds_initialized) {
2181 nprintf(("Sound","SOUND ==> No more OpenAL buffers available\n"));
2185 alGenBuffers (1, &i);
2187 sound_buffers[sid].buf_id = i;
2188 sound_buffers[sid].source_id = -1;
2189 sound_buffers[sid].frequency = frequency;
2190 sound_buffers[sid].bits_per_sample = bits_per_sample;
2191 sound_buffers[sid].nchannels = nchannels;
2192 sound_buffers[sid].nseconds = nseconds;
2201 if (!ds_initialized) {
2207 nprintf(("Sound","SOUND ==> No more software secondary buffers available\n"));
2211 // Set up buffer format
2212 wfx.wFormatTag = WAVE_FORMAT_PCM;
2213 wfx.nChannels = (unsigned short)nchannels;
2214 wfx.nSamplesPerSec = frequency;
2215 wfx.wBitsPerSample = (unsigned short)bits_per_sample;
2217 wfx.nBlockAlign = (unsigned short)(wfx.nChannels * (wfx.wBitsPerSample / 8));
2218 wfx.nAvgBytesPerSec = wfx.nBlockAlign * wfx.nSamplesPerSec;
2220 memset(&dsbd, 0, sizeof(DSBUFFERDESC));
2221 dsbd.dwSize = sizeof(DSBUFFERDESC);
2222 dsbd.dwBufferBytes = wfx.nAvgBytesPerSec * nseconds;
2223 dsbd.lpwfxFormat = &wfx;
2224 dsbd.dwFlags = DSBCAPS_STATIC | DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_CTRLDEFAULT | DSBCAPS_LOCSOFTWARE;
2226 dsrval = pDirectSound->CreateSoundBuffer(&dsbd, &ds_software_buffers[sid].pdsb, NULL);
2227 if ( dsrval != DS_OK ) {
2231 ds_software_buffers[sid].desc = dsbd;
2236 // Lock data into an existing buffer
2237 int ds_lock_data(int sid, unsigned char *data, int size)
2242 ALuint buf_id = sound_buffers[sid].buf_id;
2245 if (sound_buffers[sid].bits_per_sample == 16) {
2246 if (sound_buffers[sid].nchannels == 2) {
2247 format = AL_FORMAT_STEREO16;
2248 } else if (sound_buffers[sid].nchannels == 1) {
2249 format = AL_FORMAT_MONO16;
2253 } else if (sound_buffers[sid].bits_per_sample == 8) {
2254 if (sound_buffers[sid].nchannels == 2) {
2255 format = AL_FORMAT_STEREO8;
2256 } else if (sound_buffers[sid].nchannels == 1) {
2257 format = AL_FORMAT_MONO8;
2265 alBufferData(buf_id, format, data, size, sound_buffers[sid].frequency);
2267 OpenAL_ErrorCheck();
2272 LPDIRECTSOUNDBUFFER pdsb;
2274 void *buffer_data, *buffer_data2;
2275 DWORD buffer_size, buffer_size2;
2278 pdsb = ds_software_buffers[sid].pdsb;
2280 memset(&caps, 0, sizeof(DSBCAPS));
2281 caps.dwSize = sizeof(DSBCAPS);
2282 dsrval = pdsb->GetCaps(&caps);
2283 if ( dsrval != DS_OK ) {
2287 pdsb->SetCurrentPosition(0);
2289 // lock the entire buffer
2290 dsrval = pdsb->Lock(0, caps.dwBufferBytes, &buffer_data, &buffer_size, &buffer_data2, &buffer_size2, 0 );
2291 if ( dsrval != DS_OK ) {
2295 // first clear it out with silence
2296 memset(buffer_data, 0x80, buffer_size);
2297 memcpy(buffer_data, data, size);
2299 dsrval = pdsb->Unlock(buffer_data, buffer_size, 0, 0);
2300 if ( dsrval != DS_OK ) {
2308 // Stop a buffer from playing directly
2309 void ds_stop_easy(int sid)
2314 int cid = sound_buffers[sid].source_id;
2317 ALuint source_id = Channels[cid].source_id;
2319 alSourceStop(source_id);
2323 LPDIRECTSOUNDBUFFER pdsb;
2326 pdsb = ds_software_buffers[sid].pdsb;
2327 dsrval = pdsb->Stop();
2331 // Play a sound without the usual baggage (used for playing back real-time voice)
2334 // sid => software id of sound
2335 // volume => volume of sound effect in DirectSound units
2336 int ds_play_easy(int sid, int volume)
2339 if (!ds_initialized)
2342 int channel = ds_get_free_channel(volume, -1, DS_MUST_PLAY);
2345 ALuint source_id = Channels[channel].source_id;
2347 alSourceStop(source_id);
2349 if (Channels[channel].buf_id != sid) {
2350 ALuint buffer_id = sound_buffers[sid].buf_id;
2352 alSourcei(source_id, AL_BUFFER, buffer_id);
2354 OpenAL_ErrorCheck();
2357 Channels[channel].buf_id = sid;
2361 alSourcei(source_id, AL_LOOPING, AL_FALSE);
2362 alSourcePlay(source_id);
2364 OpenAL_ErrorCheck();
2372 LPDIRECTSOUNDBUFFER pdsb;
2375 pdsb = ds_software_buffers[sid].pdsb;
2377 pdsb->SetVolume(volume);
2378 dsrval=pdsb->Play(0, 0, 0);
2379 if ( dsrval != DS_OK ) {
2387 // ---------------------------------------------------------------------------------------
2388 // Play a DirectSound secondary buffer.
2392 // sid => software id of sound
2393 // hid => hardware id of sound ( -1 if not in hardware )
2394 // snd_id => what kind of sound this is
2395 // priority => DS_MUST_PLAY
2399 // volume => volume of sound effect in DirectSound units
2400 // pan => pan of sound in DirectSound units
2401 // looping => whether the sound effect is looping or not
2403 // returns: -1 => sound effect could not be started
2404 // >=0 => sig for sound effect successfully started
2406 int ds_play(int sid, int hid, int snd_id, int priority, int volume, int pan, int looping, bool is_voice_msg)
2411 if (!ds_initialized)
2414 channel = ds_get_free_channel(volume, snd_id, priority);
2417 if ( Channels[channel].source_id == 0 ) {
2421 if ( ds_using_ds3d() ) {
2425 Channels[channel].vol = volume;
2426 Channels[channel].looping = looping;
2427 Channels[channel].priority = priority;
2430 // Channels[channel].pdsb->SetPan(pan);
2433 // Channels[channel].pdsb->SetVolume(volume);
2435 Channels[channel].is_voice_msg = is_voice_msg;
2437 OpenAL_ErrorCheck();
2440 alGetSourceiv(Channels[channel].source_id, AL_SOURCE_STATE, &status);
2442 OpenAL_ErrorCheck();
2444 if (status == AL_PLAYING)
2445 alSourceStop(Channels[channel].source_id);
2447 OpenAL_ErrorCheck();
2449 alSourcei (Channels[channel].source_id, AL_BUFFER, sound_buffers[sid].buf_id);
2451 OpenAL_ErrorCheck();
2453 alSourcei (Channels[channel].source_id, AL_LOOPING, (looping) ? AL_TRUE : AL_FALSE);
2455 OpenAL_ErrorCheck();
2457 alSourcePlay(Channels[channel].source_id);
2459 OpenAL_ErrorCheck();
2461 sound_buffers[sid].source_id = channel;
2462 Channels[channel].buf_id = sid;
2465 // nprintf(( "Sound", "SOUND ==> Not playing sound requested at volume %.2f\n", ds_get_percentage_vol(volume) ));
2469 Channels[channel].snd_id = snd_id;
2470 Channels[channel].sig = channel_next_sig++;
2471 if (channel_next_sig < 0 ) {
2472 channel_next_sig = 1;
2475 Channels[channel].last_position = 0;
2477 // make sure there aren't any looping voice messages
2478 for (int i=0; i<MAX_CHANNELS; i++) {
2479 if (Channels[i].is_voice_msg == true) {
2480 if (Channels[i].source_id == 0) {
2485 // DWORD current_position = ds_get_play_position(i);
2486 // if (current_position != 0) {
2487 // if (current_position < Channels[i].last_position) {
2488 // ds_close_channel(i);
2490 // Channels[i].last_position = current_position;
2496 return Channels[channel].sig;
2501 if (!ds_initialized)
2504 channel = ds_get_free_channel(volume, snd_id, priority);
2507 if ( Channels[channel].pdsb != NULL ) {
2511 // First check if the sound is in hardware, and try to duplicate from there
2514 if ( ds_channel_dup(ds_hardware_buffers[hid].pdsb, channel, 0) == 0 ) {
2515 // nprintf(("Sound", "SOUND ==> Played sound in hardware..\n"));
2519 // Channel will be NULL if hardware dup failed, or there was no hardware dup attempted
2520 if ( Channels[channel].pdsb == NULL ) {
2521 if ( ds_channel_dup(ds_software_buffers[sid].pdsb, channel, 0) == 0 ) {
2522 // nprintf(("Sound", "SOUND ==> Played sound in software..\n"));
2526 if ( Channels[channel].pdsb == NULL ) {
2530 if ( ds_using_ds3d() ) {
2531 if ( ds_is_3d_buffer(Channels[channel].pdsb) ) {
2532 if (Channels[channel].pds3db == NULL) {
2533 ds_get_3d_interface(Channels[channel].pdsb, &Channels[channel].pds3db);
2535 if ( Channels[channel].pds3db ) {
2536 Channels[channel].pds3db->SetMode(DS3DMODE_DISABLE,DS3D_IMMEDIATE);
2542 Channels[channel].vol = volume;
2543 Channels[channel].looping = looping;
2544 Channels[channel].priority = priority;
2545 Channels[channel].pdsb->SetPan(pan);
2546 Channels[channel].pdsb->SetVolume(volume);
2547 Channels[channel].is_voice_msg = is_voice_msg;
2551 ds_flags |= DSBPLAY_LOOPING;
2553 DSResult = Channels[channel].pdsb->Play(0, 0, ds_flags );
2556 if (Stop_logging_sounds == false) {
2558 sprintf(buf, "channel %d, address: %x, ds_flags: %d", channel, Channels[channel].pdsb, ds_flags);
2559 HUD_add_to_scrollback(buf, 3);
2563 if ( DSResult == DSERR_BUFFERLOST ) {
2564 ds_restore_buffer(Channels[channel].pdsb);
2565 DSResult = Channels[channel].pdsb->Play(0, 0, ds_flags );
2568 if ( DSResult != DS_OK ) {
2569 nprintf(("Sound", "Sound ==> Play failed with return value %s\n", get_DSERR_text(DSResult) ));
2574 // nprintf(( "Sound", "SOUND ==> Not playing sound requested at volume %.2f\n", ds_get_percentage_vol(volume) ));
2578 Channels[channel].snd_id = snd_id;
2579 Channels[channel].sig = channel_next_sig++;
2580 if (channel_next_sig < 0 ) {
2581 channel_next_sig = 1;
2585 if (Stop_logging_sounds == false) {
2588 sprintf(buf, "VOICE sig: %d, sid: %d, snd_id: %d, ch: %d", Channels[channel].sig, sid, snd_id, channel);
2589 HUD_add_to_scrollback(buf, 3);
2594 Channels[channel].last_position = 0;
2596 // make sure there aren't any looping voice messages
2597 for (int i=0; i<MAX_CHANNELS; i++) {
2598 if (Channels[i].is_voice_msg == true) {
2599 if (Channels[i].pdsb == NULL) {
2603 DWORD current_position = ds_get_play_position(i);
2604 if (current_position != 0) {
2605 if (current_position < Channels[i].last_position) {
2606 ds_close_channel(i);
2608 Channels[i].last_position = current_position;
2614 return Channels[channel].sig;
2619 // ---------------------------------------------------------------------------------------
2622 // Return the channel number that is playing the sound identified by sig. If that sound is
2623 // not playing, return -1.
2625 int ds_get_channel(int sig)
2630 for ( i = 0; i < MAX_CHANNELS; i++ ) {
2631 if ( Channels[i].source_id && Channels[i].sig == sig ) {
2632 if ( ds_is_channel_playing(i) == TRUE ) {
2642 for ( i = 0; i < MAX_CHANNELS; i++ ) {
2643 if ( Channels[i].pdsb && Channels[i].sig == sig ) {
2644 if ( ds_is_channel_playing(i) == TRUE ) {
2653 // ---------------------------------------------------------------------------------------
2654 // ds_is_channel_playing()
2657 int ds_is_channel_playing(int channel)
2660 if ( Channels[channel].source_id != 0 ) {
2663 alGetSourceiv(Channels[channel].source_id, AL_SOURCE_STATE, &status);
2664 OpenAL_ErrorCheck();
2666 return (status == AL_PLAYING);
2672 unsigned long status;
2674 if ( !Channels[channel].pdsb ) {
2678 hr = Channels[channel].pdsb->GetStatus(&status);
2679 if ( hr != DS_OK ) {
2680 nprintf(("Sound", "SOUND ==> GetStatus failed with return value %s\n", get_DSERR_text(hr) ));
2684 if ( status & DSBSTATUS_PLAYING )
2691 // ---------------------------------------------------------------------------------------
2692 // ds_stop_channel()
2695 void ds_stop_channel(int channel)
2698 if ( Channels[channel].source_id != 0 ) {
2699 alSourceStop(Channels[channel].source_id);
2702 ds_close_channel(channel);
2706 // ---------------------------------------------------------------------------------------
2707 // ds_stop_channel_all()
2710 void ds_stop_channel_all()
2715 for ( i=0; i<MAX_CHANNELS; i++ ) {
2716 if ( Channels[i].source_id != 0 ) {
2717 alSourceStop(Channels[i].source_id);
2723 for ( i=0; i<MAX_CHANNELS; i++ ) {
2724 if ( Channels[i].pdsb != NULL ) {
2731 // ---------------------------------------------------------------------------------------
2734 // Set the volume for a channel. The volume is expected to be in DirectSound units
2736 // If the sound is a 3D sound buffer, this is like re-establishing the maximum
2739 void ds_set_volume( int channel, int vol )
2745 unsigned long status;
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 ) {
2754 Channels[channel].pdsb->SetVolume(vol);
2759 // ---------------------------------------------------------------------------------------
2762 // Set the pan for a channel. The pan is expected to be in DirectSound units
2764 void ds_set_pan( int channel, int pan )
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->SetPan(pan);
2784 // ---------------------------------------------------------------------------------------
2787 // Get the pitch of a channel
2789 int ds_get_pitch(int channel)
2796 unsigned long status, pitch = 0;
2799 hr = Channels[channel].pdsb->GetStatus(&status);
2801 if ( hr != DS_OK ) {
2802 nprintf(("Sound", "SOUND ==> GetStatus failed with return value %s\n", get_DSERR_text(hr) ));
2806 if ( status & DSBSTATUS_PLAYING ) {
2807 hr = Channels[channel].pdsb->GetFrequency(&pitch);
2808 if ( hr != DS_OK ) {
2809 nprintf(("Sound", "SOUND ==> GetFrequency failed with return value %s\n", get_DSERR_text(hr) ));
2818 // ---------------------------------------------------------------------------------------
2821 // Set the pitch of a channel
2823 void ds_set_pitch(int channel, int pitch)
2828 unsigned long status;
2831 hr = Channels[channel].pdsb->GetStatus(&status);
2832 if ( hr != DS_OK ) {
2833 nprintf(("Sound", "SOUND ==> GetStatus failed with return value %s\n", get_DSERR_text(hr) ));
2837 if ( pitch < MIN_PITCH )
2840 if ( pitch > MAX_PITCH )
2843 if ( status & DSBSTATUS_PLAYING ) {
2844 Channels[channel].pdsb->SetFrequency((unsigned long)pitch);
2849 // ---------------------------------------------------------------------------------------
2850 // ds_chg_loop_status()
2853 void ds_chg_loop_status(int channel, int loop)
2856 ALuint source_id = Channels[channel].source_id;
2858 alSourcei(source_id, AL_LOOPING, loop ? AL_TRUE : AL_FALSE);
2860 unsigned long status;
2863 hr = Channels[channel].pdsb->GetStatus(&status);
2864 if ( hr != DS_OK ) {
2865 nprintf(("Sound", "SOUND ==> GetStatus failed with return value %s\n", get_DSERR_text(hr) ));
2869 if ( !(status & DSBSTATUS_PLAYING) )
2870 return; // sound is not playing anymore
2872 if ( status & DSBSTATUS_LOOPING ) {
2874 return; // we are already looping
2876 // stop the sound from looping
2877 hr = Channels[channel].pdsb->Play(0,0,0);
2882 return; // the sound is already not looping
2884 // start the sound looping
2885 hr = Channels[channel].pdsb->Play(0,0,DSBPLAY_LOOPING);
2891 // ---------------------------------------------------------------------------------------
2894 // Starts a ds3d sound playing
2898 // sid => software id for sound to play
2899 // hid => hardware id for sound to play (-1 if not in hardware)
2900 // snd_id => identifies what type of sound is playing
2901 // pos => world pos of sound
2902 // vel => velocity of object emitting sound
2903 // min => distance at which sound doesn't get any louder
2904 // max => distance at which sound becomes inaudible
2905 // looping => boolean, whether to loop the sound or not
2906 // max_volume => volume (-10000 to 0) for 3d sound at maximum
2907 // estimated_vol => manual estimated volume
2908 // priority => DS_MUST_PLAY
2913 // returns: 0 => sound started successfully
2914 // -1 => sound could not be played
2916 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 )
2926 if (!ds_initialized)
2929 channel = ds_get_free_channel(estimated_vol, snd_id, priority);
2932 Assert(Channels[channel].pdsb == NULL);
2934 // First check if the sound is in hardware, and try to duplicate from there
2937 if ( ds_is_3d_buffer(ds_hardware_buffers[hid].pdsb) == FALSE ) {
2938 nprintf(("Sound", "SOUND ==> Tried to play non-3d buffer in ds3d_play()..\n"));
2942 if ( ds_channel_dup(ds_hardware_buffers[hid].pdsb, channel, 1) == 0 ) {
2943 nprintf(("Sound", "SOUND ==> Played sound using DirectSound3D in hardware..\n"));
2947 // Channel will be NULL if hardware dup failed, or there was no hardware dup attempted
2948 if ( Channels[channel].pdsb == NULL ) {
2951 if ( ds_is_3d_buffer(ds_software_buffers[sid].pdsb) == FALSE ) {
2952 nprintf(("Sound", "SOUND ==> Tried to play non-3d buffer in ds3d_play()..\n"));
2957 if ( ds_channel_dup(ds_software_buffers[sid].pdsb, channel, 1) == 0 ) {
2958 // nprintf(("Sound", "SOUND ==> Played sound using DirectSound3D \n"));
2962 if ( Channels[channel].pdsb == NULL ) {
2967 desc = ds_software_buffers[sid].desc;
2968 desc.lpwfxFormat = &ds_software_buffers[sid].wfx;
2970 // duplicate buffer failed, so call CreateBuffer instead
2972 hr = pDirectSound->CreateSoundBuffer(&desc, &Channels[channel].pdsb, NULL );
2974 if ( (hr == DS_OK) && (Channels[channel].pdsb) ) {
2975 BYTE *pdest, *pdest2;
2977 DWORD src_ds_size, dest_ds_size, not_used;
2980 if ( ds_get_size(sid, &src_size) != 0 ) {
2982 Channels[channel].pdsb->Release();
2986 // lock the src buffer
2987 hr = ds_software_buffers[sid].pdsb->Lock(0, src_size, (void**)&psrc, &src_ds_size, (void**)&psrc2, ¬_used, 0);
2988 if ( hr != DS_OK ) {
2989 mprintf(("err: %s\n", get_DSERR_text(hr)));
2991 Channels[channel].pdsb->Release();
2995 if ( Channels[channel].pdsb->Lock(0, src_ds_size, (void**)(&pdest), &dest_ds_size, (void**)&pdest2, ¬_used, 0) == DS_OK) {
2996 memcpy(pdest, psrc, src_ds_size);
2997 Channels[channel].pdsb->Unlock(pdest, dest_ds_size, 0, 0);
2998 ds_get_3d_interface(Channels[channel].pdsb, &Channels[channel].pds3db);
3000 Channels[channel].pdsb->Release();
3007 Assert(Channels[channel].pds3db );
3008 Channels[channel].pds3db->SetMode(DS3DMODE_NORMAL,DS3D_IMMEDIATE);
3010 // set up 3D sound data here
3011 ds3d_update_buffer(channel, i2fl(min), i2fl(max), pos, vel);
3013 Channels[channel].vol = estimated_vol;
3014 Channels[channel].looping = looping;
3016 // sets the maximum "inner cone" volume
3017 Channels[channel].pdsb->SetVolume(max_volume);
3021 ds_flags |= DSBPLAY_LOOPING;
3024 hr = Channels[channel].pdsb->Play(0, 0, ds_flags );
3026 if ( hr == DSERR_BUFFERLOST ) {
3027 ds_restore_buffer(Channels[channel].pdsb);
3028 hr = Channels[channel].pdsb->Play(0, 0, ds_flags );
3031 if ( hr != DS_OK ) {
3032 nprintf(("Sound", "Sound ==> Play failed with return value %s\n", get_DSERR_text(hr) ));
3033 if ( Channels[channel].pdsb ) {
3035 while(++attempts < 10) {
3036 hr = Channels[channel].pdsb->Release();
3037 if ( hr == DS_OK ) {
3040 nprintf(("Sound","SOUND ==> DirectSound Release() failed with code %s\n.",get_DSERR_text(hr) ));
3044 Channels[channel].pdsb = NULL;
3050 nprintf(( "Sound", "SOUND ==> Not playing requested 3D sound\n"));
3054 Channels[channel].snd_id = snd_id;
3055 Channels[channel].sig = channel_next_sig++;
3056 if (channel_next_sig < 0 ) {
3057 channel_next_sig = 1;
3059 return Channels[channel].sig;
3063 void ds_set_position(int channel, DWORD offset)
3068 // set the position of the sound buffer
3069 Channels[channel].pdsb->SetCurrentPosition(offset);
3073 DWORD ds_get_play_position(int channel)
3081 if ( Channels[channel].pdsb ) {
3082 Channels[channel].pdsb->GetCurrentPosition((LPDWORD)&play,(LPDWORD)&write);
3091 DWORD ds_get_write_position(int channel)
3099 if ( Channels[channel].pdsb ) {
3100 Channels[channel].pdsb->GetCurrentPosition((LPDWORD)&play,(LPDWORD)&write);
3109 int ds_get_channel_size(int channel)
3120 if ( Channels[channel].pdsb ) {
3121 memset(&caps, 0, sizeof(DSBCAPS));
3122 caps.dwSize = sizeof(DSBCAPS);
3123 dsrval = Channels[channel].pdsb->GetCaps(&caps);
3124 if ( dsrval != DS_OK ) {
3127 size = caps.dwBufferBytes;
3136 // Returns the number of channels that are actually playing
3137 int ds_get_number_channels()
3143 for ( i = 0; i < MAX_CHANNELS; i++ ) {
3144 if ( Channels[i].source_id ) {
3145 if ( ds_is_channel_playing(i) == TRUE ) {
3156 for ( i = 0; i < MAX_CHANNELS; i++ ) {
3157 if ( Channels[i].pdsb ) {
3158 if ( ds_is_channel_playing(i) == TRUE ) {
3168 // retreive raw data from a sound buffer
3169 int ds_get_data(int sid, char *data)
3177 LPDIRECTSOUNDBUFFER pdsb;
3183 pdsb = ds_software_buffers[sid].pdsb;
3185 memset(&caps, 0, sizeof(DSBCAPS));
3186 caps.dwSize = sizeof(DSBCAPS);
3187 dsrval = pdsb->GetCaps(&caps);
3188 if ( dsrval != DS_OK ) {
3192 // lock the entire buffer
3193 dsrval = pdsb->Lock(0, caps.dwBufferBytes, &buffer_data, &buffer_size, 0, 0, 0);
3194 if ( dsrval != DS_OK ) {
3198 memcpy(data, buffer_data, buffer_size);
3200 dsrval = pdsb->Unlock(buffer_data, buffer_size, 0, 0);
3201 if ( dsrval != DS_OK ) {
3209 // return the size of the raw sound data
3210 int ds_get_size(int sid, int *size)
3220 LPDIRECTSOUNDBUFFER pdsb;
3224 pdsb = ds_software_buffers[sid].pdsb;
3226 memset(&caps, 0, sizeof(DSBCAPS));
3227 caps.dwSize = sizeof(DSBCAPS);
3228 dsrval = pdsb->GetCaps(&caps);
3229 if ( dsrval != DS_OK ) {
3233 *size = caps.dwBufferBytes;
3242 // Return the primary buffer interface. Note that we cast to a uint to avoid
3243 // having to include dsound.h (and thus windows.h) in ds.h.
3245 uint ds_get_primary_buffer_interface()
3251 return (uint)pPrimaryBuffer;
3255 // Return the DirectSound Interface.
3257 uint ds_get_dsound_interface()
3263 return (uint)pDirectSound;
3267 uint ds_get_property_set_interface()
3272 return (uint)pPropertySet;
3276 // --------------------
3278 // EAX Functions below
3280 // --------------------
3282 // Set the master volume for the reverb added to all sound sources.
3284 // volume: volume, range from 0 to 1.0
3286 // returns: 0 if the volume is set successfully, otherwise return -1
3288 int ds_eax_set_volume(float volume)
3295 if (Ds_eax_inited == 0) {
3299 Assert(Ds_eax_reverb);
3301 CAP(volume, 0.0f, 1.0f);
3303 hr = Ds_eax_reverb->Set(DSPROPSETID_EAX_ReverbProperties_Def, DSPROPERTY_EAX_VOLUME, NULL, 0, &volume, sizeof(float));
3304 if (SUCCEEDED(hr)) {
3312 // Set the decay time for the EAX environment (ie all sound sources)
3314 // seconds: decay time in seconds
3316 // returns: 0 if decay time is successfully set, otherwise return -1
3318 int ds_eax_set_decay_time(float seconds)
3325 if (Ds_eax_inited == 0) {
3329 Assert(Ds_eax_reverb);
3331 CAP(seconds, 0.1f, 20.0f);
3333 hr = Ds_eax_reverb->Set(DSPROPSETID_EAX_ReverbProperties_Def, DSPROPERTY_EAX_DECAYTIME, NULL, 0, &seconds, sizeof(float));
3334 if (SUCCEEDED(hr)) {
3342 // Set the damping value for the EAX environment (ie all sound sources)
3344 // damp: damp value from 0 to 2.0
3346 // returns: 0 if the damp value is successfully set, otherwise return -1
3348 int ds_eax_set_damping(float damp)
3355 if (Ds_eax_inited == 0) {
3359 Assert(Ds_eax_reverb);
3361 CAP(damp, 0.0f, 2.0f);
3363 hr = Ds_eax_reverb->Set(DSPROPSETID_EAX_ReverbProperties_Def, DSPROPERTY_EAX_DAMPING, NULL, 0, &damp, sizeof(float));
3364 if (SUCCEEDED(hr)) {
3372 // Set up the environment type for all sound sources.
3374 // envid: value from the EAX_ENVIRONMENT_* enumeration in ds_eax.h
3376 // returns: 0 if the environment is set successfully, otherwise return -1
3378 int ds_eax_set_environment(unsigned long envid)
3385 if (Ds_eax_inited == 0) {
3389 Assert(Ds_eax_reverb);
3391 hr = Ds_eax_reverb->Set(DSPROPSETID_EAX_ReverbProperties_Def, DSPROPERTY_EAX_ENVIRONMENT, NULL, 0, &envid, sizeof(unsigned long));
3392 if (SUCCEEDED(hr)) {
3400 // Set up a predefined environment for EAX
3402 // envid: value from teh EAX_ENVIRONMENT_* enumeration
3404 // returns: 0 if successful, otherwise return -1
3406 int ds_eax_set_preset(unsigned long envid)
3413 if (Ds_eax_inited == 0) {
3417 Assert(Ds_eax_reverb);
3418 Assert(envid < EAX_ENVIRONMENT_COUNT);
3420 hr = Ds_eax_reverb->Set(DSPROPSETID_EAX_ReverbProperties_Def, DSPROPERTY_EAX_ALL, NULL, 0, &Ds_eax_presets[envid], sizeof(EAX_REVERBPROPERTIES));
3421 if (SUCCEEDED(hr)) {
3430 // Set up all the parameters for an environment
3432 // id: value from teh EAX_ENVIRONMENT_* enumeration
3433 // volume: volume for the environment (0 to 1.0)
3434 // damping: damp value for the environment (0 to 2.0)
3435 // decay: decay time in seconds (0.1 to 20.0)
3437 // returns: 0 if successful, otherwise return -1
3439 int ds_eax_set_all(unsigned long id, float vol, float damping, float decay)
3446 if (Ds_eax_inited == 0) {
3450 Assert(Ds_eax_reverb);
3451 Assert(id < EAX_ENVIRONMENT_COUNT);
3453 EAX_REVERBPROPERTIES er;
3455 er.environment = id;
3457 er.fDecayTime_sec = decay;
3458 er.fDamping = damping;
3460 hr = Ds_eax_reverb->Set(DSPROPSETID_EAX_ReverbProperties_Def, DSPROPERTY_EAX_ALL, NULL, 0, &er, sizeof(EAX_REVERBPROPERTIES));
3461 if (SUCCEEDED(hr)) {
3469 // Get up the parameters for the current environment
3471 // er: (output) hold environment parameters
3473 // returns: 0 if successful, otherwise return -1
3475 int ds_eax_get_all(EAX_REVERBPROPERTIES *er)
3481 unsigned long outsize;
3483 if (Ds_eax_inited == 0) {
3487 Assert(Ds_eax_reverb);
3489 hr = Ds_eax_reverb->Get(DSPROPSETID_EAX_ReverbProperties_Def, DSPROPERTY_EAX_ALL, NULL, 0, er, sizeof(EAX_REVERBPROPERTIES), &outsize);
3490 if (SUCCEEDED(hr)) {
3498 // Close down EAX, freeing any allocated resources
3503 if (Ds_eax_inited == 0) {
3513 // returns: 0 if initialization is successful, otherwise return -1
3519 unsigned long driver_support = 0;
3521 if (Ds_eax_inited) {
3525 Assert(Ds_eax_reverb == NULL);
3527 Ds_eax_reverb = (LPKSPROPERTYSET)ds_get_property_set_interface();
3528 if (Ds_eax_reverb == NULL) {
3532 // check if the listener property is supported by the audio driver
3533 hr = Ds_eax_reverb->QuerySupport(DSPROPSETID_EAX_ReverbProperties_Def, DSPROPERTY_EAX_ALL, &driver_support);
3535 nprintf(("Sound", "QuerySupport for the EAX Listener property set failed.. disabling EAX\n"));
3536 goto ds_eax_init_failed;
3539 if ((driver_support & (KSPROPERTY_SUPPORT_GET | KSPROPERTY_SUPPORT_SET)) != (KSPROPERTY_SUPPORT_GET | KSPROPERTY_SUPPORT_SET)) {
3540 goto ds_eax_init_failed;
3543 ds_eax_set_all(EAX_ENVIRONMENT_GENERIC, 0.0f, 0.0f, 0.0f);
3549 if (Ds_eax_reverb != NULL) {
3550 Ds_eax_reverb->Release();
3551 Ds_eax_reverb = NULL;
3560 int ds_eax_is_inited()
3565 return Ds_eax_inited;
3574 if (Ds_use_a3d == 0) {
3582 // Called once per game frame to make sure voice messages aren't looping
3588 for (int i=0; i<MAX_CHANNELS; i++) {
3590 if (cp->is_voice_msg) {
3591 if (cp->source_id == 0) {
3595 int current_position = ds_get_play_position(i);
3596 if (current_position != 0) {
3597 if (current_position < cp->last_position) {
3598 ds_close_channel(i);
3600 cp->last_position = current_position;
3613 int ds3d_update_buffer(int channel, float min, float max, vector *pos, vector *vel)
3620 int ds3d_update_listener(vector *pos, vector *vel, matrix *orient)
3625 ALfloat posv[] = { pos->x, pos->y, pos->z };
3626 ALfloat velv[] = { vel->x, vel->y, vel->z };
3627 ALfloat oriv[] = { orient->a1d[0],
3628 orient->a1d[1], orient->a1d[2],
3629 orient->a1d[3], orient->a1d[4],
3631 alListenerfv(AL_POSITION, posv);
3632 alListenerfv(AL_VELOCITY, velv);
3633 alListenerfv(AL_ORIENTATION, oriv);
3639 int ds3d_init (int unused)
3644 ALfloat pos[] = { 0.0, 0.0, 0.0 },
3645 vel[] = { 0.0, 0.0, 0.0 },
3646 ori[] = { 0.0, 0.0, 1.0, 0.0, -1.0, 0.0 };
3648 alListenerfv (AL_POSITION, pos);
3649 alListenerfv (AL_VELOCITY, vel);
3650 alListenerfv (AL_ORIENTATION, ori);
3652 if(alGetError() != AL_NO_ERROR)
3666 int dscap_create_buffer(int freq, int bits_per_sample, int nchannels, int nseconds)
3673 int dscap_get_raw_data(unsigned char *outbuf, unsigned int max_size)
3680 int dscap_max_buffersize()
3687 void dscap_release_buffer()
3692 int dscap_start_record()
3699 int dscap_stop_record()
3706 int dscap_supported()