Skip to content

Commit

Permalink
ctors for Osc subclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
gewang committed Nov 17, 2023
1 parent 02dcfa6 commit d48c9a9
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 11 deletions.
139 changes: 129 additions & 10 deletions src/core/ugen_osc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,19 @@ DLL_QUERY osc_query( Chuck_DL_Query * QUERY )
doc.c_str() ) )
return FALSE;

// overload constructor (float freq)
func = make_new_mfun( "void", "Phasor", oscx_ctor_1 );
func->add_arg( "float", "freq" );
func->doc = "Constructor to initialize a Phasor at specified frequency.";
if( !type_engine_import_mfun( env, func ) ) goto error;

// overload constructor (float freq, float phase)
func = make_new_mfun( "void", "Phasor", 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.";
if( !type_engine_import_mfun( env, func ) ) goto error;

// add examples | 1.5.0.0 added
if( !type_engine_import_add_ex( env, "basic/phasor.ck" ) ) goto error;

Expand All @@ -151,6 +164,19 @@ DLL_QUERY osc_query( Chuck_DL_Query * QUERY )
doc.c_str() ) )
return FALSE;

// overload constructor (float freq)
func = make_new_mfun( "void", "SinOsc", oscx_ctor_1 );
func->add_arg( "float", "freq" );
func->doc = "Constructor to initialize a SinOsc at specified frequency.";
if( !type_engine_import_mfun( env, func ) ) goto error;

// overload constructor (float freq, float phase)
func = make_new_mfun( "void", "SinOsc", 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.";
if( !type_engine_import_mfun( env, func ) ) goto error;

// add examples | 1.5.0.0 (ge)
type_engine_import_add_ex( env, "otf_05.ck" );
type_engine_import_add_ex( env, "otf_06.ck" );
Expand All @@ -174,6 +200,19 @@ DLL_QUERY osc_query( Chuck_DL_Query * QUERY )
doc.c_str() ) )
return FALSE;

// overload constructor (float freq)
func = make_new_mfun( "void", "TriOsc", oscx_ctor_1 );
func->add_arg( "float", "freq" );
func->doc = "Constructor to initialize a TriOsc at specified frequency.";
if( !type_engine_import_mfun( env, func ) ) goto error;

// overload constructor (float freq, float phase)
func = make_new_mfun( "void", "TriOsc", 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.";
if( !type_engine_import_mfun( env, func ) ) goto error;

func = make_new_mfun( "float", "width", osc_ctrl_width );
func->add_arg( "float", "width" );
func->doc = "set width of triangle wave (ratio of rise time to fall time).";
Expand All @@ -200,6 +239,19 @@ DLL_QUERY osc_query( Chuck_DL_Query * QUERY )
doc.c_str() ) )
return FALSE;

// overload constructor (float freq)
func = make_new_mfun( "void", "SawOsc", oscx_ctor_1 );
func->add_arg( "float", "freq" );
func->doc = "Constructor to initialize a SawOsc at specified frequency.";
if( !type_engine_import_mfun( env, func ) ) goto error;

// overload constructor (float freq, float phase)
func = make_new_mfun( "void", "SawOsc", 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.";
if( !type_engine_import_mfun( env, func ) ) goto error;

func = make_new_mfun( "float", "width", sawosc_ctrl_width );
func->add_arg( "float", "width" );
func->doc = "whether sawtooth wave is to fall (0) or rise (1).";
Expand All @@ -226,6 +278,19 @@ DLL_QUERY osc_query( Chuck_DL_Query * QUERY )
doc.c_str() ) )
return FALSE;

// overload constructor (float freq)
func = make_new_mfun( "void", "PulseOsc", oscx_ctor_1 );
func->add_arg( "float", "freq" );
func->doc = "Constructor to initialize a PulseOsc at specified frequency.";
if( !type_engine_import_mfun( env, func ) ) goto error;

// overload constructor (float freq, float phase)
func = make_new_mfun( "void", "PulseOsc", 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.";
if( !type_engine_import_mfun( env, func ) ) goto error;

func = make_new_mfun( "float", "width", osc_ctrl_width );
func->add_arg( "float", "width" );
func->doc = "set width of duty cycle [0,1).";
Expand All @@ -251,6 +316,19 @@ DLL_QUERY osc_query( Chuck_DL_Query * QUERY )
doc.c_str() ) )
return FALSE;

// overload constructor (float freq)
func = make_new_mfun( "void", "SqrOsc", oscx_ctor_1 );
func->add_arg( "float", "freq" );
func->doc = "Constructor to initialize a SqrOsc at specified frequency.";
if( !type_engine_import_mfun( env, func ) ) goto error;

