Audio / AudioServer Bugged in Godot 4 GDScript?
Asked Answered
S

3

0

Howdy everyone,
I was about to create the audio environment for a game.
In order to do so, I wanted to script it, because it allows for more and better control of the sound.

So what I did was, Setup the node:

Then I loaded some sounds and linked the buses,

Till now, it works. you click play, and it plays, no problem. Now we link it with the animations,

now, when we play the game. and play the animation. we hear a gun shot, awesome.
Now i reach the point to script the sound for better control.

func soundplayer(node):
	var audio = node.get_node('Skeleton/AudioStreamPlayer2D')
	var reverb = AudioServer.get_bus_effect(1, 0)
	var play_time = audio.get_playback_position()
	print(audio, " ", reverb)
	print(AudioServer.get_bus_volume_db(1), " <1 -- 0> ", AudioServer.get_bus_volume_db(0))
	if audio:
		if play_time != 0:
			print(play_time)
	else:
		print("no audio stream")

We call it every frame,

soundplayer($Player)

And the result is surprising.
AudioStreamPlayer2D:<AudioStreamPlayer2D#54274296405> <AudioEffectReverb#-9223372014037761388>
-0.52601301670074 <1 -- 0> -1.91699004173279

This output is consistent, nothing changes whatsoever, doesn't matter which kind of sound is played.
Whereof I, AT LEAST, expected the audioserver's volume to change. which it didn't

Yet the busses work, the reverb effect alters the sounds played.

But GDScript doesn't seem to "work"

Am I facing a bug?

The editor is running in Compatability.

Subacute answered 9/7, 2023 at 12:45 Comment(0)
S
0

Subacute I, AT LEAST, expected the audioserver's volume to change

What would cause the volume to change? Nothing in the script you posted would affect playback, it just looks up current values and prints them.

Showers answered 9/7, 2023 at 20:6 Comment(0)
S
0

Showers You're entirely right, I thought it would return the "current" output volume.

But nevertheless

var play_time = audio.get_playback_position()

This line of code should return a different value.
Which is always 0.

I need that value in order to play the effect at a certain timestamp.

So to further debug the issue I changed the prints.

var audio = node.get_node('Skeleton/AudioStreamPlayer2D')
var reverb = AudioServer.get_bus_effect(1, 0)
print(audio, " ", reverb)
print("%s %s %s" % [audio.get_playback_position(), audio.has_stream_playback(), audio.playing])

with the following results

AudioStreamPlayer2D:<AudioStreamPlayer2D#55599696493> <AudioEffectReverb#-9223372013400226777>
0 true true
AudioStreamPlayer2D:<AudioStreamPlayer2D#55599696493> <AudioEffectReverb#-9223372013400226777>
0 false false

That looks better. it does show whether it plays or not.
But the timestamp, the one I need the most, is not returned at all, unfortunately.

Subacute answered 10/7, 2023 at 6:10 Comment(0)
S
0

Yeah that does seem odd that get_playback_position always returns zero, although apparently even the official documentation refers to that method as "not that useful":

Using AudioStreamPlayer.get_playback_position() to obtain the current position for the song sounds ideal, but it's not that useful as-is. This value will increment in chunks (every time the audio callback mixed a block of sound), so many calls can return the same value. Added to this, the value will be out of sync with the speakers too because of the previously mentioned reasons.

That page has some suggestions for more accurate syncing, but in this case I think there may be a simpler solution - since you've got that sound playing as part of an animation, you can add a "Call Method Track" to the animation and use to that to trigger whatever code you want at the appropriate time.

Showers answered 10/7, 2023 at 23:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.