OpenShot Audio Library | OpenShotAudio: juce_SmoothedValue.cpp Source File

OpenShot Audio Library | OpenShotAudio 0.3.0

Loading...
Searching...
No Matches
juce_SmoothedValue.cpp
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2018 - ROLI Ltd.
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26#if JUCE_UNIT_TESTS
27
28static CommonSmoothedValueTests <SmoothedValue<float, ValueSmoothingTypes::Linear>> commonLinearSmoothedValueTests;
29static CommonSmoothedValueTests <SmoothedValue<float, ValueSmoothingTypes::Multiplicative>> commonMultiplicativeSmoothedValueTests;
30
31class SmoothedValueTests : public UnitTest
32{
33public:
34 SmoothedValueTests()
35 : UnitTest ("SmoothedValueTests", UnitTestCategories::smoothedValues)
36 {}
37
38 void runTest() override
39 {
40 beginTest ("Linear moving target");
41 {
42 SmoothedValue<float, ValueSmoothingTypes::Linear> sv;
43
44 sv.reset (12);
45 float initialValue = 0.0f;
46 sv.setCurrentAndTargetValue (initialValue);
47 sv.setTargetValue (1.0f);
48
49 auto delta = sv.getNextValue() - initialValue;
50
51 sv.skip (6);
52
53 auto newInitialValue = sv.getCurrentValue();
54 sv.setTargetValue (newInitialValue + 2.0f);
55 auto doubleDelta = sv.getNextValue() - newInitialValue;
56
57 expectWithinAbsoluteError (doubleDelta, delta * 2.0f, 1.0e-7f);
58 }
59
60 beginTest ("Multiplicative curve");
61 {
62 SmoothedValue<double, ValueSmoothingTypes::Multiplicative> sv;
63
64 auto numSamples = 12;
65 AudioBuffer<double> values (2, numSamples + 1);
66
67 sv.reset (numSamples);
68 sv.setCurrentAndTargetValue (1.0);
69 sv.setTargetValue (2.0f);
70
71 values.setSample (0, 0, sv.getCurrentValue());
72
73 for (int i = 1; i < values.getNumSamples(); ++i)
74 values.setSample (0, i, sv.getNextValue());
75
76 sv.setTargetValue (1.0f);
77 values.setSample (1, values.getNumSamples() - 1, sv.getCurrentValue());
78
79 for (int i = values.getNumSamples() - 2; i >= 0 ; --i)
80 values.setSample (1, i, sv.getNextValue());
81
82 for (int i = 0; i < values.getNumSamples(); ++i)
83 expectWithinAbsoluteError (values.getSample (0, i), values.getSample (1, i), 1.0e-9);
84 }
85 }
86};
87
88static SmoothedValueTests smoothedValueTests;
89
90#endif
91
92} // namespace juce