hydrogen 1.2.6
Interpolation.h
Go to the documentation of this file.
1/*
2 * Hydrogen
3 * Copyright(c) 2002-2008 by Alex >Comix< Cominu [comix@users.sourceforge.net]
4 * Copyright(c) 2008-2025 The hydrogen development team [hydrogen-devel@lists.sourceforge.net]
5 *
6 * http://www.hydrogen-music.org
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY, without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see https://www.gnu.org/licenses
20 *
21 */
22
23#ifndef INTERPOLATION_H
24#define INTERPOLATION_H
25
26#include <cmath>
27
28namespace H2Core
29{
30
32{
33 enum class InterpolateMode { Linear = 0,
34 Cosine = 1,
35 Third = 2,
36 Cubic = 3,
37 Hermite = 4 };
38
39 static const QString ModeToQString( InterpolateMode mode )
40 {
41 switch ( mode ) {
42 case InterpolateMode::Linear:
43 return "Linear";
44 case InterpolateMode::Cosine:
45 return "Cosine";
46 case InterpolateMode::Third:
47 return "Third";
48 case InterpolateMode::Cubic:
49 return "Cubic";
50 case InterpolateMode::Hermite:
51 return "Hermite";
52 default:
53 return "<unknown>";
54 }
55 }
56
57 inline static float linear_Interpolate( float y1, float y2, float mu )
58 {
59 /*
60 * mu defines where to estimate the value on the interpolated line
61 * y1 = buffervalue on position
62 * y2 = buffervalue on position +1
63 */
64 return y1 * ( 1 - mu ) + y2 * mu;
65 };
66
67 inline static float cosine_Interpolate( float y1, float y2, double mu )
68 {
69 /*
70 * mu defines where to estimate the value on the interpolated line
71 * y1 = buffervalue on position
72 * y2 = buffervalue on position +1
73 */
74 double mu2;
75
76 mu2 = ( 1 - cos ( mu * 3.14159 ) ) / 2;
77 return( y1 * (1 - mu2 ) + y2 * mu2 );
78 };
79
80 inline static float third_Interpolate( float y0, float y1, float y2, float y3, double mu )
81 {
82 /*
83 * mu defines where to estimate the value on the interpolated line
84 * y0 = buffervalue on position -1
85 * y1 = buffervalue on position
86 * y2 = buffervalue on position +1
87 * y3 = buffervalue on position +2
88 */
89
90 float c0 = y1;
91 float c1 = 0.5f * ( y2 - y0 );
92 float c3 = 1.5f * ( y1 - y2 ) + 0.5f * ( y3 - y0 );
93 float c2 = y0 - y1 + c1 - c3;
94 return ( ( c3 * mu + c2 ) * mu + c1 ) * mu + c0;
95 };
96
97 inline static float cubic_Interpolate( float y0, float y1, float y2, float y3, double mu)
98 {
99 /*
100 * mu defines where to estimate the value on the interpolated line
101 * y0 = buffervalue on position -1
102 * y1 = buffervalue on position
103 * y2 = buffervalue on position +1
104 * y3 = buffervalue on position +2
105 */
106
107 double a0, a1, a2, a3, mu2;
108
109 mu2 = mu * mu;
110 a0 = y3 - y2 - y0 + y1;
111 a1 = y0 - y1 - a0;
112 a2 = y2 - y0;
113 a3 = y1;
114
115 return( a0 * mu * mu2 + a1 * mu2 + a2 * mu + a3 );
116 };
117
118 inline static float hermite_Interpolate( float y0, float y1, float y2, float y3, double mu )
119 {
120 /*
121 * mu defines where to estimate the value on the interpolated line
122 * y0 = buffervalue on position -1
123 * y1 = buffervalue on position
124 * y2 = buffervalue on position +1
125 * y3 = buffervalue on position +2
126 */
127
128 double a0, a1, a2, a3, mu2;
129
130 mu2 = mu * mu;
131 a0 = -0.5 * y0 + 1.5 * y1 - 1.5 * y2 + 0.5 * y3;
132 a1 = y0 - 2.5 * y1 + 2 * y2 - 0.5 * y3;
133 a2 = -0.5 * y0 + 0.5 * y2;
134 a3 = y1;
135
136 return( a0 * mu * mu2 + a1 * mu2 + a2 * mu + a3 );
137 };
138
139};
140
141}
142
143#endif // INTERPOLATION_H
static const QString ModeToQString(InterpolateMode mode)
static float cosine_Interpolate(float y1, float y2, double mu)
static float cubic_Interpolate(float y0, float y1, float y2, float y3, double mu)
static float linear_Interpolate(float y1, float y2, float mu)
static float hermite_Interpolate(float y0, float y1, float y2, float y3, double mu)
static float third_Interpolate(float y0, float y1, float y2, float y3, double mu)