35template <
typename SmoothedValueType>
42 template <
template <
typename>
class SmoothedValueClass,
typename FloatType>
48 template <
template <
typename,
typename>
class SmoothedValueClass,
typename FloatType,
typename SmoothingType>
93 for (
int i = 0;
i < numSamples; ++
i)
98 FloatVectorOperations::multiply (samples,
target, numSamples);
114 for (
int i = 0;
i < numSamples; ++
i)
119 FloatVectorOperations::multiply (samplesOut, samplesIn,
target, numSamples);
130 if (buffer.getNumChannels() == 1)
132 auto* samples = buffer.getWritePointer (0);
134 for (
int i = 0;
i < numSamples; ++
i)
139 for (
auto i = 0;
i < numSamples; ++
i)
143 for (
int channel = 0; channel < buffer.getNumChannels(); channel++)
144 buffer.setSample (channel,
i, buffer.getSample (channel,
i) * gain);
150 buffer.applyGain (0, numSamples,
target);
158 return static_cast <SmoothedValueType*
> (
this)->getNextValue();
225template <
typename FloatType,
typename SmoothingType = ValueSmoothingTypes::Linear>
240 jassert (! (std::is_same<SmoothingType, ValueSmoothingTypes::Multiplicative>::value && initialValue == 0));
252 void reset (
double sampleRate,
double rampLengthInSeconds)
noexcept
254 jassert (sampleRate > 0 && rampLengthInSeconds >= 0);
255 reset ((
int) std::floor (rampLengthInSeconds * sampleRate));
273 if (newValue == this->
target)
283 jassert (! (std::is_same<SmoothingType, ValueSmoothingTypes::Multiplicative>::value && newValue == 0));
340 [[deprecated (
"Use setTargetValue and setCurrentAndTargetValue instead.")]]
355 template <
typename T>
356 using LinearVoid =
typename std::enable_if <std::is_same <T, ValueSmoothingTypes::Linear>::value,
void>
::type;
358 template <
typename T>
362 template <
typename T = SmoothingType>
368 template <
typename T = SmoothingType>
375 template <
typename T = SmoothingType>
381 template <
typename T = SmoothingType>
388 template <
typename T = SmoothingType>
394 template <
typename T = SmoothingType>
405template <
typename FloatType>
413template <
class SmoothedValueType>
414class CommonSmoothedValueTests :
public UnitTest
417 CommonSmoothedValueTests()
421 void runTest()
override
423 beginTest (
"Initial state");
425 SmoothedValueType sv;
427 auto value = sv.getCurrentValue();
428 expectEquals (sv.getTargetValue(),
value);
431 expectEquals (sv.getCurrentValue(),
value);
432 expect (! sv.isSmoothing());
435 beginTest (
"Resetting");
437 auto initialValue = 15.0f;
439 SmoothedValueType sv (initialValue);
441 expectEquals (sv.getCurrentValue(), initialValue);
443 auto targetValue = initialValue + 1.0f;
444 sv.setTargetValue (targetValue);
445 expectEquals (sv.getTargetValue(), targetValue);
446 expectEquals (sv.getCurrentValue(), initialValue);
447 expect (sv.isSmoothing());
449 auto currentValue = sv.getNextValue();
450 expect (currentValue > initialValue);
451 expectEquals (sv.getCurrentValue(), currentValue);
452 expectEquals (sv.getTargetValue(), targetValue);
453 expect (sv.isSmoothing());
457 expectEquals (sv.getCurrentValue(), targetValue);
458 expectEquals (sv.getTargetValue(), targetValue);
459 expect (! sv.isSmoothing());
462 expectEquals (sv.getCurrentValue(), targetValue);
464 sv.setTargetValue (1.5f);
467 float newStart = 0.2f;
468 sv.setCurrentAndTargetValue (newStart);
469 expectEquals (sv.getNextValue(), newStart);
470 expectEquals (sv.getTargetValue(), newStart);
471 expectEquals (sv.getCurrentValue(), newStart);
472 expect (! sv.isSmoothing());
475 beginTest (
"Sample rate");
477 SmoothedValueType svSamples { 3.0f };
478 auto svTime = svSamples;
480 auto numSamples = 12;
482 svSamples.reset (numSamples);
483 svTime.reset (numSamples * 2, 1.0);
485 for (
int i = 0;
i < numSamples; ++
i)
488 expectWithinAbsoluteError (svSamples.getNextValue(),
489 svTime.getNextValue(),
494 beginTest (
"Block processing");
496 SmoothedValueType sv (1.0f);
499 sv.setTargetValue (2.0f);
501 const auto numSamples = 15;
505 for (
int i = 0;
i < numSamples; ++
i)
506 referenceData.
setSample (0,
i, sv.getNextValue());
508 expect (referenceData.
getSample (0, 0) > 0);
509 expect (referenceData.
getSample (0, 10) < sv.getTargetValue());
510 expectWithinAbsoluteError (referenceData.
getSample (0, 11),
514 auto getUnitData = [] (
int numSamplesToGenerate)
518 for (
int i = 0;
i < numSamplesToGenerate; ++
i)
527 for (
int i = 0;
i <
test.getNumSamples(); ++
i)
528 expectWithinAbsoluteError (
test.getSample (0,
i),
529 reference.getSample (0,
i),
533 auto testData = getUnitData (numSamples);
534 sv.setCurrentAndTargetValue (1.0f);
535 sv.setTargetValue (2.0f);
536 sv.applyGain (testData.getWritePointer (0), numSamples);
537 compareData (testData, referenceData);
539 testData = getUnitData (numSamples);
541 sv.setCurrentAndTargetValue (1.0f);
542 sv.setTargetValue (2.0f);
544 testData.getReadPointer (0),
546 compareData (destData, referenceData);
547 compareData (testData, getUnitData (numSamples));
549 testData = getUnitData (numSamples);
550 sv.setCurrentAndTargetValue (1.0f);
551 sv.setTargetValue (2.0f);
552 sv.applyGain (testData, numSamples);
553 compareData (testData, referenceData);
558 SmoothedValueType sv;
561 sv.setCurrentAndTargetValue (1.0f);
562 sv.setTargetValue (2.0f);
566 for (
int i = 0;
i < 15; ++
i)
567 reference.
add (sv.getNextValue());
569 sv.setCurrentAndTargetValue (1.0f);
570 sv.setTargetValue (2.0f);
572 expectWithinAbsoluteError (sv.skip (1), reference[0], 1.0e-6f);
573 expectWithinAbsoluteError (sv.skip (1), reference[1], 1.0e-6f);
574 expectWithinAbsoluteError (sv.skip (2), reference[3], 1.0e-6f);
576 expectWithinAbsoluteError (sv.getCurrentValue(), reference[6], 1.0e-6f);
577 expectEquals (sv.skip (300), sv.getTargetValue());
578 expectEquals (sv.getCurrentValue(), sv.getTargetValue());
581 beginTest (
"Negative");
583 SmoothedValueType sv;
586 sv.reset (numValues);
588 std::vector<std::pair<float, float>> ranges = { { -1.0f, -2.0f },
589 { -100.0f, -3.0f } };
591 for (
auto range : ranges)
593 auto start = range.first,
end = range.second;
595 sv.setCurrentAndTargetValue (
start);
596 sv.setTargetValue (
end);
598 auto val = sv.skip (numValues / 2);
605 auto nextVal = sv.getNextValue();
608 auto endVal = sv.skip (500);
609 expectEquals (endVal,
end);
610 expectEquals (sv.getNextValue(),
end);
611 expectEquals (sv.getCurrentValue(),
end);
613 sv.setCurrentAndTargetValue (
start);
614 sv.setTargetValue (
end);
616 SmoothedValueType positiveSv { -
start };
617 positiveSv.reset (numValues);
618 positiveSv.setTargetValue (-
end);
620 for (
int i = 0;
i < numValues + 2; ++
i)
621 expectEquals (sv.getNextValue(), -positiveSv.getNextValue());
#define noexcept
Definition DistrhoDefines.h:72
CAdPlugDatabase::CRecord::RecordType type
Definition adplugdb.cpp:93
Definition juce_Array.h:56
void add(const ElementType &newElement)
Definition juce_Array.h:418
Definition juce_AudioSampleBuffer.h:34
Type getSample(int channel, int sampleIndex) const noexcept
Definition juce_AudioSampleBuffer.h:636
Type * getWritePointer(int channelNumber) noexcept
Definition juce_AudioSampleBuffer.h:291
void setSample(int destChannel, int destSample, Type newValue) noexcept
Definition juce_AudioSampleBuffer.h:651
FloatType target
Definition juce_SmoothedValue.h:164
FloatType currentValue
Definition juce_SmoothedValue.h:163
bool isSmoothing() const noexcept
Definition juce_SmoothedValue.h:63
int countdown
Definition juce_SmoothedValue.h:165
SmoothedValueBase()=default
void applyGain(FloatType *samples, int numSamples) noexcept
Definition juce_SmoothedValue.h:87
FloatType getNextSmoothedValue() noexcept
Definition juce_SmoothedValue.h:156
void applyGain(AudioBuffer< FloatType > &buffer, int numSamples) noexcept
Definition juce_SmoothedValue.h:124
void setCurrentAndTargetValue(FloatType newValue)
Definition juce_SmoothedValue.h:75
FloatType getTargetValue() const noexcept
Definition juce_SmoothedValue.h:70
FloatType getCurrentValue() const noexcept
Definition juce_SmoothedValue.h:66
typename FloatTypeHelper< SmoothedValueType >::Type FloatType
Definition juce_SmoothedValue.h:55
void applyGain(FloatType *samplesOut, const FloatType *samplesIn, int numSamples) noexcept
Definition juce_SmoothedValue.h:108
FloatType skip(int numSamples) noexcept
Definition juce_SmoothedValue.h:316
FloatType getNextValue() noexcept
Definition juce_SmoothedValue.h:295
void setValue(FloatType newValue, bool force=false) noexcept
Definition juce_SmoothedValue.h:341
int stepsToTarget
Definition juce_SmoothedValue.h:402
MultiplicativeVoid< T > setNextValue() noexcept
Definition juce_SmoothedValue.h:382
typename std::enable_if< std::is_same< T, ValueSmoothingTypes::Multiplicative >::value, void >::type MultiplicativeVoid
Definition juce_SmoothedValue.h:359
LinearVoid< T > skipCurrentValue(int numSamples) noexcept
Definition juce_SmoothedValue.h:389
LinearVoid< T > setStepSize() noexcept
Definition juce_SmoothedValue.h:363
SmoothedValue(FloatType initialValue) noexcept
Definition juce_SmoothedValue.h:237
void reset(double sampleRate, double rampLengthInSeconds) noexcept
Definition juce_SmoothedValue.h:252
FloatType step
Definition juce_SmoothedValue.h:401
MultiplicativeVoid< T > skipCurrentValue(int numSamples)
Definition juce_SmoothedValue.h:395
LinearVoid< T > setNextValue() noexcept
Definition juce_SmoothedValue.h:376
SmoothedValue() noexcept
Definition juce_SmoothedValue.h:231
void reset(int numSteps) noexcept
Definition juce_SmoothedValue.h:261
MultiplicativeVoid< T > setStepSize()
Definition juce_SmoothedValue.h:369
typename std::enable_if< std::is_same< T, ValueSmoothingTypes::Linear >::value, void >::type LinearVoid
Definition juce_SmoothedValue.h:356
void setTargetValue(FloatType newValue) noexcept
Definition juce_SmoothedValue.h:271
Definition juce_UnitTest.h:70
register unsigned i
Definition inflate.c:1575
static PuglViewHint int value
Definition pugl.h:1708
virtual ASIOError start()=0
int val
Definition jpeglib.h:956
static const String smoothedValues
Definition juce_UnitTestCategories.h:46
Definition juce_SmoothedValue.h:179
Definition carla_juce.cpp:31
RangedDirectoryIterator end(const RangedDirectoryIterator &)
Definition juce_RangedDirectoryIterator.h:184
SmoothedValue< FloatType, ValueSmoothingTypes::Linear > LinearSmoothedValue
Definition juce_SmoothedValue.h:406
Definition juce_Uuid.h:141
static int test(SerdEnv *env, bool top_level, bool pretty_numbers)
Definition sratom_test.c:79
FloatType Type
Definition juce_SmoothedValue.h:51
FloatType Type
Definition juce_SmoothedValue.h:45
Definition juce_SmoothedValue.h:40
Definition juce_SmoothedValue.h:185
Definition juce_SmoothedValue.h:192
int result
Definition process.c:1455
#define const
Definition zconf.h:137