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