- Home /
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?
Answer by Jonatan Crafoord · Oct 15, 2015 at 03:13 PM
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!
The thing is I had this isue with the m4 shoot audio, from the image you can see that it has a fade out inside it self. At the end the of it the amplitude is zero so there is no leap and thus there should be no pop/click ? Yet if it is wav and decompress on load you hear the pop/click. Try it your self: https://dl.dropboxusercontent.com/u/70097519/UNITY/m4%20shoot.wav simply make scene with this audio in the audio source and let it play only once. Load type has to be decompress on load.
Answer by aeroson · Oct 12, 2015 at 04:43 PM
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.
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
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
UnityWebRequestMultimedia.GetAudioClip dont't work in WebGL build (2019.3.15f1). 1 Answer
Generate instances of prefab, then save to GameObject array... How? 1 Answer