ds3d8.c 47.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Tests the panning and 3D functions of DirectSound
 *
 * Part of this test involves playing test tones. But this only makes
 * sense if someone is going to carefully listen to it, and would only
 * bother everyone else.
 * So this is only done if the test is being run in interactive mode.
 *
 * Copyright (c) 2002-2004 Francois Gouget
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
23
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 25 26 27 28 29 30 31
 */

#include <windows.h>

#include <math.h>

#include "wine/test.h"
#include "dsound.h"
32
#include "mmreg.h"
33 34
#include "ks.h"
#include "ksmedia.h"
35 36
#include "dsound_test.h"

37
static HRESULT (WINAPI *pDirectSoundEnumerateA)(LPDSENUMCALLBACKA,LPVOID)=NULL;
38 39
static HRESULT (WINAPI *pDirectSoundCreate8)(LPCGUID,LPDIRECTSOUND8*,LPUNKNOWN)=NULL;

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
typedef struct {
    char* wave;
    DWORD wave_len;

    LPDIRECTSOUNDBUFFER dsbo;
    LPWAVEFORMATEX wfx;
    DWORD buffer_size;
    DWORD written;
    DWORD played;
    DWORD offset;
} play_state_t;

static int buffer_refill8(play_state_t* state, DWORD size)
{
    LPVOID ptr1,ptr2;
    DWORD len1,len2;
    HRESULT rc;

    if (size>state->wave_len-state->written)
        size=state->wave_len-state->written;

    rc=IDirectSoundBuffer_Lock(state->dsbo,state->offset,size,
                               &ptr1,&len1,&ptr2,&len2,0);
63
    ok(rc==DS_OK,"IDirectSoundBuffer_Lock() failed: %08x\n", rc);
64 65 66 67 68 69 70 71 72 73 74
    if (rc!=DS_OK)
        return -1;

    memcpy(ptr1,state->wave+state->written,len1);
    state->written+=len1;
    if (ptr2!=NULL) {
        memcpy(ptr2,state->wave+state->written,len2);
        state->written+=len2;
    }
    state->offset=state->written % state->buffer_size;
    rc=IDirectSoundBuffer_Unlock(state->dsbo,ptr1,len1,ptr2,len2);
75
    ok(rc==DS_OK,"IDirectSoundBuffer_Unlock() failed: %08x\n", rc);
76 77 78 79 80 81 82 83 84 85 86 87 88 89
    if (rc!=DS_OK)
        return -1;
    return size;
}

static int buffer_silence8(play_state_t* state, DWORD size)
{
    LPVOID ptr1,ptr2;
    DWORD len1,len2;
    HRESULT rc;
    BYTE s;

    rc=IDirectSoundBuffer_Lock(state->dsbo,state->offset,size,
                               &ptr1,&len1,&ptr2,&len2,0);
90
    ok(rc==DS_OK,"IDirectSoundBuffer_Lock() failed: %08x\n", rc);
91 92 93 94 95 96 97 98 99 100
    if (rc!=DS_OK)
        return -1;

    s=(state->wfx->wBitsPerSample==8?0x80:0);
    memset(ptr1,s,len1);
    if (ptr2!=NULL) {
        memset(ptr2,s,len2);
    }
    state->offset=(state->offset+size) % state->buffer_size;
    rc=IDirectSoundBuffer_Unlock(state->dsbo,ptr1,len1,ptr2,len2);
101
    ok(rc==DS_OK,"IDirectSoundBuffer_Unlock() failed: %08x\n", rc);
102 103 104 105 106 107 108 109 110 111 112
    if (rc!=DS_OK)
        return -1;
    return size;
}

static int buffer_service8(play_state_t* state)
{
    DWORD last_play_pos,play_pos,buf_free;
    HRESULT rc;

    rc=IDirectSoundBuffer_GetCurrentPosition(state->dsbo,&play_pos,NULL);
113
    ok(rc==DS_OK,"IDirectSoundBuffer_GetCurrentPosition() failed: %08x\n", rc);
114 115 116 117 118 119 120 121 122 123 124 125
    if (rc!=DS_OK) {
        goto STOP;
    }

    /* Update the amount played */
    last_play_pos=state->played % state->buffer_size;
    if (play_pos<last_play_pos)
        state->played+=state->buffer_size-last_play_pos+play_pos;
    else
        state->played+=play_pos-last_play_pos;

    if (winetest_debug > 1)
126
        trace("buf size=%d last_play_pos=%d play_pos=%d played=%d / %d\n",
127 128
              state->buffer_size,last_play_pos,play_pos,state->played,
              state->wave_len);
129 130 131 132 133 134 135 136 137 138 139 140 141 142

    if (state->played>state->wave_len)
    {
        /* Everything has been played */
        goto STOP;
    }

    /* Refill the buffer */
    if (state->offset<=play_pos)
        buf_free=play_pos-state->offset;
    else
        buf_free=state->buffer_size-state->offset+play_pos;

    if (winetest_debug > 1)
143
        trace("offset=%d free=%d written=%d / %d\n",
144 145 146 147 148 149 150 151 152 153 154
              state->offset,buf_free,state->written,state->wave_len);
    if (buf_free==0)
        return 1;

    if (state->written<state->wave_len)
    {
        int w=buffer_refill8(state,buf_free);
        if (w==-1)
            goto STOP;
        buf_free-=w;
        if (state->written==state->wave_len && winetest_debug > 1)
155
            trace("last sound byte at %d\n",
156 157 158 159 160 161
                  (state->written % state->buffer_size));
    }

    if (buf_free>0) {
        /* Fill with silence */
        if (winetest_debug > 1)
162
            trace("writing %d bytes of silence\n",buf_free);
163 164 165 166 167 168 169 170 171
        if (buffer_silence8(state,buf_free)==-1)
            goto STOP;
    }
    return 1;

STOP:
    if (winetest_debug > 1)
        trace("stopping playback\n");
    rc=IDirectSoundBuffer_Stop(state->dsbo);
172
    ok(rc==DS_OK,"IDirectSoundBuffer_Stop() failed: %08x\n", rc);
173 174 175
    return 0;
}

