# Artifacts when adjusting gain while audio still playing

**URL:** <https://forum.juce.com/t/artifacts-when-adjusting-gain-while-audio-still-playing/13297>\
**Category:** General JUCE discussion\
**Created:** [August 4, 2014, 5:49am UTC](https://forum.juce.com/t/artifacts-when-adjusting-gain-while-audio-still-playing/13297 "2014-08-04T05:49:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![brain-deaf](https://avatars.discourse-cdn.com/v4/letter/b/eb9ed0/32.png) [@brain-deaf](https://forum.juce.com/u/brain-deaf)\
**Post date:** [August 4, 2014, 5:49am UTC](https://forum.juce.com/t/artifacts-when-adjusting-gain-while-audio-still-playing/13297/1 "2014-08-04T05:49:01Z")

</div>

I'm new to audio proessing in general so I'm sure this'll be straight forward for somebody to answer, but I honestly can't find much information about this online.

If I adjust the gain value before playing the audio and THEN play the audio, its gain is adjusted correctly and there are no artifacts. If I adjust the gain during playback, the gain is adjusted properly but there are all kinds of clicks in the sound. Is there a different way of implementing gain other than a simple multiplier on the output? What am I missing here? The extra stack allocation that might happen from retrieving a member by value can't possibly cause the problem, could it?

In the renderNextBlock() function of the SamplerVoice class, I'm adjusting the signal's gain like so:

void SampleVoice::renderNextBlock(AudioSampleBuffer& buffer, int startSample, int numSamples){  
&nbsp; &nbsp; SampleSound::Ptr s = (SampleSound::Ptr)getCurrentlyPlayingSound();  
&nbsp; &nbsp; if (s != nullptr){  
&nbsp; &nbsp; &nbsp; &nbsp; double sample\_length = s-\>getAudioData()-\>getNumSamples();  
&nbsp; &nbsp; &nbsp; &nbsp; int num\_channels = s-\>getAudioData()-\>getNumChannels();  
&nbsp; &nbsp; &nbsp; &nbsp; if (getCurrentlyPlayingSound().get()){

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//get write pointers for buffer, etc...

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i= start; i\<numSamples+start; i++){  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const int pos = (int) samplePosition;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const double alpha = (double) (samplePosition - pos);  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const double invAlpha = 1.0f - alpha;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // just using a very simple linear interpolation here..  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; float l = ((inL[pos]) \* invAlpha + &nbsp;(inL[pos+1]) \* alpha);  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; float r = inR != nullptr ? (inR[pos]) \* invAlpha + (inR[pos+1] \* alpha) : l;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \*outL += l\*noteEvent-\>getVolume();  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \*outR += r\*noteEvent-\>getVolume();  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outL++;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outR++;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; samplePosition += pitchRatio;  
&nbsp; &nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; }  
}

&nbsp;

noteEvent-\>getVolume() refers to a member containing a gain multiplier (double) value ranging from 0.0 to 1.0. Why would changing this value result in clicks in the sound? The audio DOES get quieter or louder when I adjust the volume, but it's also very clicky. Not sure what's up. There's no allocation involved with updating that member or retrieving its value.  
&nbsp;

---

<div class="post-metadata">

**Author:** ![brain-deaf](https://avatars.discourse-cdn.com/v4/letter/b/eb9ed0/32.png) [@brain-deaf](https://forum.juce.com/u/brain-deaf)\
**Post date:** [August 4, 2014, 7:15am UTC](https://forum.juce.com/t/artifacts-when-adjusting-gain-while-audio-still-playing/13297/2 "2014-08-04T07:15:00Z")

</div>

I fixed it by changing the gain in increments of the amount changed. For instance, if the gain multiplier was set to 0.8 from 1.0, that's a difference of -0.2 gain. For each sample in the callback, add the difference / numSamples to the previous gain setting to create a slurring or gradual gain change.&nbsp;

---

<div class="post-metadata">

**Author:** ![lalala](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/lalala/32/8577_2.png) [@lalala](https://forum.juce.com/u/lalala)\
**Post date:** [August 4, 2014, 8:40am UTC](https://forum.juce.com/t/artifacts-when-adjusting-gain-while-audio-still-playing/13297/3 "2014-08-04T08:40:40Z")

</div>

yes, you'll be&nbsp;fine making a ramp between the actual gain and the target one. that's the usual way to avoid what's commonly called 'zipper noise'.

---

<div class="post-metadata">

**Author:** ![brain-deaf](https://avatars.discourse-cdn.com/v4/letter/b/eb9ed0/32.png) [@brain-deaf](https://forum.juce.com/u/brain-deaf)\
**Post date:** [August 4, 2014, 4:03pm UTC](https://forum.juce.com/t/artifacts-when-adjusting-gain-while-audio-still-playing/13297/4 "2014-08-04T16:03:45Z")

</div>

"zipper noise," interesting. I have a lot to learn :)

---

<div class="post-metadata">

**Author:** ![jimc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jimc/32/16067_2.png) [@jimc](https://forum.juce.com/u/jimc)\
**Post date:** [August 4, 2014, 4:38pm UTC](https://forum.juce.com/t/artifacts-when-adjusting-gain-while-audio-still-playing/13297/5 "2014-08-04T16:38:36Z")

</div>

It gets quite complicated if you aren't careful.&nbsp;There are a bunch of solutions to the problem. &nbsp;Here's the low-pass filter solution from the music-dsp archives.&nbsp;

http://www.musicdsp.org/archive.php?classid=3#257

I've got a little set of&nbsp;classes which do the ramp solution, the important one I've put here:

http://pastebin.com/7JbpEDbV

But I don't really like my current approach. See the&nbsp;comments.&nbsp;
