File: s_ds3d.c

package info (click to toggle)
doomlegacy 1.32beta4-3
  • links: PTS
  • area: contrib
  • in suites: woody
  • size: 6,768 kB
  • ctags: 13,999
  • sloc: ansic: 110,665; asm: 1,413; makefile: 915; pascal: 63
file content (1068 lines) | stat: -rw-r--r-- 31,314 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
// Emacs style mode select   -*- C++ -*-
//-----------------------------------------------------------------------------
//
// $Id: s_ds3d.c,v 1.3 2001/08/21 21:50:06 judgecutor Exp $
//
// Copyright (C) 2001 by DooM Legacy Team.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
//
// $Log: s_ds3d.c,v $
// Revision 1.3  2001/08/21 21:50:06  judgecutor
// Fixed distance effect
//
// Revision 1.2  2001/04/06 21:36:53  judgecutor
// Added detection for speaker configuration
//
// Revision 1.1  2001/04/04 19:49:44  judgecutor
// Initial release
//
//
// DESCRIPTION:
//      General driver for 3D sound system.
//      Implementend via DirectSound3D API
//
//-----------------------------------------------------------------------------

#define INITGUID
#include <windows.h>
#include <cguid.h>
#include <dsound.h>


#define  _CREATE_DLL_
#include "../hw3dsdrv.h"
#include "../../m_fixed.h"

#undef DEBUG_TO_FILE
#define DEBUG_TO_FILE


// Internal sound stack
typedef struct stack_snd_s
{
    // Sound data
    LPDIRECTSOUNDBUFFER     dsbuffer;

    // 3D data of 3D source
    LPDIRECTSOUND3DBUFFER   dsbuffer3D;     // 3D data

    // Current parameters of 3D source
    // Valid only when source is 3D source
    // (dsbuffer3D is not NULL)
    DS3DBUFFER              parameters;

    // Currently unused
    int                     sfx_id;

    // Currently unused
    int                     LRU;

    // Flag of static source
    // Driver does not manage intrenally such sources
    int                     permanent;
    
} stack_t;

// Just for now...
#define SOUND_ALLOCATE_DELTA    16       // Amount of sources per stack incrementation
#define MAX_LRU                 16       // Maximum iterations to keep source in stack

static stack_t  *_stack;                 // Sound stack
static int      allocated_sounds;        // Size of stack

static int      srate;                   // Default sample rate


// output all debugging messages to this file
#ifdef DEBUG_TO_FILE
static HANDLE  logstream;
#endif

static LPDIRECTSOUND            DSnd            = NULL;  // Main DirectSound object
static LPDIRECTSOUNDBUFFER      PrimaryBuffer   = NULL;  // 
static LPDIRECTSOUND3DLISTENER  Listener        = NULL;  //

static DS3DLISTENER             listener_parms;          // Listener papameters

static BOOL                     virtualization;          // TRUE if HRTF virtualization enabled
static DWORD                    update_mode;             // Current update mode of listener
//static DWORD                    max_3d_buffers;

static I_Error_t I_ErrorDS3D = NULL;

//static stack_snd_t sound_stack[MAX_SOUNDS];

// Safe buffer release
#define RELEASE_BUFFER(buf)     {if (buf) { IDirectSoundBuffer_Release(buf); (buf) = NULL; }}
#define RELEASE_3DBUFFER(buf)   {if (buf) { IDirectSound3DBuffer_Release(buf); (buf) = NULL; }}

// Default flags for buffers
#define _2DSOURCE_FLAGS (DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME | DSBCAPS_STICKYFOCUS | DSBCAPS_STATIC)
#define _3DSOURCE_FLAGS (DSBCAPS_CTRL3D | DSBCAPS_STATIC | DSBCAPS_MUTE3DATMAXDISTANCE | DSBCAPS_CTRLVOLUME)


enum {IS_2DSOURCE = 0, IS_3DSOURCE = 1};

#define NEW_HANDLE  -1