176
void test_buffer8(LPDIRECTSOUND8 dso, LPDIRECTSOUNDBUFFER * dsbo,
177 178 179 180 181 182 183 184 185
                  BOOL is_primary, BOOL set_volume, LONG volume,
                  BOOL set_pan, LONG pan, BOOL play, double duration,
                  BOOL buffer3d, LPDIRECTSOUND3DLISTENER listener,
                  BOOL move_listener, BOOL move_sound)
{
    HRESULT rc;
    DSBCAPS dsbcaps;
    WAVEFORMATEX wfx,wfx2;
    DWORD size,status,freq;
186
    BOOL ieee = FALSE;
187 188 189
    int ref;

    /* DSOUND: Error: Invalid caps pointer */
190
    rc=IDirectSoundBuffer_GetCaps(*dsbo,0);
191
    ok(rc==DSERR_INVALIDPARAM,"IDirectSoundBuffer_GetCaps() should have "
192
       "returned DSERR_INVALIDPARAM, returned: %08x\n",rc);
193 194 195 196

    ZeroMemory(&dsbcaps, sizeof(dsbcaps));

    /* DSOUND: Error: Invalid caps pointer */
197
    rc=IDirectSoundBuffer_GetCaps(*dsbo,&dsbcaps);
198
    ok(rc==DSERR_INVALIDPARAM,"IDirectSoundBuffer_GetCaps() should have "
199
       "returned DSERR_INVALIDPARAM, returned: %08x\n",rc);
200 201

    dsbcaps.dwSize=sizeof(dsbcaps);
202
    rc=IDirectSoundBuffer_GetCaps(*dsbo,&dsbcaps);
203
    ok(rc==DS_OK,"IDirectSoundBuffer_GetCaps() failed: %08x\n", rc);
204
    if (rc==DS_OK && winetest_debug > 1) {
205
        trace("    Caps: flags=0x%08x size=%d\n",dsbcaps.dwFlags,
206 207 208
              dsbcaps.dwBufferBytes);
    }

209
    /* Query the format size. */
210
    size=0;
211
    rc=IDirectSoundBuffer_GetFormat(*dsbo,NULL,0,&size);
212
    ok(rc==DS_OK && size!=0,"IDirectSoundBuffer_GetFormat() should have "
213
       "returned the needed size: rc=%08x size=%d\n",rc,size);
214

215 216 217 218 219
    ok(size == sizeof(WAVEFORMATEX) || size == sizeof(WAVEFORMATEXTENSIBLE),
       "Expected a correct structure size, got %d\n", size);

    if (size == sizeof(WAVEFORMATEX)) {
        rc=IDirectSoundBuffer_GetFormat(*dsbo,&wfx,size,NULL);
220
        ieee = (wfx.wFormatTag == WAVE_FORMAT_IEEE_FLOAT);
221
    } else if (size == sizeof(WAVEFORMATEXTENSIBLE)) {
222
        WAVEFORMATEXTENSIBLE wfxe;
223
        rc=IDirectSoundBuffer_GetFormat(*dsbo,(WAVEFORMATEX*)&wfxe,size,NULL);
224
        wfx = wfxe.Format;
225
        ieee = IsEqualGUID(&wfxe.SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT);
226
    }
227
    ok(rc==DS_OK,"IDirectSoundBuffer_GetFormat() failed: %08x\n", rc);
228
    if (rc==DS_OK && winetest_debug > 1) {
229
        trace("    Format: %s tag=0x%04x %dx%dx%d avg.B/s=%d align=%d\n",
230
              is_primary ? "Primary" : "Secondary",
231 232 233 234 235
              wfx.wFormatTag,wfx.nSamplesPerSec,wfx.wBitsPerSample,
              wfx.nChannels,wfx.nAvgBytesPerSec,wfx.nBlockAlign);
    }

    /* DSOUND: Error: Invalid frequency buffer */
236
    rc=IDirectSoundBuffer_GetFrequency(*dsbo,0);
237
    ok(rc==DSERR_INVALIDPARAM,"IDirectSoundBuffer_GetFrequency() should have "
238
       "returned DSERR_INVALIDPARAM, returned: %08x\n",rc);
239 240

    /* DSOUND: Error: Primary buffers don't support CTRLFREQUENCY */
241
    rc=IDirectSoundBuffer_GetFrequency(*dsbo,&freq);
242 243
    ok((rc==DS_OK && !is_primary) || (rc==DSERR_CONTROLUNAVAIL&&is_primary) ||
       (rc==DSERR_CONTROLUNAVAIL&&!(dsbcaps.dwFlags&DSBCAPS_CTRLFREQUENCY)),
244
       "IDirectSoundBuffer_GetFrequency() failed: %08x\n",rc);
245
    if (rc==DS_OK) {
246
        ok(freq==wfx.nSamplesPerSec,"The frequency returned by GetFrequency "
247
           "%d does not match the format %d\n",freq,wfx.nSamplesPerSec);
248 249 250
    }

    /* DSOUND: Error: Invalid status pointer */
251
    rc=IDirectSoundBuffer_GetStatus(*dsbo,0);
252
    ok(rc==DSERR_INVALIDPARAM,"IDirectSoundBuffer_GetStatus() should have "
253
       "returned DSERR_INVALIDPARAM, returned: %08x\n",rc);
254

255
    rc=IDirectSoundBuffer_GetStatus(*dsbo,&status);
256
    ok(rc==DS_OK,"IDirectSoundBuffer_GetStatus() failed: %08x\n", rc);
257
    ok(status==0,"status=0x%x instead of 0\n",status);
258 259

    if (is_primary) {
260
        DSBCAPS new_dsbcaps;
261 262 263
        /* We must call SetCooperativeLevel to be allowed to call SetFormat */
        /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
        rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
264
        ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_PRIORITY) "
265
           "failed: %08x\n",rc);
266 267 268 269
        if (rc!=DS_OK)
            return;

        /* DSOUND: Error: Invalid format pointer */
270
        rc=IDirectSoundBuffer_SetFormat(*dsbo,0);
271
        ok(rc==DSERR_INVALIDPARAM,"IDirectSoundBuffer_SetFormat() should have "
272
           "returned DSERR_INVALIDPARAM, returned: %08x\n",rc);
273 274

        init_format(&wfx2,WAVE_FORMAT_PCM,11025,16,2);
275
        rc=IDirectSoundBuffer_SetFormat(*dsbo,&wfx2);
276 277
        ok(rc==DS_OK,"IDirectSoundBuffer_SetFormat(%s) failed: %08x\n",
           format_string(&wfx2), rc);
278

279
        /* There is no guarantee that SetFormat will actually change the
280 281 282
	 * format to what we asked for. It depends on what the soundcard
	 * supports. So we must re-query the format.
	 */
283
        rc=IDirectSoundBuffer_GetFormat(*dsbo,&wfx,sizeof(wfx),NULL);
284
        ok(rc==DS_OK,"IDirectSoundBuffer_GetFormat() failed: %08x\n", rc);
285 286 287 288 289
        if (rc==DS_OK &&
            (wfx.wFormatTag!=wfx2.wFormatTag ||
             wfx.nSamplesPerSec!=wfx2.nSamplesPerSec ||
             wfx.wBitsPerSample!=wfx2.wBitsPerSample ||
             wfx.nChannels!=wfx2.nChannels)) {
290
            trace("Requested format tag=0x%04x %dx%dx%d avg.B/s=%d align=%d\n",
291 292
                  wfx2.wFormatTag,wfx2.nSamplesPerSec,wfx2.wBitsPerSample,
                  wfx2.nChannels,wfx2.nAvgBytesPerSec,wfx2.nBlockAlign);
293
            trace("Got tag=0x%04x %dx%dx%d avg.B/s=%d align=%d\n",
294 295 296 297
                  wfx.wFormatTag,wfx.nSamplesPerSec,wfx.wBitsPerSample,
                  wfx.nChannels,wfx.nAvgBytesPerSec,wfx.nBlockAlign);
        }

298 299 300
        ZeroMemory(&new_dsbcaps, sizeof(new_dsbcaps));
        new_dsbcaps.dwSize = sizeof(new_dsbcaps);
        rc=IDirectSoundBuffer_GetCaps(*dsbo,&new_dsbcaps);
301
        ok(rc==DS_OK,"IDirectSoundBuffer_GetCaps() failed: %08x\n", rc);
302
        if (rc==DS_OK && winetest_debug > 1) {
303
            trace("    new Caps: flags=0x%08x size=%d\n",new_dsbcaps.dwFlags,
304 305 306 307
                  new_dsbcaps.dwBufferBytes);
        }

        /* Check for primary buffer size change */
