测试
// 創建 AudioContext (處理所有音效) const audioCtx = new (window.AudioContext || window.webkitAudioContext)(); // 預設音量控制:控制整體音量不要太大聲 (0.3 為適中偏小) const masterGain = audioCtx.createGain(); masterGain.gain.value = 0.3; masterGain.connect(audioCtx.destination); // ========================================== // 1. 閃電聲音效 (使用白噪音 + 快速衰減,模擬雷擊) // ========================================== function playLightningSound() { const bufferSize = audioCtx.sampleRate * 1.0; // 1秒長度 const buffer = audioCtx.createBuffer(1, bufferSize, audioCtx.sampleRate); const data = buffer.getChannelData(0); // 生成白噪音 for (let i = 0; i < bufferSize; i++) { data[i] = Math.random() * 2 - 1; } const noise = audioCtx.createBufferSource(); noise.buffer = buffer; const filter = audioCtx.createBiquadFilter(); filter.type = 'lowpass'; filter.frequency.value = 1000; // 低通濾波,保留悶雷聲 const gainNode = audioCtx.createGain(); noise.connect(...