*** empty log message ***
[chuck-blob.git] / v2 / util_math.c
blob21674dfe87a95b170e65d56489c545bd41dfaf1a
1 /*----------------------------------------------------------------------------
2 ChucK Concurrent, On-the-fly Audio Programming Language
3 Compiler and Virtual Machine
5 Copyright (c) 2004 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.cs.princeton.edu/
7 http://soundlab.cs.princeton.edu/
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 U.S.A.
23 -----------------------------------------------------------------------------*/
25 //-----------------------------------------------------------------------------
26 // name: util_math.c
27 // desc: ...
29 // author: Ge Wang (gewang@cs.princeton.edu)
30 // Perry R. Cook (prc@cs.princeton.edu)
31 // Philip Davidson (philipd@alumni.princeton.edu)
32 // date: Spring 2004
33 //-----------------------------------------------------------------------------
34 #include "util_math.h"
35 #include <math.h>
38 // windows / visual c++
39 #ifdef __PLATFORM_WIN32__
42 //-----------------------------------------------------------------------------
43 // name: round()
44 // desc: ...
45 //-----------------------------------------------------------------------------
46 double round( double a )
48 if( a >= 0 ) return (double)(long)( a + .5 );
49 else return (double)(long)( a - .5 );
53 //-----------------------------------------------------------------------------
54 // name: trunc()
55 // desc: ...
56 //-----------------------------------------------------------------------------
57 double trunc( double a )
59 return (double)(long)a;
63 //-----------------------------------------------------------------------------
64 // name: remainder()
65 // desc: ...
66 //-----------------------------------------------------------------------------
67 double remainder( long a, long b )
69 long div = a/b;
70 return a - b*div;
74 #endif
78 // the following 6 functions are
79 // lifted from PD source
80 // specifically x_acoustics.c
81 // http://puredata.info/downloads
82 #define LOGTWO 0.69314718055994528623
83 #define LOGTEN 2.302585092994
86 //-----------------------------------------------------------------------------
87 // name: mtof()
88 // desc: midi to freq
89 //-----------------------------------------------------------------------------
90 double mtof( double f )
92 if( f <= -1500 ) return (0);
93 else if( f > 1499 ) return (mtof(1499));
94 // else return (8.17579891564 * exp(.0577622650 * f));
95 // TODO: optimize
96 else return ( pow(2,(f-69)/12.0) * 440.0 );
100 //-----------------------------------------------------------------------------
101 // name: ftom()
102 // desc: freq to midi
103 //-----------------------------------------------------------------------------
104 double ftom( double f )
106 // return (f > 0 ? 17.3123405046 * log(.12231220585 * f) : -1500);
107 // TODO: optimize
108 return (f > 0 ? (log(f/440.0) / LOGTWO) * 12.0 + 69 : -1500);
112 //-----------------------------------------------------------------------------
113 // name: powtodb()
114 // desc: ...
115 //-----------------------------------------------------------------------------
116 double powtodb( double f )
118 if( f <= 0 ) return (0);
119 else
121 double val = 100 + 10./LOGTEN * log(f);
122 return (val < 0 ? 0 : val);
127 //-----------------------------------------------------------------------------
128 // name: rmstodb()
129 // desc: ...
130 //-----------------------------------------------------------------------------
131 double rmstodb( double f )
133 if( f <= 0 ) return (0);
134 else
136 double val = 100 + 20./LOGTEN * log(f);
137 return (val < 0 ? 0 : val);
142 //-----------------------------------------------------------------------------
143 // name: dbtopow()
144 // desc: ...
145 //-----------------------------------------------------------------------------
146 double dbtopow( double f )
148 if( f <= 0 )
149 return (0);
150 else
152 if( f > 870 ) f = 870;
153 return (exp((LOGTEN * 0.1) * (f-100.)));
158 //-----------------------------------------------------------------------------
159 // name: dbtorms()
160 // desc: ...
161 //-----------------------------------------------------------------------------
162 double dbtorms( double f )
164 if( f <= 0 )
165 return (0);
166 else
168 if( f > 485 ) f = 485;
169 return (exp((LOGTEN * 0.05) * (f-100.)));
176 //-----------------------------------------------------------------------------
177 // name: nextpow2()
178 // desc: ...
179 //-----------------------------------------------------------------------------
180 unsigned long nextpow2( unsigned long n )
182 unsigned long nn = n;
183 for( ; n &= n-1; nn = n );
184 return nn * 2;
190 //-----------------------------------------------------------------------------
191 // name: ensurepow2()
192 // desc: ...
193 //-----------------------------------------------------------------------------
194 unsigned long ensurepow2( unsigned long n )
196 return nextpow2( n-1 );