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
22 * When PrimarySetFormat (via ReopenDevice or PrimaryOpen) fails,
23 * it leaves dsound in unusable (not really open) state.
29 #define NONAMELESSSTRUCT
30 #define NONAMELESSUNION
37 #include "wine/debug.h"
39 #include "dsound_private.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
43 /** Calculate how long a fragment length of about 10 ms should be in frames
45 * nSamplesPerSec: Frequency rate in samples per second
46 * nBlockAlign: Size of a single blockalign
49 * Size in bytes of a single fragment
51 DWORD DSOUND_fraglen(DWORD nSamplesPerSec, DWORD nBlockAlign)
53 /* Given a timer delay of 10ms, the fragment size is approximately:
54 * fraglen = (nSamplesPerSec * 10 / 1000) * nBlockAlign
55 * ==> fraglen = (nSamplesPerSec / 100) * nBlockSize
57 * ALSA uses buffers that are powers of 2. Because of this, fraglen
58 * is rounded up to the nearest power of 2:
61 if (nSamplesPerSec <= 12800)
62 return 128 * nBlockAlign;
64 if (nSamplesPerSec <= 25600)
65 return 256 * nBlockAlign;
67 if (nSamplesPerSec <= 51200)
68 return 512 * nBlockAlign;
70 return 1024 * nBlockAlign;
73 static void DSOUND_RecalcPrimary(DirectSoundDevice *device)
75 TRACE("(%p)\n", device);
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);
81 /* calculate the 10ms write lead */
82 device->writelead = (device->pwfx->nSamplesPerSec / 100) * device->pwfx->nBlockAlign;
85 HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave)
89 TRACE("(%p, %d)\n", device, forcewave);
92 IAudioClient_Release(device->client);
93 device->client = NULL;
96 IAudioRenderClient_Release(device->render);
97 device->render = NULL;
100 IAudioClock_Release(device->clock);
101 device->clock = NULL;
104 IAudioStreamVolume_Release(device->volume);
105 device->volume = NULL;
108 hres = IMMDevice_Activate(device->mmdevice, &IID_IAudioClient,
109 CLSCTX_INPROC_SERVER, NULL, (void **)&device->client);
111 WARN("Activate failed: %08x\n", hres);
115 /* buffer size = 200 * 100000 (100 ns) = 2.0 seconds */
116 hres = IAudioClient_Initialize(device->client,
117 AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_NOPERSIST,
118 200 * 100000, 50000, device->pwfx, NULL);
120 IAudioClient_Release(device->client);
121 device->client = NULL;
122 WARN("Initialize failed: %08x\n", hres);
126 hres = IAudioClient_GetService(device->client, &IID_IAudioRenderClient,
127 (void**)&device->render);
129 IAudioClient_Release(device->client);
130 device->client = NULL;
131 WARN("GetService failed: %08x\n", hres);
135 hres = IAudioClient_GetService(device->client, &IID_IAudioClock,
136 (void**)&device->clock);
138 IAudioClient_Release(device->client);
139 IAudioRenderClient_Release(device->render);
140 device->client = NULL;
141 device->render = NULL;
142 WARN("GetService failed: %08x\n", hres);
146 hres = IAudioClient_GetService(device->client, &IID_IAudioStreamVolume,
147 (void**)&device->volume);
149 IAudioClient_Release(device->client);
150 IAudioRenderClient_Release(device->render);
151 IAudioClock_Release(device->clock);
152 device->client = NULL;
153 device->render = NULL;
154 device->clock = NULL;
155 WARN("GetService failed: %08x\n", hres);
162 static HRESULT DSOUND_PrimaryOpen(DirectSoundDevice *device)
167 TRACE("(%p)\n", device);
169 /* on original windows, the buffer it set to a fixed size, no matter what the settings are.
170 on windows this size is always fixed (tested on win-xp) */
172 device->buflen = ds_hel_buflen;
173 buflen = device->buflen;
174 buflen -= buflen % device->pwfx->nBlockAlign;
175 device->buflen = buflen;
177 device->mix_buffer_len = DSOUND_bufpos_to_mixpos(device, device->buflen);
178 device->mix_buffer = HeapAlloc(GetProcessHeap(), 0, device->mix_buffer_len);
179 if (!device->mix_buffer)
180 return DSERR_OUTOFMEMORY;
182 if (device->state == STATE_PLAYING) device->state = STATE_STARTING;
183 else if (device->state == STATE_STOPPING) device->state = STATE_STOPPED;
185 TRACE("desired buflen=%d, old buffer=%p\n", buflen, device->buffer);
187 /* reallocate emulated primary buffer */
189 newbuf = HeapReAlloc(GetProcessHeap(),0,device->buffer, buflen);
191 newbuf = HeapAlloc(GetProcessHeap(),0, buflen);
194 ERR("failed to allocate primary buffer\n");
195 return DSERR_OUTOFMEMORY;
196 /* but the old buffer might still exist and must be re-prepared */
199 DSOUND_RecalcPrimary(device);
201 device->buffer = newbuf;
203 TRACE("fraglen=%d\n", device->fraglen);
205 device->mixfunction = mixfunctions[device->pwfx->wBitsPerSample/8 - 1];
206 device->normfunction = normfunctions[device->pwfx->wBitsPerSample/8 - 1];
207 FillMemory(device->buffer, device->buflen, (device->pwfx->wBitsPerSample == 8) ? 128 : 0);
208 FillMemory(device->mix_buffer, device->mix_buffer_len, 0);
209 device->last_pos_bytes = device->pwplay = device->pwqueue = device->playpos = device->mixpos = 0;
214 static void DSOUND_PrimaryClose(DirectSoundDevice *device)
218 TRACE("(%p)\n", device);
220 device->pwqueue = (DWORD)-1; /* resetting queues */
223 hr = IAudioClient_Stop(device->client);
225 WARN("Stop failed: %08x\n", hr);
228 /* clear the queue */
232 HRESULT DSOUND_PrimaryCreate(DirectSoundDevice *device)
235 TRACE("(%p)\n", device);
237 device->buflen = ds_hel_buflen;
238 err = DSOUND_PrimaryOpen(device);
241 WARN("DSOUND_PrimaryOpen failed\n");
245 device->state = STATE_STOPPED;
249 HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device)
251 TRACE("(%p)\n", device);
254 EnterCriticalSection(&(device->mixlock));
256 DSOUND_PrimaryClose(device);
257 HeapFree(GetProcessHeap(),0,device->pwfx);
260 LeaveCriticalSection(&(device->mixlock));
266 HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device)
270 TRACE("(%p)\n", device);
272 hr = IAudioClient_Start(device->client);
274 WARN("Start failed: %08x\n", hr);
281 HRESULT DSOUND_PrimaryStop(DirectSoundDevice *device)
285 TRACE("(%p)\n", device);
287 hr = IAudioClient_Stop(device->client);
289 WARN("Stop failed: %08x\n", hr);
296 HRESULT DSOUND_PrimaryGetPosition(DirectSoundDevice *device, LPDWORD playpos, LPDWORD writepos)
298 TRACE("(%p,%p,%p)\n", device, playpos, writepos);
300 TRACE("pwplay=%i, pwqueue=%i\n", device->pwplay, device->pwqueue);
302 /* check if playpos was requested */
304 /* use the cached play position */
305 *playpos = device->pwplay * device->fraglen;
307 /* check if writepos was requested */
309 /* the writepos is the first non-queued position */
310 *writepos = ((device->pwplay + device->pwqueue) % device->helfrags) * device->fraglen;
312 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:-1, writepos?*writepos:-1, device, GetTickCount());
316 static DWORD DSOUND_GetFormatSize(LPCWAVEFORMATEX wfex)
318 if (wfex->wFormatTag == WAVE_FORMAT_PCM)
319 return sizeof(WAVEFORMATEX);
321 return sizeof(WAVEFORMATEX) + wfex->cbSize;
324 LPWAVEFORMATEX DSOUND_CopyFormat(LPCWAVEFORMATEX wfex)
326 DWORD size = DSOUND_GetFormatSize(wfex);
327 LPWAVEFORMATEX pwfx = HeapAlloc(GetProcessHeap(),0,size);
329 WARN("out of memory\n");
330 } else if (wfex->wFormatTag != WAVE_FORMAT_PCM) {
331 CopyMemory(pwfx, wfex, size);
333 CopyMemory(pwfx, wfex, sizeof(PCMWAVEFORMAT));
335 if (pwfx->nBlockAlign != pwfx->nChannels * pwfx->wBitsPerSample/8) {
336 WARN("Fixing bad nBlockAlign (%u)\n", pwfx->nBlockAlign);
337 pwfx->nBlockAlign = pwfx->nChannels * pwfx->wBitsPerSample/8;
339 if (pwfx->nAvgBytesPerSec != pwfx->nSamplesPerSec * pwfx->nBlockAlign) {
340 WARN("Fixing bad nAvgBytesPerSec (%u)\n", pwfx->nAvgBytesPerSec);
341 pwfx->nAvgBytesPerSec = pwfx->nSamplesPerSec * pwfx->nBlockAlign;
347 HRESULT primarybuffer_SetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX passed_fmt)
349 HRESULT err = DSERR_BUFFERLOST;
351 WAVEFORMATEX *old_fmt;
352 WAVEFORMATEXTENSIBLE *fmtex;
353 BOOL forced = (device->priolevel == DSSCL_WRITEPRIMARY);
355 TRACE("(%p,%p)\n", device, passed_fmt);
357 if (device->priolevel == DSSCL_NORMAL) {
358 WARN("failed priority check!\n");
359 return DSERR_PRIOLEVELNEEDED;
362 /* Let's be pedantic! */
363 if (passed_fmt == NULL) {
364 WARN("invalid parameter: passed_fmt==NULL!\n");
365 return DSERR_INVALIDPARAM;
367 TRACE("(formattag=0x%04x,chans=%d,samplerate=%d,"
368 "bytespersec=%d,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
369 passed_fmt->wFormatTag, passed_fmt->nChannels, passed_fmt->nSamplesPerSec,
370 passed_fmt->nAvgBytesPerSec, passed_fmt->nBlockAlign,
371 passed_fmt->wBitsPerSample, passed_fmt->cbSize);
374 RtlAcquireResourceExclusive(&(device->buffer_list_lock), TRUE);
375 EnterCriticalSection(&(device->mixlock));
377 old_fmt = device->pwfx;
378 device->pwfx = DSOUND_CopyFormat(passed_fmt);
379 fmtex = (WAVEFORMATEXTENSIBLE *)device->pwfx;
380 if (device->pwfx == NULL) {
381 device->pwfx = old_fmt;
383 err = DSERR_OUTOFMEMORY;
387 DSOUND_PrimaryClose(device);
389 err = DSOUND_ReopenDevice(device, FALSE);
393 /* requested format failed, so try others */
394 if(device->pwfx->wFormatTag == WAVE_FORMAT_IEEE_FLOAT){
395 device->pwfx->wFormatTag = WAVE_FORMAT_PCM;
396 device->pwfx->wBitsPerSample = 32;
397 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
398 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
400 err = DSOUND_ReopenDevice(device, FALSE);
405 if(device->pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
406 IsEqualGUID(&fmtex->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)){
407 fmtex->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
408 device->pwfx->wBitsPerSample = 32;
409 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
410 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
412 err = DSOUND_ReopenDevice(device, FALSE);
417 device->pwfx->wBitsPerSample = 32;
418 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
419 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
420 err = DSOUND_ReopenDevice(device, FALSE);
424 device->pwfx->wBitsPerSample = 16;
425 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
426 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
427 err = DSOUND_ReopenDevice(device, FALSE);
431 device->pwfx->wBitsPerSample = 8;
432 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
433 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
434 err = DSOUND_ReopenDevice(device, FALSE);
438 device->pwfx->nChannels = (passed_fmt->nChannels == 2) ? 1 : 2;
439 device->pwfx->wBitsPerSample = passed_fmt->wBitsPerSample;
440 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
441 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
442 err = DSOUND_ReopenDevice(device, FALSE);
446 device->pwfx->wBitsPerSample = 32;
447 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
448 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
449 err = DSOUND_ReopenDevice(device, FALSE);
453 device->pwfx->wBitsPerSample = 16;
454 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
455 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
456 err = DSOUND_ReopenDevice(device, FALSE);
460 device->pwfx->wBitsPerSample = 8;
461 device->pwfx->nAvgBytesPerSec = passed_fmt->nSamplesPerSec * device->pwfx->nBlockAlign;
462 device->pwfx->nBlockAlign = passed_fmt->nChannels * (device->pwfx->wBitsPerSample / 8);
463 err = DSOUND_ReopenDevice(device, FALSE);
467 WARN("No formats could be opened\n");
471 err = DSOUND_PrimaryOpen(device);
473 WARN("DSOUND_PrimaryOpen failed\n");
477 if (passed_fmt->nSamplesPerSec/100 != device->pwfx->nSamplesPerSec/100 && forced && device->buffer)
479 DSOUND_PrimaryClose(device);
480 device->pwfx->nSamplesPerSec = passed_fmt->nSamplesPerSec;
481 err = DSOUND_ReopenDevice(device, TRUE);
483 WARN("DSOUND_ReopenDevice(2) failed: %08x\n", err);
484 else if (FAILED((err = DSOUND_PrimaryOpen(device))))
485 WARN("DSOUND_PrimaryOpen(2) failed: %08x\n", err);
488 device->mix_buffer_len = DSOUND_bufpos_to_mixpos(device, device->buflen);
489 device->mix_buffer = HeapReAlloc(GetProcessHeap(), 0, device->mix_buffer, device->mix_buffer_len);
490 FillMemory(device->mix_buffer, device->mix_buffer_len, 0);
491 device->mixfunction = mixfunctions[device->pwfx->wBitsPerSample/8 - 1];
492 device->normfunction = normfunctions[device->pwfx->wBitsPerSample/8 - 1];
494 if (old_fmt->nSamplesPerSec != device->pwfx->nSamplesPerSec ||
495 old_fmt->wBitsPerSample != device->pwfx->wBitsPerSample ||
496 old_fmt->nChannels != device->pwfx->nChannels) {
497 IDirectSoundBufferImpl** dsb = device->buffers;
498 for (i = 0; i < device->nrofbuffers; i++, dsb++) {
500 RtlAcquireResourceExclusive(&(*dsb)->lock, TRUE);
502 (*dsb)->freqAdjust = ((DWORD64)(*dsb)->freq << DSOUND_FREQSHIFT) / device->pwfx->nSamplesPerSec;
503 DSOUND_RecalcFormat((*dsb));
504 DSOUND_MixToTemporary((*dsb), 0, (*dsb)->buflen, FALSE);
505 (*dsb)->primary_mixpos = 0;
507 RtlReleaseResource(&(*dsb)->lock);
513 LeaveCriticalSection(&(device->mixlock));
514 RtlReleaseResource(&(device->buffer_list_lock));
517 HeapFree(GetProcessHeap(), 0, old_fmt);
521 /*******************************************************************************
524 static inline IDirectSoundBufferImpl *impl_from_IDirectSoundBuffer(IDirectSoundBuffer *iface)
526 /* IDirectSoundBuffer and IDirectSoundBuffer8 use the same iface. */
527 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSoundBuffer8_iface);
530 /* This sets this format for the <em>Primary Buffer Only</em> */
531 /* See file:///cdrom/sdk52/docs/worddoc/dsound.doc page 120 */
532 static HRESULT WINAPI PrimaryBufferImpl_SetFormat(
533 LPDIRECTSOUNDBUFFER iface,
534 LPCWAVEFORMATEX wfex)
536 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
537 TRACE("(%p,%p)\n", iface, wfex);
538 return primarybuffer_SetFormat(This->device, wfex);
541 static HRESULT WINAPI PrimaryBufferImpl_SetVolume(
542 LPDIRECTSOUNDBUFFER iface,LONG vol
544 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
545 DirectSoundDevice *device = This->device;
549 TRACE("(%p,%d)\n", iface, vol);
551 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
552 WARN("control unavailable\n");
553 return DSERR_CONTROLUNAVAIL;
556 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
557 WARN("invalid parameter: vol = %d\n", vol);
558 return DSERR_INVALIDPARAM;
562 EnterCriticalSection(&device->mixlock);
564 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
566 LeaveCriticalSection(&device->mixlock);
567 WARN("GetChannelVolume failed: %08x\n", hr);
571 if(device->pwfx->nChannels > 1){
572 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
574 LeaveCriticalSection(&device->mixlock);
575 WARN("GetChannelVolume failed: %08x\n", hr);
581 device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
582 device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
584 DSOUND_AmpFactorToVolPan(&device->volpan);
585 if (vol != device->volpan.lVolume) {
586 device->volpan.lVolume=vol;
587 DSOUND_RecalcVolPan(&device->volpan);
588 lvol = (float)((DWORD)(device->volpan.dwTotalLeftAmpFactor & 0xFFFF) / (float)0xFFFF);
589 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 0, lvol);
591 LeaveCriticalSection(&device->mixlock);
592 WARN("SetChannelVolume failed: %08x\n", hr);
596 if(device->pwfx->nChannels > 1){
597 rvol = (float)((DWORD)(device->volpan.dwTotalRightAmpFactor & 0xFFFF) / (float)0xFFFF);
598 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 1, rvol);
600 LeaveCriticalSection(&device->mixlock);
601 WARN("SetChannelVolume failed: %08x\n", hr);
607 LeaveCriticalSection(&(device->mixlock));
613 static HRESULT WINAPI PrimaryBufferImpl_GetVolume(
614 LPDIRECTSOUNDBUFFER iface,LPLONG vol
616 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
617 DirectSoundDevice *device = This->device;
620 TRACE("(%p,%p)\n", iface, vol);
622 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
623 WARN("control unavailable\n");
624 return DSERR_CONTROLUNAVAIL;
628 WARN("invalid parameter: vol = NULL\n");
629 return DSERR_INVALIDPARAM;
632 EnterCriticalSection(&device->mixlock);
634 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
636 LeaveCriticalSection(&device->mixlock);
637 WARN("GetChannelVolume failed: %08x\n", hr);
641 if(device->pwfx->nChannels > 1){
642 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
644 LeaveCriticalSection(&device->mixlock);
645 WARN("GetChannelVolume failed: %08x\n", hr);
651 device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
652 device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
654 DSOUND_AmpFactorToVolPan(&device->volpan);
655 *vol = device->volpan.lVolume;
657 LeaveCriticalSection(&device->mixlock);
662 static HRESULT WINAPI PrimaryBufferImpl_SetFrequency(
663 LPDIRECTSOUNDBUFFER iface,DWORD freq
665 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
666 TRACE("(%p,%d)\n",This,freq);
668 /* You cannot set the frequency of the primary buffer */
669 WARN("control unavailable\n");
670 return DSERR_CONTROLUNAVAIL;
673 static HRESULT WINAPI PrimaryBufferImpl_Play(
674 LPDIRECTSOUNDBUFFER iface,DWORD reserved1,DWORD reserved2,DWORD flags
676 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
677 DirectSoundDevice *device = This->device;
678 TRACE("(%p,%08x,%08x,%08x)\n", iface, reserved1, reserved2, flags);
680 if (!(flags & DSBPLAY_LOOPING)) {
681 WARN("invalid parameter: flags = %08x\n", flags);
682 return DSERR_INVALIDPARAM;
686 EnterCriticalSection(&(device->mixlock));
688 if (device->state == STATE_STOPPED)
689 device->state = STATE_STARTING;
690 else if (device->state == STATE_STOPPING)
691 device->state = STATE_PLAYING;
693 LeaveCriticalSection(&(device->mixlock));
699 static HRESULT WINAPI PrimaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER iface)
701 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
702 DirectSoundDevice *device = This->device;
703 TRACE("(%p)\n", iface);
706 EnterCriticalSection(&(device->mixlock));
708 if (device->state == STATE_PLAYING)
709 device->state = STATE_STOPPING;
710 else if (device->state == STATE_STARTING)
711 device->state = STATE_STOPPED;
713 LeaveCriticalSection(&(device->mixlock));
719 static ULONG WINAPI PrimaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER iface)
721 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
722 ULONG ref = InterlockedIncrement(&(This->ref));
723 TRACE("(%p) ref was %d\n", This, ref - 1);
725 InterlockedIncrement(&This->numIfaces);
729 void primarybuffer_destroy(IDirectSoundBufferImpl *This)
731 This->device->primary = NULL;
732 HeapFree(GetProcessHeap(), 0, This);
733 TRACE("(%p) released\n", This);
736 static ULONG WINAPI PrimaryBufferImpl_Release(LPDIRECTSOUNDBUFFER iface)
738 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
739 DWORD ref = InterlockedDecrement(&(This->ref));
740 TRACE("(%p) ref was %d\n", This, ref + 1);
742 if (!ref && !InterlockedDecrement(&This->numIfaces))
743 primarybuffer_destroy(This);
747 static HRESULT WINAPI PrimaryBufferImpl_GetCurrentPosition(
748 LPDIRECTSOUNDBUFFER iface,LPDWORD playpos,LPDWORD writepos
751 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
752 DirectSoundDevice *device = This->device;
753 TRACE("(%p,%p,%p)\n", iface, playpos, writepos);
756 EnterCriticalSection(&(device->mixlock));
758 hres = DSOUND_PrimaryGetPosition(device, playpos, writepos);
760 WARN("DSOUND_PrimaryGetPosition failed\n");
761 LeaveCriticalSection(&(device->mixlock));
765 if (device->state != STATE_STOPPED)
766 /* apply the documented 10ms lead to writepos */
767 *writepos += device->writelead;
768 while (*writepos >= device->buflen) *writepos -= device->buflen;
771 LeaveCriticalSection(&(device->mixlock));
774 TRACE("playpos = %d, writepos = %d (%p, time=%d)\n", playpos?*playpos:0, writepos?*writepos:0, device, GetTickCount());
778 static HRESULT WINAPI PrimaryBufferImpl_GetStatus(
779 LPDIRECTSOUNDBUFFER iface,LPDWORD status
781 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
782 DirectSoundDevice *device = This->device;
783 TRACE("(%p,%p)\n", iface, status);
785 if (status == NULL) {
786 WARN("invalid parameter: status == NULL\n");
787 return DSERR_INVALIDPARAM;
791 if ((device->state == STATE_STARTING) ||
792 (device->state == STATE_PLAYING))
793 *status |= DSBSTATUS_PLAYING | DSBSTATUS_LOOPING;
795 TRACE("status=%x\n", *status);
800 static HRESULT WINAPI PrimaryBufferImpl_GetFormat(
801 LPDIRECTSOUNDBUFFER iface,
807 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
808 DirectSoundDevice *device = This->device;
809 TRACE("(%p,%p,%d,%p)\n", iface, lpwf, wfsize, wfwritten);
811 size = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
813 if (lpwf) { /* NULL is valid */
814 if (wfsize >= size) {
815 CopyMemory(lpwf,device->pwfx,size);
819 WARN("invalid parameter: wfsize too small\n");
822 return DSERR_INVALIDPARAM;
826 *wfwritten = sizeof(WAVEFORMATEX) + device->pwfx->cbSize;
828 WARN("invalid parameter: wfwritten == NULL\n");
829 return DSERR_INVALIDPARAM;
836 static HRESULT WINAPI PrimaryBufferImpl_Lock(
837 LPDIRECTSOUNDBUFFER iface,DWORD writecursor,DWORD writebytes,LPVOID *lplpaudioptr1,LPDWORD audiobytes1,LPVOID *lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
840 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
841 DirectSoundDevice *device = This->device;
842 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
855 return DSERR_INVALIDPARAM;
857 if (device->priolevel != DSSCL_WRITEPRIMARY) {
858 WARN("failed priority check!\n");
859 return DSERR_PRIOLEVELNEEDED;
862 /* when this flag is set, writecursor is meaningless and must be calculated */
863 if (flags & DSBLOCK_FROMWRITECURSOR) {
864 /* GetCurrentPosition does too much magic to duplicate here */
865 hres = IDirectSoundBuffer_GetCurrentPosition(iface, NULL, &writecursor);
867 WARN("IDirectSoundBuffer_GetCurrentPosition failed\n");
872 /* when this flag is set, writebytes is meaningless and must be set */
873 if (flags & DSBLOCK_ENTIREBUFFER)
874 writebytes = device->buflen;
876 if (writecursor >= device->buflen) {
877 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
878 writecursor, device->buflen);
879 return DSERR_INVALIDPARAM;
882 if (writebytes > device->buflen) {
883 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
884 writebytes, device->buflen);
885 return DSERR_INVALIDPARAM;
888 if (writecursor+writebytes <= device->buflen) {
889 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
890 *audiobytes1 = writebytes;
892 *(LPBYTE*)lplpaudioptr2 = NULL;
895 TRACE("->%d.0\n",writebytes);
897 *(LPBYTE*)lplpaudioptr1 = device->buffer+writecursor;
898 *audiobytes1 = device->buflen-writecursor;
900 *(LPBYTE*)lplpaudioptr2 = device->buffer;
902 *audiobytes2 = writebytes-(device->buflen-writecursor);
903 TRACE("->%d.%d\n",*audiobytes1,audiobytes2?*audiobytes2:0);
908 static HRESULT WINAPI PrimaryBufferImpl_SetCurrentPosition(
909 LPDIRECTSOUNDBUFFER iface,DWORD newpos
911 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
912 TRACE("(%p,%d)\n",This,newpos);
914 /* You cannot set the position of the primary buffer */
915 WARN("invalid call\n");
916 return DSERR_INVALIDCALL;
919 static HRESULT WINAPI PrimaryBufferImpl_SetPan(
920 LPDIRECTSOUNDBUFFER iface,LONG pan
922 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
923 DirectSoundDevice *device = This->device;
926 TRACE("(%p,%d)\n", iface, pan);
928 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
929 WARN("control unavailable\n");
930 return DSERR_CONTROLUNAVAIL;
933 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
934 WARN("invalid parameter: pan = %d\n", pan);
935 return DSERR_INVALIDPARAM;
939 EnterCriticalSection(&device->mixlock);
941 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
943 LeaveCriticalSection(&device->mixlock);
944 WARN("GetChannelVolume failed: %08x\n", hr);
948 if(device->pwfx->nChannels > 1){
949 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
951 LeaveCriticalSection(&device->mixlock);
952 WARN("GetChannelVolume failed: %08x\n", hr);
958 device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
959 device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
961 DSOUND_AmpFactorToVolPan(&device->volpan);
962 if (pan != device->volpan.lPan) {
963 device->volpan.lPan=pan;
964 DSOUND_RecalcVolPan(&device->volpan);
966 lvol = (float)((DWORD)(device->volpan.dwTotalLeftAmpFactor & 0xFFFF) / (float)0xFFFF);
967 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 0, lvol);
969 LeaveCriticalSection(&device->mixlock);
970 WARN("SetChannelVolume failed: %08x\n", hr);
974 if(device->pwfx->nChannels > 1){
975 rvol = (float)((DWORD)(device->volpan.dwTotalRightAmpFactor & 0xFFFF) / (float)0xFFFF);
976 hr = IAudioStreamVolume_SetChannelVolume(device->volume, 1, rvol);
978 LeaveCriticalSection(&device->mixlock);
979 WARN("SetChannelVolume failed: %08x\n", hr);
985 LeaveCriticalSection(&device->mixlock);
991 static HRESULT WINAPI PrimaryBufferImpl_GetPan(
992 LPDIRECTSOUNDBUFFER iface,LPLONG pan
994 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
995 DirectSoundDevice *device = This->device;
998 TRACE("(%p,%p)\n", iface, pan);
1000 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
1001 WARN("control unavailable\n");
1002 return DSERR_CONTROLUNAVAIL;
1006 WARN("invalid parameter: pan == NULL\n");
1007 return DSERR_INVALIDPARAM;
1010 EnterCriticalSection(&device->mixlock);
1012 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 0, &lvol);
1014 LeaveCriticalSection(&device->mixlock);
1015 WARN("GetChannelVolume failed: %08x\n", hr);
1019 if(device->pwfx->nChannels > 1){
1020 hr = IAudioStreamVolume_GetChannelVolume(device->volume, 1, &rvol);
1022 LeaveCriticalSection(&device->mixlock);
1023 WARN("GetChannelVolume failed: %08x\n", hr);
1029 device->volpan.dwTotalLeftAmpFactor = ((UINT16)(lvol * (DWORD)0xFFFF));
1030 device->volpan.dwTotalRightAmpFactor = ((UINT16)(rvol * (DWORD)0xFFFF));
1032 DSOUND_AmpFactorToVolPan(&device->volpan);
1033 *pan = device->volpan.lPan;
1035 LeaveCriticalSection(&device->mixlock);
1040 static HRESULT WINAPI PrimaryBufferImpl_Unlock(
1041 LPDIRECTSOUNDBUFFER iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
1043 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1044 DirectSoundDevice *device = This->device;
1045 TRACE("(%p,%p,%d,%p,%d)\n", iface, p1, x1, p2, x2);
1047 if (device->priolevel != DSSCL_WRITEPRIMARY) {
1048 WARN("failed priority check!\n");
1049 return DSERR_PRIOLEVELNEEDED;
1052 if((p1 && ((BYTE*)p1 < device->buffer ||
1053 (BYTE*)p1 >= device->buffer + device->buflen)) ||
1054 (p2 && ((BYTE*)p2 < device->buffer ||
1055 (BYTE*)p2 >= device->buffer + device->buflen)))
1056 return DSERR_INVALIDPARAM;
1061 static HRESULT WINAPI PrimaryBufferImpl_Restore(
1062 LPDIRECTSOUNDBUFFER iface
1064 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1065 FIXME("(%p):stub\n",This);
1069 static HRESULT WINAPI PrimaryBufferImpl_GetFrequency(
1070 LPDIRECTSOUNDBUFFER iface,LPDWORD freq
1072 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1073 DirectSoundDevice *device = This->device;
1074 TRACE("(%p,%p)\n", iface, freq);
1077 WARN("invalid parameter: freq == NULL\n");
1078 return DSERR_INVALIDPARAM;
1081 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
1082 WARN("control unavailable\n");
1083 return DSERR_CONTROLUNAVAIL;
1086 *freq = device->pwfx->nSamplesPerSec;
1087 TRACE("-> %d\n", *freq);
1092 static HRESULT WINAPI PrimaryBufferImpl_Initialize(
1093 LPDIRECTSOUNDBUFFER iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd
1095 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1096 WARN("(%p) already initialized\n", This);
1097 return DSERR_ALREADYINITIALIZED;
1100 static HRESULT WINAPI PrimaryBufferImpl_GetCaps(
1101 LPDIRECTSOUNDBUFFER iface,LPDSBCAPS caps
1103 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1104 DirectSoundDevice *device = This->device;
1105 TRACE("(%p,%p)\n", iface, caps);
1108 WARN("invalid parameter: caps == NULL\n");
1109 return DSERR_INVALIDPARAM;
1112 if (caps->dwSize < sizeof(*caps)) {
1113 WARN("invalid parameter: caps->dwSize = %d\n", caps->dwSize);
1114 return DSERR_INVALIDPARAM;
1117 caps->dwFlags = This->dsbd.dwFlags;
1118 caps->dwBufferBytes = device->buflen;
1120 /* Windows reports these as zero */
1121 caps->dwUnlockTransferRate = 0;
1122 caps->dwPlayCpuOverhead = 0;
1127 static HRESULT WINAPI PrimaryBufferImpl_QueryInterface(
1128 LPDIRECTSOUNDBUFFER iface,REFIID riid,LPVOID *ppobj
1130 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer(iface);
1131 DirectSoundDevice *device = This->device;
1132 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(riid), ppobj);
1134 if (ppobj == NULL) {
1135 WARN("invalid parameter\n");
1136 return E_INVALIDARG;
1139 *ppobj = NULL; /* assume failure */
1141 if ( IsEqualGUID(riid, &IID_IUnknown) ||
1142 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ) {
1143 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)This);
1148 /* DirectSoundBuffer and DirectSoundBuffer8 are different and */
1149 /* a primary buffer can't have a DirectSoundBuffer8 interface */
1150 if ( IsEqualGUID( &IID_IDirectSoundBuffer8, riid ) ) {
1151 WARN("app requested DirectSoundBuffer8 on primary buffer\n");
1152 return E_NOINTERFACE;
1155 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
1156 ERR("app requested IDirectSoundNotify on primary buffer\n");
1157 /* FIXME: should we support this? */
1158 return E_NOINTERFACE;
1161 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
1162 ERR("app requested IDirectSound3DBuffer on primary buffer\n");
1163 return E_NOINTERFACE;
1166 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
1167 if (!device->listener)
1168 IDirectSound3DListenerImpl_Create(device, &device->listener);
1169 if (device->listener) {
1170 *ppobj = device->listener;
1171 IDirectSound3DListener_AddRef((LPDIRECTSOUND3DLISTENER)*ppobj);
1175 WARN("IID_IDirectSound3DListener failed\n");
1176 return E_NOINTERFACE;
1179 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
1180 FIXME("app requested IKsPropertySet on primary buffer\n");
1181 return E_NOINTERFACE;
1184 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
1185 return E_NOINTERFACE;
1188 static const IDirectSoundBufferVtbl dspbvt =
1190 PrimaryBufferImpl_QueryInterface,
1191 PrimaryBufferImpl_AddRef,
1192 PrimaryBufferImpl_Release,
1193 PrimaryBufferImpl_GetCaps,
1194 PrimaryBufferImpl_GetCurrentPosition,
1195 PrimaryBufferImpl_GetFormat,
1196 PrimaryBufferImpl_GetVolume,
1197 PrimaryBufferImpl_GetPan,
1198 PrimaryBufferImpl_GetFrequency,
1199 PrimaryBufferImpl_GetStatus,
1200 PrimaryBufferImpl_Initialize,
1201 PrimaryBufferImpl_Lock,
1202 PrimaryBufferImpl_Play,
1203 PrimaryBufferImpl_SetCurrentPosition,
1204 PrimaryBufferImpl_SetFormat,
1205 PrimaryBufferImpl_SetVolume,
1206 PrimaryBufferImpl_SetPan,
1207 PrimaryBufferImpl_SetFrequency,
1208 PrimaryBufferImpl_Stop,
1209 PrimaryBufferImpl_Unlock,
1210 PrimaryBufferImpl_Restore
1213 HRESULT primarybuffer_create(DirectSoundDevice *device, IDirectSoundBufferImpl **ppdsb,
1214 const DSBUFFERDESC *dsbd)
1216 IDirectSoundBufferImpl *dsb;
1217 TRACE("%p,%p,%p)\n",device,ppdsb,dsbd);
1219 if (dsbd->lpwfxFormat) {
1220 WARN("invalid parameter: dsbd->lpwfxFormat != NULL\n");
1222 return DSERR_INVALIDPARAM;
1225 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1228 WARN("out of memory\n");
1230 return DSERR_OUTOFMEMORY;
1235 dsb->device = device;
1236 dsb->IDirectSoundBuffer8_iface.lpVtbl = (IDirectSoundBuffer8Vtbl *)&dspbvt;
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);