Skip to content

Commit

Permalink
add ADSR and Envelope constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
gewang committed Jun 16, 2024
1 parent 533030b commit 477f91d
Show file tree
Hide file tree
Showing 3 changed files with 238 additions and 3 deletions.
25 changes: 24 additions & 1 deletion VERSIONS
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,30 @@ ChucK VERSIONS log
=======
- (updated) chugin API version from 10.1 to 10.2
- (added) chugin API for callback on shutdown
- (added) chugin API for setting array elements (thanks @AndrewAday)
- (added) overloaded constructors added (more to come)
==================
Envelope( dur durationToTarget )
Construct an Envelope with duration to reach target (assumed to be 1.0)
Envelope( float secondsToTarget )
Construct an Envelope with duration (in seconds) to reach target (assumed to be 1.0)
Envelope( dur durationToTarget, float target )
Construct an Envelope with duration to reach target.
Envelope( float durationToTarget, float target )
Construct an Envelope with duration (in seconds) to reach target.

ADSR(dur attack, dur decay, float sustain, dur release)
Construct an ADSR with attack, decay, sustain, and release values. Attack,
decay, and release values are durations; sustain is a float value typically
between 0 and 1.
ADSR(float attack, float decay, float sustain, float release)
Construct an ADSR with attack, decay, sustain, and release values. Attack,
decay, and release values are in seconds; sustain is a float value
typically between 0 and 1.
==================
- (added) void Envelope.set( dur durationToTarget, float target );
void Envelope.set( float secondsToTarget, float target );
examples/basic/envelope2.ck
- (added) command line query system `--query:<name>`
- (added) examples/deep/smb.ck -- a ChucK rendition of Super Mario Bros.
original theme by Koji Kondo; (meticulously) modeled in ChucK by
Expand All @@ -17,7 +41,6 @@ ChucK VERSIONS log
scream generator
- (added) Type type added to CKDoc script and deployed online:
https://chuck.stanford.edu/doc/reference/utils.html#Type
- (added) chugin DL API for setting array elements (thanks @AndrewAday)
- (fixed) globals events system synchronization (FYI this fixes a long-running,
elusive Chunity crashing bug involving various syncers)

Expand Down
42 changes: 42 additions & 0 deletions examples/basic/envelope2.ck
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// name: envelope2.ck
// desc: real-time segmented envelope generation
// requires: 1.5.2.5 or higher

// no sound in this example; only printing
Step s(1) => Envelope e => blackhole;

// spork printing shred
spork ~ print();

// infinite time-loop
while( true )
{
// next duration to target
Math.random2f(500,1000)::ms => dur duration;
// next target
Math.random2f(.1, 10) => float target;
// print
<<< "duration:", duration/second, "target:", target >>>;

// set duration and target
e.set( duration, target );
// key on (without key off): ramp to new target
e.keyOn();
// advance by duration-to-target
duration => now;
}

// print function
fun void print()
{
// infinite time loop
while( true )
{
// print value of e
// NOTE: this is only a rough print-out and
// will not necessary print the target values
// but values approaching towards the targets
<<< e.last(), "" >>>;
50::ms => now;
}
}
174 changes: 172 additions & 2 deletions src/core/ugen_stk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ CK_DLL_CTOR( ADSR_ctor );
CK_DLL_DTOR( ADSR_dtor );
CK_DLL_TICK( ADSR_tick );
CK_DLL_PMSG( ADSR_pmsg );
CK_DLL_CTOR( ADSR_ctor_floats );
CK_DLL_CTOR( ADSR_ctor_durs );
CK_DLL_CTRL( ADSR_ctrl_attackTime );
CK_DLL_CTRL( ADSR_ctrl_attackRate );
CK_DLL_CTRL( ADSR_ctrl_decayTime );
Expand Down Expand Up @@ -256,6 +258,12 @@ CK_DLL_CTOR( Envelope_ctor );
CK_DLL_DTOR( Envelope_dtor );
CK_DLL_TICK( Envelope_tick );
CK_DLL_PMSG( Envelope_pmsg );
CK_DLL_CTOR( Envelope_ctor_duration );
CK_DLL_CTOR( Envelope_ctor_float );
CK_DLL_CTOR( Envelope_ctor_duration_target );
CK_DLL_CTOR( Envelope_ctor_float_target );
CK_DLL_MFUN( Envelope_mfun_duration_target );
CK_DLL_MFUN( Envelope_mfun_float_target );
CK_DLL_CTRL( Envelope_ctrl_rate );
CK_DLL_CTRL( Envelope_ctrl_target );
CK_DLL_CTRL( Envelope_cget_target );
Expand Down Expand Up @@ -3707,13 +3715,54 @@ by Perry R. Cook and Gary P. Scavone, 1995 - 2002.";
Envelope_tick, Envelope_pmsg, doc.c_str() ) ) return FALSE;

