| 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_xxx.cpp |
| 28 | // desc: non-specific unit generators |
| 29 | // |
| 30 | // author: Ge Wang ([email protected]) |
| 31 | // Ananya Misra ([email protected]) |
| 32 | // Perry R. Cook ([email protected]) |
| 33 | // Dan Trueman ([email protected]) |
| 34 | // Matt Hoffman (for Dyno) |
| 35 | // date: Spring 2004 |
| 36 | // Summer 2005 - updated |
| 37 | //----------------------------------------------------------------------------- |
| 38 | #include "ugen_xxx.h" |
| 39 | #include "chuck_type.h" |
| 40 | #include "chuck_ugen.h" |
| 41 | #include "chuck_vm.h" |
| 42 | #include "chuck_compile.h" |
| 43 | #include "chuck_instr.h" |
| 44 | #include "util_math.h" |
| 45 | #include "util_raw.h" |
| 46 | #include "util_string.h" |
| 47 | |
| 48 | #include <math.h> |
| 49 | #include <stdio.h> |
| 50 | #include <stdlib.h> |
| 51 | #include <string.h> |
| 52 | #include <sys/stat.h> |
| 53 | #include <limits.h> |
| 54 | |
| 55 | #ifndef __DISABLE_SNDBUF__ |
| 56 | #if defined(__CK_SNDFILE_NATIVE__) |
| 57 | #include <sndfile.h> |
| 58 | #else |
| 59 | #include "util_sndfile.h" |
| 60 | #endif |
| 61 | #endif |
| 62 | |
| 63 | #ifdef __ANDROID__ |
| 64 | #include "util_platforms.h" |
| 65 | #endif |
| 66 | |
| 67 | #include <fstream> |
| 68 | using namespace std; |
| 69 | |
| 70 | |
| 71 | // subgraph |
| 72 | CK_DLL_CTOR( subgraph_ctor ); |
| 73 | |
| 74 | // foogen |
| 75 | CK_DLL_CTOR( foogen_ctor ); |
| 76 | CK_DLL_DTOR( foogen_dtor ); |
| 77 | CK_DLL_TICK( foogen_tick ); |
| 78 | |
| 79 | |
| 80 | // LiSa query |
| 81 | DLL_QUERY lisa_query( Chuck_DL_Query * query ); |
| 82 | |
| 83 | // sample rate |
| 84 | static t_CKUINT g_srateXxx; |
| 85 | |
| 86 | // offset |
| 87 | static t_CKUINT subgraph_offset_inlet = 0; |
| 88 | static t_CKUINT subgraph_offset_outlet = 0; |
| 89 | static t_CKUINT foogen_offset_data = 0; |
| 90 | static t_CKUINT stereo_offset_left = 0; |
| 91 | static t_CKUINT stereo_offset_right = 0; |
| 92 | static t_CKUINT stereo_offset_pan = 0; |
| 93 | static t_CKUINT stereo_offset_panType = 0; |
| 94 | static t_CKUINT cnoise_offset_data = 0; |
| 95 | static t_CKUINT impulse_offset_data = 0; |
| 96 | static t_CKUINT step_offset_data = 0; |
| 97 | static t_CKUINT delayp_offset_data = 0; |
| 98 | static t_CKUINT sndbuf_offset_data = 0; |
| 99 | static t_CKUINT dyno_offset_data = 0; |
| 100 | // static t_CKUINT zerox_offset_data = 0; |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | // panning types enumeration |
| 106 | enum PanTypesEnum |
| 107 | { |
| 108 | PAN_WTF_UNITY = 0, // default in dac/adc |
| 109 | PAN_EQ_POWER, // default in Pan2 |
| 110 | PAN_LINEAR // not supported |
| 111 | }; |
| 112 | |
| 113 | //----------------------------------------------------------------------------- |
| 114 | // this is called for this module to know when sample rate changes | 1.5.4.2 (ge) added |
| 115 | //----------------------------------------------------------------------------- |
| 116 | void xxx_srate_update_cb( t_CKUINT srate, void * userdata ) { g_srateXxx = srate; } |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | //----------------------------------------------------------------------------- |
| 122 | // name: xxx_query() |
| 123 | // desc: ... |
| 124 | //----------------------------------------------------------------------------- |
| 125 | DLL_QUERY xxx_query( Chuck_DL_Query * QUERY ) |
| 126 | { |
| 127 | // set sample rate for this module |
| 128 | g_srateXxx = QUERY->srate(); |
| 129 | // register callback to be notified if/when sample rate changes | 1.5.4.2 (ge) added |
| 130 | QUERY->register_callback_on_srate_update( QUERY, xxx_srate_update_cb, NULL ); |
| 131 | |
| 132 | // get the env |
| 133 | Chuck_Env * env = QUERY->env(); |
| 134 | |
| 135 | Chuck_DL_Func * func = NULL; |
| 136 | std::string doc; |
| 137 | |
| 138 | // deprecate |
| 139 | type_engine_register_deprecate( env, "dac", "DAC" ); |
| 140 | type_engine_register_deprecate( env, "adc", "ADC" ); |
| 141 | type_engine_register_deprecate( env, "pan2", "Pan2" ); |
| 142 | type_engine_register_deprecate( env, "mix2", "Mix2" ); |
| 143 | type_engine_register_deprecate( env, "gain", "Gain" ); |
| 144 | type_engine_register_deprecate( env, "noise", "Noise" ); |
| 145 | type_engine_register_deprecate( env, "cnoise", "CNoise" ); |
| 146 | type_engine_register_deprecate( env, "impulse", "Impulse" ); |
| 147 | type_engine_register_deprecate( env, "step", "Step" ); |
| 148 | type_engine_register_deprecate( env, "halfrect", "HalfRect" ); |
| 149 | type_engine_register_deprecate( env, "fullrect", "FullRect" ); |
| 150 | type_engine_register_deprecate( env, "zerox", "ZeroX" ); |
| 151 | type_engine_register_deprecate( env, "delayp", "DelayP" ); |
| 152 | type_engine_register_deprecate( env, "sndbuf", "SndBuf" ); |
| 153 | // 1.5.0.0 (ge) removed deprecate; allow both |
| 154 | type_engine_register_deprecate( env, "Chubgraph", "Chugraph" ); |
| 155 | |
| 156 | //! \section audio output |
| 157 | |
| 158 | //------------------------------------------------------------------------- |
| 159 | // init as base class: Subgraph |
| 160 | //------------------------------------------------------------------------- |
| 161 | doc = "base class for subgraph-based user-created in-language unit generators."; |
| 162 | if( !type_engine_import_ugen_begin( env, "Chugraph", "UGen", env->global(), |
| 163 | subgraph_ctor, NULL, NULL, NULL, 1, 1, doc.c_str() ) ) |
| 164 | return FALSE; |
| 165 | |
| 166 | // add examples |
| 167 | if( !type_engine_import_add_ex( env, "extend/chugraph.ck" ) ) goto error; |
| 168 | if( !type_engine_import_add_ex( env, "class/dinky.ck" ) ) goto error; |
| 169 | if( !type_engine_import_add_ex( env, "class/try.ck" ) ) goto error; |
| 170 | if( !type_engine_import_add_ex( env, "deep/ks-chord.ck" ) ) goto error; |
| 171 | |
| 172 | doc = "terminal for sources chucked into this ugen."; |
| 173 | subgraph_offset_inlet = type_engine_import_mvar( env, "UGen", "inlet", TRUE, doc.c_str() ); |
| 174 | if( subgraph_offset_inlet == CK_INVALID_OFFSET ) goto error; |
| 175 | |
| 176 | doc = "terminal for the output of this ugen."; |
| 177 | subgraph_offset_outlet = type_engine_import_mvar( env, "UGen", "outlet", TRUE, doc.c_str() ); |
| 178 | if( subgraph_offset_outlet == CK_INVALID_OFFSET ) goto error; |
| 179 | |
| 180 | // end import |
| 181 | if( !type_engine_import_class_end( env ) ) |
| 182 | return FALSE; |
| 183 | |
| 184 | |
| 185 | //------------------------------------------------------------------------- |
| 186 | // init as base class: FooGen |
| 187 | //------------------------------------------------------------------------- |
| 188 | doc = "base class for user-created in-language unit generators."; |
| 189 | if( !type_engine_import_ugen_begin( env, "Chugen", "UGen", env->global(), |
| 190 | foogen_ctor, foogen_dtor, foogen_tick, NULL, 1, 1, |
| 191 | doc.c_str() ) ) |
| 192 | return FALSE; |
| 193 | |
| 194 | if( !type_engine_import_add_ex( env, "extend/chugen.ck" ) ) goto error; |
| 195 | |
| 196 | foogen_offset_data = type_engine_import_mvar( env, "int", "@foogen_data", FALSE ); |
| 197 | if( foogen_offset_data == CK_INVALID_OFFSET ) goto error; |
| 198 | |
| 199 | // end import |
| 200 | if( !type_engine_import_class_end( env ) ) |
| 201 | return FALSE; |
| 202 | |
| 203 | |
| 204 | //------------------------------------------------------------------------- |
| 205 | // init as base class: UGen_Multi |
| 206 | //------------------------------------------------------------------------- |
| 207 | doc = "base class for multi-channel unit generators."; |
| 208 | if( !type_engine_import_ugen_begin( env, "UGen_Multi", "UGen", env->global(), |
| 209 | multi_ctor, (f_dtor)NULL, (f_tick)NULL, (f_pmsg)NULL, 0, 0, doc.c_str() ) ) |
| 210 | return FALSE; |
| 211 | |
| 212 | // add chan |
| 213 | func = make_new_mfun( "UGen", "chan", multi_cget_chan ); |
| 214 | func->add_arg( "int", "which" ); |
| 215 | func->doc = "get UGen representing a specific channel of this UGen, or null if no such channel is available."; |
| 216 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 217 | |
| 218 | // add pan |
| 219 | /* func = make_new_mfun( "float", "pan", multi_ctrl_pan ); |
| 220 | func->add_arg( "float", "val" ); |
| 221 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 222 | func = make_new_mfun( "float", "pan", multi_cget_pan ); |
| 223 | if( !type_engine_import_mfun( env, func ) ) goto error; */ |
| 224 | |
| 225 | // end import |
| 226 | if( !type_engine_import_class_end( env ) ) |
| 227 | return FALSE; |
| 228 | |
| 229 | |
| 230 | //--------------------------------------------------------------------- |
| 231 | // init as base class: UGen_Stereo |
| 232 | //--------------------------------------------------------------------- |
| 233 | doc = "base class for stereo unit generators."; |
| 234 | if( !type_engine_import_ugen_begin( env, "UGen_Stereo", "UGen_Multi", env->global(), |
| 235 | stereo_ctor, stereo_dtor, NULL, NULL, 2, 2, doc.c_str() ) ) |
| 236 | return FALSE; |
| 237 | |
| 238 | // add left |
| 239 | doc = "left channel (same as .chan(0))."; |
| 240 | stereo_offset_left = type_engine_import_mvar( env, "UGen", "left", FALSE, doc.c_str() ); |
| 241 | if( stereo_offset_left == CK_INVALID_OFFSET ) goto error; |
| 242 | |
| 243 | // add right |
| 244 | doc = "right channel (same as .chan(1))."; |
| 245 | stereo_offset_right = type_engine_import_mvar( env, "UGen", "right", FALSE, doc.c_str() ); |
| 246 | if( stereo_offset_right == CK_INVALID_OFFSET ) goto error; |
| 247 | |
| 248 | // add pan |
| 249 | stereo_offset_pan = type_engine_import_mvar( env, "float", "@pan", FALSE ); |
| 250 | if( stereo_offset_pan == CK_INVALID_OFFSET ) goto error; |
| 251 | |
| 252 | // add panning law (see PanTypesEnum) |
| 253 | stereo_offset_panType = type_engine_import_mvar( env, "int", "@panType", FALSE ); |
| 254 | if( stereo_offset_panType == CK_INVALID_OFFSET ) goto error; |
| 255 | |
| 256 | // add pan |
| 257 | func = make_new_mfun( "float", "pan", stereo_ctrl_pan ); |
| 258 | func->add_arg( "float", "val" ); |
| 259 | func->doc = "pan between left and right channels, in range [-1,1], with -1 being far-left, 1 far-right, and 0 centered."; |
| 260 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 261 | func = make_new_mfun( "float", "pan", stereo_cget_pan ); |
| 262 | func->doc = "pan between left and right channels, in range [-1,1], with -1 being far-left, 1 far-right, and 0 centered."; |
| 263 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 264 | |
| 265 | // add panType |
| 266 | func = make_new_mfun( "int", "panType", stereo_ctrl_panType ); |
| 267 | func->add_arg( "int", "val" ); |
| 268 | func->doc = "set the panning type: (1) constant power panning, (0) unity gain anti-panning."; |
| 269 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 270 | func = make_new_mfun( "int", "panType", stereo_cget_panType ); |
| 271 | func->doc = "get the panning type: (1) constant power panning, (0) unity gain anti-panning."; |
| 272 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 273 | |
| 274 | // end import |
| 275 | if( !type_engine_import_class_end( env ) ) |
| 276 | return FALSE; |
| 277 | |
| 278 | // add dac |
| 279 | //! digital/analog converter |
| 280 | //! abstraction for underlying audio output device |
| 281 | //--------------------------------------------------------------------- |
| 282 | // init as base class: dac |
| 283 | //--------------------------------------------------------------------- |
| 284 | env->ckt_dac = type_engine_import_ugen_begin( env, "DAC", "UGen_Stereo", env->global(), |
| 285 | NULL, NULL, NULL, NULL, 2, 2 ); |
| 286 | if( !env->ckt_dac ) |
| 287 | return FALSE; |
| 288 | |
| 289 | // end import |
| 290 | if( !type_engine_import_class_end( env ) ) |
| 291 | return FALSE; |
| 292 | |
| 293 | // add adc |
| 294 | //! analog/digital converter |
| 295 | //! abstraction for underlying audio input device |
| 296 | //--------------------------------------------------------------------- |
| 297 | // init as base class: adc |
| 298 | //--------------------------------------------------------------------- |
| 299 | env->ckt_adc = type_engine_import_ugen_begin( env, "ADC", "UGen_Stereo", env->global(), |
| 300 | (f_ctor)NULL, (f_dtor)NULL, (f_tick)NULL, |
| 301 | (f_pmsg)NULL, 0, 2 ); |
| 302 | if( !env->ckt_adc ) |
| 303 | return FALSE; |
| 304 | |
| 305 | // end import |
| 306 | if( !type_engine_import_class_end( env ) ) |
| 307 | return FALSE; |
| 308 | |
| 309 | //--------------------------------------------------------------------- |
| 310 | // init as base class: pan2 |
| 311 | //--------------------------------------------------------------------- |
| 312 | doc = "a mono-to-stereo unit generator for stereo panning."; |
| 313 | if( !type_engine_import_ugen_begin( env, "Pan2", "UGen_Stereo", env->global(), |
| 314 | pan2_ctor, NULL, NULL, NULL, 2, 2, doc.c_str() ) ) |
| 315 | return FALSE; |
| 316 | |
| 317 | // add examples | 1.5.0.0 (ge) |
| 318 | if( !type_engine_import_add_ex( env, "basic/chirp2.ck" ) ) goto error; |
| 319 | if( !type_engine_import_add_ex( env, "stereo/array.ck" ) ) goto error; |
| 320 | if( !type_engine_import_add_ex( env, "stereo/powerup2.ck" ) ) goto error; |
| 321 | if( !type_engine_import_add_ex( env, "stereo/moe2.ck" ) ) goto error; |
| 322 | if( !type_engine_import_add_ex( env, "stereo/larry2.ck" ) ) goto error; |
| 323 | if( !type_engine_import_add_ex( env, "stereo/curly2.ck" ) ) goto error; |
| 324 | if( !type_engine_import_add_ex( env, "stereo/ugen-array.ck" ) ) goto error; |
| 325 | if( !type_engine_import_add_ex( env, "stereo/stereo-noise.ck" ) ) goto error; |
| 326 | if( !type_engine_import_add_ex( env, "stk/honkeytonk-algo1.ck" ) ) goto error; |
| 327 | if( !type_engine_import_add_ex( env, "hanoi++.ck" ) ) goto error; |
| 328 | if( !type_engine_import_add_ex( env, "hanoi2.ck" ) ) goto error; |
| 329 | if( !type_engine_import_add_ex( env, "hanoi3.ck" ) ) goto error; |
| 330 | |
| 331 | // end import |
| 332 | if( !type_engine_import_class_end( env ) ) |
| 333 | return FALSE; |
| 334 | |
| 335 | //--------------------------------------------------------------------- |
| 336 | // init as base class: mix2 |
| 337 | //--------------------------------------------------------------------- |
| 338 | doc = "a stereo-to-mono unit generator for mixing stereo signal to mono."; |
| 339 | if( !type_engine_import_ugen_begin( env, "Mix2", "UGen_Stereo", env->global(), |
| 340 | NULL, NULL, NULL, NULL, 2, 2, doc.c_str() ) ) |
| 341 | return FALSE; |
| 342 | |
| 343 | // end import |
| 344 | if( !type_engine_import_class_end( env ) ) |
| 345 | return FALSE; |
| 346 | |
| 347 | // add blackhole |
| 348 | //! sample rate sample sucker |
| 349 | //! ( like dac, ticks ugens, but no more ) |
| 350 | //! see \example pwm.ck |
| 351 | //QUERY->ugen_add( QUERY, "blackhole", NULL ); |
| 352 | // set funcs |
| 353 | //QUERY->ugen_func( QUERY, NULL, NULL, bunghole_tick, NULL ); |
| 354 | |
| 355 | // add bunghole |
| 356 | //! sample rate sample sucker |
| 357 | //! ( like dac, ticks ugens, but no more ) |
| 358 | //QUERY->ugen_add( QUERY, "bunghole", NULL ); |
| 359 | // set funcs |
| 360 | //QUERY->ugen_func( QUERY, NULL, NULL, bunghole_tick, NULL ); |
| 361 | |
| 362 | // add gain |
| 363 | //! gain control |
| 364 | //! (NOTE - all unit generators can themselves change their gain) |
| 365 | //! (this is a way to add N outputs together and scale them) |
| 366 | //! used in \example i-robot.ck |
| 367 | //--------------------------------------------------------------------- |
| 368 | // init as base class: gain |
| 369 | //--------------------------------------------------------------------- |
| 370 | doc = "a gain control unit generator. (All unit generators can already change their own gain; this is an explicit way to add multiple outputs together and scale them.)"; |
| 371 | if( !type_engine_import_ugen_begin( env, "Gain", "UGen", env->global(), |
| 372 | NULL, NULL, NULL, NULL, doc.c_str() ) ) |
| 373 | return FALSE; |
| 374 | |
| 375 | // examples |
| 376 | if( !type_engine_import_add_ex( env, "basic/gain.ck" ) ) goto error; |
| 377 | if( !type_engine_import_add_ex( env, "basic/i-robot.ck" ) ) goto error; |
| 378 | |
| 379 | // add ctrl: fprob |
| 380 | func = make_new_mfun( "void", "Gain", gain_ctor_1 ); |
| 381 | func->doc = "construct a Gain with default value."; |
| 382 | func->add_arg( "float", "gain" ); |
| 383 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 384 | |
| 385 | // set gain in dB rather than linearly |
| 386 | func = make_new_mfun( "float", "db", gain_set_db ); |
| 387 | func->add_arg( "float", "db" ); |
| 388 | func->doc = "set gain (in dB)"; |
| 389 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 390 | |
| 391 | func = make_new_mfun( "float", "db", gain_get_db ); |
| 392 | func->doc = "get gain (in dB)"; |
| 393 | |
| 394 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 395 | |
| 396 | // end import |
| 397 | if( !type_engine_import_class_end( env ) ) |
| 398 | return FALSE; |
| 399 | /*! \example |
| 400 | noise n => gain g => dac; |
| 401 | sinosc s => g; |
| 402 | .3 => g.gain; |
| 403 | while( true ) { 100::ms => now; } |
| 404 | */ |
| 405 | |
| 406 | //! GainDB |
| 407 | doc = "the same as the Gain UGen, but the constructor sets the gain in deciBels rather than linear scaling."; |
| 408 | |
| 409 | if( !type_engine_import_ugen_begin( env, "GainDB", "Gain", env->global(), |
| 410 | NULL, NULL, NULL, NULL, doc.c_str() ) ) |
| 411 | return FALSE; |
| 412 | |
| 413 | // example |
| 414 | if( !type_engine_import_add_ex( env, "basic/gain.ck" ) ) goto error; |
| 415 | |
| 416 | func = make_new_ctor( gainDB_ctor ); |
| 417 | func->doc = "construct a GainDB with default value (in deciBels)."; |
| 418 | func->add_arg( "float", "db" ); |
| 419 | if( !type_engine_import_ctor( env, func ) ) goto error; |
| 420 | |
| 421 | // end import |
| 422 | if( !type_engine_import_class_end( env ) ) |
| 423 | return FALSE; |
| 424 | |
| 425 | //! \section wave forms |
| 426 | |
| 427 | // add noise |
| 428 | //! white noise generator |
| 429 | //! see \example noise.ck \example powerup.ck |
| 430 | //--------------------------------------------------------------------- |
| 431 | // init as base class: noise |
| 432 | //--------------------------------------------------------------------- |
| 433 | doc = "a white noise generator."; |
| 434 | if( !type_engine_import_ugen_begin( env, "Noise", "UGen", env->global(), |
| 435 | NULL, NULL, noise_tick, NULL, doc.c_str() ) ) |
| 436 | return FALSE; |
| 437 | |
| 438 | if( !type_engine_import_add_ex( env, "basic/wind.ck" ) ) goto error; |
| 439 | if( !type_engine_import_add_ex( env, "deep/smb.ck" ) ) goto error; |
| 440 | if( !type_engine_import_add_ex( env, "shred/powerup.ck" ) ) goto error; |
| 441 | |
| 442 | // end import |
| 443 | if( !type_engine_import_class_end( env ) ) |
| 444 | return FALSE; |
| 445 | //zzz |
| 446 | |
| 447 | // add cnoise |
| 448 | /*QUERY->ugen_add( QUERY, "cnoise", NULL ); |
| 449 | QUERY->ugen_func( QUERY, cnoise_ctor, cnoise_dtor, cnoise_tick, NULL ); |
| 450 | QUERY->ugen_ctrl( QUERY, cnoise_ctrl_mode, NULL, "string", "mode" ); |
| 451 | QUERY->ugen_ctrl( QUERY, cnoise_ctrl_fprob, NULL, "float", "fprob" );*/ |
| 452 | |
| 453 | //--------------------------------------------------------------------- |
| 454 | // init as base class: cnoise |
| 455 | //--------------------------------------------------------------------- |
| 456 | doc = "a noise generator with multiple noise synthesis modes."; |
| 457 | if( !type_engine_import_ugen_begin( env, "CNoise", "UGen", env->global(), |
| 458 | cnoise_ctor, cnoise_dtor, cnoise_tick, NULL, doc.c_str() ) ) |
| 459 | return FALSE; |
| 460 | |
| 461 | // add member variable |
| 462 | cnoise_offset_data = type_engine_import_mvar( env, "int", "@cnoise_data", FALSE ); |
| 463 | if( cnoise_offset_data == CK_INVALID_OFFSET ) goto error; |
| 464 | |
| 465 | // add ctor( string mode ) | 1.5.2.5 (ge) added |
| 466 | func = make_new_ctor( cnoise_ctor_mode ); |
| 467 | func->add_arg( "string", "mode" ); |
| 468 | func->doc = "Construct a CNoise with synthesis mode. Supported modes are \"white\", \"pink\", \"flip\", and \"xor\"."; |
| 469 | if( !type_engine_import_ctor( env, func ) ) goto error; |
| 470 | |
| 471 | // add ctrl: mode |
| 472 | func = make_new_mfun( "string", "mode", cnoise_ctrl_mode ); |
| 473 | func->add_arg( "string", "mode" ); |
| 474 | func->doc = "Noise synthesis mode. Supported modes are \"white\", \"pink\", \"flip\", and \"xor\"."; |
| 475 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 476 | // add cget: mode |
| 477 | //func = make_new_mfun( "string", "mode", cnoise_cget_mode ); |
| 478 | //if( !type_engine_import_mfun( env, func ) ) goto error; |
| 479 | |
| 480 | // add ctrl: fprob |
| 481 | func = make_new_mfun( "float", "fprob", cnoise_ctrl_fprob ); |
| 482 | func->doc = "Probability [0-1] used for calculating XOR noise."; |
| 483 | func->add_arg( "float", "fprob" ); |
| 484 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 485 | // add cget: fprob |
| 486 | //func = make_new_mfun( "float", "fprob", cnoise_cget_fprob ); |
| 487 | //if( !type_engine_import_mfun( env, func ) ) goto error; |
| 488 | |
| 489 | // end import |
| 490 | if( !type_engine_import_class_end( env ) ) |
| 491 | return FALSE; |
| 492 | |
| 493 | |
| 494 | // add impulse |
| 495 | //! pulse generator - can set the value of the current sample |
| 496 | //! default for each sample is 0 if not set |
| 497 | //QUERY->ugen_add( QUERY, "impulse", NULL ); |
| 498 | // set funcs |
| 499 | //QUERY->ugen_func( QUERY, impulse_ctor, impulse_dtor, impulse_tick, NULL ); |
| 500 | // ctrl func |
| 501 | //QUERY->ugen_ctrl( QUERY, impulse_ctrl_value, impulse_cget_value, "float", "value" ); |
| 502 | //QUERY->ugen_ctrl( QUERY, impulse_ctrl_value, impulse_cget_value, "float", "next" ); //! set value of next sample |
| 503 | /*! \example |
| 504 | impulse i => dac; |
| 505 | |
| 506 | while( true ) { |
| 507 | 1.0 => i.next; |
| 508 | 100::ms => now; |
| 509 | } |
| 510 | */ |
| 511 | //--------------------------------------------------------------------- |
| 512 | // init as base class: impulse |
| 513 | //--------------------------------------------------------------------- |
| 514 | doc = "An impulse generator. Can be used to set the value of the next sample; default for each sample is 0 if not set. Additionally, this can be used to generate a digital signal, one sample at a time."; |
| 515 | if( !type_engine_import_ugen_begin( env, "Impulse", "UGen", env->global(), |
| 516 | impulse_ctor, impulse_dtor, impulse_tick, NULL, doc.c_str() ) ) |
| 517 | return FALSE; |
| 518 | |
| 519 | // add ctrl: value |
| 520 | //func = make_new_mfun( "float", "value", impulse_ctrl_value ); |
| 521 | //func->add_arg( "float", "value" ); |
| 522 | //if( !type_engine_import_mfun( env, func ) ) goto error; |
| 523 | // add cget: value |
| 524 | //func = make_new_mfun( "float", "value", impulse_cget_value ); |
| 525 | //if( !type_engine_import_mfun( env, func ) ) goto error; |
| 526 | |
| 527 | // add member variable |
| 528 | impulse_offset_data = type_engine_import_mvar( env, "int", "@impulse_data", FALSE ); |
| 529 | if( impulse_offset_data == CK_INVALID_OFFSET ) goto error; |
| 530 | |
| 531 | // add ctrl: next |
| 532 | func = make_new_mfun( "float", "next", impulse_ctrl_next ); |
| 533 | func->add_arg( "float", "next" ); |
| 534 | func->doc = "set the value of next sample. (Note: if you are using the UGen.last() method to read the output of the impulse, the value set by Impulse.next() does not appear as the output until after the next sample boundary. In this case, there is a consistent 1::samp offset between setting .next(...) and reading that value using .last().)"; |
| 535 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 536 | // add cget: next |
| 537 | func = make_new_mfun( "float", "next", impulse_cget_next ); |
| 538 | func->doc = "get value of next sample to be generated."; |
| 539 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 540 | |
| 541 | // add examples |
| 542 | if( !type_engine_import_add_ex( env, "basic/imp.ck" ) ) goto error; |
| 543 | if( !type_engine_import_add_ex( env, "basic/comb.ck" ) ) goto error; |
| 544 | if( !type_engine_import_add_ex( env, "deep/chant.ck" ) ) goto error; |
| 545 | |
| 546 | // end import |
| 547 | if( !type_engine_import_class_end( env ) ) |
| 548 | return FALSE; |
| 549 | |
| 550 | // add step |
| 551 | //! step generator - like impulse, but once a value is set, |
| 552 | //! it is held for all following samples, until value is set again |
| 553 | //! see \example step.ck |
| 554 | //QUERY->ugen_add( QUERY, "step", NULL ); |
| 555 | // set funcs |
| 556 | //QUERY->ugen_func( QUERY, step_ctor, step_dtor, step_tick, NULL ); |
| 557 | // ctrl func |
| 558 | //QUERY->ugen_ctrl( QUERY, step_ctrl_value, step_cget_value, "float", "value" ); |
| 559 | //QUERY->ugen_ctrl( QUERY, step_ctrl_value, step_cget_value, "float", "next" ); //! set the step value |
| 560 | /*! \example |
| 561 | step s => dac; |
| 562 | -1.0 => float amp; |
| 563 | |
| 564 | // square wave using step |
| 565 | while( true ) { |
| 566 | -amp => amp => s.next; |
| 567 | 800::samp => now; |
| 568 | } |
| 569 | */ |
| 570 | //--------------------------------------------------------------------- |
| 571 | // init as base class: step |
| 572 | //--------------------------------------------------------------------- |
| 573 | doc = "a step generator. Step behaves like Impulse, except that once a next value is set, that value is held for all following samples, until a different value is set."; |
| 574 | if( !type_engine_import_ugen_begin( env, "Step", "UGen", env->global(), |
| 575 | step_ctor, step_dtor, step_tick, NULL, doc.c_str() ) ) |
| 576 | return FALSE; |
| 577 | |
| 578 | // add examples | 1.5.0.0 (ge) |
| 579 | if( !type_engine_import_add_ex( env, "basic/step.ck" ) ) goto error; |
| 580 | if( !type_engine_import_add_ex( env, "basic/fm3.ck" ) ) goto error; |
| 581 | if( !type_engine_import_add_ex( env, "basic/zerox.ck" ) ) goto error; |
| 582 | |
| 583 | // add ctrl: value |
| 584 | //func = make_new_mfun( "float", "value", step_ctrl_value ); |
| 585 | //func->add_arg( "float", "value" ); |
| 586 | //if( !type_engine_import_mfun( env, func ) ) goto error; |
| 587 | // add cget: value |
| 588 | //func = make_new_mfun( "float", "value", step_cget_value ); |
| 589 | //if( !type_engine_import_mfun( env, func ) ) goto error; |
| 590 | |
| 591 | // add member variable |
| 592 | step_offset_data = type_engine_import_mvar( env, "int", "@step_data", FALSE ); |
| 593 | if( step_offset_data == CK_INVALID_OFFSET ) goto error; |
| 594 | |
| 595 | // add ctor( float value ) |
| 596 | func = make_new_ctor( step_ctor_value ); |
| 597 | func->add_arg( "float", "value" ); |
| 598 | func->doc = "construct a Step with default value."; |
| 599 | if( !type_engine_import_ctor( env, func ) ) goto error; |
| 600 | |
| 601 | // add ctrl: next |
| 602 | func = make_new_mfun( "float", "next", step_ctrl_next ); |
| 603 | func->add_arg( "float", "next" ); |
| 604 | func->doc = "set the next step value."; |
| 605 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 606 | // add cget: next |
| 607 | func = make_new_mfun( "float", "next", step_cget_next ); |
| 608 | func->doc = "get the next step value."; |
| 609 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 610 | |
| 611 | // end import |
| 612 | if( !type_engine_import_class_end( env ) ) |
| 613 | return FALSE; |
| 614 | |
| 615 | // add halfrect |
| 616 | //! half wave rectifier |
| 617 | //! for half-wave rectification. |
| 618 | //QUERY->ugen_add( QUERY, "halfrect", NULL ); |
| 619 | // set funcs |
| 620 | //QUERY->ugen_func( QUERY, NULL, NULL, halfrect_tick, NULL ); |
| 621 | //--------------------------------------------------------------------- |
| 622 | // init as base class: halfrect |
| 623 | //--------------------------------------------------------------------- |
| 624 | doc = "a half wave rectifier."; |
| 625 | if( !type_engine_import_ugen_begin( env, "HalfRect", "UGen", env->global(), |
| 626 | NULL, NULL, halfrect_tick, NULL, doc.c_str() ) ) |
| 627 | return FALSE; |
| 628 | |
| 629 | // end import |
| 630 | if( !type_engine_import_class_end( env ) ) |
| 631 | return FALSE; |
| 632 | |
| 633 | |
| 634 | // add fullrect |
| 635 | //! full wave rectifier |
| 636 | //QUERY->ugen_add( QUERY, "fullrect", NULL ); |
| 637 | // set funcs |
| 638 | //QUERY->ugen_func( QUERY, NULL, NULL, fullrect_tick, NULL ); |
| 639 | //--------------------------------------------------------------------- |
| 640 | // init as base class: fullrect |
| 641 | //--------------------------------------------------------------------- |
| 642 | doc = "a full wave rectifier."; |
| 643 | if( !type_engine_import_ugen_begin( env, "FullRect", "UGen", env->global(), |
| 644 | NULL, NULL, fullrect_tick, NULL, doc.c_str() ) ) |
| 645 | return FALSE; |
| 646 | |
| 647 | // end import |
| 648 | if( !type_engine_import_class_end( env ) ) |
| 649 | return FALSE; |
| 650 | |
| 651 | |
| 652 | // add zerox |
| 653 | //! zero crossing detector |
| 654 | //! emits a single pulse at the the zero crossing in the direction of the zero crossing. |
| 655 | //! (see \example zerox.ck) |
| 656 | //QUERY->ugen_add( QUERY, "zerox", NULL ); |
| 657 | // set funcs |
| 658 | //QUERY->ugen_func( QUERY, zerox_ctor, zerox_dtor, zerox_tick, NULL ); |
| 659 | //--------------------------------------------------------------------- |
| 660 | // init as base class: zerox |
| 661 | //--------------------------------------------------------------------- |
| 662 | /* if( !type_engine_import_ugen_begin( env, "ZeroX", "UGen", env->global(), |
| 663 | zerox_ctor, zerox_dtor, zerox_tick, NULL ) ) |
| 664 | return FALSE; |
| 665 | |
| 666 | // add member variable |
| 667 | zerox_offset_data = type_engine_import_mvar( env, "int", "@zerox_data", FALSE ); |
| 668 | if( zerox_offset_data == CK_INVALID_OFFSET ) goto error; |
| 669 | |
| 670 | // end import |
| 671 | if( !type_engine_import_class_end( env ) ) |
| 672 | return FALSE; */ |
| 673 | |
| 674 | |
| 675 | //! \section delay lines |
| 676 | |
| 677 | //! delay with varying write position ( instead of read position ) |
| 678 | //! change to delay length will not affect the delay of samples already in |
| 679 | //! the buffer. |
| 680 | //! see \example delayp.ck |
| 681 | |
| 682 | //QUERY->ugen_add( QUERY, "delayp" , NULL); |
| 683 | //QUERY->ugen_func ( QUERY, delayp_ctor, delayp_dtor, delayp_tick, delayp_pmsg); |
| 684 | //QUERY->ugen_ctrl( QUERY, delayp_ctrl_delay, delayp_cget_delay , "dur", "delay" ); //! delay before subsequent values emerge |
| 685 | //QUERY->ugen_ctrl( QUERY, delayp_ctrl_window, delayp_cget_window , "dur", "window" ); //! time for 'write head' to move |
| 686 | //QUERY->ugen_ctrl( QUERY, delayp_ctrl_max, delayp_cget_max , "dur", "max" ); //! max delay possible. trashes buffer, so do it first! |
| 687 | |
| 688 | //--------------------------------------------------------------------- |
| 689 | // init as base class: delayp |
| 690 | //--------------------------------------------------------------------- |
| 691 | if( !type_engine_import_ugen_begin( env, "DelayP", "UGen", env->global(), |
| 692 | delayp_ctor, delayp_dtor, |
| 693 | delayp_tick, delayp_pmsg ) ) |
| 694 | return FALSE; |
| 695 | |
| 696 | // add member variable |
| 697 | delayp_offset_data = type_engine_import_mvar( env, "int", "@delayp_data", FALSE ); |
| 698 | if( delayp_offset_data == CK_INVALID_OFFSET ) goto error; |
| 699 | |
| 700 | // add ctrl: delay |
| 701 | func = make_new_mfun( "dur", "delay", delayp_ctrl_delay ); |
| 702 | func->add_arg( "dur", "delay" ); |
| 703 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 704 | // add cget: delay |
| 705 | func = make_new_mfun( "dur", "delay", delayp_cget_delay ); |
| 706 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 707 | |
| 708 | // add ctrl: window |
| 709 | func = make_new_mfun( "dur", "window", delayp_ctrl_window ); |
| 710 | func->add_arg( "dur", "window" ); |
| 711 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 712 | // add cget: window |
| 713 | func = make_new_mfun( "dur", "window", delayp_cget_window ); |
| 714 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 715 | |
| 716 | // add ctrl: max |
| 717 | func = make_new_mfun( "dur", "max", delayp_ctrl_max ); |
| 718 | func->add_arg( "dur", "max" ); |
| 719 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 720 | // add cget: max |
| 721 | func = make_new_mfun( "dur", "max", delayp_cget_max ); |
| 722 | |
| 723 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 724 | |
| 725 | // end import |
| 726 | if( !type_engine_import_class_end( env ) ) |
| 727 | return FALSE; |
| 728 | |
| 729 | //! \section sound files |
| 730 | |
| 731 | #ifndef __DISABLE_SNDBUF__ |
| 732 | |
| 733 | // add sndbuf |
| 734 | //! sound buffer ( now interpolating ) |
| 735 | //! reads from a variety of file formats |
| 736 | //! see \example sndbuf.ck |
| 737 | //QUERY->ugen_add( QUERY, "sndbuf", NULL ); |
| 738 | // set funcs |
| 739 | //QUERY->ugen_func( QUERY, sndbuf_ctor, sndbuf_dtor, sndbuf_tick, NULL ); |
| 740 | // set ctrl |
| 741 | //QUERY->ugen_ctrl( QUERY, sndbuf_ctrl_read, NULL, "string", "read" ); //! loads file for reading |
| 742 | //QUERY->ugen_ctrl( QUERY, sndbuf_ctrl_write, NULL, "string", "write" ); //! loads a file for writing ( or not ) |
| 743 | //QUERY->ugen_ctrl( QUERY, sndbuf_ctrl_pos, sndbuf_cget_pos, "int", "pos" ); //! set position ( 0 < p < .samples ) |
| 744 | //QUERY->ugen_ctrl( QUERY, sndbuf_ctrl_loop, sndbuf_cget_loop, "int", "loop" ); //! toggle looping |
| 745 | //QUERY->ugen_ctrl( QUERY, sndbuf_ctrl_interp, sndbuf_cget_interp, "int", "interp" ); //! set interpolation ( 0=drop, 1=linear, 2=sinc ) |
| 746 | //QUERY->ugen_ctrl( QUERY, sndbuf_ctrl_rate, sndbuf_cget_rate, "float", "rate" ); //! playback rate ( relative to file's natural speed ) |
| 747 | //QUERY->ugen_ctrl( QUERY, sndbuf_ctrl_rate, sndbuf_cget_rate, "float", "play" ); //! play (same as rate) |
| 748 | //QUERY->ugen_ctrl( QUERY, sndbuf_ctrl_freq, sndbuf_cget_freq, "float", "freq" ); //! playback rate ( file loops / second ) |
| 749 | //QUERY->ugen_ctrl( QUERY, sndbuf_ctrl_phase, sndbuf_cget_phase, "float", "phase" ); //! set phase position ( 0-1 ) |
| 750 | //QUERY->ugen_ctrl( QUERY, sndbuf_ctrl_channel, sndbuf_cget_channel, "int", "channel" ); //! select channel ( 0 < p < .channels ) |
| 751 | //QUERY->ugen_ctrl( QUERY, sndbuf_ctrl_phase_offset, sndbuf_cget_phase, "float", "phase_offset" ); //! set a phase offset |
| 752 | //QUERY->ugen_ctrl( QUERY, NULL, sndbuf_cget_samples, "int", "samples" ); //! fetch number of samples |
| 753 | //QUERY->ugen_ctrl( QUERY, NULL, sndbuf_cget_length, "dur", "length" ); //! fetch length |
| 754 | //QUERY->ugen_ctrl( QUERY, NULL, sndbuf_cget_channels, "int", "channels" ); //! fetch number of channels |
| 755 | |
| 756 | //--------------------------------------------------------------------- |
| 757 | // init as base class: sndbuf |
| 758 | //--------------------------------------------------------------------- |
| 759 | doc = "an interpolating sound buffer/player with single-channel output; can read audio data from a variety of uncompressed formats."; |
| 760 | if( !type_engine_import_ugen_begin( env, "SndBuf", "UGen", env->global(), |
| 761 | sndbuf_ctor, sndbuf_dtor, |
| 762 | sndbuf_tick, NULL, 1, 1, doc.c_str() ) ) |
| 763 | return FALSE; |
| 764 | |
| 765 | if( !type_engine_import_add_ex( env, "basic/sndbuf.ck" ) ) goto error; |
| 766 | if( !type_engine_import_add_ex( env, "basic/doh.ck" ) ) goto error; |
| 767 | if( !type_engine_import_add_ex( env, "basic/valueat.ck" ) ) goto error; |
| 768 | if( !type_engine_import_add_ex( env, "otf_01.ck" ) ) goto error; |
| 769 | if( !type_engine_import_add_ex( env, "otf_02.ck" ) ) goto error; |
| 770 | if( !type_engine_import_add_ex( env, "otf_03.ck" ) ) goto error; |
| 771 | if( !type_engine_import_add_ex( env, "otf_04.ck" ) ) goto error; |
| 772 | |
| 773 | // add member variable |
| 774 | sndbuf_offset_data = type_engine_import_mvar( env, "int", "@sndbuf_data", FALSE ); |
| 775 | if( sndbuf_offset_data == CK_INVALID_OFFSET ) goto error; |
| 776 | |
| 777 | // add ctor( string path ) |
| 778 | func = make_new_ctor( sndbuf_ctor_path ); |
| 779 | func->add_arg( "string", "path" ); |
| 780 | func->doc = "construct a SndBuf with the 'path' to a sound file to read."; |
| 781 | if( !type_engine_import_ctor( env, func ) ) goto error; |
| 782 | |
| 783 | // add ctor( string path, float rate ) |
| 784 | func = make_new_ctor( sndbuf_ctor_path_rate ); |
| 785 | func->add_arg( "string", "path" ); |
| 786 | func->add_arg( "float", "rate" ); |
| 787 | func->doc = "construct a SndBuf with the 'path' to a sound file to read, and a default playback 'rate' (1.0 is normal rate)"; |
| 788 | if( !type_engine_import_ctor( env, func ) ) goto error; |
| 789 | |
| 790 | // add ctor( string path ) |
| 791 | func = make_new_ctor( sndbuf_ctor_path_rate_pos ); |
| 792 | func->add_arg( "string", "path" ); |
| 793 | func->add_arg( "float", "rate" ); |
| 794 | func->add_arg( "int", "pos" ); |
| 795 | func->doc = "construct a SndBuf with the 'path' to a sound file to read, a default playback 'rate' (1.0 is normal rate), and starting at sample position 'pos'"; |
| 796 | if( !type_engine_import_ctor( env, func ) ) goto error; |
| 797 | |
| 798 | // add ctrl: read |
| 799 | func = make_new_mfun( "string", "read", sndbuf_ctrl_read ); |
| 800 | func->add_arg( "string", "read" ); |
| 801 | func->doc = "read file for reading."; |
| 802 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 803 | |
| 804 | // add cget: ready |
| 805 | func = make_new_mfun( "int", "ready", sndbuf_cget_ready ); |
| 806 | func->doc = "query whether the SndBuf is ready for use (e.g., sound file successfully loaded)."; |
| 807 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 808 | |
| 809 | // add ctrl: write |
| 810 | func = make_new_mfun( "string", "write", sndbuf_ctrl_write ); |
| 811 | func->add_arg( "string", "read" ); |
| 812 | func->doc = "set file for writing (currently unsupported)."; |
| 813 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 814 | // add cget: write |
| 815 | //func = make_new_mfun( "string", "write", sndbuf_cget_write ); |
| 816 | //if( !type_engine_import_mfun( env, func ) ) goto error; |
| 817 | |
| 818 | // add ctrl: pos |
| 819 | func = make_new_mfun( "int", "pos", sndbuf_ctrl_pos ); |
| 820 | func->add_arg( "int", "pos" ); |
| 821 | func->doc = "set play position (between 0 and number of samples)."; |
| 822 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 823 | // add cget: pos |
| 824 | func = make_new_mfun( "int", "pos", sndbuf_cget_pos ); |
| 825 | func->doc = "get play position (between 0 and number of samples)."; |
| 826 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 827 | |
| 828 | // add ctrl: loop |
| 829 | func = make_new_mfun( "int", "loop", sndbuf_ctrl_loop ); |
| 830 | func->add_arg( "int", "loop" ); |
| 831 | func->doc = "toggle looping file playback."; |
| 832 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 833 | // add cget: loop |
| 834 | func = make_new_mfun( "int", "loop", sndbuf_cget_loop ); |
| 835 | func->doc = "get whether file playback is set to loop."; |
| 836 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 837 | |
| 838 | // add ctrl: interp |
| 839 | func = make_new_mfun( "int", "interp", sndbuf_ctrl_interp ); |
| 840 | func->add_arg( "int", "interp" ); |
| 841 | func->doc = "set interpolation mode: (0) drop sample; (1) linear interpolation; (2) sinc interpolation."; |
| 842 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 843 | // add cget: interp |
| 844 | func = make_new_mfun( "int", "interp", sndbuf_cget_interp ); |
| 845 | func->doc = "get interpolation mode: (0) drop sample; (1) linear interpolation; (2) sinc interpolation."; |
| 846 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 847 | |
| 848 | // add ctrl: rate |
| 849 | func = make_new_mfun( "float", "rate", sndbuf_ctrl_rate ); |
| 850 | func->add_arg( "float", "rate" ); |
| 851 | func->doc = "set playback rate (relative to file's natural speed). For example, 1.0 is 'normal', 0.5 is half speed, and 2 is twice as fast."; |
| 852 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 853 | // add cget: rate |
| 854 | func = make_new_mfun( "float", "rate", sndbuf_cget_rate ); |
| 855 | func->doc = "get playback rate (relative to file's natural speed). For example, 1.0 is 'normal', 0.5 is half speed, and 2 is twice as fast."; |
| 856 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 857 | |
| 858 | // add ctrl: play |
| 859 | func = make_new_mfun( "float", "play", sndbuf_ctrl_rate ); |
| 860 | func->add_arg( "float", "play" ); |
| 861 | func->doc = "(same as .rate)"; |
| 862 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 863 | // add cget: play // good |
| 864 | func = make_new_mfun( "float", "play", sndbuf_cget_rate ); |
| 865 | func->doc = "(same as .rate)"; |
| 866 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 867 | |
| 868 | // add ctrl: freq |
| 869 | func = make_new_mfun( "float", "freq", sndbuf_ctrl_freq ); |
| 870 | func->add_arg( "float", "freq" ); |
| 871 | func->doc = "set loop rate (in file loops per second)."; |
| 872 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 873 | // add cget: freq |
| 874 | func = make_new_mfun( "float", "freq", sndbuf_cget_freq ); |
| 875 | func->doc = "get loop rate (in file loops per second)."; |
| 876 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 877 | |
| 878 | // add ctrl: phase |
| 879 | func = make_new_mfun( "float", "phase", sndbuf_ctrl_phase ); |
| 880 | func->add_arg( "float", "phase" ); |
| 881 | func->doc = "set phase position, normalized to [0,1)."; |
| 882 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 883 | // add cget: phase |
| 884 | func = make_new_mfun( "float", "phase", sndbuf_cget_phase ); |
| 885 | func->doc = "get phase position, normalized to [0,1)."; |
| 886 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 887 | |
| 888 | // add ctrl: channel |
| 889 | func = make_new_mfun( "int", "channel", sndbuf_ctrl_channel ); |
| 890 | func->add_arg( "int", "channel" ); |
| 891 | func->doc = "for sound file containing more than one channel of audio, select the channel to play."; |
| 892 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 893 | // add cget: channel |
| 894 | func = make_new_mfun( "int", "channel", sndbuf_cget_channel ); |
| 895 | func->doc = "get the selected chanel of audio this buffer is playing."; |
| 896 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 897 | |
| 898 | // add ctrl: phase_offset |
| 899 | func = make_new_mfun( "float", "phaseOffset", sndbuf_ctrl_phase_offset ); |
| 900 | func->add_arg( "float", "value" ); |
| 901 | func->doc = "advance the playhead by the specified phase offset in [0,1), where 0 is no advance and 1 advance the entire length of the file."; |
| 902 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 903 | // add cget: phase_offset |
| 904 | // func = make_new_mfun( "float", "phaseOffset", sndbuf_cget_phase_offset ); |
| 905 | // if( !type_engine_import_mfun( env, func ) ) goto error; |
| 906 | |
| 907 | // add ctrl: chunks |
| 908 | func = make_new_mfun( "int", "chunks", sndbuf_ctrl_chunks ); |
| 909 | func->add_arg( "int", "frames" ); |
| 910 | func->doc = "set chunk size, in frames, for loading the file from disk. Set to 0 to disable chunking."; |
| 911 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 912 | // add cget: chunks |
| 913 | func = make_new_mfun( "int", "chunks", sndbuf_cget_chunks ); |
| 914 | func->doc = "get chunk size, in frames, for loading the file from disk. 0 indicates that chunking is disabled."; |
| 915 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 916 | |
| 917 | // add cget: samples |
| 918 | func = make_new_mfun( "int", "samples", sndbuf_cget_samples ); |
| 919 | func->doc = "get total number of sample frames in the file; same as .frames()."; |
| 920 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 921 | |
| 922 | func = make_new_mfun( "int", "sampleRate", sndbuf_cget_samplerate ); |
| 923 | func->doc = "get sample rate of the source audio."; |
| 924 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 925 | |
| 926 | // add cget: frames |
| 927 | func = make_new_mfun( "int", "frames", sndbuf_cget_samples ); |
| 928 | func->doc = "get total number of sample frames in the file; same as .samples()."; |
| 929 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 930 | |
| 931 | // add cget: length |
| 932 | func = make_new_mfun( "dur", "length", sndbuf_cget_length ); |
| 933 | func->doc = "get total length of the file as a duration."; |
| 934 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 935 | |
| 936 | // add cget: channels |
| 937 | func = make_new_mfun( "int", "channels", sndbuf_cget_channels ); |
| 938 | func->doc = "get number of channels available in the sound file."; |
| 939 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 940 | |
| 941 | // add cget: valueAt |
| 942 | func = make_new_mfun( "float", "valueAt", sndbuf_cget_valueAt ); |
| 943 | func->add_arg( "int", "pos" ); |
| 944 | func->doc = "get sample value at given position (in samples)."; |
| 945 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 946 | |
| 947 | // end import |
| 948 | if( !type_engine_import_class_end( env ) ) |
| 949 | return FALSE; |
| 950 | |
| 951 | |
| 952 | //--------------------------------------------------------------------- |
| 953 | // init as base class: SndBuf2 |
| 954 | //--------------------------------------------------------------------- |
| 955 | doc = "an interpolating sound buffer with two-channel output. Reads from a variety of uncompressed formats."; |
| 956 | if( !type_engine_import_ugen_begin( env, "SndBuf2", "SndBuf", env->global(), |
| 957 | NULL, NULL, |
| 958 | NULL, sndbuf_tickf, NULL, 2, 2, doc.c_str() ) ) |
| 959 | return FALSE; |
| 960 | |
| 961 | // add ctor( string path ) |
| 962 | func = make_new_ctor( sndbuf2_ctor_path ); |
| 963 | func->add_arg( "string", "path" ); |
| 964 | func->doc = "construct a SndBuf2 with the 'path' to a sound file to read."; |
| 965 | if( !type_engine_import_ctor( env, func ) ) goto error; |
| 966 | |
| 967 | // add ctor( string path, float rate ) |
| 968 | func = make_new_ctor( sndbuf2_ctor_path_rate ); |
| 969 | func->add_arg( "string", "path" ); |
| 970 | func->add_arg( "float", "rate" ); |
| 971 | func->doc = "construct a SndBuf2 with the 'path' to a sound file to read, and a default playback 'rate' (1.0 is normal rate)"; |
| 972 | if( !type_engine_import_ctor( env, func ) ) goto error; |
| 973 | |
| 974 | // add ctor( string path ) |
| 975 | func = make_new_ctor( sndbuf2_ctor_path_rate_pos ); |
| 976 | func->add_arg( "string", "path" ); |
| 977 | func->add_arg( "float", "rate" ); |
| 978 | func->add_arg( "int", "pos" ); |
| 979 | func->doc = "construct a SndBuf2 with the 'path' to a sound file to read, a default playback 'rate' (1.0 is normal rate), and starting at sample position 'pos'"; |
| 980 | if( !type_engine_import_ctor( env, func ) ) goto error; |
| 981 | |
| 982 | |
| 983 | // end import |
| 984 | if( !type_engine_import_class_end( env ) ) |
| 985 | return FALSE; |
| 986 | |
| 987 | #endif // __DISABLE_SNDBUF__ |
| 988 | |
| 989 | |
| 990 | //--------------------------------------------------------------------- |
| 991 | // init as base class: Dyno |
| 992 | //--------------------------------------------------------------------- |
| 993 | doc = "a dynamics processor. Includes presets for limiter, compressor, expander, noise gate, and ducker."; |
| 994 | if( !type_engine_import_ugen_begin( env, "Dyno", "UGen", env->global(), |
| 995 | dyno_ctor, dyno_dtor, dyno_tick, NULL, doc.c_str() ) ) |
| 996 | return FALSE; |
| 997 | |
| 998 | if( !type_engine_import_add_ex( env, "special/Dyno-compress.ck" ) ) goto error; |
| 999 | if( !type_engine_import_add_ex( env, "special/Dyno-duck.ck" ) ) goto error; |
| 1000 | if( !type_engine_import_add_ex( env, "special/Dyno-limit.ck" ) ) goto error; |
| 1001 | |
| 1002 | // add member variable |
| 1003 | dyno_offset_data = type_engine_import_mvar( env, "int", "@dyno_data", FALSE ); |
| 1004 | if( dyno_offset_data == CK_INVALID_OFFSET ) goto error; |
| 1005 | |
| 1006 | // add ctrl: limit |
| 1007 | func = make_new_mfun( "void", "limit", dyno_ctrl_limit ); |
| 1008 | //func->add_arg( "string", "mode" ); |
| 1009 | func->doc = "set parameters to default limiter values: slopeAbove = 0.1, slopeBelow = 1.0, thresh = 0.5, attackTime = 5 ms, releaseTime = 300 ms, externalSideInput = 0 (false)"; |
| 1010 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1011 | |
| 1012 | // add ctrl: compress |
| 1013 | func = make_new_mfun( "void", "compress", dyno_ctrl_compress ); |
| 1014 | //func->add_arg( "string", "mode" ); |
| 1015 | func->doc = "set parameters to default compressor values: slopeAbove = 0.5, slopeBelow = 1.0, thresh = 0.5, attackTime = 5 ms, releaseTime = 500 ms, externalSideInput = 0 (false)"; |
| 1016 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1017 | |
| 1018 | // add ctrl: gate |
| 1019 | func = make_new_mfun( "void", "gate", dyno_ctrl_gate ); |
| 1020 | //func->add_arg( "string", "mode" ); |
| 1021 | func->doc = "set parameters to default noise gate values: slopeAbove = 1.0, slopeBelow = 1.0E08, thresh = 0.1, attackTime = 11 ms, releaseTime = 100 ms, externalSideInput = 0 (false)"; |
| 1022 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1023 | |
| 1024 | // add ctrl: expand |
| 1025 | func = make_new_mfun( "void", "expand", dyno_ctrl_expand ); |
| 1026 | //func->add_arg( "string", "mode" ); |
| 1027 | func->doc = "set parameters to default expander values: slopeAbove = 2.0, slopeBelow = 1.0, thresh = 0.5, attackTime = 20 ms, releaseTime = 400 ms, externalSideInput = 0 (false)"; |
| 1028 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1029 | |
| 1030 | // add ctrl: duck |
| 1031 | func = make_new_mfun( "void", "duck", dyno_ctrl_duck ); |
| 1032 | //func->add_arg( "string", "mode" ); |
| 1033 | func->doc = "set parameters to default ducker values: slopeAbove = 0.5, slopeBelow = 1.0, thresh = 0.5, attackTime = 10 ms, releaseTime = 1000 ms, externalSideInput = 1 (true)"; |
| 1034 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1035 | |
| 1036 | //add ctrl: thresh |
| 1037 | func = make_new_mfun( "float", "thresh", dyno_ctrl_thresh ); |
| 1038 | func->add_arg( "float", "thresh" ); |
| 1039 | func->doc = "set threshold, above which to stop using slopeBelow and start using slopeAbove to determine output gain vs input gain."; |
| 1040 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1041 | |
| 1042 | // add cget: thresh |
| 1043 | func = make_new_mfun( "float", "thresh", dyno_cget_thresh ); |
| 1044 | func->doc = "get threshold, above which to stop using slopeBelow and start using slopeAbove to determine output gain vs input gain."; |
| 1045 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1046 | |
| 1047 | //add ctrl: attackTime |
| 1048 | func = make_new_mfun( "dur", "attackTime", dyno_ctrl_attackTime ); |
| 1049 | func->add_arg( "dur", "aTime" ); |
| 1050 | func->doc = "set duration for the envelope to move linearly from current value to the absolute value of the signal's amplitude."; |
| 1051 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1052 | |
| 1053 | //add cget: attackTime |
| 1054 | func = make_new_mfun( "dur", "attackTime", dyno_cget_attackTime ); |
| 1055 | func->doc = "get duration for the envelope to move linearly from current value to the absolute value of the signal's amplitude."; |
| 1056 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1057 | |
| 1058 | //add ctrl: releaseTime |
| 1059 | func = make_new_mfun( "dur", "releaseTime", dyno_ctrl_releaseTime ); |
| 1060 | func->add_arg( "dur", "rTime" ); |
| 1061 | func->doc = "set duration for the envelope to decay down to around 1/10 of its current amplitude, if not brought back up by the signal."; |
| 1062 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1063 | |
| 1064 | //add ctrl: releaseTime |
| 1065 | func = make_new_mfun( "dur", "releaseTime", dyno_cget_releaseTime ); |
| 1066 | func->doc = "get duration for the envelope to decay down to around 1/10 of its current amplitude, if not brought back up by the signal."; |
| 1067 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1068 | |
| 1069 | //add ctrl: ratio |
| 1070 | func = make_new_mfun( "float", "ratio", dyno_ctrl_ratio ); |
| 1071 | func->add_arg( "float", "ratio" ); |
| 1072 | func->doc = "an alternate way of setting slopeAbove and slopeBelow; sets slopeBelow to 1.0 and slopeAbove to 1.0 / ratio."; |
| 1073 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1074 | |
| 1075 | //add cget: ratio |
| 1076 | func = make_new_mfun( "float", "ratio", dyno_cget_ratio ); |
| 1077 | func->doc = "get the ratio set by .ratio(float); slopeBelow to 1.0 and slopeAbove to 1.0 / ratio."; |
| 1078 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1079 | |
| 1080 | //add ctrl: slopeBelow |
| 1081 | func = make_new_mfun( "float", "slopeBelow", dyno_ctrl_slopeBelow ); |
| 1082 | func->add_arg( "float", "slopeBelow" ); |
| 1083 | func->doc = "set the slope of the output gain vs the input envelope's level when the envelope is below thresh."; |
| 1084 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1085 | |
| 1086 | //add cget: slopeBelow |
| 1087 | func = make_new_mfun( "float", "slopeBelow", dyno_cget_slopeBelow ); |
| 1088 | func->doc = "get the slope of the output gain vs the input envelope's level when the envelope is below thresh."; |
| 1089 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1090 | |
| 1091 | //add ctrl: slopeAbove |
| 1092 | func = make_new_mfun( "float", "slopeAbove", dyno_ctrl_slopeAbove ); |
| 1093 | func->add_arg( "float", "slopeAbove" ); |
| 1094 | func->doc = "set the slope of the output gain vs the input envelope's level when the envelope is above thresh."; |
| 1095 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1096 | |
| 1097 | //add cget: slopeAbove |
| 1098 | func = make_new_mfun( "float", "slopeAbove", dyno_cget_slopeAbove ); |
| 1099 | func->doc = "get the slope of the output gain vs the input envelope's level when the envelope is above thresh."; |
| 1100 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1101 | |
| 1102 | //add ctrl: sideInput |
| 1103 | func = make_new_mfun( "float", "sideInput", dyno_ctrl_sideInput ); |
| 1104 | func->add_arg( "float", "sideInput" ); |
| 1105 | func->doc = "if externalSideInput is set to true, replace the signal being processed as the input to the amplitude envelope."; |
| 1106 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1107 | |
| 1108 | //add cget: sideInput |
| 1109 | func = make_new_mfun( "float", "sideInput", dyno_cget_sideInput ); |
| 1110 | func->doc = "if externalSideInput is set to true, replaces the signal being processed as the input to the amplitude envelope."; |
| 1111 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1112 | |
| 1113 | //add ctrl: externalSideInput |
| 1114 | func = make_new_mfun( "int", "externalSideInput", dyno_ctrl_externalSideInput ); |
| 1115 | func->add_arg( "int", "externalSideInput" ); |
| 1116 | func->doc = "set to true to cue the amplitude envelope off sideInput instead of the input signal. Note that this means you will need to manually set sideInput every so often."; |
| 1117 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1118 | |
| 1119 | //add cget: externalSideInput |
| 1120 | func = make_new_mfun( "int", "externalSideInput", dyno_cget_externalSideInput ); |
| 1121 | func->doc = "get externalSideInput state. If set to true, the amplitude envelope will be cued off sideInput instead of the input signal. Note that this means you will need to manually set sideInput every so often."; |
| 1122 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1123 | |
| 1124 | // end import |
| 1125 | if( !type_engine_import_class_end( env ) ) |
| 1126 | return FALSE; |
| 1127 | |
| 1128 | //--------------------------------------------------------------------- |
| 1129 | // init as base class: Identity2 |
| 1130 | // 1.5.0.0 (ge + nshaheed) made this for exploring multichannel conundrums |
| 1131 | //--------------------------------------------------------------------- |
| 1132 | doc = "A 2-channel UGen that outputs the inputs."; |
| 1133 | // multichannel |
| 1134 | if( !type_engine_import_ugen_begin( env, "Identity2", "UGen", env->global(), |
| 1135 | NULL, NULL, NULL, Identity2_tickf, |
| 1136 | NULL, 2, 2, doc.c_str() ) ) |
| 1137 | return FALSE; |
| 1138 | // end the class import |
| 1139 | type_engine_import_class_end( env ); |
| 1140 | |
| 1141 | // import LiSa! |
| 1142 | if( !lisa_query( QUERY ) ) |
| 1143 | return FALSE; |
| 1144 | |
| 1145 | return TRUE; |
| 1146 | |
| 1147 | error: |
| 1148 | |
| 1149 | // end the class import |
| 1150 | type_engine_import_class_end( env ); |
| 1151 | |
| 1152 | return FALSE; |
| 1153 | } |
| 1154 | |
| 1155 | |
| 1156 | |
| 1157 | |
| 1158 | // LiSa (live sampling data offset) |
| 1159 | //static t_CKUINT LiSaBasic_offset_data = 0; |
| 1160 | static t_CKUINT LiSaMulti_offset_data = 0; |
| 1161 | |
| 1162 | // max channels for multichannel LiSa |
| 1163 | #define LiSa_MAXCHANNELS 16 // 1.4.1.0 (ge) increased from 10 to 16 |
| 1164 | |
| 1165 | |
| 1166 | |
| 1167 | |
| 1168 | //----------------------------------------------------------------------------- |
| 1169 | // name: lisa_query() |
| 1170 | // desc: ... |
| 1171 | //----------------------------------------------------------------------------- |
| 1172 | DLL_QUERY lisa_query( Chuck_DL_Query * QUERY ) |
| 1173 | { |
| 1174 | Chuck_Env * env = QUERY->env(); |
| 1175 | Chuck_DL_Func * func = NULL; |
| 1176 | std::string doc; |
| 1177 | |
| 1178 | //--------------------------------------------------------------------- |
| 1179 | // init class: LiSa; overloaded class for both LiSaBasic and LiSaMulti |
| 1180 | // - probably don't need the others anymore.... |
| 1181 | // author: Dan Trueman (dan /at/ music.princeton.edu) |
| 1182 | //--------------------------------------------------------------------- |
| 1183 | |
| 1184 | // doc string |
| 1185 | doc = "LiSa provides basic live sampling functionality,\ |
| 1186 | and is also often used for granular synthesis.\ |
| 1187 | An internal buffer stores samples chucked to LiSa's input.\ |
| 1188 | Segments of this buffer can be played back, with ramping and\ |
| 1189 | speed/direction control.\ |
| 1190 | Multiple voice facility is built in, allowing for a single\ |
| 1191 | LiSa object to serve as a source for sample layering and\ |
| 1192 | granular textures.\ |
| 1193 | by Dan Trueman (2007)\n\ |
| 1194 | See also: a slowly growing <a target=\"_blank\" href=\"../program/lisa/tutorial-1.html\">tutorial</a>\ |
| 1195 | | <a target=\"_blank\" href=\"../examples/#lisa\">LiSa examples</a> in the ChucK distribution | <a target=\"_blank\" href=\"https://www.youtube.com/watch?v=7F75v_73pF4\">video tutorial</a> by Clint Hoagland."; |
| 1196 | if( !type_engine_import_ugen_begin( env, "LiSa", "UGen", env->global(), |
| 1197 | LiSaMulti_ctor, LiSaMulti_dtor, |
| 1198 | LiSaMulti_tick, NULL, |
| 1199 | LiSaMulti_pmsg, 1, 1, doc.c_str() )) |
| 1200 | return FALSE; |
| 1201 | |
| 1202 | // add examples | 1.5.0.0 (ge) |
| 1203 | if( !type_engine_import_add_ex( env, "special/LiSa-munger1.ck" ) ) goto error; |
| 1204 | if( !type_engine_import_add_ex( env, "special/LiSa-munger2.ck" ) ) goto error; |
| 1205 | if( !type_engine_import_add_ex( env, "special/LiSa-munger3.ck" ) ) goto error; |
| 1206 | if( !type_engine_import_add_ex( env, "special/LiSa-load.ck" ) ) goto error; |
| 1207 | if( !type_engine_import_add_ex( env, "special/LiSa-simplelooping.ck" ) ) goto error; |
| 1208 | if( !type_engine_import_add_ex( env, "special/LiSa-trigger.ck" ) ) goto error; |
| 1209 | if( !type_engine_import_add_ex( env, "special/LiSa-track1.ck" ) ) goto error; |
| 1210 | if( !type_engine_import_add_ex( env, "special/LiSa-track2.ck" ) ) goto error; |
| 1211 | if( !type_engine_import_add_ex( env, "special/LiSa-track3.ck" ) ) goto error; |
| 1212 | if( !type_engine_import_add_ex( env, "special/LiSa-track4.ck" ) ) goto error; |
| 1213 | if( !type_engine_import_add_ex( env, "special/LiSa-track5.ck" ) ) goto error; |
| 1214 | if( !type_engine_import_add_ex( env, "special/LiSa-stereo.ck" ) ) goto error; |
| 1215 | if( !type_engine_import_add_ex( env, "special/scream-o-matic/scream-o-matic.ck" ) ) goto error; |
| 1216 | if( !type_engine_import_add_ex( env, "special/twilight/twilight-granular-kb.ck" ) ) goto error; |
| 1217 | if( !type_engine_import_add_ex( env, "special/twilight/twilight-granular-kb-interp.ck" ) ) goto error; |
| 1218 | |
| 1219 | // add ctor( string path ) |
| 1220 | func = make_new_ctor( LiSaMulti_ctor_path ); |
| 1221 | func->add_arg( "string", "path" ); |
| 1222 | func->doc = "construct a LiSa with the 'path' to an audio file to read."; |
| 1223 | if( !type_engine_import_ctor( env, func ) ) goto error; |
| 1224 | |
| 1225 | // set/get buffer size |
| 1226 | func = make_new_mfun( "dur", "duration", LiSaMulti_size ); |
| 1227 | func->doc = "Set buffer size; required to allocate memory, also resets all parameter values to default."; |
| 1228 | func->add_arg( "dur", "val" ); |
| 1229 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1230 | func = make_new_mfun( "dur", "duration", LiSaMulti_cget_size ); |
| 1231 | func->doc = "Get buffer size."; |
| 1232 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1233 | |
| 1234 | func = make_new_mfun( "string", "read", LiSaMulti_ctrl_read ); |
| 1235 | func->add_arg( "string", "path" ); |
| 1236 | func->doc = "read audio file at 'path' into LiSa"; |
| 1237 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1238 | |
| 1239 | // start/stop recording |
| 1240 | func = make_new_mfun( "int", "record", LiSaMulti_start_record ); |
| 1241 | func->doc = "Turn recording on and off"; |
| 1242 | func->add_arg( "int", "toggle" ); |
| 1243 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1244 | |
| 1245 | // start/stop playing |
| 1246 | func = make_new_mfun( "int", "play", LiSaMulti_start_play ); |
| 1247 | func->doc = "For particular voice (arg 1), turn on/off sample playback"; |
| 1248 | func->add_arg( "int", "voice" ); |
| 1249 | func->add_arg( "int", "toggle" ); |
| 1250 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1251 | func = make_new_mfun( "int", "play", LiSaMulti_start_play0 ); |
| 1252 | func->doc = "Turn on/off sample playback (voice 0)"; |
| 1253 | func->add_arg( "int", "toggle" ); |
| 1254 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1255 | |
| 1256 | // set/get playback rate |
| 1257 | func = make_new_mfun( "float", "rate", LiSaMulti_ctrl_rate ); |
| 1258 | func->doc = "For particular voice (arg 1), set playback rate"; |
| 1259 | func->add_arg( "int", "voice" ); |
| 1260 | func->add_arg( "float", "val" ); |
| 1261 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1262 | func = make_new_mfun( "float", "rate", LiSaMulti_cget_rate ); |
| 1263 | func->doc = "For particular voice (arg 1), get playback rate"; |
| 1264 | func->add_arg( "int", "voice" ); |
| 1265 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1266 | func = make_new_mfun( "float", "rate", LiSaMulti_ctrl_rate0 ); |
| 1267 | func->doc = "Set playback rate (voice 0). Note that the int/float type for this method will determine whether the rate is being set (float, for voice 0) or read (int, for voice number)."; |
| 1268 | func->add_arg( "float", "val" ); |
| 1269 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1270 | func = make_new_mfun( "float", "rate", LiSaMulti_cget_rate0 ); |
| 1271 | func->doc = "Get playback rate (voice 0)."; |
| 1272 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1273 | |
| 1274 | // playback position |
| 1275 | func = make_new_mfun( "dur", "playPos", LiSaMulti_ctrl_pindex ); |
| 1276 | func->doc = "For particular voice (arg 1), set playback position."; |
| 1277 | func->add_arg( "int", "voice" ); |
| 1278 | func->add_arg( "dur", "val" ); |
| 1279 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1280 | func = make_new_mfun( "dur", "playPos", LiSaMulti_cget_pindex ); |
| 1281 | func->doc = "For particular voice (arg 1), get playback position."; |
| 1282 | func->add_arg( "int", "voice" ); |
| 1283 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1284 | func = make_new_mfun( "dur", "playPos", LiSaMulti_ctrl_pindex0 ); |
| 1285 | func->doc = "Set playback position (voice 0)."; |
| 1286 | func->add_arg( "dur", "val" ); |
| 1287 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1288 | func = make_new_mfun( "dur", "playPos", LiSaMulti_cget_pindex0 ); |
| 1289 | func->doc = "Get playback position (voice 0)."; |
| 1290 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1291 | |
| 1292 | // record position |
| 1293 | func = make_new_mfun( "dur", "recPos", LiSaMulti_ctrl_rindex ); |
| 1294 | func->doc = "Set record position."; |
| 1295 | func->add_arg( "dur", "val" ); |
| 1296 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1297 | func = make_new_mfun( "dur", "recPos", LiSaMulti_cget_rindex ); |
| 1298 | func->doc = "Get record position."; |
| 1299 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1300 | |
| 1301 | // loopstart position |
| 1302 | func = make_new_mfun( "dur", "loopStart", LiSaMulti_ctrl_lstart ); |
| 1303 | func->doc = "For particular voice (arg 1), set loop starting point for playback. only applicable when .loop(voice, 1)."; |
| 1304 | func->add_arg( "int", "voice" ); |
| 1305 | func->add_arg( "dur", "val" ); |
| 1306 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1307 | func = make_new_mfun( "dur", "loopStart", LiSaMulti_cget_lstart ); |
| 1308 | func->doc = "For particular voice (arg 1), get loop starting point for playback. only applicable when .loop(voice, 1)."; |
| 1309 | func->add_arg( "int", "voice" ); |
| 1310 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1311 | func = make_new_mfun( "dur", "loopStart", LiSaMulti_ctrl_lstart0 ); |
| 1312 | func->doc = "Set loop starting point for playback (voice 0). only applicable when 1 => loop."; |
| 1313 | func->add_arg( "dur", "val" ); |
| 1314 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1315 | func = make_new_mfun( "dur", "loopStart", LiSaMulti_cget_lstart0 ); |
| 1316 | func->doc = "Get loop starting point for playback (voice 0). only applicable when 1 => loop."; |
| 1317 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1318 | |
| 1319 | // loopend position |
| 1320 | func = make_new_mfun( "dur", "loopEnd", LiSaMulti_ctrl_lend ); |
| 1321 | func->doc = "For particular voice (arg 1), set loop ending point for playback. only applicable when .loop(voice, 1)."; |
| 1322 | func->add_arg( "int", "voice" ); |
| 1323 | func->add_arg( "dur", "val" ); |
| 1324 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1325 | func = make_new_mfun( "dur", "loopEnd", LiSaMulti_cget_lend); |
| 1326 | func->doc = "For particular voice (arg 1), get loop ending point for playback. only applicable when .loop(voice, 1)."; |
| 1327 | func->add_arg( "int", "voice" ); |
| 1328 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1329 | func = make_new_mfun( "dur", "loopEnd", LiSaMulti_ctrl_lend0 ); |
| 1330 | func->doc = "Set loop ending point for playback (voice 0). only applicable when 1 => loop."; |
| 1331 | func->add_arg( "dur", "val" ); |
| 1332 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1333 | func = make_new_mfun( "dur", "loopEnd", LiSaMulti_cget_lend0); |
| 1334 | func->doc = "Get loop ending point for playback (voice 0). only applicable when 1 => loop."; |
| 1335 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1336 | |
| 1337 | // loop |
| 1338 | func = make_new_mfun( "int", "loop", LiSaMulti_ctrl_loop ); |
| 1339 | func->doc = "For particular voice (arg 1), turn on/off looping."; |
| 1340 | func->add_arg( "int", "voice" ); |
| 1341 | func->add_arg( "int", "val" ); |
| 1342 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1343 | |
| 1344 | func = make_new_mfun( "int", "loop", LiSaMulti_cget_loop); |
| 1345 | func->doc = "For particular voice (arg 1), get looping status."; |
| 1346 | func->add_arg( "int", "voice" ); |
| 1347 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1348 | |
| 1349 | func = make_new_mfun( "int", "loop0", LiSaMulti_ctrl_loop0 ); |
| 1350 | func->doc = "Turn on/off looping (voice 0)."; |
| 1351 | func->add_arg( "int", "val" ); |
| 1352 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1353 | |
| 1354 | func = make_new_mfun( "int", "loop0", LiSaMulti_cget_loop0); |
| 1355 | func->doc = "Get looping status (voice 0)."; |
| 1356 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1357 | |
| 1358 | // bidirectional looping |
| 1359 | func = make_new_mfun( "int", "bi", LiSaMulti_ctrl_bi ); |
| 1360 | func->doc = "For particular voice (arg 1), turn on/off bidirectional playback."; |
| 1361 | func->add_arg( "int", "voice" ); |
| 1362 | func->add_arg( "int", "val" ); |
| 1363 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1364 | func = make_new_mfun( "int", "getbi", LiSaMulti_cget_bi); |
| 1365 | func->doc = "Turn on/off bidirectional playback (voice 0)."; |
| 1366 | func->add_arg( "int", "voice" ); |
| 1367 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1368 | func = make_new_mfun( "int", "bi", LiSaMulti_ctrl_bi0 ); |
| 1369 | func->doc = "For particular voice (arg 1), get bidirectional playback status."; |
| 1370 | func->add_arg( "int", "val" ); |
| 1371 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1372 | func = make_new_mfun( "int", "bi", LiSaMulti_cget_bi0); |
| 1373 | func->doc = "Get birectional playback status."; |
| 1374 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1375 | |
| 1376 | // loopend_rec position |
| 1377 | func = make_new_mfun( "dur", "loopEndRec", LiSaMulti_ctrl_loop_end_rec ); |
| 1378 | func->doc = "Set end point in buffer for loop recording."; |
| 1379 | func->add_arg( "dur", "val" ); |
| 1380 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1381 | func = make_new_mfun( "dur", "loopEndRec", LiSaMulti_cget_loop_end_rec); |
| 1382 | func->doc = "Get end point in buffer for loop recording."; |
| 1383 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1384 | |
| 1385 | // loop_rec toggle set; for turning on/off loop recording |
| 1386 | func = make_new_mfun( "int", "loopRec", LiSaMulti_ctrl_loop_rec ); |
| 1387 | func->doc = "Turn on/off loop recording."; |
| 1388 | func->add_arg( "int", "val" ); |
| 1389 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1390 | func = make_new_mfun( "int", "loopRec", LiSaMulti_cget_loop_rec); |
| 1391 | func->doc = "Get loop recording status."; |
| 1392 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1393 | |
| 1394 | // look at or put sample directly in record buffer, do not pass go |
| 1395 | func = make_new_mfun( "float", "valueAt", LiSaMulti_ctrl_sample ); |
| 1396 | func->doc = "Set value directly in record buffer."; |
| 1397 | func->add_arg( "float", "val" ); |
| 1398 | func->add_arg( "dur", "index" ); |
| 1399 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1400 | |
| 1401 | func = make_new_mfun( "float", "valueAt", LiSaMulti_cget_sample); |
| 1402 | func->doc = "Get value directly from record buffer."; |
| 1403 | func->add_arg( "dur", "index" ); |
| 1404 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1405 | |
| 1406 | // set/get voiceGain |
| 1407 | func = make_new_mfun( "float", "voiceGain", LiSaMulti_ctrl_voicegain ); |
| 1408 | func->doc = "For particular voice (arg 1), set gain."; |
| 1409 | func->add_arg( "int", "voice" ); |
| 1410 | func->add_arg( "float", "val" ); |
| 1411 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1412 | func = make_new_mfun( "float", "voiceGain", LiSaMulti_cget_voicegain); |
| 1413 | func->doc = "Set playback gain (voice 0)."; |
| 1414 | func->add_arg( "int", "voice" ); |
| 1415 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1416 | |
| 1417 | // set/get voicePan [value between 0 and numchans-1, to place voice] |
| 1418 | func = make_new_mfun( "float", "voicePan", LiSaMulti_ctrl_voicepan ); |
| 1419 | func->doc = "For particular voice (arg 1), set panning value [0.0, number of channels - 1.0]."; |
| 1420 | func->add_arg( "int", "voice" ); |
| 1421 | func->add_arg( "float", "val" ); |
| 1422 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1423 | func = make_new_mfun( "float", "voicePan", LiSaMulti_cget_voicepan); |
| 1424 | func->doc = "For particular voice (arg 1), get panning value."; |
| 1425 | func->add_arg( "int", "voice" ); |
| 1426 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1427 | |
| 1428 | // set/get voicePan [value between 0 and numchans-1, to place voice] |
| 1429 | func = make_new_mfun( "float", "pan", LiSaMulti_ctrl_voicepan ); |
| 1430 | func->doc = "For particular voice (arg 1), set panning value [0.0, number of channels - 1.0]."; |
| 1431 | func->add_arg( "int", "voice" ); |
| 1432 | func->add_arg( "float", "val" ); |
| 1433 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1434 | func = make_new_mfun( "float", "pan", LiSaMulti_cget_voicepan); |
| 1435 | func->doc = "For particular voice (arg 1), get panning value."; |
| 1436 | func->add_arg( "int", "voice" ); |
| 1437 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1438 | |
| 1439 | func = make_new_mfun( "float", "pan", LiSaMulti_ctrl_voicepan0 ); |
| 1440 | func->doc = "For voice 0, set panning value [0.0, number of channels - 1.0]."; |
| 1441 | func->add_arg( "float", "val" ); |
| 1442 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1443 | |
| 1444 | func = make_new_mfun( "float", "pan", LiSaMulti_cget_voicepan0); |
| 1445 | func->doc = "For voice 0, get panning value."; |
| 1446 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1447 | |
| 1448 | // set record feedback coefficient |
| 1449 | func = make_new_mfun( "float", "feedback", LiSaMulti_ctrl_coeff ); |
| 1450 | func->doc = "Set feedback amount when overdubbing (loop recording; how much to retain)."; |
| 1451 | func->add_arg( "float", "val" ); |
| 1452 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1453 | func = make_new_mfun( "float", "feedback", LiSaMulti_cget_coeff); |
| 1454 | func->doc = "Get feedback amount when overdubbing (loop recording; how much to retain)."; |
| 1455 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1456 | |
| 1457 | // clear buffer |
| 1458 | func = make_new_mfun( "void", "clear", LiSaMulti_ctrl_clear ); |
| 1459 | func->doc = "Clear recording buffer."; |
| 1460 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1461 | |
| 1462 | // get free voice |
| 1463 | func = make_new_mfun( "int", "getVoice", LiSaMulti_cget_voice ); |
| 1464 | func->doc = "Return an available voice (one that is not currently playing). Return -1 if no voice is available."; |
| 1465 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1466 | |
| 1467 | // set maxvoices |
| 1468 | func = make_new_mfun( "int", "maxVoices", LiSaMulti_ctrl_maxvoices ); |
| 1469 | func->doc = "Set the maximum number of voices allowable; 10 by default (256 is the current hardwired internal limit)."; |
| 1470 | func->add_arg( "int", "val" ); |
| 1471 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1472 | func = make_new_mfun( "int", "maxVoices", LiSaMulti_cget_maxvoices); |
| 1473 | func->doc = "Get the maximum number of voices allowable; 10 by default (256 is the current hardwired internal limit)."; |
| 1474 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1475 | |
| 1476 | // ramp stuff |
| 1477 | func = make_new_mfun( "void", "rampUp", LiSaMulti_ctrl_rampup ); |
| 1478 | func->doc = "For particular voice (arg 1), turn on sample playback, with ramp."; |
| 1479 | func->add_arg( "int", "voice" ); |
| 1480 | func->add_arg( "dur", "val" ); |
| 1481 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1482 | |
| 1483 | func = make_new_mfun( "void", "rampDown", LiSaMulti_ctrl_rampdown ); |
| 1484 | func->doc = "For particular voice (arg 1), turn off sample playback, with ramp"; |
| 1485 | func->add_arg( "int", "voice" ); |
| 1486 | func->add_arg( "dur", "val" ); |
| 1487 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1488 | |
| 1489 | func = make_new_mfun( "dur", "recRamp", LiSaMulti_ctrl_rec_ramplen ); |
| 1490 | func->doc = "Set ramping when recording (from 0 to loopEndRec)."; |
| 1491 | func->add_arg( "dur", "val" ); |
| 1492 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1493 | |
| 1494 | func = make_new_mfun( "void", "rampUp", LiSaMulti_ctrl_rampup0 ); |
| 1495 | func->doc = "Turn on sample playback, with ramp (voice 0)."; |
| 1496 | func->add_arg( "dur", "val" ); |
| 1497 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1498 | |
| 1499 | func = make_new_mfun( "void", "rampDown", LiSaMulti_ctrl_rampdown0 ); |
| 1500 | func->doc = "Turn off sample playback, with ramp (voice 0)."; |
| 1501 | func->add_arg( "dur", "val" ); |
| 1502 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1503 | |
| 1504 | // get value |
| 1505 | func = make_new_mfun( "dur", "value", LiSaMulti_cget_value ); |
| 1506 | func->doc = "For particular voice (arg 1), get value from the voice."; |
| 1507 | func->add_arg( "int", "voice" ); |
| 1508 | func->add_arg( "dur", "val" ); |
| 1509 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1510 | func = make_new_mfun( "dur", "value", LiSaMulti_cget_value0 ); |
| 1511 | func->doc = "Get value from voice 0."; |
| 1512 | func->add_arg( "dur", "val" ); |
| 1513 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1514 | |
| 1515 | // track |
| 1516 | func = make_new_mfun( "int", "track", LiSaMulti_ctrl_track ); |
| 1517 | func->doc = "Identical to sync."; |
| 1518 | func->add_arg( "int", "val" ); |
| 1519 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1520 | func = make_new_mfun( "int", "track", LiSaMulti_cget_track); |
| 1521 | func->doc = "Identical to sync."; |
| 1522 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1523 | |
| 1524 | // sync = track |
| 1525 | func = make_new_mfun( "int", "sync", LiSaMulti_ctrl_track ); |
| 1526 | func->doc = "Set input mode; (0) input is recorded to internal buffer, (1) input sets playback position [0,1] (phase value between loopStart and loopEnd for all active voices), (2) input sets playback position, interpreted as a time value in samples (only works with voice 0)"; |
| 1527 | func->add_arg( "int", "val" ); |
| 1528 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1529 | func = make_new_mfun( "int", "sync", LiSaMulti_cget_track); |
| 1530 | func->doc = "Get input mode; (0) input is recorded to internal buffer, (1) input sets playback position [0,1] (phase value between loopStart and loopEnd for all active voices), (2) input sets playback position, interpreted as a time value in samples (only works with voice 0)"; |
| 1531 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1532 | |
| 1533 | // playing |
| 1534 | func = make_new_mfun( "int", "playing", LiSaMulti_cget_playing ); |
| 1535 | func->doc = "Get playing status."; |
| 1536 | func->add_arg( "int", "val" ); |
| 1537 | if( !type_engine_import_mfun( env, func ) ) goto error; |
| 1538 | |
| 1539 | // end the class import |
| 1540 | type_engine_import_class_end( env ); |
| 1541 | |
| 1542 | // LiSa2 description |
| 1543 | doc = "a (li)ve (sa)mpling unit generator (stereo edition); also popularly used for granular synthesis."; |
| 1544 | // multichannel |
| 1545 | if( !type_engine_import_ugen_begin( env, "LiSa2", "LiSa", env->global(), |
| 1546 | NULL, NULL, // 1.5.2.4 (ge) null ctor dtor; these duplicate parent ctor and dtor |
| 1547 | NULL, LiSaMulti_tickf, |
| 1548 | LiSaMulti_pmsg, 1, 2, doc.c_str() ) ) |
| 1549 | return FALSE; |
| 1550 | |
| 1551 | // add ctor( string path ) |
| 1552 | func = make_new_ctor( LiSa2_ctor_path ); |
| 1553 | func->add_arg( "string", "path" ); |
| 1554 | func->doc = "construct a LiSa2 with the 'path' to an audio file to read."; |
| 1555 | if( !type_engine_import_ctor( env, func ) ) goto error; |
| 1556 | |
| 1557 | // add examples |
| 1558 | if( !type_engine_import_add_ex( env, "special/LiSa-stereo.ck" ) ) goto error; |
| 1559 | |
| 1560 | // end the class import |
| 1561 | type_engine_import_class_end( env ); |
| 1562 | |
| 1563 | |
| 1564 | // LiSa4 description |
| 1565 | doc = "a (li)ve (sa)mpling unit generator (quad edition); also popularly used for granular synthesis."; |
| 1566 | // multichannel |
| 1567 | if( !type_engine_import_ugen_begin( env, "LiSa4", "LiSa", env->global(), |
| 1568 | NULL, NULL, // 1.5.2.4 (ge) null ctor dtor; these duplicate parent ctor and dtor |
| 1569 | NULL, LiSaMulti_tickf, |
| 1570 | LiSaMulti_pmsg, 1, 4, doc.c_str() ) ) |
| 1571 | return FALSE; |
| 1572 | |
| 1573 | // add ctor( string path ) |
| 1574 | func = make_new_ctor( LiSa4_ctor_path ); |
| 1575 | func->add_arg( "string", "path" ); |
| 1576 | func->doc = "construct a LiSa4 with the 'path' to an audio file to read."; |
| 1577 | if( !type_engine_import_ctor( env, func ) ) goto error; |
| 1578 | |
| 1579 | // end the class import |
| 1580 | type_engine_import_class_end( env ); |
| 1581 | |
| 1582 | // LiSa6 description |
| 1583 | doc = "a (li)ve (sa)mpling unit generator (6-channel edition); also popularly used for granular synthesis."; |
| 1584 | // multichannel |
| 1585 | if( !type_engine_import_ugen_begin( env, "LiSa6", "LiSa", env->global(), |
| 1586 | NULL, NULL, // 1.5.2.4 (ge) null ctor dtor; these duplicate parent ctor and dtor |
| 1587 | NULL, LiSaMulti_tickf, |
| 1588 | LiSaMulti_pmsg, 1, 6, doc.c_str() ) ) |
| 1589 | return FALSE; |
| 1590 | |
| 1591 | // add ctor( string path ) |
| 1592 | func = make_new_ctor( LiSa6_ctor_path ); |
| 1593 | func->add_arg( "string", "path" ); |
| 1594 | func->doc = "construct a LiSa6 with the 'path' to an audio file to read."; |
| 1595 | if( !type_engine_import_ctor( env, func ) ) goto error; |
| 1596 | |
| 1597 | // end the class import |
| 1598 | type_engine_import_class_end( env ); |
| 1599 | |
| 1600 | // LiSa8 description |
| 1601 | doc = "a (li)ve (sa)mpling unit generator (8-channel edition); also popularly used for granular synthesis."; |
| 1602 | // multichannel |
| 1603 | if( !type_engine_import_ugen_begin( env, "LiSa8", "LiSa", env->global(), |
| 1604 | NULL, NULL, // 1.5.2.4 (ge) null ctor dtor; these duplicate parent ctor and dtor |
| 1605 | NULL, LiSaMulti_tickf, |
| 1606 | LiSaMulti_pmsg, 1, 8, doc.c_str() ) ) |
| 1607 | return FALSE; |
| 1608 | |
| 1609 | // add ctor( string path ) |
| 1610 | func = make_new_ctor( LiSa8_ctor_path ); |
| 1611 | func->add_arg( "string", "path" ); |
| 1612 | func->doc = "construct a LiSa8 with the 'path' to an audio file to read."; |
| 1613 | if( !type_engine_import_ctor( env, func ) ) goto error; |
| 1614 | |
| 1615 | // end the class import |
| 1616 | type_engine_import_class_end( env ); |
| 1617 | |
| 1618 | |
| 1619 | // LiSa16 description |
| 1620 | doc = "a (li)ve (sa)mpling unit generator (10-channel edition); also popularly used for granular synthesis."; |
| 1621 | // multichannel |
| 1622 | if( !type_engine_import_ugen_begin( env, "LiSa10", "LiSa", env->global(), |
| 1623 | NULL, NULL, // 1.5.2.4 (ge) null ctor dtor; these duplicate parent ctor and dtor |
| 1624 | NULL, LiSaMulti_tickf, |
| 1625 | LiSaMulti_pmsg, 1, 10, doc.c_str() ) ) |
| 1626 | return FALSE; |
| 1627 | |
| 1628 | // add ctor( string path ) |
| 1629 | func = make_new_ctor( LiSa10_ctor_path ); |
| 1630 | func->add_arg( "string", "path" ); |
| 1631 | func->doc = "construct a LiSa10 with the 'path' to an audio file to read."; |
| 1632 | if( !type_engine_import_ctor( env, func ) ) goto error; |
| 1633 | |
| 1634 | // end the class import |
| 1635 | type_engine_import_class_end( env ); |
| 1636 | |
| 1637 | // LiSa16 description |
| 1638 | doc = "a (li)ve (sa)mpling unit generator (16-channel edition); also popularly used for granular synthesis."; |
| 1639 | // multichannel |
| 1640 | if( !type_engine_import_ugen_begin( env, "LiSa16", "LiSa", env->global(), |
| 1641 | NULL, NULL, // 1.5.2.4 (ge) null ctor dtor; these duplicate parent ctor and dtor |
| 1642 | NULL, LiSaMulti_tickf, |
| 1643 | LiSaMulti_pmsg, 1, LiSa_MAXCHANNELS, doc.c_str() ) ) |
| 1644 | return FALSE; |
| 1645 | |
| 1646 | // add ctor( string path ) |
| 1647 | func = make_new_ctor( LiSa16_ctor_path ); |
| 1648 | func->add_arg( "string", "path" ); |
| 1649 | func->doc = "construct a LiSa16 with the 'path' to an audio file to read."; |
| 1650 | if( !type_engine_import_ctor( env, func ) ) goto error; |
| 1651 | |
| 1652 | // end the class import |
| 1653 | type_engine_import_class_end( env ); |
| 1654 | |
| 1655 | return TRUE; |
| 1656 | |
| 1657 | error: |
| 1658 | |
| 1659 | // end the class import |
| 1660 | type_engine_import_class_end( env ); |
| 1661 | |
| 1662 | return FALSE; |
| 1663 | } |
| 1664 | |
| 1665 | |
| 1666 | //----------------------------------------------------------------------------- |
| 1667 | // name: subgraph_ctor() |
| 1668 | // desc: subgraph constructor |
| 1669 | //----------------------------------------------------------------------------- |
| 1670 | CK_DLL_CTOR( subgraph_ctor ) |
| 1671 | { |
| 1672 | // get ugen |
| 1673 | Chuck_UGen * ugen = (Chuck_UGen *)SELF; |
| 1674 | |
| 1675 | // initialize the ugen as subgraph |
| 1676 | ugen->init_subgraph(); |
| 1677 | |
| 1678 | // handle the inlet |
| 1679 | if( ugen->inlet() ) |
| 1680 | { |
| 1681 | // get the ugen |
| 1682 | Chuck_UGen * in = ugen->inlet(); |
| 1683 | // set the mvar |
| 1684 | OBJ_MEMBER_OBJECT(SELF, subgraph_offset_inlet) = in; |
| 1685 | // add refcount for the mvar | 1.5.2.0 (ge) |
| 1686 | // NOTE will be released as part of Object cleanup |
| 1687 | CK_SAFE_ADD_REF(in); |
| 1688 | } |
| 1689 | |
| 1690 | // handle the outlet |
| 1691 | if( ugen->outlet() ) |
| 1692 | { |
| 1693 | // get the ugen |
| 1694 | Chuck_UGen * out = ugen->outlet(); |
| 1695 | // set the mvar |
| 1696 | OBJ_MEMBER_OBJECT(SELF, subgraph_offset_outlet) = out; |
| 1697 | // add refcount for the mvar | 1.5.2.0 (ge) |
| 1698 | // NOTE will be released as part of Object cleanup |
| 1699 | CK_SAFE_ADD_REF(out); |
| 1700 | } |
| 1701 | } |
| 1702 | |
| 1703 | |
| 1704 | //----------------------------------------------------------------------------- |
| 1705 | // name: ck_subgraph_cleaup_inlet_outlet() |
| 1706 | // desc: release and cleanup subgraph inlet/outlet |
| 1707 | //----------------------------------------------------------------------------- |
| 1708 | void ck_subgraph_cleaup_inlet_outlet( Chuck_UGen * ugen ) |
| 1709 | { |
| 1710 | Chuck_UGen * inlet = (Chuck_UGen *)OBJ_MEMBER_OBJECT(ugen, subgraph_offset_inlet); |
| 1711 | Chuck_UGen * outlet = (Chuck_UGen *)OBJ_MEMBER_OBJECT(ugen, subgraph_offset_outlet); |
| 1712 | |
| 1713 | // release |
| 1714 | CK_SAFE_RELEASE( inlet ); |
| 1715 | CK_SAFE_RELEASE( outlet ); |
| 1716 | |
| 1717 | // zero out |
| 1718 | OBJ_MEMBER_OBJECT(ugen, subgraph_offset_inlet) = NULL; |
| 1719 | OBJ_MEMBER_OBJECT(ugen, subgraph_offset_outlet) = NULL; |
| 1720 | } |
| 1721 | |
| 1722 | |
| 1723 | //----------------------------------------------------------------------------- |
| 1724 | // name: FooGen_Data |
| 1725 | // desc: ... |
| 1726 | //----------------------------------------------------------------------------- |
| 1727 | struct FooGen_Data |
| 1728 | { |
| 1729 | Chuck_VM * vm; |
| 1730 | // invoker of member functions |
| 1731 | Chuck_VM_MFunInvoker * invoker; |
| 1732 | |
| 1733 | t_CKFLOAT input; |
| 1734 | t_CKFLOAT output; |
| 1735 | |
| 1736 | // constructor |
| 1737 | FooGen_Data() : vm(NULL), invoker(NULL), input(0), output(0) { } |
| 1738 | }; |
| 1739 | |
| 1740 | |
| 1741 | //----------------------------------------------------------------------------- |
| 1742 | // name: foogen_ctor() |
| 1743 | // desc: ... |
| 1744 | //----------------------------------------------------------------------------- |
| 1745 | CK_DLL_CTOR( foogen_ctor ) |
| 1746 | { |
| 1747 | // create new data |
| 1748 | FooGen_Data * data = new FooGen_Data; |
| 1749 | // set data |
| 1750 | data->vm = SHRED->vm_ref; |
| 1751 | |
| 1752 | // 1.3.1.0: changed from unsigned int to t_CKUINT |
| 1753 | OBJ_MEMBER_UINT(SELF, foogen_offset_data) = (t_CKUINT)data; |
| 1754 | |
| 1755 | // this |
| 1756 | Chuck_UGen * ugen = (Chuck_UGen *)SELF; |
| 1757 | // a chuck function ref |
| 1758 | Chuck_Func * func = NULL; |
| 1759 | // function vtable offset |
| 1760 | t_CKINT tick_fun_index = -1; |
| 1761 | |
| 1762 | // iterate over functions in the virtual table |
| 1763 | for( t_CKINT i = 0; i < ugen->vtable->funcs.size(); i++ ) |
| 1764 | { |
| 1765 | // the function |
| 1766 | func = ugen->vtable->funcs[i]; |
| 1767 | // check for specific signature |
| 1768 | if(func->name.find("tick") == 0 && |
| 1769 | // ensure has one argument |
| 1770 | func->def()->arg_list != NULL && |
| 1771 | // ensure first argument is float |
| 1772 | func->def()->arg_list->type == SHRED->vm_ref->env()->ckt_float && |
| 1773 | // ensure has only one argument |
| 1774 | func->def()->arg_list->next == NULL && |
| 1775 | // ensure returns float |
| 1776 | func->def()->ret_type == SHRED->vm_ref->env()->ckt_float ) |
| 1777 | { |
| 1778 | tick_fun_index = i; |
| 1779 | break; |
| 1780 | } |
| 1781 | } |
| 1782 | |
| 1783 | // if we have a valid |
| 1784 | if( tick_fun_index < 0 ) |
| 1785 | { |
| 1786 | // SPENCERTODO: warn on Chugen definition instead of instantiation? |
| 1787 | EM_error3( "ChuGen '%s' does not define a `fun float tick( float int )` function...", |
| 1788 | ugen->type_ref->base_name.c_str()); |
| 1789 | return; |
| 1790 | } |
| 1791 | |
| 1792 | // create invoker | 1.5.1.5 |
| 1793 | data->invoker = new Chuck_VM_MFunInvoker(); |
| 1794 | // set up the invoker | 1.5.1.5 (ge) encapsulated into invoker |
| 1795 | data->invoker->setup( func, tick_fun_index, VM, SHRED ); |
| 1796 | } |
| 1797 | |
| 1798 | |
| 1799 | //----------------------------------------------------------------------------- |
| 1800 | // name: foogen_dtor() |
| 1801 | // desc: ... |
| 1802 | //----------------------------------------------------------------------------- |
| 1803 | CK_DLL_DTOR( foogen_dtor ) |
| 1804 | { |
| 1805 | FooGen_Data * data = (FooGen_Data *)OBJ_MEMBER_UINT(SELF, foogen_offset_data); |
| 1806 | OBJ_MEMBER_UINT( SELF, foogen_offset_data ) = 0; |
| 1807 | CK_SAFE_DELETE( data->invoker ); |
| 1808 | CK_SAFE_DELETE( data ); |
| 1809 | } |
| 1810 | |
| 1811 | //----------------------------------------------------------------------------- |
| 1812 | // name: foogen_tick() |
| 1813 | // desc: Chugen tick function, which in turn calls user provided tick() |
| 1814 | //----------------------------------------------------------------------------- |
| 1815 | CK_DLL_TICK( foogen_tick ) |
| 1816 | { |
| 1817 | // get internal data |
| 1818 | FooGen_Data * data = (FooGen_Data *) OBJ_MEMBER_UINT(SELF, foogen_offset_data); |
| 1819 | |
| 1820 | // set up argument into tick |
| 1821 | vector<Chuck_DL_Arg> args; |
| 1822 | args.push_back( Chuck_DL_Arg() ); |
| 1823 | args[0].kind = kindof_FLOAT; |
| 1824 | args[0].value.v_float = in; |
| 1825 | // the return value |
| 1826 | Chuck_DL_Return ret; |
| 1827 | |
| 1828 | // default: passthru |
| 1829 | data->output = in; |
| 1830 | // check we have an invoker |
| 1831 | if( data->invoker ) |
| 1832 | { |
| 1833 | // set input |
| 1834 | data->input = in; |
| 1835 | // invoke the function | 1.5.1.5 (ge) encapsulated into invoker |
| 1836 | ret = data->invoker->invoke( SELF, args, SELF->originShred() ); |
| 1837 | // set output |
| 1838 | data->output = ret.v_float; |
| 1839 | } |
| 1840 | // set out for tick function |
| 1841 | *out = data->output; |
| 1842 | |
| 1843 | // done |
| 1844 | return TRUE; |
| 1845 | } |
| 1846 | |
| 1847 | |
| 1848 | |
| 1849 | //----------------------------------------------------------------------------- |
| 1850 | // name: multi_ctor() |
| 1851 | // desc: ... |
| 1852 | //----------------------------------------------------------------------------- |
| 1853 | CK_DLL_CTOR( multi_ctor ) |
| 1854 | { |
| 1855 | // do nothing |
| 1856 | } |
| 1857 | |
| 1858 | |
| 1859 | |
| 1860 | |
| 1861 | //----------------------------------------------------------------------------- |
| 1862 | // name: multi_cget_chan() |
| 1863 | // desc: ... |
| 1864 | //----------------------------------------------------------------------------- |
| 1865 | CK_DLL_CGET( multi_cget_chan ) |
| 1866 | { |
| 1867 | // get ugen |
| 1868 | Chuck_UGen * ugen = (Chuck_UGen *)SELF; |
| 1869 | // value |
| 1870 | t_CKINT index = GET_NEXT_INT( ARGS ); |
| 1871 | // return |
| 1872 | RETURN->v_object = index >= 0 && index < ugen->m_multi_chan_size ? |
| 1873 | ugen->m_multi_chan[index] : NULL ; |
| 1874 | } |
| 1875 | |
| 1876 | |
| 1877 | |
| 1878 | |
| 1879 | //----------------------------------------------------------------------------- |
| 1880 | // name: pan_eq_power() |
| 1881 | // desc: helper function; pan in range [-1,1] |
| 1882 | //----------------------------------------------------------------------------- |
| 1883 | void pan_eq_power( Chuck_UGen * left, Chuck_UGen * right, t_CKFLOAT pan) |
| 1884 | { |
| 1885 | // remap pan from [-1,1] to [0,pi/2] |
| 1886 | t_CKFLOAT panme = (pan+1.0)/2 * CK_ONE_PI/2; |
| 1887 | // pan it (NEW: constant-power panning; fixed 1.4.1.0) |
| 1888 | left->m_pan = ::cos(panme); |
| 1889 | right->m_pan = ::sin(panme); |
| 1890 | } |
| 1891 | |
| 1892 | |
| 1893 | |
| 1894 | |
| 1895 | //----------------------------------------------------------------------------- |
| 1896 | // name: pan_this() |
| 1897 | // desc: helper function; pan in range [-1,1] |
| 1898 | // |
| 1899 | // for more info on panning laws |
| 1900 | // https://www.cs.cmu.edu/~music/icm-online/readings/panlaws/index.html |
| 1901 | //----------------------------------------------------------------------------- |
| 1902 | void pan_this( Chuck_UGen * left, Chuck_UGen * right, t_CKFLOAT pan, t_CKINT law ) |
| 1903 | { |
| 1904 | // unity wtf "pan" |
| 1905 | if( law == PAN_WTF_UNITY ) |
| 1906 | { |
| 1907 | left->m_pan = pan < 0.0 ? 1.0 : 1.0 - pan; |
| 1908 | right->m_pan = pan > 0.0 ? 1.0 : 1.0 + pan; |
| 1909 | } |
| 1910 | // constant power panning |
| 1911 | else if( law == PAN_EQ_POWER ) |
| 1912 | { |
| 1913 | pan_eq_power( left, right, pan ); |
| 1914 | } |
| 1915 | else |
| 1916 | { |
| 1917 | // unsupported |
| 1918 | // TODONOW: error message |
| 1919 | } |
| 1920 | |
| 1921 | // wondering what these do? might observe by uncommenting this |
| 1922 | // cerr << "pan: " << pan << " type: " << law |
| 1923 | // << " left: " << left->m_pan << " right: " << right->m_pan << endl; |
| 1924 | } |
| 1925 | |
| 1926 | |
| 1927 | |
| 1928 | |
| 1929 | //----------------------------------------------------------------------------- |
| 1930 | // name: stereo_ctor() |
| 1931 | // desc: stereo constructor |
| 1932 | //----------------------------------------------------------------------------- |
| 1933 | CK_DLL_CTOR( stereo_ctor ) |
| 1934 | { |
| 1935 | // get ugen |
| 1936 | Chuck_UGen * ugen = (Chuck_UGen *)SELF; |
| 1937 | // default panning law to preserve unity gain (but a WTF panning scheme) |
| 1938 | OBJ_MEMBER_INT(SELF, stereo_offset_panType) = PAN_WTF_UNITY; |
| 1939 | |
| 1940 | // 2 or more |
| 1941 | if( ugen->m_multi_chan_size ) |
| 1942 | { |
| 1943 | // set left |
| 1944 | OBJ_MEMBER_UINT(SELF, stereo_offset_left) = (t_CKUINT)(ugen->m_multi_chan[0]); |
| 1945 | // add ref | 1.5.2.0 |
| 1946 | CK_SAFE_ADD_REF( ugen->m_multi_chan[0] ); |
| 1947 | |
| 1948 | // set right |
| 1949 | OBJ_MEMBER_UINT(SELF, stereo_offset_right) = (t_CKUINT)(ugen->m_multi_chan[1]); |
| 1950 | // add ref | 1.5.2.0 |
| 1951 | CK_SAFE_ADD_REF( ugen->m_multi_chan[1] ); |
| 1952 | } |
| 1953 | else // mono |
| 1954 | { |
| 1955 | // set left |
| 1956 | OBJ_MEMBER_OBJECT(SELF, stereo_offset_left) = ugen; |
| 1957 | // set right |
| 1958 | OBJ_MEMBER_OBJECT(SELF, stereo_offset_right) = ugen; |
| 1959 | |
| 1960 | // add refs 2x | 1.5.2.0 |
| 1961 | CK_SAFE_ADD_REF(ugen); CK_SAFE_ADD_REF(ugen); |
| 1962 | } |
| 1963 | } |
| 1964 | |
| 1965 | |
| 1966 | |
| 1967 | |
| 1968 | //----------------------------------------------------------------------------- |
| 1969 | // name: stereo_dtor() |
| 1970 | // desc: stereo destructor | 1.5.2.0 |
| 1971 | //----------------------------------------------------------------------------- |
| 1972 | CK_DLL_DTOR( stereo_dtor ) |
| 1973 | { |
| 1974 | // left & right |
| 1975 | Chuck_UGen * left = (Chuck_UGen *)OBJ_MEMBER_OBJECT(SELF, stereo_offset_left); |
| 1976 | Chuck_UGen * right = (Chuck_UGen *)OBJ_MEMBER_OBJECT(SELF, stereo_offset_right); |
| 1977 | |
| 1978 | // release |
| 1979 | CK_SAFE_RELEASE(left); |
| 1980 | CK_SAFE_RELEASE(right); |
| 1981 | |
| 1982 | // zero out |
| 1983 | OBJ_MEMBER_OBJECT(SELF, stereo_offset_left) = NULL; |
| 1984 | OBJ_MEMBER_OBJECT(SELF, stereo_offset_right) = NULL; |
| 1985 | } |
| 1986 | |
| 1987 | |
| 1988 | |
| 1989 | |
| 1990 | //----------------------------------------------------------------------------- |
| 1991 | // name: stereo_ctrl_pan() |
| 1992 | // desc: ... |
| 1993 | //----------------------------------------------------------------------------- |
| 1994 | CK_DLL_CTRL( stereo_ctrl_pan ) |
| 1995 | { |
| 1996 | Chuck_UGen * ugen = (Chuck_UGen * )SELF; |
| 1997 | Chuck_UGen * left = ugen->m_multi_chan[0]; |
| 1998 | Chuck_UGen * right = ugen->m_multi_chan[1]; |
| 1999 | // get arg |
| 2000 | t_CKFLOAT pan = GET_CK_FLOAT(ARGS); |
| 2001 | // clip it |
| 2002 | if( pan < -1.0 ) pan = -1.0; |
| 2003 | else if( pan > 1.0 ) pan = 1.0; |
| 2004 | // set it |
| 2005 | OBJ_MEMBER_FLOAT(SELF, stereo_offset_pan) = pan; |
| 2006 | // get panning law |
| 2007 | t_CKINT law = OBJ_MEMBER_INT(SELF, stereo_offset_panType); |
| 2008 | // pan it! |
| 2009 | pan_this( left, right, pan, law ); |
| 2010 | |
| 2011 | // pass through pan value |
| 2012 | RETURN->v_float = pan; |
| 2013 | } |
| 2014 | |
| 2015 | |
| 2016 | |
| 2017 | |
| 2018 | //----------------------------------------------------------------------------- |
| 2019 | // name: stereo_cget_pan() |
| 2020 | // desc: ... |
| 2021 | //----------------------------------------------------------------------------- |
| 2022 | CK_DLL_CGET( stereo_cget_pan ) |
| 2023 | { |
| 2024 | RETURN->v_float = OBJ_MEMBER_FLOAT(SELF, stereo_offset_pan); |
| 2025 | } |
| 2026 | |
| 2027 | |
| 2028 | |
| 2029 | |
| 2030 | //----------------------------------------------------------------------------- |
| 2031 | // name: stereo_ctrl_panType() |
| 2032 | // desc: ... |
| 2033 | //----------------------------------------------------------------------------- |
| 2034 | CK_DLL_CTRL( stereo_ctrl_panType ) |
| 2035 | { |
| 2036 | Chuck_UGen * ugen = (Chuck_UGen * )SELF; |
| 2037 | Chuck_UGen * left = ugen->m_multi_chan[0]; |
| 2038 | Chuck_UGen * right = ugen->m_multi_chan[1]; |
| 2039 | |
| 2040 | // get arg |
| 2041 | t_CKINT panType = GET_CK_INT(ARGS); |
| 2042 | // set it |
| 2043 | OBJ_MEMBER_INT(SELF, stereo_offset_panType) = panType; |
| 2044 | // get pan |
| 2045 | t_CKFLOAT pan = OBJ_MEMBER_FLOAT(SELF, stereo_offset_pan); |
| 2046 | // pan it! |
| 2047 | pan_this( left, right, pan, panType ); |
| 2048 | |
| 2049 | // pass through pan value |
| 2050 | RETURN->v_int = panType; |
| 2051 | } |
| 2052 | |
| 2053 | |
| 2054 | |
| 2055 | |
| 2056 | //----------------------------------------------------------------------------- |
| 2057 | // name: stereo_cget_panType() |
| 2058 | // desc: ... |
| 2059 | //----------------------------------------------------------------------------- |
| 2060 | CK_DLL_CGET( stereo_cget_panType ) |
| 2061 | { |
| 2062 | RETURN->v_int = OBJ_MEMBER_INT(SELF, stereo_offset_panType); |
| 2063 | } |
| 2064 | |
| 2065 | |
| 2066 | |
| 2067 | |
| 2068 | //----------------------------------------------------------------------------- |
| 2069 | // name: dac_tick() |
| 2070 | // desc: ... |
| 2071 | //----------------------------------------------------------------------------- |
| 2072 | CK_DLL_TICK( dac_tick ) |
| 2073 | { |
| 2074 | *out = in; return TRUE; |
| 2075 | } |
| 2076 | |
| 2077 | |
| 2078 | |
| 2079 | |
| 2080 | //----------------------------------------------------------------------------- |
| 2081 | // name: bunghole_tick |
| 2082 | // desc: ... |
| 2083 | //----------------------------------------------------------------------------- |
| 2084 | CK_DLL_TICK( bunghole_tick ) |
| 2085 | { |
| 2086 | *out = 0.0f; return 0; |
| 2087 | } |
| 2088 | |
| 2089 | |
| 2090 | |
| 2091 | |
| 2092 | //----------------------------------------------------------------------------- |
| 2093 | // name: Gain( float gain ) |
| 2094 | // desc: Gain constructor that takes a float |
| 2095 | //----------------------------------------------------------------------------- |
| 2096 | CK_DLL_MFUN( gain_ctor_1 ) |
| 2097 | { |
| 2098 | // get ugen |
| 2099 | Chuck_UGen * ugen = (Chuck_UGen *)SELF; |
| 2100 | // set from constructor arg |
| 2101 | ugen->m_gain = GET_NEXT_FLOAT(ARGS); |
| 2102 | } |
| 2103 | |
| 2104 | |
| 2105 | |
| 2106 | |
| 2107 | //----------------------------------------------------------------------------- |
| 2108 | // name: Gain.db( float db ) |
| 2109 | // desc: Set Gain gain (in dB) |
| 2110 | //----------------------------------------------------------------------------- |
| 2111 | CK_DLL_MFUN( gain_set_db ) |
| 2112 | { |
| 2113 | // get ugen |
| 2114 | Chuck_UGen * ugen = (Chuck_UGen *)SELF; |
| 2115 | // set from constructor arg |
| 2116 | t_CKFLOAT gain = GET_NEXT_FLOAT(ARGS); |
| 2117 | ugen->m_gain = pow(10.0, gain / 20.0); // dB power scaling |
| 2118 | |
| 2119 | RETURN->v_float = gain; |
| 2120 | } |
| 2121 | |
| 2122 | |
| 2123 | |
| 2124 | |
| 2125 | //----------------------------------------------------------------------------- |
| 2126 | // name: Gain.gain() |
| 2127 | // desc: Get Gain gain (in dB) |
| 2128 | //----------------------------------------------------------------------------- |
| 2129 | CK_DLL_MFUN( gain_get_db ) |
| 2130 | { |
| 2131 | // get ugen |
| 2132 | Chuck_UGen * ugen = (Chuck_UGen *)SELF; |
| 2133 | // set from constructor arg |
| 2134 | t_CKFLOAT dB = 20 * log10(ugen->m_gain); // dB power scaling |
| 2135 | |
| 2136 | RETURN->v_float = dB; |
| 2137 | } |
| 2138 | |
| 2139 | |
| 2140 | |
| 2141 | |
| 2142 | //----------------------------------------------------------------------------- |
| 2143 | // name: GainDB( float gain ) |
| 2144 | // desc: GainDB constructor that takes a dB float |
| 2145 | //----------------------------------------------------------------------------- |
| 2146 | CK_DLL_CTOR( gainDB_ctor ) |
| 2147 | { |
| 2148 | // get ugen |
| 2149 | Chuck_UGen * ugen = (Chuck_UGen *)SELF; |
| 2150 | // set from constructor arg |
| 2151 | t_CKFLOAT gain = GET_NEXT_FLOAT(ARGS); |
| 2152 | ugen->m_gain = pow(10.0, gain / 20.0); // dB power scaling |
| 2153 | } |
| 2154 | |
| 2155 | |
| 2156 | |
| 2157 | |
| 2158 | //----------------------------------------------------------------------------- |
| 2159 | // name: pan2_ctor() |
| 2160 | // desc: ... |
| 2161 | //----------------------------------------------------------------------------- |
| 2162 | CK_DLL_CTOR( pan2_ctor ) |
| 2163 | { |
| 2164 | // get ugen |
| 2165 | Chuck_UGen * ugen = (Chuck_UGen *)SELF; |
| 2166 | |
| 2167 | // default panning law to constant power |
| 2168 | OBJ_MEMBER_INT(SELF, stereo_offset_panType) = PAN_EQ_POWER; |
| 2169 | |
| 2170 | // do the pan |
| 2171 | pan_this( ugen->m_multi_chan[0], ugen->m_multi_chan[1], |
| 2172 | OBJ_MEMBER_FLOAT(SELF, stereo_offset_pan), |
| 2173 | OBJ_MEMBER_INT(SELF, stereo_offset_panType) ); |
| 2174 | |
| 2175 | } |
| 2176 | |
| 2177 | |
| 2178 | |
| 2179 | |
| 2180 | //----------------------------------------------------------------------------- |
| 2181 | // name: noise_tick() |
| 2182 | // desc: ... |
| 2183 | //----------------------------------------------------------------------------- |
| 2184 | CK_DLL_TICK( noise_tick ) |
| 2185 | { |
| 2186 | // 1.5.0.4 (ge) updated to use ck_random_f() |
| 2187 | *out = (SAMPLE)( -1.0 + 2.0 * ck_random_f() ); |
| 2188 | return TRUE; |
| 2189 | } |
| 2190 | |
| 2191 | |
| 2192 | enum { NOISE_WHITE=0, NOISE_PINK, NOISE_BROWN, NOISE_FBM, NOISE_FLIP, NOISE_XOR }; |
| 2193 | |
| 2194 | class CNoise_Data |
| 2195 | { |
| 2196 | private: |
| 2197 | SAMPLE value; |
| 2198 | |
| 2199 | t_CKFLOAT fbmH; |
| 2200 | t_CKINT counter; |
| 2201 | t_CKINT * pink_array; |
| 2202 | t_CKINT pink_depth; |
| 2203 | bool pink_rand; |
| 2204 | t_CKINT rand_bits; |
| 2205 | double scale; |
| 2206 | double bias; |
| 2207 | |
| 2208 | t_CKINT last; |
| 2209 | |
| 2210 | public: |
| 2211 | CNoise_Data() { |
| 2212 | value = 0; |
| 2213 | mode = NOISE_PINK; |
| 2214 | pink_depth = 24; |
| 2215 | pink_array = NULL; |
| 2216 | counter = 1; |
| 2217 | scale = 2.0 / (double) CK_RANDOM_MAX ; |
| 2218 | bias = -1.0; // 1.5.0.1 (anthonyhawes) changed from 0.0, removing a DC bias |
| 2219 | pink_rand = false; |
| 2220 | t_CKINT randt = CK_RANDOM_MAX; |
| 2221 | rand_bits = 0; |
| 2222 | fprob = (t_CKINT)(CK_RANDOM_MAX/32.0); |
| 2223 | while ( randt > 0 ) { |
| 2224 | rand_bits++; |
| 2225 | randt = randt >> 1; |
| 2226 | } |
| 2227 | // CK_FPRINTF_STDERR( "random bits - %d", rand_bits ); |
| 2228 | setMode ( "pink" ); |
| 2229 | } |
| 2230 | ~CNoise_Data() { |
| 2231 | // 1.5.0.1 (anthonyhawes) added |
| 2232 | CK_SAFE_FREE( pink_array ); |
| 2233 | } |
| 2234 | |
| 2235 | t_CKINT fprob; |
| 2236 | t_CKUINT mode; |
| 2237 | void tick( t_CKTIME now, SAMPLE * out ); |
| 2238 | void setMode( const std::string & mode ); |
| 2239 | |
| 2240 | t_CKINT pink_tick( SAMPLE * out); |
| 2241 | t_CKINT brown_tick( SAMPLE * out); |
| 2242 | t_CKINT xor_tick( SAMPLE * out); |
| 2243 | t_CKINT flip_tick( SAMPLE * out); |
| 2244 | t_CKINT fbm_tick( SAMPLE * out); |
| 2245 | }; |
| 2246 | |
| 2247 | |
| 2248 | // pre-constructor |
| 2249 | CK_DLL_CTOR( cnoise_ctor ) |
| 2250 | { |
| 2251 | // create cnoise internal object |
| 2252 | OBJ_MEMBER_UINT(SELF, cnoise_offset_data) = (t_CKUINT)new CNoise_Data; |
| 2253 | } |
| 2254 | |
| 2255 | CK_DLL_CTOR( cnoise_ctor_mode ) |
| 2256 | { |
| 2257 | // 1.5.5.6 | internal object should be already created in pre-ctor |
| 2258 | CNoise_Data * n = (CNoise_Data *)OBJ_MEMBER_UINT(SELF, cnoise_offset_data); |
| 2259 | // get arg |
| 2260 | Chuck_String * ckstr = GET_NEXT_STRING(ARGS); |
| 2261 | // set |
| 2262 | n->setMode( ckstr ? ckstr->str() : "" ); |
| 2263 | } |
| 2264 | |
| 2265 | |
| 2266 | CK_DLL_DTOR( cnoise_dtor ) |
| 2267 | { |
| 2268 | delete (CNoise_Data *)OBJ_MEMBER_UINT(SELF, cnoise_offset_data); |
| 2269 | OBJ_MEMBER_UINT(SELF, cnoise_offset_data) = 0; |
| 2270 | } |
| 2271 | |
| 2272 | CK_DLL_TICK( cnoise_tick ) |
| 2273 | { |
| 2274 | CNoise_Data * d = ( CNoise_Data * )OBJ_MEMBER_UINT(SELF, cnoise_offset_data); |
| 2275 | switch( d->mode ) { |
| 2276 | case NOISE_WHITE: |
| 2277 | return noise_tick(SELF,in,out,API); |
| 2278 | break; |
| 2279 | case NOISE_PINK: |
| 2280 | return d->pink_tick(out); |
| 2281 | break; |
| 2282 | case NOISE_BROWN: |
| 2283 | return d->brown_tick(out); |
| 2284 | break; |
| 2285 | case NOISE_XOR: |
| 2286 | return d->xor_tick(out); |
| 2287 | break; |
| 2288 | case NOISE_FLIP: |
| 2289 | return d->flip_tick(out); |
| 2290 | break; |
| 2291 | case NOISE_FBM: |
| 2292 | return d->fbm_tick(out); |
| 2293 | break; |
| 2294 | } |
| 2295 | return TRUE; |
| 2296 | } |
| 2297 | |
| 2298 | t_CKINT CNoise_Data::pink_tick( SAMPLE * out ) |
| 2299 | { |
| 2300 | //based on Voss-McCartney |
| 2301 | |
| 2302 | if ( pink_array == NULL ) { |
| 2303 | pink_array = (t_CKINT *) malloc ( sizeof ( t_CKINT ) * pink_depth ); |
| 2304 | last = 0; |
| 2305 | // 1.5.0.1 (ge) updated to ck_random() |
| 2306 | for ( t_CKINT i = 0 ; i < pink_depth ; i++ ) |
| 2307 | { pink_array[i] = ck_random(); last += pink_array[i]; } |
| 2308 | scale = 2.0 / ((double)CK_RANDOM_MAX * ( pink_depth + 1.0 ) ); |
| 2309 | bias = -1.0; |
| 2310 | // CK_FPRINTF_STDERR( "scale %f %f %d %d \n", scale, bias, CK_RANDOM_MAX, pink_depth + 1 ); |
| 2311 | } |
| 2312 | |
| 2313 | t_CKINT pind = 0; |
| 2314 | |
| 2315 | //count trailing zeroes |
| 2316 | while ( pind < pink_depth && ! (counter & ( ((t_CKINT)1) << pind ) ) ) pind++; |
| 2317 | |
| 2318 | // fprintf (stderr, "counter %d pink - %d \n", counter, pind ); |
| 2319 | |
| 2320 | if ( pind < pink_depth ) { |
| 2321 | t_CKINT diff = ck_random() - pink_array[pind]; |
| 2322 | pink_array[pind] += diff; |
| 2323 | last += diff; |
| 2324 | } |
| 2325 | |
| 2326 | *out = bias + scale * ( ck_random() + last ); |
| 2327 | counter++; |
| 2328 | if ( pink_rand ) counter = ck_random(); |
| 2329 | return TRUE; |
| 2330 | } |
| 2331 | |
| 2332 | t_CKINT CNoise_Data::xor_tick( SAMPLE * out ) |
| 2333 | { |
| 2334 | t_CKINT mask = 0; |
| 2335 | for ( t_CKINT i = 0; i < rand_bits ; i++ ) |
| 2336 | if ( ck_random() <= fprob ) |
| 2337 | mask |= ( ((t_CKINT)1) << i ); |
| 2338 | last = last ^ mask; |
| 2339 | *out = bias + scale * (SAMPLE)last; |
| 2340 | return TRUE; |
| 2341 | } |
| 2342 | |
| 2343 | t_CKINT CNoise_Data::flip_tick( SAMPLE * out ) |
| 2344 | { |
| 2345 | t_CKINT ind = (t_CKINT)( (double)rand_bits * ck_random() / ( CK_RANDOM_MAX + 1.0 ) ); |
| 2346 | last = last ^ ( ((t_CKINT)1) << ind ); |
| 2347 | // fprintf ( stderr, "ind - %d %d %f %f", ind, last, bias, scale ); |
| 2348 | *out = bias + scale * (SAMPLE)last; |
| 2349 | return TRUE; |
| 2350 | } |
| 2351 | |
| 2352 | t_CKINT |
| 2353 | CNoise_Data::brown_tick( SAMPLE * out ) { |
| 2354 | //brownian noise function..later! |
| 2355 | *out = 0; |
| 2356 | return TRUE; |
| 2357 | } |
| 2358 | |
| 2359 | t_CKINT |
| 2360 | CNoise_Data::fbm_tick( SAMPLE * out ) { |
| 2361 | //non-brownian noise function..later! |
| 2362 | *out = 0; |
| 2363 | return TRUE; |
| 2364 | } |
| 2365 | |
| 2366 | void |
| 2367 | CNoise_Data::setMode( const std::string & name ) { |
| 2368 | // process |
| 2369 | std::string str = tolower(trim(name)); |
| 2370 | // c string (pointer valid only in this function) |
| 2371 | const char * c = str.c_str(); |
| 2372 | |
| 2373 | if ( strcmp ( c, "white" ) == 0 ) { |
| 2374 | // CK_FPRINTF_STDERR( "white noise\n" ); |
| 2375 | mode = NOISE_WHITE; |
| 2376 | scale = 2.0 / (t_CKFLOAT)CK_RANDOM_MAX; |
| 2377 | bias = -1.0; |
| 2378 | } |
| 2379 | else if ( strcmp ( c, "pink" ) == 0 ) { |
| 2380 | // CK_FPRINTF_STDERR( "pink noise\n" ); |
| 2381 | mode = NOISE_PINK; |
| 2382 | scale = 2.0 / (double)(CK_RANDOM_MAX * ( pink_depth + 1 ) ); |
| 2383 | bias = -1.0; |
| 2384 | } |
| 2385 | else if ( strcmp ( c, "flip" ) == 0) { |
| 2386 | // CK_FPRINTF_STDERR( "bitflip noise\n" ); |
| 2387 | mode = NOISE_FLIP; |
| 2388 | scale = 2.0 / (t_CKFLOAT)CK_RANDOM_MAX; |
| 2389 | bias = -1.0; |
| 2390 | } |
| 2391 | else if ( strcmp ( c, "xor" ) == 0) { |
| 2392 | // CK_FPRINTF_STDERR( "xor noise\n" ); |
| 2393 | mode = NOISE_XOR; |
| 2394 | scale = 2.0 / (t_CKFLOAT)CK_RANDOM_MAX; |
| 2395 | bias = -1.0; |
| 2396 | } |
| 2397 | else if ( strcmp ( c, "brown" ) == 0) { |
| 2398 | // CK_FPRINTF_STDERR( "brownian noise\n" ); |
| 2399 | mode = NOISE_BROWN; |
| 2400 | scale = 2.0 / (t_CKFLOAT)CK_RANDOM_MAX; |
| 2401 | bias = -1.0; |
| 2402 | } |
| 2403 | else if ( strcmp ( c, "fbm" ) == 0) { |
| 2404 | // CK_FPRINTF_STDERR( "fbm noise\n" ); |
| 2405 | mode = NOISE_FBM; |
| 2406 | scale = 2.0 / (t_CKFLOAT)CK_RANDOM_MAX; |
| 2407 | bias = -1.0; |
| 2408 | } |
| 2409 | else |
| 2410 | { |
| 2411 | // default pink |
| 2412 | mode = NOISE_PINK; |
| 2413 | // error message if mode string non-empty |
| 2414 | if( strlen(c) > 0 ) |
| 2415 | EM_error2b( 0, "unrecognized CNoise mode '%s'; defaulting to 'pink'...", c ); |
| 2416 | } |
| 2417 | } |
| 2418 | |
| 2419 | CK_DLL_CTRL( cnoise_ctrl_mode ) |
| 2420 | { |
| 2421 | CNoise_Data * d = ( CNoise_Data * )OBJ_MEMBER_UINT(SELF, cnoise_offset_data); |
| 2422 | // get arg |
| 2423 | Chuck_String * ckstr = GET_NEXT_STRING(ARGS); |
| 2424 | // set |
| 2425 | d->setMode( ckstr ? ckstr->str() : "" ); |
| 2426 | // pass through |
| 2427 | RETURN->v_string = ckstr; |
| 2428 | } |
| 2429 | |
| 2430 | CK_DLL_CTRL( cnoise_ctrl_fprob ) |
| 2431 | { |
| 2432 | CNoise_Data * d = (CNoise_Data *)OBJ_MEMBER_UINT(SELF, cnoise_offset_data); |
| 2433 | t_CKFLOAT p = GET_CK_FLOAT(ARGS); |
| 2434 | d->fprob = (t_CKINT)(p*CK_RANDOM_MAX); |
| 2435 | RETURN->v_float = p; |
| 2436 | } |
| 2437 | |
| 2438 | |
| 2439 | |
| 2440 | //----------------------------------------------------------------------------- |
| 2441 | // name: struct Pulse_Data |
| 2442 | // desc: ... |
| 2443 | //----------------------------------------------------------------------------- |
| 2444 | struct Pulse_Data |
| 2445 | { |
| 2446 | SAMPLE value; |
| 2447 | t_CKUINT when; |
| 2448 | Pulse_Data( ) { value = 1.0f; when = 0; } |
| 2449 | }; |
| 2450 | |
| 2451 | |
| 2452 | //----------------------------------------------------------------------------- |
| 2453 | // name: impulse_ctor() |
| 2454 | // desc: ... |
| 2455 | //----------------------------------------------------------------------------- |
| 2456 | CK_DLL_CTOR( impulse_ctor ) |
| 2457 | { |
| 2458 | // return data to be used later |
| 2459 | OBJ_MEMBER_UINT(SELF, impulse_offset_data) = (t_CKUINT)new Pulse_Data; |
| 2460 | } |
| 2461 | |
| 2462 | |
| 2463 | |
| 2464 | |
| 2465 | //----------------------------------------------------------------------------- |
| 2466 | // name: impulse_dtor() |
| 2467 | // desc: ... |
| 2468 | //----------------------------------------------------------------------------- |
| 2469 | CK_DLL_DTOR( impulse_dtor ) |
| 2470 | { |
| 2471 | // delete |
| 2472 | delete (Pulse_Data *)OBJ_MEMBER_UINT(SELF, impulse_offset_data); |
| 2473 | OBJ_MEMBER_UINT(SELF, impulse_offset_data) = 0; |
| 2474 | } |
| 2475 | |
| 2476 | |
| 2477 | |
| 2478 | |
| 2479 | //----------------------------------------------------------------------------- |
| 2480 | // name: impulse_tick() |
| 2481 | // desc: ... |
| 2482 | //----------------------------------------------------------------------------- |
| 2483 | CK_DLL_TICK( impulse_tick ) |
| 2484 | { |
| 2485 | Pulse_Data * d = (Pulse_Data *)OBJ_MEMBER_UINT(SELF, impulse_offset_data); |
| 2486 | if( d->when ) |
| 2487 | { |
| 2488 | *out = d->value; |
| 2489 | d->when = 0; |
| 2490 | } |
| 2491 | else |
| 2492 | *out = 0.0f; |
| 2493 | |
| 2494 | return TRUE; |
| 2495 | } |
| 2496 | |
| 2497 | |
| 2498 | |
| 2499 | //----------------------------------------------------------------------------- |
| 2500 | // name: impulse_ctrl_next() |
| 2501 | // desc: ... |
| 2502 | //----------------------------------------------------------------------------- |
| 2503 | CK_DLL_CTRL( impulse_ctrl_next ) |
| 2504 | { |
| 2505 | Pulse_Data * d = (Pulse_Data *)OBJ_MEMBER_UINT(SELF, impulse_offset_data); |
| 2506 | d->value = (SAMPLE)GET_CK_FLOAT(ARGS); |
| 2507 | d->when = 1; |
| 2508 | RETURN->v_float = (t_CKFLOAT)d->value; |
| 2509 | } |
| 2510 | |
| 2511 | |
| 2512 | |
| 2513 | |
| 2514 | //----------------------------------------------------------------------------- |
| 2515 | // name: impulse_cget_next() |
| 2516 | // desc: ... |
| 2517 | //----------------------------------------------------------------------------- |
| 2518 | CK_DLL_CGET( impulse_cget_next ) |
| 2519 | { |
| 2520 | Pulse_Data * d = (Pulse_Data *)OBJ_MEMBER_UINT(SELF, impulse_offset_data); |
| 2521 | RETURN->v_float = (t_CKFLOAT)d->value; |
| 2522 | } |
| 2523 | |
| 2524 | |
| 2525 | |
| 2526 | |
| 2527 | //----------------------------------------------------------------------------- |
| 2528 | // name: step_ctor() |
| 2529 | // desc: base/pre constructor (always run) |
| 2530 | //----------------------------------------------------------------------------- |
| 2531 | CK_DLL_CTOR( step_ctor ) |
| 2532 | { |
| 2533 | // return data to be used later |
| 2534 | OBJ_MEMBER_UINT(SELF, step_offset_data) = (t_CKUINT)new SAMPLE( 1.0f ); |
| 2535 | } |
| 2536 | |
| 2537 | |
| 2538 | //----------------------------------------------------------------------------- |
| 2539 | // name: step_dtor() |
| 2540 | // desc: ... |
| 2541 | //----------------------------------------------------------------------------- |
| 2542 | CK_DLL_DTOR( step_dtor ) |
| 2543 | { |
| 2544 | // delete |
| 2545 | delete (SAMPLE *)OBJ_MEMBER_UINT(SELF, step_offset_data); |
| 2546 | OBJ_MEMBER_UINT(SELF, step_offset_data) = 0; |
| 2547 | } |
| 2548 | |
| 2549 | |
| 2550 | //----------------------------------------------------------------------------- |
| 2551 | // name: step_ctor_value() |
| 2552 | // desc: overloaded constructor |
| 2553 | //----------------------------------------------------------------------------- |
| 2554 | CK_DLL_CTOR( step_ctor_value ) |
| 2555 | { |
| 2556 | // NOTE internal state should be already allocated in pre-ctor |
| 2557 | |
| 2558 | // call next() to set the value |
| 2559 | Chuck_DL_Return RETURN; |
| 2560 | step_ctrl_next( SELF, ARGS, &RETURN, VM, SHRED, API ); |
| 2561 | } |
| 2562 | |
| 2563 | |
| 2564 | //----------------------------------------------------------------------------- |
| 2565 | // name: step_tick() |
| 2566 | // desc: ... |
| 2567 | //----------------------------------------------------------------------------- |
| 2568 | CK_DLL_TICK( step_tick ) |
| 2569 | { |
| 2570 | SAMPLE * d = (SAMPLE *)OBJ_MEMBER_UINT(SELF, step_offset_data); |
| 2571 | *out = *d; |
| 2572 | |
| 2573 | return TRUE; |
| 2574 | } |
| 2575 | |
| 2576 | |
| 2577 | //----------------------------------------------------------------------------- |
| 2578 | // name: step_ctrl_next() |
| 2579 | // desc: ... |
| 2580 | //----------------------------------------------------------------------------- |
| 2581 | CK_DLL_CTRL( step_ctrl_next ) |
| 2582 | { |
| 2583 | SAMPLE * d = (SAMPLE *)OBJ_MEMBER_UINT(SELF, step_offset_data); |
| 2584 | *d = (SAMPLE)GET_CK_FLOAT(ARGS); |
| 2585 | RETURN->v_float = (t_CKFLOAT)(*d); |
| 2586 | } |
| 2587 | |
| 2588 | |
| 2589 | //----------------------------------------------------------------------------- |
| 2590 | // name: step_cget_next() |
| 2591 | // desc: ... |
| 2592 | //----------------------------------------------------------------------------- |
| 2593 | CK_DLL_CGET( step_cget_next ) |
| 2594 | { |
| 2595 | SAMPLE * d = (SAMPLE *)OBJ_MEMBER_UINT(SELF, step_offset_data); |
| 2596 | RETURN->v_float = (t_CKFLOAT)(*d); |
| 2597 | } |
| 2598 | |
| 2599 | |
| 2600 | |
| 2601 | |
| 2602 | //----------------------------------------------------------------------------- |
| 2603 | // name: halfrect_tick() |
| 2604 | // desc: ... |
| 2605 | //----------------------------------------------------------------------------- |
| 2606 | CK_DLL_TICK( halfrect_tick ) |
| 2607 | { |
| 2608 | *out = in > 0.0f ? in : 0.0f; |
| 2609 | return TRUE; |
| 2610 | } |
| 2611 | |
| 2612 | |
| 2613 | |
| 2614 | |
| 2615 | //----------------------------------------------------------------------------- |
| 2616 | // name: fullrect_tick() |
| 2617 | // desc: ... |
| 2618 | //----------------------------------------------------------------------------- |
| 2619 | CK_DLL_TICK( fullrect_tick ) |
| 2620 | { |
| 2621 | *out = in >= 0.0f ? in : -in; |
| 2622 | return TRUE; |
| 2623 | } |
| 2624 | |
| 2625 | |
| 2626 | |
| 2627 | |
| 2628 | /* |
| 2629 | //----------------------------------------------------------------------------- |
| 2630 | // name: zerox_ctor() |
| 2631 | // desc: ... |
| 2632 | //----------------------------------------------------------------------------- |
| 2633 | CK_DLL_CTOR( zerox_ctor ) |
| 2634 | { |
| 2635 | OBJ_MEMBER_UINT(SELF, zerox_offset_data) = (t_CKUINT)new SAMPLE( 0.0f ); |
| 2636 | } |
| 2637 | |
| 2638 | |
| 2639 | //----------------------------------------------------------------------------- |
| 2640 | // name: zerox_dtor() |
| 2641 | // desc: ... |
| 2642 | //----------------------------------------------------------------------------- |
| 2643 | CK_DLL_DTOR( zerox_dtor ) |
| 2644 | { |
| 2645 | delete (SAMPLE *)OBJ_MEMBER_UINT(SELF, zerox_offset_data); |
| 2646 | OBJ_MEMBER_UINT(SELF, zerox_offset_data) = 0; |
| 2647 | } |
| 2648 | |
| 2649 | |
| 2650 | #define __SGN(x) (x >= 0.0f ? 1.0f : -1.0f ) |
| 2651 | //----------------------------------------------------------------------------- |
| 2652 | // name: zerox_tick() |
| 2653 | // desc: ... |
| 2654 | //----------------------------------------------------------------------------- |
| 2655 | CK_DLL_TICK( zerox_tick ) |
| 2656 | { |
| 2657 | SAMPLE * d = (SAMPLE *)OBJ_MEMBER_UINT(SELF, zerox_offset_data); |
| 2658 | *out = __SGN(in) != __SGN(*d); |
| 2659 | *out *= __SGN(in); |
| 2660 | *d = in; |
| 2661 | |
| 2662 | return TRUE; |
| 2663 | } |
| 2664 | */ |
| 2665 | |
| 2666 | |
| 2667 | |
| 2668 | |
| 2669 | struct delayp_data |
| 2670 | { |
| 2671 | |
| 2672 | SAMPLE * buffer; |
| 2673 | t_CKINT bufsize; |
| 2674 | |
| 2675 | t_CKTIME now; |
| 2676 | |
| 2677 | double readpos; //readpos ( moves at constant speed, sample per sample |
| 2678 | double writepos; // relative to read position |
| 2679 | |
| 2680 | t_CKTIME offset; // distance between read and write |
| 2681 | |
| 2682 | t_CKDUR offset_start; |
| 2683 | t_CKDUR offset_target; |
| 2684 | |
| 2685 | t_CKTIME move_end_time; //target time |
| 2686 | t_CKDUR move_duration; //time we started shift |
| 2687 | |
| 2688 | SAMPLE last_sample; |
| 2689 | t_CKDUR last_offset; |
| 2690 | |
| 2691 | #ifdef _DEBUG |
| 2692 | int lasti; |
| 2693 | #endif |
| 2694 | |
| 2695 | double acoeff[2]; |
| 2696 | double bcoeff[2]; |
| 2697 | SAMPLE outputs[3]; |
| 2698 | SAMPLE inputs[3]; |
| 2699 | |
| 2700 | delayp_data() |
| 2701 | { |
| 2702 | bufsize = 2 * g_srateXxx; |
| 2703 | buffer = ( SAMPLE * ) realloc ( NULL, sizeof ( SAMPLE ) * bufsize ); |
| 2704 | t_CKINT i; |
| 2705 | |
| 2706 | for ( i = 0 ; i < bufsize ; i++ ) buffer[i] = 0; |
| 2707 | for ( i = 0 ; i < 2 ; i++ ) { acoeff[i] = 0; bcoeff[i] = 0; } |
| 2708 | |
| 2709 | acoeff[0] = 1.0; |
| 2710 | acoeff[1] = -.99; |
| 2711 | bcoeff[0] = 1.0; |
| 2712 | bcoeff[1] = -1.0; |
| 2713 | |
| 2714 | readpos = 0.0; |
| 2715 | |
| 2716 | now = 0.0; |
| 2717 | |
| 2718 | offset = 0.0; |
| 2719 | last_offset = 0.0; |
| 2720 | offset_start = 0.0; |
| 2721 | offset_target = 0.0; |
| 2722 | |
| 2723 | move_duration = 1.0; |
| 2724 | move_end_time = 0.0; |
| 2725 | |
| 2726 | #ifdef _DEBUG |
| 2727 | lasti = -1; |
| 2728 | #endif |
| 2729 | last_sample = 0; |
| 2730 | } |
| 2731 | |
| 2732 | ~delayp_data() |
| 2733 | { |
| 2734 | CK_SAFE_DELETE_ARRAY( buffer ); |
| 2735 | } |
| 2736 | }; |
| 2737 | |
| 2738 | CK_DLL_CTOR( delayp_ctor ) |
| 2739 | { |
| 2740 | OBJ_MEMBER_UINT(SELF, delayp_offset_data) = (t_CKUINT)new delayp_data; |
| 2741 | } |
| 2742 | |
| 2743 | CK_DLL_DTOR( delayp_dtor ) |
| 2744 | { |
| 2745 | delayp_data * d = (delayp_data *)OBJ_MEMBER_UINT(SELF, delayp_offset_data); |
| 2746 | CK_SAFE_DELETE(d); |
| 2747 | OBJ_MEMBER_UINT(SELF, delayp_offset_data) = 0; |
| 2748 | } |
| 2749 | |
| 2750 | CK_DLL_PMSG( delayp_pmsg ) |
| 2751 | { |
| 2752 | return TRUE; |
| 2753 | } |
| 2754 | |
| 2755 | CK_DLL_TICK( delayp_tick ) |
| 2756 | { |
| 2757 | delayp_data * d = (delayp_data *)OBJ_MEMBER_UINT(SELF, delayp_offset_data); |
| 2758 | if ( !d->buffer ) return FALSE; |
| 2759 | |
| 2760 | // area |
| 2761 | d->now = ((Chuck_UGen*)SELF)->originVM()->shreduler()->now_system; // 1.5.1.5 |
| 2762 | // d->now = ((Chuck_UGen*)SELF)->originShred()->vm_ref->shreduler()->now_system; |
| 2763 | |
| 2764 | //calculate new write-offset position ( we interpolate if we've been assigned a new write-offset ) |
| 2765 | if ( d->now >= d->move_end_time || d->move_duration == 0 ) d->offset = d->offset_target; |
| 2766 | else { |
| 2767 | double dt = 1.0 + ( d->now - d->move_end_time ) / ( d->move_duration ); |
| 2768 | d->offset = d->offset_start + dt * ( d->offset_target - d->offset_start ); |
| 2769 | // fprintf (stderr, "dt %f, off %f , start %f target %f\n", dt, d->writeoff, d->writeoff_start, d->writeoff_target ); |
| 2770 | } |
| 2771 | |
| 2772 | //find locations in buffer... |
| 2773 | double write = (d->readpos ) + d->offset; |
| 2774 | double last_write = (d->readpos - 1.0) + d->last_offset ; |
| 2775 | |
| 2776 | //linear interpolation. will introduce some lowpass/aliasing. |
| 2777 | double write_delta = write - last_write; |
| 2778 | double sample_delta = in - d->last_sample; |
| 2779 | |
| 2780 | double duck_constant = 0.69; |
| 2781 | |
| 2782 | |
| 2783 | double gee = fabs(write_delta) - 1.0; |
| 2784 | |
| 2785 | if ( gee < 24.0 ) { |
| 2786 | double head_contact = ( gee > 0 ) ? exp ( - duck_constant * gee ) : 1.0; |
| 2787 | t_CKINT i, smin, smax, sampi; |
| 2788 | SAMPLE sampf = 0; |
| 2789 | if ( write_delta >= 0 ) { //forward. |
| 2790 | smin = (t_CKINT) floor ( last_write ); |
| 2791 | smax = (t_CKINT) floor ( write ); |
| 2792 | for ( i = smin+1 ; i <= smax ; i++ ) { |
| 2793 | sampf = d->last_sample + sample_delta * ( double(i) - last_write ) / write_delta; |
| 2794 | sampi = ( i + d->bufsize * 2 ) % d->bufsize; |
| 2795 | #ifdef _DEBUG |
| 2796 | if ( d->lasti == sampi ) { |
| 2797 | CK_FPRINTF_STDERR( "[chuck](via Curve): over!\n"); |
| 2798 | } |
| 2799 | d->lasti = sampi; |
| 2800 | #endif |
| 2801 | d->buffer[sampi] += sampf * head_contact ; |
| 2802 | } |
| 2803 | } |
| 2804 | else { //moving in reverse |
| 2805 | smin = (t_CKINT) floor ( write ); |
| 2806 | smax = (t_CKINT) floor ( last_write ); |
| 2807 | for ( i = smin+1 ; i <= smax ; i++ ) { |
| 2808 | sampf = d->last_sample + sample_delta * ( double(i) - last_write ) / write_delta; |
| 2809 | sampi = ( i + d->bufsize * 2 ) % d->bufsize; |
| 2810 | #ifdef _DEBUG |
| 2811 | if ( d->lasti == sampi ) { |
| 2812 | CK_FPRINTF_STDERR( "[chuck](via Curve): over!\n" ); |
| 2813 | } |
| 2814 | d->lasti = sampi; |
| 2815 | #endif |
| 2816 | d->buffer[sampi] += sampf * head_contact; |
| 2817 | } |
| 2818 | } |
| 2819 | } |
| 2820 | |
| 2821 | d->last_offset = d->offset; |
| 2822 | d->last_sample = in; |
| 2823 | |
| 2824 | |
| 2825 | //output should go through a dc blocking filter, for cases where |
| 2826 | //we are zipping around in the buffer leaving a fairly constant |
| 2827 | //trail of samples |
| 2828 | |
| 2829 | //output last sample |
| 2830 | |
| 2831 | t_CKINT rpos = (t_CKINT) fmod( d->readpos, d->bufsize ) ; |
| 2832 | |
| 2833 | // *out = d->buffer[rpos]; |
| 2834 | |
| 2835 | /* |
| 2836 | // did i try to write a dc blocking filter? |
| 2837 | d->outputs[0] = 0.0; |
| 2838 | d->inputs [0] = d->buffer[rpos]; |
| 2839 | |
| 2840 | d->outputs[0] += d->bcoeff[1] * d->inputs[1]; |
| 2841 | d->inputs [1] = d->inputs[0]; |
| 2842 | |
| 2843 | d->outputs[0] += d->bcoeff[0] * d->inputs[0]; |
| 2844 | |
| 2845 | d->outputs[0] += -d->acoeff[1] * d->outputs[1]; |
| 2846 | d->outputs[1] = d->outputs[0]; |
| 2847 | |
| 2848 | //clear at readpos ( write doesn't !) |
| 2849 | *out = d->outputs[0]; |
| 2850 | */ |
| 2851 | |
| 2852 | *out = d->buffer[rpos]; |
| 2853 | |
| 2854 | d->buffer[rpos] = 0; //clear once it's been read |
| 2855 | d->readpos = fmod ( d->readpos + 1.0 , double( d->bufsize ) ); |
| 2856 | |
| 2857 | return TRUE; |
| 2858 | |
| 2859 | } |
| 2860 | |
| 2861 | CK_DLL_CTRL( delayp_ctrl_delay ) |
| 2862 | { |
| 2863 | delayp_data * d = ( delayp_data * ) OBJ_MEMBER_UINT(SELF, delayp_offset_data); |
| 2864 | t_CKDUR target = GET_CK_DUR(ARGS); // rate |
| 2865 | // area |
| 2866 | |
| 2867 | if ( target != d->offset_target ) { |
| 2868 | if ( target > d->bufsize ) { |
| 2869 | CK_FPRINTF_STDERR( "[chuck](via delayp): delay time %f over max! set max first!\n", target); |
| 2870 | return; |
| 2871 | } |
| 2872 | d->offset_target = target; |
| 2873 | d->offset_start = d->last_offset; |
| 2874 | |
| 2875 | t_CKTIME snow = SHRED->now; // 1.5.1.5 was: ((Chuck_UGen*)SELF)->originShred()->now; |
| 2876 | d->move_end_time = snow + d->move_duration; |
| 2877 | } |
| 2878 | RETURN->v_dur = d->last_offset; // TODO: |
| 2879 | } |
| 2880 | |
| 2881 | CK_DLL_CGET( delayp_cget_delay ) |
| 2882 | { |
| 2883 | delayp_data * d = ( delayp_data * ) OBJ_MEMBER_UINT(SELF, delayp_offset_data); |
| 2884 | //SET_NEXT_DUR( out, d->writeoff_last ); |
| 2885 | RETURN->v_dur = d->last_offset; // TODO: |
| 2886 | } |
| 2887 | |
| 2888 | CK_DLL_CTRL( delayp_ctrl_window ) |
| 2889 | { |
| 2890 | delayp_data * d = ( delayp_data * ) OBJ_MEMBER_UINT(SELF, delayp_offset_data); |
| 2891 | t_CKDUR window = GET_CK_DUR(ARGS); // rate |
| 2892 | if ( window >= 0 ) { |
| 2893 | d->move_duration = window; |
| 2894 | //fprintf ( stderr, "set window time %f , %f , %d \n", d->writeoff_window_time, d->writeoff, d->bufsize ); |
| 2895 | } |
| 2896 | RETURN->v_dur = d->move_duration; // TODO: |
| 2897 | } |
| 2898 | |
| 2899 | CK_DLL_CGET( delayp_cget_window ) |
| 2900 | { |
| 2901 | delayp_data * d = ( delayp_data * ) OBJ_MEMBER_UINT(SELF, delayp_offset_data); |
| 2902 | RETURN->v_dur = d->move_duration; // TODO: |
| 2903 | } |
| 2904 | |
| 2905 | |
| 2906 | CK_DLL_CTRL( delayp_ctrl_max ) |
| 2907 | { |
| 2908 | delayp_data * d = ( delayp_data * ) OBJ_MEMBER_UINT(SELF, delayp_offset_data); |
| 2909 | t_CKDUR nmax = GET_CK_DUR(ARGS); // rate |
| 2910 | if ( d->bufsize != (t_CKINT)nmax && nmax > 1.0 ) { |
| 2911 | d->bufsize = (t_CKINT)(nmax+.5); |
| 2912 | d->buffer = ( SAMPLE * ) realloc ( d->buffer, sizeof ( SAMPLE ) * d->bufsize ); |
| 2913 | for ( t_CKINT i = 0; i < d->bufsize; i++ ) d->buffer[i] = 0; |
| 2914 | } |
| 2915 | RETURN->v_dur = d->bufsize; // TODO?? |
| 2916 | } |
| 2917 | |
| 2918 | CK_DLL_CGET( delayp_cget_max ) |
| 2919 | { |
| 2920 | delayp_data * d = ( delayp_data * ) OBJ_MEMBER_UINT(SELF, delayp_offset_data); |
| 2921 | //SET_NEXT_DUR( out, (t_CKDUR) d->bufsize ); |
| 2922 | RETURN->v_dur = d->bufsize; |
| 2923 | } |
| 2924 | |
| 2925 | |
| 2926 | #ifndef __DISABLE_SNDBUF__ |
| 2927 | |
| 2928 | //----------------------------------------------------------------------------- |
| 2929 | // name: sndbuf |
| 2930 | // desc: ... |
| 2931 | //----------------------------------------------------------------------------- |
| 2932 | enum { SNDBUF_DROP = 0, SNDBUF_INTERP, SNDBUF_SINC}; |
| 2933 | |
| 2934 | |
| 2935 | #define WIDTH 16 /* this controls the number of neighboring samples |
| 2936 | which are used to interpolate the new samples. The |
| 2937 | processing time is linearly related to this width */ |
| 2938 | #define DELAY_SIZE 140 |
| 2939 | |
| 2940 | // default chunk size |
| 2941 | #define CK_SNDBUF_DEFAULT_CHUNK_SIZE (32768) // a little less than 1s of 44.1kHz |
| 2942 | |
| 2943 | #define USE_TABLE TRUE /* this controls whether a linearly interpolated lookup |
| 2944 | table is used for sinc function calculation, or the |
| 2945 | sinc is calculated by floating point trig function calls. */ |
| 2946 | |
| 2947 | #define USE_INTERP TRUE /* this controls whether the table is linear |
| 2948 | interpolated or not. If you re not using the |
| 2949 | table, this has no effect */ |
| 2950 | |
| 2951 | #define SAMPLES_PER_ZERO_CROSSING 32 /* this defines how finely the sinc function |
| 2952 | is sampled for storage in the table */ |
| 2953 | |
| 2954 | #ifdef CK_SNDBUF_MEMORY_BUFFER |
| 2955 | //------------------------------------------------------------------------------ |
| 2956 | // name: MultiBuffer |
| 2957 | // desc: presents multiple buffers in memory as a single sequential logical |
| 2958 | // buffer, for constant time reallocing. tries to be as discreet as possible, |
| 2959 | // so you can basically use it like a pointer. optimized for iterative access, |
| 2960 | // as random access has T = O(bufsize) |
| 2961 | //------------------------------------------------------------------------------ |
| 2962 | template< class _type > |
| 2963 | class MultiBuffer |
| 2964 | { |
| 2965 | |
| 2966 | public: |
| 2967 | MultiBuffer() |
| 2968 | { |
| 2969 | m_size = 0; |
| 2970 | } |
| 2971 | |
| 2972 | ~MultiBuffer() |
| 2973 | { |
| 2974 | deallocate(); |
| 2975 | } |
| 2976 | |
| 2977 | size_t size() |
| 2978 | { |
| 2979 | return m_size; |
| 2980 | } |
| 2981 | |
| 2982 | void resize( size_t size ) |
| 2983 | { |
| 2984 | assert( size > m_size ); // for now |
| 2985 | size_t new_buffer_size = size - m_size; |
| 2986 | |
| 2987 | extent new_extent; |
| 2988 | new_extent.position = new _type[new_buffer_size]; |
| 2989 | new_extent.length = new_buffer_size; |
| 2990 | m_bufs.push_back( new_extent ); |
| 2991 | m_size = size; |
| 2992 | } |
| 2993 | |
| 2994 | void deallocate() |
| 2995 | { |
| 2996 | // delete everything |
| 2997 | size_t i, len = m_bufs.size(); |
| 2998 | for( i = 0; i < len; i++ ) |
| 2999 | delete[] m_bufs[i].position; |
| 3000 | m_size = 0; |
| 3001 | } |
| 3002 | |
| 3003 | class Pointer |
| 3004 | { |
| 3005 | public: |
| 3006 | Pointer() |
| 3007 | { |
| 3008 | m_mb = NULL; |
| 3009 | m_lpos = 0; |
| 3010 | m_extent = 0; |
| 3011 | m_extpos = 0; |
| 3012 | } |
| 3013 | |
| 3014 | Pointer( MultiBuffer * mb ) |
| 3015 | { |
| 3016 | m_mb = mb; |
| 3017 | m_lpos = 0; |
| 3018 | m_extent = 0; |
| 3019 | m_extpos = 0; |
| 3020 | } |
| 3021 | |
| 3022 | Pointer( const Pointer & mbp ) |
| 3023 | { |
| 3024 | m_mb = mbp.m_mb; |
| 3025 | m_lpos = mbp.m_lpos; |
| 3026 | m_extent = mbp.m_extent; |
| 3027 | m_extpos = mbp.m_extpos; |
| 3028 | } |
| 3029 | |
| 3030 | Pointer & operator=( MultiBuffer & mb ) |
| 3031 | { |
| 3032 | m_mb = &mb; |
| 3033 | m_lpos = 0; |
| 3034 | m_extent = 0; |
| 3035 | m_extpos = 0; |
| 3036 | |
| 3037 | return *this; |
| 3038 | } |
| 3039 | |
| 3040 | Pointer & operator=( const Pointer & mbp ) |
| 3041 | { |
| 3042 | m_mb = mbp.m_mb; |
| 3043 | m_lpos = mbp.m_lpos; |
| 3044 | m_extent = mbp.m_extent; |
| 3045 | m_extpos = mbp.m_extpos; |
| 3046 | |
| 3047 | return *this; |
| 3048 | } |
| 3049 | |
| 3050 | Pointer & operator=( size_t i ) |
| 3051 | { |
| 3052 | reset(); |
| 3053 | increment( i ); |
| 3054 | return *this; |
| 3055 | } |
| 3056 | |
| 3057 | _type & operator*() |
| 3058 | { |
| 3059 | return m_mb->m_bufs[m_extent].position[m_extpos]; |
| 3060 | } |
| 3061 | |
| 3062 | Pointer operator+( size_t i ) const |
| 3063 | { |
| 3064 | Pointer mbp( *this ); |
| 3065 | mbp.increment( i ); |
| 3066 | return mbp; |
| 3067 | } |
| 3068 | |
| 3069 | Pointer operator++( int ) |
| 3070 | { |
| 3071 | Pointer mbp = *this; |
| 3072 | this->increment( 1 ); |
| 3073 | return mbp; |
| 3074 | } |
| 3075 | |
| 3076 | bool operator>=( const Pointer & mbp ) const |
| 3077 | { |
| 3078 | return ( m_lpos >= mbp.m_lpos ); |
| 3079 | } |
| 3080 | |
| 3081 | bool operator>=( const size_t i ) const |
| 3082 | { |
| 3083 | return ( m_lpos >= i ); |
| 3084 | } |
| 3085 | |
| 3086 | void increment( size_t i ) |
| 3087 | { |
| 3088 | m_lpos += i; |
| 3089 | if( m_lpos >= (m_mb->m_size) ) |
| 3090 | { |
| 3091 | m_extent = m_mb->m_bufs.size(); |
| 3092 | m_extpos = m_lpos - m_mb->m_size; |
| 3093 | return; |
| 3094 | } |
| 3095 | |
| 3096 | extent ext_current = m_mb->m_bufs[m_extent]; |
| 3097 | i += m_extpos; |
| 3098 | while( i >= ext_current.length ) |
| 3099 | { |
| 3100 | i -= ext_current.length; |
| 3101 | m_extent++; |
| 3102 | ext_current = m_mb->m_bufs[m_extent]; |
| 3103 | } |
| 3104 | |
| 3105 | m_extpos = i; |
| 3106 | } |
| 3107 | |
| 3108 | void reset() |
| 3109 | { |
| 3110 | m_lpos = 0; |
| 3111 | m_extent = 0; |
| 3112 | m_extpos = 0; |
| 3113 | } |
| 3114 | |
| 3115 | protected: |
| 3116 | MultiBuffer * m_mb; |
| 3117 | |
| 3118 | size_t m_lpos; // position in logical buffer |
| 3119 | size_t m_extent; // current extent |
| 3120 | size_t m_extpos; // position within the extent |
| 3121 | }; |
| 3122 | |
| 3123 | protected: |
| 3124 | |
| 3125 | struct extent |
| 3126 | { |
| 3127 | _type * position; |
| 3128 | size_t length; |
| 3129 | }; |
| 3130 | |
| 3131 | /* data shared by a set of MultiBuffers */ |
| 3132 | vector< extent > m_bufs; // array of sequentially allocated buffers |
| 3133 | size_t m_size; // overall size of total memory represented |
| 3134 | }; |
| 3135 | #endif /* CK_SNDBUF_MEMORY_BUFFER */ |
| 3136 | |
| 3137 | // data for each sndbuf |
| 3138 | struct sndbuf_data |
| 3139 | { |
| 3140 | SAMPLE * buffer; |
| 3141 | t_CKUINT num_samples; |
| 3142 | t_CKUINT num_channels; |
| 3143 | t_CKUINT num_frames; |
| 3144 | t_CKUINT samplerate; |
| 3145 | t_CKUINT chan; |
| 3146 | |
| 3147 | t_CKUINT chunks; |
| 3148 | t_CKUINT chunks_read; |
| 3149 | t_CKUINT chunk_num; |
| 3150 | SAMPLE ** chunk_map; |
| 3151 | // 1.5.0.0 (ge) added (chunks will only affect next .read) |
| 3152 | t_CKUINT chunks_on_next_load; |
| 3153 | |
| 3154 | SAMPLE * eob; |
| 3155 | //SAMPLE * curr; |
| 3156 | SAMPLE current_val; |
| 3157 | t_CKFLOAT sampleratio; |
| 3158 | t_CKFLOAT curf; |
| 3159 | t_CKFLOAT rate_factor; |
| 3160 | t_CKFLOAT rate; |
| 3161 | t_CKINT interp; |
| 3162 | t_CKBOOL loop; |
| 3163 | |
| 3164 | bool sinc_table_built; |
| 3165 | bool sinc_use_table; |
| 3166 | bool sinc_use_interp; |
| 3167 | t_CKINT sinc_samples_per_zero_crossing; |
| 3168 | t_CKINT sinc_width; |
| 3169 | double * sinc_table; |
| 3170 | |
| 3171 | #ifdef CK_SNDBUF_MEMORY_BUFFER |
| 3172 | MultiBuffer< SAMPLE > mb_buffer; |
| 3173 | t_CKUINT mb_max_samples; |
| 3174 | MultiBuffer< SAMPLE >::Pointer mb_record_position; |
| 3175 | MultiBuffer< SAMPLE >::Pointer mb_playback_position; |
| 3176 | #endif /* CK_SNDBUF_MEMORY_BUFFER */ |
| 3177 | |
| 3178 | SNDFILE * fd; |
| 3179 | |
| 3180 | // constructor |
| 3181 | sndbuf_data() |
| 3182 | { |
| 3183 | buffer = NULL; |
| 3184 | interp = SNDBUF_INTERP; |
| 3185 | num_channels = 0; |
| 3186 | num_frames = 0; |
| 3187 | num_samples = 0; |
| 3188 | chunks = CK_SNDBUF_DEFAULT_CHUNK_SIZE; |
| 3189 | chunks_read = 0; |
| 3190 | samplerate = 0; |
| 3191 | sampleratio = 1.0; |
| 3192 | chan = 0; |
| 3193 | curf = 0.0; |
| 3194 | rate_factor = 1.0; |
| 3195 | rate = 1.0; |
| 3196 | eob = NULL; |
| 3197 | current_val = 0.0; |
| 3198 | chunk_map = NULL; |
| 3199 | chunk_num = 0; |
| 3200 | chunks_on_next_load = chunks; // whatever chunks default is |
| 3201 | |
| 3202 | sinc_table_built = false; |
| 3203 | sinc_use_table = USE_TABLE; |
| 3204 | sinc_use_interp = USE_INTERP; |
| 3205 | sinc_width = WIDTH; |
| 3206 | sinc_samples_per_zero_crossing = SAMPLES_PER_ZERO_CROSSING; |
| 3207 | sinc_table = NULL; |
| 3208 | |
| 3209 | #ifdef CK_SNDBUF_MEMORY_BUFFER |
| 3210 | mb_buffer = MultiBuffer< SAMPLE >(); |
| 3211 | #endif /* CK_SNDBUF_MEMORY_BUFFER */ |
| 3212 | |
| 3213 | loop = FALSE; |
| 3214 | fd = NULL; |
| 3215 | } |
| 3216 | |
| 3217 | ~sndbuf_data() |
| 3218 | { |
| 3219 | // open file descriptor? | 1.5.4.2 (ge) added #ugen-refs |
| 3220 | if( this->fd ) |
| 3221 | { |
| 3222 | // close file descriptor |
| 3223 | sf_close( this->fd ); |
| 3224 | // zero out |
| 3225 | this->fd = NULL; |
| 3226 | } |
| 3227 | |
| 3228 | CK_SAFE_DELETE_ARRAY( buffer ); |
| 3229 | |
| 3230 | if( chunk_map ) |
| 3231 | { |
| 3232 | for(int i = 0; i < chunk_num; i++) |
| 3233 | CK_SAFE_DELETE_ARRAY(chunk_map[i]); |
| 3234 | CK_SAFE_DELETE_ARRAY(chunk_map); |
| 3235 | } |
| 3236 | } |
| 3237 | |
| 3238 | inline void sampleIndex2FrameIndexAndChannel(t_CKINT sample, t_CKINT *frame, t_CKINT *channel) |
| 3239 | { |
| 3240 | *frame = (t_CKINT) floorf(sample/this->num_channels); |
| 3241 | *channel = sample%this->num_channels; |
| 3242 | } |
| 3243 | }; |
| 3244 | |
| 3245 | |
| 3246 | double sndbuf_sinc( sndbuf_data * d, double x ); |
| 3247 | double sndbuf_t_sinc( sndbuf_data * d, double x ); |
| 3248 | void sndbuf_make_sinc( sndbuf_data * d ); |
| 3249 | void sndbuf_sinc_interpolate( sndbuf_data * d, SAMPLE * out ); |
| 3250 | |
| 3251 | CK_DLL_CTOR( sndbuf_ctor ) |
| 3252 | { |
| 3253 | OBJ_MEMBER_UINT(SELF, sndbuf_offset_data) = (t_CKUINT)new sndbuf_data; |
| 3254 | } |
| 3255 | |
| 3256 | CK_DLL_DTOR( sndbuf_dtor ) |
| 3257 | { |
| 3258 | sndbuf_data * d = (sndbuf_data *)OBJ_MEMBER_UINT(SELF, sndbuf_offset_data); |
| 3259 | CK_SAFE_DELETE(d); |
| 3260 | OBJ_MEMBER_UINT(SELF, sndbuf_offset_data) = 0; |
| 3261 | } |
| 3262 | |
| 3263 | CK_DLL_CTOR( sndbuf_ctor_path ) |
| 3264 | { |
| 3265 | // NOTE: internal state should be already allocated in pre-ctor |
| 3266 | |
| 3267 | Chuck_DL_Return RETURN; |
| 3268 | // read it |
| 3269 | sndbuf_ctrl_read( SELF, ARGS, &RETURN, VM, SHRED, API ); |
| 3270 | } |
| 3271 | |
| 3272 | CK_DLL_CTOR( sndbuf_ctor_path_rate ) |
| 3273 | { |
| 3274 | // NOTE: internal state should be already allocated in pre-ctor |
| 3275 | |
| 3276 | Chuck_DL_Return RETURN; |
| 3277 | // read it |
| 3278 | sndbuf_ctrl_read( SELF, ARGS, &RETURN, VM, SHRED, API ); |
| 3279 | |
| 3280 | // advance args, skip over path |
| 3281 | GET_NEXT_STRING(ARGS); |
| 3282 | // control rate |
| 3283 | sndbuf_ctrl_rate( SELF, ARGS, &RETURN, VM, SHRED, API ); |
| 3284 | } |
| 3285 | |
| 3286 | CK_DLL_CTOR( sndbuf_ctor_path_rate_pos ) |
| 3287 | { |
| 3288 | // NOTE: internal state should be already allocated in pre-ctor |
| 3289 | |
| 3290 | Chuck_DL_Return RETURN; |
| 3291 | // read it |
| 3292 | sndbuf_ctrl_read( SELF, ARGS, &RETURN, VM, SHRED, API ); |
| 3293 | |
| 3294 | // advance args, skip over path |
| 3295 | GET_NEXT_STRING(ARGS); |
| 3296 | // control rate |
| 3297 | sndbuf_ctrl_rate( SELF, ARGS, &RETURN, VM, SHRED, API ); |
| 3298 | |
| 3299 | // advance args, skip over rate |
| 3300 | GET_NEXT_FLOAT(ARGS); |
| 3301 | sndbuf_ctrl_pos( SELF, ARGS, &RETURN, VM, SHRED, API ); |
| 3302 | } |
| 3303 | |
| 3304 | inline t_CKUINT sndbuf_read( sndbuf_data * d, t_CKUINT frame, t_CKUINT num_frames ) |
| 3305 | { |
| 3306 | // check |
| 3307 | if( d->fd == NULL ) return 0; |
| 3308 | if( frame >= d->num_frames ) return 0; |
| 3309 | |
| 3310 | // log |
| 3311 | // EM_log( CK_LOG_FINE, "(sndbuf): reading %d:%d frames...", offset, howmuch ); |
| 3312 | |
| 3313 | // prevent overflow |
| 3314 | if( num_frames > d->num_frames - frame ) |
| 3315 | num_frames = d->num_frames - frame; |
| 3316 | |
| 3317 | t_CKUINT n; |
| 3318 | // seek |
| 3319 | sf_seek( d->fd, frame, SEEK_SET ); |
| 3320 | t_CKUINT index = frame*d->num_channels; |
| 3321 | #if defined(__CHUCK_USE_64_BIT_SAMPLE__) |
| 3322 | assert(0); // SPENCERTODO |
| 3323 | n = sf_readf_double( d->fd, d->buffer+frame*d->num_channels, num_frames ); |
| 3324 | #else |
| 3325 | if(d->buffer) |
| 3326 | n = sf_readf_float( d->fd, d->buffer+frame*d->num_channels, num_frames ); |
| 3327 | else |
| 3328 | n = sf_readf_float( d->fd, d->chunk_map[index/d->chunks], num_frames ); |
| 3329 | #endif |
| 3330 | |
| 3331 | d->chunks_read += n*d->num_channels; |
| 3332 | |
| 3333 | // close |
| 3334 | if( d->chunks_read >= d->num_frames*d->num_channels ) |
| 3335 | { |
| 3336 | // log |
| 3337 | EM_log( CK_LOG_INFO, "(sndbuf): all frames read, closing file..." ); |
| 3338 | sf_close( d->fd ); |
| 3339 | d->fd = NULL; |
| 3340 | } |
| 3341 | |
| 3342 | return n; |
| 3343 | } |
| 3344 | |
| 3345 | inline t_CKINT sndbuf_load( sndbuf_data * d, t_CKUINT sample ) |
| 3346 | { |
| 3347 | // map to bin |
| 3348 | t_CKUINT bin = floor(((t_CKFLOAT) sample) / ((t_CKFLOAT) d->chunks)); |
| 3349 | |
| 3350 | assert(bin < d->chunk_num); |
| 3351 | |
| 3352 | // already loaded |
| 3353 | if( d->chunk_map[bin] ) return 0; |
| 3354 | |
| 3355 | // allocate |
| 3356 | d->chunk_map[bin] = new SAMPLE[d->chunks]; |
| 3357 | |
| 3358 | // read it |
| 3359 | t_CKINT ret = sndbuf_read( d, bin*d->chunks/d->num_channels, d->chunks/d->num_channels ); |
| 3360 | |
| 3361 | // log |
| 3362 | // EM_log( CK_LOG_FINER, "chunk test: pos: %d bin: %d read:%d/%d", where, bin, d->chunks_read, d->num_frames ); |
| 3363 | // CK_FPRINTF_STDERR( "chunks: loaded bin %lu\n", bin ); |
| 3364 | |
| 3365 | return ret; |
| 3366 | } |
| 3367 | |
| 3368 | inline void sndbuf_setpos( sndbuf_data *d, double frame_pos ) |
| 3369 | { |
| 3370 | // if no buffer allocate (nothing read) |
| 3371 | if( d->buffer == NULL && d->chunk_map == NULL ) return; |
| 3372 | |
| 3373 | d->curf = frame_pos; |
| 3374 | |
| 3375 | // set curf within bounds |
| 3376 | if( d->loop ) |
| 3377 | { |
| 3378 | while( d->curf >= d->num_frames ) d->curf -= d->num_frames; |
| 3379 | while( d->curf < 0 ) d->curf += d->num_frames; |
| 3380 | } |
| 3381 | else |
| 3382 | { |
| 3383 | if( d->curf < 0 ) |
| 3384 | { |
| 3385 | d->curf = -1; |
| 3386 | d->current_val = 0; // 1.4.1.0 (ge) added to avoid DC |
| 3387 | return; // 1.4.1.0 (ge) added |
| 3388 | } |
| 3389 | else if( d->curf >= d->num_frames ) // 1.5.0.0 (ge) | changed from > to >= |
| 3390 | { |
| 3391 | d->curf = d->num_frames; |
| 3392 | d->current_val = 0; |
| 3393 | return; |
| 3394 | } |
| 3395 | } |
| 3396 | |
| 3397 | t_CKUINT index = d->chan + ((t_CKINT)d->curf) * d->num_channels; |
| 3398 | // ensure load |
| 3399 | if( d->fd != NULL ) sndbuf_load( d, index ); |
| 3400 | |
| 3401 | // sets curr to correct position ( account for channels ) |
| 3402 | // t_CKUINT index = d->chan + i * d->num_channels; |
| 3403 | if(d->buffer) |
| 3404 | d->current_val = d->buffer[index]; |
| 3405 | else |
| 3406 | d->current_val = d->chunk_map[index/d->chunks][index%d->chunks]; |
| 3407 | } |
| 3408 | |
| 3409 | inline SAMPLE sndbuf_sampleAt( sndbuf_data * d, t_CKINT frame_pos, t_CKINT arg_chan = -1 ) |
| 3410 | { |
| 3411 | // boundary cases |
| 3412 | t_CKINT nf = d->num_frames; |
| 3413 | if( d->loop ) { |
| 3414 | while( frame_pos >= nf ) frame_pos -= nf; |
| 3415 | while( frame_pos < 0 ) frame_pos += nf; |
| 3416 | } |
| 3417 | else { |
| 3418 | if( frame_pos >= nf ) frame_pos = nf; // 1.5.0.0 (ge) | set frame_pos to nf, instead of nf-1 |
| 3419 | if( frame_pos < 0 ) frame_pos = 0; |
| 3420 | } |
| 3421 | |
| 3422 | // 1.5.0.0 (ge) | added check |
| 3423 | if( frame_pos == nf ) return 0; |
| 3424 | |
| 3425 | // if specific channel was requested, use that one |
| 3426 | t_CKUINT chan = 0; |
| 3427 | // I love sentinal values |
| 3428 | if(arg_chan < 0) |
| 3429 | chan = d->chan; |
| 3430 | else if(arg_chan < d->num_channels) |
| 3431 | chan = arg_chan; |
| 3432 | |
| 3433 | t_CKUINT index = chan + frame_pos * d->num_channels; |
| 3434 | // ensure load |
| 3435 | if( d->fd != NULL ) sndbuf_load( d, index ); |
| 3436 | |
| 3437 | // return sample |
| 3438 | if(d->buffer != NULL) |
| 3439 | return d->buffer[index]; |
| 3440 | else |
| 3441 | return d->chunk_map[index/d->chunks][index%d->chunks]; |
| 3442 | } |
| 3443 | |
| 3444 | inline double sndbuf_getpos( sndbuf_data * d ) |
| 3445 | { |
| 3446 | // if no buffer allocated (nothing read) |
| 3447 | if( d->buffer == NULL && d->chunk_map == NULL ) return 0; |
| 3448 | return floor(d->curf); |
| 3449 | } |
| 3450 | |
| 3451 | CK_DLL_CTRL( sndbuf_ctrl_loop ) |
| 3452 | { |
| 3453 | sndbuf_data * d = (sndbuf_data *)OBJ_MEMBER_UINT(SELF, sndbuf_offset_data); |
| 3454 | d->loop = GET_CK_INT(ARGS); |
| 3455 | RETURN->v_int = d->loop; // TODO: Check (everything) |
| 3456 | } |
| 3457 | |
| 3458 | CK_DLL_CGET( sndbuf_cget_loop ) |
| 3459 | { |
| 3460 | sndbuf_data * d = (sndbuf_data *)OBJ_MEMBER_UINT(SELF, sndbuf_offset_data); |
| 3461 | RETURN->v_int = d->loop; |
| 3462 | } |
| 3463 | |
| 3464 | // PRC's sinc interpolation function.. as found |
| 3465 | // http://www.cs.princeton.edu/courses/archive/spring03/cs325/src/TimeStuf/srconvrt.c |
| 3466 | // |
| 3467 | // there's probably a lot in there that could be optimized, if we care to.. |
| 3468 | |
| 3469 | // #define PI 3.14159265358979323846 |
| 3470 | // wow... we are sensitive.. |
| 3471 | |
| 3472 | inline double sndbuf_linear_interp( double * first, double * second, double * frac ); |
| 3473 | bool sinc_table_built = false; |
| 3474 | |
| 3475 | void sndbuf_sinc_interpolate( sndbuf_data *d, SAMPLE * out ) |
| 3476 | { |
| 3477 | signed long j; |
| 3478 | double factor = d->rate; |
| 3479 | double time_now = d->curf; |
| 3480 | double one_over_factor; |
| 3481 | // UNUSED: double int_time = 0; |
| 3482 | // UNUSED: double last_time = 0; |
| 3483 | double temp1 = 0.0; |
| 3484 | |
| 3485 | long time_i = (long)time_now; |
| 3486 | |
| 3487 | // bounds checking now in sampleAt function... |
| 3488 | if( factor < 1.0 ) { |
| 3489 | for( j = -d->sinc_width + 1; j < d->sinc_width; j++ ) |
| 3490 | { |
| 3491 | temp1 += sndbuf_sampleAt(d,time_i+j) * sndbuf_sinc( d, (double)j ); |
| 3492 | } |
| 3493 | *out = (SAMPLE)temp1; |
| 3494 | } |
| 3495 | else { |
| 3496 | one_over_factor = 1.0 / factor; |
| 3497 | for( j = -d->sinc_width + 1; j < d->sinc_width; j++ ) { |
| 3498 | temp1 += sndbuf_sampleAt(d,time_i+j) * one_over_factor * sndbuf_sinc( d, one_over_factor * (double)j ); |
| 3499 | } |
| 3500 | *out = (SAMPLE)temp1; |
| 3501 | } |
| 3502 | } |
| 3503 | |
| 3504 | void sndbuf_make_sinc( sndbuf_data * d ) |
| 3505 | { |
| 3506 | t_CKINT i; |
| 3507 | // CK_FPRINTF_STDERR( "building sinc table\n" ); |
| 3508 | double temp, win_freq, win; |
| 3509 | win_freq = CK_ONE_PI / d->sinc_width / d->sinc_samples_per_zero_crossing; |
| 3510 | t_CKINT tabsize = d->sinc_width * d->sinc_samples_per_zero_crossing; |
| 3511 | |
| 3512 | d->sinc_table = (double *) realloc( d->sinc_table, tabsize * sizeof(double) ); |
| 3513 | d->sinc_table[0] = 1.0; |
| 3514 | for( i = 1; i < tabsize; i++ ) { |
| 3515 | temp = (double) i * CK_ONE_PI / d->sinc_samples_per_zero_crossing; |
| 3516 | d->sinc_table[i] = (float)(sin(temp) / temp); |
| 3517 | win = 0.5 + 0.5 * cos(win_freq * i); |
| 3518 | d->sinc_table[i] *= (float)win; |
| 3519 | } |
| 3520 | d->sinc_table_built = true; |
| 3521 | } |
| 3522 | |
| 3523 | inline double sndbuf_linear_interp( double first_number, double second_number, double fraction ) |
| 3524 | { |
| 3525 | return (first_number + ((second_number - first_number) * fraction)); |
| 3526 | } |
| 3527 | |
| 3528 | double sndbuf_t_sinc( sndbuf_data * d, double x ) |
| 3529 | { |
| 3530 | t_CKINT low; |
| 3531 | double temp,delta; |
| 3532 | if( !d->sinc_table_built ) sndbuf_make_sinc(d); |
| 3533 | if( fabs(x) >= d->sinc_width-1 ) |
| 3534 | return 0.0; |
| 3535 | else { |
| 3536 | temp = fabs(x) * (double) d->sinc_samples_per_zero_crossing; |
| 3537 | low = (t_CKINT)temp; /* these are interpolation steps */ |
| 3538 | if( d->sinc_use_interp ) { |
| 3539 | delta = temp - low; /* and can be ommited if desired */ |
| 3540 | return sndbuf_linear_interp( d->sinc_table[low], d->sinc_table[low + 1], delta ); |
| 3541 | } |
| 3542 | else return d->sinc_table[low]; |
| 3543 | } |
| 3544 | } |
| 3545 | |
| 3546 | double sndbuf_sinc( sndbuf_data * d, double x ) |
| 3547 | { |
| 3548 | double temp; |
| 3549 | |
| 3550 | if( d->sinc_use_table ) return sndbuf_t_sinc(d,x); |
| 3551 | else { |
| 3552 | if( x == 0.0 ) return 1.0; |
| 3553 | else { |
| 3554 | temp = CK_ONE_PI * x; |
| 3555 | return sin(temp) / (temp); |
| 3556 | } |
| 3557 | } |
| 3558 | } |
| 3559 | |
| 3560 | CK_DLL_TICK( sndbuf_tick ) |
| 3561 | { |
| 3562 | sndbuf_data * d = (sndbuf_data *)OBJ_MEMBER_UINT(SELF, sndbuf_offset_data); |
| 3563 | |
| 3564 | #ifdef CK_SNDBUF_MEMORY_BUFFER |
| 3565 | // spencer's memory buffer stuff |
| 3566 | if( d->fd == 0 ) |
| 3567 | // no file open - memory buffer mode |
| 3568 | { |
| 3569 | Chuck_UGen * ugen = (Chuck_UGen *)SELF; |
| 3570 | if( ugen->m_num_src ) |
| 3571 | // recording mode |
| 3572 | { |
| 3573 | if( d->mb_buffer.size() == 0 ) |
| 3574 | { |
| 3575 | d->mb_buffer.resize( 44100 * 4 ); // default size: 4 seconds |
| 3576 | d->mb_record_position = d->mb_buffer; |
| 3577 | d->mb_playback_position = d->mb_buffer; |
| 3578 | } |
| 3579 | |
| 3580 | if( d->mb_record_position >= d->mb_buffer.size() ) |
| 3581 | d->mb_buffer.resize( d->mb_buffer.size() * 2 ); |
| 3582 | |
| 3583 | *(d->mb_record_position++) = in; |
| 3584 | } |
| 3585 | |
| 3586 | if( d->mb_buffer.size() ) |
| 3587 | { |
| 3588 | if( d->mb_playback_position >= d->mb_record_position ) |
| 3589 | { |
| 3590 | if( d->loop ) |
| 3591 | d->mb_playback_position = 0; |
| 3592 | |
| 3593 | else if( ugen->m_num_src ) |
| 3594 | { |
| 3595 | *out = 0; |
| 3596 | return TRUE; |
| 3597 | } |
| 3598 | |
| 3599 | else |
| 3600 | return FALSE; |
| 3601 | } |
| 3602 | |
| 3603 | *out = *(d->mb_playback_position++); |
| 3604 | } |
| 3605 | |
| 3606 | return TRUE; |
| 3607 | } |
| 3608 | #endif /* CK_SNDBUF_MEMORY_BUFFER */ |
| 3609 | |
| 3610 | // check if we have data to work with |
| 3611 | // 1.5.0.0 (ge) modified for clarity; |
| 3612 | // was: if( !(d->buffer || d->chunk_map) ) { ... } |
| 3613 | // if no buffer allocated; nothing read |
| 3614 | if( d->buffer == NULL && d->chunk_map == NULL ) |
| 3615 | { |
| 3616 | *out = 0; |
| 3617 | return TRUE; |
| 3618 | } |
| 3619 | |
| 3620 | // we're ticking once per sample ( system ) |
| 3621 | // curf in samples; |
| 3622 | if( !d->loop && (d->curf >= d->num_frames || d->curf < 0) ) |
| 3623 | { |
| 3624 | *out = 0; |
| 3625 | } |
| 3626 | else // 1.4.1.0 (ge): put this into else block so that rate can advance below even for the *out == 0 case above |
| 3627 | { |
| 3628 | // calculate frame |
| 3629 | if( d->interp == SNDBUF_DROP ) |
| 3630 | { |
| 3631 | *out = d->current_val; |
| 3632 | } |
| 3633 | else if( d->interp == SNDBUF_INTERP ) |
| 3634 | { |
| 3635 | // samplewise linear interp |
| 3636 | double alpha = d->curf - floor(d->curf); |
| 3637 | *out = d->current_val; |
| 3638 | *out += (float)alpha * ( sndbuf_sampleAt(d, (long)d->curf+1 ) - *out ); |
| 3639 | } |
| 3640 | else if( d->interp == SNDBUF_SINC ) { |
| 3641 | // do that fancy sinc function! |
| 3642 | sndbuf_sinc_interpolate(d, out); |
| 3643 | } |
| 3644 | } |
| 3645 | |
| 3646 | // advance |
| 3647 | d->curf += d->rate; |
| 3648 | sndbuf_setpos(d, d->curf); |
| 3649 | |
| 3650 | return TRUE; |
| 3651 | } |
| 3652 | |
| 3653 | /* multi-chan tick */ |
| 3654 | CK_DLL_TICKF( sndbuf_tickf ) |
| 3655 | { |
| 3656 | Chuck_UGen * ugen = (Chuck_UGen *) SELF; |
| 3657 | |
| 3658 | sndbuf_data * d = (sndbuf_data *)OBJ_MEMBER_UINT(SELF, sndbuf_offset_data); |
| 3659 | |
| 3660 | // if no buffer allocated, nothing read |
| 3661 | if( d->buffer == NULL && d->chunk_map == NULL ) return FALSE; |
| 3662 | |
| 3663 | // we're ticking once per sample ( system ) |
| 3664 | // curf in samples; |
| 3665 | |
| 3666 | if( !d->loop && d->curf >= d->num_frames ) return FALSE; |
| 3667 | |
| 3668 | // normalize amplitude across channels |
| 3669 | // TODO: normalize power? |
| 3670 | SAMPLE amp_factor = 1; |
| 3671 | if(d->num_channels == 1) |
| 3672 | { |
| 3673 | amp_factor = 0.5; |
| 3674 | } |
| 3675 | else if(d->num_channels == 2) |
| 3676 | { |
| 3677 | amp_factor = 1; |
| 3678 | } |
| 3679 | else |
| 3680 | { |
| 3681 | // ruh roh |
| 3682 | } |
| 3683 | |
| 3684 | t_CKUINT frame_idx; |
| 3685 | t_CKUINT nchans = ugen->m_num_outs; |
| 3686 | for(frame_idx = 0; frame_idx < nframes && (d->loop || d->curf < d->num_frames); frame_idx++) |
| 3687 | { |
| 3688 | for(unsigned int chan_idx = 0; chan_idx < nchans; chan_idx++) |
| 3689 | { |
| 3690 | // calculate frame |
| 3691 | if( d->interp == SNDBUF_DROP ) |
| 3692 | { |
| 3693 | out[frame_idx*nchans+chan_idx] = sndbuf_sampleAt(d, d->curf, chan_idx % d->num_channels) * amp_factor; |
| 3694 | } |
| 3695 | else if( d->interp == SNDBUF_INTERP ) |
| 3696 | { |
| 3697 | // SPENCERTODO |
| 3698 | // samplewise linear interp |
| 3699 | double alpha = d->curf - floor(d->curf); |
| 3700 | SAMPLE current_sample = sndbuf_sampleAt(d, d->curf, chan_idx % d->num_channels) * amp_factor; |
| 3701 | out[frame_idx*nchans+chan_idx] = current_sample; |
| 3702 | out[frame_idx*nchans+chan_idx] += (float)alpha * ( sndbuf_sampleAt(d, (long)d->curf+1, chan_idx % d->num_channels ) - current_sample ); |
| 3703 | out[frame_idx*nchans+chan_idx] *= amp_factor; |
| 3704 | } |
| 3705 | else if( d->interp == SNDBUF_SINC ) |
| 3706 | { |
| 3707 | // SPENCERTODO |
| 3708 | // do that fancy sinc function! |
| 3709 | //sndbuf_sinc_interpolate(d, out); |
| 3710 | } |
| 3711 | } |
| 3712 | |
| 3713 | // advance |
| 3714 | sndbuf_setpos(d, d->curf + d->rate); |
| 3715 | } |
| 3716 | |
| 3717 | for(; frame_idx < nframes; frame_idx++) |
| 3718 | { |
| 3719 | for(t_CKUINT chan_idx = 0; chan_idx < nchans; chan_idx++) |
| 3720 | { |
| 3721 | out[frame_idx*nchans+chan_idx] = 0; |
| 3722 | } |
| 3723 | } |
| 3724 | |
| 3725 | |
| 3726 | return TRUE; |
| 3727 | } |
| 3728 | |
| 3729 | CK_DLL_CTRL( sndbuf_ctrl_read ) |
| 3730 | { |
| 3731 | sndbuf_data * d = (sndbuf_data *)OBJ_MEMBER_UINT(SELF, sndbuf_offset_data); |
| 3732 | Chuck_String * ckfilename = GET_CK_STRING(ARGS); |
| 3733 | const char * filename = NULL; |
| 3734 | |
| 3735 | // set return value |
| 3736 | RETURN->v_string = ckfilename; |
| 3737 | |
| 3738 | // cleanup |
| 3739 | CK_SAFE_DELETE_ARRAY(d->buffer); |
| 3740 | // clean up chunk map |
| 3741 | if( d->chunk_map ) |
| 3742 | { |
| 3743 | for(int i = 0; i < d->chunk_num; i++) |
| 3744 | CK_SAFE_DELETE_ARRAY(d->chunk_map[i]); |
| 3745 | CK_SAFE_DELETE_ARRAY(d->chunk_map); |
| 3746 | d->chunk_num = 0; |
| 3747 | } |
| 3748 | // close file descriptor |
| 3749 | if( d->fd ) |
| 3750 | { |
| 3751 | sf_close( d->fd ); |
| 3752 | d->fd = NULL; |
| 3753 | } |
| 3754 | |
| 3755 | // check | 1.5.0.0 (ge) added check to avert crash on null argument |
| 3756 | if( !ckfilename ) |
| 3757 | { |
| 3758 | CK_FPRINTF_STDERR( "[chuck] SndBuf.read() given null argument; nothing read...\n" ); |
| 3759 | return; |
| 3760 | } |
| 3761 | // get filename c string |
| 3762 | filename = ckfilename->str().c_str(); |
| 3763 | |
| 3764 | // log |
| 3765 | EM_log( CK_LOG_INFO, "(sndbuf): reading '%s'...", filename ); |
| 3766 | |
| 3767 | // copy in the chunks size | 1.5.0.0 (ge) |
| 3768 | d->chunks = d->chunks_on_next_load; |
| 3769 | |
| 3770 | // built in |
| 3771 | if( strstr(filename, "special:") ) |
| 3772 | { |
| 3773 | SAMPLE * rawdata = NULL; |
| 3774 | t_CKUINT rawsize = 0; |
| 3775 | t_CKUINT srate = 22050; |
| 3776 | |
| 3777 | // which |
| 3778 | if( strstr(filename, "special:sinewave") ) { |
| 3779 | rawsize = 1024; rawdata = NULL; |
| 3780 | } |
| 3781 | else if( strstr(filename, "special:aaa") || |
| 3782 | strstr(filename, "special:aah") || |
| 3783 | strstr(filename, "special:ahh") ) |
| 3784 | { |
| 3785 | rawsize = ahh_size; rawdata = ahh_data; |
| 3786 | } |
| 3787 | else if( strstr(filename, "special:britestk") ) { |
| 3788 | rawsize = britestk_size; rawdata = britestk_data; |
| 3789 | } |
| 3790 | else if( strstr(filename, "special:doh") || strstr(filename, "special:dope") ) { |
| 3791 | rawsize = dope_size; rawdata = dope_data; |
| 3792 | } |
| 3793 | else if( strstr(filename, "special:eee") ) { |
| 3794 | rawsize = eee_size; rawdata = eee_data; |
| 3795 | } |
| 3796 | else if( strstr(filename, "special:fwavblnk") ) { |
| 3797 | rawsize = fwavblnk_size; rawdata = fwavblnk_data; |
| 3798 | } |
| 3799 | else if( strstr(filename, "special:halfwave") ) { |
| 3800 | rawsize = halfwave_size; rawdata = halfwave_data; |
| 3801 | } |
| 3802 | else if( strstr(filename, "special:impuls10") ) { |
| 3803 | rawsize = impuls10_size; rawdata = impuls10_data; |
| 3804 | } |
| 3805 | else if( strstr(filename, "special:impuls20") ) { |
| 3806 | rawsize = impuls20_size; rawdata = impuls20_data; |
| 3807 | } |
| 3808 | else if( strstr(filename, "special:impuls40") ) { |
| 3809 | rawsize = impuls40_size; rawdata = impuls40_data; |
| 3810 | } |
| 3811 | else if( strstr(filename, "special:mand1") ) { |
| 3812 | rawsize = mand1_size; rawdata = mand1_data; |
| 3813 | } |
| 3814 | else if( strstr(filename, "special:mandpluk") ) { |
| 3815 | rawsize = mandpluk_size; rawdata = mandpluk_data; |
| 3816 | } |
| 3817 | else if( strstr(filename, "special:marmstk1") ) { |
| 3818 | rawsize = marmstk1_size; rawdata = marmstk1_data; |
| 3819 | } |
| 3820 | else if( strstr(filename, "special:ooo") ) { |
| 3821 | rawsize = ooo_size; rawdata = ooo_data; |
| 3822 | } |
| 3823 | else if( strstr(filename, "special:peksblnk") ) { |
| 3824 | rawsize = peksblnk_size; rawdata = peksblnk_data; |
| 3825 | } |
| 3826 | else if( strstr(filename, "special:ppksblnk") ) { |
| 3827 | rawsize = ppksblnk_size; rawdata = ppksblnk_data; |
| 3828 | } |
| 3829 | else if( strstr(filename, "special:silence") ) { |
| 3830 | rawsize = silence_size; rawdata = silence_data; |
| 3831 | } |
| 3832 | else if( strstr(filename, "special:sineblnk") ) { |
| 3833 | rawsize = sineblnk_size; rawdata = sineblnk_data; |
| 3834 | } |
| 3835 | else if( strstr(filename, "special:sinewave") ) { |
| 3836 | rawsize = sinewave_size; rawdata = sinewave_data; |
| 3837 | } |
| 3838 | else if( strstr(filename, "special:snglpeak") ) { |
| 3839 | rawsize = snglpeak_size; rawdata = snglpeak_data; |
| 3840 | } |
| 3841 | else if( strstr(filename, "special:twopeaks") ) { |
| 3842 | rawsize = twopeaks_size; rawdata = twopeaks_data; |
| 3843 | } |
| 3844 | else if( strstr(filename, "special:glot_ahh") ) { |
| 3845 | rawsize = glot_ahh_size; rawdata = glot_ahh_data; srate = 44100; |
| 3846 | } |
| 3847 | else if( strstr(filename, "special:glot_eee") ) { |
| 3848 | rawsize = glot_eee_size; rawdata = glot_eee_data; srate = 44100; |
| 3849 | } |
| 3850 | else if( strstr(filename, "special:glot_ooo") ) { |
| 3851 | rawsize = glot_ooo_size; rawdata = glot_ooo_data; srate = 44100; |
| 3852 | } |
| 3853 | else if( strstr(filename, "special:glot_pop") ) { |
| 3854 | rawsize = glot_pop_size; rawdata = glot_pop_data; srate = 44100; |
| 3855 | } |
| 3856 | |
| 3857 | d->num_frames = rawsize; |
| 3858 | d->num_channels = 1; |
| 3859 | d->chan = 0; |
| 3860 | d->samplerate = srate; |
| 3861 | d->num_samples = rawsize; |
| 3862 | |
| 3863 | // no chunking |
| 3864 | d->chunks_read = d->num_frames; |
| 3865 | |
| 3866 | if( rawdata ) { |
| 3867 | d->buffer = new SAMPLE[rawsize+1]; |
| 3868 | for( t_CKUINT j = 0; j < rawsize; j++ ) { |
| 3869 | d->buffer[j] = (SAMPLE)rawdata[j]/(SAMPLE)SHRT_MAX; |
| 3870 | } |
| 3871 | } |
| 3872 | else if( strstr(filename, "special:sinewave") ) { |
| 3873 | d->buffer = new SAMPLE[rawsize+1]; |
| 3874 | for( t_CKUINT j = 0; j < rawsize; j++ ) |
| 3875 | d->buffer[j] = sin(2*CK_ONE_PI*j/rawsize); |
| 3876 | } |
| 3877 | else { |
| 3878 | CK_FPRINTF_STDERR( "[chuck](via SndBuf): cannot load '%s'\n", filename ); |
| 3879 | return; |
| 3880 | } |
| 3881 | |
| 3882 | d->buffer[rawsize] = d->buffer[0]; |
| 3883 | } |
| 3884 | else // read file |
| 3885 | { |
| 3886 | #ifdef __ANDROID__ |
| 3887 | bool is_jar_url = strstr(filename, "jar:") == filename; |
| 3888 | int jar_fd = 0; |
| 3889 | if( is_jar_url ) |
| 3890 | { |
| 3891 | if( !ChuckAndroid::copyJARURLFileToTemporary(filename, &jar_fd) ) |
| 3892 | { |
| 3893 | CK_FPRINTF_STDERR( "[chuck](via SndBuf): could not download file '%s' from JAR\n", filename ); |
| 3894 | return; |
| 3895 | } |
| 3896 | } |
| 3897 | else |
| 3898 | #endif |
| 3899 | { |
| 3900 | // check if file exists |
| 3901 | struct stat s; |
| 3902 | if( stat( filename, &s ) ) |
| 3903 | { |
| 3904 | CK_FPRINTF_STDERR( "[chuck](via SndBuf): cannot open file '%s'...\n", filename ); |
| 3905 | return; |
| 3906 | } |
| 3907 | } |
| 3908 | |
| 3909 | // open it |
| 3910 | SF_INFO info; |
| 3911 | info.format = 0; |
| 3912 | const char * format = (const char *)strrchr( filename, '.'); |
| 3913 | if( format && strcmp( format, ".raw" ) == 0 ) |
| 3914 | { |
| 3915 | CK_FPRINTF_STDERR( "[chuck](via SndBuf) %s :: type is '.raw'...\n", filename ); |
| 3916 | CK_FPRINTF_STDERR( "[chuck](via SndBuf) |- assuming 16 bit signed mono (PCM)\n]" ); |
| 3917 | info.format = SF_FORMAT_RAW | SF_FORMAT_PCM_16 | SF_ENDIAN_CPU ; |
| 3918 | info.channels = 1; |
| 3919 | info.samplerate = 44100; |
| 3920 | } |
| 3921 | |
| 3922 | // open the handle |
| 3923 | #ifdef __ANDROID__ |
| 3924 | if( is_jar_url ) |
| 3925 | { |
| 3926 | d->fd = sf_open_fd( jar_fd, SFM_READ, &info, 0 ); |
| 3927 | } |
| 3928 | else |
| 3929 | #endif |
| 3930 | { |
| 3931 | d->fd = sf_open( filename, SFM_READ, &info ); |
| 3932 | } |
| 3933 | t_CKINT er = sf_error( d->fd ); |
| 3934 | if( er ) |
| 3935 | { |
| 3936 | CK_FPRINTF_STDERR( "[chuck](via SndBuf): sndfile error '%li' opening '%s'...\n", er, filename ); |
| 3937 | CK_FPRINTF_STDERR( "[chuck](via SndBuf): ...(reason: %s)\n", sf_strerror( d->fd ) ); |
| 3938 | if( d->fd ) |
| 3939 | { |
| 3940 | sf_close( d->fd ); |
| 3941 | d->fd = NULL; // 1.5.0.0 (ge) added |
| 3942 | } |
| 3943 | // escape |
| 3944 | return; |
| 3945 | } |
| 3946 | |
| 3947 | // allocate |
| 3948 | t_CKINT size = info.channels * info.frames; |
| 3949 | if(d->chunks) |
| 3950 | { |
| 3951 | // split into small allocations |
| 3952 | d->chunk_num = ceil(((t_CKFLOAT) size) / ((t_CKFLOAT) d->chunks)); // 1.5.0.0 (ge) | ceilf => ceil |
| 3953 | d->buffer = NULL; |
| 3954 | d->chunk_map = new SAMPLE*[d->chunk_num]; |
| 3955 | memset(d->chunk_map, 0, d->chunk_num * sizeof(SAMPLE *)); |
| 3956 | d->chunks_read = 0; |
| 3957 | } |
| 3958 | else |
| 3959 | { |
| 3960 | // all in one go |
| 3961 | d->buffer = new SAMPLE[size+info.channels]; |
| 3962 | memset( d->buffer, 0, (size+info.channels)*sizeof(SAMPLE) ); |
| 3963 | d->chunk_map = NULL; |
| 3964 | } |
| 3965 | |
| 3966 | d->chan = 0; |
| 3967 | d->num_frames = info.frames; |
| 3968 | d->num_channels = info.channels; |
| 3969 | d->samplerate = info.samplerate; |
| 3970 | d->num_samples = size; |
| 3971 | |
| 3972 | // log |
| 3973 | EM_pushlog(); |
| 3974 | EM_log( CK_LOG_INFO, "channels: %d", d->num_channels ); |
| 3975 | EM_log( CK_LOG_INFO, "frames: %d", d->num_frames ); |
| 3976 | EM_log( CK_LOG_INFO, "srate: %d", d->samplerate ); |
| 3977 | EM_log( CK_LOG_INFO, "chunks: %d", d->chunks ); |
| 3978 | EM_poplog(); |
| 3979 | |
| 3980 | // read |
| 3981 | sf_seek( d->fd, 0, SEEK_SET ); |
| 3982 | |
| 3983 | // no chunk |
| 3984 | if( !d->chunks ) |
| 3985 | { |
| 3986 | // read all |
| 3987 | t_CKUINT f = sndbuf_read( d, 0, d->num_frames ); |
| 3988 | // check |
| 3989 | if( f != (t_CKUINT)d->num_frames ) |
| 3990 | { |
| 3991 | CK_FPRINTF_STDERR( "[chuck](via SndBuf): read %lu rather than %ld frames from %s\n", |
| 3992 | f, size, filename ); |
| 3993 | sf_close( d->fd ); d->fd = NULL; |
| 3994 | return; |
| 3995 | } |
| 3996 | |
| 3997 | assert( d->fd == NULL ); |
| 3998 | } |
| 3999 | } |
| 4000 | |
| 4001 | // d->interp = SNDBUF_INTERP; |
| 4002 | d->sampleratio = (double)d->samplerate / (double)g_srateXxx; |
| 4003 | // set the rate |
| 4004 | d->rate = d->sampleratio * d->rate_factor; |
| 4005 | d->current_val = 0; |
| 4006 | d->curf = 0; |
| 4007 | d->eob = d->buffer + d->num_samples; |
| 4008 | } |
| 4009 | |
| 4010 | CK_DLL_CTRL( sndbuf_ctrl_write ) |
| 4011 | { |
| 4012 | #ifdef SPENCER_SNDBUF_WRITE |
| 4013 | sndbuf_data * d = (sndbuf_data *)OBJ_MEMBER_UINT(SELF, sndbuf_offset_data); |
| 4014 | const char * filename = GET_CK_STRING(ARGS)->str.c_str(); |
| 4015 | |
| 4016 | if( d->buffer ) |
| 4017 | { |
| 4018 | delete [] d->buffer; |
| 4019 | d->buffer = NULL; |
| 4020 | } |
| 4021 | |
| 4022 | struct stat s; |
| 4023 | if( stat( filename, &s ) ) |
| 4024 | { |
| 4025 | CK_FPRINTF_STDERR( "[chuck](via SndBuf): cannot open file '%s'...\n", filename ); |
| 4026 | return; |
| 4027 | } |
| 4028 | |
| 4029 | d->curr = d->buffer; |
| 4030 | d->eob = d->buffer + d->num_samples; |
| 4031 | #endif |
| 4032 | } |
| 4033 | |
| 4034 | CK_DLL_CGET( sndbuf_cget_ready ) |
| 4035 | { |
| 4036 | // get the internal representation |
| 4037 | sndbuf_data * d = ( sndbuf_data * )OBJ_MEMBER_UINT(SELF, sndbuf_offset_data); |
| 4038 | // ready if we have a file descriptor |
| 4039 | RETURN->v_int = d->fd != NULL; |
| 4040 | } |
| 4041 | |
| 4042 | CK_DLL_CTRL( sndbuf_ctrl_rate ) |
| 4043 | { |
| 4044 | sndbuf_data * d = ( sndbuf_data * )OBJ_MEMBER_UINT(SELF, sndbuf_offset_data); |
| 4045 | t_CKFLOAT rate = GET_CK_FLOAT(ARGS); // rate |
| 4046 | d->rate = rate * d->sampleratio; |
| 4047 | d->rate_factor = rate; |
| 4048 | RETURN->v_float = d->rate_factor; // TODO: (or not TODO:) |
| 4049 | } |
| 4050 | |
| 4051 | CK_DLL_CGET( sndbuf_cget_rate ) |
| 4052 | { |
| 4053 | sndbuf_data * d = (sndbuf_data *)OBJ_MEMBER_UINT(SELF, sndbuf_offset_data); |
| 4054 | RETURN->v_float = d->rate_factor; |
| 4055 | } |
| 4056 | |
| 4057 | |
| 4058 | CK_DLL_CTRL( sndbuf_ctrl_freq ) |
| 4059 | { |
| 4060 | sndbuf_data * d = ( sndbuf_data * ) OBJ_MEMBER_UINT(SELF, sndbuf_offset_data); |
| 4061 | t_CKFLOAT freq = GET_CK_FLOAT(ARGS); //hz |
| 4062 | |
| 4063 | d->rate = ( freq * (double) d->num_frames / (double)g_srateXxx); |
| 4064 | d->rate_factor = d->rate / d->sampleratio; |
| 4065 | RETURN->v_float = d->rate * (t_CKFLOAT)g_srateXxx / ( (t_CKFLOAT) d->num_frames ); // TODO: really? |
| 4066 | } |
| 4067 | |
| 4068 | CK_DLL_CGET( sndbuf_cget_freq ) |
| 4069 | { |
| 4070 | sndbuf_data * d = (sndbuf_data *)OBJ_MEMBER_UINT(SELF, sndbuf_offset_data); |
| 4071 | RETURN->v_float = d->rate * (t_CKFLOAT)g_srateXxx / ( (t_CKFLOAT) d->num_frames ); |
| 4072 | } |
| 4073 | |
| 4074 | CK_DLL_CTRL( sndbuf_ctrl_phase ) |
| 4075 | { |
| 4076 | sndbuf_data * d = ( sndbuf_data * ) OBJ_MEMBER_UINT(SELF, sndbuf_offset_data); |
| 4077 | t_CKFLOAT phase = GET_CK_FLOAT(ARGS); |
| 4078 | sndbuf_setpos(d, phase * (double)d->num_frames); |
| 4079 | RETURN->v_float = (t_CKFLOAT) d->curf / (t_CKFLOAT)d->num_frames; // TODO: |
| 4080 | } |
| 4081 | |
| 4082 | CK_DLL_CGET( sndbuf_cget_phase ) |
| 4083 | { |
| 4084 | sndbuf_data * d = (sndbuf_data *)OBJ_MEMBER_UINT(SELF, sndbuf_offset_data); |
| 4085 | //SET_NEXT_FLOAT( out, (t_CKFLOAT) d->curf / (t_CKFLOAT)d->num_frames ); |
| 4086 | RETURN->v_float = (t_CKFLOAT) d->curf / (t_CKFLOAT)d->num_frames; |
| 4087 | } |
| 4088 | |
| 4089 | CK_DLL_CTRL( sndbuf_ctrl_channel ) |
| 4090 | { |
| 4091 | sndbuf_data * d = ( sndbuf_data * ) OBJ_MEMBER_UINT(SELF, sndbuf_offset_data); |
| 4092 | t_CKINT chan = GET_CK_INT(ARGS); |
| 4093 | if ( chan >= 0 && chan < d->num_channels ) { |
| 4094 | d->chan = chan; |
| 4095 | } |
| 4096 | RETURN->v_int = (t_CKINT)d->chan; |
| 4097 | } |
| 4098 | |
| 4099 | CK_DLL_CGET( sndbuf_cget_channel ) |
| 4100 | { |
| 4101 | sndbuf_data * d = (sndbuf_data *)OBJ_MEMBER_UINT(SELF, sndbuf_offset_data); |
| 4102 | //SET_NEXT_INT( out, d->chan ); |
| 4103 | RETURN->v_int = (t_CKINT)d->chan; |
| 4104 | } |
| 4105 | |
| 4106 | CK_DLL_CTRL( sndbuf_ctrl_pos ) |
| 4107 | { |
| 4108 | sndbuf_data * d = ( sndbuf_data * ) OBJ_MEMBER_UINT(SELF, sndbuf_offset_data); |
| 4109 | t_CKINT pos = GET_CK_INT(ARGS); |
| 4110 | #ifdef CK_SNDBUF_MEMORY_BUFFER |
| 4111 | if( pos >= 0 && pos < d->mb_max_samples ) |
| 4112 | d->mb_playback_position = pos; |
| 4113 | #endif /* CK_SNDBUF_MEMORY_BUFFER */ |
| 4114 | sndbuf_setpos(d, pos); |
| 4115 | RETURN->v_int = (t_CKINT)sndbuf_getpos(d); // TODO TODO TODOOO |
| 4116 | } |
| 4117 | |
| 4118 | CK_DLL_CGET( sndbuf_cget_pos ) |
| 4119 | { |
| 4120 | sndbuf_data * d = (sndbuf_data *)OBJ_MEMBER_UINT(SELF, sndbuf_offset_data); |
| 4121 | RETURN->v_int = (t_CKINT)sndbuf_getpos(d); |
| 4122 | } |
| 4123 | |
| 4124 | CK_DLL_CTRL( sndbuf_ctrl_interp ) |
| 4125 | { |
| 4126 | sndbuf_data * d = ( sndbuf_data * ) OBJ_MEMBER_UINT(SELF, sndbuf_offset_data); |
| 4127 | t_CKINT interp = GET_CK_INT(ARGS); |
| 4128 | d->interp = interp; |
| 4129 | RETURN->v_int = d->interp; |
| 4130 | } |
| 4131 | |
| 4132 | CK_DLL_CGET( sndbuf_cget_interp ) |
| 4133 | { |
| 4134 | sndbuf_data * d = (sndbuf_data *)OBJ_MEMBER_UINT(SELF, sndbuf_offset_data); |
| 4135 | RETURN->v_int = d->interp; |
| 4136 | } |
| 4137 | |
| 4138 | CK_DLL_CTRL( sndbuf_ctrl_chunks ) |
| 4139 | { |
| 4140 | sndbuf_data * d = (sndbuf_data *)OBJ_MEMBER_UINT(SELF, sndbuf_offset_data); |
| 4141 | t_CKINT frames = GET_NEXT_INT(ARGS); |
| 4142 | // 1.5.0.0 (ge) | modified to set chunks_on_next_load |
| 4143 | // setting chunks after load violates some assumptions |
| 4144 | d->chunks_on_next_load = frames >= 0 ? frames : 0; |
| 4145 | // d->chunks = frames >= 0 ? frames : 0; |
| 4146 | RETURN->v_int = d->chunks; |
| 4147 | } |
| 4148 | |
| 4149 | CK_DLL_CGET( sndbuf_cget_chunks ) |
| 4150 | { |
| 4151 | sndbuf_data * d = (sndbuf_data *)OBJ_MEMBER_UINT(SELF, sndbuf_offset_data); |
| 4152 | // 1.5.0.0 (ge) | retuns d->chunks_on_next_load |
| 4153 | RETURN->v_int = d->chunks_on_next_load; |
| 4154 | // RETURN->v_int = d->chunks; |
| 4155 | } |
| 4156 | |
| 4157 | CK_DLL_CTRL( sndbuf_ctrl_phase_offset ) |
| 4158 | { |
| 4159 | sndbuf_data * d = (sndbuf_data *)OBJ_MEMBER_UINT(SELF, sndbuf_offset_data); |
| 4160 | t_CKFLOAT phase_offset = GET_CK_FLOAT(ARGS); |
| 4161 | sndbuf_setpos(d, d->curf + phase_offset * (t_CKFLOAT)d->num_frames ); |
| 4162 | } |
| 4163 | |
| 4164 | CK_DLL_CGET( sndbuf_cget_samples ) |
| 4165 | { |
| 4166 | sndbuf_data * d = (sndbuf_data *)OBJ_MEMBER_UINT(SELF, sndbuf_offset_data); |
| 4167 | //SET_NEXT_INT( out, d->num_frames ); |
| 4168 | RETURN->v_int = d->num_frames; |
| 4169 | } |
| 4170 | |
| 4171 | CK_DLL_CGET( sndbuf_cget_samplerate ) |
| 4172 | { |
| 4173 | sndbuf_data * d = (sndbuf_data *)OBJ_MEMBER_UINT(SELF, sndbuf_offset_data); |
| 4174 | RETURN->v_int = d->samplerate; |
| 4175 | } |
| 4176 | |
| 4177 | CK_DLL_CGET( sndbuf_cget_length ) |
| 4178 | { |
| 4179 | sndbuf_data * d = (sndbuf_data *)OBJ_MEMBER_UINT(SELF, sndbuf_offset_data); |
| 4180 | //SET_NEXT_DUR( out, (t_CKDUR)d->num_frames ); |
| 4181 | RETURN->v_dur = (t_CKDUR)d->num_frames / d->sampleratio; |
| 4182 | } |
| 4183 | |
| 4184 | CK_DLL_CGET( sndbuf_cget_channels ) |
| 4185 | { |
| 4186 | sndbuf_data * d = (sndbuf_data *)OBJ_MEMBER_UINT(SELF, sndbuf_offset_data); |
| 4187 | //SET_NEXT_INT( out, d->num_channels ); |
| 4188 | RETURN->v_int = d->num_channels; |
| 4189 | } |
| 4190 | |
| 4191 | CK_DLL_CGET( sndbuf_cget_valueAt ) |
| 4192 | { |
| 4193 | sndbuf_data * d = (sndbuf_data *)OBJ_MEMBER_UINT(SELF, sndbuf_offset_data); |
| 4194 | t_CKINT sample = GET_CK_INT(ARGS); |
| 4195 | t_CKINT frame, channel; |
| 4196 | d->sampleIndex2FrameIndexAndChannel(sample, &frame, &channel); |
| 4197 | if( d->fd ) sndbuf_load( d, sample ); |
| 4198 | // 1.5.0.0 (ge) 'frame >' => 'frame >=' |
| 4199 | RETURN->v_float = ( frame >= d->num_frames || frame < 0 ) ? 0 : sndbuf_sampleAt(d, frame, channel); |
| 4200 | } |
| 4201 | |
| 4202 | CK_DLL_CTOR( sndbuf2_ctor_path ) |
| 4203 | { |
| 4204 | // call up to corresponding SndBuf contructor |
| 4205 | sndbuf_ctor_path( SELF, ARGS, VM, SHRED, API ); |
| 4206 | } |
| 4207 | |
| 4208 | CK_DLL_CTOR( sndbuf2_ctor_path_rate ) |
| 4209 | { |
| 4210 | // call up to corresponding SndBuf contructor |
| 4211 | sndbuf_ctor_path_rate( SELF, ARGS, VM, SHRED, API ); |
| 4212 | } |
| 4213 | |
| 4214 | CK_DLL_CTOR( sndbuf2_ctor_path_rate_pos ) |
| 4215 | { |
| 4216 | // call up to corresponding SndBuf contructor |
| 4217 | sndbuf_ctor_path_rate_pos( SELF, ARGS, VM, SHRED, API ); |
| 4218 | } |
| 4219 | |
| 4220 | #endif // __DISABLE_SNDBUF__ |
| 4221 | |
| 4222 | |
| 4223 | class Dyno_Data |
| 4224 | { |
| 4225 | private: |
| 4226 | t_CKDUR ms; // changed 1.3.5.3 ge from const static |
| 4227 | |
| 4228 | public: |
| 4229 | t_CKFLOAT slopeAbove; |
| 4230 | t_CKFLOAT slopeBelow; |
| 4231 | t_CKFLOAT thresh; |
| 4232 | t_CKFLOAT rt; |
| 4233 | t_CKFLOAT at; |
| 4234 | t_CKFLOAT xd; //sidechain |
| 4235 | t_CKINT externalSideInput; // use input signal or a ctrl signal for env |
| 4236 | t_CKFLOAT sideInput; // the ctrl signal for the envelope |
| 4237 | |
| 4238 | int count; //diagnostic |
| 4239 | |
| 4240 | Dyno_Data( Chuck_VM * vm ) { |
| 4241 | ms = vm->srate() / 1000.0; |
| 4242 | xd = 0.0; |
| 4243 | count = 0; |
| 4244 | sideInput = 0; |
| 4245 | limit(); |
| 4246 | } |
| 4247 | ~Dyno_Data() {} |
| 4248 | |
| 4249 | void limit(); |
| 4250 | void compress(); |
| 4251 | void gate(); |
| 4252 | void expand(); |
| 4253 | void duck(); |
| 4254 | |
| 4255 | //set the time constants for rt, at, and tav |
| 4256 | static t_CKFLOAT computeTimeConst(t_CKDUR t) { |
| 4257 | //AT = 1 - e ^ (-2.2T/t<AT) |
| 4258 | //as per chuck_type.cpp, T(sampling period) = 1.0 |
| 4259 | return 1.0 - exp( -2.2 / t ); |
| 4260 | } |
| 4261 | |
| 4262 | static t_CKDUR timeConstToDur(t_CKFLOAT x) { |
| 4263 | return -2.2 / log(1.0 - x); |
| 4264 | } |
| 4265 | |
| 4266 | //setters for timing constants |
| 4267 | void setAttackTime(t_CKDUR t); |
| 4268 | void setReleaseTime(t_CKDUR t); |
| 4269 | |
| 4270 | //other setters |
| 4271 | void setRatio(t_CKFLOAT newRatio); |
| 4272 | t_CKFLOAT getRatio(); |
| 4273 | }; |
| 4274 | |
| 4275 | //setters for the timing constants |
| 4276 | void Dyno_Data::setAttackTime(t_CKDUR t) { |
| 4277 | at = computeTimeConst(t); |
| 4278 | } |
| 4279 | |
| 4280 | void Dyno_Data::setReleaseTime(t_CKDUR t) { |
| 4281 | rt = computeTimeConst(t); |
| 4282 | } |
| 4283 | |
| 4284 | void Dyno_Data::setRatio(t_CKFLOAT newRatio) { |
| 4285 | this->slopeAbove = 1.0 / newRatio; |
| 4286 | this->slopeBelow = 1.0; |
| 4287 | } |
| 4288 | |
| 4289 | t_CKFLOAT Dyno_Data::getRatio() |
| 4290 | { |
| 4291 | return this->slopeBelow / this->slopeAbove; |
| 4292 | } |
| 4293 | |
| 4294 | //TODO: come up with better/good presets? |
| 4295 | |
| 4296 | //presets for the dynomics processor |
| 4297 | void Dyno_Data::limit() { |
| 4298 | slopeAbove = 0.1; // 10:1 compression above thresh |
| 4299 | slopeBelow = 1.0; // no compression below |
| 4300 | thresh = 0.5; |
| 4301 | at = computeTimeConst( 5.0 * ms ); |
| 4302 | rt = computeTimeConst( 300.0 * ms ); |
| 4303 | externalSideInput = 0; |
| 4304 | } |
| 4305 | |
| 4306 | void Dyno_Data::compress() { |
| 4307 | slopeAbove = 0.5; // 2:1 compression |
| 4308 | slopeBelow = 1.0; |
| 4309 | thresh = 0.5; |
| 4310 | at = computeTimeConst( 5.0 * ms ); |
| 4311 | rt = computeTimeConst( 500.0 * ms ); |
| 4312 | externalSideInput = 0; |
| 4313 | } |
| 4314 | |
| 4315 | void Dyno_Data::gate() { |
| 4316 | slopeAbove = 1.0; |
| 4317 | slopeBelow = 100000000; // infinity (more or less) |
| 4318 | thresh = 0.1; |
| 4319 | at = computeTimeConst( 11.0 * ms ); |
| 4320 | rt = computeTimeConst( 100.0 * ms ); |
| 4321 | externalSideInput = 0; |
| 4322 | } |
| 4323 | |
| 4324 | void Dyno_Data::expand() { |
| 4325 | slopeAbove = 2.0; // 1:2 expansion |
| 4326 | slopeBelow = 1.0; |
| 4327 | thresh = 0.5; |
| 4328 | at = computeTimeConst( 20.0 * ms ); |
| 4329 | rt = computeTimeConst( 400.0 * ms ); |
| 4330 | externalSideInput = 0; |
| 4331 | } |
| 4332 | |
| 4333 | void Dyno_Data::duck() { |
| 4334 | slopeAbove = 0.5; // when sideInput rises above thresh, gain starts going |
| 4335 | slopeBelow = 1.0; // down. it'll drop more as sideInput gets louder. |
| 4336 | thresh = 0.1; |
| 4337 | at = computeTimeConst( 10.0 * ms ); |
| 4338 | rt = computeTimeConst( 1000.0 * ms ); |
| 4339 | externalSideInput = 1; |
| 4340 | } |
| 4341 | |
| 4342 | |
| 4343 | // controls for the preset modes |
| 4344 | CK_DLL_CTRL( dyno_ctrl_limit ) { |
| 4345 | Dyno_Data * d = ( Dyno_Data * )OBJ_MEMBER_UINT(SELF, dyno_offset_data); |
| 4346 | d->limit(); |
| 4347 | } |
| 4348 | |
| 4349 | CK_DLL_CTRL( dyno_ctrl_compress ) { |
| 4350 | Dyno_Data * d = ( Dyno_Data * )OBJ_MEMBER_UINT(SELF, dyno_offset_data); |
| 4351 | d->compress(); |
| 4352 | } |
| 4353 | |
| 4354 | CK_DLL_CTRL( dyno_ctrl_gate ) { |
| 4355 | Dyno_Data * d = ( Dyno_Data * )OBJ_MEMBER_UINT(SELF, dyno_offset_data); |
| 4356 | d->gate(); |
| 4357 | } |
| 4358 | |
| 4359 | CK_DLL_CTRL( dyno_ctrl_expand ) { |
| 4360 | Dyno_Data * d = ( Dyno_Data * )OBJ_MEMBER_UINT(SELF, dyno_offset_data); |
| 4361 | d->expand(); |
| 4362 | } |
| 4363 | |
| 4364 | CK_DLL_CTRL( dyno_ctrl_duck ) { |
| 4365 | Dyno_Data * d = ( Dyno_Data * )OBJ_MEMBER_UINT(SELF, dyno_offset_data); |
| 4366 | d->duck(); |
| 4367 | } |
| 4368 | |
| 4369 | //additional controls: thresh |
| 4370 | CK_DLL_CTRL( dyno_ctrl_thresh ) { |
| 4371 | Dyno_Data * d = ( Dyno_Data * )OBJ_MEMBER_UINT(SELF, dyno_offset_data); |
| 4372 | d->thresh = GET_CK_FLOAT(ARGS); |
| 4373 | RETURN->v_float = (t_CKFLOAT)d->thresh; |
| 4374 | } |
| 4375 | |
| 4376 | CK_DLL_CGET( dyno_cget_thresh ) { |
| 4377 | Dyno_Data * d = ( Dyno_Data * )OBJ_MEMBER_UINT(SELF, dyno_offset_data); |
| 4378 | RETURN->v_float = (t_CKFLOAT)d->thresh; |
| 4379 | } |
| 4380 | |
| 4381 | //additional controls: attackTime |
| 4382 | CK_DLL_CTRL( dyno_ctrl_attackTime ) { |
| 4383 | Dyno_Data * d = ( Dyno_Data * )OBJ_MEMBER_UINT(SELF, dyno_offset_data); |
| 4384 | d->setAttackTime( GET_CK_FLOAT(ARGS) ); |
| 4385 | RETURN->v_dur = d->timeConstToDur(d->at); |
| 4386 | } |
| 4387 | |
| 4388 | CK_DLL_CGET( dyno_cget_attackTime ) { |
| 4389 | Dyno_Data * d = ( Dyno_Data * )OBJ_MEMBER_UINT(SELF, dyno_offset_data); |
| 4390 | RETURN->v_dur = d->timeConstToDur(d->at); |
| 4391 | } |
| 4392 | |
| 4393 | //additional controls: releaseTime |
| 4394 | CK_DLL_CTRL( dyno_ctrl_releaseTime ) { |
| 4395 | Dyno_Data * d = ( Dyno_Data * )OBJ_MEMBER_UINT(SELF, dyno_offset_data); |
| 4396 | d->setReleaseTime( GET_CK_FLOAT(ARGS) ); |
| 4397 | RETURN->v_dur = d->timeConstToDur(d->rt); |
| 4398 | } |
| 4399 | |
| 4400 | CK_DLL_CGET( dyno_cget_releaseTime ) { |
| 4401 | Dyno_Data * d = ( Dyno_Data * )OBJ_MEMBER_UINT(SELF, dyno_offset_data); |
| 4402 | RETURN->v_dur = d->timeConstToDur(d->rt); |
| 4403 | } |
| 4404 | |
| 4405 | //additional controls: ratio |
| 4406 | CK_DLL_CTRL( dyno_ctrl_ratio ) { |
| 4407 | Dyno_Data * d = ( Dyno_Data * )OBJ_MEMBER_UINT(SELF, dyno_offset_data); |
| 4408 | d->setRatio( GET_CK_FLOAT(ARGS) ); |
| 4409 | RETURN->v_float = d->getRatio(); |
| 4410 | } |
| 4411 | |
| 4412 | CK_DLL_CGET( dyno_cget_ratio ) { |
| 4413 | Dyno_Data * d = ( Dyno_Data * )OBJ_MEMBER_UINT(SELF, dyno_offset_data); |
| 4414 | RETURN->v_float = d->getRatio(); |
| 4415 | } |
| 4416 | |
| 4417 | //additional controls: slopeBelow |
| 4418 | CK_DLL_CTRL( dyno_ctrl_slopeBelow ) { |
| 4419 | Dyno_Data * d = ( Dyno_Data * )OBJ_MEMBER_UINT(SELF, dyno_offset_data); |
| 4420 | d->slopeBelow = GET_CK_FLOAT(ARGS); |
| 4421 | |
| 4422 | RETURN->v_float = d->slopeBelow; |
| 4423 | } |
| 4424 | |
| 4425 | CK_DLL_CGET( dyno_cget_slopeBelow ) { |
| 4426 | Dyno_Data * d = ( Dyno_Data * )OBJ_MEMBER_UINT(SELF, dyno_offset_data); |
| 4427 | RETURN->v_float = d->slopeBelow; |
| 4428 | } |
| 4429 | |
| 4430 | //additional controls: slopeAbove |
| 4431 | CK_DLL_CTRL( dyno_ctrl_slopeAbove ) { |
| 4432 | Dyno_Data * d = ( Dyno_Data * )OBJ_MEMBER_UINT(SELF, dyno_offset_data); |
| 4433 | d->slopeAbove = GET_CK_FLOAT(ARGS); |
| 4434 | |
| 4435 | RETURN->v_float = d->slopeAbove; |
| 4436 | } |
| 4437 | |
| 4438 | CK_DLL_CGET( dyno_cget_slopeAbove ) { |
| 4439 | Dyno_Data * d = ( Dyno_Data * )OBJ_MEMBER_UINT(SELF, dyno_offset_data); |
| 4440 | RETURN->v_float = d->slopeAbove; |
| 4441 | } |
| 4442 | |
| 4443 | //additional controls: sideInput |
| 4444 | CK_DLL_CTRL( dyno_ctrl_sideInput ) { |
| 4445 | Dyno_Data * d = ( Dyno_Data * )OBJ_MEMBER_UINT(SELF, dyno_offset_data); |
| 4446 | d->sideInput = GET_CK_FLOAT(ARGS); |
| 4447 | |
| 4448 | RETURN->v_float = d->sideInput; |
| 4449 | } |
| 4450 | |
| 4451 | CK_DLL_CGET( dyno_cget_sideInput ) { |
| 4452 | Dyno_Data * d = ( Dyno_Data * )OBJ_MEMBER_UINT(SELF, dyno_offset_data); |
| 4453 | RETURN->v_float = d->sideInput; |
| 4454 | } |
| 4455 | |
| 4456 | //additional controls: externalSideInput |
| 4457 | CK_DLL_CTRL( dyno_ctrl_externalSideInput ) { |
| 4458 | Dyno_Data * d = ( Dyno_Data * )OBJ_MEMBER_UINT(SELF, dyno_offset_data); |
| 4459 | d->externalSideInput = GET_CK_INT(ARGS); |
| 4460 | |
| 4461 | RETURN->v_int = d->externalSideInput; |
| 4462 | } |
| 4463 | |
| 4464 | CK_DLL_CGET( dyno_cget_externalSideInput ) { |
| 4465 | Dyno_Data * d = ( Dyno_Data * )OBJ_MEMBER_UINT(SELF, dyno_offset_data); |
| 4466 | RETURN->v_int = d->externalSideInput; |
| 4467 | } |
| 4468 | |
| 4469 | //constructor |
| 4470 | CK_DLL_CTOR( dyno_ctor ) |
| 4471 | { |
| 4472 | OBJ_MEMBER_UINT(SELF, dyno_offset_data) = (t_CKUINT)new Dyno_Data( SHRED->vm_ref ); |
| 4473 | } |
| 4474 | |
| 4475 | CK_DLL_DTOR( dyno_dtor ) |
| 4476 | { |
| 4477 | delete (Dyno_Data *)OBJ_MEMBER_UINT(SELF, dyno_offset_data); |
| 4478 | OBJ_MEMBER_UINT(SELF, dyno_offset_data) = 0; |
| 4479 | } |
| 4480 | |
| 4481 | // recomputes envelope, determines how the current amp envelope compares with |
| 4482 | // thresh, applies the appropriate new slope depending on how far above/below |
| 4483 | // the threshold the current envelope is. |
| 4484 | CK_DLL_TICK( dyno_tick ) |
| 4485 | { |
| 4486 | Dyno_Data * d = ( Dyno_Data * )OBJ_MEMBER_UINT(SELF, dyno_offset_data); |
| 4487 | |
| 4488 | // only change sideInput if we're not using an external ctrl signal. |
| 4489 | // otherwise we'll just use whatever the user sent us last as the ctrl signal |
| 4490 | if(!d->externalSideInput) |
| 4491 | d->sideInput = in >= 0 ? in : -in; |
| 4492 | |
| 4493 | // 'a' is signal left after subtracting xd (to recompute sideChain envelope) |
| 4494 | double a = d->sideInput - d->xd; |
| 4495 | // a is only needed if positive to pull the envelope up, not to bring it down |
| 4496 | if ( a < 0 ) a=0; |
| 4497 | // the attack/release (peak) exponential filter to guess envelope |
| 4498 | d->xd = d->xd * (1 - d->rt) + d->at * a; |
| 4499 | |
| 4500 | // if you were to use the rms filter, |
| 4501 | // it would probably look, a sumpthin' like this |
| 4502 | // d->xd = TAV * in*in + (1+TAV) * d->xd |
| 4503 | |
| 4504 | // decide which slope to use, depending on whether we're below/above thresh |
| 4505 | double slope = d->xd > d->thresh ? d->slopeAbove : d->slopeBelow; |
| 4506 | // the gain function - apply the slope chosen above |
| 4507 | double f = slope == 1.0 ? 1.0 : pow( d->xd / d->thresh, slope - 1.0 ); |
| 4508 | |
| 4509 | // apply the gain found above to input sample |
| 4510 | *out = f * in; |
| 4511 | |
| 4512 | return TRUE; |
| 4513 | } |
| 4514 | |
| 4515 | |
| 4516 | |
| 4517 | /* |
| 4518 | LiSa updates/fixes: |
| 4519 | 1. added playing(int voice) method. returns whether a voice is playing |
| 4520 | 2. added getbi(int voice) method. returns whether a voice is bi |
| 4521 | 3. added multichan; pan(int voice, float where (between 0. and C-1) ) method, to equal-power place across the channels |
| 4522 | 4. corrected loopEnd so it is the same as duration, not 1 less than duration, when set... |
| 4523 | */ |
| 4524 | |
| 4525 | #define LiSa_MAXVOICES 256 // 1.4.1.0 (ge) increased from 200 |
| 4526 | #define LiSa_MAXBUFSIZE 441000000 // 1.5.5.6 (nshaheed) increased by an order of magnitude from 44100000 |
| 4527 | //----------------------------------------------------------------------------- |
| 4528 | // name: LiSaMulti_data |
| 4529 | // desc: ... |
| 4530 | //----------------------------------------------------------------------------- |
| 4531 | struct LiSaMulti_data |
| 4532 | { |
| 4533 | SAMPLE * mdata; |
| 4534 | SAMPLE * outsamples; // samples by channel to send out |
| 4535 | t_CKINT mdata_len; |
| 4536 | t_CKINT maxvoices; |
| 4537 | t_CKINT loop_start[LiSa_MAXVOICES], loop_end[LiSa_MAXVOICES], loop_end_rec; |
| 4538 | t_CKINT rindex; // record and play indices |
| 4539 | t_CKBOOL record, looprec, loopplay[LiSa_MAXVOICES], reset, append, play[LiSa_MAXVOICES], bi[LiSa_MAXVOICES]; |
| 4540 | t_CKFLOAT coeff; // feedback coeff |
| 4541 | t_CKFLOAT voiceGain[LiSa_MAXVOICES]; // gain control for each voice |
| 4542 | t_CKFLOAT voicePan[LiSa_MAXVOICES]; // pan control for each voice; places voice between any pair of channels. |
| 4543 | t_CKFLOAT channelGain[LiSa_MAXVOICES][LiSa_MAXCHANNELS]; |
| 4544 | // 1.5.5.1 (ge & nick) added pindex_now to address discrepancy of pindex between getNextSample and playPos |
| 4545 | t_CKDOUBLE p_inc[LiSa_MAXVOICES], pindex[LiSa_MAXVOICES], pindex_now[LiSa_MAXVOICES]; // playback increment |
| 4546 | |
| 4547 | // ramp stuff |
| 4548 | t_CKDOUBLE rampup_len[LiSa_MAXVOICES], rampdown_len[LiSa_MAXVOICES], rec_ramplen, rec_ramplen_inv; |
| 4549 | t_CKDOUBLE rampup_len_inv[LiSa_MAXVOICES], rampdown_len_inv[LiSa_MAXVOICES]; |
| 4550 | t_CKDOUBLE rampctr[LiSa_MAXVOICES]; |
| 4551 | t_CKBOOL rampup[LiSa_MAXVOICES], rampdown[LiSa_MAXVOICES]; |
| 4552 | |
| 4553 | t_CKINT track; |
| 4554 | t_CKINT num_chans; |
| 4555 | |
| 4556 | // constructor; 1.4.1.0 (ge) added |
| 4557 | LiSaMulti_data() |
| 4558 | : mdata(NULL), outsamples(NULL), mdata_len(0), maxvoices(0), |
| 4559 | loop_end_rec(0), rindex(0), record(FALSE), looprec(FALSE), reset(FALSE), |
| 4560 | append(FALSE), coeff(0), rec_ramplen(0), rec_ramplen_inv(0), |
| 4561 | track(0), num_chans(0) |
| 4562 | { |
| 4563 | // zero out | 1.5.0.0 (ge) | added (instead of big memset outside!) |
| 4564 | memset( loop_start, 0, sizeof(loop_start) ); |
| 4565 | memset( loop_end, 0, sizeof(loop_end) ); |
| 4566 | memset( loopplay, 0, sizeof(loopplay) ); |
| 4567 | memset( play, 0, sizeof(play) ); |
| 4568 | memset( bi, 0, sizeof(bi) ); |
| 4569 | memset( voiceGain, 0, sizeof(voiceGain) ); |
| 4570 | memset( voicePan, 0, sizeof(voicePan) ); |
| 4571 | memset( channelGain, 0, sizeof(channelGain) ); |
| 4572 | memset( p_inc, 0, sizeof(p_inc) ); |
| 4573 | memset( pindex, 0, sizeof(pindex) ); |
| 4574 | memset( pindex, 0, sizeof(pindex_now) ); |
| 4575 | memset( rampup_len, 0, sizeof(rampup_len) ); |
| 4576 | memset( rampdown_len, 0, sizeof(rampdown_len) ); |
| 4577 | memset( rampup_len_inv, 0, sizeof(rampup_len_inv) ); |
| 4578 | memset( rampdown_len_inv, 0, sizeof(rampdown_len_inv) ); |
| 4579 | memset( rampctr, 0, sizeof(rampctr) ); |
| 4580 | memset( rampup, 0, sizeof(rampup) ); |
| 4581 | memset( rampdown, 0, sizeof(rampdown) ); |
| 4582 | |
| 4583 | // 1.5.0.0 (ge) moved to constructor from buffer_alloc() |
| 4584 | // ...in case user sets this before allocating |
| 4585 | // 1.4.1.0 (ge) increased from 10 to 16 |
| 4586 | // default maxvoices; user can set |
| 4587 | maxvoices = 16; |
| 4588 | |
| 4589 | // initialize more stuff |
| 4590 | rec_ramplen = 0.; |
| 4591 | rec_ramplen_inv = 1.; |
| 4592 | track = 0; |
| 4593 | |
| 4594 | // initialize to reasonable values |
| 4595 | // 1.5.0.0 (ge) moved to constructor from buffer_alloc() |
| 4596 | for( t_CKINT i=0; i < LiSa_MAXVOICES; i++ ) |
| 4597 | { |
| 4598 | pindex[i] = rindex = 0; |
| 4599 | pindex_now[i] = pindex[i]; // added 1.5.5.1 (ge & nick) |
| 4600 | play[i] = record = bi[i] = false; |
| 4601 | looprec = loopplay[i] = true; |
| 4602 | coeff = 0.; |
| 4603 | p_inc[i] = 1.; |
| 4604 | voiceGain[i] = 1.; |
| 4605 | voicePan[i] = 0.5; |
| 4606 | |
| 4607 | // ramp stuff |
| 4608 | rampup[i] = rampdown[i] = false; |
| 4609 | rampup_len[i] = rampdown_len[i] = 0.; |
| 4610 | rampup_len_inv[i] = rampdown_len_inv[i] = 1.; |
| 4611 | rampctr[i] = 0.; |
| 4612 | |
| 4613 | for(t_CKINT j=2; j<num_chans; j++) { |
| 4614 | channelGain[i][j] = 1.; |
| 4615 | } |
| 4616 | channelGain[i][0] = 1.0; |
| 4617 | // channelGain[i][0] = 0.707; |
| 4618 | // channelGain[i][1] = 0.707; |
| 4619 | } |
| 4620 | } |
| 4621 | |
| 4622 | // destructor | 1.5.0.0 (ge) added |
| 4623 | ~LiSaMulti_data() |
| 4624 | { |
| 4625 | // cleanup |
| 4626 | cleanup(); |
| 4627 | } |
| 4628 | |
| 4629 | // clean up LiSa |
| 4630 | void cleanup() |
| 4631 | { |
| 4632 | // deallocate |
| 4633 | CK_SAFE_FREE( mdata ); |
| 4634 | } |
| 4635 | |
| 4636 | // allocate memory, length in samples |
| 4637 | /* inline */ int buffer_alloc(t_CKINT length) |
| 4638 | { |
| 4639 | // cleanup | 1.5.0.0 (ge) added |
| 4640 | cleanup(); |
| 4641 | |
| 4642 | // allocate |
| 4643 | mdata = (SAMPLE *)malloc((length + 1) * sizeof(SAMPLE)); // extra sample for safety.... |
| 4644 | if( !mdata ) { |
| 4645 | CK_FPRINTF_STDERR( "LiSaBasic: unable to allocate memory!\n" ); |
| 4646 | return false; |
| 4647 | } |
| 4648 | // zero out |
| 4649 | memset( mdata, 0, (length + 1) * sizeof(SAMPLE) ); |
| 4650 | // remember data length |
| 4651 | mdata_len = length; |
| 4652 | |
| 4653 | for( t_CKINT i=0; i < LiSa_MAXVOICES; i++ ) |
| 4654 | { |
| 4655 | loop_start[i] = 0; |
| 4656 | //loop_end[i] = length - 1; //no idea why i had this |
| 4657 | loop_end[i] = length; |
| 4658 | loop_end_rec = length; |
| 4659 | } |
| 4660 | |
| 4661 | return true; |
| 4662 | } |
| 4663 | |
| 4664 | // dump a sample into the buffer; retain existing sample, scaled by "coeff" |
| 4665 | inline void recordSamp( SAMPLE insample ) |
| 4666 | { |
| 4667 | SAMPLE tempsample; |
| 4668 | |
| 4669 | if( record ) |
| 4670 | { |
| 4671 | if( looprec ) |
| 4672 | { |
| 4673 | if(rindex >= loop_end_rec) rindex = loop_start[0]; |
| 4674 | tempsample = coeff * mdata[rindex] + insample; |
| 4675 | // mdata[rindex] = coeff * mdata[rindex] + insample; |
| 4676 | // rindex++; |
| 4677 | } |
| 4678 | else |
| 4679 | { |
| 4680 | if( rindex < loop_end_rec ) |
| 4681 | { |
| 4682 | //mdata[rindex] = coeff * mdata[rindex] + insample; |
| 4683 | tempsample = coeff * mdata[rindex] + insample; |
| 4684 | //rindex++; |
| 4685 | } |
| 4686 | else |
| 4687 | { |
| 4688 | record = 0; |
| 4689 | return; |
| 4690 | } |
| 4691 | } |
| 4692 | |
| 4693 | // ramp stuff here |
| 4694 | if( rindex < rec_ramplen ) |
| 4695 | { |
| 4696 | tempsample *= (rindex * rec_ramplen_inv); |
| 4697 | //CK_FPRINTF_STDERR( "ramping up %f\n", rindex * rec_ramplen_inv ); |
| 4698 | } |
| 4699 | else if( rindex > (loop_end_rec - rec_ramplen) ) |
| 4700 | { |
| 4701 | tempsample *= (loop_end_rec - rindex) * rec_ramplen_inv; |
| 4702 | //CK_FPRINTF_STDERR( "ramping down %f\n", (loop_end_rec - rindex) * rec_ramplen_inv ); |
| 4703 | } |
| 4704 | |
| 4705 | mdata[rindex] = tempsample; |
| 4706 | rindex++; |
| 4707 | } |
| 4708 | } |
| 4709 | |
| 4710 | // grab a sample from the buffer, with linear interpolation (add prc's SINC interp later) |
| 4711 | // increment play index |
| 4712 | // which specifies voice number |
| 4713 | inline SAMPLE getNextSamp(t_CKINT which) |
| 4714 | { |
| 4715 | // constrain |
| 4716 | if(loopplay[which]) { |
| 4717 | if(bi[which]) { // change direction if bidirectional mode |
| 4718 | if(pindex[which] >= loop_end[which] || pindex[which] < loop_start[which]) { //should be >= ? |
| 4719 | pindex[which] -= p_inc[which]; |
| 4720 | p_inc[which] = -p_inc[which]; |
| 4721 | } |
| 4722 | } |
| 4723 | if( loop_start[which] == loop_end[which] ) pindex[which] = loop_start[which]; //catch this condition to avoid infinite while loops |
| 4724 | else { |
| 4725 | while(pindex[which] >= loop_end[which]) pindex[which] = loop_start[which] + (pindex[which] - loop_end[which]); //again, >=? |
| 4726 | while(pindex[which] < loop_start[which]) pindex[which] = loop_end[which] - (loop_start[which] - pindex[which]); |
| 4727 | } |
| 4728 | |
| 4729 | } else if(pindex[which] >= mdata_len || pindex[which] < 0) { //should be >=, no? |
| 4730 | play[which] = 0; |
| 4731 | //CK_FPRINTF_STDERR( "turning voice %d off!\n", which ); |
| 4732 | return (SAMPLE) 0.; |
| 4733 | } |
| 4734 | |
| 4735 | // update pindex_now for accurate play position reporting between calls to this function | 1.5.5.1 (ge & nick) |
| 4736 | pindex_now[which] = pindex[which]; |
| 4737 | |
| 4738 | // interp |
| 4739 | t_CKINT whereTrunc = (t_CKINT) pindex[which]; |
| 4740 | t_CKDOUBLE whereFrac = pindex[which] - (t_CKDOUBLE)whereTrunc; |
| 4741 | t_CKINT whereNext = whereTrunc + 1; |
| 4742 | |
| 4743 | if (loopplay[which]) { |
| 4744 | if((whereNext) >= loop_end[which]) { |
| 4745 | whereNext = loop_start[which]; |
| 4746 | } |
| 4747 | if((whereTrunc) >= loop_end[which]) { |
| 4748 | whereTrunc = loop_start[which]; |
| 4749 | } |
| 4750 | } else { |
| 4751 | if((whereTrunc) >= mdata_len) { |
| 4752 | whereTrunc = mdata_len - 1; //should correct this, in case we've overshot by more than 1 sample |
| 4753 | whereNext = 0; |
| 4754 | } |
| 4755 | if((whereNext) >= mdata_len) { |
| 4756 | whereNext = 0; |
| 4757 | } |
| 4758 | } |
| 4759 | |
| 4760 | // advance play position |
| 4761 | pindex[which] += p_inc[which]; |
| 4762 | |
| 4763 | t_CKDOUBLE outsample; |
| 4764 | outsample = (t_CKDOUBLE)mdata[whereTrunc] + (t_CKDOUBLE)(mdata[whereNext] - mdata[whereTrunc]) * whereFrac; |
| 4765 | |
| 4766 | // ramp stuff |
| 4767 | if(rampup[which]) { |
| 4768 | outsample *= rampctr[which]++ * rampup_len_inv[which]; //remove divide |
| 4769 | if(rampctr[which] >= rampup_len[which]) rampup[which] = false; |
| 4770 | } |
| 4771 | else if(rampdown[which]) { |
| 4772 | outsample *= (rampdown_len[which] - rampctr[which]++) * rampdown_len_inv[which]; |
| 4773 | if(rampctr[which] >= rampdown_len[which]) { |
| 4774 | rampdown[which] = false; |
| 4775 | play[which] = false; |
| 4776 | } |
| 4777 | } |
| 4778 | |
| 4779 | outsample *= voiceGain[which]; |
| 4780 | |
| 4781 | return (SAMPLE)outsample; |
| 4782 | } |
| 4783 | |
| 4784 | // grab a sample from the buffer, with linear interpolation (add prc's SINC interp later) |
| 4785 | // given a position within the buffer |
| 4786 | inline SAMPLE getSamp(t_CKDOUBLE where, t_CKINT which) |
| 4787 | { |
| 4788 | // constrain |
| 4789 | if(where > loop_end[which]) where = loop_end[which]; |
| 4790 | if(where < loop_start[which]) where = loop_start[which]; |
| 4791 | |
| 4792 | // interp |
| 4793 | t_CKINT whereTrunc = (t_CKINT) where; |
| 4794 | t_CKDOUBLE whereFrac = where - (t_CKDOUBLE)whereTrunc; |
| 4795 | t_CKINT whereNext = whereTrunc + 1; |
| 4796 | |
| 4797 | if((whereNext) == loop_end[which]) whereNext = loop_start[which]; |
| 4798 | |
| 4799 | t_CKDOUBLE outsample; |
| 4800 | outsample = (t_CKDOUBLE)mdata[whereTrunc] + (t_CKDOUBLE)(mdata[whereNext] - mdata[whereTrunc]) * whereFrac; |
| 4801 | outsample *= voiceGain[which]; |
| 4802 | |
| 4803 | //add voiceGain ctl here; return (SAMPLE)vgain[which]*outsample; |
| 4804 | return (SAMPLE)outsample; |
| 4805 | } |
| 4806 | |
| 4807 | // ramp stuff |
| 4808 | inline void ramp_up( t_CKINT voicenum, t_CKDUR uptime ) |
| 4809 | { |
| 4810 | // CK_FPRINTF_STDERR( "ramping up voice %d", voicenum ); |
| 4811 | |
| 4812 | rampup[voicenum] = true; |
| 4813 | play[voicenum] = true; |
| 4814 | rampup_len[voicenum] = (t_CKDOUBLE)uptime; |
| 4815 | if(rampup_len[voicenum] > 0.) rampup_len_inv[voicenum] = 1./rampup_len[voicenum]; |
| 4816 | else rampup_len[voicenum] = 1.; |
| 4817 | |
| 4818 | // check to make sure we are not mid ramping down |
| 4819 | if(rampdown[voicenum]) { |
| 4820 | rampctr[voicenum] = rampup_len[voicenum] * (1. - rampctr[voicenum]/rampdown_len[voicenum]); |
| 4821 | rampdown[voicenum] = false; |
| 4822 | } else rampctr[voicenum] = 0; |
| 4823 | } |
| 4824 | |
| 4825 | inline void ramp_down(t_CKINT voicenum, t_CKDUR downtime) |
| 4826 | { |
| 4827 | rampdown[voicenum] = true; |
| 4828 | rampdown_len[voicenum] = (t_CKDOUBLE)downtime; |
| 4829 | if(rampdown_len[voicenum] > 0.) rampdown_len_inv[voicenum] = 1./rampdown_len[voicenum]; |
| 4830 | else rampdown_len[voicenum] = 1.; |
| 4831 | |
| 4832 | // check to make sure we are not mid ramping up |
| 4833 | if(rampup[voicenum]) { |
| 4834 | rampctr[voicenum] = rampdown_len[voicenum] * (1. - rampctr[voicenum]/rampup_len[voicenum]); |
| 4835 | rampup[voicenum] = false; |
| 4836 | } else rampctr[voicenum] = 0; |
| 4837 | } |
| 4838 | |
| 4839 | inline void set_rec_ramplen(t_CKDUR newlen) |
| 4840 | { |
| 4841 | rec_ramplen = (t_CKDOUBLE)newlen; |
| 4842 | if(rec_ramplen > 0.) rec_ramplen_inv = 1./rec_ramplen; |
| 4843 | else rec_ramplen_inv = 1.; |
| 4844 | //fprintf ( stderr, "rec_ramplen = %f, inv = %f \n", rec_ramplen, rec_ramplen_inv ); |
| 4845 | } |
| 4846 | |
| 4847 | // for simple stereo panning of a particular voice, and... |
| 4848 | // l.channelGain(voice, channel, gain) |
| 4849 | // to set the gain for a particular voice going to a particular channel; |
| 4850 | // ...good for > 2 voices (like 6 channels!) |
| 4851 | inline SAMPLE * tick_multi( SAMPLE in ) |
| 4852 | { |
| 4853 | // local sample var |
| 4854 | SAMPLE tempsample = 0; |
| 4855 | // zero out |
| 4856 | for( t_CKINT i = 0; i < num_chans; i++ ) outsamples[i] = 0.; |
| 4857 | // check |
| 4858 | if( !mdata ) return outsamples; |
| 4859 | |
| 4860 | // record the sample |
| 4861 | recordSamp(in); |
| 4862 | |
| 4863 | if( track == 0 ) |
| 4864 | { |
| 4865 | for( t_CKINT i=0; i < maxvoices; i++ ) |
| 4866 | { |
| 4867 | if( play[i] ) |
| 4868 | { |
| 4869 | tempsample = getNextSamp(i); |
| 4870 | for( t_CKINT j=0; j<num_chans; j++ ) |
| 4871 | { |
| 4872 | // channelGain should return gain for voice i in channel j |
| 4873 | outsamples[j] += tempsample * channelGain[i][j]; |
| 4874 | // outsamples[j] += tempsample; //mono for now, testing... |
| 4875 | } |
| 4876 | } |
| 4877 | } |
| 4878 | } |
| 4879 | else if( track == 1 ) |
| 4880 | { |
| 4881 | if( in < 0.0 ) in = -in; |
| 4882 | for( t_CKINT i = 0; i < maxvoices; i++ ) |
| 4883 | { |
| 4884 | if( play[i] ) |
| 4885 | { |
| 4886 | t_CKDOUBLE location = loop_start[i] + (t_CKDOUBLE)in * (loop_end[i] - loop_start[i]); |
| 4887 | tempsample = getSamp(location, i); |
| 4888 | |
| 4889 | // spencer 2013/5/13: fix LiSa track-mode multichannel |
| 4890 | for( t_CKINT j = 0; j < num_chans; j++ ) |
| 4891 | { |
| 4892 | // channelGain should return gain for voice i in channel j |
| 4893 | outsamples[j] += tempsample * channelGain[i][j]; |
| 4894 | } |
| 4895 | } |
| 4896 | } |
| 4897 | } |
| 4898 | else if( track == 2 && play[0] ) |
| 4899 | { |
| 4900 | if( in < 0.0 ) in = -in; // only use voice 0 when tracking with durs. |
| 4901 | tempsample = getSamp( (t_CKDOUBLE)in, 0 ); |
| 4902 | |
| 4903 | // spencer 2013/5/13: fix LiSa track-mode multichannel |
| 4904 | for( t_CKINT j = 0; j < num_chans; j++ ) |
| 4905 | { |
| 4906 | // channelGain should return gain for voice i in channel j |
| 4907 | outsamples[j] += tempsample * channelGain[0][j]; |
| 4908 | // outsamples[j] += tempsample; // mono for now, testing... |
| 4909 | } |
| 4910 | } |
| 4911 | |
| 4912 | return outsamples; |
| 4913 | } |
| 4914 | |
| 4915 | inline void clear_buf() |
| 4916 | { |
| 4917 | for( t_CKINT i = 0; i < mdata_len; i++ ) |
| 4918 | mdata[i] = 0.; |
| 4919 | } |
| 4920 | |
| 4921 | inline t_CKINT get_free_voice() |
| 4922 | { |
| 4923 | t_CKINT voicenumber = 0; |
| 4924 | while( play[voicenumber] && voicenumber < maxvoices ) |
| 4925 | { |
| 4926 | voicenumber++; |
| 4927 | } |
| 4928 | if( voicenumber == maxvoices ) voicenumber = -1; |
| 4929 | return voicenumber; |
| 4930 | } |
| 4931 | |
| 4932 | // stick sample in record buffer |
| 4933 | inline void pokeSample( SAMPLE insample, t_CKINT index ) |
| 4934 | { |
| 4935 | if( index >= mdata_len || index < 0 ) |
| 4936 | { |
| 4937 | index = 0; |
| 4938 | CK_FPRINTF_STDERR( "LiSa: trying to put sample out of buffer range; ignoring" ); |
| 4939 | } |
| 4940 | else |
| 4941 | { |
| 4942 | mdata[index] = insample; |
| 4943 | } |
| 4944 | } |
| 4945 | |
| 4946 | // grab sample directly from record buffer, with linear interpolation |
| 4947 | inline SAMPLE grabSample( t_CKDOUBLE where ) |
| 4948 | { |
| 4949 | if( where > mdata_len || where < 0 ) |
| 4950 | { |
| 4951 | where = 0; |
| 4952 | CK_FPRINTF_STDERR( "LiSa: trying to grab sample out of buffer range; ignoring" ); |
| 4953 | return 0.; |
| 4954 | } |
| 4955 | else |
| 4956 | { |
| 4957 | // interp |
| 4958 | t_CKINT whereTrunc = (t_CKINT) where; |
| 4959 | t_CKDOUBLE whereFrac = where - (t_CKDOUBLE)whereTrunc; |
| 4960 | t_CKINT whereNext = whereTrunc + 1; |
| 4961 | |
| 4962 | if( (whereNext) == mdata_len ) whereNext = 0; |
| 4963 | |
| 4964 | t_CKDOUBLE outsample; |
| 4965 | outsample = (t_CKDOUBLE)mdata[whereTrunc] + (t_CKDOUBLE)(mdata[whereNext] - mdata[whereTrunc]) * whereFrac; |
| 4966 | |
| 4967 | // add voiceGain ctl here; return (SAMPLE)vgain[which]*outsample; |
| 4968 | return (SAMPLE)outsample; |
| 4969 | } |
| 4970 | } |
| 4971 | }; |
| 4972 | |
| 4973 | |
| 4974 | |
| 4975 | |
| 4976 | //++++++++++++++++++++++++++++++++++++++++ |
| 4977 | // |
| 4978 | // |
| 4979 | //++++++++++++++++++++++++++++++++++++++++ |
| 4980 | // |
| 4981 | // |
| 4982 | //++++++++++++++++++++++++++++++++++++++++ |
| 4983 | |
| 4984 | |
| 4985 | |
| 4986 | |
| 4987 | //----------------------------------------------------------------------------- |
| 4988 | // name: LiSaMulti_ctor() |
| 4989 | // desc: CTOR function ... |
| 4990 | //----------------------------------------------------------------------------- |
| 4991 | CK_DLL_CTOR( LiSaMulti_ctor ) |
| 4992 | { |
| 4993 | // instantiate an internal LiSa implementation instance |
| 4994 | LiSaMulti_data * f = new LiSaMulti_data; |
| 4995 | // get the LiSa object in ChucK |
| 4996 | Chuck_UGen * ugen = (Chuck_UGen *)SELF; |
| 4997 | // set channel information |
| 4998 | f->num_chans = ugen->m_multi_chan_size > 0 ? ugen->m_multi_chan_size : 1; |
| 4999 | // CK_FPRINTF_STDERR( "LiSa: number of channels = %d\n", f->num_chans ); |
| 5000 | // allocate array of samples based on channel information |
| 5001 | f->outsamples = new SAMPLE[f->num_chans]; |
| 5002 | // zero out |
| 5003 | memset( f->outsamples, 0, (f->num_chans)*sizeof(SAMPLE) ); |
| 5004 | |
| 5005 | // remember implementation instance in address space of LiSa object in ChucK |
| 5006 | OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data) = (t_CKUINT)f; |
| 5007 | } |
| 5008 | |
| 5009 | |
| 5010 | |
| 5011 | |
| 5012 | //----------------------------------------------------------------------------- |
| 5013 | // name: LiSaMulti_ctor_path() |
| 5014 | // desc: 1.5.5.6 (nshaheed) constructor that takes a path and load the file into the buffer |
| 5015 | //----------------------------------------------------------------------------- |
| 5016 | CK_DLL_CTOR( LiSaMulti_ctor_path ) |
| 5017 | { |
| 5018 | Chuck_DL_Return RETURN; |
| 5019 | // read it |
| 5020 | LiSaMulti_ctrl_read( SELF, ARGS, &RETURN, VM, SHRED, API ); |
| 5021 | } |
| 5022 | //----------------------------------------------------------------------------- |
| 5023 | // name: LiSa2_ctor_path() |
| 5024 | // desc: 1.5.5.6 (nshaheed) variations on base base LiSa constructor |
| 5025 | //----------------------------------------------------------------------------- |
| 5026 | CK_DLL_CTOR( LiSa2_ctor_path ) |
| 5027 | { |
| 5028 | // call up to base LiSa corresponding constructor |
| 5029 | LiSaMulti_ctor_path( SELF, ARGS, VM, SHRED, API ); |
| 5030 | } |
| 5031 | //----------------------------------------------------------------------------- |
| 5032 | // name: LiSa4_ctor_path() |
| 5033 | // desc: 1.5.5.6 (nshaheed) variations on base base LiSa constructor |
| 5034 | //----------------------------------------------------------------------------- |
| 5035 | CK_DLL_CTOR( LiSa4_ctor_path ) |
| 5036 | { |
| 5037 | // call up to base LiSa corresponding constructor |
| 5038 | LiSaMulti_ctor_path( SELF, ARGS, VM, SHRED, API ); |
| 5039 | } |
| 5040 | //----------------------------------------------------------------------------- |
| 5041 | // name: LiSa6_ctor_path() |
| 5042 | // desc: 1.5.5.6 (nshaheed) variations on base base LiSa constructor |
| 5043 | //----------------------------------------------------------------------------- |
| 5044 | CK_DLL_CTOR( LiSa6_ctor_path ) |
| 5045 | { |
| 5046 | // call up to base LiSa corresponding constructor |
| 5047 | LiSaMulti_ctor_path( SELF, ARGS, VM, SHRED, API ); |
| 5048 | } |
| 5049 | //----------------------------------------------------------------------------- |
| 5050 | // name: LiSa8_ctor_path() |
| 5051 | // desc: 1.5.5.6 (nshaheed) variations on base base LiSa constructor |
| 5052 | //----------------------------------------------------------------------------- |
| 5053 | CK_DLL_CTOR( LiSa8_ctor_path ) |
| 5054 | { |
| 5055 | // call up to base LiSa corresponding constructor |
| 5056 | LiSaMulti_ctor_path( SELF, ARGS, VM, SHRED, API ); |
| 5057 | } |
| 5058 | //----------------------------------------------------------------------------- |
| 5059 | // name: LiSa10_ctor_path() |
| 5060 | // desc: 1.5.5.6 (nshaheed) variations on base base LiSa constructor |
| 5061 | //----------------------------------------------------------------------------- |
| 5062 | CK_DLL_CTOR( LiSa10_ctor_path ) |
| 5063 | { |
| 5064 | // call up to base LiSa corresponding constructor |
| 5065 | LiSaMulti_ctor_path( SELF, ARGS, VM, SHRED, API ); |
| 5066 | } |
| 5067 | //----------------------------------------------------------------------------- |
| 5068 | // name: LiSa16_ctor_path() |
| 5069 | // desc: 1.5.5.6 (nshaheed) variations on base base LiSa constructor |
| 5070 | //----------------------------------------------------------------------------- |
| 5071 | CK_DLL_CTOR( LiSa16_ctor_path ) |
| 5072 | { |
| 5073 | // call up to base LiSa corresponding constructor |
| 5074 | LiSaMulti_ctor_path( SELF, ARGS, VM, SHRED, API ); |
| 5075 | } |
| 5076 | |
| 5077 | |
| 5078 | |
| 5079 | //----------------------------------------------------------------------------- |
| 5080 | // name: LiSaMulti_dtor() |
| 5081 | // desc: DTOR function ... |
| 5082 | //----------------------------------------------------------------------------- |
| 5083 | CK_DLL_DTOR( LiSaMulti_dtor ) |
| 5084 | { |
| 5085 | // get data |
| 5086 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5087 | // clean up | 1.5.0.0 (ge) added |
| 5088 | if ( d ) CK_SAFE_DELETE_ARRAY( d->outsamples ); |
| 5089 | // delete |
| 5090 | CK_SAFE_DELETE(d); |
| 5091 | // set |
| 5092 | OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data) = 0; |
| 5093 | } |
| 5094 | |
| 5095 | |
| 5096 | |
| 5097 | |
| 5098 | //----------------------------------------------------------------------------- |
| 5099 | // name: () |
| 5100 | // desc: TICK function ... |
| 5101 | //----------------------------------------------------------------------------- |
| 5102 | CK_DLL_TICK( LiSaMulti_tick ) |
| 5103 | { |
| 5104 | // get "this" |
| 5105 | // Chuck_UGen * ugen = (Chuck_UGen *)SELF; |
| 5106 | |
| 5107 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5108 | SAMPLE * temp_out_samples = d->tick_multi( in ); |
| 5109 | |
| 5110 | // for( t_CKUINT i = 0; i < ugen->m_multi_chan_size; i++ ) |
| 5111 | // // yay this works! |
| 5112 | // ugen->m_multi_chan[i]->m_sum = ugen->m_multi_chan[i]->m_current = temp_out_samples[i]; |
| 5113 | *out = temp_out_samples[0]; |
| 5114 | |
| 5115 | return TRUE; |
| 5116 | } |
| 5117 | |
| 5118 | |
| 5119 | |
| 5120 | |
| 5121 | //----------------------------------------------------------------------------- |
| 5122 | // name: () |
| 5123 | // desc: TICKF function ... |
| 5124 | //----------------------------------------------------------------------------- |
| 5125 | CK_DLL_TICKF( LiSaMulti_tickf ) |
| 5126 | { |
| 5127 | // get "this" |
| 5128 | Chuck_UGen * ugen = (Chuck_UGen *)SELF; |
| 5129 | // get LiSa data structure |
| 5130 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5131 | |
| 5132 | t_CKUINT nchans = ugen->m_num_outs; |
| 5133 | for( t_CKUINT frame_idx = 0; frame_idx < nframes; frame_idx++ ) |
| 5134 | { |
| 5135 | // 1.5.0.0 (ge) | changed to in[frame_idx*nchans+0]; was:+1 |
| 5136 | SAMPLE * temp_out_samples = d->tick_multi( in[frame_idx*nchans+0] ); |
| 5137 | // CK_FPRINTF_STDERR( "%0.2f ", in[frame_idx*nchans+0] ); |
| 5138 | |
| 5139 | for( t_CKUINT chan_idx = 0; chan_idx < nchans; chan_idx++ ) |
| 5140 | { |
| 5141 | out[frame_idx*nchans+chan_idx] = temp_out_samples[chan_idx]; |
| 5142 | } |
| 5143 | } |
| 5144 | |
| 5145 | return TRUE; |
| 5146 | } |
| 5147 | |
| 5148 | |
| 5149 | |
| 5150 | |
| 5151 | //----------------------------------------------------------------------------- |
| 5152 | // name: LiSaMulti_size() |
| 5153 | // desc: set size of buffer allocation |
| 5154 | //----------------------------------------------------------------------------- |
| 5155 | CK_DLL_CTRL( LiSaMulti_size ) |
| 5156 | { |
| 5157 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5158 | t_CKDUR buflen = GET_NEXT_DUR(ARGS); |
| 5159 | if( buflen > LiSa_MAXBUFSIZE ) |
| 5160 | { |
| 5161 | CK_FPRINTF_STDERR( "LiSa: buffer size request too large, resizing to %i...\n", LiSa_MAXBUFSIZE ); |
| 5162 | buflen = LiSa_MAXBUFSIZE; |
| 5163 | } |
| 5164 | // allocate |
| 5165 | d->buffer_alloc((t_CKINT)buflen); |
| 5166 | |
| 5167 | RETURN->v_dur = (t_CKDUR)buflen; |
| 5168 | } |
| 5169 | |
| 5170 | |
| 5171 | |
| 5172 | |
| 5173 | //----------------------------------------------------------------------------- |
| 5174 | // name: LiSaMulti_cget_size() |
| 5175 | // desc: get size of buffer allocation |
| 5176 | //----------------------------------------------------------------------------- |
| 5177 | CK_DLL_CGET( LiSaMulti_cget_size ) |
| 5178 | { |
| 5179 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5180 | |
| 5181 | RETURN->v_dur = (t_CKDUR)d->mdata_len; |
| 5182 | } |
| 5183 | |
| 5184 | |
| 5185 | |
| 5186 | |
| 5187 | //----------------------------------------------------------------------------- |
| 5188 | // name: LiSaMulti_start_record() |
| 5189 | // desc: CTRL function ... |
| 5190 | //----------------------------------------------------------------------------- |
| 5191 | CK_DLL_CTRL( LiSaMulti_start_record ) |
| 5192 | { |
| 5193 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5194 | d->record = GET_NEXT_INT(ARGS); |
| 5195 | |
| 5196 | RETURN->v_int = (t_CKINT)d->record; |
| 5197 | } |
| 5198 | |
| 5199 | |
| 5200 | |
| 5201 | |
| 5202 | //----------------------------------------------------------------------------- |
| 5203 | // name: LiSaMulti_start_play() |
| 5204 | // desc: CTRL function |
| 5205 | //----------------------------------------------------------------------------- |
| 5206 | CK_DLL_CTRL( LiSaMulti_start_play ) |
| 5207 | { |
| 5208 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5209 | t_CKINT which = GET_NEXT_INT(ARGS); |
| 5210 | d->play[which] = GET_NEXT_INT(ARGS); |
| 5211 | // CK_FPRINTF_STDERR( "voice %d playing = %d\n", which, d->play[which] ); |
| 5212 | |
| 5213 | // turn off ramping toggles |
| 5214 | d->rampdown[which] = false; |
| 5215 | d->rampup[which] = false; |
| 5216 | |
| 5217 | RETURN->v_int = (t_CKINT)d->play[which]; |
| 5218 | } |
| 5219 | |
| 5220 | |
| 5221 | |
| 5222 | |
| 5223 | //----------------------------------------------------------------------------- |
| 5224 | // name: LiSaMulti_start_play() |
| 5225 | // desc: CTRL function |
| 5226 | //----------------------------------------------------------------------------- |
| 5227 | CK_DLL_CTRL( LiSaMulti_start_play0 ) |
| 5228 | { |
| 5229 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5230 | d->play[0] = GET_NEXT_INT(ARGS); |
| 5231 | //CK_FPRINTF_STDERR( "voice %d playing = %d\n", which, d->play[which] ); |
| 5232 | |
| 5233 | // turn off ramping toggles |
| 5234 | d->rampdown[0] = false; |
| 5235 | d->rampup[0] = false; |
| 5236 | |
| 5237 | RETURN->v_int = (t_CKINT)d->play[0]; |
| 5238 | } |
| 5239 | |
| 5240 | |
| 5241 | |
| 5242 | |
| 5243 | //----------------------------------------------------------------------------- |
| 5244 | // name: LiSaMulti_ctrl_rate() |
| 5245 | // desc: CTRL function |
| 5246 | //----------------------------------------------------------------------------- |
| 5247 | CK_DLL_CTRL( LiSaMulti_ctrl_rate ) |
| 5248 | { |
| 5249 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5250 | t_CKINT which = GET_NEXT_INT(ARGS); |
| 5251 | d->p_inc[which] = (t_CKDOUBLE)GET_NEXT_FLOAT(ARGS); |
| 5252 | |
| 5253 | //CK_FPRINTF_STDERR( "setting voice %d rate to %f\n", which, d->p_inc[which] ); |
| 5254 | |
| 5255 | RETURN->v_float = d->p_inc[which]; |
| 5256 | } |
| 5257 | |
| 5258 | |
| 5259 | |
| 5260 | |
| 5261 | CK_DLL_CTRL( LiSaMulti_ctrl_rate0 ) |
| 5262 | { |
| 5263 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5264 | d->p_inc[0] = (t_CKDOUBLE)GET_CK_FLOAT(ARGS); |
| 5265 | |
| 5266 | //CK_FPRINTF_STDERR( "setting voice %d rate to %f\n", 0, d->p_inc[0] ); |
| 5267 | |
| 5268 | RETURN->v_float = d->p_inc[0]; |
| 5269 | } |
| 5270 | |
| 5271 | |
| 5272 | |
| 5273 | |
| 5274 | //----------------------------------------------------------------------------- |
| 5275 | // name: LiSaMulti_cget_rate() |
| 5276 | // desc: CTRL function |
| 5277 | //----------------------------------------------------------------------------- |
| 5278 | CK_DLL_CTRL( LiSaMulti_cget_rate ) |
| 5279 | { |
| 5280 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5281 | t_CKINT which = GET_NEXT_INT(ARGS); |
| 5282 | //CK_FPRINTF_STDERR( "setting voice %d rate to %f\n", which, d->p_inc[which] ); |
| 5283 | |
| 5284 | RETURN->v_float = d->p_inc[which]; |
| 5285 | } |
| 5286 | |
| 5287 | |
| 5288 | |
| 5289 | |
| 5290 | CK_DLL_CTRL( LiSaMulti_cget_rate0 ) |
| 5291 | { |
| 5292 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5293 | //CK_FPRINTF_STDERR( "setting voice %d rate to %f\n", which, d->p_inc[which] ); |
| 5294 | |
| 5295 | RETURN->v_float = d->p_inc[0]; |
| 5296 | } |
| 5297 | |
| 5298 | |
| 5299 | |
| 5300 | |
| 5301 | //----------------------------------------------------------------------------- |
| 5302 | // name: LiSaMulti_ctrl_pindex() |
| 5303 | // desc: CTRL function |
| 5304 | //----------------------------------------------------------------------------- |
| 5305 | CK_DLL_CTRL( LiSaMulti_ctrl_pindex ) |
| 5306 | { |
| 5307 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5308 | t_CKINT which = GET_NEXT_INT(ARGS); |
| 5309 | |
| 5310 | // 1.5.5.1 (ge & nick) check for bounds |
| 5311 | if( which < 0 || which >= LiSa_MAXVOICES ) |
| 5312 | { |
| 5313 | // print warning |
| 5314 | CK_STDCERR << "[chuck] LiSa.playPos(int, dur) voice argument '" << which << "' out of expected range [0," << LiSa_MAXVOICES-1 << "]" << CK_STDENDL; |
| 5315 | // return 0 |
| 5316 | RETURN->v_dur = 0; |
| 5317 | // bail out |
| 5318 | return; |
| 5319 | } |
| 5320 | |
| 5321 | // set the play position |
| 5322 | d->pindex[which] = (t_CKDOUBLE)GET_NEXT_DUR(ARGS); |
| 5323 | // NOTE: this is returning pindex and not the new (1.5.5.1) pindex_now, the latter is for cget only |
| 5324 | RETURN->v_dur = (t_CKDUR)d->pindex[which]; |
| 5325 | } |
| 5326 | |
| 5327 | |
| 5328 | CK_DLL_CTRL( LiSaMulti_ctrl_pindex0 ) |
| 5329 | { |
| 5330 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5331 | |
| 5332 | // set the play position |
| 5333 | d->pindex[0] = (t_CKDOUBLE)GET_NEXT_DUR(ARGS); |
| 5334 | // NOTE: this is returning pindex and not the new (1.5.5.1) pindex_now, the latter is for cget only |
| 5335 | RETURN->v_dur = (t_CKDUR)d->pindex[0]; |
| 5336 | } |
| 5337 | |
| 5338 | |
| 5339 | |
| 5340 | |
| 5341 | //----------------------------------------------------------------------------- |
| 5342 | // name: LiSaMulti_cget_pindex() |
| 5343 | // desc: CGET function |
| 5344 | //----------------------------------------------------------------------------- |
| 5345 | CK_DLL_CGET( LiSaMulti_cget_pindex ) |
| 5346 | { |
| 5347 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5348 | t_CKINT which = GET_NEXT_INT(ARGS); |
| 5349 | |
| 5350 | // 1.5.5.1 (ge & nick) check for bounds |
| 5351 | if( which < 0 || which >= LiSa_MAXVOICES ) |
| 5352 | { |
| 5353 | // print warning |
| 5354 | CK_STDCERR << "[chuck] Lisa.playPos(int) argument '" << which << "' out of expected range [0," << LiSa_MAXVOICES-1 << "]" << CK_STDENDL; |
| 5355 | // return 0 |
| 5356 | RETURN->v_dur = 0; |
| 5357 | // bail out |
| 5358 | return; |
| 5359 | } |
| 5360 | |
| 5361 | // return |
| 5362 | RETURN->v_dur = (t_CKDUR)d->pindex_now[which]; |
| 5363 | } |
| 5364 | |
| 5365 | |
| 5366 | CK_DLL_CGET( LiSaMulti_cget_pindex0 ) |
| 5367 | { |
| 5368 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5369 | |
| 5370 | // return |
| 5371 | RETURN->v_dur = (t_CKDUR)d->pindex_now[0]; |
| 5372 | } |
| 5373 | |
| 5374 | |
| 5375 | |
| 5376 | |
| 5377 | //----------------------------------------------------------------------------- |
| 5378 | // name: LiSaMulti_ctrl_pindex() |
| 5379 | // desc: CTRL function |
| 5380 | //----------------------------------------------------------------------------- |
| 5381 | CK_DLL_CTRL( LiSaMulti_ctrl_rindex ) |
| 5382 | { |
| 5383 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5384 | d->rindex = /* gewang-> */(t_CKINT)GET_NEXT_DUR(ARGS); |
| 5385 | |
| 5386 | RETURN->v_dur = (t_CKDUR)d->rindex; |
| 5387 | } |
| 5388 | |
| 5389 | |
| 5390 | |
| 5391 | |
| 5392 | //----------------------------------------------------------------------------- |
| 5393 | // name: LiSaMulti_cget_rindex() |
| 5394 | // desc: CGET function |
| 5395 | //----------------------------------------------------------------------------- |
| 5396 | CK_DLL_CGET( LiSaMulti_cget_rindex ) |
| 5397 | { |
| 5398 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5399 | |
| 5400 | // return |
| 5401 | RETURN->v_dur = (t_CKDUR)d->rindex; |
| 5402 | } |
| 5403 | |
| 5404 | |
| 5405 | |
| 5406 | |
| 5407 | //----------------------------------------------------------------------------- |
| 5408 | // name: LiSaMulti_ctrl_lstart() |
| 5409 | // desc: CTRL function |
| 5410 | //----------------------------------------------------------------------------- |
| 5411 | CK_DLL_CTRL( LiSaMulti_ctrl_lstart ) |
| 5412 | { |
| 5413 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5414 | t_CKINT which = GET_NEXT_INT(ARGS); |
| 5415 | d->loop_start[which] = /* gewang-> */(t_CKINT)GET_NEXT_DUR(ARGS); |
| 5416 | |
| 5417 | if (d->loop_start[which] < 0) d->loop_start[which] = 0; |
| 5418 | |
| 5419 | RETURN->v_dur = (t_CKDUR)d->loop_start[which]; |
| 5420 | } |
| 5421 | |
| 5422 | |
| 5423 | CK_DLL_CTRL( LiSaMulti_ctrl_lstart0 ) |
| 5424 | { |
| 5425 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5426 | d->loop_start[0] = /* gewang-> */(t_CKINT)GET_NEXT_DUR(ARGS); |
| 5427 | |
| 5428 | if (d->loop_start[0] < 0) d->loop_start[0] = 0; |
| 5429 | |
| 5430 | RETURN->v_dur = (t_CKDUR)d->loop_start[0]; |
| 5431 | } |
| 5432 | |
| 5433 | |
| 5434 | |
| 5435 | |
| 5436 | //----------------------------------------------------------------------------- |
| 5437 | // name: LiSaMulti_cget_lstart() |
| 5438 | // desc: CGET function |
| 5439 | //----------------------------------------------------------------------------- |
| 5440 | CK_DLL_CGET( LiSaMulti_cget_lstart ) |
| 5441 | { |
| 5442 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5443 | |
| 5444 | t_CKINT which = GET_NEXT_INT(ARGS); |
| 5445 | // return |
| 5446 | RETURN->v_dur = (t_CKDUR)d->loop_start[which]; |
| 5447 | } |
| 5448 | |
| 5449 | |
| 5450 | CK_DLL_CGET( LiSaMulti_cget_lstart0 ) |
| 5451 | { |
| 5452 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5453 | |
| 5454 | // return |
| 5455 | RETURN->v_dur = (t_CKDUR)d->loop_start[0]; |
| 5456 | } |
| 5457 | |
| 5458 | |
| 5459 | |
| 5460 | |
| 5461 | //----------------------------------------------------------------------------- |
| 5462 | // name: LiSaMulti_ctrl_lend() |
| 5463 | // desc: CTRL function |
| 5464 | //----------------------------------------------------------------------------- |
| 5465 | CK_DLL_CTRL( LiSaMulti_ctrl_lend ) |
| 5466 | { |
| 5467 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5468 | t_CKINT which = GET_NEXT_INT(ARGS); |
| 5469 | d->loop_end[which] = /* gewang-> */(t_CKINT)GET_NEXT_DUR(ARGS); |
| 5470 | |
| 5471 | //check to make sure loop_end is not too large |
| 5472 | if (d->loop_end[which] >= d->mdata_len) d->loop_end[which] = d->mdata_len - 1; |
| 5473 | |
| 5474 | RETURN->v_dur = (t_CKDUR)d->loop_end[which]; |
| 5475 | } |
| 5476 | |
| 5477 | |
| 5478 | CK_DLL_CTRL( LiSaMulti_ctrl_lend0 ) |
| 5479 | { |
| 5480 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5481 | d->loop_end[0] = /* gewang-> */(t_CKINT)GET_NEXT_DUR(ARGS); |
| 5482 | |
| 5483 | //check to make sure loop_end is not too large |
| 5484 | if (d->loop_end[0] >= d->mdata_len) d->loop_end[0] = d->mdata_len - 1; |
| 5485 | |
| 5486 | RETURN->v_dur = (t_CKDUR)d->loop_end[0]; |
| 5487 | } |
| 5488 | |
| 5489 | |
| 5490 | |
| 5491 | |
| 5492 | //----------------------------------------------------------------------------- |
| 5493 | // name: LiSaMulti_cget_lend() |
| 5494 | // desc: CGET function |
| 5495 | //----------------------------------------------------------------------------- |
| 5496 | CK_DLL_CGET( LiSaMulti_cget_lend ) |
| 5497 | { |
| 5498 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5499 | t_CKINT which = GET_NEXT_INT(ARGS); |
| 5500 | |
| 5501 | // return |
| 5502 | RETURN->v_dur = (t_CKDUR)d->loop_end[which]; |
| 5503 | } |
| 5504 | |
| 5505 | |
| 5506 | CK_DLL_CGET( LiSaMulti_cget_lend0 ) |
| 5507 | { |
| 5508 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5509 | |
| 5510 | // return |
| 5511 | RETURN->v_dur = (t_CKDUR)d->loop_end[0]; |
| 5512 | } |
| 5513 | |
| 5514 | |
| 5515 | |
| 5516 | |
| 5517 | //----------------------------------------------------------------------------- |
| 5518 | // name: LiSaMulti_ctrl_loop() |
| 5519 | // desc: CTRL function |
| 5520 | //----------------------------------------------------------------------------- |
| 5521 | CK_DLL_CTRL( LiSaMulti_ctrl_loop ) |
| 5522 | { |
| 5523 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5524 | t_CKINT which = GET_NEXT_INT(ARGS); |
| 5525 | d->loopplay[which] = (t_CKBOOL)GET_NEXT_INT(ARGS); |
| 5526 | } |
| 5527 | |
| 5528 | |
| 5529 | CK_DLL_CTRL( LiSaMulti_ctrl_loop0 ) |
| 5530 | { |
| 5531 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5532 | d->loopplay[0] = (t_CKBOOL)GET_NEXT_INT(ARGS); |
| 5533 | |
| 5534 | RETURN->v_int = (t_CKINT)d->loopplay[0]; |
| 5535 | } |
| 5536 | |
| 5537 | |
| 5538 | |
| 5539 | |
| 5540 | //----------------------------------------------------------------------------- |
| 5541 | // name: LiSaMulti_cget_loop() |
| 5542 | // desc: CGET function |
| 5543 | //----------------------------------------------------------------------------- |
| 5544 | CK_DLL_CGET( LiSaMulti_cget_loop ) |
| 5545 | { |
| 5546 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5547 | t_CKINT which = GET_NEXT_INT(ARGS); |
| 5548 | |
| 5549 | // return |
| 5550 | RETURN->v_int = (t_CKINT)d->loopplay[which]; |
| 5551 | } |
| 5552 | |
| 5553 | |
| 5554 | CK_DLL_CGET( LiSaMulti_cget_loop0 ) |
| 5555 | { |
| 5556 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5557 | |
| 5558 | // return |
| 5559 | RETURN->v_int = (t_CKINT)d->loopplay[0]; |
| 5560 | } |
| 5561 | |
| 5562 | |
| 5563 | |
| 5564 | |
| 5565 | //----------------------------------------------------------------------------- |
| 5566 | // name: LiSaMulti_ctrl_bi() |
| 5567 | // desc: CTRL function |
| 5568 | //----------------------------------------------------------------------------- |
| 5569 | CK_DLL_CTRL( LiSaMulti_ctrl_bi ) |
| 5570 | { |
| 5571 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5572 | t_CKINT which = GET_NEXT_INT(ARGS); |
| 5573 | d->bi[which] = (t_CKBOOL)GET_NEXT_INT(ARGS); |
| 5574 | |
| 5575 | RETURN->v_int = (t_CKINT)d->bi[which]; |
| 5576 | } |
| 5577 | |
| 5578 | |
| 5579 | CK_DLL_CTRL( LiSaMulti_ctrl_bi0 ) |
| 5580 | { |
| 5581 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5582 | d->bi[0] = (t_CKBOOL)GET_NEXT_INT(ARGS); |
| 5583 | |
| 5584 | RETURN->v_int = (t_CKINT)d->bi[0]; |
| 5585 | } |
| 5586 | |
| 5587 | |
| 5588 | |
| 5589 | |
| 5590 | //----------------------------------------------------------------------------- |
| 5591 | // name: LiSaMulti_cget_bi() |
| 5592 | // desc: CGET function |
| 5593 | //----------------------------------------------------------------------------- |
| 5594 | CK_DLL_CGET( LiSaMulti_cget_bi ) |
| 5595 | { |
| 5596 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5597 | t_CKINT which = GET_NEXT_INT(ARGS); |
| 5598 | |
| 5599 | // return |
| 5600 | RETURN->v_int = (t_CKINT)d->bi[which]; |
| 5601 | } |
| 5602 | |
| 5603 | |
| 5604 | CK_DLL_CGET( LiSaMulti_cget_bi0 ) |
| 5605 | { |
| 5606 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5607 | |
| 5608 | // return |
| 5609 | RETURN->v_int = (t_CKINT)d->bi[0]; |
| 5610 | } |
| 5611 | |
| 5612 | |
| 5613 | |
| 5614 | |
| 5615 | //----------------------------------------------------------------------------- |
| 5616 | // name: LiSaMulti_ctrl_loop_end_rec() |
| 5617 | // desc: CTRL function |
| 5618 | //----------------------------------------------------------------------------- |
| 5619 | CK_DLL_CTRL( LiSaMulti_ctrl_loop_end_rec ) |
| 5620 | { |
| 5621 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5622 | d->loop_end_rec = /* gewang-> */(t_CKINT)GET_NEXT_DUR(ARGS); |
| 5623 | |
| 5624 | RETURN->v_dur = (t_CKDUR)d->loop_end_rec; |
| 5625 | } |
| 5626 | |
| 5627 | |
| 5628 | //----------------------------------------------------------------------------- |
| 5629 | // name: LiSaMulti_cget_loop_end_rec() |
| 5630 | // desc: CGET function |
| 5631 | //----------------------------------------------------------------------------- |
| 5632 | CK_DLL_CGET( LiSaMulti_cget_loop_end_rec ) |
| 5633 | { |
| 5634 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5635 | |
| 5636 | // return |
| 5637 | RETURN->v_dur = (t_CKDUR)d->loop_end_rec; |
| 5638 | } |
| 5639 | |
| 5640 | |
| 5641 | |
| 5642 | |
| 5643 | //----------------------------------------------------------------------------- |
| 5644 | // name: LiSaMulti_ctrl_loop_rec() |
| 5645 | // desc: CTRL function |
| 5646 | //----------------------------------------------------------------------------- |
| 5647 | CK_DLL_CTRL( LiSaMulti_ctrl_loop_rec ) |
| 5648 | { |
| 5649 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5650 | d->looprec = (t_CKBOOL)GET_NEXT_INT(ARGS); |
| 5651 | |
| 5652 | RETURN->v_int = (t_CKINT)d->looprec; |
| 5653 | } |
| 5654 | |
| 5655 | |
| 5656 | |
| 5657 | |
| 5658 | //----------------------------------------------------------------------------- |
| 5659 | // name: LiSaMulti_cget_loop_rec() |
| 5660 | // desc: CGET function |
| 5661 | //----------------------------------------------------------------------------- |
| 5662 | CK_DLL_CGET( LiSaMulti_cget_loop_rec ) |
| 5663 | { |
| 5664 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5665 | |
| 5666 | // return |
| 5667 | RETURN->v_int = (t_CKINT)d->looprec; |
| 5668 | } |
| 5669 | |
| 5670 | |
| 5671 | |
| 5672 | |
| 5673 | //----------------------------------------------------------------------------- |
| 5674 | // name: LiSaMulti_ctrl_sample(); put a sample directly into record buffer |
| 5675 | // desc: CTRL function |
| 5676 | //----------------------------------------------------------------------------- |
| 5677 | CK_DLL_CTRL( LiSaMulti_ctrl_sample ) |
| 5678 | { |
| 5679 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5680 | SAMPLE sample_in = (SAMPLE)GET_NEXT_FLOAT(ARGS); |
| 5681 | t_CKINT index_in = (t_CKINT)GET_NEXT_DUR(ARGS); |
| 5682 | |
| 5683 | d->pokeSample( sample_in, index_in ); |
| 5684 | |
| 5685 | RETURN->v_float = (t_CKFLOAT)sample_in; //pass input through |
| 5686 | } |
| 5687 | |
| 5688 | |
| 5689 | |
| 5690 | |
| 5691 | //----------------------------------------------------------------------------- |
| 5692 | // name: LiSaMulti_cget_sample(); grab a sample from the record buffer |
| 5693 | // desc: CGET function |
| 5694 | //----------------------------------------------------------------------------- |
| 5695 | CK_DLL_CGET( LiSaMulti_cget_sample ) |
| 5696 | { |
| 5697 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5698 | double index_in = (t_CKDOUBLE)GET_NEXT_DUR(ARGS); |
| 5699 | // return |
| 5700 | RETURN->v_float = (t_CKFLOAT)d->grabSample( index_in ); //change this to getSamp for interpolation |
| 5701 | } |
| 5702 | |
| 5703 | |
| 5704 | |
| 5705 | |
| 5706 | //----------------------------------------------------------------------------- |
| 5707 | // name: LiSaMulti_ctrl_voicegain() |
| 5708 | // desc: CTRL function |
| 5709 | //----------------------------------------------------------------------------- |
| 5710 | CK_DLL_CTRL( LiSaMulti_ctrl_voicegain ) |
| 5711 | { |
| 5712 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5713 | t_CKINT which = GET_NEXT_INT(ARGS); |
| 5714 | d->voiceGain[which] = (t_CKDOUBLE)GET_NEXT_FLOAT(ARGS); |
| 5715 | |
| 5716 | RETURN->v_float = (t_CKFLOAT)d->voiceGain[which]; |
| 5717 | } |
| 5718 | |
| 5719 | |
| 5720 | |
| 5721 | |
| 5722 | //----------------------------------------------------------------------------- |
| 5723 | // name: LiSaMulti_cget_voicegain() |
| 5724 | // desc: CGET function |
| 5725 | //----------------------------------------------------------------------------- |
| 5726 | CK_DLL_CGET( LiSaMulti_cget_voicegain ) |
| 5727 | { |
| 5728 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5729 | t_CKINT which = GET_NEXT_INT(ARGS); |
| 5730 | |
| 5731 | // return |
| 5732 | RETURN->v_float = (t_CKFLOAT)d->voiceGain[which]; |
| 5733 | } |
| 5734 | |
| 5735 | |
| 5736 | |
| 5737 | |
| 5738 | //----------------------------------------------------------------------------- |
| 5739 | // name: LiSaMulti_ctrl_voicepan() |
| 5740 | // desc: CTRL function |
| 5741 | //----------------------------------------------------------------------------- |
| 5742 | CK_DLL_CTRL( LiSaMulti_ctrl_voicepan ) |
| 5743 | { |
| 5744 | // get LiSa data structure |
| 5745 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5746 | // get voice number argument |
| 5747 | t_CKINT which = GET_NEXT_INT(ARGS); |
| 5748 | // get pan argument |
| 5749 | d->voicePan[which] = GET_NEXT_FLOAT(ARGS); |
| 5750 | |
| 5751 | t_CKINT i; |
| 5752 | |
| 5753 | // reset channel gains for voice |
| 5754 | for( i = 0; i < d->num_chans; i++ ) d->channelGain[which][i] = 0.; |
| 5755 | |
| 5756 | // iterate over channels |
| 5757 | for( i = 0; i < d->num_chans; i++ ) |
| 5758 | { |
| 5759 | // get truncated value (0.4 -> 0; 5.6 -> 5) as channel index |
| 5760 | t_CKINT panTrunc = (t_CKINT)d->voicePan[which]; |
| 5761 | // if channel of interest |
| 5762 | if( i == panTrunc) |
| 5763 | { |
| 5764 | // calculate pan value |
| 5765 | d->channelGain[which][i] = 1.0 - (d->voicePan[which]-(t_CKFLOAT)i); |
| 5766 | // cerr << "1) d->channelGain[" << which << "][" << i << "]: " << d->channelGain[which][i] << endl; |
| 5767 | // if channels > 1 |
| 5768 | if( d->num_chans > 1 ) |
| 5769 | { |
| 5770 | // handle the case that pan is between last and first channel |
| 5771 | if(i == d->num_chans - 1) |
| 5772 | { |
| 5773 | // compute the first channel |
| 5774 | d->channelGain[which][0] = 1. - d->channelGain[which][i]; |
| 5775 | d->channelGain[which][0] = sqrt(d->channelGain[which][0]); |
| 5776 | } |
| 5777 | else |
| 5778 | { |
| 5779 | // compute the adjacent channel pan |
| 5780 | d->channelGain[which][i+1] = 1. - d->channelGain[which][i]; |
| 5781 | d->channelGain[which][i+1] = sqrt(d->channelGain[which][i+1]); |
| 5782 | } |
| 5783 | } |
| 5784 | // calculate sqrt |
| 5785 | d->channelGain[which][i] = sqrt(d->channelGain[which][i]); |
| 5786 | } |
| 5787 | |
| 5788 | // CK_FPRINTF_STDERR( "gain for channel %d and voice %d = %f\n", i, which, d->channelGain[which][i] ); |
| 5789 | } |
| 5790 | |
| 5791 | RETURN->v_float = d->voicePan[which]; |
| 5792 | } |
| 5793 | |
| 5794 | |
| 5795 | |
| 5796 | |
| 5797 | //----------------------------------------------------------------------------- |
| 5798 | // name: LiSaMulti_cget_voicepan() |
| 5799 | // desc: CGET function |
| 5800 | //----------------------------------------------------------------------------- |
| 5801 | CK_DLL_CGET( LiSaMulti_cget_voicepan ) |
| 5802 | { |
| 5803 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5804 | t_CKINT which = GET_NEXT_INT(ARGS); |
| 5805 | |
| 5806 | // return |
| 5807 | RETURN->v_float = (t_CKFLOAT)d->voicePan[which]; |
| 5808 | } |
| 5809 | |
| 5810 | |
| 5811 | |
| 5812 | |
| 5813 | //----------------------------------------------------------------------------- |
| 5814 | // name: LiSaMulti_ctrl_voicepan0() |
| 5815 | // desc: set pan for default voice 0 |
| 5816 | //----------------------------------------------------------------------------- |
| 5817 | CK_DLL_CTRL( LiSaMulti_ctrl_voicepan0 ) |
| 5818 | { |
| 5819 | // LiSa data structure |
| 5820 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5821 | // voice 0 for this one |
| 5822 | t_CKINT which = 0; |
| 5823 | // get argument |
| 5824 | d->voicePan[which] = (t_CKFLOAT)GET_NEXT_FLOAT(ARGS); |
| 5825 | |
| 5826 | t_CKINT i; |
| 5827 | |
| 5828 | // zero out all channel gains for this voice |
| 5829 | for( i = 0; i < d->num_chans; i++ ) d->channelGain[which][i] = 0.; |
| 5830 | // iterate through channels |
| 5831 | for( i = 0; i < d->num_chans; i++ ) |
| 5832 | { |
| 5833 | // get truncated value (0.4 -> 0; 5.6 -> 5) as channel index |
| 5834 | t_CKINT panTrunc = (t_CKINT)d->voicePan[which]; |
| 5835 | // match channel |
| 5836 | if( i == panTrunc ) |
| 5837 | { |
| 5838 | // calculate channel gain (between 0 and 1) from pan and trunc |
| 5839 | d->channelGain[which][i] = 1. - (d->voicePan[which]-(t_CKFLOAT)i); |
| 5840 | // for channels > 1 |
| 5841 | if( d->num_chans > 1 ) |
| 5842 | { |
| 5843 | // between first and last channel |
| 5844 | if( i == d->num_chans-1 ) |
| 5845 | { |
| 5846 | d->channelGain[which][0] = 1. - d->channelGain[which][i]; |
| 5847 | d->channelGain[which][0] = sqrt(d->channelGain[which][0]); |
| 5848 | } |
| 5849 | else |
| 5850 | { |
| 5851 | d->channelGain[which][i+1] = 1. - d->channelGain[which][i]; |
| 5852 | d->channelGain[which][i+1] = sqrt(d->channelGain[which][i+1]); |
| 5853 | } |
| 5854 | } |
| 5855 | // sqrt the gain |
| 5856 | d->channelGain[which][i] = sqrt(d->channelGain[which][i]); |
| 5857 | } |
| 5858 | |
| 5859 | // CK_FPRINTF_STDERR( "gain for voice %d channel %d = %f\n", which, i, d->channelGain[which][i] ); |
| 5860 | } |
| 5861 | |
| 5862 | RETURN->v_float = (t_CKFLOAT)d->voicePan[which]; |
| 5863 | } |
| 5864 | |
| 5865 | |
| 5866 | CK_DLL_CGET( LiSaMulti_cget_voicepan0 ) |
| 5867 | { |
| 5868 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5869 | |
| 5870 | // return |
| 5871 | RETURN->v_float = (t_CKFLOAT)d->voicePan[0]; |
| 5872 | } |
| 5873 | |
| 5874 | |
| 5875 | |
| 5876 | |
| 5877 | //----------------------------------------------------------------------------- |
| 5878 | // name: LiSaMulti_ctrl_coeff() |
| 5879 | // desc: CTRL function |
| 5880 | //----------------------------------------------------------------------------- |
| 5881 | CK_DLL_CTRL( LiSaMulti_ctrl_coeff ) |
| 5882 | { |
| 5883 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5884 | d->coeff = (t_CKDOUBLE)GET_NEXT_FLOAT(ARGS); |
| 5885 | |
| 5886 | RETURN->v_float = (t_CKFLOAT)d->coeff; |
| 5887 | } |
| 5888 | |
| 5889 | |
| 5890 | |
| 5891 | |
| 5892 | //----------------------------------------------------------------------------- |
| 5893 | // name: LiSaMulti_cget_coeff() |
| 5894 | // desc: CGET function |
| 5895 | //----------------------------------------------------------------------------- |
| 5896 | CK_DLL_CGET( LiSaMulti_cget_coeff ) |
| 5897 | { |
| 5898 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5899 | |
| 5900 | // return |
| 5901 | RETURN->v_float = (t_CKFLOAT)d->coeff; |
| 5902 | } |
| 5903 | |
| 5904 | |
| 5905 | |
| 5906 | |
| 5907 | //----------------------------------------------------------------------------- |
| 5908 | // name: LiSaMulti_ctrl_clear() |
| 5909 | // desc: CTRL function |
| 5910 | //----------------------------------------------------------------------------- |
| 5911 | CK_DLL_CTRL( LiSaMulti_ctrl_clear ) |
| 5912 | { |
| 5913 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5914 | d->clear_buf(); |
| 5915 | } |
| 5916 | |
| 5917 | |
| 5918 | |
| 5919 | |
| 5920 | //----------------------------------------------------------------------------- |
| 5921 | // name: LiSaMulti_cget_voice() |
| 5922 | // desc: CGET function |
| 5923 | //----------------------------------------------------------------------------- |
| 5924 | CK_DLL_CGET( LiSaMulti_cget_voice ) |
| 5925 | { |
| 5926 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5927 | |
| 5928 | // return |
| 5929 | RETURN->v_int = (t_CKINT)d->get_free_voice(); |
| 5930 | } |
| 5931 | |
| 5932 | |
| 5933 | |
| 5934 | |
| 5935 | // ramp stuff |
| 5936 | //----------------------------------------------------------------------------- |
| 5937 | // name: LiSaMulti_ctrl_rampup() |
| 5938 | // desc: CTRL function |
| 5939 | //----------------------------------------------------------------------------- |
| 5940 | CK_DLL_CTRL( LiSaMulti_ctrl_rampup ) |
| 5941 | { |
| 5942 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5943 | t_CKINT voice = GET_NEXT_INT(ARGS); |
| 5944 | t_CKDUR len = GET_NEXT_DUR(ARGS); |
| 5945 | |
| 5946 | d->ramp_up(voice, len); |
| 5947 | |
| 5948 | RETURN->v_dur = (t_CKDUR)len; |
| 5949 | } |
| 5950 | |
| 5951 | |
| 5952 | CK_DLL_CTRL( LiSaMulti_ctrl_rampup0 ) |
| 5953 | { |
| 5954 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5955 | t_CKDUR len = GET_NEXT_DUR(ARGS); |
| 5956 | |
| 5957 | d->ramp_up(0, len); |
| 5958 | |
| 5959 | RETURN->v_dur = (t_CKDUR)len; |
| 5960 | } |
| 5961 | |
| 5962 | |
| 5963 | |
| 5964 | |
| 5965 | //----------------------------------------------------------------------------- |
| 5966 | // name: LiSaMulti_ctrl_rampdown() |
| 5967 | // desc: CTRL function |
| 5968 | //----------------------------------------------------------------------------- |
| 5969 | CK_DLL_CTRL( LiSaMulti_ctrl_rampdown ) |
| 5970 | { |
| 5971 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5972 | t_CKINT voice = GET_NEXT_INT(ARGS); |
| 5973 | t_CKDUR len = GET_NEXT_DUR(ARGS); |
| 5974 | |
| 5975 | d->ramp_down(voice, len); |
| 5976 | |
| 5977 | RETURN->v_dur = (t_CKDUR)len; |
| 5978 | } |
| 5979 | |
| 5980 | |
| 5981 | CK_DLL_CTRL( LiSaMulti_ctrl_rampdown0 ) |
| 5982 | { |
| 5983 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 5984 | t_CKDUR len = GET_NEXT_DUR(ARGS); |
| 5985 | |
| 5986 | d->ramp_down(0, len); |
| 5987 | |
| 5988 | RETURN->v_dur = (t_CKDUR)len; |
| 5989 | } |
| 5990 | |
| 5991 | |
| 5992 | |
| 5993 | |
| 5994 | //----------------------------------------------------------------------------- |
| 5995 | // name: LiSaMulti_ctrl_rec_ramplen() |
| 5996 | // desc: CTRL function |
| 5997 | //----------------------------------------------------------------------------- |
| 5998 | CK_DLL_CTRL( LiSaMulti_ctrl_rec_ramplen ) |
| 5999 | { |
| 6000 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 6001 | t_CKDUR newramplen = GET_NEXT_DUR(ARGS); |
| 6002 | d->set_rec_ramplen(newramplen); |
| 6003 | |
| 6004 | RETURN->v_dur = (t_CKDUR)newramplen; |
| 6005 | } |
| 6006 | |
| 6007 | |
| 6008 | |
| 6009 | |
| 6010 | //----------------------------------------------------------------------------- |
| 6011 | // name: LiSaMulti_ctrl_maxvoices() |
| 6012 | // desc: CTRL function |
| 6013 | //----------------------------------------------------------------------------- |
| 6014 | CK_DLL_CTRL( LiSaMulti_ctrl_maxvoices ) |
| 6015 | { |
| 6016 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 6017 | d->maxvoices = GET_NEXT_INT(ARGS); |
| 6018 | if( d->maxvoices > LiSa_MAXVOICES) { |
| 6019 | d->maxvoices = LiSa_MAXVOICES; |
| 6020 | CK_FPRINTF_STDERR( "LiSa: MAXVOICES limited to %d.\n", LiSa_MAXVOICES ); |
| 6021 | } |
| 6022 | RETURN->v_int = d->maxvoices; |
| 6023 | } |
| 6024 | |
| 6025 | |
| 6026 | |
| 6027 | |
| 6028 | //----------------------------------------------------------------------------- |
| 6029 | // name: LiSaMulti_cget_maxvoices() |
| 6030 | // desc: CGET function |
| 6031 | //----------------------------------------------------------------------------- |
| 6032 | CK_DLL_CGET( LiSaMulti_cget_maxvoices ) |
| 6033 | { |
| 6034 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 6035 | |
| 6036 | // return |
| 6037 | RETURN->v_int = d->maxvoices; |
| 6038 | } |
| 6039 | |
| 6040 | |
| 6041 | |
| 6042 | |
| 6043 | //----------------------------------------------------------------------------- |
| 6044 | // name: LiSaMulti_cget_samp() |
| 6045 | // desc: CGET function |
| 6046 | //----------------------------------------------------------------------------- |
| 6047 | CK_DLL_CGET( LiSaMulti_cget_value ) |
| 6048 | { |
| 6049 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 6050 | t_CKINT voice = GET_NEXT_INT(ARGS); |
| 6051 | t_CKDOUBLE where = (t_CKDOUBLE) GET_NEXT_DUR(ARGS); |
| 6052 | |
| 6053 | // return |
| 6054 | RETURN->v_dur = (t_CKDUR)d->getSamp(where, voice); |
| 6055 | } |
| 6056 | |
| 6057 | |
| 6058 | |
| 6059 | |
| 6060 | CK_DLL_CGET( LiSaMulti_cget_value0 ) |
| 6061 | { |
| 6062 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 6063 | |
| 6064 | t_CKDOUBLE where = (t_CKDOUBLE) GET_NEXT_DUR(ARGS); |
| 6065 | // return |
| 6066 | RETURN->v_dur = (t_CKDUR)d->getSamp(where, 0); |
| 6067 | } |
| 6068 | |
| 6069 | |
| 6070 | |
| 6071 | |
| 6072 | //----------------------------------------------------------------------------- |
| 6073 | // name: LiSaMulti_ctrl_track() |
| 6074 | // desc: CTRL function |
| 6075 | //----------------------------------------------------------------------------- |
| 6076 | CK_DLL_CTRL( LiSaMulti_ctrl_track ) |
| 6077 | { |
| 6078 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 6079 | d->track = (t_CKINT)GET_NEXT_INT(ARGS); |
| 6080 | |
| 6081 | RETURN->v_int = d->track; |
| 6082 | } |
| 6083 | |
| 6084 | |
| 6085 | |
| 6086 | |
| 6087 | //----------------------------------------------------------------------------- |
| 6088 | // name: LiSaMulti_cget_sync() |
| 6089 | // desc: CGET function |
| 6090 | //----------------------------------------------------------------------------- |
| 6091 | CK_DLL_CGET( LiSaMulti_cget_track ) |
| 6092 | { |
| 6093 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 6094 | |
| 6095 | // return |
| 6096 | RETURN->v_int = d->track; |
| 6097 | } |
| 6098 | |
| 6099 | |
| 6100 | |
| 6101 | |
| 6102 | //----------------------------------------------------------------------------- |
| 6103 | // name: LiSaMulti_cget_playing() |
| 6104 | // desc: CGET function; indicates whether a particular voice is playing |
| 6105 | //----------------------------------------------------------------------------- |
| 6106 | CK_DLL_CGET( LiSaMulti_cget_playing ) |
| 6107 | { |
| 6108 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 6109 | t_CKINT voice = GET_NEXT_INT(ARGS); |
| 6110 | if(voice >= d->maxvoices) { |
| 6111 | CK_FPRINTF_STDERR( "LiSa: requesting info greater than MAXVOICES %d.\n", LiSa_MAXVOICES ); |
| 6112 | voice = 0; |
| 6113 | } |
| 6114 | |
| 6115 | // return |
| 6116 | RETURN->v_int = d->play[voice]; |
| 6117 | } |
| 6118 | |
| 6119 | |
| 6120 | |
| 6121 | |
| 6122 | //----------------------------------------------------------------------------- |
| 6123 | // name: LiSaMulti_ctrl_read() |
| 6124 | // desc: 1.5.5.6 (nshaheed) Read audio file into lisa buffer ala SndBuf |
| 6125 | //----------------------------------------------------------------------------- |
| 6126 | CK_DLL_CTRL( LiSaMulti_ctrl_read ) |
| 6127 | { |
| 6128 | LiSaMulti_data * d = (LiSaMulti_data *)OBJ_MEMBER_UINT(SELF, LiSaMulti_offset_data); |
| 6129 | |
| 6130 | Chuck_String * ckfilename = GET_CK_STRING(ARGS); |
| 6131 | const char * filename = NULL; |
| 6132 | |
| 6133 | // set return value |
| 6134 | RETURN->v_string = ckfilename; |
| 6135 | |
| 6136 | // check to avert crash on null argument |
| 6137 | if( !ckfilename ) |
| 6138 | { |
| 6139 | CK_FPRINTF_STDERR( "[chuck] LiSa.read() given null argument; nothing read...\n" ); |
| 6140 | return; |
| 6141 | } |
| 6142 | // get filename c string |
| 6143 | filename = ckfilename->str().c_str(); |
| 6144 | |
| 6145 | // log |
| 6146 | EM_log( CK_LOG_INFO, "(LiSa): reading '%s'...", filename ); |
| 6147 | |
| 6148 | // built in |
| 6149 | if( strstr(filename, "special:") ) |
| 6150 | { |
| 6151 | // TODO how to handle special? |
| 6152 | SAMPLE * rawdata = NULL; |
| 6153 | t_CKUINT rawsize = 0; |
| 6154 | t_CKUINT srate = 22050; |
| 6155 | |
| 6156 | // which |
| 6157 | if( strstr(filename, "special:sinewave") ) { |
| 6158 | rawsize = 1024; rawdata = NULL; |
| 6159 | } |
| 6160 | else if( strstr(filename, "special:aaa") || |
| 6161 | strstr(filename, "special:aah") || |
| 6162 | strstr(filename, "special:ahh") ) |
| 6163 | { |
| 6164 | rawsize = ahh_size; rawdata = ahh_data; |
| 6165 | } |
| 6166 | else if( strstr(filename, "special:britestk") ) { |
| 6167 | rawsize = britestk_size; rawdata = britestk_data; |
| 6168 | } |
| 6169 | else if( strstr(filename, "special:doh") || strstr(filename, "special:dope") ) { |
| 6170 | rawsize = dope_size; rawdata = dope_data; |
| 6171 | } |
| 6172 | else if( strstr(filename, "special:eee") ) { |
| 6173 | rawsize = eee_size; rawdata = eee_data; |
| 6174 | } |
| 6175 | else if( strstr(filename, "special:fwavblnk") ) { |
| 6176 | rawsize = fwavblnk_size; rawdata = fwavblnk_data; |
| 6177 | } |
| 6178 | else if( strstr(filename, "special:halfwave") ) { |
| 6179 | rawsize = halfwave_size; rawdata = halfwave_data; |
| 6180 | } |
| 6181 | else if( strstr(filename, "special:impuls10") ) { |
| 6182 | rawsize = impuls10_size; rawdata = impuls10_data; |
| 6183 | } |
| 6184 | else if( strstr(filename, "special:impuls20") ) { |
| 6185 | rawsize = impuls20_size; rawdata = impuls20_data; |
| 6186 | } |
| 6187 | else if( strstr(filename, "special:impuls40") ) { |
| 6188 | rawsize = impuls40_size; rawdata = impuls40_data; |
| 6189 | } |
| 6190 | else if( strstr(filename, "special:mand1") ) { |
| 6191 | rawsize = mand1_size; rawdata = mand1_data; |
| 6192 | } |
| 6193 | else if( strstr(filename, "special:mandpluk") ) { |
| 6194 | rawsize = mandpluk_size; rawdata = mandpluk_data; |
| 6195 | } |
| 6196 | else if( strstr(filename, "special:marmstk1") ) { |
| 6197 | rawsize = marmstk1_size; rawdata = marmstk1_data; |
| 6198 | } |
| 6199 | else if( strstr(filename, "special:ooo") ) { |
| 6200 | rawsize = ooo_size; rawdata = ooo_data; |
| 6201 | } |
| 6202 | else if( strstr(filename, "special:peksblnk") ) { |
| 6203 | rawsize = peksblnk_size; rawdata = peksblnk_data; |
| 6204 | } |
| 6205 | else if( strstr(filename, "special:ppksblnk") ) { |
| 6206 | rawsize = ppksblnk_size; rawdata = ppksblnk_data; |
| 6207 | } |
| 6208 | else if( strstr(filename, "special:silence") ) { |
| 6209 | rawsize = silence_size; rawdata = silence_data; |
| 6210 | } |
| 6211 | else if( strstr(filename, "special:sineblnk") ) { |
| 6212 | rawsize = sineblnk_size; rawdata = sineblnk_data; |
| 6213 | } |
| 6214 | else if( strstr(filename, "special:sinewave") ) { |
| 6215 | rawsize = sinewave_size; rawdata = sinewave_data; |
| 6216 | } |
| 6217 | else if( strstr(filename, "special:snglpeak") ) { |
| 6218 | rawsize = snglpeak_size; rawdata = snglpeak_data; |
| 6219 | } |
| 6220 | else if( strstr(filename, "special:twopeaks") ) { |
| 6221 | rawsize = twopeaks_size; rawdata = twopeaks_data; |
| 6222 | } |
| 6223 | else if( strstr(filename, "special:glot_ahh") ) { |
| 6224 | rawsize = glot_ahh_size; rawdata = glot_ahh_data; srate = 44100; |
| 6225 | } |
| 6226 | else if( strstr(filename, "special:glot_eee") ) { |
| 6227 | rawsize = glot_eee_size; rawdata = glot_eee_data; srate = 44100; |
| 6228 | } |
| 6229 | else if( strstr(filename, "special:glot_ooo") ) { |
| 6230 | rawsize = glot_ooo_size; rawdata = glot_ooo_data; srate = 44100; |
| 6231 | } |
| 6232 | else if( strstr(filename, "special:glot_pop") ) { |
| 6233 | rawsize = glot_pop_size; rawdata = glot_pop_data; srate = 44100; |
| 6234 | } |
| 6235 | |
| 6236 | if( rawdata ) { |
| 6237 | float ratio = g_srateXxx / srate; |
| 6238 | int interp_size = (int) ratio * rawsize; |
| 6239 | |
| 6240 | if( !d->buffer_alloc(interp_size) ) return; |
| 6241 | |
| 6242 | // linearly interpolate special audio into runtime srate |
| 6243 | for( t_CKUINT j = 0; j < interp_size; j++ ) { |
| 6244 | int point_left; |
| 6245 | float weight; |
| 6246 | float pos = j / ratio; |
| 6247 | point_left = (int)pos; |
| 6248 | weight = pos - point_left; |
| 6249 | |
| 6250 | SAMPLE samp = (SAMPLE)rawdata[point_left] * (1 - weight) + (rawdata[point_left] + 1) * weight; |
| 6251 | |
| 6252 | d->mdata[j] = samp / (SAMPLE)SHRT_MAX; |
| 6253 | } |
| 6254 | } |
| 6255 | else if( strstr(filename, "special:sinewave") ) { |
| 6256 | if( !d->buffer_alloc(rawsize+1) ) return; |
| 6257 | for( t_CKUINT j = 0; j < rawsize; j++ ) |
| 6258 | d->mdata[j] = sin(2*CK_ONE_PI*j/rawsize); |
| 6259 | } |
| 6260 | else { |
| 6261 | CK_FPRINTF_STDERR( "[chuck](via LiSa): cannot load '%s'\n", filename ); |
| 6262 | return; |
| 6263 | } |
| 6264 | } |
| 6265 | else // read file |
| 6266 | { |
| 6267 | #ifdef __ANDROID__ |
| 6268 | bool is_jar_url = strstr(filename, "jar:") == filename; |
| 6269 | int jar_fd = 0; |
| 6270 | if( is_jar_url ) |
| 6271 | { |
| 6272 | if( !ChuckAndroid::copyJARURLFileToTemporary(filename, &jar_fd) ) |
| 6273 | { |
| 6274 | CK_FPRINTF_STDERR( "[chuck](via SndBuf): could not download file '%s' from JAR\n", filename ); |
| 6275 | return; |
| 6276 | } |
| 6277 | } |
| 6278 | else |
| 6279 | #endif |
| 6280 | { |
| 6281 | // check if file exists |
| 6282 | struct stat s; |
| 6283 | if( stat( filename, &s ) ) |
| 6284 | { |
| 6285 | CK_FPRINTF_STDERR( "[chuck](via LiSa): cannot open file '%s'...\n", filename ); |
| 6286 | return; |
| 6287 | } |
| 6288 | } |
| 6289 | |
| 6290 | // open it |
| 6291 | SF_INFO info; |
| 6292 | SNDFILE * fd; |
| 6293 | info.format = 0; |
| 6294 | const char * format = (const char *)strrchr( filename, '.'); |
| 6295 | if( format && strcmp( format, ".raw" ) == 0 ) |
| 6296 | { |
| 6297 | CK_FPRINTF_STDERR( "[chuck](via LiSa) %s :: type is '.raw'...\n", filename ); |
| 6298 | CK_FPRINTF_STDERR( "[chuck](via LiSa) |- assuming 16 bit signed mono (PCM)\n]" ); |
| 6299 | info.format = SF_FORMAT_RAW | SF_FORMAT_PCM_16 | SF_ENDIAN_CPU ; |
| 6300 | info.channels = 1; |
| 6301 | info.samplerate = 44100; |
| 6302 | } |
| 6303 | |
| 6304 | // open the handle |
| 6305 | #ifdef __ANDROID__ |
| 6306 | if( is_jar_url ) |
| 6307 | { |
| 6308 | fd = sf_open_fd( jar_fd, SFM_READ, &info, 0 ); |
| 6309 | } |
| 6310 | else |
| 6311 | #endif |
| 6312 | { |
| 6313 | fd = sf_open( filename, SFM_READ, &info ); |
| 6314 | } |
| 6315 | t_CKINT er = sf_error( fd ); |
| 6316 | if( er ) |
| 6317 | { |
| 6318 | CK_FPRINTF_STDERR( "[chuck](via LiSa): sndfile error '%li' opening '%s'...\n", er, filename ); |
| 6319 | CK_FPRINTF_STDERR( "[chuck](via LiSa): ...(reason: %s)\n", sf_strerror( fd ) ); |
| 6320 | if( fd ) |
| 6321 | { |
| 6322 | sf_close( fd ); |
| 6323 | } |
| 6324 | // escape |
| 6325 | return; |
| 6326 | } |
| 6327 | |
| 6328 | |
| 6329 | t_CKDUR buflen = info.frames; |
| 6330 | if( buflen > LiSa_MAXBUFSIZE ) |
| 6331 | { |
| 6332 | CK_FPRINTF_STDERR( "LiSa: buffer size request too large, resizing to %i...\n", LiSa_MAXBUFSIZE ); |
| 6333 | buflen = LiSa_MAXBUFSIZE; |
| 6334 | } |
| 6335 | if( !d->buffer_alloc(buflen) ) return; |
| 6336 | |
| 6337 | // copy file to buffer, if file is multichannel, store a mono mixdown. |
| 6338 | float multi_data[2048]; |
| 6339 | int frames_read; |
| 6340 | sf_count_t dataout = 0; |
| 6341 | |
| 6342 | while(dataout < info.frames) |
| 6343 | { |
| 6344 | int this_read; |
| 6345 | if (2048 / info.channels < info.frames - dataout) |
| 6346 | this_read = 2048 / info.channels; |
| 6347 | else |
| 6348 | this_read = info.frames - dataout; |
| 6349 | |
| 6350 | frames_read = sf_readf_float(fd, multi_data, this_read); |
| 6351 | if (frames_read == 0) |
| 6352 | break; |
| 6353 | |
| 6354 | // mixdown each channel into mono |
| 6355 | for (int i = 0; i < frames_read; i++) { |
| 6356 | float mix = 0.0; |
| 6357 | |
| 6358 | for (int ch = 0 ; ch < info.channels ; ch++) |
| 6359 | mix += multi_data [i * info.channels + ch]; |
| 6360 | d->mdata[dataout + i] = mix / info.channels; |
| 6361 | } |
| 6362 | |
| 6363 | dataout += frames_read ; |
| 6364 | } |
| 6365 | } |
| 6366 | } |
| 6367 | |
| 6368 | |
| 6369 | |
| 6370 | |
| 6371 | //----------------------------------------------------------------------------- |
| 6372 | // name: LiSaMulti_pmsg() |
| 6373 | // desc: PMSG function ... |
| 6374 | //----------------------------------------------------------------------------- |
| 6375 | CK_DLL_PMSG(LiSaMulti_pmsg ) |
| 6376 | { |
| 6377 | return FALSE; |
| 6378 | } |
| 6379 | |
| 6380 | |
| 6381 | |
| 6382 | |
| 6383 | //----------------------------------------------------------------------------- |
| 6384 | // name: Identity2_tickf() |
| 6385 | // desc: TICKF function ... |
| 6386 | //----------------------------------------------------------------------------- |
| 6387 | CK_DLL_TICKF( Identity2_tickf ) |
| 6388 | { |
| 6389 | // get "this" |
| 6390 | Chuck_UGen * ugen = (Chuck_UGen *)SELF; |
| 6391 | |
| 6392 | t_CKUINT nchans = ugen->m_num_outs; |
| 6393 | |
| 6394 | // print input to tickf |
| 6395 | for( t_CKUINT f = 0; f < nframes; f++ ) |
| 6396 | { |
| 6397 | // look over chans |
| 6398 | for( t_CKUINT c = 0; c < nchans; c++ ) |
| 6399 | { |
| 6400 | // copy input to output |
| 6401 | out[f*nchans+c] = in[f*nchans+c]; |
| 6402 | } |
| 6403 | |
| 6404 | // printing (can be useful for observing multichan ugen logic) |
| 6405 | // cerr << "inputs: "; |
| 6406 | // for( t_CKUINT c = 0; c < nchans; c++ ) |
| 6407 | // { cerr << in[f*nchans+c] << " "; } |
| 6408 | // cerr << "output: "; |
| 6409 | // for( t_CKUINT c = 0; c < nchans; c++ ) |
| 6410 | // { cerr << out[f*nchans+c] << " "; } |
| 6411 | // cerr << endl; |
| 6412 | } |
| 6413 | |
| 6414 | return TRUE; |
| 6415 | } |