tracktion-engine 3.0-10-g034fdde4aa5
Tracktion Engine — High level data model for audio applications

« « « Anklang Documentation
Loading...
Searching...
No Matches
tracktion_Spline.h
Go to the documentation of this file.
1 /*
2 ,--. ,--. ,--. ,--.
3 ,-' '-.,--.--.,--,--.,---.| |,-.,-' '-.`--' ,---. ,--,--, Copyright 2024
4 '-. .-'| .--' ,-. | .--'| /'-. .-',--.| .-. || \ Tracktion Software
5 | | | | \ '-' \ `--.| \ \ | | | |' '-' '| || | Corporation
6 `---' `--' `--`--'`---'`--'`--' `---' `--' `---' `--''--' www.tracktion.com
7
8 Tracktion Engine uses a GPL/commercial licence - see LICENCE.md for details.
9*/
10
11namespace tracktion { inline namespace engine
12{
13
17struct Spline
18{
19 int getNumPoints() const noexcept { return points.size(); }
20 juce::Point<float> getPoint (int index) const { return points.getReference (index); }
21
22 float getY (float x) const
23 {
24 const int num = points.size();
25
26 int i = num;
27 while (--i >= 0)
28 if (points.getReference(i).x <= x)
29 break;
30
31 if (i >= num - 1)
32 return points.getLast().y;
33
34 if (i < 0)
35 return points.getFirst().y;
36
37 auto p0 = points.getReference (juce::jmax (0, i - 1));
38 auto p1 = points.getReference (i);
39 auto p2 = points.getReference (i + 1);
40 auto p3 = points.getReference (juce::jmin (num - 1, i + 2));
41
42 const float alpha = (x - p1.x) / (p2.x - p1.x);
43 const float alpha2 = alpha * alpha;
44 const float a0 = p3.y - p2.y - p0.y + p1.y;
45 return (a0 * alpha * alpha2) + ((p0.y - p1.y - a0) * alpha2) + ((p2.y - p0.y) * alpha) + p1.y;
46 }
47
48 //==============================================================================
49 void clear() { points.clear(); }
50 void removePoint (int index) { points.remove (index); }
51
52 void addPoint (float x, float y)
53 {
54 int i = points.size();
55
56 while (--i >= 0)
57 {
58 juce::Point<float>& p = points.getReference(i);
59
60 if (p.x <= x)
61 {
62 if (p.x == x)
63 {
64 p.y = y;
65 return;
66 }
67
68 break;
69 }
70 }
71
72 points.insert (++i, { x, y });
73 }
74
75
76private:
78};
79
80}} // namespace tracktion { inline namespace engine
int size() const noexcept
void remove(int indexToRemove)
void insert(int indexToInsertAt, ParameterType newElement)
ElementType getFirst() const noexcept
void clear()
ElementType & getReference(int index) noexcept
ElementType getLast() const noexcept
constexpr Type jmin(Type a, Type b)
constexpr Type jmax(Type a, Type b)
An interpolated spline curve, used by the EQ to draw its response graph.