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