audio_driver_xaudio2.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*************************************************************************/
  2. /* audio_driver_xaudio2.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. #include "audio_driver_xaudio2.h"
  31. #include "os/os.h"
  32. #include "project_settings.h"
  33. const char *AudioDriverXAudio2::get_name() const {
  34. return "XAudio2";
  35. }
  36. Error AudioDriverXAudio2::init() {
  37. active = false;
  38. thread_exited = false;
  39. exit_thread = false;
  40. pcm_open = false;
  41. samples_in = NULL;
  42. mix_rate = 48000;
  43. // FIXME: speaker_mode seems unused in the Xaudio2 driver so far
  44. speaker_mode = SPEAKER_MODE_STEREO;
  45. channels = 2;
  46. int latency = GLOBAL_DEF("audio/output_latency", 25);
  47. buffer_size = closest_power_of_2(latency * mix_rate / 1000);
  48. samples_in = memnew_arr(int32_t, buffer_size * channels);
  49. for (int i = 0; i < AUDIO_BUFFERS; i++) {
  50. samples_out[i] = memnew_arr(int16_t, buffer_size * channels);
  51. xaudio_buffer[i].AudioBytes = buffer_size * channels * sizeof(int16_t);
  52. xaudio_buffer[i].pAudioData = (const BYTE *)(samples_out[i]);
  53. xaudio_buffer[i].Flags = 0;
  54. }
  55. HRESULT hr;
  56. hr = XAudio2Create(&xaudio, 0, XAUDIO2_DEFAULT_PROCESSOR);
  57. if (hr != S_OK) {
  58. ERR_EXPLAIN("Error creating XAudio2 engine.");
  59. ERR_FAIL_V(ERR_UNAVAILABLE);
  60. }
  61. hr = xaudio->CreateMasteringVoice(&mastering_voice);
  62. if (hr != S_OK) {
  63. ERR_EXPLAIN("Error creating XAudio2 mastering voice.");
  64. ERR_FAIL_V(ERR_UNAVAILABLE);
  65. }
  66. wave_format.nChannels = channels;
  67. wave_format.cbSize = 0;
  68. wave_format.nSamplesPerSec = mix_rate;
  69. wave_format.wFormatTag = WAVE_FORMAT_PCM;
  70. wave_format.wBitsPerSample = 16;
  71. wave_format.nBlockAlign = channels * wave_format.wBitsPerSample >> 3;
  72. wave_format.nAvgBytesPerSec = mix_rate * wave_format.nBlockAlign;
  73. hr = xaudio->CreateSourceVoice(&source_voice, &wave_format, 0, XAUDIO2_MAX_FREQ_RATIO, &voice_callback);
  74. if (hr != S_OK) {
  75. ERR_EXPLAIN("Error creating XAudio2 source voice. " + itos(hr));
  76. ERR_FAIL_V(ERR_UNAVAILABLE);
  77. }
  78. mutex = Mutex::create();
  79. thread = Thread::create(AudioDriverXAudio2::thread_func, this);
  80. return OK;
  81. };
  82. void AudioDriverXAudio2::thread_func(void *p_udata) {
  83. AudioDriverXAudio2 *ad = (AudioDriverXAudio2 *)p_udata;
  84. uint64_t usdelay = (ad->buffer_size / float(ad->mix_rate)) * 1000000;
  85. while (!ad->exit_thread) {
  86. if (!ad->active) {
  87. for (int i = 0; i < AUDIO_BUFFERS; i++) {
  88. ad->xaudio_buffer[i].Flags = XAUDIO2_END_OF_STREAM;
  89. }
  90. } else {
  91. ad->lock();
  92. ad->audio_server_process(ad->buffer_size, ad->samples_in);
  93. ad->unlock();
  94. for (unsigned int i = 0; i < ad->buffer_size * ad->channels; i++) {
  95. ad->samples_out[ad->current_buffer][i] = ad->samples_in[i] >> 16;
  96. }
  97. ad->xaudio_buffer[ad->current_buffer].Flags = 0;
  98. ad->xaudio_buffer[ad->current_buffer].AudioBytes = ad->buffer_size * ad->channels * sizeof(int16_t);
  99. ad->xaudio_buffer[ad->current_buffer].pAudioData = (const BYTE *)(ad->samples_out[ad->current_buffer]);
  100. ad->xaudio_buffer[ad->current_buffer].PlayBegin = 0;
  101. ad->source_voice->SubmitSourceBuffer(&(ad->xaudio_buffer[ad->current_buffer]));
  102. ad->current_buffer = (ad->current_buffer + 1) % AUDIO_BUFFERS;
  103. XAUDIO2_VOICE_STATE state;
  104. while (ad->source_voice->GetState(&state), state.BuffersQueued > AUDIO_BUFFERS - 1) {
  105. WaitForSingleObject(ad->voice_callback.buffer_end_event, INFINITE);
  106. }
  107. }
  108. };
  109. ad->thread_exited = true;
  110. };
  111. void AudioDriverXAudio2::start() {
  112. active = true;
  113. HRESULT hr = source_voice->Start(0);
  114. if (hr != S_OK) {
  115. ERR_EXPLAIN("XAudio2 start error " + itos(hr));
  116. ERR_FAIL();
  117. }
  118. };
  119. int AudioDriverXAudio2::get_mix_rate() const {
  120. return mix_rate;
  121. };
  122. AudioDriver::SpeakerMode AudioDriverXAudio2::get_speaker_mode() const {
  123. return speaker_mode;
  124. };
  125. float AudioDriverXAudio2::get_latency() {
  126. XAUDIO2_PERFORMANCE_DATA perf_data;
  127. xaudio->GetPerformanceData(&perf_data);
  128. if (perf_data.CurrentLatencyInSamples) {
  129. return (float)(perf_data.CurrentLatencyInSamples / ((float)mix_rate));
  130. } else {
  131. return 0;
  132. }
  133. }
  134. void AudioDriverXAudio2::lock() {
  135. if (!thread || !mutex)
  136. return;
  137. mutex->lock();
  138. };
  139. void AudioDriverXAudio2::unlock() {
  140. if (!thread || !mutex)
  141. return;
  142. mutex->unlock();
  143. };
  144. void AudioDriverXAudio2::finish() {
  145. if (!thread)
  146. return;
  147. exit_thread = true;
  148. Thread::wait_to_finish(thread);
  149. if (source_voice) {
  150. source_voice->Stop(0);
  151. source_voice->DestroyVoice();
  152. }
  153. if (samples_in) {
  154. memdelete_arr(samples_in);
  155. };
  156. if (samples_out[0]) {
  157. for (int i = 0; i < AUDIO_BUFFERS; i++) {
  158. memdelete_arr(samples_out[i]);
  159. }
  160. };
  161. mastering_voice->DestroyVoice();
  162. memdelete(thread);
  163. if (mutex)
  164. memdelete(mutex);
  165. thread = NULL;
  166. };
  167. AudioDriverXAudio2::AudioDriverXAudio2() {
  168. mutex = NULL;
  169. thread = NULL;
  170. wave_format = { 0 };
  171. for (int i = 0; i < AUDIO_BUFFERS; i++) {
  172. xaudio_buffer[i] = { 0 };
  173. samples_out[i] = 0;
  174. }
  175. current_buffer = 0;
  176. };
  177. AudioDriverXAudio2::~AudioDriverXAudio2(){
  178. };