audio_driver_wasapi.h 4.4 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) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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_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 "servers/audio_server.h"
  36. #include <audioclient.h>
  37. #include <mmdeviceapi.h>
  38. #include <windows.h>
  39. class AudioDriverWASAPI : public AudioDriver {
  40. class AudioDeviceWASAPI {
  41. public:
  42. IAudioClient *audio_client;
  43. IAudioRenderClient *render_client;
  44. IAudioCaptureClient *capture_client;
  45. bool active;
  46. WORD format_tag;
  47. WORD bits_per_sample;
  48. unsigned int channels;
  49. unsigned int frame_size;
  50. String device_name;
  51. String new_device;
  52. AudioDeviceWASAPI() :
  53. audio_client(NULL),
  54. render_client(NULL),
  55. capture_client(NULL),
  56. active(false),
  57. format_tag(0),
  58. bits_per_sample(0),
  59. channels(0),
  60. frame_size(0),
  61. device_name("Default"),
  62. new_device("Default") {
  63. }
  64. };
  65. AudioDeviceWASAPI audio_input;
  66. AudioDeviceWASAPI audio_output;
  67. Mutex *mutex;
  68. Thread *thread;
  69. Vector<int32_t> samples_in;
  70. unsigned int channels;
  71. int mix_rate;
  72. int buffer_frames;
  73. bool thread_exited;
  74. mutable bool 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 // AUDIO_DRIVER_WASAPI_H
  107. #endif