0 votes

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.

in Engine by (22 points)
edited by

1 Answer

+1 vote

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.

by (29,042 points)
edited by

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.

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.