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