Document the different filter types, and combine some split lines
[openal-soft.git] / Alc / backends / dsound.c
blob3ca398ed9adb254753c4f393b7b35780cc5d9d94
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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, 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_7POINT1
47 # define DSSPEAKER_7POINT1 0x00000007
48 #endif
49 #ifndef DSSPEAKER_7POINT1_SURROUND
50 # define DSSPEAKER_7POINT1_SURROUND 0x00000008
51 #endif
52 #ifndef DSSPEAKER_5POINT1_SURROUND
53 # define DSSPEAKER_5POINT1_SURROUND 0x00000009
54 #endif
57 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM, 0x00000001, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
58 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, 0x00000003, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
61 #ifdef HAVE_DYNLOAD
62 static void *ds_handle;
63 static HRESULT (WINAPI *pDirectSoundCreate)(const GUID *pcGuidDevice, IDirectSound **ppDS, IUnknown *pUnkOuter);
64 static HRESULT (WINAPI *pDirectSoundEnumerateW)(LPDSENUMCALLBACKW pDSEnumCallback, void *pContext);
65 static HRESULT (WINAPI *pDirectSoundCaptureCreate)(const GUID *pcGuidDevice, IDirectSoundCapture **ppDSC, IUnknown *pUnkOuter);
66 static HRESULT (WINAPI *pDirectSoundCaptureEnumerateW)(LPDSENUMCALLBACKW pDSEnumCallback, void *pContext);
68 #define DirectSoundCreate pDirectSoundCreate
69 #define DirectSoundEnumerateW pDirectSoundEnumerateW
70 #define DirectSoundCaptureCreate pDirectSoundCaptureCreate
71 #define DirectSoundCaptureEnumerateW pDirectSoundCaptureEnumerateW
72 #endif
75 static ALCboolean DSoundLoad(void)
77 #ifdef HAVE_DYNLOAD
78 if(!ds_handle)
80 ds_handle = LoadLib("dsound.dll");
81 if(ds_handle == NULL)
83 ERR("Failed to load dsound.dll\n");
84 return ALC_FALSE;
87 #define LOAD_FUNC(f) do { \
88 p##f = GetSymbol(ds_handle, #f); \
89 if(p##f == NULL) { \
90 CloseLib(ds_handle); \
91 ds_handle = NULL; \
92 return ALC_FALSE; \
93 } \
94 } while(0)
95 LOAD_FUNC(DirectSoundCreate);
96 LOAD_FUNC(DirectSoundEnumerateW);
97 LOAD_FUNC(DirectSoundCaptureCreate);
98 LOAD_FUNC(DirectSoundCaptureEnumerateW);
99 #undef LOAD_FUNC
101 #endif
102 return ALC_TRUE;
106 #define MAX_UPDATES 128
108 typedef struct {
109 al_string name;
110 GUID guid;
111 } DevMap;
112 DECL_VECTOR(DevMap)
114 vector_DevMap PlaybackDevices;
115 vector_DevMap CaptureDevices;
117 static void clear_devlist(vector_DevMap *list)
119 DevMap *iter, *end;
121 iter = VECTOR_ITER_BEGIN(*list);
122 end = VECTOR_ITER_END(*list);
123 for(;iter != end;++iter)
124 AL_STRING_DEINIT(iter->name);
125 VECTOR_RESIZE(*list, 0);
128 static BOOL CALLBACK DSoundEnumDevices(GUID *guid, const WCHAR *desc, const WCHAR* UNUSED(drvname), void *data)
130 vector_DevMap *devices = data;
131 OLECHAR *guidstr = NULL;
132 DevMap *iter, *end;
133 DevMap entry;
134 HRESULT hr;
135 int count;
137 if(!guid)
138 return TRUE;
140 AL_STRING_INIT(entry.name);
142 count = 0;
143 do {
144 al_string_copy_wcstr(&entry.name, desc);
145 if(count != 0)
147 char str[64];
148 snprintf(str, sizeof(str), " #%d", count+1);
149 al_string_append_cstr(&entry.name, str);
151 count++;
153 iter = VECTOR_ITER_BEGIN(*devices);
154 end = VECTOR_ITER_END(*devices);
155 for(;iter != end;++iter)
157 if(al_string_cmp(entry.name, iter->name) == 0)
158 break;
160 } while(iter != end);
161 entry.guid = *guid;
163 hr = StringFromCLSID(guid, &guidstr);
164 if(SUCCEEDED(hr))
166 TRACE("Got device \"%s\", GUID \"%ls\"\n", al_string_get_cstr(entry.name), guidstr);
167 CoTaskMemFree(guidstr);
170 VECTOR_PUSH_BACK(*devices, entry);
172 return TRUE;
176 typedef struct ALCdsoundPlayback {
177 DERIVE_FROM_TYPE(ALCbackend);
179 IDirectSound *DS;
180 IDirectSoundBuffer *PrimaryBuffer;
181 IDirectSoundBuffer *Buffer;
182 IDirectSoundNotify *Notifies;
183 HANDLE NotifyEvent;
185 volatile int killNow;
186 althrd_t thread;
187 } ALCdsoundPlayback;
189 static int ALCdsoundPlayback_mixerProc(void *ptr);
191 static void ALCdsoundPlayback_Construct(ALCdsoundPlayback *self, ALCdevice *device);
192 static DECLARE_FORWARD(ALCdsoundPlayback, ALCbackend, void, Destruct)
193 static ALCenum ALCdsoundPlayback_open(ALCdsoundPlayback *self, const ALCchar *name);
194 static void ALCdsoundPlayback_close(ALCdsoundPlayback *self);
195 static ALCboolean ALCdsoundPlayback_reset(ALCdsoundPlayback *self);
196 static ALCboolean ALCdsoundPlayback_start(ALCdsoundPlayback *self);
197 static void ALCdsoundPlayback_stop(ALCdsoundPlayback *self);
198 static DECLARE_FORWARD2(ALCdsoundPlayback, ALCbackend, ALCenum, captureSamples, void*, ALCuint)
199 static DECLARE_FORWARD(ALCdsoundPlayback, ALCbackend, ALCuint, availableSamples)
200 static DECLARE_FORWARD(ALCdsoundPlayback, ALCbackend, ALint64, getLatency)
201 static DECLARE_FORWARD(ALCdsoundPlayback, ALCbackend, void, lock)
202 static DECLARE_FORWARD(ALCdsoundPlayback, ALCbackend, void, unlock)
203 DECLARE_DEFAULT_ALLOCATORS(ALCdsoundPlayback)
205 DEFINE_ALCBACKEND_VTABLE(ALCdsoundPlayback);
208 static void ALCdsoundPlayback_Construct(ALCdsoundPlayback *self, ALCdevice *device)
210 ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
211 SET_VTABLE2(ALCdsoundPlayback, ALCbackend, self);
215 FORCE_ALIGN static int ALCdsoundPlayback_mixerProc(void *ptr)
217 ALCdsoundPlayback *self = ptr;
218 ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
219 DSBCAPS DSBCaps;
220 DWORD LastCursor = 0;
221 DWORD PlayCursor;
222 void *WritePtr1, *WritePtr2;
223 DWORD WriteCnt1, WriteCnt2;
224 BOOL Playing = FALSE;
225 DWORD FrameSize;
226 DWORD FragSize;
227 DWORD avail;
228 HRESULT err;
230 SetRTPriority();
231 althrd_setname(althrd_current(), MIXER_THREAD_NAME);
233 memset(&DSBCaps, 0, sizeof(DSBCaps));
234 DSBCaps.dwSize = sizeof(DSBCaps);
235 err = IDirectSoundBuffer_GetCaps(self->Buffer, &DSBCaps);
236 if(FAILED(err))
238 ERR("Failed to get buffer caps: 0x%lx\n", err);
239 ALCdevice_Lock(device);
240 aluHandleDisconnect(device);
241 ALCdevice_Unlock(device);
242 return 1;
245 FrameSize = FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
246 FragSize = device->UpdateSize * FrameSize;
248 IDirectSoundBuffer_GetCurrentPosition(self->Buffer, &LastCursor, NULL);
249 while(!self->killNow)
251 // Get current play cursor
252 IDirectSoundBuffer_GetCurrentPosition(self->Buffer, &PlayCursor, NULL);
253 avail = (PlayCursor-LastCursor+DSBCaps.dwBufferBytes) % DSBCaps.dwBufferBytes;
255 if(avail < FragSize)
257 if(!Playing)
259 err = IDirectSoundBuffer_Play(self->Buffer, 0, 0, DSBPLAY_LOOPING);
260 if(FAILED(err))
262 ERR("Failed to play buffer: 0x%lx\n", err);
263 ALCdevice_Lock(device);
264 aluHandleDisconnect(device);
265 ALCdevice_Unlock(device);
266 return 1;
268 Playing = TRUE;
271 avail = WaitForSingleObjectEx(self->NotifyEvent, 2000, FALSE);
272 if(avail != WAIT_OBJECT_0)
273 ERR("WaitForSingleObjectEx error: 0x%lx\n", avail);
274 continue;
276 avail -= avail%FragSize;
278 // Lock output buffer
279 WriteCnt1 = 0;
280 WriteCnt2 = 0;
281 err = IDirectSoundBuffer_Lock(self->Buffer, LastCursor, avail, &WritePtr1, &WriteCnt1, &WritePtr2, &WriteCnt2, 0);
283 // If the buffer is lost, restore it and lock
284 if(err == DSERR_BUFFERLOST)
286 WARN("Buffer lost, restoring...\n");
287 err = IDirectSoundBuffer_Restore(self->Buffer);
288 if(SUCCEEDED(err))
290 Playing = FALSE;
291 LastCursor = 0;
292 err = IDirectSoundBuffer_Lock(self->Buffer, 0, DSBCaps.dwBufferBytes, &WritePtr1, &WriteCnt1, &WritePtr2, &WriteCnt2, 0);
296 // Successfully locked the output buffer
297 if(SUCCEEDED(err))
299 // If we have an active context, mix data directly into output buffer otherwise fill with silence
300 aluMixData(device, WritePtr1, WriteCnt1/FrameSize);
301 aluMixData(device, WritePtr2, WriteCnt2/FrameSize);
303 // Unlock output buffer only when successfully locked
304 IDirectSoundBuffer_Unlock(self->Buffer, WritePtr1, WriteCnt1, WritePtr2, WriteCnt2);
306 else
308 ERR("Buffer lock error: %#lx\n", err);
309 ALCdevice_Lock(device);
310 aluHandleDisconnect(device);
311 ALCdevice_Unlock(device);
312 return 1;
315 // Update old write cursor location
316 LastCursor += WriteCnt1+WriteCnt2;
317 LastCursor %= DSBCaps.dwBufferBytes;
320 return 0;
323 static ALCenum ALCdsoundPlayback_open(ALCdsoundPlayback *self, const ALCchar *deviceName)
325 ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
326 GUID *guid = NULL;
327 HRESULT hr, hrcom;
329 if(VECTOR_SIZE(PlaybackDevices) == 0)
331 /* Initialize COM to prevent name truncation */
332 hrcom = CoInitialize(NULL);
333 hr = DirectSoundEnumerateW(DSoundEnumDevices, &PlaybackDevices);
334 if(FAILED(hr))
335 ERR("Error enumerating DirectSound devices (0x%lx)!\n", hr);
336 if(SUCCEEDED(hrcom))
337 CoUninitialize();
340 if(!deviceName && VECTOR_SIZE(PlaybackDevices) > 0)
342 deviceName = al_string_get_cstr(VECTOR_FRONT(PlaybackDevices).name);
343 guid = &VECTOR_FRONT(PlaybackDevices).guid;
345 else
347 DevMap *iter, *end;
349 iter = VECTOR_ITER_BEGIN(PlaybackDevices);
350 end = VECTOR_ITER_END(PlaybackDevices);
351 for(;iter != end;++iter)
353 if(al_string_cmp_cstr(iter->name, deviceName) == 0)
355 guid = &iter->guid;
356 break;
359 if(iter == end)
360 return ALC_INVALID_VALUE;
363 hr = DS_OK;
364 self->NotifyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
365 if(self->NotifyEvent == NULL)
366 hr = E_FAIL;
368 //DirectSound Init code
369 if(SUCCEEDED(hr))
370 hr = DirectSoundCreate(guid, &self->DS, NULL);
371 if(SUCCEEDED(hr))
372 hr = IDirectSound_SetCooperativeLevel(self->DS, GetForegroundWindow(), DSSCL_PRIORITY);
373 if(FAILED(hr))
375 if(self->DS)
376 IDirectSound_Release(self->DS);
377 self->DS = NULL;
378 if(self->NotifyEvent)
379 CloseHandle(self->NotifyEvent);
380 self->NotifyEvent = NULL;
382 ERR("Device init failed: 0x%08lx\n", hr);
383 return ALC_INVALID_VALUE;
386 al_string_copy_cstr(&device->DeviceName, deviceName);
388 return ALC_NO_ERROR;
391 static void ALCdsoundPlayback_close(ALCdsoundPlayback *self)
393 if(self->Notifies)
394 IDirectSoundNotify_Release(self->Notifies);
395 self->Notifies = NULL;
396 if(self->Buffer)
397 IDirectSoundBuffer_Release(self->Buffer);
398 self->Buffer = NULL;
399 if(self->PrimaryBuffer != NULL)
400 IDirectSoundBuffer_Release(self->PrimaryBuffer);
401 self->PrimaryBuffer = NULL;
403 IDirectSound_Release(self->DS);
404 self->DS = NULL;
405 CloseHandle(self->NotifyEvent);
406 self->NotifyEvent = NULL;
409 static ALCboolean ALCdsoundPlayback_reset(ALCdsoundPlayback *self)
411 ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
412 DSBUFFERDESC DSBDescription;
413 WAVEFORMATEXTENSIBLE OutputType;
414 DWORD speakers;
415 HRESULT hr;
417 memset(&OutputType, 0, sizeof(OutputType));
419 if(self->Notifies)
420 IDirectSoundNotify_Release(self->Notifies);
421 self->Notifies = NULL;
422 if(self->Buffer)
423 IDirectSoundBuffer_Release(self->Buffer);
424 self->Buffer = NULL;
425 if(self->PrimaryBuffer != NULL)
426 IDirectSoundBuffer_Release(self->PrimaryBuffer);
427 self->PrimaryBuffer = NULL;
429 switch(device->FmtType)
431 case DevFmtByte:
432 device->FmtType = DevFmtUByte;
433 break;
434 case DevFmtFloat:
435 if((device->Flags&DEVICE_SAMPLE_TYPE_REQUEST))
436 break;
437 /* fall-through */
438 case DevFmtUShort:
439 device->FmtType = DevFmtShort;
440 break;
441 case DevFmtUInt:
442 device->FmtType = DevFmtInt;
443 break;
444 case DevFmtUByte:
445 case DevFmtShort:
446 case DevFmtInt:
447 break;
450 hr = IDirectSound_GetSpeakerConfig(self->DS, &speakers);
451 if(SUCCEEDED(hr))
453 if(!(device->Flags&DEVICE_CHANNELS_REQUEST))
455 speakers = DSSPEAKER_CONFIG(speakers);
456 if(speakers == DSSPEAKER_MONO)
457 device->FmtChans = DevFmtMono;
458 else if(speakers == DSSPEAKER_STEREO || speakers == DSSPEAKER_HEADPHONE)
459 device->FmtChans = DevFmtStereo;
460 else if(speakers == DSSPEAKER_QUAD)
461 device->FmtChans = DevFmtQuad;
462 else if(speakers == DSSPEAKER_5POINT1 || speakers == DSSPEAKER_5POINT1_SURROUND)
463 device->FmtChans = DevFmtX51;
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);
470 switch(device->FmtChans)
472 case DevFmtMono:
473 OutputType.dwChannelMask = SPEAKER_FRONT_CENTER;
474 break;
475 case DevFmtStereo:
476 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
477 SPEAKER_FRONT_RIGHT;
478 break;
479 case DevFmtQuad:
480 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
481 SPEAKER_FRONT_RIGHT |
482 SPEAKER_BACK_LEFT |
483 SPEAKER_BACK_RIGHT;
484 break;
485 case DevFmtX51:
486 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
487 SPEAKER_FRONT_RIGHT |
488 SPEAKER_FRONT_CENTER |
489 SPEAKER_LOW_FREQUENCY |
490 SPEAKER_BACK_LEFT |
491 SPEAKER_BACK_RIGHT;
492 break;
493 case DevFmtX51Side:
494 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
495 SPEAKER_FRONT_RIGHT |
496 SPEAKER_FRONT_CENTER |
497 SPEAKER_LOW_FREQUENCY |
498 SPEAKER_SIDE_LEFT |
499 SPEAKER_SIDE_RIGHT;
500 break;
501 case DevFmtX61:
502 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
503 SPEAKER_FRONT_RIGHT |
504 SPEAKER_FRONT_CENTER |
505 SPEAKER_LOW_FREQUENCY |
506 SPEAKER_BACK_CENTER |
507 SPEAKER_SIDE_LEFT |
508 SPEAKER_SIDE_RIGHT;
509 break;
510 case DevFmtX71:
511 OutputType.dwChannelMask = SPEAKER_FRONT_LEFT |
512 SPEAKER_FRONT_RIGHT |
513 SPEAKER_FRONT_CENTER |
514 SPEAKER_LOW_FREQUENCY |
515 SPEAKER_BACK_LEFT |
516 SPEAKER_BACK_RIGHT |
517 SPEAKER_SIDE_LEFT |
518 SPEAKER_SIDE_RIGHT;
519 break;
522 retry_open:
523 hr = S_OK;
524 OutputType.Format.wFormatTag = WAVE_FORMAT_PCM;
525 OutputType.Format.nChannels = ChannelsFromDevFmt(device->FmtChans);
526 OutputType.Format.wBitsPerSample = BytesFromDevFmt(device->FmtType) * 8;
527 OutputType.Format.nBlockAlign = OutputType.Format.nChannels*OutputType.Format.wBitsPerSample/8;
528 OutputType.Format.nSamplesPerSec = device->Frequency;
529 OutputType.Format.nAvgBytesPerSec = OutputType.Format.nSamplesPerSec*OutputType.Format.nBlockAlign;
530 OutputType.Format.cbSize = 0;
533 if(OutputType.Format.nChannels > 2 || device->FmtType == DevFmtFloat)
535 OutputType.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
536 OutputType.Samples.wValidBitsPerSample = OutputType.Format.wBitsPerSample;
537 OutputType.Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
538 if(device->FmtType == DevFmtFloat)
539 OutputType.SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
540 else
541 OutputType.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
543 if(self->PrimaryBuffer)
544 IDirectSoundBuffer_Release(self->PrimaryBuffer);
545 self->PrimaryBuffer = NULL;
547 else
549 if(SUCCEEDED(hr) && !self->PrimaryBuffer)
551 memset(&DSBDescription,0,sizeof(DSBUFFERDESC));
552 DSBDescription.dwSize=sizeof(DSBUFFERDESC);
553 DSBDescription.dwFlags=DSBCAPS_PRIMARYBUFFER;
554 hr = IDirectSound_CreateSoundBuffer(self->DS, &DSBDescription, &self->PrimaryBuffer, NULL);
556 if(SUCCEEDED(hr))
557 hr = IDirectSoundBuffer_SetFormat(self->PrimaryBuffer,&OutputType.Format);
560 if(SUCCEEDED(hr))
562 if(device->NumUpdates > MAX_UPDATES)
564 device->UpdateSize = (device->UpdateSize*device->NumUpdates +
565 MAX_UPDATES-1) / MAX_UPDATES;
566 device->NumUpdates = MAX_UPDATES;
569 memset(&DSBDescription,0,sizeof(DSBUFFERDESC));
570 DSBDescription.dwSize=sizeof(DSBUFFERDESC);
571 DSBDescription.dwFlags=DSBCAPS_CTRLPOSITIONNOTIFY|DSBCAPS_GETCURRENTPOSITION2|DSBCAPS_GLOBALFOCUS;
572 DSBDescription.dwBufferBytes=device->UpdateSize * device->NumUpdates *
573 OutputType.Format.nBlockAlign;
574 DSBDescription.lpwfxFormat=&OutputType.Format;
575 hr = IDirectSound_CreateSoundBuffer(self->DS, &DSBDescription, &self->Buffer, NULL);
576 if(FAILED(hr) && device->FmtType == DevFmtFloat)
578 device->FmtType = DevFmtShort;
579 goto retry_open;
583 if(SUCCEEDED(hr))
585 hr = IDirectSoundBuffer_QueryInterface(self->Buffer, &IID_IDirectSoundNotify, (void**)&self->Notifies);
586 if(SUCCEEDED(hr))
588 DSBPOSITIONNOTIFY notifies[MAX_UPDATES];
589 ALuint i;
591 for(i = 0;i < device->NumUpdates;++i)
593 notifies[i].dwOffset = i * device->UpdateSize *
594 OutputType.Format.nBlockAlign;
595 notifies[i].hEventNotify = self->NotifyEvent;
597 if(IDirectSoundNotify_SetNotificationPositions(self->Notifies, device->NumUpdates, notifies) != DS_OK)
598 hr = E_FAIL;
602 if(FAILED(hr))
604 if(self->Notifies != NULL)
605 IDirectSoundNotify_Release(self->Notifies);
606 self->Notifies = NULL;
607 if(self->Buffer != NULL)
608 IDirectSoundBuffer_Release(self->Buffer);
609 self->Buffer = NULL;
610 if(self->PrimaryBuffer != NULL)
611 IDirectSoundBuffer_Release(self->PrimaryBuffer);
612 self->PrimaryBuffer = NULL;
613 return ALC_FALSE;
616 ResetEvent(self->NotifyEvent);
617 SetDefaultWFXChannelOrder(device);
619 return ALC_TRUE;
622 static ALCboolean ALCdsoundPlayback_start(ALCdsoundPlayback *self)
624 self->killNow = 0;
625 if(althrd_create(&self->thread, ALCdsoundPlayback_mixerProc, self) != althrd_success)
626 return ALC_FALSE;
628 return ALC_TRUE;
631 static void ALCdsoundPlayback_stop(ALCdsoundPlayback *self)
633 int res;
635 if(self->killNow)
636 return;
638 self->killNow = 1;
639 althrd_join(self->thread, &res);
641 IDirectSoundBuffer_Stop(self->Buffer);
646 typedef struct ALCdsoundCapture {
647 DERIVE_FROM_TYPE(ALCbackend);
649 IDirectSoundCapture *DSC;
650 IDirectSoundCaptureBuffer *DSCbuffer;
651 DWORD BufferBytes;
652 DWORD Cursor;
653 RingBuffer *Ring;
654 } ALCdsoundCapture;
656 static void ALCdsoundCapture_Construct(ALCdsoundCapture *self, ALCdevice *device);
657 static DECLARE_FORWARD(ALCdsoundCapture, ALCbackend, void, Destruct)
658 static ALCenum ALCdsoundCapture_open(ALCdsoundCapture *self, const ALCchar *name);
659 static void ALCdsoundCapture_close(ALCdsoundCapture *self);
660 static DECLARE_FORWARD(ALCdsoundCapture, ALCbackend, ALCboolean, reset)
661 static ALCboolean ALCdsoundCapture_start(ALCdsoundCapture *self);
662 static void ALCdsoundCapture_stop(ALCdsoundCapture *self);
663 static ALCenum ALCdsoundCapture_captureSamples(ALCdsoundCapture *self, ALCvoid *buffer, ALCuint samples);
664 static ALCuint ALCdsoundCapture_availableSamples(ALCdsoundCapture *self);
665 static DECLARE_FORWARD(ALCdsoundCapture, ALCbackend, ALint64, getLatency)
666 static DECLARE_FORWARD(ALCdsoundCapture, ALCbackend, void, lock)
667 static DECLARE_FORWARD(ALCdsoundCapture, ALCbackend, void, unlock)
668 DECLARE_DEFAULT_ALLOCATORS(ALCdsoundCapture)
670 DEFINE_ALCBACKEND_VTABLE(ALCdsoundCapture);
672 static void ALCdsoundCapture_Construct(ALCdsoundCapture *self, ALCdevice *device)
674 ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
675 SET_VTABLE2(ALCdsoundCapture, ALCbackend, self);
679 static ALCenum ALCdsoundCapture_open(ALCdsoundCapture *self, const ALCchar *deviceName)
681 ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
682 WAVEFORMATEXTENSIBLE InputType;
683 DSCBUFFERDESC DSCBDescription;
684 GUID *guid = NULL;
685 HRESULT hr, hrcom;
686 ALuint samples;
688 if(VECTOR_SIZE(CaptureDevices) == 0)
690 /* Initialize COM to prevent name truncation */
691 hrcom = CoInitialize(NULL);
692 hr = DirectSoundCaptureEnumerateW(DSoundEnumDevices, &CaptureDevices);
693 if(FAILED(hr))
694 ERR("Error enumerating DirectSound devices (0x%lx)!\n", hr);
695 if(SUCCEEDED(hrcom))
696 CoUninitialize();
699 if(!deviceName && VECTOR_SIZE(CaptureDevices) > 0)
701 deviceName = al_string_get_cstr(VECTOR_FRONT(CaptureDevices).name);
702 guid = &VECTOR_FRONT(CaptureDevices).guid;
704 else
706 DevMap *iter, *end;
708 iter = VECTOR_ITER_BEGIN(CaptureDevices);
709 end = VECTOR_ITER_END(CaptureDevices);
710 for(;iter != end;++iter)
712 if(al_string_cmp_cstr(iter->name, deviceName) == 0)
714 guid = &iter->guid;
715 break;
718 if(iter == end)
719 return ALC_INVALID_VALUE;
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 //DirectSoundCapture Init code
738 hr = DirectSoundCaptureCreate(guid, &self->DSC, NULL);
739 if(SUCCEEDED(hr))
741 memset(&InputType, 0, sizeof(InputType));
743 switch(device->FmtChans)
745 case DevFmtMono:
746 InputType.dwChannelMask = SPEAKER_FRONT_CENTER;
747 break;
748 case DevFmtStereo:
749 InputType.dwChannelMask = SPEAKER_FRONT_LEFT |
750 SPEAKER_FRONT_RIGHT;
751 break;
752 case DevFmtQuad:
753 InputType.dwChannelMask = SPEAKER_FRONT_LEFT |
754 SPEAKER_FRONT_RIGHT |
755 SPEAKER_BACK_LEFT |
756 SPEAKER_BACK_RIGHT;
757 break;
758 case DevFmtX51:
759 InputType.dwChannelMask = SPEAKER_FRONT_LEFT |
760 SPEAKER_FRONT_RIGHT |
761 SPEAKER_FRONT_CENTER |
762 SPEAKER_LOW_FREQUENCY |
763 SPEAKER_BACK_LEFT |
764 SPEAKER_BACK_RIGHT;
765 break;
766 case DevFmtX51Side:
767 InputType.dwChannelMask = SPEAKER_FRONT_LEFT |
768 SPEAKER_FRONT_RIGHT |
769 SPEAKER_FRONT_CENTER |
770 SPEAKER_LOW_FREQUENCY |
771 SPEAKER_SIDE_LEFT |
772 SPEAKER_SIDE_RIGHT;
773 break;
774 case DevFmtX61:
775 InputType.dwChannelMask = SPEAKER_FRONT_LEFT |
776 SPEAKER_FRONT_RIGHT |
777 SPEAKER_FRONT_CENTER |
778 SPEAKER_LOW_FREQUENCY |
779 SPEAKER_BACK_CENTER |
780 SPEAKER_SIDE_LEFT |
781 SPEAKER_SIDE_RIGHT;
782 break;
783 case DevFmtX71:
784 InputType.dwChannelMask = SPEAKER_FRONT_LEFT |
785 SPEAKER_FRONT_RIGHT |
786 SPEAKER_FRONT_CENTER |
787 SPEAKER_LOW_FREQUENCY |
788 SPEAKER_BACK_LEFT |
789 SPEAKER_BACK_RIGHT |
790 SPEAKER_SIDE_LEFT |
791 SPEAKER_SIDE_RIGHT;
792 break;
795 InputType.Format.wFormatTag = WAVE_FORMAT_PCM;
796 InputType.Format.nChannels = ChannelsFromDevFmt(device->FmtChans);
797 InputType.Format.wBitsPerSample = BytesFromDevFmt(device->FmtType) * 8;
798 InputType.Format.nBlockAlign = InputType.Format.nChannels*InputType.Format.wBitsPerSample/8;
799 InputType.Format.nSamplesPerSec = device->Frequency;
800 InputType.Format.nAvgBytesPerSec = InputType.Format.nSamplesPerSec*InputType.Format.nBlockAlign;
801 InputType.Format.cbSize = 0;
803 if(InputType.Format.nChannels > 2 || device->FmtType == DevFmtFloat)
805 InputType.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
806 InputType.Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
807 InputType.Samples.wValidBitsPerSample = InputType.Format.wBitsPerSample;
808 if(device->FmtType == DevFmtFloat)
809 InputType.SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
810 else
811 InputType.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
814 samples = device->UpdateSize * device->NumUpdates;
815 samples = maxu(samples, 100 * device->Frequency / 1000);
817 memset(&DSCBDescription, 0, sizeof(DSCBUFFERDESC));
818 DSCBDescription.dwSize = sizeof(DSCBUFFERDESC);
819 DSCBDescription.dwFlags = 0;
820 DSCBDescription.dwBufferBytes = samples * InputType.Format.nBlockAlign;
821 DSCBDescription.lpwfxFormat = &InputType.Format;
823 hr = IDirectSoundCapture_CreateCaptureBuffer(self->DSC, &DSCBDescription, &self->DSCbuffer, NULL);
825 if(SUCCEEDED(hr))
827 self->Ring = CreateRingBuffer(InputType.Format.nBlockAlign, device->UpdateSize * device->NumUpdates);
828 if(self->Ring == NULL)
829 hr = DSERR_OUTOFMEMORY;
832 if(FAILED(hr))
834 ERR("Device init failed: 0x%08lx\n", hr);
836 DestroyRingBuffer(self->Ring);
837 self->Ring = NULL;
838 if(self->DSCbuffer != NULL)
839 IDirectSoundCaptureBuffer_Release(self->DSCbuffer);
840 self->DSCbuffer = NULL;
841 if(self->DSC)
842 IDirectSoundCapture_Release(self->DSC);
843 self->DSC = NULL;
845 return ALC_INVALID_VALUE;
848 self->BufferBytes = DSCBDescription.dwBufferBytes;
849 SetDefaultWFXChannelOrder(device);
851 al_string_copy_cstr(&device->DeviceName, deviceName);
853 return ALC_NO_ERROR;
856 static void ALCdsoundCapture_close(ALCdsoundCapture *self)
858 DestroyRingBuffer(self->Ring);
859 self->Ring = NULL;
861 if(self->DSCbuffer != NULL)
863 IDirectSoundCaptureBuffer_Stop(self->DSCbuffer);
864 IDirectSoundCaptureBuffer_Release(self->DSCbuffer);
865 self->DSCbuffer = NULL;
868 IDirectSoundCapture_Release(self->DSC);
869 self->DSC = NULL;
872 static ALCboolean ALCdsoundCapture_start(ALCdsoundCapture *self)
874 HRESULT hr;
876 hr = IDirectSoundCaptureBuffer_Start(self->DSCbuffer, DSCBSTART_LOOPING);
877 if(FAILED(hr))
879 ERR("start failed: 0x%08lx\n", hr);
880 aluHandleDisconnect(STATIC_CAST(ALCbackend, self)->mDevice);
881 return ALC_FALSE;
884 return ALC_TRUE;
887 static void ALCdsoundCapture_stop(ALCdsoundCapture *self)
889 HRESULT hr;
891 hr = IDirectSoundCaptureBuffer_Stop(self->DSCbuffer);
892 if(FAILED(hr))
894 ERR("stop failed: 0x%08lx\n", hr);
895 aluHandleDisconnect(STATIC_CAST(ALCbackend, self)->mDevice);
899 static ALCenum ALCdsoundCapture_captureSamples(ALCdsoundCapture *self, ALCvoid *buffer, ALCuint samples)
901 ReadRingBuffer(self->Ring, buffer, samples);
902 return ALC_NO_ERROR;
905 static ALCuint ALCdsoundCapture_availableSamples(ALCdsoundCapture *self)
907 ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
908 DWORD ReadCursor, LastCursor, BufferBytes, NumBytes;
909 void *ReadPtr1, *ReadPtr2;
910 DWORD ReadCnt1, ReadCnt2;
911 DWORD FrameSize;
912 HRESULT hr;
914 if(!device->Connected)
915 goto done;
917 FrameSize = FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
918 BufferBytes = self->BufferBytes;
919 LastCursor = self->Cursor;
921 hr = IDirectSoundCaptureBuffer_GetCurrentPosition(self->DSCbuffer, NULL, &ReadCursor);
922 if(SUCCEEDED(hr))
924 NumBytes = (ReadCursor-LastCursor + BufferBytes) % BufferBytes;
925 if(NumBytes == 0)
926 goto done;
927 hr = IDirectSoundCaptureBuffer_Lock(self->DSCbuffer, LastCursor, NumBytes,
928 &ReadPtr1, &ReadCnt1,
929 &ReadPtr2, &ReadCnt2, 0);
931 if(SUCCEEDED(hr))
933 WriteRingBuffer(self->Ring, ReadPtr1, ReadCnt1/FrameSize);
934 if(ReadPtr2 != NULL)
935 WriteRingBuffer(self->Ring, ReadPtr2, ReadCnt2/FrameSize);
936 hr = IDirectSoundCaptureBuffer_Unlock(self->DSCbuffer,
937 ReadPtr1, ReadCnt1,
938 ReadPtr2, ReadCnt2);
939 self->Cursor = (LastCursor+ReadCnt1+ReadCnt2) % BufferBytes;
942 if(FAILED(hr))
944 ERR("update failed: 0x%08lx\n", hr);
945 aluHandleDisconnect(device);
948 done:
949 return RingBufferSize(self->Ring);
953 static inline void AppendAllDevicesList2(const DevMap *entry)
954 { AppendAllDevicesList(al_string_get_cstr(entry->name)); }
955 static inline void AppendCaptureDeviceList2(const DevMap *entry)
956 { AppendCaptureDeviceList(al_string_get_cstr(entry->name)); }
958 typedef struct ALCdsoundBackendFactory {
959 DERIVE_FROM_TYPE(ALCbackendFactory);
960 } ALCdsoundBackendFactory;
961 #define ALCDSOUNDBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCdsoundBackendFactory, ALCbackendFactory) } }
963 ALCbackendFactory *ALCdsoundBackendFactory_getFactory(void);
965 static ALCboolean ALCdsoundBackendFactory_init(ALCdsoundBackendFactory *self);
966 static void ALCdsoundBackendFactory_deinit(ALCdsoundBackendFactory *self);
967 static ALCboolean ALCdsoundBackendFactory_querySupport(ALCdsoundBackendFactory *self, ALCbackend_Type type);
968 static void ALCdsoundBackendFactory_probe(ALCdsoundBackendFactory *self, enum DevProbe type);
969 static ALCbackend* ALCdsoundBackendFactory_createBackend(ALCdsoundBackendFactory *self, ALCdevice *device, ALCbackend_Type type);
970 DEFINE_ALCBACKENDFACTORY_VTABLE(ALCdsoundBackendFactory);
973 ALCbackendFactory *ALCdsoundBackendFactory_getFactory(void)
975 static ALCdsoundBackendFactory factory = ALCDSOUNDBACKENDFACTORY_INITIALIZER;
976 return STATIC_CAST(ALCbackendFactory, &factory);
980 static ALCboolean ALCdsoundBackendFactory_init(ALCdsoundBackendFactory* UNUSED(self))
982 VECTOR_INIT(PlaybackDevices);
983 VECTOR_INIT(CaptureDevices);
985 if(!DSoundLoad())
986 return ALC_FALSE;
987 return ALC_TRUE;
990 static void ALCdsoundBackendFactory_deinit(ALCdsoundBackendFactory* UNUSED(self))
992 clear_devlist(&PlaybackDevices);
993 VECTOR_DEINIT(PlaybackDevices);
995 clear_devlist(&CaptureDevices);
996 VECTOR_DEINIT(CaptureDevices);
998 #ifdef HAVE_DYNLOAD
999 if(ds_handle)
1000 CloseLib(ds_handle);
1001 ds_handle = NULL;
1002 #endif
1005 static ALCboolean ALCdsoundBackendFactory_querySupport(ALCdsoundBackendFactory* UNUSED(self), ALCbackend_Type type)
1007 if(type == ALCbackend_Playback || type == ALCbackend_Capture)
1008 return ALC_TRUE;
1009 return ALC_FALSE;
1012 static void ALCdsoundBackendFactory_probe(ALCdsoundBackendFactory* UNUSED(self), enum DevProbe type)
1014 HRESULT hr, hrcom;
1016 /* Initialize COM to prevent name truncation */
1017 hrcom = CoInitialize(NULL);
1018 switch(type)
1020 case ALL_DEVICE_PROBE:
1021 clear_devlist(&PlaybackDevices);
1022 hr = DirectSoundEnumerateW(DSoundEnumDevices, &PlaybackDevices);
1023 if(FAILED(hr))
1024 ERR("Error enumerating DirectSound playback devices (0x%lx)!\n", hr);
1025 VECTOR_FOR_EACH(const DevMap, PlaybackDevices, AppendAllDevicesList2);
1026 break;
1028 case CAPTURE_DEVICE_PROBE:
1029 clear_devlist(&CaptureDevices);
1030 hr = DirectSoundCaptureEnumerateW(DSoundEnumDevices, &CaptureDevices);
1031 if(FAILED(hr))
1032 ERR("Error enumerating DirectSound capture devices (0x%lx)!\n", hr);
1033 VECTOR_FOR_EACH(const DevMap, CaptureDevices, AppendCaptureDeviceList2);
1034 break;
1036 if(SUCCEEDED(hrcom))
1037 CoUninitialize();
1040 static ALCbackend* ALCdsoundBackendFactory_createBackend(ALCdsoundBackendFactory* UNUSED(self), ALCdevice *device, ALCbackend_Type type)
1042 if(type == ALCbackend_Playback)
1044 ALCdsoundPlayback *backend;
1046 backend = ALCdsoundPlayback_New(sizeof(*backend));
1047 if(!backend) return NULL;
1048 memset(backend, 0, sizeof(*backend));
1050 ALCdsoundPlayback_Construct(backend, device);
1052 return STATIC_CAST(ALCbackend, backend);
1055 if(type == ALCbackend_Capture)
1057 ALCdsoundCapture *backend;
1059 backend = ALCdsoundCapture_New(sizeof(*backend));
1060 if(!backend) return NULL;
1061 memset(backend, 0, sizeof(*backend));
1063 ALCdsoundCapture_Construct(backend, device);
1065 return STATIC_CAST(ALCbackend, backend);
1068 return NULL;