audio_driver_wasapi.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. /**************************************************************************/
  2. /* audio_driver_wasapi.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifdef WASAPI_ENABLED
  31. #include "audio_driver_wasapi.h"
  32. #include "core/config/project_settings.h"
  33. #include "core/os/os.h"
  34. #include <functiondiscoverykeys.h>
  35. #include <wrl/client.h>
  36. using Microsoft::WRL::ComPtr;
  37. // Define IAudioClient3 if not already defined by MinGW headers
  38. #if defined __MINGW32__ || defined __MINGW64__
  39. #ifndef __IAudioClient3_FWD_DEFINED__
  40. #define __IAudioClient3_FWD_DEFINED__
  41. typedef interface IAudioClient3 IAudioClient3;
  42. #endif // __IAudioClient3_FWD_DEFINED__
  43. #ifndef __IAudioClient3_INTERFACE_DEFINED__
  44. #define __IAudioClient3_INTERFACE_DEFINED__
  45. // clang-format off
  46. MIDL_INTERFACE("7ED4EE07-8E67-4CD4-8C1A-2B7A5987AD42")
  47. IAudioClient3 : public IAudioClient2 {
  48. public:
  49. virtual HRESULT STDMETHODCALLTYPE GetSharedModeEnginePeriod(
  50. /* [annotation][in] */
  51. _In_ const WAVEFORMATEX *pFormat,
  52. /* [annotation][out] */
  53. _Out_ UINT32 *pDefaultPeriodInFrames,
  54. /* [annotation][out] */
  55. _Out_ UINT32 *pFundamentalPeriodInFrames,
  56. /* [annotation][out] */
  57. _Out_ UINT32 *pMinPeriodInFrames,
  58. /* [annotation][out] */
  59. _Out_ UINT32 *pMaxPeriodInFrames) = 0;
  60. virtual HRESULT STDMETHODCALLTYPE GetCurrentSharedModeEnginePeriod(
  61. /* [unique][annotation][out] */
  62. _Out_ WAVEFORMATEX * *ppFormat,
  63. /* [annotation][out] */
  64. _Out_ UINT32 * pCurrentPeriodInFrames) = 0;
  65. virtual HRESULT STDMETHODCALLTYPE InitializeSharedAudioStream(
  66. /* [annotation][in] */
  67. _In_ DWORD StreamFlags,
  68. /* [annotation][in] */
  69. _In_ UINT32 PeriodInFrames,
  70. /* [annotation][in] */
  71. _In_ const WAVEFORMATEX *pFormat,
  72. /* [annotation][in] */
  73. _In_opt_ LPCGUID AudioSessionGuid) = 0;
  74. };
  75. // clang-format on
  76. __CRT_UUID_DECL(IAudioClient3, 0x7ED4EE07, 0x8E67, 0x4CD4, 0x8C, 0x1A, 0x2B, 0x7A, 0x59, 0x87, 0xAD, 0x42)
  77. #endif // __IAudioClient3_INTERFACE_DEFINED__
  78. #endif // __MINGW32__ || __MINGW64__
  79. #ifndef PKEY_Device_FriendlyNameGodot
  80. #undef DEFINE_PROPERTYKEY
  81. /* clang-format off */
  82. #define DEFINE_PROPERTYKEY(id, a, b, c, d, e, f, g, h, i, j, k, l) \
  83. const PROPERTYKEY id = { { a, b, c, { d, e, f, g, h, i, j, k, } }, l };
  84. /* clang-format on */
  85. DEFINE_PROPERTYKEY(PKEY_Device_FriendlyNameGodot, 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 14);
  86. #endif
  87. const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
  88. const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
  89. const IID IID_IAudioClient = __uuidof(IAudioClient);
  90. const IID IID_IAudioClient3 = __uuidof(IAudioClient3);
  91. const IID IID_IAudioRenderClient = __uuidof(IAudioRenderClient);
  92. const IID IID_IAudioCaptureClient = __uuidof(IAudioCaptureClient);
  93. #define SAFE_RELEASE(memory) \
  94. if ((memory) != nullptr) { \
  95. (memory)->Release(); \
  96. (memory) = nullptr; \
  97. }
  98. #define REFTIMES_PER_SEC 10000000
  99. #define REFTIMES_PER_MILLISEC 10000
  100. #define CAPTURE_BUFFER_CHANNELS 2
  101. static bool default_output_device_changed = false;
  102. static bool default_input_device_changed = false;
  103. static int output_reinit_countdown = 0;
  104. static int input_reinit_countdown = 0;
  105. GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Wnon-virtual-dtor") // Silence warning due to a COM API weirdness (GH-35194).
  106. class CMMNotificationClient : public IMMNotificationClient {
  107. LONG _cRef = 1;
  108. public:
  109. ComPtr<IMMDeviceEnumerator> enumerator = nullptr;
  110. CMMNotificationClient() {}
  111. virtual ~CMMNotificationClient() {}
  112. ULONG STDMETHODCALLTYPE AddRef() {
  113. return InterlockedIncrement(&_cRef);
  114. }
  115. ULONG STDMETHODCALLTYPE Release() {
  116. ULONG ulRef = InterlockedDecrement(&_cRef);
  117. if (0 == ulRef) {
  118. delete this;
  119. }
  120. return ulRef;
  121. }
  122. HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, VOID **ppvInterface) {
  123. if (IID_IUnknown == riid) {
  124. AddRef();
  125. *ppvInterface = (IUnknown *)this;
  126. } else if (__uuidof(IMMNotificationClient) == riid) {
  127. AddRef();
  128. *ppvInterface = (IMMNotificationClient *)this;
  129. } else {
  130. *ppvInterface = nullptr;
  131. return E_NOINTERFACE;
  132. }
  133. return S_OK;
  134. }
  135. HRESULT STDMETHODCALLTYPE OnDeviceAdded(LPCWSTR pwstrDeviceId) {
  136. return S_OK;
  137. }
  138. HRESULT STDMETHODCALLTYPE OnDeviceRemoved(LPCWSTR pwstrDeviceId) {
  139. return S_OK;
  140. }
  141. HRESULT STDMETHODCALLTYPE OnDeviceStateChanged(LPCWSTR pwstrDeviceId, DWORD dwNewState) {
  142. return S_OK;
  143. }
  144. HRESULT STDMETHODCALLTYPE OnDefaultDeviceChanged(EDataFlow flow, ERole role, LPCWSTR pwstrDeviceId) {
  145. if (role == eConsole) {
  146. if (flow == eRender) {
  147. default_output_device_changed = true;
  148. } else if (flow == eCapture) {
  149. default_input_device_changed = true;
  150. }
  151. }
  152. return S_OK;
  153. }
  154. HRESULT STDMETHODCALLTYPE OnPropertyValueChanged(LPCWSTR pwstrDeviceId, const PROPERTYKEY key) {
  155. return S_OK;
  156. }
  157. };
  158. GODOT_GCC_WARNING_POP
  159. static CMMNotificationClient notif_client;
  160. Error AudioDriverWASAPI::audio_device_init(AudioDeviceWASAPI *p_device, bool p_input, bool p_reinit, bool p_no_audio_client_3) {
  161. // This function can be called recursively, so clean up before starting:
  162. audio_device_finish(p_device);
  163. WAVEFORMATEX *pwfex;
  164. ComPtr<IMMDeviceEnumerator> enumerator = nullptr;
  165. ComPtr<IMMDevice> output_device = nullptr;
  166. HRESULT hr = CoCreateInstance(CLSID_MMDeviceEnumerator, nullptr, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void **)&enumerator);
  167. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  168. if (p_device->device_name == "Default") {
  169. hr = enumerator->GetDefaultAudioEndpoint(p_input ? eCapture : eRender, eConsole, &output_device);
  170. } else {
  171. ComPtr<IMMDeviceCollection> devices = nullptr;
  172. hr = enumerator->EnumAudioEndpoints(p_input ? eCapture : eRender, DEVICE_STATE_ACTIVE, &devices);
  173. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  174. LPWSTR strId = nullptr;
  175. bool found = false;
  176. UINT count = 0;
  177. hr = devices->GetCount(&count);
  178. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  179. for (ULONG i = 0; i < count && !found; i++) {
  180. ComPtr<IMMDevice> tmp_device = nullptr;
  181. hr = devices->Item(i, &tmp_device);
  182. ERR_BREAK_MSG(hr != S_OK, "Cannot get devices item.");
  183. ComPtr<IPropertyStore> props = nullptr;
  184. hr = tmp_device->OpenPropertyStore(STGM_READ, &props);
  185. ERR_BREAK_MSG(hr != S_OK, "Cannot open property store.");
  186. PROPVARIANT propvar;
  187. PropVariantInit(&propvar);
  188. hr = props->GetValue(PKEY_Device_FriendlyNameGodot, &propvar);
  189. ERR_BREAK_MSG(hr != S_OK, "Cannot get value.");
  190. if (p_device->device_name == String(propvar.pwszVal)) {
  191. hr = tmp_device->GetId(&strId);
  192. if (unlikely(hr != S_OK)) {
  193. PropVariantClear(&propvar);
  194. ERR_PRINT("Cannot get device ID string.");
  195. break;
  196. }
  197. found = true;
  198. }
  199. PropVariantClear(&propvar);
  200. }
  201. if (found) {
  202. hr = enumerator->GetDevice(strId, &output_device);
  203. }
  204. if (strId) {
  205. CoTaskMemFree(strId);
  206. }
  207. if (output_device == nullptr) {
  208. hr = enumerator->GetDefaultAudioEndpoint(p_input ? eCapture : eRender, eConsole, &output_device);
  209. }
  210. }
  211. if (p_reinit) {
  212. // In case we're trying to re-initialize the device, prevent throwing this error on the console,
  213. // otherwise if there is currently no device available this will spam the console.
  214. if (hr != S_OK) {
  215. return ERR_CANT_OPEN;
  216. }
  217. } else {
  218. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  219. }
  220. if (notif_client.enumerator != nullptr) {
  221. notif_client.enumerator->UnregisterEndpointNotificationCallback(&notif_client);
  222. notif_client.enumerator = nullptr;
  223. }
  224. hr = enumerator->RegisterEndpointNotificationCallback(&notif_client);
  225. if (hr == S_OK) {
  226. notif_client.enumerator = enumerator;
  227. } else {
  228. ERR_PRINT("WASAPI: RegisterEndpointNotificationCallback error");
  229. }
  230. using_audio_client_3 = !p_input; // IID_IAudioClient3 is only used for adjustable output latency (not input)
  231. if (p_no_audio_client_3) {
  232. using_audio_client_3 = false;
  233. }
  234. if (using_audio_client_3) {
  235. hr = output_device->Activate(IID_IAudioClient3, CLSCTX_ALL, nullptr, (void **)&p_device->audio_client);
  236. if (hr != S_OK) {
  237. // IID_IAudioClient3 will never activate on OS versions before Windows 10.
  238. // Older Windows versions should fall back gracefully.
  239. using_audio_client_3 = false;
  240. print_verbose("WASAPI: Couldn't activate output_device with IAudioClient3 interface, falling back to IAudioClient interface");
  241. } else {
  242. print_verbose("WASAPI: Activated output_device using IAudioClient3 interface");
  243. }
  244. }
  245. if (!using_audio_client_3) {
  246. hr = output_device->Activate(IID_IAudioClient, CLSCTX_ALL, nullptr, (void **)&p_device->audio_client);
  247. }
  248. if (p_reinit) {
  249. if (hr != S_OK) {
  250. return ERR_CANT_OPEN;
  251. }
  252. } else {
  253. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  254. }
  255. if (using_audio_client_3) {
  256. AudioClientProperties audioProps{};
  257. audioProps.cbSize = sizeof(AudioClientProperties);
  258. audioProps.bIsOffload = FALSE;
  259. audioProps.eCategory = AudioCategory_GameEffects;
  260. hr = ((IAudioClient3 *)p_device->audio_client)->SetClientProperties(&audioProps);
  261. ERR_FAIL_COND_V_MSG(hr != S_OK, ERR_CANT_OPEN, "WASAPI: SetClientProperties failed with error 0x" + String::num_uint64(hr, 16) + ".");
  262. }
  263. hr = p_device->audio_client->GetMixFormat(&pwfex);
  264. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  265. // From this point onward, CoTaskMemFree(pwfex) must be called before returning or pwfex will leak!
  266. print_verbose("WASAPI: wFormatTag = " + itos(pwfex->wFormatTag));
  267. print_verbose("WASAPI: nChannels = " + itos(pwfex->nChannels));
  268. print_verbose("WASAPI: nSamplesPerSec = " + itos(pwfex->nSamplesPerSec));
  269. print_verbose("WASAPI: nAvgBytesPerSec = " + itos(pwfex->nAvgBytesPerSec));
  270. print_verbose("WASAPI: nBlockAlign = " + itos(pwfex->nBlockAlign));
  271. print_verbose("WASAPI: wBitsPerSample = " + itos(pwfex->wBitsPerSample));
  272. print_verbose("WASAPI: cbSize = " + itos(pwfex->cbSize));
  273. WAVEFORMATEX *closest = nullptr;
  274. hr = p_device->audio_client->IsFormatSupported(AUDCLNT_SHAREMODE_SHARED, pwfex, &closest);
  275. if (hr == S_FALSE) {
  276. WARN_PRINT("WASAPI: Mix format is not supported by the output_device");
  277. if (closest) {
  278. print_verbose("WASAPI: closest->wFormatTag = " + itos(closest->wFormatTag));
  279. print_verbose("WASAPI: closest->nChannels = " + itos(closest->nChannels));
  280. print_verbose("WASAPI: closest->nSamplesPerSec = " + itos(closest->nSamplesPerSec));
  281. print_verbose("WASAPI: closest->nAvgBytesPerSec = " + itos(closest->nAvgBytesPerSec));
  282. print_verbose("WASAPI: closest->nBlockAlign = " + itos(closest->nBlockAlign));
  283. print_verbose("WASAPI: closest->wBitsPerSample = " + itos(closest->wBitsPerSample));
  284. print_verbose("WASAPI: closest->cbSize = " + itos(closest->cbSize));
  285. WARN_PRINT("WASAPI: Using closest match instead");
  286. CoTaskMemFree(pwfex);
  287. pwfex = closest;
  288. }
  289. }
  290. // Since we're using WASAPI Shared Mode we can't control any of these, we just tag along
  291. p_device->channels = pwfex->nChannels;
  292. p_device->format_tag = pwfex->wFormatTag;
  293. p_device->bits_per_sample = pwfex->wBitsPerSample;
  294. p_device->frame_size = (p_device->bits_per_sample / 8) * p_device->channels;
  295. if (p_device->format_tag == WAVE_FORMAT_EXTENSIBLE) {
  296. WAVEFORMATEXTENSIBLE *wfex = (WAVEFORMATEXTENSIBLE *)pwfex;
  297. if (wfex->SubFormat == KSDATAFORMAT_SUBTYPE_PCM) {
  298. p_device->format_tag = WAVE_FORMAT_PCM;
  299. } else if (wfex->SubFormat == KSDATAFORMAT_SUBTYPE_IEEE_FLOAT) {
  300. p_device->format_tag = WAVE_FORMAT_IEEE_FLOAT;
  301. } else {
  302. ERR_PRINT("WASAPI: Format not supported");
  303. CoTaskMemFree(pwfex);
  304. ERR_FAIL_V(ERR_CANT_OPEN);
  305. }
  306. } else {
  307. if (p_device->format_tag != WAVE_FORMAT_PCM && p_device->format_tag != WAVE_FORMAT_IEEE_FLOAT) {
  308. ERR_PRINT("WASAPI: Format not supported");
  309. CoTaskMemFree(pwfex);
  310. ERR_FAIL_V(ERR_CANT_OPEN);
  311. }
  312. }
  313. if (!using_audio_client_3) {
  314. DWORD streamflags = 0;
  315. if ((DWORD)mix_rate != pwfex->nSamplesPerSec) {
  316. streamflags |= AUDCLNT_STREAMFLAGS_RATEADJUST;
  317. pwfex->nSamplesPerSec = mix_rate;
  318. pwfex->nAvgBytesPerSec = pwfex->nSamplesPerSec * pwfex->nChannels * (pwfex->wBitsPerSample / 8);
  319. }
  320. hr = p_device->audio_client->Initialize(AUDCLNT_SHAREMODE_SHARED, streamflags, p_input ? REFTIMES_PER_SEC : 0, 0, pwfex, nullptr);
  321. if (p_reinit) {
  322. // In case we're trying to re-initialize the device, prevent throwing this error on the console,
  323. // otherwise if there is currently no device available this will spam the console.
  324. if (hr != S_OK) {
  325. print_verbose("WASAPI: Initialize failed with error 0x" + String::num_uint64(hr, 16) + ".");
  326. CoTaskMemFree(pwfex);
  327. return ERR_CANT_OPEN;
  328. }
  329. } else {
  330. if (unlikely(hr != S_OK)) {
  331. CoTaskMemFree(pwfex);
  332. ERR_FAIL_V_MSG(ERR_CANT_OPEN, "WASAPI: Initialize failed with error 0x" + String::num_uint64(hr, 16) + ".");
  333. }
  334. }
  335. UINT32 max_frames;
  336. hr = p_device->audio_client->GetBufferSize(&max_frames);
  337. if (unlikely(hr != S_OK)) {
  338. CoTaskMemFree(pwfex);
  339. ERR_FAIL_V(ERR_CANT_OPEN);
  340. }
  341. // Due to WASAPI Shared Mode we have no control of the buffer size
  342. if (!p_input) {
  343. buffer_frames = max_frames;
  344. int64_t latency = 0;
  345. audio_output.audio_client->GetStreamLatency(&latency);
  346. // WASAPI REFERENCE_TIME units are 100 nanoseconds per unit
  347. // https://docs.microsoft.com/en-us/windows/win32/directshow/reference-time
  348. // Convert REFTIME to seconds as godot uses for latency
  349. real_latency = (float)latency / (float)REFTIMES_PER_SEC;
  350. }
  351. } else {
  352. IAudioClient3 *device_audio_client_3 = (IAudioClient3 *)p_device->audio_client;
  353. // AUDCLNT_STREAMFLAGS_RATEADJUST is an invalid flag with IAudioClient3, therefore we have to use
  354. // the closest supported mix rate supported by the audio driver.
  355. mix_rate = pwfex->nSamplesPerSec;
  356. print_verbose("WASAPI: mix_rate = " + itos(mix_rate));
  357. UINT32 default_period_frames, fundamental_period_frames, min_period_frames, max_period_frames;
  358. hr = device_audio_client_3->GetSharedModeEnginePeriod(
  359. pwfex,
  360. &default_period_frames,
  361. &fundamental_period_frames,
  362. &min_period_frames,
  363. &max_period_frames);
  364. if (hr != S_OK) {
  365. print_verbose("WASAPI: GetSharedModeEnginePeriod failed with error 0x" + String::num_uint64(hr, 16) + ", falling back to IAudioClient.");
  366. CoTaskMemFree(pwfex);
  367. return audio_device_init(p_device, p_input, p_reinit, true);
  368. }
  369. // Period frames must be an integral multiple of fundamental_period_frames or IAudioClient3 initialization will fail,
  370. // so we need to select the closest multiple to the user-specified latency.
  371. UINT32 desired_period_frames = target_latency_ms * mix_rate / 1000;
  372. UINT32 period_frames = (desired_period_frames / fundamental_period_frames) * fundamental_period_frames;
  373. if (Math::abs((int64_t)period_frames - (int64_t)desired_period_frames) > Math::abs((int64_t)(period_frames + fundamental_period_frames) - (int64_t)desired_period_frames)) {
  374. period_frames = period_frames + fundamental_period_frames;
  375. }
  376. period_frames = CLAMP(period_frames, min_period_frames, max_period_frames);
  377. print_verbose("WASAPI: fundamental_period_frames = " + itos(fundamental_period_frames));
  378. print_verbose("WASAPI: min_period_frames = " + itos(min_period_frames));
  379. print_verbose("WASAPI: max_period_frames = " + itos(max_period_frames));
  380. print_verbose("WASAPI: selected a period frame size of " + itos(period_frames));
  381. buffer_frames = period_frames;
  382. hr = device_audio_client_3->InitializeSharedAudioStream(0, period_frames, pwfex, nullptr);
  383. if (hr != S_OK) {
  384. print_verbose("WASAPI: InitializeSharedAudioStream failed with error 0x" + String::num_uint64(hr, 16) + ", falling back to IAudioClient.");
  385. CoTaskMemFree(pwfex);
  386. return audio_device_init(p_device, p_input, p_reinit, true);
  387. } else {
  388. uint32_t output_latency_in_frames;
  389. WAVEFORMATEX *current_pwfex;
  390. hr = device_audio_client_3->GetCurrentSharedModeEnginePeriod(&current_pwfex, &output_latency_in_frames);
  391. if (hr == OK) {
  392. real_latency = (float)output_latency_in_frames / (float)current_pwfex->nSamplesPerSec;
  393. CoTaskMemFree(current_pwfex);
  394. } else {
  395. print_verbose("WASAPI: GetCurrentSharedModeEnginePeriod failed with error 0x" + String::num_uint64(hr, 16) + ", falling back to IAudioClient.");
  396. CoTaskMemFree(pwfex);
  397. return audio_device_init(p_device, p_input, p_reinit, true);
  398. }
  399. }
  400. }
  401. if (p_input) {
  402. hr = p_device->audio_client->GetService(IID_IAudioCaptureClient, (void **)&p_device->capture_client);
  403. } else {
  404. hr = p_device->audio_client->GetService(IID_IAudioRenderClient, (void **)&p_device->render_client);
  405. }
  406. if (unlikely(hr != S_OK)) {
  407. CoTaskMemFree(pwfex);
  408. ERR_FAIL_V(ERR_CANT_OPEN);
  409. }
  410. // Free memory
  411. CoTaskMemFree(pwfex);
  412. return OK;
  413. }
  414. Error AudioDriverWASAPI::init_output_device(bool p_reinit) {
  415. Error err = audio_device_init(&audio_output, false, p_reinit);
  416. if (err != OK) {
  417. // We've tried to init the device, but have failed. Time to clean up.
  418. Error finish_err = finish_output_device();
  419. if (finish_err != OK) {
  420. ERR_PRINT("WASAPI: finish_output_device error after failed output audio_device_init");
  421. }
  422. return err;
  423. }
  424. switch (audio_output.channels) {
  425. case 1: // Mono
  426. case 3: // Surround 2.1
  427. case 5: // Surround 5.0
  428. case 7: // Surround 7.0
  429. // We will downmix as required.
  430. channels = audio_output.channels + 1;
  431. break;
  432. case 2: // Stereo
  433. case 4: // Surround 3.1
  434. case 6: // Surround 5.1
  435. case 8: // Surround 7.1
  436. channels = audio_output.channels;
  437. break;
  438. default:
  439. WARN_PRINT("WASAPI: Unsupported number of channels: " + itos(audio_output.channels));
  440. channels = 2;
  441. break;
  442. }
  443. // Sample rate is independent of channels (ref: https://stackoverflow.com/questions/11048825/audio-sample-frequency-rely-on-channels)
  444. samples_in.resize(buffer_frames * channels);
  445. input_position = 0;
  446. input_size = 0;
  447. print_verbose("WASAPI: detected " + itos(audio_output.channels) + " channels");
  448. print_verbose("WASAPI: audio buffer frames: " + itos(buffer_frames) + " calculated latency: " + itos(buffer_frames * 1000 / mix_rate) + "ms");
  449. return OK;
  450. }
  451. Error AudioDriverWASAPI::init_input_device(bool p_reinit) {
  452. if (audio_input.active.is_set()) {
  453. return ERR_ALREADY_IN_USE;
  454. }
  455. Error err = audio_device_init(&audio_input, true, p_reinit);
  456. if (err != OK) {
  457. // We've tried to init the device, but have failed. Time to clean up.
  458. Error finish_err = finish_input_device();
  459. if (finish_err != OK) {
  460. ERR_PRINT("WASAPI: finish_input_device error after failed input audio_device_init");
  461. }
  462. return err;
  463. }
  464. // Get the max frames
  465. UINT32 max_frames;
  466. HRESULT hr = audio_input.audio_client->GetBufferSize(&max_frames);
  467. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  468. input_buffer_init(max_frames);
  469. return OK;
  470. }
  471. Error AudioDriverWASAPI::audio_device_finish(AudioDeviceWASAPI *p_device) {
  472. if (p_device->active.is_set()) {
  473. if (p_device->audio_client) {
  474. p_device->audio_client->Stop();
  475. }
  476. p_device->active.clear();
  477. }
  478. SAFE_RELEASE(p_device->audio_client)
  479. SAFE_RELEASE(p_device->render_client)
  480. SAFE_RELEASE(p_device->capture_client)
  481. return OK;
  482. }
  483. Error AudioDriverWASAPI::finish_output_device() {
  484. return audio_device_finish(&audio_output);
  485. }
  486. Error AudioDriverWASAPI::finish_input_device() {
  487. return audio_device_finish(&audio_input);
  488. }
  489. Error AudioDriverWASAPI::init() {
  490. mix_rate = _get_configured_mix_rate();
  491. target_latency_ms = Engine::get_singleton()->get_audio_output_latency();
  492. exit_thread.clear();
  493. Error err = init_output_device();
  494. ERR_FAIL_COND_V_MSG(err != OK, err, "WASAPI: init_output_device error.");
  495. thread.start(thread_func, this);
  496. return OK;
  497. }
  498. int AudioDriverWASAPI::get_mix_rate() const {
  499. return mix_rate;
  500. }
  501. float AudioDriverWASAPI::get_latency() {
  502. return real_latency;
  503. }
  504. AudioDriver::SpeakerMode AudioDriverWASAPI::get_speaker_mode() const {
  505. return get_speaker_mode_by_total_channels(channels);
  506. }
  507. PackedStringArray AudioDriverWASAPI::audio_device_get_list(bool p_input) {
  508. PackedStringArray list;
  509. ComPtr<IMMDeviceCollection> devices = nullptr;
  510. ComPtr<IMMDeviceEnumerator> enumerator = nullptr;
  511. list.push_back(String("Default"));
  512. HRESULT hr = CoCreateInstance(CLSID_MMDeviceEnumerator, nullptr, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void **)&enumerator);
  513. ERR_FAIL_COND_V(hr != S_OK, PackedStringArray());
  514. hr = enumerator->EnumAudioEndpoints(p_input ? eCapture : eRender, DEVICE_STATE_ACTIVE, &devices);
  515. ERR_FAIL_COND_V(hr != S_OK, PackedStringArray());
  516. UINT count = 0;
  517. hr = devices->GetCount(&count);
  518. ERR_FAIL_COND_V(hr != S_OK, PackedStringArray());
  519. for (ULONG i = 0; i < count; i++) {
  520. ComPtr<IMMDevice> output_device = nullptr;
  521. hr = devices->Item(i, &output_device);
  522. ERR_BREAK(hr != S_OK);
  523. ComPtr<IPropertyStore> props = nullptr;
  524. hr = output_device->OpenPropertyStore(STGM_READ, &props);
  525. ERR_BREAK(hr != S_OK);
  526. PROPVARIANT propvar;
  527. PropVariantInit(&propvar);
  528. hr = props->GetValue(PKEY_Device_FriendlyNameGodot, &propvar);
  529. ERR_BREAK(hr != S_OK);
  530. list.push_back(String(propvar.pwszVal));
  531. PropVariantClear(&propvar);
  532. }
  533. return list;
  534. }
  535. PackedStringArray AudioDriverWASAPI::get_output_device_list() {
  536. return audio_device_get_list(false);
  537. }
  538. String AudioDriverWASAPI::get_output_device() {
  539. lock();
  540. String name = audio_output.device_name;
  541. unlock();
  542. return name;
  543. }
  544. void AudioDriverWASAPI::set_output_device(const String &p_name) {
  545. lock();
  546. audio_output.new_device = p_name;
  547. unlock();
  548. }
  549. int32_t AudioDriverWASAPI::read_sample(WORD format_tag, int bits_per_sample, BYTE *buffer, int i) {
  550. if (format_tag == WAVE_FORMAT_PCM) {
  551. int32_t sample = 0;
  552. switch (bits_per_sample) {
  553. case 8:
  554. sample = int32_t(((int8_t *)buffer)[i]) << 24;
  555. break;
  556. case 16:
  557. sample = int32_t(((int16_t *)buffer)[i]) << 16;
  558. break;
  559. case 24:
  560. sample |= int32_t(((int8_t *)buffer)[i * 3 + 2]) << 24;
  561. sample |= int32_t(((int8_t *)buffer)[i * 3 + 1]) << 16;
  562. sample |= int32_t(((int8_t *)buffer)[i * 3 + 0]) << 8;
  563. break;
  564. case 32:
  565. sample = ((int32_t *)buffer)[i];
  566. break;
  567. }
  568. return sample;
  569. } else if (format_tag == WAVE_FORMAT_IEEE_FLOAT) {
  570. return int32_t(((float *)buffer)[i] * 32768.0) << 16;
  571. } else {
  572. ERR_PRINT("WASAPI: Unknown format tag");
  573. }
  574. return 0;
  575. }
  576. void AudioDriverWASAPI::write_sample(WORD format_tag, int bits_per_sample, BYTE *buffer, int i, int32_t sample) {
  577. if (format_tag == WAVE_FORMAT_PCM) {
  578. switch (bits_per_sample) {
  579. case 8:
  580. ((int8_t *)buffer)[i] = sample >> 24;
  581. break;
  582. case 16:
  583. ((int16_t *)buffer)[i] = sample >> 16;
  584. break;
  585. case 24:
  586. ((int8_t *)buffer)[i * 3 + 2] = sample >> 24;
  587. ((int8_t *)buffer)[i * 3 + 1] = sample >> 16;
  588. ((int8_t *)buffer)[i * 3 + 0] = sample >> 8;
  589. break;
  590. case 32:
  591. ((int32_t *)buffer)[i] = sample;
  592. break;
  593. }
  594. } else if (format_tag == WAVE_FORMAT_IEEE_FLOAT) {
  595. ((float *)buffer)[i] = (sample >> 16) / 32768.f;
  596. } else {
  597. ERR_PRINT("WASAPI: Unknown format tag");
  598. }
  599. }
  600. void AudioDriverWASAPI::thread_func(void *p_udata) {
  601. CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
  602. AudioDriverWASAPI *ad = static_cast<AudioDriverWASAPI *>(p_udata);
  603. uint32_t avail_frames = 0;
  604. uint32_t write_ofs = 0;
  605. while (!ad->exit_thread.is_set()) {
  606. uint32_t read_frames = 0;
  607. uint32_t written_frames = 0;
  608. if (avail_frames == 0) {
  609. ad->lock();
  610. ad->start_counting_ticks();
  611. if (ad->audio_output.active.is_set()) {
  612. ad->audio_server_process(ad->buffer_frames, ad->samples_in.ptrw());
  613. } else {
  614. for (int i = 0; i < ad->samples_in.size(); i++) {
  615. ad->samples_in.write[i] = 0;
  616. }
  617. }
  618. avail_frames = ad->buffer_frames;
  619. write_ofs = 0;
  620. ad->stop_counting_ticks();
  621. ad->unlock();
  622. }
  623. ad->lock();
  624. ad->start_counting_ticks();
  625. if (avail_frames > 0 && ad->audio_output.audio_client) {
  626. UINT32 buffer_size;
  627. UINT32 cur_frames;
  628. bool invalidated = false;
  629. HRESULT hr = ad->audio_output.audio_client->GetBufferSize(&buffer_size);
  630. if (hr != S_OK) {
  631. ERR_PRINT("WASAPI: GetBufferSize error");
  632. }
  633. hr = ad->audio_output.audio_client->GetCurrentPadding(&cur_frames);
  634. if (hr == S_OK) {
  635. // Check how much frames are available on the WASAPI buffer
  636. UINT32 write_frames = MIN(buffer_size - cur_frames, avail_frames);
  637. if (write_frames > 0) {
  638. BYTE *buffer = nullptr;
  639. hr = ad->audio_output.render_client->GetBuffer(write_frames, &buffer);
  640. if (hr == S_OK) {
  641. // We're using WASAPI Shared Mode so we must convert the buffer
  642. if (ad->channels == ad->audio_output.channels) {
  643. for (unsigned int i = 0; i < write_frames * ad->channels; i++) {
  644. ad->write_sample(ad->audio_output.format_tag, ad->audio_output.bits_per_sample, buffer, i, ad->samples_in.write[write_ofs++]);
  645. }
  646. } else if (ad->channels == ad->audio_output.channels + 1) {
  647. // Pass all channels except the last two as-is, and then mix the last two
  648. // together as one channel. E.g. stereo -> mono, or 3.1 -> 2.1.
  649. unsigned int last_chan = ad->audio_output.channels - 1;
  650. for (unsigned int i = 0; i < write_frames; i++) {
  651. for (unsigned int j = 0; j < last_chan; j++) {
  652. ad->write_sample(ad->audio_output.format_tag, ad->audio_output.bits_per_sample, buffer, i * ad->audio_output.channels + j, ad->samples_in.write[write_ofs++]);
  653. }
  654. int32_t l = ad->samples_in.write[write_ofs++];
  655. int32_t r = ad->samples_in.write[write_ofs++];
  656. int32_t c = (int32_t)(((int64_t)l + (int64_t)r) / 2);
  657. ad->write_sample(ad->audio_output.format_tag, ad->audio_output.bits_per_sample, buffer, i * ad->audio_output.channels + last_chan, c);
  658. }
  659. } else {
  660. for (unsigned int i = 0; i < write_frames; i++) {
  661. for (unsigned int j = 0; j < MIN(ad->channels, ad->audio_output.channels); j++) {
  662. ad->write_sample(ad->audio_output.format_tag, ad->audio_output.bits_per_sample, buffer, i * ad->audio_output.channels + j, ad->samples_in.write[write_ofs++]);
  663. }
  664. if (ad->audio_output.channels > ad->channels) {
  665. for (unsigned int j = ad->channels; j < ad->audio_output.channels; j++) {
  666. ad->write_sample(ad->audio_output.format_tag, ad->audio_output.bits_per_sample, buffer, i * ad->audio_output.channels + j, 0);
  667. }
  668. }
  669. }
  670. }
  671. hr = ad->audio_output.render_client->ReleaseBuffer(write_frames, 0);
  672. if (hr != S_OK) {
  673. ERR_PRINT("WASAPI: Release buffer error");
  674. }
  675. avail_frames -= write_frames;
  676. written_frames += write_frames;
  677. } else if (hr == AUDCLNT_E_DEVICE_INVALIDATED) {
  678. // output_device is not valid anymore, reopen it
  679. Error err = ad->finish_output_device();
  680. if (err != OK) {
  681. ERR_PRINT("WASAPI: finish_output_device error");
  682. } else {
  683. // We reopened the output device and samples_in may have resized, so invalidate the current avail_frames
  684. avail_frames = 0;
  685. }
  686. } else {
  687. ERR_PRINT("WASAPI: Get buffer error");
  688. ad->exit_thread.set();
  689. }
  690. }
  691. } else if (hr == AUDCLNT_E_DEVICE_INVALIDATED) {
  692. invalidated = true;
  693. } else {
  694. ERR_PRINT("WASAPI: GetCurrentPadding error");
  695. }
  696. if (invalidated) {
  697. // output_device is not valid anymore
  698. WARN_PRINT("WASAPI: Current output_device invalidated, closing output_device");
  699. Error err = ad->finish_output_device();
  700. if (err != OK) {
  701. ERR_PRINT("WASAPI: finish_output_device error");
  702. }
  703. }
  704. }
  705. // If we're using the Default output device and it changed finish it so we'll re-init the output device
  706. if (ad->audio_output.device_name == "Default" && default_output_device_changed) {
  707. Error err = ad->finish_output_device();
  708. if (err != OK) {
  709. ERR_PRINT("WASAPI: finish_output_device error");
  710. }
  711. default_output_device_changed = false;
  712. }
  713. // User selected a new output device, finish the current one so we'll init the new output device
  714. if (ad->audio_output.device_name != ad->audio_output.new_device) {
  715. ad->audio_output.device_name = ad->audio_output.new_device;
  716. Error err = ad->finish_output_device();
  717. if (err != OK) {
  718. ERR_PRINT("WASAPI: finish_output_device error");
  719. }
  720. }
  721. if (!ad->audio_output.audio_client) {
  722. if (output_reinit_countdown < 1) {
  723. Error err = ad->init_output_device(true);
  724. if (err == OK) {
  725. ad->start();
  726. } else {
  727. output_reinit_countdown = 1000;
  728. }
  729. } else {
  730. output_reinit_countdown--;
  731. }
  732. avail_frames = 0;
  733. write_ofs = 0;
  734. }
  735. if (ad->audio_input.active.is_set()) {
  736. UINT32 packet_length = 0;
  737. BYTE *data;
  738. UINT32 num_frames_available;
  739. DWORD flags;
  740. HRESULT hr = ad->audio_input.capture_client->GetNextPacketSize(&packet_length);
  741. if (hr == S_OK) {
  742. while (packet_length != 0) {
  743. hr = ad->audio_input.capture_client->GetBuffer(&data, &num_frames_available, &flags, nullptr, nullptr);
  744. ERR_BREAK(hr != S_OK);
  745. // fixme: Only works for floating point atm
  746. for (UINT32 j = 0; j < num_frames_available; j++) {
  747. int32_t l, r;
  748. if (flags & AUDCLNT_BUFFERFLAGS_SILENT) {
  749. l = r = 0;
  750. } else {
  751. if (ad->audio_input.channels == 2) {
  752. l = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j * 2);
  753. r = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j * 2 + 1);
  754. } else if (ad->audio_input.channels == 1) {
  755. l = r = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j);
  756. } else {
  757. l = r = 0;
  758. ERR_PRINT("WASAPI: unsupported channel count in microphone!");
  759. }
  760. }
  761. ad->input_buffer_write(l);
  762. ad->input_buffer_write(r);
  763. }
  764. read_frames += num_frames_available;
  765. hr = ad->audio_input.capture_client->ReleaseBuffer(num_frames_available);
  766. ERR_BREAK(hr != S_OK);
  767. hr = ad->audio_input.capture_client->GetNextPacketSize(&packet_length);
  768. ERR_BREAK(hr != S_OK);
  769. }
  770. }
  771. // If we're using the Default output device and it changed finish it so we'll re-init the output device
  772. if (ad->audio_input.device_name == "Default" && default_input_device_changed) {
  773. Error err = ad->finish_input_device();
  774. if (err != OK) {
  775. ERR_PRINT("WASAPI: finish_input_device error");
  776. }
  777. default_input_device_changed = false;
  778. }
  779. // User selected a new input device, finish the current one so we'll init the new input device
  780. if (ad->audio_input.device_name != ad->audio_input.new_device) {
  781. ad->audio_input.device_name = ad->audio_input.new_device;
  782. Error err = ad->finish_input_device();
  783. if (err != OK) {
  784. ERR_PRINT("WASAPI: finish_input_device error");
  785. }
  786. }
  787. if (!ad->audio_input.audio_client) {
  788. if (input_reinit_countdown < 1) {
  789. Error err = ad->init_input_device(true);
  790. if (err == OK) {
  791. ad->input_start();
  792. } else {
  793. input_reinit_countdown = 1000;
  794. }
  795. } else {
  796. input_reinit_countdown--;
  797. }
  798. }
  799. }
  800. ad->stop_counting_ticks();
  801. ad->unlock();
  802. // Let the thread rest a while if we haven't read or write anything
  803. if (written_frames == 0 && read_frames == 0) {
  804. OS::get_singleton()->delay_usec(1000);
  805. }
  806. }
  807. CoUninitialize();
  808. }
  809. void AudioDriverWASAPI::start() {
  810. if (audio_output.audio_client) {
  811. HRESULT hr = audio_output.audio_client->Start();
  812. if (hr != S_OK) {
  813. ERR_PRINT("WASAPI: Start failed");
  814. } else {
  815. audio_output.active.set();
  816. }
  817. }
  818. }
  819. void AudioDriverWASAPI::lock() {
  820. mutex.lock();
  821. }
  822. void AudioDriverWASAPI::unlock() {
  823. mutex.unlock();
  824. }
  825. void AudioDriverWASAPI::finish() {
  826. exit_thread.set();
  827. if (thread.is_started()) {
  828. thread.wait_to_finish();
  829. }
  830. finish_input_device();
  831. finish_output_device();
  832. }
  833. Error AudioDriverWASAPI::input_start() {
  834. Error err = init_input_device();
  835. if (err != OK) {
  836. ERR_PRINT("WASAPI: init_input_device error");
  837. return err;
  838. }
  839. if (audio_input.active.is_set()) {
  840. return FAILED;
  841. }
  842. audio_input.audio_client->Start();
  843. audio_input.active.set();
  844. return OK;
  845. }
  846. Error AudioDriverWASAPI::input_stop() {
  847. if (audio_input.active.is_set()) {
  848. audio_input.audio_client->Stop();
  849. audio_input.active.clear();
  850. }
  851. return OK;
  852. }
  853. PackedStringArray AudioDriverWASAPI::get_input_device_list() {
  854. return audio_device_get_list(true);
  855. }
  856. String AudioDriverWASAPI::get_input_device() {
  857. lock();
  858. String name = audio_input.device_name;
  859. unlock();
  860. return name;
  861. }
  862. void AudioDriverWASAPI::set_input_device(const String &p_name) {
  863. lock();
  864. audio_input.new_device = p_name;
  865. unlock();
  866. }
  867. AudioDriverWASAPI::AudioDriverWASAPI() {
  868. samples_in.clear();
  869. }
  870. #endif // WASAPI_ENABLED