audio_driver_javascript.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 <emscripten.h>
  32. #include <string.h>
  33. #define MAX_NUMBER_INTERFACES 3
  34. #define MAX_NUMBER_OUTPUT_DEVICES 6
  35. /* Structure for passing information to callback function */
  36. //AudioDriverJavaScript* AudioDriverJavaScript::s_ad=NULL;
  37. AudioDriverJavaScript *AudioDriverJavaScript::singleton_js = NULL;
  38. const char *AudioDriverJavaScript::get_name() const {
  39. return "JavaScript";
  40. }
  41. extern "C" {
  42. void js_audio_driver_mix_function(int p_frames) {
  43. //print_line("MIXI! "+itos(p_frames));
  44. AudioDriverJavaScript::singleton_js->mix_to_js(p_frames);
  45. }
  46. }
  47. void AudioDriverJavaScript::mix_to_js(int p_frames) {
  48. int todo = p_frames;
  49. int offset = 0;
  50. while (todo) {
  51. int tomix = MIN(todo, INTERNAL_BUFFER_SIZE);
  52. audio_server_process(p_frames, stream_buffer);
  53. for (int i = 0; i < tomix * internal_buffer_channels; i++) {
  54. internal_buffer[i] = float(stream_buffer[i] >> 16) * 32768.0;
  55. }
  56. /* clang-format off */
  57. EM_ASM_({
  58. var data = HEAPF32.subarray($0 / 4, $0 / 4 + $2 * 2);
  59. for (var channel = 0; channel < _as_output_buffer.numberOfChannels; channel++) {
  60. var outputData = _as_output_buffer.getChannelData(channel);
  61. // Loop through samples
  62. for (var sample = 0; sample < $2; sample++) {
  63. // make output equal to the same as the input
  64. outputData[sample + $1] = data[sample * 2 + channel];
  65. }
  66. }
  67. }, internal_buffer, offset, tomix);
  68. /* clang-format on */
  69. todo -= tomix;
  70. offset += tomix;
  71. }
  72. }
  73. Error AudioDriverJavaScript::init() {
  74. return OK;
  75. }
  76. void AudioDriverJavaScript::start() {
  77. internal_buffer_channels = 2;
  78. internal_buffer = memnew_arr(float, INTERNAL_BUFFER_SIZE *internal_buffer_channels);
  79. stream_buffer = memnew_arr(int32_t, INTERNAL_BUFFER_SIZE * 4); //max 4 channels
  80. /* clang-format off */
  81. EM_ASM(
  82. _as_audioctx = new (window.AudioContext || window.webkitAudioContext)();
  83. audio_server_mix_function = Module.cwrap('js_audio_driver_mix_function', 'void', ['number']);
  84. );
  85. int buffer_latency = 16384;
  86. EM_ASM_( {
  87. _as_script_node = _as_audioctx.createScriptProcessor($0, 0, 2);
  88. _as_script_node.connect(_as_audioctx.destination);
  89. console.log(_as_script_node.bufferSize);
  90. _as_script_node.onaudioprocess = function(audioProcessingEvent) {
  91. // The output buffer contains the samples that will be modified and played
  92. _as_output_buffer = audioProcessingEvent.outputBuffer;
  93. audio_server_mix_function(_as_output_buffer.getChannelData(0).length);
  94. }
  95. }, buffer_latency);
  96. /* clang-format on */
  97. }
  98. int AudioDriverJavaScript::get_mix_rate() const {
  99. return mix_rate;
  100. }
  101. AudioDriver::SpeakerMode AudioDriverJavaScript::get_speaker_mode() const {
  102. return SPEAKER_MODE_STEREO;
  103. }
  104. void AudioDriverJavaScript::lock() {
  105. /*no locking, as threads are not supported
  106. if (active && mutex)
  107. mutex->lock();
  108. */
  109. }
  110. void AudioDriverJavaScript::unlock() {
  111. /*no locking, as threads are not supported
  112. if (active && mutex)
  113. mutex->unlock();
  114. */
  115. }
  116. void AudioDriverJavaScript::finish() {
  117. }
  118. AudioDriverJavaScript::AudioDriverJavaScript() {
  119. mix_rate = 44100;
  120. singleton_js = this;
  121. }