Spaces:
Running
Running
File size: 4,219 Bytes
8800c81 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | // Web Audio API Synthesizer for tactile pencil strokes and glowing node chimes
class SoundEngine {
private ctx: AudioContext | null = null;
private enabled: boolean = true;
private initCtx() {
if (!this.ctx && typeof window !== 'undefined') {
const AudioCtx = window.AudioContext || (window as unknown as { webkitAudioContext: typeof AudioContext }).webkitAudioContext;
if (AudioCtx) {
this.ctx = new AudioCtx();
}
}
if (this.ctx && this.ctx.state === 'suspended') {
this.ctx.resume();
}
}
public setEnabled(val: boolean) {
this.enabled = val;
if (val) this.initCtx();
}
public isEnabled() {
return this.enabled;
}
// Soft luminous bell/crystal chime when a node lights up
public playNodeLightUp(nodeIndex: number, maxNodes: number = 150) {
if (!this.enabled) return;
this.initCtx();
if (!this.ctx) return;
try {
const now = this.ctx.currentTime;
// Musical pentatonic scale progression based on node index
const baseFreqs = [261.63, 293.66, 329.63, 392.00, 440.00, 523.25, 587.33, 659.25, 783.99, 880.00];
const safeMax = Math.max(1, maxNodes);
const scaleStep = Math.floor((nodeIndex / safeMax) * 20);
const octaveMultiplier = 1 + Math.floor(scaleStep / baseFreqs.length) * 0.5;
const baseFreq = baseFreqs[scaleStep % baseFreqs.length] * octaveMultiplier;
const osc = this.ctx.createOscillator();
const osc2 = this.ctx.createOscillator();
const gain = this.ctx.createGain();
osc.type = 'sine';
osc.frequency.setValueAtTime(baseFreq, now);
osc2.type = 'triangle';
osc2.frequency.setValueAtTime(baseFreq * 2.01, now);
gain.gain.setValueAtTime(0.001, now);
gain.gain.linearRampToValueAtTime(0.12, now + 0.02);
gain.gain.exponentialRampToValueAtTime(0.0001, now + 0.55);
osc.connect(gain);
osc2.connect(gain);
gain.connect(this.ctx.destination);
osc.start(now);
osc2.start(now);
osc.stop(now + 0.6);
osc2.stop(now + 0.6);
} catch {
// Audio context might fail silently in restricted environments
}
}
// Scratchy soft pencil graphite sound on step
public playPencilScratch() {
if (!this.enabled) return;
this.initCtx();
if (!this.ctx) return;
try {
const now = this.ctx.currentTime;
const bufferSize = Math.floor(this.ctx.sampleRate * 0.06); // 60ms noise burst
const buffer = this.ctx.createBuffer(1, bufferSize, this.ctx.sampleRate);
const data = buffer.getChannelData(0);
for (let i = 0; i < bufferSize; i++) {
data[i] = (Math.random() * 2 - 1) * Math.exp(-i / (bufferSize * 0.4));
}
const noise = this.ctx.createBufferSource();
noise.buffer = buffer;
const filter = this.ctx.createBiquadFilter();
filter.type = 'bandpass';
filter.frequency.setValueAtTime(1400 + Math.random() * 800, now);
filter.Q.setValueAtTime(3.5, now);
const gain = this.ctx.createGain();
gain.gain.setValueAtTime(0.04, now);
gain.gain.exponentialRampToValueAtTime(0.001, now + 0.05);
noise.connect(filter);
filter.connect(gain);
gain.connect(this.ctx.destination);
noise.start(now);
} catch {
// Ignore
}
}
// Triumphant chime when reaching the final milestone
public playVictoryChime() {
if (!this.enabled) return;
this.initCtx();
if (!this.ctx) return;
const chords = [523.25, 659.25, 783.99, 1046.50];
chords.forEach((freq, idx) => {
setTimeout(() => {
if (!this.ctx) return;
const now = this.ctx.currentTime;
const osc = this.ctx.createOscillator();
const gain = this.ctx.createGain();
osc.type = 'sine';
osc.frequency.setValueAtTime(freq, now);
gain.gain.setValueAtTime(0.001, now);
gain.gain.linearRampToValueAtTime(0.15, now + 0.05);
gain.gain.exponentialRampToValueAtTime(0.0001, now + 1.2);
osc.connect(gain);
gain.connect(this.ctx.destination);
osc.start(now);
osc.stop(now + 1.3);
}, idx * 120);
});
}
}
export const soundManager = new SoundEngine();
|