| 1 | /*---------------------------------------------------------------------------- |
| 2 | ChucK Strongly-timed Audio Programming Language |
| 3 | Compiler and Virtual Machine |
| 4 | |
| 5 | Copyright (c) 2003 Ge Wang and Perry R. Cook. All rights reserved. |
| 6 | http://chuck.cs.princeton.edu/ |
| 7 | http://soundlab.cs.princeton.edu/ |
| 8 | |
| 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. |
| 13 | |
| 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. |
| 18 | |
| 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 | -----------------------------------------------------------------------------*/ |
| 24 | |
| 25 | //----------------------------------------------------------------------------- |
| 26 | // name: util_math.cpp |
| 27 | // desc: a mini-compatibility library for math functions |
| 28 | // |
| 29 | // author: Ge Wang ([email protected]) |
| 30 | // Perry R. Cook ([email protected]) |
| 31 | // Philip Davidson ([email protected]) |
| 32 | // date: Spring 2004 |
| 33 | //----------------------------------------------------------------------------- |
| 34 | #include "util_math.h" |
| 35 | #include <math.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <time.h> |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | //----------------------------------------------------------------------------- |
| 43 | #ifndef __OLDSCHOOL_RANDOM__ // not using old school, pre-c++11 compatibility |
| 44 | //----------------------------------------------------------------------------- |
| 45 | // name: ck_random() and ck_srandom() |
| 46 | // desc: chuck wrappers for random number generators |
| 47 | // 1.5.0.1 (ge) using mt19937 (requires c++11) |
| 48 | //----------------------------------------------------------------------------- |
| 49 | #include <random> |
| 50 | // non-deterministic thing |
| 51 | static std::random_device g_ck_rd; |
| 52 | // mersenne twister RNG, based on the Mersenne prime (2^19937-1) |
| 53 | static std::mt19937 g_ck_global_rng; |
| 54 | // int and real distributions |
| 55 | static std::uniform_int_distribution<t_CKINT> g_ck_int_dist(0, CK_RANDOM_MAX); |
| 56 | static std::uniform_real_distribution<t_CKFLOAT> g_ck_real_dist(0.0, 1.0); |
| 57 | // ck_random() returns a signed int no greater than CK_RANDOM_MAX |
| 58 | // to maintain parity between 64-bit and 32-bit systems, CK_RANDOM_MAX is set |
| 59 | // to 0x7fffffff (or 2,147,483,647--the largest 32-bit signed number) rather |
| 60 | // than mt19937's actual max of 0xffffffff (or 4,294,967,295, 2^32-1) |
| 61 | t_CKINT ck_random() { return g_ck_int_dist(g_ck_global_rng); } |
| 62 | // get t_CKFLOAT in [0,1] |
| 63 | t_CKFLOAT ck_random_f() { return g_ck_real_dist(g_ck_global_rng); } |
| 64 | // seed the random number generator |
| 65 | void ck_srandom( unsigned s ) { g_ck_global_rng.seed(s); } |
| 66 | // randomize using underlying mechanic |
| 67 | void ck_randomize() { ck_srandom( g_ck_rd() ); } |
| 68 | //----------------------------------------------------------------------------- |
| 69 | #else // using old school random (pre-c++11) |
| 70 | //----------------------------------------------------------------------------- |
| 71 | // name: ck_random() and ck_srandom() |
| 72 | // desc: chuck wrappers for random number generators | 1.4.2.0 (ge) |
| 73 | //----------------------------------------------------------------------------- |
| 74 | #ifndef __PLATFORM_WINDOWS__ |
| 75 | t_CKINT ck_random() { return random(); } |
| 76 | t_CKFLOAT ck_random_f() { return random() / (t_CKFLOAT)CK_RANDOM_MAX; } |
| 77 | void ck_srandom( unsigned s ) { srandom( s ); } |
| 78 | #else // __PLATFORM_WINDOWS__ |
| 79 | t_CKINT ck_random() { return rand(); } |
| 80 | t_CKFLOAT ck_random_f() { return rand() / (t_CKFLOAT)CK_RANDOM_MAX; } |
| 81 | void ck_srandom( unsigned s ) { srand( s ); } |
| 82 | #endif |
| 83 | // randomize using underlying mechanic |
| 84 | void ck_randomize() { ck_srandom( (unsigned)time(NULL) ); } |
| 85 | //----------------------------------------------------------------------------- |
| 86 | #endif // __OLDSCHOOL_RANDOM__ |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | // windows / visual c++ |
| 92 | #ifdef __PLATFORM_WINDOWS__ |
| 93 | #ifdef __CK_MATH_DEFINE_ROUND_TRUNC__ |
| 94 | //----------------------------------------------------------------------------- |
| 95 | // name: round() |
| 96 | // desc: round a to the nearest interger number |
| 97 | //----------------------------------------------------------------------------- |
| 98 | double round( double a ) |
| 99 | { |
| 100 | if (a >= 0) return (double)(t_CKINT)(a + .5); |
| 101 | else return (double)(t_CKINT)(a - .5); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | //----------------------------------------------------------------------------- |
| 106 | // name: trunc() |
| 107 | // desc: remove non-integer aspect of a |
| 108 | //----------------------------------------------------------------------------- |
| 109 | double trunc( double a ) |
| 110 | { |
| 111 | return (double)(long long)a; |
| 112 | } |
| 113 | #endif // __CK_MATH_DEFINE_ROUND_TRUNC |
| 114 | #endif // __PLATFORM_WINDOWS__ |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | //----------------------------------------------------------------------------- |
| 120 | // name: ck_remainder() |
| 121 | // desc: the remainder of a / b |
| 122 | //----------------------------------------------------------------------------- |
| 123 | double ck_remainder( double a, double b ) |
| 124 | { |
| 125 | #ifdef __PLATFORM_WINDOWS__ |
| 126 | t_CKINT div = a/b; |
| 127 | return a - b*div; |
| 128 | #else |
| 129 | return remainder( a, b ); |
| 130 | #endif |
| 131 | } |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | // the following 6 functions are lifted from PD source |
| 137 | // specifically x_acoustics.c |
| 138 | // http://puredata.info/downloads |
| 139 | #define LOGTWO 0.69314718055994528623 |
| 140 | #define LOGTEN 2.302585092994 |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | //----------------------------------------------------------------------------- |
| 146 | // name: ck_mtof() |
| 147 | // desc: midi to freq |
| 148 | //----------------------------------------------------------------------------- |
| 149 | double ck_mtof( double f ) |
| 150 | { |
| 151 | if( f <= -1500 ) return (0); |
| 152 | else if( f > 1499 ) return (ck_mtof(1499)); |
| 153 | // else return (8.17579891564 * exp(.0577622650 * f)); |
| 154 | // TODO: optimize |
| 155 | else return ( pow(2,(f-69)/12.0) * 440.0 ); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | //----------------------------------------------------------------------------- |
| 162 | // name: ck_ftom() |
| 163 | // desc: freq to midi |
| 164 | //----------------------------------------------------------------------------- |
| 165 | double ck_ftom( double f ) |
| 166 | { |
| 167 | // return (f > 0 ? 17.3123405046 * log(.12231220585 * f) : -1500); |
| 168 | // TODO: optimize |
| 169 | return (f > 0 ? (log(f/440.0) / LOGTWO) * 12.0 + 69 : -1500); |
| 170 | } |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | //----------------------------------------------------------------------------- |
| 176 | // name: ck_powtodb() |
| 177 | // desc: pow to db |
| 178 | //----------------------------------------------------------------------------- |
| 179 | double ck_powtodb( double f ) |
| 180 | { |
| 181 | if( f <= 0 ) return (0); |
| 182 | else |
| 183 | { |
| 184 | double val = 100 + 10./LOGTEN * log(f); |
| 185 | return (val < 0 ? 0 : val); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | //----------------------------------------------------------------------------- |
| 193 | // name: ck_rmstodb() |
| 194 | // desc: rms to db |
| 195 | //----------------------------------------------------------------------------- |
| 196 | double ck_rmstodb( double f ) |
| 197 | { |
| 198 | if( f <= 0 ) return (0); |
| 199 | else |
| 200 | { |
| 201 | double val = 100 + 20./LOGTEN * log(f); |
| 202 | return (val < 0 ? 0 : val); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | //----------------------------------------------------------------------------- |
| 210 | // name: ck_dbtopow() |
| 211 | // desc: db to pow |
| 212 | //----------------------------------------------------------------------------- |
| 213 | double ck_dbtopow( double f ) |
| 214 | { |
| 215 | if( f <= 0 ) |
| 216 | return (0); |
| 217 | else |
| 218 | { |
| 219 | if( f > 870 ) f = 870; |
| 220 | return (exp((LOGTEN * 0.1) * (f-100.))); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | //----------------------------------------------------------------------------- |
| 228 | // name: ck_dbtorms() |
| 229 | // desc: db to rms |
| 230 | //----------------------------------------------------------------------------- |
| 231 | double ck_dbtorms( double f ) |
| 232 | { |
| 233 | if( f <= 0 ) |
| 234 | return (0); |
| 235 | else |
| 236 | { |
| 237 | if( f > 485 ) f = 485; |
| 238 | return (exp((LOGTEN * 0.05) * (f-100.))); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | //----------------------------------------------------------------------------- |
| 246 | // name: ck_nextpow2() |
| 247 | // desc: compute the next power of two greater than n |
| 248 | //----------------------------------------------------------------------------- |
| 249 | unsigned long ck_nextpow2( unsigned long n ) |
| 250 | { |
| 251 | unsigned long nn = n; |
| 252 | for( ; n &= n-1; nn = n ); |
| 253 | return nn * 2; |
| 254 | } |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | //----------------------------------------------------------------------------- |
| 260 | // name: ck_ensurepow2() |
| 261 | // desc: return the largest power of two no less than n |
| 262 | //----------------------------------------------------------------------------- |
| 263 | unsigned long ck_ensurepow2( unsigned long n ) |
| 264 | { |
| 265 | return ck_nextpow2( n-1 ); |
| 266 | } |
| 267 | |
| 268 | |
| 269 | |
| 270 | //----------------------------------------------------------------------------- |
| 271 | // overloaded; uses default epsilon of 1e-8 |
| 272 | //----------------------------------------------------------------------------- |
| 273 | t_CKBOOL ck_equals( t_CKFLOAT x, t_CKFLOAT y ) |
| 274 | { |
| 275 | // a small number 1e-8 |
| 276 | const t_CKFLOAT epsilon = .00000001; |
| 277 | // call with default epsilon |
| 278 | return ck_equals_ex( x, y, epsilon ); |
| 279 | } |
| 280 | //----------------------------------------------------------------------------- |
| 281 | // equal( x, y ) -- 1.4.1.1 (added ge) |
| 282 | // returns whether x and y (floats) are considered equal |
| 283 | // |
| 284 | // Knuth, Donald E., /The Art of Computer Programming |
| 285 | // Volume II: Seminumerical Algorithms/, Addison-Wesley, 1969. |
| 286 | // based on Knuth section 4.2.2 pages 217-218 |
| 287 | // https://www.cs.technion.ac.il/users/yechiel/c++-faq/floating-point-arith.html |
| 288 | //----------------------------------------------------------------------------- |
| 289 | t_CKBOOL ck_equals_ex( t_CKFLOAT x, t_CKFLOAT y, t_CKFLOAT epsilon ) |
| 290 | { |
| 291 | // absolute values |
| 292 | t_CKFLOAT abs_x = (x >= 0.0 ? x : -x); |
| 293 | t_CKFLOAT abs_y = (y >= 0.0 ? y : -y); |
| 294 | // smaller of the two absolute values (this step added by ge; ensures symmetry) |
| 295 | t_CKFLOAT min = abs_x < abs_y ? abs_x : abs_y; |
| 296 | // absolute value of the difference |
| 297 | t_CKFLOAT v = x-y; t_CKFLOAT abs_v = (v >= 0.0 ? v : -v); |
| 298 | // test whether difference is less/equal to episilon * smaller of two abs values |
| 299 | return abs_v <= (epsilon * min); |
| 300 | } |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | //----------------------------------------------------------------------------- |
| 306 | // name: ck_complex_magnitude() |
| 307 | // desc: magnitude of complex number |
| 308 | //----------------------------------------------------------------------------- |
| 309 | t_CKFLOAT ck_complex_magnitude( const t_CKCOMPLEX & cmp ) |
| 310 | { |
| 311 | return ::sqrt( cmp.re*cmp.re + cmp.im*cmp.im ); |
| 312 | } |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | //----------------------------------------------------------------------------- |
| 318 | // name: ck_complex_phase() |
| 319 | // desc: phase of complex number |
| 320 | //----------------------------------------------------------------------------- |
| 321 | t_CKFLOAT ck_complex_phase( const t_CKCOMPLEX & cmp ) |
| 322 | { |
| 323 | return ::atan2( cmp.im, cmp.re ); |
| 324 | } |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | //----------------------------------------------------------------------------- |
| 330 | // name: ck_vec2_magnitude() |
| 331 | // desc: magnitude of vec2 |
| 332 | //----------------------------------------------------------------------------- |
| 333 | t_CKFLOAT ck_vec2_magnitude( const t_CKVEC2 & v ) |
| 334 | { |
| 335 | return ::sqrt( v.x*v.x + v.y*v.y ); |
| 336 | } |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | //----------------------------------------------------------------------------- |
| 342 | // name: ck_vec2_phase() |
| 343 | // desc: phase of vec2 (treated as a complex value) |
| 344 | //----------------------------------------------------------------------------- |
| 345 | t_CKFLOAT ck_vec2_phase( const t_CKVEC2 & cmp ) |
| 346 | { |
| 347 | return ::atan2( cmp.x, cmp.y ); |
| 348 | } |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | //----------------------------------------------------------------------------- |
| 354 | // name: ck_vec3_magnitude() |
| 355 | // desc: magnitude of vec3 |
| 356 | //----------------------------------------------------------------------------- |
| 357 | t_CKFLOAT ck_vec3_magnitude( const t_CKVEC3 & v ) |
| 358 | { |
| 359 | return ::sqrt( v.x*v.x + v.y*v.y + v.z*v.z ); |
| 360 | } |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | //----------------------------------------------------------------------------- |
| 366 | // name: ck_vec4_magnitude() |
| 367 | // desc: magnitude of vec4 |
| 368 | //----------------------------------------------------------------------------- |
| 369 | t_CKFLOAT ck_vec4_magnitude( const t_CKVEC4 & v ) |
| 370 | { |
| 371 | return ::sqrt( v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w ); |
| 372 | } |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | //----------------------------------------------------------------------------- |
| 378 | // name: ck_factorial32() |
| 379 | // desc: factorial calculator function (for 32 bit) |
| 380 | // author: everett m. carpenter | 1.5.5.8 |
| 381 | // modified for 32-bit (ge, nick) |
| 382 | //----------------------------------------------------------------------------- |
| 383 | t_CKINT ck_factorial32( t_CKINT n ) |
| 384 | { |
| 385 | // 12! is the largest factorial that can fit in a 32-bit integer |
| 386 | if( n < 0 || n > 12 ) return -1; |
| 387 | |
| 388 | t_CKINT result = 1; |
| 389 | for( t_CKINT i = 1; i <= n; i++ ) |
| 390 | { result = i * result; } |
| 391 | |
| 392 | return result; |
| 393 | } |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | //----------------------------------------------------------------------------- |
| 399 | // name: ck_associated_legendre() |
| 400 | // desc: closed form associated legendre polynomials of some order l |
| 401 | // author: everett m. carpenter | 1.5.5.8 |
| 402 | //----------------------------------------------------------------------------- |
| 403 | t_CKFLOAT ck_associated_legendre( const t_CKINT m, const t_CKINT l, const t_CKFLOAT x ) |
| 404 | { |
| 405 | // check bounds |
| 406 | if (l < 0 || l > 5) return 0.0; |
| 407 | if (abs(m) > l) return 0.0; |
| 408 | |
| 409 | float x2 = x * x; // x^2 |
| 410 | float x3 = x2 * x; // x^3 |
| 411 | float x4 = x2 * x2; // x^4 |
| 412 | float cf = 1.0 - x2; // pops up in every associated legendre |
| 413 | float sqrtcf = sqrt(cf);; // also pops up everywhere |
| 414 | t_CKINT absm = abs(m); // assume m is positive, then apply -m case at end |
| 415 | float result = 0.f; |
| 416 | float flip = 1.f; |
| 417 | |
| 418 | if (m < 0) flip = (float)(pow(-1.0, absm) * (ck_factorial32(l - absm) / ck_factorial32(l + absm))); |
| 419 | |
| 420 | switch (l) // order and absm |
| 421 | { |
| 422 | case(0): |
| 423 | { result = 1.0; break; } |
| 424 | case(1): |
| 425 | { |
| 426 | if (absm == 0) { result = x; } |
| 427 | else if (absm == 1) { result = -sqrtcf; } |
| 428 | break; |
| 429 | } |
| 430 | case(2): |
| 431 | { |
| 432 | if (absm == 0) { result = 0.5 * (3.0 * x2 - 1); } |
| 433 | else if (absm == 1) { result = -3.0 * x * sqrtcf; } |
| 434 | else if (absm == 2) { result = 3.0 * cf; } |
| 435 | break; |
| 436 | } |
| 437 | case(3): |
| 438 | { |
| 439 | if (absm == 0) { result = 0.5 * (5.0 * x3 - 3.0 * x); } |
| 440 | else if (absm == 1) { result = 1.5 * (1.0 - 5.0 * x2) * sqrtcf; } |
| 441 | else if (absm == 2) { result = 15.0 * x * cf; } |
| 442 | else if (absm == 3) { result = -15.0 * sqrtcf * cf; } |
| 443 | break; |
| 444 | } |
| 445 | case(4): |
| 446 | { |
| 447 | if (absm == 0) { result = 0.125 * (35.0 * x4 - 30.0 * x2 + 3.0); } |
| 448 | else if (absm == 1) { result = -2.5 * (7.0 * x3 - 3.0 * x) * sqrtcf; } |
| 449 | else if (absm == 2) { result = 7.5 * (7.0 * x2 - 1) * cf; } |
| 450 | else if (absm == 3) { result = -105.0 * x * sqrtcf * cf; } |
| 451 | else if (absm == 4) { result = 105.0 * cf * cf; } |
| 452 | break; |
| 453 | } |
| 454 | case(5): |
| 455 | { |
| 456 | if (absm == 0) { result = 0.125 * (63.0 * x3 * x2 - 70 * x3 + 15 * x); } |
| 457 | else if (absm == 1) { result = -1.875 * sqrtcf * (21.0 * x4 - 14 * x2 + 1); } |
| 458 | else if (absm == 2) { result = 52.5 * x * cf * (3.0 * x2 - 1.0); } |
| 459 | else if (absm == 3) { result = -52.5 * cf * sqrtcf * (9.0 * x2 - 1.0); } |
| 460 | else if (absm == 4) { result = 945.0 * x * cf * cf; } |
| 461 | else if (absm == 5) { result = -945.0 * sqrtcf * cf * cf; } |
| 462 | break; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | return pow(-1.0, m) * result * flip; |
| 467 | } |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | //----------------------------------------------------------------------------- |
| 473 | // name: ck_SN3D |
| 474 | // desc: normalization calculator for spherical harmonics |
| 475 | // author: everett m. carpenter | 1.5.5.8 |
| 476 | //----------------------------------------------------------------------------- |
| 477 | t_CKFLOAT ck_SN3D( const t_CKUINT order, const t_CKINT degree ) // calculate SN3D value |
| 478 | { |
| 479 | int d = (degree == 0) ? 1 : 0; // kronecker delta |
| 480 | float ratio = static_cast<float>(ck_factorial32(order - abs(degree))) / static_cast<float>(ck_factorial32(order + abs(degree))); // ratio of factorials |
| 481 | return sqrtf((2.f - d) * ratio); |
| 482 | } |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | //----------------------------------------------------------------------------- |
| 488 | // name: ck_SH() |
| 489 | // desc: calculation of spherical harmonics according to some order and normalization |
| 490 | // author: everett m. carpenter | 1.5.5.8 |
| 491 | //----------------------------------------------------------------------------- |
| 492 | void ck_SH( t_CKFLOAT * output, const t_CKUINT order_, const t_CKFLOAT azimuth_, const t_CKFLOAT zenith_, const t_CKBOOL n3d ) |
| 493 | { |
| 494 | float azimuth_shift = (azimuth_) * 0.01745329252; // degree 2 rad |
| 495 | float zenith_shift = (90.f - zenith_) * 0.01745329252; |
| 496 | float coszeni = cosf(zenith_shift); |
| 497 | t_CKINT size = (order_ + 1) * (order_ + 1); |
| 498 | for (int order = 0; order <= (int)order_; order++) |
| 499 | { |
| 500 | if (order == 0) |
| 501 | output[0] = ck_SN3D(order, 0); |
| 502 | for (int degree = -order; degree <= order; degree++) |
| 503 | { |
| 504 | float n = n3d ? sqrtf(2 * order + 1) * ck_SN3D(order, degree) : ck_SN3D(order, degree); // normalization term if n3d bool = TRUE, return N3D else SN3D |
| 505 | float p = (ck_associated_legendre((int)abs(degree), (int)order, coszeni)); |
| 506 | float r = (degree < 0) ? sinf(abs(degree) * (azimuth_shift)) : cosf(degree * (azimuth_shift)); // degree positive? Re(exp(i*azimuth*degree)) degree negative? Im(exp(i*azimuth*degree)) |
| 507 | output[(order * order) + order + degree] = n * p * r; |
| 508 | } |
| 509 | } |
| 510 | } |