Skip to content

Commit 96644ee

Browse files
NandoWandoNandoWando
authored andcommitted
Bugs Fixed/Update Game Data
1 parent 79ebedb commit 96644ee

File tree

7 files changed

+1324
-0
lines changed

7 files changed

+1324
-0
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
game/game.wasm filter=lfs diff=lfs merge=lfs -text

game/.DS_Store

6 KB
Binary file not shown.

game/game.audio.worklet.js

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
class RingBuffer {
2+
constructor(p_buffer, p_state, p_threads) {
3+
this.buffer = p_buffer;
4+
this.avail = p_state;
5+
this.threads = p_threads;
6+
this.rpos = 0;
7+
this.wpos = 0;
8+
}
9+
10+
data_left() {
11+
return this.threads ? Atomics.load(this.avail, 0) : this.avail;
12+
}
13+
14+
space_left() {
15+
return this.buffer.length - this.data_left();
16+
}
17+
18+
read(output) {
19+
const size = this.buffer.length;
20+
let from = 0;
21+
let to_write = output.length;
22+
if (this.rpos + to_write > size) {
23+
const high = size - this.rpos;
24+
output.set(this.buffer.subarray(this.rpos, size));
25+
from = high;
26+
to_write -= high;
27+
this.rpos = 0;
28+
}
29+
if (to_write) {
30+
output.set(this.buffer.subarray(this.rpos, this.rpos + to_write), from);
31+
}
32+
this.rpos += to_write;
33+
if (this.threads) {
34+
Atomics.add(this.avail, 0, -output.length);
35+
Atomics.notify(this.avail, 0);
36+
} else {
37+
this.avail -= output.length;
38+
}
39+
}
40+
41+
write(p_buffer) {
42+
const to_write = p_buffer.length;
43+
const mw = this.buffer.length - this.wpos;
44+
if (mw >= to_write) {
45+
this.buffer.set(p_buffer, this.wpos);
46+
this.wpos += to_write;
47+
if (mw === to_write) {
48+
this.wpos = 0;
49+
}
50+
} else {
51+
const high = p_buffer.subarray(0, mw);
52+
const low = p_buffer.subarray(mw);
53+
this.buffer.set(high, this.wpos);
54+
this.buffer.set(low);
55+
this.wpos = low.length;
56+
}
57+
if (this.threads) {
58+
Atomics.add(this.avail, 0, to_write);
59+
Atomics.notify(this.avail, 0);
60+
} else {
61+
this.avail += to_write;
62+
}
63+
}
64+
}
65+
66+
class GodotProcessor extends AudioWorkletProcessor {
67+
constructor() {
68+
super();
69+
this.threads = false;
70+
this.running = true;
71+
this.lock = null;
72+
this.notifier = null;
73+
this.output = null;
74+
this.output_buffer = new Float32Array();
75+
this.input = null;
76+
this.input_buffer = new Float32Array();
77+
this.port.onmessage = (event) => {
78+
const cmd = event.data["cmd"];
79+
const data = event.data["data"];
80+
this.parse_message(cmd, data);
81+
};
82+
}
83+
84+
process_notify() {
85+
if (this.notifier) {
86+
Atomics.add(this.notifier, 0, 1);
87+
Atomics.notify(this.notifier, 0);
88+
}
89+
}
90+
91+
parse_message(p_cmd, p_data) {
92+
if (p_cmd === "start" && p_data) {
93+
const state = p_data[0];
94+
let idx = 0;
95+
this.threads = true;
96+
this.lock = state.subarray(idx, ++idx);
97+
this.notifier = state.subarray(idx, ++idx);
98+
const avail_in = state.subarray(idx, ++idx);
99+
const avail_out = state.subarray(idx, ++idx);
100+
this.input = new RingBuffer(p_data[1], avail_in, true);
101+
this.output = new RingBuffer(p_data[2], avail_out, true);
102+
} else if (p_cmd === "stop") {
103+
this.running = false;
104+
this.output = null;
105+
this.input = null;
106+
this.lock = null;
107+
this.notifier = null;
108+
} else if (p_cmd === "start_nothreads") {
109+
this.output = new RingBuffer(p_data[0], p_data[0].length, false);
110+
} else if (p_cmd === "chunk") {
111+
this.output.write(p_data);
112+
}
113+
}
114+
115+
static array_has_data(arr) {
116+
return arr.length && arr[0].length && arr[0][0].length;
117+
}
118+
119+
process(inputs, outputs, parameters) {
120+
if (!this.running) {
121+
return false; // Stop processing.
122+
}
123+
if (this.output === null) {
124+
return true; // Not ready yet, keep processing.
125+
}
126+
const process_input = GodotProcessor.array_has_data(inputs);
127+
if (process_input) {
128+
const input = inputs[0];
129+
const chunk = input[0].length * input.length;
130+
if (this.input_buffer.length !== chunk) {
131+
this.input_buffer = new Float32Array(chunk);
132+
}
133+
if (!this.threads) {
134+
GodotProcessor.write_input(this.input_buffer, input);
135+
this.port.postMessage({ cmd: "input", data: this.input_buffer });
136+
} else if (this.input.space_left() >= chunk) {
137+
GodotProcessor.write_input(this.input_buffer, input);
138+
this.input.write(this.input_buffer);
139+
} else {
140+
// this.port.postMessage('Input buffer is full! Skipping input frame.'); // Uncomment this line to debug input buffer.
141+
}
142+
}
143+
const process_output = GodotProcessor.array_has_data(outputs);
144+
if (process_output) {
145+
const output = outputs[0];
146+
const chunk = output[0].length * output.length;
147+
if (this.output_buffer.length !== chunk) {
148+
this.output_buffer = new Float32Array(chunk);
149+
}
150+
if (this.output.data_left() >= chunk) {
151+
this.output.read(this.output_buffer);
152+
GodotProcessor.write_output(output, this.output_buffer);
153+
if (!this.threads) {
154+
this.port.postMessage({ cmd: "read", data: chunk });
155+
}
156+
} else {
157+
// this.port.postMessage('Output buffer has not enough frames! Skipping output frame.'); // Uncomment this line to debug output buffer.
158+
}
159+
}
160+
this.process_notify();
161+
return true;
162+
}
163+
164+
static write_output(dest, source) {
165+
const channels = dest.length;
166+
for (let ch = 0; ch < channels; ch++) {
167+
for (let sample = 0; sample < dest[ch].length; sample++) {
168+
dest[ch][sample] = source[sample * channels + ch];
169+
}
170+
}
171+
}
172+
173+
static write_input(dest, source) {
174+
const channels = source.length;
175+
for (let ch = 0; ch < channels; ch++) {
176+
for (let sample = 0; sample < source[ch].length; sample++) {
177+
dest[sample * channels + ch] = source[ch][sample];
178+
}
179+
}
180+
}
181+
}
182+
183+
registerProcessor("godot-processor", GodotProcessor);

0 commit comments

Comments
 (0)