1-
21class SoundManager {
32 constructor ( ) {
43 this . ctx = null ;
@@ -12,7 +11,7 @@ class SoundManager {
1211 this . ctx = new AudioContext ( ) ;
1312 }
1413 }
15- // Resume context if it's suspended (browsers auto-suspend)
14+
1615 if ( this . ctx && this . ctx . state === "suspended" ) {
1716 this . ctx . resume ( ) ;
1817 }
@@ -24,72 +23,34 @@ class SoundManager {
2423 return this . enabled ;
2524 }
2625
27- // Helper to play a tone
28- playTone ( freq , type , duration , volume = 0.1 ) {
26+ blip ( freq , dur ) {
2927 if ( ! this . enabled ) return ;
3028 this . init ( ) ;
3129 if ( ! this . ctx ) return ;
3230
33- const osc = this . ctx . createOscillator ( ) ;
34- const gain = this . ctx . createGain ( ) ;
35-
36- osc . type = type ;
37- osc . frequency . setValueAtTime ( freq , this . ctx . currentTime ) ;
38-
39- gain . gain . setValueAtTime ( volume , this . ctx . currentTime ) ;
40- gain . gain . exponentialRampToValueAtTime ( 0.01 , this . ctx . currentTime + duration ) ;
41-
42- osc . connect ( gain ) ;
43- gain . connect ( this . ctx . destination ) ;
44-
45- osc . start ( ) ;
46- osc . stop ( this . ctx . currentTime + duration ) ;
31+ const o = this . ctx . createOscillator ( ) ;
32+ const g = this . ctx . createGain ( ) ;
33+ o . type = "sine" ;
34+ o . frequency . value = freq ;
35+ o . connect ( g ) ;
36+ g . connect ( this . ctx . destination ) ;
37+ g . gain . setValueAtTime ( 0.0001 , this . ctx . currentTime ) ;
38+ g . gain . exponentialRampToValueAtTime ( 0.2 , this . ctx . currentTime + 0.005 ) ;
39+ g . gain . exponentialRampToValueAtTime ( 0.0001 , this . ctx . currentTime + dur ) ;
40+ o . start ( ) ;
41+ o . stop ( this . ctx . currentTime + dur ) ;
4742 }
4843
4944 playSent ( ) {
50- // Satisfying "pop" or "click" for sending
51- // Sine wave starting at 600Hz dropping fast
52- if ( ! this . enabled ) return ;
53- this . playTone ( 600 , "sine" , 0.15 , 0.1 ) ;
45+ this . blip ( 832 , 0.05 ) ;
5446 }
5547
5648 playReceived ( ) {
57- // Soft "bloop" for receiving
58- // Sine wave starting at 400Hz
59- if ( ! this . enabled ) return ;
60- this . playTone ( 400 , "sine" , 0.15 , 0.1 ) ;
49+ this . blip ( 624 , 0.05 ) ;
6150 }
6251
6352 playWhisper ( ) {
64- // Distinct "ding" for whispers
65- // Two tones slightly separated
66- if ( ! this . enabled ) return ;
67- this . init ( ) ;
68- if ( ! this . ctx ) return ;
69-
70- const now = this . ctx . currentTime ;
71-
72- // Tone 1
73- const osc1 = this . ctx . createOscillator ( ) ;
74- const gain1 = this . ctx . createGain ( ) ;
75- osc1 . frequency . setValueAtTime ( 800 , now ) ;
76- gain1 . gain . setValueAtTime ( 0.1 , now ) ;
77- gain1 . gain . exponentialRampToValueAtTime ( 0.01 , now + 0.3 ) ;
78- osc1 . connect ( gain1 ) ;
79- gain1 . connect ( this . ctx . destination ) ;
80- osc1 . start ( now ) ;
81- osc1 . stop ( now + 0.3 ) ;
82-
83- // Tone 2 (higher)
84- const osc2 = this . ctx . createOscillator ( ) ;
85- const gain2 = this . ctx . createGain ( ) ;
86- osc2 . frequency . setValueAtTime ( 1200 , now + 0.1 ) ;
87- gain2 . gain . setValueAtTime ( 0.1 , now + 0.1 ) ;
88- gain2 . gain . exponentialRampToValueAtTime ( 0.01 , now + 0.4 ) ;
89- osc2 . connect ( gain2 ) ;
90- gain2 . connect ( this . ctx . destination ) ;
91- osc2 . start ( now + 0.1 ) ;
92- osc2 . stop ( now + 0.4 ) ;
53+ this . blip ( 1872 , 1.5 ) ;
9354 }
9455}
9556
0 commit comments