# Avoiding glitches in AudioSampleBuffer::applyGain

**URL:** <https://forum.juce.com/t/avoiding-glitches-in-audiosamplebuffer-applygain/7692>\
**Category:** General JUCE discussion\
**Created:** [November 11, 2011, 9:52am UTC](https://forum.juce.com/t/avoiding-glitches-in-audiosamplebuffer-applygain/7692 "2011-11-11T09:52:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![masshacker](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/masshacker/32/650_2.png) [@masshacker](https://forum.juce.com/u/masshacker)\
**Post date:** [November 11, 2011, 9:52am UTC](https://forum.juce.com/t/avoiding-glitches-in-audiosamplebuffer-applygain/7692/1 "2011-11-11T09:52:10Z")

</div>

Is there a way to avoid audio glitches when calling AudioSampleBuffer::applyGain in an audio callback with the gain parameter changing quickly?  
Maybe I’m missing something obvious :roll:

---

<div class="post-metadata">

**Author:** ![anima](https://avatars.discourse-cdn.com/v4/letter/a/c67d28/32.png) [@anima](https://forum.juce.com/u/anima)\
**Post date:** [November 11, 2011, 10:27am UTC](https://forum.juce.com/t/avoiding-glitches-in-audiosamplebuffer-applygain/7692/2 "2011-11-11T10:27:24Z")

</div>

```auto

Maybe the ramp version? buffer 1 -> (start gain 0.5), (end gain 1.0), next buffer-> (startgain 1.0, end gain 0.2) etc

That way you'll get a smooth transition for every buffer and per sample adjustments.
```

Maybe the ramp version? buffer 1 -\> (start gain 0.5), (end gain 1.0), next buffer-\> (startgain 1.0, end gain 0.2) etc

That way you’ll get a smooth transition for every buffer and per sample adjustments.

---

<div class="post-metadata">

**Author:** ![masshacker](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/masshacker/32/650_2.png) [@masshacker](https://forum.juce.com/u/masshacker)\
**Post date:** [November 11, 2011, 10:33am UTC](https://forum.juce.com/t/avoiding-glitches-in-audiosamplebuffer-applygain/7692/3 "2011-11-11T10:33:59Z")

</div>

Yes, I was aware of applyGainRamp, but in that case I don’t understand how can I get 2 gain values, since I have just one gain value (the one coming from, let’s say, a slider)…

EDIT: (always think 3 times before posting) nevermind, I did it! Thank you 😃

---

<div class="post-metadata">

**Author:** ![TheVinn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/thevinn/32/806_2.png) [@TheVinn](https://forum.juce.com/u/TheVinn)\
**Post date:** [November 14, 2011, 12:27pm UTC](https://forum.juce.com/t/avoiding-glitches-in-audiosamplebuffer-applygain/7692/4 "2011-11-14T12:27:19Z")

</div>

DSPFilters Demo has a perfect example of a volume control knob that smooths its parameter changes. Same with the playback speed control.

---

<div class="post-metadata">

**Author:** ![billythekid](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/billythekid/32/88_2.png) [@billythekid](https://forum.juce.com/u/billythekid)\
**Post date:** [November 14, 2011, 2:20pm UTC](https://forum.juce.com/t/avoiding-glitches-in-audiosamplebuffer-applygain/7692/5 "2011-11-14T14:20:03Z")

</div>

> [@masshacker](#):
>
> I don’t understand how can I get 2 gain values, since I have just one gain value (the one coming from, let’s say, a slider)…

You have the current gain value and a gain value from the past. I think the applyGainRamp function is a linear fade. You may wish to consider connecting the previous and current values with a one-pole lowpass filter. You can then just set the time constant of the filter to alter the sluggishness with which the internal gain value follows the slider value.
