audio_driver_wasapi.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/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;
  45. IAudioRenderClient *render_client;
  46. IAudioCaptureClient *capture_client;
  47. SafeFlag active;
  48. WORD format_tag;
  49. WORD bits_per_sample;
  50. unsigned int channels;
  51. unsigned int frame_size;
  52. String device_name;
  53. String new_device;
  54. AudioDeviceWASAPI() :
  55. audio_client(NULL),
  56. render_client(NULL),
  57. capture_client(NULL),
  58. format_tag(0),
  59. bits_per_sample(0),
  60. channels(0),
  61. frame_size(0),
  62. device_name("Default"),
  63. new_device("Default") {
  64. }
  65. };
  66. AudioDeviceWASAPI audio_input;
  67. AudioDeviceWASAPI audio_output;
  68. Mutex mutex;
  69. Thread thread;
  70. Vector<int32_t> samples_in;
  71. unsigned int channels;
  72. int mix_rate;
  73. int buffer_frames;
  74. SafeFlag exit_thread;
  75. static _FORCE_INLINE_ void write_sample(WORD format_tag, int bits_per_sample, BYTE *buffer, int i, int32_t sample);
  76. static _FORCE_INLINE_ int32_t read_sample(WORD format_tag, int bits_per_sample, BYTE *buffer, int i);
  77. static void thread_func(void *p_udata);
  78. Error init_render_device(bool reinit = false);
  79. Error init_capture_device(bool reinit = false);
  80. Error finish_render_device();
  81. Error finish_capture_device();
  82. Error audio_device_init(AudioDeviceWASAPI *p_device, bool p_capture, bool reinit);
  83. Error audio_device_finish(AudioDeviceWASAPI *p_device);
  84. Array audio_device_get_list(bool p_capture);
  85. public:
  86. virtual const char *get_name() const {
  87. return "WASAPI";
  88. }
  89. virtual Error init();
  90. virtual void start();
  91. virtual int get_mix_rate() const;
  92. virtual SpeakerMode get_speaker_mode() const;
  93. virtual Array get_device_list();
  94. virtual String get_device();
  95. virtual void set_device(String device);
  96. virtual void lock();
  97. virtual void unlock();
  98. virtual void finish();
  99. virtual Error capture_start();
  100. virtual Error capture_stop();
  101. virtual Array capture_get_device_list();
  102. virtual void capture_set_device(const String &p_name);
  103. virtual String capture_get_device();
  104. AudioDriverWASAPI();
  105. };
  106. #endif // WASAPI_ENABLED
  107. #endif // AUDIO_DRIVER_WASAPI_H