audio_driver_javascript.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*************************************************************************/
  2. /* audio_driver_javascript.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. #include "audio_driver_javascript.h"
  31. #include "core/project_settings.h"
  32. #include <emscripten.h>
  33. AudioDriverJavaScript::AudioContext AudioDriverJavaScript::audio_context;
  34. bool AudioDriverJavaScript::is_available() {
  35. return godot_audio_is_available() != 0;
  36. }
  37. void AudioDriverJavaScript::_state_change_callback(int p_state) {
  38. AudioDriverJavaScript::audio_context.state = p_state;
  39. }
  40. void AudioDriverJavaScript::_latency_update_callback(float p_latency) {
  41. AudioDriverJavaScript::audio_context.output_latency = p_latency;
  42. }
  43. void AudioDriverJavaScript::_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 AudioDriverJavaScript::_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 AudioDriverJavaScript::init() {
  92. int latency = GLOBAL_GET("audio/output_latency");
  93. if (!audio_context.inited) {
  94. audio_context.mix_rate = GLOBAL_GET("audio/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. output_rb = memnew_arr(float, buffer_length *channel_count);
  109. if (!output_rb) {
  110. return ERR_OUT_OF_MEMORY;
  111. }
  112. if (input_rb) {
  113. memdelete_arr(input_rb);
  114. }
  115. input_rb = memnew_arr(float, buffer_length *channel_count);
  116. if (!input_rb) {
  117. return ERR_OUT_OF_MEMORY;
  118. }
  119. return OK;
  120. }
  121. void AudioDriverJavaScript::start() {
  122. start(output_rb, memarr_len(output_rb), input_rb, memarr_len(input_rb));
  123. }
  124. void AudioDriverJavaScript::resume() {
  125. if (audio_context.state == 0) { // 'suspended'
  126. godot_audio_resume();
  127. }
  128. }
  129. float AudioDriverJavaScript::get_latency() {
  130. return audio_context.output_latency + (float(buffer_length) / mix_rate);
  131. }
  132. int AudioDriverJavaScript::get_mix_rate() const {
  133. return mix_rate;
  134. }
  135. AudioDriver::SpeakerMode AudioDriverJavaScript::get_speaker_mode() const {
  136. return get_speaker_mode_by_total_channels(channel_count);
  137. }
  138. void AudioDriverJavaScript::finish() {
  139. finish_driver();
  140. if (output_rb) {
  141. memdelete_arr(output_rb);
  142. output_rb = nullptr;
  143. }
  144. if (input_rb) {
  145. memdelete_arr(input_rb);
  146. input_rb = nullptr;
  147. }
  148. }
  149. Error AudioDriverJavaScript::capture_start() {
  150. lock();
  151. input_buffer_init(buffer_length);
  152. unlock();
  153. if (godot_audio_capture_start()) {
  154. return FAILED;
  155. }
  156. return OK;
  157. }
  158. Error AudioDriverJavaScript::capture_stop() {
  159. godot_audio_capture_stop();
  160. lock();
  161. input_buffer.clear();
  162. unlock();
  163. return OK;
  164. }
  165. #ifdef NO_THREADS
  166. /// ScriptProcessorNode implementation
  167. AudioDriverScriptProcessor *AudioDriverScriptProcessor::singleton = nullptr;
  168. void AudioDriverScriptProcessor::_process_callback() {
  169. AudioDriverScriptProcessor::singleton->_audio_driver_capture();
  170. AudioDriverScriptProcessor::singleton->_audio_driver_process();
  171. }
  172. Error AudioDriverScriptProcessor::create(int &p_buffer_samples, int p_channels) {
  173. if (!godot_audio_has_script_processor()) {
  174. return ERR_UNAVAILABLE;
  175. }
  176. return (Error)godot_audio_script_create(&p_buffer_samples, p_channels);
  177. }
  178. void AudioDriverScriptProcessor::start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) {
  179. godot_audio_script_start(p_in_buf, p_in_buf_size, p_out_buf, p_out_buf_size, &_process_callback);
  180. }
  181. /// AudioWorkletNode implementation (no threads)
  182. AudioDriverWorklet *AudioDriverWorklet::singleton = nullptr;
  183. Error AudioDriverWorklet::create(int &p_buffer_size, int p_channels) {
  184. if (!godot_audio_has_worklet()) {
  185. return ERR_UNAVAILABLE;
  186. }
  187. return (Error)godot_audio_worklet_create(p_channels);
  188. }
  189. void AudioDriverWorklet::start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) {
  190. _audio_driver_process();
  191. godot_audio_worklet_start_no_threads(p_out_buf, p_out_buf_size, &_process_callback, p_in_buf, p_in_buf_size, &_capture_callback);
  192. }
  193. void AudioDriverWorklet::_process_callback(int p_pos, int p_samples) {
  194. AudioDriverWorklet *driver = AudioDriverWorklet::singleton;
  195. driver->_audio_driver_process(p_pos, p_samples);
  196. }
  197. void AudioDriverWorklet::_capture_callback(int p_pos, int p_samples) {
  198. AudioDriverWorklet *driver = AudioDriverWorklet::singleton;
  199. driver->_audio_driver_capture(p_pos, p_samples);
  200. }
  201. #else
  202. /// AudioWorkletNode implementation (threads)
  203. void AudioDriverWorklet::_audio_thread_func(void *p_data) {
  204. AudioDriverWorklet *driver = static_cast<AudioDriverWorklet *>(p_data);
  205. const int out_samples = memarr_len(driver->get_output_rb());
  206. const int in_samples = memarr_len(driver->get_input_rb());
  207. int wpos = 0;
  208. int to_write = out_samples;
  209. int rpos = 0;
  210. int to_read = 0;
  211. int32_t step = 0;
  212. while (!driver->quit) {
  213. if (to_read) {
  214. driver->lock();
  215. driver->_audio_driver_capture(rpos, to_read);
  216. godot_audio_worklet_state_add(driver->state, STATE_SAMPLES_IN, -to_read);
  217. driver->unlock();
  218. rpos += to_read;
  219. if (rpos >= in_samples) {
  220. rpos -= in_samples;
  221. }
  222. }
  223. if (to_write) {
  224. driver->lock();
  225. driver->_audio_driver_process(wpos, to_write);
  226. godot_audio_worklet_state_add(driver->state, STATE_SAMPLES_OUT, to_write);
  227. driver->unlock();
  228. wpos += to_write;
  229. if (wpos >= out_samples) {
  230. wpos -= out_samples;
  231. }
  232. }
  233. step = godot_audio_worklet_state_wait(driver->state, STATE_PROCESS, step, 1);
  234. to_write = out_samples - godot_audio_worklet_state_get(driver->state, STATE_SAMPLES_OUT);
  235. to_read = godot_audio_worklet_state_get(driver->state, STATE_SAMPLES_IN);
  236. }
  237. }
  238. Error AudioDriverWorklet::create(int &p_buffer_size, int p_channels) {
  239. if (!godot_audio_has_worklet()) {
  240. return ERR_UNAVAILABLE;
  241. }
  242. return (Error)godot_audio_worklet_create(p_channels);
  243. }
  244. void AudioDriverWorklet::start(float *p_out_buf, int p_out_buf_size, float *p_in_buf, int p_in_buf_size) {
  245. godot_audio_worklet_start(p_in_buf, p_in_buf_size, p_out_buf, p_out_buf_size, state);
  246. thread.start(_audio_thread_func, this);
  247. }
  248. void AudioDriverWorklet::lock() {
  249. mutex.lock();
  250. }
  251. void AudioDriverWorklet::unlock() {
  252. mutex.unlock();
  253. }
  254. void AudioDriverWorklet::finish_driver() {
  255. quit = true; // Ask thread to quit.
  256. thread.wait_to_finish();
  257. }
  258. #endif