opengl32: Avoid generating a wrapper for internal functions when we can call the...
[wine] / dlls / dsound / tests / ds3d.c
1 /*
2  * Tests the panning and 3D functions of DirectSound
3  *
4  * Part of this test involves playing test tones. But this only makes
5  * sense if someone is going to carefully listen to it, and would only
6  * bother everyone else.
7  * So this is only done if the test is being run in interactive mode.
8  *
9  * Copyright (c) 2002-2004 Francois Gouget
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25
26 #include <windows.h>
27
28 #include <math.h>
29
30 #include "wine/test.h"
31 #include "dsound.h"
32 #include "dxerr8.h"
33
34 #include "dsound_test.h"
35
36 #define PI 3.14159265358979323846
37 char* wave_generate_la(WAVEFORMATEX* wfx, double duration, DWORD* size)
38 {
39     int i;
40     int nb_samples;
41     char* buf;
42     char* b;
43
44     nb_samples=(int)(duration*wfx->nSamplesPerSec);
45     *size=nb_samples*wfx->nBlockAlign;
46     b=buf=malloc(*size);
47     for (i=0;i<nb_samples;i++) {
48         double y=sin(440.0*2*PI*i/wfx->nSamplesPerSec);
49         if (wfx->wBitsPerSample==8) {
50             unsigned char sample=(unsigned char)((double)127.5*(y+1.0));
51             *b++=sample;
52             if (wfx->nChannels==2)
53                 *b++=sample;
54         } else {
55             signed short sample=(signed short)((double)32767.5*y-0.5);
56             b[0]=sample & 0xff;
57             b[1]=sample >> 8;
58             b+=2;
59             if (wfx->nChannels==2) {
60                 b[0]=sample & 0xff;
61                 b[1]=sample >> 8;
62                 b+=2;
63             }
64         }
65     }
66     return buf;
67 }
68
69 const char * getDSBCAPS(DWORD xmask) {
70     static struct {
71         DWORD   mask;
72         const char    *name;
73     } flags[] = {
74 #define FE(x) { x, #x },
75         FE(DSBCAPS_PRIMARYBUFFER)
76         FE(DSBCAPS_STATIC)
77         FE(DSBCAPS_LOCHARDWARE)
78         FE(DSBCAPS_LOCSOFTWARE)
79         FE(DSBCAPS_CTRL3D)
80         FE(DSBCAPS_CTRLFREQUENCY)
81         FE(DSBCAPS_CTRLPAN)
82         FE(DSBCAPS_CTRLVOLUME)
83         FE(DSBCAPS_CTRLPOSITIONNOTIFY)
84         FE(DSBCAPS_STICKYFOCUS)
85         FE(DSBCAPS_GLOBALFOCUS)
86         FE(DSBCAPS_GETCURRENTPOSITION2)
87         FE(DSBCAPS_MUTE3DATMAXDISTANCE)
88 #undef FE
89     };
90     static char buffer[512];
91     unsigned int i;
92     BOOL first = TRUE;
93
94     buffer[0] = 0;
95
96     for (i=0;i<sizeof(flags)/sizeof(flags[0]);i++) {
97         if ((flags[i].mask & xmask) == flags[i].mask) {
98             if (first)
99                 first = FALSE;
100             else
101                 strcat(buffer, "|");
102             strcat(buffer, flags[i].name);
103         }
104     }
105
106     return buffer;
107 }
108
109 HWND get_hwnd()
110 {
111     HWND hwnd=GetForegroundWindow();
112     if (!hwnd)
113         hwnd=GetDesktopWindow();
114     return hwnd;
115 }
116
117 void init_format(WAVEFORMATEX* wfx, int format, int rate, int depth,
118                  int channels)
119 {
120     wfx->wFormatTag=format;
121     wfx->nChannels=channels;
122     wfx->wBitsPerSample=depth;
123     wfx->nSamplesPerSec=rate;
124     wfx->nBlockAlign=wfx->nChannels*wfx->wBitsPerSample/8;
125     /* FIXME: Shouldn't this test be if (format!=WAVE_FORMAT_PCM) */
126     if (wfx->nBlockAlign==0)
127     {
128         /* align compressed formats to byte boundary */
129         wfx->nBlockAlign=1;
130     }
131     wfx->nAvgBytesPerSec=wfx->nSamplesPerSec*wfx->nBlockAlign;
132     wfx->cbSize=0;
133 }
134
135 typedef struct {
136     char* wave;
137     DWORD wave_len;
138
139     LPDIRECTSOUNDBUFFER dsbo;
140     LPWAVEFORMATEX wfx;
141     DWORD buffer_size;
142     DWORD written;
143     DWORD played;
144     DWORD offset;
145 } play_state_t;
146
147 static int buffer_refill(play_state_t* state, DWORD size)
148 {
149     LPVOID ptr1,ptr2;
150     DWORD len1,len2;
151     HRESULT rc;
152
153     if (size>state->wave_len-state->written)
154         size=state->wave_len-state->written;
155
156     rc=IDirectSoundBuffer_Lock(state->dsbo,state->offset,size,
157                                &ptr1,&len1,&ptr2,&len2,0);
158     ok(rc==DS_OK,"IDirectSoundBuffer_Lock() failed: %s\n",
159        DXGetErrorString8(rc));
160     if (rc!=DS_OK)
161         return -1;
162
163     memcpy(ptr1,state->wave+state->written,len1);
164     state->written+=len1;
165     if (ptr2!=NULL) {
166         memcpy(ptr2,state->wave+state->written,len2);
167         state->written+=len2;
168     }
169     state->offset=state->written % state->buffer_size;
170     rc=IDirectSoundBuffer_Unlock(state->dsbo,ptr1,len1,ptr2,len2);
171     ok(rc==DS_OK,"IDirectSoundBuffer_Unlock() failed: %s\n",
172        DXGetErrorString8(rc));
173     if (rc!=DS_OK)
174         return -1;
175     return size;
176 }
177
178 static int buffer_silence(play_state_t* state, DWORD size)
179 {
180     LPVOID ptr1,ptr2;
181     DWORD len1,len2;
182     HRESULT rc;
183     BYTE s;
184
185     rc=IDirectSoundBuffer_Lock(state->dsbo,state->offset,size,
186                                &ptr1,&len1,&ptr2,&len2,0);
187     ok(rc==DS_OK,"IDirectSoundBuffer_Lock() failed: %s\n",
188        DXGetErrorString8(rc));
189     if (rc!=DS_OK)
190         return -1;
191
192     s=(state->wfx->wBitsPerSample==8?0x80:0);
193     memset(ptr1,s,len1);
194     if (ptr2!=NULL) {
195         memset(ptr2,s,len2);
196     }
197     state->offset=(state->offset+size) % state->buffer_size;
198     rc=IDirectSoundBuffer_Unlock(state->dsbo,ptr1,len1,ptr2,len2);
199     ok(rc==DS_OK,"IDirectSoundBuffer_Unlock() failed: %s\n",
200        DXGetErrorString8(rc));
201     if (rc!=DS_OK)
202         return -1;
203     return size;
204 }
205
206 static int buffer_service(play_state_t* state)
207 {
208     DWORD last_play_pos,play_pos,buf_free;
209     HRESULT rc;
210
211     rc=IDirectSoundBuffer_GetCurrentPosition(state->dsbo,&play_pos,NULL);
212     ok(rc==DS_OK,"IDirectSoundBuffer_GetCurrentPosition() failed: %s\n",
213        DXGetErrorString8(rc));
214     if (rc!=DS_OK) {
215         goto STOP;
216     }
217
218     /* Update the amount played */
219     last_play_pos=state->played % state->buffer_size;
220     if (play_pos<last_play_pos)
221         state->played+=state->buffer_size-last_play_pos+play_pos;
222     else
223         state->played+=play_pos-last_play_pos;
224
225     if (winetest_debug > 1)
226         trace("buf size=%d last_play_pos=%d play_pos=%d played=%d / %d\n",
227               state->buffer_size,last_play_pos,play_pos,state->played,
228               state->wave_len);
229
230     if (state->played>state->wave_len)
231     {
232         /* Everything has been played */
233         goto STOP;
234     }
235
236     /* Refill the buffer */
237     if (state->offset<=play_pos)
238         buf_free=play_pos-state->offset;
239     else
240         buf_free=state->buffer_size-state->offset+play_pos;
241
242     if (winetest_debug > 1)
243         trace("offset=%d free=%d written=%d / %d\n",
244               state->offset,buf_free,state->written,state->wave_len);
245     if (buf_free==0)
246         return 1;
247
248     if (state->written<state->wave_len)
249     {
250         int w=buffer_refill(state,buf_free);
251         if (w==-1)
252             goto STOP;
253         buf_free-=w;
254         if (state->written==state->wave_len && winetest_debug > 1)
255             trace("last sound byte at %d\n",
256                   (state->written % state->buffer_size));
257     }
258
259     if (buf_free>0) {
260         /* Fill with silence */
261         if (winetest_debug > 1)
262             trace("writing %d bytes of silence\n",buf_free);
263         if (buffer_silence(state,buf_free)==-1)
264             goto STOP;
265     }
266     return 1;
267
268 STOP:
269     if (winetest_debug > 1)
270         trace("stopping playback\n");
271     rc=IDirectSoundBuffer_Stop(state->dsbo);
272     ok(rc==DS_OK,"IDirectSoundBuffer_Stop() failed: %s\n",
273        DXGetErrorString8(rc));
274     return 0;
275 }
276
277 void test_buffer(LPDIRECTSOUND dso, LPDIRECTSOUNDBUFFER *dsbo,
278                  BOOL is_primary, BOOL set_volume, LONG volume,
279                  BOOL set_pan, LONG pan, BOOL play, double duration,
280                  BOOL buffer3d, LPDIRECTSOUND3DLISTENER listener,
281                  BOOL move_listener, BOOL move_sound,
282                  BOOL set_frequency, DWORD frequency)
283 {
284     HRESULT rc;
285     DSBCAPS dsbcaps;
286     WAVEFORMATEX wfx,wfx2;
287     DWORD size,status,freq;
288     int ref;
289
290     if (set_frequency) {
291         rc=IDirectSoundBuffer_SetFrequency(*dsbo,frequency);
292         ok(rc==DS_OK||rc==DSERR_CONTROLUNAVAIL,
293            "IDirectSoundBuffer_SetFrequency() failed to set frequency "
294            "%s\n",DXGetErrorString8(rc));
295         if (rc!=DS_OK)
296             return;
297     }
298
299     /* DSOUND: Error: Invalid caps pointer */
300     rc=IDirectSoundBuffer_GetCaps(*dsbo,0);
301     ok(rc==DSERR_INVALIDPARAM,"IDirectSoundBuffer_GetCaps() should have "
302        "returned DSERR_INVALIDPARAM, returned: %s\n",DXGetErrorString8(rc));
303
304     ZeroMemory(&dsbcaps, sizeof(dsbcaps));
305
306     /* DSOUND: Error: Invalid caps pointer */
307     rc=IDirectSoundBuffer_GetCaps(*dsbo,&dsbcaps);
308     ok(rc==DSERR_INVALIDPARAM,"IDirectSoundBuffer_GetCaps() should have "
309        "returned DSERR_INVALIDPARAM, returned: %s\n",DXGetErrorString8(rc));
310
311     dsbcaps.dwSize=sizeof(dsbcaps);
312     rc=IDirectSoundBuffer_GetCaps(*dsbo,&dsbcaps);
313     ok(rc==DS_OK,"IDirectSoundBuffer_GetCaps() failed: %s\n",
314        DXGetErrorString8(rc));
315     if (rc==DS_OK && winetest_debug > 1) {
316         trace("    Caps: flags=0x%08x size=%d\n",dsbcaps.dwFlags,
317               dsbcaps.dwBufferBytes);
318     }
319
320     /* Query the format size. Note that it may not match sizeof(wfx) */
321     size=0;
322     rc=IDirectSoundBuffer_GetFormat(*dsbo,NULL,0,&size);
323     ok(rc==DS_OK && size!=0,"IDirectSoundBuffer_GetFormat() should have "
324        "returned the needed size: rc=%s size=%d\n",DXGetErrorString8(rc),size);
325
326     rc=IDirectSoundBuffer_GetFormat(*dsbo,&wfx,sizeof(wfx),NULL);
327     ok(rc==DS_OK,"IDirectSoundBuffer_GetFormat() failed: %s\n",
328        DXGetErrorString8(rc));
329     if (rc==DS_OK && winetest_debug > 1) {
330         trace("    Format: %s tag=0x%04x %dx%dx%d avg.B/s=%d align=%d\n",
331               is_primary ? "Primary" : "Secondary",
332               wfx.wFormatTag,wfx.nSamplesPerSec,wfx.wBitsPerSample,
333               wfx.nChannels,wfx.nAvgBytesPerSec,wfx.nBlockAlign);
334     }
335
336     /* DSOUND: Error: Invalid frequency buffer */
337     rc=IDirectSoundBuffer_GetFrequency(*dsbo,0);
338     ok(rc==DSERR_INVALIDPARAM,"IDirectSoundBuffer_GetFrequency() should have "
339        "returned DSERR_INVALIDPARAM, returned: %s\n",DXGetErrorString8(rc));
340
341     /* DSOUND: Error: Primary buffers don't support CTRLFREQUENCY */
342     rc=IDirectSoundBuffer_GetFrequency(*dsbo,&freq);
343     ok((rc==DS_OK && !is_primary) || (rc==DSERR_CONTROLUNAVAIL&&is_primary) ||
344        (rc==DSERR_CONTROLUNAVAIL&&!(dsbcaps.dwFlags&DSBCAPS_CTRLFREQUENCY)),
345        "IDirectSoundBuffer_GetFrequency() failed: %s\n",DXGetErrorString8(rc));
346     if (rc==DS_OK) {
347         DWORD f = set_frequency?frequency:wfx.nSamplesPerSec;
348         ok(freq==f,"The frequency returned by GetFrequency "
349            "%d does not match the format %d\n",freq,f);
350     }
351
352     /* DSOUND: Error: Invalid status pointer */
353     rc=IDirectSoundBuffer_GetStatus(*dsbo,0);
354     ok(rc==DSERR_INVALIDPARAM,"IDirectSoundBuffer_GetStatus() should have "
355        "returned DSERR_INVALIDPARAM, returned: %s\n",DXGetErrorString8(rc));
356
357     rc=IDirectSoundBuffer_GetStatus(*dsbo,&status);
358     ok(rc==DS_OK,"IDirectSoundBuffer_GetStatus() failed: %s\n",
359        DXGetErrorString8(rc));
360     ok(status==0,"status=0x%x instead of 0\n",status);
361
362     if (is_primary) {
363         DSBCAPS new_dsbcaps;
364         /* We must call SetCooperativeLevel to be allowed to call SetFormat */
365         /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
366         rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
367         ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel(DSSCL_PRIORITY) failed: "
368            "%s\n",DXGetErrorString8(rc));
369         if (rc!=DS_OK)
370             return;
371
372         /* DSOUND: Error: Invalid format pointer */
373         rc=IDirectSoundBuffer_SetFormat(*dsbo,0);
374         ok(rc==DSERR_INVALIDPARAM,"IDirectSoundBuffer_SetFormat() should have "
375            "returned DSERR_INVALIDPARAM, returned: %s\n",DXGetErrorString8(rc));
376
377         init_format(&wfx2,WAVE_FORMAT_PCM,11025,16,2);
378         rc=IDirectSoundBuffer_SetFormat(*dsbo,&wfx2);
379         ok(rc==DS_OK,"IDirectSoundBuffer_SetFormat(%s) failed: %s\n",
380            format_string(&wfx2), DXGetErrorString8(rc));
381
382         /* There is no guarantee that SetFormat will actually change the
383          * format to what we asked for. It depends on what the soundcard
384          * supports. So we must re-query the format.
385          */
386         rc=IDirectSoundBuffer_GetFormat(*dsbo,&wfx,sizeof(wfx),NULL);
387         ok(rc==DS_OK,"IDirectSoundBuffer_GetFormat() failed: %s\n",
388            DXGetErrorString8(rc));
389         if (rc==DS_OK &&
390             (wfx.wFormatTag!=wfx2.wFormatTag ||
391              wfx.nSamplesPerSec!=wfx2.nSamplesPerSec ||
392              wfx.wBitsPerSample!=wfx2.wBitsPerSample ||
393              wfx.nChannels!=wfx2.nChannels)) {
394             trace("Requested format tag=0x%04x %dx%dx%d avg.B/s=%d align=%d\n",
395                   wfx2.wFormatTag,wfx2.nSamplesPerSec,wfx2.wBitsPerSample,
396                   wfx2.nChannels,wfx2.nAvgBytesPerSec,wfx2.nBlockAlign);
397             trace("Got tag=0x%04x %dx%dx%d avg.B/s=%d align=%d\n",
398                   wfx.wFormatTag,wfx.nSamplesPerSec,wfx.wBitsPerSample,
399                   wfx.nChannels,wfx.nAvgBytesPerSec,wfx.nBlockAlign);
400         }
401
402         ZeroMemory(&new_dsbcaps, sizeof(new_dsbcaps));
403         new_dsbcaps.dwSize = sizeof(new_dsbcaps);
404         rc=IDirectSoundBuffer_GetCaps(*dsbo,&new_dsbcaps);
405         ok(rc==DS_OK,"IDirectSoundBuffer_GetCaps() failed: %s\n",
406            DXGetErrorString8(rc));
407         if (rc==DS_OK && winetest_debug > 1) {
408             trace("    new Caps: flags=0x%08x size=%d\n",new_dsbcaps.dwFlags,
409                   new_dsbcaps.dwBufferBytes);
410         }
411
412         /* Check for primary buffer size change */
413         ok(new_dsbcaps.dwBufferBytes == dsbcaps.dwBufferBytes,
414            "    buffer size changed after SetFormat() - "
415            "previous size was %u, current size is %u\n",
416            dsbcaps.dwBufferBytes, new_dsbcaps.dwBufferBytes);
417
418         /* Check for primary buffer flags change */
419         ok(new_dsbcaps.dwFlags == dsbcaps.dwFlags,
420            "    flags changed after SetFormat() - "
421            "previous flags were %08x, current flags are %08x\n",
422            dsbcaps.dwFlags, new_dsbcaps.dwFlags);
423
424         /* Set the CooperativeLevel back to normal */
425         /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
426         rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
427         ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel(DSSCL_NORMAL) failed: "
428            "%s\n",DXGetErrorString8(rc));
429     }
430
431     if (play) {
432         play_state_t state;
433         DS3DLISTENER listener_param;
434         LPDIRECTSOUND3DBUFFER buffer=NULL;
435         DS3DBUFFER buffer_param;
436         DWORD start_time,now;
437         LPVOID buffer1;
438         DWORD length1;
439
440         if (winetest_interactive) {
441             if (set_frequency)
442                 trace("    Playing %g second 440Hz tone at %dx%dx%d with a "
443                       "frequency of %d (%dHz)\n", duration,
444                       wfx.nSamplesPerSec, wfx.wBitsPerSample, wfx.nChannels,
445                       frequency, (440 * frequency) / wfx.nSamplesPerSec);
446             else
447                 trace("    Playing %g second 440Hz tone at %dx%dx%d\n", duration,
448                       wfx.nSamplesPerSec, wfx.wBitsPerSample, wfx.nChannels);
449         }
450
451         if (is_primary) {
452             /* We must call SetCooperativeLevel to be allowed to call Lock */
453             /* DSOUND: Setting DirectSound cooperative level to
454              * DSSCL_WRITEPRIMARY */
455             rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),
456                                                 DSSCL_WRITEPRIMARY);
457             ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel(DSSCL_WRITEPRIMARY) "
458                "failed: %s\n",DXGetErrorString8(rc));
459             if (rc!=DS_OK)
460                 return;
461         }
462         if (buffer3d) {
463             LPDIRECTSOUNDBUFFER temp_buffer;
464
465             rc=IDirectSoundBuffer_QueryInterface(*dsbo,&IID_IDirectSound3DBuffer,
466                                                  (LPVOID *)&buffer);
467             ok(rc==DS_OK,"IDirectSoundBuffer_QueryInterface() failed: %s\n",
468                DXGetErrorString8(rc));
469             if (rc!=DS_OK)
470                 return;
471
472             /* check the COM interface */
473             rc=IDirectSoundBuffer_QueryInterface(*dsbo, &IID_IDirectSoundBuffer,
474                                                  (LPVOID *)&temp_buffer);
475             ok(rc==DS_OK && temp_buffer!=NULL,
476                "IDirectSoundBuffer_QueryInterface() failed: %s\n",
477                DXGetErrorString8(rc));
478             ok(temp_buffer==*dsbo,"COM interface broken: %p != %p\n",
479                temp_buffer,*dsbo);
480             ref=IDirectSoundBuffer_Release(temp_buffer);
481             ok(ref==1,"IDirectSoundBuffer_Release() has %d references, "
482                "should have 1\n",ref);
483
484             temp_buffer=NULL;
485             rc=IDirectSound3DBuffer_QueryInterface(*dsbo,
486                                                    &IID_IDirectSoundBuffer,
487                                                    (LPVOID *)&temp_buffer);
488             ok(rc==DS_OK && temp_buffer!=NULL,
489                "IDirectSound3DBuffer_QueryInterface() failed: %s\n",
490                DXGetErrorString8(rc));
491             ok(temp_buffer==*dsbo,"COM interface broken: %p != %p\n",
492                temp_buffer,*dsbo);
493             ref=IDirectSoundBuffer_Release(temp_buffer);
494             ok(ref==1,"IDirectSoundBuffer_Release() has %d references, "
495                "should have 1\n",ref);
496
497             ref=IDirectSoundBuffer_Release(*dsbo);
498             ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
499                "should have 0\n",ref);
500
501             rc=IDirectSound3DBuffer_QueryInterface(buffer,
502                                                    &IID_IDirectSoundBuffer,
503                                                    (LPVOID *)dsbo);
504             ok(rc==DS_OK && *dsbo!=NULL,"IDirectSound3DBuffer_QueryInterface() "
505                "failed: %s\n",DXGetErrorString8(rc));
506
507             /* DSOUND: Error: Invalid buffer */
508             rc=IDirectSound3DBuffer_GetAllParameters(buffer,0);
509             ok(rc==DSERR_INVALIDPARAM,"IDirectSound3DBuffer_GetAllParameters() "
510                "failed: %s\n",DXGetErrorString8(rc));
511
512             ZeroMemory(&buffer_param, sizeof(buffer_param));
513
514             /* DSOUND: Error: Invalid buffer */
515             rc=IDirectSound3DBuffer_GetAllParameters(buffer,&buffer_param);
516             ok(rc==DSERR_INVALIDPARAM,"IDirectSound3DBuffer_GetAllParameters() "
517                "failed: %s\n",DXGetErrorString8(rc));
518
519             buffer_param.dwSize=sizeof(buffer_param);
520             rc=IDirectSound3DBuffer_GetAllParameters(buffer,&buffer_param);
521             ok(rc==DS_OK,"IDirectSound3DBuffer_GetAllParameters() failed: %s\n",
522                DXGetErrorString8(rc));
523         }
524         if (set_volume) {
525             if (dsbcaps.dwFlags & DSBCAPS_CTRLVOLUME) {
526                 LONG val;
527                 rc=IDirectSoundBuffer_GetVolume(*dsbo,&val);
528                 ok(rc==DS_OK,"IDirectSoundBuffer_GetVolume() failed: %s\n",
529                    DXGetErrorString8(rc));
530
531                 rc=IDirectSoundBuffer_SetVolume(*dsbo,volume);
532                 ok(rc==DS_OK,"IDirectSoundBuffer_SetVolume() failed: %s\n",
533                    DXGetErrorString8(rc));
534             } else {
535                 /* DSOUND: Error: Buffer does not have CTRLVOLUME */
536                 rc=IDirectSoundBuffer_GetVolume(*dsbo,&volume);
537                 ok(rc==DSERR_CONTROLUNAVAIL,"IDirectSoundBuffer_GetVolume() "
538                    "should have returned DSERR_CONTROLUNAVAIL, returned: %s\n",
539                    DXGetErrorString8(rc));
540             }
541         }
542
543         if (set_pan) {
544             if (dsbcaps.dwFlags & DSBCAPS_CTRLPAN) {
545                 LONG val;
546                 rc=IDirectSoundBuffer_GetPan(*dsbo,&val);
547                 ok(rc==DS_OK,"IDirectSoundBuffer_GetPan() failed: %s\n",
548                    DXGetErrorString8(rc));
549
550                 rc=IDirectSoundBuffer_SetPan(*dsbo,pan);
551                 ok(rc==DS_OK,"IDirectSoundBuffer_SetPan() failed: %s\n",
552                    DXGetErrorString8(rc));
553             } else {
554                 /* DSOUND: Error: Buffer does not have CTRLPAN */
555                 rc=IDirectSoundBuffer_GetPan(*dsbo,&pan);
556                 ok(rc==DSERR_CONTROLUNAVAIL,"IDirectSoundBuffer_GetPan() "
557                    "should have returned DSERR_CONTROLUNAVAIL, returned: %s\n",
558                    DXGetErrorString8(rc));
559             }
560         }
561
562         /* try an offset past the end of the buffer */
563         rc = IDirectSoundBuffer_Lock(*dsbo, dsbcaps.dwBufferBytes, 0, &buffer1,
564                                       &length1, NULL, NULL,
565                                       DSBLOCK_ENTIREBUFFER);
566         ok(rc==DSERR_INVALIDPARAM, "IDirectSoundBuffer_Lock() should have "
567            "returned DSERR_INVALIDPARAM, returned %s\n", DXGetErrorString8(rc));
568
569         /* try a size larger than the buffer */
570         rc = IDirectSoundBuffer_Lock(*dsbo, 0, dsbcaps.dwBufferBytes + 1,
571                                      &buffer1, &length1, NULL, NULL,
572                                      DSBLOCK_FROMWRITECURSOR);
573         ok(rc==DSERR_INVALIDPARAM, "IDirectSoundBuffer_Lock() should have "
574            "returned DSERR_INVALIDPARAM, returned %s\n", DXGetErrorString8(rc));
575
576         if (set_frequency)
577             state.wave=wave_generate_la(&wfx,(duration*frequency)/wfx.nSamplesPerSec,&state.wave_len);
578         else
579             state.wave=wave_generate_la(&wfx,duration,&state.wave_len);
580
581         state.dsbo=*dsbo;
582         state.wfx=&wfx;
583         state.buffer_size=dsbcaps.dwBufferBytes;
584         state.played=state.written=state.offset=0;
585         buffer_refill(&state,state.buffer_size);
586
587         rc=IDirectSoundBuffer_Play(*dsbo,0,0,DSBPLAY_LOOPING);
588         ok(rc==DS_OK,"IDirectSoundBuffer_Play() failed: %s\n",
589            DXGetErrorString8(rc));
590
591         rc=IDirectSoundBuffer_GetStatus(*dsbo,&status);
592         ok(rc==DS_OK,"IDirectSoundBuffer_GetStatus() failed: %s\n",
593            DXGetErrorString8(rc));
594         ok(status==(DSBSTATUS_PLAYING|DSBSTATUS_LOOPING),
595            "GetStatus: bad status: %x\n",status);
596
597         if (listener) {
598             ZeroMemory(&listener_param,sizeof(listener_param));
599             listener_param.dwSize=sizeof(listener_param);
600             rc=IDirectSound3DListener_GetAllParameters(listener,
601                                                        &listener_param);
602             ok(rc==DS_OK,"IDirectSound3dListener_GetAllParameters() "
603                "failed: %s\n",DXGetErrorString8(rc));
604             if (move_listener) {
605                 listener_param.vPosition.x = -5.0;
606                 listener_param.vVelocity.x = 10.0/duration;
607             }
608             rc=IDirectSound3DListener_SetAllParameters(listener,
609                                                        &listener_param,
610                                                        DS3D_IMMEDIATE);
611             ok(rc==DS_OK,"IDirectSound3dListener_SetPosition() failed: %s\n",
612                DXGetErrorString8(rc));
613         }
614         if (buffer3d) {
615             if (move_sound) {
616                 buffer_param.vPosition.x = 100.0;
617                 buffer_param.vVelocity.x = -200.0/duration;
618             }
619             buffer_param.flMinDistance = 10;
620             rc=IDirectSound3DBuffer_SetAllParameters(buffer,&buffer_param,
621                                                      DS3D_IMMEDIATE);
622             ok(rc==DS_OK,"IDirectSound3dBuffer_SetPosition() failed: %s\n",
623                DXGetErrorString8(rc));
624         }
625
626         start_time=GetTickCount();
627         while (buffer_service(&state)) {
628             WaitForSingleObject(GetCurrentProcess(),TIME_SLICE);
629             now=GetTickCount();
630             if (listener && move_listener) {
631                 listener_param.vPosition.x = -5.0+10.0*(now-start_time)/
632                     1000/duration;
633                 if (winetest_debug>2)
634                     trace("listener position=%g\n",listener_param.vPosition.x);
635                 rc=IDirectSound3DListener_SetPosition(listener,
636                     listener_param.vPosition.x,listener_param.vPosition.y,
637                     listener_param.vPosition.z,DS3D_IMMEDIATE);
638                 ok(rc==DS_OK,"IDirectSound3dListener_SetPosition() failed: "
639                    "%s\n",DXGetErrorString8(rc));
640             }
641             if (buffer3d && move_sound) {
642                 buffer_param.vPosition.x = 100-200.0*(now-start_time)/
643                     1000/duration;
644                 if (winetest_debug>2)
645                     trace("sound position=%g\n",buffer_param.vPosition.x);
646                 rc=IDirectSound3DBuffer_SetPosition(buffer,
647                     buffer_param.vPosition.x,buffer_param.vPosition.y,
648                     buffer_param.vPosition.z,DS3D_IMMEDIATE);
649                 ok(rc==DS_OK,"IDirectSound3dBuffer_SetPosition() failed: %s\n",
650                    DXGetErrorString8(rc));
651             }
652         }
653         /* Check the sound duration was within 10% of the expected value */
654         now=GetTickCount();
655         ok(fabs(1000*duration-now+start_time)<=100*duration,
656            "The sound played for %d ms instead of %g ms\n",
657            now-start_time,1000*duration);
658
659         free(state.wave);
660         if (is_primary) {
661             /* Set the CooperativeLevel back to normal */
662             /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
663             rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
664             ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel(DSSCL_NORMAL) "
665                "failed: %s\n",DXGetErrorString8(rc));
666         }
667         if (buffer3d) {
668             ref=IDirectSound3DBuffer_Release(buffer);
669             ok(ref==0,"IDirectSound3DBuffer_Release() has %d references, "
670                "should have 0\n",ref);
671         }
672     }
673 }
674
675 static HRESULT test_secondary(LPGUID lpGuid, int play,
676                               int has_3d, int has_3dbuffer,
677                               int has_listener, int has_duplicate,
678                               int move_listener, int move_sound)
679 {
680     HRESULT rc;
681     LPDIRECTSOUND dso=NULL;
682     LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
683     LPDIRECTSOUND3DLISTENER listener=NULL;
684     DSBUFFERDESC bufdesc;
685     WAVEFORMATEX wfx, wfx1;
686     int ref;
687
688     /* Create the DirectSound object */
689     rc=DirectSoundCreate(lpGuid,&dso,NULL);
690     ok(rc==DS_OK||rc==DSERR_NODRIVER,"DirectSoundCreate() failed: %s\n",
691        DXGetErrorString8(rc));
692     if (rc!=DS_OK)
693         return rc;
694
695     /* We must call SetCooperativeLevel before creating primary buffer */
696     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
697     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
698     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel(DSSCL_PRIORITY) failed: "
699        "%s\n",DXGetErrorString8(rc));
700     if (rc!=DS_OK)
701         goto EXIT;
702
703     ZeroMemory(&bufdesc, sizeof(bufdesc));
704     bufdesc.dwSize=sizeof(bufdesc);
705     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
706     if (has_3d)
707         bufdesc.dwFlags|=DSBCAPS_CTRL3D;
708     else
709         bufdesc.dwFlags|=(DSBCAPS_CTRLVOLUME|DSBCAPS_CTRLPAN);
710     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
711     ok((rc==DS_OK && primary!=NULL) || (rc==DSERR_CONTROLUNAVAIL),
712        "IDirectSound_CreateSoundBuffer() failed to create a %sprimary buffer: "
713        "%s\n",has_3d?"3D ":"", DXGetErrorString8(rc));
714     if (rc==DSERR_CONTROLUNAVAIL)
715         trace("  No Primary\n");
716     else if (rc==DS_OK && primary!=NULL) {
717         rc=IDirectSoundBuffer_GetFormat(primary,&wfx1,sizeof(wfx1),NULL);
718         ok(rc==DS_OK,"IDirectSoundBuffer8_Getformat() failed: %s\n",
719            DXGetErrorString8(rc));
720         if (rc!=DS_OK)
721             goto EXIT1;
722
723         if (has_listener) {
724             rc=IDirectSoundBuffer_QueryInterface(primary,
725                                                  &IID_IDirectSound3DListener,
726                                                  (void **)&listener);
727             ok(rc==DS_OK && listener!=NULL,
728                "IDirectSoundBuffer_QueryInterface() failed to get a 3D "
729                "listener: %s\n",DXGetErrorString8(rc));
730             ref=IDirectSoundBuffer_Release(primary);
731             ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
732                "should have 0\n",ref);
733             if (rc==DS_OK && listener!=NULL) {
734                 DS3DLISTENER listener_param;
735                 ZeroMemory(&listener_param,sizeof(listener_param));
736                 /* DSOUND: Error: Invalid buffer */
737                 rc=IDirectSound3DListener_GetAllParameters(listener,0);
738                 ok(rc==DSERR_INVALIDPARAM,
739                    "IDirectSound3dListener_GetAllParameters() should have "
740                    "returned DSERR_INVALIDPARAM, returned: %s\n",
741                    DXGetErrorString8(rc));
742
743                 /* DSOUND: Error: Invalid buffer */
744                 rc=IDirectSound3DListener_GetAllParameters(listener,
745                                                            &listener_param);
746                 ok(rc==DSERR_INVALIDPARAM,
747                    "IDirectSound3dListener_GetAllParameters() should have "
748                    "returned DSERR_INVALIDPARAM, returned: %s\n",
749                    DXGetErrorString8(rc));
750
751                 listener_param.dwSize=sizeof(listener_param);
752                 rc=IDirectSound3DListener_GetAllParameters(listener,
753                                                            &listener_param);
754                 ok(rc==DS_OK,"IDirectSound3dListener_GetAllParameters() "
755                    "failed: %s\n",DXGetErrorString8(rc));
756             } else {
757                 ok(listener==NULL, "IDirectSoundBuffer_QueryInterface() "
758                    "failed but returned a listener anyway\n");
759                 ok(rc!=DS_OK, "IDirectSoundBuffer_QueryInterface() succeeded "
760                    "but returned a NULL listener\n");
761                 if (listener) {
762                     ref=IDirectSound3DListener_Release(listener);
763                     ok(ref==0,"IDirectSound3dListener_Release() listener has "
764                        "%d references, should have 0\n",ref);
765                 }
766                 goto EXIT2;
767             }
768         }
769
770         init_format(&wfx,WAVE_FORMAT_PCM,22050,16,2);
771         secondary=NULL;
772         ZeroMemory(&bufdesc, sizeof(bufdesc));
773         bufdesc.dwSize=sizeof(bufdesc);
774         bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
775         if (has_3d)
776             bufdesc.dwFlags|=DSBCAPS_CTRL3D;
777         else
778             bufdesc.dwFlags|=
779                 (DSBCAPS_CTRLFREQUENCY|DSBCAPS_CTRLVOLUME|DSBCAPS_CTRLPAN);
780         bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
781                                     wfx.nBlockAlign);
782         bufdesc.lpwfxFormat=&wfx;
783         if (winetest_interactive) {
784             trace("  Testing a %s%ssecondary buffer %s%s%s%sat %dx%dx%d "
785                   "with a primary buffer at %dx%dx%d\n",
786                   has_3dbuffer?"3D ":"",
787                   has_duplicate?"duplicated ":"",
788                   listener!=NULL||move_sound?"with ":"",
789                   move_listener?"moving ":"",
790                   listener!=NULL?"listener ":"",
791                   listener&&move_sound?"and moving sound ":move_sound?
792                   "moving sound ":"",
793                   wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,
794                   wfx1.nSamplesPerSec,wfx1.wBitsPerSample,wfx1.nChannels);
795         }
796         rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
797         ok(rc==DS_OK && secondary!=NULL,"IDirectSound_CreateSoundBuffer() "
798            "failed to create a %s%ssecondary buffer %s%s%s%sat %dx%dx%d (%s): %s\n",
799            has_3dbuffer?"3D ":"", has_duplicate?"duplicated ":"",
800            listener!=NULL||move_sound?"with ":"", move_listener?"moving ":"",
801            listener!=NULL?"listener ":"",
802            listener&&move_sound?"and moving sound ":move_sound?
803            "moving sound ":"",
804            wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,
805            getDSBCAPS(bufdesc.dwFlags),DXGetErrorString8(rc));
806         if (rc==DS_OK && secondary!=NULL) {
807             if (!has_3d) {
808                 LONG refvol,vol,refpan,pan;
809
810                 /* Check the initial secondary buffer's volume and pan */
811                 rc=IDirectSoundBuffer_GetVolume(secondary,&vol);
812                 ok(rc==DS_OK,"IDirectSoundBuffer_GetVolume(secondary) failed: "
813                    "%s\n",DXGetErrorString8(rc));
814                 ok(vol==0,"wrong volume for a new secondary buffer: %d\n",vol);
815                 rc=IDirectSoundBuffer_GetPan(secondary,&pan);
816                 ok(rc==DS_OK,"IDirectSoundBuffer_GetPan(secondary) failed: "
817                    "%s\n",DXGetErrorString8(rc));
818                 ok(pan==0,"wrong pan for a new secondary buffer: %d\n",pan);
819
820                 /* Check that changing the secondary buffer's volume and pan
821                  * does not impact the primary buffer's volume and pan
822                  */
823                 rc=IDirectSoundBuffer_GetVolume(primary,&refvol);
824                 ok(rc==DS_OK,"IDirectSoundBuffer_GetVolume(primary) failed: "
825                    "%s\n",DXGetErrorString8(rc));
826                 rc=IDirectSoundBuffer_GetPan(primary,&refpan);
827                 ok(rc==DS_OK,"IDirectSoundBuffer_GetPan(primary) failed: %s\n",
828                    DXGetErrorString8(rc));
829
830                 rc=IDirectSoundBuffer_SetVolume(secondary,-1000);
831                 ok(rc==DS_OK,"IDirectSoundBuffer_SetVolume(secondary) failed: "
832                    "%s\n",DXGetErrorString8(rc));
833                 rc=IDirectSoundBuffer_GetVolume(secondary,&vol);
834                 ok(rc==DS_OK,"IDirectSoundBuffer_SetVolume(secondary) failed: "
835                    "%s\n",DXGetErrorString8(rc));
836                 ok(vol==-1000,"secondary: wrong volume %d instead of -1000\n",
837                    vol);
838                 rc=IDirectSoundBuffer_SetPan(secondary,-1000);
839                 ok(rc==DS_OK,"IDirectSoundBuffer_SetPan(secondary) failed: "
840                    "%s\n",DXGetErrorString8(rc));
841                 rc=IDirectSoundBuffer_GetPan(secondary,&pan);
842                 ok(rc==DS_OK,"IDirectSoundBuffer_SetPan(secondary) failed: "
843                    "%s\n",DXGetErrorString8(rc));
844                 ok(pan==-1000,"secondary: wrong pan %d instead of -1000\n",
845                    pan);
846
847                 rc=IDirectSoundBuffer_GetVolume(primary,&vol);
848                 ok(rc==DS_OK,"IDirectSoundBuffer_GetVolume(primary) failed: "
849                    "%s\n",DXGetErrorString8(rc));
850                 ok(vol==refvol,"The primary volume changed from %d to %d\n",
851                    refvol,vol);
852                 rc=IDirectSoundBuffer_GetPan(primary,&pan);
853                 ok(rc==DS_OK,"IDirectSoundBuffer_GetPan(primary) failed: %s\n",
854                    DXGetErrorString8(rc));
855                 ok(pan==refpan,"The primary pan changed from %d to %d\n",
856                    refpan,pan);
857
858                 rc=IDirectSoundBuffer_SetVolume(secondary,0);
859                 ok(rc==DS_OK,"IDirectSoundBuffer_SetVolume(secondary) failed: "
860                    "%s\n",DXGetErrorString8(rc));
861                 rc=IDirectSoundBuffer_SetPan(secondary,0);
862                 ok(rc==DS_OK,"IDirectSoundBuffer_SetPan(secondary) failed: "
863                    "%s\n",DXGetErrorString8(rc));
864             }
865             if (has_duplicate) {
866                 LPDIRECTSOUNDBUFFER duplicated=NULL;
867
868                 /* DSOUND: Error: Invalid source buffer */
869                 rc=IDirectSound_DuplicateSoundBuffer(dso,0,0);
870                 ok(rc==DSERR_INVALIDPARAM,
871                    "IDirectSound_DuplicateSoundBuffer() should have returned "
872                    "DSERR_INVALIDPARAM, returned: %s\n",DXGetErrorString8(rc));
873
874                 /* DSOUND: Error: Invalid dest buffer */
875                 rc=IDirectSound_DuplicateSoundBuffer(dso,secondary,0);
876                 ok(rc==DSERR_INVALIDPARAM,
877                    "IDirectSound_DuplicateSoundBuffer() should have returned "
878                    "DSERR_INVALIDPARAM, returned: %s\n",DXGetErrorString8(rc));
879
880                 /* DSOUND: Error: Invalid source buffer */
881                 rc=IDirectSound_DuplicateSoundBuffer(dso,0,&duplicated);
882                 ok(rc==DSERR_INVALIDPARAM,
883                   "IDirectSound_DuplicateSoundBuffer() should have returned "
884                   "DSERR_INVALIDPARAM, returned: %s\n",DXGetErrorString8(rc));
885
886                 duplicated=NULL;
887                 rc=IDirectSound_DuplicateSoundBuffer(dso,secondary,
888                                                      &duplicated);
889                 ok(rc==DS_OK && duplicated!=NULL,
890                    "IDirectSound_DuplicateSoundBuffer() failed to duplicate "
891                    "a secondary buffer: %s\n",DXGetErrorString8(rc));
892
893                 if (rc==DS_OK && duplicated!=NULL) {
894                     ref=IDirectSoundBuffer_Release(secondary);
895                     ok(ref==0,"IDirectSoundBuffer_Release() secondary has %d "
896                       "references, should have 0\n",ref);
897                     secondary=duplicated;
898                 }
899             }
900
901             if (rc==DS_OK && secondary!=NULL) {
902                 double duration;
903                 duration=(move_listener || move_sound?4.0:1.0);
904                 test_buffer(dso,&secondary,0,FALSE,0,FALSE,0,
905                             winetest_interactive,duration,has_3dbuffer,
906                             listener,move_listener,move_sound,FALSE,0);
907                 ref=IDirectSoundBuffer_Release(secondary);
908                 ok(ref==0,"IDirectSoundBuffer_Release() %s has %d references, "
909                    "should have 0\n",has_duplicate?"duplicated":"secondary",
910                    ref);
911             }
912         }
913 EXIT1:
914         if (has_listener) {
915             ref=IDirectSound3DListener_Release(listener);
916             ok(ref==0,"IDirectSound3dListener_Release() listener has %d "
917                "references, should have 0\n",ref);
918         } else {
919             ref=IDirectSoundBuffer_Release(primary);
920             ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
921                "should have 0\n",ref);
922         }
923     } else {
924         ok(primary==NULL,"IDirectSound_CreateSoundBuffer(primary) failed "
925            "but primary created anyway\n");
926         ok(rc!=DS_OK,"IDirectSound_CreateSoundBuffer(primary) succeeded "
927            "but primary not created\n");
928         if (primary) {
929             ref=IDirectSoundBuffer_Release(primary);
930             ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
931                "should have 0\n",ref);
932         }
933     }
934 EXIT2:
935     /* Set the CooperativeLevel back to normal */
936     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
937     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
938     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel(DSSCL_NORMAL) failed: %s\n",
939        DXGetErrorString8(rc));
940
941 EXIT:
942     ref=IDirectSound_Release(dso);
943     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
944     if (ref!=0)
945         return DSERR_GENERIC;
946
947     return rc;
948 }
949
950 static HRESULT test_for_driver(LPGUID lpGuid)
951 {
952     HRESULT rc;
953     LPDIRECTSOUND dso=NULL;
954     int ref;
955
956     /* Create the DirectSound object */
957     rc=DirectSoundCreate(lpGuid,&dso,NULL);
958     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
959        "DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
960     if (rc!=DS_OK)
961         return rc;
962
963     ref=IDirectSound_Release(dso);
964     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
965     if (ref!=0)
966         return DSERR_GENERIC;
967
968     return rc;
969 }
970
971 static HRESULT test_primary(LPGUID lpGuid)
972 {
973     HRESULT rc;
974     LPDIRECTSOUND dso=NULL;
975     LPDIRECTSOUNDBUFFER primary=NULL;
976     DSBUFFERDESC bufdesc;
977     DSCAPS dscaps;
978     int ref, i;
979
980     /* Create the DirectSound object */
981     rc=DirectSoundCreate(lpGuid,&dso,NULL);
982     ok(rc==DS_OK||rc==DSERR_NODRIVER,"DirectSoundCreate() failed: %s\n",
983        DXGetErrorString8(rc));
984     if (rc!=DS_OK)
985         return rc;
986
987     /* Get the device capabilities */
988     ZeroMemory(&dscaps, sizeof(dscaps));
989     dscaps.dwSize=sizeof(dscaps);
990     rc=IDirectSound_GetCaps(dso,&dscaps);
991     ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %s\n",DXGetErrorString8(rc));
992     if (rc!=DS_OK)
993         goto EXIT;
994
995     /* We must call SetCooperativeLevel before calling CreateSoundBuffer */
996     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
997     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
998     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel(DSSCL_PRIORITY) failed: "
999        "%s\n",DXGetErrorString8(rc));
1000     if (rc!=DS_OK)
1001         goto EXIT;
1002
1003     /* Testing the primary buffer */
1004     primary=NULL;
1005     ZeroMemory(&bufdesc, sizeof(bufdesc));
1006     bufdesc.dwSize=sizeof(bufdesc);
1007     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRLVOLUME|DSBCAPS_CTRLPAN;
1008     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
1009     ok((rc==DS_OK && primary!=NULL) || (rc==DSERR_CONTROLUNAVAIL),
1010        "IDirectSound_CreateSoundBuffer() failed to create a primary buffer: "
1011        "%s\n",DXGetErrorString8(rc));
1012     if (rc==DSERR_CONTROLUNAVAIL)
1013         trace("  No Primary\n");
1014     else if (rc==DS_OK && primary!=NULL) {
1015         test_buffer(dso,&primary,1,TRUE,0,TRUE,0,winetest_interactive &&
1016                     !(dscaps.dwFlags & DSCAPS_EMULDRIVER),1.0,0,NULL,0,0,
1017                     FALSE,0);
1018         if (winetest_interactive) {
1019             LONG volume,pan;
1020
1021             volume = DSBVOLUME_MAX;
1022             for (i = 0; i < 6; i++) {
1023                 test_buffer(dso,&primary,1,TRUE,volume,TRUE,0,
1024                             winetest_interactive &&
1025                             !(dscaps.dwFlags & DSCAPS_EMULDRIVER),
1026                             1.0,0,NULL,0,0,FALSE,0);
1027                 volume -= ((DSBVOLUME_MAX-DSBVOLUME_MIN) / 40);
1028             }
1029
1030             pan = DSBPAN_LEFT;
1031             for (i = 0; i < 7; i++) {
1032                 test_buffer(dso,&primary,1,TRUE,0,TRUE,pan,
1033                             winetest_interactive &&
1034                             !(dscaps.dwFlags & DSCAPS_EMULDRIVER),1.0,0,0,0,0,FALSE,0);
1035                 pan += ((DSBPAN_RIGHT-DSBPAN_LEFT) / 6);
1036             }
1037         }
1038         ref=IDirectSoundBuffer_Release(primary);
1039         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
1040            "should have 0\n",ref);
1041     }
1042
1043     /* Set the CooperativeLevel back to normal */
1044     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
1045     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
1046     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel(DSSCL_NORMAL) failed: %s\n",
1047        DXGetErrorString8(rc));
1048
1049 EXIT:
1050     ref=IDirectSound_Release(dso);
1051     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
1052     if (ref!=0)
1053         return DSERR_GENERIC;
1054
1055     return rc;
1056 }
1057
1058 static HRESULT test_primary_3d(LPGUID lpGuid)
1059 {
1060     HRESULT rc;
1061     LPDIRECTSOUND dso=NULL;
1062     LPDIRECTSOUNDBUFFER primary=NULL;
1063     DSBUFFERDESC bufdesc;
1064     DSCAPS dscaps;
1065     int ref;
1066
1067     /* Create the DirectSound object */
1068     rc=DirectSoundCreate(lpGuid,&dso,NULL);
1069     ok(rc==DS_OK||rc==DSERR_NODRIVER,"DirectSoundCreate() failed: %s\n",
1070        DXGetErrorString8(rc));
1071     if (rc!=DS_OK)
1072         return rc;
1073
1074     /* Get the device capabilities */
1075     ZeroMemory(&dscaps, sizeof(dscaps));
1076     dscaps.dwSize=sizeof(dscaps);
1077     rc=IDirectSound_GetCaps(dso,&dscaps);
1078     ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %s\n",DXGetErrorString8(rc));
1079     if (rc!=DS_OK)
1080         goto EXIT;
1081
1082     /* We must call SetCooperativeLevel before calling CreateSoundBuffer */
1083     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
1084     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
1085     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel(DSSCL_PRIORITY) failed: "
1086        "%s\n",DXGetErrorString8(rc));
1087     if (rc!=DS_OK)
1088         goto EXIT;
1089
1090     primary=NULL;
1091     ZeroMemory(&bufdesc, sizeof(bufdesc));
1092     bufdesc.dwSize=sizeof(bufdesc);
1093     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
1094     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
1095     ok(rc==DS_OK && primary!=NULL,"IDirectSound_CreateSoundBuffer() failed "
1096        "to create a primary buffer: %s\n",DXGetErrorString8(rc));
1097     if (rc==DS_OK && primary!=NULL) {
1098         ref=IDirectSoundBuffer_Release(primary);
1099         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
1100            "should have 0\n",ref);
1101         primary=NULL;
1102         ZeroMemory(&bufdesc, sizeof(bufdesc));
1103         bufdesc.dwSize=sizeof(bufdesc);
1104         bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRL3D;
1105         rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
1106         ok(rc==DS_OK && primary!=NULL,"IDirectSound_CreateSoundBuffer() "
1107            "failed to create a 3D primary buffer: %s\n",DXGetErrorString8(rc));
1108         if (rc==DS_OK && primary!=NULL) {
1109             test_buffer(dso,&primary,1,FALSE,0,FALSE,0,winetest_interactive &&
1110                         !(dscaps.dwFlags & DSCAPS_EMULDRIVER),1.0,0,0,0,0,
1111                         FALSE,0);
1112             ref=IDirectSoundBuffer_Release(primary);
1113             ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
1114                "should have 0\n",ref);
1115         }
1116     }
1117     /* Set the CooperativeLevel back to normal */
1118     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
1119     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
1120     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel(DSSCL_NORMAL) failed: %s\n",
1121        DXGetErrorString8(rc));
1122
1123 EXIT:
1124     ref=IDirectSound_Release(dso);
1125     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
1126     if (ref!=0)
1127         return DSERR_GENERIC;
1128
1129     return rc;
1130 }
1131
1132 static HRESULT test_primary_3d_with_listener(LPGUID lpGuid)
1133 {
1134     HRESULT rc;
1135     LPDIRECTSOUND dso=NULL;
1136     LPDIRECTSOUNDBUFFER primary=NULL;
1137     DSBUFFERDESC bufdesc;
1138     DSCAPS dscaps;
1139     int ref;
1140
1141     /* Create the DirectSound object */
1142     rc=DirectSoundCreate(lpGuid,&dso,NULL);
1143     ok(rc==DS_OK||rc==DSERR_NODRIVER,"DirectSoundCreate() failed: %s\n",
1144        DXGetErrorString8(rc));
1145     if (rc!=DS_OK)
1146         return rc;
1147
1148     /* Get the device capabilities */
1149     ZeroMemory(&dscaps, sizeof(dscaps));
1150     dscaps.dwSize=sizeof(dscaps);
1151     rc=IDirectSound_GetCaps(dso,&dscaps);
1152     ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %s\n",DXGetErrorString8(rc));
1153     if (rc!=DS_OK)
1154         goto EXIT;
1155
1156     /* We must call SetCooperativeLevel before calling CreateSoundBuffer */
1157     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
1158     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
1159     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel(DSSCL_PRIORITY) failed: "
1160        "%s\n",DXGetErrorString8(rc));
1161     if (rc!=DS_OK)
1162         goto EXIT;
1163     primary=NULL;
1164     ZeroMemory(&bufdesc, sizeof(bufdesc));
1165     bufdesc.dwSize=sizeof(bufdesc);
1166     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRL3D;
1167     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
1168     ok(rc==DS_OK && primary!=NULL,"IDirectSound_CreateSoundBuffer() failed "
1169        "to create a 3D primary buffer: %s\n",DXGetErrorString8(rc));
1170     if (rc==DS_OK && primary!=NULL) {
1171         LPDIRECTSOUND3DLISTENER listener=NULL;
1172         rc=IDirectSoundBuffer_QueryInterface(primary,
1173             &IID_IDirectSound3DListener,(void **)&listener);
1174         ok(rc==DS_OK && listener!=NULL,"IDirectSoundBuffer_QueryInterface() "
1175            "failed to get a 3D listener: %s\n",DXGetErrorString8(rc));
1176         if (rc==DS_OK && listener!=NULL) {
1177             LPDIRECTSOUNDBUFFER temp_buffer=NULL;
1178
1179             /* Checking the COM interface */
1180             rc=IDirectSoundBuffer_QueryInterface(primary,
1181                 &IID_IDirectSoundBuffer,(LPVOID *)&temp_buffer);
1182             ok(rc==DS_OK && temp_buffer!=NULL,
1183                "IDirectSoundBuffer_QueryInterface() failed: %s\n",
1184                DXGetErrorString8(rc));
1185             ok(temp_buffer==primary,
1186                "COM interface broken: %p != %p\n",
1187                temp_buffer,primary);
1188             if (rc==DS_OK && temp_buffer!=NULL) {
1189                 ref=IDirectSoundBuffer_Release(temp_buffer);
1190                 ok(ref==1,"IDirectSoundBuffer_Release() has %d references, "
1191                    "should have 1\n",ref);
1192
1193                 temp_buffer=NULL;
1194                 rc=IDirectSound3DListener_QueryInterface(listener,
1195                     &IID_IDirectSoundBuffer,(LPVOID *)&temp_buffer);
1196                 ok(rc==DS_OK && temp_buffer!=NULL,
1197                    "IDirectSoundBuffer_QueryInterface() failed: %s\n",
1198                    DXGetErrorString8(rc));
1199                 ok(temp_buffer==primary,
1200                    "COM interface broken: %p != %p\n",
1201                    temp_buffer,primary);
1202                 ref=IDirectSoundBuffer_Release(temp_buffer);
1203                 ok(ref==1,"IDirectSoundBuffer_Release() has %d references, "
1204                    "should have 1\n",ref);
1205
1206                 /* Testing the buffer */
1207                 test_buffer(dso,&primary,1,FALSE,0,FALSE,0,
1208                             winetest_interactive &&
1209                             !(dscaps.dwFlags & DSCAPS_EMULDRIVER),1.0,0,
1210                             listener,0,0,FALSE,0);
1211             }
1212
1213             /* Testing the reference counting */
1214             ref=IDirectSound3DListener_Release(listener);
1215             ok(ref==0,"IDirectSound3DListener_Release() listener has %d "
1216                "references, should have 0\n",ref);
1217         }
1218
1219         /* Testing the reference counting */
1220         ref=IDirectSoundBuffer_Release(primary);
1221         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
1222            "should have 0\n",ref);
1223     }
1224
1225 EXIT:
1226     ref=IDirectSound_Release(dso);
1227     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
1228     if (ref!=0)
1229 return DSERR_GENERIC;
1230
1231     return rc;
1232 }
1233
1234 static BOOL WINAPI dsenum_callback(LPGUID lpGuid, LPCSTR lpcstrDescription,
1235                                    LPCSTR lpcstrModule, LPVOID lpContext)
1236 {
1237     HRESULT rc;
1238     trace("*** Testing %s - %s ***\n",lpcstrDescription,lpcstrModule);
1239
1240     rc = test_for_driver(lpGuid);
1241     if (rc == DSERR_NODRIVER) {
1242         trace("  No Driver\n");
1243         return 1;
1244     } else if (rc == DSERR_ALLOCATED) {
1245         trace("  Already In Use\n");
1246         return 1;
1247     } else if (rc == E_FAIL) {
1248         trace("  No Device\n");
1249         return 1;
1250     }
1251
1252     trace("  Testing the primary buffer\n");
1253     test_primary(lpGuid);
1254
1255     trace("  Testing 3D primary buffer\n");
1256     test_primary_3d(lpGuid);
1257
1258     trace("  Testing 3D primary buffer with listener\n");
1259     test_primary_3d_with_listener(lpGuid);
1260
1261     /* Testing secondary buffers */
1262     test_secondary(lpGuid,winetest_interactive,0,0,0,0,0,0);
1263     test_secondary(lpGuid,winetest_interactive,0,0,0,1,0,0);
1264
1265     /* Testing 3D secondary buffers */
1266     test_secondary(lpGuid,winetest_interactive,1,0,0,0,0,0);
1267     test_secondary(lpGuid,winetest_interactive,1,1,0,0,0,0);
1268     test_secondary(lpGuid,winetest_interactive,1,1,0,1,0,0);
1269     test_secondary(lpGuid,winetest_interactive,1,0,1,0,0,0);
1270     test_secondary(lpGuid,winetest_interactive,1,0,1,1,0,0);
1271     test_secondary(lpGuid,winetest_interactive,1,1,1,0,0,0);
1272     test_secondary(lpGuid,winetest_interactive,1,1,1,1,0,0);
1273     test_secondary(lpGuid,winetest_interactive,1,1,1,0,1,0);
1274     test_secondary(lpGuid,winetest_interactive,1,1,1,0,0,1);
1275     test_secondary(lpGuid,winetest_interactive,1,1,1,0,1,1);
1276
1277     return 1;
1278 }
1279
1280 static void ds3d_tests(void)
1281 {
1282     HRESULT rc;
1283     rc=DirectSoundEnumerateA(&dsenum_callback,NULL);
1284     ok(rc==DS_OK,"DirectSoundEnumerateA() failed: %s\n",DXGetErrorString8(rc));
1285 }
1286
1287 START_TEST(ds3d)
1288 {
1289     CoInitialize(NULL);
1290
1291     trace("DLL Version: %s\n", get_file_version("dsound.dll"));
1292
1293     ds3d_tests();
1294
1295     CoUninitialize();
1296 }