Audio Artefacts When Stopping/Destroying during play.

I’ve had this issue since I started using Unity, only I’ve never had it as a pressing enough problem to warrant exploring too much, but after much searching around and finding no other immediately visible topics on the issue (though there may be some which I simply have not found) I wanted to find out if this is a known issue and if there’s anything I can do about it.

Essentially often, when I stop an audio clip mid play, or destroy the object upon which the relevant audio source resides, the audio will cut with a awful spluttering click. I’m currently working on a title for full release, and I’m not sure that kind of thing is acceptable for something you would be expecting people to pay money for. Am I simply doing something wrong? I’ve had this same issue on several different PCs, over more than 2 years, and it happens both in editor and build.

Does anyone have any suggestions?

When you stop a sound in Unity, or it stops because its AudioSource was destroyed, the sound will stop instantly. This sometimes results in the waveform making a “leap” from high amplitude to zero, which results in the click or pop you’re hearing. To solve this, most commercial software for playing back music or other audio make a small fade-out when stopping the sound.

If you want your audio to behave similarly, you can either look at implementing a middleware for playing back sound that includes fades and smooth stops when destroying, or you can program your own little wrapper to do this whenever you stop a sound. If you’re going with middleware, I’d recommend Fabric (http://www.tazman-audio.co.uk/).

If you want to do it yourself, make a function that lowers the volume of the AudioSource over a short duration, then stops it when it has reached zero. This fade-out can be very short, a tenth of a second could suffice. Note that this will not work when you are destroying objects, unless you first move the AudioSource to a new GameObject at the same position, and destroy that when the sound has stopped.

Good luck!

I have the same problem, playing the sound thru any other software is just fine. But inside Unity both my runtime import and editor import ends with this spluttering click at the end.

Visualising the AudioClip data shows no artefacts at the end, so it must be issue inside Unity’s audio.

I managed to fix it by stopping the audioSource few samples before the end.

void Update() 
{
    if (audioSource.timeSamples > audioSource.clip.samples - 300) audioSource.Stop();
}

Another fix is to use different Load Type, just not Decompress On Load.

56035-ox63yx0.png

Also another fix is to convert the audio file into different format, converting from wav to mp3 using Audacity worked for me.

Here is the audio file that has this issue: https://dl.dropboxusercontent.com/u/70097519/UNITY/m4%20shoot.wav