BOOL APIENTRY DllMain( HANDLE hModule,      // handle to DLL module
                       DWORD fdwReason,     // reason for calling function
                       LPVOID lpReserved )  // reserved
{
    // Perform actions based on the reason for calling.
    switch( fdwReason )
    {
        case DLL_PROCESS_ATTACH:
         // Initialize once for each new process.
         // Return FALSE to fail DLL load.
#ifdef DEBUG_TO_FILE
            logstream = INVALID_HANDLE_VALUE;
            logstream = CreateFile ("s_ds3d.log", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
                                     FILE_ATTRIBUTE_NORMAL/*|FILE_FLAG_WRITE_THROUGH*/, NULL);
            if (logstream == INVALID_HANDLE_VALUE)
                return FALSE;
#endif
            break;

        case DLL_THREAD_ATTACH:
         // Do thread-specific initialization.
            break;

        case DLL_THREAD_DETACH:
         // Do thread-specific cleanup.
            break;

        case DLL_PROCESS_DETACH:
         // Perform any necessary cleanup.
#ifdef DEBUG_TO_FILE
            if ( logstream != INVALID_HANDLE_VALUE ) {
                CloseHandle ( logstream );
                logstream  = INVALID_HANDLE_VALUE;
            }
#endif
            break;
    }
    return TRUE;  // Successful DLL_PROCESS_ATTACH.
}

/***************************************************************
 *
 * DBG_Printf
 * Output error messages to debug log if DEBUG_TO_FILE is defined,
 * else do nothing
 *
 ***************************************************************
 */
void DBG_Printf (LPCTSTR lpFmt, ...)
{
#ifdef DEBUG_TO_FILE
    char    str[1999];
    va_list arglist;
    DWORD   bytesWritten;

    va_start  (arglist, lpFmt);
    vsprintf (str, lpFmt, arglist);
    va_end    (arglist);

    if ( logstream != INVALID_HANDLE_VALUE )
        WriteFile (logstream, str, lstrlen(str), &bytesWritten, NULL);
#endif
}


/***************************************************************
 *
 * Grow intrenal sound stack by SOUND_ALLOCATE_DELTA amount
 *
 ***************************************************************
 */
static BOOL reallocate_stack(void)
{
    stack_t*   new_stack;

    new_stack = realloc(_stack, sizeof(stack_t) * (allocated_sounds + SOUND_ALLOCATE_DELTA));
    if (new_stack)
    {
       _stack = new_stack;
       ZeroMemory(&_stack[allocated_sounds], SOUND_ALLOCATE_DELTA * sizeof(stack_t));
       allocated_sounds += SOUND_ALLOCATE_DELTA;
    }
    return (new_stack != NULL);
}


/***************************************************************
 *
 * Destroys source in stack
 *
 ***************************************************************
 */
static void kill_sound(stack_t *snd)
{
    //stack_t *snd = _stack + handle;
    
    if (snd->dsbuffer3D)
        RELEASE_3DBUFFER(snd->dsbuffer3D);
    
    if (snd->dsbuffer)
        RELEASE_BUFFER(snd->dsbuffer);

    ZeroMemory(snd, sizeof(stack_t));

}


/***************************************************************
 *
 * Returns TRUE if source currently playing
 *
 ***************************************************************
 */
BOOL is_playing(stack_t *snd)
{
    DWORD               status;
    LPDIRECTSOUNDBUFFER dsbuffer = snd->dsbuffer;

    if (dsbuffer == NULL)
        return 0;

	IDirectSoundBuffer_GetStatus(dsbuffer, &status);
    return (status & (DSBSTATUS_PLAYING | DSBSTATUS_LOOPING));

}


/***************************************************************
 *
 * Creates DirectSound buffer and fills with sound data
 * NULL sound data pointer are valid (empty buffer
 * will be created)
 *
 ***************************************************************
 */
