library_godot_audio.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*************************************************************************/
  2. /* library_godot_audio.js */
  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. var GodotAudio = {
  31. $GodotAudio: {
  32. ctx: null,
  33. input: null,
  34. script: null,
  35. },
  36. godot_audio_is_available__proxy: 'sync',
  37. godot_audio_is_available: function () {
  38. if (!(window.AudioContext || window.webkitAudioContext)) {
  39. return 0;
  40. }
  41. return 1;
  42. },
  43. godot_audio_init: function(mix_rate, latency) {
  44. GodotAudio.ctx = new (window.AudioContext || window.webkitAudioContext)({
  45. sampleRate: mix_rate,
  46. latencyHint: latency
  47. });
  48. return GodotAudio.ctx.destination.channelCount;
  49. },
  50. godot_audio_create_processor: function(buffer_length, channel_count) {
  51. GodotAudio.script = GodotAudio.ctx.createScriptProcessor(buffer_length, 2, channel_count);
  52. GodotAudio.script.connect(GodotAudio.ctx.destination);
  53. return GodotAudio.script.bufferSize;
  54. },
  55. godot_audio_start: function(buffer_ptr) {
  56. var audioDriverProcessStart = cwrap('audio_driver_process_start');
  57. var audioDriverProcessEnd = cwrap('audio_driver_process_end');
  58. var audioDriverProcessCapture = cwrap('audio_driver_process_capture', null, ['number']);
  59. GodotAudio.script.onaudioprocess = function(audioProcessingEvent) {
  60. audioDriverProcessStart();
  61. var input = audioProcessingEvent.inputBuffer;
  62. var output = audioProcessingEvent.outputBuffer;
  63. var internalBuffer = HEAPF32.subarray(
  64. buffer_ptr / HEAPF32.BYTES_PER_ELEMENT,
  65. buffer_ptr / HEAPF32.BYTES_PER_ELEMENT + output.length * output.numberOfChannels);
  66. for (var channel = 0; channel < output.numberOfChannels; channel++) {
  67. var outputData = output.getChannelData(channel);
  68. // Loop through samples.
  69. for (var sample = 0; sample < outputData.length; sample++) {
  70. outputData[sample] = internalBuffer[sample * output.numberOfChannels + channel];
  71. }
  72. }
  73. if (GodotAudio.input) {
  74. var inputDataL = input.getChannelData(0);
  75. var inputDataR = input.getChannelData(1);
  76. for (var i = 0; i < inputDataL.length; i++) {
  77. audioDriverProcessCapture(inputDataL[i]);
  78. audioDriverProcessCapture(inputDataR[i]);
  79. }
  80. }
  81. audioDriverProcessEnd();
  82. };
  83. },
  84. godot_audio_resume: function() {
  85. if (GodotAudio.ctx && GodotAudio.ctx.state != 'running') {
  86. GodotAudio.ctx.resume();
  87. }
  88. },
  89. godot_audio_finish_async: function() {
  90. Module.async_finish.push(new Promise(function(accept, reject) {
  91. if (!GodotAudio.ctx) {
  92. setTimeout(accept, 0);
  93. } else {
  94. if (GodotAudio.script) {
  95. GodotAudio.script.disconnect();
  96. GodotAudio.script = null;
  97. }
  98. if (GodotAudio.input) {
  99. GodotAudio.input.disconnect();
  100. GodotAudio.input = null;
  101. }
  102. GodotAudio.ctx.close().then(function() {
  103. accept();
  104. }).catch(function(e) {
  105. accept();
  106. });
  107. GodotAudio.ctx = null;
  108. }
  109. }));
  110. },
  111. godot_audio_get_latency__proxy: 'sync',
  112. godot_audio_get_latency: function() {
  113. var latency = 0;
  114. if (GodotAudio.ctx) {
  115. if (GodotAudio.ctx.baseLatency) {
  116. latency += GodotAudio.ctx.baseLatency;
  117. }
  118. if (GodotAudio.ctx.outputLatency) {
  119. latency += GodotAudio.ctx.outputLatency;
  120. }
  121. }
  122. return latency;
  123. },
  124. godot_audio_capture_start__proxy: 'sync',
  125. godot_audio_capture_start: function() {
  126. if (GodotAudio.input) {
  127. return; // Already started.
  128. }
  129. function gotMediaInput(stream) {
  130. GodotAudio.input = GodotAudio.ctx.createMediaStreamSource(stream);
  131. GodotAudio.input.connect(GodotAudio.script);
  132. }
  133. function gotMediaInputError(e) {
  134. out(e);
  135. }
  136. if (navigator.mediaDevices.getUserMedia) {
  137. navigator.mediaDevices.getUserMedia({"audio": true}).then(gotMediaInput, gotMediaInputError);
  138. } else {
  139. if (!navigator.getUserMedia)
  140. navigator.getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
  141. navigator.getUserMedia({"audio": true}, gotMediaInput, gotMediaInputError);
  142. }
  143. },
  144. godot_audio_capture_stop__proxy: 'sync',
  145. godot_audio_capture_stop: function() {
  146. if (GodotAudio.input) {
  147. const tracks = GodotAudio.input.mediaStream.getTracks();
  148. for (var i = 0; i < tracks.length; i++) {
  149. tracks[i].stop();
  150. }
  151. GodotAudio.input.disconnect();
  152. GodotAudio.input = null;
  153. }
  154. },
  155. };
  156. autoAddDeps(GodotAudio, "$GodotAudio");
  157. mergeInto(LibraryManager.library, GodotAudio);