36 const HRESULT coInit = CoInitializeEx(
nullptr, COINIT_MULTITHREADED);
37 comInitialized_ = (coInit == S_OK || coInit == S_FALSE);
39 ComPtr<IMMDeviceEnumerator> enumeratorPtr;
40 HRESULT hr = CoCreateInstance(
41 __uuidof(MMDeviceEnumerator),
44 __uuidof(IMMDeviceEnumerator),
45 reinterpret_cast<void**
>(enumeratorPtr.GetAddressOf())
52 ComPtr<IMMDevice> devicePtr;
53 hr = enumeratorPtr->GetDefaultAudioEndpoint(eRender, eConsole, devicePtr.GetAddressOf());
56 throw ARCException(
"Failed to get default audio endpoint", hr);
59 ComPtr<IAudioClient> clientPtr;
60 hr = devicePtr->Activate(
61 __uuidof(IAudioClient),
64 reinterpret_cast<void**
>(clientPtr.GetAddressOf())
68 throw ARCException(
"Failed to activate IAudioClient instance", hr);
71 hr = clientPtr->GetMixFormat(&pwfx);
72 if (FAILED(hr) || !pwfx)
74 throw ARCException(
"Failed to query the mix format", FAILED(hr) ? hr : E_POINTER);
77 auto initializeAt = [&](DWORD sampleRate) -> HRESULT
79 pwfx->nSamplesPerSec = sampleRate;
80 pwfx->nAvgBytesPerSec = sampleRate * pwfx->nBlockAlign;
81 return clientPtr->Initialize(
82 AUDCLNT_SHAREMODE_SHARED,
91 hr = clientPtr->Initialize(
92 AUDCLNT_SHAREMODE_SHARED,
101 hr = initializeAt(48000);
103 hr = initializeAt(44100);
107 throw ARCException(
"Failed to initialize IAudioClient instance", hr);
110 ComPtr<IAudioRenderClient> arcPtr;
111 hr = clientPtr->GetService(
112 __uuidof(IAudioRenderClient),
113 reinterpret_cast<void**
>(arcPtr.GetAddressOf())
117 throw ARCException(
"Failed to request IAudioRenderClient service", hr);
120 enumerator = enumeratorPtr.Detach();
121 device = devicePtr.Detach();
122 client = clientPtr.Detach();
123 arc = arcPtr.Detach();