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:
parent
578551f617
commit
2885236707
@ -172,27 +172,31 @@ export function playTypingTick() {
|
|||||||
a.play().catch(() => {})
|
a.play().catch(() => {})
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Keyboard input sound - plays on any character typed in inputs. Separate from typing tick/intro typing. */
|
/** Keyboard input sound - short synthesized click per key. Does NOT use typing.mp3 or intro-typing.mp3. */
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
export function playKeyboardTypingSound() {
|
export function playKeyboardTypingSound() {
|
||||||
const a = getKeyboardInputSound()
|
const ctx = getContext()
|
||||||
a.currentTime = 0
|
if (!ctx) return
|
||||||
a.play().catch(() => {})
|
|
||||||
|
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 */
|
/** Gaming-style boot thud - soft impact when dashboard loads */
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user