Kaynağa Gözat

refactor: unify all notification sounds to use shared AudioContext pattern

unknown 3 gün önce
ebeveyn
işleme
e3d5eb865a
1 değiştirilmiş dosya ile 27 ekleme ve 24 silme
  1. 27 24
      src/stores/auth.ts

+ 27 - 24
src/stores/auth.ts

@@ -32,30 +32,7 @@ export const useAuthStore = defineStore("auth", () => {
   let pingInterval: number | null = null;
   const unreadMessagesCount = ref(0);
 
-  function playNotificationSound() {
-    try {
-      const AudioCtx = window.AudioContext || (window as any).webkitAudioContext;
-      if (!AudioCtx) return;
-      const ctx = new AudioCtx();
-      const osc = ctx.createOscillator();
-      const gainNode = ctx.createGain();
-      
-      osc.type = 'sine';
-      osc.frequency.setValueAtTime(880, ctx.currentTime); // A5
-      osc.frequency.exponentialRampToValueAtTime(1760, ctx.currentTime + 0.1); // Up to A6
-      
-      gainNode.gain.setValueAtTime(0, ctx.currentTime);
-      gainNode.gain.linearRampToValueAtTime(0.1, ctx.currentTime + 0.05);
-      gainNode.gain.linearRampToValueAtTime(0, ctx.currentTime + 0.2);
-      
-      osc.connect(gainNode);
-      gainNode.connect(ctx.destination);
-      osc.start();
-      osc.stop(ctx.currentTime + 0.2);
-    } catch (e) {
-      console.warn("Audio disabled or not supported", e);
-    }
-  }
+
 
   let globalWs: WebSocket | null = null;
   let reconnectTimer: number | null = null;
@@ -106,6 +83,32 @@ export const useAuthStore = defineStore("auth", () => {
       console.warn("Audio update sound error", e);
     }
   }
+
+  async function playNotificationSound() {
+    try {
+      const ctx = getAudioCtx();
+      if (!ctx) return;
+      if (ctx.state === 'suspended') await ctx.resume();
+
+      const osc = ctx.createOscillator();
+      const gainNode = ctx.createGain();
+      
+      osc.type = 'sine';
+      osc.frequency.setValueAtTime(880, ctx.currentTime); // A5
+      osc.frequency.exponentialRampToValueAtTime(1760, ctx.currentTime + 0.1); // Up to A6
+      
+      gainNode.gain.setValueAtTime(0, ctx.currentTime);
+      gainNode.gain.linearRampToValueAtTime(0.1, ctx.currentTime + 0.05);
+      gainNode.gain.linearRampToValueAtTime(0, ctx.currentTime + 0.2);
+      
+      osc.connect(gainNode);
+      gainNode.connect(ctx.destination);
+      osc.start();
+      osc.stop(ctx.currentTime + 0.2);
+    } catch (e) {
+      console.warn("Audio notification sound error", e);
+    }
+  }
   // ───────────────────────────────────────────────────────────────────────────
 
   function startPing() {