static LPDIRECTSOUNDBUFFER create_buffer (void *data, int length, BOOL as3d)
{
    LPDIRECTSOUNDBUFFER     dsbuffer;
    HRESULT                 hr;
    WAVEFORMATEX            wfm;
    DSBUFFERDESC            dsbdesc;
    LPVOID                  lpvAudio1;              // receives address of lock start
    DWORD                   dwBytes1;               // receives number of bytes locked
    LPVOID                  lpvAudio2;              // receives address of lock start
    DWORD                   dwBytes2;               // receives number of bytes locked

    ZeroMemory (&wfm, sizeof(WAVEFORMATEX));
    wfm.wFormatTag = WAVE_FORMAT_PCM;
    wfm.nChannels = 1;
    wfm.nSamplesPerSec = data?*((unsigned short*)data+1):srate;      //mostly 11025, but some at 22050.
    wfm.wBitsPerSample = 8;
    wfm.nBlockAlign = wfm.wBitsPerSample / 8 * wfm.nChannels;
    wfm.nAvgBytesPerSec = wfm.nSamplesPerSec * wfm.nBlockAlign;

    // Set up DSBUFFERDESC structure.
    ZeroMemory (&dsbdesc, sizeof(DSBUFFERDESC) );
    dsbdesc.dwSize = sizeof (DSBUFFERDESC);
    dsbdesc.dwFlags = as3d?_3DSOURCE_FLAGS:_2DSOURCE_FLAGS;
    dsbdesc.dwBufferBytes = length;
    dsbdesc.lpwfxFormat = &wfm;

    // DirectX 7.0 and above!
    // Try to enable full HRTF virtualization algorithm for 
    // two-speakers or headphones
    if (as3d) 
        dsbdesc.guid3DAlgorithm = (virtualization?DS3DALG_HRTF_FULL:DS3DALG_DEFAULT);

    hr = IDirectSound_CreateSoundBuffer (DSnd, &dsbdesc, &dsbuffer, NULL);

    // CreateSoundBuffer might return DS_NO_VIRTUALIZATION so uses FAILED
    // macro rather than check explictly for DS_OK value
    if (FAILED(hr))
    {
        DBG_Printf("CreateSoundBuffer FAILED. Code %d\n", hr);
        return NULL;
    }

    if (data)
    {
        hr = IDirectSoundBuffer_Lock (dsbuffer, 0, length, &lpvAudio1, &dwBytes1, &lpvAudio2, &dwBytes2, 0);

        // If DSERR_BUFFERLOST is returned, restore and retry lock. 
        if (hr == DSERR_BUFFERLOST) 
        { 
            hr = IDirectSoundBuffer_Restore (dsbuffer);
            if( FAILED (hr) )
                I_ErrorDS3D("Restore fail on %x, code %d\n",dsbuffer, hr);
            hr = IDirectSoundBuffer_Lock (dsbuffer, 0, length, &lpvAudio1, &dwBytes1, NULL, NULL, 0);
            if( FAILED (hr) )
                I_ErrorDS3D("Lock fail(2) on %x, code %d\n",dsbuffer, hr);
        }
        else
            if( FAILED (hr) )
                I_ErrorDS3D("Lock fail(1) on %x, code %d\n",dsbuffer, hr);

        // copy wave data into the buffer (note: dwBytes1 should equal to dsbdesc->dwBufferBytes ...)
        CopyMemory (lpvAudio1, (byte*)data+8, dwBytes1);

        if ( dwBytes2 && lpvAudio2)  
            CopyMemory(lpvAudio2, ((byte*)data + 8) + dwBytes1, dwBytes2); 

    
        // finally, unlock the buffer
        hr = IDirectSoundBuffer_Unlock (dsbuffer, lpvAudio1, dwBytes1, lpvAudio2, dwBytes2);

        if( FAILED (hr) )
            I_ErrorDS3D("Unlock fail on %x, code %d\n",dsbuffer, hr);
    }

    return dsbuffer;
}


/***************************************************************
 *
 * Creates 3D source data buffer
 *
 ***************************************************************
 */
static LPDIRECTSOUND3DBUFFER create_3dbuffer(LPDIRECTSOUNDBUFFER dsbuffer, LPDIRECTSOUND3DBUFFER source3d)
{
    HRESULT hr;

    hr = IDirectSoundBuffer_QueryInterface(dsbuffer,
            &IID_IDirectSound3DBuffer, (void **)&source3d);

    if (FAILED(hr))
    {
        DBG_Printf("Couldn't obtain 3D Buffer interface. Code %d\n", hr);
        return NULL;
    }

    if (hr == DS_NO_VIRTUALIZATION)
    {
        DBG_Printf("The 3D virtualization not supported under this OS.\n");
            virtualization = FALSE;
    }
    return source3d;
}


/***************************************************************
 *
 * Returns free (unused) source stack slot
 * If none available sound stack will be increased
 *
 ***************************************************************
 */
static int find_handle(int new_sfx_id, int new_is3d)
{
	int			    free_sfx;
	stack_t         *snd;
    
    
    // At first do look for sound with same sfx ID and reuse it
    for (free_sfx = 0, snd = _stack; free_sfx < allocated_sounds; snd++, free_sfx++)
    {
        
        if (snd->permanent)
            continue;
/*
        if (!is_playing(snd)) 
        {
            if (same_sfx == 0 && snd->sfx_id == new_sfx_id && snd->dsbuffer)
            {
                same_sfx = i;
                continue;
            }

            if (snd->sfx_id && ++snd->LRU >= MAX_LRU)
                kill_sound(snd);
        }
*/
        if (snd->dsbuffer == 0)
            break;
            //free_sfx = i;

    }

    // No suitable resource found so increase sound stack
    if (free_sfx == allocated_sounds)
    {
        DBG_Printf("No free or same sfx found so increase stack (currently %d srcs)\n", allocated_sounds);
        free_sfx = reallocate_stack() ? free_sfx : -1;
    };
   
    return free_sfx;
        
}


