audio_driver_xaudio2.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**************************************************************************/
  2. /* audio_driver_xaudio2.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. #include "audio_driver_xaudio2.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/os/os.h"
  33. Error AudioDriverXAudio2::init() {
  34. active.clear();
  35. exit_thread.clear();
  36. pcm_open = false;
  37. samples_in = nullptr;
  38. mix_rate = _get_configured_mix_rate();
  39. // FIXME: speaker_mode seems unused in the Xaudio2 driver so far
  40. speaker_mode = SPEAKER_MODE_STEREO;
  41. channels = 2;
  42. int latency = GLOBAL_GET("audio/driver/output_latency");
  43. buffer_size = closest_power_of_2(latency * mix_rate / 1000);
  44. samples_in = memnew_arr(int32_t, buffer_size * channels);
  45. for (int i = 0; i < AUDIO_BUFFERS; i++) {
  46. samples_out[i] = memnew_arr(int16_t, buffer_size * channels);
  47. xaudio_buffer[i].AudioBytes = buffer_size * channels * sizeof(int16_t);
  48. xaudio_buffer[i].pAudioData = (const BYTE *)(samples_out[i]);
  49. xaudio_buffer[i].Flags = 0;
  50. }
  51. HRESULT hr;
  52. hr = XAudio2Create(&xaudio, 0, XAUDIO2_DEFAULT_PROCESSOR);
  53. ERR_FAIL_COND_V_MSG(hr != S_OK, ERR_UNAVAILABLE, "Error creating XAudio2 engine.");
  54. hr = xaudio->CreateMasteringVoice(&mastering_voice);
  55. ERR_FAIL_COND_V_MSG(hr != S_OK, ERR_UNAVAILABLE, "Error creating XAudio2 mastering voice.");
  56. wave_format.nChannels = channels;
  57. wave_format.cbSize = 0;
  58. wave_format.nSamplesPerSec = mix_rate;
  59. wave_format.wFormatTag = WAVE_FORMAT_PCM;
  60. wave_format.wBitsPerSample = 16;
  61. wave_format.nBlockAlign = channels * wave_format.wBitsPerSample >> 3;
  62. wave_format.nAvgBytesPerSec = mix_rate * wave_format.nBlockAlign;
  63. hr = xaudio->CreateSourceVoice(&source_voice, &wave_format, 0, XAUDIO2_MAX_FREQ_RATIO, &voice_callback);
  64. ERR_FAIL_COND_V_MSG(hr != S_OK, ERR_UNAVAILABLE, "Error creating XAudio2 source voice. Error code: " + itos(hr) + ".");
  65. thread.start(AudioDriverXAudio2::thread_func, this);
  66. return OK;
  67. }
  68. void AudioDriverXAudio2::thread_func(void *p_udata) {
  69. AudioDriverXAudio2 *ad = static_cast<AudioDriverXAudio2 *>(p_udata);
  70. while (!ad->exit_thread.is_set()) {
  71. if (!ad->active.is_set()) {
  72. for (int i = 0; i < AUDIO_BUFFERS; i++) {
  73. ad->xaudio_buffer[i].Flags = XAUDIO2_END_OF_STREAM;
  74. }
  75. } else {
  76. ad->lock();
  77. ad->start_counting_ticks();
  78. ad->audio_server_process(ad->buffer_size, ad->samples_in);
  79. ad->stop_counting_ticks();
  80. ad->unlock();
  81. for (unsigned int i = 0; i < ad->buffer_size * ad->channels; i++) {
  82. ad->samples_out[ad->current_buffer][i] = ad->samples_in[i] >> 16;
  83. }
  84. ad->xaudio_buffer[ad->current_buffer].Flags = 0;
  85. ad->xaudio_buffer[ad->current_buffer].AudioBytes = ad->buffer_size * ad->channels * sizeof(int16_t);
  86. ad->xaudio_buffer[ad->current_buffer].pAudioData = (const BYTE *)(ad->samples_out[ad->current_buffer]);
  87. ad->xaudio_buffer[ad->current_buffer].PlayBegin = 0;
  88. ad->source_voice->SubmitSourceBuffer(&(ad->xaudio_buffer[ad->current_buffer]));
  89. ad->current_buffer = (ad->current_buffer + 1) % AUDIO_BUFFERS;
  90. XAUDIO2_VOICE_STATE state;
  91. while (ad->source_voice->GetState(&state), state.BuffersQueued > AUDIO_BUFFERS - 1) {
  92. WaitForSingleObject(ad->voice_callback.buffer_end_event, INFINITE);
  93. }
  94. }
  95. }
  96. }
  97. void AudioDriverXAudio2::start() {
  98. active.set();
  99. HRESULT hr = source_voice->Start(0);
  100. ERR_FAIL_COND_MSG(hr != S_OK, "Error starting XAudio2 driver. Error code: " + itos(hr) + ".");
  101. }
  102. int AudioDriverXAudio2::get_mix_rate() const {
  103. return mix_rate;
  104. }
  105. AudioDriver::SpeakerMode AudioDriverXAudio2::get_speaker_mode() const {
  106. return speaker_mode;
  107. }
  108. float AudioDriverXAudio2::get_latency() {
  109. XAUDIO2_PERFORMANCE_DATA perf_data;
  110. xaudio->GetPerformanceData(&perf_data);
  111. if (perf_data.CurrentLatencyInSamples) {
  112. return (float)(perf_data.CurrentLatencyInSamples / ((float)mix_rate));
  113. } else {
  114. return 0;
  115. }
  116. }
  117. void AudioDriverXAudio2::lock() {
  118. mutex.lock();
  119. }
  120. void AudioDriverXAudio2::unlock() {
  121. mutex.unlock();
  122. }
  123. void AudioDriverXAudio2::finish() {
  124. exit_thread.set();
  125. thread.wait_to_finish();
  126. if (source_voice) {
  127. source_voice->Stop(0);
  128. source_voice->DestroyVoice();
  129. }
  130. if (samples_in) {
  131. memdelete_arr(samples_in);
  132. }
  133. if (samples_out[0]) {
  134. for (int i = 0; i < AUDIO_BUFFERS; i++) {
  135. memdelete_arr(samples_out[i]);
  136. }
  137. }
  138. mastering_voice->DestroyVoice();
  139. }
  140. AudioDriverXAudio2::AudioDriverXAudio2() {
  141. for (int i = 0; i < AUDIO_BUFFERS; i++) {
  142. xaudio_buffer[i] = { 0 };
  143. samples_out[i] = 0;
  144. }
  145. }