# AudioProcessorValueTreeState::Listener::parameterChanged - get previous value?

**URL:** <https://forum.juce.com/t/audioprocessorvaluetreestate-parameterchanged-get-previous-value/58775>\
**Category:** Audio Plugins\
**Created:** [November 10, 2023, 8:03pm UTC](https://forum.juce.com/t/audioprocessorvaluetreestate-parameterchanged-get-previous-value/58775 "2023-11-10T20:03:19Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![AdamVenn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/adamvenn/32/17023_2.png) [@AdamVenn](https://forum.juce.com/u/AdamVenn)\
**Post date:** [November 10, 2023, 8:03pm UTC](https://forum.juce.com/t/audioprocessorvaluetreestate-parameterchanged-get-previous-value/58775/1 "2023-11-10T20:03:19Z")

</div>

AudioProcessorValueTreeState::Listener::parameterChanged() gives us the newValue, but what is the best/correct way to get the previous value?

---

<div class="post-metadata">

**Author:** ![kriskeillor](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/kriskeillor/32/20074_2.png) [@kriskeillor](https://forum.juce.com/u/kriskeillor)\
**Post date:** [November 10, 2023, 9:52pm UTC](https://forum.juce.com/t/audioprocessorvaluetreestate-parameterchanged-get-previous-value/58775/2 "2023-11-10T21:52:46Z")

</div>

Well, the parameterChanged callback requires you to assign newValue to your internal data structures yourself, so the previous value should be intact until you overwrite it, right? You can do what you need to do before writing.

---

<div class="post-metadata">

**Author:** ![Mrugalla](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mrugalla/32/13092_2.png) [@Mrugalla](https://forum.juce.com/u/Mrugalla)\
**Post date:** [November 11, 2023, 6:49pm UTC](https://forum.juce.com/t/audioprocessorvaluetreestate-parameterchanged-get-previous-value/58775/3 "2023-11-11T18:49:10Z")

</div>

```auto
struct PRM
{
    bool changed(float v)
    {
        if(val == v)
            return false;
        val = v;
        return true;
    }

    float val;
};

```

you could just have this stateful little thing to represent your parameter values in processBlock. i would personally also add parameter smoothing functionality to it, while you’re at it

---

<div class="post-metadata">

**Author:** ![AdamVenn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/adamvenn/32/17023_2.png) [@AdamVenn](https://forum.juce.com/u/AdamVenn)\
**Post date:** [November 11, 2023, 7:04pm UTC](https://forum.juce.com/t/audioprocessorvaluetreestate-parameterchanged-get-previous-value/58775/4 "2023-11-11T19:04:59Z")

</div>

Thanks, guys!

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [November 11, 2023, 8:29pm UTC](https://forum.juce.com/t/audioprocessorvaluetreestate-parameterchanged-get-previous-value/58775/5 "2023-11-11T20:29:43Z")

</div>

You might start getting warnings with that code, because you should not test float for equality for numerical reasons. Use [juce::approximatelyEqual()](https://docs.juce.com/master/group __juce__ core-maths.html#ga0b5e033b90fb3a43e0c48a48660e131d) instead:

```auto
    bool changed(float newValue)
    {
        if (juce::approximatelyEqual (newValue, value)) // you can set a tolerance here
            return false;

        value = newValue;
        return true;
    }

    float value = 0.0f;

```

---

<div class="post-metadata">

**Author:** ![AdamVenn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/adamvenn/32/17023_2.png) [@AdamVenn](https://forum.juce.com/u/AdamVenn)\
**Post date:** [November 11, 2023, 11:34pm UTC](https://forum.juce.com/t/audioprocessorvaluetreestate-parameterchanged-get-previous-value/58775/6 "2023-11-11T23:34:07Z")

</div>

Thank you, Daniel!

---

<div class="post-metadata">

**Author:** ![Mrugalla](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mrugalla/32/13092_2.png) [@Mrugalla](https://forum.juce.com/u/Mrugalla)\
**Post date:** [November 12, 2023, 8:08am UTC](https://forum.juce.com/t/audioprocessorvaluetreestate-parameterchanged-get-previous-value/58775/7 "2023-11-12T08:08:56Z")

</div>

right. and that approxEqual branch should trigger snap to val. i’m used to this due to my lowpass-based parameter smoother

---

<div class="post-metadata">

**Author:** ![kerfuffle](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/kerfuffle/32/11805_2.png) [@kerfuffle](https://forum.juce.com/u/kerfuffle)\
**Post date:** [November 12, 2023, 10:23am UTC](https://forum.juce.com/t/audioprocessorvaluetreestate-parameterchanged-get-previous-value/58775/8 "2023-11-12T10:23:12Z")

</div>

While the `approximatelyEqual` approach is useful for checking whether the change is large enough, `if (val == v)` is still a valid check to see whether the value actually changed or not. If this check returns false, `v` clearly is no longer the same as `val`, even if the difference is tiny.

---

<div class="post-metadata">

**Author:** ![Nitsuj70](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/nitsuj70/32/12754_2.png) [@Nitsuj70](https://forum.juce.com/u/Nitsuj70)\
**Post date:** [November 12, 2023, 3:53pm UTC](https://forum.juce.com/t/audioprocessorvaluetreestate-parameterchanged-get-previous-value/58775/9 "2023-11-12T15:53:10Z")

</div>

What numerical reasons though? As others have said, this is a legitimate case for testing exact equality bacause you want to know if the value has changed at all.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [November 12, 2023, 4:31pm UTC](https://forum.juce.com/t/audioprocessorvaluetreestate-parameterchanged-get-previous-value/58775/10 "2023-11-12T16:31:25Z")

</div>

It totally depends on your use case. Something what could happen is some accidental implicit cast to double and back…  
But I think we are bikeshedding at that point.

---

<div class="post-metadata">

**Author:** ![anthony-nicholls](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/anthony-nicholls/32/1246_2.png) [@anthony-nicholls](https://forum.juce.com/u/anthony-nicholls)\
**Post date:** [November 12, 2023, 5:55pm UTC](https://forum.juce.com/t/audioprocessorvaluetreestate-parameterchanged-get-previous-value/58775/11 "2023-11-12T17:55:31Z")

</div>

If your comparing say a normalised value but somewhere along the line a conversion has taken place between real parameter values and normalised parameter values, this could be a possible source to introduce a difference such that the exact equality may not work as expected. That being said if you are sure in your case that you really need exact comparisons, or maybe you’re OK with the false positives in favour of saving a few cycles, then you can also use [juce::exactlyEqual()](https://docs.juce.com/master/group __juce__ core-maths.html#ga3a88bf45ed4fb61151fe5eda49b550a3), this will silence any warnings, and also communicate to your future self that you really meant to do exact equality comparisons.

---

<div class="post-metadata">

**Author:** ![Nitsuj70](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/nitsuj70/32/12754_2.png) [@Nitsuj70](https://forum.juce.com/u/Nitsuj70)\
**Post date:** [November 12, 2023, 8:40pm UTC](https://forum.juce.com/t/audioprocessorvaluetreestate-parameterchanged-get-previous-value/58775/12 "2023-11-12T20:40:02Z")

</div>

In this particular case the value would only change at, say, the host. If nothing was altering the parameter there then the value would be the same. In principle, exactly the same. But yes, in my code I’m using juce::exactlyEqual() to avoid the warning.
