Click noise

Hello to everybody,
I downloaded some helicopter sound effects from the Internet. Now I’m testing them to decide which one suits my game best. I created a sound effect instance to play the sounds. When the sound starts everything is ok, while the sound is playing everything is still ok, but when I stop the sound instance I hear an annoying “click” noise, just when the sound stops. Sometimes the click noise is louder than others, and not all the sound effects have the same issue. I tried to set the volume of the instance to zero instead of stopping it and the click doesn’t occur, but if I set the volume to zero and immediately after I stop the sound, the click is still there. Maybe I can try to set the volume to zero and after a few moments stop the instance, but I’m looking for a better solution. Has anyone else had the same issue?
Thanks

Can you provide some of the code used to play the sound? There may be something off in there.

Declarations:

    SoundEffect soundEffect;
    SoundEffectInstance soundEffectInstance;

In the LoadContent method:

        soundEffect = Content.Load<SoundEffect>("r3");
        soundEffectInstance = soundEffect.CreateInstance();
        soundEffectInstance.IsLooped = true;

In the Update method:

        if (Keyboard.GetState().IsKeyDown(Keys.D1) && soundEffectInstance.State == SoundState.Stopped)
        {
            soundEffectInstance.Play();
        }
        if (Keyboard.GetState().IsKeyDown(Keys.D2) && soundEffectInstance.State == SoundState.Playing)
        {
            soundEffectInstance.Stop();
        }

Exactly when the instance is stopped, the “click” (or I should say “tick”) noise occurs. I don’t think it’s a question of code but of how the wave that produces the sound is made, since not all the sound effects I downloaded from the internet have the same issue.
As I said, if I do:

            soundEffectInstance.Volume = 0;

instead of:

            soundEffectInstance.Stop();

the “click” doesn’t occur, but I don’t want to use such a solution.

Does the issue occur if IsLooped is false?

Yes, the issue occurs anyway.

Since nobody has a solution, I will adopt this trick: when I want to stop the sound effect, first I set the volume to 0, and only during the next game frame I actually stop the instance. This way no “click” or “tick” occurs.
Thank you anyway.

Glad you found a solution! Which version of MonoGame are you using and which platform? There’s a possibility of a bug with stopping sound effects. For example, this issue shows there’s a difference in Stop between the Windows and DesktopGL platforms and this issue revealed some more platform inconsistencies (which have since been fixed). If you’re on an older version of MonoGame, consider upgrading and seeing if that helps.

The issues you indicate seem a bit different from mine. Anyway, I’m using Monogame 3.7.1 on a Windows Project.

If someone would like to do some tests, he can download one of the sounds in question from here
When you stop the instance, sometimes you can hear a “tick” noise, more or less loud, and sometimes it doesn’t occur.

I also tried:

    SoundEffectInstance.Stop(false);

But this doesn’t help, since it stops the sound only when the current loop has finished.
Mmmhh… it seems it doesn’t work, anyway.

If anyone should be interested, I did a test with a Cross Platform Desktop Project and the issue is the same.
I hope it will be fixed in the future.

I think this is an issue with the laws of physics :smile:

If you stop any sound immediately when it’s waveform is not at a zero-crossing (i.e. amplitude zero) you will get a click as you are basically creating a square wave. The click will be worse the higher the amplitude of the waveform at that point.

This phenomenon is usually particularly obvious on low-frequency content due to the lower frequency of zero crossings.

It’s a common ‘noob’ question from people doing sound design on monophonic synthesizers.

Theoretically setting the volume to zero would also create a click but maybe there is a very fast ramp down going on under the hood? Ideally you want to do a very fast fade out.

3 Likes

Dunno if its relevant but I’m currently doing a lot of reading on open al and stumbled across this stackoverflow:

1 Like

To confirm what you say, I did some tests with the sound whose waveform is visible in the following image.


As you can see, the waveform never cross the zero amplitude.
So, I created a SoundEffectInstance with the property IsLooped set to true. When I start the sound, I can hear a very clean click noise. While the sound is playing no clicks occur. When I stop the sound, again I hear the clean click.
I tried to set the volume to 0 instead of stopping the sound and I can still hear the click noise, but grimer (or duller) and not so loud. In such a case, a fast fade out should be applied. In other cases, as I said, it’s enough to set the volume to 0 to avoid the click.
If I normalize the same sound, as in the following image
so that the waveform is always near the zero amplitude, no clicks occur at all.
I was hoping there was a simple solution programmatically but it doesn’t seem so.

OK, well you seem to have it figured out to an extent but looking at your original sound file that is largely where the problem is!!

No sound file should look like that, it should be centered on zero amplitude. Your file has what’s known as a ‘DC offset’, and a ridiculous one at that, given that it doesn’t pass through zero at all!

If you have a lot of files like this you may be able to find an app that will batch process them to get rid of the DC offset.

What I said about sudden stops vs fades still applies but if you fix your sound files you may get away without this.

In fact, that was a sound that I came across some time ago and that I just wanted to use here to demonstrate what you said.
Instead, one of the sounds I downloaded for my game looks like this:


It appears as a normal sound and it is perfectly centered on the zero amplitude, but when I stop the sound effect instance I can hear the click, sometimes louder and sometimes less.
However, the fact remains that to avoid these clicks you need to set the volume to 0 or, if it’s not enough, apply a fade out.

If you zoom that waveform, you’ll probably see something like this.

If you stop your sound at A, you’ll get a big click.
If you stop your sound at B, you’ll get a softer clck
If you stop your sound at C (zero), you’ll get no click.
If you stop your sound at D, you’ll get a big click too.

If you stop your sound between B and C, you’ll get a louder click than stopping at C but softer than stopping at B.

As you can’t really control where you stop the waveform, sometimes you stop it at A, sometimes at C. That’s why sometimes you get a click, and sometimes not.

The only way to avoid that is fading out instead of stopping.

As Bitbull said, I don’t know why setting volume to 0 doesn’t produce a click. Actually it should produce a click too. It’s probably doing a quick fadeout instead of setting it directly to 0.

The concept is clear now. So these clicks should be a common issue for all game developers. Therefore I think that Monogame could implement a method that automatically prevents these clicks instead of forcing each programmer to create a FadeOut method manually. It would be practical for everyone. But I guess I expect too much.