audio_driver_xaudio2.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**************************************************************************/
  2. /* audio_driver_xaudio2.h */
  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. #pragma once
  31. #include "core/os/mutex.h"
  32. #include "core/os/thread.h"
  33. #include "core/templates/safe_refcount.h"
  34. #include "servers/audio_server.h"
  35. #include <mmsystem.h>
  36. #define WIN32_LEAN_AND_MEAN
  37. #include <windows.h>
  38. #include <wrl/client.h>
  39. #include <xaudio2.h>
  40. class AudioDriverXAudio2 : public AudioDriver {
  41. enum {
  42. AUDIO_BUFFERS = 2
  43. };
  44. struct XAudio2DriverVoiceCallback : public IXAudio2VoiceCallback {
  45. HANDLE buffer_end_event;
  46. XAudio2DriverVoiceCallback() :
  47. buffer_end_event(CreateEvent(nullptr, FALSE, FALSE, nullptr)) {}
  48. void STDMETHODCALLTYPE OnBufferEnd(void *pBufferContext) {
  49. SetEvent(buffer_end_event);
  50. }
  51. //Unused methods are stubs
  52. void STDMETHODCALLTYPE OnStreamEnd() {}
  53. void STDMETHODCALLTYPE OnVoiceProcessingPassEnd() {}
  54. void STDMETHODCALLTYPE OnVoiceProcessingPassStart(UINT32 SamplesRequired) {}
  55. void STDMETHODCALLTYPE OnBufferStart(void *pBufferContext) {}
  56. void STDMETHODCALLTYPE OnLoopEnd(void *pBufferContext) {}
  57. void STDMETHODCALLTYPE OnVoiceError(void *pBufferContext, HRESULT Error) {}
  58. };
  59. Thread thread;
  60. Mutex mutex;
  61. int32_t *samples_in = nullptr;
  62. int16_t *samples_out[AUDIO_BUFFERS];
  63. static void thread_func(void *p_udata);
  64. int buffer_size = 0;
  65. unsigned int mix_rate = 0;
  66. SpeakerMode speaker_mode = SpeakerMode::SPEAKER_MODE_STEREO;
  67. int channels = 0;
  68. SafeFlag active;
  69. SafeFlag exit_thread;
  70. bool pcm_open = false;
  71. WAVEFORMATEX wave_format = { 0 };
  72. Microsoft::WRL::ComPtr<IXAudio2> xaudio;
  73. int current_buffer = 0;
  74. IXAudio2MasteringVoice *mastering_voice = nullptr;
  75. XAUDIO2_BUFFER xaudio_buffer[AUDIO_BUFFERS];
  76. IXAudio2SourceVoice *source_voice = nullptr;
  77. XAudio2DriverVoiceCallback voice_callback;
  78. public:
  79. virtual const char *get_name() const override {
  80. return "XAudio2";
  81. }
  82. virtual Error init() override;
  83. virtual void start() override;
  84. virtual int get_mix_rate() const override;
  85. virtual SpeakerMode get_speaker_mode() const override;
  86. virtual float get_latency() override;
  87. virtual void lock() override;
  88. virtual void unlock() override;
  89. virtual void finish() override;
  90. AudioDriverXAudio2();
  91. ~AudioDriverXAudio2() {}
  92. };