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