audio_driver_javascript.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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. AudioDriverJavaScript *AudioDriverJavaScript::singleton = NULL;
  33. const char *AudioDriverJavaScript::get_name() const {
  34. return "JavaScript";
  35. }
  36. extern "C" EMSCRIPTEN_KEEPALIVE void audio_driver_js_mix() {
  37. AudioDriverJavaScript::singleton->mix_to_js();
  38. }
  39. void AudioDriverJavaScript::mix_to_js() {
  40. int channel_count = get_total_channels_by_speaker_mode(get_speaker_mode());
  41. int sample_count = memarr_len(internal_buffer) / channel_count;
  42. int32_t *stream_buffer = reinterpret_cast<int32_t *>(internal_buffer);
  43. audio_server_process(sample_count, stream_buffer);
  44. for (int i = 0; i < sample_count * channel_count; i++) {
  45. internal_buffer[i] = float(stream_buffer[i] >> 16) / 32768.0;
  46. }
  47. }
  48. Error AudioDriverJavaScript::init() {
  49. /* clang-format off */
  50. EM_ASM({
  51. _audioDriver_audioContext = new (window.AudioContext || window.webkitAudioContext);
  52. _audioDriver_scriptNode = null;
  53. });
  54. /* clang-format on */
  55. int channel_count = get_total_channels_by_speaker_mode(get_speaker_mode());
  56. /* clang-format off */
  57. int buffer_length = EM_ASM_INT({
  58. var CHANNEL_COUNT = $0;
  59. var channelCount = _audioDriver_audioContext.destination.channelCount;
  60. try {
  61. // Try letting the browser recommend a buffer length.
  62. _audioDriver_scriptNode = _audioDriver_audioContext.createScriptProcessor(0, 0, channelCount);
  63. } catch (e) {
  64. // ...otherwise, default to 4096.
  65. _audioDriver_scriptNode = _audioDriver_audioContext.createScriptProcessor(4096, 0, channelCount);
  66. }
  67. _audioDriver_scriptNode.connect(_audioDriver_audioContext.destination);
  68. return _audioDriver_scriptNode.bufferSize;
  69. }, channel_count);
  70. /* clang-format on */
  71. if (!buffer_length) {
  72. return FAILED;
  73. }
  74. if (!internal_buffer || memarr_len(internal_buffer) != buffer_length * channel_count) {
  75. if (internal_buffer)
  76. memdelete_arr(internal_buffer);
  77. internal_buffer = memnew_arr(float, buffer_length *channel_count);
  78. }
  79. return internal_buffer ? OK : ERR_OUT_OF_MEMORY;
  80. }
  81. void AudioDriverJavaScript::start() {
  82. /* clang-format off */
  83. EM_ASM({
  84. var INTERNAL_BUFFER_PTR = $0;
  85. var audioDriverMixFunction = cwrap('audio_driver_js_mix');
  86. _audioDriver_scriptNode.onaudioprocess = function(audioProcessingEvent) {
  87. audioDriverMixFunction();
  88. // The output buffer contains the samples that will be modified and played.
  89. var output = audioProcessingEvent.outputBuffer;
  90. var input = HEAPF32.subarray(
  91. INTERNAL_BUFFER_PTR / HEAPF32.BYTES_PER_ELEMENT,
  92. INTERNAL_BUFFER_PTR / HEAPF32.BYTES_PER_ELEMENT + output.length * output.numberOfChannels);
  93. for (var channel = 0; channel < output.numberOfChannels; channel++) {
  94. var outputData = output.getChannelData(channel);
  95. // Loop through samples.
  96. for (var sample = 0; sample < outputData.length; sample++) {
  97. // Set output equal to input.
  98. outputData[sample] = input[sample * output.numberOfChannels + channel];
  99. }
  100. }
  101. };
  102. }, internal_buffer);
  103. /* clang-format on */
  104. }
  105. int AudioDriverJavaScript::get_mix_rate() const {
  106. /* clang-format off */
  107. return EM_ASM_INT_V({
  108. return _audioDriver_audioContext.sampleRate;
  109. });
  110. /* clang-format on */
  111. }
  112. AudioDriver::SpeakerMode AudioDriverJavaScript::get_speaker_mode() const {
  113. /* clang-format off */
  114. return get_speaker_mode_by_total_channels(EM_ASM_INT_V({
  115. return _audioDriver_audioContext.destination.channelCount;
  116. }));
  117. /* clang-format on */
  118. }
  119. // No locking, as threads are not supported.
  120. void AudioDriverJavaScript::lock() {
  121. }
  122. void AudioDriverJavaScript::unlock() {
  123. }
  124. void AudioDriverJavaScript::finish() {
  125. /* clang-format off */
  126. EM_ASM({
  127. _audioDriver_audioContext = null;
  128. _audioDriver_scriptNode = null;
  129. });
  130. /* clang-format on */
  131. memdelete_arr(internal_buffer);
  132. internal_buffer = NULL;
  133. }
  134. AudioDriverJavaScript::AudioDriverJavaScript() {
  135. singleton = this;
  136. }