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