msvcirt: Add implementation of streambuf::sputbackc.
[wine.git] / dlls / dsound / primary.c
blob3f8a4781e60ace6e98ad8332ef235086cde374a6
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 "winternl.h"
36 #include "mmddk.h"
37 #include "wine/debug.h"
38 #include "dsound.h"
39 #include "dsound_private.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
43 static DWORD DSOUND_fraglen(DirectSoundDevice *device)
45 REFERENCE_TIME period;
46 HRESULT hr;
47 DWORD ret;
49 hr = IAudioClient_GetDevicePeriod(device->client, &period, NULL);
50 if(FAILED(hr)){
51 /* just guess at 10ms */
52 WARN("GetDevicePeriod failed: %08x\n", hr);
53 ret = MulDiv(device->pwfx->nBlockAlign, device->pwfx->nSamplesPerSec, 100);
54 }else
55 ret = MulDiv(device->pwfx->nSamplesPerSec * device->pwfx->nBlockAlign, period, 10000000);
57 ret -= ret % device->pwfx->nBlockAlign;
58 return ret;
61 static DWORD speaker_config_to_channel_mask(DWORD speaker_config)
63 switch (DSSPEAKER_CONFIG(speaker_config)) {
64 case DSSPEAKER_MONO:
65 return SPEAKER_FRONT_LEFT;
67 case DSSPEAKER_STEREO:
68 case DSSPEAKER_HEADPHONE:
69 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
71 case DSSPEAKER_QUAD:
72 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT;
74 case DSSPEAKER_5POINT1_BACK:
75 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_FRONT_CENTER | SPEAKER_LOW_FREQUENCY | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT;
78 WARN("unknown speaker_config %u\n", speaker_config);
79 return SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
82 static DWORD DSOUND_FindSpeakerConfig(IMMDevice *mmdevice, int channels)
84 IPropertyStore *store;
85 HRESULT hr;
86 PROPVARIANT pv;
87 ULONG phys_speakers;
89 const DWORD def = DSSPEAKER_COMBINED(DSSPEAKER_STEREO, DSSPEAKER_GEOMETRY_WIDE);
91 hr = IMMDevice_OpenPropertyStore(mmdevice, STGM_READ, &store);
92 if (FAILED(hr)) {
93 WARN("IMMDevice_OpenPropertyStore failed: %08x\n", hr);
94 return def;
97 hr = IPropertyStore_GetValue(store, &PKEY_AudioEndpoint_PhysicalSpeakers, &pv);
99 if (FAILED(hr)) {
100 WARN("IPropertyStore_GetValue failed: %08x\n", hr);
101 IPropertyStore_Release(store);
102 return def;
105 if (pv.vt != VT_UI4) {
106 WARN("PKEY_AudioEndpoint_PhysicalSpeakers is not a ULONG: 0x%x\n", pv.vt);
107 PropVariantClear(&pv);
108 IPropertyStore_Release(store);
109 return def;
112 phys_speakers = pv.u.ulVal;
114 PropVariantClear(&pv);
115 IPropertyStore_Release(store);
117 if ((channels >= 6 || channels == 0) && (phys_speakers & KSAUDIO_SPEAKER_5POINT1) == KSAUDIO_SPEAKER_5POINT1)
118 return DSSPEAKER_5POINT1_BACK;
119 else if ((channels >= 6 || channels == 0) && (phys_speakers & KSAUDIO_SPEAKER_5POINT1_SURROUND) == KSAUDIO_SPEAKER_5POINT1_SURROUND)
120 return DSSPEAKER_5POINT1_SURROUND;
121 else if ((channels >= 4 || channels == 0) && (phys_speakers & KSAUDIO_SPEAKER_QUAD) == KSAUDIO_SPEAKER_QUAD)
122 return DSSPEAKER_QUAD;
123 else if ((channels >= 2 || channels == 0) && (phys_speakers & KSAUDIO_SPEAKER_STEREO) == KSAUDIO_SPEAKER_STEREO)
124 return DSSPEAKER_COMBINED(DSSPEAKER_STEREO, DSSPEAKER_GEOMETRY_WIDE);
125 else if ((phys_speakers & KSAUDIO_SPEAKER_MONO) == KSAUDIO_SPEAKER_MONO)
126 return DSSPEAKER_MONO;
128 return def;
131 static HRESULT DSOUND_WaveFormat(DirectSoundDevice *device, IAudioClient *client,
132 BOOL forcewave, WAVEFORMATEX **wfx)
134 WAVEFORMATEXTENSIBLE *retwfe = NULL;
135 WAVEFORMATEX *w;
136 HRESULT hr;
138 if (!forcewave) {
139 WAVEFORMATEXTENSIBLE *mixwfe;
140 hr = IAudioClient_GetMixFormat(client, (WAVEFORMATEX**)&mixwfe);
142 if (FAILED(hr))
143 return hr;
145 if (mixwfe->Format.nChannels < device->num_speakers) {
146 device->speaker_config = DSOUND_FindSpeakerConfig(device->mmdevice, mixwfe->Format.nChannels);
147 DSOUND_ParseSpeakerConfig(device);
148 } else if (mixwfe->Format.nChannels > device->num_speakers) {
149 mixwfe->Format.nChannels = device->num_speakers;
150 mixwfe->Format.nBlockAlign = mixwfe->Format.nChannels * mixwfe->Format.wBitsPerSample / 8;
151 mixwfe->Format.nAvgBytesPerSec = mixwfe->Format.nSamplesPerSec * mixwfe->Format.nBlockAlign;
152 mixwfe->dwChannelMask = speaker_config_to_channel_mask(device->speaker_config);
155 if (!IsEqualGUID(&mixwfe->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)) {
156 WAVEFORMATEXTENSIBLE testwfe = *mixwfe;
158 testwfe.SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
159 testwfe.Samples.wValidBitsPerSample = testwfe.Format.wBitsPerSample = 32;
160 testwfe.Format.nBlockAlign = testwfe.Format.nChannels * testwfe.Format.wBitsPerSample / 8;
161 testwfe.Format.nAvgBytesPerSec = testwfe.Format.nSamplesPerSec * testwfe.Format.nBlockAlign;
163 if (FAILED(IAudioClient_IsFormatSupported(client, AUDCLNT_SHAREMODE_SHARED, &testwfe.Format, (WAVEFORMATEX**)&retwfe)))
164 w = DSOUND_CopyFormat(&mixwfe->Format);
165 else if (retwfe)
166 w = DSOUND_CopyFormat(&retwfe->Format);
167 else
168 w = DSOUND_CopyFormat(&testwfe.Format);
169 CoTaskMemFree(retwfe);
170 retwfe = NULL;
171 } else
172 w = DSOUND_CopyFormat(&mixwfe->Format);
173 CoTaskMemFree(mixwfe);
174 } else if (device->primary_pwfx->wFormatTag == WAVE_FORMAT_PCM ||
175 device->primary_pwfx->wFormatTag == WAVE_FORMAT_IEEE_FLOAT) {
176 WAVEFORMATEX *wi = device->primary_pwfx;
177 WAVEFORMATEXTENSIBLE *wfe;
179 /* Convert to WAVEFORMATEXTENSIBLE */
180 w = HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEXTENSIBLE));
181 wfe = (WAVEFORMATEXTENSIBLE*)w;
182 if (!wfe)
183 return DSERR_OUTOFMEMORY;
185 wfe->Format = *wi;
186 w->wFormatTag = WAVE_FORMAT_EXTENSIBLE;
187 w->cbSize = sizeof(*wfe) - sizeof(*w);
188 w->nBlockAlign = w->nChannels * w->wBitsPerSample / 8;
189 w->nAvgBytesPerSec = w->nSamplesPerSec * w->nBlockAlign;
191 wfe->dwChannelMask = 0;
192 if (wi->wFormatTag == WAVE_FORMAT_IEEE_FLOAT) {
193 w->wBitsPerSample = 32;
194 wfe->SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
195 } else
196 wfe->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
197 wfe->Samples.wValidBitsPerSample = w->wBitsPerSample;
198 } else
199 w = DSOUND_CopyFormat(device->primary_pwfx);
201 if (!w)
202 return DSERR_OUTOFMEMORY;
204 hr = IAudioClient_IsFormatSupported(client, AUDCLNT_SHAREMODE_SHARED, w, (WAVEFORMATEX**)&retwfe);
205 if (retwfe) {
206 memcpy(w, retwfe, sizeof(WAVEFORMATEX) + retwfe->Format.cbSize);
207 CoTaskMemFree(retwfe);
209 if (FAILED(hr)) {
210 WARN("IsFormatSupported failed: %08x\n", hr);
211 HeapFree(GetProcessHeap(), 0, w);
212 return hr;
214 *wfx = w;
215 return S_OK;
218 HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave)
220 UINT prebuf_frames;
221 REFERENCE_TIME prebuf_rt;
222 WAVEFORMATEX *wfx = NULL;
223 HRESULT hres;
224 REFERENCE_TIME period;
225 DWORD period_ms;
227 TRACE("(%p, %d)\n", device, forcewave);
229 if(device->client){
230 IAudioClient_Release(device->client);
231 device->client = NULL;
233 if(device->render){
234 IAudioRenderClient_Release(device->render);
235 device->render = NULL;
237 if(device->clock){
238 IAudioClock_Release(device->clock);
239 device->clock = NULL;
241 if(device->volume){
242 IAudioStreamVolume_Release(device->volume);
243 device->volume = NULL;
246 hres = IMMDevice_Activate(device->mmdevice, &IID_IAudioClient,
247 CLSCTX_INPROC_SERVER, NULL, (void **)&device->client);
248 if(FAILED(hres)) {
249 WARN("Activate failed: %08x\n", hres);
250 return hres;
253 device->speaker_config = DSOUND_FindSpeakerConfig(device->mmdevice, 0);
255 DSOUND_ParseSpeakerConfig(device);
257 hres = DSOUND_WaveFormat(device, device->client, forcewave, &wfx);
258 if (FAILED(hres)) {
259 IAudioClient_Release(device->client);
260 device->client = NULL;
261 return hres;
263 HeapFree(GetProcessHeap(), 0, device->pwfx);
264 device->pwfx = wfx;
266 prebuf_frames = device->prebuf * DSOUND_fraglen(device) / device->pwfx->nBlockAlign;
267 prebuf_rt = (10000000 * (UINT64)prebuf_frames) / device->pwfx->nSamplesPerSec;
269 hres = IAudioClient_Initialize(device->client,
270 AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_NOPERSIST |
271 AUDCLNT_STREAMFLAGS_EVENTCALLBACK, prebuf_rt, 0, device->pwfx, NULL);
272 if(FAILED(hres)){
273 IAudioClient_Release(device->client);
274 device->client = NULL;
275 WARN("Initialize failed: %08x\n", hres);
276 return hres;
278 IAudioClient_SetEventHandle(device->client, device->sleepev);
280 hres = IAudioClient_GetService(device->client, &IID_IAudioRenderClient,
281 (void**)&device->render);
282 if(FAILED(hres)){
283 IAudioClient_Release(device->client);
284 device->client = NULL;
285 WARN("GetService failed: %08x\n", hres);
286 return hres;
289 hres = IAudioClient_GetService(device->client, &IID_IAudioClock,
290 (void**)&device->clock);
291 if(FAILED(hres)){
292 IAudioClient_Release(device->client);
293 IAudioRenderClient_Release(device->render);
294 device->client = NULL;
295 device->render = NULL;
296 WARN("GetService failed: %08x\n", hres);
297 return hres;
300 hres = IAudioClient_GetService(device->client, &IID_IAudioStreamVolume,
301 (void**)&device->volume);
302 if(FAILED(hres)){
303 IAudioClient_Release(device->client);
304 IAudioRenderClient_Release(device->render);
305 IAudioClock_Release(device->clock);
306 device->client = NULL;
307 device->render = NULL;
308 device->clock = NULL;
309 WARN("GetService failed: %08x\n", hres);
310 return hres;
313 /* Now kick off the timer so the event fires periodically */
314 hres = IAudioClient_Start(device->client);
315 if (FAILED(hres))
316 WARN("starting failed with %08x\n", hres);
318 hres = IAudioClient_GetStreamLatency(device->client, &period);
319 if (FAILED(hres)) {
320 WARN("GetStreamLatency failed with %08x\n", hres);
321 period_ms = 10;
322 } else
323 period_ms = (period + 9999) / 10000;
324 TRACE("period %u ms fraglen %u prebuf %u\n", period_ms, device->fraglen, device->prebuf);
326 if (period_ms < 3)
327 device->sleeptime = 5;
328 else
329 device->sleeptime = period_ms * 5 / 2;
331 return S_OK;
334 HRESULT DSOUND_PrimaryOpen(DirectSoundDevice *device)
336 IDirectSoundBufferImpl** dsb = device->buffers;
337 LPBYTE newbuf;
338 int i;
340 TRACE("(%p)\n", device);
342 device->fraglen = DSOUND_fraglen(device);
344 /* on original windows, the buffer it set to a fixed size, no matter what the settings are.
345 on windows this size is always fixed (tested on win-xp) */
346 if (!device->buflen)
347 device->buflen = ds_hel_buflen;
348 device->buflen -= device->buflen % device->pwfx->nBlockAlign;
349 while(device->buflen < device->fraglen * device->prebuf){
350 device->buflen += ds_hel_buflen;
351 device->buflen -= device->buflen % device->pwfx->nBlockAlign;
354 HeapFree(GetProcessHeap(), 0, device->mix_buffer);
355 device->mix_buffer_len = (device->buflen / (device->pwfx->wBitsPerSample / 8)) * sizeof(float);
356 device->mix_buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, device->mix_buffer_len);
357 if (!device->mix_buffer)
358 return DSERR_OUTOFMEMORY;
360 if (device->state == STATE_PLAYING) device->state = STATE_STARTING;
361 else if (device->state == STATE_STOPPING) device->state = STATE_STOPPED;
363 /* reallocate emulated primary buffer */
364 if (device->buffer)
365 newbuf = HeapReAlloc(GetProcessHeap(),0,device->buffer, device->buflen);
366 else
367 newbuf = HeapAlloc(GetProcessHeap(),0, device->buflen);
369 if (!newbuf) {
370 ERR("failed to allocate primary buffer\n");
371 return DSERR_OUTOFMEMORY;
372 /* but the old buffer might still exist and must be re-prepared */
375 device->writelead = (device->pwfx->nSamplesPerSec / 100) * device->pwfx->nBlockAlign;
377 device->buffer = newbuf;
379 TRACE("buflen: %u, fraglen: %u, mix_buffer_len: %u\n",
380 device->buflen, device->fraglen, device->mix_buffer_len);
382 if(device->pwfx->wFormatTag == WAVE_FORMAT_IEEE_FLOAT ||
383 (device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
384 IsEqualGUID(&((WAVEFORMATEXTENSIBLE*)device->pwfx)->SubFormat,
385 &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)))
386 device->normfunction = normfunctions[4];
387 else
388 device->normfunction = normfunctions[device->pwfx->wBitsPerSample/8 - 1];
390 FillMemory(device->buffer, device->buflen, (device->pwfx->wBitsPerSample == 8) ? 128 : 0);
391 FillMemory(device->mix_buffer, device->mix_buffer_len, 0);
392 device->playpos = 0;
394 if (device->pwfx->wFormatTag == WAVE_FORMAT_IEEE_FLOAT ||
395 (device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
396 IsEqualGUID(&((WAVEFORMATEXTENSIBLE*)device->pwfx)->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)))
397 device->normfunction = normfunctions[4];
398 else
399 device->normfunction = normfunctions[device->pwfx->wBitsPerSample/8 - 1];
401 for (i = 0; i < device->nrofbuffers; i++) {
402 RtlAcquireResourceExclusive(&dsb[i]->lock, TRUE);
403 DSOUND_RecalcFormat(dsb[i]);
404 RtlReleaseResource(&dsb[i]->lock);
407 return DS_OK;
411 static void DSOUND_PrimaryClose(DirectSoundDevice *device)
413 HRESULT hr;
415 TRACE("(%p)\n", device);
417 if(device->client){
418 hr = IAudioClient_Stop(device->client);
419 if(FAILED(hr))
420 WARN("Stop failed: %08x\n", hr);
423 /* clear the queue */
424 device->in_mmdev_bytes = 0;
427 HRESULT DSOUND_PrimaryCreate(DirectSoundDevice *device)
429 HRESULT err = DS_OK;
430 TRACE("(%p)\n", device);
432 device->buflen = ds_hel_buflen;
433 err = DSOUND_PrimaryOpen(device);
435 if (err != DS_OK) {
436 WARN("DSOUND_PrimaryOpen failed\n");
437 return err;
440 device->state = STATE_STOPPED;
441 return DS_OK;
444 HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device)
446 TRACE("(%p)\n", device);
448 /* **** */
449 EnterCriticalSection(&(device->mixlock));
451 DSOUND_PrimaryClose(device);
453 if(device->primary && (device->primary->ref || device->primary->numIfaces))
454 WARN("Destroying primary buffer while references held (%u %u)\n", device->primary->ref, device->primary->numIfaces);
456 HeapFree(GetProcessHeap(), 0, device->primary);
457 device->primary = NULL;
459 HeapFree(GetProcessHeap(),0,device->primary_pwfx);
460 HeapFree(GetProcessHeap(),0,device->pwfx);
461 device->pwfx=NULL;
463 LeaveCriticalSection(&(device->mixlock));
464 /* **** */
466 return DS_OK;
469 HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device)
471 HRESULT hr;
473 TRACE("(%p)\n", device);
475 hr = IAudioClient_Start(device->client);
476 if(FAILED(hr) && hr != AUDCLNT_E_NOT_STOPPED){
477 WARN("Start failed: %08x\n", hr);
478 return hr;
481 return DS_OK;
484 HRESULT DSOUND_PrimaryStop(DirectSoundDevice *device)
486 HRESULT hr;
488 TRACE("(%p)\n", device);
490 hr = IAudioClient_Stop(device->client);
491 if(FAILED(hr)){
492 WARN("Stop failed: %08x\n", hr);
493 return hr;
496 return DS_OK;
499 HRESULT DSOUND_PrimaryGetPosition(DirectSoundDevice *device, LPDWORD playpos, LPDWORD writepos)
501 TRACE("(%p,%p,%p)\n", device, playpos, writepos);
503 /* check if playpos was requested */
504 if (playpos)
505 *playpos = device->playing_offs_bytes;
507 /* check if writepos was requested */
508 if (writepos)
509 /* the writepos is the first non-queued position */
510 *writepos = (device->playing_offs_bytes + device->in_mmdev_bytes) % device->buflen;
512 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:-1, writepos?*writepos:-1, device, GetTickCount());
513 return DS_OK;
516 WAVEFORMATEX *DSOUND_CopyFormat(const WAVEFORMATEX *wfex)
518 WAVEFORMATEX *pwfx;
519 if(wfex->wFormatTag == WAVE_FORMAT_PCM){
520 pwfx = HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEX));
521 CopyMemory(pwfx, wfex, sizeof(PCMWAVEFORMAT));
522 pwfx->cbSize = 0;
523 }else{
524 pwfx = HeapAlloc(GetProcessHeap(), 0, sizeof(WAVEFORMATEX) + wfex->cbSize);
525 CopyMemory(pwfx, wfex, sizeof(WAVEFORMATEX) + wfex->cbSize);
528 if(pwfx->wFormatTag == WAVE_FORMAT_PCM ||
529 (pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
530 IsEqualGUID(&((const WAVEFORMATEXTENSIBLE*)pwfx)->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)))
531 pwfx->nBlockAlign = (pwfx->nChannels * pwfx->wBitsPerSample) / 8;
533 return pwfx;
536 HRESULT primarybuffer_SetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX passed_fmt)
538 HRESULT err = S_OK;
539 WAVEFORMATEX *old_fmt;
540 WAVEFORMATEXTENSIBLE *fmtex, *passed_fmtex = (WAVEFORMATEXTENSIBLE*)passed_fmt;
541 BOOL forced = (device->priolevel == DSSCL_WRITEPRIMARY);
543 TRACE("(%p,%p)\n", device, passed_fmt);
545 if (device->priolevel == DSSCL_NORMAL) {
546 WARN("failed priority check!\n");
547 return DSERR_PRIOLEVELNEEDED;
550 /* Let's be pedantic! */
551 if (passed_fmt == NULL) {
552 WARN("invalid parameter: passed_fmt==NULL!\n");
553 return DSERR_INVALIDPARAM;
555 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
556 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
557 passed_fmt->wFormatTag, passed_fmt->nChannels, passed_fmt->nSamplesPerSec,
558 passed_fmt->nAvgBytesPerSec, passed_fmt->nBlockAlign,
559 passed_fmt->wBitsPerSample, passed_fmt->cbSize);
561 if(passed_fmt->wBitsPerSample < 8 || passed_fmt->wBitsPerSample % 8 != 0 ||
562 passed_fmt->nChannels == 0 || passed_fmt->nSamplesPerSec == 0 ||
563 passed_fmt->nAvgBytesPerSec == 0 ||
564 passed_fmt->nBlockAlign != passed_fmt->nChannels * passed_fmt->wBitsPerSample / 8)
565 return DSERR_INVALIDPARAM;
567 if(passed_fmt->wFormatTag == WAVE_FORMAT_EXTENSIBLE){
568 if(passed_fmtex->Samples.wValidBitsPerSample > passed_fmtex->Format.wBitsPerSample)
569 return DSERR_INVALIDPARAM;
572 /* **** */
573 RtlAcquireResourceExclusive(&(device->buffer_list_lock), TRUE);
574 EnterCriticalSection(&(device->mixlock));
576 if (device->priolevel == DSSCL_WRITEPRIMARY) {
577 old_fmt = device->primary_pwfx;
578 device->primary_pwfx = DSOUND_CopyFormat(passed_fmt);
579 fmtex = (WAVEFORMATEXTENSIBLE *)device->primary_pwfx;
580 if (device->primary_pwfx == NULL) {
581 err = DSERR_OUTOFMEMORY;
582 goto out;
585 if (fmtex->Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
586 fmtex->Samples.wValidBitsPerSample == 0) {
587 TRACE("Correcting 0 valid bits per sample\n");
588 fmtex->Samples.wValidBitsPerSample = fmtex->Format.wBitsPerSample;
591 DSOUND_PrimaryClose(device);
593 err = DSOUND_ReopenDevice(device, forced);
594 if (FAILED(err)) {
595 ERR("No formats could be opened\n");
596 goto done;
599 err = DSOUND_PrimaryOpen(device);
600 if (err != DS_OK) {
601 ERR("DSOUND_PrimaryOpen failed\n");
602 goto done;
605 done:
606 if (err != DS_OK)
607 device->primary_pwfx = old_fmt;
608 else
609 HeapFree(GetProcessHeap(), 0, old_fmt);
610 } else {
611 HeapFree(GetProcessHeap(), 0, device->primary_pwfx);
612 device->primary_pwfx = DSOUND_CopyFormat(passed_fmt);
615 out:
616 LeaveCriticalSection(&(device->mixlock));
617 RtlReleaseResource(&(device->buffer_list_lock));
618 /* **** */
620 return err;
623 /*******************************************************************************
624 * PrimaryBuffer
626 static inline IDirectSoundBufferImpl *impl_from_IDirectSoundBuffer(IDirectSoundBuffer *iface)
628 /* IDirectSoundBuffer and IDirectSoundBuffer8 use the same iface. */
629 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSoundBuffer8_iface);
632 /* This sets this format for the primary buffer only */
633 static HRESULT WINAPI PrimaryBufferImpl_SetFormat(IDirectSoundBuffer *iface,
634 const WAVEFORMATEX *wfex)
636 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
637 TRACE("(%p,%p)\n", iface, wfex);
638 return primarybuffer_SetFormat(This->device, wfex);
641 static HRESULT WINAPI PrimaryBufferImpl_SetVolume(IDirectSoundBuffer *iface, LONG vol)
643 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
644 DirectSoundDevice *device = This->device;
645 HRESULT hr;
646 float fvol;
647 int i;
649 TRACE("(%p,%d)\n", iface, vol);
651 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
652 WARN("control unavailable\n");
653 return DSERR_CONTROLUNAVAIL;
656 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
657 WARN("invalid parameter: vol = %d\n", vol);
658 return DSERR_INVALIDPARAM;
661 /* **** */
662 EnterCriticalSection(&device->mixlock);
664 for (i = 0; i < DS_MAX_CHANNELS; i++) {
665 if (device->pwfx->nChannels > i){
666 hr = IAudioStreamVolume_GetChannelVolume(device->volume, i, &fvol);
667 if (FAILED(hr)){
668 LeaveCriticalSection(&device->mixlock);
669 WARN("GetChannelVolume failed: %08x\n", hr);
670 return hr;
672 } else
673 fvol=1.0f;
675 device->volpan.dwTotalAmpFactor[i]=((UINT16)(fvol * (DWORD)0xFFFF));
678 DSOUND_AmpFactorToVolPan(&device->volpan);
679 if (vol != device->volpan.lVolume) {
680 device->volpan.lVolume=vol;
681 DSOUND_RecalcVolPan(&device->volpan);
683 for (i = 0; i < DS_MAX_CHANNELS; i++) {
684 if (device->pwfx->nChannels > i){
685 fvol = (float)((DWORD)(device->volpan.dwTotalAmpFactor[i] & 0xFFFF) / (float)0xFFFF);
686 hr = IAudioStreamVolume_SetChannelVolume(device->volume, i, fvol);
687 if (FAILED(hr)){
688 LeaveCriticalSection(&device->mixlock);
689 WARN("SetChannelVolume failed: %08x\n", hr);
690 return hr;
696 LeaveCriticalSection(&(device->mixlock));
697 /* **** */
699 return DS_OK;
702 static HRESULT WINAPI PrimaryBufferImpl_GetVolume(IDirectSoundBuffer *iface, LONG *vol)
704 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
705 DirectSoundDevice *device = This->device;
706 float fvol;
707 HRESULT hr;
708 int i;
710 TRACE("(%p,%p)\n", iface, vol);
712 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
713 WARN("control unavailable\n");
714 return DSERR_CONTROLUNAVAIL;
717 if (vol == NULL) {
718 WARN("invalid parameter: vol = NULL\n");
719 return DSERR_INVALIDPARAM;
722 EnterCriticalSection(&device->mixlock);
724 for (i = 0; i < DS_MAX_CHANNELS; i++) {
725 if (device->pwfx->nChannels > i){
726 hr = IAudioStreamVolume_GetChannelVolume(device->volume, i, &fvol);
727 if (FAILED(hr)){
728 LeaveCriticalSection(&device->mixlock);
729 WARN("GetChannelVolume failed: %08x\n", hr);
730 return hr;
732 } else
733 fvol = 1;
735 device->volpan.dwTotalAmpFactor[i] = ((UINT16)(fvol * (DWORD)0xFFFF));
738 DSOUND_AmpFactorToVolPan(&device->volpan);
739 *vol = device->volpan.lVolume;
741 LeaveCriticalSection(&device->mixlock);
743 return DS_OK;
746 static HRESULT WINAPI PrimaryBufferImpl_SetFrequency(IDirectSoundBuffer *iface, DWORD freq)
748 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
749 TRACE("(%p,%d)\n",This,freq);
751 /* You cannot set the frequency of the primary buffer */
752 WARN("control unavailable\n");
753 return DSERR_CONTROLUNAVAIL;
756 static HRESULT WINAPI PrimaryBufferImpl_Play(IDirectSoundBuffer *iface, DWORD reserved1,
757 DWORD reserved2, DWORD flags)
759 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
760 DirectSoundDevice *device = This->device;
761 TRACE("(%p,%08x,%08x,%08x)\n", iface, reserved1, reserved2, flags);
763 if (!(flags & DSBPLAY_LOOPING)) {
764 WARN("invalid parameter: flags = %08x\n", flags);
765 return DSERR_INVALIDPARAM;
768 /* **** */
769 EnterCriticalSection(&(device->mixlock));
771 if (device->state == STATE_STOPPED)
772 device->state = STATE_STARTING;
773 else if (device->state == STATE_STOPPING)
774 device->state = STATE_PLAYING;
776 LeaveCriticalSection(&(device->mixlock));
777 /* **** */
779 return DS_OK;
782 static HRESULT WINAPI PrimaryBufferImpl_Stop(IDirectSoundBuffer *iface)
784 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
785 DirectSoundDevice *device = This->device;
786 TRACE("(%p)\n", iface);
788 /* **** */
789 EnterCriticalSection(&(device->mixlock));
791 if (device->state == STATE_PLAYING)
792 device->state = STATE_STOPPING;
793 else if (device->state == STATE_STARTING)
794 device->state = STATE_STOPPED;
796 LeaveCriticalSection(&(device->mixlock));
797 /* **** */
799 return DS_OK;
802 static ULONG WINAPI PrimaryBufferImpl_AddRef(IDirectSoundBuffer *iface)
804 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
805 ULONG ref = InterlockedIncrement(&(This->ref));
806 TRACE("(%p) ref was %d\n", This, ref - 1);
807 if(ref == 1)
808 InterlockedIncrement(&This->numIfaces);
809 return ref;
812 /* Decreases *out by 1 to no less than 0.
813 * Returns the new value of *out. */
814 LONG capped_refcount_dec(LONG *out)
816 LONG ref, oldref;
817 do {
818 ref = *out;
819 if(!ref)
820 return 0;
821 oldref = InterlockedCompareExchange(out, ref - 1, ref);
822 } while(oldref != ref);
823 return ref - 1;
826 static ULONG WINAPI PrimaryBufferImpl_Release(IDirectSoundBuffer *iface)
828 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
829 ULONG ref;
831 ref = capped_refcount_dec(&This->ref);
832 if(!ref)
833 capped_refcount_dec(&This->numIfaces);
835 TRACE("(%p) primary ref is now %d\n", This, ref);
837 return ref;
840 static HRESULT WINAPI PrimaryBufferImpl_GetCurrentPosition(IDirectSoundBuffer *iface,
841 DWORD *playpos, DWORD *writepos)
843 HRESULT hres;
844 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
845 DirectSoundDevice *device = This->device;
846 TRACE("(%p,%p,%p)\n", iface, playpos, writepos);
848 /* **** */
849 EnterCriticalSection(&(device->mixlock));
851 hres = DSOUND_PrimaryGetPosition(device, playpos, writepos);
852 if (hres != DS_OK) {
853 WARN("DSOUND_PrimaryGetPosition failed\n");
854 LeaveCriticalSection(&(device->mixlock));
855 return hres;
857 if (writepos) {
858 if (device->state != STATE_STOPPED)
859 /* apply the documented 10ms lead to writepos */
860 *writepos += device->writelead;
861 while (*writepos >= device->buflen) *writepos -= device->buflen;
864 LeaveCriticalSection(&(device->mixlock));
865 /* **** */
867 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:0, writepos?*writepos:0, device, GetTickCount());
868 return DS_OK;
871 static HRESULT WINAPI PrimaryBufferImpl_GetStatus(IDirectSoundBuffer *iface, DWORD *status)
873 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
874 DirectSoundDevice *device = This->device;
875 TRACE("(%p,%p)\n", iface, status);
877 if (status == NULL) {
878 WARN("invalid parameter: status == NULL\n");
879 return DSERR_INVALIDPARAM;
882 *status = 0;
883 if ((device->state == STATE_STARTING) ||
884 (device->state == STATE_PLAYING))
885 *status |= DSBSTATUS_PLAYING | DSBSTATUS_LOOPING;
887 TRACE("status=%x\n", *status);
888 return DS_OK;
892 static HRESULT WINAPI PrimaryBufferImpl_GetFormat(IDirectSoundBuffer *iface, WAVEFORMATEX *lpwf,
893 DWORD wfsize, DWORD *wfwritten)
895 DWORD size;
896 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
897 DirectSoundDevice *device = This->device;
898 TRACE("(%p,%p,%d,%p)\n", iface, lpwf, wfsize, wfwritten);
900 size = sizeof(WAVEFORMATEX) + device->primary_pwfx->cbSize;
902 if (lpwf) { /* NULL is valid */
903 if (wfsize >= size) {
904 CopyMemory(lpwf,device->primary_pwfx,size);
905 if (wfwritten)
906 *wfwritten = size;
907 } else {
908 WARN("invalid parameter: wfsize too small\n");
909 if (wfwritten)
910 *wfwritten = 0;
911 return DSERR_INVALIDPARAM;
913 } else {
914 if (wfwritten)
915 *wfwritten = sizeof(WAVEFORMATEX) + device->primary_pwfx->cbSize;
916 else {
917 WARN("invalid parameter: wfwritten == NULL\n");
918 return DSERR_INVALIDPARAM;
922 return DS_OK;
925 static HRESULT WINAPI PrimaryBufferImpl_Lock(IDirectSoundBuffer *iface, DWORD writecursor,
926 DWORD writebytes, void **lplpaudioptr1, DWORD *audiobytes1, void **lplpaudioptr2,
927 DWORD *audiobytes2, DWORD flags)
929 HRESULT hres;
930 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
931 DirectSoundDevice *device = This->device;
932 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
933 iface,
934 writecursor,
935 writebytes,
936 lplpaudioptr1,
937 audiobytes1,
938 lplpaudioptr2,
939 audiobytes2,
940 flags,
941 GetTickCount()
944 if (!audiobytes1)
945 return DSERR_INVALIDPARAM;
947 if (device->priolevel != DSSCL_WRITEPRIMARY) {
948 WARN("failed priority check!\n");
949 return DSERR_PRIOLEVELNEEDED;
952 /* when this flag is set, writecursor is meaningless and must be calculated */
953 if (flags & DSBLOCK_FROMWRITECURSOR) {
954 /* GetCurrentPosition does too much magic to duplicate here */
955 hres = IDirectSoundBuffer_GetCurrentPosition(iface, NULL, &writecursor);
956 if (hres != DS_OK) {
957 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
958 return hres;
962 /* when this flag is set, writebytes is meaningless and must be set */
963 if (flags & DSBLOCK_ENTIREBUFFER)
964 writebytes = device->buflen;
966 if (writecursor >= device->buflen) {
967 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
968 writecursor, device->buflen);
969 return DSERR_INVALIDPARAM;
972 if (writebytes > device->buflen) {
973 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
974 writebytes, device->buflen);
975 return DSERR_INVALIDPARAM;
978 if (writecursor+writebytes <= device->buflen) {
979 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
980 *audiobytes1 = writebytes;
981 if (lplpaudioptr2)
982 *(LPBYTE*)lplpaudioptr2 = NULL;
983 if (audiobytes2)
984 *audiobytes2 = 0;
985 TRACE("->%d.0\n",writebytes);
986 } else {
987 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
988 *audiobytes1 = device->buflen-writecursor;
989 if (lplpaudioptr2)
990 *(LPBYTE*)lplpaudioptr2 = device->buffer;
991 if (audiobytes2)
992 *audiobytes2 = writebytes-(device->buflen-writecursor);
993 TRACE("->%d.%d\n",*audiobytes1,audiobytes2?*audiobytes2:0);
995 return DS_OK;
998 static HRESULT WINAPI PrimaryBufferImpl_SetCurrentPosition(IDirectSoundBuffer *iface, DWORD newpos)
1000 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1001 TRACE("(%p,%d)\n",This,newpos);
1003 /* You cannot set the position of the primary buffer */
1004 WARN("invalid call\n");
1005 return DSERR_INVALIDCALL;
1008 static HRESULT WINAPI PrimaryBufferImpl_SetPan(IDirectSoundBuffer *iface, LONG pan)
1010 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1011 DirectSoundDevice *device = This->device;
1012 float fvol;
1013 HRESULT hr;
1014 int i;
1016 TRACE("(%p,%d)\n", iface, pan);
1018 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
1019 WARN("control unavailable\n");
1020 return DSERR_CONTROLUNAVAIL;
1023 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
1024 WARN("invalid parameter: pan = %d\n", pan);
1025 return DSERR_INVALIDPARAM;
1028 /* **** */
1029 EnterCriticalSection(&device->mixlock);
1031 for (i = 0; i < DS_MAX_CHANNELS; i++) {
1032 if (device->pwfx->nChannels > i){
1033 hr = IAudioStreamVolume_GetChannelVolume(device->volume, i, &fvol);
1034 if (FAILED(hr)){
1035 LeaveCriticalSection(&device->mixlock);
1036 WARN("GetChannelVolume failed: %08x\n", hr);
1037 return hr;
1039 } else
1040 fvol = 1;
1042 device->volpan.dwTotalAmpFactor[i] = ((UINT16)(fvol * (DWORD)0xFFFF));
1045 DSOUND_AmpFactorToVolPan(&device->volpan);
1046 if (pan != device->volpan.lPan) {
1047 device->volpan.lPan=pan;
1048 DSOUND_RecalcVolPan(&device->volpan);
1050 for (i = 0; i < DS_MAX_CHANNELS; i++) {
1051 if (device->pwfx->nChannels > i) {
1052 fvol = (float)((DWORD)(device->volpan.dwTotalAmpFactor[i] & 0xFFFF) / (float)0xFFFF);
1053 hr = IAudioStreamVolume_SetChannelVolume(device->volume, i, fvol);
1054 if (FAILED(hr)){
1055 LeaveCriticalSection(&device->mixlock);
1056 WARN("SetChannelVolume failed: %08x\n", hr);
1057 return hr;
1063 LeaveCriticalSection(&device->mixlock);
1064 /* **** */
1066 return DS_OK;
1069 static HRESULT WINAPI PrimaryBufferImpl_GetPan(IDirectSoundBuffer *iface, LONG *pan)
1071 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1072 DirectSoundDevice *device = This->device;
1073 float fvol;
1074 HRESULT hr;
1075 int i;
1077 TRACE("(%p,%p)\n", iface, pan);
1079 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
1080 WARN("control unavailable\n");
1081 return DSERR_CONTROLUNAVAIL;
1084 if (pan == NULL) {
1085 WARN("invalid parameter: pan == NULL\n");
1086 return DSERR_INVALIDPARAM;
1089 EnterCriticalSection(&device->mixlock);
1091 for (i = 0; i < DS_MAX_CHANNELS; i++) {
1092 if (device->pwfx->nChannels > i) {
1093 hr = IAudioStreamVolume_GetChannelVolume(device->volume, i, &fvol);
1094 if (FAILED(hr)){
1095 LeaveCriticalSection(&device->mixlock);
1096 WARN("GetChannelVolume failed: %08x\n", hr);
1097 return hr;
1099 } else
1100 fvol = 1;
1102 device->volpan.dwTotalAmpFactor[i] = ((UINT16)(fvol * (DWORD)0xFFFF));
1105 DSOUND_AmpFactorToVolPan(&device->volpan);
1106 *pan = device->volpan.lPan;
1108 LeaveCriticalSection(&device->mixlock);
1110 return DS_OK;
1113 static HRESULT WINAPI PrimaryBufferImpl_Unlock(IDirectSoundBuffer *iface, void *p1, DWORD x1,
1114 void *p2, DWORD x2)
1116 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1117 DirectSoundDevice *device = This->device;
1118 TRACE("(%p,%p,%d,%p,%d)\n", iface, p1, x1, p2, x2);
1120 if (device->priolevel != DSSCL_WRITEPRIMARY) {
1121 WARN("failed priority check!\n");
1122 return DSERR_PRIOLEVELNEEDED;
1125 if ((p1 && ((BYTE*)p1 < device->buffer || (BYTE*)p1 >= device->buffer + device->buflen)) ||
1126 (p2 && ((BYTE*)p2 < device->buffer || (BYTE*)p2 >= device->buffer + device->buflen)))
1127 return DSERR_INVALIDPARAM;
1129 return DS_OK;
1132 static HRESULT WINAPI PrimaryBufferImpl_Restore(IDirectSoundBuffer *iface)
1134 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1135 FIXME("(%p):stub\n",This);
1136 return DS_OK;
1139 static HRESULT WINAPI PrimaryBufferImpl_GetFrequency(IDirectSoundBuffer *iface, DWORD *freq)
1141 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1142 DirectSoundDevice *device = This->device;
1143 TRACE("(%p,%p)\n", iface, freq);
1145 if (freq == NULL) {
1146 WARN("invalid parameter: freq == NULL\n");
1147 return DSERR_INVALIDPARAM;
1150 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
1151 WARN("control unavailable\n");
1152 return DSERR_CONTROLUNAVAIL;
1155 *freq = device->pwfx->nSamplesPerSec;
1156 TRACE("-> %d\n", *freq);
1158 return DS_OK;
1161 static HRESULT WINAPI PrimaryBufferImpl_Initialize(IDirectSoundBuffer *iface, IDirectSound *dsound,
1162 const DSBUFFERDESC *dbsd)
1164 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1165 WARN("(%p) already initialized\n", This);
1166 return DSERR_ALREADYINITIALIZED;
1169 static HRESULT WINAPI PrimaryBufferImpl_GetCaps(IDirectSoundBuffer *iface, DSBCAPS *caps)
1171 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1172 DirectSoundDevice *device = This->device;
1173 TRACE("(%p,%p)\n", iface, caps);
1175 if (caps == NULL) {
1176 WARN("invalid parameter: caps == NULL\n");
1177 return DSERR_INVALIDPARAM;
1180 if (caps->dwSize < sizeof(*caps)) {
1181 WARN("invalid parameter: caps->dwSize = %d\n", caps->dwSize);
1182 return DSERR_INVALIDPARAM;
1185 caps->dwFlags = This->dsbd.dwFlags;
1186 caps->dwBufferBytes = device->buflen;
1188 /* Windows reports these as zero */
1189 caps->dwUnlockTransferRate = 0;
1190 caps->dwPlayCpuOverhead = 0;
1192 return DS_OK;
1195 static HRESULT WINAPI PrimaryBufferImpl_QueryInterface(IDirectSoundBuffer *iface, REFIID riid,
1196 void **ppobj)
1198 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1200 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppobj);
1202 if (ppobj == NULL) {
1203 WARN("invalid parameter\n");
1204 return E_INVALIDARG;
1207 *ppobj = NULL; /* assume failure */
1209 if ( IsEqualGUID(riid, &IID_IUnknown) ||
1210 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ) {
1211 IDirectSoundBuffer_AddRef(iface);
1212 *ppobj = iface;
1213 return S_OK;
1216 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
1217 /* a primary buffer can't have a DirectSoundBuffer8 interface */
1218 if ( IsEqualGUID( &IID_IDirectSoundBuffer8, riid ) ) {
1219 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
1220 return E_NOINTERFACE;
1223 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
1224 ERR("app requested IDirectSoundNotify on primary buffer\n");
1225 /* FIXME: should we support this? */
1226 return E_NOINTERFACE;
1229 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
1230 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1231 return E_NOINTERFACE;
1234 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
1235 *ppobj = &This->IDirectSound3DListener_iface;
1236 IDirectSound3DListener_AddRef(&This->IDirectSound3DListener_iface);
1237 return S_OK;
1240 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
1241 *ppobj = &This->IKsPropertySet_iface;
1242 IKsPropertySet_AddRef(&This->IKsPropertySet_iface);
1243 return S_OK;
1246 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
1247 return E_NOINTERFACE;
1250 static const IDirectSoundBufferVtbl dspbvt =
1252 PrimaryBufferImpl_QueryInterface,
1253 PrimaryBufferImpl_AddRef,
1254 PrimaryBufferImpl_Release,
1255 PrimaryBufferImpl_GetCaps,
1256 PrimaryBufferImpl_GetCurrentPosition,
1257 PrimaryBufferImpl_GetFormat,
1258 PrimaryBufferImpl_GetVolume,
1259 PrimaryBufferImpl_GetPan,
1260 PrimaryBufferImpl_GetFrequency,
1261 PrimaryBufferImpl_GetStatus,
1262 PrimaryBufferImpl_Initialize,
1263 PrimaryBufferImpl_Lock,
1264 PrimaryBufferImpl_Play,
1265 PrimaryBufferImpl_SetCurrentPosition,
1266 PrimaryBufferImpl_SetFormat,
1267 PrimaryBufferImpl_SetVolume,
1268 PrimaryBufferImpl_SetPan,
1269 PrimaryBufferImpl_SetFrequency,
1270 PrimaryBufferImpl_Stop,
1271 PrimaryBufferImpl_Unlock,
1272 PrimaryBufferImpl_Restore
1275 HRESULT primarybuffer_create(DirectSoundDevice *device, IDirectSoundBufferImpl **ppdsb,
1276 const DSBUFFERDESC *dsbd)
1278 IDirectSoundBufferImpl *dsb;
1279 TRACE("%p,%p,%p)\n",device,ppdsb,dsbd);
1281 if (dsbd->lpwfxFormat) {
1282 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1283 *ppdsb = NULL;
1284 return DSERR_INVALIDPARAM;
1287 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1289 if (dsb == NULL) {
1290 WARN("out of memory\n");
1291 *ppdsb = NULL;
1292 return DSERR_OUTOFMEMORY;
1295 dsb->ref = 0;
1296 dsb->ref3D = 0;
1297 dsb->refiks = 0;
1298 dsb->numIfaces = 0;
1299 dsb->device = device;
1300 dsb->IDirectSoundBuffer8_iface.lpVtbl = (IDirectSoundBuffer8Vtbl *)&dspbvt;
1301 dsb->IDirectSound3DListener_iface.lpVtbl = &ds3dlvt;
1302 dsb->IKsPropertySet_iface.lpVtbl = &iksbvt;
1303 dsb->dsbd = *dsbd;
1305 /* IDirectSound3DListener */
1306 device->ds3dl.dwSize = sizeof(DS3DLISTENER);
1307 device->ds3dl.vPosition.x = 0.0;
1308 device->ds3dl.vPosition.y = 0.0;
1309 device->ds3dl.vPosition.z = 0.0;
1310 device->ds3dl.vVelocity.x = 0.0;
1311 device->ds3dl.vVelocity.y = 0.0;
1312 device->ds3dl.vVelocity.z = 0.0;
1313 device->ds3dl.vOrientFront.x = 0.0;
1314 device->ds3dl.vOrientFront.y = 0.0;
1315 device->ds3dl.vOrientFront.z = 1.0;
1316 device->ds3dl.vOrientTop.x = 0.0;
1317 device->ds3dl.vOrientTop.y = 1.0;
1318 device->ds3dl.vOrientTop.z = 0.0;
1319 device->ds3dl.flDistanceFactor = DS3D_DEFAULTDISTANCEFACTOR;
1320 device->ds3dl.flRolloffFactor = DS3D_DEFAULTROLLOFFFACTOR;
1321 device->ds3dl.flDopplerFactor = DS3D_DEFAULTDOPPLERFACTOR;
1322 device->ds3dl_need_recalc = TRUE;
1324 TRACE("Created primary buffer at %p\n", dsb);
1325 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
1326 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
1327 device->pwfx->wFormatTag, device->pwfx->nChannels,
1328 device->pwfx->nSamplesPerSec, device->pwfx->nAvgBytesPerSec,
1329 device->pwfx->nBlockAlign, device->pwfx->wBitsPerSample,
1330 device->pwfx->cbSize);
1332 IDirectSoundBuffer_AddRef(&dsb->IDirectSoundBuffer8_iface);
1333 *ppdsb = dsb;
1334 return S_OK;