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