audio_driver_javascript.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**************************************************************************/
  2. /* audio_driver_javascript.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_JAVASCRIPT_H
  31. #define AUDIO_DRIVER_JAVASCRIPT_H
  32. #include "core/os/mutex.h"
  33. #include "core/os/thread.h"
  34. #include "servers/audio_server.h"
  35. #include "godot_audio.h"
  36. class AudioDriverJavaScript : public AudioDriver {
  37. private:
  38. struct AudioContext {
  39. bool inited = false;
  40. float output_latency = 0.0;
  41. int state = -1;
  42. int channel_count = 0;
  43. int mix_rate = 0;
  44. };
  45. static AudioContext audio_context;
  46. float *output_rb = nullptr;
  47. float *input_rb = nullptr;
  48. int buffer_length = 0;
  49. int mix_rate = 0;
  50. int channel_count = 0;
  51. static void _state_change_callback(int p_state);
  52. static void _latency_update_callback(float p_latency);
  53. static AudioDriverJavaScript *singleton;
  54. protected:
  55. void _audio_driver_process(int p_from = 0, int p_samples = 0);
  56. void _audio_driver_capture(int p_from = 0, int p_samples = 0);
  57. float *get_output_rb() const { return output_rb; }
  58. float *get_input_rb() const { return input_rb; }
  59. virtual Error create(int &p_buffer_samples, int p_channels) = 0;
  60. virtual void start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) = 0;
  61. virtual void finish_driver() {}
  62. public:
  63. static bool is_available();
  64. virtual Error init() final;
  65. virtual void start() final;
  66. virtual void finish() final;
  67. virtual float get_latency() override;
  68. virtual int get_mix_rate() const override;
  69. virtual SpeakerMode get_speaker_mode() const override;
  70. virtual Error capture_start() override;
  71. virtual Error capture_stop() override;
  72. static void resume();
  73. AudioDriverJavaScript() {}
  74. };
  75. #ifdef NO_THREADS
  76. class AudioDriverScriptProcessor : public AudioDriverJavaScript {
  77. private:
  78. static void _process_callback();
  79. static AudioDriverScriptProcessor *singleton;
  80. protected:
  81. Error create(int &p_buffer_samples, int p_channels) override;
  82. void start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) override;
  83. public:
  84. virtual const char *get_name() const override { return "ScriptProcessor"; }
  85. virtual void lock() override {}
  86. virtual void unlock() override {}
  87. AudioDriverScriptProcessor() { singleton = this; }
  88. };
  89. class AudioDriverWorklet : public AudioDriverJavaScript {
  90. private:
  91. static void _process_callback(int p_pos, int p_samples);
  92. static void _capture_callback(int p_pos, int p_samples);
  93. static AudioDriverWorklet *singleton;
  94. protected:
  95. virtual Error create(int &p_buffer_size, int p_output_channels) override;
  96. virtual void start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) override;
  97. public:
  98. virtual const char *get_name() const override { return "AudioWorklet"; }
  99. virtual void lock() override {}
  100. virtual void unlock() override {}
  101. AudioDriverWorklet() { singleton = this; }
  102. };
  103. #else
  104. class AudioDriverWorklet : public AudioDriverJavaScript {
  105. private:
  106. enum {
  107. STATE_LOCK,
  108. STATE_PROCESS,
  109. STATE_SAMPLES_IN,
  110. STATE_SAMPLES_OUT,
  111. STATE_MAX,
  112. };
  113. Mutex mutex;
  114. Thread thread;
  115. bool quit = false;
  116. int32_t state[STATE_MAX] = { 0 };
  117. static void _audio_thread_func(void *p_data);
  118. protected:
  119. virtual Error create(int &p_buffer_size, int p_output_channels) override;
  120. virtual void start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) override;
  121. virtual void finish_driver() override;
  122. public:
  123. virtual const char *get_name() const override { return "AudioWorklet"; }
  124. void lock() override;
  125. void unlock() override;
  126. };
  127. #endif
  128. #endif // AUDIO_DRIVER_JAVASCRIPT_H