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
30 #ifndef _WAVEFORMATEXTENSIBLE_
41 #include "backends/base.h"
43 #ifndef DSSPEAKER_5POINT1
44 # define DSSPEAKER_5POINT1 0x00000006
46 #ifndef DSSPEAKER_7POINT1
47 # define DSSPEAKER_7POINT1 0x00000007
49 #ifndef DSSPEAKER_7POINT1_SURROUND
50 # define DSSPEAKER_7POINT1_SURROUND 0x00000008
52 #ifndef DSSPEAKER_5POINT1_SURROUND
53 # define DSSPEAKER_5POINT1_SURROUND 0x00000009
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);
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
75 static ALCboolean
DSoundLoad(void)
80 ds_handle
= LoadLib("dsound.dll");
83 ERR("Failed to load dsound.dll\n");
87 #define LOAD_FUNC(f) do { \
88 p##f = GetSymbol(ds_handle, #f); \
90 CloseLib(ds_handle); \
95 LOAD_FUNC(DirectSoundCreate
);
96 LOAD_FUNC(DirectSoundEnumerateW
);
97 LOAD_FUNC(DirectSoundCaptureCreate
);
98 LOAD_FUNC(DirectSoundCaptureEnumerateW
);
106 #define MAX_UPDATES 128
114 vector_DevMap PlaybackDevices
;
115 vector_DevMap CaptureDevices
;
117 static void clear_devlist(vector_DevMap
*list
)
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
;
140 AL_STRING_INIT(entry
.name
);
144 al_string_copy_wcstr(&entry
.name
, desc
);
148 snprintf(str
, sizeof(str
), " #%d", count
+1);
149 al_string_append_cstr(&entry
.name
, str
);
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)
160 } while(iter
!= end
);
163 hr
= StringFromCLSID(guid
, &guidstr
);
166 TRACE("Got device \"%s\", GUID \"%ls\"\n", al_string_get_cstr(entry
.name
), guidstr
);
167 CoTaskMemFree(guidstr
);
170 VECTOR_PUSH_BACK(*devices
, entry
);
176 typedef struct ALCdsoundPlayback
{
177 DERIVE_FROM_TYPE(ALCbackend
);
180 IDirectSoundBuffer
*PrimaryBuffer
;
181 IDirectSoundBuffer
*Buffer
;
182 IDirectSoundNotify
*Notifies
;
185 volatile int killNow
;
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
;
220 DWORD LastCursor
= 0;
222 void *WritePtr1
, *WritePtr2
;
223 DWORD WriteCnt1
, WriteCnt2
;
224 BOOL Playing
= FALSE
;
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
);
238 ERR("Failed to get buffer caps: 0x%lx\n", err
);
239 ALCdevice_Lock(device
);
240 aluHandleDisconnect(device
);
241 ALCdevice_Unlock(device
);
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
;
259 err
= IDirectSoundBuffer_Play(self
->Buffer
, 0, 0, DSBPLAY_LOOPING
);
262 ERR("Failed to play buffer: 0x%lx\n", err
);
263 ALCdevice_Lock(device
);
264 aluHandleDisconnect(device
);
265 ALCdevice_Unlock(device
);
271 avail
= WaitForSingleObjectEx(self
->NotifyEvent
, 2000, FALSE
);
272 if(avail
!= WAIT_OBJECT_0
)
273 ERR("WaitForSingleObjectEx error: 0x%lx\n", avail
);
276 avail
-= avail
%FragSize
;
278 // Lock output buffer
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
);
292 err
= IDirectSoundBuffer_Lock(self
->Buffer
, 0, DSBCaps
.dwBufferBytes
, &WritePtr1
, &WriteCnt1
, &WritePtr2
, &WriteCnt2
, 0);
296 // Successfully locked the output buffer
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
);
308 ERR("Buffer lock error: %#lx\n", err
);
309 ALCdevice_Lock(device
);
310 aluHandleDisconnect(device
);
311 ALCdevice_Unlock(device
);
315 // Update old write cursor location
316 LastCursor
+= WriteCnt1
+WriteCnt2
;
317 LastCursor
%= DSBCaps
.dwBufferBytes
;
323 static ALCenum
ALCdsoundPlayback_open(ALCdsoundPlayback
*self
, const ALCchar
*deviceName
)
325 ALCdevice
*device
= STATIC_CAST(ALCbackend
, self
)->mDevice
;
329 if(VECTOR_SIZE(PlaybackDevices
) == 0)
331 /* Initialize COM to prevent name truncation */
332 hrcom
= CoInitialize(NULL
);
333 hr
= DirectSoundEnumerateW(DSoundEnumDevices
, &PlaybackDevices
);
335 ERR("Error enumerating DirectSound devices (0x%lx)!\n", hr
);
340 if(!deviceName
&& VECTOR_SIZE(PlaybackDevices
) > 0)
342 deviceName
= al_string_get_cstr(VECTOR_FRONT(PlaybackDevices
).name
);
343 guid
= &VECTOR_FRONT(PlaybackDevices
).guid
;
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)
360 return ALC_INVALID_VALUE
;
364 self
->NotifyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
365 if(self
->NotifyEvent
== NULL
)
368 //DirectSound Init code
370 hr
= DirectSoundCreate(guid
, &self
->DS
, NULL
);
372 hr
= IDirectSound_SetCooperativeLevel(self
->DS
, GetForegroundWindow(), DSSCL_PRIORITY
);
376 IDirectSound_Release(self
->DS
);
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
);
391 static void ALCdsoundPlayback_close(ALCdsoundPlayback
*self
)
394 IDirectSoundNotify_Release(self
->Notifies
);
395 self
->Notifies
= NULL
;
397 IDirectSoundBuffer_Release(self
->Buffer
);
399 if(self
->PrimaryBuffer
!= NULL
)
400 IDirectSoundBuffer_Release(self
->PrimaryBuffer
);
401 self
->PrimaryBuffer
= NULL
;
403 IDirectSound_Release(self
->DS
);
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
;
417 memset(&OutputType
, 0, sizeof(OutputType
));
420 IDirectSoundNotify_Release(self
->Notifies
);
421 self
->Notifies
= NULL
;
423 IDirectSoundBuffer_Release(self
->Buffer
);
425 if(self
->PrimaryBuffer
!= NULL
)
426 IDirectSoundBuffer_Release(self
->PrimaryBuffer
);
427 self
->PrimaryBuffer
= NULL
;
429 switch(device
->FmtType
)
432 device
->FmtType
= DevFmtUByte
;
435 if((device
->Flags
&DEVICE_SAMPLE_TYPE_REQUEST
))
439 device
->FmtType
= DevFmtShort
;
442 device
->FmtType
= DevFmtInt
;
450 hr
= IDirectSound_GetSpeakerConfig(self
->DS
, &speakers
);
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
;
467 ERR("Unknown system speaker config: 0x%lx\n", speakers
);
470 switch(device
->FmtChans
)
473 OutputType
.dwChannelMask
= SPEAKER_FRONT_CENTER
;
476 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
480 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
481 SPEAKER_FRONT_RIGHT
|
486 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
487 SPEAKER_FRONT_RIGHT
|
488 SPEAKER_FRONT_CENTER
|
489 SPEAKER_LOW_FREQUENCY
|
494 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
495 SPEAKER_FRONT_RIGHT
|
496 SPEAKER_FRONT_CENTER
|
497 SPEAKER_LOW_FREQUENCY
|
502 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
503 SPEAKER_FRONT_RIGHT
|
504 SPEAKER_FRONT_CENTER
|
505 SPEAKER_LOW_FREQUENCY
|
506 SPEAKER_BACK_CENTER
|
511 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
512 SPEAKER_FRONT_RIGHT
|
513 SPEAKER_FRONT_CENTER
|
514 SPEAKER_LOW_FREQUENCY
|
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
;
541 OutputType
.SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
543 if(self
->PrimaryBuffer
)
544 IDirectSoundBuffer_Release(self
->PrimaryBuffer
);
545 self
->PrimaryBuffer
= NULL
;
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
);
557 hr
= IDirectSoundBuffer_SetFormat(self
->PrimaryBuffer
,&OutputType
.Format
);
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
;
585 hr
= IDirectSoundBuffer_QueryInterface(self
->Buffer
, &IID_IDirectSoundNotify
, (void**)&self
->Notifies
);
588 DSBPOSITIONNOTIFY notifies
[MAX_UPDATES
];
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
)
604 if(self
->Notifies
!= NULL
)
605 IDirectSoundNotify_Release(self
->Notifies
);
606 self
->Notifies
= NULL
;
607 if(self
->Buffer
!= NULL
)
608 IDirectSoundBuffer_Release(self
->Buffer
);
610 if(self
->PrimaryBuffer
!= NULL
)
611 IDirectSoundBuffer_Release(self
->PrimaryBuffer
);
612 self
->PrimaryBuffer
= NULL
;
616 ResetEvent(self
->NotifyEvent
);
617 SetDefaultWFXChannelOrder(device
);
622 static ALCboolean
ALCdsoundPlayback_start(ALCdsoundPlayback
*self
)
625 if(althrd_create(&self
->thread
, ALCdsoundPlayback_mixerProc
, self
) != althrd_success
)
631 static void ALCdsoundPlayback_stop(ALCdsoundPlayback
*self
)
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
;
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
;
688 if(VECTOR_SIZE(CaptureDevices
) == 0)
690 /* Initialize COM to prevent name truncation */
691 hrcom
= CoInitialize(NULL
);
692 hr
= DirectSoundCaptureEnumerateW(DSoundEnumDevices
, &CaptureDevices
);
694 ERR("Error enumerating DirectSound devices (0x%lx)!\n", hr
);
699 if(!deviceName
&& VECTOR_SIZE(CaptureDevices
) > 0)
701 deviceName
= al_string_get_cstr(VECTOR_FRONT(CaptureDevices
).name
);
702 guid
= &VECTOR_FRONT(CaptureDevices
).guid
;
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)
719 return ALC_INVALID_VALUE
;
722 switch(device
->FmtType
)
727 WARN("%s capture samples not supported\n", DevFmtTypeString(device
->FmtType
));
728 return ALC_INVALID_ENUM
;
737 //DirectSoundCapture Init code
738 hr
= DirectSoundCaptureCreate(guid
, &self
->DSC
, NULL
);
741 memset(&InputType
, 0, sizeof(InputType
));
743 switch(device
->FmtChans
)
746 InputType
.dwChannelMask
= SPEAKER_FRONT_CENTER
;
749 InputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
753 InputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
754 SPEAKER_FRONT_RIGHT
|
759 InputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
760 SPEAKER_FRONT_RIGHT
|
761 SPEAKER_FRONT_CENTER
|
762 SPEAKER_LOW_FREQUENCY
|
767 InputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
768 SPEAKER_FRONT_RIGHT
|
769 SPEAKER_FRONT_CENTER
|
770 SPEAKER_LOW_FREQUENCY
|
775 InputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
776 SPEAKER_FRONT_RIGHT
|
777 SPEAKER_FRONT_CENTER
|
778 SPEAKER_LOW_FREQUENCY
|
779 SPEAKER_BACK_CENTER
|
784 InputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
785 SPEAKER_FRONT_RIGHT
|
786 SPEAKER_FRONT_CENTER
|
787 SPEAKER_LOW_FREQUENCY
|
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
;
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
);
827 self
->Ring
= CreateRingBuffer(InputType
.Format
.nBlockAlign
, device
->UpdateSize
* device
->NumUpdates
);
828 if(self
->Ring
== NULL
)
829 hr
= DSERR_OUTOFMEMORY
;
834 ERR("Device init failed: 0x%08lx\n", hr
);
836 DestroyRingBuffer(self
->Ring
);
838 if(self
->DSCbuffer
!= NULL
)
839 IDirectSoundCaptureBuffer_Release(self
->DSCbuffer
);
840 self
->DSCbuffer
= NULL
;
842 IDirectSoundCapture_Release(self
->DSC
);
845 return ALC_INVALID_VALUE
;
848 self
->BufferBytes
= DSCBDescription
.dwBufferBytes
;
849 SetDefaultWFXChannelOrder(device
);
851 al_string_copy_cstr(&device
->DeviceName
, deviceName
);
856 static void ALCdsoundCapture_close(ALCdsoundCapture
*self
)
858 DestroyRingBuffer(self
->Ring
);
861 if(self
->DSCbuffer
!= NULL
)
863 IDirectSoundCaptureBuffer_Stop(self
->DSCbuffer
);
864 IDirectSoundCaptureBuffer_Release(self
->DSCbuffer
);
865 self
->DSCbuffer
= NULL
;
868 IDirectSoundCapture_Release(self
->DSC
);
872 static ALCboolean
ALCdsoundCapture_start(ALCdsoundCapture
*self
)
876 hr
= IDirectSoundCaptureBuffer_Start(self
->DSCbuffer
, DSCBSTART_LOOPING
);
879 ERR("start failed: 0x%08lx\n", hr
);
880 aluHandleDisconnect(STATIC_CAST(ALCbackend
, self
)->mDevice
);
887 static void ALCdsoundCapture_stop(ALCdsoundCapture
*self
)
891 hr
= IDirectSoundCaptureBuffer_Stop(self
->DSCbuffer
);
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
);
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
;
914 if(!device
->Connected
)
917 FrameSize
= FrameSizeFromDevFmt(device
->FmtChans
, device
->FmtType
);
918 BufferBytes
= self
->BufferBytes
;
919 LastCursor
= self
->Cursor
;
921 hr
= IDirectSoundCaptureBuffer_GetCurrentPosition(self
->DSCbuffer
, NULL
, &ReadCursor
);
924 NumBytes
= (ReadCursor
-LastCursor
+ BufferBytes
) % BufferBytes
;
927 hr
= IDirectSoundCaptureBuffer_Lock(self
->DSCbuffer
, LastCursor
, NumBytes
,
928 &ReadPtr1
, &ReadCnt1
,
929 &ReadPtr2
, &ReadCnt2
, 0);
933 WriteRingBuffer(self
->Ring
, ReadPtr1
, ReadCnt1
/FrameSize
);
935 WriteRingBuffer(self
->Ring
, ReadPtr2
, ReadCnt2
/FrameSize
);
936 hr
= IDirectSoundCaptureBuffer_Unlock(self
->DSCbuffer
,
939 self
->Cursor
= (LastCursor
+ReadCnt1
+ReadCnt2
) % BufferBytes
;
944 ERR("update failed: 0x%08lx\n", hr
);
945 aluHandleDisconnect(device
);
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
);
990 static void ALCdsoundBackendFactory_deinit(ALCdsoundBackendFactory
* UNUSED(self
))
992 clear_devlist(&PlaybackDevices
);
993 VECTOR_DEINIT(PlaybackDevices
);
995 clear_devlist(&CaptureDevices
);
996 VECTOR_DEINIT(CaptureDevices
);
1000 CloseLib(ds_handle
);
1005 static ALCboolean
ALCdsoundBackendFactory_querySupport(ALCdsoundBackendFactory
* UNUSED(self
), ALCbackend_Type type
)
1007 if(type
== ALCbackend_Playback
|| type
== ALCbackend_Capture
)
1012 static void ALCdsoundBackendFactory_probe(ALCdsoundBackendFactory
* UNUSED(self
), enum DevProbe type
)
1016 /* Initialize COM to prevent name truncation */
1017 hrcom
= CoInitialize(NULL
);
1020 case ALL_DEVICE_PROBE
:
1021 clear_devlist(&PlaybackDevices
);
1022 hr
= DirectSoundEnumerateW(DSoundEnumDevices
, &PlaybackDevices
);
1024 ERR("Error enumerating DirectSound playback devices (0x%lx)!\n", hr
);
1025 VECTOR_FOR_EACH(const DevMap
, PlaybackDevices
, AppendAllDevicesList2
);
1028 case CAPTURE_DEVICE_PROBE
:
1029 clear_devlist(&CaptureDevices
);
1030 hr
= DirectSoundCaptureEnumerateW(DSoundEnumDevices
, &CaptureDevices
);
1032 ERR("Error enumerating DirectSound capture devices (0x%lx)!\n", hr
);
1033 VECTOR_FOR_EACH(const DevMap
, CaptureDevices
, AppendCaptureDeviceList2
);
1036 if(SUCCEEDED(hrcom
))
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
);