Properly initialize with the default distance model
[openal-soft.git] / Alc / backends / dsound.c
blobf730dc95e534210fb48a2a759d87031d97b45109
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <memory.h>
27 #include <dsound.h>
28 #include <cguid.h>
29 #include <mmreg.h>
30 #ifndef _WAVEFORMATEXTENSIBLE_
31 #include <ks.h>
32 #include <ksmedia.h>
33 #endif
35 #include "alMain.h"
36 #include "alu.h"
37 #include "threads.h"
38 #include "compat.h"
39 #include "alstring.h"
41 #include "backends/base.h"
43 #ifndef DSSPEAKER_5POINT1
44 # define DSSPEAKER_5POINT1 0x00000006
45 #endif
46 #ifndef DSSPEAKER_5POINT1_BACK
47 # define DSSPEAKER_5POINT1_BACK 0x00000006
48 #endif
49 #ifndef DSSPEAKER_7POINT1
50 # define DSSPEAKER_7POINT1 0x00000007
51 #endif
52 #ifndef DSSPEAKER_7POINT1_SURROUND
53 # define DSSPEAKER_7POINT1_SURROUND 0x00000008
54 #endif
55 #ifndef DSSPEAKER_5POINT1_SURROUND
56 # define DSSPEAKER_5POINT1_SURROUND 0x00000009
57 #endif
60 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM, 0x00000001, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
61 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, 0x00000003, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
63 #define DEVNAME_HEAD "OpenAL Soft on "
66 #ifdef HAVE_DYNLOAD
67 static void *ds_handle;
68 static HRESULT (WINAPI *pDirectSoundCreate)(const GUID *pcGuidDevice, IDirectSound **ppDS, IUnknown *pUnkOuter);
69 static HRESULT (WINAPI *pDirectSoundEnumerateW)(LPDSENUMCALLBACKW pDSEnumCallback, void *pContext);
70 static HRESULT (WINAPI *pDirectSoundCaptureCreate)(const GUID *pcGuidDevice, IDirectSoundCapture **ppDSC, IUnknown *pUnkOuter);
71 static HRESULT (WINAPI *pDirectSoundCaptureEnumerateW)(LPDSENUMCALLBACKW pDSEnumCallback, void *pContext);
73 #define DirectSoundCreate pDirectSoundCreate
74 #define DirectSoundEnumerateW pDirectSoundEnumerateW
75 #define DirectSoundCaptureCreate pDirectSoundCaptureCreate
76 #define DirectSoundCaptureEnumerateW pDirectSoundCaptureEnumerateW
77 #endif
80 static ALCboolean DSoundLoad(void)
82 #ifdef HAVE_DYNLOAD
83 if(!ds_handle)
85 ds_handle = LoadLib("dsound.dll");
86 if(ds_handle == NULL)
88 ERR("Failed to load dsound.dll\n");
89 return ALC_FALSE;
92 #define LOAD_FUNC(f) do { \
93 p##f = GetSymbol(ds_handle, #f); \
94 if(p##f == NULL) { \
95 CloseLib(ds_handle); \
96 ds_handle = NULL; \
97 return ALC_FALSE; \
98 } \
99 } while(0)
100 LOAD_FUNC(DirectSoundCreate);
101 LOAD_FUNC(DirectSoundEnumerateW);
102 LOAD_FUNC(DirectSoundCaptureCreate);
103 LOAD_FUNC(DirectSoundCaptureEnumerateW);
104 #undef LOAD_FUNC
106 #endif
107 return ALC_TRUE;
111 #define MAX_UPDATES 128
113 typedef struct {
114 al_string name;
115 GUID guid;
116 } DevMap;
117 TYPEDEF_VECTOR(DevMap, vector_DevMap)
119 static vector_DevMap PlaybackDevices;
120 static vector_DevMap CaptureDevices;
122 static void clear_devlist(vector_DevMap *list)
124 #define DEINIT_STR(i) AL_STRING_DEINIT((i)->name)
125 VECTOR_FOR_EACH(DevMap, *list, DEINIT_STR);
126 VECTOR_RESIZE(*list, 0, 0);
127 #undef DEINIT_STR
130 static BOOL CALLBACK DSoundEnumDevices(GUID *guid, const WCHAR *desc, const WCHAR* UNUSED(drvname), void *data)
132 vector_DevMap *devices = data;
133 OLECHAR *guidstr = NULL;
134 DevMap entry;
135 HRESULT hr;
136 int count;
138 if(!guid)
139 return TRUE;
141 AL_STRING_INIT(entry.name);
143 count = 0;
144 while(1)
146 const DevMap *iter;
148 alstr_copy_cstr(&entry.name, DEVNAME_HEAD);
149 alstr_append_wcstr(&entry.name, desc);
150 if(count != 0)
152 char str[64];
153 snprintf(str, sizeof(str), " #%d", count+1);
154 alstr_append_cstr(&entry.name, str);
157 #define MATCH_ENTRY(i) (alstr_cmp(entry.name, (i)->name) == 0)
158 VECTOR_FIND_IF(iter, const DevMap, *devices, MATCH_ENTRY);
159 if(iter == VECTOR_END(*devices)) break;
160 #undef MATCH_ENTRY
161 count++;
163 entry.guid = *guid;
165 hr = StringFromCLSID(guid, &guidstr);
166 if(SUCCEEDED(hr))
168 TRACE("Got device \"%s\", GUID \"%ls\"\n", alstr_get_cstr(entry.name), guidstr);
169 CoTaskMemFree(guidstr);
172 VECTOR_PUSH_BACK(*devices, entry);
174 return TRUE;
178 typedef struct ALCdsoundPlayback {
179 DERIVE_FROM_TYPE(ALCbackend);
181 IDirectSound *DS;
182 IDirectSoundBuffer *PrimaryBuffer;
183 IDirectSoundBuffer *Buffer;
184 IDirectSoundNotify *Notifies;
185 HANDLE NotifyEvent;
187 volatile int killNow;
188 althrd_t thread;
189 } ALCdsoundPlayback;
191 static int ALCdsoundPlayback_mixerProc(void *ptr);
193 static void ALCdsoundPlayback_Construct(ALCdsoundPlayback *self, ALCdevice *device);
194 static DECLARE_FORWARD(ALCdsoundPlayback, ALCbackend, void, Destruct)
195 static ALCenum ALCdsoundPlayback_open(ALCdsoundPlayback *self, const ALCchar *name);
196 static void ALCdsoundPlayback_close(ALCdsoundPlayback *self);
197 static ALCboolean ALCdsoundPlayback_reset(ALCdsoundPlayback *self);
198 static ALCboolean ALCdsoundPlayback_start(ALCdsoundPlayback *self);
199 static void ALCdsoundPlayback_stop(ALCdsoundPlayback *self);
200 static DECLARE_FORWARD2(ALCdsoundPlayback, ALCbackend, ALCenum, captureSamples, void*, ALCuint)
201 static DECLARE_FORWARD(ALCdsoundPlayback, ALCbackend, ALCuint, availableSamples)
202 static DECLARE_FORWARD(ALCdsoundPlayback, ALCbackend, ClockLatency, getClockLatency)
203 static DECLARE_FORWARD(ALCdsoundPlayback, ALCbackend, void, lock)
204 static DECLARE_FORWARD(ALCdsoundPlayback, ALCbackend, void, unlock)
205 DECLARE_DEFAULT_ALLOCATORS(ALCdsoundPlayback)
207 DEFINE_ALCBACKEND_VTABLE(ALCdsoundPlayback);
210 static void ALCdsoundPlayback_Construct(ALCdsoundPlayback *self, ALCdevice *device)
212 ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
213 SET_VTABLE2(ALCdsoundPlayback, ALCbackend, self);
217 FORCE_ALIGN static int ALCdsoundPlayback_mixerProc(void *ptr)
219 ALCdsoundPlayback *self = ptr;
220 ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
221 DSBCAPS DSBCaps;
222 DWORD LastCursor = 0;
223 DWORD PlayCursor;
224 void *WritePtr1, *WritePtr2;
225 DWORD WriteCnt1, WriteCnt2;
226 BOOL Playing = FALSE;
227 DWORD FrameSize;
228 DWORD FragSize;
229 DWORD avail;
230 HRESULT err;
232 SetRTPriority();
233 althrd_setname(althrd_current(), MIXER_THREAD_NAME);
235 memset(&DSBCaps, 0, sizeof(DSBCaps));
236 DSBCaps.dwSize = sizeof(DSBCaps);
237 err = IDirectSoundBuffer_GetCaps(self->Buffer, &DSBCaps);
238 if(FAILED(err))
240 ERR("Failed to get buffer caps: 0x%lx\n", err);
241 ALCdevice_Lock(device);
242 aluHandleDisconnect(device);
243 ALCdevice_Unlock(device);
244 return 1;
247 FrameSize = FrameSizeFromDevFmt(device->FmtChans, device->FmtType, device->AmbiOrder);
248 FragSize = device->UpdateSize * FrameSize;
250 IDirectSoundBuffer_GetCurrentPosition(self->Buffer, &LastCursor, NULL);
251 while(!self->killNow)
253 // Get current play cursor
254 IDirectSoundBuffer_GetCurrentPosition(self->Buffer, &PlayCursor, NULL);
255 avail = (PlayCursor-LastCursor+DSBCaps.dwBufferBytes) % DSBCaps.dwBufferBytes;
257 if(avail < FragSize)
259 if(!Playing)
261 err = IDirectSoundBuffer_Play(self->Buffer, 0, 0, DSBPLAY_LOOPING);
262 if(FAILED(err))
264 ERR("Failed to play buffer: 0x%lx\n", err);
265 ALCdevice_Lock(device);
266 aluHandleDisconnect(device);
267 ALCdevice_Unlock(device);
268 return 1;
270 Playing = TRUE;
273 avail = WaitForSingleObjectEx(self->NotifyEvent, 2000, FALSE);
274 if(avail != WAIT_OBJECT_0)
275 ERR("WaitForSingleObjectEx error: 0x%lx\n", avail);
276 continue;
278 avail -= avail%FragSize;
280 // Lock output buffer
281 WriteCnt1 = 0;
282 WriteCnt2 = 0;
283 err = IDirectSoundBuffer_Lock(self->Buffer, LastCursor, avail, &WritePtr1, &WriteCnt1, &WritePtr2, &WriteCnt2, 0);
285 // If the buffer is lost, restore it and lock
286 if(err == DSERR_BUFFERLOST)
288 WARN("Buffer lost, restoring...\n");
289 err = IDirectSoundBuffer_Restore(self->Buffer);
290 if(SUCCEEDED(err))
292 Playing = FALSE;
293 LastCursor = 0;
294 err = IDirectSoundBuffer_Lock(self->Buffer, 0, DSBCaps.dwBufferBytes, &WritePtr1, &WriteCnt1, &WritePtr2, &WriteCnt2, 0);
298 // Successfully locked the output buffer
299 if(SUCCEEDED(err))
301 // If we have an active context, mix data directly into output buffer otherwise fill with silence
302 ALCdevice_Lock(device);
303 aluMixData(device, WritePtr1, WriteCnt1/FrameSize);
304 aluMixData(device, WritePtr2, WriteCnt2/FrameSize);
305 ALCdevice_Unlock(device);
307 // Unlock output buffer only when successfully locked
308 IDirectSoundBuffer_Unlock(self->Buffer, WritePtr1, WriteCnt1, WritePtr2, WriteCnt2);
310 else
312 ERR("Buffer lock error: %#lx\n", err);
313 ALCdevice_Lock(device);
314 aluHandleDisconnect(device);
315 ALCdevice_Unlock(device);
316 return 1;
319 // Update old write cursor location
320 LastCursor += WriteCnt1+WriteCnt2;
321 LastCursor %= DSBCaps.dwBufferBytes;
324 return 0;
327 static ALCenum ALCdsoundPlayback_open(ALCdsoundPlayback *self, const ALCchar *deviceName)
329 ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
330 const GUID *guid = NULL;
331 HRESULT hr, hrcom;
333 if(VECTOR_SIZE(PlaybackDevices) == 0)
335 /* Initialize COM to prevent name truncation */
336 hrcom = CoInitialize(NULL);
337 hr = DirectSoundEnumerateW(DSoundEnumDevices, &PlaybackDevices);
338 if(FAILED(hr))
339 ERR("Error enumerating DirectSound devices (0x%lx)!\n", hr);
340 if(SUCCEEDED(hrcom))
341 CoUninitialize();
344 if(!deviceName && VECTOR_SIZE(PlaybackDevices) > 0)
346 deviceName = alstr_get_cstr(VECTOR_FRONT(PlaybackDevices).name);
347 guid = &VECTOR_FRONT(PlaybackDevices).guid;
349 else
351 const DevMap *iter;
353 #define MATCH_NAME(i) (alstr_cmp_cstr((i)->name, deviceName) == 0)
354 VECTOR_FIND_IF(iter, const DevMap, PlaybackDevices, MATCH_NAME);
355 #undef MATCH_NAME
356 if(iter == VECTOR_END(PlaybackDevices))
357 return ALC_INVALID_VALUE;
358 guid = &iter->guid;
361 hr = DS_OK;
362 self->NotifyEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
363 if(self->NotifyEvent == NULL)
364 hr = E_FAIL;
366 //DirectSound Init code
367 if(SUCCEEDED(hr))
368 hr = DirectSoundCreate(guid, &self->DS, NULL);
369 if(SUCCEEDED(hr))
370 hr = IDirectSound_SetCooperativeLevel(self->DS, GetForegroundWindow(), DSSCL_PRIORITY);
371 if(FAILED(hr))
373 if(self->DS)
374 IDirectSound_Release(self->DS);
375 self->DS = NULL;
376 if(self->NotifyEvent)
377 CloseHandle(self->NotifyEvent);
378 self->NotifyEvent = NULL;
380 ERR("Device init failed: 0x%08lx\n", hr);
381 return ALC_INVALID_VALUE;
384 alstr_copy_cstr(&device->DeviceName, deviceName);
386 return ALC_NO_ERROR;
389 static void ALCdsoundPlayback_close(ALCdsoundPlayback *self)
391 if(self->Notifies)
392 IDirectSoundNotify_Release(self->Notifies);
393 self->Notifies = NULL;
394 if(self->Buffer)
395 IDirectSoundBuffer_Release(self->Buffer);
396 self->Buffer = NULL;
397 if(self->PrimaryBuffer != NULL)
398 IDirectSoundBuffer_Release(self->PrimaryBuffer);
399 self->PrimaryBuffer = NULL;
401 IDirectSound_Release(self->DS);
402 self->DS = NULL;
403 CloseHandle(self->NotifyEvent);
404 self->NotifyEvent = NULL;
407 static ALCboolean ALCdsoundPlayback_reset(ALCdsoundPlayback *self)
409 ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
410 DSBUFFERDESC DSBDescription;
411 WAVEFORMATEXTENSIBLE OutputType;
412 DWORD speakers;
413 HRESULT hr;
415 memset(&OutputType, 0, sizeof(OutputType));
417 if(self->Notifies)
418 IDirectSoundNotify_Release(self->Notifies);
419 self->Notifies = NULL;
420 if(self->Buffer)
421 IDirectSoundBuffer_Release(self->Buffer);
422 self->Buffer = NULL;
423 if(self->PrimaryBuffer != NULL)
424 IDirectSoundBuffer_Release(self->PrimaryBuffer);
425 self->PrimaryBuffer = NULL;
427 switch(device->FmtType)
429 case DevFmtByte:
430 device->FmtType = DevFmtUByte;
431 break;
432 case DevFmtFloat:
433 if((device->Flags&DEVICE_SAMPLE_TYPE_REQUEST))
434 break;
435 /* fall-through */
436 case DevFmtUShort:
437 device->FmtType = DevFmtShort;
438 break;
439 case DevFmtUInt:
440 device->FmtType = DevFmtInt;
441 break;
442 case DevFmtUByte:
443 case DevFmtShort:
444 case DevFmtInt:
445 break;
448 hr = IDirectSound_GetSpeakerConfig(self->DS, &speakers);
449 if(SUCCEEDED(hr))
451 speakers = DSSPEAKER_CONFIG(speakers);
452 if(!(device->Flags&DEVICE_CHANNELS_REQUEST))
454 if(speakers == DSSPEAKER_MONO)
455 device->FmtChans = DevFmtMono;
456 else if(speakers == DSSPEAKER_STEREO || speakers == DSSPEAKER_HEADPHONE)
457 device->FmtChans = DevFmtStereo;
458 else if(speakers == DSSPEAKER_QUAD)
459 device->FmtChans = DevFmtQuad;
460 else if(speakers == DSSPEAKER_5POINT1_SURROUND)
461 device->FmtChans = DevFmtX51;
462 else if(speakers == DSSPEAKER_5POINT1_BACK)
463 device->FmtChans = DevFmtX51Rear;
464 else if(speakers == DSSPEAKER_7POINT1 || speakers == DSSPEAKER_7POINT1_SURROUND)
465 device->FmtChans = DevFmtX71;
466 else
467 ERR("Unknown system speaker config: 0x%lx\n", speakers);
469 device->IsHeadphones = (device->FmtChans == DevFmtStereo &&
470 speakers == DSSPEAKER_HEADPHONE);
472 switch(device->FmtChans)
474 case DevFmtMono:
475 OutputType.dwChannelMask = SPEAKER_FRONT_CENTER;
476 break;
477 case DevFmtAmbi3D:
478 device->FmtChans = DevFmtStereo;
479 /*fall-through*/
480 case DevFmtStereo:
481 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
482 SPEAKER_FRONT_RIGHT;
483 break;
484 case DevFmtQuad:
485 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
486 SPEAKER_FRONT_RIGHT |
487 SPEAKER_BACK_LEFT |
488 SPEAKER_BACK_RIGHT;
489 break;
490 case DevFmtX51:
491 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
492 SPEAKER_FRONT_RIGHT |
493 SPEAKER_FRONT_CENTER |
494 SPEAKER_LOW_FREQUENCY |
495 SPEAKER_SIDE_LEFT |
496 SPEAKER_SIDE_RIGHT;
497 break;
498 case DevFmtX51Rear:
499 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
500 SPEAKER_FRONT_RIGHT |
501 SPEAKER_FRONT_CENTER |
502 SPEAKER_LOW_FREQUENCY |
503 SPEAKER_BACK_LEFT |
504 SPEAKER_BACK_RIGHT;
505 break;
506 case DevFmtX61:
507 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
508 SPEAKER_FRONT_RIGHT |
509 SPEAKER_FRONT_CENTER |
510 SPEAKER_LOW_FREQUENCY |
511 SPEAKER_BACK_CENTER |
512 SPEAKER_SIDE_LEFT |
513 SPEAKER_SIDE_RIGHT;
514 break;
515 case DevFmtX71:
516 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
517 SPEAKER_FRONT_RIGHT |
518 SPEAKER_FRONT_CENTER |
519 SPEAKER_LOW_FREQUENCY |
520 SPEAKER_BACK_LEFT |
521 SPEAKER_BACK_RIGHT |
522 SPEAKER_SIDE_LEFT |
523 SPEAKER_SIDE_RIGHT;
524 break;
527 retry_open:
528 hr = S_OK;
529 OutputType.Format.wFormatTag = WAVE_FORMAT_PCM;
530 OutputType.Format.nChannels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
531 OutputType.Format.wBitsPerSample = BytesFromDevFmt(device->FmtType) * 8;
532 OutputType.Format.nBlockAlign = OutputType.Format.nChannels*OutputType.Format.wBitsPerSample/8;
533 OutputType.Format.nSamplesPerSec = device->Frequency;
534 OutputType.Format.nAvgBytesPerSec = OutputType.Format.nSamplesPerSec*OutputType.Format.nBlockAlign;
535 OutputType.Format.cbSize = 0;
538 if(OutputType.Format.nChannels > 2 || device->FmtType == DevFmtFloat)
540 OutputType.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
541 OutputType.Samples.wValidBitsPerSample = OutputType.Format.wBitsPerSample;
542 OutputType.Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
543 if(device->FmtType == DevFmtFloat)
544 OutputType.SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
545 else
546 OutputType.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
548 if(self->PrimaryBuffer)
549 IDirectSoundBuffer_Release(self->PrimaryBuffer);
550 self->PrimaryBuffer = NULL;
552 else
554 if(SUCCEEDED(hr) && !self->PrimaryBuffer)
556 memset(&DSBDescription,0,sizeof(DSBUFFERDESC));
557 DSBDescription.dwSize=sizeof(DSBUFFERDESC);
558 DSBDescription.dwFlags=DSBCAPS_PRIMARYBUFFER;
559 hr = IDirectSound_CreateSoundBuffer(self->DS, &DSBDescription, &self->PrimaryBuffer, NULL);
561 if(SUCCEEDED(hr))
562 hr = IDirectSoundBuffer_SetFormat(self->PrimaryBuffer,&OutputType.Format);
565 if(SUCCEEDED(hr))
567 if(device->NumUpdates > MAX_UPDATES)
569 device->UpdateSize = (device->UpdateSize*device->NumUpdates +
570 MAX_UPDATES-1) / MAX_UPDATES;
571 device->NumUpdates = MAX_UPDATES;
574 memset(&DSBDescription,0,sizeof(DSBUFFERDESC));
575 DSBDescription.dwSize=sizeof(DSBUFFERDESC);
576 DSBDescription.dwFlags=DSBCAPS_CTRLPOSITIONNOTIFY|DSBCAPS_GETCURRENTPOSITION2|DSBCAPS_GLOBALFOCUS;
577 DSBDescription.dwBufferBytes=device->UpdateSize * device->NumUpdates *
578 OutputType.Format.nBlockAlign;
579 DSBDescription.lpwfxFormat=&OutputType.Format;
580 hr = IDirectSound_CreateSoundBuffer(self->DS, &DSBDescription, &self->Buffer, NULL);
581 if(FAILED(hr) && device->FmtType == DevFmtFloat)
583 device->FmtType = DevFmtShort;
584 goto retry_open;
588 if(SUCCEEDED(hr))
590 hr = IDirectSoundBuffer_QueryInterface(self->Buffer, &IID_IDirectSoundNotify, (void**)&self->Notifies);
591 if(SUCCEEDED(hr))
593 DSBPOSITIONNOTIFY notifies[MAX_UPDATES];
594 ALuint i;
596 for(i = 0;i < device->NumUpdates;++i)
598 notifies[i].dwOffset = i * device->UpdateSize *
599 OutputType.Format.nBlockAlign;
600 notifies[i].hEventNotify = self->NotifyEvent;
602 if(IDirectSoundNotify_SetNotificationPositions(self->Notifies, device->NumUpdates, notifies) != DS_OK)
603 hr = E_FAIL;
607 if(FAILED(hr))
609 if(self->Notifies != NULL)
610 IDirectSoundNotify_Release(self->Notifies);
611 self->Notifies = NULL;
612 if(self->Buffer != NULL)
613 IDirectSoundBuffer_Release(self->Buffer);
614 self->Buffer = NULL;
615 if(self->PrimaryBuffer != NULL)
616 IDirectSoundBuffer_Release(self->PrimaryBuffer);
617 self->PrimaryBuffer = NULL;
618 return ALC_FALSE;
621 ResetEvent(self->NotifyEvent);
622 SetDefaultWFXChannelOrder(device);
624 return ALC_TRUE;
627 static ALCboolean ALCdsoundPlayback_start(ALCdsoundPlayback *self)
629 self->killNow = 0;
630 if(althrd_create(&self->thread, ALCdsoundPlayback_mixerProc, self) != althrd_success)
631 return ALC_FALSE;
633 return ALC_TRUE;
636 static void ALCdsoundPlayback_stop(ALCdsoundPlayback *self)
638 int res;
640 if(self->killNow)
641 return;
643 self->killNow = 1;
644 althrd_join(self->thread, &res);
646 IDirectSoundBuffer_Stop(self->Buffer);
651 typedef struct ALCdsoundCapture {
652 DERIVE_FROM_TYPE(ALCbackend);
654 IDirectSoundCapture *DSC;
655 IDirectSoundCaptureBuffer *DSCbuffer;
656 DWORD BufferBytes;
657 DWORD Cursor;
659 ll_ringbuffer_t *Ring;
660 } ALCdsoundCapture;
662 static void ALCdsoundCapture_Construct(ALCdsoundCapture *self, ALCdevice *device);
663 static DECLARE_FORWARD(ALCdsoundCapture, ALCbackend, void, Destruct)
664 static ALCenum ALCdsoundCapture_open(ALCdsoundCapture *self, const ALCchar *name);
665 static void ALCdsoundCapture_close(ALCdsoundCapture *self);
666 static DECLARE_FORWARD(ALCdsoundCapture, ALCbackend, ALCboolean, reset)
667 static ALCboolean ALCdsoundCapture_start(ALCdsoundCapture *self);
668 static void ALCdsoundCapture_stop(ALCdsoundCapture *self);
669 static ALCenum ALCdsoundCapture_captureSamples(ALCdsoundCapture *self, ALCvoid *buffer, ALCuint samples);
670 static ALCuint ALCdsoundCapture_availableSamples(ALCdsoundCapture *self);
671 static DECLARE_FORWARD(ALCdsoundCapture, ALCbackend, ClockLatency, getClockLatency)
672 static DECLARE_FORWARD(ALCdsoundCapture, ALCbackend, void, lock)
673 static DECLARE_FORWARD(ALCdsoundCapture, ALCbackend, void, unlock)
674 DECLARE_DEFAULT_ALLOCATORS(ALCdsoundCapture)
676 DEFINE_ALCBACKEND_VTABLE(ALCdsoundCapture);
678 static void ALCdsoundCapture_Construct(ALCdsoundCapture *self, ALCdevice *device)
680 ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
681 SET_VTABLE2(ALCdsoundCapture, ALCbackend, self);
685 static ALCenum ALCdsoundCapture_open(ALCdsoundCapture *self, const ALCchar *deviceName)
687 ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
688 WAVEFORMATEXTENSIBLE InputType;
689 DSCBUFFERDESC DSCBDescription;
690 const GUID *guid = NULL;
691 HRESULT hr, hrcom;
692 ALuint samples;
694 if(VECTOR_SIZE(CaptureDevices) == 0)
696 /* Initialize COM to prevent name truncation */
697 hrcom = CoInitialize(NULL);
698 hr = DirectSoundCaptureEnumerateW(DSoundEnumDevices, &CaptureDevices);
699 if(FAILED(hr))
700 ERR("Error enumerating DirectSound devices (0x%lx)!\n", hr);
701 if(SUCCEEDED(hrcom))
702 CoUninitialize();
705 if(!deviceName && VECTOR_SIZE(CaptureDevices) > 0)
707 deviceName = alstr_get_cstr(VECTOR_FRONT(CaptureDevices).name);
708 guid = &VECTOR_FRONT(CaptureDevices).guid;
710 else
712 const DevMap *iter;
714 #define MATCH_NAME(i) (alstr_cmp_cstr((i)->name, deviceName) == 0)
715 VECTOR_FIND_IF(iter, const DevMap, CaptureDevices, MATCH_NAME);
716 #undef MATCH_NAME
717 if(iter == VECTOR_END(CaptureDevices))
718 return ALC_INVALID_VALUE;
719 guid = &iter->guid;
722 switch(device->FmtType)
724 case DevFmtByte:
725 case DevFmtUShort:
726 case DevFmtUInt:
727 WARN("%s capture samples not supported\n", DevFmtTypeString(device->FmtType));
728 return ALC_INVALID_ENUM;
730 case DevFmtUByte:
731 case DevFmtShort:
732 case DevFmtInt:
733 case DevFmtFloat:
734 break;
737 memset(&InputType, 0, sizeof(InputType));
738 switch(device->FmtChans)
740 case DevFmtMono:
741 InputType.dwChannelMask = SPEAKER_FRONT_CENTER;
742 break;
743 case DevFmtStereo:
744 InputType.dwChannelMask = SPEAKER_FRONT_LEFT |
745 SPEAKER_FRONT_RIGHT;
746 break;
747 case DevFmtQuad:
748 InputType.dwChannelMask = SPEAKER_FRONT_LEFT |
749 SPEAKER_FRONT_RIGHT |
750 SPEAKER_BACK_LEFT |
751 SPEAKER_BACK_RIGHT;
752 break;
753 case DevFmtX51:
754 InputType.dwChannelMask = SPEAKER_FRONT_LEFT |
755 SPEAKER_FRONT_RIGHT |
756 SPEAKER_FRONT_CENTER |
757 SPEAKER_LOW_FREQUENCY |
758 SPEAKER_SIDE_LEFT |
759 SPEAKER_SIDE_RIGHT;
760 break;
761 case DevFmtX51Rear:
762 InputType.dwChannelMask = SPEAKER_FRONT_LEFT |
763 SPEAKER_FRONT_RIGHT |
764 SPEAKER_FRONT_CENTER |
765 SPEAKER_LOW_FREQUENCY |
766 SPEAKER_BACK_LEFT |
767 SPEAKER_BACK_RIGHT;
768 break;
769 case DevFmtX61:
770 InputType.dwChannelMask = SPEAKER_FRONT_LEFT |
771 SPEAKER_FRONT_RIGHT |
772 SPEAKER_FRONT_CENTER |
773 SPEAKER_LOW_FREQUENCY |
774 SPEAKER_BACK_CENTER |
775 SPEAKER_SIDE_LEFT |
776 SPEAKER_SIDE_RIGHT;
777 break;
778 case DevFmtX71:
779 InputType.dwChannelMask = SPEAKER_FRONT_LEFT |
780 SPEAKER_FRONT_RIGHT |
781 SPEAKER_FRONT_CENTER |
782 SPEAKER_LOW_FREQUENCY |
783 SPEAKER_BACK_LEFT |
784 SPEAKER_BACK_RIGHT |
785 SPEAKER_SIDE_LEFT |
786 SPEAKER_SIDE_RIGHT;
787 break;
788 case DevFmtAmbi3D:
789 WARN("%s capture not supported\n", DevFmtChannelsString(device->FmtChans));
790 return ALC_INVALID_ENUM;
793 InputType.Format.wFormatTag = WAVE_FORMAT_PCM;
794 InputType.Format.nChannels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
795 InputType.Format.wBitsPerSample = BytesFromDevFmt(device->FmtType) * 8;
796 InputType.Format.nBlockAlign = InputType.Format.nChannels*InputType.Format.wBitsPerSample/8;
797 InputType.Format.nSamplesPerSec = device->Frequency;
798 InputType.Format.nAvgBytesPerSec = InputType.Format.nSamplesPerSec*InputType.Format.nBlockAlign;
799 InputType.Format.cbSize = 0;
800 InputType.Samples.wValidBitsPerSample = InputType.Format.wBitsPerSample;
801 if(device->FmtType == DevFmtFloat)
802 InputType.SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
803 else
804 InputType.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
806 if(InputType.Format.nChannels > 2 || device->FmtType == DevFmtFloat)
808 InputType.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
809 InputType.Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
812 samples = device->UpdateSize * device->NumUpdates;
813 samples = maxu(samples, 100 * device->Frequency / 1000);
815 memset(&DSCBDescription, 0, sizeof(DSCBUFFERDESC));
816 DSCBDescription.dwSize = sizeof(DSCBUFFERDESC);
817 DSCBDescription.dwFlags = 0;
818 DSCBDescription.dwBufferBytes = samples * InputType.Format.nBlockAlign;
819 DSCBDescription.lpwfxFormat = &InputType.Format;
821 //DirectSoundCapture Init code
822 hr = DirectSoundCaptureCreate(guid, &self->DSC, NULL);
823 if(SUCCEEDED(hr))
824 hr = IDirectSoundCapture_CreateCaptureBuffer(self->DSC, &DSCBDescription, &self->DSCbuffer, NULL);
825 if(SUCCEEDED(hr))
827 self->Ring = ll_ringbuffer_create(device->UpdateSize*device->NumUpdates + 1,
828 InputType.Format.nBlockAlign);
829 if(self->Ring == NULL)
830 hr = DSERR_OUTOFMEMORY;
833 if(FAILED(hr))
835 ERR("Device init failed: 0x%08lx\n", hr);
837 ll_ringbuffer_free(self->Ring);
838 self->Ring = NULL;
839 if(self->DSCbuffer != NULL)
840 IDirectSoundCaptureBuffer_Release(self->DSCbuffer);
841 self->DSCbuffer = NULL;
842 if(self->DSC)
843 IDirectSoundCapture_Release(self->DSC);
844 self->DSC = NULL;
846 return ALC_INVALID_VALUE;
849 self->BufferBytes = DSCBDescription.dwBufferBytes;
850 SetDefaultWFXChannelOrder(device);
852 alstr_copy_cstr(&device->DeviceName, deviceName);
854 return ALC_NO_ERROR;
857 static void ALCdsoundCapture_close(ALCdsoundCapture *self)
859 ll_ringbuffer_free(self->Ring);
860 self->Ring = NULL;
862 if(self->DSCbuffer != NULL)
864 IDirectSoundCaptureBuffer_Stop(self->DSCbuffer);
865 IDirectSoundCaptureBuffer_Release(self->DSCbuffer);
866 self->DSCbuffer = NULL;
869 IDirectSoundCapture_Release(self->DSC);
870 self->DSC = NULL;
873 static ALCboolean ALCdsoundCapture_start(ALCdsoundCapture *self)
875 HRESULT hr;
877 hr = IDirectSoundCaptureBuffer_Start(self->DSCbuffer, DSCBSTART_LOOPING);
878 if(FAILED(hr))
880 ERR("start failed: 0x%08lx\n", hr);
881 aluHandleDisconnect(STATIC_CAST(ALCbackend, self)->mDevice);
882 return ALC_FALSE;
885 return ALC_TRUE;
888 static void ALCdsoundCapture_stop(ALCdsoundCapture *self)
890 HRESULT hr;
892 hr = IDirectSoundCaptureBuffer_Stop(self->DSCbuffer);
893 if(FAILED(hr))
895 ERR("stop failed: 0x%08lx\n", hr);
896 aluHandleDisconnect(STATIC_CAST(ALCbackend, self)->mDevice);
900 static ALCenum ALCdsoundCapture_captureSamples(ALCdsoundCapture *self, ALCvoid *buffer, ALCuint samples)
902 ll_ringbuffer_read(self->Ring, buffer, samples);
903 return ALC_NO_ERROR;
906 static ALCuint ALCdsoundCapture_availableSamples(ALCdsoundCapture *self)
908 ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
909 DWORD ReadCursor, LastCursor, BufferBytes, NumBytes;
910 void *ReadPtr1, *ReadPtr2;
911 DWORD ReadCnt1, ReadCnt2;
912 DWORD FrameSize;
913 HRESULT hr;
915 if(!device->Connected)
916 goto done;
918 FrameSize = FrameSizeFromDevFmt(device->FmtChans, device->FmtType, device->AmbiOrder);
919 BufferBytes = self->BufferBytes;
920 LastCursor = self->Cursor;
922 hr = IDirectSoundCaptureBuffer_GetCurrentPosition(self->DSCbuffer, NULL, &ReadCursor);
923 if(SUCCEEDED(hr))
925 NumBytes = (ReadCursor-LastCursor + BufferBytes) % BufferBytes;
926 if(NumBytes == 0)
927 goto done;
928 hr = IDirectSoundCaptureBuffer_Lock(self->DSCbuffer, LastCursor, NumBytes,
929 &ReadPtr1, &ReadCnt1,
930 &ReadPtr2, &ReadCnt2, 0);
932 if(SUCCEEDED(hr))
934 ll_ringbuffer_write(self->Ring, ReadPtr1, ReadCnt1/FrameSize);
935 if(ReadPtr2 != NULL)
936 ll_ringbuffer_write(self->Ring, ReadPtr2, ReadCnt2/FrameSize);
937 hr = IDirectSoundCaptureBuffer_Unlock(self->DSCbuffer,
938 ReadPtr1, ReadCnt1,
939 ReadPtr2, ReadCnt2);
940 self->Cursor = (LastCursor+ReadCnt1+ReadCnt2) % BufferBytes;
943 if(FAILED(hr))
945 ERR("update failed: 0x%08lx\n", hr);
946 aluHandleDisconnect(device);
949 done:
950 return ll_ringbuffer_read_space(self->Ring);
954 static inline void AppendAllDevicesList2(const DevMap *entry)
955 { AppendAllDevicesList(alstr_get_cstr(entry->name)); }
956 static inline void AppendCaptureDeviceList2(const DevMap *entry)
957 { AppendCaptureDeviceList(alstr_get_cstr(entry->name)); }
959 typedef struct ALCdsoundBackendFactory {
960 DERIVE_FROM_TYPE(ALCbackendFactory);
961 } ALCdsoundBackendFactory;
962 #define ALCDSOUNDBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCdsoundBackendFactory, ALCbackendFactory) } }
964 ALCbackendFactory *ALCdsoundBackendFactory_getFactory(void);
966 static ALCboolean ALCdsoundBackendFactory_init(ALCdsoundBackendFactory *self);
967 static void ALCdsoundBackendFactory_deinit(ALCdsoundBackendFactory *self);
968 static ALCboolean ALCdsoundBackendFactory_querySupport(ALCdsoundBackendFactory *self, ALCbackend_Type type);
969 static void ALCdsoundBackendFactory_probe(ALCdsoundBackendFactory *self, enum DevProbe type);
970 static ALCbackend* ALCdsoundBackendFactory_createBackend(ALCdsoundBackendFactory *self, ALCdevice *device, ALCbackend_Type type);
971 DEFINE_ALCBACKENDFACTORY_VTABLE(ALCdsoundBackendFactory);
974 ALCbackendFactory *ALCdsoundBackendFactory_getFactory(void)
976 static ALCdsoundBackendFactory factory = ALCDSOUNDBACKENDFACTORY_INITIALIZER;
977 return STATIC_CAST(ALCbackendFactory, &factory);
981 static ALCboolean ALCdsoundBackendFactory_init(ALCdsoundBackendFactory* UNUSED(self))
983 VECTOR_INIT(PlaybackDevices);
984 VECTOR_INIT(CaptureDevices);
986 if(!DSoundLoad())
987 return ALC_FALSE;
988 return ALC_TRUE;
991 static void ALCdsoundBackendFactory_deinit(ALCdsoundBackendFactory* UNUSED(self))
993 clear_devlist(&PlaybackDevices);
994 VECTOR_DEINIT(PlaybackDevices);
996 clear_devlist(&CaptureDevices);
997 VECTOR_DEINIT(CaptureDevices);
999 #ifdef HAVE_DYNLOAD
1000 if(ds_handle)
1001 CloseLib(ds_handle);
1002 ds_handle = NULL;
1003 #endif
1006 static ALCboolean ALCdsoundBackendFactory_querySupport(ALCdsoundBackendFactory* UNUSED(self), ALCbackend_Type type)
1008 if(type == ALCbackend_Playback || type == ALCbackend_Capture)
1009 return ALC_TRUE;
1010 return ALC_FALSE;
1013 static void ALCdsoundBackendFactory_probe(ALCdsoundBackendFactory* UNUSED(self), enum DevProbe type)
1015 HRESULT hr, hrcom;
1017 /* Initialize COM to prevent name truncation */
1018 hrcom = CoInitialize(NULL);
1019 switch(type)
1021 case ALL_DEVICE_PROBE:
1022 clear_devlist(&PlaybackDevices);
1023 hr = DirectSoundEnumerateW(DSoundEnumDevices, &PlaybackDevices);
1024 if(FAILED(hr))
1025 ERR("Error enumerating DirectSound playback devices (0x%lx)!\n", hr);
1026 VECTOR_FOR_EACH(const DevMap, PlaybackDevices, AppendAllDevicesList2);
1027 break;
1029 case CAPTURE_DEVICE_PROBE:
1030 clear_devlist(&CaptureDevices);
1031 hr = DirectSoundCaptureEnumerateW(DSoundEnumDevices, &CaptureDevices);
1032 if(FAILED(hr))
1033 ERR("Error enumerating DirectSound capture devices (0x%lx)!\n", hr);
1034 VECTOR_FOR_EACH(const DevMap, CaptureDevices, AppendCaptureDeviceList2);
1035 break;
1037 if(SUCCEEDED(hrcom))
1038 CoUninitialize();
1041 static ALCbackend* ALCdsoundBackendFactory_createBackend(ALCdsoundBackendFactory* UNUSED(self), ALCdevice *device, ALCbackend_Type type)
1043 if(type == ALCbackend_Playback)
1045 ALCdsoundPlayback *backend;
1046 NEW_OBJ(backend, ALCdsoundPlayback)(device);
1047 if(!backend) return NULL;
1048 return STATIC_CAST(ALCbackend, backend);
1051 if(type == ALCbackend_Capture)
1053 ALCdsoundCapture *backend;
1054 NEW_OBJ(backend, ALCdsoundCapture)(device);
1055 if(!backend) return NULL;
1056 return STATIC_CAST(ALCbackend, backend);
1059 return NULL;