Playing a quickly repeating sound efficiently without the annoying 'click' sound

You might’ve come across a very annoying clicking sound which is played when attempting to play audio repeatedly:

For example:

Before

When doing this, it will interrupt the currently playing waveform and skip back the the beginning of the audio, this causes the click to occur.

EDIT I quote @vsnry for his great technical analysis of what is happening here:

The click happens because the sound changes frequencies instantly. MP3 prevents this by creating short fades at the beginning and end of an audio track, but it messes up if you want to loop something. As someone else already suggested if you want to make a perfect loop, use the .ogg file type.


The Fix

In order to fix this problem, you would want to clone the sound and then play it upon removal.

After

This essentially creates multiple instances of sound files playing at once, instead of having it interrupted by the Play() Command, and then essentially removed, so you don’t have to worry about the debris of clones left over.


Scenario

  • This can be used in UI interfaces, to improve the sound quality should you want to make a sound play on MouseLeave or on MouseEnter

  • This can be used alongside with effects where text is typed onto a value by a script (Typewriter effect).

  • Easy to follow
  • Confusing

0 voters

You’ve mastered this.

17 Likes

You should use the PlayLocalSound method instead of handling this yourself. Using that method, a sound will play without interruption locally, and you can still use the sound object for other purposes / playing the sound using the method again.

local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://SOUNDID"

while wait() do
   game:GetService("SoundService"):PlayLocalSound(sound)
end
20 Likes

Interesting method, hadn’t noticed it before, but what if you need rapidly played 3D sound? Would be handy if that method received optional argument, that allowed specifying position at which to play it, or at least part that should be the source.

1 Like

Quite a bit of people know how to do this, but it’s a good tutorial nonetheless.

Never knew about PlayLocalSound. Awesome.

1 Like

Unfortunately no method for this, I just wrote a helper function to do this instead with similar syntax to the PlayLocalSound call

It’s not “local” as in “just like it was parented to SoundService”, it’s literally “play this sound but only for this client”.

So using it with 3D sounds is as easy as

SoundService:PlayLocalSound(workspace.Part.Sound)

EDIT: Tested again after what BAADF00D said. Seems like even though it does work for 3D sounds, they won’t move (like in my example if the part moved the sound would still be coming from where it was when PlayLocalSound was called).

Haven’t seen anyone point this out yet, but MP3 files almost always start with a tiny amount of silence. I’m not sure if this is our engine or just some oddity of the format, but there’s a good way around it: use .ogg for a seamless looping sound.

Audacity can import MP3 and export .ogg. You can also trim silence out with it. It’s great. :+1:

Also, you can just set sound.Looped to true. There’s a DidLoop event if you need to run code every time it loops. If you use a .ogg with silence trimmed, you can get a perfectly seamless loop.

If you want to loop yourself, I recommend something like this:

while true do
    sound:Play()
    sound.Ended:wait()
end

That will quell any timing issues.

Also, if you want a sound to repeat faster than its length without cutting off, you will indeed need multiple Sound objects or just use PlayLocalSound. But remember that PlayLocalSound does not allow you to do any 3D effects.

7 Likes

Yeah, I noticed this. I tried uploading and playing a short looping theme for a character in mp3 format and it sounds like it loops with a delay.

@gkku’s post made me unsure about this, so I went and checked it out, apparently it does work with 3D sounds too. Pretty useful! (Or did you mean that it doesn’t move along with the basepart it is in / has no doppler effect / etc?)

Now I just need PlayLocalSound to accept an optional start time and end time, so it will play segments of sounds instead of the whole thing, and it’d be one of my favourite API members…

1 Like

Yeah, you can play any Sound object you want, but it won’t be played “in 3D” as far as I know. So no directional effect, no surround sound, no distance attenuation, and no doppler. It is perfectly suited for UI sounds though, and it will respect sound effects and sound groups.

It does actually attenuate for distance and direction in stereo, it’s just that it stays at one point even though the base part may be moving. Still useful for quickly parenting a sound to a static 3D object, playing it, and then unparenting the sound object I suppose

2 Likes

Oh, interesting!

The click happens because the sound changes frequencies instantly. MP3 prevents this by creating short fades at the beginning and end of an audio track, but it messes up if you want to loop something. As someone else already suggested if you want to make a perfect loop, use the .ogg file type.

1 Like

Yeah, I did understand that part, but being able to play client sound in 3D space would very useful feature.

EDIT: Just checked out the method again. First time I didn’t notice it accepted sound instance as argument, somehow assumed it was sound ID. So it does make sense that 3D sound already works. That’s resolved then, the feature is already in place.

I’m often approached by a lot of people asking how to fix this exact issue. I’m glad there’s a tutorial on here I can direct them to.
This tutorial is well explained and is easy to follow. Well done. :slight_smile:

1 Like

The parent doesn’t have to be Soundscape.
I just tested it and successfully made a part OUGH 50 times a second.

Thank you!

1 Like

To me, It’s a matter of raising awareness to this annoying issue which greatly improves the game in general.

I’ve even come across this on @Quenty 's WFYB’s Game, the issue can be found here if you navigate to the settings and start moving the slider forwards and backwards.


( no audio just demo )

  • Also it came to my mind that if there are no problems and .ogg usage has more advantages over other audio files, i.e: .mp3 can ROBLOX implement a file type conversion system?
2 Likes