/***************************************************************
 *
 * Recalculates volume from Doom scale to DirectSound scale
 *
 ***************************************************************
 */
static int recalc_volume(int base_vol, int steps)
{
    return (base_vol * ((DSBVOLUME_MAX-DSBVOLUME_MIN)/4)) / steps
           + (DSBVOLUME_MAX - ((DSBVOLUME_MAX-DSBVOLUME_MIN)/4));
}


/***************************************************************
 *
 * Updates sound volume of the DirectSound buffer
 *
 ***************************************************************
 */
static void UpdateSoundVolume (LPDIRECTSOUNDBUFFER lpSnd, int volume)
{
    /*volume = (volume * ((DSBVOLUME_MAX-DSBVOLUME_MIN)/4)) / 256 +
                        (DSBVOLUME_MAX - ((DSBVOLUME_MAX-DSBVOLUME_MIN)/4));

    IDirectSoundBuffer_SetVolume (lpSnd, volume);*/
    IDirectSoundBuffer_SetVolume (lpSnd, recalc_volume(volume, 256));
}


// --------------------------------------------------------------------------
// Update the panning for a secondary buffer, make sure it was created with
// DSBCAPS_CTRLPAN
// --------------------------------------------------------------------------
#define DSBPAN_RANGE    (DSBPAN_RIGHT-(DSBPAN_LEFT))

//Doom sounds pan range 0-255 (128 is centre)
#define SEP_RANGE       256             
static void Update2DSoundPanning (LPDIRECTSOUNDBUFFER lpSnd, int sep)
{
    HRESULT hr;
    hr = IDirectSoundBuffer_SetPan (lpSnd, (sep * DSBPAN_RANGE)/SEP_RANGE - DSBPAN_RIGHT);
    //if (FAILED(hr))
    //    CONS_Printf ("SetPan FAILED for sep %d pan %d\n", sep, (sep * DSBPAN_RANGE)/SEP_RANGE - DSBPAN_RIGHT);
}



/******************************************************************************
 *
 * Initialise driver and listener
 *
 *****************************************************************************/
