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