|
I have a queue to store wav audio buffer, also I use audioContext with decodeAudioData to play this audioBuffer sound, this code has glitch(buzzing) sound between gap, so I change the start part to const OVERLAP_TIME = 0.2
const preloadAudioBuffer = async (queue: Queue, audioCtx: AudioContext) => {
const { buffer } = Buffer.from(queue.chunk.data)
try {
return await audioCtx.decodeAudioData(buffer)
} catch (error) {
console.log('decodeAudioData error: ', error)
return null
}
}
const playAudioBuffer = () => {
const audioCtx = audioContextRef.current!
const audioGainNode = audioGainNodeRef.current!
const queue = queueArr.current[0]
if (!queue) {
console.log(`AudioBuffer queue is empty`)
return
}
const audioBuffer = await preloadAudioBuffer(queue, audioCtx)
if (!audioBuffer) return
audioGainNode.gain.value = audioVolumeRef.current // change audio volume
sourceRef.current = audioCtx.createBufferSource()
sourceRef.current.buffer = audioBuffer
// connect audio sourceRef.current to the nodes
audioGainNode.connect(audioCtx.destination)
sourceRef.current.connect(audioGainNode)
const startTime = Date.now()
sourceRef.current.onended = async () => {
// calculate delay time
const endTime = Date.now()
const mills = endTime - startTime - 1000
delayTimeRef.current += mills
queueArr.current.shift()
isPlayingRef.current = false
// recursive to play audio
await playAudioBuffer()
}
isPlayingRef.current = true
// sourceRef.current.start(audioCtx.currentTime - OVERLAP_TIME)
sourceRef.current.start()
} |
Answered by
padenot
Nov 6, 2023
Replies: 1 comment 1 reply
|
This repository is home to the Web Audio API specification. Questions on how to do something with the Web Audio API are better suited to either Stack Overflow or the Web Audio API slack That said, https://web.dev/articles/audio-scheduling explains what you're seeing. |
1 reply
Answer selected by
Mayvis
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This repository is home to the Web Audio API specification.
Questions on how to do something with the Web Audio API are better suited to either Stack Overflow or the Web Audio API slack
That said, https://web.dev/articles/audio-scheduling explains what you're seeing.