|
| 1 | +class AudioManager { |
| 2 | + static instance = null; |
| 3 | + |
| 4 | + static getInstance() { |
| 5 | + if (!AudioManager.instance) { |
| 6 | + AudioManager.instance = new AudioManager(); |
| 7 | + } |
| 8 | + return AudioManager.instance; |
| 9 | + } |
| 10 | + |
| 11 | + constructor() { |
| 12 | + if (AudioManager.instance) { |
| 13 | + return AudioManager.instance; |
| 14 | + } |
| 15 | + AudioManager.instance = this; |
| 16 | + |
| 17 | + this.context = new (window.AudioContext || window.webkitAudioContext)(); |
| 18 | + this.masterGain = this.context.createGain(); |
| 19 | + this.musicGain = this.context.createGain(); |
| 20 | + this.fxGain = this.context.createGain(); |
| 21 | + |
| 22 | + // Connect gains to master |
| 23 | + this.musicGain.connect(this.masterGain); |
| 24 | + this.fxGain.connect(this.masterGain); |
| 25 | + |
| 26 | + // Connect master to speakers by default |
| 27 | + this.masterGain.connect(this.context.destination); |
| 28 | + |
| 29 | + this.sounds = new Map(); |
| 30 | + this.music = new Map(); |
| 31 | + |
| 32 | + // Set default volumes |
| 33 | + this.masterGain.gain.value = 1.0; |
| 34 | + this.musicGain.gain.value = 0.5; // Lower music volume |
| 35 | + this.fxGain.gain.value = 0.7; // Slightly lower FX volume |
| 36 | + |
| 37 | + // Add initialization state |
| 38 | + this.isInitialized = false; |
| 39 | + } |
| 40 | + |
| 41 | + createAudioNodes(source, config = {}) { |
| 42 | + const gainNode = this.context.createGain(); |
| 43 | + const panNode = this.context.createStereoPanner(); |
| 44 | + |
| 45 | + // Configure nodes with provided values or defaults |
| 46 | + gainNode.gain.value = config.volume ?? 1; |
| 47 | + panNode.pan.value = config.pan ?? 0; |
| 48 | + |
| 49 | + // Connect nodes |
| 50 | + source.connect(panNode); |
| 51 | + panNode.connect(gainNode); |
| 52 | + gainNode.connect(this.fxGain); |
| 53 | + |
| 54 | + // Apply pitch if specified |
| 55 | + if (config.pitch !== undefined) { |
| 56 | + source.playbackRate.value = config.pitch; |
| 57 | + } |
| 58 | + |
| 59 | + // Apply decay if specified |
| 60 | + if (config.decay > 0) { |
| 61 | + const startTime = this.context.currentTime; |
| 62 | + gainNode.gain.setValueAtTime(config.volume ?? 1, startTime); |
| 63 | + gainNode.gain.exponentialRampToValueAtTime(0.001, startTime + config.decay); |
| 64 | + } |
| 65 | + |
| 66 | + return { gainNode, panNode }; |
| 67 | + } |
| 68 | + |
| 69 | + async loadSound(key, url) { |
| 70 | + const response = await fetch(url); |
| 71 | + const arrayBuffer = await response.arrayBuffer(); |
| 72 | + const audioBuffer = await this.context.decodeAudioData(arrayBuffer); |
| 73 | + this.sounds.set(key, audioBuffer); |
| 74 | + } |
| 75 | + |
| 76 | + async loadMusic(key, url) { |
| 77 | + const response = await fetch(url); |
| 78 | + const arrayBuffer = await response.arrayBuffer(); |
| 79 | + const audioBuffer = await this.context.decodeAudioData(arrayBuffer); |
| 80 | + this.music.set(key, audioBuffer); |
| 81 | + } |
| 82 | + |
| 83 | + async preloadGameSounds(musicTracks = []) { |
| 84 | + try { |
| 85 | + console.log('Loading game sounds...'); |
| 86 | + |
| 87 | + // Load standard game sounds |
| 88 | + const standardSounds = [ |
| 89 | + this.loadSound('explosion', './audio/explosion.mp3'), |
| 90 | + this.loadSound('laser', './audio/player-shoot.wav'), |
| 91 | + this.loadSound('alien-laser', './audio/alien-shoot.mp3') |
| 92 | + ]; |
| 93 | + |
| 94 | + // Load music tracks |
| 95 | + const musicLoading = musicTracks.map((track, index) => { |
| 96 | + return this.loadMusic(`track${index}`, track) |
| 97 | + .then(() => console.log(`Loaded music track: ${track.split('/').pop()}`)); |
| 98 | + }); |
| 99 | + |
| 100 | + await Promise.all([...standardSounds, ...musicLoading]); |
| 101 | + |
| 102 | + this.isInitialized = true; |
| 103 | + console.log('All sounds loaded successfully'); |
| 104 | + this.checkAudioState(); |
| 105 | + |
| 106 | + } catch (error) { |
| 107 | + console.error('Failed to load sounds:', error); |
| 108 | + throw error; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + playSound(key, config = {}) { |
| 113 | + if (!this.isInitialized) { |
| 114 | + console.warn('Attempted to play sound before initialization:', key); |
| 115 | + return null; |
| 116 | + } |
| 117 | + |
| 118 | + const buffer = this.sounds.get(key); |
| 119 | + if (!buffer) { |
| 120 | + console.warn(`Sound not found: ${key}`); |
| 121 | + return null; |
| 122 | + } |
| 123 | + |
| 124 | + try { |
| 125 | + const source = this.context.createBufferSource(); |
| 126 | + source.buffer = buffer; |
| 127 | + |
| 128 | + // Create and connect audio processing nodes |
| 129 | + const nodes = this.createAudioNodes(source, config); |
| 130 | + |
| 131 | + source.start(0); |
| 132 | + console.log(`Playing sound: ${key} with config:`, config); |
| 133 | + |
| 134 | + return { source, ...nodes }; |
| 135 | + } catch (error) { |
| 136 | + console.error(`Error playing sound ${key}:`, error); |
| 137 | + return null; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + playMusic(key, loop = true) { |
| 142 | + const buffer = this.music.get(key); |
| 143 | + if (buffer) { |
| 144 | + const source = this.context.createBufferSource(); |
| 145 | + source.buffer = buffer; |
| 146 | + source.loop = loop; |
| 147 | + source.connect(this.musicGain); |
| 148 | + source.start(0); |
| 149 | + return source; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + connectToDestination(destination) { |
| 154 | + // Disconnect from speakers |
| 155 | + this.masterGain.disconnect(); |
| 156 | + // Connect to new destination |
| 157 | + this.masterGain.connect(destination); |
| 158 | + } |
| 159 | + |
| 160 | + reconnectToSpeakers() { |
| 161 | + // Reconnect to speakers |
| 162 | + this.masterGain.disconnect(); |
| 163 | + this.masterGain.connect(this.context.destination); |
| 164 | + } |
| 165 | + |
| 166 | + // Add debug method |
| 167 | + checkAudioState() { |
| 168 | + console.log({ |
| 169 | + contextState: this.context.state, |
| 170 | + masterVolume: this.masterGain.gain.value, |
| 171 | + fxVolume: this.fxGain.gain.value, |
| 172 | + musicVolume: this.musicGain.gain.value, |
| 173 | + loadedSounds: Array.from(this.sounds.keys()), |
| 174 | + loadedMusic: Array.from(this.music.keys()) |
| 175 | + }); |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +export default AudioManager; |
0 commit comments