audio_driver_javascript.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. AudioDriverJavaScript *AudioDriverJavaScript::singleton = NULL;
  34. bool AudioDriverJavaScript::is_available() {
  35. return EM_ASM_INT({
  36. if (!(window.AudioContext || window.webkitAudioContext)) {
  37. return 0;
  38. }
  39. return 1;
  40. }) != 0;
  41. }
  42. const char *AudioDriverJavaScript::get_name() const {
  43. return "JavaScript";
  44. }
  45. extern "C" EMSCRIPTEN_KEEPALIVE void audio_driver_js_mix() {
  46. AudioDriverJavaScript::singleton->mix_to_js();
  47. }
  48. extern "C" EMSCRIPTEN_KEEPALIVE void audio_driver_process_capture(float sample) {
  49. AudioDriverJavaScript::singleton->process_capture(sample);
  50. }
  51. void AudioDriverJavaScript::mix_to_js() {
  52. int channel_count = get_total_channels_by_speaker_mode(get_speaker_mode());
  53. int sample_count = memarr_len(internal_buffer) / channel_count;
  54. int32_t *stream_buffer = reinterpret_cast<int32_t *>(internal_buffer);
  55. audio_server_process(sample_count, stream_buffer);
  56. for (int i = 0; i < sample_count * channel_count; i++) {
  57. internal_buffer[i] = float(stream_buffer[i] >> 16) / 32768.f;
  58. }
  59. }
  60. void AudioDriverJavaScript::process_capture(float sample) {
  61. int32_t sample32 = int32_t(sample * 32768.f) * (1U << 16);
  62. input_buffer_write(sample32);
  63. }
  64. Error AudioDriverJavaScript::init() {
  65. int mix_rate = GLOBAL_GET("audio/mix_rate");
  66. int latency = GLOBAL_GET("audio/output_latency");
  67. /* clang-format off */
  68. _driver_id = EM_ASM_INT({
  69. const MIX_RATE = $0;
  70. const LATENCY = $1 / 1000;
  71. return Module.IDHandler.add({
  72. 'context': new (window.AudioContext || window.webkitAudioContext)({ sampleRate: MIX_RATE, latencyHint: LATENCY}),
  73. 'input': null,
  74. 'stream': null,
  75. 'script': null
  76. });
  77. }, mix_rate, latency);
  78. /* clang-format on */
  79. int channel_count = get_total_channels_by_speaker_mode(get_speaker_mode());
  80. buffer_length = closest_power_of_2((latency * mix_rate / 1000) * channel_count);
  81. /* clang-format off */
  82. buffer_length = EM_ASM_INT({
  83. var ref = Module.IDHandler.get($0);
  84. const ctx = ref['context'];
  85. const BUFFER_LENGTH = $1;
  86. const CHANNEL_COUNT = $2;
  87. var script = ctx.createScriptProcessor(BUFFER_LENGTH, 2, CHANNEL_COUNT);
  88. script.connect(ctx.destination);
  89. ref['script'] = script;
  90. return script.bufferSize;
  91. }, _driver_id, buffer_length, channel_count);
  92. /* clang-format on */
  93. if (!buffer_length) {
  94. return FAILED;
  95. }
  96. if (!internal_buffer || (int)memarr_len(internal_buffer) != buffer_length * channel_count) {
  97. if (internal_buffer)
  98. memdelete_arr(internal_buffer);
  99. internal_buffer = memnew_arr(float, buffer_length *channel_count);
  100. }
  101. return internal_buffer ? OK : ERR_OUT_OF_MEMORY;
  102. }
  103. void AudioDriverJavaScript::start() {
  104. /* clang-format off */
  105. EM_ASM({
  106. const ref = Module.IDHandler.get($0);
  107. var INTERNAL_BUFFER_PTR = $1;
  108. var audioDriverMixFunction = cwrap('audio_driver_js_mix');
  109. var audioDriverProcessCapture = cwrap('audio_driver_process_capture', null, ['number']);
  110. ref['script'].onaudioprocess = function(audioProcessingEvent) {
  111. audioDriverMixFunction();
  112. var input = audioProcessingEvent.inputBuffer;
  113. var output = audioProcessingEvent.outputBuffer;
  114. var internalBuffer = HEAPF32.subarray(
  115. INTERNAL_BUFFER_PTR / HEAPF32.BYTES_PER_ELEMENT,
  116. INTERNAL_BUFFER_PTR / HEAPF32.BYTES_PER_ELEMENT + output.length * output.numberOfChannels);
  117. for (var channel = 0; channel < output.numberOfChannels; channel++) {
  118. var outputData = output.getChannelData(channel);
  119. // Loop through samples.
  120. for (var sample = 0; sample < outputData.length; sample++) {
  121. outputData[sample] = internalBuffer[sample * output.numberOfChannels + channel];
  122. }
  123. }
  124. if (ref['input']) {
  125. var inputDataL = input.getChannelData(0);
  126. var inputDataR = input.getChannelData(1);
  127. for (var i = 0; i < inputDataL.length; i++) {
  128. audioDriverProcessCapture(inputDataL[i]);
  129. audioDriverProcessCapture(inputDataR[i]);
  130. }
  131. }
  132. };
  133. }, _driver_id, internal_buffer);
  134. /* clang-format on */
  135. }
  136. void AudioDriverJavaScript::resume() {
  137. /* clang-format off */
  138. EM_ASM({
  139. const ref = Module.IDHandler.get($0);
  140. if (ref && ref['context'] && ref['context'].resume)
  141. ref['context'].resume();
  142. }, _driver_id);
  143. /* clang-format on */
  144. }
  145. float AudioDriverJavaScript::get_latency() {
  146. /* clang-format off */
  147. return EM_ASM_DOUBLE({
  148. const ref = Module.IDHandler.get($0);
  149. var latency = 0;
  150. if (ref && ref['context']) {
  151. const ctx = ref['context'];
  152. if (ctx.baseLatency) {
  153. latency += ctx.baseLatency;
  154. }
  155. if (ctx.outputLatency) {
  156. latency += ctx.outputLatency;
  157. }
  158. }
  159. return latency;
  160. }, _driver_id);
  161. /* clang-format on */
  162. }
  163. int AudioDriverJavaScript::get_mix_rate() const {
  164. /* clang-format off */
  165. return EM_ASM_INT({
  166. const ref = Module.IDHandler.get($0);
  167. return ref && ref['context'] ? ref['context'].sampleRate : 0;
  168. }, _driver_id);
  169. /* clang-format on */
  170. }
  171. AudioDriver::SpeakerMode AudioDriverJavaScript::get_speaker_mode() const {
  172. /* clang-format off */
  173. return get_speaker_mode_by_total_channels(EM_ASM_INT({
  174. const ref = Module.IDHandler.get($0);
  175. return ref && ref['context'] ? ref['context'].destination.channelCount : 0;
  176. }, _driver_id));
  177. /* clang-format on */
  178. }
  179. // No locking, as threads are not supported.
  180. void AudioDriverJavaScript::lock() {
  181. }
  182. void AudioDriverJavaScript::unlock() {
  183. }
  184. void AudioDriverJavaScript::finish_async() {
  185. // Close the context, add the operation to the async_finish list in module.
  186. int id = _driver_id;
  187. _driver_id = 0;
  188. /* clang-format off */
  189. EM_ASM({
  190. const id = $0;
  191. var ref = Module.IDHandler.get(id);
  192. Module.async_finish.push(new Promise(function(accept, reject) {
  193. if (!ref) {
  194. console.log("Ref not found!", id, Module.IDHandler);
  195. setTimeout(accept, 0);
  196. } else {
  197. Module.IDHandler.remove(id);
  198. const context = ref['context'];
  199. // Disconnect script and input.
  200. ref['script'].disconnect();
  201. if (ref['input'])
  202. ref['input'].disconnect();
  203. ref = null;
  204. context.close().then(function() {
  205. accept();
  206. }).catch(function(e) {
  207. accept();
  208. });
  209. }
  210. }));
  211. }, id);
  212. /* clang-format on */
  213. }
  214. void AudioDriverJavaScript::finish() {
  215. if (internal_buffer) {
  216. memdelete_arr(internal_buffer);
  217. internal_buffer = NULL;
  218. }
  219. }
  220. Error AudioDriverJavaScript::capture_start() {
  221. input_buffer_init(buffer_length);
  222. /* clang-format off */
  223. EM_ASM({
  224. function gotMediaInput(stream) {
  225. var ref = Module.IDHandler.get($0);
  226. ref['stream'] = stream;
  227. ref['input'] = ref['context'].createMediaStreamSource(stream);
  228. ref['input'].connect(ref['script']);
  229. }
  230. function gotMediaInputError(e) {
  231. out(e);
  232. }
  233. if (navigator.mediaDevices.getUserMedia) {
  234. navigator.mediaDevices.getUserMedia({"audio": true}).then(gotMediaInput, gotMediaInputError);
  235. } else {
  236. if (!navigator.getUserMedia)
  237. navigator.getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
  238. navigator.getUserMedia({"audio": true}, gotMediaInput, gotMediaInputError);
  239. }
  240. }, _driver_id);
  241. /* clang-format on */
  242. return OK;
  243. }
  244. Error AudioDriverJavaScript::capture_stop() {
  245. /* clang-format off */
  246. EM_ASM({
  247. var ref = Module.IDHandler.get($0);
  248. if (ref['stream']) {
  249. const tracks = ref['stream'].getTracks();
  250. for (var i = 0; i < tracks.length; i++) {
  251. tracks[i].stop();
  252. }
  253. ref['stream'] = null;
  254. }
  255. if (ref['input']) {
  256. ref['input'].disconnect();
  257. ref['input'] = null;
  258. }
  259. }, _driver_id);
  260. /* clang-format on */
  261. input_buffer.clear();
  262. return OK;
  263. }
  264. AudioDriverJavaScript::AudioDriverJavaScript() {
  265. _driver_id = 0;
  266. internal_buffer = NULL;
  267. buffer_length = 0;
  268. singleton = this;
  269. }