|
| 1 | +/* |
| 2 | + ============================================================================== |
| 3 | +
|
| 4 | + This file is part of the JUCE library. |
| 5 | + Copyright (c) 2015 - ROLI Ltd. |
| 6 | +
|
| 7 | + Permission is granted to use this software under the terms of either: |
| 8 | + a) the GPL v2 (or any later version) |
| 9 | + b) the Affero GPL v3 |
| 10 | +
|
| 11 | + Details of these licenses can be found at: www.gnu.org/licenses |
| 12 | +
|
| 13 | + JUCE is distributed in the hope that it will be useful, but WITHOUT ANY |
| 14 | + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 15 | + A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 16 | +
|
| 17 | + ------------------------------------------------------------------------------ |
| 18 | +
|
| 19 | + To release a closed-source product which uses JUCE, commercial licenses are |
| 20 | + available: visit www.juce.com for more information. |
| 21 | +
|
| 22 | + ============================================================================== |
| 23 | +*/ |
| 24 | + |
| 25 | +#include "LinearSmoothedValueAtomic.h" |
| 26 | + |
| 27 | + |
| 28 | +// Explicit instantiations |
| 29 | +template class LinearSmoothedValueAtomic<float>; |
| 30 | +template class LinearSmoothedValueAtomic<double>; |
| 31 | + |
| 32 | + |
| 33 | +template <typename FloatType> |
| 34 | +LinearSmoothedValueAtomic<FloatType>::LinearSmoothedValueAtomic() noexcept |
| 35 | + : currentValue (0) |
| 36 | + , target (0) |
| 37 | + , step (0) |
| 38 | + , countdown (0) |
| 39 | + , stepsToTarget (0) |
| 40 | +{ |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +template <typename FloatType> |
| 45 | +LinearSmoothedValueAtomic<FloatType>::LinearSmoothedValueAtomic (FloatType initialValue) noexcept |
| 46 | + |
| 47 | + : currentValue (initialValue) |
| 48 | + , target (initialValue) |
| 49 | + , step (0) |
| 50 | + , countdown (0) |
| 51 | + , stepsToTarget (0) |
| 52 | +{ |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +template<typename FloatType> |
| 57 | +void LinearSmoothedValueAtomic<FloatType>::reset (double sampleRate, double rampLengthInSeconds) noexcept |
| 58 | +{ |
| 59 | + jassert (sampleRate > 0 && rampLengthInSeconds >= 0); |
| 60 | + stepsToTarget = (int) std::floor (rampLengthInSeconds * sampleRate); |
| 61 | + currentValue = target; |
| 62 | + countdown = 0; |
| 63 | +} |
| 64 | + |
| 65 | + |
| 66 | +template<typename FloatType> |
| 67 | +void LinearSmoothedValueAtomic<FloatType>::setValue (FloatType newValue) noexcept |
| 68 | +{ |
| 69 | + target.store (newValue); |
| 70 | +} |
| 71 | + |
| 72 | + |
| 73 | +template<typename FloatType> |
| 74 | +void LinearSmoothedValueAtomic<FloatType>::updateTarget() noexcept |
| 75 | +{ |
| 76 | + FloatType newTarget = target.load(); |
| 77 | + if (newTarget != currentTarget) |
| 78 | + { |
| 79 | + currentTarget = newTarget; |
| 80 | + countdown = stepsToTarget; |
| 81 | + |
| 82 | + if (countdown <= 0) |
| 83 | + currentValue = currentTarget; |
| 84 | + else |
| 85 | + step = (currentTarget - currentValue) / (FloatType) countdown; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | + |
| 90 | +template<typename FloatType> |
| 91 | +FloatType LinearSmoothedValueAtomic<FloatType>::getNextValue() noexcept |
| 92 | +{ |
| 93 | + if (countdown <= 0) |
| 94 | + return currentTarget; |
| 95 | + |
| 96 | + --countdown; |
| 97 | + currentValue += step; |
| 98 | + return currentValue; |
| 99 | +} |
0 commit comments