Web: add panning, move mixing to wasm

This commit is contained in:
2024-11-02 11:14:24 -05:00
parent 8b9152dde9
commit b4b9131f39
5 changed files with 175 additions and 73 deletions
+17 -20
View File
@@ -82,23 +82,20 @@ new class Core {
let sim = {
canvas : null,
keys : Constants.VB.SGN,
pointer : sims[x] = this.CreateSim(),
volume : 1
pointer : sims[x] = this.CreateSim()
};
this.sims.set(sim.pointer, sim);
// Video
this.SetAnaglyph(sim.pointer,
Constants.web.STEREO_RED, Constants.web.STEREO_CYAN);
sim.pixels = this.#noalloc(
this.GetExt(sim.pointer, Constants.web.EXT_PIXELS),
this.GetExtPixels(sim.pointer),
384*224*4, sim, "pixels", Uint8ClampedArray
);
sim.image = new ImageData(sim.pixels, 384, 224);
// Audio
sim.samples = this.#noalloc(
this.GetExt(sim.pointer, Constants.web.EXT_SAMPLES),
this.GetExtSamples(sim.pointer),
41700 / 50 * 2, sim, "samples", Float32Array
);
@@ -296,6 +293,12 @@ new class Core {
this.dom.postMessage({ promised: true });
}
// Specify audio panning
setPanning(message) {
this.SetPanning(message.sim, message.panning);
this.dom.postMessage({ promised: true });
}
// Specify a new communication peer
setPeer(message) {
let orphaned = [];
@@ -318,7 +321,7 @@ new class Core {
// Specify audio volume
setVolume(message) {
this.sims.get(message.sim).volume = message.volume;
this.SetVolume(message.sim, message.volume);
this.dom.postMessage({ promised: true });
}
@@ -408,19 +411,13 @@ new class Core {
// Mix and output audio samples
let buffer = this.audio.buffers.shift();
if (this.automatic.sims.length > 1) {
for (let x = 0; x < buffer.length; x++)
buffer[x] = 0;
for (let sim of this.automatic.sims)
for (let x = 0; x < buffer.length; x++)
buffer[x] += sim.samples[x] * sim.volume;
for (let x = 0; x < buffer.length; x++)
buffer[x] = Math.min(Math.max(-1, buffer[x]), +1);
} else {
let sim = this.automatic.sims[0];
for (let x = 0; x < buffer.length; x++)
buffer[x] = sim.samples[x] * sim.volume;
}
this.Mix(
this.audio.samples.pointer,
this.automatic.pointers.pointer,
this.automatic.sims.length
);
for (let x = 0; x < buffer.length; x++)
buffer[x] = this.audio.samples[x];
this.audio.postMessage(buffer.buffer, [ buffer.buffer ]);
// Output staged images if there's one audio buffer to go