File size: 4,160 Bytes
8800c81
 
 
 
664a5eb
 
 
 
8800c81
 
 
664a5eb
 
8800c81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
664a5eb
 
 
8800c81
 
 
664a5eb
8800c81
664a5eb
 
8800c81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
664a5eb
 
 
8800c81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
664a5eb
 
 
8800c81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
664a5eb
 
 
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Web Audio API Synthesizer for Retro RPG Dialogue, Chimes, and Fanfares

let audioCtx: AudioContext | null = null;

type CustomWindow = Window & typeof globalThis & {
  webkitAudioContext?: typeof AudioContext;
};

function getAudioContext(): AudioContext | null {
  if (typeof window === 'undefined') return null;
  if (!audioCtx) {
    const customWin = window as CustomWindow;
    const AudioContextClass = window.AudioContext || customWin.webkitAudioContext;
    if (AudioContextClass) {
      audioCtx = new AudioContextClass();
    }
  }
  if (audioCtx && audioCtx.state === 'suspended') {
    audioCtx.resume().catch(() => {});
  }
  return audioCtx;
}

export function isAudioMuted(): boolean {
  try {
    const saved = localStorage.getItem('companion_sound_enabled');
    return saved === 'false';
  } catch {
    return false;
  }
}

export function setAudioMuted(muted: boolean): void {
  try {
    localStorage.setItem('companion_sound_enabled', muted ? 'false' : 'true');
  } catch {
    // Graceful fallback if localStorage is disabled
  }
}

/**
 * Typewriter dialogue character blip (disabled)
 */
export function playTypewriterBlip(_pitchVariation: number = 0): void {
  // Disabled: typewriter blip sound removed
}

/**
 * Playful RPG character pop-up chime (C5 -> E5 -> G5 -> C6)
 */
export function playPopupChime(): void {
  if (isAudioMuted()) return;
  const ctx = getAudioContext();
  if (!ctx) return;

  try {
    const notes = [523.25, 659.25, 783.99, 1046.5]; // C5, E5, G5, C6
    const startTime = ctx.currentTime;

    notes.forEach((freq, idx) => {
      const osc = ctx.createOscillator();
      const gain = ctx.createGain();

      osc.type = 'sine';
      osc.frequency.setValueAtTime(freq, startTime + idx * 0.06);

      gain.gain.setValueAtTime(0.06, startTime + idx * 0.06);
      gain.gain.exponentialRampToValueAtTime(0.001, startTime + idx * 0.06 + 0.18);

      osc.connect(gain);
      gain.connect(ctx.destination);

      osc.start(startTime + idx * 0.06);
      osc.stop(startTime + idx * 0.06 + 0.2);
    });
  } catch {
    // Ignore audio errors gracefully
  }
}

/**
 * Gentle break reminder tone (Warm harp-like interval)
 */
export function playBreakReminderTone(): void {
  if (isAudioMuted()) return;
  const ctx = getAudioContext();
  if (!ctx) return;

  try {
    const notes = [440.0, 554.37, 659.25, 880.0]; // A4, C#5, E5, A5
    const startTime = ctx.currentTime;

    notes.forEach((freq, idx) => {
      const osc = ctx.createOscillator();
      const gain = ctx.createGain();

      osc.type = 'triangle';
      osc.frequency.setValueAtTime(freq, startTime + idx * 0.09);

      gain.gain.setValueAtTime(0.07, startTime + idx * 0.09);
      gain.gain.exponentialRampToValueAtTime(0.0001, startTime + idx * 0.09 + 0.35);

      osc.connect(gain);
      gain.connect(ctx.destination);

      osc.start(startTime + idx * 0.09);
      osc.stop(startTime + idx * 0.09 + 0.4);
    });
  } catch {
    // Ignore audio errors gracefully
  }
}

/**
 * Tower floor milestone level-up fanfare
 */
export function playLevelUpFanfare(): void {
  if (isAudioMuted()) return;
  const ctx = getAudioContext();
  if (!ctx) return;

  try {
    const melody = [
      { freq: 523.25, time: 0.0, dur: 0.1 }, // C5
      { freq: 659.25, time: 0.1, dur: 0.1 }, // E5
      { freq: 783.99, time: 0.2, dur: 0.1 }, // G5
      { freq: 1046.5, time: 0.3, dur: 0.25 }, // C6
      { freq: 880.0, time: 0.55, dur: 0.12 }, // A5
      { freq: 1046.5, time: 0.7, dur: 0.45 }, // C6 hold
    ];

    const startTime = ctx.currentTime;

    melody.forEach((item) => {
      const osc = ctx.createOscillator();
      const gain = ctx.createGain();

      osc.type = 'triangle';
      osc.frequency.setValueAtTime(item.freq, startTime + item.time);

      gain.gain.setValueAtTime(0.09, startTime + item.time);
      gain.gain.exponentialRampToValueAtTime(0.0001, startTime + item.time + item.dur);

      osc.connect(gain);
      gain.connect(ctx.destination);

      osc.start(startTime + item.time);
      osc.stop(startTime + item.time + item.dur + 0.05);
    });
  } catch {
    // Ignore audio errors gracefully
  }
}