iPlug2 - C++ Audio Plug-in Framework
Loading...
Searching...
No Matches
Easing.h
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the iPlug 2 library. Copyright (C) the iPlug 2 developers.
5
6 See LICENSE.txt for more info.
7
8 ==============================================================================
9*/
10
11#pragma once
12
19#ifndef _USE_MATH_DEFINES
20#define _USE_MATH_DEFINES
21#endif
22#include <math.h>
23#include "IPlugPlatform.h"
24
25BEGIN_IPLUG_NAMESPACE
26
27// line y = x ^ 1/c;
28template<class T>
29T EasePowCurve(T x, T c)
30{
31 return std::pow(x, 1.0 / c);
32}
33
34// line y = x
35template<class T>
36T EaseLinear(T x)
37{
38 return x;
39}
40
41// parabola y = x^2
42template<class T>
43T EaseQuadraticIn(T x)
44{
45 return x * x;
46}
47
48// parabola y = -x^2 + 2x
49template<class T>
50T EaseQuadraticOut(T x)
51{
52 return -(x * (x - 2));
53}
54
55// piecewise quadratic
56// y = (1/2)((2x)^2) ; [0, 0.5)
57// y = -(1/2)((2x-1)*(2x-3) - 1) ; [0.5, 1]
58template<class T>
59T EaseQuadraticInOut(T x)
60{
61 if(x < 0.5)
62 {
63 return 2 * x * x;
64 }
65 else
66 {
67 return (-2 * x * x) + (4 * x) - 1;
68 }
69}
70
71// cubic y = x^3
72template<class T>
73T EaseCubicIn(T x)
74{
75 return x * x * x;
76}
77
78// cubic y = (x - 1)^3 + 1
79template<class T>
80T EaseCubicOut(T x)
81{
82 T f = (x - 1);
83 return f * f * f + 1;
84}
85
86// piecewise cubic
87// y = (1/2)((2x)^3) ; [0, 0.5)
88// y = (1/2)((2x-2)^3 + 2) ; [0.5, 1]
89template<class T>
90T EaseCubicInOut(T x)
91{
92 if(x < 0.5)
93 {
94 return 4 * x * x * x;
95 }
96 else
97 {
98 T f = ((2 * x) - 2);
99 return 0.5 * f * f * f + 1;
100 }
101}
102
103// quartic x^4
104template<class T>
105T EaseQuarticIn(T x)
106{
107 return x * x * x * x;
108}
109
110// quartic y = 1 - (x - 1)^4
111template<class T>
112T EaseQuarticOut(T x)
113{
114 T f = (x - 1);
115 return f * f * f * (1 - x) + 1;
116}
117
118// piecewise quartic
119// y = (1/2)((2x)^4) ; [0, 0.5)
120// y = -(1/2)((2x-2)^4 - 2) ; [0.5, 1]
121template<class T>
122T EaseQuarticInOut(T x)
123{
124 if(x < 0.5)
125 {
126 return 8 * x * x * x * x;
127 }
128 else
129 {
130 T f = (x - 1);
131 return -8 * f * f * f * f + 1;
132 }
133}
134
135// quintic y = x^5
136template<class T>
137T EaseQuinticIn(T x)
138{
139 return x * x * x * x * x;
140}
141
142// quintic y = (x - 1)^5 + 1
143template<class T>
144T EaseQuinticOut(T x)
145{
146 T f = (x - 1);
147 return f * f * f * f * f + 1;
148}
149
150// piecewise quintic
151// y = (1/2)((2x)^5) ; [0, 0.5)
152// y = (1/2)((2x-2)^5 + 2) ; [0.5, 1]
153template<class T>
154T EaseQuinticInOut(T x)
155{
156 if(x < 0.5)
157 {
158 return 16 * x * x * x * x * x;
159 }
160 else
161 {
162 T f = ((2 * x) - 2);
163 return 0.5 * f * f * f * f * f + 1;
164 }
165}
166
167// Modeled after quarter-cycle of sine wave
168template<class T>
169T EaseSineIn(T x)
170{
171 return std::sin((x - 1) * M_PI_2) + 1;
172}
173
174// Modeled after quarter-cycle of sine wave (different phase)
175template<class T>
176T EaseSineOut(T x)
177{
178 return std::sin(x * M_PI_2);
179}
180
181// Modeled after half sine wave
182template<class T>
183T EaseSineInOut(T x)
184{
185 return 0.5 * (1 - std::cos(x * M_PI));
186}
187
188// Modeled after shifted quadrant IV of unit circle
189template<class T>
190T EaseCircularIn(T x)
191{
192 return 1 - std::sqrt(1 - (x * x));
193}
194
195// Modeled after shifted quadrant II of unit circle
196template<class T>
197T EaseCircularOut(T x)
198{
199 return std::sqrt((2 - x) * x);
200}
201
202// piecewise circular function
203// y = (1/2)(1 - std::sqrt(1 - 4x^2)) ; [0, 0.5)
204// y = (1/2)(std::sqrt(-(2x - 3)*(2x - 1)) + 1) ; [0.5, 1]
205template<class T>
206T EaseCircularInOut(T x)
207{
208 if(x < 0.5)
209 {
210 return 0.5 * (1 - std::sqrt(1 - 4 * (x * x)));
211 }
212 else
213 {
214 return 0.5 * (std::sqrt(-((2 * x) - 3) * ((2 * x) - 1)) + 1);
215 }
216}
217
218// exponential function y = 2^(10(x - 1))
219template<class T>
220T EaseExponentialIn(T x)
221{
222 return (x == 0.0) ? x : std::pow(2, 10 * (x - 1));
223}
224
225// exponential function y = -2^(-10x) + 1
226template<class T>
227T EaseExponentialOut(T x)
228{
229 return (x == 1.0) ? x : 1 - std::pow(2, -10 * x);
230}
231
232// piecewise exponential
233// y = (1/2)2^(10(2x - 1)) ; [0,0.5)
234// y = -(1/2)*2^(-10(2x - 1))) + 1 ; [0.5,1]
235template<class T>
236T EaseExponentialInOut(T x)
237{
238 if(x == 0.0 || x == 1.0) return x;
239
240 if(x < 0.5)
241 {
242 return 0.5 * std::pow(2, (20 * x) - 10);
243 }
244 else
245 {
246 return -0.5 * std::pow(2, (-20 * x) + 10) + 1;
247 }
248}
249
250// damped sine wave y = std::sin(13pi/2*x)*std::pow(2, 10 * (x - 1))
251template<class T>
252T EaseElasticIn(T x)
253{
254 return std::sin(13 * M_PI_2 * x) * std::pow(2, 10 * (x - 1));
255}
256
257// damped sine wave y = std::sin(-13pi/2*(x + 1))*std::pow(2, -10x) + 1
258template<class T>
259T EaseElasticOut(T x)
260{
261 return std::sin(-13 * M_PI_2 * (x + 1)) * std::pow(2, -10 * x) + 1;
262}
263
264// piecewise exponentially-damped sine wave:
265// y = (1/2)*std::sin(13pi/2*(2*x))*std::pow(2, 10 * ((2*x) - 1)) ; [0,0.5)
266// y = (1/2)*(std::sin(-13pi/2*((2x-1)+1))*std::pow(2,-10(2*x-1)) + 2) ; [0.5, 1]
267template<class T>
268T EaseElasticInOut(T x)
269{
270 if(x < 0.5)
271 {
272 return 0.5 * std::sin(13 * M_PI_2 * (2 * x)) * std::pow(2, 10 * ((2 * x) - 1));
273 }
274 else
275 {
276 return 0.5 * (std::sin(-13 * M_PI_2 * ((2 * x - 1) + 1)) * std::pow(2, -10 * (2 * x - 1)) + 2);
277 }
278}
279
280// overshooting cubic y = x^3-x*std::sin(x*pi)
281template<class T>
282T EaseBackIn(T x)
283{
284 return x * x * x - x * std::sin(x * M_PI);
285}
286
287// Modeled after overshooting cubic y = 1-((1-x)^3-(1-x)*std::sin((1-x)*pi))
288template<class T>
289T EaseBackOut(T x)
290{
291 T f = (1 - x);
292 return 1 - (f * f * f - f * std::sin(f * M_PI));
293}
294
295// piecewise overshooting cubic function:
296// y = (1/2)*((2x)^3-(2x)*std::sin(2*x*pi)) ; [0, 0.5)
297// y = (1/2)*(1-((1-x)^3-(1-x)*std::sin((1-x)*pi))+1) ; [0.5, 1]
298template<class T>
299T EaseBackInOut(T x)
300{
301 if(x < 0.5)
302 {
303 T f = 2 * x;
304 return 0.5 * (f * f * f - f * std::sin(f * M_PI));
305 }
306 else
307 {
308 T f = (1 - (2*x - 1));
309 return 0.5 * (1 - (f * f * f - f * std::sin(f * M_PI))) + 0.5;
310 }
311}
312
313template<class T>
314T EaseBounceOut(T x)
315{
316 if(x < 4/11.0)
317 {
318 return (121 * x * x)/16.0;
319 }
320 else if(x < 8/11.0)
321 {
322 return (363/40.0 * x * x) - (99/10.0 * x) + 17/5.0;
323 }
324 else if(x < 9/10.0)
325 {
326 return (4356/361.0 * x * x) - (35442/1805.0 * x) + 16061/1805.0;
327 }
328 else
329 {
330 return (54/5.0 * x * x) - (513/25.0 * x) + 268/25.0;
331 }
332}
333
334template<class T>
335T EaseBounceIn(T x)
336{
337 return 1 - EaseBounceOut(1 - x);
338}
339
340template<class T>
341T EaseBounceInOut(T x)
342{
343 if(x < 0.5)
344 {
345 return 0.5 * EaseBounceIn(x*2);
346 }
347 else
348 {
349 return 0.5 * EaseBounceOut(x * 2 - 1) + 0.5;
350 }
351}
352
353END_IPLUG_NAMESPACE
Include to get consistently named preprocessor macros for different platforms and logging functionali...