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