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