Split everything up into dynamically loaded modules.
This commit is contained in:
+103
-6
@@ -2,11 +2,63 @@
|
||||
* AudioManager Module
|
||||
* Manages loading and playback of non-TTS audio effects triggered by tags.
|
||||
*/
|
||||
export class AudioManager {
|
||||
import { BaseModule } from './base-module.js';
|
||||
import { moduleRegistry } from './module-registry.js';
|
||||
|
||||
class AudioManagerModule extends BaseModule {
|
||||
constructor() {
|
||||
super('audio-manager', 'Audio Manager');
|
||||
this.sounds = new Map();
|
||||
this.currentAudio = null;
|
||||
this.currentLoop = null;
|
||||
this.masterVolume = 1.0;
|
||||
this.musicVolume = 1.0;
|
||||
this.sfxVolume = 1.0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load module dependencies
|
||||
* @returns {Promise} - Resolves when dependencies are loaded
|
||||
*/
|
||||
async loadDependencies() {
|
||||
try {
|
||||
this.reportProgress(40, "Initializing audio system");
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error("Error loading AudioManager dependencies:", error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the module
|
||||
* @returns {Promise<boolean>} - Resolves with success status
|
||||
*/
|
||||
async initialize() {
|
||||
try {
|
||||
// Set up audio context if needed
|
||||
this.setupAudioContext();
|
||||
|
||||
// Load some basic sound effects
|
||||
this.reportProgress(80, "Loading sound effects");
|
||||
|
||||
this.reportProgress(100, "Audio system ready");
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error("Error initializing AudioManager:", error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up Web Audio API context if needed
|
||||
*/
|
||||
setupAudioContext() {
|
||||
// Only create if needed for advanced audio features
|
||||
if (typeof AudioContext !== 'undefined' || typeof webkitAudioContext !== 'undefined') {
|
||||
const AudioContextClass = window.AudioContext || window.webkitAudioContext;
|
||||
this.audioContext = new AudioContextClass();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -132,20 +184,53 @@ export class AudioManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the volume for all sounds
|
||||
* Set the master volume for all sounds
|
||||
* @param {number} volume - The volume level (0.0 to 1.0)
|
||||
*/
|
||||
setVolume(volume) {
|
||||
setMasterVolume(volume) {
|
||||
this.masterVolume = Math.max(0, Math.min(1, volume));
|
||||
this.updateVolumes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the music volume
|
||||
* @param {number} volume - The volume level (0.0 to 1.0)
|
||||
*/
|
||||
setMusicVolume(volume) {
|
||||
this.musicVolume = Math.max(0, Math.min(1, volume));
|
||||
// Apply to current loop if it exists
|
||||
if (this.currentLoop) {
|
||||
this.currentLoop.volume = this.masterVolume * this.musicVolume;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the sound effects volume
|
||||
* @param {number} volume - The volume level (0.0 to 1.0)
|
||||
*/
|
||||
setSfxVolume(volume) {
|
||||
this.sfxVolume = Math.max(0, Math.min(1, volume));
|
||||
// Apply to current non-loop audio if it exists
|
||||
if (this.currentAudio) {
|
||||
this.currentAudio.volume = this.masterVolume * this.sfxVolume;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update all volume levels based on current settings
|
||||
*/
|
||||
updateVolumes() {
|
||||
this.sounds.forEach(audio => {
|
||||
audio.volume = volume;
|
||||
const isMusic = audio.loop;
|
||||
audio.volume = this.masterVolume * (isMusic ? this.musicVolume : this.sfxVolume);
|
||||
});
|
||||
|
||||
if (this.currentAudio) {
|
||||
this.currentAudio.volume = volume;
|
||||
this.currentAudio.volume = this.masterVolume * this.sfxVolume;
|
||||
}
|
||||
|
||||
if (this.currentLoop) {
|
||||
this.currentLoop.volume = volume;
|
||||
this.currentLoop.volume = this.masterVolume * this.musicVolume;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -191,3 +276,15 @@ export class AudioManager {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Create the singleton instance
|
||||
const AudioManager = new AudioManagerModule();
|
||||
|
||||
// Register with the module registry
|
||||
moduleRegistry.register(AudioManager);
|
||||
|
||||
// Export the module
|
||||
export { AudioManager };
|
||||
|
||||
// Keep a reference in window for loader system
|
||||
window.AudioManager = AudioManager;
|
||||
|
||||
Reference in New Issue
Block a user