308 309
        ok(new_dsbcaps.dwBufferBytes == dsbcaps.dwBufferBytes,
           "    buffer size changed after SetFormat() - "
310
           "previous size was %u, current size is %u\n",
311
           dsbcaps.dwBufferBytes, new_dsbcaps.dwBufferBytes);
312
        dsbcaps.dwBufferBytes = new_dsbcaps.dwBufferBytes;
313 314

        /* Check for primary buffer flags change */
315 316
        ok(new_dsbcaps.dwFlags == dsbcaps.dwFlags,
           "    flags changed after SetFormat() - "
317
           "previous flags were %08x, current flags are %08x\n",
318
           dsbcaps.dwFlags, new_dsbcaps.dwFlags);
319

320 321 322
        /* Set the CooperativeLevel back to normal */
        /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
        rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
323
        ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_NORMAL) "
324
           "failed: %08x\n",rc);
325 326 327 328 329 330 331 332
    }

    if (play) {
        play_state_t state;
        DS3DLISTENER listener_param;
        LPDIRECTSOUND3DBUFFER buffer=NULL;
        DS3DBUFFER buffer_param;
        DWORD start_time,now;
333 334
        LPVOID buffer1;
        DWORD length1;
335

336
        if (winetest_interactive) {
337
            trace("    Playing %g second 440Hz tone at %dx%dx%d\n", duration,
338 339
                  wfx.nSamplesPerSec, wfx.wBitsPerSample,wfx.nChannels);
        }
340 341 342

        if (is_primary) {
            /* We must call SetCooperativeLevel to be allowed to call Lock */
343 344 345 346
            /* DSOUND: Setting DirectSound cooperative level to
             * DSSCL_WRITEPRIMARY */
            rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),
                                                 DSSCL_WRITEPRIMARY);
347
            ok(rc==DS_OK,
348
               "IDirectSound8_SetCooperativeLevel(DSSCL_WRITEPRIMARY) failed: %08x\n",rc);
349 350 351 352 353 354
            if (rc!=DS_OK)
                return;
        }
        if (buffer3d) {
            LPDIRECTSOUNDBUFFER temp_buffer;

355
            rc=IDirectSoundBuffer_QueryInterface(*dsbo,&IID_IDirectSound3DBuffer,
356
                                                 (LPVOID *)&buffer);
357
            ok(rc==DS_OK,"IDirectSoundBuffer_QueryInterface() failed: %08x\n", rc);
358 359 360 361
            if (rc!=DS_OK)
                return;

            /* check the COM interface */
362
            rc=IDirectSoundBuffer_QueryInterface(*dsbo, &IID_IDirectSoundBuffer,
363 364
                                                 (LPVOID *)&temp_buffer);
            ok(rc==DS_OK && temp_buffer!=NULL,
365
               "IDirectSoundBuffer_QueryInterface() failed: %08x\n", rc);
366 367
            ok(temp_buffer==*dsbo,"COM interface broken: %p != %p\n",
               temp_buffer,*dsbo);
368
            ref=IDirectSoundBuffer_Release(temp_buffer);
369 370
            ok(ref==1,"IDirectSoundBuffer_Release() has %d references, "
               "should have 1\n",ref);
371 372

            temp_buffer=NULL;
373
            rc=IDirectSound3DBuffer_QueryInterface(*dsbo, &IID_IDirectSoundBuffer,
374 375
                                                   (LPVOID *)&temp_buffer);
            ok(rc==DS_OK && temp_buffer!=NULL,
376
               "IDirectSound3DBuffer_QueryInterface() failed: %08x\n", rc);
377 378
            ok(temp_buffer==*dsbo,"COM interface broken: %p != %p\n",
               temp_buffer,*dsbo);
379
            ref=IDirectSoundBuffer_Release(temp_buffer);
380 381
            ok(ref==1,"IDirectSoundBuffer_Release() has %d references, "
               "should have 1\n",ref);
382

383
            ref=IDirectSoundBuffer_Release(*dsbo);
384 385 386 387 388
            ok(ref==0,"IDirectSoundBuffer_Release() has %d references, "
               "should have 0\n",ref);

            rc=IDirectSound3DBuffer_QueryInterface(buffer,
                                                   &IID_IDirectSoundBuffer,
389 390
                                                   (LPVOID *)dsbo);
            ok(rc==DS_OK && *dsbo!=NULL,"IDirectSound3DBuffer_QueryInterface() "
391
               "failed: %08x\n",rc);
392 393 394

            /* DSOUND: Error: Invalid buffer */
            rc=IDirectSound3DBuffer_GetAllParameters(buffer,0);
395
            ok(rc==DSERR_INVALIDPARAM,"IDirectSound3DBuffer_GetAllParameters() "
396
               "failed: %08x\n",rc);
397 398 399 400 401

            ZeroMemory(&buffer_param, sizeof(buffer_param));

            /* DSOUND: Error: Invalid buffer */
            rc=IDirectSound3DBuffer_GetAllParameters(buffer,&buffer_param);
402
            ok(rc==DSERR_INVALIDPARAM,"IDirectSound3DBuffer_GetAllParameters() "
403
               "failed: %08x\n",rc);
404 405 406

            buffer_param.dwSize=sizeof(buffer_param);
            rc=IDirectSound3DBuffer_GetAllParameters(buffer,&buffer_param);
407
            ok(rc==DS_OK,"IDirectSound3DBuffer_GetAllParameters() failed: %08x\n", rc);
408 409 410 411
        }
        if (set_volume) {
            if (dsbcaps.dwFlags & DSBCAPS_CTRLVOLUME) {
                LONG val;
412
                rc=IDirectSoundBuffer_GetVolume(*dsbo,&val);
413
                ok(rc==DS_OK,"IDirectSoundBuffer_GetVolume() failed: %08x\n", rc);
414

415
                rc=IDirectSoundBuffer_SetVolume(*dsbo,volume);
416
                ok(rc==DS_OK,"IDirectSoundBuffer_SetVolume() failed: %08x\n", rc);
417 418
            } else {
                /* DSOUND: Error: Buffer does not have CTRLVOLUME */
419
                rc=IDirectSoundBuffer_GetVolume(*dsbo,&volume);
420
                ok(rc==DSERR_CONTROLUNAVAIL,"IDirectSoundBuffer_GetVolume() "
421
                   "should have returned DSERR_CONTROLUNAVAIL, returned: %08x\n", rc);
422 423 424 425 426 427
            }
        }

        if (set_pan) {
            if (dsbcaps.dwFlags & DSBCAPS_CTRLPAN) {
                LONG val;
428
                rc=IDirectSoundBuffer_GetPan(*dsbo,&val);
429
                ok(rc==DS_OK,"IDirectSoundBuffer_GetPan() failed: %08x\n", rc);
430

431
                rc=IDirectSoundBuffer_SetPan(*dsbo,pan);
432
                ok(rc==DS_OK,"IDirectSoundBuffer_SetPan() failed: %08x\n", rc);
433 434
            } else {
                /* DSOUND: Error: Buffer does not have CTRLPAN */
435
                rc=IDirectSoundBuffer_GetPan(*dsbo,&pan);
436
                ok(rc==DSERR_CONTROLUNAVAIL,"IDirectSoundBuffer_GetPan() "
437
                   "should have returned DSERR_CONTROLUNAVAIL, returned: %08x\n", rc);
438 439 440
            }
        }

