wintrust/tests: Fix test on win9x.
[wine] / dlls / dsound / tests / dsound.c
1 /*
2  * Tests basic sound playback in DirectSound.
3  * In particular we test each standard Windows sound format to make sure
4  * we handle the sound card/driver quirks correctly.
5  *
6  * Part of this test involves playing test tones. But this only makes
7  * sense if someone is going to carefully listen to it, and would only
8  * bother everyone else.
9  * So this is only done if the test is being run in interactive mode.
10  *
11  * Copyright (c) 2002-2004 Francois Gouget
12  * Copyright (c) 2007 Maarten Lankhorst
13  *
14  * This library is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU Lesser General Public
16  * License as published by the Free Software Foundation; either
17  * version 2.1 of the License, or (at your option) any later version.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27  */
28
29 #include <windows.h>
30
31 #include "wine/test.h"
32 #include "dsound.h"
33 #include "dxerr8.h"
34 #include "dsconf.h"
35 #include "mmreg.h"
36 /* MinGW doesn't have ksguid, needed for make crosstest to work. */
37 #include "initguid.h"
38 #include "ks.h"
39 #include "ksmedia.h"
40
41 #include "dsound_test.h"
42
43 static HRESULT (WINAPI *pDirectSoundEnumerateA)(LPDSENUMCALLBACKA,LPVOID)=NULL;
44 static HRESULT (WINAPI *pDirectSoundCreate)(LPCGUID,LPDIRECTSOUND*,
45     LPUNKNOWN)=NULL;
46
47 static BOOL gotdx8;
48
49 static void IDirectSound_test(LPDIRECTSOUND dso, BOOL initialized,
50                               LPCGUID lpGuid)
51 {
52     HRESULT rc;
53     DSCAPS dscaps;
54     int ref;
55     IUnknown * unknown;
56     IDirectSound * ds;
57     IDirectSound8 * ds8;
58     DWORD speaker_config, new_speaker_config;
59
60     /* Try to Query for objects */
61     rc=IDirectSound_QueryInterface(dso,&IID_IUnknown,(LPVOID*)&unknown);
62     ok(rc==DS_OK,"IDirectSound_QueryInterface(IID_IUnknown) failed: %s\n",
63        DXGetErrorString8(rc));
64     if (rc==DS_OK)
65         IDirectSound_Release(unknown);
66
67     rc=IDirectSound_QueryInterface(dso,&IID_IDirectSound,(LPVOID*)&ds);
68     ok(rc==DS_OK,"IDirectSound_QueryInterface(IID_IDirectSound) failed: %s\n",
69        DXGetErrorString8(rc));
70     if (rc==DS_OK)
71         IDirectSound_Release(ds);
72
73     rc=IDirectSound_QueryInterface(dso,&IID_IDirectSound8,(LPVOID*)&ds8);
74     ok(rc==E_NOINTERFACE,"IDirectSound_QueryInterface(IID_IDirectSound8) "
75        "should have failed: %s\n",DXGetErrorString8(rc));
76     if (rc==DS_OK)
77         IDirectSound8_Release(ds8);
78
79     if (initialized == FALSE) {
80         /* try uninitialized object */
81         rc=IDirectSound_GetCaps(dso,0);
82         ok(rc==DSERR_UNINITIALIZED,"IDirectSound_GetCaps(NULL) "
83            "should have returned DSERR_UNINITIALIZED, returned: %s\n",
84            DXGetErrorString8(rc));
85
86         rc=IDirectSound_GetCaps(dso,&dscaps);
87         ok(rc==DSERR_UNINITIALIZED,"IDirectSound_GetCaps() "
88            "should have returned DSERR_UNINITIALIZED, returned: %s\n",
89            DXGetErrorString8(rc));
90
91         rc=IDirectSound_Compact(dso);
92         ok(rc==DSERR_UNINITIALIZED,"IDirectSound_Compact() "
93            "should have returned DSERR_UNINITIALIZED, returned: %s\n",
94            DXGetErrorString8(rc));
95
96         rc=IDirectSound_GetSpeakerConfig(dso,&speaker_config);
97         ok(rc==DSERR_UNINITIALIZED,"IDirectSound_GetSpeakerConfig() "
98            "should have returned DSERR_UNINITIALIZED, returned: %s\n",
99            DXGetErrorString8(rc));
100
101         rc=IDirectSound_Initialize(dso,lpGuid);
102         ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
103            "IDirectSound_Initialize() failed: %s\n",DXGetErrorString8(rc));
104         if (rc==DSERR_NODRIVER) {
105             trace("  No Driver\n");
106             goto EXIT;
107         } else if (rc==E_FAIL) {
108             trace("  No Device\n");
109             goto EXIT;
110         } else if (rc==DSERR_ALLOCATED) {
111             trace("  Already In Use\n");
112             goto EXIT;
113         }
114     }
115
116     rc=IDirectSound_Initialize(dso,lpGuid);
117     ok(rc==DSERR_ALREADYINITIALIZED, "IDirectSound_Initialize() "
118        "should have returned DSERR_ALREADYINITIALIZED: %s\n",
119        DXGetErrorString8(rc));
120
121     /* DSOUND: Error: Invalid caps buffer */
122     rc=IDirectSound_GetCaps(dso,0);
123     ok(rc==DSERR_INVALIDPARAM,"IDirectSound_GetCaps(NULL) "
124        "should have returned DSERR_INVALIDPARAM, returned: %s\n",
125        DXGetErrorString8(rc));
126
127     ZeroMemory(&dscaps, sizeof(dscaps));
128
129     /* DSOUND: Error: Invalid caps buffer */
130     rc=IDirectSound_GetCaps(dso,&dscaps);
131     ok(rc==DSERR_INVALIDPARAM,"IDirectSound_GetCaps() "
132        "should have returned DSERR_INVALIDPARAM, returned: %s\n",
133        DXGetErrorString8(rc));
134
135     dscaps.dwSize=sizeof(dscaps);
136
137     /* DSOUND: Running on a certified driver */
138     rc=IDirectSound_GetCaps(dso,&dscaps);
139     ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %s\n",DXGetErrorString8(rc));
140
141     rc=IDirectSound_Compact(dso);
142     ok(rc==DSERR_PRIOLEVELNEEDED,"IDirectSound_Compact() failed: %s\n",
143        DXGetErrorString8(rc));
144
145     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
146     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
147        DXGetErrorString8(rc));
148
149     rc=IDirectSound_Compact(dso);
150     ok(rc==DS_OK,"IDirectSound_Compact() failed: %s\n",DXGetErrorString8(rc));
151
152     rc=IDirectSound_GetSpeakerConfig(dso,0);
153     ok(rc==DSERR_INVALIDPARAM,"IDirectSound_GetSpeakerConfig(NULL) "
154        "should have returned DSERR_INVALIDPARAM, returned: %s\n",
155        DXGetErrorString8(rc));
156
157     rc=IDirectSound_GetSpeakerConfig(dso,&speaker_config);
158     ok(rc==DS_OK,"IDirectSound_GetSpeakerConfig() failed: %s\n",
159        DXGetErrorString8(rc));
160
161     speaker_config = DSSPEAKER_COMBINED(DSSPEAKER_STEREO,
162                                         DSSPEAKER_GEOMETRY_WIDE);
163     rc=IDirectSound_SetSpeakerConfig(dso,speaker_config);
164     ok(rc==DS_OK,"IDirectSound_SetSpeakerConfig() failed: %s\n",
165        DXGetErrorString8(rc));
166     if (rc==DS_OK) {
167         rc=IDirectSound_GetSpeakerConfig(dso,&new_speaker_config);
168         ok(rc==DS_OK,"IDirectSound_GetSpeakerConfig() failed: %s\n",
169            DXGetErrorString8(rc));
170         if (rc==DS_OK && speaker_config!=new_speaker_config)
171                trace("IDirectSound_GetSpeakerConfig() failed to set speaker "
172                "config: expected 0x%08x, got 0x%08x\n",
173                speaker_config,new_speaker_config);
174     }
175
176 EXIT:
177     ref=IDirectSound_Release(dso);
178     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
179 }
180
181 static void IDirectSound_tests(void)
182 {
183     HRESULT rc;
184     LPDIRECTSOUND dso=NULL;
185     LPCLASSFACTORY cf=NULL;
186
187     trace("Testing IDirectSound\n");
188
189     rc=CoGetClassObject(&CLSID_DirectSound, CLSCTX_INPROC_SERVER, NULL,
190                         &IID_IClassFactory, (void**)&cf);
191     ok(rc==S_OK,"CoGetClassObject(CLSID_DirectSound, IID_IClassFactory) "
192        "failed: %s\n", DXGetErrorString8(rc));
193
194     rc=CoGetClassObject(&CLSID_DirectSound, CLSCTX_INPROC_SERVER, NULL,
195                         &IID_IUnknown, (void**)&cf);
196     ok(rc==S_OK,"CoGetClassObject(CLSID_DirectSound, IID_IUnknown) "
197        "failed: %s\n", DXGetErrorString8(rc));
198
199     /* try the COM class factory method of creation with no device specified */
200     rc=CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
201                         &IID_IDirectSound, (void**)&dso);
202     ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSound) failed: %s\n",
203        DXGetErrorString8(rc));
204     if (dso)
205         IDirectSound_test(dso, FALSE, NULL);
206
207     /* try the COM class factory method of creation with default playback
208      * device specified */
209     rc=CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
210                         &IID_IDirectSound, (void**)&dso);
211     ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSound) failed: %s\n",
212        DXGetErrorString8(rc));
213     if (dso)
214         IDirectSound_test(dso, FALSE, &DSDEVID_DefaultPlayback);
215
216     /* try the COM class factory method of creation with default voice
217      * playback device specified */
218     rc=CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
219                         &IID_IDirectSound, (void**)&dso);
220     ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSound) failed: %s\n",
221        DXGetErrorString8(rc));
222     if (dso)
223         IDirectSound_test(dso, FALSE, &DSDEVID_DefaultVoicePlayback);
224
225     /* try the COM class factory method of creation with a bad
226      * IID specified */
227     rc=CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
228                         &CLSID_DirectSoundPrivate, (void**)&dso);
229     ok(rc==E_NOINTERFACE,
230        "CoCreateInstance(CLSID_DirectSound,CLSID_DirectSoundPrivate) "
231        "should have failed: %s\n",DXGetErrorString8(rc));
232
233     /* try the COM class factory method of creation with a bad
234      * GUID and IID specified */
235     rc=CoCreateInstance(&CLSID_DirectSoundPrivate, NULL, CLSCTX_INPROC_SERVER,
236                         &IID_IDirectSound, (void**)&dso);
237     ok(rc==REGDB_E_CLASSNOTREG,
238        "CoCreateInstance(CLSID_DirectSoundPrivate,IID_IDirectSound) "
239        "should have failed: %s\n",DXGetErrorString8(rc));
240
241     /* try with no device specified */
242     rc=pDirectSoundCreate(NULL,&dso,NULL);
243     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
244        "DirectSoundCreate(NULL) failed: %s\n",DXGetErrorString8(rc));
245     if (rc==S_OK && dso)
246         IDirectSound_test(dso, TRUE, NULL);
247
248     /* try with default playback device specified */
249     rc=pDirectSoundCreate(&DSDEVID_DefaultPlayback,&dso,NULL);
250     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
251        "DirectSoundCreate(DSDEVID_DefaultPlayback) failed: %s\n",
252        DXGetErrorString8(rc));
253     if (rc==DS_OK && dso)
254         IDirectSound_test(dso, TRUE, NULL);
255
256     /* try with default voice playback device specified */
257     rc=pDirectSoundCreate(&DSDEVID_DefaultVoicePlayback,&dso,NULL);
258     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
259        "DirectSoundCreate(DSDEVID_DefaultVoicePlayback) failed: %s\n",
260        DXGetErrorString8(rc));
261     if (rc==DS_OK && dso)
262         IDirectSound_test(dso, TRUE, NULL);
263
264     /* try with a bad device specified */
265     rc=pDirectSoundCreate(&DSDEVID_DefaultVoiceCapture,&dso,NULL);
266     ok(rc==DSERR_NODRIVER,"DirectSoundCreate(DSDEVID_DefaultVoiceCapture) "
267        "should have failed: %s\n",DXGetErrorString8(rc));
268     if (rc==DS_OK && dso)
269         IDirectSound_Release(dso);
270 }
271
272 static HRESULT test_dsound(LPGUID lpGuid)
273 {
274     HRESULT rc;
275     LPDIRECTSOUND dso=NULL;
276     int ref;
277
278     /* DSOUND: Error: Invalid interface buffer */
279     rc=pDirectSoundCreate(lpGuid,0,NULL);
280     ok(rc==DSERR_INVALIDPARAM,"DirectSoundCreate() should have returned "
281        "DSERR_INVALIDPARAM, returned: %s\n",DXGetErrorString8(rc));
282
283     /* Create the DirectSound object */
284     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
285     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
286        "DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
287     if (rc!=DS_OK)
288         return rc;
289
290     /* Try the enumerated device */
291     IDirectSound_test(dso, TRUE, lpGuid);
292
293     /* Try the COM class factory method of creation with enumerated device */
294     rc=CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
295                         &IID_IDirectSound, (void**)&dso);
296     ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSound) failed: %s\n",
297        DXGetErrorString8(rc));
298     if (dso)
299         IDirectSound_test(dso, FALSE, lpGuid);
300
301     /* Create a DirectSound object */
302     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
303     ok(rc==DS_OK,"DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
304     if (rc==DS_OK) {
305         LPDIRECTSOUND dso1=NULL;
306
307         /* Create a second DirectSound object */
308         rc=pDirectSoundCreate(lpGuid,&dso1,NULL);
309         ok(rc==DS_OK,"DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
310         if (rc==DS_OK) {
311             /* Release the second DirectSound object */
312             ref=IDirectSound_Release(dso1);
313             ok(ref==0,"IDirectSound_Release() has %d references, should have "
314                "0\n",ref);
315             ok(dso!=dso1,"DirectSound objects should be unique: dso=%p,dso1=%p\n",dso,dso1);
316         }
317
318         /* Release the first DirectSound object */
319         ref=IDirectSound_Release(dso);
320         ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",
321            ref);
322         if (ref!=0)
323             return DSERR_GENERIC;
324     } else
325         return rc;
326
327     /* Create a DirectSound object */
328     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
329     ok(rc==DS_OK,"DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
330     if (rc==DS_OK) {
331         LPDIRECTSOUNDBUFFER secondary;
332         DSBUFFERDESC bufdesc;
333         WAVEFORMATEX wfx;
334
335         init_format(&wfx,WAVE_FORMAT_PCM,11025,8,1);
336         ZeroMemory(&bufdesc, sizeof(bufdesc));
337         bufdesc.dwSize=sizeof(bufdesc);
338         bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_CTRL3D;
339         bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
340                                     wfx.nBlockAlign);
341         bufdesc.lpwfxFormat=&wfx;
342         rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
343         ok(rc==DS_OK && secondary!=NULL,
344            "IDirectSound_CreateSoundBuffer() failed to create a secondary "
345            "buffer %s\n",DXGetErrorString8(rc));
346         if (rc==DS_OK && secondary!=NULL) {
347             LPDIRECTSOUND3DBUFFER buffer3d;
348             rc=IDirectSound_QueryInterface(secondary, &IID_IDirectSound3DBuffer,
349                                            (void **)&buffer3d);
350             ok(rc==DS_OK && buffer3d!=NULL,"IDirectSound_QueryInterface() "
351                "failed:  %s\n",DXGetErrorString8(rc));
352             if (rc==DS_OK && buffer3d!=NULL) {
353                 ref=IDirectSound3DBuffer_AddRef(buffer3d);
354                 ok(ref==2,"IDirectSound3DBuffer_AddRef() has %d references, "
355                    "should have 2\n",ref);
356             }
357             ref=IDirectSoundBuffer_AddRef(secondary);
358             ok(ref==2,"IDirectSoundBuffer_AddRef() has %d references, "
359                "should have 2\n",ref);
360         }
361         /* release with buffer */
362         ref=IDirectSound_Release(dso);
363         ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",
364            ref);
365         if (ref!=0)
366             return DSERR_GENERIC;
367     } else
368         return rc;
369
370     return DS_OK;
371 }
372
373 static HRESULT test_primary(LPGUID lpGuid)
374 {
375     HRESULT rc;
376     LPDIRECTSOUND dso=NULL;
377     LPDIRECTSOUNDBUFFER primary=NULL,second=NULL,third=NULL;
378     DSBUFFERDESC bufdesc;
379     DSCAPS dscaps;
380     WAVEFORMATEX wfx;
381     int ref;
382
383     /* Create the DirectSound object */
384     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
385     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
386        "DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
387     if (rc!=DS_OK)
388         return rc;
389
390     /* Get the device capabilities */
391     ZeroMemory(&dscaps, sizeof(dscaps));
392     dscaps.dwSize=sizeof(dscaps);
393     rc=IDirectSound_GetCaps(dso,&dscaps);
394     ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %s\n",DXGetErrorString8(rc));
395     if (rc!=DS_OK)
396         goto EXIT;
397
398     /* DSOUND: Error: Invalid buffer description pointer */
399     rc=IDirectSound_CreateSoundBuffer(dso,0,0,NULL);
400     ok(rc==DSERR_INVALIDPARAM,
401        "IDirectSound_CreateSoundBuffer() should have failed: %s\n",
402        DXGetErrorString8(rc));
403
404     /* DSOUND: Error: Invalid buffer description pointer */
405     rc=IDirectSound_CreateSoundBuffer(dso,0,&primary,NULL);
406     ok(rc==DSERR_INVALIDPARAM && primary==0,
407        "IDirectSound_CreateSoundBuffer() should have failed: rc=%s,"
408        "dsbo=%p\n",DXGetErrorString8(rc),primary);
409
410     /* DSOUND: Error: Invalid buffer description pointer */
411     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,0,NULL);
412     ok(rc==DSERR_INVALIDPARAM && primary==0,
413        "IDirectSound_CreateSoundBuffer() should have failed: rc=%s,"
414        "dsbo=0x%p\n",DXGetErrorString8(rc),primary);
415
416     ZeroMemory(&bufdesc, sizeof(bufdesc));
417
418     /* DSOUND: Error: Invalid size */
419     /* DSOUND: Error: Invalid buffer description */
420     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
421     ok(rc==DSERR_INVALIDPARAM && primary==0,
422        "IDirectSound_CreateSoundBuffer() should have failed: rc=%s,"
423        "primary=%p\n",DXGetErrorString8(rc),primary);
424
425     /* We must call SetCooperativeLevel before calling CreateSoundBuffer */
426     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
427     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
428     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
429        DXGetErrorString8(rc));
430     if (rc!=DS_OK)
431         goto EXIT;
432
433     /* Testing the primary buffer */
434     primary=NULL;
435     ZeroMemory(&bufdesc, sizeof(bufdesc));
436     bufdesc.dwSize=sizeof(bufdesc);
437     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRLVOLUME;
438     bufdesc.lpwfxFormat = &wfx;
439     init_format(&wfx,WAVE_FORMAT_PCM,11025,8,2);
440     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
441     ok(rc==DSERR_INVALIDPARAM,"IDirectSound_CreateSoundBuffer() should have "
442        "returned DSERR_INVALIDPARAM, returned: %s\n", DXGetErrorString8(rc));
443     if (rc==DS_OK && primary!=NULL)
444         IDirectSoundBuffer_Release(primary);
445
446     primary=NULL;
447     ZeroMemory(&bufdesc, sizeof(bufdesc));
448     bufdesc.dwSize=sizeof(bufdesc);
449     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRLVOLUME;
450     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
451     ok((rc==DS_OK && primary!=NULL) || (rc==DSERR_CONTROLUNAVAIL),
452        "IDirectSound_CreateSoundBuffer() failed to create a primary buffer: "
453        "%s\n",DXGetErrorString8(rc));
454     if (rc==DSERR_CONTROLUNAVAIL)
455         trace("  No Primary\n");
456     else if (rc==DS_OK && primary!=NULL) {
457         LONG vol;
458
459         /* Try to create a second primary buffer */
460         /* DSOUND: Error: The primary buffer already exists.
461          * Any changes made to the buffer description will be ignored. */
462         rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&second,NULL);
463         ok(rc==DS_OK && second==primary,
464            "IDirectSound_CreateSoundBuffer() should have returned original "
465            "primary buffer: %s\n",DXGetErrorString8(rc));
466         ref=IDirectSoundBuffer_Release(second);
467         ok(ref==1,"IDirectSoundBuffer_Release() primary has %d references, "
468            "should have 1\n",ref);
469
470         /* Try to duplicate a primary buffer */
471         /* DSOUND: Error: Can't duplicate primary buffers */
472         rc=IDirectSound_DuplicateSoundBuffer(dso,primary,&third);
473         /* rc=0x88780032 */
474         ok(rc!=DS_OK,"IDirectSound_DuplicateSoundBuffer() primary buffer "
475            "should have failed %s\n",DXGetErrorString8(rc));
476
477         rc=IDirectSoundBuffer_GetVolume(primary,&vol);
478         ok(rc==DS_OK,"IDirectSoundBuffer_GetVolume() failed: %s\n",
479            DXGetErrorString8(rc));
480
481         if (winetest_interactive) {
482             trace("Playing a 5 seconds reference tone at the current "
483                   "volume.\n");
484             if (rc==DS_OK)
485                 trace("(the current volume is %d according to DirectSound)\n",
486                       vol);
487             trace("All subsequent tones should be identical to this one.\n");
488             trace("Listen for stutter, changes in pitch, volume, etc.\n");
489         }
490         test_buffer(dso,&primary,1,FALSE,0,FALSE,0,winetest_interactive &&
491                     !(dscaps.dwFlags & DSCAPS_EMULDRIVER),5.0,0,0,0,0,FALSE,0);
492
493         ref=IDirectSoundBuffer_Release(primary);
494         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
495            "should have 0\n",ref);
496     }
497
498     /* Set the CooperativeLevel back to normal */
499     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
500     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
501     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
502        DXGetErrorString8(rc));
503
504 EXIT:
505     ref=IDirectSound_Release(dso);
506     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
507     if (ref!=0)
508         return DSERR_GENERIC;
509
510     return rc;
511 }
512
513 /*
514  * Test the primary buffer at different formats while keeping the
515  * secondary buffer at a constant format.
516  */
517 static HRESULT test_primary_secondary(LPGUID lpGuid)
518 {
519     HRESULT rc;
520     LPDIRECTSOUND dso=NULL;
521     LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
522     DSBUFFERDESC bufdesc;
523     DSCAPS dscaps;
524     WAVEFORMATEX wfx, wfx2;
525     int f,ref;
526
527     /* Create the DirectSound object */
528     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
529     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
530        "DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
531     if (rc!=DS_OK)
532         return rc;
533
534     /* Get the device capabilities */
535     ZeroMemory(&dscaps, sizeof(dscaps));
536     dscaps.dwSize=sizeof(dscaps);
537     rc=IDirectSound_GetCaps(dso,&dscaps);
538     ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %s\n",DXGetErrorString8(rc));
539     if (rc!=DS_OK)
540         goto EXIT;
541
542     /* We must call SetCooperativeLevel before creating primary buffer */
543     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
544     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
545     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
546        DXGetErrorString8(rc));
547     if (rc!=DS_OK)
548         goto EXIT;
549
550     ZeroMemory(&bufdesc, sizeof(bufdesc));
551     bufdesc.dwSize=sizeof(bufdesc);
552     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
553     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
554     ok(rc==DS_OK && primary!=NULL,
555        "IDirectSound_CreateSoundBuffer() failed to create a primary buffer "
556        "%s\n",DXGetErrorString8(rc));
557
558     if (rc==DS_OK && primary!=NULL) {
559         for (f=0;f<NB_FORMATS;f++) {
560             /* We must call SetCooperativeLevel to be allowed to call
561              * SetFormat */
562             /* DSOUND: Setting DirectSound cooperative level to
563              * DSSCL_PRIORITY */
564             rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
565             ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
566                DXGetErrorString8(rc));
567             if (rc!=DS_OK)
568                 goto EXIT;
569
570             init_format(&wfx,WAVE_FORMAT_PCM,formats[f][0],formats[f][1],
571                         formats[f][2]);
572             wfx2=wfx;
573             rc=IDirectSoundBuffer_SetFormat(primary,&wfx);
574
575             if (wfx.wBitsPerSample <= 16)
576                 ok(rc==DS_OK,"IDirectSoundBuffer_SetFormat(%s) failed: %s\n",
577                    format_string(&wfx), DXGetErrorString8(rc));
578             else
579                 ok(rc==DS_OK || rc == E_INVALIDARG, "SetFormat (%s) failed: %s\n",
580                    format_string(&wfx), DXGetErrorString8(rc));
581
582             /* There is no guarantee that SetFormat will actually change the
583              * format to what we asked for. It depends on what the soundcard
584              * supports. So we must re-query the format.
585              */
586             rc=IDirectSoundBuffer_GetFormat(primary,&wfx,sizeof(wfx),NULL);
587             ok(rc==DS_OK,"IDirectSoundBuffer_GetFormat() failed: %s\n",
588                DXGetErrorString8(rc));
589             if (rc==DS_OK &&
590                 (wfx.wFormatTag!=wfx2.wFormatTag ||
591                  wfx.nSamplesPerSec!=wfx2.nSamplesPerSec ||
592                  wfx.wBitsPerSample!=wfx2.wBitsPerSample ||
593                  wfx.nChannels!=wfx2.nChannels)) {
594                 trace("Requested primary format tag=0x%04x %dx%dx%d "
595                       "avg.B/s=%d align=%d\n",
596                       wfx2.wFormatTag,wfx2.nSamplesPerSec,wfx2.wBitsPerSample,
597                       wfx2.nChannels,wfx2.nAvgBytesPerSec,wfx2.nBlockAlign);
598                 trace("Got tag=0x%04x %dx%dx%d avg.B/s=%d align=%d\n",
599                       wfx.wFormatTag,wfx.nSamplesPerSec,wfx.wBitsPerSample,
600                       wfx.nChannels,wfx.nAvgBytesPerSec,wfx.nBlockAlign);
601             }
602
603             /* Set the CooperativeLevel back to normal */
604             /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
605             rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
606             ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
607                DXGetErrorString8(rc));
608
609             init_format(&wfx2,WAVE_FORMAT_PCM,11025,16,2);
610
611             secondary=NULL;
612             ZeroMemory(&bufdesc, sizeof(bufdesc));
613             bufdesc.dwSize=sizeof(bufdesc);
614             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
615             bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
616                                         wfx.nBlockAlign);
617             bufdesc.lpwfxFormat=&wfx2;
618             if (winetest_interactive) {
619                 trace("  Testing a primary buffer at %dx%dx%d with a "
620                       "secondary buffer at %dx%dx%d\n",
621                       wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,
622                       wfx2.nSamplesPerSec,wfx2.wBitsPerSample,wfx2.nChannels);
623             }
624             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
625             ok(rc==DS_OK && secondary!=NULL,
626                "IDirectSound_CreateSoundBuffer() failed to create a secondary "
627                "buffer %s\n",DXGetErrorString8(rc));
628
629             if (rc==DS_OK && secondary!=NULL) {
630                 test_buffer(dso,&secondary,0,FALSE,0,FALSE,0,
631                             winetest_interactive,1.0,0,NULL,0,0,FALSE,0);
632
633                 ref=IDirectSoundBuffer_Release(secondary);
634                 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
635                    "should have 0\n",ref);
636             }
637         }
638
639         ref=IDirectSoundBuffer_Release(primary);
640         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
641            "should have 0\n",ref);
642     }
643
644     /* Set the CooperativeLevel back to normal */
645     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
646     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
647     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
648        DXGetErrorString8(rc));
649
650 EXIT:
651     ref=IDirectSound_Release(dso);
652     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
653     if (ref!=0)
654         return DSERR_GENERIC;
655
656     return rc;
657 }
658
659 static HRESULT test_secondary(LPGUID lpGuid)
660 {
661     HRESULT rc;
662     LPDIRECTSOUND dso=NULL;
663     LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
664     DSBUFFERDESC bufdesc;
665     DSCAPS dscaps;
666     WAVEFORMATEX wfx, wfx1;
667     DWORD f;
668     int ref;
669
670     /* Create the DirectSound object */
671     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
672     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
673        "DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
674     if (rc!=DS_OK)
675         return rc;
676
677     /* Get the device capabilities */
678     ZeroMemory(&dscaps, sizeof(dscaps));
679     dscaps.dwSize=sizeof(dscaps);
680     rc=IDirectSound_GetCaps(dso,&dscaps);
681     ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %s\n",DXGetErrorString8(rc));
682     if (rc!=DS_OK)
683         goto EXIT;
684
685     /* We must call SetCooperativeLevel before creating primary buffer */
686     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
687     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
688     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
689        DXGetErrorString8(rc));
690     if (rc!=DS_OK)
691         goto EXIT;
692
693     ZeroMemory(&bufdesc, sizeof(bufdesc));
694     bufdesc.dwSize=sizeof(bufdesc);
695     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
696     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
697     ok(rc==DS_OK && primary!=NULL,
698        "IDirectSound_CreateSoundBuffer() failed to create a primary buffer "
699        "%s\n",DXGetErrorString8(rc));
700
701     if (rc==DS_OK && primary!=NULL) {
702         rc=IDirectSoundBuffer_GetFormat(primary,&wfx1,sizeof(wfx1),NULL);
703         ok(rc==DS_OK,"IDirectSoundBuffer8_Getformat() failed: %s\n",
704            DXGetErrorString8(rc));
705         if (rc!=DS_OK)
706             goto EXIT1;
707
708         for (f=0;f<NB_FORMATS;f++) {
709             WAVEFORMATEXTENSIBLE wfxe;
710             init_format(&wfx,WAVE_FORMAT_PCM,formats[f][0],formats[f][1],
711                         formats[f][2]);
712             secondary=NULL;
713             ZeroMemory(&bufdesc, sizeof(bufdesc));
714             bufdesc.dwSize=sizeof(bufdesc);
715             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
716             bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
717                                         wfx.nBlockAlign);
718             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
719             ok(rc==DSERR_INVALIDPARAM,"IDirectSound_CreateSoundBuffer() "
720                "should have returned DSERR_INVALIDPARAM, returned: %s\n",
721                DXGetErrorString8(rc));
722             if (rc==DS_OK && secondary!=NULL)
723                 IDirectSoundBuffer_Release(secondary);
724
725             secondary=NULL;
726             ZeroMemory(&bufdesc, sizeof(bufdesc));
727             bufdesc.dwSize=sizeof(bufdesc);
728             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
729             bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
730                                         wfx.nBlockAlign);
731             bufdesc.lpwfxFormat=&wfx;
732             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
733             if (gotdx8 || wfx.wBitsPerSample <= 16)
734             {
735                 if (wfx.wBitsPerSample > 16)
736                     ok(rc == DSERR_CONTROLUNAVAIL && !secondary, "IDirectSound_CreateSoundBuffer() "
737                         "should have returned DSERR_CONTROLUNAVAIL and NULL, returned: %s %p\n",
738                         DXGetErrorString8(rc), secondary);
739                 else
740                     ok(rc==DS_OK && secondary!=NULL,
741                         "IDirectSound_CreateSoundBuffer() failed to create a secondary "
742                         "buffer %s\n",DXGetErrorString8(rc));
743             }
744             else
745                 ok(rc==E_INVALIDARG, "Creating %d bpp buffer on dx < 8 returned: %p %s\n",
746                    wfx.wBitsPerSample, secondary, DXGetErrorString8(rc));
747
748             if (!gotdx8)
749             {
750                 skip("Not doing the WAVE_FORMAT_EXTENSIBLE tests\n");
751                 /* Apparently they succeed with bogus values,
752                  * which means that older dsound doesn't look at them
753                  */
754                 goto no_wfe;
755             }
756
757             if (secondary)
758                 IDirectSoundBuffer_Release(secondary);
759             secondary = NULL;
760
761             bufdesc.lpwfxFormat=(WAVEFORMATEX*)&wfxe;
762             wfxe.Format = wfx;
763             wfxe.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
764             wfxe.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
765             wfxe.Format.cbSize = 1;
766             wfxe.Samples.wValidBitsPerSample = wfx.wBitsPerSample;
767             wfxe.dwChannelMask = (wfx.nChannels == 1 ? KSAUDIO_SPEAKER_MONO : KSAUDIO_SPEAKER_STEREO);
768
769             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
770             ok(rc==DSERR_INVALIDPARAM && !secondary,
771                 "IDirectSound_CreateSoundBuffer() returned: %s %p\n",
772                 DXGetErrorString8(rc), secondary);
773             if (secondary)
774                 IDirectSoundBuffer_Release(secondary);
775
776             wfxe.Format.cbSize = sizeof(wfxe) - sizeof(wfx) + 1;
777
778             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
779             ok(DSERR_CONTROLUNAVAIL && !secondary,
780                 "IDirectSound_CreateSoundBuffer() returned: %s %p\n",
781                 DXGetErrorString8(rc), secondary);
782             if (secondary)
783                 IDirectSoundBuffer_Release(secondary);
784
785             wfxe.Format.cbSize = sizeof(wfxe) - sizeof(wfx);
786             wfxe.SubFormat = GUID_NULL;
787             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
788             ok(rc==DSERR_INVALIDPARAM && !secondary,
789                 "IDirectSound_CreateSoundBuffer() returned: %s %p\n",
790                 DXGetErrorString8(rc), secondary);
791             if (secondary)
792                 IDirectSoundBuffer_Release(secondary);
793             wfxe.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
794
795             ++wfxe.Samples.wValidBitsPerSample;
796             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
797             ok(rc==DSERR_INVALIDPARAM && !secondary,
798                 "IDirectSound_CreateSoundBuffer() returned: %s %p\n",
799                 DXGetErrorString8(rc), secondary);
800             if (secondary)
801                 IDirectSoundBuffer_Release(secondary);
802             --wfxe.Samples.wValidBitsPerSample;
803
804             wfxe.Samples.wValidBitsPerSample = 0;
805             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
806             ok(rc==DS_OK && secondary,
807                 "IDirectSound_CreateSoundBuffer() returned: %s %p\n",
808                 DXGetErrorString8(rc), secondary);
809             if (secondary)
810                 IDirectSoundBuffer_Release(secondary);
811             wfxe.Samples.wValidBitsPerSample = wfxe.Format.wBitsPerSample;
812
813             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
814             ok(rc==DS_OK && secondary!=NULL,
815                 "IDirectSound_CreateSoundBuffer() failed to create a secondary "
816                 "buffer %s\n",DXGetErrorString8(rc));
817
818 no_wfe:
819             if (rc==DS_OK && secondary!=NULL) {
820                 if (winetest_interactive) {
821                     trace("  Testing a secondary buffer at %dx%dx%d "
822                         "with a primary buffer at %dx%dx%d\n",
823                         wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,
824                         wfx1.nSamplesPerSec,wfx1.wBitsPerSample,wfx1.nChannels);
825                 }
826                 test_buffer(dso,&secondary,0,FALSE,0,FALSE,0,
827                             winetest_interactive,1.0,0,NULL,0,0,FALSE,0);
828
829                 ref=IDirectSoundBuffer_Release(secondary);
830                 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
831                    "should have 0\n",ref);
832             }
833         }
834 EXIT1:
835         ref=IDirectSoundBuffer_Release(primary);
836         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
837            "should have 0\n",ref);
838     }
839
840     /* Set the CooperativeLevel back to normal */
841     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
842     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
843     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
844        DXGetErrorString8(rc));
845
846 EXIT:
847     ref=IDirectSound_Release(dso);
848     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
849     if (ref!=0)
850         return DSERR_GENERIC;
851
852     return rc;
853 }
854
855 static HRESULT test_block_align(LPGUID lpGuid)
856 {
857     HRESULT rc;
858     LPDIRECTSOUND dso=NULL;
859     LPDIRECTSOUNDBUFFER secondary=NULL;
860     DSBUFFERDESC bufdesc;
861     DSBCAPS dsbcaps;
862     WAVEFORMATEX wfx;
863     DWORD pos, pos2;
864     int ref;
865
866     /* Create the DirectSound object */
867     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
868     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
869        "DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
870     if (rc!=DS_OK)
871         return rc;
872
873     init_format(&wfx,WAVE_FORMAT_PCM,11025,16,2);
874     ZeroMemory(&bufdesc, sizeof(bufdesc));
875     bufdesc.dwSize=sizeof(bufdesc);
876     bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
877     bufdesc.dwBufferBytes=wfx.nAvgBytesPerSec + 1;
878     bufdesc.lpwfxFormat=&wfx;
879     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
880     ok(rc==DS_OK,"IDirectSound_CreateSoundBuffer() "
881        "should have returned DS_OK, returned: %s\n",
882        DXGetErrorString8(rc));
883
884     if (rc==DS_OK && secondary!=NULL) {
885         ZeroMemory(&dsbcaps, sizeof(dsbcaps));
886         dsbcaps.dwSize = sizeof(dsbcaps);
887         rc=IDirectSoundBuffer_GetCaps(secondary,&dsbcaps);
888         ok(rc==DS_OK,"IDirectSoundBuffer_GetCaps() should have returned DS_OK, "
889            "returned: %s\n", DXGetErrorString8(rc));
890         if (rc==DS_OK && wfx.nBlockAlign > 1)
891         {
892             ok(dsbcaps.dwBufferBytes==(wfx.nAvgBytesPerSec + wfx.nBlockAlign),
893                "Buffer size not a multiple of nBlockAlign: requested %d, "
894                "got %d, should be %d\n", bufdesc.dwBufferBytes,
895                dsbcaps.dwBufferBytes, wfx.nAvgBytesPerSec + wfx.nBlockAlign);
896
897             rc = IDirectSoundBuffer_SetCurrentPosition(secondary, 0);
898             ok(rc == DS_OK, "Could not set position to 0: %s\n", DXGetErrorString8(rc));
899             rc = IDirectSoundBuffer_GetCurrentPosition(secondary, &pos, NULL);
900             ok(rc == DS_OK, "Could not get position: %s\n", DXGetErrorString8(rc));
901             rc = IDirectSoundBuffer_SetCurrentPosition(secondary, 1);
902             ok(rc == DS_OK, "Could not set position to 1: %s\n", DXGetErrorString8(rc));
903             rc = IDirectSoundBuffer_GetCurrentPosition(secondary, &pos2, NULL);
904             ok(rc == DS_OK, "Could not get new position: %s\n", DXGetErrorString8(rc));
905             ok(pos == pos2, "Positions not the same! Old position: %d, new position: %d\n", pos, pos2);
906         }
907         ref=IDirectSoundBuffer_Release(secondary);
908         ok(ref==0,"IDirectSoundBuffer_Release() secondary has %d references, "
909            "should have 0\n",ref);
910     }
911
912     ref=IDirectSound_Release(dso);
913     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
914     if (ref!=0)
915         return DSERR_GENERIC;
916
917     return rc;
918 }
919
920 static struct fmt {
921     int bits;
922     int channels;
923 } fmts[] = { { 8, 1 }, { 8, 2 }, { 16, 1 }, {16, 2 } };
924
925 static HRESULT test_frequency(LPGUID lpGuid)
926 {
927     HRESULT rc;
928     LPDIRECTSOUND dso=NULL;
929     LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
930     DSBUFFERDESC bufdesc;
931     DSCAPS dscaps;
932     WAVEFORMATEX wfx, wfx1;
933     DWORD f, r;
934     int ref;
935     int rates[] = { 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100,
936                     48000, 96000 };
937
938     /* Create the DirectSound object */
939     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
940     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
941        "DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
942     if (rc!=DS_OK)
943         return rc;
944
945     /* Get the device capabilities */
946     ZeroMemory(&dscaps, sizeof(dscaps));
947     dscaps.dwSize=sizeof(dscaps);
948     rc=IDirectSound_GetCaps(dso,&dscaps);
949     ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %s\n",DXGetErrorString8(rc));
950     if (rc!=DS_OK)
951         goto EXIT;
952
953     /* We must call SetCooperativeLevel before creating primary buffer */
954     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
955     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
956     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
957        DXGetErrorString8(rc));
958     if (rc!=DS_OK)
959         goto EXIT;
960
961     ZeroMemory(&bufdesc, sizeof(bufdesc));
962     bufdesc.dwSize=sizeof(bufdesc);
963     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
964     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
965     ok(rc==DS_OK && primary!=NULL,
966        "IDirectSound_CreateSoundBuffer() failed to create a primary buffer "
967        "%s\n",DXGetErrorString8(rc));
968
969     if (rc==DS_OK && primary!=NULL) {
970         rc=IDirectSoundBuffer_GetFormat(primary,&wfx1,sizeof(wfx1),NULL);
971         ok(rc==DS_OK,"IDirectSoundBuffer8_Getformat() failed: %s\n",
972            DXGetErrorString8(rc));
973         if (rc!=DS_OK)
974             goto EXIT1;
975
976         for (f=0;f<sizeof(fmts)/sizeof(fmts[0]);f++) {
977         for (r=0;r<sizeof(rates)/sizeof(rates[0]);r++) {
978             init_format(&wfx,WAVE_FORMAT_PCM,11025,fmts[f].bits,
979                         fmts[f].channels);
980             secondary=NULL;
981             ZeroMemory(&bufdesc, sizeof(bufdesc));
982             bufdesc.dwSize=sizeof(bufdesc);
983             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2|DSBCAPS_CTRLFREQUENCY;
984             bufdesc.dwBufferBytes=align((wfx.nAvgBytesPerSec*rates[r]/11025)*
985                                         BUFFER_LEN/1000,wfx.nBlockAlign);
986             bufdesc.lpwfxFormat=&wfx;
987             if (winetest_interactive) {
988                 trace("  Testing a secondary buffer at %dx%dx%d "
989                       "with a primary buffer at %dx%dx%d\n",
990                       wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,
991                       wfx1.nSamplesPerSec,wfx1.wBitsPerSample,wfx1.nChannels);
992             }
993             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
994             ok(rc==DS_OK && secondary!=NULL,
995                "IDirectSound_CreateSoundBuffer() failed to create a secondary "
996                "buffer %s\n",DXGetErrorString8(rc));
997
998             if (rc==DS_OK && secondary!=NULL) {
999                 test_buffer(dso,&secondary,0,FALSE,0,FALSE,0,
1000                             winetest_interactive,1.0,0,NULL,0,0,TRUE,rates[r]);
1001
1002                 ref=IDirectSoundBuffer_Release(secondary);
1003                 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
1004                    "should have 0\n",ref);
1005             }
1006         }
1007         }
1008 EXIT1:
1009         ref=IDirectSoundBuffer_Release(primary);
1010         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
1011            "should have 0\n",ref);
1012     }
1013
1014     /* Set the CooperativeLevel back to normal */
1015     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
1016     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
1017     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
1018        DXGetErrorString8(rc));
1019
1020 EXIT:
1021     ref=IDirectSound_Release(dso);
1022     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
1023     if (ref!=0)
1024         return DSERR_GENERIC;
1025
1026     return rc;
1027 }
1028
1029 static unsigned int number;
1030
1031 static BOOL WINAPI dsenum_callback(LPGUID lpGuid, LPCSTR lpcstrDescription,
1032                                    LPCSTR lpcstrModule, LPVOID lpContext)
1033 {
1034     HRESULT rc;
1035     trace("*** Testing %s - %s ***\n",lpcstrDescription,lpcstrModule);
1036
1037     /* Don't test the primary device */
1038     if (!number++)
1039     {
1040         ok (!lpcstrModule[0], "lpcstrModule(%s) != NULL\n", lpcstrModule);
1041         return 1;
1042     }
1043
1044     rc = test_dsound(lpGuid);
1045     if (rc == DSERR_NODRIVER)
1046         trace("  No Driver\n");
1047     else if (rc == DSERR_ALLOCATED)
1048         trace("  Already In Use\n");
1049     else if (rc == E_FAIL)
1050         trace("  No Device\n");
1051     else {
1052         test_block_align(lpGuid);
1053         test_primary(lpGuid);
1054         test_primary_secondary(lpGuid);
1055         test_secondary(lpGuid);
1056         test_frequency(lpGuid);
1057     }
1058
1059     return 1;
1060 }
1061
1062 static void dsound_tests(void)
1063 {
1064     HRESULT rc;
1065     rc=pDirectSoundEnumerateA(&dsenum_callback,NULL);
1066     ok(rc==DS_OK,"DirectSoundEnumerateA() failed: %s\n",DXGetErrorString8(rc));
1067 }
1068
1069 START_TEST(dsound)
1070 {
1071     HMODULE hDsound;
1072
1073     CoInitialize(NULL);
1074
1075     hDsound = LoadLibrary("dsound.dll");
1076     if (hDsound)
1077     {
1078         ok( FreeLibrary(hDsound), "FreeLibrary(1) returned %d\n", GetLastError());
1079         ok( FreeLibrary(hDsound), "FreeLibrary(2) returned %d\n", GetLastError());
1080         ok(!FreeLibrary(hDsound), "DirectSound DLL still loaded\n");
1081     }
1082
1083     hDsound = LoadLibrary("dsound.dll");
1084     if (hDsound)
1085     {
1086         trace("DLL Version: %s\n", get_file_version("dsound.dll"));
1087
1088         pDirectSoundEnumerateA = (void*)GetProcAddress(hDsound,
1089             "DirectSoundEnumerateA");
1090         pDirectSoundCreate = (void*)GetProcAddress(hDsound,
1091             "DirectSoundCreate");
1092
1093         gotdx8 = !!GetProcAddress(hDsound, "DirectSoundCreate8");
1094
1095         IDirectSound_tests();
1096         dsound_tests();
1097
1098         FreeLibrary(hDsound);
1099     }
1100     else
1101         skip("dsound.dll not found!\n");
1102
1103     CoUninitialize();
1104 }