mshtml: Added IHTMLWindow2::focus implementation.
[wine] / dlls / dsound / tests / ds3d8.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 "mmreg.h"
33 #include "ks.h"
34 #include "ksmedia.h"
35 #include "dsound_test.h"
36
37 static HRESULT (WINAPI *pDirectSoundEnumerateA)(LPDSENUMCALLBACKA,LPVOID)=NULL;
38 static HRESULT (WINAPI *pDirectSoundCreate8)(LPCGUID,LPDIRECTSOUND8*,LPUNKNOWN)=NULL;
39
40 typedef struct {
41     char* wave;
42     DWORD wave_len;
43
44     LPDIRECTSOUNDBUFFER dsbo;
45     LPWAVEFORMATEX wfx;
46     DWORD buffer_size;
47     DWORD written;
48     DWORD played;
49     DWORD offset;
50 } play_state_t;
51
52 static int buffer_refill8(play_state_t* state, DWORD size)
53 {
54     LPVOID ptr1,ptr2;
55     DWORD len1,len2;
56     HRESULT rc;
57
58     if (size>state->wave_len-state->written)
59         size=state->wave_len-state->written;
60
61     rc=IDirectSoundBuffer_Lock(state->dsbo,state->offset,size,
62                                &ptr1,&len1,&ptr2,&len2,0);
63     ok(rc==DS_OK,"IDirectSoundBuffer_Lock() failed: %08x\n", rc);
64     if (rc!=DS_OK)
65         return -1;
66
67     memcpy(ptr1,state->wave+state->written,len1);
68     state->written+=len1;
69     if (ptr2!=NULL) {
70         memcpy(ptr2,state->wave+state->written,len2);
71         state->written+=len2;
72     }
73     state->offset=state->written % state->buffer_size;
74     rc=IDirectSoundBuffer_Unlock(state->dsbo,ptr1,len1,ptr2,len2);
75     ok(rc==DS_OK,"IDirectSoundBuffer_Unlock() failed: %08x\n", rc);
76     if (rc!=DS_OK)
77         return -1;
78     return size;
79 }
80
81 static int buffer_silence8(play_state_t* state, DWORD size)
82 {
83     LPVOID ptr1,ptr2;
84     DWORD len1,len2;
85     HRESULT rc;
86     BYTE s;
87
88     rc=IDirectSoundBuffer_Lock(state->dsbo,state->offset,size,
89                                &ptr1,&len1,&ptr2,&len2,0);
90     ok(rc==DS_OK,"IDirectSoundBuffer_Lock() failed: %08x\n", rc);
91     if (rc!=DS_OK)
92         return -1;
93
94     s=(state->wfx->wBitsPerSample==8?0x80:0);
95     memset(ptr1,s,len1);
96     if (ptr2!=NULL) {
97         memset(ptr2,s,len2);
98     }
99     state->offset=(state->offset+size) % state->buffer_size;
100     rc=IDirectSoundBuffer_Unlock(state->dsbo,ptr1,len1,ptr2,len2);
101     ok(rc==DS_OK,"IDirectSoundBuffer_Unlock() failed: %08x\n", rc);
102     if (rc!=DS_OK)
103         return -1;
104     return size;
105 }
106
107 static int buffer_service8(play_state_t* state)
108 {
109     DWORD last_play_pos,play_pos,buf_free;
110     HRESULT rc;
111
112     rc=IDirectSoundBuffer_GetCurrentPosition(state->dsbo,&play_pos,NULL);
113     ok(rc==DS_OK,"IDirectSoundBuffer_GetCurrentPosition() failed: %08x\n", rc);
114     if (rc!=DS_OK) {
115         goto STOP;
116     }
117
118     /* Update the amount played */
119     last_play_pos=state->played % state->buffer_size;
120     if (play_pos<last_play_pos)
121         state->played+=state->buffer_size-last_play_pos+play_pos;
122     else
123         state->played+=play_pos-last_play_pos;
124
125     if (winetest_debug > 1)
126         trace("buf size=%d last_play_pos=%d play_pos=%d played=%d / %d\n",
127               state->buffer_size,last_play_pos,play_pos,state->played,
128               state->wave_len);
129
130     if (state->played>state->wave_len)
131     {
132         /* Everything has been played */
133         goto STOP;
134     }
135
136     /* Refill the buffer */
137     if (state->offset<=play_pos)
138         buf_free=play_pos-state->offset;
139     else
140         buf_free=state->buffer_size-state->offset+play_pos;
141
142     if (winetest_debug > 1)
143         trace("offset=%d free=%d written=%d / %d\n",
144               state->offset,buf_free,state->written,state->wave_len);
145     if (buf_free==0)
146         return 1;
147
148     if (state->written<state->wave_len)
149     {
150         int w=buffer_refill8(state,buf_free);
151         if (w==-1)
152             goto STOP;
153         buf_free-=w;
154         if (state->written==state->wave_len && winetest_debug > 1)
155             trace("last sound byte at %d\n",
156                   (state->written % state->buffer_size));
157     }
158
159     if (buf_free>0) {
160         /* Fill with silence */
161         if (winetest_debug > 1)
162             trace("writing %d bytes of silence\n",buf_free);
163         if (buffer_silence8(state,buf_free)==-1)
164             goto STOP;
165     }
166     return 1;
167
168 STOP:
169     if (winetest_debug > 1)
170         trace("stopping playback\n");
171     rc=IDirectSoundBuffer_Stop(state->dsbo);
172     ok(rc==DS_OK,"IDirectSoundBuffer_Stop() failed: %08x\n", rc);
173     return 0;
174 }
175
176 void test_buffer8(LPDIRECTSOUND8 dso, LPDIRECTSOUNDBUFFER * dsbo,
177                   BOOL is_primary, BOOL set_volume, LONG volume,
178                   BOOL set_pan, LONG pan, BOOL play, double duration,
179                   BOOL buffer3d, LPDIRECTSOUND3DLISTENER listener,
180                   BOOL move_listener, BOOL move_sound)
181 {
182     HRESULT rc;
183     DSBCAPS dsbcaps;
184     WAVEFORMATEX wfx,wfx2;
185     DWORD size,status,freq;
186     BOOL ieee = FALSE;
187     int ref;
188
189     /* DSOUND: Error: Invalid caps pointer */
190     rc=IDirectSoundBuffer_GetCaps(*dsbo,0);
191     ok(rc==DSERR_INVALIDPARAM,"IDirectSoundBuffer_GetCaps() should have "
192        "returned DSERR_INVALIDPARAM, returned: %08x\n",rc);
193
194     ZeroMemory(&dsbcaps, sizeof(dsbcaps));
195
196     /* DSOUND: Error: Invalid caps pointer */
197     rc=IDirectSoundBuffer_GetCaps(*dsbo,&dsbcaps);
198     ok(rc==DSERR_INVALIDPARAM,"IDirectSoundBuffer_GetCaps() should have "
199        "returned DSERR_INVALIDPARAM, returned: %08x\n",rc);
200
201     dsbcaps.dwSize=sizeof(dsbcaps);
202     rc=IDirectSoundBuffer_GetCaps(*dsbo,&dsbcaps);
203     ok(rc==DS_OK,"IDirectSoundBuffer_GetCaps() failed: %08x\n", rc);
204     if (rc==DS_OK && winetest_debug > 1) {
205         trace("    Caps: flags=0x%08x size=%d\n",dsbcaps.dwFlags,
206               dsbcaps.dwBufferBytes);
207     }
208
209     /* Query the format size. */
210     size=0;
211     rc=IDirectSoundBuffer_GetFormat(*dsbo,NULL,0,&size);
212     ok(rc==DS_OK && size!=0,"IDirectSoundBuffer_GetFormat() should have "
213        "returned the needed size: rc=%08x size=%d\n",rc,size);
214
215     ok(size == sizeof(WAVEFORMATEX) || size == sizeof(WAVEFORMATEXTENSIBLE),
216        "Expected a correct structure size, got %d\n", size);
217
218     if (size == sizeof(WAVEFORMATEX)) {
219         rc=IDirectSoundBuffer_GetFormat(*dsbo,&wfx,size,NULL);
220         ieee = (wfx.wFormatTag == WAVE_FORMAT_IEEE_FLOAT);
221     } else if (size == sizeof(WAVEFORMATEXTENSIBLE)) {
222         WAVEFORMATEXTENSIBLE wfxe;
223         rc=IDirectSoundBuffer_GetFormat(*dsbo,(WAVEFORMATEX*)&wfxe,size,NULL);
224         wfx = wfxe.Format;
225         ieee = IsEqualGUID(&wfxe.SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT);
226     }
227     ok(rc==DS_OK,"IDirectSoundBuffer_GetFormat() failed: %08x\n", rc);
228     if (rc==DS_OK && winetest_debug > 1) {
229         trace("    Format: %s tag=0x%04x %dx%dx%d avg.B/s=%d align=%d\n",
230               is_primary ? "Primary" : "Secondary",
231               wfx.wFormatTag,wfx.nSamplesPerSec,wfx.wBitsPerSample,
232               wfx.nChannels,wfx.nAvgBytesPerSec,wfx.nBlockAlign);
233     }
234
235     /* DSOUND: Error: Invalid frequency buffer */
236     rc=IDirectSoundBuffer_GetFrequency(*dsbo,0);
237     ok(rc==DSERR_INVALIDPARAM,"IDirectSoundBuffer_GetFrequency() should have "
238        "returned DSERR_INVALIDPARAM, returned: %08x\n",rc);
239
240     /* DSOUND: Error: Primary buffers don't support CTRLFREQUENCY */
241     rc=IDirectSoundBuffer_GetFrequency(*dsbo,&freq);
242     ok((rc==DS_OK && !is_primary) || (rc==DSERR_CONTROLUNAVAIL&&is_primary) ||
243        (rc==DSERR_CONTROLUNAVAIL&&!(dsbcaps.dwFlags&DSBCAPS_CTRLFREQUENCY)),
244        "IDirectSoundBuffer_GetFrequency() failed: %08x\n",rc);
245     if (rc==DS_OK) {
246         ok(freq==wfx.nSamplesPerSec,"The frequency returned by GetFrequency "
247            "%d does not match the format %d\n",freq,wfx.nSamplesPerSec);
248     }
249
250     /* DSOUND: Error: Invalid status pointer */
251     rc=IDirectSoundBuffer_GetStatus(*dsbo,0);
252     ok(rc==DSERR_INVALIDPARAM,"IDirectSoundBuffer_GetStatus() should have "
253        "returned DSERR_INVALIDPARAM, returned: %08x\n",rc);
254
255     rc=IDirectSoundBuffer_GetStatus(*dsbo,&status);
256     ok(rc==DS_OK,"IDirectSoundBuffer_GetStatus() failed: %08x\n", rc);
257     ok(status==0,"status=0x%x instead of 0\n",status);
258
259     if (is_primary) {
260         DSBCAPS new_dsbcaps;
261         /* We must call SetCooperativeLevel to be allowed to call SetFormat */
262         /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
263         rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
264         ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_PRIORITY) "
265            "failed: %08x\n",rc);
266         if (rc!=DS_OK)
267             return;
268
269         /* DSOUND: Error: Invalid format pointer */
270         rc=IDirectSoundBuffer_SetFormat(*dsbo,0);
271         ok(rc==DSERR_INVALIDPARAM,"IDirectSoundBuffer_SetFormat() should have "
272            "returned DSERR_INVALIDPARAM, returned: %08x\n",rc);
273
274         init_format(&wfx2,WAVE_FORMAT_PCM,11025,16,2);
275         rc=IDirectSoundBuffer_SetFormat(*dsbo,&wfx2);
276         ok(rc==DS_OK,"IDirectSoundBuffer_SetFormat(%s) failed: %08x\n",
277            format_string(&wfx2), rc);
278
279         /* There is no guarantee that SetFormat will actually change the
280          * format to what we asked for. It depends on what the soundcard
281          * supports. So we must re-query the format.
282          */
283         rc=IDirectSoundBuffer_GetFormat(*dsbo,&wfx,sizeof(wfx),NULL);
284         ok(rc==DS_OK,"IDirectSoundBuffer_GetFormat() failed: %08x\n", rc);
285         if (rc==DS_OK &&
286             (wfx.wFormatTag!=wfx2.wFormatTag ||
287              wfx.nSamplesPerSec!=wfx2.nSamplesPerSec ||
288              wfx.wBitsPerSample!=wfx2.wBitsPerSample ||
289              wfx.nChannels!=wfx2.nChannels)) {
290             trace("Requested format tag=0x%04x %dx%dx%d avg.B/s=%d align=%d\n",
291                   wfx2.wFormatTag,wfx2.nSamplesPerSec,wfx2.wBitsPerSample,
292                   wfx2.nChannels,wfx2.nAvgBytesPerSec,wfx2.nBlockAlign);
293             trace("Got tag=0x%04x %dx%dx%d avg.B/s=%d align=%d\n",
294                   wfx.wFormatTag,wfx.nSamplesPerSec,wfx.wBitsPerSample,
295                   wfx.nChannels,wfx.nAvgBytesPerSec,wfx.nBlockAlign);
296         }
297
298         ZeroMemory(&new_dsbcaps, sizeof(new_dsbcaps));
299         new_dsbcaps.dwSize = sizeof(new_dsbcaps);
300         rc=IDirectSoundBuffer_GetCaps(*dsbo,&new_dsbcaps);
301         ok(rc==DS_OK,"IDirectSoundBuffer_GetCaps() failed: %08x\n", rc);
302         if (rc==DS_OK && winetest_debug > 1) {
303             trace("    new Caps: flags=0x%08x size=%d\n",new_dsbcaps.dwFlags,
304                   new_dsbcaps.dwBufferBytes);
305         }
306
307         /* Check for primary buffer size change */
308         ok(new_dsbcaps.dwBufferBytes == dsbcaps.dwBufferBytes,
309            "    buffer size changed after SetFormat() - "
310            "previous size was %u, current size is %u\n",
311            dsbcaps.dwBufferBytes, new_dsbcaps.dwBufferBytes);
312         dsbcaps.dwBufferBytes = new_dsbcaps.dwBufferBytes;
313
314         /* Check for primary buffer flags change */
315         ok(new_dsbcaps.dwFlags == dsbcaps.dwFlags,
316            "    flags changed after SetFormat() - "
317            "previous flags were %08x, current flags are %08x\n",
318            dsbcaps.dwFlags, new_dsbcaps.dwFlags);
319
320         /* Set the CooperativeLevel back to normal */
321         /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
322         rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
323         ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_NORMAL) "
324            "failed: %08x\n",rc);
325     }
326
327     if (play) {
328         play_state_t state;
329         DS3DLISTENER listener_param;
330         LPDIRECTSOUND3DBUFFER buffer=NULL;
331         DS3DBUFFER buffer_param;
332         DWORD start_time,now;
333         LPVOID buffer1;
334         DWORD length1;
335
336         if (winetest_interactive) {
337             trace("    Playing %g second 440Hz tone at %dx%dx%d\n", duration,
338                   wfx.nSamplesPerSec, wfx.wBitsPerSample,wfx.nChannels);
339         }
340
341         if (is_primary) {
342             /* We must call SetCooperativeLevel to be allowed to call Lock */
343             /* DSOUND: Setting DirectSound cooperative level to
344              * DSSCL_WRITEPRIMARY */
345             rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),
346                                                  DSSCL_WRITEPRIMARY);
347             ok(rc==DS_OK,
348                "IDirectSound8_SetCooperativeLevel(DSSCL_WRITEPRIMARY) failed: %08x\n",rc);
349             if (rc!=DS_OK)
350                 return;
351         }
352         if (buffer3d) {
353             LPDIRECTSOUNDBUFFER temp_buffer;
354
355             rc=IDirectSoundBuffer_QueryInterface(*dsbo,&IID_IDirectSound3DBuffer,
356                                                  (LPVOID *)&buffer);
357             ok(rc==DS_OK,"IDirectSoundBuffer_QueryInterface() failed: %08x\n", rc);
358             if (rc!=DS_OK)
359                 return;
360
361             /* check the COM interface */
362             rc=IDirectSoundBuffer_QueryInterface(*dsbo, &IID_IDirectSoundBuffer,
363                                                  (LPVOID *)&temp_buffer);
364             ok(rc==DS_OK && temp_buffer!=NULL,
365                "IDirectSoundBuffer_QueryInterface() failed: %08x\n", rc);
366             ok(temp_buffer==*dsbo,"COM interface broken: %p != %p\n",
367                temp_buffer,*dsbo);
368             ref=IDirectSoundBuffer_Release(temp_buffer);
369             ok(ref==1,"IDirectSoundBuffer_Release() has %d references, "
370                "should have 1\n",ref);
371
372             temp_buffer=NULL;
373             rc=IDirectSound3DBuffer_QueryInterface(*dsbo, &IID_IDirectSoundBuffer,
374                                                    (LPVOID *)&temp_buffer);
375             ok(rc==DS_OK && temp_buffer!=NULL,
376                "IDirectSound3DBuffer_QueryInterface() failed: %08x\n", rc);
377             ok(temp_buffer==*dsbo,"COM interface broken: %p != %p\n",
378                temp_buffer,*dsbo);
379             ref=IDirectSoundBuffer_Release(temp_buffer);
380             ok(ref==1,"IDirectSoundBuffer_Release() has %d references, "
381                "should have 1\n",ref);
382
383             ref=IDirectSoundBuffer_Release(*dsbo);
384             ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
385                "should have 0\n",ref);
386
387             rc=IDirectSound3DBuffer_QueryInterface(buffer,
388                                                    &IID_IDirectSoundBuffer,
389                                                    (LPVOID *)dsbo);
390             ok(rc==DS_OK && *dsbo!=NULL,"IDirectSound3DBuffer_QueryInterface() "
391                "failed: %08x\n",rc);
392
393             /* DSOUND: Error: Invalid buffer */
394             rc=IDirectSound3DBuffer_GetAllParameters(buffer,0);
395             ok(rc==DSERR_INVALIDPARAM,"IDirectSound3DBuffer_GetAllParameters() "
396                "failed: %08x\n",rc);
397
398             ZeroMemory(&buffer_param, sizeof(buffer_param));
399
400             /* DSOUND: Error: Invalid buffer */
401             rc=IDirectSound3DBuffer_GetAllParameters(buffer,&buffer_param);
402             ok(rc==DSERR_INVALIDPARAM,"IDirectSound3DBuffer_GetAllParameters() "
403                "failed: %08x\n",rc);
404
405             buffer_param.dwSize=sizeof(buffer_param);
406             rc=IDirectSound3DBuffer_GetAllParameters(buffer,&buffer_param);
407             ok(rc==DS_OK,"IDirectSound3DBuffer_GetAllParameters() failed: %08x\n", rc);
408         }
409         if (set_volume) {
410             if (dsbcaps.dwFlags & DSBCAPS_CTRLVOLUME) {
411                 LONG val;
412                 rc=IDirectSoundBuffer_GetVolume(*dsbo,&val);
413                 ok(rc==DS_OK,"IDirectSoundBuffer_GetVolume() failed: %08x\n", rc);
414
415                 rc=IDirectSoundBuffer_SetVolume(*dsbo,volume);
416                 ok(rc==DS_OK,"IDirectSoundBuffer_SetVolume() failed: %08x\n", rc);
417             } else {
418                 /* DSOUND: Error: Buffer does not have CTRLVOLUME */
419                 rc=IDirectSoundBuffer_GetVolume(*dsbo,&volume);
420                 ok(rc==DSERR_CONTROLUNAVAIL,"IDirectSoundBuffer_GetVolume() "
421                    "should have returned DSERR_CONTROLUNAVAIL, returned: %08x\n", rc);
422             }
423         }
424
425         if (set_pan) {
426             if (dsbcaps.dwFlags & DSBCAPS_CTRLPAN) {
427                 LONG val;
428                 rc=IDirectSoundBuffer_GetPan(*dsbo,&val);
429                 ok(rc==DS_OK,"IDirectSoundBuffer_GetPan() failed: %08x\n", rc);
430
431                 rc=IDirectSoundBuffer_SetPan(*dsbo,pan);
432                 ok(rc==DS_OK,"IDirectSoundBuffer_SetPan() failed: %08x\n", rc);
433             } else {
434                 /* DSOUND: Error: Buffer does not have CTRLPAN */
435                 rc=IDirectSoundBuffer_GetPan(*dsbo,&pan);
436                 ok(rc==DSERR_CONTROLUNAVAIL,"IDirectSoundBuffer_GetPan() "
437                    "should have returned DSERR_CONTROLUNAVAIL, returned: %08x\n", rc);
438             }
439         }
440
441         /* try an offset past the end of the buffer */
442         rc = IDirectSoundBuffer_Lock(*dsbo, dsbcaps.dwBufferBytes, 0, &buffer1,
443                                       &length1, NULL, NULL,
444                                       DSBLOCK_ENTIREBUFFER);
445         ok(rc==DSERR_INVALIDPARAM, "IDirectSoundBuffer_Lock() should have "
446            "returned DSERR_INVALIDPARAM, returned %08x\n", rc);
447
448         /* try a size larger than the buffer */
449         rc = IDirectSoundBuffer_Lock(*dsbo, 0, dsbcaps.dwBufferBytes + 1,
450                                      &buffer1, &length1, NULL, NULL,
451                                      DSBLOCK_FROMWRITECURSOR);
452         ok(rc==DSERR_INVALIDPARAM, "IDirectSoundBuffer_Lock() should have "
453            "returned DSERR_INVALIDPARAM, returned %08x\n", rc);
454
455         state.wave=wave_generate_la(&wfx,duration,&state.wave_len,ieee);
456
457         state.dsbo=*dsbo;
458         state.wfx=&wfx;
459         state.buffer_size=dsbcaps.dwBufferBytes;
460         state.played=state.written=state.offset=0;
461         buffer_refill8(&state,state.buffer_size);
462
463         rc=IDirectSoundBuffer_Play(*dsbo,0,0,DSBPLAY_LOOPING);
464         ok(rc==DS_OK,"IDirectSoundBuffer_Play() failed: %08x\n", rc);
465
466         rc=IDirectSoundBuffer_GetStatus(*dsbo,&status);
467         ok(rc==DS_OK,"IDirectSoundBuffer_GetStatus() failed: %08x\n", rc);
468         ok(status==(DSBSTATUS_PLAYING|DSBSTATUS_LOOPING),
469            "GetStatus: bad status: %x\n",status);
470
471         if (listener) {
472             ZeroMemory(&listener_param,sizeof(listener_param));
473             listener_param.dwSize=sizeof(listener_param);
474             rc=IDirectSound3DListener_GetAllParameters(listener,&listener_param);
475             ok(rc==DS_OK,"IDirectSound3dListener_GetAllParameters() "
476                "failed: %08x\n",rc);
477             if (move_listener) {
478                 listener_param.vPosition.x = -5.0f;
479                 listener_param.vVelocity.x = (float)(10.0/duration);
480             }
481             rc=IDirectSound3DListener_SetAllParameters(listener,
482                                                        &listener_param,
483                                                        DS3D_IMMEDIATE);
484             ok(rc==DS_OK,"IDirectSound3dListener_SetPosition() failed: %08x\n", rc);
485         }
486         if (buffer3d) {
487             if (move_sound) {
488                 buffer_param.vPosition.x = 100.0f;
489                 buffer_param.vVelocity.x = (float)(-200.0/duration);
490             }
491             buffer_param.flMinDistance = 10;
492             rc=IDirectSound3DBuffer_SetAllParameters(buffer,&buffer_param,
493                                                      DS3D_IMMEDIATE);
494             ok(rc==DS_OK,"IDirectSound3dBuffer_SetPosition() failed: %08x\n", rc);
495         }
496
497         start_time=GetTickCount();
498         while (buffer_service8(&state)) {
499             WaitForSingleObject(GetCurrentProcess(),TIME_SLICE);
500             now=GetTickCount();
501             if (listener && move_listener) {
502                 listener_param.vPosition.x = (float)(-5.0+10.0*(now-start_time)/1000/duration);
503                 if (winetest_debug>2)
504                     trace("listener position=%g\n",listener_param.vPosition.x);
505                 rc=IDirectSound3DListener_SetPosition(listener,
506                     listener_param.vPosition.x,listener_param.vPosition.y,
507                     listener_param.vPosition.z,DS3D_IMMEDIATE);
508                 ok(rc==DS_OK,"IDirectSound3dListener_SetPosition() failed: %08x\n",rc);
509             }
510             if (buffer3d && move_sound) {
511                 buffer_param.vPosition.x = (float)(100-200.0*(now-start_time)/1000/duration);
512                 if (winetest_debug>2)
513                     trace("sound position=%g\n",buffer_param.vPosition.x);
514                 rc=IDirectSound3DBuffer_SetPosition(buffer,
515                     buffer_param.vPosition.x,buffer_param.vPosition.y,
516                     buffer_param.vPosition.z,DS3D_IMMEDIATE);
517                 ok(rc==DS_OK,"IDirectSound3dBuffer_SetPosition() failed: %08x\n", rc);
518             }
519         }
520         /* Check the sound duration was within 10% of the expected value */
521         now=GetTickCount();
522         ok(fabs(1000*duration-now+start_time)<=100*duration,
523            "The sound played for %d ms instead of %g ms\n",
524            now-start_time,1000*duration);
525
526         HeapFree(GetProcessHeap(), 0, state.wave);
527         if (is_primary) {
528             /* Set the CooperativeLevel back to normal */
529             /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
530             rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
531             ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_NORMAL) "
532                "failed: %08x\n",rc);
533         }
534         if (buffer3d) {
535             ref=IDirectSound3DBuffer_Release(buffer);
536             ok(ref==0,"IDirectSound3DBuffer_Release() has %d references, "
537                "should have 0\n",ref);
538         }
539     }
540 }
541
542 static HRESULT test_secondary8(LPGUID lpGuid, int play,
543                                int has_3d, int has_3dbuffer,
544                                int has_listener, int has_duplicate,
545                                int move_listener, int move_sound)
546 {
547     HRESULT rc;
548     LPDIRECTSOUND8 dso=NULL;
549     LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
550     LPDIRECTSOUND3DLISTENER listener=NULL;
551     DSBUFFERDESC bufdesc;
552     WAVEFORMATEX wfx, wfx1;
553     int ref;
554
555     /* Create the DirectSound object */
556     rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
557     ok(rc==DS_OK||rc==DSERR_NODRIVER,"DirectSoundCreate8() failed: %08x\n", rc);
558     if (rc!=DS_OK)
559         return rc;
560
561     /* We must call SetCooperativeLevel before creating primary buffer */
562     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
563     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
564     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_PRIORITY) failed: %08x\n",rc);
565     if (rc!=DS_OK)
566         goto EXIT;
567
568     ZeroMemory(&bufdesc, sizeof(bufdesc));
569     bufdesc.dwSize=sizeof(bufdesc);
570     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
571     if (has_3d)
572         bufdesc.dwFlags|=DSBCAPS_CTRL3D;
573     else
574         bufdesc.dwFlags|=(DSBCAPS_CTRLVOLUME|DSBCAPS_CTRLPAN);
575     rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
576     ok((rc==DS_OK && primary!=NULL) || (rc == DSERR_CONTROLUNAVAIL),
577        "IDirectSound8_CreateSoundBuffer() failed to create a %sprimary buffer: %08x\n",has_3d?"3D ":"", rc);
578     if (rc == DSERR_CONTROLUNAVAIL)
579         trace("  No Primary\n");
580     else if (rc==DS_OK && primary!=NULL) {
581         rc=IDirectSoundBuffer_GetFormat(primary,&wfx1,sizeof(wfx1),NULL);
582         ok(rc==DS_OK,"IDirectSoundBuffer8_Getformat() failed: %08x\n", rc);
583         if (rc!=DS_OK)
584             goto EXIT1;
585
586         if (has_listener) {
587             rc=IDirectSoundBuffer_QueryInterface(primary,
588                                                  &IID_IDirectSound3DListener,
589                                                  (void **)&listener);
590             ok(rc==DS_OK && listener!=NULL,
591                "IDirectSoundBuffer_QueryInterface() failed to get a 3D "
592                "listener %08x\n",rc);
593             ref=IDirectSoundBuffer_Release(primary);
594             ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
595                "should have 0\n",ref);
596             if (rc==DS_OK && listener!=NULL) {
597                 DS3DLISTENER listener_param;
598                 ZeroMemory(&listener_param,sizeof(listener_param));
599                 /* DSOUND: Error: Invalid buffer */
600                 rc=IDirectSound3DListener_GetAllParameters(listener,0);
601                 ok(rc==DSERR_INVALIDPARAM,
602                    "IDirectSound3dListener_GetAllParameters() should have "
603                    "returned DSERR_INVALIDPARAM, returned: %08x\n", rc);
604
605                 /* DSOUND: Error: Invalid buffer */
606                 rc=IDirectSound3DListener_GetAllParameters(listener,
607                                                            &listener_param);
608                 ok(rc==DSERR_INVALIDPARAM,
609                    "IDirectSound3dListener_GetAllParameters() should have "
610                    "returned DSERR_INVALIDPARAM, returned: %08x\n", rc);
611
612                 listener_param.dwSize=sizeof(listener_param);
613                 rc=IDirectSound3DListener_GetAllParameters(listener,
614                                                            &listener_param);
615                 ok(rc==DS_OK,"IDirectSound3dListener_GetAllParameters() "
616                    "failed: %08x\n",rc);
617             } else {
618                 ok(listener==NULL, "IDirectSoundBuffer_QueryInterface() "
619                    "failed but returned a listener anyway\n");
620                 ok(rc!=DS_OK, "IDirectSoundBuffer_QueryInterface() succeeded "
621                    "but returned a NULL listener\n");
622                 if (listener) {
623                     ref=IDirectSound3DListener_Release(listener);
624                     ok(ref==0,"IDirectSound3dListener_Release() listener has "
625                        "%d references, should have 0\n",ref);
626                 }
627                 goto EXIT2;
628             }
629         }
630
631         init_format(&wfx,WAVE_FORMAT_PCM,22050,16,2);
632         secondary=NULL;
633         ZeroMemory(&bufdesc, sizeof(bufdesc));
634         bufdesc.dwSize=sizeof(bufdesc);
635         bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
636         if (has_3d)
637             bufdesc.dwFlags|=DSBCAPS_CTRL3D;
638         else
639             bufdesc.dwFlags|=
640                 (DSBCAPS_CTRLFREQUENCY|DSBCAPS_CTRLVOLUME|DSBCAPS_CTRLPAN);
641         bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
642                                     wfx.nBlockAlign);
643         bufdesc.lpwfxFormat=&wfx;
644         if (has_3d) {
645             /* a stereo 3D buffer should fail */
646             rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
647             ok(rc==DSERR_INVALIDPARAM,
648                "IDirectSound8_CreateSoundBuffer(secondary) should have "
649                "returned DSERR_INVALIDPARAM, returned %08x\n", rc);
650             if (secondary)
651                 ref=IDirectSoundBuffer_Release(secondary);
652             init_format(&wfx,WAVE_FORMAT_PCM,22050,16,1);
653         }
654
655         if (winetest_interactive) {
656             trace("  Testing a %s%ssecondary buffer %s%s%s%sat %dx%dx%d "
657                   "with a primary buffer at %dx%dx%d\n",
658                   has_3dbuffer?"3D ":"",
659                   has_duplicate?"duplicated ":"",
660                   listener!=NULL||move_sound?"with ":"",
661                   move_listener?"moving ":"",
662                   listener!=NULL?"listener ":"",
663                   listener&&move_sound?"and moving sound ":move_sound?
664                   "moving sound ":"",
665                   wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,
666                   wfx1.nSamplesPerSec,wfx1.wBitsPerSample,wfx1.nChannels);
667         }
668         rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
669         ok(rc==DS_OK && secondary!=NULL,"IDirectSound8_CreateSoundBuffer() "
670            "failed to create a %s%ssecondary buffer %s%s%s%sat %dx%dx%d (%s): %08x\n",
671            has_3dbuffer?"3D ":"", has_duplicate?"duplicated ":"",
672            listener!=NULL||move_sound?"with ":"", move_listener?"moving ":"",
673            listener!=NULL?"listener ":"",
674            listener&&move_sound?"and moving sound ":move_sound?
675            "moving sound ":"",
676            wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,
677            getDSBCAPS(bufdesc.dwFlags),rc);
678         if (rc==DS_OK && secondary!=NULL) {
679             if (!has_3d) {
680                 LONG refvol,vol,refpan,pan;
681
682                 /* Check the initial secondary buffer's volume and pan */
683                 rc=IDirectSoundBuffer_GetVolume(secondary,&vol);
684                 ok(rc==DS_OK,"IDirectSoundBuffer_GetVolume(secondary) failed: %08x\n",rc);
685                 ok(vol==0,"wrong volume for a new secondary buffer: %d\n",vol);
686                 rc=IDirectSoundBuffer_GetPan(secondary,&pan);
687                 ok(rc==DS_OK,"IDirectSoundBuffer_GetPan(secondary) failed: %08x\n",rc);
688                 ok(pan==0,"wrong pan for a new secondary buffer: %d\n",pan);
689
690                 /* Check that changing the secondary buffer's volume and pan
691                  * does not impact the primary buffer's volume and pan
692                  */
693                 rc=IDirectSoundBuffer_GetVolume(primary,&refvol);
694                 ok(rc==DS_OK,"IDirectSoundBuffer_GetVolume(primary) failed: %08x\n",rc);
695                 rc=IDirectSoundBuffer_GetPan(primary,&refpan);
696                 ok(rc==DS_OK,"IDirectSoundBuffer_GetPan(primary) failed: %08x\n",rc);
697
698                 rc=IDirectSoundBuffer_SetVolume(secondary,-1000);
699                 ok(rc==DS_OK,"IDirectSoundBuffer_SetVolume(secondary) failed: %08x\n",rc);
700                 rc=IDirectSoundBuffer_GetVolume(secondary,&vol);
701                 ok(rc==DS_OK,"IDirectSoundBuffer_SetVolume(secondary) failed: %08x\n",rc);
702                 ok(vol==-1000,"secondary: wrong volume %d instead of -1000\n",
703                    vol);
704                 rc=IDirectSoundBuffer_SetPan(secondary,-1000);
705                 ok(rc==DS_OK,"IDirectSoundBuffer_SetPan(secondary) failed: %08x\n",rc);
706                 rc=IDirectSoundBuffer_GetPan(secondary,&pan);
707                 ok(rc==DS_OK,"IDirectSoundBuffer_SetPan(secondary) failed: %08x\n",rc);
708                 ok(pan==-1000,"secondary: wrong pan %d instead of -1000\n",
709                    pan);
710
711                 rc=IDirectSoundBuffer_GetVolume(primary,&vol);
712                 ok(rc==DS_OK,"IDirectSoundBuffer_`GetVolume(primary) failed: i%08x\n",rc);
713                 ok(vol==refvol,"The primary volume changed from %d to %d\n",
714                    refvol,vol);
715                 rc=IDirectSoundBuffer_GetPan(primary,&pan);
716                 ok(rc==DS_OK,"IDirectSoundBuffer_GetPan(primary) failed: %08x\n",rc);
717                 ok(pan==refpan,"The primary pan changed from %d to %d\n",
718                    refpan,pan);
719
720                 rc=IDirectSoundBuffer_SetVolume(secondary,0);
721                 ok(rc==DS_OK,"IDirectSoundBuffer_SetVolume(secondary) failed: %08x\n",rc);
722                 rc=IDirectSoundBuffer_SetPan(secondary,0);
723                 ok(rc==DS_OK,"IDirectSoundBuffer_SetPan(secondary) failed: %08x\n",rc);
724             }
725             if (has_duplicate) {
726                 LPDIRECTSOUNDBUFFER duplicated=NULL;
727
728                 /* DSOUND: Error: Invalid source buffer */
729                 rc=IDirectSound8_DuplicateSoundBuffer(dso,0,0);
730                 ok(rc==DSERR_INVALIDPARAM,
731                    "IDirectSound8_DuplicateSoundBuffer() should have returned "
732                    "DSERR_INVALIDPARAM, returned: %08x\n",rc);
733
734                 /* DSOUND: Error: Invalid dest buffer */
735                 rc=IDirectSound8_DuplicateSoundBuffer(dso,secondary,0);
736                 ok(rc==DSERR_INVALIDPARAM,
737                    "IDirectSound8_DuplicateSoundBuffer() should have returned "
738                    "DSERR_INVALIDPARAM, returned: %08x\n",rc);
739
740                 /* DSOUND: Error: Invalid source buffer */
741                 rc=IDirectSound8_DuplicateSoundBuffer(dso,0,&duplicated);
742                 ok(rc==DSERR_INVALIDPARAM,
743                    "IDirectSound8_DuplicateSoundBuffer() should have returned "
744                    "DSERR_INVALIDPARAM, returned: %08x\n",rc);
745
746                 duplicated=NULL;
747                 rc=IDirectSound8_DuplicateSoundBuffer(dso,secondary,
748                                                       &duplicated);
749                 ok(rc==DS_OK && duplicated!=NULL,
750                    "IDirectSound8_DuplicateSoundBuffer() failed to duplicate "
751                    "a secondary buffer: %08x\n",rc);
752
753                 if (rc==DS_OK && duplicated!=NULL) {
754                     ref=IDirectSoundBuffer_Release(secondary);
755                     ok(ref==0,"IDirectSoundBuffer_Release() secondary has %d "
756                        "references, should have 0\n",ref);
757                     secondary=duplicated;
758                 }
759             }
760
761             if (rc==DS_OK && secondary!=NULL) {
762                 double duration;
763                 duration=(move_listener || move_sound?4.0:1.0);
764                 test_buffer8(dso,&secondary,0,FALSE,0,FALSE,0,
765                              winetest_interactive,duration,has_3dbuffer,
766                              listener,move_listener,move_sound);
767                 ref=IDirectSoundBuffer_Release(secondary);
768                 ok(ref==0,"IDirectSoundBuffer_Release() %s has %d references, "
769                    "should have 0\n",has_duplicate?"duplicated":"secondary",
770                    ref);
771             }
772         }
773 EXIT1:
774         if (has_listener) {
775             if (listener) {
776                 ref=IDirectSound3DListener_Release(listener);
777                 ok(ref==0,"IDirectSound3dListener_Release() listener has %d "
778                    "references, should have 0\n",ref);
779             }
780         } else {
781             ref=IDirectSoundBuffer_Release(primary);
782             ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
783                "should have 0\n",ref);
784         }
785     } else {
786         ok(primary==NULL,"IDirectSound8_CreateSoundBuffer(primary) failed "
787            "but primary created anyway\n");
788         ok(rc!=DS_OK,"IDirectSound8_CreateSoundBuffer(primary) succeeded "
789            "but primary not created\n");
790         if (primary) {
791             ref=IDirectSoundBuffer_Release(primary);
792             ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
793                "should have 0\n",ref);
794         }
795     }
796 EXIT2:
797     /* Set the CooperativeLevel back to normal */
798     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
799     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
800     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_NORMAL) failed: %08x\n",rc);
801
802 EXIT:
803     ref=IDirectSound8_Release(dso);
804     ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",ref);
805     if (ref!=0)
806         return DSERR_GENERIC;
807
808     return rc;
809 }
810
811 static HRESULT test_for_driver8(LPGUID lpGuid)
812 {
813     HRESULT rc;
814     LPDIRECTSOUND8 dso=NULL;
815     int ref;
816
817     /* Create the DirectSound object */
818     rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
819     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
820        "DirectSoundCreate8() failed: %08x\n",rc);
821     if (rc!=DS_OK)
822         return rc;
823
824     ref=IDirectSound8_Release(dso);
825     ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",ref);
826     if (ref!=0)
827         return DSERR_GENERIC;
828
829     return rc;
830 }
831
832 static HRESULT test_primary8(LPGUID lpGuid)
833 {
834     HRESULT rc;
835     LPDIRECTSOUND8 dso=NULL;
836     LPDIRECTSOUNDBUFFER primary=NULL;
837     DSBUFFERDESC bufdesc;
838     DSCAPS dscaps;
839     int ref, i;
840
841     /* Create the DirectSound object */
842     rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
843     ok(rc==DS_OK||rc==DSERR_NODRIVER,"DirectSoundCreate8() failed: %08x\n", rc);
844     if (rc!=DS_OK)
845         return rc;
846
847     /* Get the device capabilities */
848     ZeroMemory(&dscaps, sizeof(dscaps));
849     dscaps.dwSize=sizeof(dscaps);
850     rc=IDirectSound8_GetCaps(dso,&dscaps);
851     ok(rc==DS_OK,"IDirectSound8_GetCaps() failed: %08x\n",rc);
852     if (rc!=DS_OK)
853         goto EXIT;
854
855     /* We must call SetCooperativeLevel before calling CreateSoundBuffer */
856     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
857     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
858     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_PRIORITY) failed: %08x\n",rc);
859     if (rc!=DS_OK)
860         goto EXIT;
861
862     /* Testing the primary buffer */
863     primary=NULL;
864     ZeroMemory(&bufdesc, sizeof(bufdesc));
865     bufdesc.dwSize=sizeof(bufdesc);
866     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRLVOLUME|DSBCAPS_CTRLPAN;
867     rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
868     ok((rc==DS_OK && primary!=NULL) || (rc == DSERR_CONTROLUNAVAIL),
869        "IDirectSound8_CreateSoundBuffer() failed to create a primary buffer: %08x\n",rc);
870     if (rc == DSERR_CONTROLUNAVAIL)
871         trace("  No Primary\n");
872     else if (rc==DS_OK && primary!=NULL) {
873         test_buffer8(dso,&primary,1,TRUE,0,TRUE,0,winetest_interactive &&
874                      !(dscaps.dwFlags & DSCAPS_EMULDRIVER),1.0,0,NULL,0,0);
875         if (winetest_interactive) {
876             LONG volume,pan;
877
878             volume = DSBVOLUME_MAX;
879             for (i = 0; i < 6; i++) {
880                 test_buffer8(dso,&primary,1,TRUE,volume,TRUE,0,
881                              winetest_interactive &&
882                              !(dscaps.dwFlags & DSCAPS_EMULDRIVER),
883                              1.0,0,NULL,0,0);
884                 volume -= ((DSBVOLUME_MAX-DSBVOLUME_MIN) / 40);
885             }
886
887             pan = DSBPAN_LEFT;
888             for (i = 0; i < 7; i++) {
889                 test_buffer8(dso,&primary,1,TRUE,0,TRUE,pan,
890                              winetest_interactive &&
891                              !(dscaps.dwFlags & DSCAPS_EMULDRIVER),1.0,0,0,0,0);
892                 pan += ((DSBPAN_RIGHT-DSBPAN_LEFT) / 6);
893             }
894         }
895         ref=IDirectSoundBuffer_Release(primary);
896         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
897            "should have 0\n",ref);
898     }
899
900     /* Set the CooperativeLevel back to normal */
901     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
902     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
903     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_NORMAL) failed: %08x\n",rc);
904
905 EXIT:
906     ref=IDirectSound8_Release(dso);
907     ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",ref);
908     if (ref!=0)
909         return DSERR_GENERIC;
910
911     return rc;
912 }
913
914 static HRESULT test_primary_3d8(LPGUID lpGuid)
915 {
916     HRESULT rc;
917     LPDIRECTSOUND8 dso=NULL;
918     LPDIRECTSOUNDBUFFER primary=NULL;
919     DSBUFFERDESC bufdesc;
920     DSCAPS dscaps;
921     int ref;
922
923     /* Create the DirectSound object */
924     rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
925     ok(rc==DS_OK||rc==DSERR_NODRIVER,"DirectSoundCreate8() failed: %08x\n", rc);
926     if (rc!=DS_OK)
927         return rc;
928
929     /* Get the device capabilities */
930     ZeroMemory(&dscaps, sizeof(dscaps));
931     dscaps.dwSize=sizeof(dscaps);
932     rc=IDirectSound8_GetCaps(dso,&dscaps);
933     ok(rc==DS_OK,"IDirectSound8_GetCaps failed: %08x\n",rc);
934     if (rc!=DS_OK)
935         goto EXIT;
936
937     /* We must call SetCooperativeLevel before calling CreateSoundBuffer */
938     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
939     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
940     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_PRIORITY) failed: %08x\n",rc);
941     if (rc!=DS_OK)
942         goto EXIT;
943
944     primary=NULL;
945     ZeroMemory(&bufdesc, sizeof(bufdesc));
946     bufdesc.dwSize=sizeof(bufdesc);
947     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
948     rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
949     ok(rc==DS_OK && primary!=NULL,"IDirectSound8_CreateSoundBuffer() failed "
950        "to create a primary buffer: %08x\n",rc);
951     if (rc==DS_OK && primary!=NULL) {
952         ref=IDirectSoundBuffer_Release(primary);
953         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
954            "should have 0\n",ref);
955         primary=NULL;
956         ZeroMemory(&bufdesc, sizeof(bufdesc));
957         bufdesc.dwSize=sizeof(bufdesc);
958         bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRL3D;
959         rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
960         ok(rc==DS_OK && primary!=NULL,"IDirectSound8_CreateSoundBuffer() "
961            "failed to create a 3D primary buffer: %08x\n",rc);
962         if (rc==DS_OK && primary!=NULL) {
963             test_buffer8(dso,&primary,1,FALSE,0,FALSE,0,
964                          winetest_interactive &&
965                          !(dscaps.dwFlags & DSCAPS_EMULDRIVER),1.0,0,0,0,0);
966             ref=IDirectSoundBuffer_Release(primary);
967             ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
968                "should have 0\n",ref);
969         }
970     }
971     /* Set the CooperativeLevel back to normal */
972     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
973     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
974     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_NORMAL) failed: %08x\n",rc);
975
976 EXIT:
977     ref=IDirectSound8_Release(dso);
978     ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",ref);
979     if (ref!=0)
980         return DSERR_GENERIC;
981
982     return rc;
983 }
984
985 static HRESULT test_primary_3d_with_listener8(LPGUID lpGuid)
986 {
987     HRESULT rc;
988     LPDIRECTSOUND8 dso=NULL;
989     LPDIRECTSOUNDBUFFER primary=NULL;
990     DSBUFFERDESC bufdesc;
991     DSCAPS dscaps;
992     int ref;
993
994     /* Create the DirectSound object */
995     rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
996     ok(rc==DS_OK||rc==DSERR_NODRIVER,"DirectSoundCreate8() failed: %08x\n", rc);
997     if (rc!=DS_OK)
998         return rc;
999
1000     /* Get the device capabilities */
1001     ZeroMemory(&dscaps, sizeof(dscaps));
1002     dscaps.dwSize=sizeof(dscaps);
1003     rc=IDirectSound8_GetCaps(dso,&dscaps);
1004     ok(rc==DS_OK,"IDirectSound8_GetCaps() failed: %08x\n",rc);
1005     if (rc!=DS_OK)
1006         goto EXIT;
1007
1008     /* We must call SetCooperativeLevel before calling CreateSoundBuffer */
1009     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
1010     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
1011     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_PRIORITY) failed: %08x\n",rc);
1012     if (rc!=DS_OK)
1013         goto EXIT;
1014     primary=NULL;
1015     ZeroMemory(&bufdesc, sizeof(bufdesc));
1016     bufdesc.dwSize=sizeof(bufdesc);
1017     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRL3D;
1018     rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
1019     ok(rc==DS_OK && primary!=NULL,"IDirectSound8_CreateSoundBuffer() failed "
1020        "to create a 3D primary buffer %08x\n",rc);
1021     if (rc==DS_OK && primary!=NULL) {
1022         LPDIRECTSOUND3DLISTENER listener=NULL;
1023         rc=IDirectSoundBuffer_QueryInterface(primary,
1024                                              &IID_IDirectSound3DListener,
1025                                              (void **)&listener);
1026         ok(rc==DS_OK && listener!=NULL,"IDirectSoundBuffer_QueryInterface() "
1027            "failed to get a 3D listener: %08x\n",rc);
1028         if (rc==DS_OK && listener!=NULL) {
1029             LPDIRECTSOUNDBUFFER temp_buffer=NULL;
1030
1031             /* Checking the COM interface */
1032             rc=IDirectSoundBuffer_QueryInterface(primary,
1033                                                  &IID_IDirectSoundBuffer,
1034                                                  (LPVOID *)&temp_buffer);
1035             ok(rc==DS_OK && temp_buffer!=NULL,
1036                "IDirectSoundBuffer_QueryInterface() failed: %08x\n", rc);
1037             ok(temp_buffer==primary,"COM interface broken: %p != %p\n",temp_buffer,primary);
1038             if (rc==DS_OK && temp_buffer!=NULL) {
1039                 ref=IDirectSoundBuffer_Release(temp_buffer);
1040                 ok(ref==1,"IDirectSoundBuffer_Release() has %d references, "
1041                    "should have 1\n",ref);
1042
1043                 temp_buffer=NULL;
1044                 rc=IDirectSound3DListener_QueryInterface(listener,
1045                     &IID_IDirectSoundBuffer,(LPVOID *)&temp_buffer);
1046                 ok(rc==DS_OK && temp_buffer!=NULL,
1047                    "IDirectSoundBuffer_QueryInterface() failed: %08x\n", rc);
1048                 ok(temp_buffer==primary,"COM interface broken: %p != %p\n",temp_buffer,primary);
1049                 ref=IDirectSoundBuffer_Release(temp_buffer);
1050                 ok(ref==1,"IDirectSoundBuffer_Release() has %d references, "
1051                    "should have 1\n",ref);
1052
1053                 /* Testing the buffer */
1054                 test_buffer8(dso,&primary,1,FALSE,0,FALSE,0,
1055                              winetest_interactive &&
1056                              !(dscaps.dwFlags & DSCAPS_EMULDRIVER),
1057                              1.0,0,listener,0,0);
1058             }
1059
1060             /* Testing the reference counting */
1061             ref=IDirectSound3DListener_Release(listener);
1062             ok(ref==0,"IDirectSound3DListener_Release() listener has %d "
1063                "references, should have 0\n",ref);
1064         }
1065
1066         /* Testing the reference counting */
1067         ref=IDirectSoundBuffer_Release(primary);
1068         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
1069            "should have 0\n",ref);
1070     }
1071
1072 EXIT:
1073     ref=IDirectSound8_Release(dso);
1074     ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",ref);
1075     if (ref!=0)
1076 return DSERR_GENERIC;
1077
1078     return rc;
1079 }
1080
1081 static BOOL WINAPI dsenum_callback(LPGUID lpGuid, LPCSTR lpcstrDescription,
1082                                    LPCSTR lpcstrModule, LPVOID lpContext)
1083 {
1084     HRESULT rc;
1085     trace("*** Testing %s - %s ***\n",lpcstrDescription,lpcstrModule);
1086
1087     rc = test_for_driver8(lpGuid);
1088     if (rc == DSERR_NODRIVER) {
1089         trace("  No Driver\n");
1090         return 1;
1091     } else if (rc == DSERR_ALLOCATED) {
1092         trace("  Already In Use\n");
1093         return 1;
1094     } else if (rc == E_FAIL) {
1095         trace("  No Device\n");
1096         return 1;
1097     }
1098
1099     trace("  Testing the primary buffer\n");
1100     test_primary8(lpGuid);
1101
1102     trace("  Testing 3D primary buffer\n");
1103     test_primary_3d8(lpGuid);
1104
1105     trace("  Testing 3D primary buffer with listener\n");
1106     test_primary_3d_with_listener8(lpGuid);
1107
1108     /* Testing secondary buffers */
1109     test_secondary8(lpGuid,winetest_interactive,0,0,0,0,0,0);
1110     test_secondary8(lpGuid,winetest_interactive,0,0,0,1,0,0);
1111
1112     /* Testing 3D secondary buffers */
1113     test_secondary8(lpGuid,winetest_interactive,1,0,0,0,0,0);
1114     test_secondary8(lpGuid,winetest_interactive,1,1,0,0,0,0);
1115     test_secondary8(lpGuid,winetest_interactive,1,1,0,1,0,0);
1116     test_secondary8(lpGuid,winetest_interactive,1,0,1,0,0,0);
1117     test_secondary8(lpGuid,winetest_interactive,1,0,1,1,0,0);
1118     test_secondary8(lpGuid,winetest_interactive,1,1,1,0,0,0);
1119     test_secondary8(lpGuid,winetest_interactive,1,1,1,1,0,0);
1120     test_secondary8(lpGuid,winetest_interactive,1,1,1,0,1,0);
1121     test_secondary8(lpGuid,winetest_interactive,1,1,1,0,0,1);
1122     test_secondary8(lpGuid,winetest_interactive,1,1,1,0,1,1);
1123
1124     return 1;
1125 }
1126
1127 static void ds3d8_tests(void)
1128 {
1129     HRESULT rc;
1130     rc=pDirectSoundEnumerateA(&dsenum_callback,NULL);
1131     ok(rc==DS_OK,"DirectSoundEnumerateA() failed: %08x\n",rc);
1132 }
1133
1134 START_TEST(ds3d8)
1135 {
1136     HMODULE hDsound;
1137
1138     CoInitialize(NULL);
1139
1140     hDsound = LoadLibrary("dsound.dll");
1141     if (hDsound)
1142     {
1143
1144         pDirectSoundEnumerateA = (void*)GetProcAddress(hDsound,
1145             "DirectSoundEnumerateA");
1146         pDirectSoundCreate8 = (void*)GetProcAddress(hDsound,
1147             "DirectSoundCreate8");
1148         if (pDirectSoundCreate8)
1149             ds3d8_tests();
1150         else
1151             skip("ds3d8 test skipped\n");
1152
1153         FreeLibrary(hDsound);
1154     }
1155     else
1156         skip("dsound.dll not found!\n");
1157
1158     CoUninitialize();
1159 }