audio_driver_wasapi.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. /*************************************************************************/
  2. /* audio_driver_wasapi.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
  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 <Functiondiscoverykeys_devpkey.h>
  33. #include "os/os.h"
  34. #include "project_settings.h"
  35. #include <functiondiscoverykeys.h>
  36. #ifndef PKEY_Device_FriendlyName
  37. #undef DEFINE_PROPERTYKEY
  38. /* clang-format off */
  39. #define DEFINE_PROPERTYKEY(id, a, b, c, d, e, f, g, h, i, j, k, l) \
  40. const PROPERTYKEY id = { { a, b, c, { d, e, f, g, h, i, j, k, } }, l };
  41. /* clang-format on */
  42. DEFINE_PROPERTYKEY(PKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 14);
  43. #endif
  44. const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
  45. const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
  46. const IID IID_IAudioClient = __uuidof(IAudioClient);
  47. const IID IID_IAudioRenderClient = __uuidof(IAudioRenderClient);
  48. const IID IID_IAudioCaptureClient = __uuidof(IAudioCaptureClient);
  49. #define SAFE_RELEASE(memory) \
  50. if ((memory) != NULL) { \
  51. (memory)->Release(); \
  52. (memory) = NULL; \
  53. }
  54. #define REFTIMES_PER_SEC 10000000
  55. #define REFTIMES_PER_MILLISEC 10000
  56. static StringName capture_device_id;
  57. static bool default_render_device_changed = false;
  58. static bool default_capture_device_changed = false;
  59. class CMMNotificationClient : public IMMNotificationClient {
  60. LONG _cRef;
  61. IMMDeviceEnumerator *_pEnumerator;
  62. public:
  63. CMMNotificationClient() :
  64. _cRef(1),
  65. _pEnumerator(NULL) {}
  66. ~CMMNotificationClient() {
  67. if ((_pEnumerator) != NULL) {
  68. (_pEnumerator)->Release();
  69. (_pEnumerator) = NULL;
  70. }
  71. }
  72. ULONG STDMETHODCALLTYPE AddRef() {
  73. return InterlockedIncrement(&_cRef);
  74. }
  75. ULONG STDMETHODCALLTYPE Release() {
  76. ULONG ulRef = InterlockedDecrement(&_cRef);
  77. if (0 == ulRef) {
  78. delete this;
  79. }
  80. return ulRef;
  81. }
  82. HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, VOID **ppvInterface) {
  83. if (IID_IUnknown == riid) {
  84. AddRef();
  85. *ppvInterface = (IUnknown *)this;
  86. } else if (__uuidof(IMMNotificationClient) == riid) {
  87. AddRef();
  88. *ppvInterface = (IMMNotificationClient *)this;
  89. } else {
  90. *ppvInterface = NULL;
  91. return E_NOINTERFACE;
  92. }
  93. return S_OK;
  94. }
  95. HRESULT STDMETHODCALLTYPE OnDeviceAdded(LPCWSTR pwstrDeviceId) {
  96. return S_OK;
  97. };
  98. HRESULT STDMETHODCALLTYPE OnDeviceRemoved(LPCWSTR pwstrDeviceId) {
  99. return S_OK;
  100. }
  101. HRESULT STDMETHODCALLTYPE OnDeviceStateChanged(LPCWSTR pwstrDeviceId, DWORD dwNewState) {
  102. return S_OK;
  103. }
  104. HRESULT STDMETHODCALLTYPE OnDefaultDeviceChanged(EDataFlow flow, ERole role, LPCWSTR pwstrDeviceId) {
  105. if (role == eConsole) {
  106. if (flow == eRender) {
  107. default_render_device_changed = true;
  108. } else if (flow == eCapture) {
  109. default_capture_device_changed = true;
  110. capture_device_id = String(pwstrDeviceId);
  111. }
  112. }
  113. return S_OK;
  114. }
  115. HRESULT STDMETHODCALLTYPE OnPropertyValueChanged(LPCWSTR pwstrDeviceId, const PROPERTYKEY key) {
  116. return S_OK;
  117. }
  118. };
  119. static CMMNotificationClient notif_client;
  120. Error AudioDriverWASAPI::init_render_device(bool reinit) {
  121. WAVEFORMATEX *pwfex;
  122. IMMDeviceEnumerator *enumerator = NULL;
  123. IMMDevice *device = NULL;
  124. CoInitialize(NULL);
  125. HRESULT hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void **)&enumerator);
  126. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  127. if (device_name == "Default") {
  128. hr = enumerator->GetDefaultAudioEndpoint(eRender, eConsole, &device);
  129. } else {
  130. IMMDeviceCollection *devices = NULL;
  131. hr = enumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &devices);
  132. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  133. LPWSTR strId = NULL;
  134. bool found = false;
  135. UINT count = 0;
  136. hr = devices->GetCount(&count);
  137. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  138. for (ULONG i = 0; i < count && !found; i++) {
  139. IMMDevice *device = NULL;
  140. hr = devices->Item(i, &device);
  141. ERR_BREAK(hr != S_OK);
  142. IPropertyStore *props = NULL;
  143. hr = device->OpenPropertyStore(STGM_READ, &props);
  144. ERR_BREAK(hr != S_OK);
  145. PROPVARIANT propvar;
  146. PropVariantInit(&propvar);
  147. hr = props->GetValue(PKEY_Device_FriendlyName, &propvar);
  148. ERR_BREAK(hr != S_OK);
  149. if (device_name == String(propvar.pwszVal)) {
  150. hr = device->GetId(&strId);
  151. ERR_BREAK(hr != S_OK);
  152. found = true;
  153. }
  154. PropVariantClear(&propvar);
  155. props->Release();
  156. device->Release();
  157. }
  158. if (found) {
  159. hr = enumerator->GetDevice(strId, &device);
  160. }
  161. if (strId) {
  162. CoTaskMemFree(strId);
  163. }
  164. if (device == NULL) {
  165. hr = enumerator->GetDefaultAudioEndpoint(eRender, eConsole, &device);
  166. }
  167. }
  168. if (reinit) {
  169. // In case we're trying to re-initialize the device prevent throwing this error on the console,
  170. // otherwise if there is currently no device available this will spam the console.
  171. if (hr != S_OK) {
  172. return ERR_CANT_OPEN;
  173. }
  174. } else {
  175. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  176. }
  177. hr = enumerator->RegisterEndpointNotificationCallback(&notif_client);
  178. SAFE_RELEASE(enumerator)
  179. if (hr != S_OK) {
  180. ERR_PRINT("WASAPI: RegisterEndpointNotificationCallback error");
  181. }
  182. hr = device->Activate(IID_IAudioClient, CLSCTX_ALL, NULL, (void **)&audio_client);
  183. SAFE_RELEASE(device)
  184. if (reinit) {
  185. if (hr != S_OK) {
  186. return ERR_CANT_OPEN;
  187. }
  188. } else {
  189. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  190. }
  191. hr = audio_client->GetMixFormat(&pwfex);
  192. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  193. // Since we're using WASAPI Shared Mode we can't control any of these, we just tag along
  194. wasapi_channels = pwfex->nChannels;
  195. format_tag = pwfex->wFormatTag;
  196. bits_per_sample = pwfex->wBitsPerSample;
  197. switch (wasapi_channels) {
  198. case 2: // Stereo
  199. case 4: // Surround 3.1
  200. case 6: // Surround 5.1
  201. case 8: // Surround 7.1
  202. channels = wasapi_channels;
  203. break;
  204. default:
  205. WARN_PRINTS("WASAPI: Unsupported number of channels: " + itos(wasapi_channels));
  206. channels = 2;
  207. break;
  208. }
  209. if (format_tag == WAVE_FORMAT_EXTENSIBLE) {
  210. WAVEFORMATEXTENSIBLE *wfex = (WAVEFORMATEXTENSIBLE *)pwfex;
  211. if (wfex->SubFormat == KSDATAFORMAT_SUBTYPE_PCM) {
  212. format_tag = WAVE_FORMAT_PCM;
  213. } else if (wfex->SubFormat == KSDATAFORMAT_SUBTYPE_IEEE_FLOAT) {
  214. format_tag = WAVE_FORMAT_IEEE_FLOAT;
  215. } else {
  216. ERR_PRINT("WASAPI: Format not supported");
  217. ERR_FAIL_V(ERR_CANT_OPEN);
  218. }
  219. } else {
  220. if (format_tag != WAVE_FORMAT_PCM && format_tag != WAVE_FORMAT_IEEE_FLOAT) {
  221. ERR_PRINT("WASAPI: Format not supported");
  222. ERR_FAIL_V(ERR_CANT_OPEN);
  223. }
  224. }
  225. DWORD streamflags = AUDCLNT_STREAMFLAGS_EVENTCALLBACK;
  226. if (mix_rate != pwfex->nSamplesPerSec) {
  227. streamflags |= AUDCLNT_STREAMFLAGS_RATEADJUST;
  228. pwfex->nSamplesPerSec = mix_rate;
  229. pwfex->nAvgBytesPerSec = pwfex->nSamplesPerSec * pwfex->nChannels * (pwfex->wBitsPerSample / 8);
  230. }
  231. hr = audio_client->Initialize(AUDCLNT_SHAREMODE_SHARED, streamflags, 0, 0, pwfex, NULL);
  232. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  233. event = CreateEvent(NULL, FALSE, FALSE, NULL);
  234. ERR_FAIL_COND_V(event == NULL, ERR_CANT_OPEN);
  235. hr = audio_client->SetEventHandle(event);
  236. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  237. hr = audio_client->GetService(IID_IAudioRenderClient, (void **)&render_client);
  238. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  239. UINT32 max_frames;
  240. hr = audio_client->GetBufferSize(&max_frames);
  241. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  242. // Due to WASAPI Shared Mode we have no control of the buffer size
  243. buffer_frames = max_frames;
  244. // Sample rate is independent of channels (ref: https://stackoverflow.com/questions/11048825/audio-sample-frequency-rely-on-channels)
  245. buffer_size = buffer_frames * channels;
  246. samples_in.resize(buffer_size);
  247. if (OS::get_singleton()->is_stdout_verbose()) {
  248. print_line("WASAPI: detected " + itos(channels) + " channels");
  249. print_line("WASAPI: audio buffer frames: " + itos(buffer_frames) + " calculated latency: " + itos(buffer_frames * 1000 / mix_rate) + "ms");
  250. }
  251. // Free memory
  252. CoTaskMemFree(pwfex);
  253. return OK;
  254. }
  255. StringName AudioDriverWASAPI::get_default_capture_device_name(IMMDeviceEnumerator *p_enumerator) {
  256. // Setup default device
  257. IMMDevice *default_device = NULL;
  258. LPWSTR pwszID = NULL;
  259. IPropertyStore *props = NULL;
  260. HRESULT hr = p_enumerator->GetDefaultAudioEndpoint(
  261. eCapture, eConsole, &default_device);
  262. ERR_FAIL_COND_V(hr != S_OK, "");
  263. // Get the device ID
  264. hr = default_device->GetId(&pwszID);
  265. ERR_FAIL_COND_V(hr != S_OK, "");
  266. // Get the device properties
  267. hr = default_device->OpenPropertyStore(
  268. STGM_READ, &props);
  269. ERR_FAIL_COND_V(hr != S_OK, "");
  270. PROPVARIANT var_name;
  271. PropVariantInit(&var_name);
  272. // Get the name of the device
  273. hr = props->GetValue(PKEY_Device_FriendlyName, &var_name);
  274. ERR_FAIL_COND_V(hr != S_OK, "");
  275. // Return the name of device
  276. return String(var_name.pwszVal);
  277. }
  278. Error AudioDriverWASAPI::init_capture_devices(bool reinit) {
  279. WAVEFORMATEX *pwfex;
  280. IMMDeviceEnumerator *enumerator = NULL;
  281. IMMDeviceCollection *device_collection = NULL;
  282. IPropertyStore *props = NULL;
  283. capture_device_id_map.clear();
  284. HRESULT hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void **)&enumerator);
  285. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  286. capture_device_default_name = get_default_capture_device_name(enumerator);
  287. // Enumerate a collection of valid devices
  288. hr = enumerator->EnumAudioEndpoints(eCapture, DEVICE_STATE_ACTIVE, &device_collection);
  289. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  290. SAFE_RELEASE(enumerator);
  291. UINT count;
  292. hr = device_collection->GetCount(&count);
  293. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  294. // Loop through the device count
  295. for (unsigned int i = 0; i < count; i++) {
  296. IMMDevice *device = NULL;
  297. LPWSTR pwszID = NULL;
  298. // Get the device
  299. hr = device_collection->Item(i, &device);
  300. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  301. // Get the device ID
  302. hr = device->GetId(&pwszID);
  303. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  304. // Get the device properties
  305. hr = device->OpenPropertyStore(STGM_READ, &props);
  306. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  307. PROPVARIANT var_name;
  308. PropVariantInit(&var_name);
  309. // Get the name of the device
  310. hr = props->GetValue(PKEY_Device_FriendlyName, &var_name);
  311. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  312. // Save the name of device
  313. StringName name = String(var_name.pwszVal);
  314. // DEBUG: print the device name and ID
  315. printf("Endpoint %d: \"%S\" (%S)\n", i, var_name.pwszVal, pwszID);
  316. capture_device_id_map[StringName(pwszID)] = name;
  317. // Cleanup the ID and properties
  318. CoTaskMemFree(pwszID);
  319. pwszID = NULL;
  320. PropVariantClear(&var_name);
  321. SAFE_RELEASE(props)
  322. // Create a new audio in block descriptor
  323. MicrophoneDeviceOutputDirectWASAPI *microphone_device_output_wasapi = memnew(MicrophoneDeviceOutputDirectWASAPI);
  324. microphone_device_output_wasapi->name = name;
  325. microphone_device_output_wasapi->active = false;
  326. // Push it into the list and assign it to the hash map for quick access
  327. microphone_device_outputs.push_back(microphone_device_output_wasapi);
  328. microphone_device_output_map[name] = microphone_device_output_wasapi;
  329. // Activate the device
  330. hr = device->Activate(IID_IAudioClient, CLSCTX_ALL, NULL, (void **)&microphone_device_output_wasapi->audio_client);
  331. SAFE_RELEASE(device)
  332. // Get the sample rate (hz)
  333. hr = microphone_device_output_wasapi->audio_client->GetMixFormat(&pwfex);
  334. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  335. microphone_device_output_wasapi->channels = pwfex->nChannels;
  336. microphone_device_output_wasapi->mix_rate = pwfex->nSamplesPerSec;
  337. microphone_device_output_wasapi->bits_per_sample = pwfex->wBitsPerSample;
  338. microphone_device_output_wasapi->frame_size = (microphone_device_output_wasapi->bits_per_sample / 8) * microphone_device_output_wasapi->channels;
  339. microphone_device_output_wasapi->current_capture_index = 0;
  340. microphone_device_output_wasapi->current_capture_size = 0;
  341. WORD format_tag = pwfex->wFormatTag;
  342. if (format_tag == WAVE_FORMAT_EXTENSIBLE) {
  343. WAVEFORMATEXTENSIBLE *wfex = (WAVEFORMATEXTENSIBLE *)pwfex;
  344. if (wfex->SubFormat == KSDATAFORMAT_SUBTYPE_PCM) {
  345. format_tag = WAVE_FORMAT_PCM;
  346. } else if (wfex->SubFormat == KSDATAFORMAT_SUBTYPE_IEEE_FLOAT) {
  347. format_tag = WAVE_FORMAT_IEEE_FLOAT;
  348. } else {
  349. ERR_PRINT("WASAPI: Format not supported");
  350. ERR_FAIL_V(ERR_CANT_OPEN);
  351. }
  352. } else {
  353. if (format_tag != WAVE_FORMAT_PCM && format_tag != WAVE_FORMAT_IEEE_FLOAT) {
  354. ERR_PRINT("WASAPI: Format not supported");
  355. ERR_FAIL_V(ERR_CANT_OPEN);
  356. }
  357. }
  358. microphone_device_output_wasapi->capture_format_tag = format_tag;
  359. hr = microphone_device_output_wasapi->audio_client->Initialize(AUDCLNT_SHAREMODE_SHARED, 0, REFTIMES_PER_SEC, 0, pwfex, NULL);
  360. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  361. // Get the max frames
  362. UINT32 max_frames;
  363. hr = microphone_device_output_wasapi->audio_client->GetBufferSize(&max_frames);
  364. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  365. // Set the buffer size
  366. microphone_device_output_wasapi->buffer.resize(max_frames);
  367. memset(microphone_device_output_wasapi->buffer.ptrw(), 0x00, microphone_device_output_wasapi->buffer.size() * microphone_device_output_wasapi->frame_size);
  368. // Get the capture client
  369. hr = microphone_device_output_wasapi->audio_client->GetService(IID_IAudioCaptureClient, (void **)&microphone_device_output_wasapi->capture_client);
  370. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  371. // TODO: set audio write stream to correct format
  372. REFERENCE_TIME hns_actual_duration = (double)REFTIMES_PER_SEC * max_frames / pwfex->nSamplesPerSec;
  373. // Free memory
  374. CoTaskMemFree(pwfex);
  375. SAFE_RELEASE(device)
  376. }
  377. SAFE_RELEASE(device_collection)
  378. return OK;
  379. }
  380. Error AudioDriverWASAPI::finish_render_device() {
  381. if (audio_client) {
  382. if (active) {
  383. audio_client->Stop();
  384. active = false;
  385. }
  386. audio_client->Release();
  387. audio_client = NULL;
  388. }
  389. SAFE_RELEASE(render_client)
  390. SAFE_RELEASE(audio_client)
  391. return OK;
  392. }
  393. Error AudioDriverWASAPI::finish_capture_devices() {
  394. microphone_device_output_map.clear();
  395. while (microphone_device_outputs.size() > 0) {
  396. MicrophoneDeviceOutputDirectWASAPI *microphone_device_output = static_cast<MicrophoneDeviceOutputDirectWASAPI *>(microphone_device_outputs.get(0));
  397. SAFE_RELEASE(microphone_device_output->capture_client)
  398. SAFE_RELEASE(microphone_device_output->audio_client)
  399. microphone_device_outputs.erase(microphone_device_output);
  400. memdelete(microphone_device_output);
  401. }
  402. return OK;
  403. }
  404. Error AudioDriverWASAPI::init() {
  405. mix_rate = GLOBAL_DEF_RST("audio/mix_rate", DEFAULT_MIX_RATE);
  406. Error err = init_render_device();
  407. if (err != OK) {
  408. ERR_PRINT("WASAPI: init_render_device error");
  409. }
  410. err = init_capture_devices();
  411. if (err != OK) {
  412. ERR_PRINT("WASAPI: init_capture_device error");
  413. }
  414. active = false;
  415. exit_thread = false;
  416. thread_exited = false;
  417. mutex = Mutex::create(true);
  418. thread = Thread::create(thread_func, this);
  419. return OK;
  420. }
  421. int AudioDriverWASAPI::get_mix_rate() const {
  422. return mix_rate;
  423. }
  424. AudioDriver::SpeakerMode AudioDriverWASAPI::get_speaker_mode() const {
  425. return get_speaker_mode_by_total_channels(channels);
  426. }
  427. Array AudioDriverWASAPI::get_device_list() {
  428. Array list;
  429. IMMDeviceCollection *devices = NULL;
  430. IMMDeviceEnumerator *enumerator = NULL;
  431. list.push_back(String("Default"));
  432. CoInitialize(NULL);
  433. HRESULT hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void **)&enumerator);
  434. ERR_FAIL_COND_V(hr != S_OK, Array());
  435. hr = enumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &devices);
  436. ERR_FAIL_COND_V(hr != S_OK, Array());
  437. UINT count = 0;
  438. hr = devices->GetCount(&count);
  439. ERR_FAIL_COND_V(hr != S_OK, Array());
  440. for (ULONG i = 0; i < count; i++) {
  441. IMMDevice *device = NULL;
  442. hr = devices->Item(i, &device);
  443. ERR_BREAK(hr != S_OK);
  444. IPropertyStore *props = NULL;
  445. hr = device->OpenPropertyStore(STGM_READ, &props);
  446. ERR_BREAK(hr != S_OK);
  447. PROPVARIANT propvar;
  448. PropVariantInit(&propvar);
  449. hr = props->GetValue(PKEY_Device_FriendlyName, &propvar);
  450. ERR_BREAK(hr != S_OK);
  451. list.push_back(String(propvar.pwszVal));
  452. PropVariantClear(&propvar);
  453. props->Release();
  454. device->Release();
  455. }
  456. devices->Release();
  457. enumerator->Release();
  458. return list;
  459. }
  460. String AudioDriverWASAPI::get_device() {
  461. return device_name;
  462. }
  463. void AudioDriverWASAPI::set_device(String device) {
  464. lock();
  465. new_device = device;
  466. unlock();
  467. }
  468. float AudioDriverWASAPI::read_sample(WORD format_tag, int bits_per_sample, BYTE *buffer, int i) {
  469. if (format_tag == WAVE_FORMAT_PCM) {
  470. int32_t sample = 0;
  471. switch (bits_per_sample) {
  472. case 8:
  473. sample = int32_t(((int8_t *)buffer)[i]) << 24;
  474. break;
  475. case 16:
  476. sample = int32_t(((int16_t *)buffer)[i]) << 16;
  477. break;
  478. case 24:
  479. sample |= int32_t(((int8_t *)buffer)[i * 3 + 2]) << 24;
  480. sample |= int32_t(((int8_t *)buffer)[i * 3 + 1]) << 16;
  481. sample |= int32_t(((int8_t *)buffer)[i * 3 + 0]) << 8;
  482. break;
  483. case 32:
  484. sample = ((int32_t *)buffer)[i];
  485. break;
  486. }
  487. return (sample >> 16) / 32768.f;
  488. } else if (format_tag == WAVE_FORMAT_IEEE_FLOAT) {
  489. return ((float *)buffer)[i];
  490. } else {
  491. ERR_PRINT("WASAPI: Unknown format tag");
  492. }
  493. return 0.f;
  494. }
  495. void AudioDriverWASAPI::write_sample(AudioDriverWASAPI *ad, BYTE *buffer, int i, int32_t sample) {
  496. if (ad->format_tag == WAVE_FORMAT_PCM) {
  497. switch (ad->bits_per_sample) {
  498. case 8:
  499. ((int8_t *)buffer)[i] = sample >> 24;
  500. break;
  501. case 16:
  502. ((int16_t *)buffer)[i] = sample >> 16;
  503. break;
  504. case 24:
  505. ((int8_t *)buffer)[i * 3 + 2] = sample >> 24;
  506. ((int8_t *)buffer)[i * 3 + 1] = sample >> 16;
  507. ((int8_t *)buffer)[i * 3 + 0] = sample >> 8;
  508. break;
  509. case 32:
  510. ((int32_t *)buffer)[i] = sample;
  511. break;
  512. }
  513. } else if (ad->format_tag == WAVE_FORMAT_IEEE_FLOAT) {
  514. ((float *)buffer)[i] = (sample >> 16) / 32768.f;
  515. } else {
  516. ERR_PRINT("WASAPI: Unknown format tag");
  517. ad->exit_thread = true;
  518. }
  519. }
  520. void AudioDriverWASAPI::thread_func(void *p_udata) {
  521. AudioDriverWASAPI *ad = (AudioDriverWASAPI *)p_udata;
  522. while (!ad->exit_thread) {
  523. // Capture
  524. if (default_capture_device_changed) {
  525. if (ad->capture_device_id_map.has(capture_device_id)) {
  526. Map<StringName, StringName>::Element *e = ad->capture_device_id_map.find(capture_device_id);
  527. ad->lock();
  528. ad->start_counting_ticks();
  529. ad->capture_device_default_name = e->get();
  530. ad->update_microphone_default(ad->capture_device_default_name);
  531. default_capture_device_changed = false;
  532. ad->stop_counting_ticks();
  533. ad->unlock();
  534. }
  535. }
  536. for (int i = 0; i < ad->microphone_device_outputs.size(); i++) {
  537. MicrophoneDeviceOutputDirectWASAPI *microphone_device_output_wasapi = static_cast<MicrophoneDeviceOutputDirectWASAPI *>(ad->microphone_device_outputs[i]);
  538. if (microphone_device_output_wasapi->active == false) {
  539. continue;
  540. }
  541. UINT32 packet_length = 0;
  542. BYTE *data;
  543. UINT32 num_frames_available;
  544. DWORD flags;
  545. HRESULT hr = microphone_device_output_wasapi->capture_client->GetNextPacketSize(&packet_length);
  546. ERR_BREAK(hr != S_OK);
  547. while (packet_length != 0) {
  548. hr = microphone_device_output_wasapi->capture_client->GetBuffer(&data, &num_frames_available, &flags, NULL, NULL);
  549. ERR_BREAK(hr != S_OK);
  550. unsigned int frames_to_copy = num_frames_available;
  551. if (flags & AUDCLNT_BUFFERFLAGS_SILENT) {
  552. memset((char *)(microphone_device_output_wasapi->buffer.ptrw()) + (microphone_device_output_wasapi->current_capture_index * microphone_device_output_wasapi->frame_size), 0, frames_to_copy * microphone_device_output_wasapi->frame_size);
  553. } else {
  554. // fixme: Only works for floating point atm
  555. for (int j = 0; j < frames_to_copy; j++) {
  556. float l, r;
  557. if (microphone_device_output_wasapi->channels == 2) {
  558. l = read_sample(microphone_device_output_wasapi->capture_format_tag, microphone_device_output_wasapi->bits_per_sample, data, j * 2);
  559. r = read_sample(microphone_device_output_wasapi->capture_format_tag, microphone_device_output_wasapi->bits_per_sample, data, j * 2 + 1);
  560. } else if (microphone_device_output_wasapi->channels == 1) {
  561. l = r = read_sample(microphone_device_output_wasapi->capture_format_tag, microphone_device_output_wasapi->bits_per_sample, data, j);
  562. } else {
  563. l = r = 0.f;
  564. ERR_PRINT("WASAPI: unsupported channel count in microphone!");
  565. }
  566. microphone_device_output_wasapi->buffer[microphone_device_output_wasapi->current_capture_index++] = AudioFrame(l, r);
  567. if (microphone_device_output_wasapi->current_capture_index >= microphone_device_output_wasapi->buffer.size()) {
  568. microphone_device_output_wasapi->current_capture_index = 0;
  569. }
  570. if (microphone_device_output_wasapi->current_capture_size < microphone_device_output_wasapi->buffer.size()) {
  571. microphone_device_output_wasapi->current_capture_size++;
  572. }
  573. }
  574. }
  575. hr = microphone_device_output_wasapi->capture_client->ReleaseBuffer(num_frames_available);
  576. ERR_BREAK(hr != S_OK);
  577. hr = microphone_device_output_wasapi->capture_client->GetNextPacketSize(&packet_length);
  578. ERR_BREAK(hr != S_OK);
  579. }
  580. }
  581. ad->lock();
  582. ad->start_counting_ticks();
  583. if (ad->active) {
  584. ad->audio_server_process(ad->buffer_frames, ad->samples_in.ptrw());
  585. } else {
  586. for (unsigned int i = 0; i < ad->buffer_size; i++) {
  587. ad->samples_in.write[i] = 0;
  588. }
  589. }
  590. ad->stop_counting_ticks();
  591. ad->unlock();
  592. unsigned int left_frames = ad->buffer_frames;
  593. unsigned int buffer_idx = 0;
  594. while (left_frames > 0 && ad->audio_client) {
  595. WaitForSingleObject(ad->event, 1000);
  596. ad->lock();
  597. ad->start_counting_ticks();
  598. UINT32 cur_frames;
  599. bool invalidated = false;
  600. HRESULT hr = ad->audio_client->GetCurrentPadding(&cur_frames);
  601. if (hr == S_OK) {
  602. // Check how much frames are available on the WASAPI buffer
  603. UINT32 avail_frames = ad->buffer_frames - cur_frames;
  604. UINT32 write_frames = avail_frames > left_frames ? left_frames : avail_frames;
  605. BYTE *buffer = NULL;
  606. hr = ad->render_client->GetBuffer(write_frames, &buffer);
  607. if (hr == S_OK) {
  608. // We're using WASAPI Shared Mode so we must convert the buffer
  609. if (ad->channels == ad->wasapi_channels) {
  610. for (unsigned int i = 0; i < write_frames * ad->channels; i++) {
  611. ad->write_sample(ad, buffer, i, ad->samples_in[buffer_idx++]);
  612. }
  613. } else {
  614. for (unsigned int i = 0; i < write_frames; i++) {
  615. for (unsigned int j = 0; j < MIN(ad->channels, ad->wasapi_channels); j++) {
  616. ad->write_sample(ad, buffer, i * ad->wasapi_channels + j, ad->samples_in[buffer_idx++]);
  617. }
  618. if (ad->wasapi_channels > ad->channels) {
  619. for (unsigned int j = ad->channels; j < ad->wasapi_channels; j++) {
  620. ad->write_sample(ad, buffer, i * ad->wasapi_channels + j, 0);
  621. }
  622. }
  623. }
  624. }
  625. hr = ad->render_client->ReleaseBuffer(write_frames, 0);
  626. if (hr != S_OK) {
  627. ERR_PRINT("WASAPI: Release buffer error");
  628. }
  629. left_frames -= write_frames;
  630. } else if (hr == AUDCLNT_E_DEVICE_INVALIDATED) {
  631. invalidated = true;
  632. } else {
  633. ERR_PRINT("WASAPI: Get buffer error");
  634. ad->exit_thread = true;
  635. }
  636. } else if (hr == AUDCLNT_E_DEVICE_INVALIDATED) {
  637. invalidated = true;
  638. } else {
  639. ERR_PRINT("WASAPI: GetCurrentPadding error");
  640. }
  641. if (invalidated) {
  642. // Device is not valid anymore
  643. WARN_PRINT("WASAPI: Current device invalidated, closing device");
  644. Error err = ad->finish_render_device();
  645. if (err != OK) {
  646. ERR_PRINT("WASAPI: finish_render_device error");
  647. }
  648. }
  649. ad->stop_counting_ticks();
  650. ad->unlock();
  651. }
  652. ad->lock();
  653. ad->start_counting_ticks();
  654. // If we're using the Default device and it changed finish it so we'll re-init the device
  655. if (ad->device_name == "Default" && default_render_device_changed) {
  656. Error err = ad->finish_render_device();
  657. if (err != OK) {
  658. ERR_PRINT("WASAPI: finish_render_device error");
  659. }
  660. default_render_device_changed = false;
  661. }
  662. // User selected a new device, finish the current one so we'll init the new device
  663. if (ad->device_name != ad->new_device) {
  664. ad->device_name = ad->new_device;
  665. Error err = ad->finish_render_device();
  666. if (err != OK) {
  667. ERR_PRINT("WASAPI: finish_render_device error");
  668. }
  669. }
  670. if (!ad->audio_client) {
  671. Error err = ad->init_render_device(true);
  672. if (err == OK) {
  673. ad->start();
  674. }
  675. }
  676. ad->stop_counting_ticks();
  677. ad->unlock();
  678. }
  679. ad->thread_exited = true;
  680. }
  681. void AudioDriverWASAPI::start() {
  682. if (audio_client) {
  683. HRESULT hr = audio_client->Start();
  684. if (hr != S_OK) {
  685. ERR_PRINT("WASAPI: Start failed");
  686. } else {
  687. active = true;
  688. }
  689. }
  690. }
  691. void AudioDriverWASAPI::lock() {
  692. if (mutex)
  693. mutex->lock();
  694. }
  695. void AudioDriverWASAPI::unlock() {
  696. if (mutex)
  697. mutex->unlock();
  698. }
  699. void AudioDriverWASAPI::finish() {
  700. if (thread) {
  701. exit_thread = true;
  702. Thread::wait_to_finish(thread);
  703. memdelete(thread);
  704. thread = NULL;
  705. }
  706. finish_capture_devices();
  707. finish_render_device();
  708. if (mutex) {
  709. memdelete(mutex);
  710. mutex = NULL;
  711. }
  712. }
  713. bool AudioDriverWASAPI::capture_device_start(StringName p_name) {
  714. if (microphone_device_output_map.has(p_name)) {
  715. MicrophoneDeviceOutputDirectWASAPI *microphone_device_output_wasapi = static_cast<MicrophoneDeviceOutputDirectWASAPI *>(microphone_device_output_map[p_name]);
  716. if (microphone_device_output_wasapi->active == false) {
  717. microphone_device_output_wasapi->audio_client->Start();
  718. microphone_device_output_wasapi->active = true;
  719. microphone_device_output_wasapi->set_read_index(-2048);
  720. }
  721. return true;
  722. }
  723. return false;
  724. }
  725. bool AudioDriverWASAPI::capture_device_stop(StringName p_name) {
  726. if (microphone_device_output_map.has(p_name)) {
  727. MicrophoneDeviceOutputDirectWASAPI *microphone_device_output_wasapi = static_cast<MicrophoneDeviceOutputDirectWASAPI *>(microphone_device_output_map[p_name]);
  728. if (microphone_device_output_wasapi->active == true) {
  729. microphone_device_output_wasapi->audio_client->Stop();
  730. microphone_device_output_wasapi->active = false;
  731. }
  732. return true;
  733. }
  734. return false;
  735. }
  736. PoolStringArray AudioDriverWASAPI::capture_device_get_names() {
  737. PoolStringArray names;
  738. for (int i = 0; i < microphone_device_outputs.size(); i++) {
  739. MicrophoneDeviceOutputDirectWASAPI *microphone_device_output_wasapi = static_cast<MicrophoneDeviceOutputDirectWASAPI *>(microphone_device_outputs.get(i));
  740. names.push_back(microphone_device_output_wasapi->name);
  741. }
  742. return names;
  743. }
  744. StringName AudioDriverWASAPI::capture_device_get_default_name() {
  745. lock();
  746. StringName capture_device_default_name_local = capture_device_default_name;
  747. unlock();
  748. return capture_device_default_name_local;
  749. }
  750. AudioDriverWASAPI::AudioDriverWASAPI() {
  751. audio_client = NULL;
  752. render_client = NULL;
  753. mutex = NULL;
  754. thread = NULL;
  755. format_tag = 0;
  756. bits_per_sample = 0;
  757. samples_in.clear();
  758. buffer_size = 0;
  759. channels = 0;
  760. wasapi_channels = 0;
  761. mix_rate = 0;
  762. buffer_frames = 0;
  763. thread_exited = false;
  764. exit_thread = false;
  765. active = false;
  766. device_name = "Default";
  767. new_device = "Default";
  768. capture_device_default_name = "";
  769. }
  770. #endif