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