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
31 #include "wine/test.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*,
49 static void IDirectSound_test(LPDIRECTSOUND dso, BOOL initialized,
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);
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);
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);
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");
101 } else if (rc==E_FAIL) {
102 trace(" No Device\n");
104 } else if (rc==DSERR_ALLOCATED) {
105 trace(" Already In Use\n");
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);
155 rc=IDirectSound_SetSpeakerConfig(dso,speaker_config);
156 ok(rc==DS_OK,"IDirectSound_SetSpeakerConfig() failed: %08x\n", rc);
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);
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)
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);
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);
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);
212 IDirectSound_test(dso, FALSE, &DSDEVID_DefaultVoicePlayback);
214 /* try the COM class factory method of creation with a bad
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);
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)
262 LPDIRECTSOUND dso=NULL;
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);
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);
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);
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);
297 /* Release the second DirectSound object */
298 ref=IDirectSound_Release(dso1);
299 ok(ref==0,"IDirectSound_Release() has %d references, should have "
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",
309 return DSERR_GENERIC;
313 /* Create a DirectSound object */
314 rc=pDirectSoundCreate(lpGuid,&dso,NULL);
315 ok(rc==DS_OK,"DirectSoundCreate() failed: %08x\n",rc);
317 LPDIRECTSOUNDBUFFER secondary;
318 DSBUFFERDESC bufdesc;
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,
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 "
332 if (rc==DS_OK && secondary!=NULL) {
333 LPDIRECTSOUND3DBUFFER buffer3d;
334 rc=IDirectSound_QueryInterface(secondary, &IID_IDirectSound3DBuffer,
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",
352 return DSERR_GENERIC;
359 static HRESULT test_primary(LPGUID lpGuid)
362 LPDIRECTSOUND dso=NULL;
363 LPDIRECTSOUNDBUFFER primary=NULL,second=NULL,third=NULL;
364 DSBUFFERDESC bufdesc;
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);
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);
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 */
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 */
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 */
421 ZeroMemory(&bufdesc, sizeof(bufdesc));
422 bufdesc.dwSize=sizeof(bufdesc);
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);
437 /* Testing the primary buffer */
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);
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) {
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);
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 "
487 trace("(the current volume is %d according to DirectSound)\n",
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);
506 ref=IDirectSound_Release(dso);
507 ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
509 return DSERR_GENERIC;
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)
521 LPDIRECTSOUND dso=NULL;
522 LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
523 DSBUFFERDESC bufdesc;
525 WAVEFORMATEX wfx, wfx2;
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);
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);
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);
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))
564 /* We must call SetCooperativeLevel to be allowed to call
566 /* DSOUND: Setting DirectSound cooperative level to
568 rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
569 ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %08x\n", rc);
573 init_format(&wfx,format_tags[tag],formats[f][0],formats[f][1],
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);
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);
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);
613 ZeroMemory(&bufdesc, sizeof(bufdesc));
614 bufdesc.dwSize=sizeof(bufdesc);
615 bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
616 bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
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);
651 ref=IDirectSound_Release(dso);
652 ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
654 return DSERR_GENERIC;
659 static HRESULT test_secondary(LPGUID lpGuid)
662 LPDIRECTSOUND dso=NULL;
663 LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
664 DSBUFFERDESC bufdesc;
666 WAVEFORMATEX wfx, wfx1;
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);
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);
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);
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);
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))
713 init_format(&wfx,format_tags[tag],formats[f][0],formats[f][1],
716 ZeroMemory(&bufdesc, sizeof(bufdesc));
717 bufdesc.dwSize=sizeof(bufdesc);
718 bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
719 bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
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);
728 ZeroMemory(&bufdesc, sizeof(bufdesc));
729 bufdesc.dwSize=sizeof(bufdesc);
730 bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
731 bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
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);
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);
748 ok(rc==E_INVALIDARG, "Creating %d bpp buffer on dx < 8 returned: %p %08x\n",
749 wfx.wBitsPerSample, secondary, rc);
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
761 IDirectSoundBuffer_Release(secondary);
764 bufdesc.lpwfxFormat=(WAVEFORMATEX*)&wfxe;
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",
778 IDirectSoundBuffer_Release(secondary);
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)
787 || rc==DS_OK, /* 2003 / 2008 */
788 "IDirectSound_CreateSoundBuffer() returned: %08x %p\n",
792 IDirectSoundBuffer_Release(secondary);
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",
804 IDirectSoundBuffer_Release(secondary);
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",
816 IDirectSoundBuffer_Release(secondary);
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",
828 IDirectSoundBuffer_Release(secondary);
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);
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);
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);
866 ref=IDirectSound_Release(dso);
867 ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
869 return DSERR_GENERIC;
874 static HRESULT test_block_align(LPGUID lpGuid)
877 LPDIRECTSOUND dso=NULL;
878 LPDIRECTSOUNDBUFFER secondary=NULL;
879 DSBUFFERDESC bufdesc;
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);
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);
933 return DSERR_GENERIC;
941 } fmts[] = { { 8, 1 }, { 8, 2 }, { 16, 1 }, {16, 2 } };
943 static HRESULT test_frequency(LPGUID lpGuid)
946 LPDIRECTSOUND dso=NULL;
947 LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
948 DSBUFFERDESC bufdesc;
950 WAVEFORMATEX wfx, wfx1;
953 int rates[] = { 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100,
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);
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);
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);
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);
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,
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);
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);
1034 ref=IDirectSound_Release(dso);
1035 ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
1037 return DSERR_GENERIC;
1042 static HRESULT test_notify(LPDIRECTSOUNDBUFFER dsb,
1043 DWORD count, LPHANDLE event,
1049 rc=IDirectSoundBuffer_SetCurrentPosition(dsb,0);
1051 "IDirectSoundBuffer_SetCurrentPosition failed %08x\n",rc);
1055 rc=IDirectSoundBuffer_Play(dsb,0,0,0);
1056 ok(rc==DS_OK,"IDirectSoundBuffer_Play failed %08x\n",rc);
1060 rc=IDirectSoundBuffer_Stop(dsb);
1061 ok(rc==DS_OK,"IDirectSoundBuffer_Stop failed %08x\n",rc);
1065 ret=WaitForMultipleObjects(count,event,FALSE,0);
1066 ok(ret==expected,"expected %d. got %d\n",expected,ret);
1070 static HRESULT test_duplicate(LPGUID lpGuid)
1073 LPDIRECTSOUND dso=NULL;
1074 LPDIRECTSOUNDBUFFER primary=NULL;
1075 DSBUFFERDESC bufdesc;
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);
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);
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);
1099 if (rc==DS_OK && primary!=NULL) {
1100 LPDIRECTSOUNDBUFFER original=NULL;
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;
1121 /* Prepare notify events */
1122 for (i=0;i<sizeof(event)/sizeof(event[0]);i++) {
1123 event[i] = CreateEvent(NULL,FALSE,FALSE,NULL);
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,
1135 ok(rc==DS_OK,"IDirectSoundBuffer_Unlock failed to unlock "
1139 rc=IDirectSoundBuffer_QueryInterface(original,
1140 &IID_IDirectSoundNotify,
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;
1149 dsbpn.dwOffset=DSBPN_OFFSETSTOP;
1150 dsbpn.hEventNotify=event[0];
1151 rc=IDirectSoundNotify_SetNotificationPositions(notify,
1153 ok(rc==DS_OK,"IDirectSoundNotify_SetNotificationPositions "
1154 "failed %08x\n",rc);
1156 rc=IDirectSound_DuplicateSoundBuffer(dso,original,&duplicated);
1157 ok(rc==DS_OK && duplicated!=NULL,
1158 "IDirectSound_DuplicateSoundBuffer failed %08x\n",rc);
1160 trace("testing duplicated buffer without notifications.\n");
1161 test_notify(duplicated,sizeof(event)/sizeof(event[0]),
1162 event,WAIT_TIMEOUT);
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,
1175 ok(rc==DS_OK,"IDirectSoundNotify_SetNotificationPositions "
1176 "failed %08x\n",rc);
1178 trace("testing duplicated buffer with a notification.\n");
1179 test_notify(duplicated,sizeof(event)/sizeof(event[0]),
1180 event,WAIT_OBJECT_0+1);
1182 ref=IDirectSoundNotify_Release(dup_notify);
1183 ok(ref==0,"IDirectSoundNotify_Release() has %d references, "
1184 "should have 0\n",ref);
1186 ref=IDirectSoundNotify_Release(notify);
1187 ok(ref==0,"IDirectSoundNotify_Release() has %d references, "
1188 "should have 0\n",ref);
1190 trace("testing original buffer with a notification.\n");
1191 test_notify(original,sizeof(event)/sizeof(event[0]),
1192 event,WAIT_OBJECT_0);
1194 ref=IDirectSoundBuffer_Release(duplicated);
1195 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
1196 "should have 0\n",ref);
1198 ref=IDirectSoundBuffer_Release(original);
1199 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
1200 "should have 0\n",ref);
1202 ref=IDirectSoundBuffer_Release(primary);
1203 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
1204 "should have 0\n",ref);
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);
1213 ref=IDirectSound_Release(dso);
1214 ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
1216 return DSERR_GENERIC;
1221 static unsigned int number;
1223 static BOOL WINAPI dsenum_callback(LPGUID lpGuid, LPCSTR lpcstrDescription,
1224 LPCSTR lpcstrModule, LPVOID lpContext)
1227 trace("*** Testing %s - %s ***\n",lpcstrDescription,lpcstrModule);
1229 /* Don't test the primary device */
1232 ok (!lpcstrModule[0], "lpcstrModule(%s) != NULL\n", lpcstrModule);
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");
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);
1255 static void dsound_tests(void)
1258 rc=pDirectSoundEnumerateA(&dsenum_callback,NULL);
1259 ok(rc==DS_OK,"DirectSoundEnumerateA() failed: %08x\n",rc);
1268 hDsound = LoadLibrary("dsound.dll");
1273 ret = FreeLibrary(hDsound);
1274 ok( ret, "FreeLibrary(1) returned %d\n", GetLastError());
1275 SetLastError(0xdeadbeef);
1276 ret = FreeLibrary(hDsound);
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");
1283 hDsound = LoadLibrary("dsound.dll");
1287 pDirectSoundEnumerateA = (void*)GetProcAddress(hDsound,
1288 "DirectSoundEnumerateA");
1289 pDirectSoundCreate = (void*)GetProcAddress(hDsound,
1290 "DirectSoundCreate");
1292 gotdx8 = !!GetProcAddress(hDsound, "DirectSoundCreate8");
1294 IDirectSound_tests();
1297 FreeLibrary(hDsound);
1300 win_skip("dsound.dll not found!\n");