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_
39 #ifndef DSSPEAKER_5POINT1
40 #define DSSPEAKER_5POINT1 6
42 #ifndef DSSPEAKER_7POINT1
43 #define DSSPEAKER_7POINT1 7
46 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM
, 0x00000001, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
47 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
, 0x00000003, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
50 static void *ds_handle
;
51 static HRESULT (WINAPI
*pDirectSoundCreate
)(LPCGUID pcGuidDevice
, IDirectSound
**ppDS
, IUnknown
*pUnkOuter
);
52 static HRESULT (WINAPI
*pDirectSoundEnumerateA
)(LPDSENUMCALLBACKA pDSEnumCallback
, void *pContext
);
53 static HRESULT (WINAPI
*pDirectSoundCaptureCreate
)(LPCGUID pcGuidDevice
, IDirectSoundCapture
**ppDSC
, IUnknown
*pUnkOuter
);
54 static HRESULT (WINAPI
*pDirectSoundCaptureEnumerateA
)(LPDSENUMCALLBACKA pDSEnumCallback
, void *pContext
);
56 #define DirectSoundCreate pDirectSoundCreate
57 #define DirectSoundEnumerateA pDirectSoundEnumerateA
58 #define DirectSoundCaptureCreate pDirectSoundCaptureCreate
59 #define DirectSoundCaptureEnumerateA pDirectSoundCaptureEnumerateA
63 // DirectSound Playback Device
65 IDirectSoundBuffer
*DSpbuffer
;
66 IDirectSoundBuffer
*DSsbuffer
;
67 IDirectSoundNotify
*DSnotify
;
75 // DirectSound Capture Device
76 IDirectSoundCapture
*DSC
;
77 IDirectSoundCaptureBuffer
*DSCbuffer
;
89 static DevMap
*PlaybackDeviceList
;
90 static ALuint NumPlaybackDevices
;
91 static DevMap
*CaptureDeviceList
;
92 static ALuint NumCaptureDevices
;
94 #define MAX_UPDATES 128
96 static ALCboolean
DSoundLoad(void)
100 ds_handle
= LoadLib("dsound.dll");
101 if(ds_handle
== NULL
)
103 ERR("Failed to load dsound.dll\n");
107 #define LOAD_FUNC(f) do { \
108 p##f = GetSymbol(ds_handle, #f); \
110 CloseLib(ds_handle); \
115 LOAD_FUNC(DirectSoundCreate
);
116 LOAD_FUNC(DirectSoundEnumerateA
);
117 LOAD_FUNC(DirectSoundCaptureCreate
);
118 LOAD_FUNC(DirectSoundCaptureEnumerateA
);
125 static BOOL CALLBACK
DSoundEnumPlaybackDevices(LPGUID guid
, LPCSTR desc
, LPCSTR drvname
, LPVOID data
)
127 LPOLESTR guidstr
= NULL
;
143 snprintf(str
, sizeof(str
), "%s", desc
);
145 snprintf(str
, sizeof(str
), "%s #%d", desc
, count
+1);
148 for(i
= 0;i
< NumPlaybackDevices
;i
++)
150 if(strcmp(str
, PlaybackDeviceList
[i
].name
) == 0)
153 } while(i
!= NumPlaybackDevices
);
155 hr
= StringFromCLSID(guid
, &guidstr
);
158 TRACE("Got device \"%s\", GUID \"%ls\"\n", str
, guidstr
);
159 CoTaskMemFree(guidstr
);
162 temp
= realloc(PlaybackDeviceList
, sizeof(DevMap
) * (NumPlaybackDevices
+1));
165 PlaybackDeviceList
= temp
;
166 PlaybackDeviceList
[NumPlaybackDevices
].name
= strdup(str
);
167 PlaybackDeviceList
[NumPlaybackDevices
].guid
= *guid
;
168 NumPlaybackDevices
++;
175 static BOOL CALLBACK
DSoundEnumCaptureDevices(LPGUID guid
, LPCSTR desc
, LPCSTR drvname
, LPVOID data
)
177 LPOLESTR guidstr
= NULL
;
193 snprintf(str
, sizeof(str
), "%s", desc
);
195 snprintf(str
, sizeof(str
), "%s #%d", desc
, count
+1);
198 for(i
= 0;i
< NumCaptureDevices
;i
++)
200 if(strcmp(str
, CaptureDeviceList
[i
].name
) == 0)
203 } while(i
!= NumCaptureDevices
);
205 hr
= StringFromCLSID(guid
, &guidstr
);
208 TRACE("Got device \"%s\", GUID \"%ls\"\n", str
, guidstr
);
209 CoTaskMemFree(guidstr
);
212 temp
= realloc(CaptureDeviceList
, sizeof(DevMap
) * (NumCaptureDevices
+1));
215 CaptureDeviceList
= temp
;
216 CaptureDeviceList
[NumCaptureDevices
].name
= strdup(str
);
217 CaptureDeviceList
[NumCaptureDevices
].guid
= *guid
;
225 static ALuint
DSoundPlaybackProc(ALvoid
*ptr
)
227 ALCdevice
*Device
= (ALCdevice
*)ptr
;
228 DSoundPlaybackData
*data
= (DSoundPlaybackData
*)Device
->ExtraData
;
230 DWORD LastCursor
= 0;
232 VOID
*WritePtr1
, *WritePtr2
;
233 DWORD WriteCnt1
, WriteCnt2
;
234 BOOL Playing
= FALSE
;
242 memset(&DSBCaps
, 0, sizeof(DSBCaps
));
243 DSBCaps
.dwSize
= sizeof(DSBCaps
);
244 err
= IDirectSoundBuffer_GetCaps(data
->DSsbuffer
, &DSBCaps
);
247 ERR("Failed to get buffer caps: 0x%lx\n", err
);
248 aluHandleDisconnect(Device
);
252 FrameSize
= FrameSizeFromDevFmt(Device
->FmtChans
, Device
->FmtType
);
253 FragSize
= Device
->UpdateSize
* FrameSize
;
255 IDirectSoundBuffer_GetCurrentPosition(data
->DSsbuffer
, &LastCursor
, NULL
);
256 while(!data
->killNow
)
258 // Get current play cursor
259 IDirectSoundBuffer_GetCurrentPosition(data
->DSsbuffer
, &PlayCursor
, NULL
);
260 avail
= (PlayCursor
-LastCursor
+DSBCaps
.dwBufferBytes
) % DSBCaps
.dwBufferBytes
;
266 err
= IDirectSoundBuffer_Play(data
->DSsbuffer
, 0, 0, DSBPLAY_LOOPING
);
269 ERR("Failed to play buffer: 0x%lx\n", err
);
270 aluHandleDisconnect(Device
);
276 avail
= WaitForSingleObjectEx(data
->NotifyEvent
, 2000, FALSE
);
277 if(avail
!= WAIT_OBJECT_0
)
278 ERR("WaitForSingleObjectEx error: 0x%lx\n", avail
);
281 avail
-= avail
%FragSize
;
283 // Lock output buffer
286 err
= IDirectSoundBuffer_Lock(data
->DSsbuffer
, LastCursor
, avail
, &WritePtr1
, &WriteCnt1
, &WritePtr2
, &WriteCnt2
, 0);
288 // If the buffer is lost, restore it and lock
289 if(err
== DSERR_BUFFERLOST
)
291 WARN("Buffer lost, restoring...\n");
292 err
= IDirectSoundBuffer_Restore(data
->DSsbuffer
);
297 err
= IDirectSoundBuffer_Lock(data
->DSsbuffer
, 0, DSBCaps
.dwBufferBytes
, &WritePtr1
, &WriteCnt1
, &WritePtr2
, &WriteCnt2
, 0);
301 // Successfully locked the output buffer
304 // If we have an active context, mix data directly into output buffer otherwise fill with silence
305 aluMixData(Device
, WritePtr1
, WriteCnt1
/FrameSize
);
306 aluMixData(Device
, WritePtr2
, WriteCnt2
/FrameSize
);
308 // Unlock output buffer only when successfully locked
309 IDirectSoundBuffer_Unlock(data
->DSsbuffer
, WritePtr1
, WriteCnt1
, WritePtr2
, WriteCnt2
);
313 ERR("Buffer lock error: %#lx\n", err
);
314 aluHandleDisconnect(Device
);
318 // Update old write cursor location
319 LastCursor
+= WriteCnt1
+WriteCnt2
;
320 LastCursor
%= DSBCaps
.dwBufferBytes
;
326 static ALCenum
DSoundOpenPlayback(ALCdevice
*device
, const ALCchar
*deviceName
)
328 DSoundPlaybackData
*data
= NULL
;
332 if(!PlaybackDeviceList
)
334 hr
= DirectSoundEnumerateA(DSoundEnumPlaybackDevices
, NULL
);
336 ERR("Error enumerating DirectSound devices (%#x)!\n", (unsigned int)hr
);
339 if(!deviceName
&& NumPlaybackDevices
> 0)
341 deviceName
= PlaybackDeviceList
[0].name
;
342 guid
= &PlaybackDeviceList
[0].guid
;
348 for(i
= 0;i
< NumPlaybackDevices
;i
++)
350 if(strcmp(deviceName
, PlaybackDeviceList
[i
].name
) == 0)
352 guid
= &PlaybackDeviceList
[i
].guid
;
356 if(i
== NumPlaybackDevices
)
357 return ALC_INVALID_VALUE
;
360 //Initialise requested device
361 data
= calloc(1, sizeof(DSoundPlaybackData
));
363 return ALC_OUT_OF_MEMORY
;
366 data
->NotifyEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
367 if(data
->NotifyEvent
== NULL
)
370 //DirectSound Init code
372 hr
= DirectSoundCreate(guid
, &data
->DS
, NULL
);
374 hr
= IDirectSound_SetCooperativeLevel(data
->DS
, GetForegroundWindow(), DSSCL_PRIORITY
);
378 IDirectSound_Release(data
->DS
);
379 if(data
->NotifyEvent
)
380 CloseHandle(data
->NotifyEvent
);
382 ERR("Device init failed: 0x%08lx\n", hr
);
383 return ALC_INVALID_VALUE
;
386 device
->DeviceName
= strdup(deviceName
);
387 device
->ExtraData
= data
;
391 static void DSoundClosePlayback(ALCdevice
*device
)
393 DSoundPlaybackData
*data
= device
->ExtraData
;
396 IDirectSoundNotify_Release(data
->DSnotify
);
397 data
->DSnotify
= NULL
;
399 IDirectSoundBuffer_Release(data
->DSsbuffer
);
400 data
->DSsbuffer
= NULL
;
401 if(data
->DSpbuffer
!= NULL
)
402 IDirectSoundBuffer_Release(data
->DSpbuffer
);
403 data
->DSpbuffer
= NULL
;
405 IDirectSound_Release(data
->DS
);
406 CloseHandle(data
->NotifyEvent
);
408 device
->ExtraData
= NULL
;
411 static ALCboolean
DSoundResetPlayback(ALCdevice
*device
)
413 DSoundPlaybackData
*data
= (DSoundPlaybackData
*)device
->ExtraData
;
414 DSBUFFERDESC DSBDescription
;
415 WAVEFORMATEXTENSIBLE OutputType
;
419 memset(&OutputType
, 0, sizeof(OutputType
));
422 IDirectSoundNotify_Release(data
->DSnotify
);
423 data
->DSnotify
= NULL
;
425 IDirectSoundBuffer_Release(data
->DSsbuffer
);
426 data
->DSsbuffer
= NULL
;
427 if(data
->DSpbuffer
!= NULL
)
428 IDirectSoundBuffer_Release(data
->DSpbuffer
);
429 data
->DSpbuffer
= NULL
;
431 switch(device
->FmtType
)
434 device
->FmtType
= DevFmtUByte
;
437 device
->FmtType
= DevFmtShort
;
440 device
->FmtType
= DevFmtInt
;
449 hr
= IDirectSound_GetSpeakerConfig(data
->DS
, &speakers
);
452 if(!(device
->Flags
&DEVICE_CHANNELS_REQUEST
))
454 speakers
= DSSPEAKER_CONFIG(speakers
);
455 if(speakers
== DSSPEAKER_MONO
)
456 device
->FmtChans
= DevFmtMono
;
457 else if(speakers
== DSSPEAKER_STEREO
|| speakers
== DSSPEAKER_HEADPHONE
)
458 device
->FmtChans
= DevFmtStereo
;
459 else if(speakers
== DSSPEAKER_QUAD
)
460 device
->FmtChans
= DevFmtQuad
;
461 else if(speakers
== DSSPEAKER_5POINT1
)
462 device
->FmtChans
= DevFmtX51
;
463 else if(speakers
== DSSPEAKER_7POINT1
)
464 device
->FmtChans
= DevFmtX71
;
466 ERR("Unknown system speaker config: 0x%lx\n", speakers
);
469 switch(device
->FmtChans
)
472 OutputType
.dwChannelMask
= SPEAKER_FRONT_CENTER
;
475 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
479 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
480 SPEAKER_FRONT_RIGHT
|
485 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
486 SPEAKER_FRONT_RIGHT
|
487 SPEAKER_FRONT_CENTER
|
488 SPEAKER_LOW_FREQUENCY
|
493 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
494 SPEAKER_FRONT_RIGHT
|
495 SPEAKER_FRONT_CENTER
|
496 SPEAKER_LOW_FREQUENCY
|
501 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
502 SPEAKER_FRONT_RIGHT
|
503 SPEAKER_FRONT_CENTER
|
504 SPEAKER_LOW_FREQUENCY
|
505 SPEAKER_BACK_CENTER
|
510 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
511 SPEAKER_FRONT_RIGHT
|
512 SPEAKER_FRONT_CENTER
|
513 SPEAKER_LOW_FREQUENCY
|
523 OutputType
.Format
.wFormatTag
= WAVE_FORMAT_PCM
;
524 OutputType
.Format
.nChannels
= ChannelsFromDevFmt(device
->FmtChans
);
525 OutputType
.Format
.wBitsPerSample
= BytesFromDevFmt(device
->FmtType
) * 8;
526 OutputType
.Format
.nBlockAlign
= OutputType
.Format
.nChannels
*OutputType
.Format
.wBitsPerSample
/8;
527 OutputType
.Format
.nSamplesPerSec
= device
->Frequency
;
528 OutputType
.Format
.nAvgBytesPerSec
= OutputType
.Format
.nSamplesPerSec
*OutputType
.Format
.nBlockAlign
;
529 OutputType
.Format
.cbSize
= 0;
532 if(OutputType
.Format
.nChannels
> 2 || device
->FmtType
== DevFmtFloat
)
534 OutputType
.Format
.wFormatTag
= WAVE_FORMAT_EXTENSIBLE
;
535 OutputType
.Samples
.wValidBitsPerSample
= OutputType
.Format
.wBitsPerSample
;
536 OutputType
.Format
.cbSize
= sizeof(WAVEFORMATEXTENSIBLE
) - sizeof(WAVEFORMATEX
);
537 if(device
->FmtType
== DevFmtFloat
)
538 OutputType
.SubFormat
= KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
;
540 OutputType
.SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
543 IDirectSoundBuffer_Release(data
->DSpbuffer
);
544 data
->DSpbuffer
= NULL
;
550 memset(&DSBDescription
,0,sizeof(DSBUFFERDESC
));
551 DSBDescription
.dwSize
=sizeof(DSBUFFERDESC
);
552 DSBDescription
.dwFlags
=DSBCAPS_PRIMARYBUFFER
;
553 hr
= IDirectSound_CreateSoundBuffer(data
->DS
, &DSBDescription
, &data
->DSpbuffer
, NULL
);
556 hr
= IDirectSoundBuffer_SetFormat(data
->DSpbuffer
,&OutputType
.Format
);
561 if(device
->NumUpdates
> MAX_UPDATES
)
563 device
->UpdateSize
= (device
->UpdateSize
*device
->NumUpdates
+
564 MAX_UPDATES
-1) / MAX_UPDATES
;
565 device
->NumUpdates
= MAX_UPDATES
;
568 memset(&DSBDescription
,0,sizeof(DSBUFFERDESC
));
569 DSBDescription
.dwSize
=sizeof(DSBUFFERDESC
);
570 DSBDescription
.dwFlags
=DSBCAPS_CTRLPOSITIONNOTIFY
|DSBCAPS_GETCURRENTPOSITION2
|DSBCAPS_GLOBALFOCUS
;
571 DSBDescription
.dwBufferBytes
=device
->UpdateSize
* device
->NumUpdates
*
572 OutputType
.Format
.nBlockAlign
;
573 DSBDescription
.lpwfxFormat
=&OutputType
.Format
;
574 hr
= IDirectSound_CreateSoundBuffer(data
->DS
, &DSBDescription
, &data
->DSsbuffer
, NULL
);
575 if(FAILED(hr
) && device
->FmtType
== DevFmtFloat
)
577 device
->FmtType
= DevFmtShort
;
584 hr
= IDirectSoundBuffer_QueryInterface(data
->DSsbuffer
, &IID_IDirectSoundNotify
, (LPVOID
*)&data
->DSnotify
);
587 DSBPOSITIONNOTIFY notifies
[MAX_UPDATES
];
590 for(i
= 0;i
< device
->NumUpdates
;++i
)
592 notifies
[i
].dwOffset
= i
* device
->UpdateSize
*
593 OutputType
.Format
.nBlockAlign
;
594 notifies
[i
].hEventNotify
= data
->NotifyEvent
;
596 if(IDirectSoundNotify_SetNotificationPositions(data
->DSnotify
, device
->NumUpdates
, notifies
) != DS_OK
)
603 if(data
->DSnotify
!= NULL
)
604 IDirectSoundNotify_Release(data
->DSnotify
);
605 data
->DSnotify
= NULL
;
606 if(data
->DSsbuffer
!= NULL
)
607 IDirectSoundBuffer_Release(data
->DSsbuffer
);
608 data
->DSsbuffer
= NULL
;
609 if(data
->DSpbuffer
!= NULL
)
610 IDirectSoundBuffer_Release(data
->DSpbuffer
);
611 data
->DSpbuffer
= NULL
;
615 ResetEvent(data
->NotifyEvent
);
616 SetDefaultWFXChannelOrder(device
);
621 static ALCboolean
DSoundStartPlayback(ALCdevice
*device
)
623 DSoundPlaybackData
*data
= (DSoundPlaybackData
*)device
->ExtraData
;
625 data
->thread
= StartThread(DSoundPlaybackProc
, device
);
626 if(data
->thread
== NULL
)
632 static void DSoundStopPlayback(ALCdevice
*device
)
634 DSoundPlaybackData
*data
= device
->ExtraData
;
640 StopThread(data
->thread
);
644 IDirectSoundBuffer_Stop(data
->DSsbuffer
);
648 static ALCenum
DSoundOpenCapture(ALCdevice
*device
, const ALCchar
*deviceName
)
650 DSoundCaptureData
*data
= NULL
;
651 WAVEFORMATEXTENSIBLE InputType
;
652 DSCBUFFERDESC DSCBDescription
;
657 if(!CaptureDeviceList
)
659 /* Initialize COM to prevent name truncation */
660 hrcom
= CoInitialize(NULL
);
661 hr
= DirectSoundCaptureEnumerateA(DSoundEnumCaptureDevices
, NULL
);
663 ERR("Error enumerating DirectSound devices (%#x)!\n", (unsigned int)hr
);
668 if(!deviceName
&& NumCaptureDevices
> 0)
670 deviceName
= CaptureDeviceList
[0].name
;
671 guid
= &CaptureDeviceList
[0].guid
;
677 for(i
= 0;i
< NumCaptureDevices
;i
++)
679 if(strcmp(deviceName
, CaptureDeviceList
[i
].name
) == 0)
681 guid
= &CaptureDeviceList
[i
].guid
;
685 if(i
== NumCaptureDevices
)
686 return ALC_INVALID_VALUE
;
689 switch(device
->FmtType
)
694 WARN("%s capture samples not supported\n", DevFmtTypeString(device
->FmtType
));
695 return ALC_INVALID_ENUM
;
704 //Initialise requested device
705 data
= calloc(1, sizeof(DSoundCaptureData
));
707 return ALC_OUT_OF_MEMORY
;
711 //DirectSoundCapture Init code
713 hr
= DirectSoundCaptureCreate(guid
, &data
->DSC
, NULL
);
716 memset(&InputType
, 0, sizeof(InputType
));
718 switch(device
->FmtChans
)
721 InputType
.dwChannelMask
= SPEAKER_FRONT_CENTER
;
724 InputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
728 InputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
729 SPEAKER_FRONT_RIGHT
|
734 InputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
735 SPEAKER_FRONT_RIGHT
|
736 SPEAKER_FRONT_CENTER
|
737 SPEAKER_LOW_FREQUENCY
|
742 InputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
743 SPEAKER_FRONT_RIGHT
|
744 SPEAKER_FRONT_CENTER
|
745 SPEAKER_LOW_FREQUENCY
|
750 InputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
751 SPEAKER_FRONT_RIGHT
|
752 SPEAKER_FRONT_CENTER
|
753 SPEAKER_LOW_FREQUENCY
|
754 SPEAKER_BACK_CENTER
|
759 InputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
760 SPEAKER_FRONT_RIGHT
|
761 SPEAKER_FRONT_CENTER
|
762 SPEAKER_LOW_FREQUENCY
|
770 InputType
.Format
.wFormatTag
= WAVE_FORMAT_PCM
;
771 InputType
.Format
.nChannels
= ChannelsFromDevFmt(device
->FmtChans
);
772 InputType
.Format
.wBitsPerSample
= BytesFromDevFmt(device
->FmtType
) * 8;
773 InputType
.Format
.nBlockAlign
= InputType
.Format
.nChannels
*InputType
.Format
.wBitsPerSample
/8;
774 InputType
.Format
.nSamplesPerSec
= device
->Frequency
;
775 InputType
.Format
.nAvgBytesPerSec
= InputType
.Format
.nSamplesPerSec
*InputType
.Format
.nBlockAlign
;
776 InputType
.Format
.cbSize
= 0;
778 if(InputType
.Format
.nChannels
> 2 || device
->FmtType
== DevFmtFloat
)
780 InputType
.Format
.wFormatTag
= WAVE_FORMAT_EXTENSIBLE
;
781 InputType
.Format
.cbSize
= sizeof(WAVEFORMATEXTENSIBLE
) - sizeof(WAVEFORMATEX
);
782 InputType
.Samples
.wValidBitsPerSample
= InputType
.Format
.wBitsPerSample
;
783 if(device
->FmtType
== DevFmtFloat
)
784 InputType
.SubFormat
= KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
;
786 InputType
.SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
789 samples
= device
->UpdateSize
* device
->NumUpdates
;
790 samples
= maxu(samples
, 100 * device
->Frequency
/ 1000);
792 memset(&DSCBDescription
, 0, sizeof(DSCBUFFERDESC
));
793 DSCBDescription
.dwSize
= sizeof(DSCBUFFERDESC
);
794 DSCBDescription
.dwFlags
= 0;
795 DSCBDescription
.dwBufferBytes
= samples
* InputType
.Format
.nBlockAlign
;
796 DSCBDescription
.lpwfxFormat
= &InputType
.Format
;
798 hr
= IDirectSoundCapture_CreateCaptureBuffer(data
->DSC
, &DSCBDescription
, &data
->DSCbuffer
, NULL
);
802 data
->Ring
= CreateRingBuffer(InputType
.Format
.nBlockAlign
, device
->UpdateSize
* device
->NumUpdates
);
803 if(data
->Ring
== NULL
)
804 hr
= DSERR_OUTOFMEMORY
;
809 ERR("Device init failed: 0x%08lx\n", hr
);
811 DestroyRingBuffer(data
->Ring
);
813 if(data
->DSCbuffer
!= NULL
)
814 IDirectSoundCaptureBuffer_Release(data
->DSCbuffer
);
815 data
->DSCbuffer
= NULL
;
817 IDirectSoundCapture_Release(data
->DSC
);
821 return ALC_INVALID_VALUE
;
824 data
->BufferBytes
= DSCBDescription
.dwBufferBytes
;
825 SetDefaultWFXChannelOrder(device
);
827 device
->DeviceName
= strdup(deviceName
);
828 device
->ExtraData
= data
;
833 static void DSoundCloseCapture(ALCdevice
*device
)
835 DSoundCaptureData
*data
= device
->ExtraData
;
837 DestroyRingBuffer(data
->Ring
);
840 if(data
->DSCbuffer
!= NULL
)
842 IDirectSoundCaptureBuffer_Stop(data
->DSCbuffer
);
843 IDirectSoundCaptureBuffer_Release(data
->DSCbuffer
);
844 data
->DSCbuffer
= NULL
;
847 IDirectSoundCapture_Release(data
->DSC
);
851 device
->ExtraData
= NULL
;
854 static void DSoundStartCapture(ALCdevice
*device
)
856 DSoundCaptureData
*data
= device
->ExtraData
;
859 hr
= IDirectSoundCaptureBuffer_Start(data
->DSCbuffer
, DSCBSTART_LOOPING
);
862 ERR("start failed: 0x%08lx\n", hr
);
863 aluHandleDisconnect(device
);
867 static void DSoundStopCapture(ALCdevice
*device
)
869 DSoundCaptureData
*data
= device
->ExtraData
;
872 hr
= IDirectSoundCaptureBuffer_Stop(data
->DSCbuffer
);
875 ERR("stop failed: 0x%08lx\n", hr
);
876 aluHandleDisconnect(device
);
880 static ALCenum
DSoundCaptureSamples(ALCdevice
*Device
, ALCvoid
*pBuffer
, ALCuint lSamples
)
882 DSoundCaptureData
*data
= Device
->ExtraData
;
883 ReadRingBuffer(data
->Ring
, pBuffer
, lSamples
);
887 static ALCuint
DSoundAvailableSamples(ALCdevice
*Device
)
889 DSoundCaptureData
*data
= Device
->ExtraData
;
890 DWORD ReadCursor
, LastCursor
, BufferBytes
, NumBytes
;
891 VOID
*ReadPtr1
, *ReadPtr2
;
892 DWORD ReadCnt1
, ReadCnt2
;
896 if(!Device
->Connected
)
899 FrameSize
= FrameSizeFromDevFmt(Device
->FmtChans
, Device
->FmtType
);
900 BufferBytes
= data
->BufferBytes
;
901 LastCursor
= data
->Cursor
;
903 hr
= IDirectSoundCaptureBuffer_GetCurrentPosition(data
->DSCbuffer
, NULL
, &ReadCursor
);
906 NumBytes
= (ReadCursor
-LastCursor
+ BufferBytes
) % BufferBytes
;
909 hr
= IDirectSoundCaptureBuffer_Lock(data
->DSCbuffer
, LastCursor
, NumBytes
,
910 &ReadPtr1
, &ReadCnt1
,
911 &ReadPtr2
, &ReadCnt2
, 0);
915 WriteRingBuffer(data
->Ring
, ReadPtr1
, ReadCnt1
/FrameSize
);
917 WriteRingBuffer(data
->Ring
, ReadPtr2
, ReadCnt2
/FrameSize
);
918 hr
= IDirectSoundCaptureBuffer_Unlock(data
->DSCbuffer
,
921 data
->Cursor
= (LastCursor
+ReadCnt1
+ReadCnt2
) % BufferBytes
;
926 ERR("update failed: 0x%08lx\n", hr
);
927 aluHandleDisconnect(Device
);
931 return RingBufferSize(data
->Ring
);
934 static const BackendFuncs DSoundFuncs
= {
944 DSoundCaptureSamples
,
945 DSoundAvailableSamples
949 ALCboolean
alcDSoundInit(BackendFuncs
*FuncList
)
953 *FuncList
= DSoundFuncs
;
957 void alcDSoundDeinit(void)
961 for(i
= 0;i
< NumPlaybackDevices
;++i
)
962 free(PlaybackDeviceList
[i
].name
);
963 free(PlaybackDeviceList
);
964 PlaybackDeviceList
= NULL
;
965 NumPlaybackDevices
= 0;
967 for(i
= 0;i
< NumCaptureDevices
;++i
)
968 free(CaptureDeviceList
[i
].name
);
969 free(CaptureDeviceList
);
970 CaptureDeviceList
= NULL
;
971 NumCaptureDevices
= 0;
978 void alcDSoundProbe(enum DevProbe type
)
985 case ALL_DEVICE_PROBE
:
986 for(i
= 0;i
< NumPlaybackDevices
;++i
)
987 free(PlaybackDeviceList
[i
].name
);
988 free(PlaybackDeviceList
);
989 PlaybackDeviceList
= NULL
;
990 NumPlaybackDevices
= 0;
992 hr
= DirectSoundEnumerateA(DSoundEnumPlaybackDevices
, NULL
);
994 ERR("Error enumerating DirectSound playback devices (%#x)!\n", (unsigned int)hr
);
997 for(i
= 0;i
< NumPlaybackDevices
;i
++)
998 AppendAllDeviceList(PlaybackDeviceList
[i
].name
);
1002 case CAPTURE_DEVICE_PROBE
:
1003 for(i
= 0;i
< NumCaptureDevices
;++i
)
1004 free(CaptureDeviceList
[i
].name
);
1005 free(CaptureDeviceList
);
1006 CaptureDeviceList
= NULL
;
1007 NumCaptureDevices
= 0;
1009 /* Initialize COM to prevent name truncation */
1010 hrcom
= CoInitialize(NULL
);
1011 hr
= DirectSoundCaptureEnumerateA(DSoundEnumCaptureDevices
, NULL
);
1013 ERR("Error enumerating DirectSound capture devices (%#x)!\n", (unsigned int)hr
);
1016 for(i
= 0;i
< NumCaptureDevices
;i
++)
1017 AppendCaptureDeviceList(CaptureDeviceList
[i
].name
);
1019 if(SUCCEEDED(hrcom
))