OpenShot Audio Library | OpenShotAudio 0.4.0
 
Loading...
Searching...
No Matches
juce_Interpolators.h
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
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
35{
36private:
37 struct WindowedSincTraits
38 {
39 static constexpr float algorithmicLatency = 100.0f;
40
41 static forcedinline float windowedSinc (float firstFrac, int index) noexcept
42 {
43 auto index2 = index + 1;
44 auto frac = firstFrac;
45
46 auto value1 = lookupTable[index];
47 auto value2 = lookupTable[index2];
48
49 return value1 + (frac * (value2 - value1));
50 }
51
52 static forcedinline float valueAtOffset (const float* const inputs, const float offset, int indexBuffer) noexcept
53 {
54 const int numCrossings = 100;
55 const float floatCrossings = (float) numCrossings;
56 float result = 0.0f;
57
58 auto samplePosition = indexBuffer;
59 float firstFrac = 0.0f;
60 float lastSincPosition = -1.0f;
61 int index = 0, sign = -1;
62
63 for (int i = -numCrossings; i <= numCrossings; ++i)
64 {
65 auto sincPosition = (1.0f - offset) + (float) i;
66
67 if (i == -numCrossings || (sincPosition >= 0 && lastSincPosition < 0))
68 {
69 auto indexFloat = (sincPosition >= 0.f ? sincPosition : -sincPosition) * 100.0f;
70 auto indexFloored = std::floor (indexFloat);
71 index = (int) indexFloored;
72 firstFrac = indexFloat - indexFloored;
73 sign = (sincPosition < 0 ? -1 : 1);
74 }
75
76 if (exactlyEqual (sincPosition, 0.0f))
77 result += inputs[samplePosition];
78 else if (sincPosition < floatCrossings && sincPosition > -floatCrossings)
79 result += inputs[samplePosition] * windowedSinc (firstFrac, index);
80
81 if (++samplePosition == numCrossings * 2)
82 samplePosition = 0;
83
84 lastSincPosition = sincPosition;
85 index += 100 * sign;
86 }
87
88 return result;
89 }
90
91 static const float lookupTable[10001];
92 };
93
94 struct LagrangeTraits
95 {
96 static constexpr float algorithmicLatency = 2.0f;
97
98 static float valueAtOffset (const float*, float, int) noexcept;
99 };
100
101 struct CatmullRomTraits
102 {
103 //==============================================================================
104 static constexpr float algorithmicLatency = 2.0f;
105
106 static forcedinline float valueAtOffset (const float* const inputs, const float offset, int index) noexcept
107 {
108 auto y0 = inputs[index]; if (++index == 4) index = 0;
109 auto y1 = inputs[index]; if (++index == 4) index = 0;
110 auto y2 = inputs[index]; if (++index == 4) index = 0;
111 auto y3 = inputs[index];
112
113 auto halfY0 = 0.5f * y0;
114 auto halfY3 = 0.5f * y3;
115
116 return y1 + offset * ((0.5f * y2 - halfY0)
117 + (offset * (((y0 + 2.0f * y2) - (halfY3 + 2.5f * y1))
118 + (offset * ((halfY3 + 1.5f * y1) - (halfY0 + 1.5f * y2))))));
119 }
120 };
121
122 struct LinearTraits
123 {
124 static constexpr float algorithmicLatency = 1.0f;
125
126 static forcedinline float valueAtOffset (const float* const inputs, const float offset, int index) noexcept
127 {
128 auto y0 = inputs[index];
129 auto y1 = inputs[index == 0 ? 1 : 0];
130
131 return y1 * offset + y0 * (1.0f - offset);
132 }
133 };
134
135 struct ZeroOrderHoldTraits
136 {
137 static constexpr float algorithmicLatency = 0.0f;
138
139 static forcedinline float valueAtOffset (const float* const inputs, const float, int) noexcept
140 {
141 return inputs[0];
142 }
143 };
144
145public:
151};
152
153//==============================================================================
171using WindowedSincInterpolator = Interpolators::WindowedSinc;
172
188using LagrangeInterpolator = Interpolators::Lagrange;
189
205using CatmullRomInterpolator = Interpolators::CatmullRom;
206
222using LinearInterpolator = Interpolators::Linear;
223
241using ZeroOrderHoldInterpolator = Interpolators::ZeroOrderHold;
242
243} // namespace juce