feat(audio): passing sounds decoded once and played through WebAudio, no lag with several cars at once (1.89.27)

This commit is contained in:
larssand committed 2026-11-14 15:40:49 +01:00
1 parent 5241cced77
commit ae829123cc
6 files changed
+112 -5

No files matched your search

+58
View File
@@ -30,6 +30,64 @@ test("missing custom passing sound never rejects and falls back safely", async (
assert.deepEqual(played, ["/race-sounds/sounds/effects/pass_01.wav", "/race-sounds/sounds/effects/transponder_pass.wav"]);
});
// A fake AudioContext: decodeAudioData and every started buffer source are recorded.
function fakeAudioContext(state = "running") {
const log = { decoded: 0, started: [] };
const context = {
state,
destination: {},
decodeAudioData: (data, ok) => { log.decoded += 1; ok({ duration: 0.1, data }); },
createGain: () => ({ gain: { value: 1 }, connect: () => {} }),
createBufferSource: () => {
const source = { buffer: null, playbackRate: { value: 1 }, connect: () => {}, start: () => log.started.push({ buffer: source.buffer, rate: source.playbackRate.value }) };
return source;
},
};
return { context, log };
}
test("passings play from a sound decoded once through WebAudio, several at once", async () => {
const fetched = [];
const elements = [];
const { context, log } = fakeAudioContext();
const engine = createRaceAudioEngine({
fetchImpl: async (url) => { fetched.push(url); return url.endsWith(".json") ? { ok: true, json: async () => manifest } : { ok: true, arrayBuffer: async () => new ArrayBuffer(8) }; },
audioFactory: (src) => ({ play: () => { elements.push(src); return Promise.resolve(); }, volume: 1 }),
getAudioContext: () => context,
getSettings: () => normalizeRaceAudioSettings(),
});
const results = await Promise.all([engine.playEffect("transponder_pass"), engine.playEffect("transponder_pass"), engine.playEffect("transponder_pass")]);
assert.deepEqual(results, [true, true, true]);
assert.equal(log.started.length, 3, "three passings, three sounds");
assert.equal(log.decoded, 1, "decoded once");
assert.equal(fetched.filter((url) => url.endsWith(".wav")).length, 1, "fetched once");
assert.deepEqual(elements, [], "no <audio> element for passings");
// Other effects (start tone, warnings) keep the <audio> path.
await engine.playEffect("start_tone");
assert.equal(elements.length, 1);
});
test("passing sound falls back to <audio> without a running AudioContext or when the file can't be decoded", async () => {
const run = async (contextFactory, fetchWav) => {
const elements = [];
const engine = createRaceAudioEngine({
fetchImpl: async (url) => (url.endsWith(".json") ? { ok: true, json: async () => manifest } : fetchWav()),
audioFactory: (src) => ({ play: () => { elements.push(src); return Promise.resolve(); }, volume: 1 }),
getAudioContext: contextFactory,
getSettings: () => normalizeRaceAudioSettings(),
});
assert.equal(await engine.playEffect("transponder_pass"), true);
return elements.length;
};
const okWav = () => ({ ok: true, arrayBuffer: async () => new ArrayBuffer(8) });
assert.equal(await run(() => fakeAudioContext("suspended").context, okWav), 1, "page not clicked yet: <audio>");
assert.equal(await run(() => null, okWav), 1, "no WebAudio: <audio>");
assert.equal(await run(() => fakeAudioContext().context, () => ({ ok: false })), 1, "file not found for WebAudio: <audio>");
const broken = fakeAudioContext();
broken.context.decodeAudioData = (data, ok, fail) => fail(new Error("bad wav"));
assert.equal(await run(() => broken.context, okWav), 1, "can't decode: <audio>");
});
test("critical control voice is prioritized over queued low-priority voice", async () => {
const resolvers = [];
const spoken = [];