audio_driver_javascript.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. #include "godot_audio.h"
  34. AudioDriverJavaScript *AudioDriverJavaScript::singleton = NULL;
  35. bool AudioDriverJavaScript::is_available() {
  36. return godot_audio_is_available() != 0;
  37. }
  38. const char *AudioDriverJavaScript::get_name() const {
  39. return "JavaScript";
  40. }
  41. #ifndef NO_THREADS
  42. void AudioDriverJavaScript::_audio_thread_func(void *p_data) {
  43. AudioDriverJavaScript *obj = static_cast<AudioDriverJavaScript *>(p_data);
  44. while (!obj->quit) {
  45. obj->lock();
  46. if (!obj->needs_process) {
  47. obj->unlock();
  48. OS::get_singleton()->delay_usec(1000); // Give the browser some slack.
  49. continue;
  50. }
  51. obj->_js_driver_process();
  52. obj->needs_process = false;
  53. obj->unlock();
  54. }
  55. }
  56. #endif
  57. extern "C" EMSCRIPTEN_KEEPALIVE void audio_driver_process_start() {
  58. #ifndef NO_THREADS
  59. AudioDriverJavaScript::singleton->lock();
  60. #else
  61. AudioDriverJavaScript::singleton->_js_driver_process();
  62. #endif
  63. }
  64. extern "C" EMSCRIPTEN_KEEPALIVE void audio_driver_process_end() {
  65. #ifndef NO_THREADS
  66. AudioDriverJavaScript::singleton->needs_process = true;
  67. AudioDriverJavaScript::singleton->unlock();
  68. #endif
  69. }
  70. extern "C" EMSCRIPTEN_KEEPALIVE void audio_driver_process_capture(float sample) {
  71. AudioDriverJavaScript::singleton->process_capture(sample);
  72. }
  73. void AudioDriverJavaScript::_js_driver_process() {
  74. int sample_count = memarr_len(internal_buffer) / channel_count;
  75. int32_t *stream_buffer = reinterpret_cast<int32_t *>(internal_buffer);
  76. audio_server_process(sample_count, stream_buffer);
  77. for (int i = 0; i < sample_count * channel_count; i++) {
  78. internal_buffer[i] = float(stream_buffer[i] >> 16) / 32768.f;
  79. }
  80. }
  81. void AudioDriverJavaScript::process_capture(float sample) {
  82. int32_t sample32 = int32_t(sample * 32768.f) * (1U << 16);
  83. input_buffer_write(sample32);
  84. }
  85. Error AudioDriverJavaScript::init() {
  86. mix_rate = GLOBAL_GET("audio/mix_rate");
  87. int latency = GLOBAL_GET("audio/output_latency");
  88. channel_count = godot_audio_init(mix_rate, latency);
  89. buffer_length = closest_power_of_2((latency * mix_rate / 1000) * channel_count);
  90. buffer_length = godot_audio_create_processor(buffer_length, channel_count);
  91. if (!buffer_length) {
  92. return FAILED;
  93. }
  94. if (!internal_buffer || (int)memarr_len(internal_buffer) != buffer_length * channel_count) {
  95. if (internal_buffer)
  96. memdelete_arr(internal_buffer);
  97. internal_buffer = memnew_arr(float, buffer_length *channel_count);
  98. }
  99. if (!internal_buffer) {
  100. return ERR_OUT_OF_MEMORY;
  101. }
  102. return OK;
  103. }
  104. void AudioDriverJavaScript::start() {
  105. #ifndef NO_THREADS
  106. mutex = Mutex::create();
  107. thread = Thread::create(_audio_thread_func, this);
  108. #endif
  109. godot_audio_start(internal_buffer);
  110. }
  111. void AudioDriverJavaScript::resume() {
  112. godot_audio_resume();
  113. }
  114. float AudioDriverJavaScript::get_latency() {
  115. return godot_audio_get_latency();
  116. }
  117. int AudioDriverJavaScript::get_mix_rate() const {
  118. return mix_rate;
  119. }
  120. AudioDriver::SpeakerMode AudioDriverJavaScript::get_speaker_mode() const {
  121. return get_speaker_mode_by_total_channels(channel_count);
  122. }
  123. void AudioDriverJavaScript::lock() {
  124. #ifndef NO_THREADS
  125. if (mutex) {
  126. mutex->lock();
  127. }
  128. #endif
  129. }
  130. void AudioDriverJavaScript::unlock() {
  131. #ifndef NO_THREADS
  132. if (mutex) {
  133. mutex->unlock();
  134. }
  135. #endif
  136. }
  137. void AudioDriverJavaScript::finish_async() {
  138. #ifndef NO_THREADS
  139. quit = true; // Ask thread to quit.
  140. #endif
  141. godot_audio_finish_async();
  142. }
  143. void AudioDriverJavaScript::finish() {
  144. #ifndef NO_THREADS
  145. Thread::wait_to_finish(thread);
  146. memdelete(thread);
  147. thread = NULL;
  148. memdelete(mutex);
  149. mutex = NULL;
  150. #endif
  151. if (internal_buffer) {
  152. memdelete_arr(internal_buffer);
  153. internal_buffer = NULL;
  154. }
  155. }
  156. Error AudioDriverJavaScript::capture_start() {
  157. input_buffer_init(buffer_length);
  158. godot_audio_capture_start();
  159. return OK;
  160. }
  161. Error AudioDriverJavaScript::capture_stop() {
  162. godot_audio_capture_stop();
  163. input_buffer.clear();
  164. return OK;
  165. }
  166. AudioDriverJavaScript::AudioDriverJavaScript() {
  167. internal_buffer = NULL;
  168. buffer_length = 0;
  169. mix_rate = 0;
  170. channel_count = 0;
  171. #ifndef NO_THREADS
  172. mutex = NULL;
  173. thread = NULL;
  174. quit = false;
  175. needs_process = true;
  176. #endif
  177. singleton = this;
  178. }