shadertoy.audio.worklet.js 6.3 KB

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