~ubuntu-branches/ubuntu/hardy/lmms/hardy

« back to all changes in this revision

Viewing changes to include/interpolation.h

  • Committer: Bazaar Package Importer
  • Author(s): Florian Ragwitz
  • Date: 2005-12-22 16:22:50 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20051222162250-key3p7x0212jy6dn
Tags: 0.1.2-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * interpolation.h - fast implementations of several interpolation-algorithms
3
 
 *
4
 
 * Linux MultiMedia Studio
5
 
 * Copyright (c) 2004-2005 Tobias Doerffel <tobydox@users.sourceforge.net>
6
 
 *
7
 
 * This program is free software; you can redistribute it and/or
8
 
 * modify it under the terms of the GNU General Public
9
 
 * License as published by the Free Software Foundation; either
10
 
 * version 2 of the License, or (at your option) any later version.
11
 
 *
12
 
 * This program is distributed in the hope that it will be useful,
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 
 * General Public License for more details.
16
 
 *
17
 
 * You should have received a copy of the GNU General Public
18
 
 * License along with this program (see COPYING); if not, write to the
19
 
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20
 
 * Boston, MA 02111-1307, USA.
21
 
 *
22
 
 */
23
 
 
24
 
 
25
 
#ifndef _INTERPOLATION_H
26
 
#define _INTERPOLATION_H
27
 
 
28
 
#ifndef __USE_XOPEN
29
 
#define __USE_XOPEN
30
 
#endif
31
 
 
32
 
#include <math.h>
33
 
 
34
 
 
35
 
inline float hermiteInterpolate( float x0, float x1, float x2, float x3,
36
 
                                                                float frac_pos )
37
 
{
38
 
        const float frsq = frac_pos*frac_pos;
39
 
        const float frsq2 = 2*frsq;
40
 
        return( ( (x2-x0) *0.5f ) * ( frac_pos * (frsq+1) -frsq2 ) +
41
 
                                ( frsq2*frac_pos - 3*frsq ) * ( x1-x2 ) +
42
 
                        frsq2 * (frac_pos-1) * ( ( x3-x1 ) * 0.25f ) + x1 );
43
 
 
44
 
/*
45
 
   const float frsq     = frac_pos*frac_pos;
46
 
   //const float frsq2  = 2*frsq;
47
 
   frac_pos *= 0.5;
48
 
   const float frcu     = frsq*frac_pos;
49
 
   return (
50
 
   
51
 
   (frcu - frsq + frac_pos) * ((x2 - x0)) +
52
 
   
53
 
   (4*frcu - 3*frsq) * (x1 - x2)
54
 
   //frsq*(2*frac_pos-3) * (x1 - x2)
55
 
   
56
 
   + (frcu - 0.5*frsq)*((x3 - x1))  
57
 
    
58
 
   + x1
59
 
   
60
 
   );
61
 
*/
62
 
}
63
 
 
64
 
 
65
 
 
66
 
inline float cubicInterpolate( float v0, float v1, float v2, float v3, float x )
67
 
{
68
 
        float frsq = x*x;
69
 
        float frcu = frsq*v0;
70
 
        float t1 = v3 + 3*v1;
71
 
 
72
 
        return( v1 + 0.5f * frcu + x * ( v2 - frcu * ( 1.0f/6.0f ) -
73
 
                t1 * ( 1.0f/6.0f ) - v0 / 3.0f ) + frsq * x * ( t1 *
74
 
                ( 1.0f/6.0f ) - 0.5f * v2 ) + frsq * ( 0.5f * v2 - v1 ) );
75
 
}
76
 
 
77
 
 
78
 
 
79
 
inline float cosinusInterpolate( float v0, float v1, float x )
80
 
{
81
 
        float f = cosf( x * ( M_PI*0.5f ) );
82
 
        return( v0*f + v1*( 1.0f-f ) );
83
 
}
84
 
 
85
 
 
86
 
 
87
 