441
        /* try an offset past the end of the buffer */
442
        rc = IDirectSoundBuffer_Lock(*dsbo, dsbcaps.dwBufferBytes, 0, &buffer1,
443 444 445
                                      &length1, NULL, NULL,
                                      DSBLOCK_ENTIREBUFFER);
        ok(rc==DSERR_INVALIDPARAM, "IDirectSoundBuffer_Lock() should have "
446
           "returned DSERR_INVALIDPARAM, returned %08x\n", rc);
447 448

        /* try a size larger than the buffer */
449
        rc = IDirectSoundBuffer_Lock(*dsbo, 0, dsbcaps.dwBufferBytes + 1,
450 451 452
                                     &buffer1, &length1, NULL, NULL,
                                     DSBLOCK_FROMWRITECURSOR);
        ok(rc==DSERR_INVALIDPARAM, "IDirectSoundBuffer_Lock() should have "
453
           "returned DSERR_INVALIDPARAM, returned %08x\n", rc);
454

455
        state.wave=wave_generate_la(&wfx,duration,&state.wave_len,ieee);
456

457
        state.dsbo=*dsbo;
458 459 460 461 462
        state.wfx=&wfx;
        state.buffer_size=dsbcaps.dwBufferBytes;
        state.played=state.written=state.offset=0;
        buffer_refill8(&state,state.buffer_size);

463
        rc=IDirectSoundBuffer_Play(*dsbo,0,0,DSBPLAY_LOOPING);
464
        ok(rc==DS_OK,"IDirectSoundBuffer_Play() failed: %08x\n", rc);
465

466
        rc=IDirectSoundBuffer_GetStatus(*dsbo,&status);
467
        ok(rc==DS_OK,"IDirectSoundBuffer_GetStatus() failed: %08x\n", rc);
468
        ok(status==(DSBSTATUS_PLAYING|DSBSTATUS_LOOPING),
469
           "GetStatus: bad status: %x\n",status);
470 471 472 473 474

        if (listener) {
            ZeroMemory(&listener_param,sizeof(listener_param));
            listener_param.dwSize=sizeof(listener_param);
            rc=IDirectSound3DListener_GetAllParameters(listener,&listener_param);
475
            ok(rc==DS_OK,"IDirectSound3dListener_GetAllParameters() "
476
               "failed: %08x\n",rc);
477
            if (move_listener) {
478 479
                listener_param.vPosition.x = -5.0f;
                listener_param.vVelocity.x = (float)(10.0/duration);
480
            }
481 482 483
            rc=IDirectSound3DListener_SetAllParameters(listener,
                                                       &listener_param,
                                                       DS3D_IMMEDIATE);
484
            ok(rc==DS_OK,"IDirectSound3dListener_SetPosition() failed: %08x\n", rc);
485 486
        }
        if (buffer3d) {
487
            if (move_sound) {
488 489
                buffer_param.vPosition.x = 100.0f;
                buffer_param.vVelocity.x = (float)(-200.0/duration);
490 491
            }
            buffer_param.flMinDistance = 10;
492 493
            rc=IDirectSound3DBuffer_SetAllParameters(buffer,&buffer_param,
                                                     DS3D_IMMEDIATE);
494
            ok(rc==DS_OK,"IDirectSound3dBuffer_SetPosition() failed: %08x\n", rc);
495 496 497 498 499 500 501
        }

        start_time=GetTickCount();
        while (buffer_service8(&state)) {
            WaitForSingleObject(GetCurrentProcess(),TIME_SLICE);
            now=GetTickCount();
            if (listener && move_listener) {
502
                listener_param.vPosition.x = (float)(-5.0+10.0*(now-start_time)/1000/duration);
503 504
                if (winetest_debug>2)
                    trace("listener position=%g\n",listener_param.vPosition.x);
505 506 507
                rc=IDirectSound3DListener_SetPosition(listener,
                    listener_param.vPosition.x,listener_param.vPosition.y,
                    listener_param.vPosition.z,DS3D_IMMEDIATE);
508
                ok(rc==DS_OK,"IDirectSound3dListener_SetPosition() failed: %08x\n",rc);
509 510
            }
            if (buffer3d && move_sound) {
511
                buffer_param.vPosition.x = (float)(100-200.0*(now-start_time)/1000/duration);
512 513
                if (winetest_debug>2)
                    trace("sound position=%g\n",buffer_param.vPosition.x);
514 515 516
                rc=IDirectSound3DBuffer_SetPosition(buffer,
                    buffer_param.vPosition.x,buffer_param.vPosition.y,
                    buffer_param.vPosition.z,DS3D_IMMEDIATE);
517
                ok(rc==DS_OK,"IDirectSound3dBuffer_SetPosition() failed: %08x\n", rc);
518 519 520 521
            }
        }
        /* Check the sound duration was within 10% of the expected value */
        now=GetTickCount();
522
        ok(fabs(1000*duration-now+start_time)<=100*duration,
523
           "The sound played for %d ms instead of %g ms\n",
524
           now-start_time,1000*duration);
525

526
        HeapFree(GetProcessHeap(), 0, state.wave);
527 528 529 530
        if (is_primary) {
            /* Set the CooperativeLevel back to normal */
            /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
            rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
531
            ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_NORMAL) "
532
               "failed: %08x\n",rc);
533 534 535
        }
        if (buffer3d) {
            ref=IDirectSound3DBuffer_Release(buffer);
536 537
            ok(ref==0,"IDirectSound3DBuffer_Release() has %d references, "
               "should have 0\n",ref);
538 539 540 541 542
        }
    }
}

static HRESULT test_secondary8(LPGUID lpGuid, int play,
543 544 545
                               int has_3d, int has_3dbuffer,
                               int has_listener, int has_duplicate,
                               int move_listener, int move_sound)
546 547 548 549 550 551
{
    HRESULT rc;
    LPDIRECTSOUND8 dso=NULL;
    LPDIRECTSOUNDBUFFER primary=NULL,secondary=NULL;
    LPDIRECTSOUND3DLISTENER listener=NULL;
    DSBUFFERDESC bufdesc;
552
    WAVEFORMATEX wfx, wfx1;
553 554 555
    int ref;

    /* Create the DirectSound object */
556
    rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
557
    ok(rc==DS_OK||rc==DSERR_NODRIVER,"DirectSoundCreate8() failed: %08x\n", rc);
558 559 560 561 562 563
    if (rc!=DS_OK)
        return rc;

    /* We must call SetCooperativeLevel before creating primary buffer */
    /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
    rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
564
    ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_PRIORITY) failed: %08x\n",rc);
565 566 567 568 569 570 571 572 573 574 575
    if (rc!=DS_OK)
        goto EXIT;

    ZeroMemory(&bufdesc, sizeof(bufdesc));
    bufdesc.dwSize=sizeof(bufdesc);
    bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
    if (has_3d)
        bufdesc.dwFlags|=DSBCAPS_CTRL3D;
    else
        bufdesc.dwFlags|=(DSBCAPS_CTRLVOLUME|DSBCAPS_CTRLPAN);
    rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
