audio_driver_wasapi.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**************************************************************************/
  2. /* audio_driver_wasapi.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_WASAPI_H
  31. #define AUDIO_DRIVER_WASAPI_H
  32. #ifdef WASAPI_ENABLED
  33. #include "core/os/mutex.h"
  34. #include "core/os/thread.h"
  35. #include "core/templates/safe_refcount.h"
  36. #include "servers/audio_server.h"
  37. #include <audioclient.h>
  38. #include <mmdeviceapi.h>
  39. #define WIN32_LEAN_AND_MEAN
  40. #include <windows.h>
  41. class AudioDriverWASAPI : public AudioDriver {
  42. class AudioDeviceWASAPI {
  43. public:
  44. IAudioClient *audio_client = nullptr;
  45. IAudioRenderClient *render_client = nullptr; // Output
  46. IAudioCaptureClient *capture_client = nullptr; // Input
  47. SafeFlag active;
  48. WORD format_tag = 0;
  49. WORD bits_per_sample = 0;
  50. unsigned int channels = 0;
  51. unsigned int frame_size = 0;
  52. String device_name = "Default"; // Output OR Input
  53. String new_device = "Default"; // Output OR Input
  54. AudioDeviceWASAPI() {}
  55. };
  56. AudioDeviceWASAPI audio_input;
  57. AudioDeviceWASAPI audio_output;
  58. Mutex mutex;
  59. Thread thread;
  60. Vector<int32_t> samples_in;
  61. unsigned int channels = 0;
  62. int mix_rate = 0;
  63. int buffer_frames = 0;
  64. int target_latency_ms = 0;
  65. float real_latency = 0.0;
  66. bool using_audio_client_3 = false;
  67. SafeFlag exit_thread;
  68. static _FORCE_INLINE_ void write_sample(WORD format_tag, int bits_per_sample, BYTE *buffer, int i, int32_t sample);
  69. static _FORCE_INLINE_ int32_t read_sample(WORD format_tag, int bits_per_sample, BYTE *buffer, int i);
  70. static void thread_func(void *p_udata);
  71. Error init_output_device(bool p_reinit = false);
  72. Error init_input_device(bool p_reinit = false);
  73. Error finish_output_device();
  74. Error finish_input_device();
  75. Error audio_device_init(AudioDeviceWASAPI *p_device, bool p_input, bool p_reinit, bool p_no_audio_client_3 = false);
  76. Error audio_device_finish(AudioDeviceWASAPI *p_device);
  77. PackedStringArray audio_device_get_list(bool p_input);
  78. public:
  79. virtual const char *get_name() const override {
  80. return "WASAPI";
  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. virtual PackedStringArray get_output_device_list() override;
  91. virtual String get_output_device() override;
  92. virtual void set_output_device(const String &p_name) override;
  93. virtual Error input_start() override;
  94. virtual Error input_stop() override;
  95. virtual PackedStringArray get_input_device_list() override;
  96. virtual String get_input_device() override;
  97. virtual void set_input_device(const String &p_name) override;
  98. AudioDriverWASAPI();
  99. };
  100. #endif // WASAPI_ENABLED
  101. #endif // AUDIO_DRIVER_WASAPI_H