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