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