mirror of
https://github.com/Since-3/since3-website.git
synced 2026-10-12 20:51:17 +00:00
added legeal stuff
This commit is contained in:
parent
f059cfb81c
commit
02af696d4f
23 changed files with 979 additions and 21 deletions
13
lib/sound.ts
13
lib/sound.ts
|
|
@ -33,7 +33,8 @@ export function setEnabled(enabled: boolean) {
|
|||
type Engine = { ctx: AudioContext; master: GainNode; noise: AudioBuffer }
|
||||
let engine: Engine | null = null
|
||||
|
||||
// Created lazily on first use, which is always inside a user gesture
|
||||
// Created lazily on first use. Hover is not a user activation, so `play`
|
||||
// checks activation before calling this — see there.
|
||||
function getEngine(): Engine {
|
||||
if (!engine) {
|
||||
const ctx = new AudioContext()
|
||||
|
|
@ -156,6 +157,16 @@ const VOICES: Record<SoundVoice, (engine: Engine, now: number) => void> = {
|
|||
export function play(voice: SoundVoice, { force = false } = {}) {
|
||||
if (typeof window === "undefined") return
|
||||
if (!force && !isEnabled()) return
|
||||
|
||||
// Before the first user activation, a new AudioContext stays "suspended"
|
||||
// and resume() is a no-op. currentTime is then frozen at 0, so every
|
||||
// sound scheduled while hovering (not an activation) would queue up at
|
||||
// t=0 and all fire at once, as one loud burst, once a click resumes it.
|
||||
const activated = navigator.userActivation?.hasBeenActive ?? true
|
||||
if (!engine && !activated) return
|
||||
if (engine && engine.ctx.state !== "running" && !activated) return
|
||||
|
||||
const current = getEngine()
|
||||
if (current.ctx.state !== "running") return
|
||||
VOICES[voice](current, current.ctx.currentTime)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue