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