From b8e8e81a3142937bb64184a4f67ea6e023660911 Mon Sep 17 00:00:00 2001 From: Ge Wang Date: Sun, 26 Nov 2023 15:43:56 -0800 Subject: [PATCH] add constructors for sndbuf and string --- VERSIONS | 17 +++++++++ examples/basic/sndbuf.ck | 11 ++++-- src/core/ugen_osc.cpp | 28 +++++++------- src/core/ugen_xxx.cpp | 81 +++++++++++++++++++++++++++++++++++----- src/core/ugen_xxx.h | 5 ++- 5 files changed, 113 insertions(+), 29 deletions(-) diff --git a/VERSIONS b/VERSIONS index 9b366725a..39283ecf6 100644 --- a/VERSIONS +++ b/VERSIONS @@ -57,6 +57,23 @@ new Foo(10,11,12) @=> Foo @ f4; - (added) Shred.parent() and Shred.ancestor() - (fixed) Type.of("int[][]").isArray() now correctly returns true - (updated) ChucK API doucmentation for constructors +- (added) overloaded constructors added (more to come) +================== + Gain( float gain ); + Construct a Gain with default value. + SndBuf( string path ); + Construct a SndBuf with the 'path' to a sound file to read. + SndBuf( string path, float rate ); + Construct a SndBuf with the 'path' to a sound file to read, and a default playback 'rate' (1.0 is normal rate) + SndBuf( string path, float rate, int pos ); + 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' + string( string str ); + Construct a string as a copy of another string. + Osc( float freq ); + Construct a Osc at specified frequency. + Osc( float freq, float phase ); + Construct an Osc at specified frequency and phase. + (similarly for SinOsc, TriOsc, SqrOsc, SawOsc, PulseOsc, Phasor) ================== chugins API update ================== diff --git a/examples/basic/sndbuf.ck b/examples/basic/sndbuf.ck index dcacd2b21..f2d9cd50a 100644 --- a/examples/basic/sndbuf.ck +++ b/examples/basic/sndbuf.ck @@ -3,10 +3,13 @@ me.dir() + "../data/snare.wav" => string filename; // if there is argument, use it as the filename if( me.args() ) me.arg(0) => filename; -// the patch -SndBuf buf => dac; -// load the file -filename => buf.read; +// the patch +SndBuf buf(filename) => dac; +// can also load the file this way +// filename => buf.read; + +// check if file successfully loaded +if( !buf.ready() ) me.exit(); // time loop while( true ) diff --git a/src/core/ugen_osc.cpp b/src/core/ugen_osc.cpp index d668cd860..7c2fee787 100644 --- a/src/core/ugen_osc.cpp +++ b/src/core/ugen_osc.cpp @@ -84,14 +84,14 @@ DLL_QUERY osc_query( Chuck_DL_Query * QUERY ) // overload constructor (float freq) func = make_new_ctor( oscx_ctor_1 ); func->add_arg( "float", "freq" ); - func->doc = "Constructor to initialize a Osc at specified frequency."; + func->doc = "construct Osc at specified frequency."; if( !type_engine_import_ctor( env, func ) ) goto error; // overload constructor (float freq, float phase) func = make_new_ctor( oscx_ctor_2 ); func->add_arg( "float", "freq" ); func->add_arg( "float", "phase" ); - func->doc = "Constructor to initialize a Osc at specified frequency and phase."; + func->doc = "construct Osc at specified frequency and phase."; if( !type_engine_import_ctor( env, func ) ) goto error; // add ctrl: freq @@ -152,14 +152,14 @@ DLL_QUERY osc_query( Chuck_DL_Query * QUERY ) // overload constructor (float freq) func = make_new_ctor( oscx_ctor_1 ); func->add_arg( "float", "freq" ); - func->doc = "Constructor to initialize a Phasor at specified frequency."; + func->doc = "construct Phasor at specified frequency."; if( !type_engine_import_ctor( env, func ) ) goto error; // overload constructor (float freq, float phase) func = make_new_ctor( oscx_ctor_2 ); func->add_arg( "float", "freq" ); func->add_arg( "float", "phase" ); - func->doc = "Constructor to initialize a Phasor at specified frequency and phase."; + func->doc = "construct Phasor at specified frequency and phase."; if( !type_engine_import_ctor( env, func ) ) goto error; // add examples | 1.5.0.0 added @@ -180,14 +180,14 @@ DLL_QUERY osc_query( Chuck_DL_Query * QUERY ) // overload constructor (float freq) func = make_new_ctor( oscx_ctor_1 ); func->add_arg( "float", "freq" ); - func->doc = "Constructor to initialize a SinOsc at specified frequency."; + func->doc = "construct SinOsc at specified frequency."; if( !type_engine_import_ctor( env, func ) ) goto error; // overload constructor (float freq, float phase) func = make_new_ctor( oscx_ctor_2 ); func->add_arg( "float", "freq" ); func->add_arg( "float", "phase" ); - func->doc = "Constructor to initialize a SinOsc at specified frequency and phase."; + func->doc = "construct SinOsc at specified frequency and phase."; if( !type_engine_import_ctor( env, func ) ) goto error; // add examples | 1.5.0.0 (ge) @@ -216,14 +216,14 @@ DLL_QUERY osc_query( Chuck_DL_Query * QUERY ) // overload constructor (float freq) func = make_new_ctor( oscx_ctor_1 ); func->add_arg( "float", "freq" ); - func->doc = "Constructor to initialize a TriOsc at specified frequency."; + func->doc = "construct TriOsc at specified frequency."; if( !type_engine_import_ctor( env, func ) ) goto error; // overload constructor (float freq, float phase) func = make_new_ctor( oscx_ctor_2 ); func->add_arg( "float", "freq" ); func->add_arg( "float", "phase" ); - func->doc = "Constructor to initialize a TriOsc at specified frequency and phase."; + func->doc = "construct TriOsc at specified frequency and phase."; if( !type_engine_import_ctor( env, func ) ) goto error; func = make_new_mfun( "float", "width", osc_ctrl_width ); @@ -255,14 +255,14 @@ DLL_QUERY osc_query( Chuck_DL_Query * QUERY ) // overload constructor (float freq) func = make_new_ctor( oscx_ctor_1 ); func->add_arg( "float", "freq" ); - func->doc = "Constructor to initialize a SawOsc at specified frequency."; + func->doc = "construct SawOsc at specified frequency."; if( !type_engine_import_ctor( env, func ) ) goto error; // overload constructor (float freq, float phase) func = make_new_ctor( oscx_ctor_2 ); func->add_arg( "float", "freq" ); func->add_arg( "float", "phase" ); - func->doc = "Constructor to initialize a SawOsc at specified frequency and phase."; + func->doc = "construct SawOsc at specified frequency and phase."; if( !type_engine_import_ctor( env, func ) ) goto error; func = make_new_mfun( "float", "width", sawosc_ctrl_width ); @@ -294,14 +294,14 @@ DLL_QUERY osc_query( Chuck_DL_Query * QUERY ) // overload constructor (float freq) func = make_new_ctor( oscx_ctor_1 ); func->add_arg( "float", "freq" ); - func->doc = "Constructor to initialize a PulseOsc at specified frequency."; + func->doc = "construct PulseOsc at specified frequency."; if( !type_engine_import_ctor( env, func ) ) goto error; // overload constructor (float freq, float phase) func = make_new_ctor( oscx_ctor_2 ); func->add_arg( "float", "freq" ); func->add_arg( "float", "phase" ); - func->doc = "Constructor to initialize a PulseOsc at specified frequency and phase."; + func->doc = "construct PulseOsc at specified frequency and phase."; if( !type_engine_import_ctor( env, func ) ) goto error; func = make_new_mfun( "float", "width", osc_ctrl_width ); @@ -332,14 +332,14 @@ DLL_QUERY osc_query( Chuck_DL_Query * QUERY ) // overload constructor (float freq) func = make_new_ctor( oscx_ctor_1 ); func->add_arg( "float", "freq" ); - func->doc = "Constructor to initialize a SqrOsc at specified frequency."; + func->doc = "construct SqrOsc at specified frequency."; if( !type_engine_import_ctor( env, func ) ) goto error; // overload constructor (float freq, float phase) func = make_new_ctor( oscx_ctor_2 ); func->add_arg( "float", "freq" ); func->add_arg( "float", "phase" ); - func->doc = "Constructor to initialize a SqrOsc at specified frequency and phase."; + func->doc = "construct SqrOsc at specified frequency and phase."; if( !type_engine_import_ctor( env, func ) ) goto error; func = make_new_mfun( "float", "width", sqrosc_ctrl_width ); diff --git a/src/core/ugen_xxx.cpp b/src/core/ugen_xxx.cpp index 4f864e6d4..f4a2c19a4 100644 --- a/src/core/ugen_xxx.cpp +++ b/src/core/ugen_xxx.cpp @@ -41,6 +41,7 @@ #include "chuck_compile.h" #include "chuck_instr.h" #include "util_math.h" +#include "util_raw.h" #include #include @@ -364,7 +365,7 @@ DLL_QUERY xxx_query( Chuck_DL_Query * QUERY ) // add ctrl: fprob func = make_new_mfun( "void", "Gain", gain_ctor_1 ); - func->doc = "Create a Gain with default value."; + func->doc = "construct a Gain with default value."; func->add_arg( "float", "gain" ); if( !type_engine_import_mfun( env, func ) ) goto error; @@ -717,14 +718,37 @@ DLL_QUERY xxx_query( Chuck_DL_Query * QUERY ) sndbuf_offset_data = type_engine_import_mvar( env, "int", "@sndbuf_data", FALSE ); if( sndbuf_offset_data == CK_INVALID_OFFSET ) goto error; + // add ctor( string path ) + func = make_new_ctor( sndbuf_ctor_path ); + func->add_arg( "string", "path" ); + func->doc = "construct a SndBuf with the 'path' to a sound file to read."; + if( !type_engine_import_ctor( env, func ) ) goto error; + + // add ctor( string path, float rate ) + func = make_new_ctor( sndbuf_ctor_path_rate ); + func->add_arg( "string", "path" ); + func->add_arg( "float", "rate" ); + func->doc = "construct a SndBuf with the 'path' to a sound file to read, and a default playback 'rate' (1.0 is normal rate)"; + if( !type_engine_import_ctor( env, func ) ) goto error; + + // add ctor( string path ) + func = make_new_ctor( sndbuf_ctor_path_rate_pos ); + func->add_arg( "string", "path" ); + func->add_arg( "float", "rate" ); + func->add_arg( "int", "pos" ); + 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'"; + if( !type_engine_import_ctor( env, func ) ) goto error; + // add ctrl: read func = make_new_mfun( "string", "read", sndbuf_ctrl_read ); func->add_arg( "string", "read" ); func->doc = "read file for reading."; if( !type_engine_import_mfun( env, func ) ) goto error; - // add cget: read // area - //func = make_new_mfun( "string", "read", sndbuf_cget_read ); - //if( !type_engine_import_mfun( env, func ) ) goto error; + + // add cget: ready + func = make_new_mfun( "int", "ready", sndbuf_cget_ready ); + func->doc = "query whether the SndBuf is ready for use (e.g., sound file successfully loaded)."; + if( !type_engine_import_mfun( env, func ) ) goto error; // add ctrl: write func = make_new_mfun( "string", "write", sndbuf_ctrl_write ); @@ -2926,7 +2950,6 @@ void sndbuf_sinc_interpolate( sndbuf_data * d, SAMPLE * out ); CK_DLL_CTOR( sndbuf_ctor ) { OBJ_MEMBER_UINT(SELF, sndbuf_offset_data) = (t_CKUINT)new sndbuf_data; - } CK_DLL_DTOR( sndbuf_dtor ) @@ -2936,6 +2959,41 @@ CK_DLL_DTOR( sndbuf_dtor ) OBJ_MEMBER_UINT(SELF, sndbuf_offset_data) = 0; } +CK_DLL_CTOR( sndbuf_ctor_path ) +{ + Chuck_DL_Return RETURN; + // read it + sndbuf_ctrl_read( SELF, ARGS, &RETURN, VM, SHRED, API ); +} + +CK_DLL_CTOR( sndbuf_ctor_path_rate ) +{ + Chuck_DL_Return RETURN; + // read it + sndbuf_ctrl_read( SELF, ARGS, &RETURN, VM, SHRED, API ); + + // advance args, skip over path + GET_NEXT_STRING(ARGS); + // control rate + sndbuf_ctrl_rate( SELF, ARGS, &RETURN, VM, SHRED, API ); +} + +CK_DLL_CTOR( sndbuf_ctor_path_rate_pos ) +{ + Chuck_DL_Return RETURN; + // read it + sndbuf_ctrl_read( SELF, ARGS, &RETURN, VM, SHRED, API ); + + // advance args, skip over path + GET_NEXT_STRING(ARGS); + // control rate + sndbuf_ctrl_rate( SELF, ARGS, &RETURN, VM, SHRED, API ); + + // advance args, skip over rate + GET_NEXT_FLOAT(ARGS); + sndbuf_ctrl_pos( SELF, ARGS, &RETURN, VM, SHRED, API ); +} + inline t_CKUINT sndbuf_read( sndbuf_data * d, t_CKUINT frame, t_CKUINT num_frames ) { // check @@ -3361,10 +3419,6 @@ CK_DLL_TICKF( sndbuf_tickf ) return TRUE; } - -#include "util_raw.h" - - CK_DLL_CTRL( sndbuf_ctrl_read ) { sndbuf_data * d = (sndbuf_data *)OBJ_MEMBER_UINT(SELF, sndbuf_offset_data); @@ -3667,10 +3721,17 @@ CK_DLL_CTRL( sndbuf_ctrl_write ) #endif } +CK_DLL_CGET( sndbuf_cget_ready ) +{ + // get the internal representation + sndbuf_data * d = ( sndbuf_data * )OBJ_MEMBER_UINT(SELF, sndbuf_offset_data); + // ready if we have a file descriptor + RETURN->v_int = d->fd != NULL; +} CK_DLL_CTRL( sndbuf_ctrl_rate ) { - sndbuf_data * d = ( sndbuf_data * ) OBJ_MEMBER_UINT(SELF, sndbuf_offset_data); + sndbuf_data * d = ( sndbuf_data * )OBJ_MEMBER_UINT(SELF, sndbuf_offset_data); t_CKFLOAT rate = GET_CK_FLOAT(ARGS); // rate d->rate = rate * d->sampleratio; d->rate_factor = rate; diff --git a/src/core/ugen_xxx.h b/src/core/ugen_xxx.h index afa93b5d0..4ce9621de 100644 --- a/src/core/ugen_xxx.h +++ b/src/core/ugen_xxx.h @@ -157,11 +157,14 @@ CK_DLL_CGET( delayp_cget_max ); // sndbuf CK_DLL_CTOR( sndbuf_ctor ); +CK_DLL_CTOR( sndbuf_ctor_path ); +CK_DLL_CTOR( sndbuf_ctor_path_rate ); +CK_DLL_CTOR( sndbuf_ctor_path_rate_pos ); CK_DLL_DTOR( sndbuf_dtor ); CK_DLL_TICK( sndbuf_tick ); CK_DLL_TICKF( sndbuf_tickf ); CK_DLL_CTRL( sndbuf_ctrl_read ); -CK_DLL_CGET( sndbuf_cget_read ); +CK_DLL_CGET( sndbuf_cget_ready ); CK_DLL_CTRL( sndbuf_ctrl_write ); CK_DLL_CGET( sndbuf_cget_write ); CK_DLL_CTRL( sndbuf_ctrl_pos );