/
VictoriaKoltsova
/
AI_2026_Module2_Project2
Обзор
Документация
Войти
/
VictoriaKoltsova
/
AI_2026_Module2_Project2
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
js/audio.js
109 строк
4 KB
moskoltsova2009-ai
Build UK Quest game platform
09 авг 2026, 23:28
09 авг 2026, 23:28
54234c0
Код
Авторство
О чём код?
(() => { const { loadProgress, updateProgress } = window.UKQuestStorage; let context; let ambientTimer; function getContext() { if (!context) { const AudioContext = window.AudioContext || window.webkitAudioContext; if (AudioContext) context = new AudioContext(); } return context; } function tone(frequency, duration = 0.08, type = "sine", volume = 0.035, delay = 0) { if (!loadProgress().soundEnabled) return; const audio = getContext(); if (!audio) return; const start = audio.currentTime + delay; const oscillator = audio.createOscillator(); const gain = audio.createGain(); oscillator.type = type; oscillator.frequency.setValueAtTime(frequency, start); gain.gain.setValueAtTime(0.0001, start); gain.gain.exponentialRampToValueAtTime(volume, start + 0.01); gain.gain.exponentialRampToValueAtTime(0.0001, start + duration); oscillator.connect(gain).connect(audio.destination); oscillator.start(start); oscillator.stop(start + duration + 0.02); } const patterns = { click: () => tone(310, 0.06, "triangle", 0.025), letter: () => tone(520, 0.05, "sine", 0.025), correct: () => { tone(520, 0.12); tone(780, 0.18, "sine", 0.04, 0.08); }, wrong: () => { tone(180, 0.11, "sawtooth", 0.025); tone(135, 0.14, "sawtooth", 0.02, 0.08); }, bonus: () => { tone(660, 0.1, "triangle"); tone(880, 0.14, "triangle", 0.035, 0.07); }, level: () => [392, 523, 659, 784].forEach((note, i) => tone(note, 0.24, "sine", 0.04, i * 0.1)), achievement: () => [740, 988].forEach((note, i) => tone(note, 0.28, "triangle", 0.035, i * 0.14)), lane: () => { tone(260, 0.045, "sine", 0.018); tone(390, 0.07, "triangle", 0.02, 0.03); }, collision: () => { tone(95, 0.18, "sawtooth", 0.05); tone(62, 0.22, "square", 0.025, 0.04); }, splash: () => { tone(170, 0.12, "sine", 0.025); tone(120, 0.2, "sawtooth", 0.015, 0.04); }, coin: () => { tone(880, 0.07, "square", 0.025); tone(1175, 0.1, "triangle", 0.025, 0.05); }, turbo: () => [220, 330, 495].forEach((note, i) => tone(note, 0.15, "sawtooth", 0.018, i * 0.045)), warning: () => { tone(420, 0.1, "square", 0.022); tone(420, 0.1, "square", 0.022, 0.16); }, boost: () => [330, 440, 660, 880].forEach((note, i) => tone(note, 0.22, "sawtooth", 0.022, i * 0.055)), pit: () => { tone(150, 0.25, "triangle", 0.024); tone(220, 0.3, "triangle", 0.02, 0.18); }, finish: () => [392, 523, 659, 784].forEach((note, i) => tone(note, 0.24, "triangle", 0.035, i * 0.09)), victory: () => [392, 494, 587, 784, 988].forEach((note, i) => tone(note, 0.32, "triangle", 0.04, i * 0.11)), }; function playSound(name) { patterns[name]?.(); } async function primeAudio() { const audio = getContext(); if (audio?.state === "suspended") await audio.resume(); if (loadProgress().soundEnabled) startAmbient(); } function startAmbient() { if (ambientTimer || !loadProgress().soundEnabled) return; const pulse = () => { tone(110, 1.8, "sine", 0.006); tone(164.8, 1.4, "sine", 0.004, 0.25); }; pulse(); ambientTimer = window.setInterval(pulse, 4200); } function stopAmbient() { window.clearInterval(ambientTimer); ambientTimer = undefined; } function toggleSound() { const enabled = !loadProgress().soundEnabled; updateProgress((progress) => { progress.soundEnabled = enabled; }); if (enabled) { primeAudio(); playSound("click"); } else { stopAmbient(); } return enabled; } function bindAudioControls(root = document) { root.querySelectorAll("[data-sound-toggle]").forEach((button) => { const render = () => { const enabled = loadProgress().soundEnabled; button.textContent = enabled ? "SOUND ON" : "SOUND OFF"; button.setAttribute("aria-pressed", String(enabled)); }; render(); button.addEventListener("click", () => { toggleSound(); render(); }); }); root.addEventListener("click", (event) => { if (event.target.closest("button, a")) { primeAudio(); if (!event.target.closest("[data-sound-toggle]")) playSound("click"); } }); } window.UKQuestAudio = { playSound, primeAudio, startAmbient, stopAmbient, toggleSound, bindAudioControls }; })();