This project has moved and is read-only. For the latest updates, please go here.

Clicking noise while changing volume/pan

Feb 29, 2016 at 5:46 AM
Hi, Im using the NAudio driver in an multi-track loop application I'm building.
Incidentally, I'm using a customized class implementing ISampleProvider, which is a sort of combination of a pan provider and volume provider in the same class, and also adds a "Normalizing" factor to normalize the audio.
(Each track has its own instance of this "CustomSampleProvider", and each is then added to a MixingSampleProvider, which is used to initialize a WaveOut which is then played.)

Essentially, it all works fine, except for one little issue which is audible clicking sounds only when the values for volume or pan are changed (values being provided by a slider and rotating knob).
To be clear, the clicks only occur while the slider or knob is being adjusted. Once the new values are set, there is no more noise.

I'm sure my PC is not the problem (my system is Win7 on a i7-2600 3.6GHz machine)
Now , I thought it might be my customized code, or something to do with the GUI, but then I tried using the NAudio FadeInOutSampleProvider as is, and I find I'm getting the same clicking during programmed fade ins/fade outs (for example, 2 seconds or longer).

My CustomSampleProvider Read method simply does this:
     buffer(System.Math.Max(System.Threading.Interlocked.Increment(outIndex), outIndex - 1)) = lpan * Me.sourceBuffer(n) * (m_volume * NormFactor)
            buffer(System.Math.Max(System.Threading.Interlocked.Increment(outIndex), outIndex - 1)) = rpan * Me.sourceBuffer(n) * (m_volume * NormFactor)
(in other words, nothing too fancy, just multiplying by a pan factor, volume factor and normalizing factor)

Any ideas why the clicking occurs when the values are being changed?

Michael
Feb 29, 2016 at 11:46 AM
Addendum:
Don't know if this is related, but I just found that I'm getting the same clicks if I move the form (WinForm) at all during playback. Maybe it is a GUI issue...?
Mar 7, 2016 at 2:00 PM
Perhaps it´s a latency issue
Mar 7, 2016 at 3:29 PM
why are you doing interlocked increments in your Read method. Taking many thousands of locks every second is not going to perform well. I'd also copy the NormFactor into a local variable to save repeated property gets.
Mar 9, 2016 at 10:40 AM
markheath wrote:
why are you doing interlocked increments in your Read method. Taking many thousands of locks every second is not going to perform well. I'd also copy the NormFactor into a local variable to save repeated property gets.
Um, funny story behind that:-)
Having borrowed the PanningSampleProvider, and not reading C# very well, I converted it to VB with Telerik which translated "buffer[outIndex++] = " to "buffer(System.Math.Max(System.Threading.Interlocked.Increment(outIndex), outIndex - 1)) ="

Anyway, fixed that so my code is now simply:
For n As Integer = 0 To sourceSamplesRead - 1
            buffer(outIndex) = lpan * Me.sourceBuffer(n) * (m_volume * NormFactor)
            buffer(outIndex + 1) = rpan * Me.sourceBuffer(n) * (m_volume * NormFactor)
            outIndex += 2
        Next n
Totally still getting those subtle glitches whenever I move the pan or volume knobs though :(
(Or move the form)
Incidentally, that NormFactor is just a variable, calculated only once when the user toggles Normalization on or off.
Mar 11, 2016 at 12:46 AM
If that glitch happens with moving the form or clicking gui elements I suppose you use WaveOut instead of WaveOutEvent.

Anyway, why you work with two sample buffers (buffer and sourceBuffer)? Perhaps they are not in sync or the double copy takes too much cpu.

I can just guess for the lack of details here.