Reuse the decoded AudioBuffer. Give each hit its own playback node.
The first press of a pad works. The second brings up InvalidStateError on start(). If both calls use the same AudioBufferSourceNode, that's the first thing to fix. Start a new source for each hit and point it at the same decoded audio. You don't need to fetch or decode the file again.
One audio file, separate playheads
The Web Audio specification only allows one successful start() per source node. That doesn't limit how often you can play an AudioBuffer. For a drum pad, keep one buffer and create one source per Note On. If the second hit lands while the first is ringing out, let both voices finish. A choke group, which deliberately cuts off a previous hit, would be a separate choice.
This function assumes the user has already enabled audio and the buffer is decoded. It takes a mix destination, such as a GainNode you connect to the context output:
function playHit(ctx, buffer, output, when = ctx.currentTime) { const source = ctx.createBufferSource(); source.buffer = buffer; source.connect(output); source.addEventListener("ended", () => source.disconnect(), { once: true }); source.start(when); return source; } Let the start time use the audio context clock. For a simple immediate MIDI pad, omit when. For sequenced hits, pass a future time in audio-context seconds. The sample's own length determines when a natural hit ends at the default playback rate without looping.
Two hits that overlap
Schedule two hits on a hypothetical 400 ms sample, 120 ms apart. The source lifetimes overlap by 280 ms. Each source has one start, and both share the original buffer. Cleaning up source A must not disconnect source B or the shared output.
time action source decoded buffer 2.000 s start A same object 2.120 s start B same object 2.400 s ended A B stays connected 2.520 s ended B output stays connected An offline JavaScript test checked source identity, the shared buffer, timestamps, and cleanup in both notification orders. It used a mock context, so the table is a scheduling illustration, not an observation of audio rendering or MIDI latency. If simultaneous hits get too loud, give the mix bus some headroom.
If there's no error but only one hit
Log the incoming events and each call to playHit. Two positive-velocity Note Ons should cause two calls for this one-shot pad. A MIDI 1.0 Note On with zero velocity represents a release, so don't treat it as a second strike. If both calls happen and neither sounds, check that the AudioContext is running before debugging the pad. If you use Sonari for Mac to generate MIDI, confirm that your browser can see the routed input first. Changing the controller won't make a started source node reusable.
Sonari Studio makes Sonari. This article was produced with AI assistance.

