wined3d: Set the max mipmap level in the pbo test.
[wine] / dlls / dsound / primary.c
1 /*                      DirectSound
2  *
3  * Copyright 1998 Marcus Meissner
4  * Copyright 1998 Rob Riggs
5  * Copyright 2000-2002 TransGaming Technologies, Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include <stdarg.h>
23
24 #define NONAMELESSSTRUCT
25 #define NONAMELESSUNION
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "mmsystem.h"
30 #include "winternl.h"
31 #include "mmddk.h"
32 #include "wine/debug.h"
33 #include "dsound.h"
34 #include "dsdriver.h"
35 #include "dsound_private.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
38
39 /** Calculate how long a fragment length of about 10 ms should be in frames
40  *
41  * nSamplesPerSec: Frequency rate in samples per second
42  * nBlockAlign: Size of a single blockalign
43  *
44  * Returns:
45  * Size in bytes of a single fragment
46  */
47 DWORD DSOUND_fraglen(DWORD nSamplesPerSec, DWORD nBlockAlign)
48 {
49     /* Given a timer delay of 10ms, the fragment size is approximately:
50      *     fraglen = (nSamplesPerSec * 10 / 1000) * nBlockAlign
51      * ==> fraglen = (nSamplesPerSec / 100) * nBlockSize
52      *
53      * ALSA uses buffers that are powers of 2. Because of this, fraglen
54      * is rounded up to the nearest power of 2:
55      */
56
57     if (nSamplesPerSec <= 12800)
58         return 128 * nBlockAlign;
59
60     if (nSamplesPerSec <= 25600)
61         return 256 * nBlockAlign;
62
63     if (nSamplesPerSec <= 51200)
64         return 512 * nBlockAlign;
65
66     return 1024 * nBlockAlign;
67 }
68
69 static void DSOUND_RecalcPrimary(DirectSoundDevice *device)
70 {
71     TRACE("(%p)\n", device);
72
73     device->fraglen = DSOUND_fraglen(device->pwfx->nSamplesPerSec, device->pwfx->nBlockAlign);
74     device->helfrags = device->buflen / device->fraglen;
75     TRACE("fraglen=%d helfrags=%d\n", device->fraglen, device->helfrags);
76
77     if (device->hwbuf && device->drvdesc.dwFlags & DSDDESC_DONTNEEDWRITELEAD)
78         device->writelead = 0;
79     else
80         /* calculate the 10ms write lead */
81         device->writelead = (device->pwfx->nSamplesPerSec / 100) * device->pwfx->nBlockAlign;
82 }
83
84 HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave)
85 {
86         HRESULT hres = DS_OK;
87         TRACE("(%p, %d)\n", device, forcewave);
88
89         if (device->driver)
90         {
91                 IDsDriver_Close(device->driver);
92                 if (device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN)
93                         waveOutClose(device->hwo);
94                 IDsDriver_Release(device->driver);
95                 device->driver = NULL;
96                 device->buffer = NULL;
97                 device->hwo = 0;
98         }
99         else if (device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN)
100                 waveOutClose(device->hwo);
101
102         /* DRV_QUERYDSOUNDIFACE is a "Wine extension" to get the DSound interface */
103         if (ds_hw_accel != DS_HW_ACCEL_EMULATION && !forcewave)
104                 waveOutMessage((HWAVEOUT)device->drvdesc.dnDevNode, DRV_QUERYDSOUNDIFACE, (DWORD_PTR)&device->driver, 0);
105
106         /* Get driver description */
107         if (device->driver) {
108                 DWORD wod = device->drvdesc.dnDevNode;
109                 hres = IDsDriver_GetDriverDesc(device->driver,&(device->drvdesc));
110                 device->drvdesc.dnDevNode = wod;
111                 if (FAILED(hres)) {
112                         WARN("IDsDriver_GetDriverDesc failed: %08x\n", hres);
113                         IDsDriver_Release(device->driver);
114                         device->driver = NULL;
115                 }
116         }
117
118         /* if no DirectSound interface available, use WINMM API instead */
119         if (!device->driver)
120                 device->drvdesc.dwFlags = DSDDESC_DOMMSYSTEMOPEN | DSDDESC_DOMMSYSTEMSETFORMAT;
121
122         if (device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN)
123         {
124                 DWORD flags = CALLBACK_FUNCTION;
125
126                 if (device->driver)
127                         flags |= WAVE_DIRECTSOUND;
128
129                 hres = mmErr(waveOutOpen(&(device->hwo), device->drvdesc.dnDevNode, device->pwfx, (DWORD_PTR)DSOUND_callback, (DWORD)device, flags));
130                 if (FAILED(hres)) {
131                         WARN("waveOutOpen failed\n");
132                         if (device->driver)
133                         {
134                                 IDsDriver_Release(device->driver);
135                                 device->driver = NULL;
136                         }
137                         return hres;
138                 }
139         }
140
141         if (device->driver)
142                 hres = IDsDriver_Open(device->driver);
143
144         return hres;
145 }
146
147 static HRESULT DSOUND_PrimaryOpen(DirectSoundDevice *device)
148 {
149         DWORD buflen;
150         HRESULT err = DS_OK;
151         TRACE("(%p)\n", device);
152
153         /* on original windows, the buffer it set to a fixed size, no matter what the settings are.
154            on windows this size is always fixed (tested on win-xp) */
155         if (!device->buflen)
156                 device->buflen = ds_hel_buflen;
157         buflen = device->buflen;
158         buflen -= buflen % device->pwfx->nBlockAlign;
159         device->buflen = buflen;
160
161         if (device->driver)
162         {
163                 err = IDsDriver_CreateSoundBuffer(device->driver,device->pwfx,
164                                                   DSBCAPS_PRIMARYBUFFER,0,
165                                                   &(device->buflen),&(device->buffer),
166                                                   (LPVOID*)&(device->hwbuf));
167
168                 if (err != DS_OK) {
169                         WARN("IDsDriver_CreateSoundBuffer failed (%08x), falling back to waveout\n", err);
170                         err = DSOUND_ReopenDevice(device, TRUE);
171                         if (FAILED(err))
172                         {
173                                 WARN("Falling back to waveout failed too! Giving up\n");
174                                 return err;
175                         }
176                 }
177                 if (device->hwbuf)
178                     IDsDriverBuffer_SetVolumePan(device->hwbuf, &device->volpan);
179
180                 DSOUND_RecalcPrimary(device);
181                 device->prebuf = ds_snd_queue_max;
182                 if (device->helfrags < ds_snd_queue_min)
183                 {
184                         WARN("Too little sound buffer to be effective (%d/%d) falling back to waveout\n", device->buflen, ds_snd_queue_min * device->fraglen);
185                         device->buflen = buflen;
186                         IDsDriverBuffer_Release(device->hwbuf);
187                         device->hwbuf = NULL;
188                         err = DSOUND_ReopenDevice(device, TRUE);
189                         if (FAILED(err))
190                         {
191                                 WARN("Falling back to waveout failed too! Giving up\n");
192                                 return err;
193                         }
194                 }
195                 else if (device->helfrags < ds_snd_queue_max)
196                         device->prebuf = device->helfrags;
197         }
198
199         device->mix_buffer_len = DSOUND_bufpos_to_mixpos(device, device->buflen);
200         device->mix_buffer = HeapAlloc(GetProcessHeap(), 0, device->mix_buffer_len);
201         if (!device->mix_buffer)
202         {
203                 if (device->hwbuf)
204                         IDsDriverBuffer_Release(device->hwbuf);
205                 device->hwbuf = NULL;
206                 return DSERR_OUTOFMEMORY;
207         }
208
209         if (device->state == STATE_PLAYING) device->state = STATE_STARTING;
210         else if (device->state == STATE_STOPPING) device->state = STATE_STOPPED;
211
212         /* are we using waveOut stuff? */
213         if (!device->driver) {
214                 LPBYTE newbuf;
215                 LPWAVEHDR headers = NULL;
216                 DWORD overshot;
217                 unsigned int c;
218
219                 /* Start in pause mode, to allow buffers to get filled */
220                 waveOutPause(device->hwo);
221
222                 TRACE("desired buflen=%d, old buffer=%p\n", buflen, device->buffer);
223
224                 /* reallocate emulated primary buffer */
225                 if (device->buffer)
226                         newbuf = HeapReAlloc(GetProcessHeap(),0,device->buffer, buflen);
227                 else
228                         newbuf = HeapAlloc(GetProcessHeap(),0, buflen);
229
230                 if (!newbuf) {
231                         ERR("failed to allocate primary buffer\n");
232                         return DSERR_OUTOFMEMORY;
233                         /* but the old buffer might still exist and must be re-prepared */
234                 }
235
236                 DSOUND_RecalcPrimary(device);
237                 if (device->pwave)
238                         headers = HeapReAlloc(GetProcessHeap(),0,device->pwave, device->helfrags * sizeof(WAVEHDR));
239                 else
240                         headers = HeapAlloc(GetProcessHeap(),0,device->helfrags * sizeof(WAVEHDR));
241
242                 if (!headers) {
243                         ERR("failed to allocate wave headers\n");
244                         HeapFree(GetProcessHeap(), 0, newbuf);
245                         DSOUND_RecalcPrimary(device);
246                         return DSERR_OUTOFMEMORY;
247                 }
248
249                 device->buffer = newbuf;
250                 device->pwave = headers;
251
252                 /* prepare fragment headers */
253                 for (c=0; c<device->helfrags; c++) {
254                         device->pwave[c].lpData = (char*)device->buffer + c*device->fraglen;
255                         device->pwave[c].dwBufferLength = device->fraglen;
256                         device->pwave[c].dwUser = (DWORD_PTR)device;
257                         device->pwave[c].dwFlags = 0;
258                         device->pwave[c].dwLoops = 0;
259                         err = mmErr(waveOutPrepareHeader(device->hwo,&device->pwave[c],sizeof(WAVEHDR)));
260                         if (err != DS_OK) {
261                                 while (c--)
262                                         waveOutUnprepareHeader(device->hwo,&device->pwave[c],sizeof(WAVEHDR));
263                                 break;
264                         }
265                 }
266
267                 overshot = device->buflen % device->fraglen;
268                 /* sanity */
269                 if(overshot)
270                 {
271                         overshot -= overshot % device->pwfx->nBlockAlign;
272                         device->pwave[device->helfrags - 1].dwBufferLength += overshot;
273                 }
274
275                 TRACE("fraglen=%d, overshot=%d\n", device->fraglen, overshot);
276         }
277         device->mixfunction = mixfunctions[device->pwfx->wBitsPerSample/8 - 1];
278         device->normfunction = normfunctions[device->pwfx->wBitsPerSample/8 - 1];
279         FillMemory(device->buffer, device->buflen, (device->pwfx->wBitsPerSample == 8) ? 128 : 0);
280         FillMemory(device->mix_buffer, device->mix_buffer_len, 0);
281         device->pwplay = device->pwqueue = device->playpos = device->mixpos = 0;
282         return err;
283 }
284
285
286 static void DSOUND_PrimaryClose(DirectSoundDevice *device)
287 {
288         TRACE("(%p)\n", device);
289
290         /* are we using waveOut stuff? */
291         if (!device->hwbuf) {
292                 unsigned c;
293
294                 /* get out of CS when calling the wave system */
295                 LeaveCriticalSection(&(device->mixlock));
296                 /* **** */
297                 device->pwqueue = (DWORD)-1; /* resetting queues */
298                 waveOutReset(device->hwo);
299                 for (c=0; c<device->helfrags; c++)
300                         waveOutUnprepareHeader(device->hwo, &device->pwave[c], sizeof(WAVEHDR));
301                 /* **** */
302                 EnterCriticalSection(&(device->mixlock));
303
304                 /* clear the queue */
305                 device->pwqueue = 0;
306         } else {
307                 ULONG ref = IDsDriverBuffer_Release(device->hwbuf);
308                 if (!ref)
309                         device->hwbuf = 0;
310                 else
311                         ERR("Still %d references on primary buffer, refcount leak?\n", ref);
312         }
313 }
314
315 HRESULT DSOUND_PrimaryCreate(DirectSoundDevice *device)
316 {
317         HRESULT err = DS_OK;
318         TRACE("(%p)\n", device);
319
320         device->buflen = ds_hel_buflen;
321         err = DSOUND_PrimaryOpen(device);
322
323         if (err != DS_OK) {
324                 WARN("DSOUND_PrimaryOpen failed\n");
325                 return err;
326         }
327
328         device->state = STATE_STOPPED;
329         return DS_OK;
330 }
331
332 HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device)
333 {
334         TRACE("(%p)\n", device);
335
336         /* **** */
337         EnterCriticalSection(&(device->mixlock));
338
339         DSOUND_PrimaryClose(device);
340         if (device->driver) {
341                 if (device->hwbuf) {
342                         if (IDsDriverBuffer_Release(device->hwbuf) == 0)
343                                 device->hwbuf = 0;
344                 }
345         } else
346                 HeapFree(GetProcessHeap(),0,device->pwave);
347         HeapFree(GetProcessHeap(),0,device->pwfx);
348         device->pwfx=NULL;
349
350         LeaveCriticalSection(&(device->mixlock));
351         /* **** */
352
353         return DS_OK;
354 }
355
356 HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device)
357 {
358         HRESULT err = DS_OK;
359         TRACE("(%p)\n", device);
360
361         if (device->hwbuf) {
362                 err = IDsDriverBuffer_Play(device->hwbuf, 0, 0, DSBPLAY_LOOPING);
363                 if (err != DS_OK)
364                         WARN("IDsDriverBuffer_Play failed\n");
365         } else {
366                 err = mmErr(waveOutRestart(device->hwo));
367                 if (err != DS_OK)
368                         WARN("waveOutRestart failed\n");
369         }
370
371         return err;
372 }
373
374 HRESULT DSOUND_PrimaryStop(DirectSoundDevice *device)
375 {
376         HRESULT err = DS_OK;
377         TRACE("(%p)\n", device);
378
379         if (device->hwbuf) {
380                 err = IDsDriverBuffer_Stop(device->hwbuf);
381                 if (err == DSERR_BUFFERLOST) {
382                         DSOUND_PrimaryClose(device);
383                         err = DSOUND_ReopenDevice(device, FALSE);
384                         if (FAILED(err))
385                                 ERR("DSOUND_ReopenDevice failed\n");
386                         else
387                         {
388                                 err = DSOUND_PrimaryOpen(device);
389                                 if (FAILED(err))
390                                         WARN("DSOUND_PrimaryOpen failed\n");
391                         }
392                 } else if (err != DS_OK) {
393                         WARN("IDsDriverBuffer_Stop failed\n");
394                 }
395         } else {
396
397                 /* don't call the wave system with the lock set */
398                 LeaveCriticalSection(&(device->mixlock));
399                 /* **** */
400
401                 err = mmErr(waveOutPause(device->hwo));
402
403                 /* **** */
404                 EnterCriticalSection(&(device->mixlock));
405
406                 if (err != DS_OK)
407                         WARN("waveOutPause failed\n");
408         }
409
410         return err;
411 }
412
413 HRESULT DSOUND_PrimaryGetPosition(DirectSoundDevice *device, LPDWORD playpos, LPDWORD writepos)
414 {
415         TRACE("(%p,%p,%p)\n", device, playpos, writepos);
416
417         if (device->hwbuf) {
418                 HRESULT err=IDsDriverBuffer_GetPosition(device->hwbuf,playpos,writepos);
419                 if (err != S_OK) {
420                         WARN("IDsDriverBuffer_GetPosition failed\n");
421                         return err;
422                 }
423         } else {
424                 TRACE("pwplay=%i, pwqueue=%i\n", device->pwplay, device->pwqueue);
425
426                 /* check if playpos was requested */
427                 if (playpos)
428                         /* use the cached play position */
429                         *playpos = device->pwplay * device->fraglen;
430
431                 /* check if writepos was requested */
432                 if (writepos)
433                         /* the writepos is the first non-queued position */
434                         *writepos = ((device->pwplay + device->pwqueue) % device->helfrags) * device->fraglen;
435         }
436         TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:-1, writepos?*writepos:-1, device, GetTickCount());
437         return DS_OK;
438 }
439
440 HRESULT DSOUND_PrimarySetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX wfex, BOOL forced)
441 {
442         HRESULT err = DSERR_BUFFERLOST;
443         int i, alloc_size, cp_size;
444         DWORD nSamplesPerSec, bpp, chans;
445         TRACE("(%p,%p)\n", device, wfex);
446
447         if (device->priolevel == DSSCL_NORMAL) {
448                 WARN("failed priority check!\n");
449                 return DSERR_PRIOLEVELNEEDED;
450         }
451
452         /* Let's be pedantic! */
453         if (wfex == NULL) {
454                 WARN("invalid parameter: wfex==NULL!\n");
455                 return DSERR_INVALIDPARAM;
456         }
457         TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
458               "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
459               wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
460               wfex->nAvgBytesPerSec, wfex->nBlockAlign,
461               wfex->wBitsPerSample, wfex->cbSize);
462
463         /* **** */
464         RtlAcquireResourceExclusive(&(device->buffer_list_lock), TRUE);
465         EnterCriticalSection(&(device->mixlock));
466
467         if (wfex->wFormatTag == WAVE_FORMAT_PCM) {
468             alloc_size = sizeof(WAVEFORMATEX);
469             cp_size = sizeof(PCMWAVEFORMAT);
470         } else
471             alloc_size = cp_size = sizeof(WAVEFORMATEX) + wfex->cbSize;
472
473         device->pwfx = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,device->pwfx,alloc_size);
474
475         nSamplesPerSec = device->pwfx->nSamplesPerSec;
476         bpp = device->pwfx->wBitsPerSample;
477         chans = device->pwfx->nChannels;
478
479         CopyMemory(device->pwfx, wfex, cp_size);
480
481         if (!(device->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMSETFORMAT) && device->hwbuf) {
482                 err = IDsDriverBuffer_SetFormat(device->hwbuf, device->pwfx);
483
484                 /* On bad format, try to re-create, big chance it will work then, only do this if we <HAVE> to */
485                 if (forced && (device->pwfx->nSamplesPerSec/100 != wfex->nSamplesPerSec/100 || err == DSERR_BADFORMAT))
486                 {
487                         err = DSERR_BUFFERLOST;
488                         CopyMemory(device->pwfx, wfex, cp_size);
489                 }
490
491                 if (err != DSERR_BUFFERLOST && FAILED(err)) {
492                         WARN("IDsDriverBuffer_SetFormat failed\n");
493                         if (!forced)
494                                 err = DS_OK;
495                         goto done;
496                 }
497
498                 if (err == S_FALSE)
499                 {
500                         /* ALSA specific: S_FALSE tells that recreation was successful,
501                          * but size and location may be changed, and buffer has to be restarted
502                          * I put it here, so if frequency doesn't match the error will be changed to DSERR_BUFFERLOST
503                          * and the entire re-initialization will occur anyway
504                          */
505                         IDsDriverBuffer_Lock(device->hwbuf, (LPVOID *)&device->buffer, &device->buflen, NULL, NULL, 0, 0, DSBLOCK_ENTIREBUFFER);
506                         IDsDriverBuffer_Unlock(device->hwbuf, device->buffer, 0, NULL, 0);
507
508                         if (device->state == STATE_PLAYING) device->state = STATE_STARTING;
509                         else if (device->state == STATE_STOPPING) device->state = STATE_STOPPED;
510                         device->pwplay = device->pwqueue = device->playpos = device->mixpos = 0;
511                         err = DS_OK;
512                 }
513                 DSOUND_RecalcPrimary(device);
514         }
515
516         if (err == DSERR_BUFFERLOST)
517         {
518                 DSOUND_PrimaryClose(device);
519
520                 err = DSOUND_ReopenDevice(device, FALSE);
521                 if (FAILED(err))
522                 {
523                         WARN("DSOUND_ReopenDevice failed: %08x\n", err);
524                         goto done;
525                 }
526                 err = DSOUND_PrimaryOpen(device);
527                 if (err != DS_OK) {
528                         WARN("DSOUND_PrimaryOpen failed\n");
529                         goto done;
530                 }
531
532                 if (wfex->nSamplesPerSec/100 != device->pwfx->nSamplesPerSec/100 && forced && device->buffer)
533                 {
534                         DSOUND_PrimaryClose(device);
535                         device->pwfx->nSamplesPerSec = wfex->nSamplesPerSec;
536                         err = DSOUND_ReopenDevice(device, TRUE);
537                         if (FAILED(err))
538                                 WARN("DSOUND_ReopenDevice(2) failed: %08x\n", err);
539                         else if (FAILED((err = DSOUND_PrimaryOpen(device))))
540                                 WARN("DSOUND_PrimaryOpen(2) failed: %08x\n", err);
541                 }
542         }
543
544         device->mix_buffer_len = DSOUND_bufpos_to_mixpos(device, device->buflen);
545         device->mix_buffer = HeapReAlloc(GetProcessHeap(), 0, device->mix_buffer, device->mix_buffer_len);
546         FillMemory(device->mix_buffer, device->mix_buffer_len, 0);
547         device->mixfunction = mixfunctions[device->pwfx->wBitsPerSample/8 - 1];
548         device->normfunction = normfunctions[device->pwfx->wBitsPerSample/8 - 1];
549
550         if (nSamplesPerSec != device->pwfx->nSamplesPerSec || bpp != device->pwfx->wBitsPerSample || chans != device->pwfx->nChannels) {
551                 IDirectSoundBufferImpl** dsb = device->buffers;
552                 for (i = 0; i < device->nrofbuffers; i++, dsb++) {
553                         /* **** */
554                         RtlAcquireResourceExclusive(&(*dsb)->lock, TRUE);
555
556                         (*dsb)->freqAdjust = ((DWORD64)(*dsb)->freq << DSOUND_FREQSHIFT) / device->pwfx->nSamplesPerSec;
557                         DSOUND_RecalcFormat((*dsb));
558                         DSOUND_MixToTemporary((*dsb), 0, (*dsb)->buflen, FALSE);
559                         (*dsb)->primary_mixpos = 0;
560
561                         RtlReleaseResource(&(*dsb)->lock);
562                         /* **** */
563                 }
564         }
565
566 done:
567         LeaveCriticalSection(&(device->mixlock));
568         RtlReleaseResource(&(device->buffer_list_lock));
569         /* **** */
570
571         return err;
572 }
573
574 /*******************************************************************************
575  *              PrimaryBuffer
576  */
577 /* This sets this format for the <em>Primary Buffer Only</em> */
578 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
579 static HRESULT WINAPI PrimaryBufferImpl_SetFormat(
580     LPDIRECTSOUNDBUFFER iface,
581     LPCWAVEFORMATEX wfex)
582 {
583     DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
584     TRACE("(%p,%p)\n", iface, wfex);
585     return DSOUND_PrimarySetFormat(device, wfex, device->priolevel == DSSCL_WRITEPRIMARY);
586 }
587
588 static HRESULT WINAPI PrimaryBufferImpl_SetVolume(
589         LPDIRECTSOUNDBUFFER iface,LONG vol
590 ) {
591         DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
592         DWORD ampfactors;
593         HRESULT hres = DS_OK;
594         TRACE("(%p,%d)\n", iface, vol);
595
596         if (!(device->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
597                 WARN("control unavailable\n");
598                 return DSERR_CONTROLUNAVAIL;
599         }
600
601         if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
602                 WARN("invalid parameter: vol = %d\n", vol);
603                 return DSERR_INVALIDPARAM;
604         }
605
606         /* **** */
607         EnterCriticalSection(&(device->mixlock));
608
609         waveOutGetVolume(device->hwo, &ampfactors);
610         device->volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
611         device->volpan.dwTotalRightAmpFactor=ampfactors >> 16;
612         DSOUND_AmpFactorToVolPan(&device->volpan);
613         if (vol != device->volpan.lVolume) {
614             device->volpan.lVolume=vol;
615             DSOUND_RecalcVolPan(&device->volpan);
616             if (device->hwbuf) {
617                 hres = IDsDriverBuffer_SetVolumePan(device->hwbuf, &device->volpan);
618                 if (hres != DS_OK)
619                     WARN("IDsDriverBuffer_SetVolumePan failed\n");
620             } else {
621                 ampfactors = (device->volpan.dwTotalLeftAmpFactor & 0xffff) | (device->volpan.dwTotalRightAmpFactor << 16);
622                 waveOutSetVolume(device->hwo, ampfactors);
623             }
624         }
625
626         LeaveCriticalSection(&(device->mixlock));
627         /* **** */
628
629         return hres;
630 }
631
632 static HRESULT WINAPI PrimaryBufferImpl_GetVolume(
633         LPDIRECTSOUNDBUFFER iface,LPLONG vol
634 ) {
635         DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
636         DWORD ampfactors;
637         TRACE("(%p,%p)\n", iface, vol);
638
639         if (!(device->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
640                 WARN("control unavailable\n");
641                 return DSERR_CONTROLUNAVAIL;
642         }
643
644         if (vol == NULL) {
645                 WARN("invalid parameter: vol = NULL\n");
646                 return DSERR_INVALIDPARAM;
647         }
648
649         if (!device->hwbuf)
650         {
651             waveOutGetVolume(device->hwo, &ampfactors);
652             device->volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
653             device->volpan.dwTotalRightAmpFactor=ampfactors >> 16;
654             DSOUND_AmpFactorToVolPan(&device->volpan);
655         }
656         *vol = device->volpan.lVolume;
657         return DS_OK;
658 }
659
660 static HRESULT WINAPI PrimaryBufferImpl_SetFrequency(
661         LPDIRECTSOUNDBUFFER iface,DWORD freq
662 ) {
663         PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
664         TRACE("(%p,%d)\n",This,freq);
665
666         /* You cannot set the frequency of the primary buffer */
667         WARN("control unavailable\n");
668         return DSERR_CONTROLUNAVAIL;
669 }
670
671 static HRESULT WINAPI PrimaryBufferImpl_Play(
672         LPDIRECTSOUNDBUFFER iface,DWORD reserved1,DWORD reserved2,DWORD flags
673 ) {
674         DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
675         TRACE("(%p,%08x,%08x,%08x)\n", iface, reserved1, reserved2, flags);
676
677         if (!(flags & DSBPLAY_LOOPING)) {
678                 WARN("invalid parameter: flags = %08x\n", flags);
679                 return DSERR_INVALIDPARAM;
680         }
681
682         /* **** */
683         EnterCriticalSection(&(device->mixlock));
684
685         if (device->state == STATE_STOPPED)
686                 device->state = STATE_STARTING;
687         else if (device->state == STATE_STOPPING)
688                 device->state = STATE_PLAYING;
689
690         LeaveCriticalSection(&(device->mixlock));
691         /* **** */
692
693         return DS_OK;
694 }
695
696 static HRESULT WINAPI PrimaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER iface)
697 {
698         DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
699         TRACE("(%p)\n", iface);
700
701         /* **** */
702         EnterCriticalSection(&(device->mixlock));
703
704         if (device->state == STATE_PLAYING)
705                 device->state = STATE_STOPPING;
706         else if (device->state == STATE_STARTING)
707                 device->state = STATE_STOPPED;
708
709         LeaveCriticalSection(&(device->mixlock));
710         /* **** */
711
712         return DS_OK;
713 }
714
715 static ULONG WINAPI PrimaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER iface)
716 {
717     PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
718     ULONG ref = InterlockedIncrement(&(This->ref));
719     TRACE("(%p) ref was %d\n", This, ref - 1);
720     return ref;
721 }
722
723 static ULONG WINAPI PrimaryBufferImpl_Release(LPDIRECTSOUNDBUFFER iface)
724 {
725     PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
726     DWORD ref = InterlockedDecrement(&(This->ref));
727     TRACE("(%p) ref was %d\n", This, ref + 1);
728
729     if (!ref) {
730         This->device->primary = NULL;
731         HeapFree(GetProcessHeap(), 0, This);
732         TRACE("(%p) released\n", This);
733     }
734     return ref;
735 }
736
737 static HRESULT WINAPI PrimaryBufferImpl_GetCurrentPosition(
738         LPDIRECTSOUNDBUFFER iface,LPDWORD playpos,LPDWORD writepos
739 ) {
740         HRESULT hres;
741         DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
742         TRACE("(%p,%p,%p)\n", iface, playpos, writepos);
743
744         /* **** */
745         EnterCriticalSection(&(device->mixlock));
746
747         hres = DSOUND_PrimaryGetPosition(device, playpos, writepos);
748         if (hres != DS_OK) {
749                 WARN("DSOUND_PrimaryGetPosition failed\n");
750                 LeaveCriticalSection(&(device->mixlock));
751                 return hres;
752         }
753         if (writepos) {
754                 if (device->state != STATE_STOPPED)
755                         /* apply the documented 10ms lead to writepos */
756                         *writepos += device->writelead;
757                 while (*writepos >= device->buflen) *writepos -= device->buflen;
758         }
759
760         LeaveCriticalSection(&(device->mixlock));
761         /* **** */
762
763         TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:0, writepos?*writepos:0, device, GetTickCount());
764         return DS_OK;
765 }
766
767 static HRESULT WINAPI PrimaryBufferImpl_GetStatus(
768         LPDIRECTSOUNDBUFFER iface,LPDWORD status
769 ) {
770         DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
771         TRACE("(%p,%p)\n", iface, status);
772
773         if (status == NULL) {
774                 WARN("invalid parameter: status == NULL\n");
775                 return DSERR_INVALIDPARAM;
776         }
777
778         *status = 0;
779         if ((device->state == STATE_STARTING) ||
780             (device->state == STATE_PLAYING))
781                 *status |= DSBSTATUS_PLAYING | DSBSTATUS_LOOPING;
782
783         TRACE("status=%x\n", *status);
784         return DS_OK;
785 }
786
787
788 static HRESULT WINAPI PrimaryBufferImpl_GetFormat(
789     LPDIRECTSOUNDBUFFER iface,
790     LPWAVEFORMATEX lpwf,
791     DWORD wfsize,
792     LPDWORD wfwritten)
793 {
794     DWORD size;
795     DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
796     TRACE("(%p,%p,%d,%p)\n", iface, lpwf, wfsize, wfwritten);
797
798     size = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
799
800     if (lpwf) { /* NULL is valid */
801         if (wfsize >= size) {
802             CopyMemory(lpwf,device->pwfx,size);
803             if (wfwritten)
804                 *wfwritten = size;
805         } else {
806             WARN("invalid parameter: wfsize too small\n");
807             if (wfwritten)
808                 *wfwritten = 0;
809             return DSERR_INVALIDPARAM;
810         }
811     } else {
812         if (wfwritten)
813             *wfwritten = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
814         else {
815             WARN("invalid parameter: wfwritten == NULL\n");
816             return DSERR_INVALIDPARAM;
817         }
818     }
819
820     return DS_OK;
821 }
822
823 static HRESULT WINAPI PrimaryBufferImpl_Lock(
824         LPDIRECTSOUNDBUFFER iface,DWORD writecursor,DWORD writebytes,LPVOID *lplpaudioptr1,LPDWORD audiobytes1,LPVOID *lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
825 ) {
826         HRESULT hres;
827         DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
828         TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
829                 iface,
830                 writecursor,
831                 writebytes,
832                 lplpaudioptr1,
833                 audiobytes1,
834                 lplpaudioptr2,
835                 audiobytes2,
836                 flags,
837                 GetTickCount()
838         );
839
840         if (!audiobytes1)
841             return DSERR_INVALIDPARAM;
842
843         if (device->priolevel != DSSCL_WRITEPRIMARY) {
844                 WARN("failed priority check!\n");
845                 return DSERR_PRIOLEVELNEEDED;
846         }
847
848         /* when this flag is set, writecursor is meaningless and must be calculated */
849         if (flags & DSBLOCK_FROMWRITECURSOR) {
850                 /* GetCurrentPosition does too much magic to duplicate here */
851                 hres = IDirectSoundBuffer_GetCurrentPosition(iface, NULL, &writecursor);
852                 if (hres != DS_OK) {
853                         WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
854                         return hres;
855                 }
856         }
857
858         /* when this flag is set, writebytes is meaningless and must be set */
859         if (flags & DSBLOCK_ENTIREBUFFER)
860                 writebytes = device->buflen;
861
862         if (writecursor >= device->buflen) {
863                 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
864                      writecursor, device->buflen);
865                 return DSERR_INVALIDPARAM;
866         }
867
868         if (writebytes > device->buflen) {
869                 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
870                      writebytes, device->buflen);
871                 return DSERR_INVALIDPARAM;
872         }
873
874         if (!(device->drvdesc.dwFlags & DSDDESC_DONTNEEDPRIMARYLOCK) && device->hwbuf) {
875                 hres = IDsDriverBuffer_Lock(device->hwbuf,
876                                             lplpaudioptr1, audiobytes1,
877                                             lplpaudioptr2, audiobytes2,
878                                             writecursor, writebytes,
879                                             0);
880                 if (hres != DS_OK) {
881                         WARN("IDsDriverBuffer_Lock failed\n");
882                         return hres;
883                 }
884         } else {
885                 if (writecursor+writebytes <= device->buflen) {
886                         *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
887                         *audiobytes1 = writebytes;
888                         if (lplpaudioptr2)
889                                 *(LPBYTE*)lplpaudioptr2 = NULL;
890                         if (audiobytes2)
891                                 *audiobytes2 = 0;
892                         TRACE("->%d.0\n",writebytes);
893                 } else {
894                         *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
895                         *audiobytes1 = device->buflen-writecursor;
896                         if (lplpaudioptr2)
897                                 *(LPBYTE*)lplpaudioptr2 = device->buffer;
898                         if (audiobytes2)
899                                 *audiobytes2 = writebytes-(device->buflen-writecursor);
900                         TRACE("->%d.%d\n",*audiobytes1,audiobytes2?*audiobytes2:0);
901                 }
902         }
903         return DS_OK;
904 }
905
906 static HRESULT WINAPI PrimaryBufferImpl_SetCurrentPosition(
907         LPDIRECTSOUNDBUFFER iface,DWORD newpos
908 ) {
909         PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
910         TRACE("(%p,%d)\n",This,newpos);
911
912         /* You cannot set the position of the primary buffer */
913         WARN("invalid call\n");
914         return DSERR_INVALIDCALL;
915 }
916
917 static HRESULT WINAPI PrimaryBufferImpl_SetPan(
918         LPDIRECTSOUNDBUFFER iface,LONG pan
919 ) {
920         DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
921         DWORD ampfactors;
922         HRESULT hres = DS_OK;
923         TRACE("(%p,%d)\n", iface, pan);
924
925         if (!(device->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
926                 WARN("control unavailable\n");
927                 return DSERR_CONTROLUNAVAIL;
928         }
929
930         if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
931                 WARN("invalid parameter: pan = %d\n", pan);
932                 return DSERR_INVALIDPARAM;
933         }
934
935         /* **** */
936         EnterCriticalSection(&(device->mixlock));
937
938         if (!device->hwbuf)
939         {
940             waveOutGetVolume(device->hwo, &ampfactors);
941             device->volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
942             device->volpan.dwTotalRightAmpFactor=ampfactors >> 16;
943             DSOUND_AmpFactorToVolPan(&device->volpan);
944         }
945         if (pan != device->volpan.lPan) {
946             device->volpan.lPan=pan;
947             DSOUND_RecalcVolPan(&device->volpan);
948             if (device->hwbuf) {
949                 hres = IDsDriverBuffer_SetVolumePan(device->hwbuf, &device->volpan);
950                 if (hres != DS_OK)
951                     WARN("IDsDriverBuffer_SetVolumePan failed\n");
952             } else {
953                 ampfactors = (device->volpan.dwTotalLeftAmpFactor & 0xffff) | (device->volpan.dwTotalRightAmpFactor << 16);
954                 waveOutSetVolume(device->hwo, ampfactors);
955             }
956         }
957
958         LeaveCriticalSection(&(device->mixlock));
959         /* **** */
960
961         return hres;
962 }
963
964 static HRESULT WINAPI PrimaryBufferImpl_GetPan(
965         LPDIRECTSOUNDBUFFER iface,LPLONG pan
966 ) {
967         DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
968         DWORD ampfactors;
969         TRACE("(%p,%p)\n", iface, pan);
970
971         if (!(device->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
972                 WARN("control unavailable\n");
973                 return DSERR_CONTROLUNAVAIL;
974         }
975
976         if (pan == NULL) {
977                 WARN("invalid parameter: pan == NULL\n");
978                 return DSERR_INVALIDPARAM;
979         }
980
981         if (!device->hwbuf)
982         {
983             waveOutGetVolume(device->hwo, &ampfactors);
984             device->volpan.dwTotalLeftAmpFactor=ampfactors & 0xffff;
985             device->volpan.dwTotalRightAmpFactor=ampfactors >> 16;
986             DSOUND_AmpFactorToVolPan(&device->volpan);
987         }
988         *pan = device->volpan.lPan;
989         return DS_OK;
990 }
991
992 static HRESULT WINAPI PrimaryBufferImpl_Unlock(
993         LPDIRECTSOUNDBUFFER iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
994 ) {
995         DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
996         TRACE("(%p,%p,%d,%p,%d)\n", iface, p1, x1, p2, x2);
997
998         if (device->priolevel != DSSCL_WRITEPRIMARY) {
999                 WARN("failed priority check!\n");
1000                 return DSERR_PRIOLEVELNEEDED;
1001         }
1002
1003         if (!(device->drvdesc.dwFlags & DSDDESC_DONTNEEDPRIMARYLOCK) && device->hwbuf) {
1004                 HRESULT hres;
1005
1006                 if ((char *)p1 - (char *)device->buffer + x1 > device->buflen)
1007                     hres = DSERR_INVALIDPARAM;
1008                 else
1009                     hres = IDsDriverBuffer_Unlock(device->hwbuf, p1, x1, p2, x2);
1010
1011                 if (hres != DS_OK) {
1012                         WARN("IDsDriverBuffer_Unlock failed\n");
1013                         return hres;
1014                 }
1015         }
1016
1017         return DS_OK;
1018 }
1019
1020 static HRESULT WINAPI PrimaryBufferImpl_Restore(
1021         LPDIRECTSOUNDBUFFER iface
1022 ) {
1023         PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
1024         FIXME("(%p):stub\n",This);
1025         return DS_OK;
1026 }
1027
1028 static HRESULT WINAPI PrimaryBufferImpl_GetFrequency(
1029         LPDIRECTSOUNDBUFFER iface,LPDWORD freq
1030 ) {
1031         DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
1032         TRACE("(%p,%p)\n", iface, freq);
1033
1034         if (freq == NULL) {
1035                 WARN("invalid parameter: freq == NULL\n");
1036                 return DSERR_INVALIDPARAM;
1037         }
1038
1039         if (!(device->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
1040                 WARN("control unavailable\n");
1041                 return DSERR_CONTROLUNAVAIL;
1042         }
1043
1044         *freq = device->pwfx->nSamplesPerSec;
1045         TRACE("-> %d\n", *freq);
1046
1047         return DS_OK;
1048 }
1049
1050 static HRESULT WINAPI PrimaryBufferImpl_Initialize(
1051         LPDIRECTSOUNDBUFFER iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd
1052 ) {
1053         PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
1054         WARN("(%p) already initialized\n", This);
1055         return DSERR_ALREADYINITIALIZED;
1056 }
1057
1058 static HRESULT WINAPI PrimaryBufferImpl_GetCaps(
1059         LPDIRECTSOUNDBUFFER iface,LPDSBCAPS caps
1060 ) {
1061         DirectSoundDevice *device = ((PrimaryBufferImpl *)iface)->device;
1062         TRACE("(%p,%p)\n", iface, caps);
1063
1064         if (caps == NULL) {
1065                 WARN("invalid parameter: caps == NULL\n");
1066                 return DSERR_INVALIDPARAM;
1067         }
1068
1069         if (caps->dwSize < sizeof(*caps)) {
1070                 WARN("invalid parameter: caps->dwSize = %d\n", caps->dwSize);
1071                 return DSERR_INVALIDPARAM;
1072         }
1073
1074         caps->dwFlags = device->dsbd.dwFlags;
1075         caps->dwBufferBytes = device->buflen;
1076
1077         /* Windows reports these as zero */
1078         caps->dwUnlockTransferRate = 0;
1079         caps->dwPlayCpuOverhead = 0;
1080
1081         return DS_OK;
1082 }
1083
1084 static HRESULT WINAPI PrimaryBufferImpl_QueryInterface(
1085         LPDIRECTSOUNDBUFFER iface,REFIID riid,LPVOID *ppobj
1086 ) {
1087         PrimaryBufferImpl *This = (PrimaryBufferImpl *)iface;
1088         DirectSoundDevice *device = This->device;
1089         TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppobj);
1090
1091         if (ppobj == NULL) {
1092                 WARN("invalid parameter\n");
1093                 return E_INVALIDARG;
1094         }
1095
1096         *ppobj = NULL;  /* assume failure */
1097
1098         if ( IsEqualGUID(riid, &IID_IUnknown) ||
1099              IsEqualGUID(riid, &IID_IDirectSoundBuffer) ) {
1100                 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)This);
1101                 *ppobj = This;
1102                 return S_OK;
1103         }
1104
1105         /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
1106         /* a primary buffer can't have a DirectSoundBuffer8 interface */
1107         if ( IsEqualGUID( &IID_IDirectSoundBuffer8, riid ) ) {
1108                 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
1109                 return E_NOINTERFACE;
1110         }
1111
1112         if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
1113                 ERR("app requested IDirectSoundNotify on primary buffer\n");
1114                 /* FIXME: should we support this? */
1115                 return E_NOINTERFACE;
1116         }
1117
1118         if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
1119                 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1120                 return E_NOINTERFACE;
1121         }
1122
1123         if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
1124                 if (!device->listener)
1125                         IDirectSound3DListenerImpl_Create(device, &device->listener);
1126                 if (device->listener) {
1127                         *ppobj = device->listener;
1128                         IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)*ppobj);
1129                         return S_OK;
1130                 }
1131
1132                 WARN("IID_IDirectSound3DListener failed\n");
1133                 return E_NOINTERFACE;
1134         }
1135
1136         if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
1137                 FIXME("app requested IKsPropertySet on primary buffer\n");
1138                 return E_NOINTERFACE;
1139         }
1140
1141         FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
1142         return E_NOINTERFACE;
1143 }
1144
1145 static const IDirectSoundBufferVtbl dspbvt =
1146 {
1147         PrimaryBufferImpl_QueryInterface,
1148         PrimaryBufferImpl_AddRef,
1149         PrimaryBufferImpl_Release,
1150         PrimaryBufferImpl_GetCaps,
1151         PrimaryBufferImpl_GetCurrentPosition,
1152         PrimaryBufferImpl_GetFormat,
1153         PrimaryBufferImpl_GetVolume,
1154         PrimaryBufferImpl_GetPan,
1155         PrimaryBufferImpl_GetFrequency,
1156         PrimaryBufferImpl_GetStatus,
1157         PrimaryBufferImpl_Initialize,
1158         PrimaryBufferImpl_Lock,
1159         PrimaryBufferImpl_Play,
1160         PrimaryBufferImpl_SetCurrentPosition,
1161         PrimaryBufferImpl_SetFormat,
1162         PrimaryBufferImpl_SetVolume,
1163         PrimaryBufferImpl_SetPan,
1164         PrimaryBufferImpl_SetFrequency,
1165         PrimaryBufferImpl_Stop,
1166         PrimaryBufferImpl_Unlock,
1167         PrimaryBufferImpl_Restore
1168 };
1169
1170 HRESULT PrimaryBufferImpl_Create(
1171         DirectSoundDevice * device,
1172         PrimaryBufferImpl ** ppdsb,
1173         LPCDSBUFFERDESC dsbd)
1174 {
1175         PrimaryBufferImpl *dsb;
1176         TRACE("%p,%p,%p)\n",device,ppdsb,dsbd);
1177
1178         if (dsbd->lpwfxFormat) {
1179                 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1180                 *ppdsb = NULL;
1181                 return DSERR_INVALIDPARAM;
1182         }
1183
1184         dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1185
1186         if (dsb == NULL) {
1187                 WARN("out of memory\n");
1188                 *ppdsb = NULL;
1189                 return DSERR_OUTOFMEMORY;
1190         }
1191
1192         dsb->ref = 0;
1193         dsb->device = device;
1194         dsb->lpVtbl = &dspbvt;
1195
1196         device->dsbd = *dsbd;
1197
1198         TRACE("Created primary buffer at %p\n", dsb);
1199         TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1200                 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1201                 device->pwfx->wFormatTag, device->pwfx->nChannels,
1202                 device->pwfx->nSamplesPerSec, device->pwfx->nAvgBytesPerSec,
1203                 device->pwfx->nBlockAlign, device->pwfx->wBitsPerSample,
1204                 device->pwfx->cbSize);
1205
1206         *ppdsb = dsb;
1207         return S_OK;
1208 }