# How change volume of a sampler sound without audio glitches?

**URL:** <https://forum.juce.com/t/how-change-volume-of-a-sampler-sound-without-audio-glitches/48715>\
**Category:** Tracktion Engine\
**Created:** [November 10, 2021, 4:49pm UTC](https://forum.juce.com/t/how-change-volume-of-a-sampler-sound-without-audio-glitches/48715 "2021-11-10T16:49:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ToxVox](https://avatars.discourse-cdn.com/v4/letter/t/76d3ee/32.png) [@ToxVox](https://forum.juce.com/u/ToxVox)\
**Post date:** [November 10, 2021, 4:49pm UTC](https://forum.juce.com/t/how-change-volume-of-a-sampler-sound-without-audio-glitches/48715/1 "2021-11-10T16:49:10Z")

</div>

I’m working with the built in sampler (te::SamplerPlugin) of the tracktion engine. I get audio glitches and very high cpu usage while changing the sound (setSoundGains()) with a slider.

```auto
volumeSlider.setSliderStyle (Slider::RotaryHorizontalVerticalDrag);
volumeSlider.setTextBoxStyle (Slider::NoTextBox, 0, 0, false);
volumeSlider.setRange (-48.0f, 0.0f, 0.10f);
volumeSlider.setValue(sampler->getSoundGainDb(soundIndex), dontSendNotification);
volumeSlider.setDoubleClickReturnValue (true, te::StepClip::defaultNoteValue);
volumeSlider.addListener(this);

```

```auto
void sliderValueChanged (Slider *slider) override
{
    sampler->setSoundGains(soundIndex, float(slider->getValue()), 0.0);
}

```

II guess that I generate unnecessary traffic on the message thread.

---

<div class="post-metadata">

**Author:** ![SourGummi](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@SourGummi](https://forum.juce.com/u/SourGummi)\
**Post date:** [November 10, 2021, 7:48pm UTC](https://forum.juce.com/t/how-change-volume-of-a-sampler-sound-without-audio-glitches/48715/2 "2021-11-10T19:48:12Z")

</div>

by using a gain ramp. [DSP Gain](https://docs.juce.com/master/classdsp_1_1Gain.html#details) as an example

### without it

 ![popop](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/0/0bbb219d046bc4a7fed4ade8c070e97a3fa56e70.png)

### with ramp

 ![popopop](https://us1.discourse-cdn.com/flex026/uploads/juce/original/2X/0/032963c9a17875d1415b6196149f67cf2ec8d22f.png)

the reason for clicks and such is, going from -INF to -1 in 1 sample theres no audio then suddenly its full volume. you need to process per sample and apply ramp value to smooth the differnce from -INF to x

---

<div class="post-metadata">

**Author:** ![psyzza](https://avatars.discourse-cdn.com/v4/letter/p/8edcca/32.png) [@psyzza](https://forum.juce.com/u/psyzza)\
**Post date:** [November 10, 2021, 11:54pm UTC](https://forum.juce.com/t/how-change-volume-of-a-sampler-sound-without-audio-glitches/48715/3 "2021-11-10T23:54:41Z")

</div>

In your `sample->setSoundGains()` method, you want to gradually increment the gain over a short period of time, to smoothen the transition, so it’s not a sudden change (which causes a click sound).

JUCE has a class called `SmoothedValue` which does this, like this:

```auto
// Create gain like this:
SmoothedValue<FloatType> gain(startingGain);

```

You’re going to want to set the time it takes to ramp before you use it, with:

```auto
gain.reset(sampleRate, numOfSecondsToTransition);

```

Then, to use it, first set the new target gain:

```auto
gain.setTargetValue(newGain);

```

It automatically transitions, you just have to get the current value:

```auto
float newGain = gain.getNextValue();

```

Then, you use `newGain` to set your gain on the next sampler update. Note that the gain is updated every time you call `getNextValue()`, so be careful not to call it twice (once for each channel) in stereo projects.

* * *

For a possibly simpler solution, JUCE’s `Gain` class uses this method, and is another way to smoothly adjust the gain. In this case, the sampler always plays the same gain, and you apply `Gain<FloatType>::processSample(sampleToAdjustGain)` to the output of the sampler to adjust the sample’s gain.

That works like this:

First you have to have the gain variable created as a property of the class:

```auto
Gain<FloatType> gain;

```

Then, after the sampler generates a sample, you edit the sample:

```auto
float newSample = sampler->generateNextSample();
gain.setGainLinear(newGain);
newSample = gain.processSample(newSample);

```

The issue with the second method is similar to how the first one generates a new gain value each time you call, `getNextValue()`. `Gain` does the same thing, so if you have a stereo configuration and need to adjust two different samples that occur at the same time, you’ll need two `Gain` instances.

You can also write your own. You really just have to transition instead of suddenly jumping to a new gain and your click should go away!

As far as high CPU usage, I don’t know. I think it depends on what `setSoundGains` is doing. Ideally, it just sets a few values, doing minimal. `Slider` also has a property called `onDragEnd` which is a lambda that is called only when the dragging has just ended. If you use that as the slider’s callback (instead of `sliderValueChanged`), it would only send a message when the slider is finished being dragged. The disadvantage of this is that you won’t be able to hear real-time updates while dragging the slider, but it would potentially reduce CPU usage (although the slider itself updating shouldn’t use that much CPU; it’s about the same as saying the mouse moved – these are high-volume events).

(Note: I haven’t tested the aforementioned code.)

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [November 11, 2021, 12:43am UTC](https://forum.juce.com/t/how-change-volume-of-a-sampler-sound-without-audio-glitches/48715/4 "2021-11-11T00:43:28Z")

</div>

Have you checked what the Tracktion Engine code actually ends up doing when you call the setSoundGains method? There are sometimes a bit “surprising” things that happen in the Tracktion Engine.

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [November 11, 2021, 11:17am UTC](https://forum.juce.com/t/how-change-volume-of-a-sampler-sound-without-audio-glitches/48715/5 "2021-11-11T11:17:23Z")

</div>

Yes, unfortunately `te::SamplerPlugin` is really a very minimal sampler implementation and setting sound gains isn’t supposed to be dynamic. Doing so will actually rebuild all the sounds asynchronously so will always cause a dropout.

If you want to do something more than just play some predetermined samples I suggest taking a copy of the class and heavily adapting it to suit your needs. Or you could write your own `te::Plugin` subclass.

---

<div class="post-metadata">

**Author:** ![ToxVox](https://avatars.discourse-cdn.com/v4/letter/t/76d3ee/32.png) [@ToxVox](https://forum.juce.com/u/ToxVox)\
**Post date:** [November 11, 2021, 12:54pm UTC](https://forum.juce.com/t/how-change-volume-of-a-sampler-sound-without-audio-glitches/48715/6 "2021-11-11T12:54:34Z")

</div>

Thanks @SourGummi @xenakios @psyzza @dave96 for your comments!  
I already suspected that. I think I’ll check `te::SamplerPlugin` and copy them.