type_engine_import_add_ex(env, "basic/envelope.ck");
type_engine_import_add_ex(env, "basic/envelope2.ck");
type_engine_import_add_ex(env, "basic/chirp2.ck");
type_engine_import_add_ex(env, "deep/say-chu.ck");

//member variable
// member variable
Envelope_offset_data = type_engine_import_mvar ( env, "int", "@Envelope_data", FALSE );
if( Envelope_offset_data == CK_INVALID_OFFSET ) goto error;

// add constructor Envelope( dur durationToTarget ) | 1.5.2.5 (added) ge & eito
func = make_new_ctor( Envelope_ctor_duration );
func->add_arg( "dur", "durationToTarget" );
func->doc = "construct an Envelope with duration to reach target (assumed to be 1.0)";
if( !type_engine_import_ctor( env, func ) ) goto error;

// add constructor Envelope( float secondsToTarget ) | 1.5.2.5 (added) ge & eito
func = make_new_ctor( Envelope_ctor_float );
func->add_arg( "float", "secondsToTarget" );
func->doc = "construct an Envelope with duration (in seconds) to reach target (assumed to be 1.0)";
if( !type_engine_import_ctor( env, func ) ) goto error;

// add constructor Envelope( dur durationToTarget, float target ) | 1.5.2.5 (added) ge & eito
func = make_new_ctor( Envelope_ctor_duration_target );
func->add_arg( "dur", "durationToTarget" );
func->add_arg( "float", "target" );
func->doc = "construct an Envelope with duration to reach target.";
if( !type_engine_import_ctor( env, func ) ) goto error;

// add constructor Envelope( float durationToTarget, float target ) | 1.5.2.5 (added) ge & eito
func = make_new_ctor( Envelope_ctor_float_target );
func->add_arg( "float", "durationToTarget" );
func->add_arg( "float", "target" );
func->doc = "construct an Envelope with duration (in seconds) to reach target.";
if( !type_engine_import_ctor( env, func ) ) goto error;

// add set( dur durationToTarget, float target ) | 1.5.2.5 (added) ge
func = make_new_mfun( "void", "set", Envelope_mfun_duration_target );
func->add_arg( "dur", "durationToTarget" );
func->add_arg( "float", "target" );
func->doc = "set duration to reach the next target.";
if( !type_engine_import_mfun( env, func ) ) goto error;

// add set( float durationToTarget, float target ) | 1.5.2.5 (added) ge
func = make_new_mfun( "void", "set", Envelope_mfun_duration_target );
func->add_arg( "float", "durationToTarget" );
func->add_arg( "float", "target" );
func->doc = "set duration to reach the next target.";
if( !type_engine_import_mfun( env, func ) ) goto error;

func = make_new_mfun( "int", "keyOn", Envelope_ctrl_keyOn0 ); //! ramp to 1.0
func->doc = "get keyOn state.";
if( !type_engine_import_mfun( env, func ) ) goto error;
Expand Down Expand Up @@ -3798,6 +3847,24 @@ by Perry R. Cook and Gary P. Scavone, 1995 - 2002.";
type_engine_import_add_ex(env, "basic/adsr.ck");
type_engine_import_add_ex(env, "basic/blit2.ck");

// add construtor ADSR( dur attack, dur decay, float sustain, dur release ) | 1.5.2.5 (added) ge & eito
func = make_new_ctor( ADSR_ctor_floats );
func->add_arg( "dur", "attack" );
func->add_arg( "dur", "decay" );
func->add_arg( "float", "sustain" );
func->add_arg( "dur", "release" );
func->doc = "construct an ADSR with attack, decay, sustain, and release values. Attack, decay, and release values are durations; sustain is a float value typically between 0 and 1.";
if( !type_engine_import_ctor( env, func ) ) goto error;

// add constructor ADSR( float attack, float decay, float sustain, float release ) | 1.5.2.5 (added) ge & eito
func = make_new_ctor( ADSR_ctor_durs );
func->add_arg( "float", "attack" );
func->add_arg( "float", "decay" );
func->add_arg( "float", "sustain" );
func->add_arg( "float", "release" );
func->doc = "construct an ADSR with attack, decay, sustain, and release values. Attack, decay, and release values are in seconds; sustain is a float value typically between 0 and 1.";
if( !type_engine_import_ctor( env, func ) ) goto error;

func = make_new_mfun( "dur", "attackTime", ADSR_ctrl_attackTime ); //! attack time
func->add_arg( "dur", "value" );
func->doc = "set attack time.";
Expand Down Expand Up @@ -23999,6 +24066,75 @@ CK_DLL_PMSG( Envelope_pmsg )
}


//-----------------------------------------------------------------------------
// name: Envelope_ctor_duration()
// desc: ctor( dur durationToTarget )
//-----------------------------------------------------------------------------
CK_DLL_CTOR( Envelope_ctor_duration )
{
Envelope * d = (Envelope *)OBJ_MEMBER_UINT(SELF, Envelope_offset_data);
d->setTime( GET_NEXT_DUR(ARGS) / Stk::sampleRate() );
}

