MCPcopy Create free account
hub / github.com/dashrobotco/robot-components / SoundEffects

Class SoundEffects

src/utils/SoundEffects.ts:5–256  ·  view source on GitHub ↗

* Sound effects utility for UI interactions

Source from the content-addressed store, hash-verified

3 */
4
5class SoundEffects {
6 private audioContext: AudioContext | null = null;
7 private audioBuffer: AudioBuffer | null = null;
8 private hoverFx2Buffer: AudioBuffer | null = null;
9 private isLoading = false;
10 private isLoadingHoverFx2 = false;
11 private isEnabledCallback: (() => boolean) | null = null;
12
13 constructor() {
14 // Initialize on first user interaction
15 this.initializeOnUserInteraction();
16 }
17
18 private initializeOnUserInteraction() {
19 if (typeof window === 'undefined' || typeof document === 'undefined') {
20 return;
21 }
22
23 const initialize = () => {
24 if (!this.audioContext) {
25 this.audioContext = new (window.AudioContext || (window as unknown as { webkitAudioContext: typeof AudioContext }).webkitAudioContext)();
26 this.loadSound('/hover3.mp3');
27 this.loadHoverFx2('/hoverfx2.mp3');
28 }
29 };
30
31 document.addEventListener('mousedown', initialize, { once: true });
32 document.addEventListener('keydown', initialize, { once: true });
33 document.addEventListener('mouseover', initialize, { once: true });
34 }
35
36 private async loadSound(url: string): Promise<void> {
37 if (this.isLoading || this.audioBuffer) return;
38
39 this.isLoading = true;
40 try {
41 const response = await fetch(url);
42 const arrayBuffer = await response.arrayBuffer();
43 if (this.audioContext) {
44 this.audioBuffer = await this.audioContext.decodeAudioData(arrayBuffer);
45 }
46 } catch (error) {
47 console.warn('Failed to load sound:', error);
48 } finally {
49 this.isLoading = false;
50 }
51 }
52
53 private async loadHoverFx2(url: string): Promise<void> {
54 if (this.isLoadingHoverFx2 || this.hoverFx2Buffer) return;
55
56 this.isLoadingHoverFx2 = true;
57 try {
58 const response = await fetch(url);
59 const arrayBuffer = await response.arrayBuffer();
60 if (this.audioContext) {
61 this.hoverFx2Buffer = await this.audioContext.decodeAudioData(arrayBuffer);
62 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected