| 1 | /*---------------------------------------------------------------------------- |
| 2 | ChucK Strongly-timed Audio Programming Language |
| 3 | Compiler, Virtual Machine, and Synthesis Engine |
| 4 | |
| 5 | Copyright (c) 2003 Ge Wang and Perry R. Cook. All rights reserved. |
| 6 | http://chuck.stanford.edu/ |
| 7 | http://chuck.cs.princeton.edu/ |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify |
| 10 | it under the dual-license terms of EITHER the MIT License OR the GNU |
| 11 | General Public License (the latter as published by the Free Software |
| 12 | Foundation; either version 2 of the License or, at your option, any |
| 13 | later version). |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful and/or |
| 16 | interesting, but WITHOUT ANY WARRANTY; without even the implied warranty |
| 17 | of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | MIT Licence and/or the GNU General Public License for details. |
| 19 | |
| 20 | You should have received a copy of the MIT License and the GNU General |
| 21 | Public License (GPL) along with this program; a copy of the GPL can also |
| 22 | be obtained by writing to the Free Software Foundation, Inc., 59 Temple |
| 23 | Place, Suite 330, Boston, MA 02111-1307 U.S.A. |
| 24 | -----------------------------------------------------------------------------*/ |
| 25 | |
| 26 | //----------------------------------------------------------------------------- |
| 27 | // file: ugen_osc.cpp |
| 28 | // desc: oscilliator unit generators |
| 29 | // |
| 30 | // author: Ge Wang ([email protected] | [email protected]) |
| 31 | // Philip L. Davidson ([email protected]) |
| 32 | // Perry R. Cook ([email protected]) |
| 33 | // Dan Trueman ([email protected]) |
| 34 | // date: Summer 2004 |
| 35 | //----------------------------------------------------------------------------- |
| 36 | #include "ugen_osc.h" |
| 37 | #include "chuck_type.h" |
| 38 | #include "chuck_ugen.h" |
| 39 | #include "chuck_compile.h" |
| 40 | #include <math.h> |
| 41 | #include <stdio.h> |
| 42 | |
| 43 | static t_CKUINT g_srateOsc = 0; |
| 44 | // for member data offset |
| 45 | static t_CKUINT osc_offset_data = 0; |
| 46 | |
| 47 | //----------------------------------------------------------------------------- |
| 48 | // this is called for this module to know when sample rate changes | 1.5.4.2 (ge) added |
| 49 | //----------------------------------------------------------------------------- |
| 50 | void osc_srate_update_cb( t_CKUINT srate, void * userdata ) { g_srateOsc = srate; } |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | //----------------------------------------------------------------------------- |
| 56 | // name: osc_query() |
| 57 | // desc: ... |
| 58 | //----------------------------------------------------------------------------- |
| 59 | DLL_QUERY osc_query( Chuck_DL_Query * QUERY ) |
| 60 | { |
| 61 | // srate |
| 62 | g_srateOsc = QUERY->srate(); |
| 63 | // register callback to be notified if/when sample rate changes | 1.5.4.2 (ge) added |
| 64 | QUERY->register_callback_on_srate_update( QUERY, osc_srate_update_cb, NULL ); |
| 65 | // get the env |
| 66 | Chuck_Env * env = QUERY->env(); |
| 67 | |
| 68 | Chuck_DL_Func * func = NULL; |
| 69 | |
| 70 | // register deprecate |
| 71 | type_engine_register_deprecate( env, "osc", "Osc" ); |
| 72 | type_engine_register_deprecate( env, "phasor", "Phasor" ); |
| 73 | type_engine_register_deprecate( env, "sinosc", "SinOsc" ); |
| 74 | type_engine_register_deprecate( env, "triosc", "TriOsc" ); |
| 75 | type_engine_register_deprecate( env, "sawosc", "SawOsc" ); |
| 76 | type_engine_register_deprecate( env, "pulseosc", "PulseOsc" ); |
| 77 | type_engine_register_deprecate( env, "sqrosc", "SqrOsc" ); |
| 78 | |
| 79 | std::string doc; |
| 80 | |
| 81 | //--------------------------------------------------------------------- |
| 82 | // init as base class: osc |
| 83 | //--------------------------------------------------------------------- |
| 84 | doc = "base class for simple oscillator unit generators."; |
| 85 | if( !type_engine_import_ugen_begin( env, "Osc", "UGen", env->global(), |
| 86 | osc_ctor, osc_dtor, osc_tick, osc_pmsg, |
| 87 | doc.c_str() ) ) |
| 88 | return FALSE; |
| 89 | |
| 90 | // add member variable |
| 91 | osc_offset_data = type_engine_import_mvar( env, "int", "@osc_data", FALSE ); |
| 92 | if( osc_offset_data == CK_INVALID_OFFSET ) goto error; |
| 93 | |
| 94 | // overload constructor (float freq) |
| 95 | func = make_new_ctor( oscx_ctor_1 ); |
| 96 | func->add_arg( "float", "freq" ); |
| 97 | func->doc = "construct Osc at specified frequency."; |
| 98 | if( !type_engine_import_ctor( env, func ) ) goto error; |
| 99 | |
| 100 | // overload constructor (float freq, float phase) |
| 101 | func = make_new_ctor( oscx_ctor_2 ); |
| 102 | func->add_arg( "float", "freq" ); |
| 103 | func->add_arg( "float", "phase" ); |
| 104 | func->doc = "construct Osc at specified frequency and phase."; |
| 105 | if( !type_engine_import_ctor( env, func ) ) goto error; |
| 106 | |
| 107 | // add ctrl: freq |
| 108 | func = make_new_mfun( "float", "freq", osc_ctrl_freq ); |
| 109 | func->add_arg( "float", "hz" ); |
| 110 | func->doc = "set frequency of oscillator in Hertz; maintains phase."; |
| 111 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 112 | func = make_new_mfun( "float", "freq", osc_cget_freq ); |
| 113 | func->doc = "get frequency of oscillator in Hertz."; |
| 114 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 115 | |
| 116 | // add ctrl: period |
| 117 | func = make_new_mfun( "dur", "period", osc_ctrl_period ); |
| 118 | func->add_arg( "dur", "value" ); |
| 119 | func->doc = "set period of oscillator (inverse of frequency)."; |
| 120 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 121 | func = make_new_mfun( "dur", "period", osc_cget_period ); |
| 122 | func->doc = "get period of oscillator (inverse of frequency)."; |
| 123 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 124 | |
| 125 | // add ctrl: sfreq ( == freq ) |
| 126 | func = make_new_mfun( "float", "sfreq", osc_ctrl_freq ); |
| 127 | func->add_arg( "float", "hz" ); |
| 128 | func->doc = "set frequency of oscillator in Hertz; resets phase to 0."; |
| 129 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 130 | |
| 131 | // add ctrl: phase |
| 132 | func = make_new_mfun( "float", "phase", osc_ctrl_phase ); |
| 133 | func->add_arg( "float", "phase" ); |
| 134 | func->doc = "set oscillator phase, in range [0,1)."; |
| 135 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 136 | func = make_new_mfun( "float", "phase", osc_cget_phase ); |
| 137 | func->doc = "get oscillator phase, in range [0,1)."; |
| 138 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 139 | |
| 140 | // add ctrl: sync |
| 141 | func = make_new_mfun( "int", "sync", osc_ctrl_sync ); |
| 142 | func->add_arg( "int", "type" ); |
| 143 | func->doc = "choose how to interpret input: (0) sync frequency to input; (1) sync phase to input; (2) frequency modulation (add input to internal frequency)."; |
| 144 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 145 | func = make_new_mfun( "int", "sync", osc_cget_sync ); |
| 146 | func->doc = "get current interpretation of input: (0) sync frequency to input; (1) sync phase to input; (2) frequency modulation (add input to internal frequency)."; |
| 147 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 148 | |
| 149 | // end the class import |
| 150 | type_engine_import_class_end( env ); |
| 151 | |
| 152 | |
| 153 | //--------------------------------------------------------------------- |
| 154 | // phasor |
| 155 | //--------------------------------------------------------------------- |
| 156 | doc = "a phasor oscillator; linearly rises from 0 to 1; can be used as a phase control."; |
| 157 | if( !type_engine_import_ugen_begin( env, "Phasor", "Osc", env->global(), |
| 158 | NULL, NULL, osc_tick, NULL, |
| 159 | doc.c_str() ) ) |
| 160 | return FALSE; |
| 161 | |
| 162 | // overload constructor (float freq) |
| 163 | func = make_new_ctor( oscx_ctor_1 ); |
| 164 | func->add_arg( "float", "freq" ); |
| 165 | func->doc = "construct Phasor at specified frequency."; |
| 166 | if( !type_engine_import_ctor( env, func ) ) goto error; |
| 167 | |
| 168 | // overload constructor (float freq, float phase) |
| 169 | func = make_new_ctor( oscx_ctor_2 ); |
| 170 | func->add_arg( "float", "freq" ); |
| 171 | func->add_arg( "float", "phase" ); |
| 172 | func->doc = "construct Phasor at specified frequency and phase."; |
| 173 | if( !type_engine_import_ctor( env, func ) ) goto error; |
| 174 | |
| 175 | // add examples | 1.5.0.0 added |
| 176 | if( !type_engine_import_add_ex( env, "basic/phasor.ck" ) ) goto error; |
| 177 | |
| 178 | // end the class import |
| 179 | type_engine_import_class_end( env ); |
| 180 | |
| 181 | //--------------------------------------------------------------------- |
| 182 | // sinosc |
| 183 | //--------------------------------------------------------------------- |
| 184 | doc = "a sine wave oscillator."; |
| 185 | if( !type_engine_import_ugen_begin( env, "SinOsc", "Osc", env->global(), |
| 186 | NULL, NULL, sinosc_tick, NULL, |
| 187 | doc.c_str() ) ) |
| 188 | return FALSE; |
| 189 | |
| 190 | // overload constructor (float freq) |
| 191 | func = make_new_ctor( oscx_ctor_1 ); |
| 192 | func->add_arg( "float", "freq" ); |
| 193 | func->doc = "construct SinOsc at specified frequency."; |
| 194 | if( !type_engine_import_ctor( env, func ) ) goto error; |
| 195 | |
| 196 | // overload constructor (float freq, float phase) |
| 197 | func = make_new_ctor( oscx_ctor_2 ); |
| 198 | func->add_arg( "float", "freq" ); |
| 199 | func->add_arg( "float", "phase" ); |
| 200 | func->doc = "construct SinOsc at specified frequency and phase."; |
| 201 | if( !type_engine_import_ctor( env, func ) ) goto error; |
| 202 | |
| 203 | // add examples | 1.5.0.0 (ge) |
| 204 | type_engine_import_add_ex( env, "otf_05.ck" ); |
| 205 | type_engine_import_add_ex( env, "otf_06.ck" ); |
| 206 | type_engine_import_add_ex( env, "basic/foo.ck" ); |
| 207 | type_engine_import_add_ex( env, "basic/bar.ck" ); |
| 208 | type_engine_import_add_ex( env, "basic/chirp.ck" ); |
| 209 | type_engine_import_add_ex( env, "basic/alarm.ck" ); |
| 210 | type_engine_import_add_ex( env, "basic/whirl.ck" ); |
| 211 | type_engine_import_add_ex( env, "analysis/fft.ck" ); |
| 212 | |
| 213 | // end the class import |
| 214 | type_engine_import_class_end( env ); |
| 215 | |
| 216 | |
| 217 | //--------------------------------------------------------------------- |
| 218 | // triosc - triangle oscillator |
| 219 | //--------------------------------------------------------------------- |
| 220 | doc = "a triangle wave oscillator."; |
| 221 | if( !type_engine_import_ugen_begin( env, "TriOsc", "Osc", env->global(), |
| 222 | NULL, NULL, triosc_tick, NULL, |
| 223 | doc.c_str() ) ) |
| 224 | return FALSE; |
| 225 | |
| 226 | // overload constructor (float freq) |
| 227 | func = make_new_ctor( oscx_ctor_1 ); |
| 228 | func->add_arg( "float", "freq" ); |
| 229 | func->doc = "construct TriOsc at specified frequency."; |
| 230 | if( !type_engine_import_ctor( env, func ) ) goto error; |
| 231 | |
| 232 | // overload constructor (float freq, float phase) |
| 233 | func = make_new_ctor( oscx_ctor_2 ); |
| 234 | func->add_arg( "float", "freq" ); |
| 235 | func->add_arg( "float", "phase" ); |
| 236 | func->doc = "construct TriOsc at specified frequency and phase."; |
| 237 | if( !type_engine_import_ctor( env, func ) ) goto error; |
| 238 | |
| 239 | func = make_new_mfun( "float", "width", osc_ctrl_width ); |
| 240 | func->add_arg( "float", "width" ); |
| 241 | func->doc = "set width of triangle wave (ratio of rise time to fall time)."; |
| 242 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 243 | |
| 244 | func = make_new_mfun( "float", "width", osc_cget_width ); |
| 245 | func->doc = "get width of triangle wave (ratio of rise time to fall time)."; |
| 246 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 247 | |
| 248 | // add examples |
| 249 | if( !type_engine_import_add_ex( env, "basic/oscillatronx.ck" ) ) goto error; |
| 250 | if( !type_engine_import_add_ex( env, "deep/shepard.ck" ) ) goto error; |
| 251 | if( !type_engine_import_add_ex( env, "deep/smb.ck" ) ) goto error; |
| 252 | |
| 253 | // end the class import |
| 254 | type_engine_import_class_end( env ); |
| 255 | |
| 256 | |
| 257 | //--------------------------------------------------------------------- |
| 258 | // sawosc - sawtooth oscillator ( 0 | 1 triangle wave ) |
| 259 | //--------------------------------------------------------------------- |
| 260 | doc = "sawtooth wave oscillator."; |
| 261 | if( !type_engine_import_ugen_begin( env, "SawOsc", "TriOsc", env->global(), |
| 262 | sawosc_ctor, NULL, NULL, NULL, |
| 263 | doc.c_str() ) ) |
| 264 | return FALSE; |
| 265 | |
| 266 | // overload constructor (float freq) |
| 267 | func = make_new_ctor( oscx_ctor_1 ); |
| 268 | func->add_arg( "float", "freq" ); |
| 269 | func->doc = "construct SawOsc at specified frequency."; |
| 270 | if( !type_engine_import_ctor( env, func ) ) goto error; |
| 271 | |
| 272 | // overload constructor (float freq, float phase) |
| 273 | func = make_new_ctor( oscx_ctor_2 ); |
| 274 | func->add_arg( "float", "freq" ); |
| 275 | func->add_arg( "float", "phase" ); |
| 276 | func->doc = "construct SawOsc at specified frequency and phase."; |
| 277 | if( !type_engine_import_ctor( env, func ) ) goto error; |
| 278 | |
| 279 | func = make_new_mfun( "float", "width", sawosc_ctrl_width ); |
| 280 | func->add_arg( "float", "width" ); |
| 281 | func->doc = "whether sawtooth wave is to fall (0) or rise (1)."; |
| 282 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 283 | |
| 284 | func = make_new_mfun( "float", "width", osc_cget_width ); |
| 285 | func->doc = "whether sawtooth wave is to fall (0) or rise (1)."; |
| 286 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 287 | |
| 288 | // add examples |
| 289 | if( !type_engine_import_add_ex( env, "deep/thx.ck" ) ) goto error; |
| 290 | if( !type_engine_import_add_ex( env, "basic/oscillatronx.ck" ) ) goto error; |
| 291 | |
| 292 | // end the class import |
| 293 | type_engine_import_class_end( env ); |
| 294 | |
| 295 | |
| 296 | //--------------------------------------------------------------------- |
| 297 | // pulseosc - pulse-width oscillator |
| 298 | //--------------------------------------------------------------------- |
| 299 | doc = "a pulse width oscillator."; |
| 300 | if( !type_engine_import_ugen_begin( env, "PulseOsc", "Osc", env->global(), |
| 301 | NULL, NULL, pulseosc_tick, NULL, |
| 302 | doc.c_str() ) ) |
| 303 | return FALSE; |
| 304 | |
| 305 | // overload constructor (float freq) |
| 306 | func = make_new_ctor( oscx_ctor_1 ); |
| 307 | func->add_arg( "float", "freq" ); |
| 308 | func->doc = "construct PulseOsc at specified frequency."; |
| 309 | if( !type_engine_import_ctor( env, func ) ) goto error; |
| 310 | |
| 311 | // overload constructor (float freq, float phase) |
| 312 | func = make_new_ctor( oscx_ctor_2 ); |
| 313 | func->add_arg( "float", "freq" ); |
| 314 | func->add_arg( "float", "phase" ); |
| 315 | func->doc = "construct PulseOsc at specified frequency and phase."; |
| 316 | if( !type_engine_import_ctor( env, func ) ) goto error; |
| 317 | |
| 318 | func = make_new_mfun( "float", "width", osc_ctrl_width ); |
| 319 | func->add_arg( "float", "width" ); |
| 320 | func->doc = "set width of duty cycle [0,1)."; |
| 321 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 322 | |
| 323 | func = make_new_mfun( "float", "width", osc_cget_width ); |
| 324 | func->doc = "get width of duty cycle [0,1)/"; |
| 325 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 326 | |
| 327 | // add examples |
| 328 | if( !type_engine_import_add_ex( env, "basic/pulse.ck" ) ) goto error; |
| 329 | |
| 330 | // end the class import |
| 331 | type_engine_import_class_end( env ); |
| 332 | |
| 333 | |
| 334 | //--------------------------------------------------------------------- |
| 335 | // sqrosc - square_wave oscillator ( 0.5 pulse ) |
| 336 | //--------------------------------------------------------------------- |
| 337 | doc = "a square wave oscillator (same as PulseOsc with 0.5 duty cycle)."; |
| 338 | if( !type_engine_import_ugen_begin( env, "SqrOsc", "PulseOsc", env->global(), |
| 339 | sqrosc_ctor, NULL, NULL, NULL, |
| 340 | doc.c_str() ) ) |
| 341 | return FALSE; |
| 342 | |
| 343 | // overload constructor (float freq) |
| 344 | func = make_new_ctor( oscx_ctor_1 ); |
| 345 | func->add_arg( "float", "freq" ); |
| 346 | func->doc = "construct SqrOsc at specified frequency."; |
| 347 | if( !type_engine_import_ctor( env, func ) ) goto error; |
| 348 | |
| 349 | // overload constructor (float freq, float phase) |
| 350 | func = make_new_ctor( oscx_ctor_2 ); |
| 351 | func->add_arg( "float", "freq" ); |
| 352 | func->add_arg( "float", "phase" ); |
| 353 | func->doc = "construct SqrOsc at specified frequency and phase."; |
| 354 | if( !type_engine_import_ctor( env, func ) ) goto error; |
| 355 | |
| 356 | func = make_new_mfun( "float", "width", sqrosc_ctrl_width ); |
| 357 | func->doc = "set width of duty cycle (always 0.5)."; |
| 358 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 359 | |
| 360 | // add examples |
| 361 | if( !type_engine_import_add_ex( env, "basic/oscillatronx.ck" ) ) goto error; |
| 362 | if( !type_engine_import_add_ex( env, "deep/smb.ck" ) ) goto error; |
| 363 | if( !type_engine_import_add_ex( env, "filter/brf.ck" ) ) goto error; |
| 364 | if( !type_engine_import_add_ex( env, "filter/rlpf.ck" ) ) goto error; |
| 365 | if( !type_engine_import_add_ex( env, "filter/rhpf.ck" ) ) goto error; |
| 366 | |
| 367 | // end the class import |
| 368 | type_engine_import_class_end( env ); |
| 369 | |
| 370 | // include GenX!!! |
| 371 | if( !genX_query( QUERY ) ) |
| 372 | return FALSE; |
| 373 | |
| 374 | return TRUE; |
| 375 | |
| 376 | error: |
| 377 | |
| 378 | // end the class import |
| 379 | type_engine_import_class_end( env ); |
| 380 | |
| 381 | return FALSE; |
| 382 | } |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | //----------------------------------------------------------------------------- |
| 388 | // name: struct Osc_Data |
| 389 | // desc: ... |
| 390 | //----------------------------------------------------------------------------- |
| 391 | struct Osc_Data |
| 392 | { |
| 393 | t_CKFLOAT num; |
| 394 | t_CKFLOAT freq; |
| 395 | t_CKINT sync; |
| 396 | t_CKINT srate; |
| 397 | t_CKFLOAT width; |
| 398 | |
| 399 | t_CKFLOAT phase; |
| 400 | |
| 401 | Osc_Data() |
| 402 | { |
| 403 | num = 0.0; |
| 404 | freq = 220.0; |
| 405 | sync = 0; // internal |
| 406 | width = 0.5; |
| 407 | srate = g_srateOsc; |
| 408 | phase = 0.0; |
| 409 | } |
| 410 | }; |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | //----------------------------------------------------------------------------- |
| 416 | // name: osc_ctor() |
| 417 | // desc: default cosntructor |
| 418 | //----------------------------------------------------------------------------- |
| 419 | CK_DLL_CTOR( osc_ctor ) |
| 420 | { |
| 421 | Osc_Data * d = new Osc_Data; |
| 422 | Chuck_DL_Return r; |
| 423 | // return data to be used later |
| 424 | OBJ_MEMBER_UINT(SELF, osc_offset_data) = (t_CKUINT)d; |
| 425 | osc_ctrl_freq( SELF, &(d->freq), &r, VM, SHRED, API ); |
| 426 | } |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | //----------------------------------------------------------------------------- |
| 432 | // generic overloaded ctor( float freq ) |
| 433 | // to be used by subclasses of Osc |
| 434 | //----------------------------------------------------------------------------- |
| 435 | CK_DLL_CTOR( oscx_ctor_1 ) |
| 436 | { |
| 437 | // return (dummy struct since ctors don't return stuff) |
| 438 | Chuck_DL_Return r; |
| 439 | // set freq |
| 440 | osc_ctrl_freq( SELF, ARGS, &r, VM, SHRED, API ); |
| 441 | } |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | //----------------------------------------------------------------------------- |
| 447 | // generic overloaded ctor( float freq, float phase ) |
| 448 | // to be used by subclasses of Osc |
| 449 | //----------------------------------------------------------------------------- |
| 450 | CK_DLL_CTOR( oscx_ctor_2 ) |
| 451 | { |
| 452 | // return (dummy struct since ctors don't return stuff) |
| 453 | Chuck_DL_Return r; |
| 454 | // set freq |
| 455 | osc_ctrl_freq( SELF, ARGS, &r, VM, SHRED, API ); |
| 456 | // advance |
| 457 | GET_NEXT_FLOAT(ARGS); |
| 458 | // set phase |
| 459 | osc_ctrl_phase( SELF, ARGS, &r, VM, SHRED, API ); |
| 460 | } |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | //----------------------------------------------------------------------------- |
| 466 | // name: osc_dtor() |
| 467 | // desc: destructor |
| 468 | //----------------------------------------------------------------------------- |
| 469 | CK_DLL_DTOR( osc_dtor ) |
| 470 | { |
| 471 | // get the data |
| 472 | Osc_Data * data = (Osc_Data *)OBJ_MEMBER_UINT(SELF, osc_offset_data ); |
| 473 | // delete |
| 474 | delete data; |
| 475 | // set to NULL |
| 476 | OBJ_MEMBER_UINT(SELF, osc_offset_data) = 0; |
| 477 | } |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | //----------------------------------------------------------------------------- |
| 483 | // name: osc_tick() |
| 484 | // desc: ... |
| 485 | // |
| 486 | // basic osx is a phasor... |
| 487 | // we use a duty-cycle rep ( 0 - 1 ) rather than angular ( 0 - TWOPI ) |
| 488 | // sinusoidal oscillators are special |
| 489 | // |
| 490 | // (maybe) as a rule, we store external phase control values |
| 491 | // so that we can have a smooth change back to internal control -pld |
| 492 | // |
| 493 | // technically this should happen even with external phase control |
| 494 | // that we'd be in the right place when translating back to internal... |
| 495 | // this was decidely inefficient and nit-picky. -pld |
| 496 | // |
| 497 | //----------------------------------------------------------------------------- |
| 498 | CK_DLL_TICK( osc_tick ) |
| 499 | { |
| 500 | // get the data |
| 501 | Osc_Data * d = (Osc_Data *)OBJ_MEMBER_UINT(SELF, osc_offset_data ); |
| 502 | Chuck_UGen * ugen = (Chuck_UGen *)SELF; |
| 503 | t_CKBOOL inc_phase = TRUE; |
| 504 | |
| 505 | // if input |
| 506 | if( ugen->m_num_src ) |
| 507 | { |
| 508 | // sync frequency to input |
| 509 | if( d->sync == 0 ) |
| 510 | { |
| 511 | // set freq |
| 512 | d->freq = in; |
| 513 | // phase increment |
| 514 | d->num = d->freq / d->srate; |
| 515 | // bound it |
| 516 | if( d->num >= 1.0 || d->num < 0.0 ) d->num -= floor( d->num ); |
| 517 | } |
| 518 | // synch phase to input |
| 519 | else if( d->sync == 1 ) |
| 520 | { |
| 521 | // set phase |
| 522 | d->phase = in; |
| 523 | // no update |
| 524 | inc_phase = FALSE; |
| 525 | // bound it (thanks Pyry) |
| 526 | if( d->phase > 1.0 || d->phase < 0.0 ) d->phase -= floor( d->phase ); |
| 527 | } |
| 528 | // fm synthesis |
| 529 | else if( d->sync == 2 ) |
| 530 | { |
| 531 | // set freq |
| 532 | t_CKFLOAT freq = d->freq + in; |
| 533 | // phase increment |
| 534 | d->num = freq / d->srate; |
| 535 | // bound it |
| 536 | if( d->num >= 1.0 || d->num < 0.0 ) d->num -= floor( d->num ); |
| 537 | } |
| 538 | // sync to now |
| 539 | // else if( d->sync == 3 ) |
| 540 | // { |
| 541 | // d->phase = now * d->num; |
| 542 | // inc_phase = FALSE; |
| 543 | // } |
| 544 | } |
| 545 | |
| 546 | // set output to current phase |
| 547 | *out = (SAMPLE)d->phase; |
| 548 | |
| 549 | // check |
| 550 | if( inc_phase ) |
| 551 | { |
| 552 | // step the phase. |
| 553 | d->phase += d->num; |
| 554 | // keep the phase between 0 and 1 |
| 555 | if( d->phase > 1.0 ) d->phase -= 1.0; |
| 556 | else if( d->phase < 0.0 ) d->phase += 1.0; |
| 557 | } |
| 558 | |
| 559 | return TRUE; |
| 560 | } |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | //----------------------------------------------------------------------------- |
| 566 | // name: sinosc_tick() |
| 567 | // desc: ... |
| 568 | //----------------------------------------------------------------------------- |
| 569 | CK_DLL_TICK( sinosc_tick ) |
| 570 | { |
| 571 | // get the data |
| 572 | Osc_Data * d = (Osc_Data *)OBJ_MEMBER_UINT(SELF, osc_offset_data ); |
| 573 | Chuck_UGen * ugen = (Chuck_UGen *)SELF; |
| 574 | t_CKBOOL inc_phase = TRUE; |
| 575 | |
| 576 | // if input |
| 577 | if( ugen->m_num_src ) |
| 578 | { |
| 579 | // sync frequency to input |
| 580 | if( d->sync == 0 ) |
| 581 | { |
| 582 | // set freq |
| 583 | d->freq = in; |
| 584 | // phase increment |
| 585 | d->num = d->freq / d->srate; |
| 586 | // bound it |
| 587 | if( d->num >= 1.0 ) d->num -= floor( d->num ); |
| 588 | else if( d->num <= -1.0 ) d->num += floor( d->num ); |
| 589 | } |
| 590 | // sync phase to input |
| 591 | else if( d->sync == 1 ) |
| 592 | { |
| 593 | // set freq |
| 594 | d->phase = in; |
| 595 | inc_phase = FALSE; |
| 596 | } |
| 597 | // FM synthesis |
| 598 | else if( d->sync == 2 ) |
| 599 | { |
| 600 | // set freq |
| 601 | t_CKFLOAT freq = d->freq + in; |
| 602 | // phase increment |
| 603 | d->num = freq / d->srate; |
| 604 | // bound it |
| 605 | if( d->num >= 1.0 ) d->num -= floor( d->num ); |
| 606 | else if( d->num <= -1.0 ) d->num += floor( d->num ); |
| 607 | } |
| 608 | // sync phase to now |
| 609 | // else if( d->sync == 3 ) |
| 610 | // { |
| 611 | // d->phase = now * d->num; |
| 612 | // inc_phase = FALSE; |
| 613 | // } |
| 614 | } |
| 615 | |
| 616 | // set output |
| 617 | *out = (SAMPLE) ::sin( d->phase * CK_TWO_PI ); |
| 618 | |
| 619 | if( inc_phase ) |
| 620 | { |
| 621 | // next phase |
| 622 | d->phase += d->num; |
| 623 | // keep the phase between 0 and 1 |
| 624 | if( d->phase > 1.0 ) d->phase -= 1.0; |
| 625 | else if( d->phase < 0.0 ) d->phase += 1.0; |
| 626 | } |
| 627 | |
| 628 | return TRUE; |
| 629 | } |
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | //----------------------------------------------------------------------------- |
| 635 | // name: triosc_tick() |
| 636 | // desc: ... |
| 637 | //----------------------------------------------------------------------------- |
| 638 | CK_DLL_TICK( triosc_tick ) |
| 639 | { |
| 640 | // get the data |
| 641 | Osc_Data * d = (Osc_Data *)OBJ_MEMBER_UINT(SELF, osc_offset_data ); |
| 642 | Chuck_UGen * ugen = (Chuck_UGen *)SELF; |
| 643 | t_CKBOOL inc_phase = TRUE; |
| 644 | |
| 645 | // if input |
| 646 | if( ugen->m_num_src ) |
| 647 | { |
| 648 | // sync frequency to input |
| 649 | if( d->sync == 0 ) |
| 650 | { |
| 651 | // set freq |
| 652 | d->freq = in; |
| 653 | // phase increment |
| 654 | d->num = d->freq / d->srate; |
| 655 | // bound it |
| 656 | if( d->num >= 1.0 ) d->num -= floor( d->num ); |
| 657 | else if( d->num <= -1.0 ) d->num += floor( d->num ); |
| 658 | } |
| 659 | // sync phase to input |
| 660 | else if( d->sync == 1 ) |
| 661 | { |
| 662 | // set phase |
| 663 | d->phase = in; |
| 664 | inc_phase = FALSE; |
| 665 | } |
| 666 | // FM synthesis |
| 667 | else if( d->sync == 2 ) |
| 668 | { |
| 669 | // set freq |
| 670 | t_CKFLOAT freq = d->freq + in; |
| 671 | // phase increment |
| 672 | d->num = freq / d->srate; |
| 673 | // bound it |
| 674 | if( d->num >= 1.0 ) d->num -= floor( d->num ); |
| 675 | else if( d->num <= -1.0 ) d->num += floor( d->num ); |
| 676 | } |
| 677 | // sync to now |
| 678 | // if( d->sync == 3 ) |
| 679 | // { |
| 680 | // d->phase = now * d->num; |
| 681 | // inc_phase = FALSE; |
| 682 | // } |
| 683 | } |
| 684 | |
| 685 | // compute |
| 686 | t_CKFLOAT phase = d->phase + .25; if( phase > 1.0 ) phase -= 1.0; |
| 687 | if( phase < d->width ) *out = (SAMPLE) (d->width == 0.0) ? 1.0 : -1.0 + 2.0 * phase / d->width; |
| 688 | else *out = (SAMPLE) (d->width == 1.0) ? 0 : 1.0 - 2.0 * (phase - d->width) / (1.0 - d->width); |
| 689 | |
| 690 | // advance internal phase |
| 691 | if( inc_phase ) |
| 692 | { |
| 693 | d->phase += d->num; |
| 694 | // keep the phase between 0 and 1 |
| 695 | if( d->phase > 1.0 ) d->phase -= 1.0; |
| 696 | else if( d->phase < 0.0 ) d->phase += 1.0; |
| 697 | } |
| 698 | |
| 699 | return TRUE; |
| 700 | } |
| 701 | |
| 702 | |
| 703 | // sawosc_tick is tri_osc tick with width=0.0 or width=1.0 -pld |
| 704 | |
| 705 | |
| 706 | //----------------------------------------------------------------------------- |
| 707 | // name: pulseosc_tick() |
| 708 | // desc: ... |
| 709 | //----------------------------------------------------------------------------- |
| 710 | CK_DLL_TICK( pulseosc_tick ) |
| 711 | { |
| 712 | // get the data |
| 713 | Osc_Data * d = (Osc_Data *)OBJ_MEMBER_UINT(SELF, osc_offset_data ); |
| 714 | Chuck_UGen * ugen = (Chuck_UGen *)SELF; |
| 715 | t_CKBOOL inc_phase = TRUE; |
| 716 | |
| 717 | // if input |
| 718 | if( ugen->m_num_src ) |
| 719 | { |
| 720 | // sync frequency to input |
| 721 | if( d->sync == 0 ) |
| 722 | { |
| 723 | // set freq |
| 724 | d->freq = in; |
| 725 | // phase increment |
| 726 | d->num = d->freq / d->srate; |
| 727 | // bound it |
| 728 | if( d->num >= 1.0 ) d->num -= floor( d->num ); |
| 729 | else if( d->num <= -1.0 ) d->num += floor( d->num ); |
| 730 | } |
| 731 | // sync phase to input |
| 732 | else if( d->sync == 1 ) |
| 733 | { |
| 734 | // set phase |
| 735 | d->phase = in; |
| 736 | inc_phase = FALSE; |
| 737 | } |
| 738 | // FM synthesis |
| 739 | else if( d->sync == 2 ) |
| 740 | { |
| 741 | // set freq |
| 742 | t_CKFLOAT freq = d->freq + in; |
| 743 | // phase increment |
| 744 | d->num = freq / d->srate; |
| 745 | // bound it |
| 746 | if( d->num >= 1.0 ) d->num -= floor( d->num ); |
| 747 | else if( d->num <= -1.0 ) d->num += floor( d->num ); |
| 748 | } |
| 749 | // sync to now |
| 750 | // if( d->sync == 3 ) |
| 751 | // { |
| 752 | // d->phase = now * d->num; |
| 753 | // inc_phase = FALSE; |
| 754 | // } |
| 755 | } |
| 756 | |
| 757 | // compute |
| 758 | *out = (SAMPLE) (d->phase < d->width) ? 1.0 : -1.0; |
| 759 | |
| 760 | // move phase |
| 761 | if( inc_phase ) |
| 762 | { |
| 763 | d->phase += d->num; |
| 764 | // keep the phase between 0 and 1 |
| 765 | if( d->phase > 1.0 ) d->phase -= 1.0; |
| 766 | else if( d->phase < 0.0 ) d->phase += 1.0; |
| 767 | } |
| 768 | |
| 769 | return TRUE; |
| 770 | } |
| 771 | |
| 772 | |
| 773 | // sqrosc_tick is pulseosc_tick at width=0.5 -pld; |
| 774 | |
| 775 | |
| 776 | //----------------------------------------------------------------------------- |
| 777 | // name: osc_ctrl_freq() |
| 778 | // desc: set oscillator frequency |
| 779 | //----------------------------------------------------------------------------- |
| 780 | CK_DLL_CTRL( osc_ctrl_freq ) |
| 781 | { |
| 782 | // get data |
| 783 | Osc_Data * d = (Osc_Data *)OBJ_MEMBER_UINT(SELF, osc_offset_data); |
| 784 | // set freq |
| 785 | d->freq = GET_CK_FLOAT(ARGS); |
| 786 | // phase increment |
| 787 | d->num = d->freq / d->srate; |
| 788 | // bound it |
| 789 | if( d->num >= 1.0 ) d->num -= ::floor( d->num ); |
| 790 | // return |
| 791 | RETURN->v_float = (t_CKFLOAT)d->freq; |
| 792 | } |
| 793 | |
| 794 | |
| 795 | |
| 796 | |
| 797 | //----------------------------------------------------------------------------- |
| 798 | // name: osc_cget_freq() |
| 799 | // desc: get oscillator frequency |
| 800 | //----------------------------------------------------------------------------- |
| 801 | CK_DLL_CGET( osc_cget_freq ) |
| 802 | { |
| 803 | // get data |
| 804 | Osc_Data * d = (Osc_Data *)OBJ_MEMBER_UINT(SELF, osc_offset_data); |
| 805 | // return |
| 806 | RETURN->v_float = (t_CKFLOAT)d->freq; |
| 807 | } |
| 808 | |
| 809 | |
| 810 | |
| 811 | |
| 812 | //----------------------------------------------------------------------------- |
| 813 | // name: osc_ctrl_period() |
| 814 | // desc: set oscillator period |
| 815 | //----------------------------------------------------------------------------- |
| 816 | CK_DLL_CTRL( osc_ctrl_period ) |
| 817 | { |
| 818 | // get data |
| 819 | Osc_Data * d = (Osc_Data *)OBJ_MEMBER_UINT(SELF, osc_offset_data); |
| 820 | t_CKDUR period = GET_CK_DUR(ARGS); |
| 821 | // test |
| 822 | if( period == 0.0 ) d->freq = 0.0; |
| 823 | // set freq |
| 824 | else d->freq = 1 / (period / d->srate); |
| 825 | d->num = d->freq / d->srate; |
| 826 | // bound it |
| 827 | if( d->num >= 1.0 ) d->num -= ::floor( d->num ); |
| 828 | // return |
| 829 | RETURN->v_dur = period; |
| 830 | } |
| 831 | |
| 832 | |
| 833 | |
| 834 | |
| 835 | //----------------------------------------------------------------------------- |
| 836 | // name: osc_cget_period() |
| 837 | // desc: get oscillator period |
| 838 | //----------------------------------------------------------------------------- |
| 839 | CK_DLL_CGET( osc_cget_period ) |
| 840 | { |
| 841 | // get data |
| 842 | Osc_Data * d = (Osc_Data *)OBJ_MEMBER_UINT(SELF, osc_offset_data); |
| 843 | t_CKDUR period = 0; |
| 844 | |
| 845 | // get period |
| 846 | if( d->freq != 0.0 ) period = 1 / d->freq * d->srate; |
| 847 | |
| 848 | // return |
| 849 | RETURN->v_dur = period; |
| 850 | } |
| 851 | |
| 852 | |
| 853 | |
| 854 | |
| 855 | //----------------------------------------------------------------------------- |
| 856 | // name: osc_ctrl_phase() |
| 857 | // desc: set oscillator phase wrapped to ( 0 - 1 ) |
| 858 | //----------------------------------------------------------------------------- |
| 859 | CK_DLL_CTRL( osc_ctrl_phase ) |
| 860 | { |
| 861 | // get data |
| 862 | Osc_Data * d = (Osc_Data *)OBJ_MEMBER_UINT(SELF, osc_offset_data ); |
| 863 | // set phase |
| 864 | d->phase = GET_CK_FLOAT(ARGS); |
| 865 | // bound ( this could be set arbitrarily high or low ) |
| 866 | if( d->phase >= 1.0 || d->phase < 0.0 ) d->phase -= floor( d->num ); |
| 867 | // return |
| 868 | RETURN->v_float = (t_CKFLOAT)d->phase; |
| 869 | } |
| 870 | |
| 871 | |
| 872 | |
| 873 | |
| 874 | //----------------------------------------------------------------------------- |
| 875 | // name: osc_cget_phase() |
| 876 | // desc: get oscillator phase wrapped to ( 0 - 1 ) |
| 877 | //----------------------------------------------------------------------------- |
| 878 | CK_DLL_CGET( osc_cget_phase ) |
| 879 | { |
| 880 | // get data |
| 881 | Osc_Data * d = (Osc_Data *)OBJ_MEMBER_UINT(SELF, osc_offset_data ); |
| 882 | // return |
| 883 | RETURN->v_float = (t_CKFLOAT)d->phase; |
| 884 | } |
| 885 | |
| 886 | |
| 887 | |
| 888 | |
| 889 | //----------------------------------------------------------------------------- |
| 890 | // name: osc_ctrl_width() |
| 891 | // desc: set width of active phase ( bound 0.0 - 1.0 ); |
| 892 | //----------------------------------------------------------------------------- |
| 893 | CK_DLL_CTRL( osc_ctrl_width ) |
| 894 | { |
| 895 | // get data |
| 896 | Osc_Data * d = (Osc_Data *)OBJ_MEMBER_UINT(SELF, osc_offset_data ); |
| 897 | // set width |
| 898 | d->width = GET_CK_FLOAT(ARGS); |
| 899 | //bound ( this could be set arbitrarily high or low ) |
| 900 | d->width = ck_max( 0.0, ck_min( 1.0, d->width ) ); |
| 901 | // return |
| 902 | RETURN->v_float = (t_CKFLOAT)d->width; |
| 903 | } |
| 904 | |
| 905 | |
| 906 | |
| 907 | |
| 908 | //----------------------------------------------------------------------------- |
| 909 | // name: osc_cget_width() |
| 910 | // desc: get width of active phase ( bound 0.0 - 1.0 ); |
| 911 | //----------------------------------------------------------------------------- |
| 912 | CK_DLL_CGET( osc_cget_width ) |
| 913 | { |
| 914 | // get data |
| 915 | Osc_Data * d = (Osc_Data *)OBJ_MEMBER_UINT(SELF, osc_offset_data ); |
| 916 | // return |
| 917 | RETURN->v_float = (t_CKFLOAT)d->width; |
| 918 | } |
| 919 | |
| 920 | |
| 921 | |
| 922 | |
| 923 | //----------------------------------------------------------------------------- |
| 924 | // name: sqrosc_ctor() |
| 925 | // desc: ... |
| 926 | //----------------------------------------------------------------------------- |
| 927 | CK_DLL_CTOR( sqrosc_ctor ) |
| 928 | { |
| 929 | Osc_Data * d = new Osc_Data; |
| 930 | Chuck_DL_Return r; |
| 931 | sqrosc_ctrl_width( SELF, &(d->width), &r, VM, SHRED, API ); |
| 932 | } |
| 933 | |
| 934 | |
| 935 | |
| 936 | |
| 937 | //----------------------------------------------------------------------------- |
| 938 | // name: sqrosc_ctrl_width() |
| 939 | // desc: force width to 0.5; |
| 940 | //----------------------------------------------------------------------------- |
| 941 | CK_DLL_CTRL( sqrosc_ctrl_width ) |
| 942 | { |
| 943 | // get data |
| 944 | Osc_Data * d = (Osc_Data *)OBJ_MEMBER_UINT(SELF, osc_offset_data ); |
| 945 | // force value |
| 946 | d->width = 0.5; |
| 947 | // return |
| 948 | RETURN->v_float = (t_CKFLOAT)d->width; |
| 949 | } |
| 950 | |
| 951 | |
| 952 | |
| 953 | |
| 954 | //----------------------------------------------------------------------------- |
| 955 | // name: sawosc_ctor() |
| 956 | // desc: ... |
| 957 | //----------------------------------------------------------------------------- |
| 958 | CK_DLL_CTOR( sawosc_ctor ) |
| 959 | { |
| 960 | Osc_Data * d = new Osc_Data; |
| 961 | Chuck_DL_Return r; |
| 962 | sawosc_ctrl_width( SELF, &(d->width), &r, VM, SHRED, API ); |
| 963 | } |
| 964 | |
| 965 | |
| 966 | |
| 967 | |
| 968 | //----------------------------------------------------------------------------- |
| 969 | // name: sawosc_ctrl_width() |
| 970 | // force width to 0.0 ( falling ) or 1.0 ( rising ) |
| 971 | //----------------------------------------------------------------------------- |
| 972 | CK_DLL_CTRL( sawosc_ctrl_width ) |
| 973 | { |
| 974 | // get data |
| 975 | Osc_Data * d = (Osc_Data *)OBJ_MEMBER_UINT(SELF, osc_offset_data ); |
| 976 | // set width |
| 977 | d->width = GET_CK_FLOAT(ARGS); |
| 978 | // bound ( this could be set arbitrarily high or low ) |
| 979 | d->width = ( d->width < 0.5 ) ? 0.0 : 1.0; //rising or falling |
| 980 | // return |
| 981 | RETURN->v_float = (t_CKFLOAT)d->width; |
| 982 | } |
| 983 | |
| 984 | |
| 985 | |
| 986 | |
| 987 | //----------------------------------------------------------------------------- |
| 988 | // name: osc_ctrl_sync() |
| 989 | // desc: select sync mode for oscillator |
| 990 | //----------------------------------------------------------------------------- |
| 991 | CK_DLL_CTRL( osc_ctrl_sync ) |
| 992 | { |
| 993 | // get data |
| 994 | Osc_Data * d = (Osc_Data *)OBJ_MEMBER_UINT(SELF, osc_offset_data ); |
| 995 | // set sync |
| 996 | t_CKINT psync = d->sync; |
| 997 | d->sync = GET_CK_INT(ARGS); |
| 998 | |
| 999 | // bound ( this could be set arbitrarily high or low ) |
| 1000 | if( d->sync < 0 || d->sync > 2 ) |
| 1001 | d->sync = 0; |
| 1002 | |
| 1003 | if( d->sync == 0 && psync != d->sync ) |
| 1004 | { |
| 1005 | // if we are switching to internal tick |
| 1006 | // we need to pre-advance the phase... |
| 1007 | // this is probably stupid. -pld |
| 1008 | d->phase += d->num; |
| 1009 | // keep the phase between 0 and 1 |
| 1010 | if( d->phase > 1.0 ) d->phase -= 1.0; |
| 1011 | } |
| 1012 | // return |
| 1013 | RETURN->v_int = (t_CKINT)d->sync; |
| 1014 | } |
| 1015 | |
| 1016 | |
| 1017 | |
| 1018 | |
| 1019 | //----------------------------------------------------------------------------- |
| 1020 | // name: osc_cget_sync() |
| 1021 | // desc: get sync mode for oscillator |
| 1022 | //----------------------------------------------------------------------------- |
| 1023 | CK_DLL_CGET( osc_cget_sync ) |
| 1024 | { |
| 1025 | // get data |
| 1026 | Osc_Data * d = (Osc_Data *)OBJ_MEMBER_UINT(SELF, osc_offset_data ); |
| 1027 | // return |
| 1028 | RETURN->v_int = (t_CKINT)d->sync; |
| 1029 | } |
| 1030 | |
| 1031 | |
| 1032 | |
| 1033 | |
| 1034 | //----------------------------------------------------------------------------- |
| 1035 | // name: osc_pmsg() |
| 1036 | // desc: ... |
| 1037 | //----------------------------------------------------------------------------- |
| 1038 | CK_DLL_PMSG( osc_pmsg ) |
| 1039 | { |
| 1040 | Osc_Data * d = (Osc_Data *)OBJ_MEMBER_UINT(SELF, osc_offset_data ); |
| 1041 | if( !strcmp( MSG, "print" ) ) |
| 1042 | { |
| 1043 | CK_FPRINTF_STDOUT( "SinOsc: (freq=%f)", d->freq ); |
| 1044 | return TRUE; |
| 1045 | } |
| 1046 | |
| 1047 | // didn't handle |
| 1048 | return FALSE; |
| 1049 | } |
| 1050 | |
| 1051 | |
| 1052 | |
| 1053 | |
| 1054 | //----------------------------------------------------------------------------- |
| 1055 | // file: ugen_genX.cpp |
| 1056 | // desc: thought it would be a good way to learn the fascinating innards of |
| 1057 | // ChucK by porting some of the classic lookup table functions and adding |
| 1058 | // a few new ones that might be of use. |
| 1059 | // mostly ported from RTcmix (all by WarpTable) |
| 1060 | // |
| 1061 | // author: Dan Trueman (dtrueman.princeton.edu) |
| 1062 | // date: Winter 2007 |
| 1063 | //----------------------------------------------------------------------------- |
| 1064 | // for member data offset |
| 1065 | static t_CKUINT genX_offset_data = 0; |
| 1066 | // for internal usage |
| 1067 | static void _transition( t_CKDOUBLE a, t_CKDOUBLE alpha, t_CKDOUBLE b, |
| 1068 | t_CKINT n, t_CKDOUBLE * output ); |
| 1069 | |
| 1070 | |
| 1071 | |
| 1072 | //----------------------------------------------------------------------------- |
| 1073 | // name: genX_query() |
| 1074 | // desc: ... |
| 1075 | //----------------------------------------------------------------------------- |
| 1076 | DLL_QUERY genX_query( Chuck_DL_Query * QUERY ) |
| 1077 | { |
| 1078 | // srate |
| 1079 | g_srateOsc = QUERY->srate(); |
| 1080 | // register callback to be notified if/when sample rate changes | 1.5.4.2 (ge) added |
| 1081 | QUERY->register_callback_on_srate_update( QUERY, osc_srate_update_cb, NULL ); |
| 1082 | |
| 1083 | // get the env |
| 1084 | Chuck_Env * env = QUERY->env(); |
| 1085 | std::string doc; |
| 1086 | Chuck_DL_Func * func = NULL; |
| 1087 | |
| 1088 | doc = "ported from rtcmix. See <a href=\"https://web.archive.org/web/20160628032324/http://www.music.columbia.edu/cmix/makegens.html\">\ |
| 1089 | makegens documentation</a> \ |
| 1090 | for more information on the GenX family of UGens. Currently coefficients past \ |
| 1091 | the 100th are ignored.\ |
| 1092 | \ |
| 1093 | Lookup can either be done using the lookup() function, or by driving the \ |
| 1094 | table with an input UGen, typically a Phasor. For an input signal between \ |
| 1095 | [ -1, 1 ], using the absolute value for [ -1, 0 ), GenX will output the \ |
| 1096 | table value indexed by the current input."; |
| 1097 | |
| 1098 | //--------------------------------------------------------------------- |
| 1099 | // init as base class: genX |
| 1100 | //--------------------------------------------------------------------- |
| 1101 | if( !type_engine_import_ugen_begin( env, "GenX", "UGen", env->global(), |
| 1102 | genX_ctor, genX_dtor, genX_tick, genX_pmsg, |
| 1103 | doc.c_str() ) ) |
| 1104 | return FALSE; |
| 1105 | |
| 1106 | // add member variable |
| 1107 | genX_offset_data = type_engine_import_mvar( env, "int", "@GenX_data", FALSE ); |
| 1108 | if( genX_offset_data == CK_INVALID_OFFSET ) goto error; |
| 1109 | |
| 1110 | func = make_new_mfun( "float", "lookup", genX_lookup ); //lookup table value |
| 1111 | func->add_arg( "float", "which" ); |
| 1112 | func->doc = "get lookup table value at index i [ -1, 1 ]; absolute value is used in the range [ -1, 0 )"; |
| 1113 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1114 | |
| 1115 | func = make_new_mfun( "float[]", "coefs", genX_coeffs ); //load table |
| 1116 | func->add_arg( "float", "v[]" ); |
| 1117 | func->doc = "set lookup table coefficients; meaning is dependent on subclass"; |
| 1118 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1119 | |
| 1120 | // add examples |
| 1121 | if( !type_engine_import_add_ex( env, "special/readme-GenX.ck" ) ) goto error; |
| 1122 | |
| 1123 | // end the class import |
| 1124 | type_engine_import_class_end( env ); |
| 1125 | |
| 1126 | |
| 1127 | //--------------------------------------------------------------------- |
| 1128 | // gen5 |
| 1129 | //--------------------------------------------------------------------- |
| 1130 | doc = "constructs a lookup table composed of sequential exponential curves. " |
| 1131 | "For a table with N curves, starting value of y', and value yn for lookup " |
| 1132 | "index xn, set the coefficients to [ y', y0, x0, ..., yN-1, xN-1 ]. " |
| 1133 | "Note that there must be an odd number of coefficients. " |
| 1134 | "If an even number of coefficients is specified, behavior is undefined. " |
| 1135 | "The sum of xn for 0 ≤ n < N must be 1. yn = 0 is approximated as 0.000001 " |
| 1136 | "to avoid strange results arising from the nature of exponential curves."; |
| 1137 | |
| 1138 | if( !type_engine_import_ugen_begin( env, "Gen5", "GenX", env->global(), |
| 1139 | NULL, NULL, genX_tick, NULL, doc.c_str() ) ) |
| 1140 | return FALSE; |
| 1141 | |
| 1142 | func = make_new_mfun( "float[]", "coefs", gen5_coeffs ); //load table |
| 1143 | func->add_arg( "float", "v[]" ); |
| 1144 | func->doc = "set lookup table coefficients."; |
| 1145 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1146 | |
| 1147 | // add examples |
| 1148 | if( !type_engine_import_add_ex( env, "special/Gen5-test.ck" ) ) goto error; |
| 1149 | |
| 1150 | // end the class import |
| 1151 | type_engine_import_class_end( env ); |
| 1152 | |
| 1153 | |
| 1154 | //--------------------------------------------------------------------- |
| 1155 | // gen7 |
| 1156 | //--------------------------------------------------------------------- |
| 1157 | doc = "constructs a lookup table composed of sequential line segments. " |
| 1158 | "For a table with N lines, starting value of y', and value yn for lookup" |
| 1159 | "index xn, set the coefficients to [ y', y0, x0, ..., yN-1, xN-1 ]. " |
| 1160 | "Note that there must be an odd number of coefficients. " |
| 1161 | "If an even number of coefficients is specified, behavior is undefined. " |
| 1162 | "The sum of xn for 0 ≤ n < N must be 1."; |
| 1163 | |
| 1164 | if( !type_engine_import_ugen_begin( env, "Gen7", "GenX", env->global(), |
| 1165 | NULL, NULL, genX_tick, NULL, doc.c_str() ) ) |
| 1166 | return FALSE; |
| 1167 | |
| 1168 | func = make_new_mfun( "float[]", "coefs", gen7_coeffs ); //load table |
| 1169 | func->add_arg( "float", "v[]" ); |
| 1170 | func->doc = "set lookup table coefficients."; |
| 1171 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1172 | |
| 1173 | // add examples |
| 1174 | if( !type_engine_import_add_ex( env, "special/Gen7-test.ck" ) ) goto error; |
| 1175 | |
| 1176 | // end the class import |
| 1177 | type_engine_import_class_end( env ); |
| 1178 | |
| 1179 | |
| 1180 | //--------------------------------------------------------------------- |
| 1181 | // gen9 |
| 1182 | //--------------------------------------------------------------------- |
| 1183 | doc = "constructs a lookup table of partials with specified amplitudes, " |
| 1184 | "phases, and harmonic ratios to the fundamental. " |
| 1185 | "Coefficients are specified in triplets of [ ratio, amplitude, phase ] " |
| 1186 | "arranged in a single linear array."; |
| 1187 | |
| 1188 | if( !type_engine_import_ugen_begin( env, "Gen9", "GenX", env->global(), |
| 1189 | NULL, NULL, genX_tick, NULL, doc.c_str() ) ) |
| 1190 | return FALSE; |
| 1191 | |
| 1192 | func = make_new_mfun( "float[]", "coefs", gen9_coeffs ); //load table |
| 1193 | func->add_arg( "float", "v[]" ); |
| 1194 | func->doc = "set lookup table coefficients."; |
| 1195 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1196 | |
| 1197 | // add examples |
| 1198 | if( !type_engine_import_add_ex( env, "special/Gen9-test.ck" ) ) goto error; |
| 1199 | |
| 1200 | // end the class import |
| 1201 | type_engine_import_class_end( env ); |
| 1202 | |
| 1203 | |
| 1204 | //--------------------------------------------------------------------- |
| 1205 | // gen10 |
| 1206 | //--------------------------------------------------------------------- |
| 1207 | doc = "constructs a lookup table of harmonic partials with specified " |
| 1208 | "amplitudes. The amplitude of partial n is specified by the nth element of " |
| 1209 | "the coefficients. For example, setting coefs to [ 1 ] will produce a sine wave."; |
| 1210 | |
| 1211 | if( !type_engine_import_ugen_begin( env, "Gen10", "GenX", env->global(), |
| 1212 | NULL, NULL, genX_tick, NULL, doc.c_str() ) ) |
| 1213 | return FALSE; |
| 1214 | |
| 1215 | func = make_new_mfun( "float[]", "coefs", gen10_coeffs ); //load table |
| 1216 | func->add_arg( "float", "v[]" ); |
| 1217 | func->doc = "set lookup table coefficients."; |
| 1218 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1219 | |
| 1220 | // add examples |
| 1221 | if( !type_engine_import_add_ex( env, "special/Gen10-test.ck" ) ) goto error; |
| 1222 | |
| 1223 | // end the class import |
| 1224 | type_engine_import_class_end( env ); |
| 1225 | |
| 1226 | |
| 1227 | //--------------------------------------------------------------------- |
| 1228 | // gen17 |
| 1229 | //--------------------------------------------------------------------- |
| 1230 | doc = "constructs a Chebyshev polynomial wavetable with harmonic partials " |
| 1231 | "of specified weights. The weight of partial n is specified by the nth " |
| 1232 | "element of the coefficients. Primarily used for waveshaping, driven by a " |
| 1233 | "SinOsc instead of a Phasor. See " |
| 1234 | "<a href=\"http://crca.ucsd.edu/~msp/techniques/v0.08/book-html/node74.html\">" |
| 1235 | "http://crca.ucsd.edu/~msp/techniques/v0.08/book-html/node74.html</a> and " |
| 1236 | "<a href=\"http://en.wikipedia.org/wiki/Distortion_synthesis\">" |
| 1237 | "http://en.wikipedia.org/wiki/Distortion_synthesis</a> for more information."; |
| 1238 | |
| 1239 | if( !type_engine_import_ugen_begin( env, "Gen17", "GenX", env->global(), |
| 1240 | NULL, NULL, genX_tick, NULL, doc.c_str() ) ) |
| 1241 | return FALSE; |
| 1242 | |
| 1243 | func = make_new_mfun( "float[]", "coefs", gen17_coeffs ); //load table |
| 1244 | func->add_arg( "float", "v[]" ); |
| 1245 | func->doc = "set lookup table coefficients."; |
| 1246 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1247 | |
| 1248 | // add examples |
| 1249 | if( !type_engine_import_add_ex( env, "special/Gen17-test.ck" ) ) goto error; |
| 1250 | |
| 1251 | // end the class import |
| 1252 | type_engine_import_class_end( env ); |
| 1253 | |
| 1254 | |
| 1255 | //--------------------------------------------------------------------- |
| 1256 | // CurveTable |
| 1257 | //--------------------------------------------------------------------- |
| 1258 | doc = "constructs a wavetable composed of segments of variable times, " |
| 1259 | "values, and curvatures. Coefficients are specified as a single linear " |
| 1260 | "array of triplets of [ time, value, curvature ] followed by a final duple " |
| 1261 | "of [ time, value ] to specify the final value of the table. time values " |
| 1262 | "are expressed in unitless, ascending values. For curvature equal to 0, " |
| 1263 | "the segment is a line; for curvature less than 0, the segment is a convex " |
| 1264 | "curve; for curvature greater than 0, the segment is a concave curve."; |
| 1265 | |
| 1266 | if( !type_engine_import_ugen_begin( env, "CurveTable", "GenX", env->global(), |
| 1267 | NULL, NULL, genX_tick, NULL, doc.c_str() ) ) |
| 1268 | return FALSE; |
| 1269 | |
| 1270 | // add examples |
| 1271 | if( !type_engine_import_add_ex( env, "special/GenX-CurveTable-test.ck" ) ) goto error; |
| 1272 | |
| 1273 | func = make_new_mfun( "float[]", "coefs", curve_coeffs ); //load table |
| 1274 | func->add_arg( "float", "v[]" ); |
| 1275 | func->doc = "set lookup table coefficients."; |
| 1276 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1277 | |
| 1278 | // end the class import |
| 1279 | type_engine_import_class_end( env ); |
| 1280 | |
| 1281 | //--------------------------------------------------------------------- |
| 1282 | // WarpTable |
| 1283 | //--------------------------------------------------------------------- |
| 1284 | doc = "An end-constrained mapping table, mostly useful for conditioning control signals."; |
| 1285 | if( !type_engine_import_ugen_begin( env, "WarpTable", "GenX", env->global(), |
| 1286 | NULL, NULL, genX_tick, NULL, doc.c_str() ) ) |
| 1287 | return FALSE; |
| 1288 | |
| 1289 | func = make_new_mfun( "float[]", "coefs", warp_coeffs ); //load table |
| 1290 | func->add_arg( "float", "v[]" ); |
| 1291 | func->doc = "set lookup table coefficients."; |
| 1292 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1293 | |
| 1294 | /* |
| 1295 | func = make_new_mfun( "float", "coefs", warp_coeffs ); //load table |
| 1296 | func->add_arg( "float", "asym" ); |
| 1297 | func->add_arg( "float", "sym" ); |
| 1298 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1299 | */ |
| 1300 | |
| 1301 | // add examples |
| 1302 | if( !type_engine_import_add_ex( env, "special/GenX-WarpTable-test.ck" ) ) goto error; |
| 1303 | |
| 1304 | // end the class import |
| 1305 | type_engine_import_class_end( env ); |
| 1306 | |
| 1307 | return TRUE; |
| 1308 | |
| 1309 | error: |
| 1310 | |
| 1311 | // end the class import |
| 1312 | type_engine_import_class_end( env ); |
| 1313 | |
| 1314 | return FALSE; |
| 1315 | } |
| 1316 | |
| 1317 | |
| 1318 | |
| 1319 | |
| 1320 | //----------------------------------------------------------------------------- |
| 1321 | // name: struct genX_Data |
| 1322 | // desc: ... |
| 1323 | //----------------------------------------------------------------------------- |
| 1324 | #define genX_tableSize 4096 |
| 1325 | #define genX_MAX_COEFFS 100 |
| 1326 | |
| 1327 | struct genX_Data |
| 1328 | { |
| 1329 | t_CKUINT genX_type; |
| 1330 | t_CKDOUBLE genX_table[genX_tableSize]; |
| 1331 | // gewang: was int |
| 1332 | t_CKINT sync; |
| 1333 | t_CKUINT srate; |
| 1334 | t_CKFLOAT xtemp; |
| 1335 | t_CKDOUBLE coeffs[genX_MAX_COEFFS]; |
| 1336 | |
| 1337 | genX_Data() |
| 1338 | { |
| 1339 | //initialize data here |
| 1340 | sync = 0; |
| 1341 | srate = g_srateOsc; |
| 1342 | |
| 1343 | t_CKINT i; |
| 1344 | for( i=0; i<genX_MAX_COEFFS; i++ ) coeffs[i] = 0.; |
| 1345 | for( i=0; i<genX_tableSize; i++ ) genX_table[i] = 0.; |
| 1346 | } |
| 1347 | }; |
| 1348 | |
| 1349 | |
| 1350 | |
| 1351 | |
| 1352 | //----------------------------------------------------------------------------- |
| 1353 | // name: genX_ctor() |
| 1354 | // desc: ... |
| 1355 | //----------------------------------------------------------------------------- |
| 1356 | CK_DLL_CTOR( genX_ctor ) |
| 1357 | { |
| 1358 | genX_Data * d = new genX_Data; |
| 1359 | Chuck_DL_Return r; |
| 1360 | // return data to be used later |
| 1361 | OBJ_MEMBER_UINT(SELF, genX_offset_data) = (t_CKUINT)d; |
| 1362 | //gen10_coeffs( SELF, &(d->xtemp), &r ); |
| 1363 | } |
| 1364 | |
| 1365 | |
| 1366 | |
| 1367 | |
| 1368 | //----------------------------------------------------------------------------- |
| 1369 | // name: genX_dtor() |
| 1370 | // desc: ... |
| 1371 | //----------------------------------------------------------------------------- |
| 1372 | CK_DLL_DTOR( genX_dtor ) |
| 1373 | { |
| 1374 | // get the data |
| 1375 | genX_Data * data = (genX_Data *)OBJ_MEMBER_UINT(SELF, genX_offset_data ); |
| 1376 | // delete |
| 1377 | CK_SAFE_DELETE(data); |
| 1378 | // set to NULL |
| 1379 | OBJ_MEMBER_UINT(SELF, genX_offset_data) = 0; |
| 1380 | } |
| 1381 | |
| 1382 | |
| 1383 | |
| 1384 | |
| 1385 | //----------------------------------------------------------------------------- |
| 1386 | // name: genX_tick() |
| 1387 | // desc: ... |
| 1388 | //----------------------------------------------------------------------------- |
| 1389 | CK_DLL_TICK( genX_tick ) |
| 1390 | { |
| 1391 | // get the data |
| 1392 | genX_Data * d = (genX_Data *)OBJ_MEMBER_UINT(SELF, genX_offset_data ); |
| 1393 | Chuck_UGen * ugen = (Chuck_UGen *)SELF; |
| 1394 | // t_CKBOOL inc_phase = TRUE; |
| 1395 | |
| 1396 | t_CKDOUBLE in_index = 0.0; |
| 1397 | t_CKDOUBLE scaled_index = 0.0; |
| 1398 | t_CKDOUBLE alpha = 0.0, omAlpha = 0.0; |
| 1399 | t_CKUINT lowIndex = 0, hiIndex = 0; |
| 1400 | t_CKDOUBLE outvalue = 0.0; |
| 1401 | |
| 1402 | // if input |
| 1403 | if( ugen->m_num_src ) { |
| 1404 | in_index = in; |
| 1405 | // gewang: moved this to here |
| 1406 | if( in_index < 0. ) in_index = -in_index; |
| 1407 | //scaled_index = (in_index + 1.) * 0.5 * genX_tableSize; //drive with oscillator, [-1, 1] |
| 1408 | scaled_index = in_index * genX_tableSize; //drive with phasor [0, 1] |
| 1409 | } else { |
| 1410 | scaled_index = 0.; |
| 1411 | } |
| 1412 | |
| 1413 | // set up interpolation parameters |
| 1414 | lowIndex = (t_CKUINT)scaled_index; |
| 1415 | hiIndex = lowIndex + 1; |
| 1416 | alpha = scaled_index - lowIndex; |
| 1417 | omAlpha = 1. - alpha; |
| 1418 | |
| 1419 | // check table index ranges |
| 1420 | while(lowIndex >= genX_tableSize) lowIndex -= genX_tableSize; |
| 1421 | while(hiIndex >= genX_tableSize) hiIndex -= genX_tableSize; |
| 1422 | |
| 1423 | // could just call |
| 1424 | // outvalue = genX_lookup(in_index);? |
| 1425 | |
| 1426 | // calculate output value with linear interpolation |
| 1427 | outvalue = d->genX_table[lowIndex]*omAlpha + d->genX_table[hiIndex]*alpha; |
| 1428 | |
| 1429 | // set output |
| 1430 | *out = (SAMPLE)outvalue; |
| 1431 | |
| 1432 | return TRUE; |
| 1433 | } |
| 1434 | |
| 1435 | |
| 1436 | //----------------------------------------------------------------------------- |
| 1437 | // name: genX_lookup() |
| 1438 | // desc: lookup call for all gens |
| 1439 | //----------------------------------------------------------------------------- |
| 1440 | CK_DLL_CTRL( genX_lookup ) |
| 1441 | { |
| 1442 | // get the data |
| 1443 | genX_Data * d = (genX_Data *)OBJ_MEMBER_UINT(SELF, genX_offset_data ); |
| 1444 | |
| 1445 | t_CKFLOAT in_index; |
| 1446 | t_CKFLOAT scaled_index; |
| 1447 | t_CKFLOAT alpha, omAlpha; |
| 1448 | t_CKUINT lowIndex, hiIndex; |
| 1449 | t_CKFLOAT outvalue; |
| 1450 | |
| 1451 | in_index = GET_NEXT_FLOAT(ARGS); |
| 1452 | |
| 1453 | // gewang: moved to here |
| 1454 | if( in_index < 0 ) in_index = -in_index; |
| 1455 | scaled_index = in_index * (genX_tableSize - 1); //drive with phasor [0, 1] |
| 1456 | |
| 1457 | //set up interpolation parameters |
| 1458 | lowIndex = (t_CKUINT)scaled_index; |
| 1459 | hiIndex = lowIndex + 1; |
| 1460 | alpha = scaled_index - lowIndex; |
| 1461 | omAlpha = 1. - alpha; |
| 1462 | |
| 1463 | //check table index ranges |
| 1464 | while(lowIndex >= genX_tableSize) lowIndex -= genX_tableSize; |
| 1465 | while(hiIndex >= genX_tableSize) hiIndex -= genX_tableSize; |
| 1466 | |
| 1467 | //calculate output value with linear interpolation |
| 1468 | outvalue = d->genX_table[lowIndex]*omAlpha + d->genX_table[hiIndex]*alpha; |
| 1469 | |
| 1470 | RETURN->v_float = (t_CKFLOAT)outvalue; |
| 1471 | |
| 1472 | } |
| 1473 | |
| 1474 | |
| 1475 | //----------------------------------------------------------------------------- |
| 1476 | // name: gen5_coeffs() |
| 1477 | // desc: setup table for gen5 |
| 1478 | //----------------------------------------------------------------------------- |
| 1479 | CK_DLL_CTRL( gen5_coeffs ) |
| 1480 | { |
| 1481 | // get data |
| 1482 | genX_Data * d = (genX_Data *)OBJ_MEMBER_UINT(SELF, genX_offset_data); |
| 1483 | t_CKINT i = 0, j, k, l, size; |
| 1484 | t_CKFLOAT wmax, xmax=0.0, c, amp2, amp1, coeffs[genX_MAX_COEFFS]; |
| 1485 | |
| 1486 | Chuck_ArrayFloat * in_args = (Chuck_ArrayFloat *)GET_CK_OBJECT(ARGS); |
| 1487 | |
| 1488 | // CK_FPRINTF_STDOUT( "calling gen10coeffs, %d\n", weights ); |
| 1489 | if(in_args == 0) return; |
| 1490 | size = in_args->size(); |
| 1491 | if(size >= genX_MAX_COEFFS) size = genX_MAX_COEFFS - 1; |
| 1492 | |
| 1493 | t_CKFLOAT v; |
| 1494 | for(t_CKUINT ii = 0; ii<size; ii++) { |
| 1495 | in_args->get(ii, &v); |
| 1496 | // CK_FPRINTF_STDOUT( "weight %d = %f...\n", ii, v ); |
| 1497 | coeffs[ii] = v; |
| 1498 | } |
| 1499 | |
| 1500 | amp2 = coeffs[0]; |
| 1501 | if (amp2 <= 0.0) amp2 = 0.000001; |
| 1502 | for(k = 1; k < size; k += 2) { |
| 1503 | amp1 = amp2; |
| 1504 | amp2 = coeffs[k+1]; |
| 1505 | if (amp2 <= 0.0) amp2 = 0.000001; |
| 1506 | j = i + 1; |
| 1507 | d->genX_table[i] = amp1; |
| 1508 | c = (t_CKFLOAT) pow((amp2/amp1),(1./(coeffs[k]*genX_tableSize))); |
| 1509 | i = (t_CKINT)((j - 1) + coeffs[k]*genX_tableSize); |
| 1510 | for(l = j; l < i; l++) { |
| 1511 | if(l < genX_tableSize) |
| 1512 | d->genX_table[l] = d->genX_table[l-1] * c; |
| 1513 | } |
| 1514 | } |
| 1515 | |
| 1516 | for(j = 0; j < genX_tableSize; j++) { |
| 1517 | if ((wmax = fabs(d->genX_table[j])) > xmax) xmax = wmax; |
| 1518 | // CK_FPRINTF_STDOUT( "table current = %f\n", wmax); |
| 1519 | } |
| 1520 | // CK_FPRINTF_STDOUT( "table max = %f\n", xmax); |
| 1521 | for(j = 0; j < genX_tableSize; j++) { |
| 1522 | d->genX_table[j] /= xmax; |
| 1523 | } |
| 1524 | |
| 1525 | // return |
| 1526 | RETURN->v_object = in_args; |
| 1527 | } |
| 1528 | |
| 1529 | |
| 1530 | //----------------------------------------------------------------------------- |
| 1531 | // name: gen7_coeffs() |
| 1532 | // desc: setup table for gen7 |
| 1533 | //----------------------------------------------------------------------------- |
| 1534 | CK_DLL_CTRL( gen7_coeffs ) |
| 1535 | { |
| 1536 | // get data |
| 1537 | genX_Data * d = (genX_Data *)OBJ_MEMBER_UINT(SELF, genX_offset_data); |
| 1538 | t_CKINT i=0, j, k, l, size; |
| 1539 | t_CKFLOAT wmax, xmax = 0.0, amp2, amp1, coeffs[genX_MAX_COEFFS]; |
| 1540 | |
| 1541 | Chuck_ArrayFloat * in_args = (Chuck_ArrayFloat *)GET_CK_OBJECT(ARGS); |
| 1542 | |
| 1543 | // CK_FPRINTF_STDOUT( "calling gen10coeffs, %d\n", weights ); |
| 1544 | if(in_args == 0) return; |
| 1545 | size = in_args->size(); |
| 1546 | if(size >= genX_MAX_COEFFS) size = genX_MAX_COEFFS - 1; |
| 1547 | |
| 1548 | t_CKFLOAT v; |
| 1549 | for(t_CKUINT ii = 0; ii<size; ii++) { |
| 1550 | in_args->get(ii, &v); |
| 1551 | // CK_FPRINTF_STDOUT( "weight %d = %f...\n", ii, v ); |
| 1552 | coeffs[ii] = v; |
| 1553 | } |
| 1554 | |
| 1555 | amp2 = coeffs[0]; |
| 1556 | for (k = 1; k < size; k += 2) { |
| 1557 | amp1 = amp2; |
| 1558 | amp2 = coeffs[k + 1]; |
| 1559 | j = i + 1; |
| 1560 | i = (t_CKINT)(j + coeffs[k]*genX_tableSize - 1); |
| 1561 | for (l = j; l <= i; l++) { |
| 1562 | if (l <= genX_tableSize) |
| 1563 | d->genX_table[l - 1] = amp1 + |
| 1564 | (amp2 - amp1) * (double) (l - j) / (i - j + 1); |
| 1565 | } |
| 1566 | } |
| 1567 | |
| 1568 | for(j = 0; j < genX_tableSize; j++) { |
| 1569 | if ((wmax = fabs(d->genX_table[j])) > xmax) xmax = wmax; |
| 1570 | // CK_FPRINTF_STDOUT( "table current = %f\n", wmax); |
| 1571 | } |
| 1572 | // CK_FPRINTF_STDOUT( "table max = %f\n", xmax); |
| 1573 | for(j = 0; j < genX_tableSize; j++) { |
| 1574 | d->genX_table[j] /= xmax; |
| 1575 | } |
| 1576 | |
| 1577 | // return |
| 1578 | RETURN->v_object = in_args; |
| 1579 | } |
| 1580 | |
| 1581 | |
| 1582 | //----------------------------------------------------------------------------- |
| 1583 | // name: gen9_coeffs() |
| 1584 | // desc: setup table for gen9 |
| 1585 | //----------------------------------------------------------------------------- |
| 1586 | CK_DLL_CTRL( gen9_coeffs ) |
| 1587 | { |
| 1588 | // get data |
| 1589 | genX_Data * d = (genX_Data *)OBJ_MEMBER_UINT(SELF, genX_offset_data); |
| 1590 | t_CKINT i, j, size; |
| 1591 | t_CKDOUBLE wmax, xmax=0.0; |
| 1592 | t_CKFLOAT coeffs[genX_MAX_COEFFS]; |
| 1593 | |
| 1594 | Chuck_ArrayFloat * weights = (Chuck_ArrayFloat *)GET_CK_OBJECT(ARGS); |
| 1595 | |
| 1596 | // CK_FPRINTF_STDOUT( "calling gen10coeffs, %d\n", weights ); |
| 1597 | if(weights == 0) return; |
| 1598 | size = weights->size(); |
| 1599 | if(size >= genX_MAX_COEFFS) size = genX_MAX_COEFFS - 1; |
| 1600 | |
| 1601 | |
| 1602 | t_CKFLOAT v; |
| 1603 | for(t_CKUINT ii = 0; ii<size; ii++) { |
| 1604 | weights->get(ii, &v); |
| 1605 | // CK_FPRINTF_STDOUT( "weight %d = %f...\n", ii, v ); |
| 1606 | coeffs[ii] = v; |
| 1607 | } |
| 1608 | |
| 1609 | for(j = size - 1; j > 0; j -= 3) { |
| 1610 | if(coeffs[j - 1] != 0) { |
| 1611 | for(i = 0; i < genX_tableSize; i++) { |
| 1612 | t_CKDOUBLE val = sin(CK_TWO_PI * ((t_CKDOUBLE) i / ((t_CKDOUBLE) (genX_tableSize) |
| 1613 | / coeffs[j - 2]) + coeffs[j] / 360.)); |
| 1614 | d->genX_table[i] += val * coeffs[j - 1]; |
| 1615 | } |
| 1616 | } |
| 1617 | } |
| 1618 | |
| 1619 | for(j = 0; j < genX_tableSize; j++) { |
| 1620 | if ((wmax = fabs(d->genX_table[j])) > xmax) xmax = wmax; |
| 1621 | // CK_FPRINTF_STDOUT( "table current = %f\n", wmax); |
| 1622 | } |
| 1623 | // CK_FPRINTF_STDOUT( "table max = %f\n", xmax); |
| 1624 | for(j = 0; j < genX_tableSize; j++) { |
| 1625 | d->genX_table[j] /= xmax; |
| 1626 | } |
| 1627 | |
| 1628 | // return |
| 1629 | RETURN->v_object = weights; |
| 1630 | } |
| 1631 | |
| 1632 | |
| 1633 | //----------------------------------------------------------------------------- |
| 1634 | // name: gen10_coeffs() |
| 1635 | // desc: setup table for gen10 |
| 1636 | //----------------------------------------------------------------------------- |
| 1637 | CK_DLL_CTRL( gen10_coeffs ) |
| 1638 | { |
| 1639 | // get data |
| 1640 | genX_Data * d = (genX_Data *)OBJ_MEMBER_UINT(SELF, genX_offset_data); |
| 1641 | t_CKINT i, j, size; |
| 1642 | t_CKDOUBLE wmax, xmax=0.0; |
| 1643 | |
| 1644 | Chuck_ArrayFloat * weights = (Chuck_ArrayFloat *)GET_CK_OBJECT(ARGS); |
| 1645 | |
| 1646 | // CK_FPRINTF_STDOUT( "calling gen10coeffs, %d\n", weights ); |
| 1647 | if(weights==0) return; |
| 1648 | size = weights->size(); |
| 1649 | if(size >= genX_MAX_COEFFS) size = genX_MAX_COEFFS - 1; |
| 1650 | |
| 1651 | t_CKFLOAT v; |
| 1652 | for(t_CKUINT ii = 0; ii<size; ii++) { |
| 1653 | weights->get(ii, &v); |
| 1654 | // CK_FPRINTF_STDOUT( "weight %d = %f...\n", ii, v ); |
| 1655 | d->coeffs[ii] = v; |
| 1656 | } |
| 1657 | |
| 1658 | // reset table - this allow not only to add but also to remove partials |
| 1659 | // 1.4.1.0 (thanks mariobuoninfante) |
| 1660 | for( i = 0; i < genX_tableSize; i++ ) |
| 1661 | { d->genX_table[i] = 0.; } |
| 1662 | // set j to size |
| 1663 | j = size; // j = genX_MAX_COEFFS; |
| 1664 | |
| 1665 | t_CKDOUBLE val; |
| 1666 | while( j-- ) { |
| 1667 | if( d->coeffs[j] != 0 ) { |
| 1668 | for( i = 0; i < genX_tableSize; i++ ) { |
| 1669 | // 1.4.1.0 updated to (j+1.0) to force floating point division |
| 1670 | val = (t_CKDOUBLE)( CK_TWO_PI * i / (genX_tableSize/(j+1.0)) ); |
| 1671 | d->genX_table[i] += sin(val) * d->coeffs[j]; |
| 1672 | } |
| 1673 | } |
| 1674 | } |
| 1675 | |
| 1676 | for(j = 0; j < genX_tableSize; j++) { |
| 1677 | if ((wmax = fabs(d->genX_table[j])) > xmax) xmax = wmax; |
| 1678 | // CK_FPRINTF_STDOUT( "table current = %f\n", wmax); |
| 1679 | } |
| 1680 | |
| 1681 | // CK_FPRINTF_STDOUT( "table max = %f\n", xmax); |
| 1682 | for(j = 0; j < genX_tableSize; j++) { |
| 1683 | d->genX_table[j] /= xmax; |
| 1684 | } |
| 1685 | |
| 1686 | // return |
| 1687 | RETURN->v_object = weights; |
| 1688 | } |
| 1689 | |
| 1690 | |
| 1691 | //----------------------------------------------------------------------------- |
| 1692 | // name: gen17_coeffs() |
| 1693 | // desc: setup table for gen17 |
| 1694 | //----------------------------------------------------------------------------- |
| 1695 | CK_DLL_CTRL( gen17_coeffs ) |
| 1696 | { |
| 1697 | // get data |
| 1698 | genX_Data * d = (genX_Data *)OBJ_MEMBER_UINT(SELF, genX_offset_data); |
| 1699 | t_CKINT i, j, size; |
| 1700 | t_CKDOUBLE Tn, Tn1, Tn2, dg, x, wmax = 0.0, xmax = 0.0; |
| 1701 | t_CKFLOAT coeffs[genX_MAX_COEFFS]; |
| 1702 | |
| 1703 | Chuck_ArrayFloat * weights = (Chuck_ArrayFloat *)GET_CK_OBJECT(ARGS); |
| 1704 | |
| 1705 | // CK_FPRINTF_STDOUT( "calling gen17coeffs, %d\n", weights ); |
| 1706 | if(weights == 0) return; |
| 1707 | size = weights->size(); |
| 1708 | if(size >= genX_MAX_COEFFS) size = genX_MAX_COEFFS - 1; |
| 1709 | |
| 1710 | dg = (t_CKDOUBLE) (genX_tableSize / 2. - .5); |
| 1711 | |
| 1712 | t_CKFLOAT v; |
| 1713 | for(t_CKUINT ii = 0; ii<size; ii++) { |
| 1714 | weights->get(ii, &v); |
| 1715 | // CK_FPRINTF_STDOUT( "weight %d = %f...\n", ii, v ); |
| 1716 | coeffs[ii] = v; |
| 1717 | } |
| 1718 | |
| 1719 | for (i = 0; i < genX_tableSize; i++) { |
| 1720 | x = (t_CKDOUBLE)(i / dg - 1.); |
| 1721 | d->genX_table[i] = 0.0; |
| 1722 | Tn1 = 1.0; |
| 1723 | Tn = x; |
| 1724 | for (j = 0; j < size; j++) { |
| 1725 | d->genX_table[i] = coeffs[j] * Tn + d->genX_table[i]; |
| 1726 | Tn2 = Tn1; |
| 1727 | Tn1 = Tn; |
| 1728 | Tn = 2.0 * x * Tn1 - Tn2; |
| 1729 | } |
| 1730 | } |
| 1731 | |
| 1732 | for(j = 0; j < genX_tableSize; j++) { |
| 1733 | if ((wmax = fabs(d->genX_table[j])) > xmax) xmax = wmax; |
| 1734 | // CK_FPRINTF_STDOUT( "table current = %f\n", wmax); |
| 1735 | } |
| 1736 | // CK_FPRINTF_STDOUT( "table max = %f\n", xmax); |
| 1737 | for(j = 0; j < genX_tableSize; j++) { |
| 1738 | d->genX_table[j] /= xmax; |
| 1739 | // CK_FPRINTF_STDOUT( "table current = %f\n", d->genX_table[j]); |
| 1740 | } |
| 1741 | |
| 1742 | // return |
| 1743 | RETURN->v_object = weights; |
| 1744 | } |
| 1745 | |
| 1746 | |
| 1747 | //----------------------------------------------------------------------------- |
| 1748 | // name: curve_coeffs() |
| 1749 | // desc: setup table for Curve |
| 1750 | // ported from RTcmix |
| 1751 | //----------------------------------------------------------------------------- |
| 1752 | #define MAX_CURVE_PTS 256 |
| 1753 | CK_DLL_CTRL( curve_coeffs ) |
| 1754 | { |
| 1755 | // get data |
| 1756 | genX_Data * d = (genX_Data *)OBJ_MEMBER_UINT(SELF, genX_offset_data); |
| 1757 | t_CKINT i, points, nargs, seglen = 0, len = genX_tableSize; |
| 1758 | t_CKDOUBLE factor, *ptr;//, xmax=0.0; |
| 1759 | t_CKDOUBLE time[MAX_CURVE_PTS], value[MAX_CURVE_PTS], alpha[MAX_CURVE_PTS]; |
| 1760 | t_CKFLOAT coeffs[genX_MAX_COEFFS]; |
| 1761 | t_CKUINT ii = 0; |
| 1762 | t_CKFLOAT v = 0.0; |
| 1763 | |
| 1764 | Chuck_ArrayFloat * weights = (Chuck_ArrayFloat *)GET_CK_OBJECT(ARGS); |
| 1765 | |
| 1766 | // CK_FPRINTF_STDOUT( "calling gen17coeffs, %d\n", weights ); |
| 1767 | if(weights==0) goto done; |
| 1768 | |
| 1769 | nargs = weights->size(); |
| 1770 | if (nargs < 5 || (nargs % 3) != 2) { // check number of args |
| 1771 | CK_FPRINTF_STDERR( "[chuck](via CurveTable): usage: \n size, time1, value1, curvature1, [ timeN-1, valueN-1, curvatureN-1, ] timeN, valueN)\n" ); |
| 1772 | goto done; |
| 1773 | } |
| 1774 | if ((nargs / 3) + 1 > MAX_CURVE_PTS) { |
| 1775 | CK_FPRINTF_STDERR( "[chuck](via CurveTable): too many arguments.\n" ); |
| 1776 | goto done; |
| 1777 | } |
| 1778 | |
| 1779 | for(ii = 0; ii<nargs; ii++) { |
| 1780 | weights->get(ii, &v); |
| 1781 | // CK_FPRINTF_STDOUT( "weight %d = %f...\n", ii, v ); |
| 1782 | coeffs[ii] = v; |
| 1783 | } |
| 1784 | |
| 1785 | if (coeffs[0] != 0.0) { |
| 1786 | CK_FPRINTF_STDERR( "[chuck](via CurveTable): first time must be zero.\n" ); |
| 1787 | goto done; |
| 1788 | } |
| 1789 | |
| 1790 | for (i = points = 0; i < nargs; points++) { |
| 1791 | time[points] = (t_CKDOUBLE) coeffs[i++]; |
| 1792 | if (points > 0 && time[points] < time[points - 1]) |
| 1793 | goto time_err; |
| 1794 | value[points] = (t_CKDOUBLE) coeffs[i++]; |
| 1795 | if (i < nargs) |
| 1796 | alpha[points] = (t_CKDOUBLE) coeffs[i++]; |
| 1797 | } |
| 1798 | |
| 1799 | factor = (t_CKDOUBLE) (len - 1) / time[points - 1]; |
| 1800 | for (i = 0; i < points; i++) |
| 1801 | time[i] *= factor; |
| 1802 | |
| 1803 | ptr = d->genX_table; |
| 1804 | for (i = 0; i < points - 1; i++) { |
| 1805 | seglen = (t_CKINT) (floor(time[i + 1] + 0.5) - floor(time[i] + 0.5)) + 1; |
| 1806 | _transition(value[i], alpha[i], value[i + 1], seglen, ptr); |
| 1807 | ptr += seglen - 1; |
| 1808 | } |
| 1809 | |
| 1810 | done: |
| 1811 | // return |
| 1812 | RETURN->v_object = weights; |
| 1813 | |
| 1814 | return; |
| 1815 | |
| 1816 | time_err: |
| 1817 | CK_FPRINTF_STDERR( "[chuck](via CurveTable): times must be in ascending order.\n" ); |
| 1818 | |
| 1819 | // return |
| 1820 | RETURN->v_object = weights; |
| 1821 | |
| 1822 | return; |
| 1823 | } |
| 1824 | |
| 1825 | |
| 1826 | static void _transition(t_CKDOUBLE a, t_CKDOUBLE alpha, t_CKDOUBLE b, t_CKINT n, t_CKDOUBLE *output) |
| 1827 | { |
| 1828 | t_CKINT i; |
| 1829 | t_CKDOUBLE delta, interval = 0.0; |
| 1830 | |
| 1831 | delta = b - a; |
| 1832 | |
| 1833 | if (n <= 1) { |
| 1834 | //warn("maketable (curve)", "Trying to transition over 1 array slot; " |
| 1835 | // "time between points is too short"); |
| 1836 | *output = a; |
| 1837 | return; |
| 1838 | } |
| 1839 | interval = 1.0 / (n - 1.0); |
| 1840 | |
| 1841 | if (alpha != 0.0) { |
| 1842 | t_CKDOUBLE denom = 1.0 / (1.0 - exp(alpha)); |
| 1843 | for (i = 0; i < n; i++) |
| 1844 | *output++ = (a + delta |
| 1845 | * (1.0 - exp((double) i * alpha * interval)) * denom); |
| 1846 | } |
| 1847 | else |
| 1848 | for (i = 0; i < n; i++) |
| 1849 | *output++ = a + delta * i * interval; |
| 1850 | } |
| 1851 | |
| 1852 | |
| 1853 | //----------------------------------------------------------------------------- |
| 1854 | // name: warp_coeffs() |
| 1855 | // desc: setup table for warp |
| 1856 | //----------------------------------------------------------------------------- |
| 1857 | CK_DLL_CTRL( warp_coeffs ) |
| 1858 | { |
| 1859 | // get data |
| 1860 | genX_Data * d = (genX_Data *)OBJ_MEMBER_UINT(SELF, genX_offset_data); |
| 1861 | t_CKINT i = 0; |
| 1862 | |
| 1863 | t_CKFLOAT k_asym = 1.; |
| 1864 | t_CKFLOAT k_sym = 1.; |
| 1865 | |
| 1866 | // gewang: |
| 1867 | Chuck_ArrayFloat * weights = (Chuck_ArrayFloat *)GET_CK_OBJECT(ARGS); |
| 1868 | // check for size |
| 1869 | if( weights->size() != 2 ) |
| 1870 | { |
| 1871 | // error |
| 1872 | CK_FPRINTF_STDERR( "[chuck](via WarpTable): expects array of exactly 2 elements.\n" ); |
| 1873 | goto done; |
| 1874 | } |
| 1875 | |
| 1876 | weights->get( 0, &k_asym ); // (t_CKDOUBLE) GET_NEXT_FLOAT(ARGS); |
| 1877 | weights->get( 1, &k_sym ); // (t_CKDOUBLE) GET_NEXT_FLOAT(ARGS); |
| 1878 | |
| 1879 | for (i = 0; i < genX_tableSize; i++) { |
| 1880 | t_CKDOUBLE inval = (t_CKDOUBLE) i/(genX_tableSize - 1); |
| 1881 | if(k_asym == 1 && k_sym == 1) { |
| 1882 | d->genX_table[i] = inval; |
| 1883 | } else if(k_sym == 1) { |
| 1884 | d->genX_table[i] = _asymwarp(inval, k_asym); |
| 1885 | } else if(k_asym == 1) { |
| 1886 | d->genX_table[i] = _symwarp(inval, k_sym); |
| 1887 | } else { |
| 1888 | inval = _asymwarp(inval, k_asym); |
| 1889 | d->genX_table[i] = _symwarp(inval, k_sym); |
| 1890 | } |
| 1891 | // CK_FPRINTF_STDOUT( "table %d = %f\n", i, d->genX_table[i] ); |
| 1892 | } |
| 1893 | |
| 1894 | done: |
| 1895 | |
| 1896 | // return |
| 1897 | RETURN->v_object = weights; |
| 1898 | } |
| 1899 | |
| 1900 | |
| 1901 | t_CKDOUBLE _asymwarp(t_CKDOUBLE inval, t_CKDOUBLE k) |
| 1902 | { |
| 1903 | return (pow(k, inval) - 1.) / (k - 1.); |
| 1904 | } |
| 1905 | |
| 1906 | |
| 1907 | t_CKDOUBLE _symwarp(t_CKDOUBLE inval, t_CKDOUBLE k) |
| 1908 | { |
| 1909 | t_CKDOUBLE sym_warped; |
| 1910 | if(inval >= 0.5) { |
| 1911 | sym_warped = pow(2.*inval - 1., 1./k); |
| 1912 | return (sym_warped + 1.) * 0.5; |
| 1913 | |
| 1914 | } |
| 1915 | inval = 1. - inval; // for S curve |
| 1916 | sym_warped = pow(2.*inval - 1., 1./k); |
| 1917 | sym_warped = (sym_warped + 1.) * 0.5; |
| 1918 | |
| 1919 | return 1. - sym_warped; |
| 1920 | } |
| 1921 | |
| 1922 | |
| 1923 | // also do RTcmix "spline" on a rainy day |
| 1924 | |
| 1925 | |
| 1926 | //----------------------------------------------------------------------------- |
| 1927 | // name: genX_pmsg() |
| 1928 | // desc: ... |
| 1929 | //----------------------------------------------------------------------------- |
| 1930 | CK_DLL_PMSG( genX_pmsg ) |
| 1931 | { |
| 1932 | //genX_Data * d = (genX_Data *)OBJ_MEMBER_UINT(SELF, genX_offset_data ); |
| 1933 | if( !strcmp( MSG, "print" ) ) |
| 1934 | { |
| 1935 | // CK_FPRINTF_STDOUT( "genX:" ); |
| 1936 | return TRUE; |
| 1937 | } |
| 1938 | |
| 1939 | // didn't handle |
| 1940 | return FALSE; |
| 1941 | } |
| 1942 | |
| 1943 | |
| 1944 | //----------------------------------------------------------------------------- |
| 1945 | // name: genX_coeffs() |
| 1946 | // desc: ... |
| 1947 | //----------------------------------------------------------------------------- |
| 1948 | CK_DLL_CTRL( genX_coeffs ) |
| 1949 | { |
| 1950 | // nope |
| 1951 | CK_FPRINTF_STDERR( "[chuck](via GenX): .coeffs called on abstract base class!\n" ); |
| 1952 | |
| 1953 | // return |
| 1954 | RETURN->v_object = GET_NEXT_OBJECT(ARGS); |
| 1955 | |
| 1956 | return; |
| 1957 | } |