wintrust: Add a helper function to initialize chain creation parameters.
[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         dsbcaps.dwBufferBytes = new_dsbcaps.dwBufferBytes;
311
312         /* Check for primary buffer flags change */
313         ok(new_dsbcaps.dwFlags == dsbcaps.dwFlags,
314            "    flags changed after SetFormat() - "
315            "previous flags were %08x, current flags are %08x\n",
316            dsbcaps.dwFlags, new_dsbcaps.dwFlags);
317
318         /* Set the CooperativeLevel back to normal */
319         /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
320         rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
321         ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_NORMAL) "
322            "failed: %s\n",DXGetErrorString8(rc));
323     }
324
325     if (play) {
326         play_state_t state;
327         DS3DLISTENER listener_param;
328         LPDIRECTSOUND3DBUFFER buffer=NULL;
329         DS3DBUFFER buffer_param;
330         DWORD start_time,now;
331         LPVOID buffer1;
332         DWORD length1;
333
334         if (winetest_interactive) {
335             trace("    Playing %g second 440Hz tone at %dx%dx%d\n", duration,
336                   wfx.nSamplesPerSec, wfx.wBitsPerSample,wfx.nChannels);
337         }
338
339         if (is_primary) {
340             /* We must call SetCooperativeLevel to be allowed to call Lock */
341             /* DSOUND: Setting DirectSound cooperative level to
342              * DSSCL_WRITEPRIMARY */
343             rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),
344                                                  DSSCL_WRITEPRIMARY);
345             ok(rc==DS_OK,
346                "IDirectSound8_SetCooperativeLevel(DSSCL_WRITEPRIMARY) failed: "
347                "%s\n",DXGetErrorString8(rc));
348             if (rc!=DS_OK)
349                 return;
350         }
351         if (buffer3d) {
352             LPDIRECTSOUNDBUFFER temp_buffer;
353
354             rc=IDirectSoundBuffer_QueryInterface(*dsbo,&IID_IDirectSound3DBuffer,
355                                                  (LPVOID *)&buffer);
356             ok(rc==DS_OK,"IDirectSoundBuffer_QueryInterface() failed: %s\n",
357                DXGetErrorString8(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: %s\n",
366                DXGetErrorString8(rc));
367             ok(temp_buffer==*dsbo,"COM interface broken: %p != %p\n",
368                temp_buffer,*dsbo);
369             ref=IDirectSoundBuffer_Release(temp_buffer);
370             ok(ref==1,"IDirectSoundBuffer_Release() has %d references, "
371                "should have 1\n",ref);
372
373             temp_buffer=NULL;
374             rc=IDirectSound3DBuffer_QueryInterface(*dsbo, &IID_IDirectSoundBuffer,
375                                                    (LPVOID *)&temp_buffer);
376             ok(rc==DS_OK && temp_buffer!=NULL,
377                "IDirectSound3DBuffer_QueryInterface() failed: %s\n",
378                DXGetErrorString8(rc));
379             ok(temp_buffer==*dsbo,"COM interface broken: %p != %p\n",
380                temp_buffer,*dsbo);
381             ref=IDirectSoundBuffer_Release(temp_buffer);
382             ok(ref==1,"IDirectSoundBuffer_Release() has %d references, "
383                "should have 1\n",ref);
384
385             ref=IDirectSoundBuffer_Release(*dsbo);
386             ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
387                "should have 0\n",ref);
388
389             rc=IDirectSound3DBuffer_QueryInterface(buffer,
390                                                    &IID_IDirectSoundBuffer,
391                                                    (LPVOID *)dsbo);
392             ok(rc==DS_OK && *dsbo!=NULL,"IDirectSound3DBuffer_QueryInterface() "
393                "failed: %s\n",DXGetErrorString8(rc));
394
395             /* DSOUND: Error: Invalid buffer */
396             rc=IDirectSound3DBuffer_GetAllParameters(buffer,0);
397             ok(rc==DSERR_INVALIDPARAM,"IDirectSound3DBuffer_GetAllParameters() "
398                "failed: %s\n",DXGetErrorString8(rc));
399
400             ZeroMemory(&buffer_param, sizeof(buffer_param));
401
402             /* DSOUND: Error: Invalid buffer */
403             rc=IDirectSound3DBuffer_GetAllParameters(buffer,&buffer_param);
404             ok(rc==DSERR_INVALIDPARAM,"IDirectSound3DBuffer_GetAllParameters() "
405                "failed: %s\n",DXGetErrorString8(rc));
406
407             buffer_param.dwSize=sizeof(buffer_param);
408             rc=IDirectSound3DBuffer_GetAllParameters(buffer,&buffer_param);
409             ok(rc==DS_OK,"IDirectSound3DBuffer_GetAllParameters() failed: %s\n",
410                DXGetErrorString8(rc));
411         }
412         if (set_volume) {
413             if (dsbcaps.dwFlags & DSBCAPS_CTRLVOLUME) {
414                 LONG val;
415                 rc=IDirectSoundBuffer_GetVolume(*dsbo,&val);
416                 ok(rc==DS_OK,"IDirectSoundBuffer_GetVolume() failed: %s\n",
417                    DXGetErrorString8(rc));
418
419                 rc=IDirectSoundBuffer_SetVolume(*dsbo,volume);
420                 ok(rc==DS_OK,"IDirectSoundBuffer_SetVolume() failed: %s\n",
421                    DXGetErrorString8(rc));
422             } else {
423                 /* DSOUND: Error: Buffer does not have CTRLVOLUME */
424                 rc=IDirectSoundBuffer_GetVolume(*dsbo,&volume);
425                 ok(rc==DSERR_CONTROLUNAVAIL,"IDirectSoundBuffer_GetVolume() "
426                    "should have returned DSERR_CONTROLUNAVAIL, returned: %s\n",
427                    DXGetErrorString8(rc));
428             }
429         }
430
431         if (set_pan) {
432             if (dsbcaps.dwFlags & DSBCAPS_CTRLPAN) {
433                 LONG val;
434                 rc=IDirectSoundBuffer_GetPan(*dsbo,&val);
435                 ok(rc==DS_OK,"IDirectSoundBuffer_GetPan() failed: %s\n",
436                    DXGetErrorString8(rc));
437
438                 rc=IDirectSoundBuffer_SetPan(*dsbo,pan);
439                 ok(rc==DS_OK,"IDirectSoundBuffer_SetPan() failed: %s\n",
440                    DXGetErrorString8(rc));
441             } else {
442                 /* DSOUND: Error: Buffer does not have CTRLPAN */
443                 rc=IDirectSoundBuffer_GetPan(*dsbo,&pan);
444                 ok(rc==DSERR_CONTROLUNAVAIL,"IDirectSoundBuffer_GetPan() "
445                    "should have returned DSERR_CONTROLUNAVAIL, returned: %s\n",
446                    DXGetErrorString8(rc));
447             }
448         }
449
450         /* try an offset past the end of the buffer */
451         rc = IDirectSoundBuffer_Lock(*dsbo, dsbcaps.dwBufferBytes, 0, &buffer1,
452                                       &length1, NULL, NULL,
453                                       DSBLOCK_ENTIREBUFFER);
454         ok(rc==DSERR_INVALIDPARAM, "IDirectSoundBuffer_Lock() should have "
455            "returned DSERR_INVALIDPARAM, returned %s\n", DXGetErrorString8(rc));
456
457         /* try a size larger than the buffer */
458         rc = IDirectSoundBuffer_Lock(*dsbo, 0, dsbcaps.dwBufferBytes + 1,
459                                      &buffer1, &length1, NULL, NULL,
460                                      DSBLOCK_FROMWRITECURSOR);
461         ok(rc==DSERR_INVALIDPARAM, "IDirectSoundBuffer_Lock() should have "
462            "returned DSERR_INVALIDPARAM, returned %s\n", DXGetErrorString8(rc));
463
464         state.wave=wave_generate_la(&wfx,duration,&state.wave_len);
465
466         state.dsbo=*dsbo;
467         state.wfx=&wfx;
468         state.buffer_size=dsbcaps.dwBufferBytes;
469         state.played=state.written=state.offset=0;
470         buffer_refill8(&state,state.buffer_size);
471
472         rc=IDirectSoundBuffer_Play(*dsbo,0,0,DSBPLAY_LOOPING);
473         ok(rc==DS_OK,"IDirectSoundBuffer_Play() failed: %s\n",
474            DXGetErrorString8(rc));
475
476         rc=IDirectSoundBuffer_GetStatus(*dsbo,&status);
477         ok(rc==DS_OK,"IDirectSoundBuffer_GetStatus() failed: %s\n",
478            DXGetErrorString8(rc));
479         ok(status==(DSBSTATUS_PLAYING|DSBSTATUS_LOOPING),
480            "GetStatus: bad status: %x\n",status);
481
482         if (listener) {
483             ZeroMemory(&listener_param,sizeof(listener_param));
484             listener_param.dwSize=sizeof(listener_param);
485             rc=IDirectSound3DListener_GetAllParameters(listener,&listener_param);
486             ok(rc==DS_OK,"IDirectSound3dListener_GetAllParameters() "
487                "failed: %s\n",DXGetErrorString8(rc));
488             if (move_listener) {
489                 listener_param.vPosition.x = -5.0f;
490                 listener_param.vVelocity.x = (float)(10.0/duration);
491             }
492             rc=IDirectSound3DListener_SetAllParameters(listener,
493                                                        &listener_param,
494                                                        DS3D_IMMEDIATE);
495             ok(rc==DS_OK,"IDirectSound3dListener_SetPosition() failed: %s\n",
496                DXGetErrorString8(rc));
497         }
498         if (buffer3d) {
499             if (move_sound) {
500                 buffer_param.vPosition.x = 100.0f;
501                 buffer_param.vVelocity.x = (float)(-200.0/duration);
502             }
503             buffer_param.flMinDistance = 10;
504             rc=IDirectSound3DBuffer_SetAllParameters(buffer,&buffer_param,
505                                                      DS3D_IMMEDIATE);
506             ok(rc==DS_OK,"IDirectSound3dBuffer_SetPosition() failed: %s\n",
507                DXGetErrorString8(rc));
508         }
509
510         start_time=GetTickCount();
511         while (buffer_service8(&state)) {
512             WaitForSingleObject(GetCurrentProcess(),TIME_SLICE);
513             now=GetTickCount();
514             if (listener && move_listener) {
515                 listener_param.vPosition.x = (float)(-5.0+10.0*(now-start_time)/1000/duration);
516                 if (winetest_debug>2)
517                     trace("listener position=%g\n",listener_param.vPosition.x);
518                 rc=IDirectSound3DListener_SetPosition(listener,
519                     listener_param.vPosition.x,listener_param.vPosition.y,
520                     listener_param.vPosition.z,DS3D_IMMEDIATE);
521                 ok(rc==DS_OK,"IDirectSound3dListener_SetPosition() failed: "
522                    "%s\n",DXGetErrorString8(rc));
523             }
524             if (buffer3d && move_sound) {
525                 buffer_param.vPosition.x = (float)(100-200.0*(now-start_time)/1000/duration);
526                 if (winetest_debug>2)
527                     trace("sound position=%g\n",buffer_param.vPosition.x);
528                 rc=IDirectSound3DBuffer_SetPosition(buffer,
529                     buffer_param.vPosition.x,buffer_param.vPosition.y,
530                     buffer_param.vPosition.z,DS3D_IMMEDIATE);
531                 ok(rc==DS_OK,"IDirectSound3dBuffer_SetPosition() failed: %s\n",
532                    DXGetErrorString8(rc));
533             }
534         }
535         /* Check the sound duration was within 10% of the expected value */
536         now=GetTickCount();
537         ok(fabs(1000*duration-now+start_time)<=100*duration,
538            "The sound played for %d ms instead of %g ms\n",
539            now-start_time,1000*duration);
540
541         free(state.wave);
542         if (is_primary) {
543             /* Set the CooperativeLevel back to normal */
544             /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
545             rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
546             ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_NORMAL) "
547                "failed: %s\n",DXGetErrorString8(rc));
548         }
549         if (buffer3d) {
550             ref=IDirectSound3DBuffer_Release(buffer);
551             ok(ref==0,"IDirectSound3DBuffer_Release() has %d references, "
552                "should have 0\n",ref);
553         }
554     }
555 }
556
557 static HRESULT test_secondary8(LPGUID lpGuid, int play,
558                                int has_3d, int has_3dbuffer,
559                                int has_listener, int has_duplicate,
560                                int move_listener, int move_sound)
561 {
562     HRESULT rc;
563     LPDIRECTSOUND8 dso=NULL;
564     LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
565     LPDIRECTSOUND3DLISTENER listener=NULL;
566     DSBUFFERDESC bufdesc;
567     WAVEFORMATEX wfx, wfx1;
568     int ref;
569
570     /* Create the DirectSound object */
571     rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
572     ok(rc==DS_OK||rc==DSERR_NODRIVER,"DirectSoundCreate8() failed: %s\n",
573        DXGetErrorString8(rc));
574     if (rc!=DS_OK)
575         return rc;
576
577     /* We must call SetCooperativeLevel before creating primary buffer */
578     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
579     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
580     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_PRIORITY) failed: "
581        "%s\n",DXGetErrorString8(rc));
582     if (rc!=DS_OK)
583         goto EXIT;
584
585     ZeroMemory(&bufdesc, sizeof(bufdesc));
586     bufdesc.dwSize=sizeof(bufdesc);
587     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
588     if (has_3d)
589         bufdesc.dwFlags|=DSBCAPS_CTRL3D;
590     else
591         bufdesc.dwFlags|=(DSBCAPS_CTRLVOLUME|DSBCAPS_CTRLPAN);
592     rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
593     ok((rc==DS_OK && primary!=NULL) || (rc == DSERR_CONTROLUNAVAIL),
594        "IDirectSound8_CreateSoundBuffer() failed to create a %sprimary buffer: "
595        "%s\n",has_3d?"3D ":"", DXGetErrorString8(rc));
596     if (rc == DSERR_CONTROLUNAVAIL)
597         trace("  No Primary\n");
598     else if (rc==DS_OK && primary!=NULL) {
599         rc=IDirectSoundBuffer_GetFormat(primary,&wfx1,sizeof(wfx1),NULL);
600         ok(rc==DS_OK,"IDirectSoundBuffer8_Getformat() failed: %s\n",
601            DXGetErrorString8(rc));
602         if (rc!=DS_OK)
603             goto EXIT1;
604
605         if (has_listener) {
606             rc=IDirectSoundBuffer_QueryInterface(primary,
607                                                  &IID_IDirectSound3DListener,
608                                                  (void **)&listener);
609             ok(rc==DS_OK && listener!=NULL,
610                "IDirectSoundBuffer_QueryInterface() failed to get a 3D "
611                "listener %s\n",DXGetErrorString8(rc));
612             ref=IDirectSoundBuffer_Release(primary);
613             ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
614                "should have 0\n",ref);
615             if (rc==DS_OK && listener!=NULL) {
616                 DS3DLISTENER listener_param;
617                 ZeroMemory(&listener_param,sizeof(listener_param));
618                 /* DSOUND: Error: Invalid buffer */
619                 rc=IDirectSound3DListener_GetAllParameters(listener,0);
620                 ok(rc==DSERR_INVALIDPARAM,
621                    "IDirectSound3dListener_GetAllParameters() should have "
622                    "returned DSERR_INVALIDPARAM, returned: %s\n",
623                    DXGetErrorString8(rc));
624
625                 /* DSOUND: Error: Invalid buffer */
626                 rc=IDirectSound3DListener_GetAllParameters(listener,
627                                                            &listener_param);
628                 ok(rc==DSERR_INVALIDPARAM,
629                    "IDirectSound3dListener_GetAllParameters() should have "
630                    "returned DSERR_INVALIDPARAM, returned: %s\n",
631                    DXGetErrorString8(rc));
632
633                 listener_param.dwSize=sizeof(listener_param);
634                 rc=IDirectSound3DListener_GetAllParameters(listener,
635                                                            &listener_param);
636                 ok(rc==DS_OK,"IDirectSound3dListener_GetAllParameters() "
637                    "failed: %s\n",DXGetErrorString8(rc));
638             } else {
639                 ok(listener==NULL, "IDirectSoundBuffer_QueryInterface() "
640                    "failed but returned a listener anyway\n");
641                 ok(rc!=DS_OK, "IDirectSoundBuffer_QueryInterface() succeeded "
642                    "but returned a NULL listener\n");
643                 if (listener) {
644                     ref=IDirectSound3DListener_Release(listener);
645                     ok(ref==0,"IDirectSound3dListener_Release() listener has "
646                        "%d references, should have 0\n",ref);
647                 }
648                 goto EXIT2;
649             }
650         }
651
652         init_format(&wfx,WAVE_FORMAT_PCM,22050,16,2);
653         secondary=NULL;
654         ZeroMemory(&bufdesc, sizeof(bufdesc));
655         bufdesc.dwSize=sizeof(bufdesc);
656         bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
657         if (has_3d)
658             bufdesc.dwFlags|=DSBCAPS_CTRL3D;
659         else
660             bufdesc.dwFlags|=
661                 (DSBCAPS_CTRLFREQUENCY|DSBCAPS_CTRLVOLUME|DSBCAPS_CTRLPAN);
662         bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
663                                     wfx.nBlockAlign);
664         bufdesc.lpwfxFormat=&wfx;
665         if (has_3d) {
666             /* a stereo 3D buffer should fail */
667             rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
668             ok(rc==DSERR_INVALIDPARAM,
669                "IDirectSound8_CreateSoundBuffer(secondary) should have "
670                "returned DSERR_INVALIDPARAM, returned %s\n",
671                DXGetErrorString8(rc));
672             if (secondary)
673                 ref=IDirectSoundBuffer_Release(secondary);
674             init_format(&wfx,WAVE_FORMAT_PCM,22050,16,1);
675         }
676
677         if (winetest_interactive) {
678             trace("  Testing a %s%ssecondary buffer %s%s%s%sat %dx%dx%d "
679                   "with a primary buffer at %dx%dx%d\n",
680                   has_3dbuffer?"3D ":"",
681                   has_duplicate?"duplicated ":"",
682                   listener!=NULL||move_sound?"with ":"",
683                   move_listener?"moving ":"",
684                   listener!=NULL?"listener ":"",
685                   listener&&move_sound?"and moving sound ":move_sound?
686                   "moving sound ":"",
687                   wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,
688                   wfx1.nSamplesPerSec,wfx1.wBitsPerSample,wfx1.nChannels);
689         }
690         rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
691         ok(rc==DS_OK && secondary!=NULL,"IDirectSound8_CreateSoundBuffer() "
692            "failed to create a %s%ssecondary buffer %s%s%s%sat %dx%dx%d (%s): %s\n",
693            has_3dbuffer?"3D ":"", has_duplicate?"duplicated ":"",
694            listener!=NULL||move_sound?"with ":"", move_listener?"moving ":"",
695            listener!=NULL?"listener ":"",
696            listener&&move_sound?"and moving sound ":move_sound?
697            "moving sound ":"",
698            wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,
699            getDSBCAPS(bufdesc.dwFlags),DXGetErrorString8(rc));
700         if (rc==DS_OK && secondary!=NULL) {
701             if (!has_3d) {
702                 LONG refvol,vol,refpan,pan;
703
704                 /* Check the initial secondary buffer's volume and pan */
705                 rc=IDirectSoundBuffer_GetVolume(secondary,&vol);
706                 ok(rc==DS_OK,"IDirectSoundBuffer_GetVolume(secondary) failed: "
707                    "%s\n",DXGetErrorString8(rc));
708                 ok(vol==0,"wrong volume for a new secondary buffer: %d\n",vol);
709                 rc=IDirectSoundBuffer_GetPan(secondary,&pan);
710                 ok(rc==DS_OK,"IDirectSoundBuffer_GetPan(secondary) failed: "
711                    "%s\n",DXGetErrorString8(rc));
712                 ok(pan==0,"wrong pan for a new secondary buffer: %d\n",pan);
713
714                 /* Check that changing the secondary buffer's volume and pan
715                  * does not impact the primary buffer's volume and pan
716                  */
717                 rc=IDirectSoundBuffer_GetVolume(primary,&refvol);
718                 ok(rc==DS_OK,"IDirectSoundBuffer_GetVolume(primary) failed: "
719                    "%s\n",DXGetErrorString8(rc));
720                 rc=IDirectSoundBuffer_GetPan(primary,&refpan);
721                 ok(rc==DS_OK,"IDirectSoundBuffer_GetPan(primary) failed: "
722                    "%s\n",DXGetErrorString8(rc));
723
724                 rc=IDirectSoundBuffer_SetVolume(secondary,-1000);
725                 ok(rc==DS_OK,"IDirectSoundBuffer_SetVolume(secondary) failed: "
726                    "%s\n",DXGetErrorString8(rc));
727                 rc=IDirectSoundBuffer_GetVolume(secondary,&vol);
728                 ok(rc==DS_OK,"IDirectSoundBuffer_SetVolume(secondary) failed: "
729                    "%s\n",DXGetErrorString8(rc));
730                 ok(vol==-1000,"secondary: wrong volume %d instead of -1000\n",
731                    vol);
732                 rc=IDirectSoundBuffer_SetPan(secondary,-1000);
733                 ok(rc==DS_OK,"IDirectSoundBuffer_SetPan(secondary) failed: "
734                    "%s\n",DXGetErrorString8(rc));
735                 rc=IDirectSoundBuffer_GetPan(secondary,&pan);
736                 ok(rc==DS_OK,"IDirectSoundBuffer_SetPan(secondary) failed: "
737                    "%s\n",DXGetErrorString8(rc));
738                 ok(pan==-1000,"secondary: wrong pan %d instead of -1000\n",
739                    pan);
740
741                 rc=IDirectSoundBuffer_GetVolume(primary,&vol);
742                 ok(rc==DS_OK,"IDirectSoundBuffer_`GetVolume(primary) failed: i"
743                    "%s\n",DXGetErrorString8(rc));
744                 ok(vol==refvol,"The primary volume changed from %d to %d\n",
745                    refvol,vol);
746                 rc=IDirectSoundBuffer_GetPan(primary,&pan);
747                 ok(rc==DS_OK,"IDirectSoundBuffer_GetPan(primary) failed: "
748                    "%s\n",DXGetErrorString8(rc));
749                 ok(pan==refpan,"The primary pan changed from %d to %d\n",
750                    refpan,pan);
751
752                 rc=IDirectSoundBuffer_SetVolume(secondary,0);
753                 ok(rc==DS_OK,"IDirectSoundBuffer_SetVolume(secondary) failed: "
754                    "%s\n",DXGetErrorString8(rc));
755                 rc=IDirectSoundBuffer_SetPan(secondary,0);
756                 ok(rc==DS_OK,"IDirectSoundBuffer_SetPan(secondary) failed: "
757                    "%s\n",DXGetErrorString8(rc));
758             }
759             if (has_duplicate) {
760                 LPDIRECTSOUNDBUFFER duplicated=NULL;
761
762                 /* DSOUND: Error: Invalid source buffer */
763                 rc=IDirectSound8_DuplicateSoundBuffer(dso,0,0);
764                 ok(rc==DSERR_INVALIDPARAM,
765                    "IDirectSound8_DuplicateSoundBuffer() should have returned "
766                    "DSERR_INVALIDPARAM, returned: %s\n",DXGetErrorString8(rc));
767
768                 /* DSOUND: Error: Invalid dest buffer */
769                 rc=IDirectSound8_DuplicateSoundBuffer(dso,secondary,0);
770                 ok(rc==DSERR_INVALIDPARAM,
771                    "IDirectSound8_DuplicateSoundBuffer() should have returned "
772                    "DSERR_INVALIDPARAM, returned: %s\n",DXGetErrorString8(rc));
773
774                 /* DSOUND: Error: Invalid source buffer */
775                 rc=IDirectSound8_DuplicateSoundBuffer(dso,0,&duplicated);
776                 ok(rc==DSERR_INVALIDPARAM,
777                    "IDirectSound8_DuplicateSoundBuffer() should have returned "
778                    "DSERR_INVALIDPARAM, returned: %s\n",DXGetErrorString8(rc));
779
780                 duplicated=NULL;
781                 rc=IDirectSound8_DuplicateSoundBuffer(dso,secondary,
782                                                       &duplicated);
783                 ok(rc==DS_OK && duplicated!=NULL,
784                    "IDirectSound8_DuplicateSoundBuffer() failed to duplicate "
785                    "a secondary buffer: %s\n",DXGetErrorString8(rc));
786
787                 if (rc==DS_OK && duplicated!=NULL) {
788                     ref=IDirectSoundBuffer_Release(secondary);
789                     ok(ref==0,"IDirectSoundBuffer_Release() secondary has %d "
790                        "references, should have 0\n",ref);
791                     secondary=duplicated;
792                 }
793             }
794
795             if (rc==DS_OK && secondary!=NULL) {
796                 double duration;
797                 duration=(move_listener || move_sound?4.0:1.0);
798                 test_buffer8(dso,&secondary,0,FALSE,0,FALSE,0,
799                              winetest_interactive,duration,has_3dbuffer,
800                              listener,move_listener,move_sound);
801                 ref=IDirectSoundBuffer_Release(secondary);
802                 ok(ref==0,"IDirectSoundBuffer_Release() %s has %d references, "
803                    "should have 0\n",has_duplicate?"duplicated":"secondary",
804                    ref);
805             }
806         }
807 EXIT1:
808         if (has_listener) {
809             ref=IDirectSound3DListener_Release(listener);
810             ok(ref==0,"IDirectSound3dListener_Release() listener has %d "
811                "references, should have 0\n",ref);
812         } else {
813             ref=IDirectSoundBuffer_Release(primary);
814             ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
815                "should have 0\n",ref);
816         }
817     } else {
818         ok(primary==NULL,"IDirectSound8_CreateSoundBuffer(primary) failed "
819            "but primary created anyway\n");
820         ok(rc!=DS_OK,"IDirectSound8_CreateSoundBuffer(primary) succeeded "
821            "but primary not created\n");
822         if (primary) {
823             ref=IDirectSoundBuffer_Release(primary);
824             ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
825                "should have 0\n",ref);
826         }
827     }
828 EXIT2:
829     /* Set the CooperativeLevel back to normal */
830     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
831     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
832     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_NORMAL) failed: "
833        "%s\n",DXGetErrorString8(rc));
834
835 EXIT:
836     ref=IDirectSound8_Release(dso);
837     ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",ref);
838     if (ref!=0)
839         return DSERR_GENERIC;
840
841     return rc;
842 }
843
844 static HRESULT test_for_driver8(LPGUID lpGuid)
845 {
846     HRESULT rc;
847     LPDIRECTSOUND8 dso=NULL;
848     int ref;
849
850     /* Create the DirectSound object */
851     rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
852     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
853        "DirectSoundCreate8() failed: %s\n",DXGetErrorString8(rc));
854     if (rc!=DS_OK)
855         return rc;
856
857     ref=IDirectSound8_Release(dso);
858     ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",ref);
859     if (ref!=0)
860         return DSERR_GENERIC;
861
862     return rc;
863 }
864
865 static HRESULT test_primary8(LPGUID lpGuid)
866 {
867     HRESULT rc;
868     LPDIRECTSOUND8 dso=NULL;
869     LPDIRECTSOUNDBUFFER primary=NULL;
870     DSBUFFERDESC bufdesc;
871     DSCAPS dscaps;
872     int ref, i;
873
874     /* Create the DirectSound object */
875     rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
876     ok(rc==DS_OK||rc==DSERR_NODRIVER,"DirectSoundCreate8() failed: %s\n",
877        DXGetErrorString8(rc));
878     if (rc!=DS_OK)
879         return rc;
880
881     /* Get the device capabilities */
882     ZeroMemory(&dscaps, sizeof(dscaps));
883     dscaps.dwSize=sizeof(dscaps);
884     rc=IDirectSound8_GetCaps(dso,&dscaps);
885     ok(rc==DS_OK,"IDirectSound8_GetCaps() failed: %s\n",DXGetErrorString8(rc));
886     if (rc!=DS_OK)
887         goto EXIT;
888
889     /* We must call SetCooperativeLevel before calling CreateSoundBuffer */
890     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
891     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
892     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_PRIORITY) failed: "
893        "%s\n",DXGetErrorString8(rc));
894     if (rc!=DS_OK)
895         goto EXIT;
896
897     /* Testing the primary buffer */
898     primary=NULL;
899     ZeroMemory(&bufdesc, sizeof(bufdesc));
900     bufdesc.dwSize=sizeof(bufdesc);
901     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRLVOLUME|DSBCAPS_CTRLPAN;
902     rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
903     ok((rc==DS_OK && primary!=NULL) || (rc == DSERR_CONTROLUNAVAIL),
904        "IDirectSound8_CreateSoundBuffer() failed to create a primary buffer: "
905        "%s\n",DXGetErrorString8(rc));
906     if (rc == DSERR_CONTROLUNAVAIL)
907         trace("  No Primary\n");
908     else if (rc==DS_OK && primary!=NULL) {
909         test_buffer8(dso,&primary,1,TRUE,0,TRUE,0,winetest_interactive &&
910                      !(dscaps.dwFlags & DSCAPS_EMULDRIVER),1.0,0,NULL,0,0);
911         if (winetest_interactive) {
912             LONG volume,pan;
913
914             volume = DSBVOLUME_MAX;
915             for (i = 0; i < 6; i++) {
916                 test_buffer8(dso,&primary,1,TRUE,volume,TRUE,0,
917                              winetest_interactive &&
918                              !(dscaps.dwFlags & DSCAPS_EMULDRIVER),
919                              1.0,0,NULL,0,0);
920                 volume -= ((DSBVOLUME_MAX-DSBVOLUME_MIN) / 40);
921             }
922
923             pan = DSBPAN_LEFT;
924             for (i = 0; i < 7; i++) {
925                 test_buffer8(dso,&primary,1,TRUE,0,TRUE,pan,
926                              winetest_interactive &&
927                              !(dscaps.dwFlags & DSCAPS_EMULDRIVER),1.0,0,0,0,0);
928                 pan += ((DSBPAN_RIGHT-DSBPAN_LEFT) / 6);
929             }
930         }
931         ref=IDirectSoundBuffer_Release(primary);
932         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
933            "should have 0\n",ref);
934     }
935
936     /* Set the CooperativeLevel back to normal */
937     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
938     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
939     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_NORMAL) failed: "
940        "%s\n",DXGetErrorString8(rc));
941
942 EXIT:
943     ref=IDirectSound8_Release(dso);
944     ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",ref);
945     if (ref!=0)
946         return DSERR_GENERIC;
947
948     return rc;
949 }
950
951 static HRESULT test_primary_3d8(LPGUID lpGuid)
952 {
953     HRESULT rc;
954     LPDIRECTSOUND8 dso=NULL;
955     LPDIRECTSOUNDBUFFER primary=NULL;
956     DSBUFFERDESC bufdesc;
957     DSCAPS dscaps;
958     int ref;
959
960     /* Create the DirectSound object */
961     rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
962     ok(rc==DS_OK||rc==DSERR_NODRIVER,"DirectSoundCreate8() failed: %s\n",
963        DXGetErrorString8(rc));
964     if (rc!=DS_OK)
965         return rc;
966
967     /* Get the device capabilities */
968     ZeroMemory(&dscaps, sizeof(dscaps));
969     dscaps.dwSize=sizeof(dscaps);
970     rc=IDirectSound8_GetCaps(dso,&dscaps);
971     ok(rc==DS_OK,"IDirectSound8_GetCaps failed: %s\n",DXGetErrorString8(rc));
972     if (rc!=DS_OK)
973         goto EXIT;
974
975     /* We must call SetCooperativeLevel before calling CreateSoundBuffer */
976     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
977     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
978     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_PRIORITY) failed: "
979        "%s\n",DXGetErrorString8(rc));
980     if (rc!=DS_OK)
981         goto EXIT;
982
983     primary=NULL;
984     ZeroMemory(&bufdesc, sizeof(bufdesc));
985     bufdesc.dwSize=sizeof(bufdesc);
986     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
987     rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
988     ok(rc==DS_OK && primary!=NULL,"IDirectSound8_CreateSoundBuffer() failed "
989        "to create a primary buffer: %s\n",DXGetErrorString8(rc));
990     if (rc==DS_OK && primary!=NULL) {
991         ref=IDirectSoundBuffer_Release(primary);
992         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
993            "should have 0\n",ref);
994         primary=NULL;
995         ZeroMemory(&bufdesc, sizeof(bufdesc));
996         bufdesc.dwSize=sizeof(bufdesc);
997         bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRL3D;
998         rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
999         ok(rc==DS_OK && primary!=NULL,"IDirectSound8_CreateSoundBuffer() "
1000            "failed to create a 3D primary buffer: %s\n",DXGetErrorString8(rc));
1001         if (rc==DS_OK && primary!=NULL) {
1002             test_buffer8(dso,&primary,1,FALSE,0,FALSE,0,
1003                          winetest_interactive &&
1004                          !(dscaps.dwFlags & DSCAPS_EMULDRIVER),1.0,0,0,0,0);
1005             ref=IDirectSoundBuffer_Release(primary);
1006             ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
1007                "should have 0\n",ref);
1008         }
1009     }
1010     /* Set the CooperativeLevel back to normal */
1011     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
1012     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
1013     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_NORMAL) failed: "
1014        "%s\n",DXGetErrorString8(rc));
1015
1016 EXIT:
1017     ref=IDirectSound8_Release(dso);
1018     ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",ref);
1019     if (ref!=0)
1020         return DSERR_GENERIC;
1021
1022     return rc;
1023 }
1024
1025 static HRESULT test_primary_3d_with_listener8(LPGUID lpGuid)
1026 {
1027     HRESULT rc;
1028     LPDIRECTSOUND8 dso=NULL;
1029     LPDIRECTSOUNDBUFFER primary=NULL;
1030     DSBUFFERDESC bufdesc;
1031     DSCAPS dscaps;
1032     int ref;
1033
1034     /* Create the DirectSound object */
1035     rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
1036     ok(rc==DS_OK||rc==DSERR_NODRIVER,"DirectSoundCreate8() failed: %s\n",
1037        DXGetErrorString8(rc));
1038     if (rc!=DS_OK)
1039         return rc;
1040
1041     /* Get the device capabilities */
1042     ZeroMemory(&dscaps, sizeof(dscaps));
1043     dscaps.dwSize=sizeof(dscaps);
1044     rc=IDirectSound8_GetCaps(dso,&dscaps);
1045     ok(rc==DS_OK,"IDirectSound8_GetCaps() failed: %s\n",DXGetErrorString8(rc));
1046     if (rc!=DS_OK)
1047         goto EXIT;
1048
1049     /* We must call SetCooperativeLevel before calling CreateSoundBuffer */
1050     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
1051     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
1052     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_PRIORITY) failed: "
1053        "%s\n",DXGetErrorString8(rc));
1054     if (rc!=DS_OK)
1055         goto EXIT;
1056     primary=NULL;
1057     ZeroMemory(&bufdesc, sizeof(bufdesc));
1058     bufdesc.dwSize=sizeof(bufdesc);
1059     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRL3D;
1060     rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
1061     ok(rc==DS_OK && primary!=NULL,"IDirectSound8_CreateSoundBuffer() failed "
1062        "to create a 3D primary buffer %s\n",DXGetErrorString8(rc));
1063     if (rc==DS_OK && primary!=NULL) {
1064         LPDIRECTSOUND3DLISTENER listener=NULL;
1065         rc=IDirectSoundBuffer_QueryInterface(primary,
1066                                              &IID_IDirectSound3DListener,
1067                                              (void **)&listener);
1068         ok(rc==DS_OK && listener!=NULL,"IDirectSoundBuffer_QueryInterface() "
1069            "failed to get a 3D listener: %s\n",DXGetErrorString8(rc));
1070         if (rc==DS_OK && listener!=NULL) {
1071             LPDIRECTSOUNDBUFFER temp_buffer=NULL;
1072
1073             /* Checking the COM interface */
1074             rc=IDirectSoundBuffer_QueryInterface(primary,
1075                                                  &IID_IDirectSoundBuffer,
1076                                                  (LPVOID *)&temp_buffer);
1077             ok(rc==DS_OK && temp_buffer!=NULL,
1078                "IDirectSoundBuffer_QueryInterface() failed: %s\n",
1079                DXGetErrorString8(rc));
1080             ok(temp_buffer==primary,"COM interface broken: %p != %p\n",temp_buffer,primary);
1081             if (rc==DS_OK && temp_buffer!=NULL) {
1082                 ref=IDirectSoundBuffer_Release(temp_buffer);
1083                 ok(ref==1,"IDirectSoundBuffer_Release() has %d references, "
1084                    "should have 1\n",ref);
1085
1086                 temp_buffer=NULL;
1087                 rc=IDirectSound3DListener_QueryInterface(listener,
1088                     &IID_IDirectSoundBuffer,(LPVOID *)&temp_buffer);
1089                 ok(rc==DS_OK && temp_buffer!=NULL,
1090                    "IDirectSoundBuffer_QueryInterface() failed: %s\n",
1091                    DXGetErrorString8(rc));
1092                 ok(temp_buffer==primary,"COM interface broken: %p != %p\n",temp_buffer,primary);
1093                 ref=IDirectSoundBuffer_Release(temp_buffer);
1094                 ok(ref==1,"IDirectSoundBuffer_Release() has %d references, "
1095                    "should have 1\n",ref);
1096
1097                 /* Testing the buffer */
1098                 test_buffer8(dso,&primary,1,FALSE,0,FALSE,0,
1099                              winetest_interactive &&
1100                              !(dscaps.dwFlags & DSCAPS_EMULDRIVER),
1101                              1.0,0,listener,0,0);
1102             }
1103
1104             /* Testing the reference counting */
1105             ref=IDirectSound3DListener_Release(listener);
1106             ok(ref==0,"IDirectSound3DListener_Release() listener has %d "
1107                "references, should have 0\n",ref);
1108         }
1109
1110         /* Testing the reference counting */
1111         ref=IDirectSoundBuffer_Release(primary);
1112         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
1113            "should have 0\n",ref);
1114     }
1115
1116 EXIT:
1117     ref=IDirectSound8_Release(dso);
1118     ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",ref);
1119     if (ref!=0)
1120 return DSERR_GENERIC;
1121
1122     return rc;
1123 }
1124
1125 static BOOL WINAPI dsenum_callback(LPGUID lpGuid, LPCSTR lpcstrDescription,
1126                                    LPCSTR lpcstrModule, LPVOID lpContext)
1127 {
1128     HRESULT rc;
1129     trace("*** Testing %s - %s ***\n",lpcstrDescription,lpcstrModule);
1130
1131     rc = test_for_driver8(lpGuid);
1132     if (rc == DSERR_NODRIVER) {
1133         trace("  No Driver\n");
1134         return 1;
1135     } else if (rc == DSERR_ALLOCATED) {
1136         trace("  Already In Use\n");
1137         return 1;
1138     } else if (rc == E_FAIL) {
1139         trace("  No Device\n");
1140         return 1;
1141     }
1142
1143     trace("  Testing the primary buffer\n");
1144     test_primary8(lpGuid);
1145
1146     trace("  Testing 3D primary buffer\n");
1147     test_primary_3d8(lpGuid);
1148
1149     trace("  Testing 3D primary buffer with listener\n");
1150     test_primary_3d_with_listener8(lpGuid);
1151
1152     /* Testing secondary buffers */
1153     test_secondary8(lpGuid,winetest_interactive,0,0,0,0,0,0);
1154     test_secondary8(lpGuid,winetest_interactive,0,0,0,1,0,0);
1155
1156     /* Testing 3D secondary buffers */
1157     test_secondary8(lpGuid,winetest_interactive,1,0,0,0,0,0);
1158     test_secondary8(lpGuid,winetest_interactive,1,1,0,0,0,0);
1159     test_secondary8(lpGuid,winetest_interactive,1,1,0,1,0,0);
1160     test_secondary8(lpGuid,winetest_interactive,1,0,1,0,0,0);
1161     test_secondary8(lpGuid,winetest_interactive,1,0,1,1,0,0);
1162     test_secondary8(lpGuid,winetest_interactive,1,1,1,0,0,0);
1163     test_secondary8(lpGuid,winetest_interactive,1,1,1,1,0,0);
1164     test_secondary8(lpGuid,winetest_interactive,1,1,1,0,1,0);
1165     test_secondary8(lpGuid,winetest_interactive,1,1,1,0,0,1);
1166     test_secondary8(lpGuid,winetest_interactive,1,1,1,0,1,1);
1167
1168     return 1;
1169 }
1170
1171 static void ds3d8_tests(void)
1172 {
1173     HRESULT rc;
1174     rc=pDirectSoundEnumerateA(&dsenum_callback,NULL);
1175     ok(rc==DS_OK,"DirectSoundEnumerateA() failed: %s\n",DXGetErrorString8(rc));
1176 }
1177
1178 START_TEST(ds3d8)
1179 {
1180     HMODULE hDsound;
1181
1182     CoInitialize(NULL);
1183
1184     hDsound = LoadLibrary("dsound.dll");
1185     if (hDsound)
1186     {
1187         trace("DLL Version: %s\n", get_file_version("dsound.dll"));
1188
1189         pDirectSoundEnumerateA = (void*)GetProcAddress(hDsound,
1190             "DirectSoundEnumerateA");
1191         pDirectSoundCreate8 = (void*)GetProcAddress(hDsound,
1192             "DirectSoundCreate8");
1193         if (pDirectSoundCreate8)
1194             ds3d8_tests();
1195         else
1196             skip("ds3d8 test skipped\n");
1197
1198         FreeLibrary(hDsound);
1199     }
1200     else
1201         skip("dsound.dll not found!\n");
1202
1203     CoUninitialize();
1204 }