Indie dev efficiency4 min read

Web Audio Slice Starts in the Wrong Place? Check Source Seconds

Separate the scheduled start time, source offset and source duration before calculating the output of a rate change.

Web Audio Slice Starts in the Wrong Place? Check Source Seconds
Headphones with a waveform and a game audio file concept
AI-generated audio concept. The waveform is illustrative.

You want the section beginning five seconds into a sound, but doubling its playback rate produces the wrong slice. Write down the intended location in the source audio first. The scheduled start time, source position and audible duration answer different questions.

Name each argument

The official reference documents when as an AudioContext time, offset as a position in the source buffer and duration as the amount of source content to play. Doubling playback speed does not turn a source offset of five seconds into 2.5 seconds. The related diagnostic guide covers the separate question of choosing the scheduled context time.

Use a fixed-rate test

Choose a buffer at least ten seconds long with a recognizable marker near source second five. Create a fresh AudioBufferSourceNode, disable looping, keep detune at zero and set a constant playbackRate of 2. Schedule start(context.currentTime + 0.2, 5, 2). The source section runs from second five to second seven, after a short scheduled delay. At that fixed rate, two seconds of source content yield about one second of output. This is a parameter calculation, not a device benchmark.

Compare the content and the duration

Log currentTime at scheduling, the three arguments, the rate and buffer duration. First play the section at rate 1 to identify the marker, then at rate 2 to check the same starting content and shorter output. Reuse the decoded buffer while creating a new source node for each play. Rate automation and looping need separate tests rather than this constant-rate calculation. Use the SFX Generator for candidate game sounds, then verify the slice in the player that will ship.

Recommended tools

Ready to try "Web Audio Slice Starts in the Wrong Place? Check Source Seconds"?

Use our online tools to apply this tutorial to your indie game project right away.