576
    ok((rc==DS_OK && primary!=NULL) || (rc == DSERR_CONTROLUNAVAIL),
577
       "IDirectSound8_CreateSoundBuffer() failed to create a %sprimary buffer: %08x\n",has_3d?"3D ":"", rc);
578 579 580
    if (rc == DSERR_CONTROLUNAVAIL)
        trace("  No Primary\n");
    else if (rc==DS_OK && primary!=NULL) {
581
        rc=IDirectSoundBuffer_GetFormat(primary,&wfx1,sizeof(wfx1),NULL);
582
        ok(rc==DS_OK,"IDirectSoundBuffer8_Getformat() failed: %08x\n", rc);
583 584 585
        if (rc!=DS_OK)
            goto EXIT1;

586
        if (has_listener) {
587 588 589 590 591
            rc=IDirectSoundBuffer_QueryInterface(primary,
                                                 &IID_IDirectSound3DListener,
                                                 (void **)&listener);
            ok(rc==DS_OK && listener!=NULL,
               "IDirectSoundBuffer_QueryInterface() failed to get a 3D "
592
               "listener %08x\n",rc);
593
            ref=IDirectSoundBuffer_Release(primary);
594 595
            ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
               "should have 0\n",ref);
596 597 598 599 600
            if (rc==DS_OK && listener!=NULL) {
                DS3DLISTENER listener_param;
                ZeroMemory(&listener_param,sizeof(listener_param));
                /* DSOUND: Error: Invalid buffer */
                rc=IDirectSound3DListener_GetAllParameters(listener,0);
601
                ok(rc==DSERR_INVALIDPARAM,
602
                   "IDirectSound3dListener_GetAllParameters() should have "
603
                   "returned DSERR_INVALIDPARAM, returned: %08x\n", rc);
604 605

                /* DSOUND: Error: Invalid buffer */
606 607 608
                rc=IDirectSound3DListener_GetAllParameters(listener,
                                                           &listener_param);
                ok(rc==DSERR_INVALIDPARAM,
609
                   "IDirectSound3dListener_GetAllParameters() should have "
610
                   "returned DSERR_INVALIDPARAM, returned: %08x\n", rc);
611 612

                listener_param.dwSize=sizeof(listener_param);
613 614 615
                rc=IDirectSound3DListener_GetAllParameters(listener,
                                                           &listener_param);
                ok(rc==DS_OK,"IDirectSound3dListener_GetAllParameters() "
616
                   "failed: %08x\n",rc);
617 618 619 620 621 622 623 624 625 626 627
            } else {
                ok(listener==NULL, "IDirectSoundBuffer_QueryInterface() "
                   "failed but returned a listener anyway\n");
                ok(rc!=DS_OK, "IDirectSoundBuffer_QueryInterface() succeeded "
                   "but returned a NULL listener\n");
                if (listener) {
                    ref=IDirectSound3DListener_Release(listener);
                    ok(ref==0,"IDirectSound3dListener_Release() listener has "
                       "%d references, should have 0\n",ref);
                }
                goto EXIT2;
628 629 630 631 632 633 634 635 636 637 638
            }
        }

        init_format(&wfx,WAVE_FORMAT_PCM,22050,16,2);
        secondary=NULL;
        ZeroMemory(&bufdesc, sizeof(bufdesc));
        bufdesc.dwSize=sizeof(bufdesc);
        bufdesc.dwFlags=DSBCAPS_GETCURRENTPOSITION2;
        if (has_3d)
            bufdesc.dwFlags|=DSBCAPS_CTRL3D;
        else
639 640
            bufdesc.dwFlags|=
                (DSBCAPS_CTRLFREQUENCY|DSBCAPS_CTRLVOLUME|DSBCAPS_CTRLPAN);
641 642
        bufdesc.dwBufferBytes=align(wfx.nAvgBytesPerSec*BUFFER_LEN/1000,
                                    wfx.nBlockAlign);
643
        bufdesc.lpwfxFormat=&wfx;
644 645 646 647 648
        if (has_3d) {
            /* a stereo 3D buffer should fail */
            rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
            ok(rc==DSERR_INVALIDPARAM,
               "IDirectSound8_CreateSoundBuffer(secondary) should have "
649
               "returned DSERR_INVALIDPARAM, returned %08x\n", rc);
650
            if (secondary)
651
            {
652
                ref=IDirectSoundBuffer_Release(secondary);
653 654
                ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, should have 0\n",ref);
            }
655 656 657
            init_format(&wfx,WAVE_FORMAT_PCM,22050,16,1);
        }

658
        if (winetest_interactive) {
659 660
            trace("  Testing a %s%ssecondary buffer %s%s%s%sat %dx%dx%d "
                  "with a primary buffer at %dx%dx%d\n",
661 662 663 664 665
                  has_3dbuffer?"3D ":"",
                  has_duplicate?"duplicated ":"",
                  listener!=NULL||move_sound?"with ":"",
                  move_listener?"moving ":"",
                  listener!=NULL?"listener ":"",
666 667
                  listener&&move_sound?"and moving sound ":move_sound?
                  "moving sound ":"",
668 669
                  wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,
                  wfx1.nSamplesPerSec,wfx1.wBitsPerSample,wfx1.nChannels);
670
        }
671
        rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&secondary,NULL);
672
        ok(rc==DS_OK && secondary!=NULL,"IDirectSound8_CreateSoundBuffer() "
673
           "failed to create a %s%ssecondary buffer %s%s%s%sat %dx%dx%d (%s): %08x\n",
674 675 676 677 678 679
           has_3dbuffer?"3D ":"", has_duplicate?"duplicated ":"",
           listener!=NULL||move_sound?"with ":"", move_listener?"moving ":"",
           listener!=NULL?"listener ":"",
           listener&&move_sound?"and moving sound ":move_sound?
           "moving sound ":"",
           wfx.nSamplesPerSec,wfx.wBitsPerSample,wfx.nChannels,
680
           getDSBCAPS(bufdesc.dwFlags),rc);
