audio_driver_web.cpp 9.7 KB

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