UNPKG

yukinovel

Version:

Yukinovel is a simple web visual novel engine.

111 lines (110 loc) 3.03 kB
import howlerModule from 'howler'; // Try to import Howler, fallback to mock if not available let Howl; let Howler; try { Howl = howlerModule.Howl; Howler = howlerModule.Howler; } catch (e) { // Mock implementation for development Howl = class MockHowl { constructor(options) { console.log('Mock Howl created with options:', options); } play() { console.log('Mock: play audio'); } stop() { console.log('Mock: stop audio'); } pause() { console.log('Mock: pause audio'); } volume(vol) { if (vol !== undefined) { console.log('Mock: set volume to', vol); return this; } return 1; } unload() { console.log('Mock: unload audio'); } }; Howler = { volume: (vol) => console.log('Mock: set global volume to', vol) }; } export class AudioManager { constructor() { this.currentMusic = null; this.soundEffects = new Map(); this.musicVolume = 0.7; this.sfxVolume = 0.8; Howler.volume(1.0); } // Music management playMusic(src, loop = true) { if (this.currentMusic) { this.currentMusic.stop(); } this.currentMusic = new Howl({ src: [src], loop, volume: this.musicVolume, autoplay: true, onloaderror: (id, error) => { console.warn(`Failed to load music: ${src}`, error); } }); } stopMusic() { if (this.currentMusic) { this.currentMusic.stop(); this.currentMusic = null; } } pauseMusic() { if (this.currentMusic) { this.currentMusic.pause(); } } resumeMusic() { if (this.currentMusic) { this.currentMusic.play(); } } // Sound effects playSound(src) { let sound = this.soundEffects.get(src); if (!sound) { sound = new Howl({ src: [src], volume: this.sfxVolume, onloaderror: (id, error) => { console.warn(`Failed to load sound: ${src}`, error); } }); this.soundEffects.set(src, sound); } sound.play(); } // Volume control setMusicVolume(volume) { this.musicVolume = Math.max(0, Math.min(1, volume)); if (this.currentMusic) { this.currentMusic.volume(this.musicVolume); } } setSfxVolume(volume) { this.sfxVolume = Math.max(0, Math.min(1, volume)); this.soundEffects.forEach(sound => { sound.volume(this.sfxVolume); }); } getMusicVolume() { return this.musicVolume; } getSfxVolume() { return this.sfxVolume; } // Cleanup dispose() { this.stopMusic(); this.soundEffects.forEach(sound => sound.unload()); this.soundEffects.clear(); } }