681
        if (rc==DS_OK && secondary!=NULL) {
682
            if (!has_3d) {
Mike McCormack's avatar
Mike McCormack committed
683
                LONG refvol,vol,refpan,pan;
684 685 686

                /* Check the initial secondary buffer's volume and pan */
                rc=IDirectSoundBuffer_GetVolume(secondary,&vol);
687
                ok(rc==DS_OK,"IDirectSoundBuffer_GetVolume(secondary) failed: %08x\n",rc);
688
                ok(vol==0,"wrong volume for a new secondary buffer: %d\n",vol);
689
                rc=IDirectSoundBuffer_GetPan(secondary,&pan);
690
                ok(rc==DS_OK,"IDirectSoundBuffer_GetPan(secondary) failed: %08x\n",rc);
691
                ok(pan==0,"wrong pan for a new secondary buffer: %d\n",pan);
692 693 694 695 696

                /* Check that changing the secondary buffer's volume and pan
                 * does not impact the primary buffer's volume and pan
                 */
                rc=IDirectSoundBuffer_GetVolume(primary,&refvol);
697
                ok(rc==DS_OK,"IDirectSoundBuffer_GetVolume(primary) failed: %08x\n",rc);
698
                rc=IDirectSoundBuffer_GetPan(primary,&refpan);
699
                ok(rc==DS_OK,"IDirectSoundBuffer_GetPan(primary) failed: %08x\n",rc);
700 701

                rc=IDirectSoundBuffer_SetVolume(secondary,-1000);
702
                ok(rc==DS_OK,"IDirectSoundBuffer_SetVolume(secondary) failed: %08x\n",rc);
703
                rc=IDirectSoundBuffer_GetVolume(secondary,&vol);
704
                ok(rc==DS_OK,"IDirectSoundBuffer_SetVolume(secondary) failed: %08x\n",rc);
705
                ok(vol==-1000,"secondary: wrong volume %d instead of -1000\n",
706
                   vol);
707
                rc=IDirectSoundBuffer_SetPan(secondary,-1000);
708
                ok(rc==DS_OK,"IDirectSoundBuffer_SetPan(secondary) failed: %08x\n",rc);
709
                rc=IDirectSoundBuffer_GetPan(secondary,&pan);
710
                ok(rc==DS_OK,"IDirectSoundBuffer_SetPan(secondary) failed: %08x\n",rc);
711
                ok(pan==-1000,"secondary: wrong pan %d instead of -1000\n",
712
                   pan);
713 714

                rc=IDirectSoundBuffer_GetVolume(primary,&vol);
715
                ok(rc==DS_OK,"IDirectSoundBuffer_`GetVolume(primary) failed: i%08x\n",rc);
716
                ok(vol==refvol,"The primary volume changed from %d to %d\n",
717
                   refvol,vol);
718
                rc=IDirectSoundBuffer_GetPan(primary,&pan);
719
                ok(rc==DS_OK,"IDirectSoundBuffer_GetPan(primary) failed: %08x\n",rc);
720
                ok(pan==refpan,"The primary pan changed from %d to %d\n",
721
                   refpan,pan);
722 723

                rc=IDirectSoundBuffer_SetVolume(secondary,0);
724
                ok(rc==DS_OK,"IDirectSoundBuffer_SetVolume(secondary) failed: %08x\n",rc);
725
                rc=IDirectSoundBuffer_SetPan(secondary,0);
726
                ok(rc==DS_OK,"IDirectSoundBuffer_SetPan(secondary) failed: %08x\n",rc);
727 728 729 730 731 732
            }
            if (has_duplicate) {
                LPDIRECTSOUNDBUFFER duplicated=NULL;

                /* DSOUND: Error: Invalid source buffer */
                rc=IDirectSound8_DuplicateSoundBuffer(dso,0,0);
733 734
                ok(rc==DSERR_INVALIDPARAM,
                   "IDirectSound8_DuplicateSoundBuffer() should have returned "
735
                   "DSERR_INVALIDPARAM, returned: %08x\n",rc);
736 737 738

                /* DSOUND: Error: Invalid dest buffer */
                rc=IDirectSound8_DuplicateSoundBuffer(dso,secondary,0);
739 740
                ok(rc==DSERR_INVALIDPARAM,
                   "IDirectSound8_DuplicateSoundBuffer() should have returned "
741
                   "DSERR_INVALIDPARAM, returned: %08x\n",rc);
742 743 744

                /* DSOUND: Error: Invalid source buffer */
                rc=IDirectSound8_DuplicateSoundBuffer(dso,0,&duplicated);
745 746
                ok(rc==DSERR_INVALIDPARAM,
                   "IDirectSound8_DuplicateSoundBuffer() should have returned "
747
                   "DSERR_INVALIDPARAM, returned: %08x\n",rc);
748 749

                duplicated=NULL;
750 751 752 753
                rc=IDirectSound8_DuplicateSoundBuffer(dso,secondary,
                                                      &duplicated);
                ok(rc==DS_OK && duplicated!=NULL,
                   "IDirectSound8_DuplicateSoundBuffer() failed to duplicate "
754
                   "a secondary buffer: %08x\n",rc);
755 756 757

                if (rc==DS_OK && duplicated!=NULL) {
                    ref=IDirectSoundBuffer_Release(secondary);
758 759
                    ok(ref==0,"IDirectSoundBuffer_Release() secondary has %d "
                       "references, should have 0\n",ref);
760 761 762 763 764 765 766
                    secondary=duplicated;
                }
            }

            if (rc==DS_OK && secondary!=NULL) {
                double duration;
                duration=(move_listener || move_sound?4.0:1.0);
767
                test_buffer8(dso,&secondary,0,FALSE,0,FALSE,0,
768 769
                             winetest_interactive,duration,has_3dbuffer,
                             listener,move_listener,move_sound);
770
                ref=IDirectSoundBuffer_Release(secondary);
771 772 773
                ok(ref==0,"IDirectSoundBuffer_Release() %s has %d references, "
                   "should have 0\n",has_duplicate?"duplicated":"secondary",
                   ref);
774 775
            }
        }
776
EXIT1:
777
        if (has_listener) {
778 779 780 781 782
            if (listener) {
                ref=IDirectSound3DListener_Release(listener);
                ok(ref==0,"IDirectSound3dListener_Release() listener has %d "
                   "references, should have 0\n",ref);
            }
783 784 785 786 787
        } else {
            ref=IDirectSoundBuffer_Release(primary);
            ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
               "should have 0\n",ref);
        }
788
    } else {
789 790 791 792 793 794 795 796 797
        ok(primary==NULL,"IDirectSound8_CreateSoundBuffer(primary) failed "
           "but primary created anyway\n");
        ok(rc!=DS_OK,"IDirectSound8_CreateSoundBuffer(primary) succeeded "
           "but primary not created\n");
        if (primary) {
            ref=IDirectSoundBuffer_Release(primary);
            ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
               "should have 0\n",ref);
        }
798
    }
799
EXIT2:
800 801 802
    /* Set the CooperativeLevel back to normal */
    /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
    rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
803
    ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_NORMAL) failed: %08x\n",rc);
804 805 806

EXIT:
    ref=IDirectSound8_Release(dso);
807
    ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",ref);
808 809 810 811 812 813
    if (ref!=0)
        return DSERR_GENERIC;

    return rc;
}

814 815 816 817 818 819 820
static HRESULT test_for_driver8(LPGUID lpGuid)
{
    HRESULT rc;
    LPDIRECTSOUND8 dso=NULL;
    int ref;

    /* Create the DirectSound object */
821
    rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
822
    ok(rc==DS_OK||rc==DSERR_NODRIVER||rc==DSERR_ALLOCATED||rc==E_FAIL,
823
       "DirectSoundCreate8() failed: %08x\n",rc);
824 825 826 827 828 829 830 831 832 833 834
    if (rc!=DS_OK)
        return rc;

    ref=IDirectSound8_Release(dso);
    ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",ref);
    if (ref!=0)
        return DSERR_GENERIC;

    return rc;
}

835 836 837 838 839 840 841 842 843 844
static HRESULT test_primary8(LPGUID lpGuid)
{
    HRESULT rc;
    LPDIRECTSOUND8 dso=NULL;
    LPDIRECTSOUNDBUFFER primary=NULL;
    DSBUFFERDESC bufdesc;
    DSCAPS dscaps;
    int ref, i;

    /* Create the DirectSound object */
845
    rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
846
    ok(rc==DS_OK||rc==DSERR_NODRIVER,"DirectSoundCreate8() failed: %08x\n", rc);
847 848 849 850 851 852 853
    if (rc!=DS_OK)
        return rc;

    /* Get the device capabilities */
    ZeroMemory(&dscaps, sizeof(dscaps));
    dscaps.dwSize=sizeof(dscaps);
    rc=IDirectSound8_GetCaps(dso,&dscaps);
854
    ok(rc==DS_OK,"IDirectSound8_GetCaps() failed: %08x\n",rc);
855 856 857 858 859 860
    if (rc!=DS_OK)
        goto EXIT;

    /* We must call SetCooperativeLevel before calling CreateSoundBuffer */
    /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
    rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
861
    ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_PRIORITY) failed: %08x\n",rc);
