# How to prevent overlapping of gunshot sounds when using fast-firing weapons

So I am now trying to find sounds for my guns but when I grab a gun sound effect and play it in my game a lot of the sounds are either terrible sounding or have this horrible echoing effect because as a gun shoots sometimes the previous sound is playing still.

public void shoot(float x, float y, float direction){

if(empty){
PlayHUD.message = "No more bullets!";
return;
}

return;
}

if(System.currentTimeMillis() - lastShot < fireRate){
return;
}

float dx = (float) (-13 * Math.cos(direction) + 75 * Math.sin(direction));
float dy = (float) (-14 * -Math.sin(direction) + 75 * Math.cos(direction));

float dx1 = (float) (-13 * Math.cos(direction) + 75 * Math.sin(direction));
float dy1 = (float) (-14 * -Math.sin(direction) + 75 * Math.cos(direction));

PlayState.effects.add(new MuzzleFlashEffect(x + dx1, y + dy1, (float) Math.toDegrees(-direction)));

PlayState.projectiles.add(new Bullet(this, x + dx, y + dy, (float) (direction + (Math.toRadians(MathUtils.random(-accuracy, accuracy))))));

if(OptionState.soundOn){
}

bulletsInClip--;
lastShot = System.currentTimeMillis();

}


Here is the code for where the sound plays. Every time this method is called the sound is called but it happens so often in this case that there is this terrible echoing. Any idea on how to fix this?

This problem is usually a sound design problem, not a coding problem. These problems occur mostly because of insufficient use of random variations in sound samples.

• You should first make sure you are using a pool of different recordings of the same sound and play a random one each time you fire a bullet, or make small random variations of the pitch on the fly via code.

• If the problem still exists, check if your variations are distinct enough or not.

• If you are doing this but still not satisfied, you should enlarge your sound set, manually introduce more variety using a sound editor, and manually introduce more random variety with code.

Some other solutions may be:

• Check if the sound effects include reverb and especially early reflections. Earl reflections are the first bounce of sound from a surface and usually heard as multiple reflections of the original sound in a few miliseconds. Playing many of such sounds in a very short time would result in accumulation of these effect and you can get strange echos effects.

• Check if your sounds are very long or too realistic samples to be perceived as real by our brain. I don't know what weapon are you talking about but, you may try digital sounds that don't exist in real life. Of course use them also with sufficient random variation.

I'm not familiar with libgdx, but judging from your code what you could do is to stop the currently playing sound, and only then play it again.

    if(OptionState.soundOn){

//Stop sound here

}


To prevent echoing, avoid overlapping all the sounds :

1. Check if bullet sound has finished playing
2. Stop the current sound and play the next/reset

if (bulleSoound.isPlaying()) {
bulletSound.reset();
} else {
bulletSound.play();
}


As GuneyOzsan says, you certainly can randomize by adding more bullet sounds

• In libgdx, Music has an isPlaying() method but Sound does not. Commented Jul 16, 2015 at 15:34