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
23 -----------------------------------------------------------------------------*/
25 //-----------------------------------------------------------------------------
29 // author: Ge Wang (gewang@cs.princeton.edu)
30 // Perry R. Cook (prc@cs.princeton.edu)
31 // Philip Davidson (philipd@alumni.princeton.edu)
33 //-----------------------------------------------------------------------------
34 #include "util_math.h"
38 // windows / visual c++
39 #ifdef __PLATFORM_WIN32__
42 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
46 double round( double a
)
48 if( a
>= 0 ) return (double)(long)( a
+ .5 );
49 else return (double)(long)( a
- .5 );
53 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
57 double trunc( double a
)
59 return (double)(long)a
;
63 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
67 double remainder( long a
, long b
)
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 //-----------------------------------------------------------------------------
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));
96 else return ( pow(2,(f
-69)/12.0) * 440.0 );
100 //-----------------------------------------------------------------------------
102 // desc: freq to midi
103 //-----------------------------------------------------------------------------
104 double ftom( double f
)
106 // return (f > 0 ? 17.3123405046 * log(.12231220585 * f) : -1500);
108 return (f
> 0 ? (log(f
/440.0) / LOGTWO
) * 12.0 + 69 : -1500);
112 //-----------------------------------------------------------------------------
115 //-----------------------------------------------------------------------------
116 double powtodb( double f
)
118 if( f
<= 0 ) return (0);
121 double val
= 100 + 10./LOGTEN
* log(f
);
122 return (val
< 0 ? 0 : val
);
127 //-----------------------------------------------------------------------------
130 //-----------------------------------------------------------------------------
131 double rmstodb( double f
)
133 if( f
<= 0 ) return (0);
136 double val
= 100 + 20./LOGTEN
* log(f
);
137 return (val
< 0 ? 0 : val
);
142 //-----------------------------------------------------------------------------
145 //-----------------------------------------------------------------------------
146 double dbtopow( double f
)
152 if( f
> 870 ) f
= 870;
153 return (exp((LOGTEN
* 0.1) * (f
-100.)));
158 //-----------------------------------------------------------------------------
161 //-----------------------------------------------------------------------------
162 double dbtorms( double f
)
168 if( f
> 485 ) f
= 485;
169 return (exp((LOGTEN
* 0.05) * (f
-100.)));