EXPORT BOOL HWRAPI( Startup ) (I_Error_t FatalErrorFunction, snddev_t *snd_dev)
{
    HRESULT         hr;
    DSBUFFERDESC    desc;
    WAVEFORMATEX    wfm;
    DSCAPS          dscaps;
    DWORD           speakers;
    DWORD           speaker_config;
    DWORD           speaker_geometry;
    
    I_ErrorDS3D = FatalErrorFunction;
    DBG_Printf ("S_DS3D Init(): DirectSound3D driver for Doom Legacy v%d.%d\n", VERSION / 100, VERSION % 100);

    DBG_Printf("Initialising DirectSound3D...\n");
    hr = DirectSoundCreate( NULL, &DSnd, NULL);
    if (FAILED(hr))
    {
        DBG_Printf("Failed to obtain DirectSound\n");
        return FALSE;
    }

    hr = IDirectSound_SetCooperativeLevel(DSnd, snd_dev->hWnd, snd_dev->cooplevel);
    if (FAILED (hr))
    {
        DBG_Printf("Couldn't set coopertive level\n");
        return FALSE;
    }

    dscaps.dwSize = sizeof(DSCAPS);

    IDirectSound_GetCaps(DSnd, &dscaps);
    IDirectSound_GetSpeakerConfig(DSnd, &speakers);

    DBG_Printf("Sound hardware capabilities:\n");
    DBG_Printf("   Driver is %scertified.\n", (dscaps.dwFlags & DSCAPS_CERTIFIED) == DSCAPS_CERTIFIED?"":"not ");
    DBG_Printf("   Maximum hardware mixing buffers %d\n", dscaps.dwMaxHwMixingAllBuffers);
    DBG_Printf("   Maximum hardware 3D buffers %d\n", dscaps.dwFreeHw3DAllBuffers);

    speaker_config = DSSPEAKER_CONFIG(speakers);
    speaker_geometry = DSSPEAKER_GEOMETRY(speakers);

    DBG_Printf("Current speaker configuration: ");

    switch (speaker_config)
    {
        case DSSPEAKER_5POINT1:
            DBG_Printf("5.1 (5 speakers with subwoofer).\n");
            break;

        case DSSPEAKER_HEADPHONE:
            DBG_Printf("headphone.\n");
            break;

        case DSSPEAKER_MONO:
            DBG_Printf("single speaker (mono).\n");
            break;

        case DSSPEAKER_QUAD:
            DBG_Printf("quadrophonic\n");
            break;

        case DSSPEAKER_SURROUND:
            DBG_Printf("surround.\n");
            break;

        case DSSPEAKER_STEREO:
            DBG_Printf("stereo with %s geometry ",
                speaker_geometry == DSSPEAKER_GEOMETRY_WIDE
                    ? "wide (arc of 20 deg.)"
                    : speaker_geometry == DSSPEAKER_GEOMETRY_NARROW
                        ? "narrow (arc of 10 deg.)"
                        : speaker_geometry == DSSPEAKER_GEOMETRY_MIN
                            ? "min (arc of 5 deg.)"
                            : speaker_geometry == DSSPEAKER_GEOMETRY_MAX
                                ? "max (arc of 180 deg.)"
                                : "unknown"); 
            break;
        default:
            DBG_Printf("undetectable.\n");

    }

    update_mode = DS3D_IMMEDIATE;

    // Create primary sound buffer
    ZeroMemory(&desc, sizeof(DSBUFFERDESC));
    desc.dwSize = sizeof(DSBUFFERDESC);
    desc.dwFlags = DSBCAPS_CTRL3D | DSBCAPS_PRIMARYBUFFER | DSBCAPS_CTRLVOLUME;
    desc.dwBufferBytes = 0;
    desc.lpwfxFormat = NULL;

    hr = IDirectSound_CreateSoundBuffer(DSnd, &desc, &PrimaryBuffer, NULL);
    if (FAILED(hr))
    {
        DBG_Printf("CreateSoundBuffer FAILED (ErrNo %d)\n", hr);
        return FALSE;
    }

    // Query for 3D Listener object
    hr = IDirectSoundBuffer_QueryInterface(PrimaryBuffer,
            &IID_IDirectSound3DListener, (void **)&Listener); 
            
    if (FAILED( hr ) )
    {
        DBG_Printf("Couldn't obtain 3D Listener interface (ErrNo %d)\n", hr);
        return FALSE;
    }

    // Set up initial listsner parameters
    IDirectSound3DListener_SetDistanceFactor(Listener, (float)1/(float)72.0, DS3D_IMMEDIATE);
    //IDirectSound3DListener_SetRolloffFactor(Listener, DS3D_MAXROLLOFFFACTOR, DS3D_IMMEDIATE);
    IDirectSound3DListener_SetRolloffFactor(Listener, 1.7, DS3D_IMMEDIATE);
    listener_parms.dwSize = sizeof(DS3DLISTENER);
    IDirectSound3DListener_GetAllParameters(Listener, &listener_parms);
    
    ZeroMemory (&wfm, sizeof(WAVEFORMATEX));
    wfm.wFormatTag = WAVE_FORMAT_PCM;
    wfm.nChannels = 2;
    wfm.nSamplesPerSec = srate = snd_dev->sample_rate;
    wfm.wBitsPerSample = snd_dev->bps;
    wfm.nBlockAlign = wfm.wBitsPerSample / 8 * wfm.nChannels;
    wfm.nAvgBytesPerSec = wfm.nSamplesPerSec * wfm.nBlockAlign;

    if (snd_dev->cooplevel >= DSSCL_PRIORITY)
    {
        hr = IDirectSoundBuffer_SetFormat (PrimaryBuffer, &wfm);
        if (FAILED(hr))
            DBG_Printf ("Couldn't set primary buffer format.\n");
        
        DBG_Printf (" Compacting onboard sound-memory...");
        hr = IDirectSound_Compact (DSnd);
        DBG_Printf (" %s\n", SUCCEEDED(hr) ? "done" : "FAILED");
    }

    // Initially enables HRTF virtualization (may be changed later)
    virtualization = FALSE;

	_stack = NULL;
	allocated_sounds = 0;
    IDirectSoundBuffer_Play(PrimaryBuffer, 0, 0, DSBPLAY_LOOPING);
    return reallocate_stack();
}



/***************************************************************
 *
 * Shutdown driver
 *
 ***************************************************************
 */
EXPORT void HWRAPI( Shutdown ) (void)
{
    int     i;

    DBG_Printf ("S_DS3D Shutdown()\n");

    for (i = 0; i < allocated_sounds; i++)
    {
        StopSource(i);
        kill_sound(_stack + i);
    }

    if (_stack)
        free(_stack);

    if (Listener)
    {
        IDirectSound3DListener_Release(Listener);
        Listener = NULL;
    }

    if (PrimaryBuffer)
        RELEASE_BUFFER(PrimaryBuffer);

    if (DSnd)
    {
        IDirectSound_Release(DSnd);
        DSnd = NULL;
    }
    
}



