Add support for more than 2 audio channels.
[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 "wine/test.h"
33 #include "dsound.h"
34 #include "dxerr8.h"
35 #include "dsconf.h"
36
37 #include "dsound_test.h"
38
39 static void IDirectSound_test(LPDIRECTSOUND dso, BOOL initialized,
40                               LPCGUID lpGuid)
41 {
42     HRESULT rc;
43     DSCAPS dscaps;
44     int ref;
45     IUnknown * unknown;
46     IDirectSound * ds;
47     IDirectSound8 * ds8;
48     DWORD speaker_config, new_speaker_config;
49
50     /* Try to Query for objects */
51     rc=IDirectSound_QueryInterface(dso,&IID_IUnknown,(LPVOID*)&unknown);
52     ok(rc==DS_OK,"IDirectSound_QueryInterface(IID_IUnknown) failed: %s\n",
53        DXGetErrorString8(rc));
54     if (rc==DS_OK)
55         IDirectSound_Release(unknown);
56
57     rc=IDirectSound_QueryInterface(dso,&IID_IDirectSound,(LPVOID*)&ds);
58     ok(rc==DS_OK,"IDirectSound_QueryInterface(IID_IDirectSound) failed: %s\n",
59        DXGetErrorString8(rc));
60     if (rc==DS_OK)
61         IDirectSound_Release(ds);
62
63     rc=IDirectSound_QueryInterface(dso,&IID_IDirectSound8,(LPVOID*)&ds8);
64     ok(rc==E_NOINTERFACE,"IDirectSound_QueryInterface(IID_IDirectSound8) "
65        "should have failed: %s\n",DXGetErrorString8(rc));
66     if (rc==DS_OK)
67         IDirectSound8_Release(ds8);
68
69     if (initialized == FALSE) {
70         /* try unitialized object */
71         rc=IDirectSound_GetCaps(dso,0);
72         ok(rc==DSERR_UNINITIALIZED,"IDirectSound_GetCaps(NULL) "
73            "should have returned DSERR_UNINITIALIZED, returned: %s\n",
74            DXGetErrorString8(rc));
75
76         rc=IDirectSound_GetCaps(dso,&dscaps);
77         ok(rc==DSERR_UNINITIALIZED,"IDirectSound_GetCaps() "
78            "should have returned DSERR_UNINITIALIZED, returned: %s\n",
79            DXGetErrorString8(rc));
80
81         rc=IDirectSound_Compact(dso);
82         ok(rc==DSERR_UNINITIALIZED,"IDirectSound_Compact() "
83            "should have returned DSERR_UNINITIALIZED, returned: %s\n",
84            DXGetErrorString8(rc));
85
86         rc=IDirectSound_GetSpeakerConfig(dso,&speaker_config);
87         ok(rc==DSERR_UNINITIALIZED,"IDirectSound_GetSpeakerConfig() "
88            "should have returned DSERR_UNINITIALIZED, returned: %s\n",
89            DXGetErrorString8(rc));
90
91         rc=IDirectSound_Initialize(dso,lpGuid);
92         ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
93            "IDirectSound_Initialize() failed: %s\n",DXGetErrorString8(rc));
94         if (rc==DSERR_NODRIVER) {
95             trace("  No Driver\n");
96             goto EXIT;
97         } else if (rc==E_FAIL) {
98             trace("  No Device\n");
99             goto EXIT;
100         } else if (rc==DSERR_ALLOCATED) {
101             trace("  Already In Use\n");
102             goto EXIT;
103         }
104     }
105
106     rc=IDirectSound_Initialize(dso,lpGuid);
107     ok(rc==DSERR_ALREADYINITIALIZED, "IDirectSound_Initialize() "
108        "should have returned DSERR_ALREADYINITIALIZED: %s\n",
109        DXGetErrorString8(rc));
110
111     /* DSOUND: Error: Invalid caps buffer */
112     rc=IDirectSound_GetCaps(dso,0);
113     ok(rc==DSERR_INVALIDPARAM,"IDirectSound_GetCaps(NULL) "
114        "should have returned DSERR_INVALIDPARAM, returned: %s\n",
115        DXGetErrorString8(rc));
116
117     ZeroMemory(&dscaps, sizeof(dscaps));
118
119     /* DSOUND: Error: Invalid caps buffer */
120     rc=IDirectSound_GetCaps(dso,&dscaps);
121     ok(rc==DSERR_INVALIDPARAM,"IDirectSound_GetCaps() "
122        "should have returned DSERR_INVALIDPARAM, returned: %s\n",
123        DXGetErrorString8(rc));
124
125     dscaps.dwSize=sizeof(dscaps);
126
127     /* DSOUND: Running on a certified driver */
128     rc=IDirectSound_GetCaps(dso,&dscaps);
129     ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %s\n",DXGetErrorString8(rc));
130
131     rc=IDirectSound_Compact(dso);
132     ok(rc==DSERR_PRIOLEVELNEEDED,"IDirectSound_Compact() failed: %s\n",
133        DXGetErrorString8(rc));
134
135     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
136     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
137        DXGetErrorString8(rc));
138
139     rc=IDirectSound_Compact(dso);
140     ok(rc==DS_OK,"IDirectSound_Compact() failed: %s\n",DXGetErrorString8(rc));
141
142     rc=IDirectSound_GetSpeakerConfig(dso,0);
143     ok(rc==DSERR_INVALIDPARAM,"IDirectSound_GetSpeakerConfig(NULL) "
144        "should have returned DSERR_INVALIDPARAM, returned: %s\n",
145        DXGetErrorString8(rc));
146
147     rc=IDirectSound_GetSpeakerConfig(dso,&speaker_config);
148     ok(rc==DS_OK,"IDirectSound_GetSpeakerConfig() failed: %s\n",
149        DXGetErrorString8(rc));
150
151     speaker_config = DSSPEAKER_COMBINED(DSSPEAKER_STEREO,
152                                         DSSPEAKER_GEOMETRY_WIDE);
153     rc=IDirectSound_SetSpeakerConfig(dso,speaker_config);
154     ok(rc==DS_OK,"IDirectSound_SetSpeakerConfig() failed: %s\n",
155        DXGetErrorString8(rc));
156     if (rc==DS_OK) {
157         rc=IDirectSound_GetSpeakerConfig(dso,&new_speaker_config);
158         ok(rc==DS_OK,"IDirectSound_GetSpeakerConfig() failed: %s\n",
159            DXGetErrorString8(rc));
160         if (rc==DS_OK && speaker_config!=new_speaker_config)
161                trace("IDirectSound_GetSpeakerConfig() failed to set speaker "
162                "config: expected 0x%08lx, got 0x%08lx\n",
163                speaker_config,new_speaker_config);
164     }
165
166 EXIT:
167     ref=IDirectSound_Release(dso);
168     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
169 }
170
171 static void IDirectSound_tests()
172 {
173     HRESULT rc;
174     LPDIRECTSOUND dso=NULL;
175
176     trace("Testing IDirectSound\n");
177
178     /* try the COM class factory method of creation with no device specified */
179     rc=CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
180                         &IID_IDirectSound, (void**)&dso);
181     ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSound) failed: %s\n",
182        DXGetErrorString8(rc));
183     if (dso)
184         IDirectSound_test(dso, FALSE, NULL);
185
186     /* try the COM class factory method of creation with default playback
187      * 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));
192     if (dso)
193         IDirectSound_test(dso, FALSE, &DSDEVID_DefaultPlayback);
194
195     /* try the COM class factory method of creation with default voice
196      * playback 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));
201     if (dso)
202         IDirectSound_test(dso, FALSE, &DSDEVID_DefaultVoicePlayback);
203
204     /* try the COM class factory method of creation with a bad
205      * IID specified */
206     rc=CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
207                         &CLSID_DirectSoundPrivate, (void**)&dso);
208     ok(rc==E_NOINTERFACE,
209        "CoCreateInstance(CLSID_DirectSound,CLSID_DirectSoundPrivate) "
210        "should have failed: %s\n",DXGetErrorString8(rc));
211
212     /* try the COM class factory method of creation with a bad
213      * GUID and IID specified */
214     rc=CoCreateInstance(&CLSID_DirectSoundPrivate, NULL, CLSCTX_INPROC_SERVER,
215                         &IID_IDirectSound, (void**)&dso);
216     ok(rc==REGDB_E_CLASSNOTREG,
217        "CoCreateInstance(CLSID_DirectSoundPrivate,IID_IDirectSound) "
218        "should have failed: %s\n",DXGetErrorString8(rc));
219
220     /* try with no device specified */
221     rc=DirectSoundCreate(NULL,&dso,NULL);
222     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
223        "DirectSoundCreate(NULL) failed: %s\n",DXGetErrorString8(rc));
224     if (rc==S_OK && dso)
225         IDirectSound_test(dso, TRUE, NULL);
226
227     /* try with default playback device specified */
228     rc=DirectSoundCreate(&DSDEVID_DefaultPlayback,&dso,NULL);
229     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
230        "DirectSoundCreate(DSDEVID_DefaultPlayback) failed: %s\n",
231        DXGetErrorString8(rc));
232     if (rc==DS_OK && dso)
233         IDirectSound_test(dso, TRUE, NULL);
234
235     /* try with default voice playback device specified */
236     rc=DirectSoundCreate(&DSDEVID_DefaultVoicePlayback,&dso,NULL);
237     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
238        "DirectSoundCreate(DSDEVID_DefaultVoicePlayback) failed: %s\n",
239        DXGetErrorString8(rc));
240     if (rc==DS_OK && dso)
241         IDirectSound_test(dso, TRUE, NULL);
242
243     /* try with a bad device specified */
244     rc=DirectSoundCreate(&DSDEVID_DefaultVoiceCapture,&dso,NULL);
245     ok(rc==DSERR_NODRIVER,"DirectSoundCreate(DSDEVID_DefaultVoiceCapture) "
246        "should have failed: %s\n",DXGetErrorString8(rc));
247     if (rc==DS_OK && dso)
248         IDirectSound_Release(dso);
249 }
250
251 static HRESULT test_dsound(LPGUID lpGuid)
252 {
253     HRESULT rc;
254     LPDIRECTSOUND dso=NULL;
255     int ref;
256
257     /* DSOUND: Error: Invalid interface buffer */
258     rc=DirectSoundCreate(lpGuid,0,NULL);
259     ok(rc==DSERR_INVALIDPARAM,"DirectSoundCreate() should have returned "
260        "DSERR_INVALIDPARAM, returned: %s\n",DXGetErrorString8(rc));
261
262     /* Create the DirectSound object */
263     rc=DirectSoundCreate(lpGuid,&dso,NULL);
264     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
265        "DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
266     if (rc!=DS_OK)
267         return rc;
268
269     /* Try the enumerated device */
270     IDirectSound_test(dso, TRUE, lpGuid);
271
272     /* Try the COM class factory method of creation with enumerated device */
273     rc=CoCreateInstance(&CLSID_DirectSound, NULL, CLSCTX_INPROC_SERVER,
274                         &IID_IDirectSound, (void**)&dso);
275     ok(rc==S_OK,"CoCreateInstance(CLSID_DirectSound) failed: %s\n",
276        DXGetErrorString8(rc));
277     if (dso)
278         IDirectSound_test(dso, FALSE, lpGuid);
279
280     /* Create a DirectSound object */
281     rc=DirectSoundCreate(lpGuid,&dso,NULL);
282     ok(rc==DS_OK,"DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
283     if (rc==DS_OK) {
284         LPDIRECTSOUND dso1=NULL;
285
286         /* Create a second DirectSound object */
287         rc=DirectSoundCreate(lpGuid,&dso1,NULL);
288         ok(rc==DS_OK,"DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
289         if (rc==DS_OK) {
290             /* Release the second DirectSound object */
291             ref=IDirectSound_Release(dso1);
292             ok(ref==0,"IDirectSound_Release() has %d references, should have "
293                "0\n",ref);
294             ok(dso!=dso1,"DirectSound objects should be unique: "
295                "dso=0x%08lx,dso1=0x%08lx\n",(DWORD)dso,(DWORD)dso1);
296         }
297
298         /* Release the first DirectSound object */
299         ref=IDirectSound_Release(dso);
300         ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",
301            ref);
302         if (ref!=0)
303             return DSERR_GENERIC;
304     } else
305         return rc;
306
307     /* Create a DirectSound object */
308     rc=DirectSoundCreate(lpGuid,&dso,NULL);
309     ok(rc==DS_OK,"DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
310     if (rc==DS_OK) {
311         LPDIRECTSOUNDBUFFER secondary;
312         DSBUFFERDESC bufdesc;
313         WAVEFORMATEX wfx;
314
315         init_format(&wfx,WAVE_FORMAT_PCM,11025,8,1);
316         ZeroMemory(&bufdesc, sizeof(bufdesc));
317         bufdesc.dwSize=sizeof(bufdesc);
318         bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_CTRL3D;
319         bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
320                                     wfx.nBlockAlign);
321         bufdesc.lpwfxFormat=&wfx;
322         rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
323         ok(rc==DS_OK && secondary!=NULL,
324            "IDirectSound_CreateSoundBuffer() failed to create a secondary "
325            "buffer %s\n",DXGetErrorString8(rc));
326         if (rc==DS_OK && secondary!=NULL) {
327             LPDIRECTSOUND3DBUFFER buffer3d;
328             rc=IDirectSound_QueryInterface(secondary, &IID_IDirectSound3DBuffer,
329                                            (void **)&buffer3d);
330             ok(rc==DS_OK && buffer3d!=NULL,"IDirectSound_QueryInterface() "
331                "failed:  %s\n",DXGetErrorString8(rc));
332             if (rc==DS_OK && buffer3d!=NULL) {
333                 ref=IDirectSound3DBuffer_AddRef(buffer3d);
334                 ok(ref==2,"IDirectSound3DBuffer_AddRef() has %d references, "
335                    "should have 2\n",ref);
336             }
337             ref=IDirectSoundBuffer_AddRef(secondary);
338             ok(ref==2,"IDirectSoundBuffer_AddRef() has %d references, "
339                "should have 2\n",ref);
340         }
341         /* release with buffer */
342         ref=IDirectSound_Release(dso);
343         ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",
344            ref);
345         if (ref!=0)
346             return DSERR_GENERIC;
347     } else
348         return rc;
349
350     return DS_OK;
351 }
352
353 static HRESULT test_primary(LPGUID lpGuid)
354 {
355     HRESULT rc;
356     LPDIRECTSOUND dso=NULL;
357     LPDIRECTSOUNDBUFFER primary=NULL,second=NULL,third=NULL;
358     DSBUFFERDESC bufdesc;
359     DSCAPS dscaps;
360     WAVEFORMATEX wfx;
361     int ref;
362
363     /* Create the DirectSound object */
364     rc=DirectSoundCreate(lpGuid,&dso,NULL);
365     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
366        "DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
367     if (rc!=DS_OK)
368         return rc;
369
370     /* Get the device capabilities */
371     ZeroMemory(&dscaps, sizeof(dscaps));
372     dscaps.dwSize=sizeof(dscaps);
373     rc=IDirectSound_GetCaps(dso,&dscaps);
374     ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %s\n",DXGetErrorString8(rc));
375     if (rc!=DS_OK)
376         goto EXIT;
377
378     /* DSOUND: Error: Invalid buffer description pointer */
379     rc=IDirectSound_CreateSoundBuffer(dso,0,0,NULL);
380     ok(rc==DSERR_INVALIDPARAM,
381        "IDirectSound_CreateSoundBuffer() should have failed: %s\n",
382        DXGetErrorString8(rc));
383
384     /* DSOUND: Error: Invalid buffer description pointer */
385     rc=IDirectSound_CreateSoundBuffer(dso,0,&primary,NULL);
386     ok(rc==DSERR_INVALIDPARAM && primary==0,
387        "IDirectSound_CreateSoundBuffer() should have failed: rc=%s,"
388        "dsbo=0x%lx\n",DXGetErrorString8(rc),(DWORD)primary);
389
390     /* DSOUND: Error: Invalid buffer description pointer */
391     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,0,NULL);
392     ok(rc==DSERR_INVALIDPARAM && primary==0,
393        "IDirectSound_CreateSoundBuffer() should have failed: rc=%s,"
394        "dsbo=0x%lx\n",DXGetErrorString8(rc),(DWORD)primary);
395
396     ZeroMemory(&bufdesc, sizeof(bufdesc));
397
398     /* DSOUND: Error: Invalid size */
399     /* DSOUND: Error: Invalid buffer description */
400     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
401     ok(rc==DSERR_INVALIDPARAM && primary==0,
402        "IDirectSound_CreateSoundBuffer() should have failed: rc=%s,"
403        "primary=0x%lx\n",DXGetErrorString8(rc),(DWORD)primary);
404
405     /* We must call SetCooperativeLevel before calling CreateSoundBuffer */
406     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
407     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
408     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
409        DXGetErrorString8(rc));
410     if (rc!=DS_OK)
411         goto EXIT;
412
413     /* Testing the primary buffer */
414     primary=NULL;
415     ZeroMemory(&bufdesc, sizeof(bufdesc));
416     bufdesc.dwSize=sizeof(bufdesc);
417     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRLVOLUME;
418     bufdesc.lpwfxFormat = &wfx;
419     init_format(&wfx,WAVE_FORMAT_PCM,11025,8,2);
420     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
421     ok(rc==DSERR_INVALIDPARAM,"IDirectSound_CreateSoundBuffer() should have "
422        "returned DSERR_INVALIDPARAM, returned: %s\n", DXGetErrorString8(rc));
423     if (rc==DS_OK && primary!=NULL)
424         IDirectSoundBuffer_Release(primary);
425
426     primary=NULL;
427     ZeroMemory(&bufdesc, sizeof(bufdesc));
428     bufdesc.dwSize=sizeof(bufdesc);
429     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRLVOLUME;
430     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
431     ok(rc==DS_OK && primary!=NULL,
432        "IDirectSound_CreateSoundBuffer() failed to create a primary buffer: "
433        "%s\n",DXGetErrorString8(rc));
434     if (rc==DS_OK && primary!=NULL) {
435         LONG vol;
436
437         /* Try to create a second primary buffer */
438         /* DSOUND: Error: The primary buffer already exists.
439          * Any changes made to the buffer description will be ignored. */
440         rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&second,NULL);
441         ok(rc==DS_OK && second==primary,
442            "IDirectSound_CreateSoundBuffer() should have returned original "
443            "primary buffer: %s\n",DXGetErrorString8(rc));
444         ref=IDirectSoundBuffer_Release(second);
445         ok(ref==1,"IDirectSoundBuffer_Release() primary has %d references, "
446            "should have 1\n",ref);
447
448         /* Try to duplicate a primary buffer */
449         /* DSOUND: Error: Can't duplicate primary buffers */
450         rc=IDirectSound_DuplicateSoundBuffer(dso,primary,&third);
451         /* rc=0x88780032 */
452         ok(rc!=DS_OK,"IDirectSound_DuplicateSoundBuffer() primary buffer "
453            "should have failed %s\n",DXGetErrorString8(rc));
454
455         rc=IDirectSoundBuffer_GetVolume(primary,&vol);
456         ok(rc==DS_OK,"IDirectSoundBuffer_GetVolume() failed: %s\n",
457            DXGetErrorString8(rc));
458
459         if (winetest_interactive) {
460             trace("Playing a 5 seconds reference tone at the current "
461                   "volume.\n");
462             if (rc==DS_OK)
463                 trace("(the current volume is %ld according to DirectSound)\n",
464                       vol);
465             trace("All subsequent tones should be identical to this one.\n");
466             trace("Listen for stutter, changes in pitch, volume, etc.\n");
467         }
468         test_buffer(dso,primary,1,FALSE,0,FALSE,0,winetest_interactive &&
469                     !(dscaps.dwFlags & DSCAPS_EMULDRIVER),5.0,0,0,0,0,FALSE,0);
470
471         ref=IDirectSoundBuffer_Release(primary);
472         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
473            "should have 0\n",ref);
474     }
475
476     /* Set the CooperativeLevel back to normal */
477     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
478     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
479     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
480        DXGetErrorString8(rc));
481
482 EXIT:
483     ref=IDirectSound_Release(dso);
484     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
485     if (ref!=0)
486         return DSERR_GENERIC;
487
488     return rc;
489 }
490
491 /*
492  * Test the primary buffer at different formats while keeping the
493  * secondary buffer at a constant format.
494  */
495 static HRESULT test_primary_secondary(LPGUID lpGuid)
496 {
497     HRESULT rc;
498     LPDIRECTSOUND dso=NULL;
499     LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
500     DSBUFFERDESC bufdesc;
501     DSCAPS dscaps;
502     WAVEFORMATEX wfx, wfx2;
503     int f,ref;
504
505     /* Create the DirectSound object */
506     rc=DirectSoundCreate(lpGuid,&dso,NULL);
507     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
508        "DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
509     if (rc!=DS_OK)
510         return rc;
511
512     /* Get the device capabilities */
513     ZeroMemory(&dscaps, sizeof(dscaps));
514     dscaps.dwSize=sizeof(dscaps);
515     rc=IDirectSound_GetCaps(dso,&dscaps);
516     ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %s\n",DXGetErrorString8(rc));
517     if (rc!=DS_OK)
518         goto EXIT;
519
520     /* We must call SetCooperativeLevel before creating primary buffer */
521     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
522     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
523     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
524        DXGetErrorString8(rc));
525     if (rc!=DS_OK)
526         goto EXIT;
527
528     ZeroMemory(&bufdesc, sizeof(bufdesc));
529     bufdesc.dwSize=sizeof(bufdesc);
530     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
531     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
532     ok(rc==DS_OK && primary!=NULL,
533        "IDirectSound_CreateSoundBuffer() failed to create a primary buffer "
534        "%s\n",DXGetErrorString8(rc));
535
536     if (rc==DS_OK && primary!=NULL) {
537         for (f=0;f<NB_FORMATS;f++) {
538             /* We must call SetCooperativeLevel to be allowed to call
539              * SetFormat */
540             /* DSOUND: Setting DirectSound cooperative level to
541              * DSSCL_PRIORITY */
542             rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
543             ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
544                DXGetErrorString8(rc));
545             if (rc!=DS_OK)
546                 goto EXIT;
547
548             init_format(&wfx,WAVE_FORMAT_PCM,formats[f][0],formats[f][1],
549                         formats[f][2]);
550             wfx2=wfx;
551             rc=IDirectSoundBuffer_SetFormat(primary,&wfx);
552             ok(rc==DS_OK,"IDirectSoundBuffer_SetFormat() failed: %s\n",
553                DXGetErrorString8(rc));
554
555             /* There is no garantee that SetFormat will actually change the
556              * format to what we asked for. It depends on what the soundcard
557              * supports. So we must re-query the format.
558              */
559             rc=IDirectSoundBuffer_GetFormat(primary,&wfx,sizeof(wfx),NULL);
560             ok(rc==DS_OK,"IDirectSoundBuffer_GetFormat() failed: %s\n",
561                DXGetErrorString8(rc));
562             if (rc==DS_OK &&
563                 (wfx.wFormatTag!=wfx2.wFormatTag ||
564                  wfx.nSamplesPerSec!=wfx2.nSamplesPerSec ||
565                  wfx.wBitsPerSample!=wfx2.wBitsPerSample ||
566                  wfx.nChannels!=wfx2.nChannels)) {
567                 trace("Requested primary format tag=0x%04x %ldx%dx%d "
568                       "avg.B/s=%ld align=%d\n",
569                       wfx2.wFormatTag,wfx2.nSamplesPerSec,wfx2.wBitsPerSample,
570                       wfx2.nChannels,wfx2.nAvgBytesPerSec,wfx2.nBlockAlign);
571                 trace("Got tag=0x%04x %ldx%dx%d avg.B/s=%ld align=%d\n",
572                       wfx.wFormatTag,wfx.nSamplesPerSec,wfx.wBitsPerSample,
573                       wfx.nChannels,wfx.nAvgBytesPerSec,wfx.nBlockAlign);
574             }
575
576             /* Set the CooperativeLevel back to normal */
577             /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
578             rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
579             ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
580                DXGetErrorString8(rc));
581
582             init_format(&wfx2,WAVE_FORMAT_PCM,11025,16,2);
583
584             secondary=NULL;
585             ZeroMemory(&bufdesc, sizeof(bufdesc));
586             bufdesc.dwSize=sizeof(bufdesc);
587             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
588             bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
589                                         wfx.nBlockAlign);
590             bufdesc.lpwfxFormat=&wfx2;
591             if (winetest_interactive) {
592                 trace("  Testing a primary buffer at %ldx%dx%d with a "
593                       "secondary buffer at %ldx%dx%d\n",
594                       wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,
595                       wfx2.nSamplesPerSec,wfx2.wBitsPerSample,wfx2.nChannels);
596             }
597             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
598             ok(rc==DS_OK && secondary!=NULL,
599                "IDirectSound_CreateSoundBuffer() failed to create a secondary "
600                "buffer %s\n",DXGetErrorString8(rc));
601
602             if (rc==DS_OK && secondary!=NULL) {
603                 test_buffer(dso,secondary,0,FALSE,0,FALSE,0,
604                             winetest_interactive,1.0,0,NULL,0,0,FALSE,0);
605
606                 ref=IDirectSoundBuffer_Release(secondary);
607                 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
608                    "should have 0\n",ref);
609             }
610         }
611
612         ref=IDirectSoundBuffer_Release(primary);
613         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
614            "should have 0\n",ref);
615     }
616
617     /* Set the CooperativeLevel back to normal */
618     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
619     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
620     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
621        DXGetErrorString8(rc));
622
623 EXIT:
624     ref=IDirectSound_Release(dso);
625     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
626     if (ref!=0)
627         return DSERR_GENERIC;
628
629     return rc;
630 }
631
632 static HRESULT test_secondary(LPGUID lpGuid)
633 {
634     HRESULT rc;
635     LPDIRECTSOUND dso=NULL;
636     LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
637     DSBUFFERDESC bufdesc;
638     DSCAPS dscaps;
639     WAVEFORMATEX wfx, wfx1;
640     DWORD f;
641     int ref;
642
643     /* Create the DirectSound object */
644     rc=DirectSoundCreate(lpGuid,&dso,NULL);
645     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
646        "DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
647     if (rc!=DS_OK)
648         return rc;
649
650     /* Get the device capabilities */
651     ZeroMemory(&dscaps, sizeof(dscaps));
652     dscaps.dwSize=sizeof(dscaps);
653     rc=IDirectSound_GetCaps(dso,&dscaps);
654     ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %s\n",DXGetErrorString8(rc));
655     if (rc!=DS_OK)
656         goto EXIT;
657
658     /* We must call SetCooperativeLevel before creating primary buffer */
659     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
660     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
661     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
662        DXGetErrorString8(rc));
663     if (rc!=DS_OK)
664         goto EXIT;
665
666     ZeroMemory(&bufdesc, sizeof(bufdesc));
667     bufdesc.dwSize=sizeof(bufdesc);
668     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
669     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
670     ok(rc==DS_OK && primary!=NULL,
671        "IDirectSound_CreateSoundBuffer() failed to create a primary buffer "
672        "%s\n",DXGetErrorString8(rc));
673
674     if (rc==DS_OK && primary!=NULL) {
675         rc=IDirectSoundBuffer_GetFormat(primary,&wfx1,sizeof(wfx1),NULL);
676         ok(rc==DS_OK,"IDirectSoundBuffer8_Getformat() failed: %s\n",
677            DXGetErrorString8(rc));
678         if (rc!=DS_OK)
679             goto EXIT1;
680
681         for (f=0;f<NB_FORMATS;f++) {
682             init_format(&wfx,WAVE_FORMAT_PCM,formats[f][0],formats[f][1],
683                         formats[f][2]);
684             secondary=NULL;
685             ZeroMemory(&bufdesc, sizeof(bufdesc));
686             bufdesc.dwSize=sizeof(bufdesc);
687             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
688             bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
689                                         wfx.nBlockAlign);
690             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
691             ok(rc==DSERR_INVALIDPARAM,"IDirectSound_CreateSoundBuffer() "
692                "should have returned DSERR_INVALIDPARAM, returned: %s\n",
693                DXGetErrorString8(rc));
694             if (rc==DS_OK && secondary!=NULL)
695                 IDirectSoundBuffer_Release(secondary);
696
697             secondary=NULL;
698             ZeroMemory(&bufdesc, sizeof(bufdesc));
699             bufdesc.dwSize=sizeof(bufdesc);
700             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
701             bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
702                                         wfx.nBlockAlign);
703             bufdesc.lpwfxFormat=&wfx;
704             if (winetest_interactive) {
705                 trace("  Testing a secondary buffer at %ldx%dx%d "
706                       "with a primary buffer at %ldx%dx%d\n",
707                       wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,
708                       wfx1.nSamplesPerSec,wfx1.wBitsPerSample,wfx1.nChannels);
709             }
710             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
711             ok(rc==DS_OK && secondary!=NULL,
712                "IDirectSound_CreateSoundBuffer() failed to create a secondary "
713                "buffer %s\n",DXGetErrorString8(rc));
714
715             if (rc==DS_OK && secondary!=NULL) {
716                 test_buffer(dso,secondary,0,FALSE,0,FALSE,0,
717                             winetest_interactive,1.0,0,NULL,0,0,FALSE,0);
718
719                 ref=IDirectSoundBuffer_Release(secondary);
720                 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
721                    "should have 0\n",ref);
722             }
723         }
724 EXIT1:
725         ref=IDirectSoundBuffer_Release(primary);
726         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
727            "should have 0\n",ref);
728     }
729
730     /* Set the CooperativeLevel back to normal */
731     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
732     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
733     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
734        DXGetErrorString8(rc));
735
736 EXIT:
737     ref=IDirectSound_Release(dso);
738     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
739     if (ref!=0)
740         return DSERR_GENERIC;
741
742     return rc;
743 }
744
745 static HRESULT test_block_align(LPGUID lpGuid)
746 {
747     HRESULT rc;
748     LPDIRECTSOUND dso=NULL;
749     LPDIRECTSOUNDBUFFER secondary=NULL;
750     DSBUFFERDESC bufdesc;
751     DSBCAPS dsbcaps;
752     WAVEFORMATEX wfx;
753     int ref;
754
755     /* Create the DirectSound object */
756     rc=DirectSoundCreate(lpGuid,&dso,NULL);
757     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
758        "DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
759     if (rc!=DS_OK)
760         return rc;
761
762     init_format(&wfx,WAVE_FORMAT_PCM,11025,16,2);
763     ZeroMemory(&bufdesc, sizeof(bufdesc));
764     bufdesc.dwSize=sizeof(bufdesc);
765     bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
766     bufdesc.dwBufferBytes=wfx.nAvgBytesPerSec + 1;
767     bufdesc.lpwfxFormat=&wfx;
768     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
769     ok(rc==DS_OK,"IDirectSound_CreateSoundBuffer() "
770        "should have returned DS_OK, returned: %s\n",
771        DXGetErrorString8(rc));
772
773     if (rc==DS_OK && secondary!=NULL) {
774         ZeroMemory(&dsbcaps, sizeof(dsbcaps));
775         dsbcaps.dwSize = sizeof(dsbcaps);
776         rc=IDirectSoundBuffer_GetCaps(secondary,&dsbcaps);
777         ok(rc==DS_OK,"IDirectSoundBuffer_GetCaps() should have returned DS_OK, "
778            "returned: %s\n", DXGetErrorString8(rc));
779         if (rc==DS_OK)
780             ok(dsbcaps.dwBufferBytes==(wfx.nAvgBytesPerSec + wfx.nBlockAlign),
781                "Buffer size not a multiple of nBlockAlign: requested %ld, "
782                "got %ld, should be %ld\n", bufdesc.dwBufferBytes,
783                dsbcaps.dwBufferBytes, wfx.nAvgBytesPerSec + wfx.nBlockAlign);
784         ref=IDirectSoundBuffer_Release(secondary);
785         ok(ref==0,"IDirectSoundBuffer_Release() secondary has %d references, "
786            "should have 0\n",ref);
787     }
788
789     ref=IDirectSound_Release(dso);
790     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
791     if (ref!=0)
792         return DSERR_GENERIC;
793
794     return rc;
795 }
796
797 static struct fmt {
798     int bits;
799     int channels;
800 } fmts[] = { { 8, 1 }, { 8, 2 }, { 16, 1 }, {16, 2 } };
801
802 static HRESULT test_frequency(LPGUID lpGuid)
803 {
804     HRESULT rc;
805     LPDIRECTSOUND dso=NULL;
806     LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
807     DSBUFFERDESC bufdesc;
808     DSCAPS dscaps;
809     WAVEFORMATEX wfx, wfx1;
810     DWORD f, r;
811     int ref;
812     int rates[] = { 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100,
813                     48000, 96000 };
814
815     /* Create the DirectSound object */
816     rc=DirectSoundCreate(lpGuid,&dso,NULL);
817     ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED,
818        "DirectSoundCreate() failed: %s\n",DXGetErrorString8(rc));
819     if (rc!=DS_OK)
820         return rc;
821
822     /* Get the device capabilities */
823     ZeroMemory(&dscaps, sizeof(dscaps));
824     dscaps.dwSize=sizeof(dscaps);
825     rc=IDirectSound_GetCaps(dso,&dscaps);
826     ok(rc==DS_OK,"IDirectSound_GetCaps() failed: %s\n",DXGetErrorString8(rc));
827     if (rc!=DS_OK)
828         goto EXIT;
829
830     /* We must call SetCooperativeLevel before creating primary buffer */
831     /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
832     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
833     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
834        DXGetErrorString8(rc));
835     if (rc!=DS_OK)
836         goto EXIT;
837
838     ZeroMemory(&bufdesc, sizeof(bufdesc));
839     bufdesc.dwSize=sizeof(bufdesc);
840     bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
841     rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
842     ok(rc==DS_OK && primary!=NULL,
843        "IDirectSound_CreateSoundBuffer() failed to create a primary buffer "
844        "%s\n",DXGetErrorString8(rc));
845
846     if (rc==DS_OK && primary!=NULL) {
847         rc=IDirectSoundBuffer_GetFormat(primary,&wfx1,sizeof(wfx1),NULL);
848         ok(rc==DS_OK,"IDirectSoundBuffer8_Getformat() failed: %s\n",
849            DXGetErrorString8(rc));
850         if (rc!=DS_OK)
851             goto EXIT1;
852
853         for (f=0;f<sizeof(fmts)/sizeof(fmts[0]);f++) {
854         for (r=0;r<sizeof(rates)/sizeof(rates[0]);r++) {
855             init_format(&wfx,WAVE_FORMAT_PCM,11025,fmts[f].bits,
856                         fmts[f].channels);
857             secondary=NULL;
858             ZeroMemory(&bufdesc, sizeof(bufdesc));
859             bufdesc.dwSize=sizeof(bufdesc);
860             bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2|DSBCAPS_CTRLFREQUENCY;
861             bufdesc.dwBufferBytes=align((wfx.nAvgBytesPerSec*rates[r]/11025)*
862                                         BUFFER_LEN/1000,wfx.nBlockAlign);
863             bufdesc.lpwfxFormat=&wfx;
864             if (winetest_interactive) {
865                 trace("  Testing a secondary buffer at %ldx%dx%d "
866                       "with a primary buffer at %ldx%dx%d\n",
867                       wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,
868                       wfx1.nSamplesPerSec,wfx1.wBitsPerSample,wfx1.nChannels);
869             }
870             rc=IDirectSound_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
871             ok(rc==DS_OK && secondary!=NULL,
872                "IDirectSound_CreateSoundBuffer() failed to create a secondary "
873                "buffer %s\n",DXGetErrorString8(rc));
874
875             if (rc==DS_OK && secondary!=NULL) {
876                 test_buffer(dso,secondary,0,FALSE,0,FALSE,0,
877                             winetest_interactive,1.0,0,NULL,0,0,TRUE,rates[r]);
878
879                 ref=IDirectSoundBuffer_Release(secondary);
880                 ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
881                    "should have 0\n",ref);
882             }
883         }
884         }
885 EXIT1:
886         ref=IDirectSoundBuffer_Release(primary);
887         ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
888            "should have 0\n",ref);
889     }
890
891     /* Set the CooperativeLevel back to normal */
892     /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
893     rc=IDirectSound_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
894     ok(rc==DS_OK,"IDirectSound_SetCooperativeLevel() failed: %s\n",
895        DXGetErrorString8(rc));
896
897 EXIT:
898     ref=IDirectSound_Release(dso);
899     ok(ref==0,"IDirectSound_Release() has %d references, should have 0\n",ref);
900     if (ref!=0)
901         return DSERR_GENERIC;
902
903     return rc;
904 }
905
906 static BOOL WINAPI dsenum_callback(LPGUID lpGuid, LPCSTR lpcstrDescription,
907                                    LPCSTR lpcstrModule, LPVOID lpContext)
908 {
909     HRESULT rc;
910     trace("*** Testing %s - %s ***\n",lpcstrDescription,lpcstrModule);
911     rc = test_dsound(lpGuid);
912     if (rc == DSERR_NODRIVER)
913         trace("  No Driver\n");
914     else if (rc == DSERR_ALLOCATED)
915         trace("  Already In Use\n");
916     else if (rc == E_FAIL)
917         trace("  No Device\n");
918     else {
919         test_block_align(lpGuid);
920         test_primary(lpGuid);
921         test_primary_secondary(lpGuid);
922         test_secondary(lpGuid);
923         test_frequency(lpGuid);
924     }
925
926     return 1;
927 }
928
929 static void dsound_tests()
930 {
931     HRESULT rc;
932     rc=DirectSoundEnumerateA(&dsenum_callback,NULL);
933     ok(rc==DS_OK,"DirectSoundEnumerateA() failed: %s\n",DXGetErrorString8(rc));
934 }
935
936 START_TEST(dsound)
937 {
938     CoInitialize(NULL);
939
940     trace("DLL Version: %s\n", get_file_version("dsound.dll"));
941
942     IDirectSound_tests();
943     dsound_tests();
944
945     CoUninitialize();
946 }