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
23 #include "backends/dsound.h"
25 #define WIN32_LEAN_AND_MEAN
34 #ifndef _WAVEFORMATEXTENSIBLE_
49 #include "ringbuffer.h"
55 /* MinGW-w64 needs this for some unknown reason now. */
56 using LPCWAVEFORMATEX
= const WAVEFORMATEX
*;
60 #ifndef DSSPEAKER_5POINT1
61 # define DSSPEAKER_5POINT1 0x00000006
63 #ifndef DSSPEAKER_5POINT1_BACK
64 # define DSSPEAKER_5POINT1_BACK 0x00000006
66 #ifndef DSSPEAKER_7POINT1
67 # define DSSPEAKER_7POINT1 0x00000007
69 #ifndef DSSPEAKER_7POINT1_SURROUND
70 # define DSSPEAKER_7POINT1_SURROUND 0x00000008
72 #ifndef DSSPEAKER_5POINT1_SURROUND
73 # define DSSPEAKER_5POINT1_SURROUND 0x00000009
77 /* Some headers seem to define these as macros for __uuidof, which is annoying
78 * since some headers don't declare them at all. Hopefully the ifdef is enough
79 * to tell if they need to be declared.
81 #ifndef KSDATAFORMAT_SUBTYPE_PCM
82 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_PCM
, 0x00000001, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
84 #ifndef KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
85 DEFINE_GUID(KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
, 0x00000003, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71);
90 #define DEVNAME_HEAD "OpenAL Soft on "
95 HRESULT (WINAPI
*pDirectSoundCreate
)(const GUID
*pcGuidDevice
, IDirectSound
**ppDS
, IUnknown
*pUnkOuter
);
96 HRESULT (WINAPI
*pDirectSoundEnumerateW
)(LPDSENUMCALLBACKW pDSEnumCallback
, void *pContext
);
97 HRESULT (WINAPI
*pDirectSoundCaptureCreate
)(const GUID
*pcGuidDevice
, IDirectSoundCapture
**ppDSC
, IUnknown
*pUnkOuter
);
98 HRESULT (WINAPI
*pDirectSoundCaptureEnumerateW
)(LPDSENUMCALLBACKW pDSEnumCallback
, void *pContext
);
100 #ifndef IN_IDE_PARSER
101 #define DirectSoundCreate pDirectSoundCreate
102 #define DirectSoundEnumerateW pDirectSoundEnumerateW
103 #define DirectSoundCaptureCreate pDirectSoundCaptureCreate
104 #define DirectSoundCaptureEnumerateW pDirectSoundCaptureEnumerateW
109 #define MAX_UPDATES 128
115 template<typename T0
, typename T1
>
116 DevMap(T0
&& name_
, T1
&& guid_
)
117 : name
{std::forward
<T0
>(name_
)}, guid
{std::forward
<T1
>(guid_
)}
121 al::vector
<DevMap
> PlaybackDevices
;
122 al::vector
<DevMap
> CaptureDevices
;
124 bool checkName(const al::vector
<DevMap
> &list
, const std::string
&name
)
126 return std::find_if(list
.cbegin(), list
.cend(),
127 [&name
](const DevMap
&entry
) -> bool
128 { return entry
.name
== name
; }
132 BOOL CALLBACK
DSoundEnumDevices(GUID
*guid
, const WCHAR
*desc
, const WCHAR
*, void *data
)
137 auto& devices
= *static_cast<al::vector
<DevMap
>*>(data
);
138 const std::string basename
{DEVNAME_HEAD
+ wstr_to_utf8(desc
)};
141 std::string newname
{basename
};
142 while(checkName(devices
, newname
))
146 newname
+= std::to_string(++count
);
148 devices
.emplace_back(std::move(newname
), *guid
);
149 const DevMap
&newentry
= devices
.back();
151 OLECHAR
*guidstr
{nullptr};
152 HRESULT hr
{StringFromCLSID(*guid
, &guidstr
)};
155 TRACE("Got device \"%s\", GUID \"%ls\"\n", newentry
.name
.c_str(), guidstr
);
156 CoTaskMemFree(guidstr
);
163 struct DSoundPlayback final
: public BackendBase
{
164 DSoundPlayback(ALCdevice
*device
) noexcept
: BackendBase
{device
} { }
165 ~DSoundPlayback() override
;
169 ALCenum
open(const ALCchar
*name
) override
;
170 bool reset() override
;
171 bool start() override
;
172 void stop() override
;
174 IDirectSound
*mDS
{nullptr};
175 IDirectSoundBuffer
*mPrimaryBuffer
{nullptr};
176 IDirectSoundBuffer
*mBuffer
{nullptr};
177 IDirectSoundNotify
*mNotifies
{nullptr};
178 HANDLE mNotifyEvent
{nullptr};
180 std::atomic
<bool> mKillNow
{true};
183 DEF_NEWDEL(DSoundPlayback
)
186 DSoundPlayback::~DSoundPlayback()
189 mNotifies
->Release();
195 mPrimaryBuffer
->Release();
196 mPrimaryBuffer
= nullptr;
202 CloseHandle(mNotifyEvent
);
203 mNotifyEvent
= nullptr;
207 FORCE_ALIGN
int DSoundPlayback::mixerProc()
210 althrd_setname(MIXER_THREAD_NAME
);
213 DSBCaps
.dwSize
= sizeof(DSBCaps
);
214 HRESULT err
{mBuffer
->GetCaps(&DSBCaps
)};
217 ERR("Failed to get buffer caps: 0x%lx\n", err
);
218 aluHandleDisconnect(mDevice
, "Failure retrieving playback buffer info: 0x%lx", err
);
222 ALuint FrameSize
{mDevice
->frameSizeFromFmt()};
223 DWORD FragSize
{mDevice
->UpdateSize
* FrameSize
};
226 DWORD LastCursor
{0u};
227 mBuffer
->GetCurrentPosition(&LastCursor
, nullptr);
228 while(!mKillNow
.load(std::memory_order_acquire
) &&
229 mDevice
->Connected
.load(std::memory_order_acquire
))
231 // Get current play cursor
233 mBuffer
->GetCurrentPosition(&PlayCursor
, nullptr);
234 DWORD avail
= (PlayCursor
-LastCursor
+DSBCaps
.dwBufferBytes
) % DSBCaps
.dwBufferBytes
;
240 err
= mBuffer
->Play(0, 0, DSBPLAY_LOOPING
);
243 ERR("Failed to play buffer: 0x%lx\n", err
);
244 aluHandleDisconnect(mDevice
, "Failure starting playback: 0x%lx", err
);
250 avail
= WaitForSingleObjectEx(mNotifyEvent
, 2000, FALSE
);
251 if(avail
!= WAIT_OBJECT_0
)
252 ERR("WaitForSingleObjectEx error: 0x%lx\n", avail
);
255 avail
-= avail
%FragSize
;
257 // Lock output buffer
258 void *WritePtr1
, *WritePtr2
;
259 DWORD WriteCnt1
{0u}, WriteCnt2
{0u};
260 err
= mBuffer
->Lock(LastCursor
, avail
, &WritePtr1
, &WriteCnt1
, &WritePtr2
, &WriteCnt2
, 0);
262 // If the buffer is lost, restore it and lock
263 if(err
== DSERR_BUFFERLOST
)
265 WARN("Buffer lost, restoring...\n");
266 err
= mBuffer
->Restore();
271 err
= mBuffer
->Lock(0, DSBCaps
.dwBufferBytes
, &WritePtr1
, &WriteCnt1
,
272 &WritePtr2
, &WriteCnt2
, 0);
279 aluMixData(mDevice
, WritePtr1
, WriteCnt1
/FrameSize
);
281 aluMixData(mDevice
, WritePtr2
, WriteCnt2
/FrameSize
);
284 mBuffer
->Unlock(WritePtr1
, WriteCnt1
, WritePtr2
, WriteCnt2
);
288 ERR("Buffer lock error: %#lx\n", err
);
289 aluHandleDisconnect(mDevice
, "Failed to lock output buffer: 0x%lx", err
);
293 // Update old write cursor location
294 LastCursor
+= WriteCnt1
+WriteCnt2
;
295 LastCursor
%= DSBCaps
.dwBufferBytes
;
301 ALCenum
DSoundPlayback::open(const ALCchar
*name
)
304 if(PlaybackDevices
.empty())
306 /* Initialize COM to prevent name truncation */
307 HRESULT hrcom
{CoInitialize(nullptr)};
308 hr
= DirectSoundEnumerateW(DSoundEnumDevices
, &PlaybackDevices
);
310 ERR("Error enumerating DirectSound devices (0x%lx)!\n", hr
);
315 const GUID
*guid
{nullptr};
316 if(!name
&& !PlaybackDevices
.empty())
318 name
= PlaybackDevices
[0].name
.c_str();
319 guid
= &PlaybackDevices
[0].guid
;
323 auto iter
= std::find_if(PlaybackDevices
.cbegin(), PlaybackDevices
.cend(),
324 [name
](const DevMap
&entry
) -> bool
325 { return entry
.name
== name
; }
327 if(iter
== PlaybackDevices
.cend())
328 return ALC_INVALID_VALUE
;
333 mNotifyEvent
= CreateEventW(nullptr, FALSE
, FALSE
, nullptr);
334 if(!mNotifyEvent
) hr
= E_FAIL
;
336 //DirectSound Init code
338 hr
= DirectSoundCreate(guid
, &mDS
, nullptr);
340 hr
= mDS
->SetCooperativeLevel(GetForegroundWindow(), DSSCL_PRIORITY
);
343 ERR("Device init failed: 0x%08lx\n", hr
);
344 return ALC_INVALID_VALUE
;
347 mDevice
->DeviceName
= name
;
351 bool DSoundPlayback::reset()
354 mNotifies
->Release();
360 mPrimaryBuffer
->Release();
361 mPrimaryBuffer
= nullptr;
363 switch(mDevice
->FmtType
)
366 mDevice
->FmtType
= DevFmtUByte
;
369 if(mDevice
->Flags
.get
<SampleTypeRequest
>())
373 mDevice
->FmtType
= DevFmtShort
;
376 mDevice
->FmtType
= DevFmtInt
;
384 WAVEFORMATEXTENSIBLE OutputType
{};
386 HRESULT hr
{mDS
->GetSpeakerConfig(&speakers
)};
389 speakers
= DSSPEAKER_CONFIG(speakers
);
390 if(!mDevice
->Flags
.get
<ChannelsRequest
>())
392 if(speakers
== DSSPEAKER_MONO
)
393 mDevice
->FmtChans
= DevFmtMono
;
394 else if(speakers
== DSSPEAKER_STEREO
|| speakers
== DSSPEAKER_HEADPHONE
)
395 mDevice
->FmtChans
= DevFmtStereo
;
396 else if(speakers
== DSSPEAKER_QUAD
)
397 mDevice
->FmtChans
= DevFmtQuad
;
398 else if(speakers
== DSSPEAKER_5POINT1_SURROUND
)
399 mDevice
->FmtChans
= DevFmtX51
;
400 else if(speakers
== DSSPEAKER_5POINT1_BACK
)
401 mDevice
->FmtChans
= DevFmtX51Rear
;
402 else if(speakers
== DSSPEAKER_7POINT1
|| speakers
== DSSPEAKER_7POINT1_SURROUND
)
403 mDevice
->FmtChans
= DevFmtX71
;
405 ERR("Unknown system speaker config: 0x%lx\n", speakers
);
407 mDevice
->IsHeadphones
= (mDevice
->FmtChans
== DevFmtStereo
&&
408 speakers
== DSSPEAKER_HEADPHONE
);
410 switch(mDevice
->FmtChans
)
413 OutputType
.dwChannelMask
= SPEAKER_FRONT_CENTER
;
416 mDevice
->FmtChans
= DevFmtStereo
;
419 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
423 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
424 SPEAKER_FRONT_RIGHT
|
429 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
430 SPEAKER_FRONT_RIGHT
|
431 SPEAKER_FRONT_CENTER
|
432 SPEAKER_LOW_FREQUENCY
|
437 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
438 SPEAKER_FRONT_RIGHT
|
439 SPEAKER_FRONT_CENTER
|
440 SPEAKER_LOW_FREQUENCY
|
445 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
446 SPEAKER_FRONT_RIGHT
|
447 SPEAKER_FRONT_CENTER
|
448 SPEAKER_LOW_FREQUENCY
|
449 SPEAKER_BACK_CENTER
|
454 OutputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
455 SPEAKER_FRONT_RIGHT
|
456 SPEAKER_FRONT_CENTER
|
457 SPEAKER_LOW_FREQUENCY
|
467 OutputType
.Format
.wFormatTag
= WAVE_FORMAT_PCM
;
468 OutputType
.Format
.nChannels
= static_cast<WORD
>(mDevice
->channelsFromFmt());
469 OutputType
.Format
.wBitsPerSample
= static_cast<WORD
>(mDevice
->bytesFromFmt() * 8);
470 OutputType
.Format
.nBlockAlign
= static_cast<WORD
>(OutputType
.Format
.nChannels
*
471 OutputType
.Format
.wBitsPerSample
/ 8);
472 OutputType
.Format
.nSamplesPerSec
= mDevice
->Frequency
;
473 OutputType
.Format
.nAvgBytesPerSec
= OutputType
.Format
.nSamplesPerSec
*
474 OutputType
.Format
.nBlockAlign
;
475 OutputType
.Format
.cbSize
= 0;
478 if(OutputType
.Format
.nChannels
> 2 || mDevice
->FmtType
== DevFmtFloat
)
480 OutputType
.Format
.wFormatTag
= WAVE_FORMAT_EXTENSIBLE
;
481 OutputType
.Samples
.wValidBitsPerSample
= OutputType
.Format
.wBitsPerSample
;
482 OutputType
.Format
.cbSize
= sizeof(WAVEFORMATEXTENSIBLE
) - sizeof(WAVEFORMATEX
);
483 if(mDevice
->FmtType
== DevFmtFloat
)
484 OutputType
.SubFormat
= KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
;
486 OutputType
.SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
489 mPrimaryBuffer
->Release();
490 mPrimaryBuffer
= nullptr;
494 if(SUCCEEDED(hr
) && !mPrimaryBuffer
)
496 DSBUFFERDESC DSBDescription
{};
497 DSBDescription
.dwSize
= sizeof(DSBDescription
);
498 DSBDescription
.dwFlags
= DSBCAPS_PRIMARYBUFFER
;
499 hr
= mDS
->CreateSoundBuffer(&DSBDescription
, &mPrimaryBuffer
, nullptr);
502 hr
= mPrimaryBuffer
->SetFormat(&OutputType
.Format
);
507 ALuint num_updates
{mDevice
->BufferSize
/ mDevice
->UpdateSize
};
508 if(num_updates
> MAX_UPDATES
)
509 num_updates
= MAX_UPDATES
;
510 mDevice
->BufferSize
= mDevice
->UpdateSize
* num_updates
;
512 DSBUFFERDESC DSBDescription
{};
513 DSBDescription
.dwSize
= sizeof(DSBDescription
);
514 DSBDescription
.dwFlags
= DSBCAPS_CTRLPOSITIONNOTIFY
| DSBCAPS_GETCURRENTPOSITION2
|
516 DSBDescription
.dwBufferBytes
= mDevice
->BufferSize
* OutputType
.Format
.nBlockAlign
;
517 DSBDescription
.lpwfxFormat
= &OutputType
.Format
;
519 hr
= mDS
->CreateSoundBuffer(&DSBDescription
, &mBuffer
, nullptr);
520 if(FAILED(hr
) && mDevice
->FmtType
== DevFmtFloat
)
522 mDevice
->FmtType
= DevFmtShort
;
530 hr
= mBuffer
->QueryInterface(IID_IDirectSoundNotify
, &ptr
);
533 auto Notifies
= static_cast<IDirectSoundNotify
*>(ptr
);
534 mNotifies
= Notifies
;
536 ALuint num_updates
{mDevice
->BufferSize
/ mDevice
->UpdateSize
};
537 assert(num_updates
<= MAX_UPDATES
);
539 std::array
<DSBPOSITIONNOTIFY
,MAX_UPDATES
> nots
;
540 for(ALuint i
{0};i
< num_updates
;++i
)
542 nots
[i
].dwOffset
= i
* mDevice
->UpdateSize
* OutputType
.Format
.nBlockAlign
;
543 nots
[i
].hEventNotify
= mNotifyEvent
;
545 if(Notifies
->SetNotificationPositions(num_updates
, nots
.data()) != DS_OK
)
553 mNotifies
->Release();
559 mPrimaryBuffer
->Release();
560 mPrimaryBuffer
= nullptr;
564 ResetEvent(mNotifyEvent
);
565 SetDefaultWFXChannelOrder(mDevice
);
570 bool DSoundPlayback::start()
573 mKillNow
.store(false, std::memory_order_release
);
574 mThread
= std::thread
{std::mem_fn(&DSoundPlayback::mixerProc
), this};
577 catch(std::exception
& e
) {
578 ERR("Failed to start mixing thread: %s\n", e
.what());
585 void DSoundPlayback::stop()
587 if(mKillNow
.exchange(true, std::memory_order_acq_rel
) || !mThread
.joinable())
595 struct DSoundCapture final
: public BackendBase
{
596 DSoundCapture(ALCdevice
*device
) noexcept
: BackendBase
{device
} { }
597 ~DSoundCapture() override
;
599 ALCenum
open(const ALCchar
*name
) override
;
600 bool start() override
;
601 void stop() override
;
602 ALCenum
captureSamples(al::byte
*buffer
, ALCuint samples
) override
;
603 ALCuint
availableSamples() override
;
605 IDirectSoundCapture
*mDSC
{nullptr};
606 IDirectSoundCaptureBuffer
*mDSCbuffer
{nullptr};
607 DWORD mBufferBytes
{0u};
612 DEF_NEWDEL(DSoundCapture
)
615 DSoundCapture::~DSoundCapture()
620 mDSCbuffer
->Release();
621 mDSCbuffer
= nullptr;
630 ALCenum
DSoundCapture::open(const ALCchar
*name
)
633 if(CaptureDevices
.empty())
635 /* Initialize COM to prevent name truncation */
636 HRESULT hrcom
{CoInitialize(nullptr)};
637 hr
= DirectSoundCaptureEnumerateW(DSoundEnumDevices
, &CaptureDevices
);
639 ERR("Error enumerating DirectSound devices (0x%lx)!\n", hr
);
644 const GUID
*guid
{nullptr};
645 if(!name
&& !CaptureDevices
.empty())
647 name
= CaptureDevices
[0].name
.c_str();
648 guid
= &CaptureDevices
[0].guid
;
652 auto iter
= std::find_if(CaptureDevices
.cbegin(), CaptureDevices
.cend(),
653 [name
](const DevMap
&entry
) -> bool
654 { return entry
.name
== name
; }
656 if(iter
== CaptureDevices
.cend())
657 return ALC_INVALID_VALUE
;
661 switch(mDevice
->FmtType
)
666 WARN("%s capture samples not supported\n", DevFmtTypeString(mDevice
->FmtType
));
667 return ALC_INVALID_ENUM
;
676 WAVEFORMATEXTENSIBLE InputType
{};
677 switch(mDevice
->FmtChans
)
680 InputType
.dwChannelMask
= SPEAKER_FRONT_CENTER
;
683 InputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
687 InputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
688 SPEAKER_FRONT_RIGHT
|
693 InputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
694 SPEAKER_FRONT_RIGHT
|
695 SPEAKER_FRONT_CENTER
|
696 SPEAKER_LOW_FREQUENCY
|
701 InputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
702 SPEAKER_FRONT_RIGHT
|
703 SPEAKER_FRONT_CENTER
|
704 SPEAKER_LOW_FREQUENCY
|
709 InputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
710 SPEAKER_FRONT_RIGHT
|
711 SPEAKER_FRONT_CENTER
|
712 SPEAKER_LOW_FREQUENCY
|
713 SPEAKER_BACK_CENTER
|
718 InputType
.dwChannelMask
= SPEAKER_FRONT_LEFT
|
719 SPEAKER_FRONT_RIGHT
|
720 SPEAKER_FRONT_CENTER
|
721 SPEAKER_LOW_FREQUENCY
|
728 WARN("%s capture not supported\n", DevFmtChannelsString(mDevice
->FmtChans
));
729 return ALC_INVALID_ENUM
;
732 InputType
.Format
.wFormatTag
= WAVE_FORMAT_PCM
;
733 InputType
.Format
.nChannels
= static_cast<WORD
>(mDevice
->channelsFromFmt());
734 InputType
.Format
.wBitsPerSample
= static_cast<WORD
>(mDevice
->bytesFromFmt() * 8);
735 InputType
.Format
.nBlockAlign
= static_cast<WORD
>(InputType
.Format
.nChannels
*
736 InputType
.Format
.wBitsPerSample
/ 8);
737 InputType
.Format
.nSamplesPerSec
= mDevice
->Frequency
;
738 InputType
.Format
.nAvgBytesPerSec
= InputType
.Format
.nSamplesPerSec
*
739 InputType
.Format
.nBlockAlign
;
740 InputType
.Format
.cbSize
= 0;
741 InputType
.Samples
.wValidBitsPerSample
= InputType
.Format
.wBitsPerSample
;
742 if(mDevice
->FmtType
== DevFmtFloat
)
743 InputType
.SubFormat
= KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
;
745 InputType
.SubFormat
= KSDATAFORMAT_SUBTYPE_PCM
;
747 if(InputType
.Format
.nChannels
> 2 || mDevice
->FmtType
== DevFmtFloat
)
749 InputType
.Format
.wFormatTag
= WAVE_FORMAT_EXTENSIBLE
;
750 InputType
.Format
.cbSize
= sizeof(WAVEFORMATEXTENSIBLE
) - sizeof(WAVEFORMATEX
);
753 ALuint samples
{mDevice
->BufferSize
};
754 samples
= maxu(samples
, 100 * mDevice
->Frequency
/ 1000);
756 DSCBUFFERDESC DSCBDescription
{};
757 DSCBDescription
.dwSize
= sizeof(DSCBDescription
);
758 DSCBDescription
.dwFlags
= 0;
759 DSCBDescription
.dwBufferBytes
= samples
* InputType
.Format
.nBlockAlign
;
760 DSCBDescription
.lpwfxFormat
= &InputType
.Format
;
762 //DirectSoundCapture Init code
763 hr
= DirectSoundCaptureCreate(guid
, &mDSC
, nullptr);
765 mDSC
->CreateCaptureBuffer(&DSCBDescription
, &mDSCbuffer
, nullptr);
768 mRing
= CreateRingBuffer(mDevice
->BufferSize
, InputType
.Format
.nBlockAlign
, false);
769 if(!mRing
) hr
= DSERR_OUTOFMEMORY
;
774 ERR("Device init failed: 0x%08lx\n", hr
);
778 mDSCbuffer
->Release();
779 mDSCbuffer
= nullptr;
784 return ALC_INVALID_VALUE
;
787 mBufferBytes
= DSCBDescription
.dwBufferBytes
;
788 SetDefaultWFXChannelOrder(mDevice
);
790 mDevice
->DeviceName
= name
;
794 bool DSoundCapture::start()
796 HRESULT hr
{mDSCbuffer
->Start(DSCBSTART_LOOPING
)};
799 ERR("start failed: 0x%08lx\n", hr
);
800 aluHandleDisconnect(mDevice
, "Failure starting capture: 0x%lx", hr
);
806 void DSoundCapture::stop()
808 HRESULT hr
{mDSCbuffer
->Stop()};
811 ERR("stop failed: 0x%08lx\n", hr
);
812 aluHandleDisconnect(mDevice
, "Failure stopping capture: 0x%lx", hr
);
816 ALCenum
DSoundCapture::captureSamples(al::byte
*buffer
, ALCuint samples
)
818 mRing
->read(buffer
, samples
);
822 ALCuint
DSoundCapture::availableSamples()
824 if(!mDevice
->Connected
.load(std::memory_order_acquire
))
825 return static_cast<ALCuint
>(mRing
->readSpace());
827 ALuint FrameSize
{mDevice
->frameSizeFromFmt()};
828 DWORD BufferBytes
{mBufferBytes
};
829 DWORD LastCursor
{mCursor
};
832 void *ReadPtr1
{}, *ReadPtr2
{};
833 DWORD ReadCnt1
{}, ReadCnt2
{};
834 HRESULT hr
{mDSCbuffer
->GetCurrentPosition(nullptr, &ReadCursor
)};
837 DWORD NumBytes
{(ReadCursor
-LastCursor
+ BufferBytes
) % BufferBytes
};
838 if(!NumBytes
) return static_cast<ALCubyte
>(mRing
->readSpace());
839 hr
= mDSCbuffer
->Lock(LastCursor
, NumBytes
, &ReadPtr1
, &ReadCnt1
, &ReadPtr2
, &ReadCnt2
, 0);
843 mRing
->write(ReadPtr1
, ReadCnt1
/FrameSize
);
844 if(ReadPtr2
!= nullptr && ReadCnt2
> 0)
845 mRing
->write(ReadPtr2
, ReadCnt2
/FrameSize
);
846 hr
= mDSCbuffer
->Unlock(ReadPtr1
, ReadCnt1
, ReadPtr2
, ReadCnt2
);
847 mCursor
= (LastCursor
+ReadCnt1
+ReadCnt2
) % BufferBytes
;
852 ERR("update failed: 0x%08lx\n", hr
);
853 aluHandleDisconnect(mDevice
, "Failure retrieving capture data: 0x%lx", hr
);
856 return static_cast<ALCuint
>(mRing
->readSpace());
862 BackendFactory
&DSoundBackendFactory::getFactory()
864 static DSoundBackendFactory factory
{};
868 bool DSoundBackendFactory::init()
873 ds_handle
= LoadLib("dsound.dll");
876 ERR("Failed to load dsound.dll\n");
880 #define LOAD_FUNC(f) do { \
881 p##f = reinterpret_cast<decltype(p##f)>(GetSymbol(ds_handle, #f)); \
884 CloseLib(ds_handle); \
885 ds_handle = nullptr; \
889 LOAD_FUNC(DirectSoundCreate
);
890 LOAD_FUNC(DirectSoundEnumerateW
);
891 LOAD_FUNC(DirectSoundCaptureCreate
);
892 LOAD_FUNC(DirectSoundCaptureEnumerateW
);
899 bool DSoundBackendFactory::querySupport(BackendType type
)
900 { return (type
== BackendType::Playback
|| type
== BackendType::Capture
); }
902 void DSoundBackendFactory::probe(DevProbe type
, std::string
*outnames
)
904 auto add_device
= [outnames
](const DevMap
&entry
) -> void
906 /* +1 to also append the null char (to ensure a null-separated list and
907 * double-null terminated list).
909 outnames
->append(entry
.name
.c_str(), entry
.name
.length()+1);
912 /* Initialize COM to prevent name truncation */
914 HRESULT hrcom
{CoInitialize(nullptr)};
917 case DevProbe::Playback
:
918 PlaybackDevices
.clear();
919 hr
= DirectSoundEnumerateW(DSoundEnumDevices
, &PlaybackDevices
);
921 ERR("Error enumerating DirectSound playback devices (0x%lx)!\n", hr
);
922 std::for_each(PlaybackDevices
.cbegin(), PlaybackDevices
.cend(), add_device
);
925 case DevProbe::Capture
:
926 CaptureDevices
.clear();
927 hr
= DirectSoundCaptureEnumerateW(DSoundEnumDevices
, &CaptureDevices
);
929 ERR("Error enumerating DirectSound capture devices (0x%lx)!\n", hr
);
930 std::for_each(CaptureDevices
.cbegin(), CaptureDevices
.cend(), add_device
);
937 BackendPtr
DSoundBackendFactory::createBackend(ALCdevice
*device
, BackendType type
)
939 if(type
== BackendType::Playback
)
940 return BackendPtr
{new DSoundPlayback
{device
}};
941 if(type
== BackendType::Capture
)
942 return BackendPtr
{new DSoundCapture
{device
}};