EXPORT int HWRAPI (IsPlaying) (int handle)
{
    if (handle < 0 || handle >= allocated_sounds)
        return FALSE;

    return is_playing(_stack + handle);
}



static stack_t *setup_source(int handle, sfx_data_t *sfx, BOOL is_3dsource)
{
    stack_t                 *snd;
    //int                     handle;
    int                     data_length;
    LPDIRECTSOUNDBUFFER     dsbuffer = NULL;
    LPDIRECTSOUND3DBUFFER   ds3dbuffer = NULL;

    if (handle == NEW_HANDLE)
        handle = find_handle(sfx?sfx->id:0, is_3dsource);

    snd = _stack + handle;

    // Check for reused source
    if (snd->dsbuffer)
        return snd;

    data_length = sfx?sfx->length - 8:DSBSIZE_MIN;

    dsbuffer = create_buffer(sfx ? sfx->data : NULL, data_length, is_3dsource);
    if (dsbuffer)
    {
        if (is_3dsource)
        {
            ds3dbuffer = create_3dbuffer(dsbuffer, ds3dbuffer);
            if (!ds3dbuffer)
            {
                RELEASE_BUFFER(dsbuffer);
                return NULL;
            }
            snd->parameters.dwSize = sizeof(DS3DBUFFER);
            IDirectSound3DBuffer_GetAllParameters(ds3dbuffer, &snd->parameters);

        }
        snd->dsbuffer = dsbuffer;
        snd->dsbuffer3D = ds3dbuffer;
        snd->LRU = 0;
        snd->sfx_id = sfx ? sfx->id : 0;
        if (!is_3dsource)
            snd->permanent = 0;
        return snd;
    }
    return NULL;

}

/******************************************************************************
 *
 * Creates 2D (stereo) source
 *
 ******************************************************************************/
EXPORT int HWRAPI ( Add2DSource ) (source2D_data_t *src, sfx_data_t *sfx)
{
    stack_t *snd;

    snd = setup_source(NEW_HANDLE, sfx, IS_2DSOURCE);
    if (snd)
    {
        UpdateSoundVolume(snd->dsbuffer, src->volume);
        Update2DSoundPanning(snd->dsbuffer, src->sep);
    }
    return snd - _stack;
}

/******************************************************************************
 *
 * Creates 3D source
 *
 ******************************************************************************/

EXPORT int HWRAPI ( Add3DSource ) (source3D_data_t *src, sfx_data_t *sfx)
{
    stack_t                 *snd;
   
    snd = setup_source(NEW_HANDLE, sfx, IS_3DSOURCE);
    
    if (snd)
    {
     
        /*x = src->pos.x;
        y = src->pos.z;
        z = src->pos.y;

        IDirectSound3DBuffer_SetPosition(source, x, y, z, update_mode);
        IDirectSound3DBuffer_SetVelocity(source, src->pos.momx, src->pos.momz, src->pos.momy, update_mode);
        IDirectSound3DBuffer_SetMode(source, src->head_relative?DS3DMODE_HEADRELATIVE:DS3DMODE_NORMAL, update_mode);*/

        snd->parameters.vPosition.x = src->pos.x;
        snd->parameters.vPosition.y = src->pos.z;
        snd->parameters.vPosition.z = src->pos.y;

        snd->parameters.vVelocity.x = src->pos.momx;
        snd->parameters.vVelocity.y = src->pos.momz;
        snd->parameters.vVelocity.z = src->pos.momy;

        snd->parameters.dwMode = src->head_relative ? DS3DMODE_HEADRELATIVE : DS3DMODE_NORMAL;

        snd->parameters.flMinDistance = src->min_distance;
        snd->parameters.flMaxDistance = src->max_distance;
        //snd->parameters.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;

        snd->permanent = src->permanent;

        UpdateSoundVolume(snd->dsbuffer, src->volume);
        IDirectSound3DBuffer_SetAllParameters(snd->dsbuffer3D, &snd->parameters, DS3D_IMMEDIATE);
        
        IDirectSoundBuffer_SetCurrentPosition(snd->dsbuffer, 0);
            
    }
        
    return (snd - _stack);
}

/******************************************************************************
 *
 * Destroy source and remove it from stack if it is a 2D source.
 * Otherwise put source into cache
 *
 *****************************************************************************/
