Release 1.5.29.
[wine] / dlls / dsound / tests / dsound.c
1 /*
2  * Tests basic sound playback in DirectSound.
3  * In particular we test each standard Windows sound format to make sure
4  * we handle the sound card/driver quirks correctly.
5  *
6  * Part of this test involves playing test tones. But this only makes
7  * sense if someone is going to carefully listen to it, and would only
8  * bother everyone else.
9  * So this is only done if the test is being run in interactive mode.
10  *
11  * Copyright (c) 2002-2004 Francois Gouget
12  * Copyright (c) 2007 Maarten Lankhorst
13  *
14  * This library is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU Lesser General Public
16  * License as published by the Free Software Foundation; either
17  * version 2.1 of the License, or (at your option) any later version.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27  */
28
29 #include <windows.h>
30
31 #include "wine/test.h"
32 #include "dsound.h"
33 #include "dsconf.h"
34 #include "mmreg.h"
35 #include "initguid.h"
36 #include "ks.h"
37 #include "ksmedia.h"
38
39 #include "dsound_test.h"
40
41 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
42
43 static HRESULT (WINAPI *pDirectSoundEnumerateA)(LPDSENUMCALLBACKA,LPVOID)=NULL;
44 static HRESULT (WINAPI *pDirectSoundCreate)(LPCGUID,LPDIRECTSOUND*,
45     LPUNKNOWN)=NULL;
46
47 static BOOL gotdx8;
48
49 static void IDirectSound_test(LPDIRECTSOUND dso, BOOL initialized,
50                               LPCGUID lpGuid)
51 {
52     HRESULT rc;
53     DSCAPS dscaps;
54     int ref;
55     IUnknown * unknown;
56     IDirectSound * ds;
57     IDirectSound8 * ds8;
58     DWORD speaker_config, new_speaker_config, ref_speaker_config;
59
60     /* Try to Query for objects */
61     rc=IDirectSound_QueryInterface(dso,&IID_IUnknown,(LPVOID*)&unknown);
62     ok(rc==DS_OK,"IDirectSound_QueryInterface(IID_IUnknown) failed: %08x\n", rc);
63     if (rc==DS_OK)
64         IDirectSound_Release(unknown);
65
66     rc=IDirectSound_QueryInterface(dso,&IID_IDirectSound,(LPVOID*)&ds);
67     ok(rc==DS_OK,"IDirectSound_QueryInterface(IID_IDirectSound) failed: %08x\n", rc);
68     if (rc==DS_OK)
69         IDirectSound_Release(ds);
70
71     rc=IDirectSound_QueryInterface(dso,&IID_IDirectSound8,(LPVOID*)&ds8);
72     ok(rc==E_NOINTERFACE,"IDirectSound_QueryInterface(IID_IDirectSound8) "
73        "should have failed: %08x\n",rc);
74     if (rc==DS_OK)
75         IDirectSound8_Release(ds8);
76
77     if (initialized == FALSE) {
78         /* try uninitialized object */
79         rc=IDirectSound_GetCaps(dso,0);
80         ok(rc==DSERR_UNINITIALIZED,"IDirectSound_GetCaps(NULL) "
81            "should have returned DSERR_UNINITIALIZED, returned: %08x\n", rc);
82
83         rc=IDirectSound_GetCaps(dso,&dscaps);
84         ok(rc==DSERR_UNINITIALIZED,"IDirectSound_GetCaps() "
85            "should have returned DSERR_UNINITIALIZED, returned: %08x\n", rc);
86
87         rc=IDirectSound_Compact(dso);
88         ok(rc==DSERR_UNINITIALIZED,"IDirectSound_Compact() "
89            "should have returned DSERR_UNINITIALIZED, returned: %08x\n", rc);
90
91         rc=IDirectSound_GetSpeakerConfig(dso,&speaker_config);
92         ok(rc==DSERR_UNINITIALIZED,"IDirectSound_GetSpeakerConfig() "
93            "should have returned DSERR_UNINITIALIZED, returned: %08x\n", rc);
94
95         rc=IDirectSound_Initialize(dso,lpGuid);
96         ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
97            "IDirectSound_Initialize() failed: %08x\n",rc);
98         if (rc==DSERR_NODRIVER) {
99             trace("  No Driver\n");
100             goto EXIT;
101         } else if (rc==E_FAIL) {
102             trace("  No Device\n");
103             goto EXIT;
104         } else if (rc==DSERR_ALLOCATED) {
105             trace("  Already In Use\n");
106             goto EXIT;
107         }
108     }
109
110     rc=IDirectSound_Initialize(dso,lpGuid);
111     ok(rc==DSERR_ALREADYINITIALIZED, "IDirectSound_Initialize() "
112        "should have returned DSERR_ALREADYINITIALIZED: %08x\n", rc);
113
114     /* DSOUND: Error: Invalid caps buffer */
115     rc=IDirectSound_GetCaps(dso,0);
116     ok(rc==DSERR_INVALIDPARAM,"IDirectSound_GetCaps(NULL) "
117        "should have returned DSERR_INVALIDPARAM, returned: %08x\n", rc);
118
119     ZeroMemory(&dscaps, sizeof(dscaps));
120
121     /* DSOUND: Error: Invalid caps buffer */
122     rc=IDirectSound_GetCaps(dso,&dscaps);
123     ok(rc==DSERR_INVALIDPARAM,"IDirectSound_GetCaps() "
124        "should have returned DSERR_INVALIDPARAM, returned: %08x\n", rc);
125
126     dscaps.dwSize=sizeof(dscaps);
127
128     /* DSOUND: Running on a certified driver */
129     rc=IDirectSound_GetCaps(dso,&dscaps);
130     ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %08x\n",rc);
131
132     rc=IDirectSound_Compact(dso);
133     ok(rc==DSERR_PRIOLEVELNEEDED,"IDirectSound_Compact() failed: %08x\n", rc);
134
135     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
136     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
137
138     rc=IDirectSound_Compact(dso);
139     ok(rc==DS_OK,"IDirectSound_Compact() failed: %08x\n",rc);
140
141     rc=IDirectSound_GetSpeakerConfig(dso,0);
142     ok(rc==DSERR_INVALIDPARAM,"IDirectSound_GetSpeakerConfig(NULL) "
143        "should have returned DSERR_INVALIDPARAM, returned: %08x\n", rc);
144
145     rc=IDirectSound_GetSpeakerConfig(dso,&speaker_config);
146     ok(rc==DS_OK,"IDirectSound_GetSpeakerConfig() failed: %08x\n", rc);
147     ref_speaker_config = speaker_config;
148
149     speaker_config = DSSPEAKER_COMBINED(DSSPEAKER_STEREO,
150                                         DSSPEAKER_GEOMETRY_WIDE);
151     if (speaker_config == ref_speaker_config)
152         speaker_config = DSSPEAKER_COMBINED(DSSPEAKER_STEREO,
153                                             DSSPEAKER_GEOMETRY_NARROW);
154     if(rc==DS_OK) {
155         rc=IDirectSound_SetSpeakerConfig(dso,speaker_config);
156         ok(rc==DS_OK,"IDirectSound_SetSpeakerConfig() failed: %08x\n", rc);
157     }
158     if (rc==DS_OK) {
159         rc=IDirectSound_GetSpeakerConfig(dso,&new_speaker_config);
160         ok(rc==DS_OK,"IDirectSound_GetSpeakerConfig() failed: %08x\n", rc);
161         if (rc==DS_OK && speaker_config!=new_speaker_config)
162                trace("IDirectSound_GetSpeakerConfig() failed to set speaker "
163                "config: expected 0x%08x, got 0x%08x\n",
164                speaker_config,new_speaker_config);
165         IDirectSound_SetSpeakerConfig(dso,ref_speaker_config);
166     }
167
168 EXIT:
169     ref=IDirectSound_Release(dso);
170     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
171 }
172
173 static void IDirectSound_tests(void)
174 {
175     HRESULT rc;
176     IDirectSound *dso=(IDirectSound*)0xdeadbeef;
177     LPCLASSFACTORY cf=NULL;
178
179     trace("Testing IDirectSound\n");
180
181     rc=CoGetClassObject(&CLSID_DirectSound, CLSCTX_INPROC_SERVER, NULL,
182                         &IID_IClassFactory, (void**)&cf);
183     ok(rc==S_OK,"CoGetClassObject(CLSID_DirectSound, IID_IClassFactory) "
184        "failed: %08x\n", rc);
185
186     rc=CoGetClassObject(&CLSID_DirectSound, CLSCTX_INPROC_SERVER, NULL,
187                         &IID_IUnknown, (void**)&cf);
188     ok(rc==S_OK,"CoGetClassObject(CLSID_DirectSound, IID_IUnknown) "
189        "failed: %08x\n", rc);
190
191     /* COM aggregation */
192     rc=CoCreateInstance(&CLSID_DirectSound, (IUnknown*)&dso, CLSCTX_INPROC_SERVER,
193                         &IID_IDirectSound, (void**)&dso);
194     ok(rc==CLASS_E_NOAGGREGATION || broken(rc==DSERR_INVALIDPARAM),
195        "DirectMusicPerformance create failed: %08x, expected CLASS_E_NOAGGREGATION\n", rc);
196
197     /* try the COM class factory method of creation with no device specified */
198     rc=CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
199                         &IID_IDirectSound, (void**)&dso);
200     ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSound) failed: %08x\n", rc);
201     if (dso)
202         IDirectSound_test(dso, FALSE, NULL);
203
204     /* try the COM class factory method of creation with default playback
205      * device specified */
206     rc=CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
207                         &IID_IDirectSound, (void**)&dso);
208     ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSound) failed: %08x\n", rc);
209     if (dso)
210         IDirectSound_test(dso, FALSE, &DSDEVID_DefaultPlayback);
211
212     /* try the COM class factory method of creation with default voice
213      * playback device specified */
214     rc=CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
215                         &IID_IDirectSound, (void**)&dso);
216     ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSound) failed: %08x\n", rc);
217     if (dso)
218         IDirectSound_test(dso, FALSE, &DSDEVID_DefaultVoicePlayback);
219
220     /* try the COM class factory method of creation with a bad
221      * IID specified */
222     rc=CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
223                         &CLSID_DirectSoundPrivate, (void**)&dso);
224     ok(rc==E_NOINTERFACE,
225        "CoCreateInstance(CLSID_DirectSound,CLSID_DirectSoundPrivate) "
226        "should have failed: %08x\n",rc);
227
228     /* try the COM class factory method of creation with a bad
229      * GUID and IID specified */
230     rc=CoCreateInstance(&CLSID_DirectSoundPrivate, NULL, CLSCTX_INPROC_SERVER,
231                         &IID_IDirectSound, (void**)&dso);
232     ok(rc==REGDB_E_CLASSNOTREG,
233        "CoCreateInstance(CLSID_DirectSoundPrivate,IID_IDirectSound) "
234        "should have failed: %08x\n",rc);
235
236     /* try with no device specified */
237     rc=pDirectSoundCreate(NULL,&dso,NULL);
238     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
239        "DirectSoundCreate(NULL) failed: %08x\n",rc);
240     if (rc==S_OK && dso)
241         IDirectSound_test(dso, TRUE, NULL);
242
243     /* try with default playback device specified */
244     rc=pDirectSoundCreate(&DSDEVID_DefaultPlayback,&dso,NULL);
245     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
246        "DirectSoundCreate(DSDEVID_DefaultPlayback) failed: %08x\n", rc);
247     if (rc==DS_OK && dso)
248         IDirectSound_test(dso, TRUE, NULL);
249
250     /* try with default voice playback device specified */
251     rc=pDirectSoundCreate(&DSDEVID_DefaultVoicePlayback,&dso,NULL);
252     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
253        "DirectSoundCreate(DSDEVID_DefaultVoicePlayback) failed: %08x\n", rc);
254     if (rc==DS_OK && dso)
255         IDirectSound_test(dso, TRUE, NULL);
256
257     /* try with a bad device specified */
258     rc=pDirectSoundCreate(&DSDEVID_DefaultVoiceCapture,&dso,NULL);
259     ok(rc==DSERR_NODRIVER,"DirectSoundCreate(DSDEVID_DefaultVoiceCapture) "
260        "should have failed: %08x\n",rc);
261     if (rc==DS_OK && dso)
262         IDirectSound_Release(dso);
263 }
264
265 static HRESULT test_dsound(LPGUID lpGuid)
266 {
267     HRESULT rc;
268     LPDIRECTSOUND dso=NULL;
269     int ref;
270
271     /* DSOUND: Error: Invalid interface buffer */
272     rc=pDirectSoundCreate(lpGuid,0,NULL);
273     ok(rc==DSERR_INVALIDPARAM,"DirectSoundCreate() should have returned "
274        "DSERR_INVALIDPARAM, returned: %08x\n",rc);
275
276     /* Create the DirectSound object */
277     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
278     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
279        "DirectSoundCreate() failed: %08x\n",rc);
280     if (rc!=DS_OK)
281         return rc;
282
283     /* Try the enumerated device */
284     IDirectSound_test(dso, TRUE, lpGuid);
285
286     /* Try the COM class factory method of creation with enumerated device */
287     rc=CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
288                         &IID_IDirectSound, (void**)&dso);
289     ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSound) failed: %08x\n", rc);
290     if (dso)
291         IDirectSound_test(dso, FALSE, lpGuid);
292
293     /* Create a DirectSound object */
294     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
295     ok(rc==DS_OK,"DirectSoundCreate() failed: %08x\n",rc);
296     if (rc==DS_OK) {
297         LPDIRECTSOUND dso1=NULL;
298
299         /* Create a second DirectSound object */
300         rc=pDirectSoundCreate(lpGuid,&dso1,NULL);
301         ok(rc==DS_OK,"DirectSoundCreate() failed: %08x\n",rc);
302         if (rc==DS_OK) {
303             /* Release the second DirectSound object */
304             ref=IDirectSound_Release(dso1);
305             ok(ref==0,"IDirectSound_Release() has %d references, should have "
306                "0\n",ref);
307             ok(dso!=dso1,"DirectSound objects should be unique: dso=%p,dso1=%p\n",dso,dso1);
308         }
309
310         /* Release the first DirectSound object */
311         ref=IDirectSound_Release(dso);
312         ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",
313            ref);
314         if (ref!=0)
315             return DSERR_GENERIC;
316     } else
317         return rc;
318
319     /* Create a DirectSound object */
320     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
321     ok(rc==DS_OK,"DirectSoundCreate() failed: %08x\n",rc);
322     if (rc==DS_OK) {
323         LPDIRECTSOUNDBUFFER secondary;
324         DSBUFFERDESC bufdesc;
325         WAVEFORMATEX wfx;
326
327         init_format(&wfx,WAVE_FORMAT_PCM,11025,8,1);
328         ZeroMemory(&bufdesc, sizeof(bufdesc));
329         bufdesc.dwSize=sizeof(bufdesc);
330         bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_CTRL3D;
331         bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
332                                     wfx.nBlockAlign);
333         bufdesc.lpwfxFormat=&wfx;
334         rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
335         ok((rc==DS_OK && secondary!=NULL) || broken(rc == DSERR_CONTROLUNAVAIL), /* vmware drivers on w2k */
336            "IDirectSound_CreateSoundBuffer() failed to create a secondary "
337            "buffer %08x\n",rc);
338         if (rc==DS_OK && secondary!=NULL) {
339             LPDIRECTSOUND3DBUFFER buffer3d;
340             rc=IDirectSound_QueryInterface(secondary, &IID_IDirectSound3DBuffer,
341                                            (void **)&buffer3d);
342             ok(rc==DS_OK && buffer3d!=NULL,"IDirectSound_QueryInterface() "
343                "failed: %08x\n",rc);
344             if (rc==DS_OK && buffer3d!=NULL) {
345                 ref=IDirectSound3DBuffer_AddRef(buffer3d);
346                 ok(ref==2,"IDirectSound3DBuffer_AddRef() has %d references, "
347                    "should have 2\n",ref);
348             }
349             ref=IDirectSoundBuffer_AddRef(secondary);
350             ok(ref==2,"IDirectSoundBuffer_AddRef() has %d references, "
351                "should have 2\n",ref);
352         }
353         /* release with buffer */
354         ref=IDirectSound_Release(dso);
355         ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",
356            ref);
357         if (ref!=0)
358             return DSERR_GENERIC;
359     } else
360         return rc;
361
362     return DS_OK;
363 }
364
365 static HRESULT test_primary(LPGUID lpGuid)
366 {
367     HRESULT rc;
368     LPDIRECTSOUND dso=NULL;
369     LPDIRECTSOUNDBUFFER primary=NULL,second=NULL,third=NULL;
370     DSBUFFERDESC bufdesc;
371     DSCAPS dscaps;
372     WAVEFORMATEX wfx;
373     int ref;
374
375     /* Create the DirectSound object */
376     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
377     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
378        "DirectSoundCreate() failed: %08x\n",rc);
379     if (rc!=DS_OK)
380         return rc;
381
382     /* Get the device capabilities */
383     ZeroMemory(&dscaps, sizeof(dscaps));
384     dscaps.dwSize=sizeof(dscaps);
385     rc=IDirectSound_GetCaps(dso,&dscaps);
386     ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %08x\n",rc);
387     if (rc!=DS_OK)
388         goto EXIT;
389
390     /* DSOUND: Error: Invalid buffer description pointer */
391     rc=IDirectSound_CreateSoundBuffer(dso,0,0,NULL);
392     ok(rc==DSERR_INVALIDPARAM,
393        "IDirectSound_CreateSoundBuffer() should have failed: %08x\n", rc);
394
395     /* DSOUND: Error: NULL pointer is invalid */
396     /* DSOUND: Error: Invalid buffer description pointer */
397     rc=IDirectSound_CreateSoundBuffer(dso,0,&primary,NULL);
398     ok(rc==DSERR_INVALIDPARAM && primary==0,
399        "IDirectSound_CreateSoundBuffer() should have failed: rc=%08x,"
400        "dsbo=%p\n",rc,primary);
401
402     /* DSOUND: Error: Invalid size */
403     /* DSOUND: Error: Invalid buffer description */
404     primary=NULL;
405     ZeroMemory(&bufdesc, sizeof(bufdesc));
406     bufdesc.dwSize=sizeof(bufdesc)-1;
407     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
408     ok(rc==DSERR_INVALIDPARAM && primary==0,
409        "IDirectSound_CreateSoundBuffer() should have failed: rc=%08x,"
410        "primary=%p\n",rc,primary);
411
412     /* DSOUND: Error: DSBCAPS_PRIMARYBUFFER flag with non-NULL lpwfxFormat */
413     /* DSOUND: Error: Invalid buffer description pointer */
414     primary=NULL;
415     ZeroMemory(&bufdesc, sizeof(bufdesc));
416     bufdesc.dwSize=sizeof(bufdesc);
417     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
418     bufdesc.lpwfxFormat=&wfx;
419     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
420     ok(rc==DSERR_INVALIDPARAM && primary==0,
421        "IDirectSound_CreateSoundBuffer() should have failed: rc=%08x,"
422        "primary=%p\n",rc,primary);
423
424     /* DSOUND: Error: No DSBCAPS_PRIMARYBUFFER flag with NULL lpwfxFormat */
425     /* DSOUND: Error: Invalid buffer description pointer */
426     primary=NULL;
427     ZeroMemory(&bufdesc, sizeof(bufdesc));
428     bufdesc.dwSize=sizeof(bufdesc);
429     bufdesc.dwFlags=0;
430     bufdesc.lpwfxFormat=NULL;
431     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
432     ok(rc==DSERR_INVALIDPARAM && primary==0,
433        "IDirectSound_CreateSoundBuffer() should have failed: rc=%08x,"
434        "primary=%p\n",rc,primary);
435
436     /* We must call SetCooperativeLevel before calling CreateSoundBuffer */
437     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
438     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
439     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
440     if (rc!=DS_OK)
441         goto EXIT;
442
443     /* Testing the primary buffer */
444     primary=NULL;
445     ZeroMemory(&bufdesc, sizeof(bufdesc));
446     bufdesc.dwSize=sizeof(bufdesc);
447     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRLVOLUME;
448     bufdesc.lpwfxFormat = &wfx;
449     init_format(&wfx,WAVE_FORMAT_PCM,11025,8,2);
450     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
451     ok(rc==DSERR_INVALIDPARAM,"IDirectSound_CreateSoundBuffer() should have "
452        "returned DSERR_INVALIDPARAM, returned: %08x\n", rc);
453     if (rc==DS_OK && primary!=NULL)
454         IDirectSoundBuffer_Release(primary);
455
456     primary=NULL;
457     ZeroMemory(&bufdesc, sizeof(bufdesc));
458     bufdesc.dwSize=sizeof(bufdesc);
459     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRLVOLUME;
460     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
461     ok((rc==DS_OK && primary!=NULL) || (rc==DSERR_CONTROLUNAVAIL),
462        "IDirectSound_CreateSoundBuffer() failed to create a primary buffer: %08x\n",rc);
463     if (rc==DSERR_CONTROLUNAVAIL)
464         trace("  No Primary\n");
465     else if (rc==DS_OK && primary!=NULL) {
466         LONG vol;
467
468         /* Try to create a second primary buffer */
469         /* DSOUND: Error: The primary buffer already exists.
470          * Any changes made to the buffer description will be ignored. */
471         rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&second,NULL);
472         ok(rc==DS_OK && second==primary,
473            "IDirectSound_CreateSoundBuffer() should have returned original "
474            "primary buffer: %08x\n",rc);
475         ref=IDirectSoundBuffer_Release(second);
476         ok(ref==1,"IDirectSoundBuffer_Release() primary has %d references, "
477            "should have 1\n",ref);
478
479         /* Try to duplicate a primary buffer */
480         /* DSOUND: Error: Can't duplicate primary buffers */
481         rc=IDirectSound_DuplicateSoundBuffer(dso,primary,&third);
482         /* rc=0x88780032 */
483         ok(rc!=DS_OK,"IDirectSound_DuplicateSoundBuffer() primary buffer "
484            "should have failed %08x\n",rc);
485
486         rc=IDirectSoundBuffer_GetVolume(primary,&vol);
487         ok(rc==DS_OK,"IDirectSoundBuffer_GetVolume() failed: %08x\n", rc);
488
489         if (winetest_interactive) {
490             trace("Playing a 5 seconds reference tone at the current "
491                   "volume.\n");
492             if (rc==DS_OK)
493                 trace("(the current volume is %d according to DirectSound)\n",
494                       vol);
495             trace("All subsequent tones should be identical to this one.\n");
496             trace("Listen for stutter, changes in pitch, volume, etc.\n");
497         }
498         test_buffer(dso,&primary,1,FALSE,0,FALSE,0,winetest_interactive &&
499                     !(dscaps.dwFlags & DSCAPS_EMULDRIVER),5.0,0,0,0,0,FALSE,0);
500
501         ref=IDirectSoundBuffer_Release(primary);
502         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references\n",ref);
503
504         ref=IDirectSoundBuffer_AddRef(primary);
505         ok(ref==1,"IDirectSoundBuffer_AddRef() primary has %d references\n",ref);
506
507         ref=IDirectSoundBuffer_Release(primary);
508         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references\n",ref);
509
510         ref=IDirectSoundBuffer_Release(primary);
511         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
512            "should have 0\n",ref);
513     }
514
515     /* Set the CooperativeLevel back to normal */
516     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
517     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
518     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
519
520 EXIT:
521     ref=IDirectSound_Release(dso);
522     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
523     if (ref!=0)
524         return DSERR_GENERIC;
525
526     return rc;
527 }
528
529 /*
530  * Test the primary buffer at different formats while keeping the
531  * secondary buffer at a constant format.
532  */
533 static HRESULT test_primary_secondary(LPGUID lpGuid)
534 {
535     HRESULT rc;
536     LPDIRECTSOUND dso=NULL;
537     LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
538     DSBUFFERDESC bufdesc;
539     DSCAPS dscaps;
540     WAVEFORMATEX wfx, wfx2;
541     int f,ref,tag;
542
543     /* Create the DirectSound object */
544     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
545     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
546        "DirectSoundCreate() failed: %08x\n",rc);
547     if (rc!=DS_OK)
548         return rc;
549
550     /* Get the device capabilities */
551     ZeroMemory(&dscaps, sizeof(dscaps));
552     dscaps.dwSize=sizeof(dscaps);
553     rc=IDirectSound_GetCaps(dso,&dscaps);
554     ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %08x\n",rc);
555     if (rc!=DS_OK)
556         goto EXIT;
557
558     /* We must call SetCooperativeLevel before creating primary buffer */
559     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
560     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
561     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
562     if (rc!=DS_OK)
563         goto EXIT;
564
565     ZeroMemory(&bufdesc, sizeof(bufdesc));
566     bufdesc.dwSize=sizeof(bufdesc);
567     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
568     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
569     ok(rc==DS_OK && primary!=NULL,
570        "IDirectSound_CreateSoundBuffer() failed to create a primary buffer %08x\n",rc);
571
572     if (rc==DS_OK && primary!=NULL) {
573         for (f=0;f<NB_FORMATS;f++) {
574           for (tag=0;tag<NB_TAGS;tag++) {
575             /* if float, we only want to test 32-bit */
576             if ((format_tags[tag] == WAVE_FORMAT_IEEE_FLOAT) && (formats[f][1] != 32))
577                 continue;
578
579             /* We must call SetCooperativeLevel to be allowed to call
580              * SetFormat */
581             /* DSOUND: Setting DirectSound cooperative level to
582              * DSSCL_PRIORITY */
583             rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
584             ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
585             if (rc!=DS_OK)
586                 goto EXIT;
587
588             init_format(&wfx,format_tags[tag],formats[f][0],formats[f][1],
589                         formats[f][2]);
590             wfx2=wfx;
591             rc=IDirectSoundBuffer_SetFormat(primary,&wfx);
592
593             if (wfx.wBitsPerSample <= 16)
594                 ok(rc==DS_OK,"IDirectSoundBuffer_SetFormat(%s) failed: %08x\n",
595                    format_string(&wfx), rc);
596             else
597                 ok(rc==DS_OK || rc == E_INVALIDARG, "SetFormat (%s) failed: %08x\n",
598                    format_string(&wfx), rc);
599
600             /* There is no guarantee that SetFormat will actually change the
601              * format to what we asked for. It depends on what the soundcard
602              * supports. So we must re-query the format.
603              */
604             rc=IDirectSoundBuffer_GetFormat(primary,&wfx,sizeof(wfx),NULL);
605             ok(rc==DS_OK,"IDirectSoundBuffer_GetFormat() failed: %08x\n", rc);
606             if (rc==DS_OK &&
607                 (wfx.wFormatTag!=wfx2.wFormatTag ||
608                  wfx.nSamplesPerSec!=wfx2.nSamplesPerSec ||
609                  wfx.wBitsPerSample!=wfx2.wBitsPerSample ||
610                  wfx.nChannels!=wfx2.nChannels)) {
611                 trace("Requested primary format tag=0x%04x %dx%dx%d "
612                       "avg.B/s=%d align=%d\n",
613                       wfx2.wFormatTag,wfx2.nSamplesPerSec,wfx2.wBitsPerSample,
614                       wfx2.nChannels,wfx2.nAvgBytesPerSec,wfx2.nBlockAlign);
615                 trace("Got tag=0x%04x %dx%dx%d avg.B/s=%d align=%d\n",
616                       wfx.wFormatTag,wfx.nSamplesPerSec,wfx.wBitsPerSample,
617                       wfx.nChannels,wfx.nAvgBytesPerSec,wfx.nBlockAlign);
618             }
619
620             /* Set the CooperativeLevel back to normal */
621             /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
622             rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
623             ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
624
625             init_format(&wfx2,WAVE_FORMAT_PCM,11025,16,2);
626
627             secondary=NULL;
628             ZeroMemory(&bufdesc, sizeof(bufdesc));
629             bufdesc.dwSize=sizeof(bufdesc);
630             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
631             bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
632                                         wfx.nBlockAlign);
633             bufdesc.lpwfxFormat=&wfx2;
634             if (winetest_interactive) {
635                 trace("  Testing a primary buffer at %dx%dx%d (fmt=%d) with a "
636                       "secondary buffer at %dx%dx%d\n",
637                       wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,format_tags[tag],
638                       wfx2.nSamplesPerSec,wfx2.wBitsPerSample,wfx2.nChannels);
639             }
640             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
641             ok((rc==DS_OK && secondary!=NULL) || broken(rc == DSERR_CONTROLUNAVAIL), /* vmware drivers on w2k */
642                "IDirectSound_CreateSoundBuffer() failed to create a secondary buffer %08x\n",rc);
643
644             if (rc==DS_OK && secondary!=NULL) {
645                 test_buffer(dso,&secondary,0,FALSE,0,FALSE,0,
646                             winetest_interactive,1.0,0,NULL,0,0,FALSE,0);
647
648                 ref=IDirectSoundBuffer_Release(secondary);
649                 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
650                    "should have 0\n",ref);
651             }
652           }
653         }
654
655         ref=IDirectSoundBuffer_Release(primary);
656         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
657            "should have 0\n",ref);
658     }
659
660     /* Set the CooperativeLevel back to normal */
661     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
662     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
663     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
664
665 EXIT:
666     ref=IDirectSound_Release(dso);
667     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
668     if (ref!=0)
669         return DSERR_GENERIC;
670
671     return rc;
672 }
673
674 static HRESULT test_secondary(LPGUID lpGuid)
675 {
676     HRESULT rc;
677     LPDIRECTSOUND dso=NULL;
678     LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
679     DSBUFFERDESC bufdesc;
680     DSCAPS dscaps;
681     WAVEFORMATEX wfx, wfx1;
682     DWORD f, tag;
683     int ref;
684
685     /* Create the DirectSound object */
686     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
687     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
688        "DirectSoundCreate() failed: %08x\n",rc);
689     if (rc!=DS_OK)
690         return rc;
691
692     /* Get the device capabilities */
693     ZeroMemory(&dscaps, sizeof(dscaps));
694     dscaps.dwSize=sizeof(dscaps);
695     rc=IDirectSound_GetCaps(dso,&dscaps);
696     ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %08x\n",rc);
697     if (rc!=DS_OK)
698         goto EXIT;
699
700     /* We must call SetCooperativeLevel before creating primary buffer */
701     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
702     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
703     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
704     if (rc!=DS_OK)
705         goto EXIT;
706
707     ZeroMemory(&bufdesc, sizeof(bufdesc));
708     bufdesc.dwSize=sizeof(bufdesc);
709     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
710     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
711     ok(rc==DS_OK && primary!=NULL,
712        "IDirectSound_CreateSoundBuffer() failed to create a primary buffer %08x\n",rc);
713
714     if (rc==DS_OK && primary!=NULL) {
715         rc=IDirectSoundBuffer_GetFormat(primary,&wfx1,sizeof(wfx1),NULL);
716         ok(rc==DS_OK,"IDirectSoundBuffer8_Getformat() failed: %08x\n", rc);
717         if (rc!=DS_OK)
718             goto EXIT1;
719
720         for (f=0;f<NB_FORMATS;f++) {
721           for (tag=0;tag<NB_TAGS;tag++) {
722             WAVEFORMATEXTENSIBLE wfxe;
723
724             /* if float, we only want to test 32-bit */
725             if ((format_tags[tag] == WAVE_FORMAT_IEEE_FLOAT) && (formats[f][1] != 32))
726                 continue;
727
728             init_format(&wfx,format_tags[tag],formats[f][0],formats[f][1],
729                         formats[f][2]);
730             secondary=NULL;
731             ZeroMemory(&bufdesc, sizeof(bufdesc));
732             bufdesc.dwSize=sizeof(bufdesc);
733             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
734             bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
735                                         wfx.nBlockAlign);
736             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
737             ok(rc==DSERR_INVALIDPARAM,"IDirectSound_CreateSoundBuffer() "
738                "should have returned DSERR_INVALIDPARAM, returned: %08x\n", rc);
739             if (rc==DS_OK && secondary!=NULL)
740                 IDirectSoundBuffer_Release(secondary);
741
742             secondary=NULL;
743             ZeroMemory(&bufdesc, sizeof(bufdesc));
744             bufdesc.dwSize=sizeof(bufdesc);
745             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
746             bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
747                                         wfx.nBlockAlign);
748             bufdesc.lpwfxFormat=&wfx;
749             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
750             if (gotdx8 || wfx.wBitsPerSample <= 16 || wfx.wFormatTag == WAVE_FORMAT_IEEE_FLOAT)
751             {
752                 if (wfx.wBitsPerSample > 16)
753                     ok(broken((rc == DSERR_CONTROLUNAVAIL || rc == DSERR_INVALIDCALL || rc == DSERR_INVALIDPARAM /* 2003 */) && !secondary)
754                         || rc == DS_OK, /* driver dependent? */
755                         "IDirectSound_CreateSoundBuffer() "
756                         "should have returned (DSERR_CONTROLUNAVAIL or DSERR_INVALIDCALL) "
757                         "and NULL, returned: %08x %p\n", rc, secondary);
758                 else
759                     ok((rc==DS_OK && secondary!=NULL) || broken(rc == DSERR_CONTROLUNAVAIL), /* vmware drivers on w2k */
760                         "IDirectSound_CreateSoundBuffer() failed to create a secondary buffer %08x\n",rc);
761             }
762             else
763                 ok(rc==E_INVALIDARG, "Creating %d bpp buffer on dx < 8 returned: %p %08x\n",
764                    wfx.wBitsPerSample, secondary, rc);
765
766             if (!gotdx8)
767             {
768                 win_skip("Not doing the WAVE_FORMAT_EXTENSIBLE tests\n");
769                 /* Apparently they succeed with bogus values,
770                  * which means that older dsound doesn't look at them
771                  */
772                 goto no_wfe;
773             }
774
775             if (secondary)
776                 IDirectSoundBuffer_Release(secondary);
777             secondary = NULL;
778
779             bufdesc.lpwfxFormat=(WAVEFORMATEX*)&wfxe;
780             wfxe.Format = wfx;
781             wfxe.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
782             wfxe.SubFormat = (format_tags[tag] == WAVE_FORMAT_PCM ? KSDATAFORMAT_SUBTYPE_PCM : KSDATAFORMAT_SUBTYPE_IEEE_FLOAT);
783             wfxe.Format.cbSize = 1;
784             wfxe.Samples.wValidBitsPerSample = wfx.wBitsPerSample;
785             wfxe.dwChannelMask = (wfx.nChannels == 1 ? KSAUDIO_SPEAKER_MONO : KSAUDIO_SPEAKER_STEREO);
786
787             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
788             ok((rc==DSERR_INVALIDPARAM || rc==DSERR_INVALIDCALL /* 2003 */) && !secondary,
789                 "IDirectSound_CreateSoundBuffer() returned: %08x %p\n",
790                 rc, secondary);
791             if (secondary)
792             {
793                 IDirectSoundBuffer_Release(secondary);
794                 secondary=NULL;
795             }
796
797             wfxe.Format.cbSize = sizeof(wfxe) - sizeof(wfx) + 1;
798
799             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
800             ok(((rc==DSERR_CONTROLUNAVAIL || rc==DSERR_INVALIDCALL || rc==DSERR_INVALIDPARAM)
801                 && !secondary)
802                || rc==DS_OK, /* 2003 / 2008 */
803                 "IDirectSound_CreateSoundBuffer() returned: %08x %p\n",
804                 rc, secondary);
805             if (secondary)
806             {
807                 IDirectSoundBuffer_Release(secondary);
808                 secondary=NULL;
809             }
810
811             wfxe.Format.cbSize = sizeof(wfxe) - sizeof(wfx);
812             wfxe.SubFormat = GUID_NULL;
813             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
814             ok((rc==DSERR_INVALIDPARAM || rc==DSERR_INVALIDCALL) && !secondary,
815                 "IDirectSound_CreateSoundBuffer() returned: %08x %p\n",
816                 rc, secondary);
817             if (secondary)
818             {
819                 IDirectSoundBuffer_Release(secondary);
820                 secondary=NULL;
821             }
822             wfxe.SubFormat = (format_tags[tag] == WAVE_FORMAT_PCM ? KSDATAFORMAT_SUBTYPE_PCM : KSDATAFORMAT_SUBTYPE_IEEE_FLOAT);
823
824             ++wfxe.Samples.wValidBitsPerSample;
825             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
826             ok(rc==DSERR_INVALIDPARAM && !secondary,
827                 "IDirectSound_CreateSoundBuffer() returned: %08x %p\n",
828                 rc, secondary);
829             if (secondary)
830             {
831                 IDirectSoundBuffer_Release(secondary);
832                 secondary=NULL;
833             }
834             --wfxe.Samples.wValidBitsPerSample;
835
836             wfxe.Samples.wValidBitsPerSample = 0;
837             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
838             ok(rc==DS_OK && secondary,
839                 "IDirectSound_CreateSoundBuffer() returned: %08x %p\n",
840                 rc, secondary);
841             if (secondary)
842             {
843                 IDirectSoundBuffer_Release(secondary);
844                 secondary=NULL;
845             }
846             wfxe.Samples.wValidBitsPerSample = wfxe.Format.wBitsPerSample;
847
848             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
849             ok(rc==DS_OK && secondary!=NULL,
850                 "IDirectSound_CreateSoundBuffer() failed to create a secondary buffer %08x\n",rc);
851
852 no_wfe:
853             if (rc==DS_OK && secondary!=NULL) {
854                 if (winetest_interactive) {
855                     trace("  Testing a secondary buffer at %dx%dx%d (fmt=%d) "
856                         "with a primary buffer at %dx%dx%d\n",
857                         wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,format_tags[tag],
858                         wfx1.nSamplesPerSec,wfx1.wBitsPerSample,wfx1.nChannels);
859                 }
860                 test_buffer(dso,&secondary,0,FALSE,0,FALSE,0,
861                             winetest_interactive,1.0,0,NULL,0,0,FALSE,0);
862
863                 ref=IDirectSoundBuffer_Release(secondary);
864                 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
865                    "should have 0\n",ref);
866             }
867           }
868         }
869 EXIT1:
870         ref=IDirectSoundBuffer_Release(primary);
871         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
872            "should have 0\n",ref);
873     }
874
875     /* Set the CooperativeLevel back to normal */
876     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
877     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
878     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
879
880 EXIT:
881     ref=IDirectSound_Release(dso);
882     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
883     if (ref!=0)
884         return DSERR_GENERIC;
885
886     return rc;
887 }
888
889 static HRESULT test_block_align(LPGUID lpGuid)
890 {
891     HRESULT rc;
892     LPDIRECTSOUND dso=NULL;
893     LPDIRECTSOUNDBUFFER secondary=NULL;
894     DSBUFFERDESC bufdesc;
895     DSBCAPS dsbcaps;
896     WAVEFORMATEX wfx;
897     DWORD pos, pos2;
898     int ref;
899
900     /* Create the DirectSound object */
901     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
902     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
903        "DirectSoundCreate() failed: %08x\n",rc);
904     if (rc!=DS_OK)
905         return rc;
906
907     init_format(&wfx,WAVE_FORMAT_PCM,11025,16,2);
908     ZeroMemory(&bufdesc, sizeof(bufdesc));
909     bufdesc.dwSize=sizeof(bufdesc);
910     bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
911     bufdesc.dwBufferBytes=wfx.nAvgBytesPerSec + 1;
912     bufdesc.lpwfxFormat=&wfx;
913     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
914     ok(rc == DS_OK || broken(rc == DSERR_CONTROLUNAVAIL), /* vmware drivers on w2k */
915        "IDirectSound_CreateSoundBuffer() should have returned DS_OK, returned: %08x\n", rc);
916
917     if (rc==DS_OK && secondary!=NULL) {
918         ZeroMemory(&dsbcaps, sizeof(dsbcaps));
919         dsbcaps.dwSize = sizeof(dsbcaps);
920         rc=IDirectSoundBuffer_GetCaps(secondary,&dsbcaps);
921         ok(rc==DS_OK,"IDirectSoundBuffer_GetCaps() should have returned DS_OK, "
922            "returned: %08x\n", rc);
923         if (rc==DS_OK && wfx.nBlockAlign > 1)
924         {
925             ok(dsbcaps.dwBufferBytes==(wfx.nAvgBytesPerSec + wfx.nBlockAlign),
926                "Buffer size not a multiple of nBlockAlign: requested %d, "
927                "got %d, should be %d\n", bufdesc.dwBufferBytes,
928                dsbcaps.dwBufferBytes, wfx.nAvgBytesPerSec + wfx.nBlockAlign);
929
930             rc = IDirectSoundBuffer_SetCurrentPosition(secondary, 0);
931             ok(rc == DS_OK, "Could not set position to 0: %08x\n", rc);
932             rc = IDirectSoundBuffer_GetCurrentPosition(secondary, &pos, NULL);
933             ok(rc == DS_OK, "Could not get position: %08x\n", rc);
934             rc = IDirectSoundBuffer_SetCurrentPosition(secondary, 1);
935             ok(rc == DS_OK, "Could not set position to 1: %08x\n", rc);
936             rc = IDirectSoundBuffer_GetCurrentPosition(secondary, &pos2, NULL);
937             ok(rc == DS_OK, "Could not get new position: %08x\n", rc);
938             ok(pos == pos2, "Positions not the same! Old position: %d, new position: %d\n", pos, pos2);
939         }
940         ref=IDirectSoundBuffer_Release(secondary);
941         ok(ref==0,"IDirectSoundBuffer_Release() secondary has %d references, "
942            "should have 0\n",ref);
943     }
944
945     ref=IDirectSound_Release(dso);
946     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
947     if (ref!=0)
948         return DSERR_GENERIC;
949
950     return rc;
951 }
952
953 static struct fmt {
954     int bits;
955     int channels;
956 } fmts[] = { { 8, 1 }, { 8, 2 }, { 16, 1 }, {16, 2 } };
957
958 static HRESULT test_frequency(LPGUID lpGuid)
959 {
960     HRESULT rc;
961     LPDIRECTSOUND dso=NULL;
962     LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
963     DSBUFFERDESC bufdesc;
964     DSCAPS dscaps;
965     WAVEFORMATEX wfx, wfx1;
966     DWORD f, r;
967     int ref;
968     int rates[] = { 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100,
969                     48000, 96000 };
970
971     /* Create the DirectSound object */
972     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
973     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
974        "DirectSoundCreate() failed: %08x\n",rc);
975     if (rc!=DS_OK)
976         return rc;
977
978     /* Get the device capabilities */
979     ZeroMemory(&dscaps, sizeof(dscaps));
980     dscaps.dwSize=sizeof(dscaps);
981     rc=IDirectSound_GetCaps(dso,&dscaps);
982     ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %08x\n",rc);
983     if (rc!=DS_OK)
984         goto EXIT;
985
986     /* We must call SetCooperativeLevel before creating primary buffer */
987     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
988     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
989     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
990     if (rc!=DS_OK)
991         goto EXIT;
992
993     ZeroMemory(&bufdesc, sizeof(bufdesc));
994     bufdesc.dwSize=sizeof(bufdesc);
995     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
996     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
997     ok(rc==DS_OK && primary!=NULL,
998        "IDirectSound_CreateSoundBuffer() failed to create a primary buffer %08x\n",rc);
999
1000     if (rc==DS_OK && primary!=NULL) {
1001         rc=IDirectSoundBuffer_GetFormat(primary,&wfx1,sizeof(wfx1),NULL);
1002         ok(rc==DS_OK,"IDirectSoundBuffer8_Getformat() failed: %08x\n", rc);
1003         if (rc!=DS_OK)
1004             goto EXIT1;
1005
1006         for (f=0;f<sizeof(fmts)/sizeof(fmts[0]);f++) {
1007         for (r=0;r<sizeof(rates)/sizeof(rates[0]);r++) {
1008             init_format(&wfx,WAVE_FORMAT_PCM,11025,fmts[f].bits,
1009                         fmts[f].channels);
1010             secondary=NULL;
1011             ZeroMemory(&bufdesc, sizeof(bufdesc));
1012             bufdesc.dwSize=sizeof(bufdesc);
1013             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2|DSBCAPS_CTRLFREQUENCY;
1014             bufdesc.dwBufferBytes=align((wfx.nAvgBytesPerSec*rates[r]/11025)*
1015                                         BUFFER_LEN/1000,wfx.nBlockAlign);
1016             bufdesc.lpwfxFormat=&wfx;
1017             if (winetest_interactive) {
1018                 trace("  Testing a secondary buffer at %dx%dx%d "
1019                       "with a primary buffer at %dx%dx%d\n",
1020                       wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,
1021                       wfx1.nSamplesPerSec,wfx1.wBitsPerSample,wfx1.nChannels);
1022             }
1023             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
1024             ok((rc==DS_OK && secondary!=NULL) || broken(rc == DSERR_CONTROLUNAVAIL), /* vmware drivers on w2k */
1025                "IDirectSound_CreateSoundBuffer() failed to create a secondary buffer %08x\n",rc);
1026
1027             if (rc==DS_OK && secondary!=NULL) {
1028                 test_buffer(dso,&secondary,0,FALSE,0,FALSE,0,
1029                             winetest_interactive,1.0,0,NULL,0,0,TRUE,rates[r]);
1030
1031                 ref=IDirectSoundBuffer_Release(secondary);
1032                 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
1033                    "should have 0\n",ref);
1034             }
1035         }
1036         }
1037 EXIT1:
1038         ref=IDirectSoundBuffer_Release(primary);
1039         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
1040            "should have 0\n",ref);
1041     }
1042
1043     /* Set the CooperativeLevel back to normal */
1044     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
1045     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
1046     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
1047
1048 EXIT:
1049     ref=IDirectSound_Release(dso);
1050     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
1051     if (ref!=0)
1052         return DSERR_GENERIC;
1053
1054     return rc;
1055 }
1056
1057 static HRESULT test_notify(LPDIRECTSOUNDBUFFER dsb,
1058                            DWORD count, LPHANDLE event,
1059                            DWORD expected)
1060 {
1061     HRESULT rc;
1062     DWORD ret;
1063
1064     rc=IDirectSoundBuffer_SetCurrentPosition(dsb,0);
1065     ok(rc==DS_OK,
1066        "IDirectSoundBuffer_SetCurrentPosition failed %08x\n",rc);
1067     if(rc!=DS_OK)
1068         return rc;
1069
1070     rc=IDirectSoundBuffer_Play(dsb,0,0,0);
1071     ok(rc==DS_OK,"IDirectSoundBuffer_Play failed %08x\n",rc);
1072     if(rc!=DS_OK)
1073         return rc;
1074
1075     rc=IDirectSoundBuffer_Stop(dsb);
1076     ok(rc==DS_OK,"IDirectSoundBuffer_Stop failed %08x\n",rc);
1077     if(rc!=DS_OK)
1078         return rc;
1079
1080     ret=WaitForMultipleObjects(count,event,FALSE,0);
1081     ok(ret==expected,"expected %d. got %d\n",expected,ret);
1082     return rc;
1083 }
1084
1085 static HRESULT test_duplicate(LPGUID lpGuid)
1086 {
1087     HRESULT rc;
1088     LPDIRECTSOUND dso=NULL;
1089     LPDIRECTSOUNDBUFFER primary=NULL;
1090     DSBUFFERDESC bufdesc;
1091     int ref;
1092
1093     /* Create the DirectSound object */
1094     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
1095     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
1096        "DirectSoundCreate() failed: %08x\n",rc);
1097     if (rc!=DS_OK)
1098         return rc;
1099
1100     /* We must call SetCooperativeLevel before creating primary buffer */
1101     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
1102     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
1103     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
1104     if (rc!=DS_OK)
1105         goto EXIT;
1106
1107     ZeroMemory(&bufdesc, sizeof(bufdesc));
1108     bufdesc.dwSize=sizeof(bufdesc);
1109     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
1110     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
1111     ok(rc==DS_OK && primary!=NULL,"IDirectSound_CreateSoundBuffer() failed "
1112        "to create a primary buffer %08x\n",rc);
1113
1114     if (rc==DS_OK && primary!=NULL) {
1115         LPDIRECTSOUNDBUFFER original=NULL;
1116         WAVEFORMATEX wfx;
1117
1118         init_format(&wfx,WAVE_FORMAT_PCM,22050,16,1);
1119         ZeroMemory(&bufdesc, sizeof(bufdesc));
1120         bufdesc.dwSize=sizeof(bufdesc);
1121         bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2|DSBCAPS_CTRLPOSITIONNOTIFY;
1122         bufdesc.dwBufferBytes=wfx.nAvgBytesPerSec/100; /* very short buffer */
1123         bufdesc.lpwfxFormat=&wfx;
1124         rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&original,NULL);
1125         ok(rc==DS_OK && original!=NULL,
1126            "IDirectSound_CreateSoundBuffer() failed to create a original "
1127            "buffer %08x\n",rc);
1128         if (rc==DS_OK && original!=NULL) {
1129             LPDIRECTSOUNDBUFFER duplicated=NULL;
1130             LPDIRECTSOUNDNOTIFY notify=NULL;
1131             HANDLE event[2];
1132             LPVOID buf=NULL;
1133             DWORD bufsize;
1134             int i;
1135
1136             /* Prepare notify events */
1137             for (i=0;i<sizeof(event)/sizeof(event[0]);i++) {
1138                 event[i] = CreateEvent(NULL,FALSE,FALSE,NULL);
1139             }
1140
1141             /* Make silent buffer */
1142             rc=IDirectSoundBuffer_Lock(original,0,0,&buf,&bufsize,
1143                                        NULL,NULL,DSBLOCK_ENTIREBUFFER);
1144             ok(rc==DS_OK && buf!=NULL,
1145                "IDirectSoundBuffer_Lock failed to lock the buffer %08x\n",rc);
1146             if (rc==DS_OK && buf!=NULL) {
1147                 ZeroMemory(buf,bufsize);
1148                 rc=IDirectSoundBuffer_Unlock(original,buf,bufsize,
1149                                              NULL,0);
1150                 ok(rc==DS_OK,"IDirectSoundBuffer_Unlock failed to unlock "
1151                    "%08x\n",rc);
1152             }
1153
1154             rc=IDirectSoundBuffer_QueryInterface(original,
1155                                                  &IID_IDirectSoundNotify,
1156                                                  (void**)&notify);
1157             ok(rc==DS_OK && notify!=NULL,
1158                "IDirectSoundBuffer_QueryInterface() failed to create a "
1159                "notification %08x\n",rc);
1160             if (rc==DS_OK && notify!=NULL) {
1161                 DSBPOSITIONNOTIFY dsbpn;
1162                 LPDIRECTSOUNDNOTIFY dup_notify=NULL;
1163
1164                 dsbpn.dwOffset=DSBPN_OFFSETSTOP;
1165                 dsbpn.hEventNotify=event[0];
1166                 rc=IDirectSoundNotify_SetNotificationPositions(notify,
1167                                                                1,&dsbpn);
1168                 ok(rc==DS_OK,"IDirectSoundNotify_SetNotificationPositions "
1169                    "failed %08x\n",rc);
1170
1171                 rc=IDirectSound_DuplicateSoundBuffer(dso,original,&duplicated);
1172                 ok(rc==DS_OK && duplicated!=NULL,
1173                    "IDirectSound_DuplicateSoundBuffer failed %08x\n",rc);
1174
1175                 trace("testing duplicated buffer without notifications.\n");
1176                 test_notify(duplicated,sizeof(event)/sizeof(event[0]),
1177                             event,WAIT_TIMEOUT);
1178
1179                 rc=IDirectSoundBuffer_QueryInterface(duplicated,
1180                                                      &IID_IDirectSoundNotify,
1181                                                      (void**)&dup_notify);
1182                 ok(rc==DS_OK&&dup_notify!=NULL,
1183                    "IDirectSoundBuffer_QueryInterface() failed to create a "
1184                    "notification %08x\n",rc);
1185                 if(rc==DS_OK&&dup_notify!=NULL) {
1186                     dsbpn.dwOffset=DSBPN_OFFSETSTOP;
1187                     dsbpn.hEventNotify=event[1];
1188                     rc=IDirectSoundNotify_SetNotificationPositions(dup_notify,
1189                                                                    1,&dsbpn);
1190                     ok(rc==DS_OK,"IDirectSoundNotify_SetNotificationPositions "
1191                        "failed %08x\n",rc);
1192
1193                     trace("testing duplicated buffer with a notification.\n");
1194                     test_notify(duplicated,sizeof(event)/sizeof(event[0]),
1195                                 event,WAIT_OBJECT_0+1);
1196
1197                     ref=IDirectSoundNotify_Release(dup_notify);
1198                     ok(ref==0,"IDirectSoundNotify_Release() has %d references, "
1199                        "should have 0\n",ref);
1200                 }
1201                 ref=IDirectSoundNotify_Release(notify);
1202                 ok(ref==0,"IDirectSoundNotify_Release() has %d references, "
1203                    "should have 0\n",ref);
1204
1205                 trace("testing original buffer with a notification.\n");
1206                 test_notify(original,sizeof(event)/sizeof(event[0]),
1207                             event,WAIT_OBJECT_0);
1208
1209                 ref=IDirectSoundBuffer_Release(duplicated);
1210                 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
1211                    "should have 0\n",ref);
1212             }
1213             ref=IDirectSoundBuffer_Release(original);
1214             ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
1215                "should have 0\n",ref);
1216         }
1217         ref=IDirectSoundBuffer_Release(primary);
1218         ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
1219            "should have 0\n",ref);
1220     }
1221
1222     /* Set the CooperativeLevel back to normal */
1223     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
1224     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
1225     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
1226
1227 EXIT:
1228     ref=IDirectSound_Release(dso);
1229     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
1230     if (ref!=0)
1231         return DSERR_GENERIC;
1232
1233     return rc;
1234 }
1235
1236 static HRESULT test_invalid_fmts(LPGUID lpGuid)
1237 {
1238     HRESULT rc;
1239     LPDIRECTSOUND dso=NULL;
1240     LPDIRECTSOUNDBUFFER primary=NULL;
1241     DSBUFFERDESC bufdesc;
1242
1243     /* Create the DirectSound object */
1244     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
1245     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
1246        "DirectSoundCreate() failed: %08x\n",rc);
1247     if (rc!=DS_OK)
1248         return rc;
1249
1250     /* We must call SetCooperativeLevel before creating primary buffer */
1251     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
1252     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
1253     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
1254     if (rc!=DS_OK){
1255         IDirectSound_Release(dso);
1256         return rc;
1257     }
1258
1259     ZeroMemory(&bufdesc, sizeof(bufdesc));
1260     bufdesc.dwSize=sizeof(bufdesc);
1261     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
1262     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
1263     ok(rc==DS_OK && primary!=NULL,"IDirectSound_CreateSoundBuffer() failed "
1264        "to create a primary buffer %08x\n",rc);
1265
1266     if (rc==DS_OK && primary!=NULL) {
1267         WAVEFORMATEX wfx;
1268         WAVEFORMATEXTENSIBLE fmtex;
1269
1270         wfx.wFormatTag = WAVE_FORMAT_PCM;
1271         wfx.nChannels = 0;
1272         wfx.nSamplesPerSec = 44100;
1273         wfx.wBitsPerSample = 16;
1274         wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8;
1275         wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
1276         rc = IDirectSoundBuffer_SetFormat(primary, &wfx);
1277         ok(rc == E_INVALIDARG, "SetFormat: %08x\n", rc);
1278
1279         wfx.nChannels = 2;
1280         wfx.nSamplesPerSec = 44100;
1281         wfx.wBitsPerSample = 0;
1282         wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8;
1283         wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
1284         rc = IDirectSoundBuffer_SetFormat(primary, &wfx);
1285         ok(rc == E_INVALIDARG, "SetFormat: %08x\n", rc);
1286
1287         wfx.nChannels = 2;
1288         wfx.nSamplesPerSec = 44100;
1289         wfx.wBitsPerSample = 2;
1290         wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8;
1291         wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
1292         rc = IDirectSoundBuffer_SetFormat(primary, &wfx);
1293         ok(rc == E_INVALIDARG, "SetFormat: %08x\n", rc);
1294
1295         wfx.nChannels = 2;
1296         wfx.nSamplesPerSec = 44100;
1297         wfx.wBitsPerSample = 12;
1298         wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8;
1299         wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
1300         rc = IDirectSoundBuffer_SetFormat(primary, &wfx);
1301         ok(rc == E_INVALIDARG, "SetFormat: %08x\n", rc);
1302
1303         wfx.nChannels = 2;
1304         wfx.nSamplesPerSec = 0;
1305         wfx.wBitsPerSample = 16;
1306         wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8;
1307         wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
1308         rc = IDirectSoundBuffer_SetFormat(primary, &wfx);
1309         ok(rc == E_INVALIDARG, "SetFormat: %08x\n", rc);
1310
1311         wfx.nChannels = 2;
1312         wfx.nSamplesPerSec = 44100;
1313         wfx.wBitsPerSample = 16;
1314         wfx.nBlockAlign = 0;
1315         wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
1316         rc = IDirectSoundBuffer_SetFormat(primary, &wfx);
1317         ok(rc == E_INVALIDARG, "SetFormat: %08x\n", rc);
1318
1319         wfx.nChannels = 2;
1320         wfx.nSamplesPerSec = 44100;
1321         wfx.wBitsPerSample = 16;
1322         wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8;
1323         wfx.nAvgBytesPerSec = 0;
1324         rc = IDirectSoundBuffer_SetFormat(primary, &wfx);
1325         ok(rc == E_INVALIDARG, "SetFormat: %08x\n", rc);
1326
1327         wfx.nChannels = 2;
1328         wfx.nSamplesPerSec = 44100;
1329         wfx.wBitsPerSample = 16;
1330         wfx.nBlockAlign = (wfx.nChannels * wfx.wBitsPerSample / 8) - 1;
1331         wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
1332         rc = IDirectSoundBuffer_SetFormat(primary, &wfx);
1333         ok(rc == E_INVALIDARG, "SetFormat: %08x\n", rc);
1334
1335         wfx.nChannels = 2;
1336         wfx.nSamplesPerSec = 44100;
1337         wfx.wBitsPerSample = 16;
1338         wfx.nBlockAlign = (wfx.nChannels * wfx.wBitsPerSample / 8) + 1;
1339         wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
1340         rc = IDirectSoundBuffer_SetFormat(primary, &wfx);
1341         ok(rc == E_INVALIDARG, "SetFormat: %08x\n", rc);
1342
1343         wfx.nChannels = 2;
1344         wfx.nSamplesPerSec = 44100;
1345         wfx.wBitsPerSample = 16;
1346         wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8;
1347         wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign + 1;
1348         rc = IDirectSoundBuffer_SetFormat(primary, &wfx);
1349         ok(rc == S_OK, "SetFormat: %08x\n", rc);
1350
1351         wfx.nChannels = 2;
1352         wfx.nSamplesPerSec = 44100;
1353         wfx.wBitsPerSample = 16;
1354         wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8;
1355         wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign - 1;
1356         rc = IDirectSoundBuffer_SetFormat(primary, &wfx);
1357         ok(rc == S_OK, "SetFormat: %08x\n", rc);
1358
1359         wfx.nChannels = 2;
1360         wfx.nSamplesPerSec = 44100;
1361         wfx.wBitsPerSample = 16;
1362         wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8;
1363         wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign + 1;
1364         rc = IDirectSoundBuffer_SetFormat(primary, &wfx);
1365         ok(rc == S_OK, "SetFormat: %08x\n", rc);
1366
1367         fmtex.Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
1368         fmtex.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
1369         fmtex.Format.nChannels = 2;
1370         fmtex.Format.nSamplesPerSec = 44100;
1371         fmtex.Format.wBitsPerSample = 16;
1372         fmtex.Format.nBlockAlign = fmtex.Format.nChannels * fmtex.Format.wBitsPerSample / 8;
1373         fmtex.Format.nAvgBytesPerSec = fmtex.Format.nSamplesPerSec * fmtex.Format.nBlockAlign;
1374         fmtex.Samples.wValidBitsPerSample = 0;
1375         fmtex.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
1376         fmtex.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
1377         rc = IDirectSoundBuffer_SetFormat(primary, (WAVEFORMATEX*)&fmtex);
1378         ok(rc == S_OK, "SetFormat: %08x\n", rc);
1379
1380         fmtex.Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
1381         fmtex.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
1382         fmtex.Format.nChannels = 2;
1383         fmtex.Format.nSamplesPerSec = 44100;
1384         fmtex.Format.wBitsPerSample = 24;
1385         fmtex.Format.nBlockAlign = fmtex.Format.nChannels * fmtex.Format.wBitsPerSample / 8;
1386         fmtex.Format.nAvgBytesPerSec = fmtex.Format.nSamplesPerSec * fmtex.Format.nBlockAlign;
1387         fmtex.Samples.wValidBitsPerSample = 20;
1388         fmtex.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
1389         fmtex.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
1390         rc = IDirectSoundBuffer_SetFormat(primary, (WAVEFORMATEX*)&fmtex);
1391         ok(rc == S_OK, "SetFormat: %08x\n", rc);
1392
1393         fmtex.Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
1394         fmtex.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
1395         fmtex.Format.nChannels = 2;
1396         fmtex.Format.nSamplesPerSec = 44100;
1397         fmtex.Format.wBitsPerSample = 24;
1398         fmtex.Format.nBlockAlign = fmtex.Format.nChannels * fmtex.Format.wBitsPerSample / 8;
1399         fmtex.Format.nAvgBytesPerSec = fmtex.Format.nSamplesPerSec * fmtex.Format.nBlockAlign;
1400         fmtex.Samples.wValidBitsPerSample = 32;
1401         fmtex.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
1402         fmtex.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
1403         rc = IDirectSoundBuffer_SetFormat(primary, (WAVEFORMATEX*)&fmtex);
1404         ok(rc == E_INVALIDARG, "SetFormat: %08x\n", rc);
1405
1406         IDirectSoundBuffer_Release(primary);
1407     }
1408
1409     IDirectSound_Release(dso);
1410
1411     return S_OK;
1412 }
1413
1414 static unsigned int number;
1415
1416 static BOOL WINAPI dsenum_callback(LPGUID lpGuid, LPCSTR lpcstrDescription,
1417                                    LPCSTR lpcstrModule, LPVOID lpContext)
1418 {
1419     HRESULT rc;
1420     trace("*** Testing %s - %s ***\n",lpcstrDescription,lpcstrModule);
1421
1422     /* Don't test the primary device */
1423     if (!number++)
1424     {
1425         ok (!lpcstrModule[0], "lpcstrModule(%s) != NULL\n", lpcstrModule);
1426         return 1;
1427     }
1428
1429     rc = test_dsound(lpGuid);
1430     if (rc == DSERR_NODRIVER)
1431         trace("  No Driver\n");
1432     else if (rc == DSERR_ALLOCATED)
1433         trace("  Already In Use\n");
1434     else if (rc == E_FAIL)
1435         trace("  No Device\n");
1436     else {
1437         test_block_align(lpGuid);
1438         test_primary(lpGuid);
1439         test_primary_secondary(lpGuid);
1440         test_secondary(lpGuid);
1441         test_frequency(lpGuid);
1442         test_duplicate(lpGuid);
1443         test_invalid_fmts(lpGuid);
1444     }
1445
1446     return 1;
1447 }
1448
1449 static void dsound_tests(void)
1450 {
1451     HRESULT rc;
1452     rc=pDirectSoundEnumerateA(&dsenum_callback,NULL);
1453     ok(rc==DS_OK,"DirectSoundEnumerateA() failed: %08x\n",rc);
1454 }
1455
1456 static void test_hw_buffers(void)
1457 {
1458     IDirectSound *ds;
1459     IDirectSoundBuffer *primary, *primary2, **secondaries, *secondary;
1460     IDirectSoundBuffer8 *buf8;
1461     DSCAPS caps;
1462     DSBCAPS bufcaps;
1463     DSBUFFERDESC bufdesc;
1464     WAVEFORMATEX fmt;
1465     UINT i;
1466     HRESULT hr;
1467
1468     hr = pDirectSoundCreate(NULL, &ds, NULL);
1469     ok(hr == S_OK || hr == DSERR_NODRIVER || hr == DSERR_ALLOCATED || hr == E_FAIL,
1470             "DirectSoundCreate failed: %08x\n", hr);
1471     if(hr != S_OK)
1472         return;
1473
1474     caps.dwSize = sizeof(caps);
1475
1476     hr = IDirectSound_GetCaps(ds, &caps);
1477     ok(hr == S_OK, "GetCaps failed: %08x\n", hr);
1478
1479     ok(caps.dwPrimaryBuffers == 1, "Got wrong number of primary buffers: %u\n",
1480             caps.dwPrimaryBuffers);
1481
1482     /* DSBCAPS_LOC* is ignored for primary buffers */
1483     bufdesc.dwSize = sizeof(bufdesc);
1484     bufdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_LOCHARDWARE |
1485         DSBCAPS_PRIMARYBUFFER;
1486     bufdesc.dwBufferBytes = 0;
1487     bufdesc.dwReserved = 0;
1488     bufdesc.lpwfxFormat = NULL;
1489     bufdesc.guid3DAlgorithm = GUID_NULL;
1490
1491     hr = IDirectSound_CreateSoundBuffer(ds, &bufdesc, &primary, NULL);
1492     ok(hr == S_OK, "CreateSoundBuffer failed: %08x\n", hr);
1493     if(hr != S_OK){
1494         IDirectSound_Release(ds);
1495         return;
1496     }
1497
1498     bufdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_LOCSOFTWARE |
1499         DSBCAPS_PRIMARYBUFFER;
1500
1501     hr = IDirectSound_CreateSoundBuffer(ds, &bufdesc, &primary2, NULL);
1502     ok(hr == S_OK, "CreateSoundBuffer failed: %08x\n", hr);
1503     ok(primary == primary2, "Got different primary buffers: %p, %p\n", primary, primary2);
1504     if(hr == S_OK)
1505         IDirectSoundBuffer_Release(primary2);
1506
1507     buf8 = (IDirectSoundBuffer8 *)0xDEADBEEF;
1508     hr = IDirectSoundBuffer_QueryInterface(primary, &IID_IDirectSoundBuffer8,
1509             (void**)&buf8);
1510     ok(hr == E_NOINTERFACE, "QueryInterface gave wrong failure: %08x\n", hr);
1511     ok(buf8 == NULL, "Pointer didn't get set to NULL\n");
1512
1513     fmt.wFormatTag = WAVE_FORMAT_PCM;
1514     fmt.nChannels = 2;
1515     fmt.nSamplesPerSec = 48000;
1516     fmt.wBitsPerSample = 16;
1517     fmt.nBlockAlign = fmt.nChannels * fmt.wBitsPerSample / 8;
1518     fmt.nAvgBytesPerSec = fmt.nBlockAlign * fmt.nSamplesPerSec;
1519     fmt.cbSize = 0;
1520
1521     bufdesc.lpwfxFormat = &fmt;
1522     bufdesc.dwBufferBytes = fmt.nSamplesPerSec * fmt.nBlockAlign / 10;
1523     bufdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_LOCHARDWARE |
1524         DSBCAPS_CTRLVOLUME;
1525
1526     secondaries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1527             sizeof(IDirectSoundBuffer *) * caps.dwMaxHwMixingAllBuffers);
1528
1529     /* try to fill all of the hw buffers */
1530     trace("dwMaxHwMixingAllBuffers: %u\n", caps.dwMaxHwMixingAllBuffers);
1531     trace("dwMaxHwMixingStaticBuffers: %u\n", caps.dwMaxHwMixingStaticBuffers);
1532     trace("dwMaxHwMixingStreamingBuffers: %u\n", caps.dwMaxHwMixingStreamingBuffers);
1533     for(i = 0; i < caps.dwMaxHwMixingAllBuffers; ++i){
1534         hr = IDirectSound_CreateSoundBuffer(ds, &bufdesc, &secondaries[i], NULL);
1535         ok(hr == S_OK || hr == E_NOTIMPL || broken(hr == DSERR_CONTROLUNAVAIL) || broken(hr == E_FAIL),
1536                 "CreateSoundBuffer(%u) failed: %08x\n", i, hr);
1537         if(hr != S_OK)
1538             break;
1539
1540         bufcaps.dwSize = sizeof(bufcaps);
1541         hr = IDirectSoundBuffer_GetCaps(secondaries[i], &bufcaps);
1542         ok(hr == S_OK, "GetCaps failed: %08x\n", hr);
1543         ok((bufcaps.dwFlags & DSBCAPS_LOCHARDWARE) != 0,
1544                 "Buffer wasn't allocated in hardware, dwFlags: %x\n", bufcaps.dwFlags);
1545     }
1546
1547     /* see if we can create one more */
1548     hr = IDirectSound_CreateSoundBuffer(ds, &bufdesc, &secondary, NULL);
1549     ok((i == caps.dwMaxHwMixingAllBuffers && hr == DSERR_ALLOCATED) || /* out of hw buffers */
1550             (caps.dwMaxHwMixingAllBuffers == 0 && hr == DSERR_INVALIDCALL) || /* no hw buffers at all */
1551             hr == E_NOTIMPL || /* don't support hw buffers */
1552             broken(hr == DSERR_CONTROLUNAVAIL) || /* vmware winxp, others? */
1553             broken(hr == E_FAIL) || /* broken AC97 driver */
1554             broken(hr == S_OK) /* broken driver allows more hw bufs than dscaps claims */,
1555             "CreateSoundBuffer(%u) gave wrong error: %08x\n", i, hr);
1556     if(hr == S_OK)
1557         IDirectSoundBuffer_Release(secondary);
1558
1559     for(i = 0; i < caps.dwMaxHwMixingAllBuffers; ++i)
1560         if(secondaries[i])
1561             IDirectSoundBuffer_Release(secondaries[i]);
1562     HeapFree(GetProcessHeap(), 0, secondaries);
1563
1564     IDirectSoundBuffer_Release(primary);
1565     IDirectSound_Release(ds);
1566 }
1567
1568 START_TEST(dsound)
1569 {
1570     HMODULE hDsound;
1571
1572     CoInitialize(NULL);
1573
1574     hDsound = LoadLibrary("dsound.dll");
1575     if (hDsound)
1576     {
1577         BOOL ret;
1578
1579         ret = FreeLibrary(hDsound);
1580         ok( ret, "FreeLibrary(1) returned %d\n", GetLastError());
1581         SetLastError(0xdeadbeef);
1582         ret = FreeLibrary(hDsound);
1583         ok( ret ||
1584             broken(!ret && GetLastError() == ERROR_MOD_NOT_FOUND), /* NT4 */
1585             "FreeLibrary(2) returned %d\n", GetLastError());
1586         ok(!FreeLibrary(hDsound), "DirectSound DLL still loaded\n");
1587     }
1588
1589     hDsound = LoadLibrary("dsound.dll");
1590     if (hDsound)
1591     {
1592
1593         pDirectSoundEnumerateA = (void*)GetProcAddress(hDsound,
1594             "DirectSoundEnumerateA");
1595         pDirectSoundCreate = (void*)GetProcAddress(hDsound,
1596             "DirectSoundCreate");
1597
1598         gotdx8 = !!GetProcAddress(hDsound, "DirectSoundCreate8");
1599
1600         IDirectSound_tests();
1601         dsound_tests();
1602         test_hw_buffers();
1603
1604         FreeLibrary(hDsound);
1605     }
1606     else
1607         win_skip("dsound.dll not found - skipping all tests\n");
1608
1609     CoUninitialize();
1610 }