mshtml: DeleteMemberByDispID should return E_NOTIMPL.
[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                 test_buffer(dso,&secondary,0,FALSE,0,FALSE,0,
631                             winetest_interactive,1.0,0,NULL,0,0,FALSE,0);
632
633                 ref=IDirectSoundBuffer_Release(secondary);
634                 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
635                    "should have 0\n",ref);
636             }
637           }
638         }
639
640         ref=IDirectSoundBuffer_Release(primary);
641         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
642            "should have 0\n",ref);
643     }
644
645     /* Set the CooperativeLevel back to normal */
646     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
647     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
648     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
649
650 EXIT:
651     ref=IDirectSound_Release(dso);
652     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
653     if (ref!=0)
654         return DSERR_GENERIC;
655
656     return rc;
657 }
658
659 static HRESULT test_secondary(LPGUID lpGuid)
660 {
661     HRESULT rc;
662     LPDIRECTSOUND dso=NULL;
663     LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
664     DSBUFFERDESC bufdesc;
665     DSCAPS dscaps;
666     WAVEFORMATEX wfx, wfx1;
667     DWORD f, tag;
668     int ref;
669
670     /* Create the DirectSound object */
671     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
672     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
673        "DirectSoundCreate() failed: %08x\n",rc);
674     if (rc!=DS_OK)
675         return rc;
676
677     /* Get the device capabilities */
678     ZeroMemory(&dscaps, sizeof(dscaps));
679     dscaps.dwSize=sizeof(dscaps);
680     rc=IDirectSound_GetCaps(dso,&dscaps);
681     ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %08x\n",rc);
682     if (rc!=DS_OK)
683         goto EXIT;
684
685     /* We must call SetCooperativeLevel before creating primary buffer */
686     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
687     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
688     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
689     if (rc!=DS_OK)
690         goto EXIT;
691
692     ZeroMemory(&bufdesc, sizeof(bufdesc));
693     bufdesc.dwSize=sizeof(bufdesc);
694     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
695     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
696     ok(rc==DS_OK && primary!=NULL,
697        "IDirectSound_CreateSoundBuffer() failed to create a primary buffer %08x\n",rc);
698
699     if (rc==DS_OK && primary!=NULL) {
700         rc=IDirectSoundBuffer_GetFormat(primary,&wfx1,sizeof(wfx1),NULL);
701         ok(rc==DS_OK,"IDirectSoundBuffer8_Getformat() failed: %08x\n", rc);
702         if (rc!=DS_OK)
703             goto EXIT1;
704
705         for (f=0;f<NB_FORMATS;f++) {
706           for (tag=0;tag<NB_TAGS;tag++) {
707             WAVEFORMATEXTENSIBLE wfxe;
708
709             /* if float, we only want to test 32-bit */
710             if ((format_tags[tag] == WAVE_FORMAT_IEEE_FLOAT) && (formats[f][1] != 32))
711                 continue;
712
713             init_format(&wfx,format_tags[tag],formats[f][0],formats[f][1],
714                         formats[f][2]);
715             secondary=NULL;
716             ZeroMemory(&bufdesc, sizeof(bufdesc));
717             bufdesc.dwSize=sizeof(bufdesc);
718             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
719             bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
720                                         wfx.nBlockAlign);
721             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
722             ok(rc==DSERR_INVALIDPARAM,"IDirectSound_CreateSoundBuffer() "
723                "should have returned DSERR_INVALIDPARAM, returned: %08x\n", rc);
724             if (rc==DS_OK && secondary!=NULL)
725                 IDirectSoundBuffer_Release(secondary);
726
727             secondary=NULL;
728             ZeroMemory(&bufdesc, sizeof(bufdesc));
729             bufdesc.dwSize=sizeof(bufdesc);
730             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
731             bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
732                                         wfx.nBlockAlign);
733             bufdesc.lpwfxFormat=&wfx;
734             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
735             if (gotdx8 || wfx.wBitsPerSample <= 16 || wfx.wFormatTag == WAVE_FORMAT_IEEE_FLOAT)
736             {
737                 if (wfx.wBitsPerSample > 16)
738                     ok(((rc == DSERR_CONTROLUNAVAIL || rc == DSERR_INVALIDCALL || rc == DSERR_INVALIDPARAM /* 2003 */) && !secondary)
739                         || rc == DS_OK, /* driver dependent? */
740                         "IDirectSound_CreateSoundBuffer() "
741                         "should have returned (DSERR_CONTROLUNAVAIL or DSERR_INVALIDCALL) "
742                         "and NULL, returned: %08x %p\n", rc, secondary);
743                 else
744                     ok((rc==DS_OK && secondary!=NULL) || broken(rc == DSERR_CONTROLUNAVAIL), /* vmware drivers on w2k */
745                         "IDirectSound_CreateSoundBuffer() failed to create a secondary buffer %08x\n",rc);
746             }
747             else
748                 ok(rc==E_INVALIDARG, "Creating %d bpp buffer on dx < 8 returned: %p %08x\n",
749                    wfx.wBitsPerSample, secondary, rc);
750
751             if (!gotdx8)
752             {
753                 win_skip("Not doing the WAVE_FORMAT_EXTENSIBLE tests\n");
754                 /* Apparently they succeed with bogus values,
755                  * which means that older dsound doesn't look at them
756                  */
757                 goto no_wfe;
758             }
759
760             if (secondary)
761                 IDirectSoundBuffer_Release(secondary);
762             secondary = NULL;
763
764             bufdesc.lpwfxFormat=(WAVEFORMATEX*)&wfxe;
765             wfxe.Format = wfx;
766             wfxe.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
767             wfxe.SubFormat = (format_tags[tag] == WAVE_FORMAT_PCM ? KSDATAFORMAT_SUBTYPE_PCM : KSDATAFORMAT_SUBTYPE_IEEE_FLOAT);
768             wfxe.Format.cbSize = 1;
769             wfxe.Samples.wValidBitsPerSample = wfx.wBitsPerSample;
770             wfxe.dwChannelMask = (wfx.nChannels == 1 ? KSAUDIO_SPEAKER_MONO : KSAUDIO_SPEAKER_STEREO);
771
772             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
773             ok((rc==DSERR_INVALIDPARAM || rc==DSERR_INVALIDCALL /* 2003 */) && !secondary,
774                 "IDirectSound_CreateSoundBuffer() returned: %08x %p\n",
775                 rc, secondary);
776             if (secondary)
777             {
778                 IDirectSoundBuffer_Release(secondary);
779                 secondary=NULL;
780             }
781
782             wfxe.Format.cbSize = sizeof(wfxe) - sizeof(wfx) + 1;
783
784             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
785             ok(((rc==DSERR_CONTROLUNAVAIL || rc==DSERR_INVALIDCALL || rc==DSERR_INVALIDPARAM)
786                 && !secondary)
787                || rc==DS_OK, /* 2003 / 2008 */
788                 "IDirectSound_CreateSoundBuffer() returned: %08x %p\n",
789                 rc, secondary);
790             if (secondary)
791             {
792                 IDirectSoundBuffer_Release(secondary);
793                 secondary=NULL;
794             }
795
796             wfxe.Format.cbSize = sizeof(wfxe) - sizeof(wfx);
797             wfxe.SubFormat = GUID_NULL;
798             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
799             ok((rc==DSERR_INVALIDPARAM || rc==DSERR_INVALIDCALL) && !secondary,
800                 "IDirectSound_CreateSoundBuffer() returned: %08x %p\n",
801                 rc, secondary);
802             if (secondary)
803             {
804                 IDirectSoundBuffer_Release(secondary);
805                 secondary=NULL;
806             }
807             wfxe.SubFormat = (format_tags[tag] == WAVE_FORMAT_PCM ? KSDATAFORMAT_SUBTYPE_PCM : KSDATAFORMAT_SUBTYPE_IEEE_FLOAT);
808
809             ++wfxe.Samples.wValidBitsPerSample;
810             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
811             ok(rc==DSERR_INVALIDPARAM && !secondary,
812                 "IDirectSound_CreateSoundBuffer() returned: %08x %p\n",
813                 rc, secondary);
814             if (secondary)
815             {
816                 IDirectSoundBuffer_Release(secondary);
817                 secondary=NULL;
818             }
819             --wfxe.Samples.wValidBitsPerSample;
820
821             wfxe.Samples.wValidBitsPerSample = 0;
822             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
823             ok(rc==DS_OK && secondary,
824                 "IDirectSound_CreateSoundBuffer() returned: %08x %p\n",
825                 rc, secondary);
826             if (secondary)
827             {
828                 IDirectSoundBuffer_Release(secondary);
829                 secondary=NULL;
830             }
831             wfxe.Samples.wValidBitsPerSample = wfxe.Format.wBitsPerSample;
832
833             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
834             ok(rc==DS_OK && secondary!=NULL,
835                 "IDirectSound_CreateSoundBuffer() failed to create a secondary buffer %08x\n",rc);
836
837 no_wfe:
838             if (rc==DS_OK && secondary!=NULL) {
839                 if (winetest_interactive) {
840                     trace("  Testing a secondary buffer at %dx%dx%d (fmt=%d) "
841                         "with a primary buffer at %dx%dx%d\n",
842                         wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,format_tags[tag],
843                         wfx1.nSamplesPerSec,wfx1.wBitsPerSample,wfx1.nChannels);
844                 }
845                 test_buffer(dso,&secondary,0,FALSE,0,FALSE,0,
846                             winetest_interactive,1.0,0,NULL,0,0,FALSE,0);
847
848                 ref=IDirectSoundBuffer_Release(secondary);
849                 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
850                    "should have 0\n",ref);
851             }
852           }
853         }
854 EXIT1:
855         ref=IDirectSoundBuffer_Release(primary);
856         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
857            "should have 0\n",ref);
858     }
859
860     /* Set the CooperativeLevel back to normal */
861     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
862     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
863     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
864
865 EXIT:
866     ref=IDirectSound_Release(dso);
867     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
868     if (ref!=0)
869         return DSERR_GENERIC;
870
871     return rc;
872 }
873
874 static HRESULT test_block_align(LPGUID lpGuid)
875 {
876     HRESULT rc;
877     LPDIRECTSOUND dso=NULL;
878     LPDIRECTSOUNDBUFFER secondary=NULL;
879     DSBUFFERDESC bufdesc;
880     DSBCAPS dsbcaps;
881     WAVEFORMATEX wfx;
882     DWORD pos, pos2;
883     int ref;
884
885     /* Create the DirectSound object */
886     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
887     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
888        "DirectSoundCreate() failed: %08x\n",rc);
889     if (rc!=DS_OK)
890         return rc;
891
892     init_format(&wfx,WAVE_FORMAT_PCM,11025,16,2);
893     ZeroMemory(&bufdesc, sizeof(bufdesc));
894     bufdesc.dwSize=sizeof(bufdesc);
895     bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
896     bufdesc.dwBufferBytes=wfx.nAvgBytesPerSec + 1;
897     bufdesc.lpwfxFormat=&wfx;
898     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
899     ok(rc == DS_OK || broken(rc == DSERR_CONTROLUNAVAIL), /* vmware drivers on w2k */
900        "IDirectSound_CreateSoundBuffer() should have returned DS_OK, returned: %08x\n", rc);
901
902     if (rc==DS_OK && secondary!=NULL) {
903         ZeroMemory(&dsbcaps, sizeof(dsbcaps));
904         dsbcaps.dwSize = sizeof(dsbcaps);
905         rc=IDirectSoundBuffer_GetCaps(secondary,&dsbcaps);
906         ok(rc==DS_OK,"IDirectSoundBuffer_GetCaps() should have returned DS_OK, "
907            "returned: %08x\n", rc);
908         if (rc==DS_OK && wfx.nBlockAlign > 1)
909         {
910             ok(dsbcaps.dwBufferBytes==(wfx.nAvgBytesPerSec + wfx.nBlockAlign),
911                "Buffer size not a multiple of nBlockAlign: requested %d, "
912                "got %d, should be %d\n", bufdesc.dwBufferBytes,
913                dsbcaps.dwBufferBytes, wfx.nAvgBytesPerSec + wfx.nBlockAlign);
914
915             rc = IDirectSoundBuffer_SetCurrentPosition(secondary, 0);
916             ok(rc == DS_OK, "Could not set position to 0: %08x\n", rc);
917             rc = IDirectSoundBuffer_GetCurrentPosition(secondary, &pos, NULL);
918             ok(rc == DS_OK, "Could not get position: %08x\n", rc);
919             rc = IDirectSoundBuffer_SetCurrentPosition(secondary, 1);
920             ok(rc == DS_OK, "Could not set position to 1: %08x\n", rc);
921             rc = IDirectSoundBuffer_GetCurrentPosition(secondary, &pos2, NULL);
922             ok(rc == DS_OK, "Could not get new position: %08x\n", rc);
923             ok(pos == pos2, "Positions not the same! Old position: %d, new position: %d\n", pos, pos2);
924         }
925         ref=IDirectSoundBuffer_Release(secondary);
926         ok(ref==0,"IDirectSoundBuffer_Release() secondary has %d references, "
927            "should have 0\n",ref);
928     }
929
930     ref=IDirectSound_Release(dso);
931     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
932     if (ref!=0)
933         return DSERR_GENERIC;
934
935     return rc;
936 }
937
938 static struct fmt {
939     int bits;
940     int channels;
941 } fmts[] = { { 8, 1 }, { 8, 2 }, { 16, 1 }, {16, 2 } };
942
943 static HRESULT test_frequency(LPGUID lpGuid)
944 {
945     HRESULT rc;
946     LPDIRECTSOUND dso=NULL;
947     LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
948     DSBUFFERDESC bufdesc;
949     DSCAPS dscaps;
950     WAVEFORMATEX wfx, wfx1;
951     DWORD f, r;
952     int ref;
953     int rates[] = { 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100,
954                     48000, 96000 };
955
956     /* Create the DirectSound object */
957     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
958     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
959        "DirectSoundCreate() failed: %08x\n",rc);
960     if (rc!=DS_OK)
961         return rc;
962
963     /* Get the device capabilities */
964     ZeroMemory(&dscaps, sizeof(dscaps));
965     dscaps.dwSize=sizeof(dscaps);
966     rc=IDirectSound_GetCaps(dso,&dscaps);
967     ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %08x\n",rc);
968     if (rc!=DS_OK)
969         goto EXIT;
970
971     /* We must call SetCooperativeLevel before creating primary buffer */
972     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
973     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
974     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
975     if (rc!=DS_OK)
976         goto EXIT;
977
978     ZeroMemory(&bufdesc, sizeof(bufdesc));
979     bufdesc.dwSize=sizeof(bufdesc);
980     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
981     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
982     ok(rc==DS_OK && primary!=NULL,
983        "IDirectSound_CreateSoundBuffer() failed to create a primary buffer %08x\n",rc);
984
985     if (rc==DS_OK && primary!=NULL) {
986         rc=IDirectSoundBuffer_GetFormat(primary,&wfx1,sizeof(wfx1),NULL);
987         ok(rc==DS_OK,"IDirectSoundBuffer8_Getformat() failed: %08x\n", rc);
988         if (rc!=DS_OK)
989             goto EXIT1;
990
991         for (f=0;f<sizeof(fmts)/sizeof(fmts[0]);f++) {
992         for (r=0;r<sizeof(rates)/sizeof(rates[0]);r++) {
993             init_format(&wfx,WAVE_FORMAT_PCM,11025,fmts[f].bits,
994                         fmts[f].channels);
995             secondary=NULL;
996             ZeroMemory(&bufdesc, sizeof(bufdesc));
997             bufdesc.dwSize=sizeof(bufdesc);
998             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2|DSBCAPS_CTRLFREQUENCY;
999             bufdesc.dwBufferBytes=align((wfx.nAvgBytesPerSec*rates[r]/11025)*
1000                                         BUFFER_LEN/1000,wfx.nBlockAlign);
1001             bufdesc.lpwfxFormat=&wfx;
1002             if (winetest_interactive) {
1003                 trace("  Testing a secondary buffer at %dx%dx%d "
1004                       "with a primary buffer at %dx%dx%d\n",
1005                       wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,
1006                       wfx1.nSamplesPerSec,wfx1.wBitsPerSample,wfx1.nChannels);
1007             }
1008             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
1009             ok((rc==DS_OK && secondary!=NULL) || broken(rc == DSERR_CONTROLUNAVAIL), /* vmware drivers on w2k */
1010                "IDirectSound_CreateSoundBuffer() failed to create a secondary buffer %08x\n",rc);
1011
1012             if (rc==DS_OK && secondary!=NULL) {
1013                 test_buffer(dso,&secondary,0,FALSE,0,FALSE,0,
1014                             winetest_interactive,1.0,0,NULL,0,0,TRUE,rates[r]);
1015
1016                 ref=IDirectSoundBuffer_Release(secondary);
1017                 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
1018                    "should have 0\n",ref);
1019             }
1020         }
1021         }
1022 EXIT1:
1023         ref=IDirectSoundBuffer_Release(primary);
1024         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
1025            "should have 0\n",ref);
1026     }
1027
1028     /* Set the CooperativeLevel back to normal */
1029     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
1030     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
1031     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
1032
1033 EXIT:
1034     ref=IDirectSound_Release(dso);
1035     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
1036     if (ref!=0)
1037         return DSERR_GENERIC;
1038
1039     return rc;
1040 }
1041
1042 static HRESULT test_notify(LPDIRECTSOUNDBUFFER dsb,
1043                            DWORD count, LPHANDLE event,
1044                            DWORD expected)
1045 {
1046     HRESULT rc;
1047     DWORD ret;
1048
1049     rc=IDirectSoundBuffer_SetCurrentPosition(dsb,0);
1050     ok(rc==DS_OK,
1051        "IDirectSoundBuffer_SetCurrentPosition failed %08x\n",rc);
1052     if(rc!=DS_OK)
1053         return rc;
1054
1055     rc=IDirectSoundBuffer_Play(dsb,0,0,0);
1056     ok(rc==DS_OK,"IDirectSoundBuffer_Play failed %08x\n",rc);
1057     if(rc!=DS_OK)
1058         return rc;
1059
1060     rc=IDirectSoundBuffer_Stop(dsb);
1061     ok(rc==DS_OK,"IDirectSoundBuffer_Stop failed %08x\n",rc);
1062     if(rc!=DS_OK)
1063         return rc;
1064
1065     ret=WaitForMultipleObjects(count,event,FALSE,0);
1066     ok(ret==expected,"expected %d. got %d\n",expected,ret);
1067     return rc;
1068 }
1069
1070 static HRESULT test_duplicate(LPGUID lpGuid)
1071 {
1072     HRESULT rc;
1073     LPDIRECTSOUND dso=NULL;
1074     LPDIRECTSOUNDBUFFER primary=NULL;
1075     DSBUFFERDESC bufdesc;
1076     int ref;
1077
1078     /* Create the DirectSound object */
1079     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
1080     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
1081        "DirectSoundCreate() failed: %08x\n",rc);
1082     if (rc!=DS_OK)
1083         return rc;
1084
1085     /* We must call SetCooperativeLevel before creating primary buffer */
1086     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
1087     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
1088     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
1089     if (rc!=DS_OK)
1090         goto EXIT;
1091
1092     ZeroMemory(&bufdesc, sizeof(bufdesc));
1093     bufdesc.dwSize=sizeof(bufdesc);
1094     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
1095     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
1096     ok(rc==DS_OK && primary!=NULL,"IDirectSound_CreateSoundBuffer() failed "
1097        "to create a primary buffer %08x\n",rc);
1098
1099     if (rc==DS_OK && primary!=NULL) {
1100         LPDIRECTSOUNDBUFFER original=NULL;
1101         WAVEFORMATEX wfx;
1102
1103         init_format(&wfx,WAVE_FORMAT_PCM,22050,16,1);
1104         ZeroMemory(&bufdesc, sizeof(bufdesc));
1105         bufdesc.dwSize=sizeof(bufdesc);
1106         bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2|DSBCAPS_CTRLPOSITIONNOTIFY;
1107         bufdesc.dwBufferBytes=wfx.nAvgBytesPerSec/100; /* very short buffer */
1108         bufdesc.lpwfxFormat=&wfx;
1109         rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&original,NULL);
1110         ok(rc==DS_OK && original!=NULL,
1111            "IDirectSound_CreateSoundBuffer() failed to create a original "
1112            "buffer %08x\n",rc);
1113         if (rc==DS_OK && original!=NULL) {
1114             LPDIRECTSOUNDBUFFER duplicated=NULL;
1115             LPDIRECTSOUNDNOTIFY notify=NULL;
1116             HANDLE event[2];
1117             LPVOID buf=NULL;
1118             DWORD bufsize;
1119             int i;
1120
1121             /* Prepare notify events */
1122             for (i=0;i<sizeof(event)/sizeof(event[0]);i++) {
1123                 event[i] = CreateEvent(NULL,FALSE,FALSE,NULL);
1124             }
1125
1126             /* Make silent buffer */
1127             rc=IDirectSoundBuffer_Lock(original,0,0,&buf,&bufsize,
1128                                        NULL,NULL,DSBLOCK_ENTIREBUFFER);
1129             ok(rc==DS_OK && buf!=NULL,
1130                "IDirectSoundBuffer_Lock failed to lock the buffer %08x\n",rc);
1131             if (rc==DS_OK && buf!=NULL) {
1132                 ZeroMemory(buf,bufsize);
1133                 rc=IDirectSoundBuffer_Unlock(original,buf,bufsize,
1134                                              NULL,0);
1135                 ok(rc==DS_OK,"IDirectSoundBuffer_Unlock failed to unlock "
1136                    "%08x\n",rc);
1137             }
1138
1139             rc=IDirectSoundBuffer_QueryInterface(original,
1140                                                  &IID_IDirectSoundNotify,
1141                                                  (void**)&notify);
1142             ok(rc==DS_OK && notify!=NULL,
1143                "IDirectSoundBuffer_QueryInterface() failed to create a "
1144                "notification %08x\n",rc);
1145             if (rc==DS_OK && notify!=NULL) {
1146                 DSBPOSITIONNOTIFY dsbpn;
1147                 LPDIRECTSOUNDNOTIFY dup_notify=NULL;
1148
1149                 dsbpn.dwOffset=DSBPN_OFFSETSTOP;
1150                 dsbpn.hEventNotify=event[0];
1151                 rc=IDirectSoundNotify_SetNotificationPositions(notify,
1152                                                                1,&dsbpn);
1153                 ok(rc==DS_OK,"IDirectSoundNotify_SetNotificationPositions "
1154                    "failed %08x\n",rc);
1155
1156                 rc=IDirectSound_DuplicateSoundBuffer(dso,original,&duplicated);
1157                 ok(rc==DS_OK && duplicated!=NULL,
1158                    "IDirectSound_DuplicateSoundBuffer failed %08x\n",rc);
1159
1160                 trace("testing duplicated buffer without notifications.\n");
1161                 test_notify(duplicated,sizeof(event)/sizeof(event[0]),
1162                             event,WAIT_TIMEOUT);
1163
1164                 rc=IDirectSoundBuffer_QueryInterface(duplicated,
1165                                                      &IID_IDirectSoundNotify,
1166                                                      (void**)&dup_notify);
1167                 ok(rc==DS_OK&&dup_notify!=NULL,
1168                    "IDirectSoundBuffer_QueryInterface() failed to create a "
1169                    "notification %08x\n",rc);
1170                 if(rc==DS_OK&&dup_notify!=NULL) {
1171                     dsbpn.dwOffset=DSBPN_OFFSETSTOP;
1172                     dsbpn.hEventNotify=event[1];
1173                     rc=IDirectSoundNotify_SetNotificationPositions(dup_notify,
1174                                                                    1,&dsbpn);
1175                     ok(rc==DS_OK,"IDirectSoundNotify_SetNotificationPositions "
1176                        "failed %08x\n",rc);
1177
1178                     trace("testing duplicated buffer with a notification.\n");
1179                     test_notify(duplicated,sizeof(event)/sizeof(event[0]),
1180                                 event,WAIT_OBJECT_0+1);
1181
1182                     ref=IDirectSoundNotify_Release(dup_notify);
1183                     ok(ref==0,"IDirectSoundNotify_Release() has %d references, "
1184                        "should have 0\n",ref);
1185                 }
1186                 ref=IDirectSoundNotify_Release(notify);
1187                 ok(ref==0,"IDirectSoundNotify_Release() has %d references, "
1188                    "should have 0\n",ref);
1189
1190                 trace("testing original buffer with a notification.\n");
1191                 test_notify(original,sizeof(event)/sizeof(event[0]),
1192                             event,WAIT_OBJECT_0);
1193
1194                 ref=IDirectSoundBuffer_Release(duplicated);
1195                 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
1196                    "should have 0\n",ref);
1197             }
1198             ref=IDirectSoundBuffer_Release(original);
1199             ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
1200                "should have 0\n",ref);
1201         }
1202         ref=IDirectSoundBuffer_Release(primary);
1203         ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
1204            "should have 0\n",ref);
1205     }
1206
1207     /* Set the CooperativeLevel back to normal */
1208     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
1209     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
1210     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
1211
1212 EXIT:
1213     ref=IDirectSound_Release(dso);
1214     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
1215     if (ref!=0)
1216         return DSERR_GENERIC;
1217
1218     return rc;
1219 }
1220
1221 static unsigned int number;
1222
1223 static BOOL WINAPI dsenum_callback(LPGUID lpGuid, LPCSTR lpcstrDescription,
1224                                    LPCSTR lpcstrModule, LPVOID lpContext)
1225 {
1226     HRESULT rc;
1227     trace("*** Testing %s - %s ***\n",lpcstrDescription,lpcstrModule);
1228
1229     /* Don't test the primary device */
1230     if (!number++)
1231     {
1232         ok (!lpcstrModule[0], "lpcstrModule(%s) != NULL\n", lpcstrModule);
1233         return 1;
1234     }
1235
1236     rc = test_dsound(lpGuid);
1237     if (rc == DSERR_NODRIVER)
1238         trace("  No Driver\n");
1239     else if (rc == DSERR_ALLOCATED)
1240         trace("  Already In Use\n");
1241     else if (rc == E_FAIL)
1242         trace("  No Device\n");
1243     else {
1244         test_block_align(lpGuid);
1245         test_primary(lpGuid);
1246         test_primary_secondary(lpGuid);
1247         test_secondary(lpGuid);
1248         test_frequency(lpGuid);
1249         test_duplicate(lpGuid);
1250     }
1251
1252     return 1;
1253 }
1254
1255 static void dsound_tests(void)
1256 {
1257     HRESULT rc;
1258     rc=pDirectSoundEnumerateA(&dsenum_callback,NULL);
1259     ok(rc==DS_OK,"DirectSoundEnumerateA() failed: %08x\n",rc);
1260 }
1261
1262 START_TEST(dsound)
1263 {
1264     HMODULE hDsound;
1265
1266     CoInitialize(NULL);
1267
1268     hDsound = LoadLibrary("dsound.dll");
1269     if (hDsound)
1270     {
1271         BOOL ret;
1272
1273         ret = FreeLibrary(hDsound);
1274         ok( ret, "FreeLibrary(1) returned %d\n", GetLastError());
1275         SetLastError(0xdeadbeef);
1276         ret = FreeLibrary(hDsound);
1277         ok( ret ||
1278             broken(!ret && GetLastError() == ERROR_MOD_NOT_FOUND), /* NT4 */
1279             "FreeLibrary(2) returned %d\n", GetLastError());
1280         ok(!FreeLibrary(hDsound), "DirectSound DLL still loaded\n");
1281     }
1282
1283     hDsound = LoadLibrary("dsound.dll");
1284     if (hDsound)
1285     {
1286
1287         pDirectSoundEnumerateA = (void*)GetProcAddress(hDsound,
1288             "DirectSoundEnumerateA");
1289         pDirectSoundCreate = (void*)GetProcAddress(hDsound,
1290             "DirectSoundCreate");
1291
1292         gotdx8 = !!GetProcAddress(hDsound, "DirectSoundCreate8");
1293
1294         IDirectSound_tests();
1295         dsound_tests();
1296
1297         FreeLibrary(hDsound);
1298     }
1299     else
1300         win_skip("dsound.dll not found!\n");
1301
1302     CoUninitialize();
1303 }