audio_driver_web.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 "core/os/mutex.h"
  34. #include "core/os/thread.h"
  35. #include "servers/audio_server.h"
  36. class AudioDriverWeb : 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 AudioDriverWeb *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 int get_mix_rate() const override;
  68. virtual SpeakerMode get_speaker_mode() const override;
  69. virtual float get_latency() override;
  70. virtual Error input_start() override;
  71. virtual Error input_stop() override;
  72. static void resume();
  73. AudioDriverWeb() {}
  74. };
  75. class AudioDriverWorklet : public AudioDriverWeb {
  76. private:
  77. enum {
  78. STATE_LOCK,
  79. STATE_PROCESS,
  80. STATE_SAMPLES_IN,
  81. STATE_SAMPLES_OUT,
  82. STATE_MAX,
  83. };
  84. Mutex mutex;
  85. Thread thread;
  86. bool quit = false;
  87. int32_t state[STATE_MAX] = { 0 };
  88. static void _audio_thread_func(void *p_data);
  89. protected:
  90. virtual Error create(int &p_buffer_size, int p_output_channels) override;
  91. virtual void start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) override;
  92. virtual void finish_driver() override;
  93. public:
  94. virtual const char *get_name() const override {
  95. return "AudioWorklet";
  96. }
  97. virtual void lock() override;
  98. virtual void unlock() override;
  99. };
  100. #endif // AUDIO_DRIVER_WEB_H