audio_driver_web.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**************************************************************************/
  2. /* audio_driver_web.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_WEB_H
  31. #define AUDIO_DRIVER_WEB_H
  32. #include "godot_audio.h"
  33. #include "godot_js.h"
  34. #include "core/os/mutex.h"
  35. #include "core/os/thread.h"
  36. #include "servers/audio_server.h"
  37. class AudioDriverWeb : public AudioDriver {
  38. private:
  39. struct AudioContext {
  40. bool inited = false;
  41. float output_latency = 0.0;
  42. int state = -1;
  43. int channel_count = 0;
  44. int mix_rate = 0;
  45. };
  46. static AudioContext audio_context;
  47. float *output_rb = nullptr;
  48. float *input_rb = nullptr;
  49. int buffer_length = 0;
  50. int mix_rate = 0;
  51. int channel_count = 0;
  52. WASM_EXPORT static void _state_change_callback(int p_state);
  53. WASM_EXPORT static void _latency_update_callback(float p_latency);
  54. WASM_EXPORT static void _sample_playback_finished_callback(const char *p_playback_object_id);
  55. static AudioDriverWeb *singleton;
  56. protected:
  57. void _audio_driver_process(int p_from = 0, int p_samples = 0);
  58. void _audio_driver_capture(int p_from = 0, int p_samples = 0);
  59. float *get_output_rb() const { return output_rb; }
  60. float *get_input_rb() const { return input_rb; }
  61. virtual Error create(int &p_buffer_samples, int p_channels) = 0;
  62. virtual void start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) = 0;
  63. virtual void finish_driver() {}
  64. public:
  65. static bool is_available();
  66. virtual Error init() final;
  67. virtual void start() final;
  68. virtual void finish() final;
  69. virtual int get_mix_rate() const override;
  70. virtual SpeakerMode get_speaker_mode() const override;
  71. virtual float get_latency() override;
  72. virtual Error input_start() override;
  73. virtual Error input_stop() override;
  74. static void resume();
  75. // Samples.
  76. virtual bool is_stream_registered_as_sample(const Ref<AudioStream> &p_stream) const override;
  77. virtual void register_sample(const Ref<AudioSample> &p_sample) override;
  78. virtual void unregister_sample(const Ref<AudioSample> &p_sample) override;
  79. virtual void start_sample_playback(const Ref<AudioSamplePlayback> &p_playback) override;
  80. virtual void stop_sample_playback(const Ref<AudioSamplePlayback> &p_playback) override;
  81. virtual void set_sample_playback_pause(const Ref<AudioSamplePlayback> &p_playback, bool p_paused) override;
  82. virtual bool is_sample_playback_active(const Ref<AudioSamplePlayback> &p_playback) override;
  83. virtual double get_sample_playback_position(const Ref<AudioSamplePlayback> &p_playback) override;
  84. virtual void update_sample_playback_pitch_scale(const Ref<AudioSamplePlayback> &p_playback, float p_pitch_scale = 0.0f) override;
  85. virtual void set_sample_playback_bus_volumes_linear(const Ref<AudioSamplePlayback> &p_playback, const HashMap<StringName, Vector<AudioFrame>> &p_bus_volumes) override;
  86. virtual void set_sample_bus_count(int p_count) override;
  87. virtual void remove_sample_bus(int p_index) override;
  88. virtual void add_sample_bus(int p_at_pos = -1) override;
  89. virtual void move_sample_bus(int p_bus, int p_to_pos) override;
  90. virtual void set_sample_bus_send(int p_bus, const StringName &p_send) override;
  91. virtual void set_sample_bus_volume_db(int p_bus, float p_volume_db) override;
  92. virtual void set_sample_bus_solo(int p_bus, bool p_enable) override;
  93. virtual void set_sample_bus_mute(int p_bus, bool p_enable) override;
  94. AudioDriverWeb() {}
  95. };
  96. #ifdef THREADS_ENABLED
  97. class AudioDriverWorklet : public AudioDriverWeb {
  98. private:
  99. enum {
  100. STATE_LOCK,
  101. STATE_PROCESS,
  102. STATE_SAMPLES_IN,
  103. STATE_SAMPLES_OUT,
  104. STATE_MAX,
  105. };
  106. Mutex mutex;
  107. Thread thread;
  108. bool quit = false;
  109. int32_t state[STATE_MAX] = { 0 };
  110. static void _audio_thread_func(void *p_data);
  111. protected:
  112. virtual Error create(int &p_buffer_size, int p_output_channels) override;
  113. virtual void start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) override;
  114. virtual void finish_driver() override;
  115. public:
  116. virtual const char *get_name() const override {
  117. return "AudioWorklet";
  118. }
  119. virtual void lock() override;
  120. virtual void unlock() override;
  121. };
  122. #else
  123. class AudioDriverWorklet : public AudioDriverWeb {
  124. private:
  125. static void _process_callback(int p_pos, int p_samples);
  126. static void _capture_callback(int p_pos, int p_samples);
  127. static AudioDriverWorklet *singleton;
  128. protected:
  129. virtual Error create(int &p_buffer_size, int p_output_channels) override;
  130. virtual void start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) override;
  131. public:
  132. virtual const char *get_name() const override {
  133. return "AudioWorklet";
  134. }
  135. virtual void lock() override {}
  136. virtual void unlock() override {}
  137. static AudioDriverWorklet *get_singleton() { return singleton; }
  138. AudioDriverWorklet() { singleton = this; }
  139. };
  140. class AudioDriverScriptProcessor : public AudioDriverWeb {
  141. private:
  142. static void _process_callback();
  143. static AudioDriverScriptProcessor *singleton;
  144. protected:
  145. virtual Error create(int &p_buffer_size, int p_output_channels) override;
  146. virtual void start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) override;
  147. virtual void finish_driver() override;
  148. public:
  149. virtual const char *get_name() const override { return "ScriptProcessor"; }
  150. virtual void lock() override {}
  151. virtual void unlock() override {}
  152. static AudioDriverScriptProcessor *get_singleton() { return singleton; }
  153. AudioDriverScriptProcessor() { singleton = this; }
  154. };
  155. #endif // THREADS_ENABLED
  156. #endif // AUDIO_DRIVER_WEB_H