#Playing a Looping Audio Track from a Random Start Point in the Loop

1 messages · Page 1 of 1 (latest)

flint trellis
#

Greetings all! I'm not much of a programmer--I'm more of a modelling and art guy--so I need some help with this: I have a torch scene in Godot, complete with shadergraphs for flames, smoke, and the lighting, but need to add a fire sound effect. I have a looping fire sound effect, but I want to have the loop start from a random point in the audio file, so that every torch in the map doesn't sound exactly the same all at once. I'm not sure how to accomplish this in Godot, but I know that, in Unity, this bit of code that I found online worked for my purposes:


public class NewMonoBehaviourScript : MonoBehaviour
{
    public AudioSource source;

    // Play the sound file attached to this object starting at a random point in the track
    void Start()
    {
        source.time = Random.Range(0f, source.clip.length);
        source.Play();
    }
}```

If someone that's knowledgeable about how C# works can parse this and get the gist of what I'm talking about, that would be rad.

Also, is AudioStreamPlayer3D the proper node for this in a 3D environment? I guess I'm not super familiar with how all the different audio nodes work, either.
fluid gale
#

AudioStreamPlayer3D is perfect if you want the sound to come from the torch

#

AudioStreams also let you pass in a start index to the play() function to start at any position in the track you want, it defaults to the beginning if no parameter is given

candid wing
#
    var rand_pitch: float = randf_range(0.1, 0.5)
    audio_player.pitch_scale = rand_pitch
    
    var rand_position: float = randf_range(0.1, 0.5)
    audio_player.play(rand_position)

audio_player is a variable storing the nodepath to your audio_player in your scene
i also added a random pitch scale as well since i feel like that adds more variation. Just make sure your audio player has your audio set correctly and its loopable

candid wing
fluid gale
#

oh what does that do?

candid wing
#

if you dont have one, it defaults to sounds in relation to 0,0

fluid gale
#

i should check my game then…

candid wing
#

wait not 0,0

#

its to the camera

#

but sometimes your camera isnt where you want audio to be played from (or you might not have a camera at all)

fluid gale
#

ok my game is first-person, so that explains why it’s been fine for me

candid wing
#

ah

flint trellis
#

@fluid gale @candid wing thank you both for the help!

candid wing
#

ofc!