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.