audio_driver_xaudio2.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 "core/os/os.h"
  32. #include "core/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_RST("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. while (!ad->exit_thread) {
  85. if (!ad->active) {
  86. for (int i = 0; i < AUDIO_BUFFERS; i++) {
  87. ad->xaudio_buffer[i].Flags = XAUDIO2_END_OF_STREAM;
  88. }
  89. } else {
  90. ad->lock();
  91. ad->audio_server_process(ad->buffer_size, ad->samples_in);
  92. ad->unlock();
  93. for (unsigned int i = 0; i < ad->buffer_size * ad->channels; i++) {
  94. ad->samples_out[ad->current_buffer][i] = ad->samples_in[i] >> 16;
  95. }
  96. ad->xaudio_buffer[ad->current_buffer].Flags = 0;
  97. ad->xaudio_buffer[ad->current_buffer].AudioBytes = ad->buffer_size * ad->channels * sizeof(int16_t);
  98. ad->xaudio_buffer[ad->current_buffer].pAudioData = (const BYTE *)(ad->samples_out[ad->current_buffer]);
  99. ad->xaudio_buffer[ad->current_buffer].PlayBegin = 0;
  100. ad->source_voice->SubmitSourceBuffer(&(ad->xaudio_buffer[ad->current_buffer]));
  101. ad->current_buffer = (ad->current_buffer + 1) % AUDIO_BUFFERS;
  102. XAUDIO2_VOICE_STATE state;
  103. while (ad->source_voice->GetState(&state), state.BuffersQueued > AUDIO_BUFFERS - 1) {
  104. WaitForSingleObject(ad->voice_callback.buffer_end_event, INFINITE);
  105. }
  106. }
  107. }
  108. ad->thread_exited = true;
  109. }
  110. void AudioDriverXAudio2::start() {
  111. active = true;
  112. HRESULT hr = source_voice->Start(0);
  113. if (hr != S_OK) {
  114. ERR_EXPLAIN("XAudio2 start error " + itos(hr));
  115. ERR_FAIL();
  116. }
  117. }
  118. int AudioDriverXAudio2::get_mix_rate() const {
  119. return mix_rate;
  120. }
  121. AudioDriver::SpeakerMode AudioDriverXAudio2::get_speaker_mode() const {
  122. return speaker_mode;
  123. }
  124. float AudioDriverXAudio2::get_latency() {
  125. XAUDIO2_PERFORMANCE_DATA perf_data;
  126. xaudio->GetPerformanceData(&perf_data);
  127. if (perf_data.CurrentLatencyInSamples) {
  128. return (float)(perf_data.CurrentLatencyInSamples / ((float)mix_rate));
  129. } else {
  130. return 0;
  131. }
  132. }
  133. void AudioDriverXAudio2::lock() {
  134. if (!thread || !mutex)
  135. return;
  136. mutex->lock();
  137. }
  138. void AudioDriverXAudio2::unlock() {
  139. if (!thread || !mutex)
  140. return;
  141. mutex->unlock();
  142. }
  143. void AudioDriverXAudio2::finish() {
  144. if (!thread)
  145. return;
  146. exit_thread = true;
  147. Thread::wait_to_finish(thread);
  148. if (source_voice) {
  149. source_voice->Stop(0);
  150. source_voice->DestroyVoice();
  151. }
  152. if (samples_in) {
  153. memdelete_arr(samples_in);
  154. }
  155. if (samples_out[0]) {
  156. for (int i = 0; i < AUDIO_BUFFERS; i++) {
  157. memdelete_arr(samples_out[i]);
  158. }
  159. }
  160. mastering_voice->DestroyVoice();
  161. memdelete(thread);
  162. if (mutex)
  163. memdelete(mutex);
  164. thread = NULL;
  165. }
  166. AudioDriverXAudio2::AudioDriverXAudio2() :
  167. thread(NULL),
  168. mutex(NULL),
  169. current_buffer(0) {
  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. }
  176. AudioDriverXAudio2::~AudioDriverXAudio2() {
  177. }