# how to make a continuous machine gun sound-effect

I am trying to make an entity fire one or more machine-guns.

For each gun I store the time between shots (1.0 / firing rate) and the time since the last shot.

Also I've loaded ~10 different gun-shot sound-effects.

Now, for each gun I do the following:

function update(deltatime):
timeSinceLastShot += deltatime

if timeSinceLastShot >= timeBetweenShots + verySmallRandomValue():
timeSinceLastShot -= timeBetweenShots

if gunIsFiring:
displayMuzzleFlash()
spawnBullet()
selectRandomSound().play()


But now I often get a crackling noise (which I assume is when two or more guns are firing at the same time and confuse the sound-device).

My question is whether

A) This a common problem and there is a well-known solution, maybe to do with the channels or something, or

B) I am using a completely wrong approach to the task. I had a look at some sound-assets for other games and they used complete burst with multiple shots. I suppose I could try that, but I would like to have organic little hickups in the gun-fire (that's what the random value is for) to make the game more gritty and dirty.

I am using Panda3D, but I had the exact same problem in PyGame and SDL.

Thanks a lot for the answers so far! One more problem with faking it though: Now how do I stop the sound? Let's say I have an effect with 5 bangs...

*bang* *bang* *bang* *bang* *bang*


And I magically manage to loop it so that there's no gap or overlap if the player fires more than 5 shots. Now, what do I do if the player stops firing halfway through the third bang? How do I know how long to keep playing the sample so that the third bang is completed and I can start playing the rumbling echo of the last shot?

Of course I can look up the shot/pause timing of that sound-sample and code accordingly, but it feels extremely hacky.

• pygame.org/docs/ref/mixer.html mentions this: "The default is set to reduce the chance of scratchy sounds on some computers. You can change the default buffer by calling pygame.mixer.pre_init before pygame.mixer.init or pygame.init is called. For example: pygame.mixer.pre_init(44100,-16,2, 1024) The default size was changed from 1024 to 3072 in pygame 1.8." It might help to fiddle with the buffer size.
– Eric
Nov 5, 2012 at 12:21
• ok, gonna look up "buffer size" then, thanks. Nov 5, 2012 at 12:27