Fix signed/unsigned comparison warnings.
[wine] / dlls / dsound / tests / dsound.c
1 /*
2  * Tests basic sound playback in DirectSound.
3  * In particular we test each standard Windows sound format to make sure
4  * we handle the sound card/driver quirks correctly.
5  *
6  * Part of this test involves playing test tones. But this only makes
7  * sense if someone is going to carefully listen to it, and would only
8  * bother everyone else.
9  * So this is only done if the test is being run in interactive mode.
10  *
11  * Copyright (c) 2002-2004 Francois Gouget
12  *
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.
17  *
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.
22  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26  */
27
28 #define NONAMELESSSTRUCT
29 #define NONAMELESSUNION
30 #include <windows.h>
31
32 #include <math.h>
33 #include <stdlib.h>
34
35 #include "wine/test.h"
36 #include "windef.h"
37 #include "wingdi.h"
38 #include "dsound.h"
39 #include "dxerr8.h"
40 #include "dsconf.h"
41
42 #include "dsound_test.h"
43
44 static void IDirectSound_test(LPDIRECTSOUND dso, BOOL initialized,
45                               LPCGUID lpGuid)
46 {
47     HRESULT rc;
48     DSCAPS dscaps;
49     int ref;
50     IUnknown * unknown;
51     IDirectSound * ds;
52     IDirectSound8 * ds8;
53     DWORD speaker_config, new_speaker_config;
54
55     /* Try to Query for objects */
56     rc=IDirectSound_QueryInterface(dso,&IID_IUnknown,(LPVOID*)&unknown);
57     ok(rc==DS_OK,"IDirectSound_QueryInterface(IID_IUnknown) failed: %s\n",
58        DXGetErrorString8(rc));
59     if (rc==DS_OK)
60         IDirectSound_Release(unknown);
61
62     rc=IDirectSound_QueryInterface(dso,&IID_IDirectSound,(LPVOID*)&ds);
63     ok(rc==DS_OK,"IDirectSound_QueryInterface(IID_IDirectSound) failed: %s\n",
64        DXGetErrorString8(rc));
65     if (rc==DS_OK)
66         IDirectSound_Release(ds);
67
68     rc=IDirectSound_QueryInterface(dso,&IID_IDirectSound8,(LPVOID*)&ds8);
69     ok(rc==E_NOINTERFACE,"IDirectSound_QueryInterface(IID_IDirectSound8) "
70        "should have failed: %s\n",DXGetErrorString8(rc));
71     if (rc==DS_OK)
72         IDirectSound8_Release(ds8);
73
74     if (initialized == FALSE) {
75         /* try unitialized object */
76         rc=IDirectSound_GetCaps(dso,0);
77         ok(rc==DSERR_UNINITIALIZED,"IDirectSound_GetCaps(NULL) "
78            "should have returned DSERR_UNINITIALIZED, returned: %s\n",
79            DXGetErrorString8(rc));
80
81         rc=IDirectSound_GetCaps(dso,&dscaps);
82         ok(rc==DSERR_UNINITIALIZED,"IDirectSound_GetCaps() "
83            "should have returned DSERR_UNINITIALIZED, returned: %s\n",
84            DXGetErrorString8(rc));
85
86         rc=IDirectSound_Compact(dso);
87         ok(rc==DSERR_UNINITIALIZED,"IDirectSound_Compact() "
88            "should have returned DSERR_UNINITIALIZED, returned: %s\n",
89            DXGetErrorString8(rc));
90
91         rc=IDirectSound_GetSpeakerConfig(dso,&speaker_config);
92         ok(rc==DSERR_UNINITIALIZED,"IDirectSound_GetSpeakerConfig() "
93            "should have returned DSERR_UNINITIALIZED, returned: %s\n",
94            DXGetErrorString8(rc));
95
96         rc=IDirectSound_Initialize(dso,lpGuid);
97         ok(rc==DS_OK,"IDirectSound_Initialize() failed: %s\n",
98            DXGetErrorString8(rc));
99     }
100
101     /* DSOUND: Error: Invalid caps buffer */
102     rc=IDirectSound_GetCaps(dso,0);
103     ok(rc==DSERR_INVALIDPARAM,"IDirectSound_GetCaps(NULL) "
104        "should have failed: %s\n",DXGetErrorString8(rc));
105
106     ZeroMemory(&dscaps, sizeof(dscaps));
107
108     /* DSOUND: Error: Invalid caps buffer */
109     rc=IDirectSound_GetCaps(dso,&dscaps);
110     ok(rc==DSERR_INVALIDPARAM,"IDirectSound_GetCaps() "
111        "should have failed: %s\n",DXGetErrorString8(rc));
112
113     dscaps.dwSize=sizeof(dscaps);
114
115     /* DSOUND: Running on a certified driver */
116     rc=IDirectSound_GetCaps(dso,&dscaps);
117     ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %s\n",DXGetErrorString8(rc));
118
119     rc=IDirectSound_Compact(dso);
120     ok(rc==DSERR_PRIOLEVELNEEDED,"IDirectSound_Compact() failed: %s\n",
121        DXGetErrorString8(rc));
122
123     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
124     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
125        DXGetErrorString8(rc));
126
127     rc=IDirectSound_Compact(dso);
128     ok(rc==DS_OK,"IDirectSound_Compact() failed: %s\n",DXGetErrorString8(rc));
129
130     rc=IDirectSound_GetSpeakerConfig(dso,0);
131     ok(rc==DSERR_INVALIDPARAM,"IDirectSound_GetSpeakerConfig(NULL) "
132        "should have failed: %s\n",DXGetErrorString8(rc));
133
134     rc=IDirectSound_GetSpeakerConfig(dso,&speaker_config);
135     ok(rc==DS_OK,"IDirectSound_GetSpeakerConfig() failed: %s\n",
136        DXGetErrorString8(rc));
137
138     speaker_config = DSSPEAKER_COMBINED(DSSPEAKER_STEREO,
139                                         DSSPEAKER_GEOMETRY_WIDE);
140     rc=IDirectSound_SetSpeakerConfig(dso,speaker_config);
141     ok(rc==DS_OK,"IDirectSound_SetSpeakerConfig() failed: %s\n",
142        DXGetErrorString8(rc));
143     if (rc==DS_OK) {
144         rc=IDirectSound_GetSpeakerConfig(dso,&new_speaker_config);
145         ok(rc==DS_OK,"IDirectSound_GetSpeakerConfig() failed: %s\n",
146            DXGetErrorString8(rc));
147         if (rc==DS_OK)
148             ok(speaker_config==new_speaker_config,
149                "IDirectSound_GetSpeakerConfig() failed to set speaker config: "
150                "expected 0x%08lx, got 0x%08lx\n",
151                speaker_config,new_speaker_config);
152     }
153
154     ref=IDirectSound_Release(dso);
155     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
156 }
157
158 static void IDirectSound_tests()
159 {
160     HRESULT rc;
161     LPDIRECTSOUND dso=NULL;
162
163     trace("Testing IDirectSound\n");
164
165     /* try the COM class factory method of creation with no device specified */
166     rc=CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
167                         &IID_IDirectSound, (void**)&dso);
168     ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSound) failed: %s\n",
169        DXGetErrorString8(rc));
170     if (dso)
171         IDirectSound_test(dso, FALSE, NULL);
172
173     /* try the COM class factory method of creation with default playback
174      * device specified */
175     rc=CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
176                         &IID_IDirectSound, (void**)&dso);
177     ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSound) failed: %s\n",
178        DXGetErrorString8(rc));
179     if (dso)
180         IDirectSound_test(dso, FALSE, &DSDEVID_DefaultPlayback);
181
182     /* try the COM class factory method of creation with default voice
183      * playback device specified */
184     rc=CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
185                         &IID_IDirectSound, (void**)&dso);
186     ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSound) failed: %s\n",
187        DXGetErrorString8(rc));
188     if (dso)
189         IDirectSound_test(dso, FALSE, &DSDEVID_DefaultVoicePlayback);
190
191     /* try the COM class factory method of creation with a bad
192      * IID specified */
193     rc=CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
194                         &CLSID_DirectSoundPrivate, (void**)&dso);
195     ok(rc==E_NOINTERFACE,
196        "CoCreateInstance(CLSID_DirectSound,CLSID_DirectSoundPrivate) "
197        "should have failed: %s\n",DXGetErrorString8(rc));
198
199     /* try the COM class factory method of creation with a bad
200      * GUID and IID specified */
201     rc=CoCreateInstance(&CLSID_DirectSoundPrivate, NULL, CLSCTX_INPROC_SERVER,
202                         &IID_IDirectSound, (void**)&dso);
203     ok(rc==REGDB_E_CLASSNOTREG,
204        "CoCreateInstance(CLSID_DirectSoundPrivate,IID_IDirectSound) "
205        "should have failed: %s\n",DXGetErrorString8(rc));
206
207     /* try with no device specified */
208     rc=DirectSoundCreate(NULL,&dso,NULL);
209     ok(rc==S_OK,"DirectSoundCreate(NULL) failed: %s\n",DXGetErrorString8(rc));
210     if (dso)
211         IDirectSound_test(dso, TRUE, NULL);
212
213     /* try with default playback device specified */
214     rc=DirectSoundCreate(&DSDEVID_DefaultPlayback,&dso,NULL);
215     ok(rc==S_OK,"DirectSoundCreate(DSDEVID_DefaultPlayback) failed: %s\n",
216        DXGetErrorString8(rc));
217     if (dso)
218         IDirectSound_test(dso, TRUE, NULL);
219
220     /* try with default voice playback device specified */
221     rc=DirectSoundCreate(&DSDEVID_DefaultVoicePlayback,&dso,NULL);
222     ok(rc==S_OK,"DirectSoundCreate(DSDEVID_DefaultVoicePlayback) failed: %s\n",
223        DXGetErrorString8(rc));
224     if (dso)
225         IDirectSound_test(dso, TRUE, NULL);
226
227     /* try with a bad device specified */
228     rc=DirectSoundCreate(&DSDEVID_DefaultVoiceCapture,&dso,NULL);
229     ok(rc==DSERR_NODRIVER,"DirectSoundCreate(DSDEVID_DefaultVoiceCapture) "
230        "should have failed: %s\n",DXGetErrorString8(rc));
231 }
232
233 static HRESULT test_dsound(LPGUID lpGuid)
234 {
235     HRESULT rc;
236     LPDIRECTSOUND dso=NULL;
237     int ref;
238
239     /* DSOUND: Error: Invalid interface buffer */
240     rc=DirectSoundCreate(lpGuid,0,NULL);
241     ok(rc==DSERR_INVALIDPARAM,"DirectSoundCreate should have failed: %s\n",
242        DXGetErrorString8(rc));
243
244     /* Create the DirectSound object */
245     rc=DirectSoundCreate(lpGuid,&dso,NULL);
246     ok(rc==DS_OK,"DirectSoundCreate failed: %s\n",DXGetErrorString8(rc));
247     if (rc!=DS_OK)
248         return rc;
249
250     /* Try the enumerated device */
251     IDirectSound_test(dso, TRUE, lpGuid);
252
253     /* Try the COM class factory method of creation with enumerated device */
254     rc=CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
255                         &IID_IDirectSound, (void**)&dso);
256     ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSound) failed: %s\n",
257        DXGetErrorString8(rc));
258     if (dso)
259         IDirectSound_test(dso, FALSE, lpGuid);
260
261     /* Create a DirectSound object */
262     rc=DirectSoundCreate(lpGuid,&dso,NULL);
263     ok(rc==DS_OK,"DirectSoundCreate failed: %s\n",DXGetErrorString8(rc));
264     if (rc==DS_OK) {
265         LPDIRECTSOUND dso1=NULL;
266
267         /* Create a second DirectSound object */
268         rc=DirectSoundCreate(lpGuid,&dso1,NULL);
269         ok(rc==DS_OK,"DirectSoundCreate failed: %s\n",DXGetErrorString8(rc));
270         if (rc==DS_OK) {
271             /* Release the second DirectSound object */
272             ref=IDirectSound_Release(dso1);
273             ok(ref==0,"IDirectSound_Release has %d references, should have 0\n",
274                ref);
275             ok(dso!=dso1,"DirectSound objects should be unique: "
276                "dso=0x%08lx,dso1=0x%08lx\n",(DWORD)dso,(DWORD)dso1);
277         }
278
279         /* Release the first DirectSound object */
280         ref=IDirectSound_Release(dso);
281         ok(ref==0,"IDirectSound_Release has %d references, should have 0\n",
282            ref);
283         if (ref!=0)
284             return DSERR_GENERIC;
285     } else
286         return rc;
287
288     /* Create a DirectSound object */
289     rc=DirectSoundCreate(lpGuid,&dso,NULL);
290     ok(rc==DS_OK,"DirectSoundCreate failed: %s\n",DXGetErrorString8(rc));
291     if (rc==DS_OK) {
292         LPDIRECTSOUNDBUFFER secondary;
293         DSBUFFERDESC bufdesc;
294         WAVEFORMATEX wfx;
295
296         init_format(&wfx,WAVE_FORMAT_PCM,11025,8,1);
297         ZeroMemory(&bufdesc, sizeof(bufdesc));
298         bufdesc.dwSize=sizeof(bufdesc);
299         bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_CTRL3D;
300         bufdesc.dwBufferBytes=wfx.nAvgBytesPerSec*BUFFER_LEN/1000;
301         bufdesc.lpwfxFormat=&wfx;
302         rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
303         ok(rc==DS_OK && secondary!=NULL,
304            "IDirectSound_CreateSoundBuffer failed to create a secondary buffer "
305            "%s\n",DXGetErrorString8(rc));
306         if (rc==DS_OK && secondary!=NULL) {
307             LPDIRECTSOUND3DBUFFER buffer3d;
308             rc=IDirectSound_QueryInterface(secondary, &IID_IDirectSound3DBuffer,
309                                            (void **)&buffer3d);
310             ok(rc==DS_OK && buffer3d!=NULL,"QueryInterface failed:  %s\n",
311                DXGetErrorString8(rc));
312             if (rc==DS_OK && buffer3d!=NULL) {
313                 ref=IDirectSound3DBuffer_AddRef(buffer3d);
314                 ok(ref==2,"IDirectSound3DBuffer_AddRef has %d references, "
315                    "should have 2\n",ref);
316             }
317             ref=IDirectSoundBuffer_AddRef(secondary);
318             ok(ref==2,"IDirectSoundBuffer_AddRef has %d references, "
319                "should have 2\n",ref);
320         }
321         /* release with buffer */
322         ref=IDirectSound_Release(dso);
323         ok(ref==0,"IDirectSound_Release has %d references, should have 0\n",
324            ref);
325         if (ref!=0)
326             return DSERR_GENERIC;
327     } else
328         return rc;
329
330     return DS_OK;
331 }
332
333 static HRESULT test_primary(LPGUID lpGuid)
334 {
335     HRESULT rc;
336     LPDIRECTSOUND dso=NULL;
337     LPDIRECTSOUNDBUFFER primary=NULL,second=NULL,third=NULL;
338     DSBUFFERDESC bufdesc;
339     DSCAPS dscaps;
340     int ref;
341
342     /* Create the DirectSound object */
343     rc=DirectSoundCreate(lpGuid,&dso,NULL);
344     ok(rc==DS_OK,"DirectSoundCreate failed: %s\n",DXGetErrorString8(rc));
345     if (rc!=DS_OK)
346         return rc;
347
348     /* Get the device capabilities */
349     ZeroMemory(&dscaps, sizeof(dscaps));
350     dscaps.dwSize=sizeof(dscaps);
351     rc=IDirectSound_GetCaps(dso,&dscaps);
352     ok(rc==DS_OK,"IDirectSound_GetCaps failed: %s\n",DXGetErrorString8(rc));
353     if (rc!=DS_OK)
354         goto EXIT;
355
356     /* DSOUND: Error: Invalid buffer description pointer */
357     rc=IDirectSound_CreateSoundBuffer(dso,0,0,NULL);
358     ok(rc==DSERR_INVALIDPARAM,
359        "IDirectSound_CreateSoundBuffer should have failed: %s\n",
360        DXGetErrorString8(rc));
361
362     /* DSOUND: Error: Invalid buffer description pointer */
363     rc=IDirectSound_CreateSoundBuffer(dso,0,&primary,NULL);
364     ok(rc==DSERR_INVALIDPARAM && primary==0,
365        "IDirectSound_CreateSoundBuffer should have failed: rc=%s,dsbo=0x%lx\n",
366        DXGetErrorString8(rc),(DWORD)primary);
367
368     /* DSOUND: Error: Invalid buffer description pointer */
369     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,0,NULL);
370     ok(rc==DSERR_INVALIDPARAM && primary==0,
371        "IDirectSound_CreateSoundBuffer should have failed: rc=%s,dsbo=0x%lx\n",
372        DXGetErrorString8(rc),(DWORD)primary);
373
374     ZeroMemory(&bufdesc, sizeof(bufdesc));
375
376     /* DSOUND: Error: Invalid size */
377     /* DSOUND: Error: Invalid buffer description */
378     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
379     ok(rc==DSERR_INVALIDPARAM && primary==0,
380        "IDirectSound_CreateSoundBuffer should have failed: rc=%s,"
381        "primary=0x%lx\n",DXGetErrorString8(rc),(DWORD)primary);
382
383     /* We must call SetCooperativeLevel before calling CreateSoundBuffer */
384     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
385     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
386     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel failed: %s\n",
387        DXGetErrorString8(rc));
388     if (rc!=DS_OK)
389         goto EXIT;
390
391     /* Testing the primary buffer */
392     primary=NULL;
393     ZeroMemory(&bufdesc, sizeof(bufdesc));
394     bufdesc.dwSize=sizeof(bufdesc);
395     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRLVOLUME;
396     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
397     ok(rc==DS_OK && primary!=NULL,
398        "IDirectSound_CreateSoundBuffer failed to create a primary buffer: %s\n",
399        DXGetErrorString8(rc));
400     if (rc==DS_OK && primary!=NULL) {
401         LONG vol;
402
403         /* Try to create a second primary buffer */
404         /* DSOUND: Error: The primary buffer already exists.
405          * Any changes made to the buffer description will be ignored. */
406         rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&second,NULL);
407         ok(rc==DS_OK && second==primary,
408            "IDirectSound_CreateSoundBuffer should have returned original "
409            "primary buffer: %s\n",DXGetErrorString8(rc));
410         ref=IDirectSoundBuffer_Release(second);
411         ok(ref==1,"IDirectSoundBuffer_Release primary has %d references, "
412            "should have 1\n",ref);
413
414         /* Try to duplicate a primary buffer */
415         /* DSOUND: Error: Can't duplicate primary buffers */
416         rc=IDirectSound_DuplicateSoundBuffer(dso,primary,&third);
417         /* rc=0x88780032 */
418         ok(rc!=DS_OK,"IDirectSound_DuplicateSoundBuffer primary buffer should "
419            "have failed %s\n",DXGetErrorString8(rc));
420
421         rc=IDirectSoundBuffer_GetVolume(primary,&vol);
422         ok(rc==DS_OK,"IDirectSoundBuffer_GetVolume failed: %s\n",
423            DXGetErrorString8(rc));
424
425         if (winetest_interactive) {
426             trace("Playing a 5 seconds reference tone at the current volume.\n");
427             if (rc==DS_OK)
428                 trace("(the current volume is %ld according to DirectSound)\n",
429                       vol);
430             trace("All subsequent tones should be identical to this one.\n");
431             trace("Listen for stutter, changes in pitch, volume, etc.\n");
432         }
433         test_buffer(dso,primary,1,FALSE,0,FALSE,0,winetest_interactive &&
434                     !(dscaps.dwFlags & DSCAPS_EMULDRIVER),5.0,0,0,0,0);
435
436         ref=IDirectSoundBuffer_Release(primary);
437         ok(ref==0,"IDirectSoundBuffer_Release primary has %d references, "
438            "should have 0\n",ref);
439     }
440
441     /* Set the CooperativeLevel back to normal */
442     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
443     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
444     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel failed: %s\n",
445        DXGetErrorString8(rc));
446
447 EXIT:
448     ref=IDirectSound_Release(dso);
449     ok(ref==0,"IDirectSound_Release has %d references, should have 0\n",ref);
450     if (ref!=0)
451         return DSERR_GENERIC;
452
453     return rc;
454 }
455
456 /*
457  * Test the primary buffer at different formats while keeping the
458  * secondary buffer at a constant format.
459  */
460 static HRESULT test_primary_secondary(LPGUID lpGuid)
461 {
462     HRESULT rc;
463     LPDIRECTSOUND dso=NULL;
464     LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
465     DSBUFFERDESC bufdesc;
466     DSCAPS dscaps;
467     WAVEFORMATEX wfx, wfx2;
468     int f,ref;
469
470     /* Create the DirectSound object */
471     rc=DirectSoundCreate(lpGuid,&dso,NULL);
472     ok(rc==DS_OK,"DirectSoundCreate failed: %s\n",DXGetErrorString8(rc));
473     if (rc!=DS_OK)
474         return rc;
475
476     /* Get the device capabilities */
477     ZeroMemory(&dscaps, sizeof(dscaps));
478     dscaps.dwSize=sizeof(dscaps);
479     rc=IDirectSound_GetCaps(dso,&dscaps);
480     ok(rc==DS_OK,"IDirectSound_GetCaps failed: %s\n",DXGetErrorString8(rc));
481     if (rc!=DS_OK)
482         goto EXIT;
483
484     /* We must call SetCooperativeLevel before creating primary buffer */
485     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
486     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
487     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel failed: %s\n",
488        DXGetErrorString8(rc));
489     if (rc!=DS_OK)
490         goto EXIT;
491
492     ZeroMemory(&bufdesc, sizeof(bufdesc));
493     bufdesc.dwSize=sizeof(bufdesc);
494     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
495     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
496     ok(rc==DS_OK && primary!=NULL,
497        "IDirectSound_CreateSoundBuffer failed to create a primary buffer %s\n",
498        DXGetErrorString8(rc));
499
500     if (rc==DS_OK && primary!=NULL) {
501         for (f=0;f<NB_FORMATS;f++) {
502             /* We must call SetCooperativeLevel to be allowed to call SetFormat */
503             /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
504             rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
505             ok(rc==DS_OK,"SetCooperativeLevel failed: 0x%0lx\n",rc);
506             if (rc!=DS_OK)
507                 goto EXIT;
508
509             init_format(&wfx,WAVE_FORMAT_PCM,formats[f][0],formats[f][1],
510                         formats[f][2]);
511             wfx2=wfx;
512             rc=IDirectSoundBuffer_SetFormat(primary,&wfx);
513             ok(rc==DS_OK,"IDirectSoundBuffer_SetFormat failed: %s\n",
514                DXGetErrorString8(rc));
515
516             /* There is no garantee that SetFormat will actually change the
517              * format to what we asked for. It depends on what the soundcard
518              * supports. So we must re-query the format.
519              */
520             rc=IDirectSoundBuffer_GetFormat(primary,&wfx,sizeof(wfx),NULL);
521             ok(rc==DS_OK,"GetFormat failed: 0x%lx\n",rc);
522             if (rc==DS_OK &&
523                 (wfx.wFormatTag!=wfx2.wFormatTag ||
524                  wfx.nSamplesPerSec!=wfx2.nSamplesPerSec ||
525                  wfx.wBitsPerSample!=wfx2.wBitsPerSample ||
526                  wfx.nChannels!=wfx2.nChannels)) {
527                 trace("Requested primary format tag=0x%04x %ldx%dx%d avg.B/s=%ld align=%d\n",
528                       wfx2.wFormatTag,wfx2.nSamplesPerSec,wfx2.wBitsPerSample,
529                       wfx2.nChannels,wfx2.nAvgBytesPerSec,wfx2.nBlockAlign);
530                 trace("Got tag=0x%04x %ldx%dx%d avg.B/s=%ld align=%d\n",
531                       wfx.wFormatTag,wfx.nSamplesPerSec,wfx.wBitsPerSample,
532                       wfx.nChannels,wfx.nAvgBytesPerSec,wfx.nBlockAlign);
533             }
534
535             /* Set the CooperativeLevel back to normal */
536             /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
537             rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
538             ok(rc==DS_OK,"SetCooperativeLevel failed: 0x%0lx\n",rc);
539
540             init_format(&wfx2,WAVE_FORMAT_PCM,11025,16,2);
541
542             secondary=NULL;
543             ZeroMemory(&bufdesc, sizeof(bufdesc));
544             bufdesc.dwSize=sizeof(bufdesc);
545             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
546             bufdesc.dwBufferBytes=wfx.nAvgBytesPerSec*BUFFER_LEN/1000;
547             bufdesc.lpwfxFormat=&wfx2;
548             trace("  Testing a primary buffer at %ldx%dx%d with a secondary buffer at %ldx%dx%d\n",
549                   wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,
550                   wfx2.nSamplesPerSec,wfx2.wBitsPerSample,wfx2.nChannels);
551             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
552             ok(rc==DS_OK && secondary!=NULL,
553                "IDirectSound_CreateSoundBuffer failed to create a secondary "
554                "buffer %s\n",DXGetErrorString8(rc));
555
556             if (rc==DS_OK && secondary!=NULL) {
557                 test_buffer(dso,secondary,0,FALSE,0,FALSE,0,
558                             winetest_interactive,1.0,0,NULL,0,0);
559
560                 ref=IDirectSoundBuffer_Release(secondary);
561                 ok(ref==0,"IDirectSoundBuffer_Release has %d references, "
562                    "should have 0\n",ref);
563             }
564         }
565
566         ref=IDirectSoundBuffer_Release(primary);
567         ok(ref==0,"IDirectSoundBuffer_Release primary has %d references, "
568            "should have 0\n",ref);
569     }
570
571     /* Set the CooperativeLevel back to normal */
572     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
573     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
574     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel failed: %s\n",
575        DXGetErrorString8(rc));
576
577 EXIT:
578     ref=IDirectSound_Release(dso);
579     ok(ref==0,"IDirectSound_Release has %d references, should have 0\n",ref);
580     if (ref!=0)
581         return DSERR_GENERIC;
582
583     return rc;
584 }
585
586 static HRESULT test_secondary(LPGUID lpGuid)
587 {
588     HRESULT rc;
589     LPDIRECTSOUND dso=NULL;
590     LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
591     DSBUFFERDESC bufdesc;
592     DSCAPS dscaps;
593     WAVEFORMATEX wfx;
594     DWORD f;
595     int ref;
596
597     /* Create the DirectSound object */
598     rc=DirectSoundCreate(lpGuid,&dso,NULL);
599     ok(rc==DS_OK,"DirectSoundCreate failed: %s\n",DXGetErrorString8(rc));
600     if (rc!=DS_OK)
601         return rc;
602
603     /* Get the device capabilities */
604     ZeroMemory(&dscaps, sizeof(dscaps));
605     dscaps.dwSize=sizeof(dscaps);
606     rc=IDirectSound_GetCaps(dso,&dscaps);
607     ok(rc==DS_OK,"IDirectSound_GetCaps failed: %s\n",DXGetErrorString8(rc));
608     if (rc!=DS_OK)
609         goto EXIT;
610
611     /* We must call SetCooperativeLevel before creating primary buffer */
612     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
613     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
614     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel failed: %s\n",
615        DXGetErrorString8(rc));
616     if (rc!=DS_OK)
617         goto EXIT;
618
619     ZeroMemory(&bufdesc, sizeof(bufdesc));
620     bufdesc.dwSize=sizeof(bufdesc);
621     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
622     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
623     ok(rc==DS_OK && primary!=NULL,
624        "IDirectSound_CreateSoundBuffer failed to create a primary buffer %s\n",
625        DXGetErrorString8(rc));
626
627     if (rc==DS_OK && primary!=NULL) {
628         for (f=0;f<NB_FORMATS;f++) {
629             init_format(&wfx,WAVE_FORMAT_PCM,formats[f][0],formats[f][1],
630                         formats[f][2]);
631             secondary=NULL;
632             ZeroMemory(&bufdesc, sizeof(bufdesc));
633             bufdesc.dwSize=sizeof(bufdesc);
634             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
635             bufdesc.dwBufferBytes=wfx.nAvgBytesPerSec*BUFFER_LEN/1000;
636             bufdesc.lpwfxFormat=&wfx;
637             trace("  Testing a secondary buffer at %ldx%dx%d\n",
638                   wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels);
639             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
640             ok(rc==DS_OK && secondary!=NULL,
641                "IDirectSound_CreateSoundBuffer failed to create a secondary "
642                "buffer %s\n",DXGetErrorString8(rc));
643
644             if (rc==DS_OK && secondary!=NULL) {
645                 test_buffer(dso,secondary,0,FALSE,0,FALSE,0,
646                             winetest_interactive,1.0,0,NULL,0,0);
647
648                 ref=IDirectSoundBuffer_Release(secondary);
649                 ok(ref==0,"IDirectSoundBuffer_Release has %d references, "
650                    "should have 0\n",ref);
651             }
652         }
653
654         ref=IDirectSoundBuffer_Release(primary);
655         ok(ref==0,"IDirectSoundBuffer_Release primary has %d references, "
656            "should have 0\n",ref);
657     }
658
659     /* Set the CooperativeLevel back to normal */
660     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
661     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
662     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel failed: %s\n",
663        DXGetErrorString8(rc));
664
665 EXIT:
666     ref=IDirectSound_Release(dso);
667     ok(ref==0,"IDirectSound_Release has %d references, should have 0\n",ref);
668     if (ref!=0)
669         return DSERR_GENERIC;
670
671     return rc;
672 }
673
674 static BOOL WINAPI dsenum_callback(LPGUID lpGuid, LPCSTR lpcstrDescription,
675                                    LPCSTR lpcstrModule, LPVOID lpContext)
676 {
677     trace("*** Testing %s - %s\n",lpcstrDescription,lpcstrModule);
678     test_dsound(lpGuid);
679     test_primary(lpGuid);
680     test_primary_secondary(lpGuid);
681     test_secondary(lpGuid);
682
683     return 1;
684 }
685
686 static void dsound_tests()
687 {
688     HRESULT rc;
689     rc=DirectSoundEnumerateA(&dsenum_callback,NULL);
690     ok(rc==DS_OK,"DirectSoundEnumerate failed: %ld\n",rc);
691 }
692
693 START_TEST(dsound)
694 {
695     CoInitialize(NULL);
696
697     IDirectSound_tests();
698     dsound_tests();
699 }