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