audio_driver_xaudio2.h 4.3 KB

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