shlwapi: Beginning implementation of IUnknown_QueryServiceForWebBrowserApp.
[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,
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;
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             /* We must call SetCooperativeLevel to be allowed to call
560              * SetFormat */
561             /* DSOUND: Setting DirectSound cooperative level to
562              * DSSCL_PRIORITY */
563             rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
564             ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
565             if (rc!=DS_OK)
566                 goto EXIT;
567
568             init_format(&wfx,WAVE_FORMAT_PCM,formats[f][0],formats[f][1],
569                         formats[f][2]);
570             wfx2=wfx;
571             rc=IDirectSoundBuffer_SetFormat(primary,&wfx);
572
573             if (wfx.wBitsPerSample <= 16)
574                 ok(rc==DS_OK,"IDirectSoundBuffer_SetFormat(%s) failed: %08x\n",
575                    format_string(&wfx), rc);
576             else
577                 ok(rc==DS_OK || rc == E_INVALIDARG, "SetFormat (%s) failed: %08x\n",
578                    format_string(&wfx), rc);
579
580             /* There is no guarantee that SetFormat will actually change the
581              * format to what we asked for. It depends on what the soundcard
582              * supports. So we must re-query the format.
583              */
584             rc=IDirectSoundBuffer_GetFormat(primary,&wfx,sizeof(wfx),NULL);
585             ok(rc==DS_OK,"IDirectSoundBuffer_GetFormat() failed: %08x\n", rc);
586             if (rc==DS_OK &&
587                 (wfx.wFormatTag!=wfx2.wFormatTag ||
588                  wfx.nSamplesPerSec!=wfx2.nSamplesPerSec ||
589                  wfx.wBitsPerSample!=wfx2.wBitsPerSample ||
590                  wfx.nChannels!=wfx2.nChannels)) {
591                 trace("Requested primary format tag=0x%04x %dx%dx%d "
592                       "avg.B/s=%d align=%d\n",
593                       wfx2.wFormatTag,wfx2.nSamplesPerSec,wfx2.wBitsPerSample,
594                       wfx2.nChannels,wfx2.nAvgBytesPerSec,wfx2.nBlockAlign);
595                 trace("Got tag=0x%04x %dx%dx%d avg.B/s=%d align=%d\n",
596                       wfx.wFormatTag,wfx.nSamplesPerSec,wfx.wBitsPerSample,
597                       wfx.nChannels,wfx.nAvgBytesPerSec,wfx.nBlockAlign);
598             }
599
600             /* Set the CooperativeLevel back to normal */
601             /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
602             rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
603             ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
604
605             init_format(&wfx2,WAVE_FORMAT_PCM,11025,16,2);
606
607             secondary=NULL;
608             ZeroMemory(&bufdesc, sizeof(bufdesc));
609             bufdesc.dwSize=sizeof(bufdesc);
610             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
611             bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
612                                         wfx.nBlockAlign);
613             bufdesc.lpwfxFormat=&wfx2;
614             if (winetest_interactive) {
615                 trace("  Testing a primary buffer at %dx%dx%d with a "
616                       "secondary buffer at %dx%dx%d\n",
617                       wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,
618                       wfx2.nSamplesPerSec,wfx2.wBitsPerSample,wfx2.nChannels);
619             }
620             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
621             ok(rc==DS_OK && secondary!=NULL,
622                "IDirectSound_CreateSoundBuffer() failed to create a secondary buffer %08x\n",rc);
623
624             if (rc==DS_OK && secondary!=NULL) {
625                 test_buffer(dso,&secondary,0,FALSE,0,FALSE,0,
626                             winetest_interactive,1.0,0,NULL,0,0,FALSE,0);
627
628                 ref=IDirectSoundBuffer_Release(secondary);
629                 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
630                    "should have 0\n",ref);
631             }
632         }
633
634         ref=IDirectSoundBuffer_Release(primary);
635         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
636            "should have 0\n",ref);
637     }
638
639     /* Set the CooperativeLevel back to normal */
640     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
641     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
642     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
643
644 EXIT:
645     ref=IDirectSound_Release(dso);
646     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
647     if (ref!=0)
648         return DSERR_GENERIC;
649
650     return rc;
651 }
652
653 static HRESULT test_secondary(LPGUID lpGuid)
654 {
655     HRESULT rc;
656     LPDIRECTSOUND dso=NULL;
657     LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
658     DSBUFFERDESC bufdesc;
659     DSCAPS dscaps;
660     WAVEFORMATEX wfx, wfx1;
661     DWORD f;
662     int ref;
663
664     /* Create the DirectSound object */
665     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
666     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
667        "DirectSoundCreate() failed: %08x\n",rc);
668     if (rc!=DS_OK)
669         return rc;
670
671     /* Get the device capabilities */
672     ZeroMemory(&dscaps, sizeof(dscaps));
673     dscaps.dwSize=sizeof(dscaps);
674     rc=IDirectSound_GetCaps(dso,&dscaps);
675     ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %08x\n",rc);
676     if (rc!=DS_OK)
677         goto EXIT;
678
679     /* We must call SetCooperativeLevel before creating primary buffer */
680     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
681     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
682     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
683     if (rc!=DS_OK)
684         goto EXIT;
685
686     ZeroMemory(&bufdesc, sizeof(bufdesc));
687     bufdesc.dwSize=sizeof(bufdesc);
688     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
689     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
690     ok(rc==DS_OK && primary!=NULL,
691        "IDirectSound_CreateSoundBuffer() failed to create a primary buffer %08x\n",rc);
692
693     if (rc==DS_OK && primary!=NULL) {
694         rc=IDirectSoundBuffer_GetFormat(primary,&wfx1,sizeof(wfx1),NULL);
695         ok(rc==DS_OK,"IDirectSoundBuffer8_Getformat() failed: %08x\n", rc);
696         if (rc!=DS_OK)
697             goto EXIT1;
698
699         for (f=0;f<NB_FORMATS;f++) {
700             WAVEFORMATEXTENSIBLE wfxe;
701             init_format(&wfx,WAVE_FORMAT_PCM,formats[f][0],formats[f][1],
702                         formats[f][2]);
703             secondary=NULL;
704             ZeroMemory(&bufdesc, sizeof(bufdesc));
705             bufdesc.dwSize=sizeof(bufdesc);
706             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
707             bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
708                                         wfx.nBlockAlign);
709             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
710             ok(rc==DSERR_INVALIDPARAM,"IDirectSound_CreateSoundBuffer() "
711                "should have returned DSERR_INVALIDPARAM, returned: %08x\n", rc);
712             if (rc==DS_OK && secondary!=NULL)
713                 IDirectSoundBuffer_Release(secondary);
714
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             bufdesc.lpwfxFormat=&wfx;
722             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
723             if (gotdx8 || wfx.wBitsPerSample <= 16)
724             {
725                 if (wfx.wBitsPerSample > 16)
726                     ok(((rc == DSERR_CONTROLUNAVAIL || rc == DSERR_INVALIDCALL || rc == DSERR_INVALIDPARAM /* 2003 */) && !secondary)
727                         || rc == DS_OK, /* driver dependent? */
728                         "IDirectSound_CreateSoundBuffer() "
729                         "should have returned (DSERR_CONTROLUNAVAIL or DSERR_INVALIDCALL) "
730                         "and NULL, returned: %08x %p\n", rc, secondary);
731                 else
732                     ok(rc==DS_OK && secondary!=NULL,
733                         "IDirectSound_CreateSoundBuffer() failed to create a secondary buffer %08x\n",rc);
734             }
735             else
736                 ok(rc==E_INVALIDARG, "Creating %d bpp buffer on dx < 8 returned: %p %08x\n",
737                    wfx.wBitsPerSample, secondary, rc);
738
739             if (!gotdx8)
740             {
741                 skip("Not doing the WAVE_FORMAT_EXTENSIBLE tests\n");
742                 /* Apparently they succeed with bogus values,
743                  * which means that older dsound doesn't look at them
744                  */
745                 goto no_wfe;
746             }
747
748             if (secondary)
749                 IDirectSoundBuffer_Release(secondary);
750             secondary = NULL;
751
752             bufdesc.lpwfxFormat=(WAVEFORMATEX*)&wfxe;
753             wfxe.Format = wfx;
754             wfxe.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
755             wfxe.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
756             wfxe.Format.cbSize = 1;
757             wfxe.Samples.wValidBitsPerSample = wfx.wBitsPerSample;
758             wfxe.dwChannelMask = (wfx.nChannels == 1 ? KSAUDIO_SPEAKER_MONO : KSAUDIO_SPEAKER_STEREO);
759
760             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
761             ok((rc==DSERR_INVALIDPARAM || rc==DSERR_INVALIDCALL /* 2003 */) && !secondary,
762                 "IDirectSound_CreateSoundBuffer() returned: %08x %p\n",
763                 rc, secondary);
764             if (secondary)
765             {
766                 IDirectSoundBuffer_Release(secondary);
767                 secondary=NULL;
768             }
769
770             wfxe.Format.cbSize = sizeof(wfxe) - sizeof(wfx) + 1;
771
772             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
773             ok(((rc==DSERR_CONTROLUNAVAIL || rc==DSERR_INVALIDCALL || rc==DSERR_INVALIDPARAM)
774                 && !secondary)
775                || rc==DS_OK, /* 2003 / 2008 */
776                 "IDirectSound_CreateSoundBuffer() returned: %08x %p\n",
777                 rc, secondary);
778             if (secondary)
779             {
780                 IDirectSoundBuffer_Release(secondary);
781                 secondary=NULL;
782             }
783
784             wfxe.Format.cbSize = sizeof(wfxe) - sizeof(wfx);
785             wfxe.SubFormat = GUID_NULL;
786             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
787             ok((rc==DSERR_INVALIDPARAM || rc==DSERR_INVALIDCALL) && !secondary,
788                 "IDirectSound_CreateSoundBuffer() returned: %08x %p\n",
789                 rc, secondary);
790             if (secondary)
791             {
792                 IDirectSoundBuffer_Release(secondary);
793                 secondary=NULL;
794             }
795             wfxe.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
796
797             ++wfxe.Samples.wValidBitsPerSample;
798             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
799             ok(rc==DSERR_INVALIDPARAM && !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.Samples.wValidBitsPerSample;
808
809             wfxe.Samples.wValidBitsPerSample = 0;
810             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
811             ok(rc==DS_OK && 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 = wfxe.Format.wBitsPerSample;
820
821             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
822             ok(rc==DS_OK && secondary!=NULL,
823                 "IDirectSound_CreateSoundBuffer() failed to create a secondary buffer %08x\n",rc);
824
825 no_wfe:
826             if (rc==DS_OK && secondary!=NULL) {
827                 if (winetest_interactive) {
828                     trace("  Testing a secondary buffer at %dx%dx%d "
829                         "with a primary buffer at %dx%dx%d\n",
830                         wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,
831                         wfx1.nSamplesPerSec,wfx1.wBitsPerSample,wfx1.nChannels);
832                 }
833                 test_buffer(dso,&secondary,0,FALSE,0,FALSE,0,
834                             winetest_interactive,1.0,0,NULL,0,0,FALSE,0);
835
836                 ref=IDirectSoundBuffer_Release(secondary);
837                 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
838                    "should have 0\n",ref);
839             }
840         }
841 EXIT1:
842         ref=IDirectSoundBuffer_Release(primary);
843         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
844            "should have 0\n",ref);
845     }
846
847     /* Set the CooperativeLevel back to normal */
848     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
849     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
850     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
851
852 EXIT:
853     ref=IDirectSound_Release(dso);
854     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
855     if (ref!=0)
856         return DSERR_GENERIC;
857
858     return rc;
859 }
860
861 static HRESULT test_block_align(LPGUID lpGuid)
862 {
863     HRESULT rc;
864     LPDIRECTSOUND dso=NULL;
865     LPDIRECTSOUNDBUFFER secondary=NULL;
866     DSBUFFERDESC bufdesc;
867     DSBCAPS dsbcaps;
868     WAVEFORMATEX wfx;
869     DWORD pos, pos2;
870     int ref;
871
872     /* Create the DirectSound object */
873     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
874     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
875        "DirectSoundCreate() failed: %08x\n",rc);
876     if (rc!=DS_OK)
877         return rc;
878
879     init_format(&wfx,WAVE_FORMAT_PCM,11025,16,2);
880     ZeroMemory(&bufdesc, sizeof(bufdesc));
881     bufdesc.dwSize=sizeof(bufdesc);
882     bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
883     bufdesc.dwBufferBytes=wfx.nAvgBytesPerSec + 1;
884     bufdesc.lpwfxFormat=&wfx;
885     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
886     ok(rc==DS_OK,"IDirectSound_CreateSoundBuffer() "
887        "should have returned DS_OK, returned: %08x\n", rc);
888
889     if (rc==DS_OK && secondary!=NULL) {
890         ZeroMemory(&dsbcaps, sizeof(dsbcaps));
891         dsbcaps.dwSize = sizeof(dsbcaps);
892         rc=IDirectSoundBuffer_GetCaps(secondary,&dsbcaps);
893         ok(rc==DS_OK,"IDirectSoundBuffer_GetCaps() should have returned DS_OK, "
894            "returned: %08x\n", rc);
895         if (rc==DS_OK && wfx.nBlockAlign > 1)
896         {
897             ok(dsbcaps.dwBufferBytes==(wfx.nAvgBytesPerSec + wfx.nBlockAlign),
898                "Buffer size not a multiple of nBlockAlign: requested %d, "
899                "got %d, should be %d\n", bufdesc.dwBufferBytes,
900                dsbcaps.dwBufferBytes, wfx.nAvgBytesPerSec + wfx.nBlockAlign);
901
902             rc = IDirectSoundBuffer_SetCurrentPosition(secondary, 0);
903             ok(rc == DS_OK, "Could not set position to 0: %08x\n", rc);
904             rc = IDirectSoundBuffer_GetCurrentPosition(secondary, &pos, NULL);
905             ok(rc == DS_OK, "Could not get position: %08x\n", rc);
906             rc = IDirectSoundBuffer_SetCurrentPosition(secondary, 1);
907             ok(rc == DS_OK, "Could not set position to 1: %08x\n", rc);
908             rc = IDirectSoundBuffer_GetCurrentPosition(secondary, &pos2, NULL);
909             ok(rc == DS_OK, "Could not get new position: %08x\n", rc);
910             ok(pos == pos2, "Positions not the same! Old position: %d, new position: %d\n", pos, pos2);
911         }
912         ref=IDirectSoundBuffer_Release(secondary);
913         ok(ref==0,"IDirectSoundBuffer_Release() secondary has %d references, "
914            "should have 0\n",ref);
915     }
916
917     ref=IDirectSound_Release(dso);
918     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
919     if (ref!=0)
920         return DSERR_GENERIC;
921
922     return rc;
923 }
924
925 static struct fmt {
926     int bits;
927     int channels;
928 } fmts[] = { { 8, 1 }, { 8, 2 }, { 16, 1 }, {16, 2 } };
929
930 static HRESULT test_frequency(LPGUID lpGuid)
931 {
932     HRESULT rc;
933     LPDIRECTSOUND dso=NULL;
934     LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
935     DSBUFFERDESC bufdesc;
936     DSCAPS dscaps;
937     WAVEFORMATEX wfx, wfx1;
938     DWORD f, r;
939     int ref;
940     int rates[] = { 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100,
941                     48000, 96000 };
942
943     /* Create the DirectSound object */
944     rc=pDirectSoundCreate(lpGuid,&dso,NULL);
945     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
946        "DirectSoundCreate() failed: %08x\n",rc);
947     if (rc!=DS_OK)
948         return rc;
949
950     /* Get the device capabilities */
951     ZeroMemory(&dscaps, sizeof(dscaps));
952     dscaps.dwSize=sizeof(dscaps);
953     rc=IDirectSound_GetCaps(dso,&dscaps);
954     ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %08x\n",rc);
955     if (rc!=DS_OK)
956         goto EXIT;
957
958     /* We must call SetCooperativeLevel before creating primary buffer */
959     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
960     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
961     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
962     if (rc!=DS_OK)
963         goto EXIT;
964
965     ZeroMemory(&bufdesc, sizeof(bufdesc));
966     bufdesc.dwSize=sizeof(bufdesc);
967     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
968     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
969     ok(rc==DS_OK && primary!=NULL,
970        "IDirectSound_CreateSoundBuffer() failed to create a primary buffer %08x\n",rc);
971
972     if (rc==DS_OK && primary!=NULL) {
973         rc=IDirectSoundBuffer_GetFormat(primary,&wfx1,sizeof(wfx1),NULL);
974         ok(rc==DS_OK,"IDirectSoundBuffer8_Getformat() failed: %08x\n", rc);
975         if (rc!=DS_OK)
976             goto EXIT1;
977
978         for (f=0;f<sizeof(fmts)/sizeof(fmts[0]);f++) {
979         for (r=0;r<sizeof(rates)/sizeof(rates[0]);r++) {
980             init_format(&wfx,WAVE_FORMAT_PCM,11025,fmts[f].bits,
981                         fmts[f].channels);
982             secondary=NULL;
983             ZeroMemory(&bufdesc, sizeof(bufdesc));
984             bufdesc.dwSize=sizeof(bufdesc);
985             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2|DSBCAPS_CTRLFREQUENCY;
986             bufdesc.dwBufferBytes=align((wfx.nAvgBytesPerSec*rates[r]/11025)*
987                                         BUFFER_LEN/1000,wfx.nBlockAlign);
988             bufdesc.lpwfxFormat=&wfx;
989             if (winetest_interactive) {
990                 trace("  Testing a secondary buffer at %dx%dx%d "
991                       "with a primary buffer at %dx%dx%d\n",
992                       wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,
993                       wfx1.nSamplesPerSec,wfx1.wBitsPerSample,wfx1.nChannels);
994             }
995             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
996             ok(rc==DS_OK && secondary!=NULL,
997                "IDirectSound_CreateSoundBuffer() failed to create a secondary buffer %08x\n",rc);
998
999             if (rc==DS_OK && secondary!=NULL) {
1000                 test_buffer(dso,&secondary,0,FALSE,0,FALSE,0,
1001                             winetest_interactive,1.0,0,NULL,0,0,TRUE,rates[r]);
1002
1003                 ref=IDirectSoundBuffer_Release(secondary);
1004                 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
1005                    "should have 0\n",ref);
1006             }
1007         }
1008         }
1009 EXIT1:
1010         ref=IDirectSoundBuffer_Release(primary);
1011         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
1012            "should have 0\n",ref);
1013     }
1014
1015     /* Set the CooperativeLevel back to normal */
1016     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
1017     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
1018     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
1019
1020 EXIT:
1021     ref=IDirectSound_Release(dso);
1022     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
1023     if (ref!=0)
1024         return DSERR_GENERIC;
1025
1026     return rc;
1027 }
1028
1029 static unsigned int number;
1030
1031 static BOOL WINAPI dsenum_callback(LPGUID lpGuid, LPCSTR lpcstrDescription,
1032                                    LPCSTR lpcstrModule, LPVOID lpContext)
1033 {
1034     HRESULT rc;
1035     trace("*** Testing %s - %s ***\n",lpcstrDescription,lpcstrModule);
1036
1037     /* Don't test the primary device */
1038     if (!number++)
1039     {
1040         ok (!lpcstrModule[0], "lpcstrModule(%s) != NULL\n", lpcstrModule);
1041         return 1;
1042     }
1043
1044     rc = test_dsound(lpGuid);
1045     if (rc == DSERR_NODRIVER)
1046         trace("  No Driver\n");
1047     else if (rc == DSERR_ALLOCATED)
1048         trace("  Already In Use\n");
1049     else if (rc == E_FAIL)
1050         trace("  No Device\n");
1051     else {
1052         test_block_align(lpGuid);
1053         test_primary(lpGuid);
1054         test_primary_secondary(lpGuid);
1055         test_secondary(lpGuid);
1056         test_frequency(lpGuid);
1057     }
1058
1059     return 1;
1060 }
1061
1062 static void dsound_tests(void)
1063 {
1064     HRESULT rc;
1065     rc=pDirectSoundEnumerateA(&dsenum_callback,NULL);
1066     ok(rc==DS_OK,"DirectSoundEnumerateA() failed: %08x\n",rc);
1067 }
1068
1069 START_TEST(dsound)
1070 {
1071     HMODULE hDsound;
1072
1073     CoInitialize(NULL);
1074
1075     hDsound = LoadLibrary("dsound.dll");
1076     if (hDsound)
1077     {
1078         BOOL ret;
1079
1080         ok( FreeLibrary(hDsound), "FreeLibrary(1) returned %d\n", GetLastError());
1081         SetLastError(0xdeadbeef);
1082         ret = FreeLibrary(hDsound);
1083         ok( ret ||
1084             broken(!ret && GetLastError() == ERROR_MOD_NOT_FOUND) || /* NT4 */
1085             broken(!ret && GetLastError() == ERROR_INVALID_HANDLE),  /* Win9x */
1086             "FreeLibrary(2) returned %d\n", GetLastError());
1087         ok(!FreeLibrary(hDsound), "DirectSound DLL still loaded\n");
1088     }
1089
1090     hDsound = LoadLibrary("dsound.dll");
1091     if (hDsound)
1092     {
1093
1094         pDirectSoundEnumerateA = (void*)GetProcAddress(hDsound,
1095             "DirectSoundEnumerateA");
1096         pDirectSoundCreate = (void*)GetProcAddress(hDsound,
1097             "DirectSoundCreate");
1098
1099         gotdx8 = !!GetProcAddress(hDsound, "DirectSoundCreate8");
1100
1101         IDirectSound_tests();
1102         dsound_tests();
1103
1104         FreeLibrary(hDsound);
1105     }
1106     else
1107         skip("dsound.dll not found!\n");
1108
1109     CoUninitialize();
1110 }