import { StorageService } from './StorageService'; declare const wx: any; interface ToneOptions { type?: OscillatorType; volume?: number; slideTo?: number | null; } export class SoundService { private ctx: any = null; private enabled: boolean; private bgmEnabled: boolean; private bgmTimer: any = null; private bgmStep = 0; private lastBlockAt = 0; // 舒缓手作五声八音盒旋律(C 大调五声:C5 D5 E5 G5 A5 C6) private readonly bgmNotes = [523.25, 659.25, 783.99, 659.25, 587.33, 659.25, 880.0, 783.99, 523.25, 587.33, 659.25, 440.0]; constructor(private readonly storage: StorageService) { this.enabled = storage.readSoundEnabled(); this.bgmEnabled = storage.readBgmEnabled(); } public isEnabled(): boolean { return this.enabled; } public isBgmEnabled(): boolean { return this.bgmEnabled; } public toggle(): boolean { this.enabled = !this.enabled; this.storage.saveSoundEnabled(this.enabled); if (this.enabled) { this.unlock(); this.playTone(620, 0.06, 0.02, { volume: 0.055 }); } return this.enabled; } public toggleBgm(): boolean { this.bgmEnabled = !this.bgmEnabled; this.storage.saveBgmEnabled(this.bgmEnabled); if (this.bgmEnabled) { this.unlock(); this.startBgmLoop(); } else { this.stopBgmLoop(); } return this.bgmEnabled; } private startBgmLoop(): void { if (!this.bgmEnabled || this.bgmTimer !== null) return; if (typeof setInterval !== 'function') return; this.bgmTimer = setInterval(() => { if (!this.bgmEnabled || !this.ctx || this.ctx.state !== 'running') return; const freq = this.bgmNotes[this.bgmStep % this.bgmNotes.length]; this.bgmStep++; this.playRawTone(freq, 0.42, 0, { type: 'sine', volume: 0.018 }); if (this.bgmStep % 4 === 1) { this.playRawTone(freq * 0.5, 0.72, 0.02, { type: 'triangle', volume: 0.011 }); } }, 920); } public stopBgmLoop(): void { if (this.bgmTimer !== null && typeof clearInterval === 'function') { clearInterval(this.bgmTimer); this.bgmTimer = null; } } public unlock(): void { if (!this.enabled && !this.bgmEnabled) return; if (!this.ctx) { if (typeof wx !== 'undefined' && typeof wx.createWebAudioContext === 'function') { this.ctx = wx.createWebAudioContext(); } else if (typeof window !== 'undefined') { const AudioCtx = (window as any).AudioContext || (window as any).webkitAudioContext; if (AudioCtx) this.ctx = new AudioCtx(); } } if (this.ctx && this.ctx.state === 'suspended' && typeof this.ctx.resume === 'function') { try { const res = this.ctx.resume(); if (res && typeof res.catch === 'function') res.catch(() => {}); } catch { // Ignore autoplay restrictions until next gesture. } } if (this.bgmEnabled) this.startBgmLoop(); } private playRawTone(freq: number, duration: number, delay = 0, opts: ToneOptions = {}): void { if (!this.ctx || this.ctx.state !== 'running') return; const { type = 'triangle', volume = 0.065, slideTo = null } = opts; try { const osc = this.ctx.createOscillator(); const gain = this.ctx.createGain(); const start = this.ctx.currentTime + delay; osc.type = type; osc.frequency.setValueAtTime(freq, start); if (slideTo) osc.frequency.exponentialRampToValueAtTime(slideTo, start + duration); gain.gain.setValueAtTime(0.0008, start); gain.gain.exponentialRampToValueAtTime(volume, start + 0.012); gain.gain.exponentialRampToValueAtTime(0.0008, start + duration + 0.035); osc.connect(gain); gain.connect(this.ctx.destination); osc.start(start); osc.stop(start + duration + 0.04); } catch { // Ignore transient WebAudio errors. } } private playTone(freq: number, duration: number, delay = 0, opts: ToneOptions = {}): void { if (!this.enabled) return; this.playRawTone(freq, duration, delay, opts); } public pick(count: number): void { this.playTone(count > 1 ? 659 : 523, 0.065, 0, { volume: 0.06 }); } public mark(count: number): void { this.playTone(count > 1 ? 880 : 740, 0.06, 0, { volume: 0.055 }); } public pickup(): void { this.playTone(494, 0.06, 0, { volume: 0.05 }); } public deliver(): void { this.playTone(659, 0.085, 0, { volume: 0.065 }); this.playTone(988, 0.09, 0.09, { volume: 0.06 }); } public block(): void { const now = Date.now(); if (now - this.lastBlockAt < 320) return; this.lastBlockAt = now; this.playTone(230, 0.09, 0, { type: 'sine', volume: 0.055, slideTo: 145 }); } public undo(): void { this.playTone(440, 0.06, 0, { volume: 0.045, slideTo: 330 }); } public bonus(): void { this.playTone(698, 0.07, 0, { volume: 0.055, slideTo: 932 }); } public dye(): void { this.playTone(587, 0.08, 0, { type: 'sine', volume: 0.06, slideTo: 880 }); } /** 「拆旧织新」抽线解压音效:轻快上升丝线抽拉 + 卷球落碗啵声 */ public unspool(): void { this.playTone(392, 0.065, 0, { type: 'triangle', volume: 0.045, slideTo: 523 }); this.playTone(523, 0.075, 0.06, { type: 'triangle', volume: 0.048, slideTo: 659 }); this.playTone(784, 0.13, 0.13, { type: 'sine', volume: 0.06, slideTo: 988 }); } public button(): void { this.playTone(784, 0.055, 0, { volume: 0.05 }); } public win(): void { [523, 659, 784, 1046].forEach((freq, i) => this.playTone(freq, 0.14, i * 0.085, { volume: 0.065 })); } }