wininet: Support the Cache-Control max-age directive for setting url cache entry...
[wine/testsucceed.git] / dlls / dsound / tests / dsound.c
blobf8b7aeb8bb4c449a2194506c82116d33c57fe4fd
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.
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.
11 * Copyright (c) 2002-2004 Francois Gouget
12 * Copyright (c) 2007 Maarten Lankhorst
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.
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.
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
29 #include <windows.h>
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"
39 #include "dsound_test.h"
41 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
43 static HRESULT (WINAPI *pDirectSoundEnumerateA)(LPDSENUMCALLBACKA,LPVOID)=NULL;
44 static HRESULT (WINAPI *pDirectSoundCreate)(LPCGUID,LPDIRECTSOUND*,
45 LPUNKNOWN)=NULL;
47 static BOOL gotdx8;
49 static void IDirectSound_test(LPDIRECTSOUND dso, BOOL initialized,
50 LPCGUID lpGuid)
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;
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);
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);
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);
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);
83 rc=IDirectSound_GetCaps(dso,&dscaps);
84 ok(rc==DSERR_UNINITIALIZED,"IDirectSound_GetCaps() "
85 "should have returned DSERR_UNINITIALIZED, returned: %08x\n", rc);
87 rc=IDirectSound_Compact(dso);
88 ok(rc==DSERR_UNINITIALIZED,"IDirectSound_Compact() "
89 "should have returned DSERR_UNINITIALIZED, returned: %08x\n", rc);
91 rc=IDirectSound_GetSpeakerConfig(dso,&speaker_config);
92 ok(rc==DSERR_UNINITIALIZED,"IDirectSound_GetSpeakerConfig() "
93 "should have returned DSERR_UNINITIALIZED, returned: %08x\n", rc);
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;
110 rc=IDirectSound_Initialize(dso,lpGuid);
111 ok(rc==DSERR_ALREADYINITIALIZED, "IDirectSound_Initialize() "
112 "should have returned DSERR_ALREADYINITIALIZED: %08x\n", rc);
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);
119 ZeroMemory(&dscaps, sizeof(dscaps));
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);
126 dscaps.dwSize=sizeof(dscaps);
128 /* DSOUND: Running on a certified driver */
129 rc=IDirectSound_GetCaps(dso,&dscaps);
130 ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %08x\n",rc);
132 rc=IDirectSound_Compact(dso);
133 ok(rc==DSERR_PRIOLEVELNEEDED,"IDirectSound_Compact() failed: %08x\n", rc);
135 rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
136 ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
138 rc=IDirectSound_Compact(dso);
139 ok(rc==DS_OK,"IDirectSound_Compact() failed: %08x\n",rc);
141 rc=IDirectSound_GetSpeakerConfig(dso,0);
142 ok(rc==DSERR_INVALIDPARAM,"IDirectSound_GetSpeakerConfig(NULL) "
143 "should have returned DSERR_INVALIDPARAM, returned: %08x\n", rc);
145 rc=IDirectSound_GetSpeakerConfig(dso,&speaker_config);
146 ok(rc==DS_OK,"IDirectSound_GetSpeakerConfig() failed: %08x\n", rc);
147 ref_speaker_config = speaker_config;
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);
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);
168 EXIT:
169 ref=IDirectSound_Release(dso);
170 ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
173 static void IDirectSound_tests(void)
175 HRESULT rc;
176 LPDIRECTSOUND dso=NULL;
177 LPCLASSFACTORY cf=NULL;
179 trace("Testing IDirectSound\n");
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
259 static HRESULT test_dsound(LPGUID lpGuid)
261 HRESULT rc;
262 LPDIRECTSOUND dso=NULL;
263 int ref;
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);
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;
277 /* Try the enumerated device */
278 IDirectSound_test(dso, TRUE, lpGuid);
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);
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;
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);
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;
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;
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);
343 ref=IDirectSoundBuffer_AddRef(secondary);
344 ok(ref==2,"IDirectSoundBuffer_AddRef() has %d references, "
345 "should have 2\n",ref);
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;
356 return DS_OK;
359 static HRESULT test_primary(LPGUID lpGuid)
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;
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;
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;
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);
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);
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);
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);
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);
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;
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);
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;
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);
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);
480 rc=IDirectSoundBuffer_GetVolume(primary,&vol);
481 ok(rc==DS_OK,"IDirectSoundBuffer_GetVolume() failed: %08x\n", rc);
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");
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);
495 ref=IDirectSoundBuffer_Release(primary);
496 ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
497 "should have 0\n",ref);
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);
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;
511 return rc;
515 * Test the primary buffer at different formats while keeping the
516 * secondary buffer at a constant format.
518 static HRESULT test_primary_secondary(LPGUID lpGuid)
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;
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;
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;
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;
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);
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;
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;
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);
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);
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.
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);
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);
610 init_format(&wfx2,WAVE_FORMAT_PCM,11025,16,2);
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);
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);
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);
633 ref=IDirectSoundBuffer_Release(secondary);
634 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
635 "should have 0\n",ref);
640 ref=IDirectSoundBuffer_Release(primary);
641 ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
642 "should have 0\n",ref);
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);
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;
656 return rc;
659 static HRESULT test_secondary(LPGUID lpGuid)
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;
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;
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;
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;
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);
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;
705 for (f=0;f<NB_FORMATS;f++) {
706 for (tag=0;tag<NB_TAGS;tag++) {
707 WAVEFORMATEXTENSIBLE wfxe;
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;
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);
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)
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);
747 else
748 ok(rc==E_INVALIDARG, "Creating %d bpp buffer on dx < 8 returned: %p %08x\n",
749 wfx.wBitsPerSample, secondary, rc);
751 if (!gotdx8)
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
757 goto no_wfe;
760 if (secondary)
761 IDirectSoundBuffer_Release(secondary);
762 secondary = NULL;
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);
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)
778 IDirectSoundBuffer_Release(secondary);
779 secondary=NULL;
782 wfxe.Format.cbSize = sizeof(wfxe) - sizeof(wfx) + 1;
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)
792 IDirectSoundBuffer_Release(secondary);
793 secondary=NULL;
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)
804 IDirectSoundBuffer_Release(secondary);
805 secondary=NULL;
807 wfxe.SubFormat = (format_tags[tag] == WAVE_FORMAT_PCM ? KSDATAFORMAT_SUBTYPE_PCM : KSDATAFORMAT_SUBTYPE_IEEE_FLOAT);
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)
816 IDirectSoundBuffer_Release(secondary);
817 secondary=NULL;
819 --wfxe.Samples.wValidBitsPerSample;
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)
828 IDirectSoundBuffer_Release(secondary);
829 secondary=NULL;
831 wfxe.Samples.wValidBitsPerSample = wfxe.Format.wBitsPerSample;
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);
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);
845 test_buffer(dso,&secondary,0,FALSE,0,FALSE,0,
846 winetest_interactive,1.0,0,NULL,0,0,FALSE,0);
848 ref=IDirectSoundBuffer_Release(secondary);
849 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
850 "should have 0\n",ref);
854 EXIT1:
855 ref=IDirectSoundBuffer_Release(primary);
856 ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
857 "should have 0\n",ref);
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);
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;
871 return rc;
874 static HRESULT test_block_align(LPGUID lpGuid)
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;
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;
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);
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)
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);
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);
925 ref=IDirectSoundBuffer_Release(secondary);
926 ok(ref==0,"IDirectSoundBuffer_Release() secondary has %d references, "
927 "should have 0\n",ref);
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;
935 return rc;
938 static struct fmt {
939 int bits;
940 int channels;
941 } fmts[] = { { 8, 1 }, { 8, 2 }, { 16, 1 }, {16, 2 } };
943 static HRESULT test_frequency(LPGUID lpGuid)
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 };
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;
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;
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;
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);
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;
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);
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);
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]);
1016 ref=IDirectSoundBuffer_Release(secondary);
1017 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
1018 "should have 0\n",ref);
1022 EXIT1:
1023 ref=IDirectSoundBuffer_Release(primary);
1024 ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
1025 "should have 0\n",ref);
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);
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;
1039 return rc;
1042 static unsigned int number;
1044 static BOOL WINAPI dsenum_callback(LPGUID lpGuid, LPCSTR lpcstrDescription,
1045 LPCSTR lpcstrModule, LPVOID lpContext)
1047 HRESULT rc;
1048 trace("*** Testing %s - %s ***\n",lpcstrDescription,lpcstrModule);
1050 /* Don't test the primary device */
1051 if (!number++)
1053 ok (!lpcstrModule[0], "lpcstrModule(%s) != NULL\n", lpcstrModule);
1054 return 1;
1057 rc = test_dsound(lpGuid);
1058 if (rc == DSERR_NODRIVER)
1059 trace(" No Driver\n");
1060 else if (rc == DSERR_ALLOCATED)
1061 trace(" Already In Use\n");
1062 else if (rc == E_FAIL)
1063 trace(" No Device\n");
1064 else {
1065 test_block_align(lpGuid);
1066 test_primary(lpGuid);
1067 test_primary_secondary(lpGuid);
1068 test_secondary(lpGuid);
1069 test_frequency(lpGuid);
1072 return 1;
1075 static void dsound_tests(void)
1077 HRESULT rc;
1078 rc=pDirectSoundEnumerateA(&dsenum_callback,NULL);
1079 ok(rc==DS_OK,"DirectSoundEnumerateA() failed: %08x\n",rc);
1082 START_TEST(dsound)
1084 HMODULE hDsound;
1086 CoInitialize(NULL);
1088 hDsound = LoadLibrary("dsound.dll");
1089 if (hDsound)
1091 BOOL ret;
1093 ret = FreeLibrary(hDsound);
1094 ok( ret, "FreeLibrary(1) returned %d\n", GetLastError());
1095 SetLastError(0xdeadbeef);
1096 ret = FreeLibrary(hDsound);
1097 ok( ret ||
1098 broken(!ret && GetLastError() == ERROR_MOD_NOT_FOUND), /* NT4 */
1099 "FreeLibrary(2) returned %d\n", GetLastError());
1100 ok(!FreeLibrary(hDsound), "DirectSound DLL still loaded\n");
1103 hDsound = LoadLibrary("dsound.dll");
1104 if (hDsound)
1107 pDirectSoundEnumerateA = (void*)GetProcAddress(hDsound,
1108 "DirectSoundEnumerateA");
1109 pDirectSoundCreate = (void*)GetProcAddress(hDsound,
1110 "DirectSoundCreate");
1112 gotdx8 = !!GetProcAddress(hDsound, "DirectSoundCreate8");
1114 IDirectSound_tests();
1115 dsound_tests();
1117 FreeLibrary(hDsound);
1119 else
1120 win_skip("dsound.dll not found!\n");
1122 CoUninitialize();