| 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 | |
| 31 | #include "audio_driver_xaudio2.h" |
| 32 | |
| 33 | #include "core/config/project_settings.h" |
| 34 | #include "core/os/os.h" |
| 35 | |
| 36 | Error AudioDriverXAudio2::init() { |
| 37 | active.clear(); |
| 38 | exit_thread.clear(); |
| 39 | pcm_open = false; |
| 40 | samples_in = nullptr; |
| 41 | |
| 42 | mix_rate = _get_configured_mix_rate(); |
| 43 | |
| 44 | // FIXME: speaker_mode seems unused in the Xaudio2 driver so far |
| 45 | speaker_mode = SPEAKER_MODE_STEREO; |
| 46 | channels = 2; |
| 47 | |
| 48 | int latency = Engine::get_singleton()->get_audio_output_latency(); |
| 49 | buffer_size = closest_power_of_2(latency * mix_rate / 1000); |
| 50 | |
| 51 | samples_in = memnew_arr(int32_t, size_t(buffer_size) * channels); |
| 52 | for (int i = 0; i < AUDIO_BUFFERS; i++) { |
| 53 | samples_out[i] = memnew_arr(int16_t, size_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 | |
| 59 | HRESULT hr; |
| 60 | hr = XAudio2Create(&xaudio, 0, XAUDIO2_DEFAULT_PROCESSOR); |
| 61 | ERR_FAIL_COND_V_MSG(hr != S_OK, ERR_UNAVAILABLE, "Error creating XAudio2 engine."); |
| 62 | |
| 63 | hr = xaudio->CreateMasteringVoice(&mastering_voice); |
| 64 | ERR_FAIL_COND_V_MSG(hr != S_OK, ERR_UNAVAILABLE, "Error creating XAudio2 mastering voice."); |
| 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 | |
| 74 | hr = xaudio->CreateSourceVoice(&source_voice, &wave_format, 0, XAUDIO2_MAX_FREQ_RATIO, &voice_callback); |
| 75 | ERR_FAIL_COND_V_MSG(hr != S_OK, ERR_UNAVAILABLE, "Error creating XAudio2 source voice. Error code: " + itos(hr) + "."); |
| 76 | |
| 77 | thread.start(AudioDriverXAudio2::thread_func, this); |
| 78 | |
| 79 | return OK; |
| 80 | } |
| 81 | |
| 82 | void AudioDriverXAudio2::thread_func(void *p_udata) { |
| 83 | AudioDriverXAudio2 *ad = static_cast<AudioDriverXAudio2 *>(p_udata); |
| 84 | |
| 85 | while (!ad->exit_thread.is_set()) { |
| 86 | if (!ad->active.is_set()) { |
| 87 | for (int i = 0; i < AUDIO_BUFFERS; i++) { |
| 88 | ad->xaudio_buffer[i].Flags = XAUDIO2_END_OF_STREAM; |
| 89 | } |
| 90 | |
| 91 | } else { |
| 92 | ad->lock(); |
| 93 | ad->start_counting_ticks(); |
| 94 | |
| 95 | ad->audio_server_process(ad->buffer_size, ad->samples_in); |
| 96 | |
| 97 | ad->stop_counting_ticks(); |
| 98 | ad->unlock(); |
| 99 | |
| 100 | for (unsigned int i = 0; i < ad->buffer_size * ad->channels; i++) { |
| 101 | ad->samples_out[ad->current_buffer][i] = ad->samples_in[i] >> 16; |
| 102 | } |
| 103 | |
| 104 | ad->xaudio_buffer[ad->current_buffer].Flags = 0; |
| 105 | ad->xaudio_buffer[ad->current_buffer].AudioBytes = ad->buffer_size * ad->channels * sizeof(int16_t); |
| 106 | ad->xaudio_buffer[ad->current_buffer].pAudioData = (const BYTE *)(ad->samples_out[ad->current_buffer]); |
| 107 | ad->xaudio_buffer[ad->current_buffer].PlayBegin = 0; |
| 108 | ad->source_voice->SubmitSourceBuffer(&(ad->xaudio_buffer[ad->current_buffer])); |
| 109 | |
| 110 | ad->current_buffer = (ad->current_buffer + 1) % AUDIO_BUFFERS; |
| 111 | |
| 112 | XAUDIO2_VOICE_STATE state; |
| 113 | while (ad->source_voice->GetState(&state), state.BuffersQueued > AUDIO_BUFFERS - 1) { |
| 114 | WaitForSingleObject(ad->voice_callback.buffer_end_event, INFINITE); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void AudioDriverXAudio2::start() { |
| 121 | active.set(); |
| 122 | HRESULT hr = source_voice->Start(0); |
| 123 | ERR_FAIL_COND_MSG(hr != S_OK, "Error starting XAudio2 driver. Error code: " + itos(hr) + "."); |
| 124 | } |
| 125 | |
| 126 | int AudioDriverXAudio2::get_mix_rate() const { |
| 127 | return mix_rate; |
| 128 | } |
| 129 | |
| 130 | AudioDriver::SpeakerMode AudioDriverXAudio2::get_speaker_mode() const { |
| 131 | return speaker_mode; |
| 132 | } |
| 133 | |
| 134 | float AudioDriverXAudio2::get_latency() { |
| 135 | XAUDIO2_PERFORMANCE_DATA perf_data; |
| 136 | xaudio->GetPerformanceData(&perf_data); |
| 137 | if (perf_data.CurrentLatencyInSamples) { |
| 138 | return (float)(perf_data.CurrentLatencyInSamples / ((float)mix_rate)); |
| 139 | } else { |
| 140 | return 0; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | void AudioDriverXAudio2::lock() { |
| 145 | mutex.lock(); |
| 146 | } |
| 147 | |
| 148 | void AudioDriverXAudio2::unlock() { |
| 149 | mutex.unlock(); |
| 150 | } |
| 151 | |
| 152 | void AudioDriverXAudio2::finish() { |
| 153 | exit_thread.set(); |
| 154 | if (thread.is_started()) { |
| 155 | thread.wait_to_finish(); |
| 156 | } |
| 157 | |
| 158 | if (source_voice) { |
| 159 | source_voice->Stop(0); |
| 160 | source_voice->DestroyVoice(); |
| 161 | } |
| 162 | |
| 163 | if (samples_in) { |
| 164 | memdelete_arr(samples_in); |
| 165 | } |
| 166 | if (samples_out[0]) { |
| 167 | for (int i = 0; i < AUDIO_BUFFERS; i++) { |
| 168 | memdelete_arr(samples_out[i]); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | mastering_voice->DestroyVoice(); |
| 173 | } |
| 174 | |
| 175 | AudioDriverXAudio2::AudioDriverXAudio2() { |
| 176 | for (int i = 0; i < AUDIO_BUFFERS; i++) { |
| 177 | xaudio_buffer[i] = { 0 }; |
| 178 | samples_out[i] = 0; |
| 179 | } |
| 180 | } |