From 9a65c8ca618995f56683792f703c7354e176f07c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=BE=E3=81=A3=E3=81=A1=E3=82=83=E3=81=A8=E3=83=BC?= =?UTF-8?q?=E3=81=AB=E3=82=85?= <17376330+u1-liquid@users.noreply.github.com> Date: Thu, 21 Mar 2024 12:41:42 +0900 Subject: [PATCH] =?UTF-8?q?fix(frontend):=20=E5=90=8C=E3=81=98=E9=9F=B3?= =?UTF-8?q?=E6=BA=90=E3=81=AF=E7=9F=AD=E6=99=82=E9=96=93=E3=81=A7=E9=87=8D?= =?UTF-8?q?=E8=A4=87=E3=81=97=E3=81=A6=E6=B5=81=E3=82=8C=E3=81=AA=E3=81=84?= =?UTF-8?q?=E3=82=88=E3=81=86=E3=81=AB=20(MisskeyIO#556)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/frontend/src/scripts/sound.ts | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/packages/frontend/src/scripts/sound.ts b/packages/frontend/src/scripts/sound.ts index b6927e0d61..783baddc93 100644 --- a/packages/frontend/src/scripts/sound.ts +++ b/packages/frontend/src/scripts/sound.ts @@ -6,10 +6,10 @@ import type { SoundStore } from '@/store.js'; import { defaultStore } from '@/store.js'; import { $i } from '@/account.js'; +import { RateLimiter } from '@/scripts/rate-limiter.js'; let ctx: AudioContext; const cache = new Map(); -let canPlay = true; export const soundsTypes = [ // 音声なし @@ -188,17 +188,13 @@ export async function loadAudio(url: string, options?: { useCache?: boolean; }) */ export function playMisskeySfx(operationType: OperationType) { const sound = defaultStore.state[`sound_${operationType}`]; - if (sound.type == null || !canPlay || ('userActivation' in navigator && !navigator.userActivation.hasBeenActive)) return; + if (sound.type == null || ('userActivation' in navigator && !navigator.userActivation.hasBeenActive)) return; - canPlay = false; - playMisskeySfxFile(sound).finally(() => { - // ごく短時間に音が重複しないように - setTimeout(() => { - canPlay = true; - }, 25); - }); + playMisskeySfxFile(sound); } +const rateLimiter = new RateLimiter({ duration: 50, max: 1 }); + /** * サウンド設定形式で指定された音声を再生する * @param soundStore サウンド設定 @@ -213,7 +209,7 @@ export async function playMisskeySfxFile(soundStore: SoundStore) { } const url = soundStore.type === '_driveFile_' ? soundStore.fileUrl : `/client-assets/sounds/${soundStore.type}.mp3`; const buffer = await loadAudio(url); - if (!buffer) return; + if (!buffer || !rateLimiter.hit(url)) return; const volume = soundStore.volume * masterVolume; createSourceNode(buffer, { volume }).soundSource.start(); }