Refactor keyboard input sound implementation in useLoginSounds.ts

- Updated the keyboard typing sound function to use a synthesized click instead of preloaded audio files, enhancing performance and responsiveness.
- Removed the previous audio pool logic for keyboard input sounds to streamline the code.
- Ensured audio context is properly managed to prevent playback issues.
This commit is contained in:
Dorian
2026-02-18 10:39:10 +00:00
parent 578551f617
commit 2885236707

View File

@@ -172,27 +172,31 @@ export function playTypingTick() {
a.play().catch(() => {})
}
/** Keyboard input sound - plays on any character typed in inputs. Separate from typing tick/intro typing. */
let keyboardInputPool: HTMLAudioElement[] = []
const KEYBOARD_INPUT_POOL_SIZE = 5
function getKeyboardInputSound(): HTMLAudioElement {
if (keyboardInputPool.length === 0) {
for (let i = 0; i < KEYBOARD_INPUT_POOL_SIZE; i++) {
const a = new Audio('/assets/audio/typing.mp3')
a.volume = 0.5
keyboardInputPool.push(a)
}
}
const a = keyboardInputPool.shift()!
keyboardInputPool.push(a)
return a
}
/** Keyboard input sound - short synthesized click per key. Does NOT use typing.mp3 or intro-typing.mp3. */
export function playKeyboardTypingSound() {
const a = getKeyboardInputSound()
a.currentTime = 0
a.play().catch(() => {})
const ctx = getContext()
if (!ctx) return
try {
if (ctx.state === 'suspended') ctx.resume()
} catch {
return
}
const t = ctx.currentTime
const osc = ctx.createOscillator()
const gain = ctx.createGain()
osc.type = 'sine'
osc.frequency.setValueAtTime(1200, t)
gain.gain.setValueAtTime(0, t)
gain.gain.linearRampToValueAtTime(0.06, t + 0.002)
gain.gain.exponentialRampToValueAtTime(0.001, t + 0.04)
osc.connect(gain)
gain.connect(ctx.destination)
osc.start(t)
osc.stop(t + 0.04)
}
/** Gaming-style boot thud - soft impact when dashboard loads */