//-----------------------------------------------------------------------------
// name: Envelope_ctor_float()
// desc: ctor( float secondsToTarget )
//-----------------------------------------------------------------------------
CK_DLL_CTOR( Envelope_ctor_float )
{
Envelope * d = (Envelope *)OBJ_MEMBER_UINT(SELF, Envelope_offset_data);
d->setTime( GET_NEXT_FLOAT(ARGS) );
}


//-----------------------------------------------------------------------------
// name: Envelope_ctor_duration_target()
// desc: ctor( dur durationToTarget, float target )
//-----------------------------------------------------------------------------
CK_DLL_CTOR( Envelope_ctor_duration_target )
{
Envelope * d = (Envelope *)OBJ_MEMBER_UINT(SELF, Envelope_offset_data);
d->setTime( GET_NEXT_DUR(ARGS) / Stk::sampleRate() );
d->setTarget( GET_NEXT_FLOAT(ARGS) );
}


//-----------------------------------------------------------------------------
// name: Envelope_ctor_float_target()
// desc: ctor( float secondsToTarget, float target )
//-----------------------------------------------------------------------------
CK_DLL_CTOR( Envelope_ctor_float_target )
{
Envelope * d = (Envelope *)OBJ_MEMBER_UINT(SELF, Envelope_offset_data);
d->setTime( GET_NEXT_FLOAT(ARGS) );
d->setTarget( GET_NEXT_FLOAT(ARGS) );
}


//-----------------------------------------------------------------------------
// name: Envelope_mfun_duration_target()
// desc: set( dur durationToTarget, float target )
//-----------------------------------------------------------------------------
CK_DLL_MFUN( Envelope_mfun_duration_target )
{
Envelope * d = (Envelope *)OBJ_MEMBER_UINT(SELF, Envelope_offset_data);
d->setTime( GET_NEXT_DUR(ARGS) / Stk::sampleRate() );
d->setTarget( GET_NEXT_FLOAT(ARGS) );
}


//-----------------------------------------------------------------------------
// name: Envelope_mfun_float_target()
// desc: set( float secondsToTarget, float target )
//-----------------------------------------------------------------------------
CK_DLL_MFUN( Envelope_mfun_float_target )
{
Envelope * d = (Envelope *)OBJ_MEMBER_UINT(SELF, Envelope_offset_data);
d->setTime( GET_NEXT_FLOAT(ARGS) );
d->setTarget( GET_NEXT_FLOAT(ARGS) );
}


//-----------------------------------------------------------------------------
// name: Envelope_ctrl_time()
// desc: CTRL function ...
Expand Down Expand Up @@ -24029,7 +24165,7 @@ CK_DLL_CGET( Envelope_cget_time )
CK_DLL_CTRL( Envelope_ctrl_duration )
{
Envelope * d = (Envelope *)OBJ_MEMBER_UINT(SELF, Envelope_offset_data);
d->setTime( GET_NEXT_FLOAT(ARGS) / Stk::sampleRate() );
d->setTime( GET_NEXT_DUR(ARGS) / Stk::sampleRate() );
RETURN->v_float = d->m_time * Stk::sampleRate();
}

Expand Down Expand Up @@ -24229,6 +24365,40 @@ CK_DLL_PMSG( ADSR_pmsg )
}


//-----------------------------------------------------------------------------
// name: ADSR_ctor_floats()
// desc: ctor( float attack, float decay, float sustain, float release )
//-----------------------------------------------------------------------------
CK_DLL_CTOR( ADSR_ctor_floats )
{
ADSR * e = (ADSR *)OBJ_MEMBER_UINT(SELF, Envelope_offset_data);
t_CKFLOAT a = GET_NEXT_FLOAT(ARGS);
t_CKFLOAT d = GET_NEXT_FLOAT(ARGS);
t_CKFLOAT s = GET_NEXT_FLOAT(ARGS);
t_CKFLOAT r = GET_NEXT_FLOAT(ARGS);

// set A D S R
e->setAllTimes( a, d, s, r );
}


//-----------------------------------------------------------------------------
// name: ADSR_ctor_durs()
// desc: ctor( dur attack, dur decay, dur sustain, dur release )
//-----------------------------------------------------------------------------
CK_DLL_CTOR( ADSR_ctor_durs )
{
ADSR * e = (ADSR *)OBJ_MEMBER_UINT(SELF, Envelope_offset_data);
t_CKDUR a = GET_NEXT_DUR(ARGS);
t_CKDUR d = GET_NEXT_DUR(ARGS);
t_CKFLOAT s = GET_NEXT_FLOAT(ARGS);
t_CKDUR r = GET_NEXT_DUR(ARGS);

// set A D S R
e->setAllTimes( a/Stk::sampleRate(), d/Stk::sampleRate(), s, r/Stk::sampleRate() );
}


//-----------------------------------------------------------------------------
// name: ADSR_ctrl_attackTime()
// desc: CTRL function ...
Expand Down

0 comments on commit 477f91d

Please sign in to comment.