Files
recreopay/public/js/sounds.js
T

153 lines
4.0 KiB
JavaScript

// Sintetizador Web Audio API para feedback sonoro en tiempo real sin dependencias externas
class SoundEffects {
constructor() {
this.ctx = null;
}
init() {
if (!this.ctx) {
const AudioCtx = window.AudioContext || window.webkitAudioContext;
this.ctx = new AudioCtx();
}
if (this.ctx.state === 'suspended') {
this.ctx.resume();
}
}
// Sonido de escaneo exitoso / cobro completado (Tono alegre ascendente)
playSuccess() {
try {
this.init();
const now = this.ctx.currentTime;
const osc1 = this.ctx.createOscillator();
const osc2 = this.ctx.createOscillator();
const gain = this.ctx.createGain();
osc1.type = 'triangle';
osc2.type = 'sine';
// Acorde alegre de confirmación: C5 -> E5 -> G5
osc1.frequency.setValueAtTime(523.25, now);
osc1.frequency.exponentialRampToValueAtTime(659.25, now + 0.1);
osc1.frequency.exponentialRampToValueAtTime(783.99, now + 0.2);
osc2.frequency.setValueAtTime(261.63, now);
osc2.frequency.setValueAtTime(329.63, now + 0.1);
osc2.frequency.setValueAtTime(392.00, now + 0.2);
gain.gain.setValueAtTime(0.2, now);
gain.gain.exponentialRampToValueAtTime(0.01, now + 0.35);
osc1.connect(gain);
osc2.connect(gain);
gain.connect(this.ctx.destination);
osc1.start(now);
osc2.start(now);
osc1.stop(now + 0.35);
osc2.stop(now + 0.35);
} catch (e) {
console.warn('Audio no disponible:', e);
}
}
// Sonido de moneda para Modo Kids
playCoin() {
try {
this.init();
const now = this.ctx.currentTime;
const osc = this.ctx.createOscillator();
const gain = this.ctx.createGain();
osc.type = 'sine';
osc.frequency.setValueAtTime(987.77, now); // B5
osc.frequency.setValueAtTime(1318.51, now + 0.08); // E6
gain.gain.setValueAtTime(0.25, now);
gain.gain.exponentialRampToValueAtTime(0.01, now + 0.3);
osc.connect(gain);
gain.connect(this.ctx.destination);
osc.start(now);
osc.stop(now + 0.3);
} catch (e) {}
}
// Chirp de escáner láser de código de barras
playScanChirp() {
try {
this.init();
const now = this.ctx.currentTime;
const osc = this.ctx.createOscillator();
const gain = this.ctx.createGain();
osc.type = 'square';
osc.frequency.setValueAtTime(2000, now);
osc.frequency.exponentialRampToValueAtTime(1200, now + 0.06);
gain.gain.setValueAtTime(0.15, now);
gain.gain.exponentialRampToValueAtTime(0.01, now + 0.07);
osc.connect(gain);
gain.connect(this.ctx.destination);
osc.start(now);
osc.stop(now + 0.07);
} catch (e) {}
}
// Sonido de error / límite superado (Tono grave de advertencia)
playError() {
try {
this.init();
const now = this.ctx.currentTime;
const osc = this.ctx.createOscillator();
const gain = this.ctx.createGain();
osc.type = 'sawtooth';
osc.frequency.setValueAtTime(220, now);
osc.frequency.setValueAtTime(180, now + 0.15);
gain.gain.setValueAtTime(0.2, now);
gain.gain.exponentialRampToValueAtTime(0.01, now + 0.35);
osc.connect(gain);
gain.connect(this.ctx.destination);
osc.start(now);
osc.stop(now + 0.35);
} catch (e) {}
}
// Sonido de clic / tap sutil
playTap() {
try {
this.init();
const now = this.ctx.currentTime;
const osc = this.ctx.createOscillator();
const gain = this.ctx.createGain();
osc.type = 'sine';
osc.frequency.setValueAtTime(600, now);
osc.frequency.exponentialRampToValueAtTime(300, now + 0.04);
gain.gain.setValueAtTime(0.08, now);
gain.gain.exponentialRampToValueAtTime(0.001, now + 0.04);
osc.connect(gain);
gain.connect(this.ctx.destination);
osc.start(now);
osc.stop(now + 0.04);
} catch (e) {}
}
// Sonido de escaneo / beep
playBeep() {
this.playScanChirp();
}
}
window.sounds = new SoundEffects();