862 863 864 865 866 867 868 869 870
    if (rc!=DS_OK)
        goto EXIT;

    /* Testing the primary buffer */
    primary=NULL;
    ZeroMemory(&bufdesc, sizeof(bufdesc));
    bufdesc.dwSize=sizeof(bufdesc);
    bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRLVOLUME|DSBCAPS_CTRLPAN;
    rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
871
    ok((rc==DS_OK && primary!=NULL) || (rc == DSERR_CONTROLUNAVAIL),
872
       "IDirectSound8_CreateSoundBuffer() failed to create a primary buffer: %08x\n",rc);
873 874 875
    if (rc == DSERR_CONTROLUNAVAIL)
        trace("  No Primary\n");
    else if (rc==DS_OK && primary!=NULL) {
876
        test_buffer8(dso,&primary,1,TRUE,0,TRUE,0,winetest_interactive &&
877
                     !(dscaps.dwFlags & DSCAPS_EMULDRIVER),1.0,0,NULL,0,0);
878 879 880 881 882
        if (winetest_interactive) {
            LONG volume,pan;

            volume = DSBVOLUME_MAX;
            for (i = 0; i < 6; i++) {
883
                test_buffer8(dso,&primary,1,TRUE,volume,TRUE,0,
884 885 886
                             winetest_interactive &&
                             !(dscaps.dwFlags & DSCAPS_EMULDRIVER),
                             1.0,0,NULL,0,0);
887 888 889 890 891
                volume -= ((DSBVOLUME_MAX-DSBVOLUME_MIN) / 40);
            }

            pan = DSBPAN_LEFT;
            for (i = 0; i < 7; i++) {
892
                test_buffer8(dso,&primary,1,TRUE,0,TRUE,pan,
893 894
                             winetest_interactive &&
                             !(dscaps.dwFlags & DSCAPS_EMULDRIVER),1.0,0,0,0,0);
895 896 897 898
                pan += ((DSBPAN_RIGHT-DSBPAN_LEFT) / 6);
            }
        }
        ref=IDirectSoundBuffer_Release(primary);
899 900
        ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
           "should have 0\n",ref);
901 902 903 904 905
    }

    /* Set the CooperativeLevel back to normal */
    /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
    rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
906
    ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_NORMAL) failed: %08x\n",rc);
907 908 909

EXIT:
    ref=IDirectSound8_Release(dso);
910
    ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",ref);
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
    if (ref!=0)
        return DSERR_GENERIC;

    return rc;
}

static HRESULT test_primary_3d8(LPGUID lpGuid)
{
    HRESULT rc;
    LPDIRECTSOUND8 dso=NULL;
    LPDIRECTSOUNDBUFFER primary=NULL;
    DSBUFFERDESC bufdesc;
    DSCAPS dscaps;
    int ref;

    /* Create the DirectSound object */
927
    rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
928
    ok(rc==DS_OK||rc==DSERR_NODRIVER,"DirectSoundCreate8() failed: %08x\n", rc);
929 930 931 932 933 934 935
    if (rc!=DS_OK)
        return rc;

    /* Get the device capabilities */
    ZeroMemory(&dscaps, sizeof(dscaps));
    dscaps.dwSize=sizeof(dscaps);
    rc=IDirectSound8_GetCaps(dso,&dscaps);
936
    ok(rc==DS_OK,"IDirectSound8_GetCaps failed: %08x\n",rc);
937 938 939 940 941 942
    if (rc!=DS_OK)
        goto EXIT;

    /* We must call SetCooperativeLevel before calling CreateSoundBuffer */
    /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
    rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
943
    ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_PRIORITY) failed: %08x\n",rc);
944 945 946 947 948 949 950 951
    if (rc!=DS_OK)
        goto EXIT;

    primary=NULL;
    ZeroMemory(&bufdesc, sizeof(bufdesc));
    bufdesc.dwSize=sizeof(bufdesc);
    bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
    rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
952
    ok(rc==DS_OK && primary!=NULL,"IDirectSound8_CreateSoundBuffer() failed "
953
       "to create a primary buffer: %08x\n",rc);
954 955
    if (rc==DS_OK && primary!=NULL) {
        ref=IDirectSoundBuffer_Release(primary);
956 957
        ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
           "should have 0\n",ref);
958 959 960 961 962
        primary=NULL;
        ZeroMemory(&bufdesc, sizeof(bufdesc));
        bufdesc.dwSize=sizeof(bufdesc);
        bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRL3D;
        rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
963
        ok(rc==DS_OK && primary!=NULL,"IDirectSound8_CreateSoundBuffer() "
964
           "failed to create a 3D primary buffer: %08x\n",rc);
965
        if (rc==DS_OK && primary!=NULL) {
966
            test_buffer8(dso,&primary,1,FALSE,0,FALSE,0,
967 968
                         winetest_interactive &&
                         !(dscaps.dwFlags & DSCAPS_EMULDRIVER),1.0,0,0,0,0);
969
            ref=IDirectSoundBuffer_Release(primary);
970 971
            ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
               "should have 0\n",ref);
972 973 974 975 976
        }
    }
    /* Set the CooperativeLevel back to normal */
    /* DSOUND: Setting DirectSound cooperative level to DSSCL_NORMAL */
    rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_NORMAL);
977
    ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_NORMAL) failed: %08x\n",rc);
978 979 980

EXIT:
    ref=IDirectSound8_Release(dso);
981
    ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",ref);
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
    if (ref!=0)
        return DSERR_GENERIC;

    return rc;
}

