winegstreamer: Register stub ColorConvertDMO transform.
[wine.git] / dlls / dsound / primary.c
blob4f46f57926d52a628665e9651ac6a3db45c32189
1 /* DirectSound
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 * Copyright 2000-2002 TransGaming Technologies, Inc.
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.
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.
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
21 * TODO:
22 * When PrimarySetFormat (via ReopenDevice or PrimaryOpen) fails,
23 * it leaves dsound in unusable (not really open) state.
26 #include <stdarg.h>
28 #define COBJMACROS
29 #define NONAMELESSUNION
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winuser.h"
34 #include "mmsystem.h"
35 #include "mmddk.h"
36 #include "wine/debug.h"
37 #include "dsound.h"
38 #include "dsound_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
42 static DWORD speaker_config_to_channel_mask(DWORD speaker_config)
44 switch (DSSPEAKER_CONFIG(speaker_config)) {
45 case DSSPEAKER_MONO:
46 return SPEAKER_FRONT_LEFT;
48 case DSSPEAKER_STEREO:
49 case DSSPEAKER_HEADPHONE:
50 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
52 case DSSPEAKER_QUAD:
53 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT;
55 case DSSPEAKER_5POINT1_BACK:
56 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_FRONT_CENTER | SPEAKER_LOW_FREQUENCY | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT;
59 WARN("unknown speaker_config %lu\n", speaker_config);
60 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
63 static DWORD DSOUND_FindSpeakerConfig(IMMDevice *mmdevice, int channels)
65 IPropertyStore *store;
66 HRESULT hr;
67 PROPVARIANT pv;
68 ULONG phys_speakers;
70 const DWORD def = DSSPEAKER_COMBINED(DSSPEAKER_STEREO, DSSPEAKER_GEOMETRY_WIDE);
72 hr = IMMDevice_OpenPropertyStore(mmdevice, STGM_READ, &store);
73 if (FAILED(hr)) {
74 WARN("IMMDevice_OpenPropertyStore failed: %08lx\n", hr);
75 return def;
78 hr = IPropertyStore_GetValue(store, &PKEY_AudioEndpoint_PhysicalSpeakers, &pv);
80 if (FAILED(hr)) {
81 WARN("IPropertyStore_GetValue failed: %08lx\n", hr);
82 IPropertyStore_Release(store);
83 return def;
86 if (pv.vt != VT_UI4) {
87 WARN("PKEY_AudioEndpoint_PhysicalSpeakers is not a ULONG: 0x%x\n", pv.vt);
88 PropVariantClear(&pv);
89 IPropertyStore_Release(store);
90 return def;
93 phys_speakers = pv.ulVal;
95 PropVariantClear(&pv);
96 IPropertyStore_Release(store);
98 if ((channels >= 6 || channels == 0) && (phys_speakers & KSAUDIO_SPEAKER_5POINT1) == KSAUDIO_SPEAKER_5POINT1)
99 return DSSPEAKER_5POINT1_BACK;
100 else if ((channels >= 6 || channels == 0) && (phys_speakers & KSAUDIO_SPEAKER_5POINT1_SURROUND) == KSAUDIO_SPEAKER_5POINT1_SURROUND)
101 return DSSPEAKER_5POINT1_SURROUND;
102 else if ((channels >= 4 || channels == 0) && (phys_speakers & KSAUDIO_SPEAKER_QUAD) == KSAUDIO_SPEAKER_QUAD)
103 return DSSPEAKER_QUAD;
104 else if ((channels >= 2 || channels == 0) && (phys_speakers & KSAUDIO_SPEAKER_STEREO) == KSAUDIO_SPEAKER_STEREO)
105 return DSSPEAKER_COMBINED(DSSPEAKER_STEREO, DSSPEAKER_GEOMETRY_WIDE);
106 else if ((phys_speakers & KSAUDIO_SPEAKER_MONO) == KSAUDIO_SPEAKER_MONO)
107 return DSSPEAKER_MONO;
109 return def;
112 static HRESULT DSOUND_WaveFormat(DirectSoundDevice *device, IAudioClient *client,
113 BOOL forcewave, WAVEFORMATEX **wfx)
115 WAVEFORMATEXTENSIBLE *retwfe = NULL;
116 WAVEFORMATEX *w;
117 HRESULT hr;
119 if (!forcewave) {
120 WAVEFORMATEXTENSIBLE *mixwfe;
121 hr = IAudioClient_GetMixFormat(client, (WAVEFORMATEX**)&mixwfe);
123 if (FAILED(hr))
124 return hr;
126 if (mixwfe->Format.nChannels < device->num_speakers) {
127 device->speaker_config = DSOUND_FindSpeakerConfig(device->mmdevice, mixwfe->Format.nChannels);
128 DSOUND_ParseSpeakerConfig(device);
129 } else if (mixwfe->Format.nChannels > device->num_speakers) {
130 mixwfe->Format.nChannels = device->num_speakers;
131 mixwfe->Format.nBlockAlign = mixwfe->Format.nChannels * mixwfe->Format.wBitsPerSample / 8;
132 mixwfe->Format.nAvgBytesPerSec = mixwfe->Format.nSamplesPerSec * mixwfe->Format.nBlockAlign;
133 mixwfe->dwChannelMask = speaker_config_to_channel_mask(device->speaker_config);
136 if (!IsEqualGUID(&mixwfe->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)) {
137 WAVEFORMATEXTENSIBLE testwfe = *mixwfe;
139 testwfe.SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
140 testwfe.Samples.wValidBitsPerSample = testwfe.Format.wBitsPerSample = 32;
141 testwfe.Format.nBlockAlign = testwfe.Format.nChannels * testwfe.Format.wBitsPerSample / 8;
142 testwfe.Format.nAvgBytesPerSec = testwfe.Format.nSamplesPerSec * testwfe.Format.nBlockAlign;
144 if (FAILED(IAudioClient_IsFormatSupported(client, AUDCLNT_SHAREMODE_SHARED, &testwfe.Format, (WAVEFORMATEX**)&retwfe)))
145 w = DSOUND_CopyFormat(&mixwfe->Format);
146 else if (retwfe)
147 w = DSOUND_CopyFormat(&retwfe->Format);
148 else
149 w = DSOUND_CopyFormat(&testwfe.Format);
150 CoTaskMemFree(retwfe);
151 retwfe = NULL;
152 } else
153 w = DSOUND_CopyFormat(&mixwfe->Format);
154 CoTaskMemFree(mixwfe);
155 } else if (device->primary_pwfx->wFormatTag == WAVE_FORMAT_PCM ||
156 device->primary_pwfx->wFormatTag == WAVE_FORMAT_IEEE_FLOAT) {
157 WAVEFORMATEX *wi = device->primary_pwfx;
158 WAVEFORMATEXTENSIBLE *wfe;
160 /* Convert to WAVEFORMATEXTENSIBLE */
161 w = HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEXTENSIBLE));
162 wfe = (WAVEFORMATEXTENSIBLE*)w;
163 if (!wfe)
164 return DSERR_OUTOFMEMORY;
166 wfe->Format = *wi;
167 w->wFormatTag = WAVE_FORMAT_EXTENSIBLE;
168 w->cbSize = sizeof(*wfe) - sizeof(*w);
169 w->nBlockAlign = w->nChannels * w->wBitsPerSample / 8;
170 w->nAvgBytesPerSec = w->nSamplesPerSec * w->nBlockAlign;
172 wfe->dwChannelMask = 0;
173 if (wi->wFormatTag == WAVE_FORMAT_IEEE_FLOAT) {
174 w->wBitsPerSample = 32;
175 wfe->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
176 } else
177 wfe->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
178 wfe->Samples.wValidBitsPerSample = w->wBitsPerSample;
179 } else
180 w = DSOUND_CopyFormat(device->primary_pwfx);
182 if (!w)
183 return DSERR_OUTOFMEMORY;
185 hr = IAudioClient_IsFormatSupported(client, AUDCLNT_SHAREMODE_SHARED, w, (WAVEFORMATEX**)&retwfe);
186 if (retwfe) {
187 memcpy(w, retwfe, sizeof(WAVEFORMATEX) + retwfe->Format.cbSize);
188 CoTaskMemFree(retwfe);
190 if (FAILED(hr)) {
191 WARN("IsFormatSupported failed: %08lx\n", hr);
192 HeapFree(GetProcessHeap(), 0, w);
193 return hr;
195 *wfx = w;
196 return S_OK;
199 static void DSOUND_ReleaseDevice(DirectSoundDevice *device)
201 if(device->client){
202 IAudioClient_Release(device->client);
203 device->client = NULL;
205 if(device->render){
206 IAudioRenderClient_Release(device->render);
207 device->render = NULL;
209 if(device->volume){
210 IAudioStreamVolume_Release(device->volume);
211 device->volume = NULL;
214 if (device->pad) {
215 device->playpos += device->pad;
216 device->playpos %= device->buflen;
217 device->pad = 0;
221 static HRESULT DSOUND_PrimaryOpen(DirectSoundDevice *device, WAVEFORMATEX *wfx, DWORD frames, BOOL forcewave)
223 IDirectSoundBufferImpl** dsb = device->buffers;
224 LPBYTE newbuf;
225 DWORD new_buflen;
226 BOOL mixfloat = FALSE;
227 int i;
229 TRACE("(%p)\n", device);
231 new_buflen = device->buflen;
232 new_buflen -= new_buflen % wfx->nBlockAlign;
234 if (wfx->wFormatTag == WAVE_FORMAT_IEEE_FLOAT ||
235 (wfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
236 IsEqualGUID(&((WAVEFORMATEXTENSIBLE*)wfx)->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)))
237 mixfloat = TRUE;
239 /* reallocate emulated primary buffer */
240 if (forcewave || !mixfloat) {
241 if (!forcewave)
242 new_buflen = frames * wfx->nChannels * sizeof(float);
244 if (device->buffer)
245 newbuf = HeapReAlloc(GetProcessHeap(), 0, device->buffer, new_buflen);
246 else
247 newbuf = HeapAlloc(GetProcessHeap(), 0, new_buflen);
249 if (!newbuf) {
250 ERR("failed to allocate primary buffer\n");
251 return DSERR_OUTOFMEMORY;
253 FillMemory(newbuf, new_buflen, (wfx->wBitsPerSample == 8) ? 128 : 0);
254 } else {
255 HeapFree(GetProcessHeap(), 0, device->buffer);
256 newbuf = NULL;
259 device->buffer = newbuf;
260 device->buflen = new_buflen;
261 HeapFree(GetProcessHeap(), 0, device->pwfx);
262 device->pwfx = wfx;
264 device->writelead = (wfx->nSamplesPerSec / 100) * wfx->nBlockAlign;
266 TRACE("buflen: %lu, frames %lu\n", device->buflen, frames);
268 if (!mixfloat)
269 device->normfunction = normfunctions[wfx->wBitsPerSample/8 - 1];
270 else
271 device->normfunction = NULL;
273 device->playpos = 0;
275 for (i = 0; i < device->nrofbuffers; i++) {
276 AcquireSRWLockExclusive(&dsb[i]->lock);
277 DSOUND_RecalcFormat(dsb[i]);
278 ReleaseSRWLockExclusive(&dsb[i]->lock);
281 return DS_OK;
284 HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave)
286 HRESULT hres;
287 REFERENCE_TIME period;
288 UINT32 acbuf_frames, aclen_frames;
289 DWORD period_ms;
290 IAudioClient *client = NULL;
291 IAudioRenderClient *render = NULL;
292 IAudioStreamVolume *volume = NULL;
293 DWORD frag_frames;
294 WAVEFORMATEX *wfx = NULL;
295 DWORD oldspeakerconfig = device->speaker_config;
297 TRACE("(%p, %d)\n", device, forcewave);
299 hres = IMMDevice_Activate(device->mmdevice, &IID_IAudioClient,
300 CLSCTX_INPROC_SERVER, NULL, (void **)&client);
301 if(FAILED(hres)){
302 WARN("Activate failed: %08lx\n", hres);
303 return hres;
306 hres = DSOUND_WaveFormat(device, client, forcewave, &wfx);
307 if (FAILED(hres)) {
308 IAudioClient_Release(client);
309 return hres;
312 hres = IAudioClient_Initialize(client,
313 AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_NOPERSIST |
314 AUDCLNT_STREAMFLAGS_EVENTCALLBACK, 800000, 0, wfx, NULL);
315 if(FAILED(hres)){
316 IAudioClient_Release(client);
317 ERR("Initialize failed: %08lx\n", hres);
318 return hres;
321 hres = IAudioClient_SetEventHandle(client, device->sleepev);
322 if (FAILED(hres)) {
323 WARN("SetEventHandle failed: %08lx\n", hres);
324 goto err;
327 hres = IAudioClient_GetService(client, &IID_IAudioRenderClient, (void**)&render);
328 if(FAILED(hres))
329 goto err_service;
331 hres = IAudioClient_GetService(client, &IID_IAudioStreamVolume, (void**)&volume);
332 if(FAILED(hres))
333 goto err_service;
335 /* Now kick off the timer so the event fires periodically */
336 hres = IAudioClient_Start(client);
337 if (FAILED(hres)) {
338 WARN("Start failed with %08lx\n", hres);
339 goto err;
341 hres = IAudioClient_GetStreamLatency(client, &period);
342 if (FAILED(hres)) {
343 WARN("GetStreamLatency failed with %08lx\n", hres);
344 goto err;
346 hres = IAudioClient_GetBufferSize(client, &acbuf_frames);
347 if (FAILED(hres)) {
348 WARN("GetBufferSize failed with %08lx\n", hres);
349 goto err;
352 period_ms = (period + 9999) / 10000;
353 frag_frames = MulDiv(wfx->nSamplesPerSec, period, 10000000);
355 aclen_frames = min(acbuf_frames, 3 * frag_frames);
357 TRACE("period %lu ms frag_frames %lu buf_frames %u\n", period_ms, frag_frames, aclen_frames);
359 hres = DSOUND_PrimaryOpen(device, wfx, aclen_frames, forcewave);
360 if(FAILED(hres))
361 goto err;
363 DSOUND_ReleaseDevice(device);
364 device->client = client;
365 device->render = render;
366 device->volume = volume;
367 device->frag_frames = frag_frames;
368 device->ac_frames = aclen_frames;
370 if (period_ms < 3)
371 device->sleeptime = 5;
372 else
373 device->sleeptime = period_ms * 5 / 2;
375 return S_OK;
377 err_service:
378 WARN("GetService failed: %08lx\n", hres);
379 err:
380 device->speaker_config = oldspeakerconfig;
381 DSOUND_ParseSpeakerConfig(device);
382 if (volume)
383 IAudioStreamVolume_Release(volume);
384 if (render)
385 IAudioRenderClient_Release(render);
386 IAudioClient_Release(client);
387 HeapFree(GetProcessHeap(), 0, wfx);
388 return hres;
391 HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device)
393 TRACE("(%p)\n", device);
395 /* **** */
396 EnterCriticalSection(&(device->mixlock));
398 if(device->primary && (device->primary->ref || device->primary->numIfaces))
399 WARN("Destroying primary buffer while references held (%lu %lu)\n", device->primary->ref, device->primary->numIfaces);
401 HeapFree(GetProcessHeap(), 0, device->primary);
402 device->primary = NULL;
404 HeapFree(GetProcessHeap(),0,device->primary_pwfx);
405 HeapFree(GetProcessHeap(),0,device->pwfx);
406 device->pwfx=NULL;
408 LeaveCriticalSection(&(device->mixlock));
409 /* **** */
411 return DS_OK;
414 WAVEFORMATEX *DSOUND_CopyFormat(const WAVEFORMATEX *wfex)
416 WAVEFORMATEX *pwfx;
417 if(wfex->wFormatTag == WAVE_FORMAT_PCM){
418 pwfx = HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEX));
419 if (!pwfx)
420 return NULL;
421 CopyMemory(pwfx, wfex, sizeof(PCMWAVEFORMAT));
422 pwfx->cbSize = 0;
423 }else{
424 pwfx = HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEX) + wfex->cbSize);
425 if (!pwfx)
426 return NULL;
427 CopyMemory(pwfx, wfex, sizeof(WAVEFORMATEX) + wfex->cbSize);
430 if(pwfx->wFormatTag == WAVE_FORMAT_PCM ||
431 (pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
432 IsEqualGUID(&((const WAVEFORMATEXTENSIBLE*)pwfx)->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)))
433 pwfx->nBlockAlign = (pwfx->nChannels * pwfx->wBitsPerSample) / 8;
435 return pwfx;
438 HRESULT primarybuffer_SetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX passed_fmt)
440 HRESULT err = S_OK;
441 WAVEFORMATEX *old_fmt;
442 WAVEFORMATEXTENSIBLE *fmtex, *passed_fmtex = (WAVEFORMATEXTENSIBLE*)passed_fmt;
444 TRACE("(%p,%p)\n", device, passed_fmt);
446 if (device->priolevel == DSSCL_NORMAL) {
447 WARN("failed priority check!\n");
448 return DSERR_PRIOLEVELNEEDED;
451 /* Let's be pedantic! */
452 if (passed_fmt == NULL) {
453 WARN("invalid parameter: passed_fmt==NULL!\n");
454 return DSERR_INVALIDPARAM;
456 TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld,"
457 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
458 passed_fmt->wFormatTag, passed_fmt->nChannels, passed_fmt->nSamplesPerSec,
459 passed_fmt->nAvgBytesPerSec, passed_fmt->nBlockAlign,
460 passed_fmt->wBitsPerSample, passed_fmt->cbSize);
462 if(passed_fmt->wBitsPerSample < 8 || passed_fmt->wBitsPerSample % 8 != 0 ||
463 passed_fmt->nChannels == 0 || passed_fmt->nSamplesPerSec == 0 ||
464 passed_fmt->nAvgBytesPerSec == 0 ||
465 passed_fmt->nBlockAlign != passed_fmt->nChannels * passed_fmt->wBitsPerSample / 8)
466 return DSERR_INVALIDPARAM;
468 if(passed_fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
469 if(passed_fmtex->Samples.wValidBitsPerSample > passed_fmtex->Format.wBitsPerSample)
470 return DSERR_INVALIDPARAM;
473 /* **** */
474 AcquireSRWLockExclusive(&device->buffer_list_lock);
475 EnterCriticalSection(&(device->mixlock));
477 if (device->priolevel == DSSCL_WRITEPRIMARY) {
478 old_fmt = device->primary_pwfx;
479 device->primary_pwfx = DSOUND_CopyFormat(passed_fmt);
480 fmtex = (WAVEFORMATEXTENSIBLE *)device->primary_pwfx;
481 if (device->primary_pwfx == NULL) {
482 err = DSERR_OUTOFMEMORY;
483 goto out;
486 if (fmtex->Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
487 fmtex->Samples.wValidBitsPerSample == 0) {
488 TRACE("Correcting 0 valid bits per sample\n");
489 fmtex->Samples.wValidBitsPerSample = fmtex->Format.wBitsPerSample;
492 err = DSOUND_ReopenDevice(device, TRUE);
493 if (FAILED(err)) {
494 ERR("No formats could be opened\n");
495 HeapFree(GetProcessHeap(), 0, device->primary_pwfx);
496 device->primary_pwfx = old_fmt;
497 } else
498 HeapFree(GetProcessHeap(), 0, old_fmt);
499 } else {
500 WAVEFORMATEX *wfx = DSOUND_CopyFormat(passed_fmt);
501 if (wfx) {
502 HeapFree(GetProcessHeap(), 0, device->primary_pwfx);
503 device->primary_pwfx = wfx;
504 } else
505 err = DSERR_OUTOFMEMORY;
508 out:
509 LeaveCriticalSection(&(device->mixlock));
510 ReleaseSRWLockExclusive(&device->buffer_list_lock);
511 /* **** */
513 return err;
516 /*******************************************************************************
517 * PrimaryBuffer
519 static inline IDirectSoundBufferImpl *impl_from_IDirectSoundBuffer8(IDirectSoundBuffer8 *iface)
521 /* IDirectSoundBuffer and IDirectSoundBuffer8 use the same iface. */
522 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSoundBuffer8_iface);
525 /* This sets this format for the primary buffer only */
526 static HRESULT WINAPI PrimaryBufferImpl_SetFormat(IDirectSoundBuffer8 *iface,
527 const WAVEFORMATEX *wfex)
529 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
530 TRACE("(%p,%p)\n", iface, wfex);
531 return primarybuffer_SetFormat(This->device, wfex);
534 static HRESULT WINAPI PrimaryBufferImpl_SetVolume(IDirectSoundBuffer8 *iface, LONG vol)
536 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
537 DirectSoundDevice *device = This->device;
538 HRESULT hr;
539 float fvol;
540 int i;
542 TRACE("(%p,%ld)\n", iface, vol);
544 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
545 WARN("control unavailable\n");
546 return DSERR_CONTROLUNAVAIL;
549 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
550 WARN("invalid parameter: vol = %ld\n", vol);
551 return DSERR_INVALIDPARAM;
554 /* **** */
555 EnterCriticalSection(&device->mixlock);
557 for (i = 0; i < DS_MAX_CHANNELS; i++) {
558 if (device->pwfx->nChannels > i){
559 hr = IAudioStreamVolume_GetChannelVolume(device->volume, i, &fvol);
560 if (FAILED(hr)){
561 LeaveCriticalSection(&device->mixlock);
562 WARN("GetChannelVolume failed: %08lx\n", hr);
563 return hr;
565 } else
566 fvol=1.0f;
568 device->volpan.dwTotalAmpFactor[i]=((UINT16)(fvol * (DWORD)0xFFFF));
571 DSOUND_AmpFactorToVolPan(&device->volpan);
572 if (vol != device->volpan.lVolume) {
573 device->volpan.lVolume=vol;
574 DSOUND_RecalcVolPan(&device->volpan);
576 for (i = 0; i < DS_MAX_CHANNELS; i++) {
577 if (device->pwfx->nChannels > i){
578 fvol = (float)((device->volpan.dwTotalAmpFactor[i] & 0xFFFF) / (float)0xFFFF);
579 hr = IAudioStreamVolume_SetChannelVolume(device->volume, i, fvol);
580 if (FAILED(hr)){
581 LeaveCriticalSection(&device->mixlock);
582 WARN("SetChannelVolume failed: %08lx\n", hr);
583 return hr;
589 LeaveCriticalSection(&(device->mixlock));
590 /* **** */
592 return DS_OK;
595 static HRESULT WINAPI PrimaryBufferImpl_GetVolume(IDirectSoundBuffer8 *iface, LONG *vol)
597 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
598 DirectSoundDevice *device = This->device;
599 float fvol;
600 HRESULT hr;
601 int i;
603 TRACE("(%p,%p)\n", iface, vol);
605 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
606 WARN("control unavailable\n");
607 return DSERR_CONTROLUNAVAIL;
610 if (vol == NULL) {
611 WARN("invalid parameter: vol = NULL\n");
612 return DSERR_INVALIDPARAM;
615 EnterCriticalSection(&device->mixlock);
617 for (i = 0; i < DS_MAX_CHANNELS; i++) {
618 if (device->pwfx->nChannels > i){
619 hr = IAudioStreamVolume_GetChannelVolume(device->volume, i, &fvol);
620 if (FAILED(hr)){
621 LeaveCriticalSection(&device->mixlock);
622 WARN("GetChannelVolume failed: %08lx\n", hr);
623 return hr;
625 } else
626 fvol = 1;
628 device->volpan.dwTotalAmpFactor[i] = ((UINT16)(fvol * (DWORD)0xFFFF));
631 DSOUND_AmpFactorToVolPan(&device->volpan);
632 *vol = device->volpan.lVolume;
634 LeaveCriticalSection(&device->mixlock);
636 return DS_OK;
639 static HRESULT WINAPI PrimaryBufferImpl_SetFrequency(IDirectSoundBuffer8 *iface, DWORD freq)
641 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
642 TRACE("(%p,%ld)\n",This,freq);
644 /* You cannot set the frequency of the primary buffer */
645 WARN("control unavailable\n");
646 return DSERR_CONTROLUNAVAIL;
649 static HRESULT WINAPI PrimaryBufferImpl_Play(IDirectSoundBuffer8 *iface, DWORD reserved1,
650 DWORD reserved2, DWORD flags)
652 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
653 DirectSoundDevice *device = This->device;
654 TRACE("(%p,%08lx,%08lx,%08lx)\n", iface, reserved1, reserved2, flags);
656 if (!(flags & DSBPLAY_LOOPING)) {
657 WARN("invalid parameter: flags = %08lx\n", flags);
658 return DSERR_INVALIDPARAM;
661 device->stopped = 0;
663 return DS_OK;
666 static HRESULT WINAPI PrimaryBufferImpl_Stop(IDirectSoundBuffer8 *iface)
668 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
669 DirectSoundDevice *device = This->device;
670 TRACE("(%p)\n", iface);
672 device->stopped = 1;
674 return DS_OK;
677 static ULONG WINAPI PrimaryBufferImpl_AddRef(IDirectSoundBuffer8 *iface)
679 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
680 ULONG ref = InterlockedIncrement(&(This->ref));
681 TRACE("(%p) ref %ld\n", This, ref);
682 if(ref == 1)
683 InterlockedIncrement(&This->numIfaces);
684 return ref;
687 /* Decreases *out by 1 to no less than 0.
688 * Returns the new value of *out. */
689 LONG capped_refcount_dec(LONG *out)
691 LONG ref, oldref;
692 do {
693 ref = *out;
694 if(!ref)
695 return 0;
696 oldref = InterlockedCompareExchange(out, ref - 1, ref);
697 } while(oldref != ref);
698 return ref - 1;
701 static ULONG WINAPI PrimaryBufferImpl_Release(IDirectSoundBuffer8 *iface)
703 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
704 ULONG ref;
706 ref = capped_refcount_dec(&This->ref);
707 if(!ref)
708 capped_refcount_dec(&This->numIfaces);
710 TRACE("(%p) primary ref %ld\n", This, ref);
712 return ref;
715 static HRESULT WINAPI PrimaryBufferImpl_GetCurrentPosition(IDirectSoundBuffer8 *iface,
716 DWORD *playpos, DWORD *writepos)
718 HRESULT hres = DS_OK;
719 UINT32 pad = 0;
720 UINT32 mixpos;
721 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
722 DirectSoundDevice *device = This->device;
723 TRACE("(%p,%p,%p)\n", iface, playpos, writepos);
725 /* **** */
726 EnterCriticalSection(&(device->mixlock));
728 if (device->client)
729 hres = IAudioClient_GetCurrentPadding(device->client, &pad);
730 if (hres != DS_OK) {
731 WARN("IAudioClient_GetCurrentPadding failed\n");
732 LeaveCriticalSection(&(device->mixlock));
733 return hres;
735 mixpos = (device->playpos + pad * device->pwfx->nBlockAlign) % device->buflen;
736 if (playpos)
737 *playpos = mixpos;
738 if (writepos) {
739 *writepos = mixpos;
740 if (!device->stopped) {
741 /* apply the documented 10ms lead to writepos */
742 *writepos += device->writelead;
743 *writepos %= device->buflen;
747 LeaveCriticalSection(&(device->mixlock));
748 /* **** */
750 TRACE("playpos = %ld, writepos = %ld (%p, time=%ld)\n", playpos?*playpos:0, writepos?*writepos:0, device, GetTickCount());
751 return DS_OK;
754 static HRESULT WINAPI PrimaryBufferImpl_GetStatus(IDirectSoundBuffer8 *iface, DWORD *status)
756 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
757 DirectSoundDevice *device = This->device;
758 TRACE("(%p,%p)\n", iface, status);
760 if (status == NULL) {
761 WARN("invalid parameter: status == NULL\n");
762 return DSERR_INVALIDPARAM;
765 *status = 0;
766 if (!device->stopped)
767 *status |= DSBSTATUS_PLAYING | DSBSTATUS_LOOPING;
769 TRACE("status=%lx\n", *status);
770 return DS_OK;
774 static HRESULT WINAPI PrimaryBufferImpl_GetFormat(IDirectSoundBuffer8 *iface, WAVEFORMATEX *lpwf,
775 DWORD wfsize, DWORD *wfwritten)
777 DWORD size;
778 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
779 DirectSoundDevice *device = This->device;
780 TRACE("(%p,%p,%ld,%p)\n", iface, lpwf, wfsize, wfwritten);
782 size = sizeof(WAVEFORMATEX) + device->primary_pwfx->cbSize;
784 if (lpwf) { /* NULL is valid */
785 if (wfsize >= size) {
786 CopyMemory(lpwf,device->primary_pwfx,size);
787 if (wfwritten)
788 *wfwritten = size;
789 } else {
790 WARN("invalid parameter: wfsize too small\n");
791 if (wfwritten)
792 *wfwritten = 0;
793 return DSERR_INVALIDPARAM;
795 } else {
796 if (wfwritten)
797 *wfwritten = sizeof(WAVEFORMATEX) + device->primary_pwfx->cbSize;
798 else {
799 WARN("invalid parameter: wfwritten == NULL\n");
800 return DSERR_INVALIDPARAM;
804 return DS_OK;
807 static HRESULT WINAPI PrimaryBufferImpl_Lock(IDirectSoundBuffer8 *iface, DWORD writecursor,
808 DWORD writebytes, void **lplpaudioptr1, DWORD *audiobytes1, void **lplpaudioptr2,
809 DWORD *audiobytes2, DWORD flags)
811 HRESULT hres;
812 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
813 DirectSoundDevice *device = This->device;
814 TRACE("(%p,%ld,%ld,%p,%p,%p,%p,0x%08lx) at %ld\n",
815 iface,
816 writecursor,
817 writebytes,
818 lplpaudioptr1,
819 audiobytes1,
820 lplpaudioptr2,
821 audiobytes2,
822 flags,
823 GetTickCount()
826 if (!audiobytes1)
827 return DSERR_INVALIDPARAM;
829 if (device->priolevel != DSSCL_WRITEPRIMARY) {
830 WARN("failed priority check!\n");
831 return DSERR_PRIOLEVELNEEDED;
834 /* when this flag is set, writecursor is meaningless and must be calculated */
835 if (flags & DSBLOCK_FROMWRITECURSOR) {
836 /* GetCurrentPosition does too much magic to duplicate here */
837 hres = IDirectSoundBuffer_GetCurrentPosition(iface, NULL, &writecursor);
838 if (hres != DS_OK) {
839 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
840 return hres;
844 /* when this flag is set, writebytes is meaningless and must be set */
845 if (flags & DSBLOCK_ENTIREBUFFER)
846 writebytes = device->buflen;
848 if (writecursor >= device->buflen) {
849 WARN("Invalid parameter, writecursor: %lu >= buflen: %lu\n",
850 writecursor, device->buflen);
851 return DSERR_INVALIDPARAM;
854 if (writebytes > device->buflen) {
855 WARN("Invalid parameter, writebytes: %lu > buflen: %lu\n",
856 writebytes, device->buflen);
857 return DSERR_INVALIDPARAM;
860 if (writecursor+writebytes <= device->buflen) {
861 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
862 *audiobytes1 = writebytes;
863 if (lplpaudioptr2)
864 *(LPBYTE*)lplpaudioptr2 = NULL;
865 if (audiobytes2)
866 *audiobytes2 = 0;
867 TRACE("->%ld.0\n",writebytes);
868 } else {
869 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
870 *audiobytes1 = device->buflen-writecursor;
871 if (lplpaudioptr2)
872 *(LPBYTE*)lplpaudioptr2 = device->buffer;
873 if (audiobytes2)
874 *audiobytes2 = writebytes-(device->buflen-writecursor);
875 TRACE("->%ld.%ld\n",*audiobytes1,audiobytes2?*audiobytes2:0);
877 return DS_OK;
880 static HRESULT WINAPI PrimaryBufferImpl_SetCurrentPosition(IDirectSoundBuffer8 *iface, DWORD newpos)
882 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
883 TRACE("(%p,%ld)\n",This,newpos);
885 /* You cannot set the position of the primary buffer */
886 WARN("invalid call\n");
887 return DSERR_INVALIDCALL;
890 static HRESULT WINAPI PrimaryBufferImpl_SetPan(IDirectSoundBuffer8 *iface, LONG pan)
892 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
893 DirectSoundDevice *device = This->device;
894 float fvol;
895 HRESULT hr;
896 int i;
898 TRACE("(%p,%ld)\n", iface, pan);
900 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
901 WARN("control unavailable\n");
902 return DSERR_CONTROLUNAVAIL;
905 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
906 WARN("invalid parameter: pan = %ld\n", pan);
907 return DSERR_INVALIDPARAM;
910 /* **** */
911 EnterCriticalSection(&device->mixlock);
913 for (i = 0; i < DS_MAX_CHANNELS; i++) {
914 if (device->pwfx->nChannels > i){
915 hr = IAudioStreamVolume_GetChannelVolume(device->volume, i, &fvol);
916 if (FAILED(hr)){
917 LeaveCriticalSection(&device->mixlock);
918 WARN("GetChannelVolume failed: %08lx\n", hr);
919 return hr;
921 } else
922 fvol = 1;
924 device->volpan.dwTotalAmpFactor[i] = ((UINT16)(fvol * (DWORD)0xFFFF));
927 DSOUND_AmpFactorToVolPan(&device->volpan);
928 if (pan != device->volpan.lPan) {
929 device->volpan.lPan=pan;
930 DSOUND_RecalcVolPan(&device->volpan);
932 for (i = 0; i < DS_MAX_CHANNELS; i++) {
933 if (device->pwfx->nChannels > i) {
934 fvol = (float)((device->volpan.dwTotalAmpFactor[i] & 0xFFFF) / (float)0xFFFF);
935 hr = IAudioStreamVolume_SetChannelVolume(device->volume, i, fvol);
936 if (FAILED(hr)){
937 LeaveCriticalSection(&device->mixlock);
938 WARN("SetChannelVolume failed: %08lx\n", hr);
939 return hr;
945 LeaveCriticalSection(&device->mixlock);
946 /* **** */
948 return DS_OK;
951 static HRESULT WINAPI PrimaryBufferImpl_GetPan(IDirectSoundBuffer8 *iface, LONG *pan)
953 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
954 DirectSoundDevice *device = This->device;
955 float fvol;
956 HRESULT hr;
957 int i;
959 TRACE("(%p,%p)\n", iface, pan);
961 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
962 WARN("control unavailable\n");
963 return DSERR_CONTROLUNAVAIL;
966 if (pan == NULL) {
967 WARN("invalid parameter: pan == NULL\n");
968 return DSERR_INVALIDPARAM;
971 EnterCriticalSection(&device->mixlock);
973 for (i = 0; i < DS_MAX_CHANNELS; i++) {
974 if (device->pwfx->nChannels > i) {
975 hr = IAudioStreamVolume_GetChannelVolume(device->volume, i, &fvol);
976 if (FAILED(hr)){
977 LeaveCriticalSection(&device->mixlock);
978 WARN("GetChannelVolume failed: %08lx\n", hr);
979 return hr;
981 } else
982 fvol = 1;
984 device->volpan.dwTotalAmpFactor[i] = ((UINT16)(fvol * (DWORD)0xFFFF));
987 DSOUND_AmpFactorToVolPan(&device->volpan);
988 *pan = device->volpan.lPan;
990 LeaveCriticalSection(&device->mixlock);
992 return DS_OK;
995 static HRESULT WINAPI PrimaryBufferImpl_Unlock(IDirectSoundBuffer8 *iface, void *p1, DWORD x1,
996 void *p2, DWORD x2)
998 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
999 DirectSoundDevice *device = This->device;
1000 TRACE("(%p,%p,%ld,%p,%ld)\n", iface, p1, x1, p2, x2);
1002 if (device->priolevel != DSSCL_WRITEPRIMARY) {
1003 WARN("failed priority check!\n");
1004 return DSERR_PRIOLEVELNEEDED;
1007 if ((p1 && ((BYTE*)p1 < device->buffer || (BYTE*)p1 >= device->buffer + device->buflen)) ||
1008 (p2 && ((BYTE*)p2 < device->buffer || (BYTE*)p2 >= device->buffer + device->buflen)))
1009 return DSERR_INVALIDPARAM;
1011 return DS_OK;
1014 static HRESULT WINAPI PrimaryBufferImpl_Restore(IDirectSoundBuffer8 *iface)
1016 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
1017 FIXME("(%p):stub\n",This);
1018 return DS_OK;
1021 static HRESULT WINAPI PrimaryBufferImpl_GetFrequency(IDirectSoundBuffer8 *iface, DWORD *freq)
1023 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
1024 DirectSoundDevice *device = This->device;
1025 TRACE("(%p,%p)\n", iface, freq);
1027 if (freq == NULL) {
1028 WARN("invalid parameter: freq == NULL\n");
1029 return DSERR_INVALIDPARAM;
1032 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
1033 WARN("control unavailable\n");
1034 return DSERR_CONTROLUNAVAIL;
1037 *freq = device->pwfx->nSamplesPerSec;
1038 TRACE("-> %ld\n", *freq);
1040 return DS_OK;
1043 static HRESULT WINAPI PrimaryBufferImpl_Initialize(IDirectSoundBuffer8 *iface, IDirectSound *dsound,
1044 const DSBUFFERDESC *dbsd)
1046 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
1047 WARN("(%p) already initialized\n", This);
1048 return DSERR_ALREADYINITIALIZED;
1051 static HRESULT WINAPI PrimaryBufferImpl_GetCaps(IDirectSoundBuffer8 *iface, DSBCAPS *caps)
1053 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
1054 DirectSoundDevice *device = This->device;
1055 TRACE("(%p,%p)\n", iface, caps);
1057 if (caps == NULL) {
1058 WARN("invalid parameter: caps == NULL\n");
1059 return DSERR_INVALIDPARAM;
1062 if (caps->dwSize < sizeof(*caps)) {
1063 WARN("invalid parameter: caps->dwSize = %ld\n", caps->dwSize);
1064 return DSERR_INVALIDPARAM;
1067 caps->dwFlags = This->dsbd.dwFlags;
1068 caps->dwBufferBytes = device->buflen;
1070 /* Windows reports these as zero */
1071 caps->dwUnlockTransferRate = 0;
1072 caps->dwPlayCpuOverhead = 0;
1074 return DS_OK;
1077 static HRESULT WINAPI PrimaryBufferImpl_QueryInterface(IDirectSoundBuffer8 *iface, REFIID riid,
1078 void **ppobj)
1080 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
1082 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppobj);
1084 if (ppobj == NULL) {
1085 WARN("invalid parameter\n");
1086 return E_INVALIDARG;
1089 *ppobj = NULL; /* assume failure */
1091 if ( IsEqualGUID(riid, &IID_IUnknown) ||
1092 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ) {
1093 IDirectSoundBuffer_AddRef(iface);
1094 *ppobj = iface;
1095 return S_OK;
1098 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
1099 /* a primary buffer can't have a DirectSoundBuffer8 interface */
1100 if ( IsEqualGUID( &IID_IDirectSoundBuffer8, riid ) ) {
1101 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
1102 return E_NOINTERFACE;
1105 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
1106 ERR("app requested IDirectSoundNotify on primary buffer\n");
1107 return E_NOINTERFACE;
1110 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
1111 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1112 return E_NOINTERFACE;
1115 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
1116 *ppobj = &This->IDirectSound3DListener_iface;
1117 IDirectSound3DListener_AddRef(&This->IDirectSound3DListener_iface);
1118 return S_OK;
1121 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
1122 *ppobj = &This->IKsPropertySet_iface;
1123 IKsPropertySet_AddRef(&This->IKsPropertySet_iface);
1124 return S_OK;
1127 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
1128 return E_NOINTERFACE;
1131 static const IDirectSoundBuffer8Vtbl dspbvt =
1133 PrimaryBufferImpl_QueryInterface,
1134 PrimaryBufferImpl_AddRef,
1135 PrimaryBufferImpl_Release,
1136 PrimaryBufferImpl_GetCaps,
1137 PrimaryBufferImpl_GetCurrentPosition,
1138 PrimaryBufferImpl_GetFormat,
1139 PrimaryBufferImpl_GetVolume,
1140 PrimaryBufferImpl_GetPan,
1141 PrimaryBufferImpl_GetFrequency,
1142 PrimaryBufferImpl_GetStatus,
1143 PrimaryBufferImpl_Initialize,
1144 PrimaryBufferImpl_Lock,
1145 PrimaryBufferImpl_Play,
1146 PrimaryBufferImpl_SetCurrentPosition,
1147 PrimaryBufferImpl_SetFormat,
1148 PrimaryBufferImpl_SetVolume,
1149 PrimaryBufferImpl_SetPan,
1150 PrimaryBufferImpl_SetFrequency,
1151 PrimaryBufferImpl_Stop,
1152 PrimaryBufferImpl_Unlock,
1153 PrimaryBufferImpl_Restore
1156 HRESULT primarybuffer_create(DirectSoundDevice *device, IDirectSoundBufferImpl **ppdsb,
1157 const DSBUFFERDESC *dsbd)
1159 IDirectSoundBufferImpl *dsb;
1160 TRACE("%p,%p,%p)\n",device,ppdsb,dsbd);
1162 if (dsbd->lpwfxFormat) {
1163 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1164 *ppdsb = NULL;
1165 return DSERR_INVALIDPARAM;
1168 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1170 if (dsb == NULL) {
1171 WARN("out of memory\n");
1172 *ppdsb = NULL;
1173 return DSERR_OUTOFMEMORY;
1176 dsb->ref = 0;
1177 dsb->ref3D = 0;
1178 dsb->refiks = 0;
1179 dsb->numIfaces = 0;
1180 dsb->device = device;
1181 dsb->IDirectSoundBuffer8_iface.lpVtbl = &dspbvt;
1182 dsb->IDirectSound3DListener_iface.lpVtbl = &ds3dlvt;
1183 dsb->IKsPropertySet_iface.lpVtbl = &iksbvt;
1184 dsb->dsbd = *dsbd;
1186 /* IDirectSound3DListener */
1187 device->ds3dl.dwSize = sizeof(DS3DLISTENER);
1188 device->ds3dl.vPosition.x = 0.0;
1189 device->ds3dl.vPosition.y = 0.0;
1190 device->ds3dl.vPosition.z = 0.0;
1191 device->ds3dl.vVelocity.x = 0.0;
1192 device->ds3dl.vVelocity.y = 0.0;
1193 device->ds3dl.vVelocity.z = 0.0;
1194 device->ds3dl.vOrientFront.x = 0.0;
1195 device->ds3dl.vOrientFront.y = 0.0;
1196 device->ds3dl.vOrientFront.z = 1.0;
1197 device->ds3dl.vOrientTop.x = 0.0;
1198 device->ds3dl.vOrientTop.y = 1.0;
1199 device->ds3dl.vOrientTop.z = 0.0;
1200 device->ds3dl.flDistanceFactor = DS3D_DEFAULTDISTANCEFACTOR;
1201 device->ds3dl.flRolloffFactor = DS3D_DEFAULTROLLOFFFACTOR;
1202 device->ds3dl.flDopplerFactor = DS3D_DEFAULTDOPPLERFACTOR;
1203 device->ds3dl_need_recalc = TRUE;
1205 TRACE("Created primary buffer at %p\n", dsb);
1206 TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld,"
1207 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1208 device->pwfx->wFormatTag, device->pwfx->nChannels,
1209 device->pwfx->nSamplesPerSec, device->pwfx->nAvgBytesPerSec,
1210 device->pwfx->nBlockAlign, device->pwfx->wBitsPerSample,
1211 device->pwfx->cbSize);
1213 IDirectSoundBuffer8_AddRef(&dsb->IDirectSoundBuffer8_iface);
1214 *ppdsb = dsb;
1215 return S_OK;