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:
1 parent
5241cced77
commit
ae829123cc
6 files changed
+112
-5
No files matched your search
@@ -6219,6 +6219,8 @@ let audioCtx = null;
|
||||
let lastFinishAnnouncementSessionId = null;
|
||||
const raceAudioEngine = createRaceAudioEngine({
|
||||
getSettings: () => state.settings.raceAudio,
|
||||
// Passing sounds through WebAudio (decoded once, low latency); overlay windows stay silent anyway.
|
||||
getAudioContext: () => (overlayMode ? null : ensureAudioContext()),
|
||||
speak: (text, options = {}) => new Promise((resolve) => {
|
||||
let settled = false;
|
||||
const done = (value) => { if (!settled) { settled = true; resolve(value); } };
|
||||
|
||||
+43
-2
@@ -234,6 +234,10 @@ export function createRaceAudioEngine(options = {}) {
|
||||
const random = options.random || Math.random;
|
||||
const now = options.now || Date.now;
|
||||
const log = options.log || (() => {});
|
||||
// WebAudio for passing sounds: each file is fetched and decoded once, then every passing plays
|
||||
// its own copy at once (overlapping, no <audio> start-up lag). No context: the <audio> path.
|
||||
const getAudioContext = options.getAudioContext || (() => null);
|
||||
const decodedBuffers = new Map(); // url -> Promise<AudioBuffer | null>
|
||||
let manifestPromise = null;
|
||||
let manifest = null;
|
||||
let currentVoice = null;
|
||||
@@ -279,6 +283,43 @@ export function createRaceAudioEngine(options = {}) {
|
||||
}
|
||||
}
|
||||
|
||||
function loadBuffer(context, url) {
|
||||
if (!decodedBuffers.has(url)) decodedBuffers.set(url, (async () => {
|
||||
try {
|
||||
const response = await fetchImpl(url);
|
||||
if (!response?.ok) return null;
|
||||
const data = await response.arrayBuffer();
|
||||
return await new Promise((resolve, reject) => {
|
||||
const result = context.decodeAudioData(data, resolve, reject);
|
||||
if (result?.then) result.then(resolve, reject);
|
||||
});
|
||||
} catch { return null; }
|
||||
})());
|
||||
return decodedBuffers.get(url);
|
||||
}
|
||||
|
||||
// true = played through WebAudio, null = not possible here (use <audio>).
|
||||
async function attemptBufferedEffect(effectPath, volume, playbackRate) {
|
||||
if (!effectPath || missingEffects.has(effectPath) || !fetchImpl) return null;
|
||||
let context = null;
|
||||
try { context = getAudioContext(); } catch { context = null; }
|
||||
// Not allowed to sound yet (no click on the page since load): <audio> as before.
|
||||
if (!context?.createBufferSource || context.state === "suspended" || context.state === "closed") return null;
|
||||
const buffer = await loadBuffer(context, joinUrl(baseUrl, effectPath));
|
||||
if (!buffer) return null;
|
||||
try {
|
||||
const source = context.createBufferSource();
|
||||
source.buffer = buffer;
|
||||
if (playbackRate) source.playbackRate.value = playbackRate;
|
||||
const gain = context.createGain();
|
||||
gain.gain.value = clamp(volume, 0.8);
|
||||
source.connect(gain);
|
||||
gain.connect(context.destination);
|
||||
source.start();
|
||||
return true;
|
||||
} catch { return null; }
|
||||
}
|
||||
|
||||
async function playEffect(key, eventOptions = {}) {
|
||||
const { settings, definition, configured } = configFor(key);
|
||||
if (!settings.enabled || settings.muted || !configured.effect || !definition?.effect) return false;
|
||||
@@ -289,9 +330,9 @@ export function createRaceAudioEngine(options = {}) {
|
||||
const isPassing = key === "transponder_pass" || key === "transponder_detected";
|
||||
const volume = settings.masterVolume * (isPassing ? settings.passingVolume : settings.effectsVolume);
|
||||
const rate = settings.perCompetitorPassingTone && isPassing && eventOptions.variant ? variantPlaybackRate(eventOptions.variant) : undefined;
|
||||
const played = await attemptEffect(effectPath, volume, rate);
|
||||
const played = (isPassing && await attemptBufferedEffect(effectPath, volume, rate)) || await attemptEffect(effectPath, volume, rate);
|
||||
const fallbackRate = rate || (customPath && isPassing ? variantPlaybackRate(customPath) : undefined);
|
||||
if (!played && customPath && isPassing) return attemptEffect(DEFAULT_PASSING_SOUND, volume, fallbackRate);
|
||||
if (!played && customPath && isPassing) return (await attemptBufferedEffect(DEFAULT_PASSING_SOUND, volume, fallbackRate)) || attemptEffect(DEFAULT_PASSING_SOUND, volume, fallbackRate);
|
||||
return played;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user