audio_driver_web.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /**************************************************************************/
  2. /* audio_driver_web.cpp */
  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. #include "audio_driver_web.h"
  31. #include "core/config/project_settings.h"
  32. #include <emscripten.h>
  33. AudioDriverWeb::AudioContext AudioDriverWeb::audio_context;
  34. bool AudioDriverWeb::is_available() {
  35. return godot_audio_is_available() != 0;
  36. }
  37. void AudioDriverWeb::_state_change_callback(int p_state) {
  38. AudioDriverWeb::audio_context.state = p_state;
  39. }
  40. void AudioDriverWeb::_latency_update_callback(float p_latency) {
  41. AudioDriverWeb::audio_context.output_latency = p_latency;
  42. }
  43. void AudioDriverWeb::_audio_driver_process(int p_from, int p_samples) {
  44. int32_t *stream_buffer = reinterpret_cast<int32_t *>(output_rb);
  45. const int max_samples = memarr_len(output_rb);
  46. int write_pos = p_from;
  47. int to_write = p_samples;
  48. if (to_write == 0) {
  49. to_write = max_samples;
  50. }
  51. // High part
  52. if (write_pos + to_write > max_samples) {
  53. const int samples_high = max_samples - write_pos;
  54. audio_server_process(samples_high / channel_count, &stream_buffer[write_pos]);
  55. for (int i = write_pos; i < max_samples; i++) {
  56. output_rb[i] = float(stream_buffer[i] >> 16) / 32768.f;
  57. }
  58. to_write -= samples_high;
  59. write_pos = 0;
  60. }
  61. // Leftover
  62. audio_server_process(to_write / channel_count, &stream_buffer[write_pos]);
  63. for (int i = write_pos; i < write_pos + to_write; i++) {
  64. output_rb[i] = float(stream_buffer[i] >> 16) / 32768.f;
  65. }
  66. }
  67. void AudioDriverWeb::_audio_driver_capture(int p_from, int p_samples) {
  68. if (get_input_buffer().size() == 0) {
  69. return; // Input capture stopped.
  70. }
  71. const int max_samples = memarr_len(input_rb);
  72. int read_pos = p_from;
  73. int to_read = p_samples;
  74. if (to_read == 0) {
  75. to_read = max_samples;
  76. }
  77. // High part
  78. if (read_pos + to_read > max_samples) {
  79. const int samples_high = max_samples - read_pos;
  80. for (int i = read_pos; i < max_samples; i++) {
  81. input_buffer_write(int32_t(input_rb[i] * 32768.f) * (1U << 16));
  82. }
  83. to_read -= samples_high;
  84. read_pos = 0;
  85. }
  86. // Leftover
  87. for (int i = read_pos; i < read_pos + to_read; i++) {
  88. input_buffer_write(int32_t(input_rb[i] * 32768.f) * (1U << 16));
  89. }
  90. }
  91. Error AudioDriverWeb::init() {
  92. int latency = GLOBAL_GET("audio/driver/output_latency");
  93. if (!audio_context.inited) {
  94. audio_context.mix_rate = _get_configured_mix_rate();
  95. audio_context.channel_count = godot_audio_init(&audio_context.mix_rate, latency, &_state_change_callback, &_latency_update_callback);
  96. audio_context.inited = true;
  97. }
  98. mix_rate = audio_context.mix_rate;
  99. channel_count = audio_context.channel_count;
  100. buffer_length = closest_power_of_2((latency * mix_rate / 1000));
  101. Error err = create(buffer_length, channel_count);
  102. if (err != OK) {
  103. return err;
  104. }
  105. if (output_rb) {
  106. memdelete_arr(output_rb);
  107. }
  108. const size_t array_size = buffer_length * (size_t)channel_count;
  109. output_rb = memnew_arr(float, array_size);
  110. if (!output_rb) {
  111. return ERR_OUT_OF_MEMORY;
  112. }
  113. if (input_rb) {
  114. memdelete_arr(input_rb);
  115. }
  116. input_rb = memnew_arr(float, array_size);
  117. if (!input_rb) {
  118. return ERR_OUT_OF_MEMORY;
  119. }
  120. return OK;
  121. }
  122. void AudioDriverWeb::start() {
  123. start(output_rb, memarr_len(output_rb), input_rb, memarr_len(input_rb));
  124. }
  125. void AudioDriverWeb::resume() {
  126. if (audio_context.state == 0) { // 'suspended'
  127. godot_audio_resume();
  128. }
  129. }
  130. float AudioDriverWeb::get_latency() {
  131. return audio_context.output_latency + (float(buffer_length) / mix_rate);
  132. }
  133. int AudioDriverWeb::get_mix_rate() const {
  134. return mix_rate;
  135. }
  136. AudioDriver::SpeakerMode AudioDriverWeb::get_speaker_mode() const {
  137. return get_speaker_mode_by_total_channels(channel_count);
  138. }
  139. void AudioDriverWeb::finish() {
  140. finish_driver();
  141. if (output_rb) {
  142. memdelete_arr(output_rb);
  143. output_rb = nullptr;
  144. }
  145. if (input_rb) {
  146. memdelete_arr(input_rb);
  147. input_rb = nullptr;
  148. }
  149. }
  150. Error AudioDriverWeb::input_start() {
  151. lock();
  152. input_buffer_init(buffer_length);
  153. unlock();
  154. if (godot_audio_input_start()) {
  155. return FAILED;
  156. }
  157. return OK;
  158. }
  159. Error AudioDriverWeb::input_stop() {
  160. godot_audio_input_stop();
  161. lock();
  162. input_buffer.clear();
  163. unlock();
  164. return OK;
  165. }
  166. /// AudioWorkletNode implementation (threads)
  167. void AudioDriverWorklet::_audio_thread_func(void *p_data) {
  168. AudioDriverWorklet *driver = static_cast<AudioDriverWorklet *>(p_data);
  169. const int out_samples = memarr_len(driver->get_output_rb());
  170. const int in_samples = memarr_len(driver->get_input_rb());
  171. int wpos = 0;
  172. int to_write = out_samples;
  173. int rpos = 0;
  174. int to_read = 0;
  175. int32_t step = 0;
  176. while (!driver->quit) {
  177. if (to_read) {
  178. driver->lock();
  179. driver->_audio_driver_capture(rpos, to_read);
  180. godot_audio_worklet_state_add(driver->state, STATE_SAMPLES_IN, -to_read);
  181. driver->unlock();
  182. rpos += to_read;
  183. if (rpos >= in_samples) {
  184. rpos -= in_samples;
  185. }
  186. }
  187. if (to_write) {
  188. driver->lock();
  189. driver->_audio_driver_process(wpos, to_write);
  190. godot_audio_worklet_state_add(driver->state, STATE_SAMPLES_OUT, to_write);
  191. driver->unlock();
  192. wpos += to_write;
  193. if (wpos >= out_samples) {
  194. wpos -= out_samples;
  195. }
  196. }
  197. step = godot_audio_worklet_state_wait(driver->state, STATE_PROCESS, step, 1);
  198. to_write = out_samples - godot_audio_worklet_state_get(driver->state, STATE_SAMPLES_OUT);
  199. to_read = godot_audio_worklet_state_get(driver->state, STATE_SAMPLES_IN);
  200. }
  201. }
  202. Error AudioDriverWorklet::create(int &p_buffer_size, int p_channels) {
  203. if (!godot_audio_has_worklet()) {
  204. return ERR_UNAVAILABLE;
  205. }
  206. return (Error)godot_audio_worklet_create(p_channels);
  207. }
  208. void AudioDriverWorklet::start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) {
  209. godot_audio_worklet_start(p_in_buf, p_in_buf_size, p_out_buf, p_out_buf_size, state);
  210. thread.start(_audio_thread_func, this);
  211. }
  212. void AudioDriverWorklet::lock() {
  213. mutex.lock();
  214. }
  215. void AudioDriverWorklet::unlock() {
  216. mutex.unlock();
  217. }
  218. void AudioDriverWorklet::finish_driver() {
  219. quit = true; // Ask thread to quit.
  220. thread.wait_to_finish();
  221. }