audio_driver_javascript.cpp 5.0 KB

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