inline float linearInterpolate( float v0, float v1, float x )
88
 
{
89
 
        return( v0*( 1.0f-x ) + v1*x );
90
 
}
91
 
 
92
 
 
93
 
#endif
 
1
/*
 
2
 * interpolation.h - fast implementations of several interpolation-algorithms
 
3
 *
 
4
 * Copyright (c) 2004-2005 Tobias Doerffel <tobydox/at/users.sourceforge.net>
 
5
 * 
 
6
 * This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net
 
7
 *
 
8
 * This program is free software; you can redistribute it and/or
 
9
 * modify it under the terms of the GNU General Public
 
10
 * License as published by the Free Software Foundation; either
 
11
 * version 2 of the License, or (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 GNU
 
16
 * General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU General Public
 
19
 * License along with this program (see COPYING); if not, write to the
 
20
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
21
 * Boston, MA 02111-1307, USA.
 
22
 *
 
23
 */
 
24
 
 
25
 
 
26
#ifndef _INTERPOLATION_H
 
27
#define _INTERPOLATION_H
 
28
 
 
29
#ifndef __USE_XOPEN
 
30
#define __USE_XOPEN
 
31
#endif
 
32
 
 
33
#include <math.h>
 
34
 
 
35
 
 
36
inline float hermiteInterpolate( float x0, float x1, float x2, float x3,
 
37
                                                                float frac_pos )
 
38
{
 
39
        const float frsq = frac_pos*frac_pos;
 
40
        const float frsq2 = 2*frsq;
 
41
        return( ( (x2-x0) *0.5f ) * ( frac_pos * (frsq+1) -frsq2 ) +
 
42
                                ( frsq2*frac_pos - 3*frsq ) * ( x1-x2 ) +
 
43
                        frsq2 * (frac_pos-1) * ( ( x3-x1 ) * 0.25f ) + x1 );
 
44
 
 
45
/*
 
46
   const float frsq     = frac_pos*frac_pos;
 
47
   //const float frsq2  = 2*frsq;
 
48
   frac_pos *= 0.5;
 
49
   const float frcu     = frsq*frac_pos;
 
50
   return (
 
51
   
 
52
   (frcu - frsq + frac_pos) * ((x2 - x0)) +
 
53
   
 
54
   (4*frcu - 3*frsq) * (x1 - x2)
 
55
   //frsq*(2*frac_pos-3) * (x1 - x2)
 
56
   
 
57
   + (frcu - 0.5*frsq)*((x3 - x1))  
 
58
    
 
59
   + x1
 
60
   
 
61
   );
 
62
*/
 
63
}
 
64
 
 
65
 
 
66
 
 
67
inline float cubicInterpolate( float v0, float v1, float v2, float v3, float x )
 
68
{
 
69
        float frsq = x*x;
 
70
        float frcu = frsq*v0;
 
71
        float t1 = v3 + 3*v1;
 
72
 
 
73
        return( v1 + 0.5f * frcu + x * ( v2 - frcu * ( 1.0f/6.0f ) -
 
74
                t1 * ( 1.0f/6.0f ) - v0 / 3.0f ) + frsq * x * ( t1 *
 
75
                ( 1.0f/6.0f ) - 0.5f * v2 ) + frsq * ( 0.5f * v2 - v1 ) );
 
76
}
 
77
 
 
78
 
 
79
 
 
80
inline float cosinusInterpolate( float v0, float v1, float x )
 
81
{
 
82
        float f = cosf( x * ( M_PI*0.5f ) );
 
83
        return( v0*f + v1*( 1.0f-f ) );
 
84
}
 
85
 
 
86
 
 
87
 
 
88
inline float linearInterpolate( float v0, float v1, float x )
 
89
{
 
90
        return( v0*( 1.0f-x ) + v1*x );
 
91
}
 
92
 
 
93
 
 
94
#endif