Can't figure out how to play rapid fire weapon sounds

Godot Version

4.3

Question

I tried multiple methods but none of them worked.
The most obvious way is to just play the fire sound for every shot, but that doesn’t work, since my weapon fires once every 0.15 seconds, which doesn’t perfectly align with the physics framerate (the weapon firing timer is handled in the _physics_process function), therefore the sound is off by one frame after a few shots, which sounds irregular and bad.

Another method I tried to make it not sound irregular was to make the sound loop (in the import settings) after the first shot and stop the sound after the weapon is no longer firing. I couldn’t get this to work either, because I need to stop the sound right as it’s about to end (otherwise the sound will get cut off too early) but if I stop it too late, the sound will start again. Unfortunately the AudioStreamPlayer.get_playback_position() function is just way too imprecise for me to be able to stop the sound at such a precise moment.

I also tried making the weapon fire exactly once every 10 frames and changed the physics fps to 67, but even that sounded irregular randomly for some reason.

There has to be a solution for something as simple as firing a weapon rapidly, right? Does anyone know how to do this?

1 Like

I done this by a timer.

2 Likes

Unfortunately, this solution is not perfect. On high frame rates it sounds good, but on lower frame rates it still sounds a bit irregular. This is because the timer node updates on a frame by frame basis (I think). Definitely sounds better than before, but users on lower end devices might experience issues. I will mark this as the solution if I can’t find a better one.

Try using your own counter and adding delta to it, then comparing it to your fire_rate. (I do this in _process). I have a 48kb audio file on a weapon that fires 0.22 seconds and have experienced no problems at all. I tried it at 0.15 following your question and again it was fine. Have you set your polyphony to 1? (That may be the default, I am not sure). Anyway, I hope you get it solved.

PS I have the audioplayer in my bullet scene set to autoplay on spawn. So I don’t actually tell it to play.

On normal fps it works fine for me too. Does it work fine for you if you set the max_fps of your project to 20?

I’ll try, give me a mo.

Sorry but yes. 20 FPS though is so bad for my movement, but the firing was fine at 0.15 fire rate.

PS I wonder what I have done wrong with my movement!

PPS Actually no. Although it appears fine on re-looking at it more closely the sound appears to be skipping shots entirely. So five or six bullets emerge but the sound only plays 2 or 3 times. Personally I am not going to worry about it though, I mean, if you are down to 20 FPS on a device then you need a better device. My game plays perfectly at 60 FPS even on my old phone.

Strange. I guess I’ll just deal with it for now I suppose. It’s unlikely many players will actually go as low as 20 fps on my game and even on 20 fps it doesn’t sound horrible. Just slightly irregular. I assume some sounds are off by one frame and on 20 fps, that one frame is enough to make it sound slightly off.

1 Like

Another problem could be the hardware, such as a cheap sound card that is not responding to everything.
Cheap sound cards can actually slow down a fast computer to make it look like junk.
For example, I had a Intel quad core running The game called ‘Oblivion’. I was having poor frame rates with it and low graphic settings. After I stuck a sound blaster audigy gamer in it, the frame rates shot up to 60. And the game was amazing. This was long time ago.

But yes, it’s possible your sound card is failing, but the programming code is good.