regarding Sound voice and the samplepayer, how do we change the parameters on the fly and where is the documentation?

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

Hi there people.

I have garnered a great deal of information since my last question and i would like to say thanks for the help, and I mean all of it on this forum.

So to my question.

Does anybody know where some comprehensive documentaionon sound for GDscript with definitions of ALL terms with some examples might be?

What can you tell me about voices?
How do i find out who is playing what voice and when?
how do i get a sampleplayer to play from a specific voice?

I understand that a lot of the current information will change in GodotEngine3.x
but Now is now.

What i really want to do is access the properties of the sampleplayer in the tick before it plays a sound and change some variables, such as pitch and whatever, for a bit of variation.

Thanks very much for any ideas or directions.

:bust_in_silhouette: Reply From: Zylann

When you play a sound with SamplePlayer with only the sound name parameter, it returns a number identifying the instance of the sound being played (a.k.a the “voice”). Once the sound finishes the voice is no longer valid and may be reused for next plays.

Changing the pitch of a voice can be done this way then:

var voice = sample_player.play("moo")
# pitch of 1.0 means no change, 2.0 means x2, 0.5 means /2 etc
sample_player.set_pitch_scale(voice, 2.0) 
# Can be changed later on as long as the voice is still playing

On SamplePlayer2D the function is voice_set_pitch_scale, not sure why the name is different but I just remind you in case you would get confused.

This is documented here, take some time to see all functions available (for example you may also be looking for is_voice_active) but I don’t know personally of a from-scratch tutorial about all concepts involved in sound playback in Godot.

I just suppose that I will have to write one when i work out what I am doing !
Thanks for the suggestion and example material.

Examples are for me so important.

And for fun.

I will report that when i added the above into a Rigidbody2d script I received the following error

Invalid call. Nonexistent function 'set_pitch_scale' in base 'GDNativeClass'

Does this mean that I have to do something special to call the function in an Extends Rigidbody2d script… I think so.

kc | 2017-02-21 16:50

You have to call set_pitch_scale on an object inheriting SamplePlayer. The error you get means you did it on something else that doesn’t have such method.
If your script inherits RigidBody2D for example, you may get the SamplePlayer node by using get_node("path/to/your/SamplePlayer") and call the method on it.
Remember that if it’s a SamplePlayer2D (mind the 2D), the name of the function is voice_set_pitch_scale instead.

Zylann | 2017-02-21 18:20