audio.worklet.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**************************************************************************/
  2. /* audio.worklet.js */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. class RingBuffer {
  31. constructor(p_buffer, p_state, p_threads) {
  32. this.buffer = p_buffer;
  33. this.avail = p_state;
  34. this.threads = p_threads;
  35. this.rpos = 0;
  36. this.wpos = 0;
  37. }
  38. data_left() {
  39. return this.threads ? Atomics.load(this.avail, 0) : this.avail;
  40. }
  41. space_left() {
  42. return this.buffer.length - this.data_left();
  43. }
  44. read(output) {
  45. const size = this.buffer.length;
  46. let from = 0;
  47. let to_write = output.length;
  48. if (this.rpos + to_write > size) {
  49. const high = size - this.rpos;
  50. output.set(this.buffer.subarray(this.rpos, size));
  51. from = high;
  52. to_write -= high;
  53. this.rpos = 0;
  54. }
  55. if (to_write) {
  56. output.set(this.buffer.subarray(this.rpos, this.rpos + to_write), from);
  57. }
  58. this.rpos += to_write;
  59. if (this.threads) {
  60. Atomics.add(this.avail, 0, -output.length);
  61. Atomics.notify(this.avail, 0);
  62. } else {
  63. this.avail -= output.length;
  64. }
  65. }
  66. write(p_buffer) {
  67. const to_write = p_buffer.length;
  68. const mw = this.buffer.length - this.wpos;
  69. if (mw >= to_write) {
  70. this.buffer.set(p_buffer, this.wpos);
  71. this.wpos += to_write;
  72. if (mw === to_write) {
  73. this.wpos = 0;
  74. }
  75. } else {
  76. const high = p_buffer.subarray(0, mw);
  77. const low = p_buffer.subarray(mw);
  78. this.buffer.set(high, this.wpos);
  79. this.buffer.set(low);
  80. this.wpos = low.length;
  81. }
  82. if (this.threads) {
  83. Atomics.add(this.avail, 0, to_write);
  84. Atomics.notify(this.avail, 0);
  85. } else {
  86. this.avail += to_write;
  87. }
  88. }
  89. }
  90. class GodotProcessor extends AudioWorkletProcessor {
  91. constructor() {
  92. super();
  93. this.threads = false;
  94. this.running = true;
  95. this.lock = null;
  96. this.notifier = null;
  97. this.output = null;
  98. this.output_buffer = new Float32Array();
  99. this.input = null;
  100. this.input_buffer = new Float32Array();
  101. this.port.onmessage = (event) => {
  102. const cmd = event.data['cmd'];
  103. const data = event.data['data'];
  104. this.parse_message(cmd, data);
  105. };
  106. }
  107. process_notify() {
  108. if (this.notifier) {
  109. Atomics.add(this.notifier, 0, 1);
  110. Atomics.notify(this.notifier, 0);
  111. }
  112. }
  113. parse_message(p_cmd, p_data) {
  114. if (p_cmd === 'start' && p_data) {
  115. const state = p_data[0];
  116. let idx = 0;
  117. this.threads = true;
  118. this.lock = state.subarray(idx, ++idx);
  119. this.notifier = state.subarray(idx, ++idx);
  120. const avail_in = state.subarray(idx, ++idx);
  121. const avail_out = state.subarray(idx, ++idx);
  122. this.input = new RingBuffer(p_data[1], avail_in, true);
  123. this.output = new RingBuffer(p_data[2], avail_out, true);
  124. } else if (p_cmd === 'stop') {
  125. this.running = false;
  126. this.output = null;
  127. this.input = null;
  128. this.lock = null;
  129. this.notifier = null;
  130. } else if (p_cmd === 'start_nothreads') {
  131. this.output = new RingBuffer(p_data[0], p_data[0].length, false);
  132. } else if (p_cmd === 'chunk') {
  133. this.output.write(p_data);
  134. }
  135. }
  136. static array_has_data(arr) {
  137. return arr.length && arr[0].length && arr[0][0].length;
  138. }
  139. process(inputs, outputs, parameters) {
  140. if (!this.running) {
  141. return false; // Stop processing.
  142. }
  143. if (this.output === null) {
  144. return true; // Not ready yet, keep processing.
  145. }
  146. const process_input = GodotProcessor.array_has_data(inputs);
  147. if (process_input) {
  148. const input = inputs[0];
  149. const chunk = input[0].length * input.length;
  150. if (this.input_buffer.length !== chunk) {
  151. this.input_buffer = new Float32Array(chunk);
  152. }
  153. if (!this.threads) {
  154. GodotProcessor.write_input(this.input_buffer, input);
  155. this.port.postMessage({ 'cmd': 'input', 'data': this.input_buffer });
  156. } else if (this.input.space_left() >= chunk) {
  157. GodotProcessor.write_input(this.input_buffer, input);
  158. this.input.write(this.input_buffer);
  159. } else {
  160. // this.port.postMessage('Input buffer is full! Skipping input frame.'); // Uncomment this line to debug input buffer.
  161. }
  162. }
  163. const process_output = GodotProcessor.array_has_data(outputs);
  164. if (process_output) {
  165. const output = outputs[0];
  166. const chunk = output[0].length * output.length;
  167. if (this.output_buffer.length !== chunk) {
  168. this.output_buffer = new Float32Array(chunk);
  169. }
  170. if (this.output.data_left() >= chunk) {
  171. this.output.read(this.output_buffer);
  172. GodotProcessor.write_output(output, this.output_buffer);
  173. if (!this.threads) {
  174. this.port.postMessage({ 'cmd': 'read', 'data': chunk });
  175. }
  176. } else {
  177. // this.port.postMessage('Output buffer has not enough frames! Skipping output frame.'); // Uncomment this line to debug output buffer.
  178. }
  179. }
  180. this.process_notify();
  181. return true;
  182. }
  183. static write_output(dest, source) {
  184. const channels = dest.length;
  185. for (let ch = 0; ch < channels; ch++) {
  186. for (let sample = 0; sample < dest[ch].length; sample++) {
  187. dest[ch][sample] = source[sample * channels + ch];
  188. }
  189. }
  190. }
  191. static write_input(dest, source) {
  192. const channels = source.length;
  193. for (let ch = 0; ch < channels; ch++) {
  194. for (let sample = 0; sample < source[ch].length; sample++) {
  195. dest[sample * channels + ch] = source[ch][sample];
  196. }
  197. }
  198. }
  199. }
  200. registerProcessor('godot-processor', GodotProcessor);