static HRESULT test_primary_3d_with_listener8(LPGUID lpGuid)
{
    HRESULT rc;
    LPDIRECTSOUND8 dso=NULL;
    LPDIRECTSOUNDBUFFER primary=NULL;
    DSBUFFERDESC bufdesc;
    DSCAPS dscaps;
    int ref;

    /* Create the DirectSound object */
998
    rc=pDirectSoundCreate8(lpGuid,&dso,NULL);
999
    ok(rc==DS_OK||rc==DSERR_NODRIVER,"DirectSoundCreate8() failed: %08x\n", rc);
1000 1001 1002 1003 1004 1005 1006
    if (rc!=DS_OK)
        return rc;

    /* Get the device capabilities */
    ZeroMemory(&dscaps, sizeof(dscaps));
    dscaps.dwSize=sizeof(dscaps);
    rc=IDirectSound8_GetCaps(dso,&dscaps);
1007
    ok(rc==DS_OK,"IDirectSound8_GetCaps() failed: %08x\n",rc);
1008 1009 1010 1011 1012 1013
    if (rc!=DS_OK)
        goto EXIT;

    /* We must call SetCooperativeLevel before calling CreateSoundBuffer */
    /* DSOUND: Setting DirectSound cooperative level to DSSCL_PRIORITY */
    rc=IDirectSound8_SetCooperativeLevel(dso,get_hwnd(),DSSCL_PRIORITY);
1014
    ok(rc==DS_OK,"IDirectSound8_SetCooperativeLevel(DSSCL_PRIORITY) failed: %08x\n",rc);
1015 1016 1017 1018 1019 1020 1021
    if (rc!=DS_OK)
        goto EXIT;
    primary=NULL;
    ZeroMemory(&bufdesc, sizeof(bufdesc));
    bufdesc.dwSize=sizeof(bufdesc);
    bufdesc.dwFlags=DSBCAPS_PRIMARYBUFFER|DSBCAPS_CTRL3D;
    rc=IDirectSound8_CreateSoundBuffer(dso,&bufdesc,&primary,NULL);
1022
    ok(rc==DS_OK && primary!=NULL,"IDirectSound8_CreateSoundBuffer() failed "
1023
       "to create a 3D primary buffer %08x\n",rc);
1024 1025
    if (rc==DS_OK && primary!=NULL) {
        LPDIRECTSOUND3DLISTENER listener=NULL;
1026 1027 1028 1029
        rc=IDirectSoundBuffer_QueryInterface(primary,
                                             &IID_IDirectSound3DListener,
                                             (void **)&listener);
        ok(rc==DS_OK && listener!=NULL,"IDirectSoundBuffer_QueryInterface() "
1030
           "failed to get a 3D listener: %08x\n",rc);
1031 1032 1033 1034
        if (rc==DS_OK && listener!=NULL) {
            LPDIRECTSOUNDBUFFER temp_buffer=NULL;

            /* Checking the COM interface */
1035 1036 1037 1038
            rc=IDirectSoundBuffer_QueryInterface(primary,
                                                 &IID_IDirectSoundBuffer,
                                                 (LPVOID *)&temp_buffer);
            ok(rc==DS_OK && temp_buffer!=NULL,
1039
               "IDirectSoundBuffer_QueryInterface() failed: %08x\n", rc);
1040
            ok(temp_buffer==primary,"COM interface broken: %p != %p\n",temp_buffer,primary);
1041 1042
            if (rc==DS_OK && temp_buffer!=NULL) {
                ref=IDirectSoundBuffer_Release(temp_buffer);
1043 1044
                ok(ref==1,"IDirectSoundBuffer_Release() has %d references, "
                   "should have 1\n",ref);
1045 1046

                temp_buffer=NULL;
1047 1048 1049
                rc=IDirectSound3DListener_QueryInterface(listener,
                    &IID_IDirectSoundBuffer,(LPVOID *)&temp_buffer);
                ok(rc==DS_OK && temp_buffer!=NULL,
1050
                   "IDirectSoundBuffer_QueryInterface() failed: %08x\n", rc);
1051
                ok(temp_buffer==primary,"COM interface broken: %p != %p\n",temp_buffer,primary);
1052
                ref=IDirectSoundBuffer_Release(temp_buffer);
1053 1054
                ok(ref==1,"IDirectSoundBuffer_Release() has %d references, "
                   "should have 1\n",ref);
1055 1056

                /* Testing the buffer */
1057
                test_buffer8(dso,&primary,1,FALSE,0,FALSE,0,
1058 1059 1060
                             winetest_interactive &&
                             !(dscaps.dwFlags & DSCAPS_EMULDRIVER),
                             1.0,0,listener,0,0);
1061 1062 1063 1064
            }

            /* Testing the reference counting */
            ref=IDirectSound3DListener_Release(listener);
1065 1066
            ok(ref==0,"IDirectSound3DListener_Release() listener has %d "
               "references, should have 0\n",ref);
1067 1068 1069 1070
        }

        /* Testing the reference counting */
        ref=IDirectSoundBuffer_Release(primary);
1071 1072
        ok(ref==0,"IDirectSoundBuffer_Release() primary has %d references, "
           "should have 0\n",ref);
1073 1074 1075 1076
    }

EXIT:
    ref=IDirectSound8_Release(dso);
1077
    ok(ref==0,"IDirectSound8_Release() has %d references, should have 0\n",ref);
1078 1079 1080 1081 1082 1083 1084 1085 1086
    if (ref!=0)
return DSERR_GENERIC;

    return rc;
}

static BOOL WINAPI dsenum_callback(LPGUID lpGuid, LPCSTR lpcstrDescription,
                                   LPCSTR lpcstrModule, LPVOID lpContext)
{
1087
    HRESULT rc;
1088
    trace("*** Testing %s - %s ***\n",lpcstrDescription,lpcstrModule);
1089

1090 1091 1092 1093
    rc = test_for_driver8(lpGuid);
    if (rc == DSERR_NODRIVER) {
        trace("  No Driver\n");
        return 1;
1094 1095 1096
    } else if (rc == DSERR_ALLOCATED) {
        trace("  Already In Use\n");
        return 1;
1097 1098 1099
    } else if (rc == E_FAIL) {
        trace("  No Device\n");
        return 1;
1100 1101
    }

1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
    trace("  Testing the primary buffer\n");
    test_primary8(lpGuid);

    trace("  Testing 3D primary buffer\n");
    test_primary_3d8(lpGuid);

    trace("  Testing 3D primary buffer with listener\n");
    test_primary_3d_with_listener8(lpGuid);

    /* Testing secondary buffers */
    test_secondary8(lpGuid,winetest_interactive,0,0,0,0,0,0);
    test_secondary8(lpGuid,winetest_interactive,0,0,0,1,0,0);

    /* Testing 3D secondary buffers */
    test_secondary8(lpGuid,winetest_interactive,1,0,0,0,0,0);
    test_secondary8(lpGuid,winetest_interactive,1,1,0,0,0,0);
    test_secondary8(lpGuid,winetest_interactive,1,1,0,1,0,0);
    test_secondary8(lpGuid,winetest_interactive,1,0,1,0,0,0);
    test_secondary8(lpGuid,winetest_interactive,1,0,1,1,0,0);
    test_secondary8(lpGuid,winetest_interactive,1,1,1,0,0,0);
    test_secondary8(lpGuid,winetest_interactive,1,1,1,1,0,0);
    test_secondary8(lpGuid,winetest_interactive,1,1,1,0,1,0);
    test_secondary8(lpGuid,winetest_interactive,1,1,1,0,0,1);
    test_secondary8(lpGuid,winetest_interactive,1,1,1,0,1,1);

    return 1;
}

1130
static void ds3d8_tests(void)
1131 1132
{
    HRESULT rc;
1133
    rc=pDirectSoundEnumerateA(&dsenum_callback,NULL);
1134
    ok(rc==DS_OK,"DirectSoundEnumerateA() failed: %08x\n",rc);
1135 1136 1137 1138 1139 1140 1141 1142
}

START_TEST(ds3d8)
{
    HMODULE hDsound;

    CoInitialize(NULL);

1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
    hDsound = LoadLibrary("dsound.dll");
    if (hDsound)
    {

        pDirectSoundEnumerateA = (void*)GetProcAddress(hDsound,
            "DirectSoundEnumerateA");
        pDirectSoundCreate8 = (void*)GetProcAddress(hDsound,
            "DirectSoundCreate8");
        if (pDirectSoundCreate8)
            ds3d8_tests();
        else
            skip("ds3d8 test skipped\n");
1155

1156
        FreeLibrary(hDsound);
1157
    }
1158 1159
    else
        skip("dsound.dll not found!\n");
1160 1161

    CoUninitialize();
1162
}