audio_driver_winrt.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*************************************************************************/
  2. /* audio_driver_winrt.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "audio_driver_winrt.h"
  30. #include "globals.h"
  31. #include "os/os.h"
  32. using namespace Windows::Media;
  33. using namespace Windows::Media::Core;
  34. using namespace Windows::Media::MediaProperties;
  35. using namespace Windows::Media::Editing;
  36. using namespace Windows::Foundation;
  37. const char *AudioDriverWinRT::get_name() const {
  38. return "WinRT";
  39. }
  40. Error AudioDriverWinRT::init() {
  41. active = false;
  42. thread_exited = false;
  43. exit_thread = false;
  44. pcm_open = false;
  45. samples_in = NULL;
  46. mix_rate = 48000;
  47. output_format = OUTPUT_STEREO;
  48. channels = 2;
  49. int latency = GLOBAL_DEF("audio/output_latency", 25);
  50. buffer_size = closest_power_of_2(latency * mix_rate / 1000);
  51. samples_in = memnew_arr(int32_t, buffer_size * channels);
  52. for (int i = 0; i < AUDIO_BUFFERS; i++) {
  53. samples_out[i] = memnew_arr(int16_t, buffer_size * channels);
  54. xaudio_buffer[i].AudioBytes = buffer_size * channels * sizeof(int16_t);
  55. xaudio_buffer[i].pAudioData = (const BYTE *)(samples_out[i]);
  56. xaudio_buffer[i].Flags = 0;
  57. }
  58. HRESULT hr;
  59. hr = XAudio2Create(&xaudio, 0, XAUDIO2_DEFAULT_PROCESSOR);
  60. if (hr != S_OK) {
  61. ERR_EXPLAIN("Error creating XAudio2 engine.");
  62. ERR_FAIL_V(ERR_UNAVAILABLE);
  63. }
  64. hr = xaudio->CreateMasteringVoice(&mastering_voice);
  65. if (hr != S_OK) {
  66. ERR_EXPLAIN("Error creating XAudio2 mastering voice.");
  67. ERR_FAIL_V(ERR_UNAVAILABLE);
  68. }
  69. wave_format.nChannels = channels;
  70. wave_format.cbSize = 0;
  71. wave_format.nSamplesPerSec = mix_rate;
  72. wave_format.wFormatTag = WAVE_FORMAT_PCM;
  73. wave_format.wBitsPerSample = 16;
  74. wave_format.nBlockAlign = channels * wave_format.wBitsPerSample >> 3;
  75. wave_format.nAvgBytesPerSec = mix_rate * wave_format.nBlockAlign;
  76. voice_callback = memnew(XAudio2DriverVoiceCallback);
  77. hr = xaudio->CreateSourceVoice(&source_voice, &wave_format, 0, XAUDIO2_MAX_FREQ_RATIO, voice_callback);
  78. if (hr != S_OK) {
  79. ERR_EXPLAIN("Error creating XAudio2 source voice. " + itos(hr));
  80. ERR_FAIL_V(ERR_UNAVAILABLE);
  81. }
  82. mutex = Mutex::create();
  83. thread = Thread::create(AudioDriverWinRT::thread_func, this);
  84. return OK;
  85. };
  86. void AudioDriverWinRT::thread_func(void *p_udata) {
  87. AudioDriverWinRT *ad = (AudioDriverWinRT *)p_udata;
  88. uint64_t usdelay = (ad->buffer_size / float(ad->mix_rate)) * 1000000;
  89. while (!ad->exit_thread) {
  90. if (!ad->active) {
  91. for (int i = 0; i < AUDIO_BUFFERS; i++) {
  92. ad->xaudio_buffer[i].Flags = XAUDIO2_END_OF_STREAM;
  93. }
  94. } else {
  95. ad->lock();
  96. ad->audio_server_process(ad->buffer_size, ad->samples_in);
  97. ad->unlock();
  98. for (unsigned int i = 0; i < ad->buffer_size * ad->channels; i++) {
  99. ad->samples_out[ad->current_buffer][i] = ad->samples_in[i] >> 16;
  100. }
  101. ad->xaudio_buffer[ad->current_buffer].Flags = 0;
  102. ad->xaudio_buffer[ad->current_buffer].AudioBytes = ad->buffer_size * ad->channels * sizeof(int16_t);
  103. ad->xaudio_buffer[ad->current_buffer].pAudioData = (const BYTE *)(ad->samples_out[ad->current_buffer]);
  104. ad->xaudio_buffer[ad->current_buffer].PlayBegin = 0;
  105. ad->source_voice->SubmitSourceBuffer(&(ad->xaudio_buffer[ad->current_buffer]));
  106. ad->current_buffer = (ad->current_buffer + 1) % AUDIO_BUFFERS;
  107. XAUDIO2_VOICE_STATE state;
  108. while (ad->source_voice->GetState(&state), state.BuffersQueued > AUDIO_BUFFERS - 1) {
  109. WaitForSingleObject(ad->voice_callback->buffer_end_event, INFINITE);
  110. }
  111. }
  112. };
  113. ad->thread_exited = true;
  114. };
  115. void AudioDriverWinRT::start() {
  116. active = true;
  117. HRESULT hr = source_voice->Start(0);
  118. if (hr != S_OK) {
  119. ERR_EXPLAIN("XAudio2 start error " + itos(hr));
  120. ERR_FAIL();
  121. }
  122. };
  123. int AudioDriverWinRT::get_mix_rate() const {
  124. return mix_rate;
  125. };
  126. AudioDriverSW::OutputFormat AudioDriverWinRT::get_output_format() const {
  127. return output_format;
  128. };
  129. float AudioDriverWinRT::get_latency() {
  130. XAUDIO2_PERFORMANCE_DATA perf_data;
  131. xaudio->GetPerformanceData(&perf_data);
  132. if (perf_data.CurrentLatencyInSamples) {
  133. return (float)(perf_data.CurrentLatencyInSamples / ((float)mix_rate));
  134. } else {
  135. return 0;
  136. }
  137. }
  138. void AudioDriverWinRT::lock() {
  139. if (!thread || !mutex)
  140. return;
  141. mutex->lock();
  142. };
  143. void AudioDriverWinRT::unlock() {
  144. if (!thread || !mutex)
  145. return;
  146. mutex->unlock();
  147. };
  148. void AudioDriverWinRT::finish() {
  149. if (!thread)
  150. return;
  151. exit_thread = true;
  152. Thread::wait_to_finish(thread);
  153. if (source_voice) {
  154. source_voice->Stop(0);
  155. memdelete(source_voice);
  156. }
  157. if (samples_in) {
  158. memdelete_arr(samples_in);
  159. };
  160. if (samples_out[0]) {
  161. for (int i = 0; i < AUDIO_BUFFERS; i++) {
  162. memdelete_arr(samples_out[i]);
  163. }
  164. };
  165. memdelete(voice_callback);
  166. memdelete(mastering_voice);
  167. memdelete(thread);
  168. if (mutex)
  169. memdelete(mutex);
  170. thread = NULL;
  171. };
  172. AudioDriverWinRT::AudioDriverWinRT() {
  173. mutex = NULL;
  174. thread = NULL;
  175. wave_format = { 0 };
  176. for (int i = 0; i < AUDIO_BUFFERS; i++) {
  177. xaudio_buffer[i] = { 0 };
  178. samples_out[i] = 0;
  179. }
  180. current_buffer = 0;
  181. };
  182. AudioDriverWinRT::~AudioDriverWinRT(){
  183. };