Distorted sound while playing a walking sound during a walking animation
Asked Answered
L

3

0

I stumbled upon a weird issue when playing sound using AudioPlayer node in my root scene when trying to run it in the _process(delta) function in a script connected to an AnimatedSprite node. The sound used is very short and only plays the sound for one step basically so I made my code to simply reflect this in the script as the following:

extends AnimatedSprite

func load_sound(sound):
	get_node("/root/AudioPlayer").load_sound(sound)

func start(delta, animation_type):
	animation = animation_type
	playing = true
	
func _process(delta): 
	if(playing):
		if(get_frame() % 2 == 0):
			get_node("/root/AudioPlayer").play_sound()

func stop():
	playing = false

Firstly, I load the sound in my player script and then whenever the animation is being triggered by having the mouse click to a different location, I set playing the animation to true.
Then the animation plays (works all fine so far), then during the play of the animation I use _process(delta) function to yield the current frame and as can be seen, anytime the frame count is at 2 play the sound. This does indeed work fine. The sound sounds step like and works anytime the animation is playing. The timing is just fine as well and sounds authentic. The only thing that bothers me is that somehow there is some glitchy / distorted noise when playing the sound. When playing the sound outside of the _process(delta) function in the script, this glitch / distorted noise is nonexistant. So my question here is, why does this noise occur? How can I fix this?

Loleta answered 10/12, 2022 at 13:14 Comment(0)
E
0

I'm not sure how that would work. You are playing the sound on each even frame. At 60 fps that is 16ms each time, surely too short to play anything. The audio is likely being clipped.

Etana answered 10/12, 2022 at 13:42 Comment(0)
L
0

Loleta Yeah I thought that as well actually, is there some way to do this in a better way? Would I need to make the track longer? Make the track run slower or somehow call at a later frame rate? The thing is I tried to change the get_frame() % 2 == 0 to 4 to 6 to 8 and to 12, it got a little better, but the effect is gone if I do it this way. Any ideas?

Loleta answered 11/12, 2022 at 17:11 Comment(0)
E
0

You can put sounds in an AnimationPlayer. Like if this is for a character walking, you can set a function call on each footstep frame, and that function can choose a random footstep sound to play.

This would probably be the best thing, as it will match exactly with the animation. Otherwise you can set a looping timer, that on timeout will call that same function. The timer could be set to half a second or 2 seconds, whatever.

Etana answered 11/12, 2022 at 19:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.