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
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/test.h"
35 #include "dsound_test.h"
37 static void IDirectSound_test(LPDIRECTSOUND dso, BOOL initialized,
46 DWORD speaker_config, new_speaker_config;
48 /* Try to Query for objects */
49 rc=IDirectSound_QueryInterface(dso,&IID_IUnknown,(LPVOID*)&unknown);
50 ok(rc==DS_OK,"IDirectSound_QueryInterface(IID_IUnknown) failed: %s\n",
51 DXGetErrorString8(rc));
53 IDirectSound_Release(unknown);
55 rc=IDirectSound_QueryInterface(dso,&IID_IDirectSound,(LPVOID*)&ds);
56 ok(rc==DS_OK,"IDirectSound_QueryInterface(IID_IDirectSound) failed: %s\n",
57 DXGetErrorString8(rc));
59 IDirectSound_Release(ds);
61 rc=IDirectSound_QueryInterface(dso,&IID_IDirectSound8,(LPVOID*)&ds8);
62 ok(rc==E_NOINTERFACE,"IDirectSound_QueryInterface(IID_IDirectSound8) "
63 "should have failed: %s\n",DXGetErrorString8(rc));
65 IDirectSound8_Release(ds8);
67 if (initialized == FALSE) {
68 /* try uninitialized object */
69 rc=IDirectSound_GetCaps(dso,0);
70 ok(rc==DSERR_UNINITIALIZED,"IDirectSound_GetCaps(NULL) "
71 "should have returned DSERR_UNINITIALIZED, returned: %s\n",
72 DXGetErrorString8(rc));
74 rc=IDirectSound_GetCaps(dso,&dscaps);
75 ok(rc==DSERR_UNINITIALIZED,"IDirectSound_GetCaps() "
76 "should have returned DSERR_UNINITIALIZED, returned: %s\n",
77 DXGetErrorString8(rc));
79 rc=IDirectSound_Compact(dso);
80 ok(rc==DSERR_UNINITIALIZED,"IDirectSound_Compact() "
81 "should have returned DSERR_UNINITIALIZED, returned: %s\n",
82 DXGetErrorString8(rc));
84 rc=IDirectSound_GetSpeakerConfig(dso,&speaker_config);
85 ok(rc==DSERR_UNINITIALIZED,"IDirectSound_GetSpeakerConfig() "
86 "should have returned DSERR_UNINITIALIZED, returned: %s\n",
87 DXGetErrorString8(rc));
89 rc=IDirectSound_Initialize(dso,lpGuid);
90 ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
91 "IDirectSound_Initialize() failed: %s\n",DXGetErrorString8(rc));
92 if (rc==DSERR_NODRIVER) {
93 trace(" No Driver\n");
95 } else if (rc==E_FAIL) {
96 trace(" No Device\n");
98 } else if (rc==DSERR_ALLOCATED) {
99 trace(" Already In Use\n");
104 rc=IDirectSound_Initialize(dso,lpGuid);
105 ok(rc==DSERR_ALREADYINITIALIZED, "IDirectSound_Initialize() "
106 "should have returned DSERR_ALREADYINITIALIZED: %s\n",
107 DXGetErrorString8(rc));
109 /* DSOUND: Error: Invalid caps buffer */
110 rc=IDirectSound_GetCaps(dso,0);
111 ok(rc==DSERR_INVALIDPARAM,"IDirectSound_GetCaps(NULL) "
112 "should have returned DSERR_INVALIDPARAM, returned: %s\n",
113 DXGetErrorString8(rc));
115 ZeroMemory(&dscaps, sizeof(dscaps));
117 /* DSOUND: Error: Invalid caps buffer */
118 rc=IDirectSound_GetCaps(dso,&dscaps);
119 ok(rc==DSERR_INVALIDPARAM,"IDirectSound_GetCaps() "
120 "should have returned DSERR_INVALIDPARAM, returned: %s\n",
121 DXGetErrorString8(rc));
123 dscaps.dwSize=sizeof(dscaps);
125 /* DSOUND: Running on a certified driver */
126 rc=IDirectSound_GetCaps(dso,&dscaps);
127 ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %s\n",DXGetErrorString8(rc));
129 rc=IDirectSound_Compact(dso);
130 ok(rc==DSERR_PRIOLEVELNEEDED,"IDirectSound_Compact() failed: %s\n",
131 DXGetErrorString8(rc));
133 rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
134 ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
135 DXGetErrorString8(rc));
137 rc=IDirectSound_Compact(dso);
138 ok(rc==DS_OK,"IDirectSound_Compact() failed: %s\n",DXGetErrorString8(rc));
140 rc=IDirectSound_GetSpeakerConfig(dso,0);
141 ok(rc==DSERR_INVALIDPARAM,"IDirectSound_GetSpeakerConfig(NULL) "
142 "should have returned DSERR_INVALIDPARAM, returned: %s\n",
143 DXGetErrorString8(rc));
145 rc=IDirectSound_GetSpeakerConfig(dso,&speaker_config);
146 ok(rc==DS_OK,"IDirectSound_GetSpeakerConfig() failed: %s\n",
147 DXGetErrorString8(rc));
149 speaker_config = DSSPEAKER_COMBINED(DSSPEAKER_STEREO,
150 DSSPEAKER_GEOMETRY_WIDE);
151 rc=IDirectSound_SetSpeakerConfig(dso,speaker_config);
152 ok(rc==DS_OK,"IDirectSound_SetSpeakerConfig() failed: %s\n",
153 DXGetErrorString8(rc));
155 rc=IDirectSound_GetSpeakerConfig(dso,&new_speaker_config);
156 ok(rc==DS_OK,"IDirectSound_GetSpeakerConfig() failed: %s\n",
157 DXGetErrorString8(rc));
158 if (rc==DS_OK && speaker_config!=new_speaker_config)
159 trace("IDirectSound_GetSpeakerConfig() failed to set speaker "
160 "config: expected 0x%08x, got 0x%08x\n",
161 speaker_config,new_speaker_config);
165 ref=IDirectSound_Release(dso);
166 ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
169 static void IDirectSound_tests(void)
172 LPDIRECTSOUND dso=NULL;
173 LPCLASSFACTORY cf=NULL;
175 trace("Testing IDirectSound\n");
177 rc=CoGetClassObject(&CLSID_DirectSound, CLSCTX_INPROC_SERVER, NULL,
178 &IID_IClassFactory, (void**)&cf);
179 ok(rc==S_OK,"CoGetClassObject(CLSID_DirectSound, IID_IClassFactory) "
180 "failed: %s\n", DXGetErrorString8(rc));
182 rc=CoGetClassObject(&CLSID_DirectSound, CLSCTX_INPROC_SERVER, NULL,
183 &IID_IUnknown, (void**)&cf);
184 ok(rc==S_OK,"CoGetClassObject(CLSID_DirectSound, IID_IUnknown) "
185 "failed: %s\n", DXGetErrorString8(rc));
187 /* try the COM class factory method of creation with no device specified */
188 rc=CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
189 &IID_IDirectSound, (void**)&dso);
190 ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSound) failed: %s\n",
191 DXGetErrorString8(rc));
193 IDirectSound_test(dso, FALSE, NULL);
195 /* try the COM class factory method of creation with default playback
196 * device specified */
197 rc=CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
198 &IID_IDirectSound, (void**)&dso);
199 ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSound) failed: %s\n",
200 DXGetErrorString8(rc));
202 IDirectSound_test(dso, FALSE, &DSDEVID_DefaultPlayback);
204 /* try the COM class factory method of creation with default voice
205 * playback device specified */
206 rc=CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
207 &IID_IDirectSound, (void**)&dso);
208 ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSound) failed: %s\n",
209 DXGetErrorString8(rc));
211 IDirectSound_test(dso, FALSE, &DSDEVID_DefaultVoicePlayback);
213 /* try the COM class factory method of creation with a bad
215 rc=CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
216 &CLSID_DirectSoundPrivate, (void**)&dso);
217 ok(rc==E_NOINTERFACE,
218 "CoCreateInstance(CLSID_DirectSound,CLSID_DirectSoundPrivate) "
219 "should have failed: %s\n",DXGetErrorString8(rc));
221 /* try the COM class factory method of creation with a bad
222 * GUID and IID specified */
223 rc=CoCreateInstance(&CLSID_DirectSoundPrivate, NULL, CLSCTX_INPROC_SERVER,
224 &IID_IDirectSound, (void**)&dso);
225 ok(rc==REGDB_E_CLASSNOTREG,
226 "CoCreateInstance(CLSID_DirectSoundPrivate,IID_IDirectSound) "
227 "should have failed: %s\n",DXGetErrorString8(rc));
229 /* try with no device specified */
230 rc=DirectSoundCreate(NULL,&dso,NULL);
231 ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
232 "DirectSoundCreate(NULL) failed: %s\n",DXGetErrorString8(rc));
234 IDirectSound_test(dso, TRUE, NULL);
236 /* try with default playback device specified */
237 rc=DirectSoundCreate(&DSDEVID_DefaultPlayback,&dso,NULL);
238 ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
239 "DirectSoundCreate(DSDEVID_DefaultPlayback) failed: %s\n",
240 DXGetErrorString8(rc));
241 if (rc==DS_OK && dso)
242 IDirectSound_test(dso, TRUE, NULL);
244 /* try with default voice playback device specified */
245 rc=DirectSoundCreate(&DSDEVID_DefaultVoicePlayback,&dso,NULL);
246 ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
247 "DirectSoundCreate(DSDEVID_DefaultVoicePlayback) failed: %s\n",
248 DXGetErrorString8(rc));
249 if (rc==DS_OK && dso)
250 IDirectSound_test(dso, TRUE, NULL);
252 /* try with a bad device specified */
253 rc=DirectSoundCreate(&DSDEVID_DefaultVoiceCapture,&dso,NULL);
254 ok(rc==DSERR_NODRIVER,"DirectSoundCreate(DSDEVID_DefaultVoiceCapture) "
255 "should have failed: %s\n",DXGetErrorString8(rc));
256 if (rc==DS_OK && dso)
257 IDirectSound_Release(dso);
260 static HRESULT test_dsound(LPGUID lpGuid)
263 LPDIRECTSOUND dso=NULL;
266 /* DSOUND: Error: Invalid interface buffer */
267 rc=DirectSoundCreate(lpGuid,0,NULL);
268 ok(rc==DSERR_INVALIDPARAM,"DirectSoundCreate() should have returned "
269 "DSERR_INVALIDPARAM, returned: %s\n",DXGetErrorString8(rc));
271 /* Create the DirectSound object */
272 rc=DirectSoundCreate(lpGuid,&dso,NULL);
273 ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
274 "DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
278 /* Try the enumerated device */
279 IDirectSound_test(dso, TRUE, lpGuid);
281 /* Try the COM class factory method of creation with enumerated device */
282 rc=CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
283 &IID_IDirectSound, (void**)&dso);
284 ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSound) failed: %s\n",
285 DXGetErrorString8(rc));
287 IDirectSound_test(dso, FALSE, lpGuid);
289 /* Create a DirectSound object */
290 rc=DirectSoundCreate(lpGuid,&dso,NULL);
291 ok(rc==DS_OK,"DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
293 LPDIRECTSOUND dso1=NULL;
295 /* Create a second DirectSound object */
296 rc=DirectSoundCreate(lpGuid,&dso1,NULL);
297 ok(rc==DS_OK,"DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
299 /* Release the second DirectSound object */
300 ref=IDirectSound_Release(dso1);
301 ok(ref==0,"IDirectSound_Release() has %d references, should have "
303 ok(dso!=dso1,"DirectSound objects should be unique: dso=%p,dso1=%p\n",dso,dso1);
306 /* Release the first DirectSound object */
307 ref=IDirectSound_Release(dso);
308 ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",
311 return DSERR_GENERIC;
315 /* Create a DirectSound object */
316 rc=DirectSoundCreate(lpGuid,&dso,NULL);
317 ok(rc==DS_OK,"DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
319 LPDIRECTSOUNDBUFFER secondary;
320 DSBUFFERDESC bufdesc;
323 init_format(&wfx,WAVE_FORMAT_PCM,11025,8,1);
324 ZeroMemory(&bufdesc, sizeof(bufdesc));
325 bufdesc.dwSize=sizeof(bufdesc);
326 bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_CTRL3D;
327 bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
329 bufdesc.lpwfxFormat=&wfx;
330 rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
331 ok(rc==DS_OK && secondary!=NULL,
332 "IDirectSound_CreateSoundBuffer() failed to create a secondary "
333 "buffer %s\n",DXGetErrorString8(rc));
334 if (rc==DS_OK && secondary!=NULL) {
335 LPDIRECTSOUND3DBUFFER buffer3d;
336 rc=IDirectSound_QueryInterface(secondary, &IID_IDirectSound3DBuffer,
338 ok(rc==DS_OK && buffer3d!=NULL,"IDirectSound_QueryInterface() "
339 "failed: %s\n",DXGetErrorString8(rc));
340 if (rc==DS_OK && buffer3d!=NULL) {
341 ref=IDirectSound3DBuffer_AddRef(buffer3d);
342 ok(ref==2,"IDirectSound3DBuffer_AddRef() has %d references, "
343 "should have 2\n",ref);
345 ref=IDirectSoundBuffer_AddRef(secondary);
346 ok(ref==2,"IDirectSoundBuffer_AddRef() has %d references, "
347 "should have 2\n",ref);
349 /* release with buffer */
350 ref=IDirectSound_Release(dso);
351 ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",
354 return DSERR_GENERIC;
361 static HRESULT test_primary(LPGUID lpGuid)
364 LPDIRECTSOUND dso=NULL;
365 LPDIRECTSOUNDBUFFER primary=NULL,second=NULL,third=NULL;
366 DSBUFFERDESC bufdesc;
371 /* Create the DirectSound object */
372 rc=DirectSoundCreate(lpGuid,&dso,NULL);
373 ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
374 "DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
378 /* Get the device capabilities */
379 ZeroMemory(&dscaps, sizeof(dscaps));
380 dscaps.dwSize=sizeof(dscaps);
381 rc=IDirectSound_GetCaps(dso,&dscaps);
382 ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %s\n",DXGetErrorString8(rc));
386 /* DSOUND: Error: Invalid buffer description pointer */
387 rc=IDirectSound_CreateSoundBuffer(dso,0,0,NULL);
388 ok(rc==DSERR_INVALIDPARAM,
389 "IDirectSound_CreateSoundBuffer() should have failed: %s\n",
390 DXGetErrorString8(rc));
392 /* DSOUND: Error: Invalid buffer description pointer */
393 rc=IDirectSound_CreateSoundBuffer(dso,0,&primary,NULL);
394 ok(rc==DSERR_INVALIDPARAM && primary==0,
395 "IDirectSound_CreateSoundBuffer() should have failed: rc=%s,"
396 "dsbo=%p\n",DXGetErrorString8(rc),primary);
398 /* DSOUND: Error: Invalid buffer description pointer */
399 rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,0,NULL);
400 ok(rc==DSERR_INVALIDPARAM && primary==0,
401 "IDirectSound_CreateSoundBuffer() should have failed: rc=%s,"
402 "dsbo=0x%p\n",DXGetErrorString8(rc),primary);
404 ZeroMemory(&bufdesc, sizeof(bufdesc));
406 /* DSOUND: Error: Invalid size */
407 /* DSOUND: Error: Invalid buffer description */
408 rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
409 ok(rc==DSERR_INVALIDPARAM && primary==0,
410 "IDirectSound_CreateSoundBuffer() should have failed: rc=%s,"
411 "primary=%p\n",DXGetErrorString8(rc),primary);
413 /* We must call SetCooperativeLevel before calling CreateSoundBuffer */
414 /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
415 rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
416 ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
417 DXGetErrorString8(rc));
421 /* Testing the primary buffer */
423 ZeroMemory(&bufdesc, sizeof(bufdesc));
424 bufdesc.dwSize=sizeof(bufdesc);
425 bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRLVOLUME;
426 bufdesc.lpwfxFormat = &wfx;
427 init_format(&wfx,WAVE_FORMAT_PCM,11025,8,2);
428 rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
429 ok(rc==DSERR_INVALIDPARAM,"IDirectSound_CreateSoundBuffer() should have "
430 "returned DSERR_INVALIDPARAM, returned: %s\n", DXGetErrorString8(rc));
431 if (rc==DS_OK && primary!=NULL)
432 IDirectSoundBuffer_Release(primary);
435 ZeroMemory(&bufdesc, sizeof(bufdesc));
436 bufdesc.dwSize=sizeof(bufdesc);
437 bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRLVOLUME;
438 rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
439 ok((rc==DS_OK && primary!=NULL) || (rc==DSERR_CONTROLUNAVAIL),
440 "IDirectSound_CreateSoundBuffer() failed to create a primary buffer: "
441 "%s\n",DXGetErrorString8(rc));
442 if (rc==DSERR_CONTROLUNAVAIL)
443 trace(" No Primary\n");
444 else if (rc==DS_OK && primary!=NULL) {
447 /* Try to create a second primary buffer */
448 /* DSOUND: Error: The primary buffer already exists.
449 * Any changes made to the buffer description will be ignored. */
450 rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&second,NULL);
451 ok(rc==DS_OK && second==primary,
452 "IDirectSound_CreateSoundBuffer() should have returned original "
453 "primary buffer: %s\n",DXGetErrorString8(rc));
454 ref=IDirectSoundBuffer_Release(second);
455 ok(ref==1,"IDirectSoundBuffer_Release() primary has %d references, "
456 "should have 1\n",ref);
458 /* Try to duplicate a primary buffer */
459 /* DSOUND: Error: Can't duplicate primary buffers */
460 rc=IDirectSound_DuplicateSoundBuffer(dso,primary,&third);
462 ok(rc!=DS_OK,"IDirectSound_DuplicateSoundBuffer() primary buffer "
463 "should have failed %s\n",DXGetErrorString8(rc));
465 rc=IDirectSoundBuffer_GetVolume(primary,&vol);
466 ok(rc==DS_OK,"IDirectSoundBuffer_GetVolume() failed: %s\n",
467 DXGetErrorString8(rc));
469 if (winetest_interactive) {
470 trace("Playing a 5 seconds reference tone at the current "
473 trace("(the current volume is %d according to DirectSound)\n",
475 trace("All subsequent tones should be identical to this one.\n");
476 trace("Listen for stutter, changes in pitch, volume, etc.\n");
478 test_buffer(dso,&primary,1,FALSE,0,FALSE,0,winetest_interactive &&
479 !(dscaps.dwFlags & DSCAPS_EMULDRIVER),5.0,0,0,0,0,FALSE,0);
481 ref=IDirectSoundBuffer_Release(primary);
482 ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
483 "should have 0\n",ref);
486 /* Set the CooperativeLevel back to normal */
487 /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
488 rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
489 ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
490 DXGetErrorString8(rc));
493 ref=IDirectSound_Release(dso);
494 ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
496 return DSERR_GENERIC;
502 * Test the primary buffer at different formats while keeping the
503 * secondary buffer at a constant format.
505 static HRESULT test_primary_secondary(LPGUID lpGuid)
508 LPDIRECTSOUND dso=NULL;
509 LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
510 DSBUFFERDESC bufdesc;
512 WAVEFORMATEX wfx, wfx2;
515 /* Create the DirectSound object */
516 rc=DirectSoundCreate(lpGuid,&dso,NULL);
517 ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
518 "DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
522 /* Get the device capabilities */
523 ZeroMemory(&dscaps, sizeof(dscaps));
524 dscaps.dwSize=sizeof(dscaps);
525 rc=IDirectSound_GetCaps(dso,&dscaps);
526 ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %s\n",DXGetErrorString8(rc));
530 /* We must call SetCooperativeLevel before creating primary buffer */
531 /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
532 rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
533 ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
534 DXGetErrorString8(rc));
538 ZeroMemory(&bufdesc, sizeof(bufdesc));
539 bufdesc.dwSize=sizeof(bufdesc);
540 bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
541 rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
542 ok(rc==DS_OK && primary!=NULL,
543 "IDirectSound_CreateSoundBuffer() failed to create a primary buffer "
544 "%s\n",DXGetErrorString8(rc));
546 if (rc==DS_OK && primary!=NULL) {
547 for (f=0;f<NB_FORMATS;f++) {
548 /* We must call SetCooperativeLevel to be allowed to call
550 /* DSOUND: Setting DirectSound cooperative level to
552 rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
553 ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
554 DXGetErrorString8(rc));
558 init_format(&wfx,WAVE_FORMAT_PCM,formats[f][0],formats[f][1],
561 rc=IDirectSoundBuffer_SetFormat(primary,&wfx);
562 ok(rc==DS_OK,"IDirectSoundBuffer_SetFormat(%s) failed: %s\n",
563 format_string(&wfx), DXGetErrorString8(rc));
565 /* There is no guarantee that SetFormat will actually change the
566 * format to what we asked for. It depends on what the soundcard
567 * supports. So we must re-query the format.
569 rc=IDirectSoundBuffer_GetFormat(primary,&wfx,sizeof(wfx),NULL);
570 ok(rc==DS_OK,"IDirectSoundBuffer_GetFormat() failed: %s\n",
571 DXGetErrorString8(rc));
573 (wfx.wFormatTag!=wfx2.wFormatTag ||
574 wfx.nSamplesPerSec!=wfx2.nSamplesPerSec ||
575 wfx.wBitsPerSample!=wfx2.wBitsPerSample ||
576 wfx.nChannels!=wfx2.nChannels)) {
577 trace("Requested primary format tag=0x%04x %dx%dx%d "
578 "avg.B/s=%d align=%d\n",
579 wfx2.wFormatTag,wfx2.nSamplesPerSec,wfx2.wBitsPerSample,
580 wfx2.nChannels,wfx2.nAvgBytesPerSec,wfx2.nBlockAlign);
581 trace("Got tag=0x%04x %dx%dx%d avg.B/s=%d align=%d\n",
582 wfx.wFormatTag,wfx.nSamplesPerSec,wfx.wBitsPerSample,
583 wfx.nChannels,wfx.nAvgBytesPerSec,wfx.nBlockAlign);
586 /* Set the CooperativeLevel back to normal */
587 /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
588 rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
589 ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
590 DXGetErrorString8(rc));
592 init_format(&wfx2,WAVE_FORMAT_PCM,11025,16,2);
595 ZeroMemory(&bufdesc, sizeof(bufdesc));
596 bufdesc.dwSize=sizeof(bufdesc);
597 bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
598 bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
600 bufdesc.lpwfxFormat=&wfx2;
601 if (winetest_interactive) {
602 trace(" Testing a primary buffer at %dx%dx%d with a "
603 "secondary buffer at %dx%dx%d\n",
604 wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,
605 wfx2.nSamplesPerSec,wfx2.wBitsPerSample,wfx2.nChannels);
607 rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
608 ok(rc==DS_OK && secondary!=NULL,
609 "IDirectSound_CreateSoundBuffer() failed to create a secondary "
610 "buffer %s\n",DXGetErrorString8(rc));
612 if (rc==DS_OK && secondary!=NULL) {
613 test_buffer(dso,&secondary,0,FALSE,0,FALSE,0,
614 winetest_interactive,1.0,0,NULL,0,0,FALSE,0);
616 ref=IDirectSoundBuffer_Release(secondary);
617 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
618 "should have 0\n",ref);
622 ref=IDirectSoundBuffer_Release(primary);
623 ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
624 "should have 0\n",ref);
627 /* Set the CooperativeLevel back to normal */
628 /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
629 rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
630 ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
631 DXGetErrorString8(rc));
634 ref=IDirectSound_Release(dso);
635 ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
637 return DSERR_GENERIC;
642 static HRESULT test_secondary(LPGUID lpGuid)
645 LPDIRECTSOUND dso=NULL;
646 LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
647 DSBUFFERDESC bufdesc;
649 WAVEFORMATEX wfx, wfx1;
653 /* Create the DirectSound object */
654 rc=DirectSoundCreate(lpGuid,&dso,NULL);
655 ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
656 "DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
660 /* Get the device capabilities */
661 ZeroMemory(&dscaps, sizeof(dscaps));
662 dscaps.dwSize=sizeof(dscaps);
663 rc=IDirectSound_GetCaps(dso,&dscaps);
664 ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %s\n",DXGetErrorString8(rc));
668 /* We must call SetCooperativeLevel before creating primary buffer */
669 /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
670 rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
671 ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
672 DXGetErrorString8(rc));
676 ZeroMemory(&bufdesc, sizeof(bufdesc));
677 bufdesc.dwSize=sizeof(bufdesc);
678 bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
679 rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
680 ok(rc==DS_OK && primary!=NULL,
681 "IDirectSound_CreateSoundBuffer() failed to create a primary buffer "
682 "%s\n",DXGetErrorString8(rc));
684 if (rc==DS_OK && primary!=NULL) {
685 rc=IDirectSoundBuffer_GetFormat(primary,&wfx1,sizeof(wfx1),NULL);
686 ok(rc==DS_OK,"IDirectSoundBuffer8_Getformat() failed: %s\n",
687 DXGetErrorString8(rc));
691 for (f=0;f<NB_FORMATS;f++) {
692 init_format(&wfx,WAVE_FORMAT_PCM,formats[f][0],formats[f][1],
695 ZeroMemory(&bufdesc, sizeof(bufdesc));
696 bufdesc.dwSize=sizeof(bufdesc);
697 bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
698 bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
700 rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
701 ok(rc==DSERR_INVALIDPARAM,"IDirectSound_CreateSoundBuffer() "
702 "should have returned DSERR_INVALIDPARAM, returned: %s\n",
703 DXGetErrorString8(rc));
704 if (rc==DS_OK && secondary!=NULL)
705 IDirectSoundBuffer_Release(secondary);
708 ZeroMemory(&bufdesc, sizeof(bufdesc));
709 bufdesc.dwSize=sizeof(bufdesc);
710 bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
711 bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
713 bufdesc.lpwfxFormat=&wfx;
714 if (winetest_interactive) {
715 trace(" Testing a secondary buffer at %dx%dx%d "
716 "with a primary buffer at %dx%dx%d\n",
717 wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,
718 wfx1.nSamplesPerSec,wfx1.wBitsPerSample,wfx1.nChannels);
720 rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
721 ok(rc==DS_OK && secondary!=NULL,
722 "IDirectSound_CreateSoundBuffer() failed to create a secondary "
723 "buffer %s\n",DXGetErrorString8(rc));
725 if (rc==DS_OK && secondary!=NULL) {
726 test_buffer(dso,&secondary,0,FALSE,0,FALSE,0,
727 winetest_interactive,1.0,0,NULL,0,0,FALSE,0);
729 ref=IDirectSoundBuffer_Release(secondary);
730 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
731 "should have 0\n",ref);
735 ref=IDirectSoundBuffer_Release(primary);
736 ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
737 "should have 0\n",ref);
740 /* Set the CooperativeLevel back to normal */
741 /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
742 rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
743 ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
744 DXGetErrorString8(rc));
747 ref=IDirectSound_Release(dso);
748 ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
750 return DSERR_GENERIC;
755 static HRESULT test_block_align(LPGUID lpGuid)
758 LPDIRECTSOUND dso=NULL;
759 LPDIRECTSOUNDBUFFER secondary=NULL;
760 DSBUFFERDESC bufdesc;
765 /* Create the DirectSound object */
766 rc=DirectSoundCreate(lpGuid,&dso,NULL);
767 ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
768 "DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
772 init_format(&wfx,WAVE_FORMAT_PCM,11025,16,2);
773 ZeroMemory(&bufdesc, sizeof(bufdesc));
774 bufdesc.dwSize=sizeof(bufdesc);
775 bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
776 bufdesc.dwBufferBytes=wfx.nAvgBytesPerSec + 1;
777 bufdesc.lpwfxFormat=&wfx;
778 rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
779 ok(rc==DS_OK,"IDirectSound_CreateSoundBuffer() "
780 "should have returned DS_OK, returned: %s\n",
781 DXGetErrorString8(rc));
783 if (rc==DS_OK && secondary!=NULL) {
784 ZeroMemory(&dsbcaps, sizeof(dsbcaps));
785 dsbcaps.dwSize = sizeof(dsbcaps);
786 rc=IDirectSoundBuffer_GetCaps(secondary,&dsbcaps);
787 ok(rc==DS_OK,"IDirectSoundBuffer_GetCaps() should have returned DS_OK, "
788 "returned: %s\n", DXGetErrorString8(rc));
790 ok(dsbcaps.dwBufferBytes==(wfx.nAvgBytesPerSec + wfx.nBlockAlign),
791 "Buffer size not a multiple of nBlockAlign: requested %d, "
792 "got %d, should be %d\n", bufdesc.dwBufferBytes,
793 dsbcaps.dwBufferBytes, wfx.nAvgBytesPerSec + wfx.nBlockAlign);
794 ref=IDirectSoundBuffer_Release(secondary);
795 ok(ref==0,"IDirectSoundBuffer_Release() secondary has %d references, "
796 "should have 0\n",ref);
799 ref=IDirectSound_Release(dso);
800 ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
802 return DSERR_GENERIC;
810 } fmts[] = { { 8, 1 }, { 8, 2 }, { 16, 1 }, {16, 2 } };
812 static HRESULT test_frequency(LPGUID lpGuid)
815 LPDIRECTSOUND dso=NULL;
816 LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
817 DSBUFFERDESC bufdesc;
819 WAVEFORMATEX wfx, wfx1;
822 int rates[] = { 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100,
825 /* Create the DirectSound object */
826 rc=DirectSoundCreate(lpGuid,&dso,NULL);
827 ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
828 "DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
832 /* Get the device capabilities */
833 ZeroMemory(&dscaps, sizeof(dscaps));
834 dscaps.dwSize=sizeof(dscaps);
835 rc=IDirectSound_GetCaps(dso,&dscaps);
836 ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %s\n",DXGetErrorString8(rc));
840 /* We must call SetCooperativeLevel before creating primary buffer */
841 /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
842 rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
843 ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
844 DXGetErrorString8(rc));
848 ZeroMemory(&bufdesc, sizeof(bufdesc));
849 bufdesc.dwSize=sizeof(bufdesc);
850 bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
851 rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
852 ok(rc==DS_OK && primary!=NULL,
853 "IDirectSound_CreateSoundBuffer() failed to create a primary buffer "
854 "%s\n",DXGetErrorString8(rc));
856 if (rc==DS_OK && primary!=NULL) {
857 rc=IDirectSoundBuffer_GetFormat(primary,&wfx1,sizeof(wfx1),NULL);
858 ok(rc==DS_OK,"IDirectSoundBuffer8_Getformat() failed: %s\n",
859 DXGetErrorString8(rc));
863 for (f=0;f<sizeof(fmts)/sizeof(fmts[0]);f++) {
864 for (r=0;r<sizeof(rates)/sizeof(rates[0]);r++) {
865 init_format(&wfx,WAVE_FORMAT_PCM,11025,fmts[f].bits,
868 ZeroMemory(&bufdesc, sizeof(bufdesc));
869 bufdesc.dwSize=sizeof(bufdesc);
870 bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2|DSBCAPS_CTRLFREQUENCY;
871 bufdesc.dwBufferBytes=align((wfx.nAvgBytesPerSec*rates[r]/11025)*
872 BUFFER_LEN/1000,wfx.nBlockAlign);
873 bufdesc.lpwfxFormat=&wfx;
874 if (winetest_interactive) {
875 trace(" Testing a secondary buffer at %dx%dx%d "
876 "with a primary buffer at %dx%dx%d\n",
877 wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,
878 wfx1.nSamplesPerSec,wfx1.wBitsPerSample,wfx1.nChannels);
880 rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
881 ok(rc==DS_OK && secondary!=NULL,
882 "IDirectSound_CreateSoundBuffer() failed to create a secondary "
883 "buffer %s\n",DXGetErrorString8(rc));
885 if (rc==DS_OK && secondary!=NULL) {
886 test_buffer(dso,&secondary,0,FALSE,0,FALSE,0,
887 winetest_interactive,1.0,0,NULL,0,0,TRUE,rates[r]);
889 ref=IDirectSoundBuffer_Release(secondary);
890 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
891 "should have 0\n",ref);
896 ref=IDirectSoundBuffer_Release(primary);
897 ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
898 "should have 0\n",ref);
901 /* Set the CooperativeLevel back to normal */
902 /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
903 rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
904 ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
905 DXGetErrorString8(rc));
908 ref=IDirectSound_Release(dso);
909 ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
911 return DSERR_GENERIC;
916 static BOOL WINAPI dsenum_callback(LPGUID lpGuid, LPCSTR lpcstrDescription,
917 LPCSTR lpcstrModule, LPVOID lpContext)
920 trace("*** Testing %s - %s ***\n",lpcstrDescription,lpcstrModule);
921 rc = test_dsound(lpGuid);
922 if (rc == DSERR_NODRIVER)
923 trace(" No Driver\n");
924 else if (rc == DSERR_ALLOCATED)
925 trace(" Already In Use\n");
926 else if (rc == E_FAIL)
927 trace(" No Device\n");
929 test_block_align(lpGuid);
930 test_primary(lpGuid);
931 test_primary_secondary(lpGuid);
932 test_secondary(lpGuid);
933 test_frequency(lpGuid);
939 static void dsound_tests(void)
942 rc=DirectSoundEnumerateA(&dsenum_callback,NULL);
943 ok(rc==DS_OK,"DirectSoundEnumerateA() failed: %s\n",DXGetErrorString8(rc));
950 trace("DLL Version: %s\n", get_file_version("dsound.dll"));
952 IDirectSound_tests();