audio_driver_xaudio2.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*************************************************************************/
  2. /* audio_driver_xaudio2.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  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 "servers/audio_server.h"
  35. #include <mmsystem.h>
  36. #include <windows.h>
  37. #include <wrl/client.h>
  38. #include <xaudio2.h>
  39. class AudioDriverXAudio2 : public AudioDriver {
  40. enum {
  41. AUDIO_BUFFERS = 2
  42. };
  43. struct XAudio2DriverVoiceCallback : public IXAudio2VoiceCallback {
  44. HANDLE buffer_end_event;
  45. XAudio2DriverVoiceCallback()
  46. : buffer_end_event(CreateEvent(NULL, FALSE, FALSE, NULL)) {}
  47. void STDMETHODCALLTYPE OnBufferEnd(void *pBufferContext) { /*print_line("buffer ended");*/
  48. SetEvent(buffer_end_event);
  49. }
  50. //Unused methods are stubs
  51. void STDMETHODCALLTYPE OnStreamEnd() {}
  52. void STDMETHODCALLTYPE OnVoiceProcessingPassEnd() {}
  53. void STDMETHODCALLTYPE OnVoiceProcessingPassStart(UINT32 SamplesRequired) {}
  54. void STDMETHODCALLTYPE OnBufferStart(void *pBufferContext) {}
  55. void STDMETHODCALLTYPE OnLoopEnd(void *pBufferContext) {}
  56. void STDMETHODCALLTYPE OnVoiceError(void *pBufferContext, HRESULT Error) {}
  57. };
  58. Thread *thread;
  59. Mutex *mutex;
  60. int32_t *samples_in;
  61. int16_t *samples_out[AUDIO_BUFFERS];
  62. static void thread_func(void *p_udata);
  63. int buffer_size;
  64. unsigned int mix_rate;
  65. SpeakerMode speaker_mode;
  66. int channels;
  67. bool active;
  68. bool thread_exited;
  69. mutable bool exit_thread;
  70. bool pcm_open;
  71. WAVEFORMATEX wave_format;
  72. Microsoft::WRL::ComPtr<IXAudio2> xaudio;
  73. int current_buffer;
  74. IXAudio2MasteringVoice *mastering_voice;
  75. XAUDIO2_BUFFER xaudio_buffer[AUDIO_BUFFERS];
  76. IXAudio2SourceVoice *source_voice;
  77. XAudio2DriverVoiceCallback voice_callback;
  78. public:
  79. const char *get_name() const;
  80. virtual Error init();
  81. virtual void start();
  82. virtual int get_mix_rate() const;
  83. virtual SpeakerMode get_speaker_mode() const;
  84. virtual float get_latency();
  85. virtual void lock();
  86. virtual void unlock();
  87. virtual void finish();
  88. AudioDriverXAudio2();
  89. ~AudioDriverXAudio2();
  90. };
  91. #endif