Skip to content

Instantly share code, notes, and snippets.

@mateusfg7
Created September 29, 2023 23:57
Show Gist options
  • Save mateusfg7/2279120d9867e437306bcab9852b0c82 to your computer and use it in GitHub Desktop.
Save mateusfg7/2279120d9867e437306bcab9852b0c82 to your computer and use it in GitHub Desktop.
Fade sound with React.js
const sleep = (timeInMs: number) =>
new Promise(resolve => setTimeout(resolve, timeInMs))
interface FadeSoundProps {
from: number
to: number
step?: number
totalFadeTimeMs: number
soundRef: React.RefObject<HTMLAudioElement>
beforeFade?: () => void | undefined
afterFade?: () => void | undefined
}
export async function fadeSound({
from,
to,
step = 0.01,
totalFadeTimeMs,
soundRef,
beforeFade,
afterFade
}: FadeSoundProps) {
if ((from || to) > 1 || (from || to) < 0) {
console.error(
'fadeSound | `from` and `to` parameters need to be in 0-1 interval'
)
return
}
const diff = to - from
const timeoutInterval = Math.abs(totalFadeTimeMs / (diff / step))
soundRef.current.volume = from
{
beforeFade && beforeFade()
}
async function fade() {
const currVolume = soundRef.current.volume
if ((to < from && currVolume <= to) || (to > from && currVolume >= to))
return
let next = soundRef.current.volume + diff * step
if (diff > 0) next = Math.min(next, to)
else next = Math.max(next, to)
soundRef.current.volume = next
await sleep(timeoutInterval)
await fade()
}
await fade()
{
afterFade && afterFade()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment