audio_driver_web.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. static AudioDriverWeb *singleton;
  55. protected:
  56. void _audio_driver_process(int p_from = 0, int p_samples = 0);
  57. void _audio_driver_capture(int p_from = 0, int p_samples = 0);
  58. float *get_output_rb() const { return output_rb; }
  59. float *get_input_rb() const { return input_rb; }
  60. virtual Error create(int &p_buffer_samples, int p_channels) = 0;
  61. virtual void start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) = 0;
  62. virtual void finish_driver() {}
  63. public:
  64. static bool is_available();
  65. virtual Error init() final;
  66. virtual void start() final;
  67. virtual void finish() final;
  68. virtual int get_mix_rate() const override;
  69. virtual SpeakerMode get_speaker_mode() const override;
  70. virtual float get_latency() override;
  71. virtual Error input_start() override;
  72. virtual Error input_stop() override;
  73. static void resume();
  74. AudioDriverWeb() {}
  75. };
  76. class AudioDriverWorklet : public AudioDriverWeb {
  77. private:
  78. enum {
  79. STATE_LOCK,
  80. STATE_PROCESS,
  81. STATE_SAMPLES_IN,
  82. STATE_SAMPLES_OUT,
  83. STATE_MAX,
  84. };
  85. Mutex mutex;
  86. Thread thread;
  87. bool quit = false;
  88. int32_t state[STATE_MAX] = { 0 };
  89. static void _audio_thread_func(void *p_data);
  90. protected:
  91. virtual Error create(int &p_buffer_size, int p_output_channels) override;
  92. virtual void start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) override;
  93. virtual void finish_driver() override;
  94. public:
  95. virtual const char *get_name() const override {
  96. return "AudioWorklet";
  97. }
  98. virtual void lock() override;
  99. virtual void unlock() override;
  100. };
  101. #endif // AUDIO_DRIVER_WEB_H