EXPORT void HWRAPI (KillSource) (int handle)
{

    if (handle < 0 || handle >= allocated_sounds)
        return;
/*
     if (_stack[handle].dsbuffer3D)
    {
        // It's a 3D source so let him chance to be reused :-)
        _stack[handle].LRU = 1;
    }
    else
    { */
        // No, it is a 2D source so kill him
        kill_sound(_stack + handle);
    //}
}


/******************************************************************************
 *
 * Update volume and separation (panning) of 2D source
 *
 *****************************************************************************/
EXPORT void HWRAPI (Update2DSoundParms) (int handle, int vol, int sep)
{
    LPDIRECTSOUNDBUFFER dsbuffer;

    if (handle < 0 || handle >= allocated_sounds)
        return;

    if ((_stack+handle)->dsbuffer3D)
        return;
    dsbuffer = (_stack + handle)->dsbuffer;
    UpdateSoundVolume(dsbuffer, vol);
    Update2DSoundPanning(dsbuffer, sep);
}



// --------------------------------------------------------------------------
// Set the global volume for sound effects
// --------------------------------------------------------------------------
EXPORT void HWRAPI (SetGlobalSfxVolume) (int volume)
{
    int     vol;
    HRESULT hr;
   
    // use the last quarter of volume range
    if (volume)
        vol = recalc_volume(volume, 31);
    else
        vol = DSBVOLUME_MIN;
    
    hr = IDirectSoundBuffer_SetVolume (PrimaryBuffer, vol);
    
}

EXPORT void HWRAPI ( StopSource) (int handle)
{
    LPDIRECTSOUNDBUFFER dsbuffer;
    
    if (handle < 0 || handle >= allocated_sounds)
        return;
    dsbuffer = (_stack + handle)->dsbuffer;

    if (dsbuffer)
    {
        IDirectSoundBuffer_Stop(dsbuffer);
        IDirectSoundBuffer_SetCurrentPosition(dsbuffer, 0);
    }
}

EXPORT int HWRAPI ( GetHW3DSVersion) (void)
{
    return VERSION;
}


EXPORT void HWRAPI (BeginFrameUpdate) (void)
{
    update_mode = DS3D_DEFERRED;
}

EXPORT void HWRAPI (EndFrameUpdate) (void)
{
    if (update_mode == DS3D_DEFERRED)
        IDirectSound3DListener_CommitDeferredSettings(Listener);

    update_mode = DS3D_IMMEDIATE;
}


/******************************************************************************
 * UpdateListener
 *
 * Set up main listener properties:
 * - position
 * - orientation
 * - velocity
 *****************************************************************************/
EXPORT void HWRAPI (UpdateListener) (listener_data_t* data)
{
    D3DVECTOR pos;
    D3DVECTOR face;
    D3DVECTOR head;
    D3DVECTOR velocity;
    
    double      f_angle;
    //double      h_angle, t_angle;
    //double      f_cos, f_sin, t_sin;

    pos.x = data->x;
    pos.y = data->z;
    pos.z = data->y;

    velocity.x = data->momx;
    velocity.y = data->momz;
    velocity.z = data->momy;

    f_angle = (data->f_angle) / 180 * PI;
    //h_angle = data->h_angle / 180 * PI;
    //h_angle = 90 / 180 * PI;
    
    // Treat listener orientation angles as spherical coordinates.
    // x = sin h * cos f
    // y = sin h * sin f
    // z = cos h

    // Front vector
    //t_angle = (PI/2 - h_angle);
    //t_angle = PI/2;
        
    //face.x = (t_sin = sin(t_angle)) * (f_cos = cos(f_angle));
    //face.z = t_sin * (f_sin = sin(f_angle));
    //face.y = cos(t_angle);
    face.x = cos(f_angle);
    face.z = sin(f_angle);
    face.y = 0;

    head.x = 0.0;
    head.y = 1.0;
    head.z = 0.0;
   

    // Top vector
    //h_angle = (-data->h_angle) / 180 * PI;

/*
    t_angle = -h_angle;
    
    head.z = (t_sin = sin(t_angle)) * f_cos;
    head.x = t_sin * f_sin;
    head.y = cos(t_angle);
*/
 
     // Update at once
    memcpy(&listener_parms.vPosition, &pos, sizeof(D3DVECTOR));
    memcpy(&listener_parms.vOrientFront, &face, sizeof(D3DVECTOR));
    memcpy(&listener_parms.vOrientTop, &head, sizeof(D3DVECTOR));
    memcpy(&listener_parms.vVelocity, &velocity, sizeof(D3DVECTOR));
    //IDirectSound3DListener_SetAllParameters(Listener, &listener_parms, DS3D_IMMEDIATE);
    IDirectSound3DListener_SetAllParameters(Listener, &listener_parms, update_mode);
}

