Search code examples
c#naudio

Sound clipping/clicking when lowering volume with NAudio


Audio is clipping (or clicking) when trying to lower the volume of a WAV file in real time.

I've tried it on a SampleChannel, VolumeSampleProvider and WaveChannel32 instance, the source being a 32bit WAV file.

If I try it on a WaveOut instance, the clipping doesn't occur anymore, but I don't want that because it lowers the volume of all sounds in the application.

And this only happens when I lower volume, rising it doesn't cause clipping.

Is this a known problem or am I supposed to approach this differently?

Note: this is how the volume drops in real time over the given time span:

0.9523049
0.9246111
0.9199954
0.89384
0.8676848
0.8415294
0.8169126
0.7907572
0.7646018
0.739985
0.7122912
0.6892129
0.6630576
0.6369023
0.6122856
0.5861301
0.5599748
0.535358
0.5092026
0.4830474
0.456892
0.4322752
0.4061199
0.3799645
0.3553477
0.3276539
0.3030371
0.2784202
0.2522649
0.2261095
0.2014928
0.176876
0.149182
0.1245652
0.09841
0.07225461
0.04763785
0.02148246
0

Solution

  • Apparently it is a DesiredLatency and NumberOfBuffers issue on the WaveOut instance. The default values cause the problem, altered values fix it.

    These are the values I used to fix the issue.

     WaveOutDevice = new WaveOut(WaveCallbackInfo.NewWindow())
            {
                DesiredLatency = 700, //! adjust the value to prevent sound clipping/clicking
                NumberOfBuffers = 3, //! number of buffers must be carefully selected to prevent audio clipping/clicking
            };