A group holds only so many voices.
Put your sounds in a named group, give it a maxInstances limit, and play as much as you like. Once the group is full, the oldest instance stops so the new one can start. Fill the slots in the voice lab, then fire faster than the limit in Audio Invaders.
createSoundGroup()play({ groupId })getGroup()
-
01
Create the group
One call, one limit for each kind of sound.
sm.createSoundGroup('weapons', { maxInstances: 3 }) -
02
Play into it
Every play() that names the group takes a slot.
sm.play('laser', { overlap: true, groupId: 'weapons' }) -
03
The limit does the rest
A full group stops its oldest voice to make room.
// 4th shot → oldest instance stops
Try it
Voice lab
Two groups, two limits. Hammer the buttons, hold one down to keep firing, or use the number keys 1 to 7. Watch a slot get taken over.
Hear why
Burst test
The same rapid fire, twice, with a different pitch on every shot. Without a group every shot rings on, so the pitches pile up and clash. With maxInstances the oldest shot stops as soon as a new one starts. You lose the tails of the old shots, you keep every new shot clear, and the voice count never goes past the limit.
Also from this workshop
Retro Paddle is a full game rather than a demo, and its audio runs on this same library: stereo-panned effects, a serve you aim yourself, power-ups, mines, and an end-of-match sequence that tells you how the match went.
- Sound soundhub, panned per hit
- Install
npm i retro-paddle - Drop-in one constructor call on any page