dsound: Block align SetCurrentPosition and add test for it.
[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.0;
489                 listener_param.vVelocity.x = 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.0;
500                 buffer_param.vVelocity.x = -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 = -5.0+10.0*(now-start_time)/
515                     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 = 100-200.0*(now-start_time)/
526                     1000/duration;
527                 if (winetest_debug>2)
528                     trace("sound position=%g\n",buffer_param.vPosition.x);
529                 rc=IDirectSound3DBuffer_SetPosition(buffer,
530                     buffer_param.vPosition.x,buffer_param.vPosition.y,
531                     buffer_param.vPosition.z,DS3D_IMMEDIATE);
532                 ok(rc==DS_OK,"IDirectSound3dBuffer_SetPosition() failed: %s\n",
533                    DXGetErrorString8(rc));
534             }
535         }
536         /* Check the sound duration was within 10% of the expected value */
537         now=GetTickCount();
538         ok(fabs(1000*duration-now+start_time)<=100*duration,
539            "The sound played for %d ms instead of %g ms\n",
540            now-start_time,1000*duration);
541
542         free(state.wave);
543         if (is_primary) {
544             /* Set the CooperativeLevel back to normal */
545             /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
546             rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
547             ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_NORMAL) "
548                "failed: %s\n",DXGetErrorString8(rc));
549         }
550         if (buffer3d) {
551             ref=IDirectSound3DBuffer_Release(buffer);
552             ok(ref==0,"IDirectSound3DBuffer_Release() has %d references, "
553                "should have 0\n",ref);
554         }
555     }
556 }
557
558 static HRESULT test_secondary8(LPGUID lpGuid, int play,
559                                int has_3d, int has_3dbuffer,
560                                int has_listener, int has_duplicate,
561                                int move_listener, int move_sound)
562 {
563     HRESULT rc;
564     LPDIRECTSOUND8 dso=NULL;
565     LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
566     LPDIRECTSOUND3DLISTENER listener=NULL;
567     DSBUFFERDESC bufdesc;
568     WAVEFORMATEX wfx, wfx1;
569     int ref;
570
571     /* Create the DirectSound object */
572     rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
573     ok(rc==DS_OK||rc==DSERR_NODRIVER,"DirectSoundCreate8() failed: %s\n",
574        DXGetErrorString8(rc));
575     if (rc!=DS_OK)
576         return rc;
577
578     /* We must call SetCooperativeLevel before creating primary buffer */
579     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
580     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
581     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_PRIORITY) failed: "
582        "%s\n",DXGetErrorString8(rc));
583     if (rc!=DS_OK)
584         goto EXIT;
585
586     ZeroMemory(&bufdesc, sizeof(bufdesc));
587     bufdesc.dwSize=sizeof(bufdesc);
588     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
589     if (has_3d)
590         bufdesc.dwFlags|=DSBCAPS_CTRL3D;
591     else
592         bufdesc.dwFlags|=(DSBCAPS_CTRLVOLUME|DSBCAPS_CTRLPAN);
593     rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
594     ok((rc==DS_OK && primary!=NULL) || (rc == DSERR_CONTROLUNAVAIL),
595        "IDirectSound8_CreateSoundBuffer() failed to create a %sprimary buffer: "
596        "%s\n",has_3d?"3D ":"", DXGetErrorString8(rc));
597     if (rc == DSERR_CONTROLUNAVAIL)
598         trace("  No Primary\n");
599     else if (rc==DS_OK && primary!=NULL) {
600         rc=IDirectSoundBuffer_GetFormat(primary,&wfx1,sizeof(wfx1),NULL);
601         ok(rc==DS_OK,"IDirectSoundBuffer8_Getformat() failed: %s\n",
602            DXGetErrorString8(rc));
603         if (rc!=DS_OK)
604             goto EXIT1;
605
606         if (has_listener) {
607             rc=IDirectSoundBuffer_QueryInterface(primary,
608                                                  &IID_IDirectSound3DListener,
609                                                  (void **)&listener);
610             ok(rc==DS_OK && listener!=NULL,
611                "IDirectSoundBuffer_QueryInterface() failed to get a 3D "
612                "listener %s\n",DXGetErrorString8(rc));
613             ref=IDirectSoundBuffer_Release(primary);
614             ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
615                "should have 0\n",ref);
616             if (rc==DS_OK && listener!=NULL) {
617                 DS3DLISTENER listener_param;
618                 ZeroMemory(&listener_param,sizeof(listener_param));
619                 /* DSOUND: Error: Invalid buffer */
620                 rc=IDirectSound3DListener_GetAllParameters(listener,0);
621                 ok(rc==DSERR_INVALIDPARAM,
622                    "IDirectSound3dListener_GetAllParameters() should have "
623                    "returned DSERR_INVALIDPARAM, returned: %s\n",
624                    DXGetErrorString8(rc));
625
626                 /* DSOUND: Error: Invalid buffer */
627                 rc=IDirectSound3DListener_GetAllParameters(listener,
628                                                            &listener_param);
629                 ok(rc==DSERR_INVALIDPARAM,
630                    "IDirectSound3dListener_GetAllParameters() should have "
631                    "returned DSERR_INVALIDPARAM, returned: %s\n",
632                    DXGetErrorString8(rc));
633
634                 listener_param.dwSize=sizeof(listener_param);
635                 rc=IDirectSound3DListener_GetAllParameters(listener,
636                                                            &listener_param);
637                 ok(rc==DS_OK,"IDirectSound3dListener_GetAllParameters() "
638                    "failed: %s\n",DXGetErrorString8(rc));
639             } else {
640                 ok(listener==NULL, "IDirectSoundBuffer_QueryInterface() "
641                    "failed but returned a listener anyway\n");
642                 ok(rc!=DS_OK, "IDirectSoundBuffer_QueryInterface() succeeded "
643                    "but returned a NULL listener\n");
644                 if (listener) {
645                     ref=IDirectSound3DListener_Release(listener);
646                     ok(ref==0,"IDirectSound3dListener_Release() listener has "
647                        "%d references, should have 0\n",ref);
648                 }
649                 goto EXIT2;
650             }
651         }
652
653         init_format(&wfx,WAVE_FORMAT_PCM,22050,16,2);
654         secondary=NULL;
655         ZeroMemory(&bufdesc, sizeof(bufdesc));
656         bufdesc.dwSize=sizeof(bufdesc);
657         bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
658         if (has_3d)
659             bufdesc.dwFlags|=DSBCAPS_CTRL3D;
660         else
661             bufdesc.dwFlags|=
662                 (DSBCAPS_CTRLFREQUENCY|DSBCAPS_CTRLVOLUME|DSBCAPS_CTRLPAN);
663         bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
664                                     wfx.nBlockAlign);
665         bufdesc.lpwfxFormat=&wfx;
666         if (has_3d) {
667             /* a stereo 3D buffer should fail */
668             rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
669             ok(rc==DSERR_INVALIDPARAM,
670                "IDirectSound8_CreateSoundBuffer(secondary) should have "
671                "returned DSERR_INVALIDPARAM, returned %s\n",
672                DXGetErrorString8(rc));
673             if (secondary)
674                 ref=IDirectSoundBuffer_Release(secondary);
675             init_format(&wfx,WAVE_FORMAT_PCM,22050,16,1);
676         }
677
678         if (winetest_interactive) {
679             trace("  Testing a %s%ssecondary buffer %s%s%s%sat %dx%dx%d "
680                   "with a primary buffer at %dx%dx%d\n",
681                   has_3dbuffer?"3D ":"",
682                   has_duplicate?"duplicated ":"",
683                   listener!=NULL||move_sound?"with ":"",
684                   move_listener?"moving ":"",
685                   listener!=NULL?"listener ":"",
686                   listener&&move_sound?"and moving sound ":move_sound?
687                   "moving sound ":"",
688                   wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,
689                   wfx1.nSamplesPerSec,wfx1.wBitsPerSample,wfx1.nChannels);
690         }
691         rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
692         ok(rc==DS_OK && secondary!=NULL,"IDirectSound8_CreateSoundBuffer() "
693            "failed to create a %s%ssecondary buffer %s%s%s%sat %dx%dx%d (%s): %s\n",
694            has_3dbuffer?"3D ":"", has_duplicate?"duplicated ":"",
695            listener!=NULL||move_sound?"with ":"", move_listener?"moving ":"",
696            listener!=NULL?"listener ":"",
697            listener&&move_sound?"and moving sound ":move_sound?
698            "moving sound ":"",
699            wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,
700            getDSBCAPS(bufdesc.dwFlags),DXGetErrorString8(rc));
701         if (rc==DS_OK && secondary!=NULL) {
702             if (!has_3d) {
703                 LONG refvol,vol,refpan,pan;
704
705                 /* Check the initial secondary buffer's volume and pan */
706                 rc=IDirectSoundBuffer_GetVolume(secondary,&vol);
707                 ok(rc==DS_OK,"IDirectSoundBuffer_GetVolume(secondary) failed: "
708                    "%s\n",DXGetErrorString8(rc));
709                 ok(vol==0,"wrong volume for a new secondary buffer: %d\n",vol);
710                 rc=IDirectSoundBuffer_GetPan(secondary,&pan);
711                 ok(rc==DS_OK,"IDirectSoundBuffer_GetPan(secondary) failed: "
712                    "%s\n",DXGetErrorString8(rc));
713                 ok(pan==0,"wrong pan for a new secondary buffer: %d\n",pan);
714
715                 /* Check that changing the secondary buffer's volume and pan
716                  * does not impact the primary buffer's volume and pan
717                  */
718                 rc=IDirectSoundBuffer_GetVolume(primary,&refvol);
719                 ok(rc==DS_OK,"IDirectSoundBuffer_GetVolume(primary) failed: "
720                    "%s\n",DXGetErrorString8(rc));
721                 rc=IDirectSoundBuffer_GetPan(primary,&refpan);
722                 ok(rc==DS_OK,"IDirectSoundBuffer_GetPan(primary) failed: "
723                    "%s\n",DXGetErrorString8(rc));
724
725                 rc=IDirectSoundBuffer_SetVolume(secondary,-1000);
726                 ok(rc==DS_OK,"IDirectSoundBuffer_SetVolume(secondary) failed: "
727                    "%s\n",DXGetErrorString8(rc));
728                 rc=IDirectSoundBuffer_GetVolume(secondary,&vol);
729                 ok(rc==DS_OK,"IDirectSoundBuffer_SetVolume(secondary) failed: "
730                    "%s\n",DXGetErrorString8(rc));
731                 ok(vol==-1000,"secondary: wrong volume %d instead of -1000\n",
732                    vol);
733                 rc=IDirectSoundBuffer_SetPan(secondary,-1000);
734                 ok(rc==DS_OK,"IDirectSoundBuffer_SetPan(secondary) failed: "
735                    "%s\n",DXGetErrorString8(rc));
736                 rc=IDirectSoundBuffer_GetPan(secondary,&pan);
737                 ok(rc==DS_OK,"IDirectSoundBuffer_SetPan(secondary) failed: "
738                    "%s\n",DXGetErrorString8(rc));
739                 ok(pan==-1000,"secondary: wrong pan %d instead of -1000\n",
740                    pan);
741
742                 rc=IDirectSoundBuffer_GetVolume(primary,&vol);
743                 ok(rc==DS_OK,"IDirectSoundBuffer_`GetVolume(primary) failed: i"
744                    "%s\n",DXGetErrorString8(rc));
745                 ok(vol==refvol,"The primary volume changed from %d to %d\n",
746                    refvol,vol);
747                 rc=IDirectSoundBuffer_GetPan(primary,&pan);
748                 ok(rc==DS_OK,"IDirectSoundBuffer_GetPan(primary) failed: "
749                    "%s\n",DXGetErrorString8(rc));
750                 ok(pan==refpan,"The primary pan changed from %d to %d\n",
751                    refpan,pan);
752
753                 rc=IDirectSoundBuffer_SetVolume(secondary,0);
754                 ok(rc==DS_OK,"IDirectSoundBuffer_SetVolume(secondary) failed: "
755                    "%s\n",DXGetErrorString8(rc));
756                 rc=IDirectSoundBuffer_SetPan(secondary,0);
757                 ok(rc==DS_OK,"IDirectSoundBuffer_SetPan(secondary) failed: "
758                    "%s\n",DXGetErrorString8(rc));
759             }
760             if (has_duplicate) {
761                 LPDIRECTSOUNDBUFFER duplicated=NULL;
762
763                 /* DSOUND: Error: Invalid source buffer */
764                 rc=IDirectSound8_DuplicateSoundBuffer(dso,0,0);
765                 ok(rc==DSERR_INVALIDPARAM,
766                    "IDirectSound8_DuplicateSoundBuffer() should have returned "
767                    "DSERR_INVALIDPARAM, returned: %s\n",DXGetErrorString8(rc));
768
769                 /* DSOUND: Error: Invalid dest buffer */
770                 rc=IDirectSound8_DuplicateSoundBuffer(dso,secondary,0);
771                 ok(rc==DSERR_INVALIDPARAM,
772                    "IDirectSound8_DuplicateSoundBuffer() should have returned "
773                    "DSERR_INVALIDPARAM, returned: %s\n",DXGetErrorString8(rc));
774
775                 /* DSOUND: Error: Invalid source buffer */
776                 rc=IDirectSound8_DuplicateSoundBuffer(dso,0,&duplicated);
777                 ok(rc==DSERR_INVALIDPARAM,
778                    "IDirectSound8_DuplicateSoundBuffer() should have returned "
779                    "DSERR_INVALIDPARAM, returned: %s\n",DXGetErrorString8(rc));
780
781                 duplicated=NULL;
782                 rc=IDirectSound8_DuplicateSoundBuffer(dso,secondary,
783                                                       &duplicated);
784                 ok(rc==DS_OK && duplicated!=NULL,
785                    "IDirectSound8_DuplicateSoundBuffer() failed to duplicate "
786                    "a secondary buffer: %s\n",DXGetErrorString8(rc));
787
788                 if (rc==DS_OK && duplicated!=NULL) {
789                     ref=IDirectSoundBuffer_Release(secondary);
790                     ok(ref==0,"IDirectSoundBuffer_Release() secondary has %d "
791                        "references, should have 0\n",ref);
792                     secondary=duplicated;
793                 }
794             }
795
796             if (rc==DS_OK && secondary!=NULL) {
797                 double duration;
798                 duration=(move_listener || move_sound?4.0:1.0);
799                 test_buffer8(dso,&secondary,0,FALSE,0,FALSE,0,
800                              winetest_interactive,duration,has_3dbuffer,
801                              listener,move_listener,move_sound);
802                 ref=IDirectSoundBuffer_Release(secondary);
803                 ok(ref==0,"IDirectSoundBuffer_Release() %s has %d references, "
804                    "should have 0\n",has_duplicate?"duplicated":"secondary",
805                    ref);
806             }
807         }
808 EXIT1:
809         if (has_listener) {
810             ref=IDirectSound3DListener_Release(listener);
811             ok(ref==0,"IDirectSound3dListener_Release() listener has %d "
812                "references, should have 0\n",ref);
813         } else {
814             ref=IDirectSoundBuffer_Release(primary);
815             ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
816                "should have 0\n",ref);
817         }
818     } else {
819         ok(primary==NULL,"IDirectSound8_CreateSoundBuffer(primary) failed "
820            "but primary created anyway\n");
821         ok(rc!=DS_OK,"IDirectSound8_CreateSoundBuffer(primary) succeeded "
822            "but primary not created\n");
823         if (primary) {
824             ref=IDirectSoundBuffer_Release(primary);
825             ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
826                "should have 0\n",ref);
827         }
828     }
829 EXIT2:
830     /* Set the CooperativeLevel back to normal */
831     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
832     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
833     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_NORMAL) failed: "
834        "%s\n",DXGetErrorString8(rc));
835
836 EXIT:
837     ref=IDirectSound8_Release(dso);
838     ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",ref);
839     if (ref!=0)
840         return DSERR_GENERIC;
841
842     return rc;
843 }
844
845 static HRESULT test_for_driver8(LPGUID lpGuid)
846 {
847     HRESULT rc;
848     LPDIRECTSOUND8 dso=NULL;
849     int ref;
850
851     /* Create the DirectSound object */
852     rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
853     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
854        "DirectSoundCreate8() failed: %s\n",DXGetErrorString8(rc));
855     if (rc!=DS_OK)
856         return rc;
857
858     ref=IDirectSound8_Release(dso);
859     ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",ref);
860     if (ref!=0)
861         return DSERR_GENERIC;
862
863     return rc;
864 }
865
866 static HRESULT test_primary8(LPGUID lpGuid)
867 {
868     HRESULT rc;
869     LPDIRECTSOUND8 dso=NULL;
870     LPDIRECTSOUNDBUFFER primary=NULL;
871     DSBUFFERDESC bufdesc;
872     DSCAPS dscaps;
873     int ref, i;
874
875     /* Create the DirectSound object */
876     rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
877     ok(rc==DS_OK||rc==DSERR_NODRIVER,"DirectSoundCreate8() failed: %s\n",
878        DXGetErrorString8(rc));
879     if (rc!=DS_OK)
880         return rc;
881
882     /* Get the device capabilities */
883     ZeroMemory(&dscaps, sizeof(dscaps));
884     dscaps.dwSize=sizeof(dscaps);
885     rc=IDirectSound8_GetCaps(dso,&dscaps);
886     ok(rc==DS_OK,"IDirectSound8_GetCaps() failed: %s\n",DXGetErrorString8(rc));
887     if (rc!=DS_OK)
888         goto EXIT;
889
890     /* We must call SetCooperativeLevel before calling CreateSoundBuffer */
891     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
892     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
893     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_PRIORITY) failed: "
894        "%s\n",DXGetErrorString8(rc));
895     if (rc!=DS_OK)
896         goto EXIT;
897
898     /* Testing the primary buffer */
899     primary=NULL;
900     ZeroMemory(&bufdesc, sizeof(bufdesc));
901     bufdesc.dwSize=sizeof(bufdesc);
902     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRLVOLUME|DSBCAPS_CTRLPAN;
903     rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
904     ok((rc==DS_OK && primary!=NULL) || (rc == DSERR_CONTROLUNAVAIL),
905        "IDirectSound8_CreateSoundBuffer() failed to create a primary buffer: "
906        "%s\n",DXGetErrorString8(rc));
907     if (rc == DSERR_CONTROLUNAVAIL)
908         trace("  No Primary\n");
909     else if (rc==DS_OK && primary!=NULL) {
910         test_buffer8(dso,&primary,1,TRUE,0,TRUE,0,winetest_interactive &&
911                      !(dscaps.dwFlags & DSCAPS_EMULDRIVER),1.0,0,NULL,0,0);
912         if (winetest_interactive) {
913             LONG volume,pan;
914
915             volume = DSBVOLUME_MAX;
916             for (i = 0; i < 6; i++) {
917                 test_buffer8(dso,&primary,1,TRUE,volume,TRUE,0,
918                              winetest_interactive &&
919                              !(dscaps.dwFlags & DSCAPS_EMULDRIVER),
920                              1.0,0,NULL,0,0);
921                 volume -= ((DSBVOLUME_MAX-DSBVOLUME_MIN) / 40);
922             }
923
924             pan = DSBPAN_LEFT;
925             for (i = 0; i < 7; i++) {
926                 test_buffer8(dso,&primary,1,TRUE,0,TRUE,pan,
927                              winetest_interactive &&
928                              !(dscaps.dwFlags & DSCAPS_EMULDRIVER),1.0,0,0,0,0);
929                 pan += ((DSBPAN_RIGHT-DSBPAN_LEFT) / 6);
930             }
931         }
932         ref=IDirectSoundBuffer_Release(primary);
933         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
934            "should have 0\n",ref);
935     }
936
937     /* Set the CooperativeLevel back to normal */
938     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
939     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
940     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_NORMAL) failed: "
941        "%s\n",DXGetErrorString8(rc));
942
943 EXIT:
944     ref=IDirectSound8_Release(dso);
945     ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",ref);
946     if (ref!=0)
947         return DSERR_GENERIC;
948
949     return rc;
950 }
951
952 static HRESULT test_primary_3d8(LPGUID lpGuid)
953 {
954     HRESULT rc;
955     LPDIRECTSOUND8 dso=NULL;
956     LPDIRECTSOUNDBUFFER primary=NULL;
957     DSBUFFERDESC bufdesc;
958     DSCAPS dscaps;
959     int ref;
960
961     /* Create the DirectSound object */
962     rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
963     ok(rc==DS_OK||rc==DSERR_NODRIVER,"DirectSoundCreate8() failed: %s\n",
964        DXGetErrorString8(rc));
965     if (rc!=DS_OK)
966         return rc;
967
968     /* Get the device capabilities */
969     ZeroMemory(&dscaps, sizeof(dscaps));
970     dscaps.dwSize=sizeof(dscaps);
971     rc=IDirectSound8_GetCaps(dso,&dscaps);
972     ok(rc==DS_OK,"IDirectSound8_GetCaps failed: %s\n",DXGetErrorString8(rc));
973     if (rc!=DS_OK)
974         goto EXIT;
975
976     /* We must call SetCooperativeLevel before calling CreateSoundBuffer */
977     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
978     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
979     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_PRIORITY) failed: "
980        "%s\n",DXGetErrorString8(rc));
981     if (rc!=DS_OK)
982         goto EXIT;
983
984     primary=NULL;
985     ZeroMemory(&bufdesc, sizeof(bufdesc));
986     bufdesc.dwSize=sizeof(bufdesc);
987     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
988     rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
989     ok(rc==DS_OK && primary!=NULL,"IDirectSound8_CreateSoundBuffer() failed "
990        "to create a primary buffer: %s\n",DXGetErrorString8(rc));
991     if (rc==DS_OK && primary!=NULL) {
992         ref=IDirectSoundBuffer_Release(primary);
993         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
994            "should have 0\n",ref);
995         primary=NULL;
996         ZeroMemory(&bufdesc, sizeof(bufdesc));
997         bufdesc.dwSize=sizeof(bufdesc);
998         bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRL3D;
999         rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
1000         ok(rc==DS_OK && primary!=NULL,"IDirectSound8_CreateSoundBuffer() "
1001            "failed to create a 3D primary buffer: %s\n",DXGetErrorString8(rc));
1002         if (rc==DS_OK && primary!=NULL) {
1003             test_buffer8(dso,&primary,1,FALSE,0,FALSE,0,
1004                          winetest_interactive &&
1005                          !(dscaps.dwFlags & DSCAPS_EMULDRIVER),1.0,0,0,0,0);
1006             ref=IDirectSoundBuffer_Release(primary);
1007             ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
1008                "should have 0\n",ref);
1009         }
1010     }
1011     /* Set the CooperativeLevel back to normal */
1012     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
1013     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
1014     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_NORMAL) failed: "
1015        "%s\n",DXGetErrorString8(rc));
1016
1017 EXIT:
1018     ref=IDirectSound8_Release(dso);
1019     ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",ref);
1020     if (ref!=0)
1021         return DSERR_GENERIC;
1022
1023     return rc;
1024 }
1025
1026 static HRESULT test_primary_3d_with_listener8(LPGUID lpGuid)
1027 {
1028     HRESULT rc;
1029     LPDIRECTSOUND8 dso=NULL;
1030     LPDIRECTSOUNDBUFFER primary=NULL;
1031     DSBUFFERDESC bufdesc;
1032     DSCAPS dscaps;
1033     int ref;
1034
1035     /* Create the DirectSound object */
1036     rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
1037     ok(rc==DS_OK||rc==DSERR_NODRIVER,"DirectSoundCreate8() failed: %s\n",
1038        DXGetErrorString8(rc));
1039     if (rc!=DS_OK)
1040         return rc;
1041
1042     /* Get the device capabilities */
1043     ZeroMemory(&dscaps, sizeof(dscaps));
1044     dscaps.dwSize=sizeof(dscaps);
1045     rc=IDirectSound8_GetCaps(dso,&dscaps);
1046     ok(rc==DS_OK,"IDirectSound8_GetCaps() failed: %s\n",DXGetErrorString8(rc));
1047     if (rc!=DS_OK)
1048         goto EXIT;
1049
1050     /* We must call SetCooperativeLevel before calling CreateSoundBuffer */
1051     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
1052     rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
1053     ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_PRIORITY) failed: "
1054        "%s\n",DXGetErrorString8(rc));
1055     if (rc!=DS_OK)
1056         goto EXIT;
1057     primary=NULL;
1058     ZeroMemory(&bufdesc, sizeof(bufdesc));
1059     bufdesc.dwSize=sizeof(bufdesc);
1060     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRL3D;
1061     rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
1062     ok(rc==DS_OK && primary!=NULL,"IDirectSound8_CreateSoundBuffer() failed "
1063        "to create a 3D primary buffer %s\n",DXGetErrorString8(rc));
1064     if (rc==DS_OK && primary!=NULL) {
1065         LPDIRECTSOUND3DLISTENER listener=NULL;
1066         rc=IDirectSoundBuffer_QueryInterface(primary,
1067                                              &IID_IDirectSound3DListener,
1068                                              (void **)&listener);
1069         ok(rc==DS_OK && listener!=NULL,"IDirectSoundBuffer_QueryInterface() "
1070            "failed to get a 3D listener: %s\n",DXGetErrorString8(rc));
1071         if (rc==DS_OK && listener!=NULL) {
1072             LPDIRECTSOUNDBUFFER temp_buffer=NULL;
1073
1074             /* Checking the COM interface */
1075             rc=IDirectSoundBuffer_QueryInterface(primary,
1076                                                  &IID_IDirectSoundBuffer,
1077                                                  (LPVOID *)&temp_buffer);
1078             ok(rc==DS_OK && temp_buffer!=NULL,
1079                "IDirectSoundBuffer_QueryInterface() failed: %s\n",
1080                DXGetErrorString8(rc));
1081             ok(temp_buffer==primary,"COM interface broken: %p != %p\n",temp_buffer,primary);
1082             if (rc==DS_OK && temp_buffer!=NULL) {
1083                 ref=IDirectSoundBuffer_Release(temp_buffer);
1084                 ok(ref==1,"IDirectSoundBuffer_Release() has %d references, "
1085                    "should have 1\n",ref);
1086
1087                 temp_buffer=NULL;
1088                 rc=IDirectSound3DListener_QueryInterface(listener,
1089                     &IID_IDirectSoundBuffer,(LPVOID *)&temp_buffer);
1090                 ok(rc==DS_OK && temp_buffer!=NULL,
1091                    "IDirectSoundBuffer_QueryInterface() failed: %s\n",
1092                    DXGetErrorString8(rc));
1093                 ok(temp_buffer==primary,"COM interface broken: %p != %p\n",temp_buffer,primary);
1094                 ref=IDirectSoundBuffer_Release(temp_buffer);
1095                 ok(ref==1,"IDirectSoundBuffer_Release() has %d references, "
1096                    "should have 1\n",ref);
1097
1098                 /* Testing the buffer */
1099                 test_buffer8(dso,&primary,1,FALSE,0,FALSE,0,
1100                              winetest_interactive &&
1101                              !(dscaps.dwFlags & DSCAPS_EMULDRIVER),
1102                              1.0,0,listener,0,0);
1103             }
1104
1105             /* Testing the reference counting */
1106             ref=IDirectSound3DListener_Release(listener);
1107             ok(ref==0,"IDirectSound3DListener_Release() listener has %d "
1108                "references, should have 0\n",ref);
1109         }
1110
1111         /* Testing the reference counting */
1112         ref=IDirectSoundBuffer_Release(primary);
1113         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
1114            "should have 0\n",ref);
1115     }
1116
1117 EXIT:
1118     ref=IDirectSound8_Release(dso);
1119     ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",ref);
1120     if (ref!=0)
1121 return DSERR_GENERIC;
1122
1123     return rc;
1124 }
1125
1126 static BOOL WINAPI dsenum_callback(LPGUID lpGuid, LPCSTR lpcstrDescription,
1127                                    LPCSTR lpcstrModule, LPVOID lpContext)
1128 {
1129     HRESULT rc;
1130     trace("*** Testing %s - %s ***\n",lpcstrDescription,lpcstrModule);
1131
1132     rc = test_for_driver8(lpGuid);
1133     if (rc == DSERR_NODRIVER) {
1134         trace("  No Driver\n");
1135         return 1;
1136     } else if (rc == DSERR_ALLOCATED) {
1137         trace("  Already In Use\n");
1138         return 1;
1139     } else if (rc == E_FAIL) {
1140         trace("  No Device\n");
1141         return 1;
1142     }
1143
1144     trace("  Testing the primary buffer\n");
1145     test_primary8(lpGuid);
1146
1147     trace("  Testing 3D primary buffer\n");
1148     test_primary_3d8(lpGuid);
1149
1150     trace("  Testing 3D primary buffer with listener\n");
1151     test_primary_3d_with_listener8(lpGuid);
1152
1153     /* Testing secondary buffers */
1154     test_secondary8(lpGuid,winetest_interactive,0,0,0,0,0,0);
1155     test_secondary8(lpGuid,winetest_interactive,0,0,0,1,0,0);
1156
1157     /* Testing 3D secondary buffers */
1158     test_secondary8(lpGuid,winetest_interactive,1,0,0,0,0,0);
1159     test_secondary8(lpGuid,winetest_interactive,1,1,0,0,0,0);
1160     test_secondary8(lpGuid,winetest_interactive,1,1,0,1,0,0);
1161     test_secondary8(lpGuid,winetest_interactive,1,0,1,0,0,0);
1162     test_secondary8(lpGuid,winetest_interactive,1,0,1,1,0,0);
1163     test_secondary8(lpGuid,winetest_interactive,1,1,1,0,0,0);
1164     test_secondary8(lpGuid,winetest_interactive,1,1,1,1,0,0);
1165     test_secondary8(lpGuid,winetest_interactive,1,1,1,0,1,0);
1166     test_secondary8(lpGuid,winetest_interactive,1,1,1,0,0,1);
1167     test_secondary8(lpGuid,winetest_interactive,1,1,1,0,1,1);
1168
1169     return 1;
1170 }
1171
1172 static void ds3d8_tests(void)
1173 {
1174     HRESULT rc;
1175     rc=pDirectSoundEnumerateA(&dsenum_callback,NULL);
1176     ok(rc==DS_OK,"DirectSoundEnumerateA() failed: %s\n",DXGetErrorString8(rc));
1177 }
1178
1179 START_TEST(ds3d8)
1180 {
1181     HMODULE hDsound;
1182
1183     CoInitialize(NULL);
1184
1185     hDsound = LoadLibrary("dsound.dll");
1186     if (hDsound)
1187     {
1188         trace("DLL Version: %s\n", get_file_version("dsound.dll"));
1189
1190         pDirectSoundEnumerateA = (void*)GetProcAddress(hDsound,
1191             "DirectSoundEnumerateA");
1192         pDirectSoundCreate8 = (void*)GetProcAddress(hDsound,
1193             "DirectSoundCreate8");
1194         if (pDirectSoundCreate8)
1195             ds3d8_tests();
1196         else
1197             skip("ds3d8 test skipped\n");
1198
1199         FreeLibrary(hDsound);
1200     }
1201     else
1202         skip("dsound.dll not found!\n");
1203
1204     CoUninitialize();
1205 }