Spaces:
Running
Running
Download algospaced-ui/src/utils/audio.ts from iamfebin/algospaced-dsa: direct link, hf CLI and curl.
- Browser
- Download file 4.22 kB
-
https://huggingface.co/spaces/iamfebin/algospaced-dsa/resolve/main/algospaced-ui/src/utils/audio.ts
- Command line
-
hf download hf://spaces/iamfebin/algospaced-dsa/algospaced-ui/src/utils/audio.ts
-
curl -L -o audio.ts https://huggingface.co/spaces/iamfebin/algospaced-dsa/resolve/main/algospaced-ui/src/utils/audio.ts
4.22 kB
| // 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(); | |