Changing Sample player Volume on the fly - And sound glitches

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Santiago
:warning: Old Version Published before Godot 3 was released.

Hi everyone!
I’ve been trying to make some kind of a music game on Godot for some time now, and I’m really stuck on something, so for short:
I have a sample player that… well… plays a sample, that part is fine,
the problem is,
when I try to change the sample player volume, the change doesn’t apply until the sample player plays again, not while the sample is being played.
I’ve noted that it is possible to change the stream player volume on the fly (with StreamPlayer.set_volume_db), but I’m not able to do so with the sample player (with SamplePlayer.set_volume_db), and both classes have methods to change the volume.

My question is: Can I change the volume of the sample player on the fly by any means?
Is there a function/method I’m missing to work that out?

Also, but not that important for now:
I’ve experienced problems with sound while having LLMS(Linux Multi Media Studio) open, being more specific, having the audio settings for output_latency under 30 or something makes all the audio output to be fuzzy and noise-like, but closing LMMS or, in some extreme cases, restarting pulse_audio from terminal seems to fix the problem.
Right now I have the output_latency on 15 and I have no problems, without opening LMMS.
I don’t know if this is something related to drivers or not, I haven’t tested it on windows for now, but it should work just fine.

BTW: I’m using Godot 2.0 Stable, on Ubuntu 15.04 64 bits

Sorry for my English and Thanks in advance!!!

:bust_in_silhouette: Reply From: Ace-Dragon

Hi.

First off, I would make sure you are using either .wav files or .ogg files directly (as opposed to Godot-specific sound formats).

If the sound still glitches and if you can prove this happens in very simple cases, I would open an issue on Godot’s github page as it may be a bug.

Hi there! I’m using ogg files for streaming and the godot sample format for the sample player, and both glitches at the same time when LMMS is open. When I open the ubuntu sound options and go to the applications tab(or something like that) the icon for the godot application blinks endlessly while the sound glitches.

Santiago | 2016-03-14 19:25

Does it only glitch when LMMS is open or does it also happen with other applications open? Does it glitch when Godot is the only thing open?

If the answer to the latter is no, then it may or may not be an issue with Ubuntu then, I don’t use Linux myself though so I can’t say for sure.

Ace-Dragon | 2016-03-14 20:16

It doesn’t glitch when Godot is the only thing open, no matter the output_latency value, I can go as low as 5 ms and the program runs fine.
I tried to capture my desktop while running the program but the two screen capture programs I’ve used so far don’t record any sound.
I have to say that I had issues with the audio no ubuntu in the past, in general, so I suppose that this is just driver/hardware related.

Now talking about dynamic volume change on the sample player, do you have any Idea on what can I do?
I’ve noted that the method to get_volume_db also give me some errors.

this link → http://www.mediafire.com/download/67dfwsrb24fq8bc/SampleTest.zip has a simple test I’ve made to show the error Godot gives me when I try to change the sample player volume with the method set_volume_db. Also, the line with the method get_volume_db for the sample player is commented as it doesn’t work.

The Debugger shows me this:
Type:Error
Description:
Time 0:00:00:0469
C Error: Condition ‘!v.active’ is true. returned: 0
C Source: scene/audio/sample_player.cpp:411
C Function: get_volume_db

something similar appears to happen with set_volume_db.

Santiago | 2016-03-15 13:16

:bust_in_silhouette: Reply From: Freeman

Santiago, the most efficient way to work with samples in Godot (latency-wise and detailed control-wise speaking, but not so comfy way, unfortunately) is to actually plug straight into AudioServer interface that stands behind SamplePlayer anyway.

It allows you to operate all audio parameters in a smooth, detailed way that as far as my experience goes (mainly audio related applications) is not possible when using just a SamplePlayer node.

It is more complicated, because you basically have to create everything from scratch - you have to put in code things that SamplePlayer node takes care of for you - but that kind of low level access gives more control and better latency.

So, if you want to have smooth, instant changes of the volume of the already playing sample, use AudioServer and it’s function voice_set_volume.
It will work like a charm.

Thank you for your answer, Freeman, and sorry for replying so late. I will try to use AudioServer. Thanyou you all!

Santiago | 2016-04-18 19:52

Hello again, I’ve tried using AudioServer but somehow, after I set the voice volume, and I play the sample again, the volume is restored to the default.

I’ve used the methods described here: Delay in sound effects from SamplePlayer2D - Archive - Godot Forum

I don’t know what I’m doing wrong :frowning:

Santiago | 2016-04-18 21:28

Congrats on making the AudioServer working. :wink:
I don’t know what’s in your code, but to remember the voice volume adjustments, you could use a global variable
http://docs.godotengine.org/en/latest/tutorials/step_by_step/singletons_autoload.html?highlight=singleton

and let the adjustment value of the volume be “remembered” there and recalled when creating new voice to play a sample again.

Freeman | 2016-04-18 22:33

Sorry to bother you again with this, but I’ve tried everything I could think of but… still no luck with that.

this link has the project in which I’ve been playing with the sample player, I honestly don’t know what I’m doing wrong :frowning:

I think I should just move on and work on other projects but I really would like to know how to change a sample’s volume dynamically.

Extra data: The version 2.0.2 of godot crashes when I try to play a sample from the sample library window on the editor, but the version 2.0 work just fine.

Thank you for all the help!!

Santiago | 2016-04-19 02:18

How are you getting to AudioServer ? AudioServer isn’t a node. It’s not a method of AudioStreamPlayer3D. Where/how do you access AudioServer?

Brinux | 2019-06-17 23:17