EXPORT int HWRAPI (SetCone) (int handle, cone_def_t *cone_def)
{
    stack_t     *snd;
    //DS3DBUFFER  parms;

    if (handle < 0 || handle >= allocated_sounds)
        return -1;

    snd = _stack + handle;

    if (snd->dsbuffer3D)
    {
        
     /*f_angle = cone_def->f_angle / 180 * PI;
        h_angle = (90 - cone_def->h_angle) / 180 * PI;
        parms.vConeOrientation.x = sin(h_angle) * cos(f_angle);
        parms.vConeOrientation.z = sin(h_angle) * sin(f_angle);
        parms.vConeOrientation.y = cos(h_angle);*/

        snd->parameters.dwInsideConeAngle = cone_def->inner;
        snd->parameters.dwOutsideConeAngle = cone_def->outer;
        snd->parameters.lConeOutsideVolume = recalc_volume(cone_def->outer_gain, 256);

        return IDirectSound3DBuffer_SetAllParameters(snd->dsbuffer3D, &snd->parameters, update_mode);
    }

    return -1;
}

EXPORT void HWRAPI (Update3DSource) (int handle, source3D_pos_t *data)
{
    stack_t     *snd;   
    
    if (handle < 0 || handle >= allocated_sounds)
        return;

    snd = _stack + handle;
    if (snd->dsbuffer3D)
    {
    
        /*parms.dwSize = sizeof(DS3DBUFFER);
        IDirectSound3DBuffer_GetAllParameters(snd, &parms);*/

        //angle = data->angle * 180 / PI;

        snd->parameters.vPosition.x = data->x;
        snd->parameters.vPosition.y = data->z;
        snd->parameters.vPosition.z = data->y;

        /*parms.vConeOrientation.x = cos(angle);
        parms.vConeOrientation.z = sin(angle);
        parms.vConeOrientation.y = 0;*/

        snd->parameters.vVelocity.x = data->momx;
        snd->parameters.vVelocity.y = data->momz;
        snd->parameters.vVelocity.z = data->momy;

        //snd->parameters.flMinDistance = data->min_distance;
        //snd->parameters.flMaxDistance = data->max_distance;
        //snd->parameters.dwMode = pos->head_realtive?DS3DMODE_HEADREALTIVE:DS3DMODE_NORMAL;

        IDirectSound3DBuffer_SetAllParameters(snd->dsbuffer3D, &snd->parameters, update_mode);
    }
}


EXPORT int HWRAPI (StartSource) (int handle)
{
    LPDIRECTSOUNDBUFFER snd;

    if (handle < 0 || handle >= allocated_sounds)
        return -1;
   
    snd = (_stack + handle)->dsbuffer;
    IDirectSoundBuffer_SetCurrentPosition(snd, 0);
    return IDirectSoundBuffer_Play(snd, 0, 0, 0);

}

//-------------------------------------------------------------
// Load new sound data into source
//-------------------------------------------------------------
EXPORT int HWRAPI (Reload3DSource) (int handle, sfx_data_t *data)
{
    DS3DBUFFER  temp;
    stack_t     *snd;
    int         perm;

    // DirectX could not load new sound data into source
    // so recreate sound buffers
    if (handle < 0 || handle >= allocated_sounds)
        return -1;

    snd = _stack + handle;
    CopyMemory(&temp, &snd->parameters, sizeof(DS3DBUFFER));
    perm = snd->permanent;

    kill_sound(snd);

    snd = setup_source(handle, data, IS_3DSOURCE);
    /*snd->dsbuffer = create_buffer(data->data, data->length, true);
    if (snd->dsbuffer == NULL)
        return -1;
    snd->dsbuffer3D = create_3dbuffer(snd->dsbuffer, snd->dsbuffer3D);
    if (snd->dsbuffer3D == NULL)
    {
        RELEASE_BUFFER(snd->dsbuffer);
        return -1;
    }

    snd->sfx_id = data->id;
    snd->LRU = 0;*/
    if (snd)
    {
        snd->permanent = perm;
        IDirectSound3DBuffer_SetAllParameters(snd->dsbuffer3D, &temp, DS3D_IMMEDIATE);
        CopyMemory(&snd->parameters, &temp, sizeof(DS3DBUFFER));
    }

    return (snd - _stack);
}

EXPORT void HWRAPI (UpdateSourceVolume) (int handle, int volume)
{
    if (handle < 0 || handle >= allocated_sounds)
        return;
    UpdateSoundVolume((_stack + handle)->dsbuffer, volume);
}