// overload constructor (float freq, float phase)
func = make_new_mfun( "void", "SqrOsc", 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.";
if( !type_engine_import_mfun( env, func ) ) goto error;

func = make_new_mfun( "float", "width", sqrosc_ctrl_width );
func->doc = "set width of duty cycle (always 0.5).";
if( !type_engine_import_mfun( env, func ) ) goto error;
Expand Down Expand Up @@ -311,7 +389,7 @@ struct Osc_Data

//-----------------------------------------------------------------------------
// name: osc_ctor()
// desc: ...
// desc: default cosntructor
//-----------------------------------------------------------------------------
CK_DLL_CTOR( osc_ctor )
{
Expand All @@ -324,10 +402,51 @@ CK_DLL_CTOR( osc_ctor )



//-----------------------------------------------------------------------------
// generic overloaded ctor( float freq )
// to be used by subclasses of Osc
//-----------------------------------------------------------------------------
CK_DLL_MFUN( oscx_ctor_1 )
{
// get the data
Osc_Data * d = (Osc_Data *)OBJ_MEMBER_UINT(SELF, osc_offset_data );
// set freq
d->freq = GET_CK_FLOAT(ARGS);
// phase increment
d->num = d->freq / d->srate;
// bound it
if( d->num >= 1.0 ) d->num -= ::floor( d->num );
}




//-----------------------------------------------------------------------------
// generic overloaded ctor( float freq, float phase )
// to be used by subclasses of Osc
//-----------------------------------------------------------------------------
CK_DLL_MFUN( oscx_ctor_2 )
{
// get the data
Osc_Data * d = (Osc_Data *)OBJ_MEMBER_UINT(SELF, osc_offset_data );
// set freq
d->freq = GET_CK_FLOAT(ARGS);
// phase increment
d->num = d->freq / d->srate;
// bound it
if( d->num >= 1.0 ) d->num -= ::floor( d->num );

// set phase
d->phase = GET_CK_FLOAT(ARGS);
// bound ( this could be set arbitrarily high or low )
if( d->phase >= 1.0 || d->phase < 0.0 ) d->phase -= floor( d->num );}




//-----------------------------------------------------------------------------
// name: osc_dtor()
// desc: ...
// desc: destructor
//-----------------------------------------------------------------------------
CK_DLL_DTOR( osc_dtor )
{
Expand Down Expand Up @@ -522,7 +641,7 @@ CK_DLL_TICK( triosc_tick )
// sync phase to input
else if( d->sync == 1 )
{
// set freq
// set phase
d->phase = in;
inc_phase = FALSE;
}
Expand Down Expand Up @@ -594,7 +713,7 @@ CK_DLL_TICK( pulseosc_tick )
// sync phase to input
else if( d->sync == 1 )
{
// set freq
// set phase
d->phase = in;
inc_phase = FALSE;
}
Expand Down Expand Up @@ -723,10 +842,10 @@ CK_DLL_CTRL( osc_ctrl_phase )
{
// get data
Osc_Data * d = (Osc_Data *)OBJ_MEMBER_UINT(SELF, osc_offset_data );
// set freq
// set phase
d->phase = GET_CK_FLOAT(ARGS);
//bound ( this could be set arbitrarily high or low )
if ( d->phase >= 1.0 || d->phase < 0.0 ) d->phase -= floor( d->num );
// bound ( this could be set arbitrarily high or low )
if( d->phase >= 1.0 || d->phase < 0.0 ) d->phase -= floor( d->num );
// return
RETURN->v_float = (t_CKFLOAT)d->phase;
}
Expand Down Expand Up @@ -757,7 +876,7 @@ CK_DLL_CTRL( osc_ctrl_width )
{
// get data
Osc_Data * d = (Osc_Data *)OBJ_MEMBER_UINT(SELF, osc_offset_data );
// set freq
// set width
d->width = GET_CK_FLOAT(ARGS);
//bound ( this could be set arbitrarily high or low )
d->width = ck_max( 0.0, ck_min( 1.0, d->width ) );
Expand Down Expand Up @@ -836,7 +955,7 @@ CK_DLL_CTRL( sawosc_ctrl_width )
{
// get data
Osc_Data * d = (Osc_Data *)OBJ_MEMBER_UINT(SELF, osc_offset_data );
// set freq
// set width
d->width = GET_CK_FLOAT(ARGS);
// bound ( this could be set arbitrarily high or low )
d->width = ( d->width < 0.5 ) ? 0.0 : 1.0; //rising or falling
Expand Down Expand Up @@ -1311,7 +1430,7 @@ CK_DLL_CTRL( genX_lookup )
in_index = GET_NEXT_FLOAT(ARGS);

// gewang: moved to here
if (in_index < 0.) in_index = -in_index;
if( in_index < 0 ) in_index = -in_index;
scaled_index = in_index * (genX_tableSize - 1); //drive with phasor [0, 1]

//set up interpolation parameters
Expand Down
5 changes: 5 additions & 0 deletions src/core/ugen_osc.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ CK_DLL_CGET( osc_cget_width );
CK_DLL_CTRL( osc_ctrl_sync );
CK_DLL_CGET( osc_cget_sync );

// generic overloaded ctor( float freq )
CK_DLL_MFUN( oscx_ctor_1 );
// generic overloaded ctor( float freq, float phase )
CK_DLL_MFUN( oscx_ctor_2 );

// sinosc
CK_DLL_TICK( sinosc_tick );

Expand Down
2 changes: 1 addition & 1 deletion src/test/01-Basic/241-ctor-builtin.ck
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// constructor
SinOsc foo => Gain g(.5) => dac;
SinOsc foo(440) => Gain g(.5) => dac;

// test
if( Math.equal(g.gain(),.5) )
Expand Down

0 comments on commit d48c9a9

Please sign in to comment.