audio_driver_wasapi.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "core/os/os.h"
  33. #include "core/project_settings.h"
  34. #include <functiondiscoverykeys.h>
  35. #ifndef PKEY_Device_FriendlyName
  36. #undef DEFINE_PROPERTYKEY
  37. /* clang-format off */
  38. #define DEFINE_PROPERTYKEY(id, a, b, c, d, e, f, g, h, i, j, k, l) \
  39. const PROPERTYKEY id = { { a, b, c, { d, e, f, g, h, i, j, k, } }, l };
  40. /* clang-format on */
  41. DEFINE_PROPERTYKEY(PKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 14);
  42. #endif
  43. const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
  44. const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
  45. const IID IID_IAudioClient = __uuidof(IAudioClient);
  46. const IID IID_IAudioRenderClient = __uuidof(IAudioRenderClient);
  47. const IID IID_IAudioCaptureClient = __uuidof(IAudioCaptureClient);
  48. #define SAFE_RELEASE(memory) \
  49. if ((memory) != nullptr) { \
  50. (memory)->Release(); \
  51. (memory) = nullptr; \
  52. }
  53. #define REFTIMES_PER_SEC 10000000
  54. #define REFTIMES_PER_MILLISEC 10000
  55. #define CAPTURE_BUFFER_CHANNELS 2
  56. static bool default_render_device_changed = false;
  57. static bool default_capture_device_changed = false;
  58. class CMMNotificationClient : public IMMNotificationClient {
  59. LONG _cRef = 1;
  60. IMMDeviceEnumerator *_pEnumerator = nullptr;
  61. public:
  62. CMMNotificationClient() {}
  63. virtual ~CMMNotificationClient() {
  64. if ((_pEnumerator) != nullptr) {
  65. (_pEnumerator)->Release();
  66. (_pEnumerator) = nullptr;
  67. }
  68. }
  69. ULONG STDMETHODCALLTYPE AddRef() {
  70. return InterlockedIncrement(&_cRef);
  71. }
  72. ULONG STDMETHODCALLTYPE Release() {
  73. ULONG ulRef = InterlockedDecrement(&_cRef);
  74. if (0 == ulRef) {
  75. delete this;
  76. }
  77. return ulRef;
  78. }
  79. HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, VOID **ppvInterface) {
  80. if (IID_IUnknown == riid) {
  81. AddRef();
  82. *ppvInterface = (IUnknown *)this;
  83. } else if (__uuidof(IMMNotificationClient) == riid) {
  84. AddRef();
  85. *ppvInterface = (IMMNotificationClient *)this;
  86. } else {
  87. *ppvInterface = nullptr;
  88. return E_NOINTERFACE;
  89. }
  90. return S_OK;
  91. }
  92. HRESULT STDMETHODCALLTYPE OnDeviceAdded(LPCWSTR pwstrDeviceId) {
  93. return S_OK;
  94. };
  95. HRESULT STDMETHODCALLTYPE OnDeviceRemoved(LPCWSTR pwstrDeviceId) {
  96. return S_OK;
  97. }
  98. HRESULT STDMETHODCALLTYPE OnDeviceStateChanged(LPCWSTR pwstrDeviceId, DWORD dwNewState) {
  99. return S_OK;
  100. }
  101. HRESULT STDMETHODCALLTYPE OnDefaultDeviceChanged(EDataFlow flow, ERole role, LPCWSTR pwstrDeviceId) {
  102. if (role == eConsole) {
  103. if (flow == eRender) {
  104. default_render_device_changed = true;
  105. } else if (flow == eCapture) {
  106. default_capture_device_changed = true;
  107. }
  108. }
  109. return S_OK;
  110. }
  111. HRESULT STDMETHODCALLTYPE OnPropertyValueChanged(LPCWSTR pwstrDeviceId, const PROPERTYKEY key) {
  112. return S_OK;
  113. }
  114. };
  115. static CMMNotificationClient notif_client;
  116. Error AudioDriverWASAPI::audio_device_init(AudioDeviceWASAPI *p_device, bool p_capture, bool reinit) {
  117. WAVEFORMATEX *pwfex;
  118. IMMDeviceEnumerator *enumerator = nullptr;
  119. IMMDevice *device = nullptr;
  120. CoInitialize(nullptr);
  121. HRESULT hr = CoCreateInstance(CLSID_MMDeviceEnumerator, nullptr, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void **)&enumerator);
  122. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  123. if (p_device->device_name == "Default") {
  124. hr = enumerator->GetDefaultAudioEndpoint(p_capture ? eCapture : eRender, eConsole, &device);
  125. } else {
  126. IMMDeviceCollection *devices = nullptr;
  127. hr = enumerator->EnumAudioEndpoints(p_capture ? eCapture : eRender, DEVICE_STATE_ACTIVE, &devices);
  128. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  129. LPWSTR strId = nullptr;
  130. bool found = false;
  131. UINT count = 0;
  132. hr = devices->GetCount(&count);
  133. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  134. for (ULONG i = 0; i < count && !found; i++) {
  135. IMMDevice *tmp_device = nullptr;
  136. hr = devices->Item(i, &tmp_device);
  137. ERR_BREAK(hr != S_OK);
  138. IPropertyStore *props = nullptr;
  139. hr = tmp_device->OpenPropertyStore(STGM_READ, &props);
  140. ERR_BREAK(hr != S_OK);
  141. PROPVARIANT propvar;
  142. PropVariantInit(&propvar);
  143. hr = props->GetValue(PKEY_Device_FriendlyName, &propvar);
  144. ERR_BREAK(hr != S_OK);
  145. if (p_device->device_name == String(propvar.pwszVal)) {
  146. hr = tmp_device->GetId(&strId);
  147. ERR_BREAK(hr != S_OK);
  148. found = true;
  149. }
  150. PropVariantClear(&propvar);
  151. props->Release();
  152. tmp_device->Release();
  153. }
  154. if (found) {
  155. hr = enumerator->GetDevice(strId, &device);
  156. }
  157. if (strId) {
  158. CoTaskMemFree(strId);
  159. }
  160. if (device == nullptr) {
  161. hr = enumerator->GetDefaultAudioEndpoint(p_capture ? eCapture : eRender, eConsole, &device);
  162. }
  163. }
  164. if (reinit) {
  165. // In case we're trying to re-initialize the device prevent throwing this error on the console,
  166. // otherwise if there is currently no device available this will spam the console.
  167. if (hr != S_OK) {
  168. return ERR_CANT_OPEN;
  169. }
  170. } else {
  171. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  172. }
  173. hr = enumerator->RegisterEndpointNotificationCallback(&notif_client);
  174. SAFE_RELEASE(enumerator)
  175. if (hr != S_OK) {
  176. ERR_PRINT("WASAPI: RegisterEndpointNotificationCallback error");
  177. }
  178. hr = device->Activate(IID_IAudioClient, CLSCTX_ALL, nullptr, (void **)&p_device->audio_client);
  179. SAFE_RELEASE(device)
  180. if (reinit) {
  181. if (hr != S_OK) {
  182. return ERR_CANT_OPEN;
  183. }
  184. } else {
  185. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  186. }
  187. hr = p_device->audio_client->GetMixFormat(&pwfex);
  188. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  189. print_verbose("WASAPI: wFormatTag = " + itos(pwfex->wFormatTag));
  190. print_verbose("WASAPI: nChannels = " + itos(pwfex->nChannels));
  191. print_verbose("WASAPI: nSamplesPerSec = " + itos(pwfex->nSamplesPerSec));
  192. print_verbose("WASAPI: nAvgBytesPerSec = " + itos(pwfex->nAvgBytesPerSec));
  193. print_verbose("WASAPI: nBlockAlign = " + itos(pwfex->nBlockAlign));
  194. print_verbose("WASAPI: wBitsPerSample = " + itos(pwfex->wBitsPerSample));
  195. print_verbose("WASAPI: cbSize = " + itos(pwfex->cbSize));
  196. WAVEFORMATEX *closest = nullptr;
  197. hr = p_device->audio_client->IsFormatSupported(AUDCLNT_SHAREMODE_SHARED, pwfex, &closest);
  198. if (hr == S_FALSE) {
  199. WARN_PRINT("WASAPI: Mix format is not supported by the Device");
  200. if (closest) {
  201. print_verbose("WASAPI: closest->wFormatTag = " + itos(closest->wFormatTag));
  202. print_verbose("WASAPI: closest->nChannels = " + itos(closest->nChannels));
  203. print_verbose("WASAPI: closest->nSamplesPerSec = " + itos(closest->nSamplesPerSec));
  204. print_verbose("WASAPI: closest->nAvgBytesPerSec = " + itos(closest->nAvgBytesPerSec));
  205. print_verbose("WASAPI: closest->nBlockAlign = " + itos(closest->nBlockAlign));
  206. print_verbose("WASAPI: closest->wBitsPerSample = " + itos(closest->wBitsPerSample));
  207. print_verbose("WASAPI: closest->cbSize = " + itos(closest->cbSize));
  208. WARN_PRINT("WASAPI: Using closest match instead");
  209. pwfex = closest;
  210. }
  211. }
  212. // Since we're using WASAPI Shared Mode we can't control any of these, we just tag along
  213. p_device->channels = pwfex->nChannels;
  214. p_device->format_tag = pwfex->wFormatTag;
  215. p_device->bits_per_sample = pwfex->wBitsPerSample;
  216. p_device->frame_size = (p_device->bits_per_sample / 8) * p_device->channels;
  217. if (p_device->format_tag == WAVE_FORMAT_EXTENSIBLE) {
  218. WAVEFORMATEXTENSIBLE *wfex = (WAVEFORMATEXTENSIBLE *)pwfex;
  219. if (wfex->SubFormat == KSDATAFORMAT_SUBTYPE_PCM) {
  220. p_device->format_tag = WAVE_FORMAT_PCM;
  221. } else if (wfex->SubFormat == KSDATAFORMAT_SUBTYPE_IEEE_FLOAT) {
  222. p_device->format_tag = WAVE_FORMAT_IEEE_FLOAT;
  223. } else {
  224. ERR_PRINT("WASAPI: Format not supported");
  225. ERR_FAIL_V(ERR_CANT_OPEN);
  226. }
  227. } else {
  228. if (p_device->format_tag != WAVE_FORMAT_PCM && p_device->format_tag != WAVE_FORMAT_IEEE_FLOAT) {
  229. ERR_PRINT("WASAPI: Format not supported");
  230. ERR_FAIL_V(ERR_CANT_OPEN);
  231. }
  232. }
  233. DWORD streamflags = 0;
  234. if ((DWORD)mix_rate != pwfex->nSamplesPerSec) {
  235. streamflags |= AUDCLNT_STREAMFLAGS_RATEADJUST;
  236. pwfex->nSamplesPerSec = mix_rate;
  237. pwfex->nAvgBytesPerSec = pwfex->nSamplesPerSec * pwfex->nChannels * (pwfex->wBitsPerSample / 8);
  238. }
  239. hr = p_device->audio_client->Initialize(AUDCLNT_SHAREMODE_SHARED, streamflags, p_capture ? REFTIMES_PER_SEC : 0, 0, pwfex, nullptr);
  240. ERR_FAIL_COND_V_MSG(hr != S_OK, ERR_CANT_OPEN, "WASAPI: Initialize failed with error 0x" + String::num_uint64(hr, 16) + ".");
  241. if (p_capture) {
  242. hr = p_device->audio_client->GetService(IID_IAudioCaptureClient, (void **)&p_device->capture_client);
  243. } else {
  244. hr = p_device->audio_client->GetService(IID_IAudioRenderClient, (void **)&p_device->render_client);
  245. }
  246. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  247. // Free memory
  248. CoTaskMemFree(pwfex);
  249. SAFE_RELEASE(device)
  250. return OK;
  251. }
  252. Error AudioDriverWASAPI::init_render_device(bool reinit) {
  253. Error err = audio_device_init(&audio_output, false, reinit);
  254. if (err != OK)
  255. return err;
  256. switch (audio_output.channels) {
  257. case 2: // Stereo
  258. case 4: // Surround 3.1
  259. case 6: // Surround 5.1
  260. case 8: // Surround 7.1
  261. channels = audio_output.channels;
  262. break;
  263. default:
  264. WARN_PRINT("WASAPI: Unsupported number of channels: " + itos(audio_output.channels));
  265. channels = 2;
  266. break;
  267. }
  268. UINT32 max_frames;
  269. HRESULT hr = audio_output.audio_client->GetBufferSize(&max_frames);
  270. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  271. // Due to WASAPI Shared Mode we have no control of the buffer size
  272. buffer_frames = max_frames;
  273. // Sample rate is independent of channels (ref: https://stackoverflow.com/questions/11048825/audio-sample-frequency-rely-on-channels)
  274. samples_in.resize(buffer_frames * channels);
  275. input_position = 0;
  276. input_size = 0;
  277. print_verbose("WASAPI: detected " + itos(channels) + " channels");
  278. print_verbose("WASAPI: audio buffer frames: " + itos(buffer_frames) + " calculated latency: " + itos(buffer_frames * 1000 / mix_rate) + "ms");
  279. return OK;
  280. }
  281. Error AudioDriverWASAPI::init_capture_device(bool reinit) {
  282. Error err = audio_device_init(&audio_input, true, reinit);
  283. if (err != OK)
  284. return err;
  285. // Get the max frames
  286. UINT32 max_frames;
  287. HRESULT hr = audio_input.audio_client->GetBufferSize(&max_frames);
  288. ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
  289. input_buffer_init(max_frames);
  290. return OK;
  291. }
  292. Error AudioDriverWASAPI::audio_device_finish(AudioDeviceWASAPI *p_device) {
  293. if (p_device->active) {
  294. if (p_device->audio_client) {
  295. p_device->audio_client->Stop();
  296. }
  297. p_device->active = false;
  298. }
  299. SAFE_RELEASE(p_device->audio_client)
  300. SAFE_RELEASE(p_device->render_client)
  301. SAFE_RELEASE(p_device->capture_client)
  302. return OK;
  303. }
  304. Error AudioDriverWASAPI::finish_render_device() {
  305. return audio_device_finish(&audio_output);
  306. }
  307. Error AudioDriverWASAPI::finish_capture_device() {
  308. return audio_device_finish(&audio_input);
  309. }
  310. Error AudioDriverWASAPI::init() {
  311. mix_rate = GLOBAL_GET("audio/mix_rate");
  312. Error err = init_render_device();
  313. if (err != OK) {
  314. ERR_PRINT("WASAPI: init_render_device error");
  315. }
  316. exit_thread = false;
  317. thread_exited = false;
  318. thread = Thread::create(thread_func, this);
  319. return OK;
  320. }
  321. int AudioDriverWASAPI::get_mix_rate() const {
  322. return mix_rate;
  323. }
  324. AudioDriver::SpeakerMode AudioDriverWASAPI::get_speaker_mode() const {
  325. return get_speaker_mode_by_total_channels(channels);
  326. }
  327. Array AudioDriverWASAPI::audio_device_get_list(bool p_capture) {
  328. Array list;
  329. IMMDeviceCollection *devices = nullptr;
  330. IMMDeviceEnumerator *enumerator = nullptr;
  331. list.push_back(String("Default"));
  332. CoInitialize(nullptr);
  333. HRESULT hr = CoCreateInstance(CLSID_MMDeviceEnumerator, nullptr, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void **)&enumerator);
  334. ERR_FAIL_COND_V(hr != S_OK, Array());
  335. hr = enumerator->EnumAudioEndpoints(p_capture ? eCapture : eRender, DEVICE_STATE_ACTIVE, &devices);
  336. ERR_FAIL_COND_V(hr != S_OK, Array());
  337. UINT count = 0;
  338. hr = devices->GetCount(&count);
  339. ERR_FAIL_COND_V(hr != S_OK, Array());
  340. for (ULONG i = 0; i < count; i++) {
  341. IMMDevice *device = nullptr;
  342. hr = devices->Item(i, &device);
  343. ERR_BREAK(hr != S_OK);
  344. IPropertyStore *props = nullptr;
  345. hr = device->OpenPropertyStore(STGM_READ, &props);
  346. ERR_BREAK(hr != S_OK);
  347. PROPVARIANT propvar;
  348. PropVariantInit(&propvar);
  349. hr = props->GetValue(PKEY_Device_FriendlyName, &propvar);
  350. ERR_BREAK(hr != S_OK);
  351. list.push_back(String(propvar.pwszVal));
  352. PropVariantClear(&propvar);
  353. props->Release();
  354. device->Release();
  355. }
  356. devices->Release();
  357. enumerator->Release();
  358. return list;
  359. }
  360. Array AudioDriverWASAPI::get_device_list() {
  361. return audio_device_get_list(false);
  362. }
  363. String AudioDriverWASAPI::get_device() {
  364. lock();
  365. String name = audio_output.device_name;
  366. unlock();
  367. return name;
  368. }
  369. void AudioDriverWASAPI::set_device(String device) {
  370. lock();
  371. audio_output.new_device = device;
  372. unlock();
  373. }
  374. int32_t AudioDriverWASAPI::read_sample(WORD format_tag, int bits_per_sample, BYTE *buffer, int i) {
  375. if (format_tag == WAVE_FORMAT_PCM) {
  376. int32_t sample = 0;
  377. switch (bits_per_sample) {
  378. case 8:
  379. sample = int32_t(((int8_t *)buffer)[i]) << 24;
  380. break;
  381. case 16:
  382. sample = int32_t(((int16_t *)buffer)[i]) << 16;
  383. break;
  384. case 24:
  385. sample |= int32_t(((int8_t *)buffer)[i * 3 + 2]) << 24;
  386. sample |= int32_t(((int8_t *)buffer)[i * 3 + 1]) << 16;
  387. sample |= int32_t(((int8_t *)buffer)[i * 3 + 0]) << 8;
  388. break;
  389. case 32:
  390. sample = ((int32_t *)buffer)[i];
  391. break;
  392. }
  393. return sample;
  394. } else if (format_tag == WAVE_FORMAT_IEEE_FLOAT) {
  395. return int32_t(((float *)buffer)[i] * 32768.0) << 16;
  396. } else {
  397. ERR_PRINT("WASAPI: Unknown format tag");
  398. }
  399. return 0;
  400. }
  401. void AudioDriverWASAPI::write_sample(WORD format_tag, int bits_per_sample, BYTE *buffer, int i, int32_t sample) {
  402. if (format_tag == WAVE_FORMAT_PCM) {
  403. switch (bits_per_sample) {
  404. case 8:
  405. ((int8_t *)buffer)[i] = sample >> 24;
  406. break;
  407. case 16:
  408. ((int16_t *)buffer)[i] = sample >> 16;
  409. break;
  410. case 24:
  411. ((int8_t *)buffer)[i * 3 + 2] = sample >> 24;
  412. ((int8_t *)buffer)[i * 3 + 1] = sample >> 16;
  413. ((int8_t *)buffer)[i * 3 + 0] = sample >> 8;
  414. break;
  415. case 32:
  416. ((int32_t *)buffer)[i] = sample;
  417. break;
  418. }
  419. } else if (format_tag == WAVE_FORMAT_IEEE_FLOAT) {
  420. ((float *)buffer)[i] = (sample >> 16) / 32768.f;
  421. } else {
  422. ERR_PRINT("WASAPI: Unknown format tag");
  423. }
  424. }
  425. void AudioDriverWASAPI::thread_func(void *p_udata) {
  426. AudioDriverWASAPI *ad = (AudioDriverWASAPI *)p_udata;
  427. uint32_t avail_frames = 0;
  428. uint32_t write_ofs = 0;
  429. while (!ad->exit_thread) {
  430. uint32_t read_frames = 0;
  431. uint32_t written_frames = 0;
  432. if (avail_frames == 0) {
  433. ad->lock();
  434. ad->start_counting_ticks();
  435. if (ad->audio_output.active) {
  436. ad->audio_server_process(ad->buffer_frames, ad->samples_in.ptrw());
  437. } else {
  438. for (int i = 0; i < ad->samples_in.size(); i++) {
  439. ad->samples_in.write[i] = 0;
  440. }
  441. }
  442. avail_frames = ad->buffer_frames;
  443. write_ofs = 0;
  444. ad->stop_counting_ticks();
  445. ad->unlock();
  446. }
  447. ad->lock();
  448. ad->start_counting_ticks();
  449. if (avail_frames > 0 && ad->audio_output.audio_client) {
  450. UINT32 cur_frames;
  451. bool invalidated = false;
  452. HRESULT hr = ad->audio_output.audio_client->GetCurrentPadding(&cur_frames);
  453. if (hr == S_OK) {
  454. // Check how much frames are available on the WASAPI buffer
  455. UINT32 write_frames = MIN(ad->buffer_frames - cur_frames, avail_frames);
  456. if (write_frames > 0) {
  457. BYTE *buffer = nullptr;
  458. hr = ad->audio_output.render_client->GetBuffer(write_frames, &buffer);
  459. if (hr == S_OK) {
  460. // We're using WASAPI Shared Mode so we must convert the buffer
  461. if (ad->channels == ad->audio_output.channels) {
  462. for (unsigned int i = 0; i < write_frames * ad->channels; i++) {
  463. ad->write_sample(ad->audio_output.format_tag, ad->audio_output.bits_per_sample, buffer, i, ad->samples_in.write[write_ofs++]);
  464. }
  465. } else {
  466. for (unsigned int i = 0; i < write_frames; i++) {
  467. for (unsigned int j = 0; j < MIN(ad->channels, ad->audio_output.channels); j++) {
  468. 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++]);
  469. }
  470. if (ad->audio_output.channels > ad->channels) {
  471. for (unsigned int j = ad->channels; j < ad->audio_output.channels; j++) {
  472. ad->write_sample(ad->audio_output.format_tag, ad->audio_output.bits_per_sample, buffer, i * ad->audio_output.channels + j, 0);
  473. }
  474. }
  475. }
  476. }
  477. hr = ad->audio_output.render_client->ReleaseBuffer(write_frames, 0);
  478. if (hr != S_OK) {
  479. ERR_PRINT("WASAPI: Release buffer error");
  480. }
  481. avail_frames -= write_frames;
  482. written_frames += write_frames;
  483. } else if (hr == AUDCLNT_E_DEVICE_INVALIDATED) {
  484. // Device is not valid anymore, reopen it
  485. Error err = ad->finish_render_device();
  486. if (err != OK) {
  487. ERR_PRINT("WASAPI: finish_render_device error");
  488. } else {
  489. // We reopened the device and samples_in may have resized, so invalidate the current avail_frames
  490. avail_frames = 0;
  491. }
  492. } else {
  493. ERR_PRINT("WASAPI: Get buffer error");
  494. ad->exit_thread = true;
  495. }
  496. }
  497. } else if (hr == AUDCLNT_E_DEVICE_INVALIDATED) {
  498. invalidated = true;
  499. } else {
  500. ERR_PRINT("WASAPI: GetCurrentPadding error");
  501. }
  502. if (invalidated) {
  503. // Device is not valid anymore
  504. WARN_PRINT("WASAPI: Current device invalidated, closing device");
  505. Error err = ad->finish_render_device();
  506. if (err != OK) {
  507. ERR_PRINT("WASAPI: finish_render_device error");
  508. }
  509. }
  510. }
  511. // If we're using the Default device and it changed finish it so we'll re-init the device
  512. if (ad->audio_output.device_name == "Default" && default_render_device_changed) {
  513. Error err = ad->finish_render_device();
  514. if (err != OK) {
  515. ERR_PRINT("WASAPI: finish_render_device error");
  516. }
  517. default_render_device_changed = false;
  518. }
  519. // User selected a new device, finish the current one so we'll init the new device
  520. if (ad->audio_output.device_name != ad->audio_output.new_device) {
  521. ad->audio_output.device_name = ad->audio_output.new_device;
  522. Error err = ad->finish_render_device();
  523. if (err != OK) {
  524. ERR_PRINT("WASAPI: finish_render_device error");
  525. }
  526. }
  527. if (!ad->audio_output.audio_client) {
  528. Error err = ad->init_render_device(true);
  529. if (err == OK) {
  530. ad->start();
  531. }
  532. avail_frames = 0;
  533. write_ofs = 0;
  534. }
  535. if (ad->audio_input.active) {
  536. UINT32 packet_length = 0;
  537. BYTE *data;
  538. UINT32 num_frames_available;
  539. DWORD flags;
  540. HRESULT hr = ad->audio_input.capture_client->GetNextPacketSize(&packet_length);
  541. if (hr == S_OK) {
  542. while (packet_length != 0) {
  543. hr = ad->audio_input.capture_client->GetBuffer(&data, &num_frames_available, &flags, nullptr, nullptr);
  544. ERR_BREAK(hr != S_OK);
  545. // fixme: Only works for floating point atm
  546. for (UINT32 j = 0; j < num_frames_available; j++) {
  547. int32_t l, r;
  548. if (flags & AUDCLNT_BUFFERFLAGS_SILENT) {
  549. l = r = 0;
  550. } else {
  551. if (ad->audio_input.channels == 2) {
  552. l = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j * 2);
  553. r = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j * 2 + 1);
  554. } else if (ad->audio_input.channels == 1) {
  555. l = r = read_sample(ad->audio_input.format_tag, ad->audio_input.bits_per_sample, data, j);
  556. } else {
  557. l = r = 0;
  558. ERR_PRINT("WASAPI: unsupported channel count in microphone!");
  559. }
  560. }
  561. ad->input_buffer_write(l);
  562. ad->input_buffer_write(r);
  563. }
  564. read_frames += num_frames_available;
  565. hr = ad->audio_input.capture_client->ReleaseBuffer(num_frames_available);
  566. ERR_BREAK(hr != S_OK);
  567. hr = ad->audio_input.capture_client->GetNextPacketSize(&packet_length);
  568. ERR_BREAK(hr != S_OK);
  569. }
  570. }
  571. // If we're using the Default device and it changed finish it so we'll re-init the device
  572. if (ad->audio_input.device_name == "Default" && default_capture_device_changed) {
  573. Error err = ad->finish_capture_device();
  574. if (err != OK) {
  575. ERR_PRINT("WASAPI: finish_capture_device error");
  576. }
  577. default_capture_device_changed = false;
  578. }
  579. // User selected a new device, finish the current one so we'll init the new device
  580. if (ad->audio_input.device_name != ad->audio_input.new_device) {
  581. ad->audio_input.device_name = ad->audio_input.new_device;
  582. Error err = ad->finish_capture_device();
  583. if (err != OK) {
  584. ERR_PRINT("WASAPI: finish_capture_device error");
  585. }
  586. }
  587. if (!ad->audio_input.audio_client) {
  588. Error err = ad->init_capture_device(true);
  589. if (err == OK) {
  590. ad->capture_start();
  591. }
  592. }
  593. }
  594. ad->stop_counting_ticks();
  595. ad->unlock();
  596. // Let the thread rest a while if we haven't read or write anything
  597. if (written_frames == 0 && read_frames == 0) {
  598. OS::get_singleton()->delay_usec(1000);
  599. }
  600. }
  601. ad->thread_exited = true;
  602. }
  603. void AudioDriverWASAPI::start() {
  604. if (audio_output.audio_client) {
  605. HRESULT hr = audio_output.audio_client->Start();
  606. if (hr != S_OK) {
  607. ERR_PRINT("WASAPI: Start failed");
  608. } else {
  609. audio_output.active = true;
  610. }
  611. }
  612. }
  613. void AudioDriverWASAPI::lock() {
  614. mutex.lock();
  615. }
  616. void AudioDriverWASAPI::unlock() {
  617. mutex.unlock();
  618. }
  619. void AudioDriverWASAPI::finish() {
  620. if (thread) {
  621. exit_thread = true;
  622. Thread::wait_to_finish(thread);
  623. memdelete(thread);
  624. thread = nullptr;
  625. }
  626. finish_capture_device();
  627. finish_render_device();
  628. }
  629. Error AudioDriverWASAPI::capture_start() {
  630. Error err = init_capture_device();
  631. if (err != OK) {
  632. ERR_PRINT("WASAPI: init_capture_device error");
  633. return err;
  634. }
  635. if (audio_input.active) {
  636. return FAILED;
  637. }
  638. audio_input.audio_client->Start();
  639. audio_input.active = true;
  640. return OK;
  641. }
  642. Error AudioDriverWASAPI::capture_stop() {
  643. if (audio_input.active) {
  644. audio_input.audio_client->Stop();
  645. audio_input.active = false;
  646. return OK;
  647. }
  648. return FAILED;
  649. }
  650. void AudioDriverWASAPI::capture_set_device(const String &p_name) {
  651. lock();
  652. audio_input.new_device = p_name;
  653. unlock();
  654. }
  655. Array AudioDriverWASAPI::capture_get_device_list() {
  656. return audio_device_get_list(true);
  657. }
  658. String AudioDriverWASAPI::capture_get_device() {
  659. lock();
  660. String name = audio_input.device_name;
  661. unlock();
  662. return name;
  663. }
  664. AudioDriverWASAPI::AudioDriverWASAPI() {
  665. samples_in.clear();
  666. }
  667. #endif // WASAPI_ENABLED