2
0

audio_driver_javascript.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*************************************************************************/
  2. /* audio_driver_javascript.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_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. public:
  38. class AudioNode {
  39. public:
  40. virtual int create(int p_buffer_size, int p_output_channels) = 0;
  41. virtual void start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) = 0;
  42. virtual void finish() {}
  43. virtual void lock() {}
  44. virtual void unlock() {}
  45. virtual ~AudioNode() {}
  46. };
  47. class WorkletNode : public AudioNode {
  48. private:
  49. enum {
  50. STATE_LOCK,
  51. STATE_PROCESS,
  52. STATE_SAMPLES_IN,
  53. STATE_SAMPLES_OUT,
  54. STATE_MAX,
  55. };
  56. Mutex mutex;
  57. Thread thread;
  58. bool quit = false;
  59. int32_t state[STATE_MAX] = { 0 };
  60. static void _audio_thread_func(void *p_data);
  61. public:
  62. int create(int p_buffer_size, int p_output_channels) override;
  63. void start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) override;
  64. void finish() override;
  65. void lock() override;
  66. void unlock() override;
  67. };
  68. class ScriptProcessorNode : public AudioNode {
  69. private:
  70. static void _process_callback();
  71. public:
  72. int create(int p_buffer_samples, int p_channels) override;
  73. void start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) override;
  74. };
  75. private:
  76. AudioNode *node = nullptr;
  77. float *output_rb = nullptr;
  78. float *input_rb = nullptr;
  79. int buffer_length = 0;
  80. int mix_rate = 0;
  81. int channel_count = 0;
  82. int state = 0;
  83. float output_latency = 0.0;
  84. static void _state_change_callback(int p_state);
  85. static void _latency_update_callback(float p_latency);
  86. protected:
  87. void _audio_driver_process(int p_from = 0, int p_samples = 0);
  88. void _audio_driver_capture(int p_from = 0, int p_samples = 0);
  89. public:
  90. static bool is_available();
  91. static AudioDriverJavaScript *singleton;
  92. virtual const char *get_name() const;
  93. virtual Error init();
  94. virtual void start();
  95. void resume();
  96. virtual float get_latency();
  97. virtual int get_mix_rate() const;
  98. virtual SpeakerMode get_speaker_mode() const;
  99. virtual void lock();
  100. virtual void unlock();
  101. virtual void finish();
  102. virtual Error capture_start();
  103. virtual Error capture_stop();
  104. AudioDriverJavaScript();
  105. };
  106. #endif